来自Php变量的Google图表

来自Php变量的Google图表,php,google-visualization,json,Php,Google Visualization,Json,我试图从GoogleChartAPI制作一个图表,但是我无法将我的php变量放入脚本:我得到一个空白页面 PHP Html视图 <div id="chart-lines"></div> <script> google.setOnLoadCallback(drawChart); function drawChart() { var data = new google.visualization.DataTable(<?php ech

我试图从GoogleChartAPI制作一个图表,但是我无法将我的php变量放入脚本:我得到一个空白页面

PHP

Html视图

<div id="chart-lines"></div>
<script>
    google.setOnLoadCallback(drawChart);
    function drawChart() {
    var data = new google.visualization.DataTable(<?php echo json_encode($table) ?>);


    // The intervals data as narrow lines (useful for showing raw source
    // data)
    var options_lines = {
        title: 'Line intervals, default',
      intervals: { 'lineWidth':2, 'barWidth': 0.5 },

        legend: 'none',
    };

    var chart_lines = new google.visualization.LineChart(document.getElementById('chart-lines'));
    chart_lines.draw(data, options_lines);
}
</script>

可视化API的DataTable构造函数的数据结构不正确

列的正确格式是列对象的数组,其中每个对象都有
类型
(必需)、
标签
id
、和
p
(所有可选)属性<代码>类型是一个字符串,可能包含以下值:
字符串
数字
布尔值
日期
日期时间
,以及
日期时间
<代码>标签和
id
是字符串
p
是列属性的对象

行的正确格式是行对象数组,其中每个对象都有
c
p
属性
c
是单元格对象的数组<代码>p是行属性的对象。单元格对象具有
v
f
p
属性,其中
v
是单元格的值,
f
是单元格的字符串格式值,
p
是单元格属性的对象

所有properties对象支持的特性因所绘制图表的类型而异

使用PHP的
json_encode
函数,将关联数组转换为对象,将非关联数组转换为数组。表的适当结构应如下所示:

$columns = array(
    array('type' => 'string', 'label' => 'x'),
    array('type' => 'number', 'label' => 'values'),
    // each interval should have its own unique id,
    // but leaving them the same won't break anything for your chart as-is
    array('type' => 'number', 'id' => 'i1', 'p' => array('role' => 'interval')),
    array('type' => 'number', 'id' => 'i1', 'p' => array('role' => 'interval')),
    array('type' => 'number', 'label' => 'OtherValues'),
    array('type' => 'number', 'id' => 'i1', 'p' => array('role' => 'interval')),
    array('type' => 'number', 'id' => 'i1', 'p' => array('role' => 'interval'))
);

$test = array(
    array('c' => array(
        array('v' => 'a'),
        array('v' => 100),
        array('v' => 90),
        array('v' => 150),
        array('v' => 15),
        array('v' => 10),
        array('v' => 20)
    )),
    // etc
);
$table['cols'] = $columns;
$table['rows'] = $test;

空白页表示无法生成图表。检查您的控制台是否存在javascript错误并发布它们!先生,你让我高兴极了。
R.Osa
R.ug
(anonymous function)
nj.(anonymous function).d
Ep
drawchart
$columns = array(
    array('type' => 'string', 'label' => 'x'),
    array('type' => 'number', 'label' => 'values'),
    // each interval should have its own unique id,
    // but leaving them the same won't break anything for your chart as-is
    array('type' => 'number', 'id' => 'i1', 'p' => array('role' => 'interval')),
    array('type' => 'number', 'id' => 'i1', 'p' => array('role' => 'interval')),
    array('type' => 'number', 'label' => 'OtherValues'),
    array('type' => 'number', 'id' => 'i1', 'p' => array('role' => 'interval')),
    array('type' => 'number', 'id' => 'i1', 'p' => array('role' => 'interval'))
);

$test = array(
    array('c' => array(
        array('v' => 'a'),
        array('v' => 100),
        array('v' => 90),
        array('v' => 150),
        array('v' => 15),
        array('v' => 10),
        array('v' => 20)
    )),
    // etc
);
$table['cols'] = $columns;
$table['rows'] = $test;