Recent

Author Topic: Mapping an image onto a coordinate of TChart  (Read 1791 times)

sunbreezy393

  • New Member
  • *
  • Posts: 11
Mapping an image onto a coordinate of TChart
« on: January 03, 2016, 04:52:53 pm »
I was wandering if anyone would know how to map an image onto a coordinate of a graph in TChart ?

wp

  • Hero Member
  • *****
  • Posts: 7968
Re: Mapping an image onto a coordinate of TChart
« Reply #1 on: January 03, 2016, 08:12:14 pm »
What do you mean? Paint an image as a datapoint of a lineseries? TLineSeries has a special event for this purpose: OnDrawPointer.
Code: Pascal  [Select][+][-]
  1. var
  2.   DatapointImage: TBitmap;
  3.   BitmapFileName: string;
  4.  
  5. procedure TForm1.FormCreate(Sender: TObject);
  6. begin
  7.   DatapointImage := TBitmap.Create;
  8.   DataPointImage.Transparent := true;
  9.   DatapointImage.LoadFromFile(BitmapFileName);
  10. end;
  11.  
  12. procedure TForm1.FormDestroy(Sender: TObject);
  13. begin
  14.   DatapointImage.Free;
  15. end;
  16.  
  17. procedure TForm1.Chart1LineSeries1DrawPointer(ASender: TChartSeries;
  18.   ACanvas: TCanvas; AIndex: Integer; ACenter: TPoint);
  19. begin
  20.   ACanvas.Draw(
  21.     ACenter.X-DatapointImage.Width div 2,
  22.     ACenter.Y-DatapointImage.Height div 2,
  23.     DatapointImage
  24.   );
  25. end;

This example uses a form with a chart and a lineseries. In the lineseries, show the pointer by setting ShowPoints to true. Note that the bitmap pointer is painted in addition to the standard data pointer. So, if you want to hide the standard pointer, reduce its size (or use the pointer style psPoint introduced some time ago), or set its Brush.Style to bsClear and its Pen.Style to psClear. Depending on the size of the image you may also want to increase to Margins of the chart in order to avoid truncating of the images at the chart borders.
« Last Edit: January 03, 2016, 08:42:45 pm by wp »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

 

TinyPortal © 2005-2018