Stacked Bar Chart with Dynamic number of groups

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
jz8
Newbie
Newbie
Posts: 13
Joined: Thu Feb 10, 2005 5:00 am

Stacked Bar Chart with Dynamic number of groups

Post by jz8 » Wed Jul 30, 2008 11:13 am

Using TeeChart 7 and Delphi 7:

I need to create a stacked bar chart (hopefully horizontal) with 2 bars but an dynamic number of items in each bar.

It is my understanding that the fixed number of bars are not the series but each item is a series. Therefore, I will need to dynamically create series.

I know it is possible, but I realize I may have to index them in an array or something like that -- since I will not always know the number. This seems do-able but difficult.

It would be easier if I could use the 2 groups as series. Is this possible?

thanks --

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Wed Jul 30, 2008 11:20 am

Hi jz8,

I may not understand what you are looking for correctly. However my suggestion would be using 2 Bar/HorizBar series and set them to mbSelfStack:

Code: Select all

  Series1.MultiBar:=mbSelfStack;
So that you can dynamically add as many values you wish to each series.

If that's not what you are trying to achieve please give us some more details. An example image would be helpful. You can post your files at news://www.steema.net/steema.public.attachments newsgroup or at our upload page.

Thanks in advance.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

jz8
Newbie
Newbie
Posts: 13
Joined: Thu Feb 10, 2005 5:00 am

Post by jz8 » Wed Jul 30, 2008 11:47 am

I may not really understand selfstack -- but I sent the example as suggested.

thanks.

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Wed Jul 30, 2008 11:52 am

Hi jz8,

Thanks for the file.

Yes, using 2 THorizBarSeries and setting them to mbSelfStack would avoid having to dynamically create series. You'll find a self stack example at All Features\Welcome!\Chart Styles\Standard\Bar\Self Stacked in the new features demo, available at TeeChart's program group.

Anyway, if you need to dynamically create and remove series and have problems with this don't hesitate to let us know.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

jz8
Newbie
Newbie
Posts: 13
Joined: Thu Feb 10, 2005 5:00 am

Post by jz8 » Wed Jul 30, 2008 3:46 pm

I have uploaded a new file -- some progress but not exactly what I want.

- thanks

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Thu Jul 31, 2008 8:29 am

Hello,

I see my original idea doesn't work to achieve what you request. In that case I'd do something like this:

Code: Select all

uses Series;

procedure TForm1.FormCreate(Sender: TObject);
var tmpSeries: THorizBarSeries;
    i: Integer;
begin

  for i:=0 to 5 do
  begin
    tmpSeries:=THorizBarSeries.Create(self);
    Chart1.AddSeries(tmpSeries);
    tmpSeries.MultiBar:=mbStacked;
    tmpSeries.Marks.Style:=smsValue;

    tmpSeries.AddBar(random, 'Winter', clTeeColor);
    tmpSeries.AddBar(random, 'Summer', clTeeColor);
    tmpSeries.Title:='My item #' + IntToStr(i+1);
  end;
end;
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

jz8
Newbie
Newbie
Posts: 13
Joined: Thu Feb 10, 2005 5:00 am

Post by jz8 » Thu Jul 31, 2008 12:02 pm

Thanks: It seems to work with the dynamic number of series.

However one additional problem, the user may change the data requiring a redrawing of the graph. Not knowing exactly what has changed I have to clear the graph and redo. I am testing this in your code with the following Button1Click procedure.

I am trying the following code (and have failed at some other attempts also), but it does not clear the series. For example, the second pass through the data I get 12 series -- not 6.

Any thoughts on how to fix this.

Code: Select all


procedure TForm1.Button1Click(Sender: TObject);
var tmpSeries: THorizBarSeries;
    i, iSrs, iCnt: Integer;
begin
  iCnt := Chart1.SeriesCount;
  if iCnt  > 0 then   //if there are series, then delete
          for iSrs := iCnt-1 to 0 do
              Chart1.Series[iSrs].Delete(iSrs);
  for i:=0 to 5 do
  begin
    tmpSeries:=THorizBarSeries.Create(self);
    Chart1.AddSeries(tmpSeries);
    tmpSeries.MultiBar:=mbStacked;
    tmpSeries.Marks.Style:=smsValue;

    tmpSeries.AddBar(random, 'Winter', clTeeColor);
    tmpSeries.AddBar(random, 'Summer', clTeeColor);
    tmpSeries.Title:='My item #' + IntToStr(i+1);
  end;
  label1.Caption := inttostr(chart1.SeriesCount);
end;

 

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Thu Jul 31, 2008 12:45 pm

Hi jz8,

Yes, there are a couple mistakes in the way you tried to remove series:

1. For loop should use downto keyword instead of to.
2. Using Chart1.Series[iSrs].Delete(iSrs) you are only removing the point indexed iSrs from Chart1.Series[iSrs] series.

So your code should be like this:

Code: Select all

uses Series;

procedure TForm1.FormCreate(Sender: TObject);
begin
  AddSeries;
end;

procedure TForm1.Button1Click(Sender: TObject);
var i, iSrs, iCnt: Integer;
begin
  iCnt := Chart1.SeriesCount;
  if iCnt > 0 then   //if there are series, then delete
    for iSrs := iCnt-1 downto 0 do
      Chart1.RemoveSeries(iSrs);

  AddSeries;

  Label1.Caption := inttostr(chart1.SeriesCount);
end;

procedure TForm1.AddSeries;
var tmpSeries: THorizBarSeries;
    i: Integer;
begin
  for i:=0 to 5 do
  begin
    tmpSeries:=THorizBarSeries.Create(self);
    Chart1.AddSeries(tmpSeries);
    tmpSeries.MultiBar:=mbStacked;
    tmpSeries.Marks.Style:=smsValue;

    tmpSeries.AddBar(random, 'Winter', clTeeColor);
    tmpSeries.AddBar(random, 'Summer', clTeeColor);
    tmpSeries.Title:='My item #' + IntToStr(i+1);
  end;
end;
Or even easier:

Code: Select all

uses Series;

procedure TForm1.FormCreate(Sender: TObject);
begin
  AddSeries;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  AddSeries;
end;

procedure TForm1.AddSeries;
var tmpSeries: THorizBarSeries;
    i: Integer;
begin
  Chart1.RemoveAllSeries;

  for i:=0 to 5 do
  begin
    tmpSeries:=THorizBarSeries.Create(self);
    Chart1.AddSeries(tmpSeries);
    tmpSeries.MultiBar:=mbStacked;
    tmpSeries.Marks.Style:=smsValue;

    tmpSeries.AddBar(random, 'Winter', clTeeColor);
    tmpSeries.AddBar(random, 'Summer', clTeeColor);
    tmpSeries.Title:='My item #' + IntToStr(i+1);
  end;

    Label1.Caption := inttostr(chart1.SeriesCount);
end;
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

jz8
Newbie
Newbie
Posts: 13
Joined: Thu Feb 10, 2005 5:00 am

Post by jz8 » Thu Jul 31, 2008 1:02 pm

super!

that is working. I did not see the RemoveSeries property.

jz8
Newbie
Newbie
Posts: 13
Joined: Thu Feb 10, 2005 5:00 am

Post by jz8 » Thu Jul 31, 2008 1:25 pm

One more thing.

I am using the RemoveAllSeries as suggested. However, when I close the application (in debug mode) I get an access error. Is there something else about RemoveAllSeries I might need to know about?

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Thu Jul 31, 2008 1:34 pm

Hi jz8,

Not that I can think of. Which exact TeeChart version are you using?

Thanks in advance.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

jz8
Newbie
Newbie
Posts: 13
Joined: Thu Feb 10, 2005 5:00 am

Post by jz8 » Thu Jul 31, 2008 1:37 pm

The download file is:

TeeChart7.07Delphi7.exe

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Thu Jul 31, 2008 1:46 pm

Hi jz8,

Thanks for the information.

Thinking a little bit more about that, when using RemoveAllSeries, series are not disposed. Only their ParentChart is set to null and series are removed from the series list but does not free any memory associated with them. FreeAllSeries destroys all series and frees their associated memory.

You may want to try using FreeAllSeries instead.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

jz8
Newbie
Newbie
Posts: 13
Joined: Thu Feb 10, 2005 5:00 am

Post by jz8 » Thu Jul 31, 2008 2:01 pm

yes -- no problem now

Post Reply