1
{ $Id: barchart.pp 30388 2011-04-19 18:34:50Z sekelsenmat $ }
3
/***************************************************************************
6
Component Library Extended Controls
8
***************************************************************************/
10
*****************************************************************************
12
* This file is part of the Lazarus Component Library (LCL) *
14
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
15
* for details about the copyright. *
17
* This program is distributed in the hope that it will be useful, *
18
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
19
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
21
*****************************************************************************
23
This unit is deprecated, because there is something better:
24
package TAChartLazarusPkg.
26
Author: Michael Van Canneyt
35
Types, SysUtils, Classes, LCLProc, LCLIntf, LCLType, Controls, ExtCtrls, Graphics,
40
TPosLabel=(plLeft, plCenter, plRight);
41
TCustomBarChart = class;
44
TBar = class(TCollectionItem)
49
procedure SetColor(const AValue: TColor);
50
procedure SetSName(const AValue: String);
51
procedure SetValue(const AValue: integer);
52
procedure UpdateBarChart;
54
function GetDisplayName: string; override;
56
property SName: String read FSName write SetSName;
57
property Value: integer read FValue write SetValue;
58
property Color: TColor read FColor write SetColor;
63
TBarChartItems = class(TCollection)
65
FBarChart: TCustomBarChart;
67
function GetOwner: TPersistent; override;
69
constructor Create(BarChart: TCustomBarChart);
74
TCustomBarChart = class(TPanel)
76
FUpdateCount: Integer;
79
FLabelPosition:TPosLabel;
81
function GetBars: TCollection;
82
function NormalizeScaleUnits(OldScale: Integer): Integer;
83
procedure SetBars(const AValue: TCollection);
84
procedure SetDepth(const AValue: byte);
85
procedure SetLabelPosition(const AValue: TPosLabel);
87
procedure Paint; override;
88
class function GetControlClassDefaultSize: TSize; override;
89
function RealGetText: TCaption; override;
91
constructor Create(AOwner: TComponent); override;
92
destructor Destroy; override;
94
function AddBar(const SName: string; Value: integer; AColor: TColor): TBar;
95
function GetBar(SId: integer): TBar;
96
function BarCount: Integer;
97
procedure BeginUpdate;
99
procedure UpdateBarChart;
101
property Bars: TCollection read GetBars write SetBars;
102
property Depth: byte read FDepth write SetDepth;
103
property LabelPosition: TPosLabel read FLabelPosition write SetLabelPosition;
109
Use package TAChartLazarusPkg instead. It has a compatible and better component.}
111
TBarChart = class(TCustomBarChart)
117
property BorderSpacing;
121
property BorderWidth;
122
property BorderStyle;
124
property ClientHeight;
125
property ClientWidth;
127
property Constraints;
131
property FullRepaint;
132
property ParentColor;
134
property ParentShowHint;
147
property OnMouseDown;
148
property OnMouseMove;
151
property OnStartDrag;
152
end deprecated; // use package TAChartLazarusPkg instead. It has a compatible and better component.
157
procedure FillBarChart(BC: TBarChart; DS: TDataset;
158
const LabelField, ValueField: String; AColor: TColor);
165
RegisterComponents('Misc',[TBarChart]);
169
procedure FillBarChart(BC: TBarChart; DS: TDataset;
170
const LabelField, ValueField: String; AColor: TColor);
177
VF:=DS.FieldByName(ValueField);
180
DS.GetFieldList(LF,LabelField);
186
For I:=0 to LF.Count-1 do
190
L:=L+TField(LF[i]).AsString;
192
BC.AddBar(L, VF.AsInteger, AColor);
201
constructor TCustomBarChart.Create(AOwner: TComponent);
203
inherited Create(AOwner);
204
FBars:=TBarChartItems.Create(Self);
206
FLabelPosition:=plLeft;
207
with GetControlClassDefaultSize do
208
SetInitialBounds(0, 0, CX, CY);
211
destructor TCustomBarChart.Destroy;
217
function TCustomBarChart.AddBar(const SName: string; Value: Integer;
218
AColor: TColor): TBar;
222
result:=TBar(FBars.Add);
223
result.FsName:=SName;
224
result.FValue:=Value;
225
result.FColor:=AColor;
231
function TCustomBarChart.GetBar(SId: integer): TBar;
233
result:=TBar(FBars.FindItemID(SId));
236
function TCustomBarChart.NormalizeScaleUnits(OldScale: Integer): Integer;
245
else if Result<=5 then
247
else if Result<=10 then
251
T:=StrToInt(IntToStr(Result)[1])+1;
253
Result:=Result div 10;
260
function TCustomBarChart.GetBars: TCollection;
265
procedure TCustomBarChart.SetBars(const AValue: TCollection);
267
FBars.Assign(AValue);
270
procedure TCustomBarChart.SetDepth(const AValue: byte);
272
if FDepth=AValue then exit;
277
procedure TCustomBarChart.SetLabelPosition(const AValue: TPosLabel);
279
if FLabelPosition=AValue then exit;
280
FLabelPosition:=AValue;
284
procedure TCustomBarChart.Paint;
287
i,k,j,h,w,h1,HMax,VMax: integer;
289
NScaleLines : Integer;
290
ScaleUnits : Integer;
291
PixelPerUnit: Double;
300
procedure ScaleLine(dk: integer; const s: string);
303
Canvas.MoveTo(hmax+dk+FDepth,h1);
304
Canvas.LineTo(hmax+dk+FDepth,h1+h);
305
Canvas.LineTo(hmax+dk,h1+FDepth+h);
306
Canvas.LineTo(hmax+dk,h1+FDepth+h+2);
307
Canvas.TextOut(HMax+dk-j,m,s);
315
FIsPainting := false;
317
bx:=GetSystemMetrics(SM_CXEDGE);
318
by:=GetSystemMetrics(SM_CYEDGE);
321
for i:=0 to FBars.Count-1 do
323
ts:=TBar(FBars.Items[i]);
324
k:=Canvas.TextWidth(ts.FsName);
327
if ts.FValue>vmax then
331
h1:=RoundToInt(1.5*Canvas.TextHeight('W'));
332
h:=Height-2*h1-Fdepth;
333
w:=Width-hmax-2*FDepth;
334
Canvas.Pen.Color:=clBlack;
336
Canvas.Pen.Style:=psSolid;
337
Canvas.Brush.Color:=clYellow;
338
Canvas.Brush.Style:=bsSolid;
339
Canvas.Polygon([Point(HMax,h1+FDepth),Point(HMax,h1+FDepth+h),Point(HMax+FDepth,h1+h),Point(HMax+FDepth,h1)]);
340
Canvas.Brush.Color:=clWhite;
341
Canvas.Polygon([Point(HMax,h1+FDepth+h),Point(HMax+w,h1+FDepth+h),Point(HMax+w+FDepth,h1+h),Point(HMax+FDepth,h1+h)]);
342
Canvas.Brush.Color:=Color;
343
Canvas.Rectangle(hmax+Fdepth,h1,hmax+w+FDepth,h1+h+1);
345
Canvas.MoveTo(hmax,h1+FDepth);
346
Canvas.LineTo(hmax,h1+FDepth+h);
347
Canvas.LineTo(hmax+w,h1+FDepth+h);
348
Canvas.TextOut(bx,by,Caption);
349
j:=Canvas.TextWidth(IntTostr(VMax));
357
PixelPerUnit:=double(w-j-6) / VMax;
358
NScaleLines:=(w-j-6) div (2*j);
360
if NScaleLines=0 then
363
ScaleUnits:=(Vmax div NScaleLines) +1;
364
ScaleUnits:=NormalizeScaleUnits(ScaleUnits);
368
NScaleLines:=VMax div ScaleUnits;
369
Canvas.Pen.Color:=clGray;
370
Canvas.Pen.Style:=psDot;
380
Canvas.TextOut(HMax-j,m,'0');
381
for k:=1 to NScaleLines do
382
ScaleLine(RoundToInt(ScaleUnits*PixelPerUnit*k),IntToStr(k*ScaleUnits));
383
If FBars.Count=0 then
386
BC:=double(h) / (2*(FBars.Count+1));
389
Canvas.Pen.Style:=psSolid;
390
for i:=0 to FBars.Count-1 do
392
ts:=TBar(FBars.Items[i]);
393
z:=h1+FDepth+h-Round(2*(I+1)*BC);
394
Canvas.Brush.Color:=ts.FColor;
396
BL:=RoundToInt(m*PixelPerUnit);
397
Canvas.Rectangle(hmax+1,z-1,hmax+BL+1,z+RBC-1);
398
Canvas.Polygon([Point(hmax,z),Point(hmax+BL,z),Point(hmax+BL+FDepth,z-FDepth),Point(hmax+FDepth,z-FDepth)]);
399
Canvas.Polygon([Point(hmax+BL,z),Point(hmax+BL,z+RBC-1),Point(hmax+BL+FDepth,z+RBC-1-FDepth),Point(hmax+BL+FDepth,z-FDepth)]);
401
w:=z+(RBC-FDepth) div 2;
402
Canvas.MoveTo(Hmax+BL+Fdepth div 2,w);
403
Canvas.LineTo(Hmax+BL+Fdepth+5-bx,w);
404
Canvas.Brush.Color:=clYellow;
407
left:=hmax+BL+FDepth+5-bx;
408
right:=left+Canvas.TextWidth(s)+2*bx;
409
top:=w-Canvas.TextHeight(s) div 2-by;
410
bottom:=w+Canvas.TextHeight(s) div 2+by;
412
Canvas.Rectangle(rc);
413
//debugln('TCustomBarChart.Paint A ',dbgs(rc),' s="',s,'"');
414
Canvas.TextOut(rc.Left+bx,rc.Top+by,s);
415
Canvas.Font.Color:=Font.Color;
416
case FLabelPosition of
417
plLeft: Canvas.TextOut(bx,z,ts.FSName);
418
plCenter: Canvas.TextOut(HMax+((BL-Canvas.TextWidth(ts.FSName)) div 2),z,ts.FSName);
419
plRight: Canvas.TextOut(HMax+BL-Canvas.TextWidth(ts.FSName)-bx,z,ts.FSName);
422
Canvas.Pen.Style:=psSolid;
425
function TCustomBarChart.RealGetText: TCaption;
430
Result := inherited RealGetText;
433
class function TCustomBarChart.GetControlClassDefaultSize: TSize;
439
procedure TCustomBarChart.Clear;
444
procedure TCustomBarChart.BeginUpdate;
449
procedure TCustomBarChart.EndUpdate;
451
if FUpdateCount=0 then
452
raise Exception.Create('TCustomBarChart.EndUpdate');
454
If FUpdateCount=0 then
458
procedure TCustomBarChart.UpdateBarChart;
460
if FUpdateCount = 0 then
464
function TCustomBarChart.BarCount: Integer;
471
procedure TBar.SetColor(const AValue: TColor);
473
if FColor=AValue then exit;
478
procedure TBar.SetSName(const AValue: String);
480
if FSName=AValue then exit;
485
procedure TBar.SetValue(const AValue: integer);
487
if FValue=AValue then exit;
492
procedure TBar.UpdateBarChart;
494
(Collection as TBarChartItems).FBarChart.UpdateBarChart;
497
function TBar.GetDisplayName: string;
504
function TBarChartItems.GetOwner: TPersistent;
509
constructor TBarChartItems.Create(BarChart: TCustomBarChart);
511
inherited Create(TBar);