Stacked bars

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
Kai
Newbie
Newbie
Posts: 11
Joined: Tue Apr 01, 2014 12:00 am

Stacked bars

Post by Kai » Tue Apr 08, 2014 2:46 pm

We are using the TeeChartStandard2014 package.

I am testing with the MultiBar mbStacked option with this sample code:

Code: Select all

var
  i, j: Integer;
  tmpDates: array of TDateTime;

  setLength(tmpDates, 5);

  for i:=0 to High(tmpDates) do
   if i mod 2 = 0 then
    tmpDates[i]:=IncDay(Today, i*2)
   else
    tmpDates[i]:=IncDay(Today, (i*2)+1);

  for i:=0 to 1 do
  with frmChart.ChartAccounting.AddSeries(TBarSeries) as TBarSeries do
   begin
    MultiBar:=mbStacked;
    MarksOnBar:=False;
    MarksLocation:=mlCenter;
    CustomBarWidth:=40;

    XValues.DateTime:=true;

    for j:=0 to High(tmpDates) do
     begin
      if j mod 2 = 0 then
       begin
        AddXY(tmpDates[j]+i, 25+random*75, 'S' + inttoStr(i) + ' L' + IntToStr(j));
       end
      else
       begin
        AddXY(tmpDates[j], 25+random*75, 'S' + inttoStr(i) + ' L' + IntToStr(j));
      end;
    end;
  end;
The data i want to use for the bars are only at some points identical, so i try to use mbStacked.
But if i have some values which are unique the bars are "up in the air" :wink: .

The result looks like this.
test.jpg
test.jpg (96.51 KiB) Viewed 4386 times
Any Ideas what i am doing wrong?

Yeray
Site Admin
Site Admin
Posts: 9612
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Stacked bars

Post by Yeray » Wed Apr 09, 2014 9:37 am

Hi Kai,

This happens because the stacked is based on the value indexes.
You could loop your series & values and, if the other series don't have any value at a given XValue, add a null point there. Ie:

Code: Select all

  for i:=0 to Chart1.SeriesCount-1 do
    for j:=0 to Chart1[i].Count-1 do
      for t:=0 to Chart1.SeriesCount-1 do
        if i<>t then
        begin
          found:=false;
          for w:=0 to Chart1[t].Count-1 do
            if Chart1[i].XValue[j]=Chart1[t].XValue[w] then
            begin
              found:=true;
              break;
            end;

          if not found then
            Chart1[t].AddNullXY(Chart1[i].XValue[j], 0);
        end;
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Kai
Newbie
Newbie
Posts: 11
Joined: Tue Apr 01, 2014 12:00 am

Re: Stacked bars

Post by Kai » Wed Apr 09, 2014 11:48 am

Hi Yeray,

works perfect. Thank you.

Post Reply