0
我正在使用MVC4和jQPlot一起構建多筆DateTime X軸圖。MVC jQPlot JSON數據渲染器不繪圖
I'using下面的代碼在控制器通過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);
}
這是我的觀點:
<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>
這是我從瀏覽器中得到(得到它通過瀏覽器上的F12):
[{"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}]
當然,jqPlot的數據是不可讀的,但我嘗試使用廣告不同的類,全部轉換爲字符串數組作爲Json數據,並且它們都不起作用。
如何在Controller中構建數據以便jQPlot可以理解DateTime和Value。
感謝您的任何幫助。
不完全是你的代碼,但它對我有用。謝謝您的幫助。 – Mendes