Recent

Author Topic: how to use IChartDrawer  (Read 3325 times)

mtanner

  • Sr. Member
  • ****
  • Posts: 287
how to use IChartDrawer
« on: June 29, 2017, 05:23:26 pm »
I want to draw some shapes on a graph. I found a forum entry showing how to do this with OnAfterDrawBackWakk. The event provides a canvas, and with the coordinate conversion routines GetTransform.AxisToGraph and the ordinary TCanvas functions I can do what I need.

The drawback is that OnAfterDrawBackWall puts my shapes behind the series, but I want them in front. So OnAfterDraw looks to be what I need, but instead of a TCanvas this event supplies an IChartDrawer. Can you please point me at an idiots guide to what an IChartDrawer is and how I use it - ideally I just want to use it like a TCanvas.

wp

  • Hero Member
  • *****
  • Posts: 11854
Re: how to use IChartDrawer
« Reply #1 on: June 29, 2017, 06:12:43 pm »
IChartDrawer provides an infrastructure of graphics calls which allows TAChart to paint itself even if a Canvas is not available. Suppose you want to draw a chart by using OpenGL, then you are faced with the problem that OpenGL does not expose a Canvas, OpenGL has its own graphics procedures. IChartDrawer is an interface put between TAChart and the final drawing device: TAChart calls a method of the IChartDrawer, and this one calls the corresponding painting routine of the device currently used.

These are the methods which IChartDrawer provides:

Code: Pascal  [Select][+][-]
  1. type
  2.   IChartDrawer = interface
  3.     procedure AddToFontOrientation(ADelta: Integer);
  4.     procedure ClippingStart(const AClipRect: TRect);
  5.     procedure ClippingStart;
  6.     procedure ClippingStop;
  7.     procedure DrawingBegin(const ABoundingBox: TRect);
  8.     procedure DrawingEnd;
  9.     procedure DrawLineDepth(AX1, AY1, AX2, AY2, ADepth: Integer);
  10.     procedure DrawLineDepth(const AP1, AP2: TPoint; ADepth: Integer);
  11.     procedure Ellipse(AX1, AY1, AX2, AY2: Integer);
  12.     procedure FillRect(AX1, AY1, AX2, AY2: Integer);
  13.     function GetBrushColor: TChartColor;
  14.     procedure SetDoChartColorToFPColorFunc(AValue: TChartColorToFPColorFunc);
  15.     procedure Line(AX1, AY1, AX2, AY2: Integer);
  16.     procedure Line(const AP1, AP2: TPoint);
  17.     procedure LineTo(AX, AY: Integer);
  18.     procedure LineTo(const AP: TPoint);
  19.     procedure MoveTo(AX, AY: Integer);
  20.     procedure MoveTo(const AP: TPoint);
  21.     procedure Polygon(
  22.       const APoints: array of TPoint; AStartIndex, ANumPts: Integer);
  23.     procedure Polyline(
  24.       const APoints: array of TPoint; AStartIndex, ANumPts: Integer);
  25.     procedure PrepareSimplePen(AColor: TChartColor);
  26.     procedure PutImage(AX, AY: Integer; AImage: TFPCustomImage);
  27.     procedure PutPixel(AX, AY: Integer; AColor: TChartColor);
  28.     procedure RadialPie(
  29.       AX1, AY1, AX2, AY2: Integer;
  30.       AStartAngle16Deg, AAngleLength16Deg: Integer);
  31.     procedure Rectangle(const ARect: TRect);
  32.     procedure Rectangle(AX1, AY1, AX2, AY2: Integer);
  33.     procedure ResetFont;
  34.     function Scale(ADistance: Integer): Integer;
  35.     procedure SetAntialiasingMode(AValue: TChartAntialiasingMode);
  36.     procedure SetBrushColor(AColor: TChartColor);
  37.     procedure SetBrush(ABrush: TFPCustomBrush);
  38.     procedure SetBrushParams(AStyle: TFPBrushStyle; AColor: TChartColor);
  39.     procedure SetFont(AValue: TFPCustomFont);
  40.     procedure SetGetFontOrientationFunc(AValue: TGetFontOrientationFunc);
  41.     procedure SetMonochromeColor(AColor: TChartColor);
  42.     procedure SetPen(APen: TFPCustomPen);
  43.     procedure SetPenParams(AStyle: TFPPenStyle; AColor: TChartColor);
  44.     function GetRightToLeft: Boolean;
  45.     procedure SetRightToLeft(AValue: Boolean);
  46.     procedure SetTransparency(ATransparency: TChartTransparency);
  47.     procedure SetXor(AXor: Boolean);
  48.     function TextExtent(const AText: String): TPoint;
  49.     function TextExtent(AText: TStrings): TPoint;
  50.     function TextOut: TChartTextOut;
  51.  
  52.     property Brush: TFPCustomBrush write SetBrush;
  53.     property BrushColor: TChartColor read GetBrushColor write SetBrushColor;
  54.     property Font: TFPCustomFont write SetFont;
  55.     property Pen: TFPCustomPen write SetPen;
  56.     property DoChartColorToFPColor: TChartColorToFPColorFunc
  57.       write SetDoChartColorToFPColorFunc;
  58.     property DoGetFontOrientation: TGetFontOrientationFunc
  59.       write SetGetFontOrientationFunc;
  60.   end;

Suppose you want to draw a logo in a corner of the chart after drawing is complete: Use the event OnAfterDraw and call the IChartDrawer's PutImage with the image passed as a TFPCustomImage. I don't know which kind of "shapes" you want to draw, but there's a Rectangle, a Pie, a Polygon, an Ellipse - probably all you need. For Text painting you call TextOut which creates an instance of the TChartTextOut class, and then you set the properties in chained calls to methods of this class; Done finally executes painting:

Code: Pascal  [Select][+][-]
  1. type
  2.   TChartTextOut = class
  3.     function Alignment(AAlignment: TAlignment): TChartTextOut;
  4.     procedure Done;
  5.     function Pos(AX, AY: Integer): TChartTextOut;
  6.     function Pos(const APos: TPoint): TChartTextOut;
  7.     function Text(const AText: String): TChartTextOut;
  8.     function Text(AText: TStrings): TChartTextOut;
  9.     function Width(AWidth: Integer): TChartTextOut;
  10.   end;
  11.  

So, in order to paint the text "Drawn by TAChart" at position (10, 10) you call

Code: Pascal  [Select][+][-]
  1.  ChartDrawer.TextOut.Pos(10, 10).Text('Drawn by TAChart').Done;

More documentation is found at http://wiki.lazarus.freepascal.org/TAChart_documentation#Drawers.

Unfortunately TAChart is not very consistent with the parameters of user-defined painting events for historic reasons. Some use a canvas, some use a IChartDrawer. In my point of view the Canvas-related calls should be removed - and I guess I'll do this when I work with TAChart the next time.
« Last Edit: June 29, 2017, 06:16:19 pm by wp »

mtanner

  • Sr. Member
  • ****
  • Posts: 287
Re: how to use IChartDrawer
« Reply #2 on: June 29, 2017, 06:42:48 pm »
Thanks, that seems a good set of information to be getting on with.

 

TinyPortal © 2005-2018