Estoy usando el plugin Flot para tener un gráfico de líneas con líneas (sales y purchases) como en este ejemplo pero la data está en una base de datos MySQL y las obtengo vía AJAX, tengo esto:
HTML:
<div id="graph" class="demo-placeholder"></div>
PHP:
sales.php
<?php
$sql = "SELECT * from sales where YEAR(date)='2013'";
$res = mysql_query($sql);
$return = [];
while($row = mysql_fetch_array($res)){
$return[] = [strtotime($row['date'])*1000,$row['amount']];
}
echo json_encode(array("label"=>"Sales","data"=>$return));
?>
purchases.php
<?php
$sql = "SELECT * from purchases where YEAR(date)='2013'";
$res = mysql_query($sql);
$return = [];
while($row = mysql_fetch_array($res)){
$return[] = [strtotime($row['date'])*1000,$row['amount']];
}
echo json_encode(array("label"=>"Purchases","data"=>$return));
?>
Y en mi código JS obtengo esta información vía AJAX y lo pongo en un gráfico Flot habilitando tooltip dentro de una función llamada show_chart invocada dentro del success de AJAX:
function show_chart(labell,dataa) {
var dataset = [{label: labell,data: dataa}];
var chart_plot_01_settings = {
series: {
lines: {
show: true,
fill: true
},
splines: {
show: false,
tension: 0.4,
lineWidth: 1,
fill: 0.4
},
points: {
radius: 3,
show: true
},
shadowSize: 2
},
grid: {
verticalLines: true,
hoverable: true,
clickable: true,
tickColor: "#d5d5d5",
borderWidth: 1,
color: '#717171'
},
colors: ["rgba(38, 185, 154, 0.38)", "rgba(3, 88, 106, 0.38)"],
xaxis: {
tickColor: "rgba(51, 51, 51, 0.06)",
mode: "time",
tickSize: [1, "month"],
//tickLength: 10,
axisLabel: "Date",
axisLabelUseCanvas: true,
axisLabelFontSizePixels: 12,
axisLabelFontFamily: 'Verdana, Arial',
axisLabelPadding: 10
},
yaxis: {
ticks: 8,
axisLabel: "Amount",
tickColor: "rgba(51, 51, 51, 0.06)",
},
tooltip: true,
}
$(document).ready(function () {
$.plot($("#graph"), dataset, chart_plot_01_settings);
//Tooltip
$("<div id='tooltip'></div>").css({
position: "absolute",
display: "none",
border: "1px solid #fdd",
padding: "2px",
"background-color": "#fee",
opacity: 0.80
}).appendTo("body");
$("#graph").bind("plothover", function (event, pos, item) {
if (item) {
var x = item.datapoint[0],
y = item.datapoint[1];
var date = new Date(x);
$("#tooltip").html("Date: " + ('0' + (date.getMonth()+1)).slice(-2) + '/'+ date.getFullYear()+ " | Amount: "+y).css({top: item.pageY+5, left: item.pageX+5}).fadeIn(200);
} else {
$("#tooltip").hide();
}
});
});
} //show chart
var purchases,sales;
$.ajax({url: "purchases.php",
type: "GET",
dataType: "json",
success: function(resp)
{
purchases = resp.data;
var label1 = resp.label;
show_chart(label1,purchases);
}
});
$.ajax({
url: "sales.php",
type: "GET",
dataType: "json",
success: function(resp)
{
sales = resp.data;
var label2 = resp.label;
show_chart(label2,sales);
}
});
El problema es que el gráfico sólo muestra una línea ya sea de Sales o Purchases pero yo deseo que muestre ambas líneas en el gráfico como se muestra en el ejemplo.
¿Cómo puedo modificarlo?
Me gustaría su ayuda.