Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C3 charts don't want decimal numbers in y axis

As you can see in output I am getting decimal values in y axis. These are number of downloads it cant be 1.5.

I tried giving min value but unable to get the result.

Also on mouse over in grid I am always getting value zero 0(Above microsoft in image). Can I have my custom text there?

function draw_tech_chart(){ 
        var chart = c3.generate({
            bindto: "#tech_chart",
            data: {
                columns: [['Microsoft', 5],['WebApplicationDevelopment', 2],['OpenSource', 2],['Content Management ', 2],['Open Source Middleware', 1],],
                type : 'bar',
                onclick: function (d, i) { console.log("onclick", d, i); },
                onmouseover: function (d, i) { console.log("onmouseover", d, i); },
                onmouseout: function (d, i) { console.log("onmouseout", d, i); }
            },size: {
              height: 250
            }               
        });

enter image description here

like image 762
Rakesh K Avatar asked Jul 29 '15 14:07

Rakesh K


2 Answers

Just format the y ticks to not display the labels

var chart = c3.generate({
    bindto: "#tech_chart",
    ...
    axis: {
        y: {
            tick: {
                format: function (d) {
                    return (parseInt(d) == d) ? d : null;
                }
            }
        }
    }
});

Alternatively, you could also loop through the values, find the max and set the y tick values manually to integers or integer multiples from 0 to max+ (see http://c3js.org/reference.html#axis-y-tick-value)


Fiddle - http://jsfiddle.net/ovvywb5j/

like image 113
potatopeelings Avatar answered Sep 30 '22 18:09

potatopeelings


You just need to manually compute the values on Y axis. You could also format the title of tooltip:

axis: {
    y: {
        tick : {values: [0,1,2,3,4,5]}
    }
}, tooltip: {
    format: {
        title: function (d) { return 'Custom text'; },
    }
}

Fiddle: http://jsfiddle.net/yymcjhgv/

like image 23
user3714582 Avatar answered Sep 30 '22 17:09

user3714582