简体   繁体   English

我可以使用C3.js制作一个堆叠的条形图,它为不同的列提供不同的数据值吗?

[英]Can I make a stacked bar chart with C3.js which has separate data values for separate columns?

I am attempting to make a bar chart with two columns, each of which is split into two different sections. 我正在尝试制作一个包含两列的条形图,每一列分为两个不同的部分。 The data in the second column is different from the data in the first column. 第二列中的数据与第一列中的数据不同。 This has proven to be a problem in C3.js for me. 对我来说,这已证明是C3.js中的问题。 The way to generate columns is by passing a "columns" array into the data value for the chart. 生成列的方法是将“列”数组传递到图表的数据值中。 The columns array is actually an array of arrays like this: columns数组实际上是这样的数组数组:

columns: [
    ['data1', 50, 100],
    ['data2', 20, 30],
    ['data3', 30, 60],
    ['data4', 20, 20]
]

where each array represents a data value. 其中每个数组代表一个数据值。 The first value in the array is the name of the data value, and the following values are the amount of space that data value will occupy in the corresponding column on the bar chart. 数组中的第一个值是数据值的名称,随后的值是数据值将在条形图的相应列中占据的空间量。 So, for example, the data1 value will be 50 units high in column 1 and 100 units high in column 2. 因此,例如,data1值在第1列中为50个单位高,在第2列中为100个单位高。

The problem is that I want to show data1 and data2 in column 1, but not at all in column 2. And I want to show data3 and data4 in column 2, but not at all in column 1. I tried passing null values instead of integers for the columns values in the arrays for the data I do not want to show, but C3.js still leaves space in the chart as if the values were there and just equal to zero. 问题是我想在第1列中显示data1和data2,但在第2列中完全不显示。我想在第2列中显示data3和data4,但在第1列中根本不显示。我尝试传递空值而不是我不想显示的数据的数组中列值的整数,但是C3.js仍然在图表中留出空间,就好像这些值在那里并且只是零。

Does anyone know if it's possible to do this in C3.js? 有谁知道是否可以在C3.js中做到这一点?

Based on what I understand of reading your problem, if I've misinterpreted it let me know: 根据我对阅读您的问题的了解,如果我对它的理解有误,请通知我:

If you use the groups configuration - http://c3js.org/reference.html#data-groups -, then x-axis space is only reserved per group rather than per data series. 如果您使用组配置-http://c3js.org/reference.html#data-groups- ,则x轴空间仅按组保留,而不按数据系列保留。 So if you then put data series 1 and 3 and data series 2 and 4 in the same groups and null the values you don't want to see, you won't get the 'empty' space issue 因此,如果将数据系列1和3以及数据系列2和4放在同一组中,并且将不需要的值设为空,则不会出现“空”空间问题

var chart = c3.generate({
    data: {
        columns: [
            ['data1', 50, null],
            ['data2', 20, null],
            ['data3', null, 60],
            ['data4', null, 20]
        ],
        type: 'bar',
        groups: [
            ['data1', 'data3'],
            ['data2', 'data4']
        ]
    },
});

http://jsfiddle.net/j7jw4suo/1/ http://jsfiddle.net/j7jw4suo/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM