ASP.NET MVC 3 MSChart Error: Only 1 Y values can be set for this data series

前端 未结 4 821
萌比男神i
萌比男神i 2020-12-20 02:34

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.H         


        
相关标签:
4条回答
  • 2020-12-20 02:57

    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.

    0 讨论(0)
  • 2020-12-20 03:00

    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");
    
    0 讨论(0)
  • 2020-12-20 03:03

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

    yFields: "Open,,High,,Low,,Close",
    
    0 讨论(0)
  • 2020-12-20 03:11

    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);
    
    0 讨论(0)
提交回复
热议问题