File: main.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (221 lines) | stat: -rw-r--r-- 6,250 bytes parent folder | download | duplicates (2)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
unit main;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, TAGraph, TASeries, TASources, TATools, Forms,
  Controls, Graphics, Dialogs, StdCtrls, ComCtrls, ExtCtrls, Types,
  TAChartUtils, TACustomSeries, TAStyles, TADataTools;

type

  { TMainForm }

  TMainForm = class(TForm)
    CalculatedChartSource: TCalculatedChartSource;
    Chart: TChart;
    BarSeriesREDYELLOW: TBarSeries;
    BarSeriesBLUE: TBarSeries;
    ChartStyles: TChartStyles;
    ChartToolset: TChartToolset;
    DataPointDragTool: TDataPointDragTool;
    DataPointClickTool: TDataPointClickTool;
    DataPointHintTool: TDataPointHintTool;
    CbHorizontal: TCheckBox;
    Cb100Percent: TCheckBox;
    LblToolTargets: TLabel;
    LblInfo: TLabel;
    ListChartSourceLABELS: TListChartSource;
    ListChartSourceREDYELLOW: TListChartSource;
    ListChartSourceBLUE: TListChartSource;
    Log: TMemo;
    ToolInfoPanel: TPanel;
    TargetPanel: TPanel;
    LeftPanel: TPanel;
    RbToolTargetDataPoint: TRadioButton;
    RbToolTargetBar: TRadioButton;
    LogSplitter: TSplitter;
    procedure DataPointClickToolPointClick(ATool: TChartTool;
      APoint: TPoint);
    procedure DataPointDragToolBeforeMouseDown(ATool: TChartTool; APoint: TPoint
      );
    procedure DataPointDragToolDrag(ASender: TDataPointDragTool;
      var AGraphPoint: TDoublePoint);
    procedure DataPointHintToolHint(ATool: TDataPointHintTool;
      const APoint: TPoint; var AHint: String);
    procedure CbHorizontalChange(Sender: TObject);
    procedure Cb100PercentChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolTargetChanged(Sender: TObject);

  private

  public

  end;

var
  MainForm: TMainForm;

implementation

{$R *.lfm}

uses
  LCLType;


{ TMainForm }

{ Flip x and y axes of the chart }
procedure TMainForm.CbHorizontalChange(Sender: TObject);
begin
  if CbHorizontal.Checked then
  begin
    TChartSeries(Chart.Series[0]).AxisIndexX := 0;
    TChartSeries(Chart.Series[0]).AxisIndexY := 1;
    TChartSeries(Chart.Series[1]).AxisIndexX := 0;
    TChartSeries(Chart.Series[1]).AxisIndexY := 1;
    Chart.LeftAxis.Marks.Source := ListChartSourceLABELS;
    Chart.LeftAxis.Marks.Style := smsLabel;
    Chart.BottomAxis.Marks.Source := nil;
    Chart.BottomAxis.Marks.Style := smsValue;
    Chart.Margins.Left := 0;
    Chart.Margins.Bottom := 4;
  end else
  begin
    TChartSeries(Chart.Series[0]).AxisIndexX := 1;
    TChartSeries(Chart.Series[0]).AxisIndexY := 0;
    TChartSeries(Chart.Series[1]).AxisIndexX := 1;
    TChartSeries(Chart.Series[1]).AxisIndexY := 0;
    Chart.BottomAxis.Marks.Source := ListChartSourceLABELS;
    Chart.BottomAxis.Marks.Style := smsLabel;
    Chart.LeftAxis.Marks.Source := nil;
    Chart.LeftAxis.Marks.Style := smsValue;
    Chart.Margins.Bottom := 0;
    Chart.Margins.Left := 4;
  end;
end;

{ Normalize the stacked bars to 100% }
procedure TMainForm.Cb100PercentChange(Sender: TObject);
begin
  if Cb100Percent.Checked then
    BarSeriesREDYELLOW.Source := CalculatedChartSource
  else
    BarSeriesREDYELLOW.Source := ListChartSourceREDYELLOW;
end;

{ Every click detected by the DatapointClickTool creates an entry in the
  log memo }
procedure TMainForm.DataPointClickToolPointClick(ATool: TChartTool;
  APoint: TPoint);
var
  ser: TChartSeries;
  idx: Integer;
  yidx: Integer;
  ttl: String;
begin
  with TDatapointClickTool(ATool) do begin
    ser := Series as TChartSeries;
    idx := PointIndex;
    yidx := YIndex;
  end;

  if ser <> nil then begin
    ttl := ser.Title;
    if yidx > 0 then
      ttl := TBarSeries(ser).Styles.Styles[yidx].Text;
    Log.Lines.Add(Format('"%s" clicked:', [ttl]));
    Log.Lines.Add(Format('    point #%d, x = %.1f (%s), y = %.1f',
      [ idx, ser.GetXValue(idx), ListChartSourceLABELS.Item[idx]^.Text,
        ser.Source[idx]^.GetY(yidx) ]
    ));
    Log.SelStart := Length(Log.Lines.Text);
  end;
end;

{ Prevent an exception if the DragTool wants to change the non-editable
  calculated chart source }
procedure TMainForm.DataPointDragToolBeforeMouseDown(ATool: TChartTool;
  APoint: TPoint);
begin
  if Cb100Percent.Checked then begin
    ShowMessage('Please uncheck the 100% checkbox.');
    ATool.Handled;
  end;
end;

{ Make sure to keep x value fixed (or y value, if series is rotated) }
procedure TMainForm.DataPointDragToolDrag(ASender: TDataPointDragTool;
  var AGraphPoint: TDoublePoint);
var
  ser: TChartSeries;
begin
  ser := TChartSeries(ASender.Series);
  if ser.IsRotated then
    AGraphPoint.Y := ASender.Origin.Y
  else
    AGraphPoint.X := ASender.Origin.X;
end;

{ Prepare the text for the hint popup window of the DataPointHintTool }
procedure TMainForm.DataPointHintToolHint(ATool: TDataPointHintTool;
  const APoint: TPoint; var AHint: String);
var
  ser: TChartSeries;
  idx: Integer;
  yidx: Integer;
  ttl: String;
begin
  with TDatapointHintTool(ATool) do begin
    ser := Series as TChartSeries;
    idx := PointIndex;
    yidx := YIndex;
  end;

  if ser <> nil then begin
    if yidx > 0 then
      ttl := TBarSeries(ser).Styles.Styles[yidx].Text else
      ttl := ser.Title;
    AHint := Format(
      'Mouse over "%s": point #%d, x value  %.1f (%s), y value %.1f',
      [ ttl, idx, ser.GetXValue(idx), ListChartSourceLABELS.Item[idx]^.Text,
        ser.Source[idx]^.GetY(yidx) ]
    );
  end else
    AHint := '';
end;

procedure TMainForm.FormCreate(Sender: TObject);
const
  N = 6;
var
  i: Integer;
begin
  for i:=0 to 5 do begin
    ListChartSourceRedYellow.AddXYList(i, [random*50, random*60]);
    ListChartSourceBlue.Add(i, Random*80);
    ListChartSourceLABELS.Add(i, i, char(ord('A') + i));
  end;
  CbHorizontalChange(nil);
end;

{ Change the clicking behavior of the click and hint tools by modifying their
  Targets. }
procedure TMainForm.ToolTargetChanged(Sender: TObject);
begin
  if Sender = RbToolTargetDatapoint then begin
    DataPointClickTool.Targets := DataPointClickTool.Targets - [nptCustom];
    DataPointHintTool.Targets := DatapointHintTool.Targets - [nptCustom];
  end else
  if Sender = RbToolTargetBar then begin
    DataPointClickTool.Targets := DataPointClickTool.Targets + [nptCustom];
    DataPointHintTool.Targets := DatapointHintTool.Targets + [nptCustom];
  end;
end;

end.