Graph point and line

I want to create a 3-point chart and then draw a line. it will be like this:! [C # form diagram] [1] But my coding barely draws a line and no points. please, help

public partial class Form5 : Form
{
    public Form5()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Point[] pts = { new Point(150, 12), new Point(130, 15), new Point(160, 18) };
        //int count = 0;

      //   pts[count] = new Point((int)NumericUpDown1.Value, (int)NumericUpDown2.Value);
        for (int i = 0; i < pts.Length; i++)
        {
            if (i != 0)
            {
                this.CreateGraphics().DrawLine(new Pen(Brushes.Black, 3), pts[i - 1], pts[1]);
            }
            else
            {
                this.CreateGraphics().DrawLine(new Pen(Brushes.Black, 3), pts[i], pts[1]);
            }
        }
    }
}

      

+3


source to share


2 answers


The main problem in your code is hardcoded pts[1]

. You should use instead pts[i]

. And remember: indexing an array in C # starts at zero.

Also you don't need to create graphics at every iteration. Declare a local variable and move it outside of the loop. And instead of checking the value of the loop at each iteration, you can move the drawing of the first point outside the loop as well. Then you should be able to handle other points in the same way:

var g = this.CreateGraphics();
var blackPen = new Pen(Brushes.Black, 3);

g.DrawLine(blackPen, pts[0], pts[0]);

for(i = 1; i < pts.Length; i++)
   g.DrawLine(blackPen, pts[i - 1], pts[i]);

      




Note. You tagged the question with a Linq tag. You can of course use Linq to create point pairs and use those pairs to draw lines:

var lines = pts.Zip(pts.Skip(1), (a,b) => new { a, b });

foreach(var line in lines)
    g.DrawLine(blackPen, line.a, line.b);

      

0


source


Here's the correct code:

// keep your data at class level:
// lists are more flexible than arrays:
List<Point> pts = new List<Point>();

 // all drawing in the paint event:
private void Form1_Paint(object sender, PaintEventArgs e)
{

    // put the pen creation in a using scope:
    using ( Pen Pen = new Pen(Color.Red, 3f) )
    {  
        // make the corners nicer:
        pen.MiterLimit = pen.Width / 2f;

        // drawing all lines in one call greatly improves quality.
        // we need at least two points:
        if (pts.Count > 1) e.Graphics.DrawLines( Pen, pts);
    }
}

// trigger the drawing when needed, eg when you have added more points:
private void button1_Click(object sender, EventArgs e)
{
    // maybe add more points..
    pts.AddRange( new Point[]  
                { new Point(150, 12), new Point(130, 15), new Point(160, 18) } );

    // trigger the paint event
    this.Invalidate();
}

      

Consider drawing on Panel

! This will allow you to move it around and place all controls regardless of your Form

..

If you are serious about creating a chart, consider using a Chart

toolbox data buffer control!

It has everything you need for a real graph, including labeled axes and a lot of display style.

To use your data, all it takes is a few lines:



using System.Windows.Forms.DataVisualization.Charting;
//..
private void button2_Click(object sender, EventArgs e)
{
    chart1.Legends.Clear();
    chart1.Series[0].ChartType = SeriesChartType.FastLine;
    chart1.Series[0].Color = Color.Red;
    chart1.Series[0].BorderWidth = 3;

    chart1.Series[0].Points.AddXY(130, 15);
    chart1.Series[0].Points.AddXY(150, 12);
    chart1.Series[0].Points.AddXY(160, 18);
}

      

Note that I ordered points, but this is not necessary for all chart types. I use Fastline and it works with your original order. It can even display multiple points with the same X-Value ..! And the curse scales automatically.

Also note that the chart plots Y-values โ€‹โ€‹naturally, that is, bottom to top, whereas GDI draws from top to bottom!

Here is a screenshot of both ways to show the diagram:

enter image description here

-1


source







All Articles