Solved

How do I define a series for TChart without using the object inspector?

Posted on 2002-06-27
2
833 Views
Last Modified: 2010-04-04
I have 56 charts I want to show on a single form.  Rather than define each individually I set the charts up using an array of Tchart and defined as shown in the code below.  

var
  TestProgressForm: TTestProgressForm;
   Charts : array[1..MAX_WFR_COL,1..MAX_WFR_ROW] of Tchart;

Procedure TTestProgressForm.AssignChartProperties;
var col,row : integer;
begin
    for row := 1 to MAX_WFR_ROW do begin
    for col := 1 to MAX_WFR_COL do begin
      Charts[col,row] := Tchart.Create(TestProgressForm);
      Charts[col,row].Parent := TestProgressForm;
      charts[col,row].Enabled := TRUE;
      charts[col,row].BackWall.Color :=clRed;
      charts[col,row].Width := 80;
      charts[col,row].Height := 80;
      charts[col,row].MarginBottom := 0;
      charts[col,row].MarginLeft := 0;
      charts[col,row].MarginRight := 0;
      charts[col,row].MarginTop := 0;
      charts[col,row].Visible := True;
      charts[col,row].BackColor := clWhite;
      Charts[col,row].BottomAxis.Visible := FALSE;
      charts[col,row].LeftAxis.Visible := FALSE;
      charts[col,row].Legend.Frame.Visible := FALSE;
      charts[col,row].RightAxis.Visible := FALSE;
      charts[col,row].TopAxis.Visible := FALSE;
      charts[col,row].Title.Frame.Visible := FALSE;
      charts[col,row].View3D := FALSE;
      charts[col,row].Top := 525 - (row-1)*(charts[col,row].Height + 5);
      charts[col,row].Left := 55 + (col-1)*(charts[col,row].Width + 5);
    end;
  end;
end;

Defining the charts seems to work fine.  What I am having problems with is setting up the series associated with the chart.  I want to set up a barchart with 9 bars.  When I tried it using the object inspector the following code seemed to work fine:

type
  TTestProgressForm = class(TForm)
    ChartC4R7: TChart;
    BarSeries5: TBarSeries;
...

  (ChartC4R7.Series[0]as TBarSeries).CustomBarWidth := 10;
  (ChartC4R7.Series[0]as TBarSeries).AddXY(1,100,'',clGreen);
  (ChartC4R7.Series[0]as TBarSeries).AddXY(2,100,'',clRed);
  (ChartC4R7.Series[0]as TBarSeries).AddXY(3,100,'',clRed);
  (ChartC4R7.Series[0]as TBarSeries).AddXY(4,100,'',clGreen);
  (ChartC4R7.Series[0]as TBarSeries).AddXY(5,100,'',clGreen);
  (ChartC4R7.Series[0]as TBarSeries).AddXY(6,100,'',clRed);
  (ChartC4R7.Series[0]as TBarSeries).AddXY(7,100,'',clSkyBlue);
  (ChartC4R7.Series[0]as TBarSeries).AddXY(8,100,'',clBlue);
  (ChartC4R7.Series[0]as TBarSeries).AddXY(9,100,'',clGreen);

When I try to do the same code using Charts[col,row] instead of ChartC4R7 I get the following Debugger Exception Notification:

Project WaferProber.exe raised exception class ElistError with message 'List index out of bounds(0)'....

I know I must be missing a step but I don't know what it is.

Thanks,
P





0
Comment
Question by:PattiGaultney
  • 2
2 Comments
 
LVL 8

Expert Comment

by:TOndrej
ID: 7114366
I guess you should also create your TBarSeries.
"List index out of bounds (0)" probably means you're trying to access Series[0] while there is no series component.
0
 
LVL 8

Accepted Solution

by:
TOndrej earned 100 total points
ID: 7114409
I've read the title of your question and it seems you're already aware of the cause of the error; and you're actually asking how to set up the bar series.
Here is a quick code snippet:

procedure TForm1.Button1Click(Sender: TObject);
var
  Chart: TChart;
  BarSeries: TBarSeries;
begin
  Chart := TChart.Create(Self);
  try
    Chart.Parent := Self;
    Chart.Left := 8;
    Chart.Top := 8;
    BarSeries := TBarSeries.Create(Self);
    try
      Chart.AddSeries(BarSeries);
      // your code
      with Chart.Series[0] as TBarSeries do
      begin
        CustomBarWidth := 10;
        AddXY(1,100,'',clGreen);
        AddXY(2,100,'',clRed);
        AddXY(3,100,'',clRed);
        AddXY(4,100,'',clGreen);
        AddXY(5,100,'',clGreen);
        AddXY(6,100,'',clRed);
        AddXY(7,100,'',clSkyBlue);
        AddXY(8,100,'',clBlue);
        AddXY(9,100,'',clGreen);
      end;
    except
      BarSeries.Free;
      raise;
    end;
  except
    Chart.Free;
    raise;
  end;
end;

HTH
TOndrej
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question