【问题标题】:Combo google chart with php from mssql组合谷歌图表与来自 mssql 的 php
【发布时间】:2013-08-28 20:10:40
【问题描述】:

我需要你对这个项目的建议,我正在尝试用 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(如果这是方法)。

感谢您的帮助!

【问题讨论】:

  • 你必须为此使用谷歌图表吗?
  • 嗨,Dagon,我浏览了很多网页,试图找到一些免费的东西,可以让我创建这种图表(组合堆叠条形图 + 线条)和 googlecharts API 似乎可以使用简单代码的工作。你有什么想法?
  • 我本来打算建议 phpchart.net 但重读它我不确定图表是否是问题
  • 我刚刚检查了 phpchart.net,但他们的精简版似乎并没有做太多:/

标签: php sql-server json charts datatable


【解决方案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;

?>

希望对你有用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-10
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多