Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude zero values from a C3.js bar chart

Tags:

c3.js

Let's take for example following chart http://c3js.org/samples/chart_bar.html but replace columns data with the data below:

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);

As we see we have a lot of white space or ZERO value bars, how can we remove it and remove white space. (not hide, I know how to hide it with CSS)

Image example, what should be removed

like image 483
Ursu Alexandr Avatar asked Sep 25 '22 15:09

Ursu Alexandr


1 Answers

as googled in https://github.com/c3js/c3/issues/81 you need replace 0 to 'null' and add this:

    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);
like image 93
nexoma Avatar answered Oct 11 '22 13:10

nexoma