如何将属性放入谷歌区域图表的{j}数据

时间:2018-04-23 13:28:15

标签: json google-visualization

我想要对区域图中的某些区域进行着色。因此,我有以下PHP代码来设置包含来自DB的数据的JSON文档:

(颜色的评估和设置仍然缺失)

$rows = array();
$table = array();

$result = $mysqli->query('SELECT * FROM Test ORDER BY DateTime');

$table['cols'] = array(


    array('label' => 'Date', 'type' => 'string'),
    array('label' => 'Value', 'type' => 'number'),
    array('label' => '', 'type' => 'string', 'p' => '{ "role": "style" }')    
);



foreach ($result as $r) 
    {

     $switch_array = array();



    $switch_array[] = array('v' => (string) $r['DateTime']);      

    $switch_array[] = array('v' => (int) $r['Value']); 

    $switch_array[] = array('v' => (string) "area { color: green }"); 




      $rows[] = array('c' => $switch_array);




    }

    $table['rows'] = $rows;

$jsonTable = json_encode($table);

当我尝试通过以下方式打印时:

         var data2 = new google.visualization.DataTable(<?= $jsonTable2 ?>);

我收到了错误消息:

无法使用'in'运算符在{“role”中搜索“role”:“style”}×

我想用JSON Data做同样的事情:

data2.addColumn('string', 'Date');
            data2.addColumn('number', 'Value');
            data2.addColumn({type:'string',role:'style'}); // certainty col.
            data2.addRows([
                ['2014-12-01',100, 'area { color: green }'],
                ['2015-01-01',200, 'area { color: green }'],
                ['2015-02-01',300, 'area { color: green }'],
                ['2015-03-01',400, 'area { color: green }'],
                ['2015-04-01',500, 'area { color: red }']
            ]);

如何将角色:'style'属性更正为JSON?

1 个答案:

答案 0 :(得分:1)

role密钥不应作为属性包含在'p'

array('label' => '', 'type' => 'string', 'p' => '{ "role": "style" }')  

它应该只是该栏目的另一个关键......

array('label' => '', 'type' => 'string', 'role' => 'style')