从 C3.js 条形图中排除零值

Exclude zero values from a C3.js bar chart

让我们以下面的图表为例http://c3js.org/samples/chart_bar.html 但用以下数据替换列数据:

var chart = c3.generate({
    data: {
        columns: [
            ['data1', 30, 0, 100, 0, 150, 250],
            ['data2', 130, 0, 140, 200, 0, 50]
        ],
        type: 'bar'
    },
    bar: {
        width: {
            ratio: 0.5 // this makes bar width 50% of length between ticks
        }
        // or
        //width: 100 // this makes bar width 100px
    }
});

setTimeout(function () {
    chart.load({
        columns: [
            ['data3', 130, -150, 200, 300, -200, 100]
        ]
    });
}, 1000);

如我们所见,我们有很多白色 space 或零值条,我们如何将其移除并移除白色 space。 (不是隐藏,我知道怎么用CSS隐藏)

Image example, what should be removed

https://github.com/c3js/c3/issues/81 中用谷歌搜索 您需要将 0 替换为 'null' 并添加:

    line: {
     connectNull: true,
    },

var chart = c3.generate({
    data: {
        columns: [
            ['data1', 30, null, 100, null, 150, 250],
            ['data2', 130, null, 140, 200, null, 50]
        ],
        line: {
         connectNull: true,
        },
        type: 'bar'
    },
    bar: {
        width: {
            ratio: 0.5 // this makes bar width 50% of length between ticks
        }
        // or
        //width: 100 // this makes bar width 100px
    }
});

setTimeout(function () {
    chart.load({
        columns: [
            ['data3', 130, -150, 200, 300, -200, 100]
        ]
    });
}, 1000);

只需添加

line: {
     connectNull: true,
    }

在您的 c3.generate 区块中。 它将连接图表上的所有 points/bars/...,其间的值为零。 它适用于 null 以及 0.