From a8b64bb621a8428e42dad06e9c080a8a5324ba88 Mon Sep 17 00:00:00 2001 From: ask Date: Sat, 4 Sep 2010 10:11:15 +0000 Subject: [PATCH] TAChart: Add multi-valued db-aware source. Update demo. git-svn-id: trunk@27263 - --- components/tachart/demo/db/dbdemo.lpi | 5 ++--- components/tachart/demo/db/main.lfm | 12 +++--------- components/tachart/tadbsource.pas | 28 +++++++++++++++++++++++---- components/tachart/tasources.pas | 8 ++++++-- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/components/tachart/demo/db/dbdemo.lpi b/components/tachart/demo/db/dbdemo.lpi index 27b96c6a52..d4d4c02c7e 100644 --- a/components/tachart/demo/db/dbdemo.lpi +++ b/components/tachart/demo/db/dbdemo.lpi @@ -1,8 +1,8 @@ + - @@ -10,12 +10,11 @@ - <ResourceType Value="res"/> </General> <VersionInfo> - <StringTable Comments="" CompanyName="" FileDescription="" FileVersion="0.0.0.0" InternalName="" LegalCopyright="" LegalTrademarks="" OriginalFilename="" ProductName="" ProductVersion=""/> + <StringTable ProductVersion=""/> </VersionInfo> <PublishOptions> <Version Value="2"/> diff --git a/components/tachart/demo/db/main.lfm b/components/tachart/demo/db/main.lfm index 49c2078a31..c4c4824023 100644 --- a/components/tachart/demo/db/main.lfm +++ b/components/tachart/demo/db/main.lfm @@ -15,15 +15,10 @@ object Form1: TForm1 Width = 347 AxisList = < item - Alignment = calLeft - Title.Font.Orientation = 900 - Transformation.Offset = 0 - Transformation.Scale = 1 + Title.LabelFont.Orientation = 900 end item Alignment = calBottom - Transformation.Offset = 0 - Transformation.Scale = 1 end> Foot.Brush.Color = clBtnFace Foot.Font.Color = clBlue @@ -42,12 +37,10 @@ object Form1: TForm1 ZPosition = 1 LinePen.Color = clTeal LinePen.Width = 3 - SeriesColor = clTeal Source = DbChartSource1 end object Chart1BarSeries1: TBarSeries BarBrush.Color = clRed - SeriesColor = clRed end end object DBGrid1: TDBGrid @@ -56,6 +49,7 @@ object Form1: TForm1 Top = 26 Width = 200 Align = alRight + Columns = <> DataSource = Datasource1 Scrollbars = ssAutoBoth TabOrder = 1 @@ -101,7 +95,7 @@ object Form1: TForm1 object DbChartSource1: TDbChartSource DataSource = Datasource1 FieldX = 'X' - FieldY = 'Y' + FieldY = 'Y,Y' left = 271 top = 156 end diff --git a/components/tachart/tadbsource.pas b/components/tachart/tadbsource.pas index a818ecdc50..54b410f6ce 100644 --- a/components/tachart/tadbsource.pas +++ b/components/tachart/tadbsource.pas @@ -16,7 +16,7 @@ } unit TADbSource; -{$mode objfpc}{$H+} +{$H+} interface @@ -36,6 +36,7 @@ type FFieldText: String; FFieldX: String; FFieldY: String; + FFieldYList: TStringList; function GetDataSource: TDataSource; procedure SetDataSource(AValue: TDataSource); @@ -46,6 +47,7 @@ type protected function GetCount: Integer; override; function GetItem(AIndex: Integer): PChartDataItem; override; + procedure SetYCount(AValue: Cardinal); override; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; @@ -66,7 +68,7 @@ procedure Register; implementation uses - SysUtils; + Math, SysUtils; type @@ -141,11 +143,13 @@ constructor TDbChartSource.Create(AOwner: TComponent); begin inherited Create(AOwner); FDataLink := TDbChartSourceDataLink.Create(Self); + FFieldYList := TStringList.Create; end; destructor TDbChartSource.Destroy; begin FreeAndNil(FDataLink); + FreeAndNil(FFieldYList); inherited; end; @@ -165,6 +169,7 @@ end; function TDbChartSource.GetItem(AIndex: Integer): PChartDataItem; var ds: TDataSet; + i: Integer; begin Result := @FCurItem; SetDataItemDefaults(FCurItem); @@ -188,8 +193,11 @@ begin if ds.RecNo <> AIndex then exit; if FieldX <> '' then FCurItem.X := ds.FieldByName(FieldX).AsFloat; - if FieldY <> '' then - FCurItem.Y := ds.FieldByName(FieldY).AsFloat; + if FYCount > 0 then begin + FCurItem.Y := ds.FieldByName(FFieldYList[0]).AsFloat; + for i := 0 to High(FCurItem.YList) do + FCurItem.YList[i] := ds.FieldByName(FFieldYList[i + 1]).AsFloat; + end; if FieldColor <> '' then FCurItem.Color := ds.FieldByName(FieldColor).AsInteger; if FieldText <> '' then @@ -233,8 +241,20 @@ procedure TDbChartSource.SetFieldY(const AValue: String); begin if FFieldY = AValue then exit; FFieldY := AValue; + if FFieldY = '' then + FFieldYList.Clear + else + FFieldYList.CommaText := FFieldY; + FYCount := FFieldYList.Count; + SetLength(FCurItem.YList, Max(FYCount - 1, 0)); Reset; end; +procedure TDbChartSource.SetYCount(AValue: Cardinal); +begin + Unused(AValue); + raise EYCountError.Create('Set FieldY instead'); +end; + end. diff --git a/components/tachart/tasources.pas b/components/tachart/tasources.pas index a90e5b762b..6635cfb06f 100644 --- a/components/tachart/tasources.pas +++ b/components/tachart/tasources.pas @@ -45,12 +45,13 @@ type private FBroadcaster: TBroadcaster; FUpdateCount: Integer; - FYCount: Cardinal; protected FExtent: TDoubleRect; FExtentIsValid: Boolean; FValuesTotal: Double; FValuesTotalIsValid: Boolean; + FYCount: Cardinal; + function GetCount: Integer; virtual; abstract; function GetItem(AIndex: Integer): PChartDataItem; virtual; abstract; procedure InvalidateCaches; @@ -718,9 +719,12 @@ begin BeginUpdate; try Clear; + YCount := ASource.YCount; for i := 0 to ASource.Count - 1 do - with ASource[i]^ do + with ASource[i]^ do begin AddAt(FData.Count, X, Y, Text, Color); + SetYList(FData.Count - 1, YList); + end; if Sorted and not ASource.IsSorted then Sort; finally EndUpdate;