如何填充两个系列样条线或线之间的区域 [英] How can I fill the area(s) between two Series of Splines or Lines
本文介绍了如何填充两个系列样条线或线之间的区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个图表
:
如何填充两个系列S0和S1
之间的区域,例如蓝色和黄色 Series
?
解决方案
为此,我们编写 Paint
活动:
这里 ValueToPixelPosition
函数有效,并在 DataPoint
值之间提供必要的转换和图表
像素..:
private void chart1_Paint PaintEventArgs e)
{
//我们假设两个系列变量被设置..:
if(sps1 == null || sps2 == null)return;
//简短引用:
轴ax = chart1.ChartAreas [0] .AxisX;
Axis ay = chart1.ChartAreas [0] .AxisY;
//现在我们将所有值转换为像素
List< PointF> points1 = sps1.Points.Select(x =>
new PointF((float)ax.ValueToPixelPosition(x.XValue),
(float)ay.ValueToPixelPosition(x.YValues [0])) ).ToList();
List< PointF> points2 = sps2.Points.Select(x =>
new PointF(float)ax.ValueToPixelPosition(x.XValue),
(float)ay.ValueToPixelPosition(x.YValues [0])) ).ToList();
//一个列表向前,另一个向后:
points2.Reverse();
GraphicsPath gp = new GraphicsPath();
gp.FillMode = FillMode.Winding; //正确的fillmode
//它可以与Splines或Lines一起使用:
if(sps1.ChartType == SeriesChartType.Spline)gp.AddCurve(points1.ToArray()) ;
else gp.AddLines(points1.ToArray());
if(sps2.ChartType == SeriesChartType.Spline)gp.AddCurve(points2.ToArray());
else gp.AddLines(points2.ToArray());
//选择你自己的颜色,也许是系列颜色的混合..
使用(SolidBrush brush = new SolidBrush(Color.FromArgb(66,Color.DarkCyan)))
e.Graphics.FillPath(brush,gp);
gp.Dispose();
}
请注意,这未经过缩放测试。
I have this Chart
:
How can I fill the area between two Series S0 and S1
, say the blue and the yellow Series
?
解决方案
To do that we code one of the Paint
events:
Here the ValueToPixelPosition
functions are valid and provide us with the necessary conversion between the DataPoint
values and Chart
pixels..:
private void chart1_Paint(object sender, PaintEventArgs e)
{
// we assume two series variables are set..:
if (sps1 == null || sps2 == null) return;
// short references:
Axis ax = chart1.ChartAreas[0].AxisX;
Axis ay = chart1.ChartAreas[0].AxisY;
// now we convert all values to pixels
List<PointF> points1 = sps1.Points.Select(x=>
new PointF((float)ax.ValueToPixelPosition(x.XValue),
(float)ay.ValueToPixelPosition(x.YValues[0]))).ToList();
List<PointF> points2 = sps2.Points.Select(x=>
new PointF((float)ax.ValueToPixelPosition(x.XValue),
(float)ay.ValueToPixelPosition(x.YValues[0]))).ToList();
// one list forward, the other backward:
points2.Reverse();
GraphicsPath gp = new GraphicsPath();
gp.FillMode = FillMode.Winding; // the right fillmode
// it will work fine with either Splines or Lines:
if (sps1.ChartType == SeriesChartType.Spline ) gp.AddCurve(points1.ToArray());
else gp.AddLines(points1.ToArray());
if (sps2.ChartType == SeriesChartType.Spline) gp.AddCurve(points2.ToArray());
else gp.AddLines(points2.ToArray());
// pick your own color, maybe a mix of the Series colors..
using (SolidBrush brush = new SolidBrush(Color.FromArgb(66, Color.DarkCyan)))
e.Graphics.FillPath(brush, gp);
gp.Dispose();
}
Note that this is not tested for zooming..
这篇关于如何填充两个系列样条线或线之间的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文