2012-07-15 48 views
0

在我的asp.net mvc 3.0應用程序中,我將繪製一個圖表,其X軸包含日期值,y軸包含每天的視圖。對於圖表繪製,我使用jqplot date-axes,我將它傳遞給json結果。這裏是模型:jqplot和使用asp.net mvc模型呈現

public class MyDateModel 
    { 
     public string Date { get; set; } 
     public int Views { get; set; } 
    } 

,這裏是動作:

public ActionResult Index() 
     { 
      var res = new List<MyDateModel>(); 

      res.Add(new MyDateModel { Date=DateTime.Now.AddDays(-9).ToString("yyyy-MM-dd hh:mm"),Views=10}); 
      res.Add(new MyDateModel { Date = DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd hh:mm"), Views = 3 }); 
      res.Add(new MyDateModel { Date = DateTime.Now.ToString("yyyy-MM-dd hh:mm"), Views = 3 }); 

      return View(res); 
     } 

最後的觀點:

@model IEnumerable<Chartjquery_jqplot.Models.MyDateModel> 
@{ 
    Layout = null; 
} 
<!DOCTYPE html> 
<html> 
<head> 
    <title>Index</title> 
    <link href="../../Content/jquery.jqplot.min.css" rel="stylesheet" type="text/css" /> 
    <script src="../../Scripts/jquery-1.7.2.min.js" type="text/javascript"></script> 
    <script src="../../Scripts/jquery.jqplot.min.js" type="text/javascript"></script> 
    <script src="../../Scripts/jqplot.dateAxisRenderer.min.js" type="text/javascript"></script> 
    <script src="../../Scripts/jqplot.json2.min.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 

      var [email protected](Json.Encode(Model)); 
      var plot2 = $.jqplot('chart2', jsonurl, { 
       title: "AJAX JSON Data Renderer", 
       axes:{xaxis:{renderer:$.jqplot.DateAxisRenderer}} 

      }); 




     }); 
    </script> 
</head> 
<body> 
    <div> 
     @Html.Raw(Json.Encode(Model)) 
     <div id="chart2" style="height: 300px; width: 500px;"> 
     </div> 
    </div> 
</body> 
</html> 

,但它不顯示頁面上的任何東西。

你能幫我嗎?

除了JSON結果是:

[{ 「日期」: 「2012-07-06 12:10」, 「看」:10},{ 「日期」:「2012-07-12 12 :10「,」Views「:3},{」Date「:」2012-07-15 12:10「,」Views「:3}]

回答

0

我找到了一種將正確的json數據傳遞給圖表的方法。下面是HTML赫普勒我寫道:

public static MvcHtmlString GetStrippedJson(this HtmlHelper helper,string json) 
     { 
      json=json.Replace("\"Date\":", string.Empty); 

      json=json.Replace("\"Views\":", string.Empty); 

      json=json.Replace('}', ']'); 

      json=json.Replace('{', '['); 

      return MvcHtmlString.Create(json); 
     } 

,但我不知道如何設置打勾根據每日間VAL蜱

0

不知道這是否會有所幫助,但我對傳遞數據的代碼是下面。 @ Model.data是一個列表。

$(document).ready(function() { 
     var data = @Html.Raw(Json.Encode(@Model.data)); 
     //debugger; 
     //alert(data); 
     $.jqplot('chartdiv', data);