使用来自mssql的php组合谷歌图表

时间:2013-08-28 20:10:40

标签: php sql-server json charts datatable

我要求你就这个项目提出建议,我试图用MS SQL Server数据库中的php来提供像这样的“组合”谷歌图表。 Chart view template

我构建了以下视图,它为我提供了相关数据。我稍后会为每个项目创建一系列这些图表(通过'ProjectUniqueId'识别)。

Table view template

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

    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(如果这是这样做的方法)。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

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

<?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;

?>

希望对你有用