Stacked bar charts in Chart.js with JSON data

本秂侑毒 提交于 2019-12-08 10:38:05

问题


I have following JSON data.

[
  {
      "date": "2016-05-01T00:00:00",
      "productInformation": [
        {
            "productName": "Apple",
            "totalWeight": 200
        }
      ]
  },
  {
      "date": "2016-09-01T00:00:00",
      "productInformation": [
        {
            "productName": "Apple",
            "totalWeight": 632
        },
        {
            "productName": "Mango",
            "totalWeight": 856
        },
        {
            "productName": "Spinach",
            "totalWeight": 545
        },
        {
            "productName": "Grapes",
            "totalWeight": 338
        }
      ]
  },
  {
      "date": "2017-01-01T00:00:00",
      "productInformation": [
        {
            "productName": "Mango",
            "totalWeight": 500
        }
      ]
  }
]

On X axis I want to display Month and year and on Y axis I want to display stacked bar of product information. for example in 2016-05 there is only Apple so it will only display apple. In 2016-09 there are 4 products so it will display 4 staked bar according to 4 products and its total weight. I have read chart.js documentation. According to documentation I have to provide Y axis values in dataset. How do I extract Y axis values for dataset to create stacked bar from given JSON data? If I want to create chart manually from JSON data given above then it would be something like this.

var barChartData = {
labels: ["May 2016", "September 2016", "January 2017"],
datasets: [{
    label: "Apple",
    data: [200, 632, 0],
    backgroundColor: "#3c8dbc"
},
{
    label: "Mango",
    data: [0,856,500],
    backgroundColor: "#3c8dbc"
},
{
    label: "Spinach",
    data: [0,545,0],
    backgroundColor: "#3c8dbc"
},
{
    label: "Grapes",
    data: [0,338,0],
    backgroundColor: "#3c8dbc"
}]
};

I need a way to extract data part of dataset from given JSON data.


回答1:


This snippet should solve the hardest part of your problem (using ES6 syntax):

const data = [{
  "date": "2016-05-01T00:00:00",
  "productInformation": [{
    "productName": "Apple",
    "totalWeight": 200
  }]
}, {
  "date": "2016-09-01T00:00:00",
  "productInformation": [{
    "productName": "Apple",
    "totalWeight": 632
  }, {
    "productName": "Mango",
    "totalWeight": 856
  }, {
    "productName": "Spinach",
    "totalWeight": 545
  }, {
    "productName": "Grapes",
    "totalWeight": 338
  }]
}, {
  "date": "2017-01-01T00:00:00",
  "productInformation": [{
    "productName": "Mango",
    "totalWeight": 500
  }]
}]

const uniq = a => [...new Set(a)]
const flatten = a => [].concat.apply([], a)

// step 1: find the distinct dates: ["2016-05-01T00:00:00", ... ]
const dates = data.map(e => e.date)

// step 2: find the distinct labels: [Apple, Mango, ... ]
const labels = uniq(
  flatten(data.map(e => e.productInformation))
  .map(e => e.productName))

// step 3: map the labels to entries containing their data by searching the original data array
const result = labels.map(label => {
  return {
    label,
    data: dates.map(date => {
      const hit = data.find(e => e.date === date)
        .productInformation
        .find(p => p.productName === label)
      return hit ? hit.totalWeight : 0
    })
  }
})

console.log(result)


来源:https://stackoverflow.com/questions/42001984/stacked-bar-charts-in-chart-js-with-json-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!