TeeChart使用小技巧之 曲线分页显示,轴分别显示日期

TeeChart里有一个Tools,TPageNumTool.可以用于在Chart上显示当前页和总页数.还有一个TChartPageNavigator,可以用于控制Chart的最前页,前一页,后一页,最终页.
Chart可以显示2个轴,1个是Bottom,1个是Top,代码里做了限制,如果添加Series的时候指定了asDateTime,那么上下2个轴都会显示为时间.不可以一个显示时间,另外一个显示别的.这里,如果要分别显示,需要修改源代码,修改方式如下:
在TeEngine.Unit的TChartAxis里,增加一个Public的属性:property NeedDateTime : Boolean read FNeedDateTime write FNeedDateTime;再增加一个私有字段:FNeedDateTime : Boolean;
修改Function TChartAxis.IsDateTime:Boolean;函数.

代码如下:

 1 Function TChartAxis.IsDateTime:Boolean;   
2 var tmpSeries : Integer;
3 tmpList : TChartValueList;
4 Begin
5 if FNeedDateTime then
6 begin
7 Result := True;
8 Exit;
9 end; //solokey
10 With ParentChart do
11 for tmpSeries:=0 to SeriesCount-1 do
12 With Series[tmpSeries] do
13 if Active then
14 begin
15 tmpList:=ValueListOfAxis(Self);
16 if Assigned(tmpList) then
17 begin
18 result:=tmpList.DateTime;
19 exit;
20 end;
21 end;
22
23 result:=False;
24 end;

做完这些修改,只要把Chart.TopAxis或者Chart.BottomAxis的NeedDateTime设置为True,就可以显示时间了.
下面的Demo,除了完成上述的分页显示,轴分别显示DateTime,还提供了一个同时Zoom横向轴和纵向轴的方法.

dfm:

 1 object Form1: TForm1   
2 Left = 194
3 Top = 170
4 Width = 870
5 Height = 500
6 Caption = 'Chart Page'
7 Color = clBtnFace
8 Font.Charset = DEFAULT_CHARSET
9 Font.Color = clWindowText
10 Font.Height = -11
11 Font.Name = 'MS Sans Serif'
12 Font.Style = []
13 OldCreateOrder = False
14 OnCreate = FormCreate
15 OnDestroy = FormDestroy
16 PixelsPerInch = 96
17 TextHeight = 13
18 object Chart1: TChart
19 Left = 16
20 Top = 48
21 Width = 833
22 Height = 409
23 Title.Text.Strings = (
24 'TChart')
25 OnZoom = Chart1Zoom
26 View3D = False
27 TabOrder = 4
28 end
29 object ChartPageNavigator1: TChartPageNavigator
30 Left = 24
31 Top = 8
32 Width = 240
33 Height = 25
34 TabOrder = 0
35 Chart = Chart1
36 OnButtonClicked = ChartPageNavigator1ButtonClicked
37 end
38 object Button1: TButton
39 Left = 576
40 Top = 8
41 Width = 75
42 Height = 25
43 Caption = 'Reset'
44 TabOrder = 1
45 OnClick = Button1Click
46 end
47 object CheckBox1: TCheckBox
48 Left = 465
49 Top = 11
50 Width = 73
51 Height = 17
52 Caption = 'Zoom'
53 TabOrder = 2
54 OnClick = CheckBox1Click
55 end
56 object ScrollBar1: TScrollBar
57 Left = 294
58 Top = 12
59 Width = 163
60 Height = 17
61 Hint = 'MaxPointsPerPage: 5'
62 Min = 5
63 PageSize = 0
64 ParentShowHint = False
65 Position = 5
66 ShowHint = True
67 TabOrder = 3
68 OnChange = ScrollBar1Change
69 end
70 end

pas:

  1 unit Unit1;   
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7 TeEngine, Series, ExtCtrls, TeeProcs, Chart, StdCtrls, TeeNavigator,
8 TeeEdiGene, TeeTools, TeePageNumTool;
9
10 type
11 TForm1 = class(TForm)
12 Chart1: TChart;
13 ChartPageNavigator1: TChartPageNavigator;
14 Button1: TButton;
15 CheckBox1: TCheckBox;
16 ScrollBar1: TScrollBar;
17 procedure Button1Click(Sender: TObject);
18 procedure FormCreate(Sender: TObject);
19 procedure FormDestroy(Sender: TObject);
20 procedure ScrollBar1Change(Sender: TObject);
21 procedure ChartPageNavigator1ButtonClicked(Index: TTeeNavigateBtn);
22 procedure CheckBox1Click(Sender: TObject);
23 procedure Chart1Zoom(Sender: TObject);
24 private
25 { Private declarations }
26 FPageTool: TPageNumTool;
27 FSeries1: TFastLineSeries;
28 FSeries2: TFastLineSeries;
29 FDateTime: TDateTime;
30 procedure InitPage;
31 public
32 { Public declarations }
33 end;
34
35 var
36 Form1: TForm1;
37
38 implementation
39
40 {$R *.DFM}
41
42 procedure TForm1.Button1Click(Sender: TObject);
43 begin
44 Chart1.UndoZoom;
45 end;
46
47 procedure TForm1.FormCreate(Sender: TObject);
48 begin
49 FPageTool := TPageNumTool.Create(Chart1);
50 FPageTool.ParentChart := Chart1;
51 FPageTool.ShowButtons := False;
52 FPageTool.Position := ppLeftTop;
53 FPageTool.Format := '第 %d页,共 %d页';
54
55 FSeries1 := TFastLineSeries.Create(Chart1);
56 FSeries1.ParentChart := Chart1;
57 FSeries1.VertAxis := aLeftAxis;
58 FSeries1.HorizAxis := aBothHorizAxis;
59
60 FSeries2 := TFastLineSeries.Create(Chart1);
61 FSeries2.ParentChart := Chart1;
62 FSeries2.VertAxis := aLeftAxis;
63 FSeries2.HorizAxis := aBothHorizAxis;
64
65 Chart1.TopAxis.NeedDateTime := True;
66 Chart1.TopAxis.Grid.Visible := False;
67 Chart1.TopAxis.LabelsSeparation := 20;
68 Chart1.TopAxis.DateTimeFormat := 'YY-MM-DD HH:NN:SS';
69
70 Chart1.BottomAxis.Title.Caption := '(Minute)';
71
72 FSeries1.FillSampleValues(100);
73 FSeries2.FillSampleValues(100);
74
75 FDateTime := Now;
76
77 Chart1.MaxPointsPerPage := ScrollBar1.Position;
78 InitPage;
79
80 Chart1.Zoom.Pen.Color := clRed;
81 end;
82
83 procedure TForm1.FormDestroy(Sender: TObject);
84 begin
85 FSeries1.Free;
86 FSeries2.Free;
87 FPageTool.Free;
88 end;
89
90 procedure TForm1.ScrollBar1Change(Sender: TObject);
91 begin
92 Chart1.MaxPointsPerPage := ScrollBar1.Position;
93 ScrollBar1.Hint := Format('MaxPointsPerPage: %d', [ScrollBar1.Position]);
94 InitPage;
95 end;
96
97 procedure TForm1.ChartPageNavigator1ButtonClicked(Index: TTeeNavigateBtn);
98 var
99 Min, Max: Double;
100 begin
101 Chart1.UndoZoom;
102 Min := (Chart1.Page - 1) * ScrollBar1.Position;
103 Max := Chart1.Page * ScrollBar1.Position;
104 Chart1.BottomAxis.SetMinMax(Min, Max);
105 Chart1.TopAxis.SetMinMax(FDateTime + Min * 60 / SecsPerDay, FDateTime + Max * 60 / SecsPerDay);
106 end;
107
108 procedure TForm1.CheckBox1Click(Sender: TObject);
109 begin
110 Chart1.OriginalCursor := crDefault;
111 if CheckBox1.Checked then
112 Chart1.OriginalCursor := crCross;
113 Chart1.Zoom.Allow := CheckBox1.Checked;
114 end;
115
116 procedure TForm1.Chart1Zoom(Sender: TObject);
117 var
118 i: Integer;
119 Min,Max: Double;
120 begin
121 with Chart1.LeftAxis, Chart1.Zoom do
122 begin
123 if Visible then
124 begin
125 Min := CalcPosPoint(Y0);
126 Max := CalcPosPoint(Y1);
127 SetMinMax(Min, Max);
128 end;
129 end;
130 end;
131
132 procedure TForm1.InitPage;
133 var
134 Min, Max: Double;
135 begin
136 Chart1.UndoZoom;
137 Min := 0;
138 Max := ScrollBar1.Position;
139 Chart1.BottomAxis.SetMinMax(Min, Max);
140 Chart1.TopAxis.SetMinMax(FDateTime + Min * 60 / SecsPerDay, FDateTime + Max * 60 / SecsPerDay);
141 ChartPageNavigator1.EnableButtons;
142 ChartPageNavigator1.Buttons[nbFirst].Click;
143 end;
144
145 end.
原文地址:https://www.cnblogs.com/solokey/p/2126616.html