YOUSEFUL / DELPHI / COMPONENTS
TAtlasChart
Line, bar and pie chart control with chart-owned series, gaps for missing points and offscreen rendering.
Description
TAtlasChart draws one or more TAtlasChartSeries as lines, grouped bars or, with a single series, a pie. AddSeries creates a series and returns it; the chart owns and frees it. There is no indexed series property, so keep the references AddSeries returns.
The axes are automatic. The Y range always includes zero, the X range is padded by 5% on each side, and each axis shows five evenly spaced values formatted with at most two decimals; they are not rounded to "nice" numbers. A bar chart whose first series has a caption on every point labels the X axis with those captions instead. Points added with AddMissing break a line and leave out a bar. The legend on the right lists the series captions (the point captions for a pie).
Coordinates must be finite, with magnitude at most 1E100. A pie accepts one series and no negative values; EAtlasChartError reports every rejected input and leaves the chart unchanged. Render draws the same picture on any canvas, which is how you export a chart to an image without showing it.
Not implemented: zoom, scrolling, axis titles, manual axis ranges, tooltips, point hit testing, a chart designer and legend layout for long captions (the legend is a fixed 150-pixel column). Series colours are not changed by TAtlasThemer, and pie slices use a fixed eight-colour palette. Use the chart on the VCL main thread.
Members
| Member | Kind | What it does |
|---|---|---|
Create | constructor | Creates a 640 x 400 chart with a white background, Kind = acLine, an empty title and no series. |
Destroy | destructor | Frees the chart and every series it owns. |
AddSeries | method | Adds an empty series and returns it. |
Clear | method | Removes and frees every series; Kind and Title stay. |
Render | method | Draws the chart into Bounds on any canvas, with the chart's Color, Font, Title and Kind; Paint calls it with the client rectangle. |
BoundsOfData | method | Minimum and maximum X and Y over the non-missing points of all series, without the zero baseline and padding that Render adds. |
SeriesCount | property | Number of series in the chart. |
Kind | property | Chart type; see TAtlasChartKind. |
Title | property | Bold text drawn at the top left. |
Align | property | Standard VCL alignment in the parent. |
Anchors | property | Standard VCL anchors. |
Color | property | Background of the whole chart; the constructor sets clWhite. |
Font | property | Font of the title, the axis values and the legend. |
ParentFont | property | Standard VCL property: use the parent's font. |
OnClick | event | Standard click event. |
OnDblClick | event | Standard double-click event. |
Constructors
Create constructor
constructor Create(AOwner: TComponent); override;Creates a 640 x 400 chart with a white background, Kind = acLine, an empty title and no series. Double buffering is on.
Destructors
Destroy destructor
destructor Destroy; override;Frees the chart and every series it owns. References returned by AddSeries become invalid.
Properties
SeriesCount property
property SeriesCount: Integer read GetSeriesCount;Number of series in the chart.
Kind property
property Kind: TAtlasChartKind read FKind write SetKind default acLine;Chart type; see TAtlasChartKind. Setting acPie raises EAtlasChartError, and keeps the previous kind, when the chart has more than one series or any non-missing negative value. Switching between acLine and acBar always succeeds.
Chart.Kind := acBar;Title property
property Title: string read FTitle write SetTitle;Bold text drawn at the top left. Setting it repaints the chart.
Align property
property Align;Standard VCL alignment in the parent.
Anchors property
property Anchors;Standard VCL anchors.
Color property
property Color;Background of the whole chart; the constructor sets clWhite. TAtlasThemer sets it to the theme surface. Grid and axis lines are fixed greys.
Font property
property Font;Font of the title, the axis values and the legend. The title is drawn bold.
ParentFont property
property ParentFont;Standard VCL property: use the parent's font.
Events
OnClick event
property OnClick;Standard click event. The chart does not report which point or series was clicked.
OnDblClick event
property OnDblClick;Standard double-click event.
Methods
AddSeries method
function AddSeries(const Caption: string; Color: TColor): TAtlasChartSeries;Adds an empty series and returns it. The chart owns the series; do not free it. Caption appears in the legend; Color is used for the line, the point markers and the bars (a pie ignores it). Raises EAtlasChartError when Kind is acPie and the chart already has a series.
Series := Chart.AddSeries('Revenue', clNavy);
Series.Add(1, 120);
Series.Add(2, 135);Clear method
procedure Clear;Removes and frees every series; Kind and Title stay. Discard the series references you hold.
Chart.Clear;
Series := Chart.AddSeries('Forecast', clGreen);Render method
procedure Render(Target: TCanvas; const Bounds: TRect);Draws the chart into Bounds on any canvas, with the chart's Color, Font, Title and Kind; Paint calls it with the client rectangle. The chart does not need a parent or a window handle. Below 160 x 120 pixels only the title is drawn. The plot area is inset 70 pixels on the left, 60 at the top, 150 on the right (the legend) and 48 at the bottom. Without any non-missing point the plot shows No data; a pie whose values add up to zero shows No positive values. Raises EAtlasChartError if the coordinate span overflows a Double.
Bitmap.SetSize(640, 400);
Chart.Render(Bitmap.Canvas, Rect(0, 0, 640, 400));BoundsOfData method
function BoundsOfData(out XMin, XMax, YMin, YMax: Double): Boolean;Minimum and maximum X and Y over the non-missing points of all series, without the zero baseline and padding that Render adds. Returns False, with the outputs set to 0, 1, 0 and 1, when there is no such point.
if Chart.BoundsOfData(XMin, XMax, YMin, YMax) then
Chart.Title := Format('Peak %.0f', [YMax]);Examples
Line chart with a gap
Two series of monthly values; April has no actual figure yet, so the Actual line breaks there.
uses System.Classes, Vcl.Controls, Vcl.Graphics, Atlas.Charts;
function AddRevenueChart(Owner: TComponent; Parent: TWinControl): TAtlasChart;
var
Actual, Target: TAtlasChartSeries;
Month: Integer;
begin
Result := TAtlasChart.Create(Owner);
Result.Parent := Parent;
Result.Align := alClient;
Result.Title := 'Revenue by month';
Actual := Result.AddSeries('Actual', $00876524);
Target := Result.AddSeries('Target', clGray);
for Month := 1 to 6 do
begin
Target.Add(Month, 100 + Month * 10);
if Month = 4 then
Actual.AddMissing(Month) // no figure yet: the line breaks here
else
Actual.Add(Month, 95 + Month * 12);
end;
end;
Bar chart with category labels, saved as PNG
Every point has a caption, so the bars are labelled North, South and East. The chart is never shown.
uses System.Types, Vcl.Graphics, Vcl.Imaging.pngimage, Atlas.Charts;
procedure SaveRegionChart(const FileName: string);
var
Chart: TAtlasChart;
Series: TAtlasChartSeries;
Bitmap: TBitmap;
Png: TPngImage;
begin
Chart := TAtlasChart.Create(nil);
Bitmap := TBitmap.Create;
Png := TPngImage.Create;
try
Chart.Kind := acBar;
Chart.Title := 'Units by region';
Series := Chart.AddSeries('Units', clNavy);
Series.Add(1, 40, 'North');
Series.Add(2, 25, 'South');
Series.Add(3, 35, 'East');
Bitmap.SetSize(640, 400);
Chart.Render(Bitmap.Canvas, Rect(0, 0, 640, 400));
Png.Assign(Bitmap);
Png.SaveToFile(FileName);
finally
Png.Free;
Bitmap.Free;
Chart.Free;
end;
end;
Pie chart
A pie takes a single series with non-negative values; the legend shows the point captions.
uses Vcl.Graphics, Atlas.Charts;
procedure ShowTicketShare(Chart: TAtlasChart);
var
Share: TAtlasChartSeries;
begin
Chart.Clear; // a pie accepts only one series
Chart.Kind := acPie;
Chart.Title := 'Tickets by state';
Share := Chart.AddSeries('Tickets', clBlack); // slices use the built-in palette
Share.Add(1, 52, 'Open');
Share.Add(2, 31, 'In progress');
Share.Add(3, 17, 'Closed');
end;
Related
TAtlasChartSeries · TAtlasChartKind · TAtlasChartPoint · EAtlasChartError · TAtlasGauge · TAtlasThemer
Also in Atlas VCL
TAtlasTable · TAtlasGrid · TAtlasValueEdit · TAtlasTree · TAtlasWorkbook · TAtlasSheetGrid · TAtlasPivot · TAtlasInspector · TAtlasRichEdit · TAtlasSpellChecker · TAtlasGauge · TAtlasThemer · TAtlasWizard · TAtlasWorkspace · TAtlasCommandBar · TAtlasActionTile · TAtlasTileBoard · TAtlasNavigation · TAtlasMap · TAtlasGraph · TAtlasDiagram · TAtlasPrintDocument · TAtlasSchedule · TAtlasScheduler · TAtlasTaskPlan · TAtlasGantt · TAtlasPdfDocument · TAtlasPdfViewer