Difference between revisions of "TAChart Runtime FAQ"

From Free Pascal wiki
Jump to navigationJump to search
(→‎Miscellaneous: Fixed axis limits)
(→‎Fixed axis limits: Add axis at runtime)
Line 86: Line 86:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
=== How to add an axis at runtime ===
 +
A chart can contain several axes which are stored in its <code>AxisList</code>. Since <code>AxisList</code> inherits from <code>TCollection</code> you can use its <code>Add</code> method to create a new axis:
 +
<syntaxhighlight>
 +
uses
 +
  TAChartAxis, TAChartUtils;
 +
 +
function AddAxis(AParentChart: TChart; ATitle: String; AAlignment: TChartAxisAlignment): TChartAxis;
 +
begin
 +
  // Create a new axis
 +
  Result := AParentChart.AxisList.Add;
 +
  // Axis orientation: calLeft, calTop, calRight or calBottom
 +
  Result.Alignment := AAlignment; 
 +
  // Axis title
 +
  Result.Title.Caption := ATitle;
 +
  Result.Title.Visible := true;
 +
  // Rotate title of a vertical axis
 +
  case AAlignment of
 +
    calLeft  : Result.Title.LabelFont.Orientation := +900;
 +
    calRight : Result.Title.LabelFont.Orientation := -900;
 +
  end;
 +
  // Show axis line
 +
  Result.AxisPen.Visible := true; 
 +
end;</syntaxhighlight>
 +
If you want to assign the new axis to AxisIndexX or AxisIndexY of a series you can use the property <code>Index</code> of the newly created axis.
  
 
== Miscellaneous ==
 
== Miscellaneous ==

Revision as of 22:22, 30 August 2018

This wiki is trying to answer frequently asked questions related to usage of TAChart at runtime

Series

How to add a series at runtime?

Just create the series, set its properties and call the chart method AddSeries

uses
  TATypes, TASeries;

function AddLineSeries(AChart: TChart; ATitle: String): TChartSeries;
begin
  Result := TLineSeries.Create(AChart.Owner);
  with TLineSeries(Result) do
  begin
    // Series title for the legend
    SeriesTitle := ATitle;
    // Show data point markers (red fill color, black border)
    ShowPoints := true;  
    Pointer.Brush.Color := clRed;
    Pointer.Pen.Color := clBlack;
    Pointer.Style := psCircle;
    // Show red line segments connecting the data points
    ShowLines := true;   
    LinePen.Style := psSolid;
    SeriesColor := clRed;
  end;
  // Add new series to the chart
  AChart.AddSeries(Result);
end;

How to iterate through the series of a chart?

The series are accessible via the array-like property Series of the chart. Note that this returns only the most fundamental series type, TBasicChartSeries, and a type-cast may be necessage before being able to access the series properties.

The following example re-colors all line series in red:

uses
  TASeries;
var 
  i: Integer;
begin
  for i := 0 to AChart.SeriesCount-1 do
    if AChart.Series[i] is TLineSeries then
      TLineSeries(AChart.Series[i]).SeriesColor := clRed;
end;

If you prefer the more modern iterators you can use the syntax

uses
  TASeries, TAEnumerators;
var
  ser: TCustomSeries;
begin
  for ser in CustomSeries(AChart) do
    TLineSeries(ser).Series.Color := clRed;
end;

How to delete/hide a series at runtime?

  • If you don't need the series any more just destroy it: series.Free.
  • If you want to keept it for other usage, for example to insert it into another chart, call Chart.DeleteSeries(series).
  • If you want to hide it, but keep it in the chart, set the property Active of the series to false.

Axes

Fixed axis limits

Normally the limits of the chart axes are determined such that the chart covers the available area as much as possible. But sometimes it is better to keep the axis limit frozen, independent of the data range.

This code uses the Extent of the chart to define a constant range for the y axis between 0 and 100. The first example manipulates the Extent consecutively, the second one simultaneously by using an intermediate variable. Note that the first method may crash the program in the third line if, for some reason, the new maximum is smaller than the currently active minimum. Note that the second code freezes also the x axis; you'll have to call Chart1.Extent.UseXMin := false and Chart1.Extent.UseXmax := false to release it again.

Chart1.Extent.YMax := 100;
Chart1.Extent.YMin := 0;
Chart1.Extent.UseYMax := true;
Chart1.Extent.UseYMin := true;

or:

var
  ex: TDoubleRect;
begin
  ex := Chart1.GetFullExtent;
  ex.b.y := 100;
  ex.a.y := 0;
  Chart1.Extent.FixTo(ex);
end;

How to add an axis at runtime

A chart can contain several axes which are stored in its AxisList. Since AxisList inherits from TCollection you can use its Add method to create a new axis:

uses
  TAChartAxis, TAChartUtils;

function AddAxis(AParentChart: TChart; ATitle: String; AAlignment: TChartAxisAlignment): TChartAxis;
begin
  // Create a new axis
  Result := AParentChart.AxisList.Add;
  // Axis orientation: calLeft, calTop, calRight or calBottom
  Result.Alignment := AAlignment;   
  // Axis title
  Result.Title.Caption := ATitle;
  Result.Title.Visible := true;
  // Rotate title of a vertical axis
  case AAlignment of
    calLeft  : Result.Title.LabelFont.Orientation := +900;
    calRight : Result.Title.LabelFont.Orientation := -900;
  end;
  // Show axis line
  Result.AxisPen.Visible := true;  
end;

If you want to assign the new axis to AxisIndexX or AxisIndexY of a series you can use the property Index of the newly created axis.

Miscellaneous

What can I do to avoid compiler error "Identifier not found 'TDoublePoint'"?

TDoublePoint is declared in unit TAChartUtils, add it to the uses clause of the unit. It also contains the declaration of

  • TDoubleRect which is needed for chart extents.

Other declarations, often not found when working with run-time code, are

  • TSeriesMarksStyle for the Style of axis and series Marks --> in unit TAChartUtils
  • TSeriesPointerStyle for the series pointer styles, e.g. psCircle, psRectangle --> in unit TATypes
  • geometrical functions (creation of TDoublePoint and TDoubleRect, overloaded operators for TPoint and TDoublePoints) are in unit TAGeometry