Real-time graph problem C#

Asked

Viewed 82 times

0

I’m trying to draw a graph in real time.

On this graph I draw the dot sign (0,y) to the point (Width,y) using a red cursor to know where you are currently drawing. When the graph reaches property value Width, must return to the beginning (ponto(0,y)). When starting the chart design again I should delete the columns of pixels the front of the red cursor.

To illustrate I am attaching the code:

public class GraphControl : Control
{
    int contador = 0;
    public int _minValue;
    public int _maxValue;
    public int _dataCount;
    private List<float> _values1 = new List<float>();

    public GraphControl()
    {
        SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
    }

    public void AddValue(float value1)
    {
        _values1.Add(value1);
        contador++;
        if (contador == 3)
        {
            Invalidate();
            contador = 0;
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        using (var p = new Pen(Color.FromArgb(95, 95, 95), 1.5f)) e.Graphics.DrawRectangle(p, 0, 0, Width - 1, Height - 1);
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        var wid = (float)Width / (float)_dataCount;

        var total = _maxValue - _minValue;

        var lastPoint1 = new PointF(0F, (float)(Height / 2));
        PointF newPoint1 = new PointF();

        for (int i = 0, a = 0; i < _values1.Count; i++, a++)
        {
            float value;
            try
            {
                value = (float)(_values1[i]);
            }
            catch
            {
                value = (float)(_values1[i - 1]);
            }

            var porcent = value / total;

            if (porcent > 1) porcent = 1;
            var hg = porcent * (Height / 4);

            newPoint1.X = (a + 1) * wid;

            if (porcent >= 1) newPoint1.Y = Height / 4 - hg;
            else newPoint1.Y = Height / 4 - hg - 1;

            using (var p = new Pen(Color.Lime, 0.1f)) e.Graphics.DrawLine(p, lastPoint1, newPoint1);

            lastPoint1 = newPoint1;

            //cursor vermelho
            using (var p = new Pen(Color.Red, 2)) e.Graphics.DrawLine(p, new PointF((a+1)*wid,0), new PointF((a+1)*wid,Height));

            //apagando coluna de pixel (com cinza, cor do grafico)
            using (var p = new Pen(Color.FromArgb(53, 53, 53))) e.Graphics.DrawLine(p, new PointF((a + 2) * wid, 0), new PointF((a + 2) * wid, Height));

            if (a == _dataCount) a = 0;
        }
        base.OnPaint(e);
    }
}

There are two problems with the code:

  • The red cursor is drawn several times along the way making a red blur on the screen
  • The way I’m erasing the next pixels no works.

Can anyone help me with this problem or give tips on resolution? I searched several forums and found nothing related.

  • Felipe, think like this, every cycle of drawing or is every completion of a drawn graphic, you need to erase the whole screen and redesign updating it, I think you are keeping the previous drawing.

  • Just friend, I wanted the red cursor to erase everything that comes in front of it (the graphic that was drawn) and not to clean all screen to redesign.

  • How can I redesign a line elsewhere instead of drawing another line?

No answers

Browser other questions tagged

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