Draw Text outside Chart (custom legend)
Draw Text outside Chart (custom legend)
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.
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.
LRS Solutions, Inc.
-
- Site Admin
- Posts: 14730
- Joined: Mon Jun 09, 2003 4:00 am
- Location: Banyoles, Catalonia
- Contact:
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.
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 |
Instructions - How to post in this forum |
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
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.
LRS Solutions, Inc.
-
- Site Admin
- Posts: 14730
- Joined: Mon Jun 09, 2003 4:00 am
- Location: Banyoles, Catalonia
- Contact:
Hi Stephen,
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.
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.
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?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.
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.
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.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?
Best Regards,
Narcís Calvet / Development & Support Steema Software Avinguda Montilivi 33, 17003 Girona, Catalonia Tel: 34 972 218 797 http://www.steema.com |
Instructions - How to post in this forum |
>> 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
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.
LRS Solutions, Inc.
-
- Site Admin
- Posts: 14730
- Joined: Mon Jun 09, 2003 4:00 am
- Location: Banyoles, Catalonia
- Contact:
Hi Stephen,
Thanks for the information, you could use something like:
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;
You should just double-click on the chart, go to "tools" tab, select "add", select "other" and select "rectangle".I could not find the way to add a TRectangleTool from the chart editor but will look again.
Best Regards,
Narcís Calvet / Development & Support Steema Software Avinguda Montilivi 33, 17003 Girona, Catalonia Tel: 34 972 218 797 http://www.steema.com |
Instructions - How to post in this forum |
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
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.
LRS Solutions, Inc.
-
- Site Admin
- Posts: 14730
- Joined: Mon Jun 09, 2003 4:00 am
- Location: Banyoles, Catalonia
- Contact:
Hi Stephen,
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.
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.
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 |
Instructions - How to post in this forum |
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
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.
LRS Solutions, Inc.
-
- Site Admin
- Posts: 14730
- Joined: Mon Jun 09, 2003 4:00 am
- Location: Banyoles, Catalonia
- Contact:
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.
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 |
Instructions - How to post in this forum |