Draw Text outside Chart (custom legend)

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
SAR
Newbie
Newbie
Posts: 5
Joined: Tue Dec 21, 2004 5:00 am

Draw Text outside Chart (custom legend)

Post by SAR » Fri Feb 10, 2006 5:45 pm

I need to draw my own text (in a box would be nice) outside the chart boundaries to show additional notes about the data in the chart. It needs to print WYSIWYG.

I have been looking at lots of demos and searching help, and experimenting with various methods in my own code but I cannot figure out how to do this. All I really need is 1) a way to locate the drawing position on the canvas - in the margins (I am using an expanded right margin for this); and 2) a method for drawing the text in the selected position.

Can anyone help me with this? I believe it is pretty basic but I haven't seen any demos that do exactly this, common as it would seem.

Thanks.
Stephen Rogers
LRS Solutions, Inc.

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Mon Feb 13, 2006 9:02 am

Hi Stephen,

I'd suggest you to use a TRectangleTool doing something like what's done in the code snippet below. If you want to place the rectangle in some special way please let us know so that we can try to help you.

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
begin
  Series1.FillSampleValues();

  ChartTool1.Text:='Hello World!';

  ChartTool1.Width:=100;
  ChartTool1.Height:=20;

  Chart1.Draw;
end;

procedure TForm1.Chart1AfterDraw(Sender: TObject);
begin
  ChartTool1.Top:=Chart1.Legend.Top - ChartTool1.Height;
  ChartTool1.Left:=Chart1.ChartRect.Right-ChartTool1.Width;
end;
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

SAR
Newbie
Newbie
Posts: 5
Joined: Tue Dec 21, 2004 5:00 am

Post by SAR » Mon Feb 13, 2006 6:55 pm

Narcís,

This code sample isn't working for me. I assumed that ChartTool1 is created somewhere using TRectangleTool.Create, and added a line to do that (ChartTool1 := TRectangleTool.Create(self) ). That seems to work, but when I try to execute the first line of the event code, it crashes immediately with an access violation - Read of address 00000000 - even though I can inspect all three variables directly.

It also seems interesting that Chart1.Legend.Top - ChartTool1.Height would yield a negative number. In my example, Chart1.Legend.Top is 84 and ChartTool1.Height is 100.

Do I have to turn on Legends somewhere in order to get this code to work properly? Note that I don't want a legend - just a list of strings in the margin next to the table. I can get a margin to appear but have no idea how to locate the rectangle in it.

Aside from not understanding why I'm getting an access error on this, I just don't quite have enough context here to understand which coordinates are which. I can only find a couple of lines of basic help description of how TRectangleTool is supposed to work, and no examples anywhere on the web. I'm groping in the dark here - can you provide a little more detailed code sample or a pointer to more complete help documentation?


Thanks - Stephen
Stephen Rogers
LRS Solutions, Inc.

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Tue Feb 14, 2006 8:44 am

Hi Stephen,
This code sample isn't working for me. I assumed that ChartTool1 is created somewhere using TRectangleTool.Create, and added a line to do that (ChartTool1 := TRectangleTool.Create(self) ). That seems to work, but when I try to execute the first line of the event code, it crashes immediately with an access violation - Read of address 00000000 - even though I can inspect all three variables directly.
Yes, I assumed you added a TRectangleTool to the chart using the chart editor. Regarding the error, does it happen in the OnAfterDraw event? Which TeeChart version are you using?
It also seems interesting that Chart1.Legend.Top - ChartTool1.Height would yield a negative number. In my example, Chart1.Legend.Top is 84 and ChartTool1.Height is 100.


Well, that was just an example of what could be done. That's why I asked you for more information about what you wanted to get.
Do I have to turn on Legends somewhere in order to get this code to work properly? Note that I don't want a legend - just a list of strings in the margin next to the table. I can get a margin to appear but have no idea how to locate the rectangle in it.


No, legend is not necessary. Could you please send us an image of what you are trying to achieve so that we can post a more accurate example? You can post your files at [url]news://www.steema.net/steema.public.attachments[/url] newsgroup.
Aside from not understanding why I'm getting an access error on this, I just don't quite have enough context here to understand which coordinates are which. I can only find a couple of lines of basic help description of how TRectangleTool is supposed to work, and no examples anywhere on the web. I'm groping in the dark here - can you provide a little more detailed code sample or a pointer to more complete help documentation?
You may find something at our support site searching in the newsgroups or forums. However, if you send us more detailed information we will try to arrange an example for you.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

SAR
Newbie
Newbie
Posts: 5
Joined: Tue Dec 21, 2004 5:00 am

Post by SAR » Tue Feb 14, 2006 7:31 pm

>> Yes, I assumed you added a TRectangleTool to the chart using the chart editor. Regarding the error, does it happen in the OnAfterDraw event? Which TeeChart version are you using?

The error does occur in the OnAfterDraw event. I could not find the way to add a TRectangleTool from the chart editor but will look again. My TeeChart version is 7.04.

>> No, legend is not necessary. Could you please send us an image of what you are trying to achieve so that we can post a more accurate example? You can post your files at news://www.steema.net/steema.public.attachments newsgroup.

I have posted a mock-up of what I am trying to achieve in the newsgroup above. I look forward to seeing what you recommend from this. It's pretty simple...

Thanks for the help.
Stephen
Stephen Rogers
LRS Solutions, Inc.

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Wed Feb 15, 2006 9:45 am

Hi Stephen,

Thanks for the information, you could use something like:

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
begin
  Series1.FillSampleValues();

  Chart1.MarginRight:=20;

  ChartTool1.Brush.Color:=clWhite;
  ChartTool1.Shape.Transparency:=0;
  ChartTool1.Text:='Line 1' + #13 + 'Line 2' + #13 + 'Line 3' + #13 + 'Line 4';

  Chart1.Draw;
end;

procedure TForm1.Chart1AfterDraw(Sender: TObject);
begin
  ChartTool1.PositionUnits:=muPixels;
  ChartTool1.Shape.Left:=Chart1.ChartRect.Right+20;
  ChartTool1.Shape.Top:=Chart1.ChartRect.Top;
  ChartTool1.Shape.Width:=120;
  ChartTool1.Shape.Height:=150;
end;
I could not find the way to add a TRectangleTool from the chart editor but will look again.
You should just double-click on the chart, go to "tools" tab, select "add", select "other" and select "rectangle".
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

SAR
Newbie
Newbie
Posts: 5
Joined: Tue Dec 21, 2004 5:00 am

Post by SAR » Wed Feb 15, 2006 5:14 pm

Narcis,

Thanks for this code. It points me in a direction that looks promising.

However, as it stands the results are not WYSIWYG - what shows properly on the screen prints completely differently - the text box winds up in the midst of the chart. What other tricks are needed...?

Another more minor issue is that when the chart first comes up the text box is improperly placed upper right halfway off the page, but springs to the correct position when the chart window is resized. What would be causing this?

Stephen
Stephen Rogers
LRS Solutions, Inc.

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Thu Feb 16, 2006 9:24 am

Hi Stephen,
However, as it stands the results are not WYSIWYG - what shows properly on the screen prints completely differently - the text box winds up in the midst of the chart. What other tricks are needed...?


Ok, if you want to custom draw on the canvas when printing you should follow the approach at the example in All Features\Welcome!\Printing\Print Preview\Custom Drawing. This example can be found at the TeeChart features demo which is at the TeeChart program group.
Another more minor issue is that when the chart first comes up the text box is improperly placed upper right halfway off the page, but springs to the correct position when the chart window is resized. What would be causing this?


Are you calling the Draw method as done in my example? This is because, to position the rectangle, we are using some chart properties that may not be set when calling them. That's why Draw method is called, to force the chart to be drawn and those properties being set.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

SAR
Newbie
Newbie
Posts: 5
Joined: Tue Dec 21, 2004 5:00 am

Post by SAR » Fri Feb 17, 2006 5:19 pm

Narcis,

Thanks for all your help - I really appreciate it.

Unfortunately, I cannot find enough documentation regarding TPreviewPanel to have much idea of how it works or fits into the TeeChart picture (I have only a vague idea of what it is from the one sentence description in the user/ref docs). The demo you referred me to is a good lead, but I would be groping in the dark to figure out TPreviewPanel behavior through trial and error, and I have run out of time for this part of the project. It is unfortunate...being stuck on drawing a simple text box that can be printed in the correct spot.

To note, I have been relatively successful at using TeeChart up to now, with a couple dozen query-driven charts embedded into our analytical product, so in general I feel good about TeeChart, but this experience reminds me that even simple things can be complex if one doesn't have enough information on the basics of how something works.

Again, thanks for your help...
Stephen
Stephen Rogers
LRS Solutions, Inc.

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Mon Feb 20, 2006 2:54 pm

Hi Stephen,

The best solution is to use a temporary metafile + StretchDraw method to print charts with custom objects (text, annotation, etc..).

The Rect parameter in StretchDraw defines the drawing rectangle, in this case printer.canvas drawing rectangle.

Code: Select all

    unit Unit1;

    interface

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

    type
        TForm1 = class(TForm)
        Chart1: TChart;
        Series1: TLineSeries;
        ChartTool1: TAnnotationTool;
        TeeCommander1: TTeeCommander;
        procedure CustomClickEvent(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        private
        { Private declarations }
        public
        { Public declarations }
    end;

    var
        Form1: TForm1;

    implementation

    Uses Printers;

    {$R *.dfm}

    procedure TForm1.CustomClickEvent(Sender: TObject);
    var tmpMeta: TMetafile;
    begin
        Chart1.BevelOuter := bvNone;
        Chart1.Frame.Visible := False;
        tmpMeta := Chart1.TeeCreateMetafile(True,Chart1.ClientRect);
        try
            Printer.Orientation := poLandscape;
            Printer.BeginDoc;
            try
                Printer.Canvas.StretchDraw(Rect(10,10,Printer.PageWidth -
10, Printer.PageHeight - 10),tmpMeta);
            finally
                Printer.EndDoc;
            end;
        finally
            tmpMeta.Free;
        end;
    end;

    procedure TForm1.FormCreate(Sender: TObject);
    begin
        Series1.FillSampleValues(10);
        TeeCommander1.ButtonPrint.OnClick := CustomClickEvent;
    end;

    end.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Post Reply