ASP.NET MVC 3 MSChart 错误:只能为此数据系列设置 1 个 Y 值
我正在尝试使用 微软的图表库。
我正在使用此代码在我的视图中创建图表:
@{
System.Web.Helpers.Chart cht = new Chart(600, 400);
cht.AddTitle(ViewData["Symbol"].ToString());
cht.AddSeries(name: "Price",
chartType: "Stock",
chartArea: "Top",
xField: "Date",
xValue: Model,
yFields: "Open,High,Low,Close",
yValues: Model);
cht.Write();
}
当调用获取图表的操作时,会引发以下异常:
ArgumentOutOfRangeException: Data points insertion error. Only 1 Y values can be set for this data series. Parameter name: yFields System.Web.UI.DataVisualization.Charting.DataPointCollection.DataBindXY(IEnumerable xValue, String xField, IEnumerable yValue, String yFields) +1076598 System.Web.Helpers.Chart.ApplySeries(Chart chart) +508 System.Web.Helpers.Chart.ExecuteChartAction(Action`1 action) +174 System.Web.Helpers.Chart.GetBytes(String format) +144 System.Web.Helpers.Chart.Write(String format) +96
“Stock”图表类型应允许 4 个 Y 值,并且在使用时似乎已确认这一点反射器来检查图表助手的代码。我错过了什么吗?
I'm attempting to build a stock chart using Microsoft's charting library.
I'm using this code to create the chart in my view:
@{
System.Web.Helpers.Chart cht = new Chart(600, 400);
cht.AddTitle(ViewData["Symbol"].ToString());
cht.AddSeries(name: "Price",
chartType: "Stock",
chartArea: "Top",
xField: "Date",
xValue: Model,
yFields: "Open,High,Low,Close",
yValues: Model);
cht.Write();
}
When the action to get the chart is invoked, the following exception is thrown:
ArgumentOutOfRangeException: Data points insertion error. Only 1 Y values can be set for this data series. Parameter name: yFields System.Web.UI.DataVisualization.Charting.DataPointCollection.DataBindXY(IEnumerable xValue, String xField, IEnumerable yValue, String yFields) +1076598 System.Web.Helpers.Chart.ApplySeries(Chart chart) +508 System.Web.Helpers.Chart.ExecuteChartAction(Action`1 action) +174 System.Web.Helpers.Chart.GetBytes(String format) +144 System.Web.Helpers.Chart.Write(String format) +96
The "Stock" chartType should allow 4 values for Y, and this appears to be confirmed when using reflector to inspect the Chart helper's code. Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,实际上最简单的方法就是将其更改为:
Well, actually the easiest way is just change it to:
我通过使用重载的 Series 构造函数修复了此问题:
Series(string name, int yValues)
。请参阅下面的示例。I fixed this by using the overloaded Series constructor:
Series(string name, int yValues)
. See the example below.处理相同的问题后 - 只能向一系列添加一个值 - 同时尝试使用 Web.Helper.Chart 类构建具有多个 Y 轴值的堆积柱形图。
我在 MSDN 或任何其他论坛(包括 StackOverflow)上都没有找到唯一的答案。事实证明,这非常简单:您可以使用 .AddSeries 方法添加多个系列。
换句话说,用简单的英语来说,对于要添加到图表的每个系列调用一次 .AddSeries 方法。
此示例用于报告以吨为单位的每日库存,就像钢铁厂一样 - 产量以库存中的钢材吨数来衡量。
After dealing with the same issue - only being able to add one value to a series - while trying to build a Stacked Column chart with multiple Y axis values - using the Web.Helper.Chart class.
I did not find the one answer anywhere on MSDN or any other forum including StackOverflow. Turns out - it is quite simple: You can add more than one series with the .AddSeries method.
In other words, in plain English, call the .AddSeries method once for each series you want to add to the chart.
This example was used to report daily inventory which is measured in tons, as it would be at a steel plant - production measured in tons of steel in inventory.
我能够通过绕过助手自行构建图表来解决这个问题。
此代码位于我的控制器中,并且不存在任何视图,因为它返回实际的图像资源。
I was able to work around this issue by building the chart myself, bypassing the helper.
This code is in my controller, and no view exists for it, because it is returning an actual image resource.