[英]How to draw a Line only between specific points in the same Chart series
我有以下问题:
一个系列可以是 Point
或 Line
类型,因此我们将需要使用两个 Series或采取一些技巧。
我建议使用两个Series,因为它很容易编码和维护。
此屏幕快照显示了100个随机点和它们之间的25个随机连接。
一个Line
图只能画一条线,连接所有的点,一个接一个。 因此,诀窍是为线形每个连接点添加两个点,在不可见颜色和某些可见颜色之间交替改变它们的颜色。
首先,我们用两个Series
和一个漂亮的Legend
设置Chart
:
chart1.Series.Clear();
ChartArea CA = chart1.ChartAreas[0];
// the series with all the points
Series SP = chart1.Series.Add("SPoint");
SP.ChartType = SeriesChartType.Point;
SP.LegendText = "Data points";
// and the series with a few (visible) lines:
Series SL = chart1.Series.Add("SLine");
SL.ChartType = SeriesChartType.Line;
SL.Color = Color.DarkOrange;
SL.LegendText = "Connections";
现在我们创建一些随机数据; 您将不得不适应您的数据源..:
Random R = new Random(2015); // I prefer repeatable randoms
List<PointF> points = new List<PointF>(); // charts actually uses double by default
List<Point> lines = new List<Point>();
int pointCount = 100;
int lineCount = 25;
for (int i = 0; i < pointCount; i++)
points.Add(new PointF(1 + R.Next(100), 1 + R.Next(50)));
for (int i = 0; i < lineCount; i++)
lines.Add(new Point(R.Next(pointCount), R.Next(pointCount)));
现在,我们直接添加要点:
foreach (PointF pt in points) SP.Points.AddXY(pt.X, pt.Y);
..和线。 在这里,我(AB)使用Point
的结构简单地存储两个整数指数进入points
数据..:
foreach (Point ln in lines)
{
int p0 = SL.Points.AddXY(points[ln.X].X, points[ln.X].Y);
int p1 = SL.Points.AddXY(points[ln.Y].X, points[ln.Y].Y);
SL.Points[p0].Color = Color.Transparent;
SL.Points[p1].Color = Color.OrangeRed;
}
}
完成。
您可以改为将LineAnnotations
添加到一个系列的点,但是恐怕并不是那么简单。
要删除连接,您将删除组成它的两个点:
要删除连接rc
使用:
chart1.Series[1].Points.RemoveAt(rc * 2); // remove 1st one
chart1.Series[1].Points.RemoveAt(rc * 2); // remove 2nd one, now at the same spot
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.