简体   繁体   中英

How to parsing data JSON to series in highcharts.js packedbubble type

i've got output from my json like this :

0: {api: 'Cars', method: 'GET', total: 3}
1: {api: 'Cars', method: 'POST', total: 4}
2: {api: 'Trucks', method: 'POST', total: 6}
3: {api: 'Boats', method: 'GET', total: 6}

and what i want to re-create array to populating series in Highchart.js with type: packedbubble

[
 Cars: [
  {name: "GET", value: 3},
  {name: "PUT", value: 0},
  {name: "POST", value: 4},
  {name: "DELETE", value: 0}
 ], 
 Trucks: [
  {name: "GET", value: 0},
  {name: "PUT", value: 0},
  {name: "POST", value: 6},
  {name: "DELETE", value: 0}
 ],
 Boats: [
  {name: "GET", value: 6},
  {name: "PUT", value: 0},
  {name: "POST", value: 0},
  {name: "DELETE", value: 0}
 ]
]

The suggested format is not correct for packedbubble series. Check the API Reference: https://api.highcharts.com/highcharts/series.packedbubble.data

If you want to convert your json data, you can do it as below:

let data = [{api: 'Cars', method: 'GET', total: 3},
            {api: 'Cars', method: 'POST', total: 4},
            {api: 'Trucks', method: 'POST', total: 6},
            {api: 'Boats', method: 'GET', total: 6}];

let cars = [],
 trucks = [],
 boats = [];

data.forEach(obj => {
  if (obj.api === "Cars") {
    cars.push({
      name: obj.method,
      value: obj.total
    })
  } else if (obj.api === "Trucks") {
    trucks.push({
      name: obj.method,
      value: obj.total
    })
  } else if (obj.api === "Boats") {
    boats.push({
      name: obj.method,
      value: obj.total
    })
  }
})

...

  series: [{
    name: 'Cars',
    data: cars
  }, {
    name: 'Trucks',
    data: trucks
  }, {
    name: 'Boats',
    data: boats
  }]

Demo: https://jsfiddle.net/BlackLabel/juxg964q/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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