1
{ $Id: chart.pp 26150 2010-06-16 09:55:25Z mattias $ }
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.
162
RegisterComponents('Misc',[TBarChart]);
166
constructor TCustomBarChart.Create(AOwner: TComponent);
168
inherited Create(AOwner);
169
FBars:=TBarChartItems.Create(Self);
171
FLabelPosition:=plLeft;
172
with GetControlClassDefaultSize do
173
SetInitialBounds(0, 0, CX, CY);
176
destructor TCustomBarChart.Destroy;
182
function TCustomBarChart.AddBar(const SName: string; Value: Integer;
183
AColor: TColor): TBar;
187
result:=TBar(FBars.Add);
188
result.FsName:=SName;
189
result.FValue:=Value;
190
result.FColor:=AColor;
196
function TCustomBarChart.GetBar(SId: integer): TBar;
198
result:=TBar(FBars.FindItemID(SId));
201
function TCustomBarChart.NormalizeScaleUnits(OldScale: Integer): Integer;
210
else if Result<=5 then
212
else if Result<=10 then
216
T:=StrToInt(IntToStr(Result)[1])+1;
218
Result:=Result div 10;
225
function TCustomBarChart.GetBars: TCollection;
230
procedure TCustomBarChart.SetBars(const AValue: TCollection);
232
FBars.Assign(AValue);
235
procedure TCustomBarChart.SetDepth(const AValue: byte);
237
if FDepth=AValue then exit;
242
procedure TCustomBarChart.SetLabelPosition(const AValue: TPosLabel);
244
if FLabelPosition=AValue then exit;
245
FLabelPosition:=AValue;
249
procedure TCustomBarChart.Paint;
252
i,k,j,h,w,h1,HMax,VMax: integer;
254
NScaleLines : Integer;
255
ScaleUnits : Integer;
256
PixelPerUnit: Double;
265
procedure ScaleLine(dk: integer; const s: string);
268
Canvas.MoveTo(hmax+dk+FDepth,h1);
269
Canvas.LineTo(hmax+dk+FDepth,h1+h);
270
Canvas.LineTo(hmax+dk,h1+FDepth+h);
271
Canvas.LineTo(hmax+dk,h1+FDepth+h+2);
272
Canvas.TextOut(HMax+dk-j,m,s);
280
FIsPainting := false;
282
bx:=GetSystemMetrics(SM_CXEDGE);
283
by:=GetSystemMetrics(SM_CYEDGE);
286
for i:=0 to FBars.Count-1 do
288
ts:=TBar(FBars.Items[i]);
289
k:=Canvas.TextWidth(ts.FsName);
292
if ts.FValue>vmax then
296
h1:=RoundToInt(1.5*Canvas.TextHeight('W'));
297
h:=Height-2*h1-Fdepth;
298
w:=Width-hmax-2*FDepth;
299
Canvas.Pen.Color:=clBlack;
301
Canvas.Pen.Style:=psSolid;
302
Canvas.Brush.Color:=clYellow;
303
Canvas.Brush.Style:=bsSolid;
304
Canvas.Polygon([Point(HMax,h1+FDepth),Point(HMax,h1+FDepth+h),Point(HMax+FDepth,h1+h),Point(HMax+FDepth,h1)]);
305
Canvas.Brush.Color:=clWhite;
306
Canvas.Polygon([Point(HMax,h1+FDepth+h),Point(HMax+w,h1+FDepth+h),Point(HMax+w+FDepth,h1+h),Point(HMax+FDepth,h1+h)]);
307
Canvas.Brush.Color:=Color;
308
Canvas.Rectangle(hmax+Fdepth,h1,hmax+w+FDepth,h1+h+1);
310
Canvas.MoveTo(hmax,h1+FDepth);
311
Canvas.LineTo(hmax,h1+FDepth+h);
312
Canvas.LineTo(hmax+w,h1+FDepth+h);
313
Canvas.TextOut(bx,by,Caption);
314
j:=Canvas.TextWidth(IntTostr(VMax));
322
PixelPerUnit:=double(w-j-6) / VMax;
323
NScaleLines:=(w-j-6) div (2*j);
325
if NScaleLines=0 then
328
ScaleUnits:=(Vmax div NScaleLines) +1;
329
ScaleUnits:=NormalizeScaleUnits(ScaleUnits);
333
NScaleLines:=VMax div ScaleUnits;
334
Canvas.Pen.Color:=clGray;
335
Canvas.Pen.Style:=psDot;
345
Canvas.TextOut(HMax-j,m,'0');
346
for k:=1 to NScaleLines do
347
ScaleLine(RoundToInt(ScaleUnits*PixelPerUnit*k),IntToStr(k*ScaleUnits));
348
If FBars.Count=0 then
351
BC:=double(h) / (2*(FBars.Count+1));
354
Canvas.Pen.Style:=psSolid;
355
for i:=0 to FBars.Count-1 do
357
ts:=TBar(FBars.Items[i]);
358
z:=h1+FDepth+h-Round(2*(I+1)*BC);
359
Canvas.Brush.Color:=ts.FColor;
361
BL:=RoundToInt(m*PixelPerUnit);
362
Canvas.Rectangle(hmax+1,z-1,hmax+BL+1,z+RBC-1);
363
Canvas.Polygon([Point(hmax,z),Point(hmax+BL,z),Point(hmax+BL+FDepth,z-FDepth),Point(hmax+FDepth,z-FDepth)]);
364
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)]);
366
w:=z+(RBC-FDepth) div 2;
367
Canvas.MoveTo(Hmax+BL+Fdepth div 2,w);
368
Canvas.LineTo(Hmax+BL+Fdepth+5-bx,w);
369
Canvas.Brush.Color:=clYellow;
372
left:=hmax+BL+FDepth+5-bx;
373
right:=left+Canvas.TextWidth(s)+2*bx;
374
top:=w-Canvas.TextHeight(s) div 2-by;
375
bottom:=w+Canvas.TextHeight(s) div 2+by;
377
Canvas.Rectangle(rc);
378
//debugln('TCustomBarChart.Paint A ',dbgs(rc),' s="',s,'"');
379
Canvas.TextOut(rc.Left+bx,rc.Top+by,s);
380
Canvas.Font.Color:=Font.Color;
381
case FLabelPosition of
382
plLeft: Canvas.TextOut(bx,z,ts.FSName);
383
plCenter: Canvas.TextOut(HMax+((BL-Canvas.TextWidth(ts.FSName)) div 2),z,ts.FSName);
384
plRight: Canvas.TextOut(HMax+BL-Canvas.TextWidth(ts.FSName)-bx,z,ts.FSName);
387
Canvas.Pen.Style:=psSolid;
390
function TCustomBarChart.RealGetText: TCaption;
395
Result := inherited RealGetText;
398
class function TCustomBarChart.GetControlClassDefaultSize: TSize;
404
procedure TCustomBarChart.Clear;
409
procedure TCustomBarChart.BeginUpdate;
414
procedure TCustomBarChart.EndUpdate;
416
if FUpdateCount=0 then
417
raise Exception.Create('TCustomBarChart.EndUpdate');
419
If FUpdateCount=0 then
423
procedure TCustomBarChart.UpdateBarChart;
425
if FUpdateCount = 0 then
429
function TCustomBarChart.BarCount: Integer;
436
procedure TBar.SetColor(const AValue: TColor);
438
if FColor=AValue then exit;
443
procedure TBar.SetSName(const AValue: String);
445
if FSName=AValue then exit;
450
procedure TBar.SetValue(const AValue: integer);
452
if FValue=AValue then exit;
457
procedure TBar.UpdateBarChart;
459
(Collection as TBarChartItems).FBarChart.UpdateBarChart;
462
function TBar.GetDisplayName: string;
469
function TBarChartItems.GetOwner: TPersistent;
474
constructor TBarChartItems.Create(BarChart: TCustomBarChart);
476
inherited Create(TBar);