Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bug with highcharts pie-basic percent

I'm using the code of demo pie-basic (Fiddle) with the following values :

series: [{
    type: 'pie',
    name: 'Browser share',
    data: [
        ['Firefox',   34],
        ['IE',       33],         
        ['Safari',    26],
        ['Opera',     7],
    ]
}]

and the problem is that it appears as 7.000000000000001% instead of 7%.

How can I get a rounded value to appear?

enter image description here

like image 232
user2393818 Avatar asked May 17 '13 12:05

user2393818


1 Answers

Well, I can't tell you how to prevent the floating point error from occurring in the first place, but I can tell you how to hide it from the user.

You can simply use Math.round() in your formatter function, as follows:

formatter: function() {
    return '<b>'+ this.point.name +'</b>: '+ Math.round(this.percentage) +' %';
}

You already have a formatter function; I've just added Math.round() to it.

I've updated your fiddle to demonstrate: http://jsfiddle.net/A2cVe/1/

[EDIT] You mention that the tooltip was also showing the error. There is also a separate formatter function for that. I've updated the fiddle again with both formatter functions now edited to show the expected value: http://jsfiddle.net/A2cVe/2/

like image 196
Spudley Avatar answered Oct 15 '22 01:10

Spudley