pacman
pacman

Reputation: 1061

Is there a better way to create bar chart with the following data-structure in extjs4?

So I have the following data-structure coming back from my server.

{date:'2/1', name: 'product 1', count: 10}
{date:'2/1', name: 'product 2', count: 5}
{date:'2/1', name: 'product 3', count: 15}
{date:'2/2', name: 'product 1', count: 11}
{date:'2/2', name: 'product 2', count: 6}

I have to run through bunch of loops and string manipulation to create the following data-structure for the stacked bar chart.

{date:'2/1', product_1: 10, product_2: 5, product_3: 15 }
{date:'2/2', product_1: 11, product_2: 6, product_3: 0}

Is there a more elegant way to solve this problem. I don't have much control over the server side of things.

Upvotes: 1

Views: 84

Answers (1)

James Clark
James Clark

Reputation: 1811

I should think the code to convert from one to the other would be fairly simple.

var dates = {};
var dataOut = [];
Ext.Array.each(dataIn, function(item) {
  var dateObj = dates[item.date];
  if (!date) {
    dates[item.date] = dateObj = {dateObj: item.date};
    dataOut.push(dateObj);
  }
  dateObj[item.name] = item.count;
});

I don't think there are any helper classes in Ext for this particular conversion, and I don't think the chart system can accept different data formats. You could write your own Model which references the original data, but I think that would be more work than the above code.

Upvotes: 1

Related Questions