ASP.NET MVC 3 MSChart 错误:只能为此数据系列设置 1 个 Y 值

发布于 2024-11-04 18:44:38 字数 1276 浏览 6 评论 0原文

我正在尝试使用 微软的图表库

我正在使用此代码在我的视图中创建图表:

@{
    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丶情人眼里出诗心の 2024-11-11 18:44:39

好吧,实际上最简单的方法就是将其更改为:

yFields: "Open,,High,,Low,,Close",

Well, actually the easiest way is just change it to:

yFields: "Open,,High,,Low,,Close",
胡大本事 2024-11-11 18:44:39

我通过使用重载的 Series 构造函数修复了此问题:Series(string name, int yValues)。请参阅下面的示例。

Series ohlc = new Series("Ohlc", 4);
...
ohlc.Points.AddXY(xValue, open, high, low, close);

I fixed this by using the overloaded Series constructor: Series(string name, int yValues). See the example below.

Series ohlc = new Series("Ohlc", 4);
...
ohlc.Points.AddXY(xValue, open, high, low, close);
红玫瑰 2024-11-11 18:44:39

处理相同的问题后 - 只能向一系列添加一个值 - 同时尝试使用 Web.Helper.Chart 类构建具有多个 Y 轴值的堆积柱形图。

我在 MSDN 或任何其他论坛(包括 StackOverflow)上都没有找到唯一的答案。事实证明,这非常简单:您可以使用 .AddSeries 方法添加多个系列。

换句话说,用简单的英语来说,对于要添加到图表的每个系列调用一次 .AddSeries 方法。

此示例用于报告以吨为单位的每日库存,就像钢铁厂一样 - 产量以库存中的钢材吨数来衡量。

        new Chart(1000, 500, ChartTheme.Blue)
            .AddTitle("Inventory")

            .AddSeries(name: "A",
                    chartType: "StackedColumn",
                    xValue: intDays,
                    yValues: dblTons_A)

            .AddSeries(name: "B",
                    chartType: "StackedColumn",
                    xValue: intDays,
                    yValues: dblTons_B)

            .AddSeries(name: "C",
                    chartType: "StackedColumn",
                    xValue: intDays,
                    yValues: dblTons_C)

            .AddSeries(name: "D",
                    chartType: "StackedColumn",
                    xValue: intDays,
                    yValues: dblTons_D)

            .Write("png");

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.

        new Chart(1000, 500, ChartTheme.Blue)
            .AddTitle("Inventory")

            .AddSeries(name: "A",
                    chartType: "StackedColumn",
                    xValue: intDays,
                    yValues: dblTons_A)

            .AddSeries(name: "B",
                    chartType: "StackedColumn",
                    xValue: intDays,
                    yValues: dblTons_B)

            .AddSeries(name: "C",
                    chartType: "StackedColumn",
                    xValue: intDays,
                    yValues: dblTons_C)

            .AddSeries(name: "D",
                    chartType: "StackedColumn",
                    xValue: intDays,
                    yValues: dblTons_D)

            .Write("png");
多彩岁月 2024-11-11 18:44:38

我能够通过绕过助手自行构建图表来解决这个问题。

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");
    }
}

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

I was able to work around this issue by building the chart myself, bypassing the helper.

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");
    }
}

This code is in my controller, and no view exists for it, because it is returning an actual image resource.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文