x轴不同间隔上不同颜色的折线图填充同一系列? [英] Line Chart with different colors on a different interval at x-axis fill in same series?

查看:19
本文介绍了x轴不同间隔上不同颜色的折线图填充同一系列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 窗体中创建了一个折线图控件.

我将 ChartArea、AxisX 分成四个区间,但我想对每个区间应用背景颜色(独特的颜色).

有人可以帮助我吗?

解决方案

您可以绘制这些区域,但这将始终显示在包括网格和数据点在内的所有图表元素之上.

因此,正如 NLindborn 所建议的,最好的方法是

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

//设置图表:ChartArea ca = chart.ChartAreas[0];chart.Series.Clear();for (int i = 0; i <5; i++){系列 s = chart.Series.Add("Series" + (i+1));s.ChartType = SeriesChartType.Line;s.BorderWidth = 2;}//添加一些测试数据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));}//设置图表区域:轴 ax = ca.AxisX;ax.Minimum = 0;ax.Maximum = 360;ax.Interval = 30;//一些半透明的颜色列表<颜色>颜色 = 新列表<颜色>() { 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 = 新的 StripLine();sl.Interval = hrange;//不小于范围,所以不会重复sl.StripWidth = hrange/4f;//宽度sl.IntervalOffset = sl.StripWidth * i;//x 位置sl.BackColor = 颜色 [i];ax.StripLines.Add(sl);}

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

还要注意 StripLine 使用的轴值.

更新:

一个常见问题是在缩放时移动带状线.没有一点帮助,他们会坚持原来的立场.编码 AxisViewChanged 会有所帮助,也许像这样:

为每条带状线计算一个IntervalOffset;在第一个最简单的情况下,这应该可以工作:

 chart1.ChartAreas[0].AxisX.StripLines[0].IntervalOffset =chart1.Series[0].Points[0].XValue - e.NewPosition;

对于其他人,添加正确的宽度倍数,如上!

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!

Also note the StripLine use axis values.

Update:

One common issue is to move the striplines when zooming. Without a little help they will stick to the original positions. Codeing the AxisViewChanged will help, maybe like so:

For each of your striplines calculate an IntervalOffset; in the simplest case of the 1st one this should work:

   chart1.ChartAreas[0].AxisX.StripLines[0].IntervalOffset = 
           chart1.Series[0].Points[0].XValue - e.NewPosition;

For the others add the correct multiple of the width as above!

这篇关于x轴不同间隔上不同颜色的折线图填充同一系列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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