【问题标题】:PHP array to HighchartsPHP数组到Highcharts
【发布时间】:2015-05-25 00:52:59
【问题描述】:

如何将这些数据从数组中获取到 Highcharts?

    [0] => Array
        (
            [0] => Date
            [1] => data1
            [2] => data2
        )

    [1] => Array
        (
            [0] => 2015-03-26 02:00
            [1] =>      1,425
            [2] =>     39,294
        )

    [2] => Array
        (
            [0] => 2015-03-26 03:00
            [1] =>      1,422
            [2] =>     39,076
        )

我是 PHP 新手,正在寻找一种将第一个键(日期)作为 x 轴,将 key2(数据2)作为 y 轴的方法。是否可以通过选择数组中的所有键值并将其传递给 Highcharts 的这个 javascript 来完成?

$(function () { 
            $('#container').highcharts({
                chart: {
                    type: 'line'
                },
                title: {
                    text: 'Chart Title'
                },
                xAxis: {
                    categories: //Dates here 2015-03-26 02:00//
                },
                yAxis: {
                    title: {
                        text: 'a text here'
                    }
                },
                series: [{
                    name: 'data1',
                    data: // key1 from array 1,425 //
                }, {
                    name: 'data2',
                    data: // key2 from array 39,294 //
                }]
            });
        });

【问题讨论】:

    标签: javascript php highcharts


    【解决方案1】:
    $data = array
    (
        array
        (
            "Date",
            "data1",
            "data2"
        ),
        array
        (
            "2015-03-26 02:00",
            "1,425",
            "39,294"
        ),
        array
        (
            "2015-03-26 03:00",
            "1,422",
            "39,076"
        )
    );
    
    $series = array();
    
    // Get the items of the first array
    $items = $data[0];
    
    foreach($items as $itemIndex => $value)
    {
        // Skip the 'Date'
        if (!$itemIndex)
            continue;
    
        // We should have the actual array to add to the 'data', if not skip
        if (empty($data[$itemIndex]))
            continue;
    
        $series[] = array
        (
            "name" => $value,
            "data" => $data[$itemIndex]
        );
    }
    
    echo json_encode($series);
    

    【讨论】:

    • 谢谢,如何将名称和数据数组字段放入 javascript?
    • 名字是串联数组,你能指定你想加哪个名字吗?
    • 我更新了我的第一篇文章。我想将日期传递给 xAxis,并将 data1 传递给第一个系列。 data2 到第二个系列。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 2018-03-23
    • 2023-03-28
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    相关资源
    最近更新 更多