1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
ExtCtrls, StdCtrls,
TAGraph, TASeries, TAStyles, TASources, TAChartListbox;
type
{ TForm1 }
TForm1 = class(TForm)
BtnAddBar: TButton;
BtnAddLevel: TButton;
BtnDelete: TButton;
CalculatedChartSource1: TCalculatedChartSource;
Chart1: TChart;
Chart1BarSeries1: TBarSeries;
CbPercent: TCheckBox;
ChartStyles1: TChartStyles;
ListBox1: TListBox;
ListChartSource1: TListChartSource;
Panel1: TPanel;
procedure BtnAddBarClick(Sender: TObject);
procedure BtnAddLevelClick(Sender: TObject);
procedure BtnDeleteClick(Sender: TObject);
procedure CbPercentChange(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
const
N = 5;
procedure TForm1.BtnAddBarClick(Sender: TObject);
var
x, y: Double;
i, n: Integer;
style: TChartStyle;
begin
x := ListChartSource1.Count;
y := Random * 0.9 + 0.1; // Random number between 0.1 and 1.0
n := ListChartSource1.Add(x, y);
for i:=0 to ListChartSource1.YCount-2 do
ListChartSource1.Item[n]^.YList[i] := random;
if ChartStyles1.Styles.Count = 0 then begin
style := TChartStyle(ChartStyles1.Styles.Add);
style.Brush.Color := RgbToColor(random(256), random(256), random(256));
style.Text := 'Y';
Listbox1.Items.Add(style.Text);
end;
BtnDelete.Enabled := true;
end;
procedure TForm1.BtnAddLevelClick(Sender: TObject);
var
yidx: Integer;
i: Integer;
style: TChartStyle;
begin
// Increment the number of y values and add random data to the new y value
ListChartSource1.BeginUpdate;
try
yidx := ListChartSource1.YCount - 1;
ListChartSource1.YCount := ListChartSource1.YCount + 1;
for i := 0 to ListChartSource1.Count-1 do
ListChartSource1.Item[i]^.YList[yidx] := random;
finally
ListChartSource1.EndUpdate;
end;
style := TChartStyle(ChartStyles1.Styles.Add);
style.Brush.Color := RgbToColor(random(256), random(256), random(256));
style.Text := 'YList[' + IntToStr(yidx) + ']';
Listbox1.Items.Add(style.Text);
Listbox1.ItemIndex := Listbox1.Items.Count - 1;
BtnDelete.Enabled := true;
end;
procedure TForm1.BtnDeleteClick(Sender: TObject);
var
idx: Integer; // Index of level to be deleted
i, j: Integer;
begin
idx := Listbox1.ItemIndex;
if (ListChartSource1.YCount = 1) then begin
ListChartSource1.Clear;
exit;
end;
// Delete level in Listsource
ListChartSource1.BeginUpdate;
try
for i := 0 to ListChartSource1.Count-1 do begin
if idx = 0 then
ListChartSource1.Item[i]^.Y := ListChartSource1.Item[i]^.YList[0];
for j:=idx to ListChartSource1.YCount-3 do
ListChartSource1.Item[i]^.YList[j] := ListChartSource1.Item[i]^.YList[j+1];
end;
ListChartSource1.YCount := ListChartSource1.YCount - 1;
finally
ListChartSource1.EndUpdate;
end;
// Delete level in ChartStyles
ChartStyles1.Styles.Delete(idx);
// Delete item in Listbox
Listbox1.Items.Delete(idx);
if idx < Listbox1.Items.Count then
Listbox1.ItemIndex := idx
else
Listbox1.ItemIndex := Listbox1.Items.Count - 1;
BtnDelete.Enabled := Listbox1.Items.Count > 0;
end;
procedure TForm1.CbPercentChange(Sender: TObject);
begin
CalculatedChartSource1.Percentage := CbPercent.Checked;
end;
end.
|