簡體   English   中英

如何只在同一圖表系列中的特定點之間繪制一條線

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM