简体   繁体   English

c3js中多类别图表的配置数据输入

[英]Config data input for multiple categories chart in c3js

I'm doing a line / column chart form, as designed in the image below.我正在制作折线图/柱形图表格,如下图所示。

The first level of categorization is grouped by month-year, the next level is name.第一级分类按月-年分组,下一级是名称。

I do not know how to configure the input data for the chart, and format X-axis tick values.我不知道如何配置图表的输入数据,以及如何格式化 X 轴刻度值。

在此处输入图片说明

I already tried several times but still not worked.我已经尝试了几次,但仍然没有奏效。

let x = ['x', '2020-07-01', '2020-07-01', '2020-08-01', '2020-08-01', '2020-09-01', '2020-09-01'];
let data1 = ['p1', 100, 200, 300, 450, 400, 500]; // 100: p1-Peter, 200: p1-Mary.......
let data2 = ['p2', 300, 400, 450, 390, 760, 800];
let ResponsibleEmployees = ["Peter", "Mary"]

        this.chart = c3.generate({

            size: {
                width: 700
            },
            data: {
                x: 'x',
                columns: [
                    x,
                    p1,
                    p2],
                type: "bar"
            },
            axis: {
                x: {
                    type: 'timeseries',
                    tick: {
                        culling: false,
                        count: 11,
                        format: function (d) {
                            let aaa = `${d.getFullYear()}-${d.getMonth() + 1}-01`;
                            
                            if (x.indexOf(aaa)) return `${d.getFullYear()}-${d.getMonth() + 1}`;
                           
                            else return ResponsibleEmployees.map(x=>x);
                        }
                    }
                }
            }
        });

Unfortunately c3 doesn't naturally do hierarchical axes.不幸的是, c3 不会自然地做分层轴。 The best you can hope for is mimicking them though the tick formatting as you've tried.您可以期望的最好方法是通过您尝试过的刻度格式来模仿它们。 Your example would seem to work best as a category type rather than a time-series though.不过,您的示例似乎最适合作为类别类型而不是时间序列。 I can't see how to get the data underneath and in between the individual ticks though, even with dummy data points.即使使用虚拟数据点,我也看不到如何在各个刻度之间获取数据。

//let x = ['x', '2020-07-01', '2020-07-01', '2020-08-01', '2020-08-01', '2020-09-01', '2020-09-01'];
var data1 = ['p1', 100, 200, 300, 450, 400, 500]; // 100: p1-Peter, 200: p1-Mary.......
var data2 = ['p2', 300, 400, 450, 390, 760, 800];
var ResponsibleEmployees = ["Peter", "Mary"]

        var chart = c3.generate({

            size: {
                width: 700
            },
            data: {
                //x: 'x',
                columns: [
                    //x,
                    data1,
                    data2],
                type: "bar"
            },
            axis: {
                x: {
                    type: 'category',
                    categories: ['2020-07-01', '2020-07-01', '2020-08-01', '2020-08-01', '2020-09-01', '2020-09-01'],
                    tick: {
                        culling: false,
                        
                        format: function (d) {
                            var i = d % 2;
                            var val = ((i === 0) ? this.api.categories()[Math.floor(d)] + " " : "...") + ResponsibleEmployees[i];
                            return val;
                        }

                    }
                }
            }
        });

https://jsfiddle.net/7rs186fw/ https://jsfiddle.net/7rs186fw/

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

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