简体   繁体   English

x轴不同间隔上不同颜色的折线图填充同一系列?

[英]Line Chart with different colors on a different interval at x-axis fill in same series?

I created a Line Chart control in Windows Forms.我在 Windows 窗体中创建了一个折线图控件。

I divided the ChartArea, AxisX into four intervals but I want to apply back color (unique color) to each interval.我将 ChartArea、AxisX 分成四个区间,但我想对每个区间应用背景颜色(独特的颜色)。

Can someone help me on this?有人可以帮我吗?

AxisX into four intervals but I want to apply back color (unique color) AxisX 分成四个间隔,但我想应用背景颜色(独特的颜色)

These intervals are created with colored StripLine (s).这些间隔是用彩色StripLine (s) 创建的。 Either via code:要么通过代码:

var stripLine = new System.Windows.Forms.DataVisualization.Charting.StripLine()
{
    BackColor = Color.Blue,
    IntervalOffset = 4, // This is where the stripline starts
    StripWidth = 2 // And this is how long the interval is
};

chart1.ChartAreas[0].AxisX.StripLines.Add(stripLine);

You need to add data points for the interval to show.您需要为要显示的间隔添加数据点。

Or, StripLines can also be added from VS design mode from (Properties) -> ChartAreas -> Select a ChartArea -> Axes -> Select the Axis you want it to show on -> StripLines, then Add StripLine.或者,也可以通过 VS 设计模式从(属性)-> ChartAreas -> 选择一个 ChartArea -> Axes -> 选择要显示的轴 -> StripLines,然后添加 StripLine。 You have to set a BackColor , IntervalOffset and StripWidth for it to show.您必须设置BackColorIntervalOffsetStripWidth才能显示。 If you set StripLine.Interval it will repeat by that interval.如果您设置StripLine.Interval ,它将按该间隔重复。

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 .因此,正如 NLindborn 所建议的那样,最好的方法是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.请注意,它们的属性在 data values 中,因此您需要知道图表中的值,或者更确切地说是 x 轴范围。

在此处输入图片说明

Here is complete code example using StripLines :这是使用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:现在我们准备好创建 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.还要注意 StripLine 使用轴值。

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:AxisViewChanged编码会有所帮助,也许像这样:

For each of your striplines calculate an IntervalOffset ;对于您的每条带状线,计算一个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!对于其他人,添加正确的宽度倍数,如上!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM