Estou tentando desenhar um gráfico em tempo real.
Nesse gráfico eu vou desenhando o sinal do ponto (0,y)
até ao ponto (Width,y)
usando um cursor vermelho para saber onde está desenhando atualmente.
Quando o gráfico atingir o valor da propriedade Width
, deve retornar ao início (ponto(0,y))
.
Ao iniciar novamente o desenho do gráfico eu devo ir apagando as colunas de pixels
a frente do cursor vermelho.
Para exemplificar estou anexando o código:
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);
}
}
Há dois problemas com o código:
- O cursor vermelho é desenhado várias vezes durante todo o percurso fazendo um borrão vermelho na tela
- O jeito que eu estou apagando os próximos pixels não funciona.
Alguém pode me ajudar com esse problema ou dar dicas da resolução? Procurei em vários fóruns e não encontrei nada relacionado.