如何填充直线和曲线下的所有内容? [英] How do I fill everything over a straight line and under a curve?
问题描述
我正在Windows窗体中使用图表组件。
I am using the Charts component in Windows Forms.
我使用
直线 >I create a straight line
using
chart1.Series["Grenzwert"].Points.Add(new DataPoint(0, y));
chart1.Series["Grenzwert"].Points.Add(new DataPoint(maxwidth, y));
我也画出了由一条线连接的一系列点,我们称之为曲线
。
Also I plot a a series of points connected by a line, let's call it curve
.
如何显示直线
以上和<$ c以下的所有内容$ c>曲线填充了吗?
How do I show everything over straight line
and under curve
filled?
列填充了整个区域,而不仅仅是在直线。
Column fills the whole area, not just above straight line
.
示例:
推荐答案
我有一个使用<$ c $的想法c> SeriesChartType.Range ,如下。
private void UpdateChart(float straight_line, List<DataPoint> curve)
{
float y = straight_line; // YValue of the straight line
var list = curve.ToList(); // Clone the curve
int count = list.Count - 2;
for (int i = 0; i < count; i++) // Calculate intersection point between the straight line and a line between (x0,y0) and (x1,y1)
{
double x0 = list[i + 0].XValue;
double y0 = list[i + 0].YValues[0];
double x1 = list[i + 1].XValue;
double y1 = list[i + 1].YValues[0];
if ((y0 > y && y1 < y) || (y0 < y && y1 > y))
{
double x = (y - y0) * (x1 - x0) / (y1 - y0) + x0;
list.Add(new DataPoint(x, y));
}
}
list.Sort((a, b) => Math.Sign(a.XValue - b.XValue));
chart1.Series[0].Points.Clear();
chart1.Series[0].ChartType = SeriesChartType.Range;
chart1.Series[0].Color = Color.Red;
chart1.Series[0].BorderColor = Color.Cyan;
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisX.Interval = 1;
for (int i = 0; i < list.Count; i++)
{
double xx = list[i].XValue;
double yy = list[i].YValues[0];
if (yy > y)
{
chart1.Series[0].Points.AddXY(xx, y, yy);
}
else
{
chart1.Series[0].Points.AddXY(xx, yy, yy);
}
}
chart1.ChartAreas[0].AxisY.StripLines.Add(new StripLine { IntervalOffset = y, Interval = 0, BorderColor = Color.Orange, BorderWidth = 2 });
}
如下图判断直线和(x0,y0)与(x1,y1)之间的直线相交,情况1为(y0< y& y1> y1> y)
,情况2为(y0> y& y1< y)
。在情况1和情况2中,它们彼此相交。在情况3和情况4中,它们彼此不相交。
As in the below drawing to judge whether the straight line and a line between (x0,y0) and (x1,y1) intersect, case 1 is (y0 < y && y1 > y)
and case 2 is (y0 > y && y1 < y)
. In case 1 and case 2, they intersect each other. In case 3 and case 4, they don't intersect each other.
这篇关于如何填充直线和曲线下的所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!