Page 1 of 1

Custom drawing

Posted: Thu Mar 09, 2006 7:55 pm
by 9042560
Newbee here, so plase bear with me:)

I have a TFastLine series displaying a running graph. Near the BottomAxis I need to draw some letters (one at the time) for some of the values displayed. The letters draw are like a labels for the values I guess, but I need the label at the bottom of the graph, and scrolling 'together' with the value it describes.

Been reading help and examples, but right now I feel like I'm getting information overload:) and can not figure out how to achive this or even where to start.

regards,
Leo

Posted: Fri Mar 10, 2006 9:52 am
by narcis
Hi Leo,

I'm not sure of what are you trying to achieve but you could do something like the code below to custom draw on TeeChart's canvas. However, you could also consider using TAnnotationTool objects (only available in TeeChart Pro) or custom labels as shown at All Features\Welcome!\Axes\Labels\Custom Labels example in TeeChart's features demo. You'll find the features demo at TeeChart's program group.

Code: Select all

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, TeEngine, TeeSurfa, TeeComma, ExtCtrls, TeeProcs, Chart, Series;

type
  TForm1 = class(TForm)
    Chart1: TChart;
    TeeCommander1: TTeeCommander;
    Series1: TFastLineSeries;
    procedure FormCreate(Sender: TObject);
    procedure Chart1AfterDraw(Sender: TObject);
    procedure Chart1Zoom(Sender: TObject);
    procedure Chart1UndoZoom(Sender: TObject);
    procedure Chart1Scroll(Sender: TObject);
  private
    { Private declarations }
    procedure CalculateLabels(Series: TChartSeries);
  public
    { Public declarations }
  end;

type
  TCustomText = record
    X,Y: Integer;
    Text: String;
  end;

var
  Form1: TForm1;
  Labels: array[0..49] of TCustomText;
  count: Integer;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Series1.FillSampleValues(Length(Labels));
  CalculateLabels(Series1);
end;

procedure TForm1.Chart1AfterDraw(Sender: TObject);
var i: Integer;
begin
  for i:=0 to count-1 do
    Chart1.Canvas.TextOut(Labels[i].X,Labels[i].Y,Labels[i].Text);
end;

procedure TForm1.CalculateLabels(Series: TChartSeries);
var i: Integer;
begin
  Chart1.Draw;
  count:=0;

  for i:=0 to Series.Count-1 do
    if Series.YValue[i] >300 then
    begin
      Labels[count].X:=Series.CalcXPos(i);
      Labels[count].Y:=Series.CalcYPos(i);
      Labels[count].Text:='Custom text '+IntToStr(i);
      inc(count);
    end;
end;

procedure TForm1.Chart1Zoom(Sender: TObject);
begin
  CalculateLabels(Series1);
end;

procedure TForm1.Chart1UndoZoom(Sender: TObject);
begin
  CalculateLabels(Series1);
end;

procedure TForm1.Chart1Scroll(Sender: TObject);
begin
  CalculateLabels(Series1);
end;

end.