【问题标题】:How can I set c3.js chart data type using keys from my data object?如何使用我的数据对象中的键设置 c3.js 图表数据类型?
【发布时间】:2017-02-18 19:17:24
【问题描述】:

我正在尝试为我的对象中的每个键动态设置数据类型。

对象看起来像:

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

键和名称的数量会根据我抓取的数据而有所不同,但我需要为每个键设置图表数据类型。

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'
                }
            }
        }      
    });
};

我该怎么做?

【问题讨论】:

    标签: javascript json c3.js


    【解决方案1】:

    您可以为您的类型/组构建另一个对象。如果您没有一些图表变体,您可以使用 type: 'area-spline' 而不是为每个条目指定相同的类型。

    当您知道您的唯一键是什么或它们的外观时(参考正则表达式),您可以这样做:

    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
    <...>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-02
      • 2015-02-14
      • 1970-01-01
      • 2017-04-16
      • 2020-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多