网上关于chart的教程基本相似。我是从数据库中读取数据形成x轴和y轴的数据源。
运行时报错:
@{
IEnumerable<mt4accountcharts> data = (IEnumerable<mt4accountcharts>)ViewData["data"];
}
@{
var myChart = new Chart(width: 600, height: 300)
.AddTitle("账户展示")
.DataBindTable(data, xField: "AccountBlance")
.AddSeries(
xValue: data, xField: "AccountBlance",
yValues: data, yFields: "AccountEquity",
chartType: "Line")
.Write();
}
直接使用数组时则不会出现此类错误。
于是用了一个笨方法把数据分别存到x,y两个数组中,代码如下:
@using mvc3_ra.Models
@{
IEnumerable<chart> data = (IEnumerable<chart>)ViewData["data"];
int length=0;int i=0;
foreach(dynamic d in data)
{
length++;
}
Array x = Array.CreateInstance(typeof(decimal), length);
Array y = Array.CreateInstance(typeof(decimal), length);
foreach(dynamic d in data)
{
x.SetValue(d.AccountBlance,i);
y.SetValue(d.AccountEquity, i);
i++;
}
var myChart = new Chart(width: 600, height: 300)
.AddTitle("账户展示")
.AddSeries(
xValue: x,
yValues: y,
chartType: "Line")
.Write();
}
图片可以正常显示了。
很费解,因为msdn中定义xValue正应该使用IEnumerable类型。
public Chart AddSeries( string name, string chartType, string chartArea, string axisLabel, string legend, int markerStep, IEnumerable xValue, string xField, IEnumerable yValues, string yFields )不知是哪里理解错误。总之问题解决了。
浙公网安备 33010602011771号