procedure TCreateMultiCharts.ProcCreateCharts;
var
i,j,Rows,Columns,RowSpace,ChartsHeight:Integer;
ShapeChart:array of array of TShape;
begin
Rows:=16; //Shape控件数组行数
Columns:=8; // Shape控件数组列数
RowSpace:=20; // Shape控件行间距
ChartsHeight:=20; // Shape控件高度
SetLength(ShapeChart,Rows,Columns);
//设置ShapeChart数组大小
for i:=0 to Rows do
for j:=0 to Columns do
begin
ShapeChart[i][j]:=TShape.Create(self);
with ShapeChart[i,j] do
begin
Parent:=Self; //此行必不可少,
否则Shape控件在屏幕显示不出
Shape:=stRectangle; // Shape控件形状为矩形
Top:=45+i*(RowSpace+ChartsHeight);
Left:=Round(180+Q[i,j].StartTime);
//因Q[i,j].StartTime为实数,故需进行四舍五入取整
Width:=Round(Q[i,j].Value)
Height:=ChartsHeight;
Brush.Color:=RandomColor;
//自定义函数,说明附后
Brush.Style:=bsSolid; //设置填充方式
Enabled:=True;
end;
end;
end;
---- 注:
---- (1)Q为一记录型二维数组,定义如下:
type
TempData=Record
Value:Real;
StartTime:Real;
end;
Q:array of array of TempData
function TCreateMultiCharts.RandomColor;
var
red,green,blue:byte;
begin
red:=random(255);
green:=random(255);
blue:=random(255);
result:=red or (green shl 8) or (blue shl 16);
end;
---- 2、动态生成Charts控件的ChartSeries组件,显示设备利用率
procedure TFormMultiMachinesBurthen.
ShowMachineBurthenCharts;
var
i:Integer;
Burthen:Real;
SeriesClass:TChartSeriesClass;
NewSeries:array of TChartSeries;
begin
SetLength(NewSeries,CreateMultiCharts.Rows);
MachinesBurthenCharts.height:=200;
MachinesBurthenCharts.Width:=550;
for i:=0 to CreateMultiCharts.Rows do
begin
SeriesClass:=TBarSeries; //设置形状为三维条形图
NewSeries[i]:=SeriesClass.Create(Self);
NewSeries[i].ParentChart:=MachinesBurthenCharts;
NewSeries[i].Clear;
Burthen:=MachineBurthen[i];
Burthen:=Round(Burthen*100)/100; //只取小数点后两位数字
NewSeries[i].add(Burthen,'',NewSeries[i].SeriesColor);
end;
end;
============================================================================== 动态创建控件
==============================================================================
function DynaCreateComponent(Owner: TComponent; CompType: TControlClass; CompName: String; Left,Top,Width,Height:Integer): TControl;
begin
if (Owner.FindComponent(CompName)<>nil) and not(Owner.FindComponent(CompName) is TControl) then
begin
Result := nil;
exit;
end;
Result := Owner.FindComponent(CompName) as TControl;
if Result=nil then
begin
Result := CompType.Create(Owner);
with Result do
begin
if Owner is TwinControl then
begin
SetBounds(Left,Top,Width,Height);
Parent := TwinControl(Owner);{如果是可视构件,则显示之}
if Owner is TForm then TForm(Owner).ActiveControl := TWinControl(Result);{设置窗口焦点}
end;
end;
Result.Name := CompName;
end
else {Result<>Nil}
if not(Result is CompType) then
begin
Result := nil;
Exit;
end;
Result.Visible := True;
end;
{ 对于未知数量的控件组,利用TList
var ControlList: Tlist; CreateNum: integer;
const CreateClass : TControlClass = TButton;//可以任意修改TControlClass = TEdit或TPanel等。效果一样。
var i:integer; APoint: Pointer;
ControlList := TList.Create;
ControlList.Clear;
CreateNum := 10;
for i:=1 to CreateNum do
begin
APoint := Pointer(DynaCreateComponent(self,CreateClass,'Button_' + IntToStr(i),0,i*20+1,60,20));//创建
ControlList.Add(APoint);
end;
TButton(ControlList.Items[i]).Caption := 'XXXX';}