asp.net-mvc-3 – ASP.NET MVC 3 MSChart错误:此数据系列只能设置1个Y值

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-3 – ASP.NET MVC 3 MSChart错误:此数据系列只能设置1个Y值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用 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");
    }
}

代码在我的控制器中,并且不存在任何视图,因为它返回实际的图像资源.

原文链接:https://www.f2er.com/aspnet/251084.html

猜你在找的asp.Net相关文章