Custom line chart

Asked

Viewed 40 times

2

Good morning, I am developing a program in c# that should draw a line chart(x,y) in a picturebox(pcbANALISE) according to the data I have in a list (aquisicaodata[ ]). This chart also has a scroll bar for navigation by graph. To draw I am using the following code.

    int camera;
    List<float> aquisicaodata = new List<float>();
    Graphics g;

    public void DrawANALISE()
    {
        g = pcbANALISE.CreateGraphics();
        g.SmoothingMode = SmoothingMode.AntiAlias;
        var lastPoint1 = new PointF(0F, (float)(2 * pcbANALISE.Height / 5) * 0.5F); //ultimo ponto
        PointF newPoint1 = new PointF(); //novo ponto

        for(int i = camera; i < pcbANALISE.Width/0.2f + camera; i++)
        {
            float porcent;
            if (i < 6000) porcent = aquisicaodata[i] / 5;
            else porcent = 0.5f;
            newPoint1.X = (i-camera+1) * 1f;
            newPoint1.Y = porcent * pcbANALISE.Height;
            using (var p = new Pen(Color.Lime, 0.1f)) g.DrawLine(p, lastPoint1, newPoint1);
            lastPoint1 = newPoint1;
        }
    }

Horizontal scroll bar event code:

    private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
    {
        camera = hScrollBar1.Value * 6000 / 100; // variavel usada para rolagem do grafico
        g.Clear(Color.FromArgb(65,65,65));
        DrawANALISE();
    }

My problem is this: When I roll the scroll, instead of the graph moving left or right, it’s redrawn from the beginning with each new scroll tick. Which is very slow, besides not being in the way intended. Can anyone help me with this problem?? Thank you very much for your attention.

  • Have you tried putting pcbANALISE.SuspendLayout() at the beginning of the method DrawANALISE and then pcbANALISE.ResumeLayout() in the end?

  • I had to put an update method in the scroll event, but the Refresh() method and the Invalidate() method are making it sparkle a lot, which does not allow me to perfectly visualize whether it has improved or not with the adjustment you recommended.

  • if I don’t put the update method the chart is overlaid every time the scroll event is called.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.