簡體   English   中英

將在圖表控件上繪制的點寫入文本文件,但是每個坐標只能寫入一次嗎?

[英]Write points drawn over a chart control to a text file the but only once each coordinate?

在按鈕單擊事件中:

private void chart1_MouseClick(object sender, MouseEventArgs e)
{
    if (outofrange == false)
    {
        valuePoints.Add(new PointF(X, Y));
        paintToCalaculate = true;

        if (X > 0 && Y > 0)
        {
            chart1.Invalidate();
            SavePointsCoordinates();
        }
    }
}

和SavePointsCoordinates方法:

StreamWriter w;
int countPoints = 0;

private void SavePointsCoordinates()
{
    if (drawPoints.Count > 0)
    {
        foreach (Point p in drawPoints)
        {
            countPoints++;
            w = new StreamWriter(@"c:\chart\chartData.txt",true);
            w.WriteLine("Point " + countPoints + "X = " + p.X + " Y = " + p.Y);
        }

        w.Close();
    }
}

問題是,如果我將單擊更多按鈕,然后一次不添加新點,它將繼續向文本文件添加相同的坐標。

實際上,它已經將相同的點坐標已添加到按鈕單擊事件中的valuePoints上。 valuePoints是列表

drawPoints是我在chart1繪畫事件中使用的List:

Pen pen = new Pen(Color.Blue, 2.5f);
SolidBrush myBrush = new SolidBrush(Color.Red);

private void chart1_Paint(object sender, PaintEventArgs e)
{
    if (paintToCalaculate)
    {
        Series s = chart1.Series.FindByName("dummy");

        if (s == null)
        {
            s = chart1.Series.Add("dummy");
        }

        drawPoints.Clear();
        s.Points.Clear();

        foreach (PointF p in valuePoints)
        {
            s.Points.AddXY(p.X, p.Y);
            DataPoint pt = s.Points[0];
            double x = chart1.ChartAreas[0].AxisX.ValueToPixelPosition(pt.XValue);
            double y = chart1.ChartAreas[0].AxisY.ValueToPixelPosition(pt.YValues[0]);
            drawPoints.Add(new Point((int)x, (int)y));
            s.Points.Clear();
        }

        paintToCalaculate = false;
        chart1.Series.Remove(s);
    }

    foreach (Point p in drawPoints)
    {
        e.Graphics.FillEllipse(Brushes.Red, p.X - 2, p.Y - 2, 4, 4);
    }

    if (drawPoints.Count > 1)
    {
        e.Graphics.DrawLines(pen, drawPoints.ToArray());
    }
}

我要做的是將我在圖表上繪制的所有點坐標都寫入文本文件,然后在構造函數中讀回文本文件,將點繪制回圖表,並繪制它們之間的線。

我不確定繪制的點和線之間的順序是否有意義。 只是將所有坐標寫入文本文件並回讀。

這是保存List<Point>的推薦方法。 Point是可Serializeable ,因此以下幾行將保存並加載List<Point> points

string yourPointsFile = "d:\\myPoints.xml";
XmlSerializer xmls = new XmlSerializer(typeof(List<Point>));

// save the points maybe when closing the program:
using (Stream writer = new FileStream(yourPointsFile, FileMode.Create))
{
    xmls.Serialize(writer, points);
    writer.Close();
}

// load the points at startup:

using (Stream reader = new FileStream(yourPointsFile, FileMode.Open))
{
    points = xmls.Deserialize(reader);
    reader.Close();
}

收集正確的點,添加或不添加它們是另一個問題。 為避免重復,您可以隨時使用

if (!points.Contains(somePoint) ) points.Add(somePoint);

保存和加載的點列表將具有相同的順序。 因此,如果要使用它們來繪制線條(順序確實很重要),則生成的線條應該相同。

我認為保存每次點擊的積分沒有道理,但這是您的程序。 也許在關閉程序時保存它們就足夠了。

如果您這樣做時想保存PointF ,只需將兩個引用都更改為PointPointF

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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