【问题标题】:Multiple series from JSON HighstockJSON Highstock 的多个系列
【发布时间】:2013-04-21 12:19:02
【问题描述】:

我的 HighStock 有问题,我需要 JSON 的另一个系列。

我的代码在 get_json.php

include('config.php');

$cp = $_REQUEST["c_prot"];

$r=("SELECT * FROM data WHERE cp='$cp'"); 
$result=mysql_query($r);

while($row = mysql_fetch_array($result)){
    $date= strtotime($row['cas'])*1000;  // timestamp
    $values=hexdec($row['data']);        // series1 
    $val=hexdec($row['src']);            // series2

    $array[]=array($date, $values,$val);  //output array
}
echo json_encode($array);

JSON 输出格式正确:[1364852734000, 557, 2884],.... 但问题是,我没有找到如何将第二系列从 JSON 添加到 Highstock 代码

我想在图表 x 轴上显示:时间戳 y轴:系列1->数据 series2->src

图表现在仅在 x 轴时间戳和 y 轴数据上显示...但 series2 不起作用:/

高股票代码:

<script>
$(function () {
    $.getJSON('http://localhost/vojto/get_json.php?c_prot=<?=$_REQUEST['
    c_prot '];?>', function (data) {

        // Create the chart
        $('#container').highcharts('StockChart', {

            chart: { //zooming
                zoomType: 'x',
                height: 400,
            },

            legend: { //legenda
                enabled: true,
                align: 'left',
                backgroundColor: '#FCFFC5',
                borderColor: 'black',
                borderWidth: 1,
                layout: 'vertical',
                verticalAlign: 'top',
                y: 100,
                shadow: true
            },

            rangeSelector: { //range selector
                buttonTheme: {
                    width: 40,

                },

                buttonSpacing: 3, //mezera mezi buttony
                enabled: true,

                buttons: [{
                    type: 'minute',
                    count: 60,
                    text: 'Hour'
                }, {
                    type: 'day',
                    count: 1,
                    text: 'Day'
                }, {
                    type: 'week',
                    count: 1,
                    text: 'Week'
                }, {
                    type: 'all',
                    text: 'Reset'
                }]
            },

            title: { //title grafu
                text: 'Chart'
            },

            series: [{ //serie
                name: 'Data',
                data: data,
                color: '#57c72f',
                marker: {
                    enabled: true,
                    radius: 3
                },
                shadow: true,
                tooltip: {
                    valueDecimals: 2
                }
            }],

            xAxis: { // X-osa
                type: 'datetime',
                title: {
                    text: 'Date/time axis',
                },
                minRange: 600000,

            },
            yAxis: {
                min: 0,
            },
            navigator: {
                series: {
                    color: '#57c72f',
                    fillOpacity: 0.3,
                }
            },
            credits: {
                enabled: false
            },

            tooltip: { // formátování hodnot po najetí kurzoru... hover
                formatter: function () {
                    var s = '<b>' + Highcharts.dateFormat('DateTime ' + '%d-%m-%y ' + '%H:%M:%S', this.x) + '</b>';

                    $.each(this.points, function (i, point) {
                        s += '<br/>Data value : ' + point.y;
                    });

                    /* formát  23-04-13 09:34:27 */
                    return s;
                }
            },
        });
    });
});
</script>

【问题讨论】:

    标签: json highcharts highstock


    【解决方案1】:

    在你的脚本中:

    $array = [];
    while($row = mysql_fetch_array($result)){
        $date= strtotime($row['cas'])*1000;  // timestamp
        $values=hexdec($row['data']);        // series1 
        $val=hexdec($row['src']);            // series2
    
        $array[0][]=array($date, $values);  //output array
        $array[1][]=array($date ,$val); 
    }
    

    您需要将值粘贴到适当的系列索引。换句话说,您可以准备 $array() 并将点添加到系列之一。老实说,我没有数据,所以这只是概念。

    【讨论】:

    • 谢谢,我修改了我的代码,但现在没有显示图表,我认为问题出在 echo json_encode($array); JSON 输出仍然是相同的格式: [1364852734000,2884],....以及如何在高库存代码中定义新系列?
    • 循环后,你可以使用 print_f($array) 并粘贴到这里吗?我建议将您的行限制为即 5 以仅检查数组结构
    • 我编辑了代码并添加了 print();功能,但它向我显示一个错误:/
    • 首先我没有你的数据库,所以我无法详细复制你的问题并准备完整的例子,但你能在屏幕上粘贴什么 printf() “显示”吗?
    猜你喜欢
    • 2013-05-16
    • 1970-01-01
    • 2016-01-04
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多