-1

I have variable data having json data as below:

[
    {
        "BillingMonth":"11",
        "BillingYear":"2016",
        "Volume":"72",
        "BillingMonthName":"November",
        "BillingProduct":"Product1"
    },
    {
        "BillingMonth":"11",
        "BillingYear":"2016",
        "Volume":"617",
        "BillingMonthName":"November",
        "BillingProduct":"Product2"
    },
    {
        "BillingMonth":"12",
        "BillingYear":"2016",
        "Volume":"72",
        "BillingMonthName":"December",
        "BillingProduct":"Product1"
    },
    {
        "BillingMonth":"12",
        "BillingYear":"2016",
        "Volume":"72",
        "BillingMonthName":"December",
        "BillingProduct":"Product2"
    }
]

What I want to split above json data using javascript/jquery and get them stored in two variables data1, data2 having json data as below as result:

 {
    type: "stackedBar",
    legendText: "Product1",
    showInLegend: "true",
    data1: [
        { x: November, y: 72 },
        { x: December, y: 72 },
    ]
  }

and

   {
        type: "stackedBar",
        legendText: "Product2",
        showInLegend: "true",
        data2: [
            { x: November, y: 617 },
            { x: December, y: 72 },
        ]
   }

The above will bind in canvas js stackedbar chart.

Thanks!

Tân
  • 1
  • 15
  • 56
  • 102
niten146
  • 1
  • 5

1 Answers1

0

Hey here's a solution I had a lot of fun working on I hope it works well for you. I wasn't sure if you would always have 2 products product1, product2 so I went with a more general approach for n amount of products. The result is in an array format, but you can use es6 destructuring to get the two variables data1 and data2 like I did below:

/*
 * helper function to restructure json in the desired format
 */
function format(obj) {
  var formatted = {
    "type": "stackedBar",
    "legendText": obj.BillingProduct,
    "showInLegend": "true",
    "data": [{
      "x": obj.BillingMonthName,
      "y": obj.Volume
    }]
  }
  return formatted;
}

/*
 *  returns an array of unique products with corresponding BillingMonth/Volume data
 */
function getStackedBarData(data) {
  // transform each obj in orignal array to desired structure
  var formattedData = data.map(format);

  // remove duplicate products and aggregate the data fields
  var stackedBarData =
    formattedData.reduce(function(acc, val){

      var getProduct = acc.filter(function(item){
        return item.legendText == val.legendText
      });

      if (getProduct.length != 0) {
        getProduct[0].data.push(val.data[0]);
        return acc;
      }

      acc.push(val);
      return acc;
    }, []);

  return stackedBarData;

}

var data = [{
  "BillingMonth": "11",
  "BillingYear": "2016",
  "Volume": "72",
  "BillingMonthName": "November",
  "BillingProduct": "Product1"
}, {
  "BillingMonth": "11",
  "BillingYear": "2016",
  "Volume": "617",
  "BillingMonthName": "November",
  "BillingProduct": "Product2"
}, {
  "BillingMonth": "12",
  "BillingYear": "2016",
  "Volume": "72",
  "BillingMonthName": "December",
  "BillingProduct": "Product1"
}, {
  "BillingMonth": "12",
  "BillingYear": "2016",
  "Volume": "72",
  "BillingMonthName": "December",
  "BillingProduct": "Product2"
}]

var dataVars = getStackedBarData(data);

var data1 = dataVars[0];
var data2 = dataVars[1];

console.log(data1);
console.log(data2);

Hope this helps you!

Jonathan Portorreal
  • 2,730
  • 4
  • 21
  • 38