geogeogeo
geogeogeo

Reputation: 381

How can I set c3.js chart data type using keys from my data object?

I'm trying to dynamically set the data type for each key in my object.

The object looks like:

{"x":["2016-01-1", "2016-01-02", etc], "uniqueKey":[4234, 4324, etc], "uniqueKey2":[432, 543, etc], ... }

The number of keys and names will vary depending on what data I grab, but I need to set the chart data type for each key.

function show_chart1(data) {

    var json = JSON.parse(data);

    var chart = c3.generate({
        bindto: '#chart',
        data: {
            x: 'x',
            xFormat: '%Y-%m-%d',

            json: json,

            #### need these to be the unique key names
            types: {
                uniqueKey: 'area-spline', 
                uniqueKey2: 'area-spline'
                ...
            }, 
            groups: [[uniqueKey, uniqueKey2, etc]]
        },
        axis: {
            x: {
                type: 'timeseries',
                tick: {
                    format: '%Y-%m-%d'
                }
            }
        }      
    });
};

How can I do this?

Upvotes: 1

Views: 552

Answers (1)

Akoya
Akoya

Reputation: 1080

You can build another object for your types/groups. If you don't have some chart variations you can just use type: 'area-spline' instead of giving each entry the same type.

When you know what your unique keys are or what they look like (reffering to regex) you can do this:

var typeDefinition = {}
for(var key in json){

    // here you can use if or switch to apply certain types
    if( key <your statement> ){
        typeDefinition[key] = 'area-spline';
    }

}
<...>
types: typeDefinition
<...>

Upvotes: 1

Related Questions