2012-05-20 49 views
0

我有以下觀點,即是負責顯示的圖表,目前用於測試目的我傳遞的靜態值數組如下: -我如何填充一個多維數組與我的模型屬性

@model IEnumerable<Medical.Models.Session> 

<script src="../../Scripts/jqplot/jquery.jqplot.min.js" type="text/javascript"></script> 
<script src="../../Scripts/jqplot/jqplot.barRenderer.min.js" type="text/javascript"></script> 
<script src="../../Scripts/jqplot/jqplot.categoryAxisRenderer.min.js" type="text/javascript"></script> 
<script src="../../Scripts/jqplot/jqplot.pointLabels.min.js" type="text/javascript"></script> 
<script src="../../Scripts/jqplot/jqplot.dateAxisRenderer.min.js" type="text/javascript"></script> 
<script src="../../Scripts/jqplot/jqplot.highlighter.min.js" type="text/javascript"></script> 
<script src="../../Scripts/jqplot/jqplot.cursor.min.js" type="text/javascript"></script> 



<p> 
<script type="text/javascript"> 
    $(document).ready(function() { 

     line1 = [['23-May-2008', 578.55], ['20-Jun-2008', 566.5], ['25-Jul-2008', 480.88], 
     ['22-Aug-2008', 509.84], ['26-Sep-2008', 454.13], ['24-Oct-2008', 379.75], 
     ['21-Nov-2008', 303], ['26-Dec-2008', 308.56], ['23-Jan-2009', 299.14], 
     ['20-Feb-2009', 346.51], ['20-Mar-2009', 325.99], ['24-Apr-2009', 386.15], ['01-Apr-2012', 786.15]]; 
     var plot1 = $.jqplot('chart1', [line1], { title: 'Data Point Highlighting', 
      axesDefaults: { pad: 1.2 }, 
      axes: { xaxis: { renderer: $.jqplot.DateAxisRenderer, 
       tickOptions: { 
        formatString: '%d/%b/%Y' 
       } 
      }, 
       yaxis: { tickOptions: { formatString: '%.2f'} } 
      }, 
      highlighter: { show: true, sizeAdjust: 7.5, tooltipLocation: 'nw' 
       //   , formatString: '<b>%s</b>' 
      }, cursor: { show: true, tooltipOffset: 6 } 
     }) 

    }); 

</script> 

<div id = "chart1"> 
</div> 

但是我試圖做的是填充(如上圖所示,而不是靜態值)的模型屬性,其中包含Date & Result的值1號線多維數組,,與此類似: -

@foreach (var item in Model) { 

    //code goes here,, 

    line1 [1,1] = [item.Date , item.Result] // only for describing what i am trying to do 
    } 

- ::: EDITED ::: -是構建模型的操作方法看起來如下: -

public PartialViewResult showpatientsessions(int Medicine, int PatientID) { 
      var m = repository.showpatientsessions(Medicine, PatientID).OrderByDescending(d => d.Date); 
      return PartialView("_showpatientsessions",m); 
     } 

的模型是: -

public partial class Session 
    { 
     public int LabTestID { get; set; } 
     public int VisitID { get; set; } 
     public decimal Result { get; set; } 
     public Nullable<System.DateTime> Date { get; set; } 
     public string Comment { get; set; } 

     public virtual LabTest LabTest { get; set; } 
     public virtual Visit Visit { get; set; } 
    } 
+0

你的模型結構如何?你能發佈你的模型代碼嗎? – rcdmk

+0

感謝您的回覆,,我更新了我的帖子來回答你的問題。 –

回答

0

增加值,你需要一個數組時做這樣的方式,

@{ 
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); 
//.... 
@for (int i=0;i<Model.Count;i++) { 
     var item=Model[i]; 
     //code goes here,, 
     line1 [i,0] = item.Date ; 
     line1[i,1] = item.Result; // only for describing what i am trying to do 
    } 
<script> 

var line1Json = @serializer.Serialize(line1); 
</script> 

} 

line1是你需要convert it to a JavaScript object伺服器端的變數。然後使用lineJson在JavaScript中繪製您的圖表。