I’m trying to populate a Flot chart with data from a MySQL query in PHP. I’m sure this is a crappy way to go about things (without Ajax or JSON, neither of which I’m comfortable with yet), but I output the query results in the format Flot needs for its data points into a hidden div on my PHP page, like so:
function get_team_chart($arr) {
$data1 = array();
$data2 = array();
foreach ($arr as $a) {
$data1[] = '[' . $a['week'] . ',' . $a['pts'] . ']';
$data2[] = '[' . $a['week'] . ',' . $a['league_avg'] . ']';
}
$str1 = implode(', ', $data1);
$str2 = implode(', ', $data2);
echo " <div id='series1' style='display:none;'>" . $str1 . "</div>
<div id='series2' style='display:none;'>" . $str2 . "</div>";
}
Then I try to pull that data into my chart in JavaScript like so:
function plot_team_chart() {
var series1 = $('#series1').html();
var series2 = $('#series2').html();
$.plot( $("#team-chart"),
[ series1,
series2],
{ xaxis: {min:0,
max:13},
yaxis: {max:150,
min:0}}
);
}
This doesn’t throw any JS errors at runtime, and the chart shows up with the specified mins and maxes, but no data is plotted. I checked the values of series1 and series2 and they are both without quotes around the numerical values (i.e. they are like [1,120.3], [2,89.0], not like ["1","120.3"], ["2","89.0"]).
This is my first foray into the world of Flot, so please be gentle. Also, I verified that the chart plots fine with hardcoded values.
Thank you.
Got it working. Feel free to respond with how this functionality would be accomplished with Ajax and/or JSON, but here is how I accomplished pulling in data within a hidden div to plot on Flot:
And the modified PHP to accommodate the changes: