问题描述
我在 Windows 窗体中使用图表组件.
I am using the Charts component in Windows Forms.
我使用
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
.
如何显示 straight line
和 curve
下的所有内容?
How do I show everything over straight line
and under curve
filled?
列填充整个区域,而不仅仅是直线
上方.
Column fills the whole area, not just above straight line
.
示例:
推荐答案
我有个想法,使用 SeriesChartType.Range
如下.
I have an idea that use SeriesChartType.Range
as follow.
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)
并且情况 2 是 (y0 > y && y1 .在情况 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.
这篇关于如何填充直线上和曲线下的所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!