Tutorial 5 - Legend design

  Contents page 
  Previous | Next 


See the coded example, Legend control, in the Visual Basic 5 demos folder for a working example of some of the techniques outlined in this Tutorial.

Contents

Legend control

Editor page
Positioning the Legend
Number of rows in a horizontal Legend

Customising Legends

OnGetLegendRect event
OnGetLegendText event
OnGetLegendText event
OnClickLegend event


Legend control

Editor page

Legend parameters are accessible via the Chart Editor, Chart tab, Legend page.

   

Legend parameters. See Legend component help for more information

Legend Style
Legend default Style "Automatic" will put Series point values in the Legend when there is only one Series in the Chart. When the Chart contains more than one Series, "Automatic" will put Series names in the Legend. In the Editor use the Dropdown Combobox to obtain values other than the default. If you change the Legend Style to display values and there are more than one Series in your Chart, TeeChart Pro will display the values of the first Series. You may modify the display using custom options. See Customising Legends

Chart1.Legend.LegendStyle := lsLastValues;
//Puts the last value of each Series in the Legend box

Text Style
See the TextStyle property for a list of possible Legend text styles.

Positioning the Legend

Alignment

There are 4 default positions available using the Alignment property, Top, Bottom, Left and Right. Right is the default position. The default positioning of the Legend will always be outside the Chart. See the section about customising Legends for more information about positioning Legends.

Resize Chart
The Resize Chart property, when not enabled, will draw the legend within the Chart frame area. Whilst this may be satisfactory for some Legend positioning requirements, better control of Legend positioning in relation to the Chart frame can be achieved by using the Legend HorizMargin and VertMargin properties.

HorizMargin & VertMargin
Horizmargin applies to Left and Right aligned Legends. VertMargin applies to Top and Bottom aligned margins. Changing the Horizmargin property value will move the Chart frame in relation to the Legend, NOT vice versa. Thus, making a Horizmargin value negative will move the Chart over the Legend (increasing the size of the Chart rectangle area). However, the properties are not intended for repositioning of the Legend over the Chart, better to use the techniques outlined in Customising Legends.

Number of rows in a horizontal Legend

When the Legend is aligned horizontally (top or bottom), the number of rows can be specified:

Chart1.Legend.MaxNumRows:=3;

By default, MaxNumRows is 0 (zero), meaning the Legend will show all values using as many rows as necessary.


Customising Legends

Legend events offer the option to completely control define the Legend appearance and content.

OnGetLegendRect event

The Legend outer rectangle, permits changes to the overall size and position of the Legend box. Use in conjunction with OnGetLegendPos to reposition the Chart Legend and contents.

eg.
procedure TForm1.Chart1GetLegendRect(Sender: TCustomChart; var Rect: TRect);
begin
  //This moves the Legend box to the left (leaving the contents where they were !)
  //Set Chart1.Legend.ResizeChart := False; to disable resizing of the Chart
  //thus placing the Legend inside the Chart rectangle
  Rect.Left := Rect.Left - 100;
  Rect.Right := Rect.Right - 100;
end;

OnGetLegendPos event

Modifies the contents of the Legend. The following example could be used with the code above to move Legend contents to the new Legend rectangle.

procedure TForm1.Chart1GetLegendPos(Sender: TCustomChart; Index: Integer;
  var X, Y, XColor: Integer);
begin
  //Moves the Legend contents to the left by 100 pixels use with OnGetLegendRect
  X := X - 100;
end;

OnGetLegendText event

Modifies the text of the Legend contents.

procedure TForm1.Chart1GetLegendText(Sender: TCustomAxisPanel;
  LegendStyle: TLegendStyle; Index: Integer; var LegendText: String);
begin
 //Modify Legend text
 LegendText := LegendText + IntToStr(Index);
end;

When placing the Legend within the Chart rectangle area, bear in mind that the Legend paints before Series and Axes and will appear below either of those at any intersection point.

OnClickLegend event

Picks up a Legend item when clicking on the Legend.

procedure TForm1.Chart1ClickLegend(Sender: TCustomChart;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
Var tmp:Integer;
begin
  tmp:=Chart1.Legend.Clicked( x,y ) ;
  if tmp<>-1 then ShowMessage( 'Clicked legend item: '+ Chart1.FormattedLegend( tmp ) );
end;



© 1998 teeMach SL. All rights reserved.