C# change color in chart

2019-08-31 05:29发布

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.

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.

2条回答
一纸荒年 Trace。
2楼-- · 2019-08-31 06:08

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;

Here is the result:

enter image description here

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!

查看更多
Rolldiameter
3楼-- · 2019-08-31 06:14

In your charts control properties :

Series --> Member[n] --> Color --> Red , and so on

OR

chart1.Series["Chart"].Color = Color.Red

Edit (According to the discussion in the comments, you can try something like):

Say we have an array of doubles, and we want to plot it each portion in a different color :

Double[] data; // my data
int i=0;

chart1.Series["Chart"].Points[0].Clear(); // initialize the chart
chart1.Series["Chart"].Color = Color.Red; // initial color

for(i=0; i < data.Length; i++)
{
  if(i >= data.Length/2)
     chart1.Series["Chart"].Color = Color.Green; // use other color after certain data #

  chart1.Series["Chart"].Points.AddXY(i, data[i]);
}
查看更多
登录 后发表回答