C#在图表中改变颜色

时间:2014-11-04 07:33:06

标签: c# winforms colors charts

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

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;

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

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

3 个答案:

答案 0 :(得分:2)

以下示例显示了两个:同一ChartAreas中的两个Chart和同一Series中的两个ChartArea。选择你想要的:

// 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;

结果如下:

enter image description here

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

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

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

答案 1 :(得分:0)

在您的charts控件的属性中:

Series - &gt; Member[n] - &gt; Color - &gt; <{1}},等等

Red

修改(根据评论中的讨论,您可以尝试类似的内容):

假设我们有一个chart1.Series["Chart"].Color = Color.Red 数组,我们想用不同颜色绘制每个部分:

doubles

答案 2 :(得分:0)

            txtTableSQL.BackColor = Color.FromArgb(255, 255, 255);
            txtTableSQL.ForeColor = Color.FromArgb(15, 15, 15);

            txtComandoSQL.BackColor = Color.FromArgb(255, 255, 255);
            txtComandoSQL.ForeColor = Color.FromArgb(15, 15, 15);

            txtReport.BackColor = Color.FromArgb(255, 255, 255);
            txtReport.ForeColor = Color.FromArgb(15, 15, 15);

            chartSeries.BackColor = Color.FromArgb(255, 255, 255);
            chartSeries.ForeColor = Color.FromArgb(15, 15, 15);

            chartSeries.ChartAreas[0].BackColor = Color.FromArgb(255, 255, 255);
            chartSeries.Series[0].Color = Color.Gray;
            chartSeries.Series[0].LabelForeColor = Color.Black;
            chartSeries.Series[0].BorderColor = Color.Black;
            chartSeries.Series[0].MarkerColor = Color.Black;


            chartSeries.ChartAreas[0].Axes[0].LineColor = Color.Black;
            chartSeries.ChartAreas[0].Axes[1].LineColor = Color.Black;
            chartSeries.ChartAreas[0].Axes[2].LineColor = Color.Black;
            chartSeries.ChartAreas[0].Axes[3].LineColor = Color.Black;

            chartSeries.ChartAreas[0].AxisX.LineColor = Color.Black;
            chartSeries.ChartAreas[0].AxisY.LineColor = Color.Black;
            chartSeries.ChartAreas[0].AxisX2.LineColor = Color.Black;
            chartSeries.ChartAreas[0].AxisY2.LineColor = Color.Black;
            chartSeries.ChartAreas[0].AxisX.MinorGrid.LineColor = Color.Black;
            chartSeries.ChartAreas[0].AxisY.MinorGrid.LineColor = Color.Black;
            chartSeries.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Black;
            chartSeries.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Black;
            chartSeries.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.Black;
            chartSeries.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.Black;
            chartSeries.ChartAreas[0].AxisX.TitleForeColor = Color.Black;

            chartSeries.Legends[0].BackColor = Color.White;