J.He
J.He

Reputation: 25

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.

Upvotes: 1

Views: 4337

Answers (2)

pawel_d
pawel_d

Reputation: 3070

First of all, you have made a mistake in your demo. Instead of data: JSON it should be series: JSON. Secondly, put your dates inside of strings, otherwise they will be treated as numbers (e.g. 2017 - 06 - 26 = 1985).

Example:
http://jsfiddle.net/3yumsp8m/

Upvotes: 0

Deep 3015
Deep 3015

Reputation: 10075

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

Upvotes: 3

Related Questions