Google Chart From Php variable

2019-01-20 06:35发布

问题:

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 view

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

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

回答1:

Your data structure is incorrect for the Visualization API's DataTable constructor.

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.

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.

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;