I'm having trouble figuring out how to add TZoomDragTool to a TChart. I know that TChart has built in support for zooming, but I want to disable panning (drag with right click) at the same time. There is a property to disable zooming, but no straight forward way to disable panning.
What I found out is that if I add a TChartToolSet to the chart, it will disable both the built in zooming and panning capabilities. If I can add a TZoomDragTool to the TChartToolSet, then it will do what I want. It works well if I do it at design time, but I don't know how to do it at run time.
Appreciate it if someone could show me how to do it.
I have a small code snippet and it gives me errors if I run it.
TForm1 = class(TForm)
Button1: TButton;
m_chart: TChart;
m_toolset: TChartToolset;
m_zoom: TZoomDragTool;
procedure Button1Click(Sender: TObject);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
m_chart:= TChart.Create(self);
with m_chart do begin
autosize:= False;
left:= 10;
top:= 40;
width:= 300;
height:= 150;
parent:= Self;
visible:= True;
end;
m_toolset:= TChartToolset.Create(m_chart); //is the owner correct?
m_zoom:= TZoomDragTool.Create(m_toolset); //is the owner correct?
m_toolset.Tools.Add(@m_zoom); //is this correct?
m_chart.Toolset:= m_toolset; //is this the way?
//or something like
//m_chart.Toolset.InsertComponent(m_toolset);
//both of them crash
end;
TAChart has a bit strange way of adding a TChartTool
(which is a TIndexedComponent
) to the TChartToolset
. The TChartToolset
has a TIndexedComponentList
, Tools
, which acts similar to a collection, and all the code of the people having difficulties here is just adding the TIndexedComponent
to the TIndexedComponentList
by calling its Add
method. However, this does not do all the work. Instead, the TChartTool
has a public property Toolset
which you must assign to the TChartToolset
component that you added to the form.
This is the correct code:
m_toolset:= TChartToolset.Create(self);
// since ChartTools can be used by several charts it is safer to have the ChartToolset be owned by the form
m_zoom:= TZoomDragTool.Create(m_toolset);
m_zoom.ToolSet := m_toolset;
Besides the ChartTools, the same code is required also for run-time created ChartTransformations.