在x轴的不同间隔处具有不同颜色的折线图是否填充相同的系列? [英] Line Chart with different colors on a different interval at x-axis fill in same series?

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

问题描述

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



我将ChartArea和AxisX划分为四个间隔,但是我想对每个间隔应用底色(唯一颜色)。



有人可以帮我吗?

解决方案

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



因此,正如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 <== 360; 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!

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

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