如何在同一图表系列中的特定点之间绘制线条 [英] How to draw a Line only between specific points in the same Chart series
问题描述
我有以下问题:
- 点图的点分布;
- 其中一些需要彼此连接,
a单点可以连接到几个不同的点; - 这些点需要连接到
- A distribution of points plotted on a dot chart;
- Some of those points need to be connected to each other in a way that a single point could be connected to several different points;
- Those points need to be connected with a line;
- Not all points could be connected.
推荐答案
一个系列 可以是点
或 行
,因此我们需要使用两个系列或使用一些技巧。
One Series can either be of type Point
or Line
, so we will need to either use two Series or resort to some tricks.
我建议使用两个系列,因为它是相当简单的代码和维护..
I suggest using two Series, as it is rather simple to code and maintain..
此屏幕截图显示了100个随机点和它们之间的25个随机连接。
This screenshot shows 100 random points and 25 random connections between them.
A 线
一根线,连接全部它的点,一个接一个。所以诀窍是每个连接添加两个点到线系列,在不可见和一些可见的颜色之间交替颜色..
A Line
chart can only draw one line, connecting all its points, one after the other. So the trick is to add two points per conncection to the Line Series, alternating their Colors between invisible and some visible Color..:
我们用其两个系列
和一个好的图例$ c $设置
图表
c>:
First we set up the Chart
with its two Series
and a nice Legend
:
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";
现在我们创建一些随机数据;你将不得不适应你的数据源..:
Now we create some random data; you will have to adapt to your data source..:
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)));
现在我们添加分数,直接:
Now we add the points, straightforward:
foreach (PointF pt in points) SP.Points.AddXY(pt.X, pt.Y);
..和行。这里我(ab)使用 Point
结构来将两个整数索引存储到 points
data ..:
..and the lines. Here I (ab)use a Point
structure to simply store the two integer indices into the points
data..:
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
添加到一系列积分中,但这不是那么简单,
You could instead add LineAnnotations
to one Series of Points, but that's not so simple, I'm afraid..
要删除某个连接,您需要移除两个点:
To remove a connection you would remove both points that make it up:
删除连接 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
这篇关于如何在同一图表系列中的特定点之间绘制线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!