【问题标题】:Google Chart API Data谷歌图表 API 数据
【发布时间】:2013-02-20 12:33:32
【问题描述】:

如何使用我自己的服务器端数据(即 PHPMySQL)填充 Google Chart API。

目前我有以下数据:

function drawChart()
{
 // Create the data table.
 var data = new google.visualization.DataTable();
 data.addColumn('string', 'City');
 data.addColumn('number', 'Number of Crimes');
 data.addRows([
             ['Cardiff', 300],
             ['London', 900],
             ['Manchester', 500],
             ['Dublin', 400],
             ['Liverpool', 600]
             ]);

// Set Chart Options
var options = {
    'legend': 'left',
    'title': 'Crimes (per day)',
    'is3D': 'True',
    'width':400,
    'height':300
};

// Instantiate and Draw Chart.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}

但是如何从 mySQL 数据库中的表中提供数据呢?

【问题讨论】:

    标签: php mysql api google-visualization


    【解决方案1】:

    在 Charts API 的 JavaScript 代码之前,您必须从数据库中获取数据:

    //Your database query goes here
    $list = mysql_query("SELECT city,crimes FROM TABLE");
    while($row = mysql_fetch_assoc($list)){
      $data[] = "['".$row['city']."', ".$row['crimes']."]";
    }
    $data_for_chart = implode(",\n"$data);
    

    现在替换你的 JS 代码:

    data.addRows([
             ['Cardiff', 300],
             ['London', 900],
             ['Manchester', 500],
             ['Dublin', 400],
             ['Liverpool', 600]
             ]);
    

    与:

    data.addRows([
             <?php echo $data_for_chart; ?>
             ]);
    

    【讨论】:

    • 对不起,你能告诉我在哪里吗?我真的不擅长这个。
    • @JohnSmith:我已经扩展了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多