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

Urs*_*ndr 5 c3.js

让我们以下面的图表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);
Run Code Online (Sandbox Code Playgroud)

正如我们所看到的,我们有很多空白或零值条,我们如何删除它并删除空白。(不是隐藏,我知道如何用 CSS 隐藏它)

图片示例,应该删除什么

小智 4

正如在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);
Run Code Online (Sandbox Code Playgroud)