Page 1 of 1
Stacked Bar Chart with Dynamic number of groups
Posted: Wed Jul 30, 2008 11:13 am
by 9235955
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 --
Posted: Wed Jul 30, 2008 11:20 am
by narcis
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:
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.
Posted: Wed Jul 30, 2008 11:47 am
by 9235955
I may not really understand selfstack -- but I sent the example as suggested.
thanks.
Posted: Wed Jul 30, 2008 11:52 am
by narcis
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.
Posted: Wed Jul 30, 2008 3:46 pm
by 9235955
I have uploaded a new file -- some progress but not exactly what I want.
- thanks
Posted: Thu Jul 31, 2008 8:29 am
by narcis
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;
Posted: Thu Jul 31, 2008 12:02 pm
by 9235955
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;
Posted: Thu Jul 31, 2008 12:45 pm
by narcis
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;
Posted: Thu Jul 31, 2008 1:02 pm
by 9235955
super!
that is working. I did not see the RemoveSeries property.
Posted: Thu Jul 31, 2008 1:25 pm
by 9235955
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?
Posted: Thu Jul 31, 2008 1:34 pm
by narcis
Hi jz8,
Not that I can think of. Which exact TeeChart version are you using?
Thanks in advance.
Posted: Thu Jul 31, 2008 1:37 pm
by 9235955
The download file is:
TeeChart7.07Delphi7.exe
Posted: Thu Jul 31, 2008 1:46 pm
by narcis
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.
Posted: Thu Jul 31, 2008 2:01 pm
by 9235955
yes -- no problem now