Page 1 of 1

Setup charts at runtime...

Posted: Tue Sep 19, 2017 1:31 am
by 16881567
Hi Yeray,

I've solved now (thanks to your help) all the issues I had with porting my app from VCL to FMX using TeeChart standard. Now I'm moving on to adding some new features in my app that make better use of TeeChart (I know my users will really like this).

But I'm having trouble knowing how to hook everything up programmatically. I've looked over the help files and have found some things, but not everything, as evidenced below. If you can help me with this simple example, I believe I can extrapolate from there.

Basically, I have a simple query that returns the following result set that I would like to be able to feed into a pie chart at run-time. Here is what I get when I manually edit the chart in Delphi Berlin 10.2 FMX and attach the query to it:
ChartFMX1.JPG
This is what the end result should look like. This is from design-time editing of the TeeChart.
ChartFMX1.JPG (28.33 KiB) Viewed 25387 times
Here is the result set from the query:
ChartResultSet.JPG
Above is the actual result set from the query.
ChartResultSet.JPG (14.56 KiB) Viewed 25383 times
So in trying to replicate this in code, I have started here:

Code: Select all

With GenderChart do
  begin
    AddSeries(TPieSeries.Create(Self));
    Series1.DataSource := Q;          // Q is the query component
    // Now I need to hook the two columns up to the pie chart. Here's where it breaks down.
    Series1.XLabelsSource := 'Gender';   
    Series1.XValues.ValueSource := 'COUNT of ALL';
  end;
I'm obviously not doing this right because when run the above code, nothing appears on the chart.

If you could point me to the place in the help files that speaks to this, or help me by just naming the properties I need to set to get the results programmatically that are shown in the example chart above, I believe I can do other charts like this from there.

Thanks for your patient help.

John

Re: Setup charts at runtime...

Posted: Tue Sep 19, 2017 7:32 am
by yeray
Hello John,

Your code looks quite correct to me, according to the Tutorial 8 - Database access.
The only thing I see is you are linking your query to Series1 but you are also creating a TPieSeries without assigning the result to Series1; so I'm not sure what series is Series1, or what in what chart is Series1.
I would do it as follows:

Code: Select all

var Series1: TPieSeries;
//...
With GenderChart do
  begin
    Series1:=AddSeries(TPieSeries.Create(Self)) as TPieSeries;
    Series1.DataSource := Q;          // Q is the query component
    // Now I need to hook the two columns up to the pie chart. Here's where it breaks down.
    Series1.XLabelsSource := 'Gender';   
    Series1.XValues.ValueSource := 'COUNT of ALL';
  end;

Re: Setup charts at runtime...

Posted: Tue Sep 19, 2017 1:50 pm
by 16881567
Yeray,

Thanks for trying to help me. Here's what I tried:

Code: Select all

procedure TChartTestForm.Button1Click(Sender: TObject);
Var Series1 : TPieSeries;
begin
  Q.Active := True;
  With Chart2 do
  begin
    Series1 := AddSeries(TPieSeries.Create(Self)) as TPieSeries;
    Series1.DataSource := Q;
    Series1.XLabelsSource := 'Gender';
    Series1.XValues.ValueSource := 'COUNT of ALL';
  end;
end;
The pie chart is still blank. Any further suggestions? Thanks so much.

John

Re: Setup charts at runtime...

Posted: Wed Sep 20, 2017 8:03 am
by yeray
Hello,

Here a simple test using "TeeChart Pro Database" shipped with the binary installation:

Code: Select all

uses Series, DBTables;

var Series1 : TPieSeries;
    Q: TQuery;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Q:=TQuery.Create(Self);
  Q.DatabaseName:='TeeChart Pro Database';
  Q.SQL.Add('SELECT LASTNAME, SALARY FROM Employee');

  Series1:=DBChart1.AddSeries(TPieSeries.Create(Self)) as TPieSeries;
  Series1.DataSource:=Q;
  Series1.XLabelsSource:='LASTNAME';
  Series1.YValues.ValueSource:='SALARY';

  Q.Active := True;
end;
Project2_2017-09-20_10-02-52.png
Project2_2017-09-20_10-02-52.png (21.5 KiB) Viewed 25358 times

Re: Setup charts at runtime...

Posted: Wed Sep 20, 2017 5:46 pm
by 16881567
Yeray,

Man, I'm still mystified! Here's my code now:

Code: Select all

procedure TChartTestForm.Button1Click(Sender: TObject);
Var Series1 : TPieSeries;
begin
    Series1 := GenderChart.AddSeries(TPieSeries.Create(Self)) as TPieSeries;
    Series1.DataSource := Q;
    Series1.XLabelsSource := 'Gender';
    Series1.XValues.ValueSource := 'COUNT of ALL';
    Q.Active := True;
 end;
When I run this, the result set is indeed returned (I've double-checked that). The pie chart looks just fine with this result set when I hook it up to a pie chart in design time. But there's something missing still in run-time.

On your code, you had Series in your Uses clause. I tried to add that and it wasn't found, so I'm assuming that's not really a part of this.

I don't see what's different about my code from your code, but mine does not work. When you first place a TDBChart component onto the form, there's a rectangular box that appears (thought it's all blank). When I run the above code, everything disappears on the TDBChart component. It's like it goes blank. Nothing is displayed.

One thing I tried to find but was not able to, was to find out what function is being done when you click the Apply button on the design-time editor when you are on the DataSource page where you assign the Dataset and the Values. Is there a run-time command that the Apply button is providing when it's clicked that I should be adding to my code?

I just don't see what I'm doing that's different from what you are doing! Sorry to be so much trouble to you about this.

John

Re: Setup charts at runtime...

Posted: Thu Sep 21, 2017 12:35 am
by 16881567
Yeray,

Found the issue, and I'm a bit embarrassed. Sorry! I had used...

Code: Select all

    Series1.XValues.ValueSource := 'COUNT of ALL';
instead of Series1.YValues (etc). That did the trick. I think I'm good for now. Thanks for your patient help.

John.

Re: Setup charts at runtime...

Posted: Thu Sep 21, 2017 6:21 am
by yeray
Hello John,

Don't worry! I glad to hear you can make it work as you wish!