2013-10-17 40 views
4

我在控制器類中的方法返回JSON數據:如何顯示JQPLOT圖,而長文本

public ActionResult ChartDataJSON() 
{ 
    Chart chart = new Chart(); 
    DataSet ds = dbLayer.GetChartData(); 
    DataTable dtChartData = ds.Tables[0]; 
    List<jqplotModel> chartData = new List<jqplotModel>(); 

    if (dtChartData .Rows.Count != 0) 
    { 
     foreach (DataRow row in dtChartData .Rows) 
     { 
      chartData.Add(new jqplotModel{ Date = DateTime.Parse(@row["Date"].ToString()), Demand= Convert.ToDouble(@row["Demand"].ToString()), Supply= Convert.ToDouble(@row["Supply"].ToString()) }); 
     } 
    } 
    return Json(chartData, JsonRequestBehavior.AllowGet); 
} 

有誰知道我可以在我的腳本中使用它。我試着用這些線,但它沒有顯示當我運行這個我沒有看到圖表的圖表

<script type="text/javascript"> 
    $(document).ready(function() { 
     var types = ['Demand', 'Supply'];    

     var rawData = 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; 
     }; 

     // The url for our json data 
     var jsonurl = "/ChartController/ChartDataJSON"; 
     var plotData = [] 

     for (var i = 0; i < rawData.length; i++) { 
      //Parse the date. 
      var date = new Date(+rawData[i].Date.match(/\d+/)); 

      plotData[i] = [date, rawData[i].Demand]; 
     } 

     var plotData2 = [] 

     for (var i = 0; i < rawData.length; i++) { 
      //Parse the date. 
      var date = new Date(+rawData[i].Date.match(/\d+/)); 

      plotData2[i] = [date, rawData[i].Supply]; 
     } 

     var plot1 = $.jqplot('chart1', [plotData, plotData2], { 
      height: 300, 
      width: 300, 
      title: 'Demand Supply', 
      dataRenderer: rawData, 
      dataRendererOptions: { 
       unusedOptionalUrl: jsonurl 
      }, 
      series: [ 
        {}, 
        { yaxis: 'y2axis' } 
      ], 
      axes: { 
       xaxis: { 
        renderer: $.jqplot.DateAxisRenderer, 
        tickOptions: { formatString: '%#I %p' }, 
        label: "Date", 
        tickInterval: '4 hour' 
       }, 
       yaxis: { 
        label: "Demand", 
        labelRenderer: $.jqplot.CanvasAxisLabelRenderer 

       }, 
       y2axis: { 
        label: "Supply", 
        labelRenderer: $.jqplot.CanvasAxisLabelRenderer 
       } 
      }, 
      highlighter: { 
       show: true, 
       sizeAdjust: 1 
      }, 
      cursor: { 
       show: false 
      }, 
      legend: { 
       show: true, 
       labels: types, 
       location: 'ne' 
      } 
     }); 

     $(window).bind('resize', function (event, ui) { 
      if (plot1) { 
       plot1.replot(); 
      } 
     }); 
    }); 
</script> 

,並在頁面顯示長文本是這樣的:

[{」需求「:4422.45,」供應「:17660,」日期「:」/ Date(1236504600000)/「,」DateString「:」3 PM「},{」Demand「:5019.27, :「/ Date(1236508200000)/」,「DateString」:「4 PM」},{「Demand」:5030.35,「Supply」:19917,「Date」:「/ Date(1236511800000)/」,「DateString」 「5PM」},{「Demand」:5172.35,「Supply」:23597,「Date」:「/ Date(1236515400000)/」,「DateString」:「6 PM」},{「Demand」:5656.51,供應「:21572,」日期「:」/ Date(1236519000000)/「,」DateString「:」7 PM「},{」Demand「:4622.88,」Supply「:7794, :「/ Date(1236522600000)/」,「DateString」:「8 PM」},{「Demand」:3116.21,「Supply」:3427,「Date」:「/ Date(1236526200000)/」,「DateString」 「9PM」},{「Demand」:1588.83,「Supply」:1883,「Date」:「/ Date(1236529800000)/」,「DateString」:「10 PM」},{「Demand」:1394.15,供應「:1403,」日期「:」/ Date(1236533400000)/「,」DateString「:」11 PM「},{」Demand「:1321.76,」Supply「:3755,」Date「:」/ Date(1236537000000 )/「DateString」:「12 AM」},{「Demand」:1130.98,「Supply」:3474,「Date」:「/ Date(1236540600000)/」,「DateString」 {「Demand」:1277.1,「Supply」:1072,「Date」:「/ Date(1236544200000)/」,「DateString」:「2 AM」},{「Demand」:1214.68,「Supply」:1025,日期「:」/ Date(1236547800000)/「,」DateString「:」3 AM「},{」Demand「:2117.91,」Supply「:1198,」Date「:」/ Date 「:」4 AM「},{」Demand「:1658.97,」Supply「:1485,」Date「:」/ Date(1236555000000)/「,」DateString「:」5 AM「},{」Demand「:1997.36 ,「Supply」:3126,「Date」:「/ Date(1236558600000)/」,「DateString」:「6 AM」},{「Demand」:2147.37,「Supply」:4785,「Date」:「 (1236562200000)/「,」DateString「:」7 AM「},{」Demand「:2114.13,」Supply「:5 「Date」:「/ Date(1236565800000)/」,「DateString」:「8 AM」},{「Demand」:2389.84,「Supply」:5264,「Date」:「/ Date(1236569400000)/」 「DateString」:「9 AM」},{「Demand」:2240.77,「Supply」:5526,「Date」:「/ Date(1236573000000)/」,「DateString」:「10 AM」}, 「:1802.43,」Supply「:4530,」Date「:」/ Date(1236576600000)/「,」DateString「:」11 AM「},{」Demand「:1929.71,」Supply「:6618,」Date「 「/ Date(1236580200000)/」,「DateString」:「12 PM」},{「Demand」:545.65,「Supply」:2767,「Date」:「/ Date(1236583800000)/」,「DateString」 1 PM 「},{」 需求 「:0,」 供應 「:1,」 日期 「:」/日期(1236587400000)/」, 「DateString」: 「14:00」}]

任何人可以幫助我弄清楚問題以及我在做什麼錯了?

編輯: 請注意,數據將是動態的,它將來自數據庫,並通過控制器類中的編碼將它返回到視圖的JSON腳本。請建議如何在jqPlot中傳遞/使用JSON對象(來自ChartDataJSON()方法)。

+0

我不知道答案,但你有沒有看到DotNet Highcharts MVC? http://dotnethighcharts.codeplex.com/ – WannaCSharp

回答

3

好了,這裏就是我的回答。

我的控制器如下:

public class jqPlotController : Controller 
{  
    public ActionResult ChartDataJSON() 
    { 
     var chartData = new List<jqplotModel>(); 

     var point1 = new jqplotModel { Date = DateTime.Now.Date.ToString("yyyy-MM-dd h:mmtt"), Demand = Convert.ToDouble(1), Supply = Convert.ToDouble(3) }; 
     var point2 = new jqplotModel { Date = DateTime.Now.AddDays(10).Date.ToString("yyyy-MM-dd h:mmtt"), Demand = Convert.ToDouble(2), Supply = Convert.ToDouble(4) }; 
     var point3 = new jqplotModel { Date = DateTime.Now.AddDays(31).Date.ToString("yyyy-MM-dd h:mmtt"), Demand = Convert.ToDouble(6), Supply = Convert.ToDouble(6) }; 
     var point4 = new jqplotModel { Date = DateTime.Now.AddDays(106).Date.ToString("yyyy-MM-dd h:mmtt"), Demand = Convert.ToDouble(4), Supply = Convert.ToDouble(2) }; 
     chartData.Add(point1); 
     chartData.Add(point2); 
     chartData.Add(point3); 
     chartData.Add(point4); 

     return Json(chartData, JsonRequestBehavior.AllowGet); 
    } 

    // 
    // GET: /jqPlot/ 

    public ActionResult Index() 
    { 
     return View(); 
    } 
} 

和模型:

public class jqplotModel 
{ 
public string Date { get; set; } 
public double Demand { get; set; } 
public double Supply { get; set; } 
} 

我硬編碼的(非常!)簡單的ChartDataJSON方法的數據集。重構代碼以相似的格式輸出數據對你來說將是非常微不足道的。

由於我是jqPlot的新手,我花了一段時間才弄清楚如何將DateTime對象傳遞給此JavaScript庫。每當我嘗試jqPlot給了我相當神祕的時間相關的錯誤信息。我找到的解決方案是將其格式化爲jqPlot可以理解的日期時間,例如, '2008-09-30 4:00 PM'(參見示例here) - 我相信這對於jqPlot處理日期時感到困惑的其他人很有用!

的觀點如下所示:

<script src="@Url.Content("Scripts/jquery-1.8.2.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("Scripts/jqPlot/jquery.jqplot.js")" type="text/javascript"></script> 
<script src="@Url.Content("Scripts/jqPlot/plugins/jqplot.barRenderer.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("Scripts/jqPlot/plugins/jqplot.categoryAxisRenderer.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("Scripts/jqPlot/plugins/jqplot.dateAxisRenderer.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("Scripts/jqPlot/plugins/jqplot.pointLabels.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("Scripts/jqPlot/plugins/jqplot.canvasAxisTickRenderer.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("Scripts/jqPlot/plugins/jqplot.highlighter.min.js")" type="text/javascript"></script> 

<script type="text/javascript"> 
    $(document).ready(function() { 
     // The url for our json data 
     var url = '@Url.Action("ChartDataJSON", "jqPlot")'; 

     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; 
      } 
     }); 

     var demands = []; 
     var supplys = []; 

     for (i = 0; i < ret.length; i++) { 
      // Save the data to the relevant array. Note how date at the zeroth position (i.e. x-axis) is common for both demand and supply arrays. 
      demands.push([ret[i].Date, ret[i].Demand]); 
      supplys.push([ret[i].Date, ret[i].Supply]); 
     } 

     var plot1 = $.jqplot('chart1', [demands, supplys], { 
      title: "Demand Supply", 
      axes: { 
       xaxis: { 
        renderer: $.jqplot.DateAxisRenderer, 
        tickOptions: { 
         formatString: '%d/%m/%Y' 
        }, 

        label: 'Date' 
       }, 
       yaxis: { 
        label: 'Demand' 
       }, 
       y2axis: { 
        label: 'Supply' 
       } 
      }, 
      series: [ 
       { yaxis: 'yaxis', label: 'demands' }, 
       { yaxis: 'y2axis', label: 'supplys' } 
      ], 
      highlighter: { 
       show: true, 
       sizeAdjust: 1 
      }, 
      cursor: { 
       show: false 
      } 
     }); 
    }); 
</script> 

@{ 
    ViewBag.Title = "jQPlot Demo"; 
} 

<h2>@ViewBag.Title</h2> 
<div id="chart1" style="height: 400px; width: 600px;"></div> 

注意,在我的解決方案我不使用datarender選項。相反,我使用jquery ajax調用來檢索數據,然後爲Demand和Supply數據創建兩個單獨的數組。這些數組中的每一個在x軸上都有日期,然後在y軸上有它們各自的值(因此日期顯然對於兩個數組都是通用的)。

一旦我有這個數據,那麼我通過jqPlot繪製它,這會導致如下圖:

Graph produced when code is run

工作仍然需要做改進的軸和標籤的蜱,但希望這是你想要的那種圖。如果沒有,那麼對我來說這肯定是一個很好的學習任務,我希望別人會覺得這很有用。

+0

Hello Fresh,我在你的應用程序中運行你的代碼,它仍然給我在網頁上的文字[{「Date」:「2013-10-21 12:00 AM」,「Demand」:1,「Supply」 :3},{「Date」:「2013-10-31 12:00 AM」,「Demand」:2,「Supply」:4},{「Date」:「2013-11-21 12:00 AM」,「需求「:6,」供應「:6},{」日期「:」2014-02-04 12:00 AM「,」需求「:4,」供應「:2}] 我複製了你的代碼,因爲它是沒有變化但仍然看不到圖形。你有什麼想法爲什麼它沒有顯示圖表。謝謝你的努力,感謝它! – user1254053

+0

檢查瀏覽器調試控制檯是否有錯誤。仔細檢查是否正確引用了必要的jquery和jqPlot庫(即js文件)。 –

+0

我檢查了所有的js文件,它們都很好。事實上,使用硬代碼數據它工作正常,但是當我更改代碼以從JSON獲取數據時,圖表不會呈現:( – user1254053

1

我想這應該解決您的問題:JsFiddle link

  • 我宣佈rawData作爲將包含ajaxreply JSON對象變量。那麼我已經使用下面的變量來繪製圖表。

  • 在你的代碼中,我觀察到你已經向圖中提供了數據,並且你還在裏面設置了dataRenderer。那是不正確的。 DataRenderer examples

    $(document).ready(function() { 
    

    變種RAWDATA = [{ 「需求」:4422.45, 「供應」:17660, 「日期」: 「/日期(1236504600000)/」, 「DateString」: 「15:00」},{」 Demand「:5019.27,」Supply「:20699,」Date「:」/ Date(1236508200000)/「,」DateString「:」4 PM「},{」Demand「:5030。35,「Supply」:19917,「Date」:「/ Date(1236511800000)/」,「DateString」:「5 PM」},{「Demand」:5172.35,「Supply」:23597,「Date」日期(1236515400000)/「,」DateString「:」6 PM「},{」Demand「:5656.51,」Supply「:21572,」Date「:」/ Date(1236519000000)/「,」DateString「 「},{」Demand「:4622.88,」Supply「:7794,」Date「:」/ Date(1236522600000)/「,」DateString「:」8 PM「},{」Demand「:3116.21,」Supply「 3427「Date」:「/ Date(1236526200000)/」,「DateString」:「9 PM」},{「Demand」:1588.83,「Supply」:1883,「Date」:「/ Date(1236529800000)/」 ,「DateString」:「10 PM」},{「Demand」:1394.15,「Supply」:1403,「Date」:「/ Date(1236533400000)/」,「DateString」:「11 PM」}, 「:1321.76,」Supply「:3755,」Date「:」/ Date(1236537000000)/「,」DateString「:」12 AM「},{」Demand「:1130.98,」Supply「:3474,」Date「 「/ Date(1236540600000)/」,「DateString」:「1 AM」},{「Demand」:1277.1,「Supply」:1072,「Date」:「/ Date(1236544200000)/」,「DateString」 「供應」:1025,「日期」:「/ Date(1236547800000)/」,「DateString」:「3 AM」},{「Demand」:2117.91,「Supply 「:1198,」Date「:」/ Date(1236551400000)/「,」DateString「:」4 AM「},{」需求「:1658.97,」供應「:1485,」日期「:」/日期(1236555000000)/「,」日期字符串「:」5AM「,{」需求「:1997.36,」供應「:3126, :「/ Date(1236558600000)/」,「DateString」:「6 AM」},{「Demand」:2147.37,「Supply」:4785,「Date」:「/ Date(1236562200000)/」,「DateString」 「7 AM」},{「Demand」:2114.13,「Supply」:5268,「Date」:「/ Date(1236565800000)/」,「DateString」:「8 AM」},{「Demand」:2389.84,日期「(1236569400000)/」,「DateString」:「9 AM」},{「Demand」:2240.77,「Supply」:5526,「Date」:「/ Date(1236573000000 )/「,」DateString「:」10 AM「},{」Demand「:1802.43,」Supply「:4530,」Date「:」/ Date(1236576600000)/「,」DateString「 {「Demand」:1929.71,「Supply」:6618,「Date」:「/ Date(1236580200000)/」,「DateString」:「12 PM」},{「Demand」:545.65,「Supply」:2767,日期「:」/ Date(1236583800000)/「,」DateString「:」1 PM「},{」Demand「:0,」Supply「:1,」Date「:」/ Date(1236587400000)/「 「:」2 PM「}];

    var types = ['Demand', 'Supply'];    
    
        var plotData = [] 
    
        for (var i = 0; i < rawData.length; i++) { 
         //Parse the date. 
         var date = new Date(+rawData[i].Date.match(/\d+/)); 
    
         plotData[i] = [date, rawData[i].Demand]; 
        } 
    
        var plotData2 = [] 
    
        for (var i = 0; i < rawData.length; i++) { 
         //Parse the date. 
         var date = new Date(+rawData[i].Date.match(/\d+/)); 
    
         plotData2[i] = [date, rawData[i].Supply]; 
        } 
    
        var plot1 = $.jqplot('chart1', [plotData, plotData2], { 
         height: 300, 
         width: 300, 
         title: 'Demand Supply', 
         //dataRenderer: rawData, 
         //dataRendererOptions: { 
         // unusedOptionalUrl: jsonurl 
         //}, 
         series: [ 
           {}, 
           { yaxis: 'y2axis' } 
         ], 
         axes: { 
          xaxis: { 
           renderer: $.jqplot.DateAxisRenderer, 
           tickOptions: { formatString: '%#I %p' }, 
           label: "Date" 
           //,tickInterval: '4 Hr' 
          }, 
          yaxis: { 
           label: "Demand" 
           //,labelRenderer: $.jqplot.CanvasAxisLabelRenderer 
    
          }, 
          y2axis: { 
           label: "Supply" 
           //,labelRenderer: $.jqplot.CanvasAxisLabelRenderer 
          } 
         }, 
         highlighter: { 
          show: true, 
          sizeAdjust: 1 
         }, 
         cursor: { 
          show: false 
         }, 
         legend: { 
          show: true, 
          labels: types, 
          location: 'ne' 
         } 
        }); 
    
        $(window).bind('resize', function (event, ui) { 
         if (plot1) { 
          plot1.replot(); 
         } 
        }); 
    
    }); 
    
+0

謝謝Gyandeep,但這不是我想要的。我的數據是動態的,會來自數據庫。我已經知道它的工作原理,如果我將數據硬編碼爲變量,任何使用jqplot的人都知道它。我想知道如何將json字符串從控制器類傳遞到視圖。希望你瞭解mvc的概念。 – user1254053

0

作爲@user1254053我也遇到了困難,實施@Fresh的解決方案。 我試圖在我的ChartDataJSON視圖中編寫js代碼,我的意思是在ChartDataJSON()方法的視圖中,我只是在運行視圖時打印出純文本對象。

然後我嘗試在另一個視圖中編寫我的視圖代碼,例如索引視圖或任何其他視圖,只是有一個return(View)聲明並計算出來。

我希望這可以幫助其他人,我希望我不會太遲。

+0

實際上,您究竟遇到了什麼問題?我也許能夠幫助。 –

+0

我想我已經在我的回答中解釋過了。我嘗試了一種方式,失敗了,然後嘗試了另一種方式並取得成功。 我認爲這是@ user1254053遇到的問題,這就是爲什麼我發佈了這個答案。 – gugol