From 5207c624ac60ecee0de27587a05ade6b5eb0a897 Mon Sep 17 00:00:00 2001 From: wp Date: Sun, 24 Feb 2019 22:11:44 +0000 Subject: [PATCH] TAChart: Some optimizations from patches by Marcin Wiazowski. Decimal separator test for TListChartSource.DataPoints. git-svn-id: trunk@60491 - --- components/tachart/tachartutils.pas | 6 +++--- components/tachart/tacustomseries.pas | 9 ++++++--- components/tachart/tadrawersvg.pas | 14 ++++---------- components/tachart/tasources.pas | 23 +++++++++-------------- components/tachart/test/SourcesTest.pas | 9 +++++++++ 5 files changed, 31 insertions(+), 30 deletions(-) diff --git a/components/tachart/tachartutils.pas b/components/tachart/tachartutils.pas index 5dabdb5747..22edcfe6a5 100644 --- a/components/tachart/tachartutils.pas +++ b/components/tachart/tachartutils.pas @@ -323,6 +323,9 @@ const ((cotNone, cotSecond), (cotFirst, cotBoth)); ORIENTATION_UNITS_PER_DEG = 10; +var + DefSeparatorSettings: TFormatSettings; + function BoundsSize(ALeft, ATop: Integer; ASize: TSize): TRect; inline; function Deg16ToRad(ADeg16: Integer): Double; inline; @@ -523,9 +526,6 @@ begin end; end; -var - DefSeparatorSettings: TFormatSettings; - function Split(AString: String; ADest: TStrings; ADelimiter: Char): TStrings; begin Result := ADest; diff --git a/components/tachart/tacustomseries.pas b/components/tachart/tacustomseries.pas index 5fdb1d6708..6db28a2651 100644 --- a/components/tachart/tacustomseries.pas +++ b/components/tachart/tacustomseries.pas @@ -1069,10 +1069,13 @@ end; procedure TChartSeries.SetSource(AValue: TCustomChartSource); begin - if FSource = AValue then exit; + if AValue = FBuiltinSource then + AValue := nil; + if FSource = AValue then + exit; + CheckSource(AValue); if FListener.IsListening then Source.Broadcaster.Unsubscribe(FListener); - CheckSource(AValue); FSource := AValue; Source.Broadcaster.Subscribe(FListener); SourceChanged(Self); @@ -1122,7 +1125,7 @@ end; procedure TChartSeries.SourceChanged(ASender: TObject); begin - if ASender is TCustomChartSource then + if (ASender <> FBuiltinSource) and (ASender is TCustomChartSource) then try CheckSource(TCustomChartSource(ASender)); except diff --git a/components/tachart/tadrawersvg.pas b/components/tachart/tadrawersvg.pas index c59dc49736..77cc980cd2 100644 --- a/components/tachart/tadrawersvg.pas +++ b/components/tachart/tadrawersvg.pas @@ -107,8 +107,6 @@ uses const RECT_FMT = ''; -var - fmtSettings: TFormatSettings; function EscapeXML(const AText: String): String; var @@ -139,12 +137,12 @@ end; function DP2S(AValue: TDoublePoint): String; begin - Result := Format('%g,%g', [AValue.X, AValue.Y], fmtSettings); + Result := Format('%g,%g', [AValue.X, AValue.Y], DefSeparatorSettings); end; function F2S(AValue: Double): String; begin - Result := FloatToStr(AValue, fmtSettings); + Result := FloatToStr(AValue, DefSeparatorSettings); end; function SVGGetFontOrientationFunc(AFont: TFPCustomFont): Integer; @@ -534,7 +532,7 @@ begin stext := Format('x="%d" y="%d"', [p.X, p.Y]); if FFontOrientation <> 0 then stext := stext + Format(' transform="rotate(%g,%d,%d)"', - [-FFontOrientation*0.1, p.X, p.Y], fmtSettings); + [-FFontOrientation*0.1, p.X, p.Y], DefSeparatorSettings); sstyle := Format('fill:%s; font-family:''%s''; font-size:%dpt;', [ColorToHex(GetFontColor), GetFontName, round(FFont.SizeInPoints)]); @@ -609,7 +607,7 @@ end; procedure TSVGDrawer.WriteFmt(const AFormat: String; AParams: array of const); begin - WriteStr(Format(AFormat, AParams, fmtSettings)); + WriteStr(Format(AFormat, AParams, DefSeparatorSettings)); end; procedure TSVGDrawer.WriteStr(const AString: String); @@ -636,9 +634,5 @@ begin end; -initialization - fmtSettings := DefaultFormatSettings; - fmtSettings.DecimalSeparator := '.'; - end. diff --git a/components/tachart/tasources.pas b/components/tachart/tasources.pas index b41fb8c2db..15017ca8c3 100644 --- a/components/tachart/tasources.pas +++ b/components/tachart/tasources.pas @@ -315,20 +315,17 @@ end; function TListChartSourceStrings.Get(Index: Integer): String; var i: Integer; - fs: TFormatSettings; begin - fs := DefaultFormatSettings; - fs.DecimalSeparator := '.'; with FSource[Index]^ do begin Result := ''; if FSource.XCount > 0 then - Result += Format('%g|', [X], fs); + Result += Format('%g|', [X], DefSeparatorSettings); for i := 0 to High(XList) do - Result += Format('%g|', [XList[i]], fs); + Result += Format('%g|', [XList[i]], DefSeparatorSettings); if FSource.YCount > 0 then - Result += Format('%g|', [Y], fs); + Result += Format('%g|', [Y], DefSeparatorSettings); for i := 0 to High(YList) do - Result += Format('%g|', [YList[i]], fs); + Result += Format('%g|', [YList[i]], DefSeparatorSettings); Result += Format('%s|%s', [IntToColorHex(Color), Text]); end; end; @@ -609,8 +606,8 @@ end; function TListChartSource.NewItem: PChartDataItem; begin New(Result); - SetLength(Result^.XList, Max(XCount - 1, 0)); - SetLength(Result^.YList, Max(YCount - 1, 0)); + if XCount > 1 then SetLength(Result^.XList, XCount - 1); + if YCount > 1 then SetLength(Result^.YList, YCount - 1); end; procedure TListChartSource.SetColor(AIndex: Integer; AColor: TChartColor); @@ -810,11 +807,9 @@ end; function CompareDataItemX(AItem1, AItem2: Pointer): Integer; var i: Integer; - item1, item2: PChartDataItem; + item1: PChartDataItem absolute AItem1; + item2: PChartDataItem absolute AItem2; begin - item1 := PChartDataItem(AItem1); - item2 := PChartDataItem(AItem2); - Result := CompareFloat(item1^.X, item2^.X); if Result = 0 then for i := 0 to Min(High(item1^.XList), High(item2^.XList)) do begin @@ -1489,7 +1484,7 @@ begin FOriginYCount := FOrigin.YCount; if ReorderYList = '' then begin - SetLength(FYOrder, FOriginYCount); + SetLength(FYOrder, FOriginYCount); for i := 0 to High(FYOrder) do FYOrder[i] := i; end diff --git a/components/tachart/test/SourcesTest.pas b/components/tachart/test/SourcesTest.pas index fdab6a903e..a0210ba8e0 100644 --- a/components/tachart/test/SourcesTest.pas +++ b/components/tachart/test/SourcesTest.pas @@ -507,6 +507,15 @@ begin on E: Exception do AssertTrue('Empty YList', E is TListChartSource.EYListEmptyError); end; + AssertEquals(2, FSource.Count); + + FSource.DataPoints.Add('1.23|2.34|3|?|t'); + AssertEquals(1.23, FSource[2]^.X); + AssertEquals(2.34, FSource[2]^.Y); + + FSource.DataPoints.Add('1,23|2,34|3|?|t'); + AssertEquals(1.23, FSource[3]^.X); + AssertEquals(2.34, FSource[3]^.Y); FSource.Clear; FSource.XCount := 2;