TAChart: Add multi-valued db-aware source. Update demo.

git-svn-id: trunk@27263 -
This commit is contained in:
ask 2010-09-04 10:11:15 +00:00
parent fd82ff4f5f
commit a8b64bb621
4 changed files with 35 additions and 18 deletions

View File

@ -1,8 +1,8 @@
<?xml version="1.0"?>
<CONFIG>
<ProjectOptions>
<Version Value="9"/>
<PathDelim Value="\"/>
<Version Value="8"/>
<General>
<Flags>
<SaveClosedFiles Value="False"/>
@ -10,12 +10,11 @@
</Flags>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<TargetFileExt Value=".exe"/>
<Title Value="Chart db-aware demo"/>
<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"/>

View File

@ -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

View File

@ -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.

View File

@ -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;