I have a form that has 56 charts on it. I defined the charts as an array and set the properties in the code snippet below:
var
TestProgressForm: TTestProgressForm;
Charts : array[1..MAX_WFR_COL,1..MAX_WFR_ROW] of Tchart
Procedure TTestProgressForm.AssignChartProperties;
var col,row : integer;
ChartSeries : TChartSeries;
begin
for col := 1 to MAX_WFR_COL do begin
for row := 1 to MAX_WFR_ROW do begin
Charts[col,row] := Tchart.Create(TestProgressForm);
Charts[col,row].Parent := TestProgressForm;
BarSeries[col,row] := TBarSeries.Create(self);
Charts[col,row].AddSeries(BarSeries[col,row]);
charts[col,row].Enabled := TRUE;
charts[col,row].Width := 80;
charts[col,row].Height := 80;
{set up properties here}
charts[col,row].Top := 15 + (row-1)*(charts[col,row].Height + 5);
charts[col,row].Left := 55 + (col-1)*(charts[col,row].Width + 5);
with charts[col,row].Series[0] as TBarSeries do begin
Clear;
CustomBarWidth := 10;
title := '';
Marks.Visible := FALSE;
ColorEachPoint := True;
Marks.Style := smsPercent;
Visible := False ;
ShowInLegend := False;
AutoMarkPosition := False;
HorizAxis := aBottomAxis;
VertAxis := aLeftAxis;
end;
end;
end;
end;
What I would like to do is set up the code so that when I click or double click on the chart a new form is displayed showing the details. The new form would be called TestDetails and would need to know which chart was double clicked on. I do not how to go about defining this.
Thank you,
p
1. In your form declaration add a new procedure declaration:
type
TTestProgressForm =
...
public
...
procedure ChartsClick(Sender: TObject); // common to all charts clicks
...
end;
2. Write that procedure in the 'implementation' section
procedure TTestProgressForm.ChartsCl
begin
if (Sender is TChart) then begin
// Assign the chart to a property of TestDetails form
TestDetails.TheChart := (Sender as TChart);
// Show the form
TestDetails.Show;
end;
end;
3. Add next line somewhere in your loop above (when creating the charts:
// This line assigns ChartsClick procedure as OnClick event handler (their declarations must be the same)
charts[col,row].OnClick := ChartsClick;
4. In TestDetails form:
TestDetails =
private
FChart: TChart; // new private field
....
public
property TheChart: TChart read FChart write FChart; // new public property to hold a reference to a chart
...
end;
Now, in the events of TestDetails form you can use 'TheChart' to obtain a reference to the chart that has been clicked last. For example:
with TheChart.Series[0] as TBarSeries do begin
...
end;
Perhaps there is an easier way but that's coming up in my mind now and it's almost 5 AM here. If you need more info, please ask.
Regards, Geo