如何在C#图表中添加两个不同的类型系列? [英] How to add two different type series to C# chart?

查看:253
本文介绍了如何在C#图表中添加两个不同的类型系列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有股票类型图表的不错的工作图。当我尝试向该图表区域添加线型系列时,我得到了大红色X。

I have nice working chart with stock typed graph. When I tried to add line typed series to this chartarea I got big red X.

Series ser = new Series(seriesName);
ser.ChartArea = "Default";
ser.Name = seriesName;
ser.ChartType=SeriesChartType.Line;
ser.Points.AddXY(1, 1);
ser.Points.AddXY(1, 2);
chart1.Series.Add(ser);

是否可以在一个图表中添加不同类型的系列?

Is it possible to add different types of series in one chartarea?

编辑

我试图按照TaW的建议应用更改,但仍然遇到相同的错误。 / p>

I tried to apply changes in accordance with the proposal of TaW, but still get same error.

string seriesName =  dataGridView1.Rows[rowSelected].Cells[0].Value + "_" + curPeriod + "_" +"line" +dataPoints.Length;
Series ser = new Series(seriesName);
chart1.Series.Add(ser);
chart1.Series[seriesName].ChartArea = chart1.ChartAreas[0].Name;
chart1.Series[seriesName].ChartType = SeriesChartType.Line;
chart1.Series[seriesName].Points.AddXY(1, 1);
chart1.Series[seriesName].Points.AddXY(1, 2);

这是我的图表的初始化。

Here is initialization of my chart.

chart1.ChartAreas.Add(new ChartArea("Default"));
chart1.Series.Add(new Series("Series1"));
chart1.ChartAreas.Add(new ChartArea("Option"));
chart1.Series.Add(new Series("Series2"));
chart1.Series.Add(new Series("Series3"));
chart1.Series["Series2"].ChartArea = "Option";
chart1.Series["Series3"].ChartArea = "Option";
chart1.Series["Series1"].ChartArea = "Default";
chart1.Series["Series1"].ChartType = SeriesChartType.Stock;
chart1.Series["Series2"].ChartType = SeriesChartType.Line;
chart1.Series["Series3"].ChartType = SeriesChartType.Line;
chart1.Series["Series1"].BorderColor = Color.Green;
chart1.Series["Series1"].Color = Color.GreenYellow;
chart1.Series["Series2"].Color = Properties.Settings.Default.InnerPrice;
chart1.Series["Series3"].Color = Properties.Settings.Default.TemporaryPrice;
chart1.Series["Series1"]["PriceUpColor"] = "Red";
chart1.Series["Series1"]["PriceDownColor"] = "Yellow";
chart1.Series["Series1"].IsXValueIndexed = true;
chart1.Series["Series2"].IsXValueIndexed = false;
chart1.Series["Series3"].IsXValueIndexed = false;
chart1.Series["Series1"].XValueType = ChartValueType.DateTime;


也许有人可以帮忙。我需要在图表中画线!谢谢!

May be somebody can help. I need to draw lines in chart!Thanks in advance!

推荐答案

您的问题很可能来自于您准备<$的方式c $ c>系列 之前将它们添加到图表中。

Most likely your problems come from the way you prepare the Series before adding them to the Chart.

我发现在将股票系列添加到图表之前,我什至无法添加点!我得到的错误声称我只能为 DataPoints 添加一个y值,这显然是错误的,因为 SeriesChartType.Stock 已经设置。

I found that I can't even add points to a Stock series before it has been added to the chart! The error I got, claimed that I could only add one y-value for my DataPoints, which was obviously wrong, as the SeriesChartType.Stock had already been set.

因此,请尝试以下命令:

So try this order:

chart1.Series.Clear();

string seriesName1 = "stock";
Series ser1 =  chart1.Series.Add(seriesName1);

ser1.ChartArea = chart1.ChartAreas[0].Name;
ser1.Name = seriesName1;
ser1.ChartType = SeriesChartType.Stock;

ser1.BorderWidth = 3;

ser1.Points.AddXY(1, 44, 11, 34, 37);  // x, high, low, open, close
ser1.Points.AddXY(2, 33, 11, 22, 33);


string seriesName2 = "line";
Series ser2 = chart1.Series.Add(seriesName2);

ser2.ChartArea = chart1.ChartAreas[0].Name;
ser2.Name = seriesName2;
ser2.ChartType = SeriesChartType.Line;
ser2.Points.AddXY(1, 44);
ser2.Points.AddXY(2, 33);

还要确保 seriesName 变量包含正确的字符串!

Also do make sure the seriesName variable contains a proper string!

更新

看到目前为止发布的完整代码,看来您正在添加一个或多个 Series ChartArea 默认,其中已经包含索引系列(系列1)。仅当该 ChartArea 中的全部(其他)系列时,才允许这样做与第一个对齐,即包含相同数量的值。由于您只添加了2个 DataPoints ,所以我怀疑它们是..

Looking at the full code posted so far it seems that you are adding one or more Series to the ChartArea "Default", which already contains an Indexed Series ("Series1"). This is only allowed if all (other) Series in that ChartArea are aligned with the first one, i.e. contain the same number of values. Since you only add 2 DataPoints, I doubt they are..

MSDN:


将Series.IsXValueIndexed属性设置为true会导致分配给同一图表区域的所有系列

的X轴(主要或次要)被索引。您必须确保系列对齐。否则,
Chart控件将引发异常。有关更多信息,请参见
对齐数据。

Setting the Series.IsXValueIndexed property to true causes all series assigned to the same chart area and X axis (primary or secondary) to be indexed. You must make sure that the series are aligned. Otherwise, the Chart control throws an exception. For more information, see Aligning Data.

注意

当尝试显示未对齐的系列以及有索引的系列时,您可以选择以下任一方式:

When trying to show an unaligned Series along with an indexed one you can get either:


  • ,解释问题(从VS运行时)

  • 或者从exe运行时或在try-catch块中忽略异常时,红色X不解释任何问题。

这篇关于如何在C#图表中添加两个不同的类型系列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆