-1

我需要你对这个项目的建议,我试图用 MS SQL Server 数据库中的 php 提供一个像这样的“组合”谷歌图表。 图表视图模板

我已经构建了以下视图,它为我提供了相关数据。稍后我将不得不为每个项目创建一系列这些图表(通过“ProjectUniqueId”标识)。

表视图模板

从到目前为止我得到的所有文档中,我了解到我必须以编程方式构建以下数据表

    var data = google.visualization.arrayToDataTable([
      ['Week', 'Hrs VSE', 'Hrs PII', 'Hrs VDG', 'Hrs PIA', 'Hrs TCIS', 'Forecast'  ],
      ['2013-W20',  165,          938,         522,         998,        450,          614.6],
      ['2013-W21',  135,          1120,        599,         1268,       288,          682],
      ['2013-W22',  157,          1167,        587,         807,        397,          1200],
      ['2013-W23',  139,          1110,        615,         968,        215,          2000],
      ['2013-W24',  136,          691,         629,         1026,       366,          569.5]
    ]);

作为此页面的一部分:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load('visualization', '1', {packages: ['corechart']});
</script>
<script type="text/javascript">
  function drawVisualization() {
    //Raw data
    var data = google.visualization.arrayToDataTable([
      ['Week', 'Hrs VSE', 'Hrs PII', 'Hrs VDG', 'Hrs PIA', 'Hrs TCIS', 'Forecast'  ],
      ['2013-W20',  165,          938,         522,         998,        450,          614.6],
      ['2013-W21',  135,          1120,        599,         1268,       288,          682],
      ['2013-W22',  157,          1167,        587,         807,        397,          1200],
      ['2013-W23',  139,          1110,        615,         968,        215,          2000],
      ['2013-W24',  136,          691,         629,         1026,       366,          569.5]
    ]);

    var options = {
      title : 'Actuals vs Forecast VLU Project per Cost-Center',
      vAxis: {title: ""},
      //Horizontal axis text vertical
      hAxis: {title: "", slantedText:true, slantedTextAngle:90},
      seriesType: "bars",
      series: {5: {type: "line"}},
      isStacked: true
    };

    var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }
  google.setOnLoadCallback(drawVisualization);
</script>

我首先尝试手动构建dataTable(我将其理解为数组的“组装”)

1)构建“列标题”数组(CcName)

2)构建“行标题”数组(WeekValue)

3) 查询 (HoursValue) 特定 WeekValue,CcName 的每个单独值 ...

最后,我从未真正设法构建所需的数组,然后找到有关 JSON 的文档以及它如何提供帮助,但没有设法在我的代码中实现它。

<?php

$myServer = "XXXXXX";
$myUser = "reportuser";
$myPass = "";
$myDB = "HOURS"; 

//Connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die("Couldn't connect to SQL Server on $myServer"); 

//Select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
  or die("Couldn't open database $myDB"); 

//Declare the SQL statement that will query the database
$query = "SELECT CcName, WeekValue, SUM(HoursValue) AS HoursValue FROM viewFunctionalHoursKpi WHERE Approval='Actuals' AND ProjectUniqueId=1286 GROUP BY CcName, WeekValue";

//Execute the SQL query and return records
$result = mssql_query($query)
  or die('An error occured: ' . mysql_error());

$resultArray = array();

while ($record = mssql_fetch_array($result))
  {
    //Fill array
    $resultArray[] = $record;
  }                    

//Output in JSON format
echo json_encode($resultArray);

//Free result set memory
mssql_free_result($result);

//Close the connection
mssql_close($dbhandle);

?>

你们有什么建议?我绝对愿意通过更改我在 MS SQL Server 中获得的当前视图来更改数据的格式,但对我来说最困难的是如何将我从 php 获得的数据传输到这个 js dataTable(如果这是方法)。

谢谢你的帮助!

4

1 回答 1

1

我已经输出了相同的图表作为我的谷歌图表数据(重写以用作示例):

<?php

    //this would be the output of your sql statement
    $resultArray = array(array("this"=>5, "is" =>3, "a"=>4, "test"=>1),
            array("this"=>25, "is" =>23, "a"=>42, "test"=>12),
            array("this"=>50, "is" =>30, "a"=>40, "test"=>10));


    //we find all the keys to use as "headers"
    $keys = array_keys($resultArray[0]);

    //loop through each key adding the needed marks
    $tempData = '';
    foreach($keys as $key){
        $tempData .= "'$key',";
    }

    //this removes the last comma (though you might not need to)
    $data ="[".rtrim($tempData,',')."], \n";

    //more looping, marking and comma removal
    //just through your whole list of results
    foreach($resultArray as $r){
        $tempData = '';
        foreach($r as $val){
            $tempData .= "'$val',";
        }
        $data .= "[".rtrim($tempData,',')."], \n";
    }
    $data = "[".rtrim($data,", \n")."]";

    //echo result
    echo $data;

?>

希望对你有用

于 2013-08-28T22:49:53.877 回答