I´m using MVC4 together with jQPlot to built multiple pen DateTime X-Axis graphs.
I´using the following code at the controller to retrive server data through ajax:
public ActionResult GetPlotData()
{
List<Tuple<DateTime, decimal>> plotData = new List<Tuple<DateTime, decimal>>();
///
/// Plot data
///
plotData.Add(new Tuple<DateTime, decimal>(DateTime.Now, 10));
plotData.Add(new Tuple<DateTime, decimal>(DateTime.Now.AddHours(-1), 20));
plotData.Add(new Tuple<DateTime, decimal>(DateTime.Now.AddHours(-1), 30));
plotData.Add(new Tuple<DateTime, decimal>(DateTime.Now.AddHours(-1), 40));
plotData.Add(new Tuple<DateTime, decimal>(DateTime.Now.AddHours(-1), 50));
return Json(plotData, JsonRequestBehavior.AllowGet);
}
And here is my view:
<script type="text/javascript" src="~/Scripts/jqplot/jquery.jqplot.min.js"></script>
<script type="text/javascript" src="~/Scripts/jqplot/plugins/jqplot.highlighter.min.js"></script>
<script type="text/javascript" src="~/Scripts/jqplot/plugins/jqplot.cursor.min.js"></script>
<script type="text/javascript" src="~/Scripts/jqplot/plugins/jqplot.dateAxisRenderer.min.js"></script>
<script type="text/javascript" src="~/Scripts/jqplot/plugins/jqplot.json2.min.js"></script>
<div id="chart"></div>
<script type="text/javascript">
$(document).ready(function () {
var ajaxDataRenderer = function (url, plot, options) {
var ret = null;
$.ajax({
// have to use synchronous here, else the function
// will return before the data is fetched
async: false,
url: url,
dataType: "json",
success: function (data) {
ret = data;
}
});
return ret;
};
var jsonurl = '@Url.Action("GetPlotData", "UserPDataTrend")';
var plot1 = $.jqplot('chart', jsonurl, {
title: 'Tendência Dados de Processo',
dataRenderer: ajaxDataRenderer,
axes: {
xaxis: {
renderer: $.jqplot.DateAxisRenderer,
tickOptions: {
formatString: '%b %#d'
}
},
yaxis: {
tickOptions: {
formatString: '$%.2f'
}
}
},
highlighter: {
show: true,
sizeAdjust: 7.5
},
cursor: {
show: false
}
});
});
</script>
This is what I get from browser (got it through F12 on browser):
[{"Item1":"\/Date(1386710588647)\/","Item2":10},{"Item1":"\/Date(1386706988649)\/","Item2":20},{"Item1":"\/Date(1386706988649)\/","Item2":30},{"Item1":"\/Date(1386706988649)\/","Item2":40},{"Item1":"\/Date(1386706988649)\/","Item2":50}]
Of course the data is unreadable for jqPlot, but I´ve tries using a different class, converted all to string array as Json data and none of them worked.
How can I build the data in Controller so that jQPlot can understand a DateTime and a Value.
Thanks for any help.