Google Chart From Php变量 [英] Google Chart From Php variable
问题描述
我正在尝试从google chart api制作图表,但是我无法将php变量放入脚本中:我得到了一个空白页.
I'm trying to make a chart from the google chart api but I can't get my php variable into the script: I get a blank page.
PHP
$columns = array(
array('string' => 'x'),
array('number' => 'values'),
array('id' => 'i1', 'type' => 'number', 'role' => 'interval'),
array('id' => 'i1', 'type' => 'number', 'role' => 'interval'),
array('number' => 'OtherValues'),
array('id' => 'i1', 'type' => 'number', 'role' => 'interval'),
array('id' => 'i1', 'type' => 'number', 'role' => 'interval')
);
$test = array(
array( 'a' => array(100, 90, 150,15,10,20)),
array( 'b' => array(120, 95, 130,20,10,30)),
array( 'c' => array(130, 105, 140,30,25,35)),
array( 'd' => array( 90, 85, 95,40,35,45)),
array( 'e' => array(70, 74, 63,50,45,55)),
array( 'f' => array(30, 39, 22,60,55,65)),
array( 'g' => array(100, 90, 150,15,10,20)),
);
$table['cols'] = $columns;
$table['rows'] = $test;
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>
使用Google Chrome Javascript控制台,对于每个包含以下详细信息的图形,我都会收到未捕获的错误:无效的类型:未定义":
Using Google Chrome Javascript Console I get "Uncaught Error: Invalid type: undefined" for every graph with these details:
R.Osa
R.ug
(anonymous function)
nj.(anonymous function).d
Ep
drawchart
推荐答案
对于Visualization API的DataTable构造函数,您的数据结构不正确.
Your data structure is incorrect for the Visualization API's DataTable constructor.
列的正确格式是列对象的数组,其中每个对象都具有type
(必选),label
,id
和p
(所有可选)属性. type
是具有可能值string
,number
,boolean
,date
,datetime
和timeofday
的字符串. label
和id
是字符串. p
是列属性的对象.
The correct format for the columns is an array of column objects, where each object has type
(mandatory), label
, id
, and p
(all optional) properties. type
is a string with possible values string
, number
, boolean
, date
, datetime
, and timeofday
. label
and id
are strings. p
is an object of column properties.
正确的行格式是行对象的数组,其中每个对象都具有c
和p
属性. c
是单元格对象的数组. p
是行属性的对象.单元格对象具有v
,f
和p
属性,其中v
是单元格的值,f
是单元格的字符串格式的值,p
是的对象单元格属性.
The correct format for rows is an array of row objects, where each object has c
and p
properties. c
is an array of cell objects. p
is an object of row properties. The cell objects have v
, f
, and p
properties, where v
is the value of the cell, f
is the string-formatted value of the cell, and p
is an object of cell properties.
所有属性对象的受支持属性都取决于您绘制的图表的类型.
Supported properties for all properties objects vary depending on the type of chart(s) you are drawing.
使用PHP的json_encode
函数,将关联数组转换为对象,将非关联数组转换为数组.表的适当结构应如下所示:
Using PHP's json_encode
function, associative arrays are translated into objects and non-associative arrays are turned into arrays. The appropriate structure for your table should look something like this:
$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;
这篇关于Google Chart From Php变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!