How to Select a “Bar” in the TChart Delphi control
Reference : http://delphi.about.com/od/adptips2006/qt/chart_selectbar.htm
Tip submitted by Meze Calin Nicolae
Here’s a method of simulating a selection in the TChart Delphi control. When a user clicks on a bar of a chart, the respective bar changes its color.
Everything is being done using one event, the onClickSeries, which provides us the index of the clicked bar.
Follow the steps:
- Drop a TChart (“Additional” tab) control on a Delphi form.
- Add one (empty) series to the Chart.
- Select a “Bar” chart style.
Declare two form level variables:
colors : array of TColor;
k : integer;
Fill in the Chart with some “dummy” data in the Form’s OnCreate event:
procedure TChartForm.FormCreate(Sender: TObject) ;
begin
SetLength(colors,7) ;
for k := 0 to 6 do colors[k] := clBlue;Chart1.Series[0].Add(10,’test2′,Colors[0]) ;
Chart1.Series[0].Add(70,’test3′,Colors[1]) ;
Chart1.Series[0].Add(90,’test4′,Colors[2]) ;
Chart1.Series[0].Add(40,’test5′,Colors[3]) ;
Chart1.Series[0].Add(66,’test6′,Colors[4]) ;
end;
Handle the OnClickSeries event as:
procedure TChartForm.Chart1ClickSeries(
Sender: TCustomChart;
Series: TChartSeries;
ValueIndex: Integer;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer) ;
begin
for k := 0 to 6 do
if k <> ValueIndex then
colors[k] := clBlue
else
colors[k] := clRed;Chart1.Series[0].Clear;
Chart1.Series[0].Add(10,’test2′,Colors[0]) ;
Chart1.Series[0].Add(70,’test3′,Colors[1]) ;
Chart1.Series[0].Add(90,’test4′,Colors[2]) ;
Chart1.Series[0].Add(40,’test5′,Colors[3]) ;
Chart1.Series[0].Add(66,’test6′,Colors[4]) ;
end;