簡體   English   中英

使用多維PHP數組填充Google組合圖

[英]Populating Google combo chart with multidimensional PHP array

我有一個學生數據的php數組。 看起來像這樣:

array(5) {
    ["question1"]=>
        array(30) {
            ["2014, 03, 02"]=>
                array(10) {
                    ["student1"]=>
                    int(54)
                    ["student2"]=>
                    int(43)
                    ... etc. ...
                    ["median"]=>
                    string(2) "49"
}

每天,學生回答5個問題,其值從1到100。每天計算一個問題的所有答案的中值。 答案和中值如上存儲。

現在,我想用此數據填充Google Charts組合圖,但無法正常工作。

我想用X軸上的日期,Y軸上的1-100值以及每個答案作為一個點來可視化數據。 中值的數據應顯示為各點之間的曲線。 每個問題的點和曲線應具有自己的顏色。

但是我幾乎被困住了。 我不知道如何插入數據。 我已經嘗試過這種方法:

var jsonData = (<?= json_encode($data)?>);
var data = new google.visualization.arrayToDataTable(jsonData);

但我只收到以下錯誤消息:

Uncaught Error: Not an array    format+da,default+da,ui+da,corechart+da.I.js:181
lda    format+da,default+da,ui+da,corechart+da.I.js:181
Gp    format+da,default+da,ui+da,corechart+da.I.js:183
drawChart    ?side=graf:4888

您需要更改數據格式。 Visualization API期望表格形式的數據,其中x軸數據在第一列中,每個數據系列(一組彩色點)都是其自己的數據列。 您可以構造DataTable的json表示形式(供DataTable構造函數使用)或數據數組的數組(供arrayToDataTable函數使用)。

由於您已經在使用arrayToDataTable函數,因此這是您需要的結構:

$data = array(
    array('Date', 'Question 1', array('type' => 'number', 'role' => 'interval', 'id' => 'Q1Median'), 'Question 2', array('type' => 'number', 'role' => 'interval', 'id' => 'Q2Median') /* repeat for other questions */),
    // format is array(date, q1 score, q1 median, q2 score, q2 median...)
    // set the median in the first row for the day, leave it null for all other rows
    array('2014, 03, 02', 54, 49, /* repeat for other questions */),
    array('2014, 03, 02', 43, null, /* repeat for other questions */),
    // repeat one row for each student for each day
);

中間值在“間隔”角色列中設置,您可以設置其樣式以在圖表選項中顯示為曲線:

interval: {
    // use the column ID of an interval to set the options for that interval
    Q1median: {
        style: 'line'
    },
    Q2median: {
        style: 'line'
    }
    // etc...
}

樣式間隔的選項在此處記錄

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM