简体   繁体   English

Highcharts:使用JSON数据创建按月和年分组的多个系列

[英]Highcharts: create multiple series grouped my month and year using JSON data

I am trying to create a line, spline chart using JSON data. 我正在尝试使用JSON数据创建折线,样条图。 I want multiple series but I am confused on how to do that. 我想要多个系列,但是我对如何做到这一点感到困惑。 Right now I am able to create the multiple series when the date is in 2019-07-06 format. 现在,当日期为2019-07-06格式时,我可以创建多个系列。 I also have a JSON that has a column for the month and a column for the year Please help on how I can fix this. 我也有一个JSON,其中包含月份的列和年份的列。请提供有关如何解决此问题的帮助。 Right now I only have the code for group by day. 现在,我只有按天分组的代码。

JSON Data: JSON数据:

[ 
 { "month": 6, 
   "year": 2019, 
   "starts": 21278998, 
   "completes": 9309458 
 }, 
 { "month": 7, 
   "year": 2019, 
   "starts": 38850115, 
   "completes": 17790105 
 } 
]

I used the solution for the date format 2019-07-06 provided in this fiddle: https://jsfiddle.net/BlackLabel/tjLvh89b/ 我使用此小提琴中提供的日期格式2019-07-06的解决方案: https : 2019-07-06

Please help with how I can create a chart for the Month, Year on the x-Axis . 请帮助我如何在x-Axis Month, Year创建Month, Year的图表。

You can achieve it simply by creating a Date object using different parameters. 您可以简单地通过使用不同的参数创建Date对象来实现。

Instead of the string date parameter new Date('2019-07-07') use year and month as separate parameters like that: new Date(2019, 7) . 代替字符串date参数, new Date('2019-07-07')使用年和月作为单独的参数,例如: new Date(2019, 7) new Date('2019-07-07') new Date(2019, 7)

Code: 码:

var json = [{
  month: 6,
  year: 2019,
  starts: 21278998,
  completes: 9309458
}, {
  month: 7,
  year: 2019,
  starts: 38850115,
  completes: 17790105
}];

var series1 = {
    name: 'starts',
    data: []
  },
  series2 = {
    name: 'completes',
    data: []
  };

json.forEach(elem => {
  series1.data.push({
    x: +new Date(elem.year, elem.month),
    y: elem.starts
  });

  series2.data.push({
    x: +new Date(elem.year, elem.month),
    y: elem.completes
  });
});

Highcharts.chart('container', {
  chart: {
    type: 'spline'
  },
  xAxis: {
    type: 'datetime'
  },
  series: [
    series1,
    series2
  ]
});

Demo: 演示:

Date object reference: 日期对象参考:

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

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