我正在尝试使用
Microsoft’s charting library建立一个股票图表.
我正在使用此代码在我的视图中创建图表:
@{ 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”chartType应该允许Y的4个值,并且当使用反射器检查Chart帮助程序的代码时,这似乎得到确认.我错过了什么吗?
解决方法
通过自己构建图表,绕过帮助程序,我能够解决这个问题.
using (Chart chart = new Chart()) { chart.Width = 600; chart.Height = 400; chart.RenderType = RenderType.BinaryStreaming; chart.Palette = ChartColorPalette.Bright; chart.ChartAreas.Add("Top"); chart.ChartAreas.Add("Bottom"); chart.Series.Add("Price"); chart.Series.Add("Volume"); chart.Series["Price"].ChartArea = "Top"; chart.Series["Volume"].ChartArea = "Bottom"; chart.Series["Price"].ChartType = SeriesChartType.Stock; chart.Series["Volume"].ChartType = SeriesChartType.Column; for (int x = 0; x < data.Quotes.Count / 2; x++) { Quote quote = data.Quotes[x]; chart.Series["Price"].Points.AddXY(quote.Date,quote.Open,quote.High,quote.Low,quote.Close); chart.Series["Volume"].Points.AddXY(quote.Date,quote.Volume); } using (MemoryStream memStream = new MemoryStream()) { chart.SaveImage(memStream,ChartImageFormat.Jpeg); return File(memStream.ToArray(),"image/jpeg"); } }
此代码在我的控制器中,并且不存在任何视图,因为它返回实际的图像资源.