简体   繁体   English

高图:使用JSON数据创建多个系列

[英]Highcharts: create multiple series using JSON data

I am trying to create a line, spline chart using JSON data. 我正在尝试使用JSON数据创建line, spline图。 I want multiple series but I am confused on how to do that. 我想要多个系列,但是我对如何做到这一点感到困惑。 Right now I am able to create one with one series but this one doesn't look right either. 现在,我可以用一个系列创建一个,但是这个系列看起来也不正确。 Plus I don't see the legend either. 另外,我也看不到任何传说。 Please help on how I can fix this. 请帮助我如何解决此问题。 Right now I only have the code for starts . 现在我只有starts的代码。 I also want to completes to my chart. 我也想completes我的图表。 I want the legends to say start and complete 我要传说说startcomplete

JSON Data: JSON数据:

[
{
    "date": "2019-07-07",
    "starts": 42,
    "completes": 142
},
{
    "date": "2019-07-08",
    "starts": 2,
    "completes": 90
},
{
    "date": "2019-07-09",
    "starts": 28,
    "completes": 175
}
]

My Code: 我的代码:

<div id="container"></div>

let theAPI = `the json file`

$.getJSON(theAPI, result => {
    let main_chart_data = [];

    result.forEach((d, i) => {
        let date_chart = new Date(d.date);
        main_chart_data.push([date_chart, d.starts, ]);
    });

    let main_chart_options = buildChart({
        chart_type: 'spline',
        height: 265
    });
    main_chart_options.legend.enabled = false;
    main_chart_options.lang = {
        'noData': 'There is currently no data available.'
    };
    main_chart_options.series = [{
        data: main_chart_data
    }];

    main_chart_options.tooltip = {
        formatter: function() {
            let tooltip = `
      <span style="font-weight:500;">${moment(this.x).format('MMM DD - ha')}</span>
      <br />
      <span>${addCommas(this.y)}</span>
    `;
            return tooltip;
        },
        useHTML: true
    }

    new Highcharts.chart('container', main_chart_options);

    let total_pvs = result.reduce((a, c) => {
        return a += c.starts;
    }, 0);

})

This is my result now: 这是我现在的结果:

Spline - Highchart 花键-高图

You can achieve it by splitting the JSON data into two separate series to look like that: 您可以通过将JSON数据分成两个独立的系列来实现它,如下所示:

[{
  "name": "starts",
  "data": [{
    "x": 1562457600000, // date in milliseconds
    "y": 42
  }, {
    "x": 1562544000000,
    "y": 2
  }, {
    "x": 1562630400000,
    "y": 28
  }]
}, {
  "name": "completes",
  "data": [{
    "x": 1562457600000,
    "y": 142
  }, {
    "x": 1562544000000,
    "y": 90
  }, {
    "x": 1562630400000,
    "y": 175
  }]
}]



Check this demo to see how you can make it: 查看此演示以了解如何制作:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM