J.He J.He - 3 years ago 125
JSON Question

Plot Highchart multiple series line chart using JSON data

I tried to plot JSON data using JSON data.JSFiddle
Below is my JSON data in JavaScript.

var JSON = [
{ name:"Maintenance",
data:[[2017-06-26,1.5],
[2017-07-03,5.2],
[2017-07-10,1.65],
[2017-07-17,2.5],
[2017-07-24,1.5]
]
},
{ name:"Others",
data:[[2017-06-26,1.5],
[2017-07-03,1.5],
[2017-07-10,1.5],
[2017-07-17,1.25],
[2017-07-24,1.5]
]
},
{ name:"Project",
data:[[2017-06-26,6.5],
[2017-07-03,6.1],
[2017-07-10,6.7],
[2017-07-17,7],
[2017-07-24,6.5]
]
},
{ name:"Training",
data:[[2017-06-26,0],
[2017-07-03,0.75],
[2017-07-10,1.9],
[2017-07-17,0.5],
[2017-07-24,1]
]
},
{ name:"Day-Off",
data:[[2017-06-26,0],
[2017-07-03,0],
[2017-07-10,0],
[2017-07-17,0],
[2017-07-24,1]
]
}]


However, the chart looks strange. For every series, there is an additional line connecting the start point and the end point. In addition, the x-axis value is not the date that I want.

//Draw chart
Highcharts.chart('trend_bl', {

title: {
text: 'Trend by Business Lines'
},
yAxis: {
title: {
text: ' Resource Allocation'
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle'
},

series : JSON,
});


Can anyone tell me why this would happen? In addition, I would also like to know the proper JSON data structure for line chart and pie chart in Highchart.

Answer Source

Your date in JSON should be string. This date should be converted to millisecond.

var JSON = [{
  name: "Maintenance",
  data: [
    ['2017-06-26', 1.5],
    ['2017-07-03', 5.2],
    ['2017-07-10', 1.65],
    ['2017-07-17', 2.5],
    ['2017-07-24', 1.5]
  ]
}, {
  name: "Others",
  data: [
    ['2017-06-26', 1.5],
    ['2017-07-03', 1.5],
    ['2017-07-10', 1.5],
    ['2017-07-17', 1.25],
    ['2017-07-24', 1.5]
  ]
}, {
  name: "Project",
  data: [
    ['2017-06-26', 6.5],
    ['2017-07-03', 6.1],
    ['2017-07-10', 6.7],
    ['2017-07-17', 7],
    ['2017-07-24', 6.5]
  ]
}, {
  name: "Training",
  data: [
    ['2017-06-26', 0],
    ['2017-07-03', 0.75],
    ['2017-07-10', 1.9],
    ['2017-07-17', 0.5],
    ['2017-07-24', 1]
  ]
}, {
  name: "Day-Off",
  data: [
    ['2017-06-26', 0],
    ['2017-07-03', 0],
    ['2017-07-10', 0],
    ['2017-07-17', 0],
    ['2017-07-24', 1]
  ]
}];
//updating jsons date to millisecond
Object.keys(JSON).map(function(key, index) {
  JSON[key].data.map(function(value, keys, index) {
    JSON[key].data[keys][0]=new Date(value[0]).getTime()
  })
});
//console.log(JSON)

Fiddle Demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download