问题描述
我有带有股票类型图表的不错的工作图。当我尝试向该图表区域添加线型系列时,我得到了大红色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..
注意:
当尝试显示未对齐的系列以及有索引的系列时,您可以选择以下任一方式:
When trying to show an unaligned Series along with an indexed one you can get either:
- ,解释问题(从VS运行时)
- 或者从exe运行时或在try-catch块中忽略异常时,红色X不解释任何问题。
这篇关于如何在C#图表中添加两个不同的类型系列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!