Reputation: 13
I´m trying to make sinus and cosinus graphs. There are pushed data like this:
data: (function exp() {
var data1 = [],
//i = 0,
x = 0,
y = 0;
for (i = -10; i <= 10; i = i + 0.1) {
//x = i.toFixed(4);
x = i;
y = Math.sin(i);
data1.push({
//x: Math.round(i * 10000)/10000,
x: x,
y: y,
});
if ((y.toFixed(4) == 0) || (y.toFixed(4) == -0)) {
//var x = Math.sin(Math.round(i * 10000)/10000);
//var y = Math.round(Math.sin(i) * 10000)/10000;
addtext(x, y);
}
}
return data1;
})()
},
So there should be x values: -10; -9.9; -9.8, -9.7 and so on...But if you look at graph(http://jsfiddle.net/K5Zhr/4/), there are x values: -10; -9.9; -9.8, and then there is -9.700000000000001 and all next x values are screwed up in same way.
I have tried to use methods .toFixed and .toPrecision, but they (for example) made point[0, sin(0)] to be something like [-0.0000, -0.0000], what is of course bad, becuse it takes x value -0.000000000000187... Could anybody help me to push just data with 1 decimal place?
Upvotes: 1
Views: 272
Reputation: 108537
You shouldn't try to adjust the input x values, that's just how computers store numbers.
To show rounded or floored numbers in the tooltip, you'll need to drop the footerFormat
, headerFormat
, and pointFormat
and take complete control of the tooltip using the formatter option. Something like this:
formatter: function() {
var s = '<b>'+ Highcharts.numberFormat(this.x,1) +'</b>';
$.each(this.points, function(i, point) {
s += '<br/>'+ point.series.name +': '+
Highcharts.numberFormat(this.point.y,4);
});
return s;
}
Should get you want you need. Fiddle here.
Upvotes: 2