c3js / c3

:bar_chart: A D3-based reusable chart library
http://c3js.org
MIT License
9.35k stars 1.39k forks source link

Load JSON into PIE Chart #1217

Open nithingit opened 9 years ago

nithingit commented 9 years ago

chart = c3.generate({ data: { json: [ {name: 'www.site1.com', upload: 200}, {name: 'www.site2.com', upload: 100}, {name: 'www.site3.com', upload: 300}, {name: 'www.site4.com', upload: 400}, ], keys: { // x: 'name', // it's possible to specify 'x' when category axis value: ['upload'], }, type:'pie' }, axis: { x: { // type: 'category' } } });

//This code is taken as an example..

I need to generate a pie chart having 4 divisions (site1,site2...)each division correspond to its respective upload value.

In the above code i am not able to achieve this(I have specified value:['upload'])...

what is the exact value i have to specify?

Thanks..

luishdez commented 9 years ago

I don't think It's possible @masayuki0812 put an example https://github.com/masayuki0812/c3/issues/430 but it's not the behavior we expect because the JSON must be transformed and if you have to transform the JSON … then it doesn't make to use the JSON parser …

This behavior for pie, donut … doesn't follow the logic used in other type of charts. I hope this can be fixed because when you export data from DBs, like MySQL, MongoDB etc… you expect that type data estructure

[
{"labelXX": 23},
{"labelYY": 113}
]

@nithingit You may want to use underscore or other map reduce function to create a valid Array for c3

_.map(data, function(obj){
  return [obj.name, obj.upload];
});
nbdavies commented 7 years ago

I'm in the same boat as these folks, trying to pass in JSON structured like:

[{name: 'Pizza', value: 20, color: '#CCA', label: 'Zza!', tooltip: '<span><strong>Pizza:</strong> Flat bread with sauce, cheese, and toppings.</span>'}, {name: 'Hamburger'...}]

...and then using data.keys to tell c3 how to map this input to the various data lists that can or must be included.

Turning this one JSON object (where everything relevant to a value is consolidated) into a bunch of arrays to pass into data.colors, data.labels, etc. seems like it defeats the purpose of exposing the data.json/data.keys pattern.