Search code examples
delphiteechart

Series not showing in TeeChart when created at runtime?


I have a dataset which returns data like below

Date, Area, Sales

01/01/2017, Canteen, 1000
01/01/2017, Gym, 3000
01/01/2017, Salon, 2000
02/01/2017, Canteen, 5000
02/01/2017, Gym, 6000
02/01/2017, Salon, 7000

I am trying to create a stacked bar chart using this data

The number of series will be different

I have the logic below to create the series, but I am not sure how to add each value, e.g. I need the X Axis to be by date and have 2 bars. Each bar is then stacked with 3 series, as per the above data.

while not tblSalesBreakdownByDate.Eof do
begin
  nIndex := objList.IndexOf(tblSalesBreakdownByDateCategory.AsString);
  if nIndex = -1  then
  begin
    objSeries := TBarSeries.Create(Self);
    objSeries.MultiBar := TMultiBar.mbStacked;
    objSeries.Title := tblSalesBreakdownByDateCategory.AsString;

    chrtBreakdownByDate.SeriesList.Add(objSeries);
    objList.AddObject(objSeries.Title, objSeries)
  end
  else
    objSeries := objList.Objects[nIndex];

  objSeries.Add(tblSalesBreakdownByDateTotalSales.AsFloat, tblSalesBreakdownByDateTransactionDate.AsString);

  tblSalesBreakdownByDate.Next;
end;

When I run this, although it doesnt crash I dont see any of the series I have added?

Can anyone see what is wrong?

Is there an easy way to do this?

Ideally I would have preferred to have used a DB Chart but I am not sure how to do that given the data is not static?

Cheers

Paul


Solution

  • Instead of:

    chrtBreakdownByDate.SeriesList.Add(objSeries);
    

    call AddSeries method:

    chrtBreakdownByDate.AddSeries(objSeries);
    

    or without any addition method call set the ParentChart to the series:

    objSeries.ParentChart := chrtBreakdownByDate;
    

    The SeriesList collection doesn't listen to collection changes, hence the series was not added to the chart. Examples of how to add series at runtime you can find on the TChart reference page.