YOUSEFUL / DELPHI / COMPONENTS
TAtlasPlotAnalysis
Adds a histogram, a polynomial fit curve or a smoothed copy as a new series of a TAtlasPlot.
Description
Helpers that add computed series to a TAtlasPlot, the general chart of Atlas Plot. They are class function ... static; call them on the class. Each one computes everything first with TAtlasStats and touches the chart only when it has a complete result, adding the series inside one BeginUpdate/EndUpdate, so a rejected request raises EAtlasPlotError and leaves the chart as it was. The result is the new TAtlasPlotSeries; style it like any other series.
Results are snapshots: they do not follow later edits of the source series. Null points (gaps) of a source series are skipped, and a source must belong to the same chart.
Members
| Member | Kind | What it does |
|---|---|---|
AddHistogram | method | Adds a bar series (pkBar) with one bar per bin of TAtlasStats.Histogram(Values, Bins): X is the bin centre, Y the count. |
AddPolynomialFit | method | Fits a least-squares polynomial of Degree (1 to 6) through the non-null points of Source and adds it as a line series titled <Source title> fit(<Degree>), with Samples (2 to 10000) evenly spaced points from the smallest to the largest X. |
AddExponentialSmoothing | method | Adds a line series titled <Source title> smooth(<Alpha>) with the exponentially smoothed Y values of the non-null points of Source, in point order, at their original X. |
Methods
AddHistogram method
class function AddHistogram(Plot: TAtlasPlot; const Values: TArray<Double>; Bins: Integer = 0; const Title: string = 'Frequency'): TAtlasPlotSeries; static;Adds a bar series (pkBar) with one bar per bin of TAtlasStats.Histogram(Values, Bins): X is the bin centre, Y the count. Bins 0 uses Sturges' rule. Raises when Plot is nil or the histogram is rejected.
Added := TAtlasPlotAnalysis.AddHistogram(Plot, [1, 2, 2, 3, 3, 3, 4, 4, 5], 4, 'Counts');AddPolynomialFit method
class function AddPolynomialFit(Plot: TAtlasPlot; Source: TAtlasPlotSeries; Degree: Integer; out RSquared: Double; Samples: Integer = 64): TAtlasPlotSeries; static;Fits a least-squares polynomial of Degree (1 to 6) through the non-null points of Source and adds it as a line series titled <Source title> fit(<Degree>), with Samples (2 to 10000) evenly spaced points from the smallest to the largest X. RSquared receives the fit's R squared (0 when the call fails). Raises when Source is nil or belongs to another chart, when Samples is out of range, or when the fit is rejected.
Added := TAtlasPlotAnalysis.AddPolynomialFit(Plot, Measured, 2, RSquared);
Added.LineWidth := 3;AddExponentialSmoothing method
class function AddExponentialSmoothing(Plot: TAtlasPlot; Source: TAtlasPlotSeries; const Alpha: Double): TAtlasPlotSeries; static;Adds a line series titled <Source title> smooth(<Alpha>) with the exponentially smoothed Y values of the non-null points of Source, in point order, at their original X. Alpha must be above 0 and at most 1.
Added := TAtlasPlotAnalysis.AddExponentialSmoothing(Plot, Measured, 0.3);Examples
Add a fit and a smoothed line to measured points
The R squared of the fit goes into the chart title.
uses
System.SysUtils, System.Math, Vcl.Controls, Vcl.Forms, Atlas.PlotSeries, Atlas.Plot,
Atlas.PlotAnalysis;
function AddAnalysedPlot(Form: TForm): TAtlasPlot;
var Measured: TAtlasPlotSeries; I: Integer; RSquared: Double;
begin
Result := TAtlasPlot.Create(Form);
Result.Parent := Form;
Result.Align := alClient;
Measured := Result.AddSeries('Measured', pkPoint);
for I := 0 to 40 do
Measured.Add(I / 4, 3 + 0.9 * (I / 4) - 0.35 * Sqr(I / 4 - 5) + 1.5 * Sin(I * 2.3));
TAtlasPlotAnalysis.AddPolynomialFit(Result, Measured, 2, RSquared).LineWidth := 3;
TAtlasPlotAnalysis.AddExponentialSmoothing(Result, Measured, 0.3);
Result.Title := Format('Quadratic fit, R squared %.3f', [RSquared]);
end;Show the distribution of a sample
Replaces whatever the chart showed with a histogram.
uses
System.SysUtils, Atlas.Plot, Atlas.PlotAnalysis;
procedure ShowDistribution(Plot: TAtlasPlot; const Values: TArray<Double>);
begin
Plot.BeginUpdate;
try
Plot.Clear;
TAtlasPlotAnalysis.AddHistogram(Plot, Values, 0, 'Requests'); // Sturges' rule
Plot.Title := Format('%d values', [Length(Values)]);
finally
Plot.EndUpdate;
end;
end;Related
TAtlasPlot · TAtlasPlotSeries · TAtlasStats · TAtlasHistogram · TAtlasPolynomial