简体   繁体   English

带有json数据的c3js多xy折线图

[英]c3js multiple xy line chart with json data

I want to plot multilines in c3js by using the JSON data format.我想使用 JSON 数据格式在 c3js 中绘制多行。

I found an example like this我找到了一个这样的例子

var chart = c3.generate({
data: {
    json: [
        {x: 1, data1: 0},
        {x: 2, data1: 1},
        {x: 3, data1: 2},
        {x: 4, data1: 3},
        {x: 11, data1: 2},
        {x: 12, data1: 0},
        {x: 13, data1: 0},
        {x2: 5, data2: 2},
        {x2: 6, data2: 3},
        {x2: 7, data2: 0},
        {x2: 8, data2: 2}
    ],
    keys: {
        xs: {
            data1: 'x',
            data2: 'x2',
        },
        value: ['data1', 'data2']
    },
    types: {
        'data1': 'line',
        'data2': 'spline'
    }
},
});

but it doesn't work properly.但它不能正常工作。 x for the x-axis, data is for the y-axis. x 为 x 轴,数据为 y 轴。

so I need mixed of these two features: https://c3js.org/samples/data_json.html https://c3js.org/samples/simple_xy_multiple.html所以我需要混合使用这两个功能: https : //c3js.org/samples/data_json.html https://c3js.org/samples/simple_xy_multiple.html

what am I missing?我错过了什么?

It is an open issue ( https://github.com/c3js/c3/issues/1082 ).这是一个悬而未决的问题( https://github.com/c3js/c3/issues/1082 )。 If you are on github you can add your thumbs up to the issue to boost it.如果你在 github 上,你可以为这个问题点赞以提升它。

So you simply have to use the columns format to present your data.因此,您只需使用列格式来呈现您的数据。

However, if you want to keep your data in json as it is, I wrote a simple utility function called jsonToColsForXS that converts them to columns format.但是,如果您想将数据按原样保留在 json 中,我编写了一个名为 jsonToColsForXS 的简单实用程序函数,可将它们转换为列格式。

var jsonToColsForXS = function(data) {
    if(
        data &&
        'data' in data &&
        'json' in data.data &&
        'keys' in data.data &&
        'xs' in data.data.keys
    ) {

        data.data.columns = [];
        var columnsByKey = {};
        for(var key in data.data.keys.xs) {
            var key1 = key;
            var key2 = data.data.keys.xs[key];
            var key1_col = [key1];
            var key2_col = [key2];
            columnsByKey[key1] = key1_col;
            columnsByKey[key2] = key2_col;
            data.data.columns.push(key1_col, key2_col);
        }
        data.data.json.forEach(function(dataPoint) {
            for(var key in dataPoint) {
                if(key in columnsByKey) {
                    columnsByKey[key].push(dataPoint[key]);
                }
            }
        })
    }
    delete data.data.json;
    data.data.xs = data.data.keys.xs;
    delete data.data.keys;
    return data;
}

var chart = c3.generate(jsonToColsForXS({
data: {
    json: [
        {x: 1, data1: 0},
        {x: 2, data1: 1},
        {x: 3, data1: 2},
        {x: 4, data1: 3},
        {x: 11, data1: 2},
        {x: 12, data1: 0},
        {x: 13, data1: 0},
        {x2: 5, data2: 2},
        {x2: 6, data2: 3},
        {x2: 7, data2: 0},
        {x2: 8, data2: 2}
    ],
    keys: {
        xs: {
            'data1': 'x',
            'data2': 'x2',
        },
        value: ['data1', 'data2']
    },
    types: {
        'data1': 'line',
        'data2': 'spline'
    }
},
}));

This works, but when we try to load the graph, it fails ie这有效,但是当我们尝试加载图形时,它失败了,即

chart.load({
json: [
    {x: 1, data1: 0},
    {x: 2, data1: 1},
    {x: 3, data1: 2},
    {x: 4, data1: 3},
    {x: 11, data1: 2},
    {x: 12, data1: 0},
    {x: 13, data1: 0},
    {x2: 5, data2: 2},
    {x2: 6, data2: 3},
    {x2: 7, data2: 0},
    {x2: 8, data2: 2}
],
keys: {
    xs: {
        'data1': 'x',
        'data2': 'x2',
    },
    value: ['data1', 'data2']
},
types: {
    'data1': 'line',
    'data2': 'spline'
}})

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

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