dre26973 2013-02-01 09:23
浏览 54
已采纳

Highstock - 如何使用JSON-Array添加多个系列

My target: I want to draw HighStock-chart with multiple series. Data is loaded by AJAX from data.php. The output of data.php is an JSON-Array

My problem: I don't know hot to grab the data from JSON Array

The output is e.g

[[timestamp,value1,value2],[timestamp,value1,value2]]

Series 1 should be -> timestamp and value1

Series 2 should be -> timestamp and value2

This is my code

// Draw the chart
$(function(){

        /* The chart is drawn by getting the specific data from "data.php".
         * The configuration settings are transmitted by GET-Method. The output is an JSON-Array.  */

        $.getJSON('data.php',
        function(data) {


        chart = new Highcharts.StockChart
        ({
        chart:  {  renderTo: 'chartcontainer', type: 'line'  },
        title:  { text: 'You see the data of the last hour!' },
        xAxis: {  type: 'datetime', title: { text: 'time'  } },
        yAxis: { title: { text: 'unit'  } },
        series: [{ name: 'series1', data: data },{ name: 'series2', data: data }],

        });
    });
});

I think i have to change

series: [{ name: 'series1', data: data },{ name: 'series2', data: data }],

But I dont know in to what

  • 写回答

1条回答 默认 最新

  • dow98764 2013-02-01 09:40
    关注

    Iterate through all items of the data array, and populate two separate arrays:

    var series1 = [];
    var series2 = [];
    for (var i = 0; i < data.length; i++) {
      series1.push([data[0], data[1]);
      series1.push([data[0], data[2]);
    }
    

    You then have the timestamp-value pairs in each of the series arrays.

    Doing this in php might be more efficient, especially if you can replace the current json creation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了