-1

我有以下JavaScript显示图表,目前出于测试目的,我正在传递硬编码数组,如下所示:

@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>

但是我如何动态构建上述Line1数组以获得模型值,类似于:

   @foreach (var item in Model) {
      //code goes here,, 
      line1 [1,1] = [item.Date, item.Result]
   }
4

2 回答 2

0

Razor 不能很好地与 javascript 混合,因此这种转换本质上是错误的。我会建议和替代。

变量 line1 的数据是一个二维数组。那么为什么不使用 JSON 动态加载它,而不是转换 Razor 语法。例如,您可能有这样的控制器:

public JsonResult LoadGraphData( ... )
{
     //Populate your two dimensional array here.
     .... 
     return Json( any_two_dim_array , JsonRequestBehavior.AllowGet);
}
于 2012-05-19T09:48:29.670 回答
0

我是否正确地假设您想将数组数组转换为对象数组?

var line2=[];
for (var i in line1) {
  var a=line1[i];
  var b=new Object;
  b.Date=a[0];
  b.Result=a[1];
  line2[i]=b
}

line2 现在包含可以使用 .Date 和 .Result 访问值的对象:

for (item in line2) {
  // do something with item.Date and item.Result
}
于 2012-05-19T04:07:23.750 回答