Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming

How to Select a "Bar" in the TChart Delphi control

By Zarko Gajic, About.com

Bar Select in Delphi Chart

Bar Select in Delphi Chart

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;

Delphi tips navigator:
» Register a File Command for a Windows Shell Menu from Delphi code
« Troubleshooting the "Left Side Cannot Be Assigned To" Compile Time Delphi Error
More Delphi Programming Quick Tips
Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2006 Tips
  7. How to Select a "Bar" in the TChart Delphi control

©2009 About.com, a part of The New York Times Company.

All rights reserved.