TAChart: create new demo runtime/chartstyles to show how to add/delete chartstyles at runtime (see http://forum.lazarus.freepascal.org/index.php/topic,35538.0.html)

git-svn-id: trunk@53991 -
This commit is contained in:
wp 2017-01-24 11:53:18 +00:00
parent abd19309ef
commit 14d17cd1e7
6 changed files with 395 additions and 0 deletions

5
.gitattributes vendored
View File

@ -4331,6 +4331,11 @@ components/tachart/demo/rotate/Main.lfm svneol=native#text/pascal
components/tachart/demo/rotate/Main.pas svneol=native#text/pascal
components/tachart/demo/rotate/rotatedemo.lpi svneol=native#text/plain
components/tachart/demo/rotate/rotatedemo.lpr svneol=native#text/pascal
components/tachart/demo/runtime/chartstyles/project1.lpi svneol=native#text/plain
components/tachart/demo/runtime/chartstyles/project1.lpr svneol=native#text/plain
components/tachart/demo/runtime/chartstyles/readme.txt svneol=native#text/plain
components/tachart/demo/runtime/chartstyles/unit1.lfm svneol=native#text/plain
components/tachart/demo/runtime/chartstyles/unit1.pas svneol=native#text/plain
components/tachart/demo/save/main.lfm svneol=native#text/plain
components/tachart/demo/save/main.pas svneol=native#text/plain
components/tachart/demo/save/savedemo.lpi svneol=native#text/plain

View File

@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="10"/>
<PathDelim Value="\"/>
<General>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<Title Value="project1"/>
<ResourceType Value="res"/>
<UseXPManifest Value="True"/>
<Icon Value="0"/>
</General>
<BuildModes Count="1">
<Item1 Name="Default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
</PublishOptions>
<RunParams>
<local>
<FormatVersion Value="1"/>
</local>
</RunParams>
<RequiredPackages Count="2">
<Item1>
<PackageName Value="TAChartLazarusPkg"/>
</Item1>
<Item2>
<PackageName Value="LCL"/>
</Item2>
</RequiredPackages>
<Units Count="3">
<Unit0>
<Filename Value="project1.lpr"/>
<IsPartOfProject Value="True"/>
</Unit0>
<Unit1>
<Filename Value="unit1.pas"/>
<IsPartOfProject Value="True"/>
<ComponentName Value="Form1"/>
<HasResources Value="True"/>
<ResourceBaseClass Value="Form"/>
<UnitName Value="Unit1"/>
</Unit1>
<Unit2>
<Filename Value="readme.txt"/>
<IsPartOfProject Value="True"/>
</Unit2>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<PathDelim Value="\"/>
<Target>
<Filename Value="project1"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Linking>
<Debugging>
<UseExternalDbgSyms Value="True"/>
</Debugging>
<Options>
<Win32>
<GraphicApplication Value="True"/>
</Win32>
</Options>
</Linking>
</CompilerOptions>
<Debugging>
<Exceptions Count="3">
<Item1>
<Name Value="EAbort"/>
</Item1>
<Item2>
<Name Value="ECodetoolError"/>
</Item2>
<Item3>
<Name Value="EFOpenError"/>
</Item3>
</Exceptions>
</Debugging>
</CONFIG>

View File

@ -0,0 +1,21 @@
program project1;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Interfaces, // this includes the LCL widgetset
Forms, Unit1, tachartlazaruspkg
{ you can add units after this };
{$R *.res}
begin
RequireDerivedFormResource := True;
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.

View File

@ -0,0 +1,21 @@
This project shows how to add and delete levels to a stacked bar series
at runtime. For every new level, a new chart style is created, and for
every deleted level, the corresponding chart style is deleted as well.
Background info on ChartStyles at runtime:
- The TChartStyles component houses a collection named Styles which contains
all styles. Therefore, you simply call ChartStyles1.Styles.Add to create a
new ChartStyle and add it to the collection. The Add method returns a pointer
to the newly created collectionitem; you must cast it TChartStyle to get
access to its properties.
- In order to delete a style just call the Delete method of the collection with
the index of the corresponding style.
Instructions for running the demo:
- Click "Add bar" to add new data points (bars)
- Click "Add level" to add a new level to the stacked bars
- Click "Delete selected" to remove the level selected in the listbox from the series.
See also:
- http://forum.lazarus.freepascal.org/index.php/topic,35538.0.html

View File

@ -0,0 +1,117 @@
object Form1: TForm1
Left = 280
Height = 334
Top = 130
Width = 725
Caption = 'Form1'
ClientHeight = 334
ClientWidth = 725
LCLVersion = '1.7'
object Chart1: TChart
Left = 0
Height = 296
Top = 0
Width = 601
AxisList = <
item
Grid.Color = clSilver
Grid.Style = psSolid
Minors = <>
Title.LabelFont.Orientation = 900
end
item
Grid.Visible = False
Alignment = calBottom
Minors = <>
end>
BackColor = clWhite
Foot.Brush.Color = clBtnFace
Foot.Font.Color = clBlue
Legend.Visible = True
Title.Brush.Color = clBtnFace
Title.Font.Color = clBlue
Title.Text.Strings = (
'TAChart'
)
Align = alClient
object Chart1BarSeries1: TBarSeries
Legend.Multiplicity = lmStyle
BarBrush.Color = clRed
Source = CalculatedChartSource1
Styles = ChartStyles1
end
end
object Panel1: TPanel
Left = 0
Height = 38
Top = 296
Width = 725
Align = alBottom
BevelOuter = bvNone
ClientHeight = 38
ClientWidth = 725
TabOrder = 1
object BtnAddBar: TButton
Left = 11
Height = 25
Top = 4
Width = 75
Caption = 'Add bar'
OnClick = BtnAddBarClick
TabOrder = 0
end
object BtnAddLevel: TButton
Left = 99
Height = 25
Top = 4
Width = 75
Caption = 'Add level'
OnClick = BtnAddLevelClick
TabOrder = 1
end
object CbPercent: TCheckBox
Left = 191
Height = 19
Top = 7
Width = 48
Caption = '100%'
OnChange = CbPercentChange
TabOrder = 2
end
object BtnDelete: TButton
Left = 610
Height = 25
Top = 4
Width = 107
Anchors = [akTop, akRight]
Caption = 'Delete selected'
Enabled = False
OnClick = BtnDeleteClick
TabOrder = 3
end
end
object ListBox1: TListBox
Left = 609
Height = 280
Top = 8
Width = 108
Align = alRight
BorderSpacing.Around = 8
ItemHeight = 0
TabOrder = 2
end
object ListChartSource1: TListChartSource
left = 117
top = 57
end
object CalculatedChartSource1: TCalculatedChartSource
Origin = ListChartSource1
left = 116
top = 112
end
object ChartStyles1: TChartStyles
Styles = <>
left = 268
top = 58
end
end

View File

@ -0,0 +1,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.