Alireza Fattahi Alireza Fattahi - 7 months ago 26
Javascript Question

Prevent jQuery Flot to convert numbers to decimal

I found that the jquery flot adds extra decimal to numbers, so

12
will be come
12.00
.

Here is a sample:

$(function() {

var data = [ [0, 3], [4, 8], [8, 5], [9, 13] ];

$.plot($("#placeholder"), [data], {
grid: {
hoverable: true,
clickable: true
},
series: { lines: {show: true},
points:{show: true}
}
});

$("<div id='tooltip'></div>").css({
position: "absolute",
display: "none",
border: "1px solid #fdd",
padding: "2px",
"background-color": "#fee",
opacity: 0.80
}).appendTo("body");


$("#placeholder").bind("plothover", function(event, pos, item) {
if (item) {
var x = item.datapoint[0].toFixed(2),
y = item.datapoint[1].toFixed(2);

$("#tooltip").html( "x is: " + x + " -- y is: " + y)
.css({
top: item.pageY + 5,
left: item.pageX + 5
})
.fadeIn(200);
} else {
$("#tooltip").hide();
}
});

});


It shows a chart with tooltip. When you hover the mouse on the points you see

x is: 4.00 -- y is 8.00


http://jsfiddle.net/b22j666L/

How can I fix it ?!

GG. GG.
Answer

Remove .toFixed(2) in your code.

var x = item.datapoint[0];
var y = item.datapoint[1];

DEMO

Comments