C#更改图表中的颜色 [英] C# change color in chart

查看:332
本文介绍了C#更改图表中的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中有一个图表,将值绑定到该图表。我有两个带有值的不同数组,并将它们连接到一张图表。现在,我想以不同的颜色显示图表的一部分(带有第一个数组中的值)。这个怎么做?绘制两个图表会导致错误,所以我想这样做。这是代码的一部分:

I have a chart in C# where I bind values to it. I have two different arrays with values and concat them to one chart. Now I wanted to display one part of the chart (with the values from the first array) in a different color. How to do this? drawing two charts causes errors so I wanted to do it this way. Here is a part of the code:

String[] x_axis = _temp_date1.Concat(_date).ToArray();
Double[] y_axis = _temp_data.Concat(_value).ToArray();

chart1.Series["Chart"].Points.DataBindXY(x_axis, y_axis);
chart1.Series["Chart"].ChartType = SeriesChartType.Spline;

chart1.Series["Chart"].Points[0].Color = System.Drawing.Color.Red;
chart1.Series["Chart"].Points[1].Color = System.Drawing.Color.Green;

带有颜色的部分不起作用。

The part with the Color doesn't work.

因此,可以说我有两个x值数组(date1和date2)和两个y值数组(data1和data2),现在我合并date1和date2数组,并合并data1和data2数组。我将它们绑定到我的图表。现在,我想用另一种颜色显示图的一部分,这是我的date1 / data1数组值的来源。数组长度可能会更改,因为从csv文件读取了数据。

So lets say I have two arrays for the x values (date1 and date2) and two arrays for the y values (data1 and data2), Now I merge the date1 and date2 arrays and I merge the data1 and data2 arrays. I bind them to my graph. Now I would like to display the part of the graph in a different color where my values from the date1/data1 arrays come from. The array length can change because the data is read from a csv file.

推荐答案

下面是显示两个示例的示例:两个单独的 ChartAreas 相同的 Chart 和两个 Series 在同一 ChartArea 中。选择所需的对象:

Here is an example that shows both: Two separate ChartAreas in the same Chart and two Series in the same ChartArea. Pick which you want:

// cleanup before we start 
chart1.ChartAreas.Clear();
chart1.Series.Clear();
// two areas one on top the other below
chart1.ChartAreas.Add("area1");
chart1.ChartAreas.Add("area2");
// three series
chart1.Series.Add("series1");
chart1.Series.Add("series2");
chart1.Series.Add("series3");
// we assign  two series to the bottom area
chart1.Series["series1"].ChartArea = "area1";
chart1.Series["series2"].ChartArea = "area2";
chart1.Series["series3"].ChartArea = "area2";
// all series are of type spline
chart1.Series["series1"].ChartType = SeriesChartType.Spline;
chart1.Series["series2"].ChartType = SeriesChartType.Spline;
chart1.Series["series3"].ChartType = SeriesChartType.Spline;
// each has a spearate color
chart1.Series["series1"].Color = Color.Red;
chart1.Series["series2"].Color = Color.Blue;
chart1.Series["series3"].Color = Color.Green;
// now we add a few points
chart1.Series["series1"].Points.AddXY(1, 100);
chart1.Series["series1"].Points.AddXY(2, 400);
chart1.Series["series1"].Points.AddXY(3, 200);
chart1.Series["series1"].Points.AddXY(4, 300);

chart1.Series["series2"].Points.AddXY(1, 120);
chart1.Series["series2"].Points.AddXY(2, 420);
chart1.Series["series2"].Points.AddXY(3, 290);
chart1.Series["series2"].Points.AddXY(4, 390);

chart1.Series["series3"].Points.AddXY(1, 220);
chart1.Series["series3"].Points.AddXY(2, 320);
chart1.Series["series3"].Points.AddXY(3, 690);
chart1.Series["series3"].Points.AddXY(4, 190);

// we can even paint a part of the spline curve in a different color
// to be precise: the part up to the point:
chart1.Series["series3"].Points[1].Color = Color.HotPink;
chart1.Series["series3"].Points[2].Color = Color.Orange;

结果是:

< img src = https://i.stack.imgur.com/kRPFk.png alt =在此处输入图片描述>

现在,如果您只是想在一定数量的点之后更改点的颜色,您可以这样:

Now if you simply want to change the color of the points after a certain number of points you could do it like this:

int start = x_axis.Length; 
for (int i = start ; i < chart1.Series[0].Points.Count; i++)
    chart1.Series[0].Points[i].Color = Color.Green;

请注意,您需要设置每个点的颜色与默认图表颜色不同的颜色!

Note that you need to set the color of each point that shall have a different color from the default chart color!

这篇关于C#更改图表中的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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