TeeChart使用小技巧之 点击Series显示名称

Excel的曲线中有一个小功能,可以点击曲线后,显示出这条曲线的名称.
当一个Chart里显示Series过多时,颜色已经无法作为区分的手段,这个时候可以提供一个Selector的功能.
使用TeeChart的自带Tool实现.


使用SelectorTool(负责判断鼠标的点击对象)以及RectangTool(负责显示Series的Title等信息).
这里做了判断,如果点击的不是Series,则不进行操作并清除选择.
描述的话,就这些了.下面直接放源码.
dfm文件:

  1 object Form1: TForm1   
2 Left = 192
3 Top = 175
4 Width = 870
5 Height = 500
6 Caption = 'Form1'
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 = 8
20 Top = 24
21 Width = 737
22 Height = 409
23 Title.Text.Strings = (
24 'TChart')
25 View3D = False
26 TabOrder = 0
27 object Series1: TFastLineSeries
28 Marks.Arrow.Visible = True
29 Marks.Callout.Brush.Color = clBlack
30 Marks.Callout.Arrow.Visible = True
31 Marks.Visible = False
32 Title = 'Test Series1'
33 LinePen.Color = clRed
34 XValues.Name = 'X'
35 XValues.Order = loAscending
36 YValues.Name = 'Y'
37 YValues.Order = loNone
38 end
39 object Series2: TFastLineSeries
40 Marks.Arrow.Visible = True
41 Marks.Callout.Brush.Color = clBlack
42 Marks.Callout.Arrow.Visible = True
43 Marks.Visible = False
44 Title = 'Test Series2'
45 LinePen.Color = clGreen
46 XValues.Name = 'X'
47 XValues.Order = loAscending
48 YValues.Name = 'Y'
49 YValues.Order = loNone
50 end
51 object Series3: TFastLineSeries
52 Marks.Arrow.Visible = True
53 Marks.Callout.Brush.Color = clBlack
54 Marks.Callout.Arrow.Visible = True
55 Marks.Visible = False
56 Title = 'Test Series3'
57 LinePen.Color = clYellow
58 XValues.Name = 'X'
59 XValues.Order = loAscending
60 YValues.Name = 'Y'
61 YValues.Order = loNone
62 end
63 object Series4: TFastLineSeries
64 Marks.Arrow.Visible = True
65 Marks.Callout.Brush.Color = clBlack
66 Marks.Callout.Arrow.Visible = True
67 Marks.Visible = False
68 Title = 'Test Series4'
69 LinePen.Color = clBlue
70 XValues.Name = 'X'
71 XValues.Order = loAscending
72 YValues.Name = 'Y'
73 YValues.Order = loNone
74 end
75 object Series5: TFastLineSeries
76 Marks.Arrow.Visible = True
77 Marks.Callout.Brush.Color = clBlack
78 Marks.Callout.Arrow.Visible = True
79 Marks.Visible = False
80 SeriesColor = clBlack
81 Title = 'Test Series5'
82 XValues.Name = 'X'
83 XValues.Order = loAscending
84 YValues.Name = 'Y'
85 YValues.Order = loNone
86 end
87 object Series6: TFastLineSeries
88 Marks.Arrow.Visible = True
89 Marks.Callout.Brush.Color = clBlack
90 Marks.Callout.Arrow.Visible = True
91 Marks.Visible = False
92 Title = 'Test Series6'
93 LinePen.Color = clGray
94 XValues.Name = 'X'
95 XValues.Order = loAscending
96 YValues.Name = 'Y'
97 YValues.Order = loNone
98 end
99 object Series7: TFastLineSeries
100 Marks.Arrow.Visible = True
101 Marks.Callout.Brush.Color = clBlack
102 Marks.Callout.Arrow.Visible = True
103 Marks.Visible = False
104 Title = 'Test Series7'
105 LinePen.Color = clFuchsia
106 XValues.Name = 'X'
107 XValues.Order = loAscending
108 YValues.Name = 'Y'
109 YValues.Order = loNone
110 end
111 object Series8: TFastLineSeries
112 Marks.Arrow.Visible = True
113 Marks.Callout.Brush.Color = clBlack
114 Marks.Callout.Arrow.Visible = True
115 Marks.Visible = False
116 Title = 'Test Series8'
117 LinePen.Color = clTeal
118 XValues.Name = 'X'
119 XValues.Order = loAscending
120 YValues.Name = 'Y'
121 YValues.Order = loNone
122 end
123 object Series9: TFastLineSeries
124 Marks.Arrow.Visible = True
125 Marks.Callout.Brush.Color = clBlack
126 Marks.Callout.Arrow.Visible = True
127 Marks.Visible = False
128 Title = 'Test Series9'
129 LinePen.Color = clNavy
130 XValues.Name = 'X'
131 XValues.Order = loAscending
132 YValues.Name = 'Y'
133 YValues.Order = loNone
134 end
135 object Series10: TFastLineSeries
136 Marks.Arrow.Visible = True
137 Marks.Callout.Brush.Color = clBlack
138 Marks.Callout.Arrow.Visible = True
139 Marks.Visible = False
140 Title = 'Test Series10'
141 LinePen.Color = clMaroon
142 XValues.Name = 'X'
143 XValues.Order = loAscending
144 YValues.Name = 'Y'
145 YValues.Order = loNone
146 end
147 end
148 object CheckBox1: TCheckBox
149 Left = 760
150 Top = 112
151 Width = 97
152 Height = 17
153 Caption = 'Selector Tool'
154 TabOrder = 1
155 OnClick = CheckBox1Click
156 end
157 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, TeeSelectorTool, TeeTools,
8 StdCtrls;
9
10 type
11 TForm1 = class(TForm)
12 Chart1: TChart;
13 Series1: TFastLineSeries;
14 Series2: TFastLineSeries;
15 Series3: TFastLineSeries;
16 Series4: TFastLineSeries;
17 Series5: TFastLineSeries;
18 Series6: TFastLineSeries;
19 CheckBox1: TCheckBox;
20 Series7: TFastLineSeries;
21 Series8: TFastLineSeries;
22 Series9: TFastLineSeries;
23 Series10: TFastLineSeries;
24 procedure FormCreate(Sender: TObject);
25 procedure FormDestroy(Sender: TObject);
26 procedure CheckBox1Click(Sender: TObject);
27 private
28 { Private declarations }
29 FSelectorTool: TSelectorTool;
30 FRectangTool: TRectangleTool;
31 procedure SelectObject(Sender: TObject);
32 public
33 { Public declarations }
34 end;
35
36 var
37 Form1: TForm1;
38
39 implementation
40
41 {$R *.DFM}
42
43 procedure TForm1.FormCreate(Sender: TObject);
44 var
45 i: Integer;
46 begin
47 for i := 0 to Chart1.SeriesList.Count - 1 do
48 begin
49 Chart1.SeriesList[i].FillSampleValues(40);
50 end;
51 //Create Selector Tool
52 FSelectorTool := TSelectorTool.Create(Chart1);
53 FSelectorTool.ParentChart := Chart1;
54 FSelectorTool.Active := False;
55 FSelectorTool.AllowDrag := False;
56 FSelectorTool.AllowResizeChart := False;
57 FSelectorTool.Pen.SmallDots := True;
58 FSelectorTool.Pen.SmallSpace := 50;
59 FSelectorTool.OnSelected := SelectObject;
60 FSelectorTool.HandleSize := 1;
61
62 //create Rectrang Tool
63 FRectangTool := TRectangleTool.Create(Chart1);
64 FRectangTool.ParentChart := Chart1;
65 FRectangTool.Active := False;
66 FRectangTool.ClipText := False;
67 FRectangTool.AutoSize := True;
68 FRectangTool.Visible := False;
69 FRectangTool.Shape.Transparency := 20;
70 end;
71
72 procedure TForm1.SelectObject(Sender: TObject);
73 var
74 Str: string;
75 aPos: TPoint;
76 Max, Min: Single;
77 begin
78 if not (FSelectorTool.Selection is TChartSeries) then
79 begin
80 FSelectorTool.ClearSelection;
81 FRectangTool.Visible := False;
82 Exit;
83 end;
84 Str := (FSelectorTool.Selection as TChartSeries).Title;
85 Max := (FSelectorTool.Selection as TChartSeries).MaxYValue;
86 Min := (FSelectorTool.Selection as TChartSeries).MinYValue;
87 Str := Str + #13#10 + Format('Max: %.2f %sMin: %.2f', [Max,#13#10, Min]);
88 aPos := Chart1.GetCursorPos;
89 FRectangTool.Shape.Font.Color := (FSelectorTool.Selection as TChartSeries).Color;
90 FRectangTool.Top := aPos.y - 10;
91 FRectangTool.Left := aPos.x + 10;
92 FRectangTool.Text := Str;
93 FRectangTool.Visible := True;
94 end;
95
96 procedure TForm1.FormDestroy(Sender: TObject);
97 begin
98 FSelectorTool.Free;
99 FRectangTool.Free;
100 end;
101
102 procedure TForm1.CheckBox1Click(Sender: TObject);
103 begin
104 FSelectorTool.Active := CheckBox1.Checked;
105 end;
106
107 end.
原文地址:https://www.cnblogs.com/solokey/p/2126612.html