I have an array with random numbers, and i want to clasify the data from this array in good (color green bar) or bad (color red bar) in the flot chart. Here's my code but it doesn't work. Can anyone please help me?
<script>
var Max = 20;
var Data = [];
for (var i=0;i<Max;i++){
Data [i]=Math.floor((Math.random()*3000)+1500);
}
var myData = [];
for(var i=0;i<Max;i++){
if(Data[i]<2000||Data[i]>2500){
myData [i]= {color: "red", data:[Data[i],i]};
}
else {
myData [i]= {color: "green", data:[Data[i],i]};
}
};
var options = {
bars:{
show: true,
horizontal: true,
},
yaxis: {
tickLength:0,
ticks:[]
},
xaxis: {
tickLength:0,
ticks:[]
},
grid: {
clickable: true,
hoverable: true,
borderWidth: 2
}
}
$(document).ready(function(){
$.plot($("#mycanvas"),[{data:myData}],options);
});
</script>
When you're building your series, you need to make the data
object an array of values:
data: [[i, Data[i]]]
From the Flot API Data Series section (notice the format of the data object):
You don't have to specify any of them except the data, the rest are options that will get default values. Typically you'd only specify label and data, like this:
{ label: "y = 3", data: [[0, 3], [10, 3]] }
This JSFiddle demonstrates how to build the data array with your example code.