且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在x轴的不同间隔处具有不同颜色的折线图是否填充相同的系列?

更新时间:2023-11-21 21:58:04

您可以画画这些区域,但这总是显示在所有图表元素之上,包括网格和数据点。



因此,正如NLindborn建议的那样,***的方法是



这里是使用 StripLines 的完整代码示例:

  //设置图表:
ChartArea ca = chart.ChartAreas [0];
chart.Series.Clear();
for(int i = 0; i< 5; i ++)
{
Series s = chart.Series.Add( Series +(i + 1));
s.ChartType = SeriesChartType.Line;
s.BorderWidth = 2;
}

//为(int i = 0; i {
图表添加一些测试数据
。系列[0] .Points.AddXY(i,Math.Sin(i * Math.PI / 180f));
chart.Series [1] .Points.AddXY(i,Math.Cos(i * Math.PI / 180f));
chart.Series [2] .Points.AddXY(i,Math.Sin(i * Math.PI / 90f));
chart.Series [3] .Points.AddXY(i,Math.Cos(i * Math.PI / 90f));
chart.Series [4] .Points.AddXY(i,Math.Sin(i * Math.PI / 30f));
}

//设置图表区域:
Axis ax = ca.AxisX;
轴,最小值= 0;
轴,最大值= 360;
ax.Interval = 30;

//一些半透明的颜色
List< Color> colors = new List< Color>(){Color.FromArgb(64,Color.LightBlue),
Color.FromArgb(64,Color.LightSalmon),Color.FromArgb(64,Color.LightSeaGreen),
Color.FromArgb(64,Color.LightGoldenrodYellow)};

现在我们准备创建带状线:

  //这是图表的宽度值:
double hrange = ax.Maximum-ax.Minimum;

//现在我们创建并添加四个带状线:
for(int i = 0; i< 4; i ++)
{
StripLine sl = new StripLine ();
sl.Interval =范围; //不小于范围,因此不会重复
sl.StripWidth = hrange / 4f; //宽度
sl.IntervalOffset = sl.StripWidth * i; // x位置
sl.BackColor = colors [i];
ax.StripLines.Add(sl);
}

请注意,每当更改轴范围时,都需要调整带状线数据!


I created a Line Chart control in Windows Forms.

I divided the ChartArea, AxisX into four intervals but I want to apply back color (unique color) to each interval.

Can someone help me on this?

You could paint those areas, but this would always show above all chart elements including grid and data points.

So, as NLindborn suggests, the best way are StripLines.

They are under all elements and will scale nicely.

Note that their properties are in data values, so you need to know the values, or rather the x-axis range, in your chart.

Here is complete code example using StripLines:

// set up the chart:
ChartArea ca = chart.ChartAreas[0];
chart.Series.Clear();
for (int i = 0; i < 5; i++)
{
    Series s = chart.Series.Add("Series" + (i+1));
    s.ChartType = SeriesChartType.Line;
    s.BorderWidth = 2;
}

// add a few test data
for (int i = 0; i <= 360; i++)
{
    chart.Series[0].Points.AddXY(i, Math.Sin(i * Math.PI / 180f));
    chart.Series[1].Points.AddXY(i, Math.Cos(i * Math.PI / 180f));
    chart.Series[2].Points.AddXY(i, Math.Sin(i * Math.PI / 90f));
    chart.Series[3].Points.AddXY(i, Math.Cos(i * Math.PI / 90f));
    chart.Series[4].Points.AddXY(i, Math.Sin(i * Math.PI / 30f));
}

// set up the chart area:  
Axis ax = ca.AxisX;
ax.Minimum = 0;
ax.Maximum = 360;
ax.Interval = 30;

// a few semi-transparent colors
List<Color> colors = new List<Color>()  { Color.FromArgb(64, Color.LightBlue),  
  Color.FromArgb(64, Color.LightSalmon),  Color.FromArgb(64, Color.LightSeaGreen),  
  Color.FromArgb(64, Color.LightGoldenrodYellow)};

Now we are ready to create the StripLines:

// this is the width of the chart in values:
double hrange = ax.Maximum - ax.Minimum;

// now we create and add four striplines:
for (int i = 0; i < 4; i++)
{
    StripLine sl = new StripLine();
    sl.Interval = hrange;                   // no less than the range, so it won't repeat
    sl.StripWidth = hrange / 4f;            // width
    sl.IntervalOffset = sl.StripWidth * i;  // x-position
    sl.BackColor = colors[i];
    ax.StripLines.Add(sl);
}

Note that you will need to adapt the stripline data whenever you change the axis range!