我正在使用 ViewModel 创建系列,如下所示:
namespace AESSmart.ViewModels
{
public class HighChartsPoint
{
public double x { set; get; }
public double y { set; get; }
public string color { set; get; }
public string id { set; get; }
public string name { set; get; }
}
public class HighChartsSeries
{
public List<HighChartsPoint> data { set; get; }
public string name { set; get; }
public string type { set; get; }
}
public class HomeIndexViewModel
{
public string HCSeries {get;set;}
public void setChartData()
{
List<HighChartsSeries> allSeries = new List<HighChartsSeries>();
List<HighChartsPoint> allPoint = new List<HighChartsPoint>();
allPoint.Add(new HighChartsPoint { x = 49.9, y = 1 });
allPoint.Add(new HighChartsPoint { x = 71.5, y = 2 });
allPoint.Add(new HighChartsPoint { x = 106.4, y = 3 });
allPoint.Add(new HighChartsPoint { x = 129.2, y = 4 });
allSeries.Add(new HighChartsSeries {
data = new List<HighChartsPoint>(allPoint),
name = "Series 1",
type = "column"
});
JavaScriptSerializer oSerializer = new JavaScriptSerializer();
HCSeries = oSerializer.Serialize(allSeries);
}
}
}
然后在我看来,我是这样设置的series: @Model.HCSeries
:
@section HeadContent {
<script type="text/javascript">
var chart;
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: "container4",
type: "column",
},
series: @Model.HCSeries
});
});
</script>
}
当我运行程序时,它不显示 HighChart。如果我查看视图源,它看起来如下所示:
<script type="text/javascript">
(function ($) { // encapsulate jQuery
var chart;
$(document).ready(function () {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container4',
type: 'column'
},
series: [[{"data":[
{"x":49.9,"y":1,"color":null,"id":null,"name":null},
{"x":71.5,"y":2,"color":null,"id":null,"name":null},
{"x":106.4,"y":3,"color":null,"id":null,"name":null},
{"x":129.2,"y":4,"color":null,"id":null,"name":null}],
"name":"Series 1",
"type":"column"}]]
});
});
})(jQuery);
</script>
如果我手动将数据直接输入到视图中,那么图表会显示。但是,我需要动态创建系列。 我需要做什么来修复我的代码以便图表显示?