[英]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.