duande1985 2019-04-14 01:54
浏览 51

如何使用关联数组生成带有图表js的图形?

I am trying to generate a graph from an array which contains other associative arrays inside it. All the arrays that are inside the main array have the same keys.

Currently, the graphing is done using variables that keep an array with the values ​​to graph them, each variable represents a series or "line" of the graph.

//Start of the code for the series of the graph
data: {
            //array that contains the labels of each data series
            labels: [
<?php
$p = "";
foreach ($categoryArray as $d) {
    echo "'" . $d['label'] . "',";
}
?>
            ],
            //arrays of the data series to plot
            datasets: [{
                    //first series
                    data: [
                        <?php
                        $t = "";
                        foreach ($dataseries1 as $d) {
                            echo $d['value'] . ",";
                        }
                        ?>
                    ],
                    label: "PTemp_C_Avg",
                    borderColor: "#000000",
                    pointRadius: 0,
                    fill: false,
                    lineTension: 0
                },
                //second series
                    data: [
                        <?php
                        $t = "";
                        foreach ($dataseries1 as $d) {
                            echo $d['value'] . ",";
                        }
                        ?>
                    ],
                    label: "PTemp_C_Avg",
                    borderColor: "#000000",
                    pointRadius: 0,
                    fill: false,
                    lineTension: 0
                },
                //n series
                ......
                .......
                .......

As you can see in the previous code you must make the code for each series, however this limits the number of series that can be plotted, so I would like to be able to generate the graph from associative arrays as shown below :

Array ( [0] => 
    Array ( 
        [5cad0e5a00d510066802a0da] => MongoDB\BSON\ObjectId Object ( [oid] => 5cad0e5a00d510066802a0db ) 
        [NS5c53244700d5101cb4008757] => 5c53244700d5101cb4008757 
        [TIMESTAMP] => MongoDB\BSON\UTCDateTime Object ( [milliseconds] => 1532939400000 ) 
        [RECORD] => 276 
        [PTemp_C_Avg] => 7.261 
        [T0_10cm_Avg] => 21.32 
        [T0_10cm_Max] => 25.47 
        [T0_10cm_Min] => 15.98 
        [T1_1m_Avg] => 27.36 
        [T1_1m_Max] => 35.4 
        [T1_1m_Min] => 20.23 
        [T2_2m_Avg] => 31.77 
        [T2_2m_Max] => 39.18 
        [T2_2m_Min] => 24.4
    )

    [1] => 
        Array ( 
        [5cad0e5a00d510066802a0da] => MongoDB\BSON\ObjectId Object ( [oid] => 5cad0e5a00d510066802a0dc ) 
        [NS5c53244700d5101cb4008757] => 5c53244700d5101cb4008757 
        [TIMESTAMP] => MongoDB\BSON\UTCDateTime Object ( [milliseconds] => 1532940300000 ) 
        [RECORD] => 277 
        [PTemp_C_Avg] => 7.215 
        [T0_10cm_Avg] => 21.18 
        [T0_10cm_Max] => 27.68 
        [T0_10cm_Min] => 14.7 
        [T1_1m_Avg] => 27.27 
        [T1_1m_Max] => 32.6 
        [T1_1m_Min] => 20.53 
        [T2_2m_Avg] => 32.04 
        [T2_2m_Max] => 38.94 
        [T2_2m_Min] => 25.3
    )

    [2] => 
        Array ( 
        [5cad0e5a00d510066802a0da] => MongoDB\BSON\ObjectId Object ( [oid] => 5cad0e5a00d510066802a0dd ) 
        [NS5c53244700d5101cb4008757] => 5c53244700d5101cb4008757 
        [TIMESTAMP] => MongoDB\BSON\UTCDateTime Object ( [milliseconds] => 1532941200000 ) 
        [RECORD] => 278 
        [PTemp_C_Avg] => 7.218 
        [T0_10cm_Avg] => 21.07 
        [T0_10cm_Max] => 26.18 
        [T0_10cm_Min] => 15.01 
        [T1_1m_Avg] => 27.74 
        [T1_1m_Max] => 34.07 
        [T1_1m_Min] => 22.81 
        [T2_2m_Avg] => 31.33 
        [T2_2m_Max] => 37.32 
        [T2_2m_Min] => 25.14
    )
)

Where each key will be a series or "line" to graph, for example all the keys "PTemp_C_Avg" that are in the array will be grouped in the same series and so on.

I would appreciate any suggestions you could give me to solve this problem and if you could give me an example I would be very helpful

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
    • ¥15 Python报错怎么解决
    • ¥15 simulink如何调用DLL文件
    • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
    • ¥30 线性代数的问题,我真的忘了线代的知识了
    • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
    • ¥188 需要修改一个工具,懂得汇编的人来。
    • ¥15 livecharts wpf piechart 属性
    • ¥20 数学建模,尽量用matlab回答,论文格式
    • ¥15 昨天挂载了一下u盘,然后拔了