1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
{
Basic code for function series of TAChart.
*****************************************************************************
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
Authors: Alexander Klenin
}
unit TACustomFuncSeries;
{$H+}
interface
uses
Classes,
TAChartUtils, TACustomSeries, TADrawUtils, TAGraph, TATypes;
type
{ TBasicFuncSeries }
TBasicFuncSeries = class(TCustomChartSeries)
strict private
FExtent: TChartExtent;
procedure SetExtent(AValue: TChartExtent);
protected
procedure AfterAdd; override;
procedure GetBounds(var ABounds: TDoubleRect); override;
public
procedure Assign(ASource: TPersistent); override;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Active default true;
property Extent: TChartExtent read FExtent write SetExtent;
property ShowInLegend;
property Title;
property ZPosition;
end;
TMakeDoublePoint = function (AX, AY: Double): TDoublePoint;
TDrawFuncHelper = class
strict private
type
TOnPoint = procedure (AXg, AXa: Double) of object;
var
FAxisToGraphXr, FAxisToGraphYr, FGraphToAxisXr: TTransformFunc;
FCalc: TTransformFunc;
FChart: TChart;
FDomainExclusions: TIntervalList;
FDrawer: IChartDrawer;
FExtent: TDoubleRect;
FExtentYMax: PDouble;
FExtentYMin: PDouble;
FGraphStep: Double;
FImageToGraph: TImageToGraphFunc;
FNearestPointParams: ^TNearestPointParams;
FNearestPointResults: ^TNearestPointResults;
FMakeDP: TMakeDoublePoint;
FPrev: TDoublePoint;
FPrevInExtent: Boolean;
FSeries: TCustomChartSeries;
procedure CalcAt(AXg, AXa: Double; out APt: TDoublePoint; out AIn: Boolean);
procedure CheckForNearestPoint(AXg, AXa: Double);
procedure ForEachPoint(AXg, AXMax: Double; AOnMoveTo, AOnLineTo: TOnPoint);
procedure LineTo(AXg, AXa: Double);
procedure MoveTo(AXg, AXa: Double);
procedure UpdateExtent(AXg, AXa: Double);
function XRange: TDoubleInterval;
public
constructor Create(
ASeries: TCustomChartSeries; ADomainExclusions:
TIntervalList; ACalc: TTransformFunc; AStep: Integer);
procedure CalcAxisExtentY(AMinX, AMaxX: Double; var AMinY, AMaxY: Double);
procedure DrawFunction(ADrawer: IChartDrawer);
function GetNearestPoint(
const AParams: TNearestPointParams;
out AResults: TNearestPointResults): Boolean;
end;
implementation
uses
Math, SysUtils,
TAGeometry, TAMath;
function DoublePointRotated(AX, AY: Double): TDoublePoint;
begin
Result.X := AY;
Result.Y := AX;
end;
{ TDrawFuncHelper }
procedure TDrawFuncHelper.CalcAt(
AXg, AXa: Double; out APt: TDoublePoint; out AIn: Boolean);
begin
APt := FMakeDP(AXg, FAxisToGraphYr(FCalc(AXa)));
AIn := (FExtent.a <= APt) and (APt <= FExtent.b);
end;
procedure TDrawFuncHelper.CalcAxisExtentY(
AMinX, AMaxX: Double; var AMinY, AMaxY: Double);
begin
FExtentYMin := @AMinY;
FExtentYMax := @AMaxY;
with XRange do
ForEachPoint(AMinX, AMaxX, @UpdateExtent, @UpdateExtent);
end;
procedure TDrawFuncHelper.CheckForNearestPoint(AXg, AXa: Double);
var
inExtent: Boolean;
gp: TDoublePoint;
ip: TPoint;
d: Integer;
begin
CalcAt(AXg, AXa, gp, inExtent);
if not inExtent then exit;
ip := FChart.GraphToImage(gp);
d := FNearestPointParams^.FDistFunc(FNearestPointParams^.FPoint, ip);
if d >= FNearestPointResults^.FDist then exit;
FNearestPointResults^.FDist := d;
FNearestPointResults^.FImg := ip;
FNearestPointResults^.FValue.X := AXa;
end;
constructor TDrawFuncHelper.Create(
ASeries: TCustomChartSeries; ADomainExclusions: TIntervalList;
ACalc: TTransformFunc; AStep: Integer);
begin
FChart := ASeries.ParentChart;
FExtent := FChart.CurrentExtent;
FSeries := ASeries;
FDomainExclusions := ADomainExclusions;
FCalc := ACalc;
with FSeries do
if IsRotated then begin
FAxisToGraphXr := @AxisToGraphY;
FAxisToGraphYr := @AxisToGraphX;
FGraphToAxisXr := @GraphToAxisY;
FMakeDP := @DoublePointRotated;
FImageToGraph := @FChart.YImageToGraph;
AStep := -AStep;
end
else begin
FAxisToGraphXr := @AxisToGraphX;
FAxisToGraphYr := @AxisToGraphY;
FGraphToAxisXr := @GraphToAxisX;
FMakeDP := @DoublePoint;
FImageToGraph := @FChart.XImageToGraph;
end;
FGraphStep := FImageToGraph(AStep) - FImageToGraph(0);
end;
procedure TDrawFuncHelper.DrawFunction(ADrawer: IChartDrawer);
begin
FDrawer := ADrawer;
with XRange do
ForEachPoint(FStart, FEnd, @MoveTo, @LineTo);
end;
procedure TDrawFuncHelper.ForEachPoint(
AXg, AXMax: Double; AOnMoveTo, AOnLineTo: TOnPoint);
var
hint: Integer;
xa, xg1, xa1, dx: Double;
begin
if FGraphStep = 0 then exit;
hint := 0;
xa := FGraphToAxisXr(AXg);
if FDomainExclusions.Intersect(xa, xa, hint) then
AXg := FAxisToGraphXr(xa);
if AXg < AXMax then
AOnMoveTo(AXg, xa);
dx := abs(FGraphStep);
while AXg < AXMax do begin
xg1 := AXg + dx;
xa1 := FGraphToAxisXr(xg1);
if FDomainExclusions.Intersect(xa, xa1, hint) then begin
AOnLineTo(FAxisToGraphXr(xa), xa);
xg1 := FAxisToGraphXr(xa1);
if xg1 < AXMax then
AOnMoveTo(xg1, xa1);
end
else
AOnLineTo(xg1, xa1);
AXg := xg1;
xa := xa1;
end;
end;
function TDrawFuncHelper.GetNearestPoint(
const AParams: TNearestPointParams;
out AResults: TNearestPointResults): Boolean;
var
x: Integer;
r: TDoubleInterval;
begin
AResults.FIndex := -1;
AResults.FDist := Sqr(AParams.FRadius) + 1;
FNearestPointParams := @AParams;
FNearestPointResults := @AResults;
with AParams do
if FOptimizeX then begin
x := TPointBoolArr(FPoint)[FSeries.IsRotated];
r := DoubleInterval(FImageToGraph(x - FRadius), FImageToGraph(x + FRadius));
EnsureOrder(r.FStart, r.FEnd);
end
else
r := DoubleInterval(NegInfinity, SafeInfinity);
with XRange do
ForEachPoint(
Max(r.FStart, FStart), Min(r.FEnd, FEnd),
@CheckForNearestPoint, @CheckForNearestPoint);
Result := AResults.FDist < Sqr(AParams.FRadius) + 1;
end;
procedure TDrawFuncHelper.LineTo(AXg, AXa: Double);
var
p, t: TDoublePoint;
inExtent: Boolean;
begin
CalcAt(AXg, AXa, p, inExtent);
t := p;
if inExtent and FPrevInExtent then
FDrawer.LineTo(FChart.GraphToImage(p))
else if LineIntersectsRect(FPrev, t, FExtent) then begin
FDrawer.MoveTo(FChart.GraphToImage(FPrev));
FDrawer.LineTo(FChart.GraphToImage(t));
end;
FPrevInExtent := inExtent;
FPrev := p;
end;
procedure TDrawFuncHelper.MoveTo(AXg, AXa: Double);
begin
CalcAt(AXg, AXa, FPrev, FPrevInExtent);
if FPrevInExtent then
FDrawer.MoveTo(FChart.GraphToImage(FPrev));
end;
procedure TDrawFuncHelper.UpdateExtent(AXg, AXa: Double);
begin
Unused(AXg);
UpdateMinMax(FCalc(AXa), FExtentYMin^, FExtentYMax^);
end;
function TDrawFuncHelper.XRange: TDoubleInterval;
begin
if FSeries.IsRotated then
Result := DoubleInterval(FExtent.a.Y, FExtent.b.Y)
else
Result := DoubleInterval(FExtent.a.X, FExtent.b.X);
end;
{ TBasicFuncSeries }
procedure TBasicFuncSeries.AfterAdd;
begin
inherited AfterAdd;
FExtent.SetOwner(FChart);
end;
procedure TBasicFuncSeries.Assign(ASource: TPersistent);
begin
if ASource is TBasicFuncSeries then
with TBasicFuncSeries(ASource) do
Self.Extent := FExtent;
inherited Assign(ASource);
end;
constructor TBasicFuncSeries.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FExtent := TChartExtent.Create(FChart);
end;
destructor TBasicFuncSeries.Destroy;
begin
FreeAndNil(FExtent);
inherited Destroy;
end;
procedure TBasicFuncSeries.GetBounds(var ABounds: TDoubleRect);
begin
with Extent do begin
if UseXMin then ABounds.a.X := XMin;
if UseYMin then ABounds.a.Y := YMin;
if UseXMax then ABounds.b.X := XMax;
if UseYMax then ABounds.b.Y := YMax;
end;
end;
procedure TBasicFuncSeries.SetExtent(AValue: TChartExtent);
begin
if FExtent = AValue then exit;
FExtent.Assign(AValue);
UpdateParentChart;
end;
end.
|