我无法访问我所有的图表数组键

时间:2018-11-02 20:36:54

标签: javascript php json google-visualization

我正在使用Google Charts开发PHP和JavaScript项目,

我想知道如何接收我的 pageSpeed pageSpeed2 元素。

PHP代码:

public function charts(){
    $this->charts['pageSpeed'] = [
        "columns" => [ // This key will store the definition of each column
                       ["key" => "string", "label" => "Browser"],
                       ["key" => "number", "label" => "Range"]
                     ],
        "rows" => [ // This looks more like a standardized model instead of random key-value pairs
                     ["string" => "JS",     "number" => 10],
                     ["string" => "Images", "number" => 30],
                     ["string" => "HTML",   "number" => 20],
                     ["string" => "CSS",    "number" => 30],
                     ["string" => "Other",  "number" => 10]
                   ]
                ];
    $this->charts['pageSpeed2'] = [
          "columns" => [ // This key will store the definition of each column
                         ["key" => "string", "label" => "Browser"],
                         ["key" => "number", "label" => "Range"]
                       ],
           "rows" => [ // This looks more like a standardized model instead of random key-value pairs
                         ["string" => "JS",     "number" => 10],
                         ["string" => "Images", "number" => 30],
                         ["string" => "HTML",   "number" => 20],
                         ["string" => "CSS",    "number" => 30],
                         ["string" => "Other",  "number" => 10]
                      ]
        ];
        echo json_encode($this->charts);
    }

我的JavaScript / JSON代码:

google.charts.load('current', {
  packages: ['corechart']
}).then(function(){
    if($('#charts').length){
        data = new google.visualization.DataTable();
        url = window.location.href+'/charts';
        $.getJSON(url, params = null, function(feedback){
            $.each(feedback, function(keys, values){
                const tableData = feedback[keys];
                const columnKeys = tableData.columns.map(({ key, label }) => {
                    data.addColumn(key, label); // ("string", "Browser") for the first iteration, then ("number", "range")
                    //return key; // the "columnKeys" variable will then look like ["string", "number"]
                });
                data.addRows(tableData.rows.map((row) => columnKeys.map((key) => row[key])));

                var options = {"pieHole":0.4, "width":100, "height":100,'legend':'none', 'chartArea': {'width': '80%', 'height': '80%'}};
                // draw the chart
                var chart = new google.visualization.PieChart(document.getElementById(keys));
                chart.draw(data, options);
            });
        });
    }
});

如何获取所有图表元素?现在我只得到pageSpeed键,而不是pageSpeed2。

0 个答案:

没有答案