Bhavesh Jadav
Bhavesh Jadav

Reputation: 705

Stacked bar charts in Chart.js with JSON data

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.

Upvotes: 0

Views: 1833

Answers (1)

Christian Zosel
Christian Zosel

Reputation: 1424

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)

Upvotes: 2

Related Questions