Canvas2D.clearRect() with which i can erase just the last candle and repaint it. Right? But what if i want to add an horizontal line going through all the chart, plus a price tag to show where the new price is. Should i erase the whole chart and repaint it just to draw a line a nd a price tag? Isnt that unefficient?
It depends... Of course you can erase an individual candle by Canvas2D.ClearRect. This works fine when there is nothing else in the cleared rectangle. If there is, such as another line or the axis grid, it is easier to erase the entire chart and redraw everything. Unless you have millions of data points you will not notice any delay.
I dont understand well this canvas thing.
Imagine you are a painter and have a canvas in front of you and paint on this canvas - it's the same here: the Canvas is surface on which you are painting using pens and brushes. BUT: There are several kinds of canvases. A Paintbox, for example, is a control which is designed for user painting. It provides you with a canvas but this canvas is not necessarily persistent. Imagine that it only "exists" during the painting cycle, i.e. in the OnPaint event of the Paintbox (similar with TForm or TPanel). So rather than painting in the OnClick event of a button
procedure TForm1.Button1Click(Sender: TCanvas);
begin
Paintbox1.Canvas.Line(0, 0, Paintbox1.Width, paintbox1.Height);
end;
you should always paint in the OnPaint event handler:
procedure TForm1.Paintbox1Paint(Sender: TCanvas);
begin
Paintbox1.Canvas.Line(0, 0, Paintbox1.Width, paintbox1.Height);
end;
Why? Because the canvas always must be able to redraw itself at any time requested. When you paint in the OnClick event the drawing probably does appear, but when the user temporarily drags another window over your drawing the Paintbox is forced to redraw itself - but the code of the OnClick event is no longer available, and your drawing will be erased.
In your special case, however, things are different since you seem to be painting on the canvas of a bitmap. This kind of canvas is not linked to a control and is always existent. You can draw on it at any time, and the pixels that you change this way will remain changed. But you must not forget to copy the bitmap onto the canvas of the control on which you want to see your drawing. And, of course, you do this on the OnPaint event of the control:
procedure TForm1.PaintboxPaint(Sender: TObject);
begin
Paintbox1.Canvas.Draw(0, 0, Your_Bitmap);
end;
And to trigger the OnPaint event you call the control's Invalidate method:
procedure TForm1.Button1Click(Sender: TObject);
begin
YourBitmap.Canvas.Line(0, 0, YourBitmap.Width, YourBitmap.Height);
Paintbox1.Invalidate;
end;