【问题标题】:Specifying "groups" dynamically in stacked bar chart c3js在堆积条形图c3js中动态指定“组”
【发布时间】:2015-06-18 04:51:31
【问题描述】:

//以这段代码为例

这里我已经指定了 yvalue[0],yvalue[1] 分组.. 但我需要一个通用设计,我不知道我必须创建的组数(即段数),这根据 json 数据而有所不同。

考虑这个例子,我有total,total1,因此我只有2个值。但是如果在json中指定了第三个变量total2,我应该在我的条形图中有一个段等等。这必须是每次添加字段时都不会更改组。有什么方法可以实现吗??

谢谢

var datajson = [ {
country : "china",
total : 20,
total1 : 10
}, {
country : "India",
total : 40,
total1 : 20
}, {
country : "aus",
total : 10,
total1 : 30
}, {
country : "nxz",
total : 50,
total1 : 40
}

    ];

    var xvalue;
    var yvalue = [];
    var i = 0;

    var obj = datajson[0]
    for ( var key in obj) {
        if (xvalue === undefined)
            xvalue = key;
        else {
            yvalue[i] = key;
            i++;
        }

    }



    var chart = c3.generate({
        bindto : '#chart',
        data : {
            json : datajson,

            type : 'bar',
            keys : {
                x : xvalue, // it's possible to specify 'x' when category axis
                value : [yvalue[0],yvalue[1]],
            },
            groups : [ [yvalue[0],yvalue[1]] ]
        },
        bar : {
            width : {
                ratio : 0.3
            // this makes bar width 50% of length between ticks
            }
        },
        axis : {
            x : {
                type : 'category'

        },


        }
    });

【问题讨论】:

    标签: c3


    【解决方案1】:

    您的问题似乎有大部分答案(您已经从对象属性生成 yvalue 数组)。

    您不必一一指定元素,而是直接传入数组即可。

    groups: [yvalue]
    

    【讨论】:

    • 我可以对数据进行分组,但条形图没有堆叠,它们是重叠的。有什么解决办法吗?
    • 你能用小提琴发布你的问题吗?更好的是,您可能想将其作为问题发布 - 更多人会参与其中。
    • 感谢您的回复,我想问题出在我使用的旧版本的 c3 js 文件上。这是 0.4.10 中的一个错误,并在 0.4.11 中得到解决
    猜你喜欢
    • 2017-02-23
    • 1970-01-01
    • 2017-06-05
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 2012-11-09
    相关资源
    最近更新 更多