1

I am trying to create data visualization using dc.js library.

But my data has multiple value fields.

So here my part of data:

[{"_1000_1249":0,"_1250_1499":2},{"_1000_1249":1,"_1250_1499":2},{"_1000_1249":2,"_1250_1499":3}]

I want to create group with the data keys like this(calculating sum of data):

  • "_1000_1249":3,(sum of "_1000_1249")
  • "_1250_1499":7,(sum of "_1250_1499")

So How can I create dc.js row chart like this?

Thanks

tcetin
  • 979
  • 1
  • 21
  • 48
  • I agree with @Ethan's answer: if you can reshape, do that. If you really can't, there is http://stackoverflow.com/questions/24737277/dc-js-how-to-create-a-row-chart-from-multiple-columns and various follow-ups. – Gordon Dec 15 '16 at 05:37

1 Answers1

1

I think you should reshape your data so that it fits better into the Crossfilter paradigm. The following should get you what you want:

var data = [{"_1000_1249":0,"_1250_1499":2},{"_1000_1249":1,"_1250_1499":2},{"_1000_1249":2,"_1250_1499":3}]

var updatedData = []
for(var i=0; i < data.length; i++) {
    for(key in data[i]) {
    updatedData.push({
        key: key,
      value: data[i][key]
    })
  }
}

var cf = crossfilter(updatedData)
var dimension = cf.dimension(function(d) { return d.key; })
var group = dimension.group().reduceSum(function(d) { return d.value; })

console.log(group.all())

If you want to experiment, this is in a JSFiddle here: https://jsfiddle.net/esjewett/1d853fwj/3/

Ethan Jewett
  • 6,002
  • 16
  • 25
  • Ok but I have a different types of data in my dataset. If there is only this numbers there is no problem but there is different types of data. This data does not make dilter with others like this.So it maybe update question – tcetin Dec 15 '16 at 07:07
  • Try this answer with your updated data. If you just have additional properties, it will work fine. – Ethan Jewett Dec 15 '16 at 15:06