c3 icon indicating copy to clipboard operation
c3 copied to clipboard

C3 Chart yAxis show extraneously long decimal points

Open tangentlin opened this issue 9 years ago • 4 comments

To reproduce, use the following snippet

var chart = c3.generate({
    data: {
        columns: [
            ['data1', 0.1, 0.05, -0.3]
        ]
    }
});

setTimeout(function () {
    chart.axis.range({
        min: { y: -0.3 }, max: { y: 0.1 } // -0.6 and -0.7 has the same issue with decimal points
    })
}, 750);

Notice all the numbers have extraneously long numbers after the decimal points.

screen shot 2017-03-28 at 5 54 37 pm

tangentlin avatar Mar 28 '17 21:03 tangentlin

Floating point operations (which is what the axis code is doing interpolating between two values) often show rounding errors, try typing 0.1+0.2 into a javascript console...

Here though, you just need to use an appropriate formatter on the y ticks:

    axis: {
      y: {
        tick: {
          format: d3.format('.2f')
        }
      }
    }

martingraham avatar Mar 29 '17 15:03 martingraham

Thanks, @martingraham Our charts need to display wide range of data, some times the numbers need to be formatted differently depending on the set. I cannot find a way to update tick marks dynamically once the chart is generated, do you happen to know how to update the tick/format function dynamically in C3?

tangentlin avatar Mar 29 '17 22:03 tangentlin

You can write custom method to format number based on set axis : { y : { tick : { format: function(d){return cutShortNumber(d)} } } } cut short the number in that function and return it.

venkatasandeepd avatar Apr 05 '17 08:04 venkatasandeepd

closing in favor of #797

panthony avatar Mar 25 '18 08:03 panthony