Page 1 of 1

Chart custom graphics drawing

Posted: Wed Jan 14, 2015 4:30 pm
by 16570117
Hi,

I am doing some custom drawing on my chart control in connection with mouse movements. In other words, whenever I move mouse I am drawing some graphics on my chart.

Right now I am handling chart's OnBeforeDrawSeries and OnMouseMove.

eg. (just an example to show my apporach):

Code: Select all

procedure xxx.OnMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  LastPt.X := x;
  LastPt.Y := y;
  Chart.Repaint;
end;

procedure xxx.OnBeforeDrawSeries(Sender: TObject);
begin
  Chart.Canvas.TextOut(LastPt.x, LastPt.Y, 'Some text');
end;
At first look this method seems to be fine but it looks like it takes some time to preocess Chart.Repain and all custom graphics seems to be drawing quite slow. I checked out my methods and measured time to process and everythings looks reasonable (<1us).

I am not drawing on canvas directly in OnMouseMove event because graphics gets messed up if Chart.Repaint is called from any other method. Therefore I just execute Chart.Repaint to start events such as BeforeDrawSeries and AfterDrawGraph and do drawing there.

Is there any better approach to do that? What about declaring new class derived from Tchart and use some protected methods? Any suggestion?

Thank you in advance.

Kind regards.

Re: Chart custom graphics drawing

Posted: Thu Jan 15, 2015 9:36 am
by yeray
Hello,

This seems to work fine for me here:

Code: Select all

uses Series;

var LastPt: TPoint;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.AddSeries(TBarSeries).FillSampleValues();
end;

procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  LastPt.X := x;
  LastPt.Y := y;
  Chart1.Repaint;
end;

procedure TForm1.Chart1BeforeDrawSeries(Sender: TObject);
begin
  Chart1.Canvas.TextOut(LastPt.x, LastPt.Y, 'Some text');
end;
This way you are redrawing all the chart each time the mouse is moved. This is the easiest way to go but it may be slow if you have many points in your chart needing some time for each repaint.

Re: Chart custom graphics drawing

Posted: Thu Jan 15, 2015 9:52 am
by 16570117
Yeah sure. I totaly forgot that all series are repainted too. I have up to 16 series with up to 4000 samples. So although my custom drawing is super fast it will always take some time to paint all 16 series. It makes sense.

Thanks for the hint and test of my code.

Re: Chart custom graphics drawing

Posted: Thu Jan 15, 2015 10:23 am
by 16570117
Is there any performance difference in calling chart.Repaint or Chart.Invalidate? What is the difference between them chart implementation wise?

Thanks

Re: Chart custom graphics drawing

Posted: Thu Jan 15, 2015 10:40 am
by yeray
PoLabs wrote:Is there any performance difference in calling chart.Repaint or Chart.Invalidate? What is the difference between them chart implementation wise?
The Repiant method ends calling Invalidate so there isn't much difference between them.