简体   繁体   English

c3.js中具有多个XY折线图的大量或未知数据阵列

[英]Large or unknown number of data arrays with Multiple XY Line Chart in c3.js

I've been using c3.js for some time now, but recently I stumbled across a problem. 我已经使用c3.js已有一段时间了,但是最近我偶然发现了一个问题。 I'm using the Multiple XY Line Chart feature and my data set is huge. 我正在使用“多XY折线图”功能,我的数据集很大。 Is there a way to do the data-axis binding (the xs part) automatically instead of hard coding it? 有没有一种方法可以自动进行数据轴绑定(xs部分),而不是对其进行硬编码?

var chart = c3.generate({
        data: {
            xs: {
                'data1': 'x1',
                'data2': 'x2',
                .
                .
                .
                'datan': 'xn',
            },
            columns: [
                ['x1', 10, 30, 45, 50, 70, 100],
                ['x2', 30, 50, 75, 100, 120],
                .
                .
                .
                ['xn', 45, 60, 80, 120, 130],
                ['data1', 30, 200, 100, 400, 150, 250],
                ['data2', 20, 180, 240, 100, 190],
                .
                .
                .
                ['datan', 10, 150, 220, 160, 300]
            ]
        }
    });

You just need to build a function that matches the syntax of your data series titles to the relevant x series 您只需要构建一个将数据系列标题的语法与相关x系列匹配的函数

Assuming all the x1 / data1 etc stuff is in an object (allmydata) before you generate the chart you could do: 假设在生成图表之前,所有x1 / data1等东西都在对象(allmydata)中,则可以执行以下操作:

var allmydata = [
                ['x1', 10, 30, 45, 50, 70, 100],
                ['x2', 30, 50, 75, 100, 120],
                ['xn', 45, 60, 80, 120, 130],
                ['data1', 30, 200, 100, 400, 150, 250],
                ['data2', 20, 180, 240, 100, 190],
                ['datan', 10, 150, 220, 160, 300]
    ];

var match = function (dataStr) {
    return dataStr.startsWith("data") ? dataStr.replace ("data", "x") : undefined;
};

var xs = {};
allmydata.forEach (function (series) {
    var seriesName = series[0];
    var xsName = match (seriesName);
    if (xsName) {
        xs[seriesName] = xsName;
    }
});

var chart = c3.generate({
        data: {
            xs: xs,
            columns: allmydata
        }
});

http://jsfiddle.net/0f38ffsy/1/ http://jsfiddle.net/0f38ffsy/1/

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

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