Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to do a stacked barchart from a dataset using teechart in delphi

I can create a stacked barchart in Delphi using TeeChart. This is using values and series added in a loop. I would prefer to create this chart by just using a query as the datasource and not have to add each bar as a separate series inside a loop. Is there a better way to do this, or another type of chart I should be looking at? The data is a cross section of rock types from a well core sample. The dataset contains one record for each measurement of depth and rock type. It is displayed as a single vertical column of rock types just like a core sample.

+----+
|    | record 1 - depth1, rock type 1
|    |
+----+
|    |
|    |
|    | record 2 - depth2, rock type 2
|    |
+----+
|    | record 3 - depth3, rock type 3
+----+


procedure TForm128.GenerateLithologyChart;
var
  LSeries: TBarSeries;
  i : integer;
  LastBot : double;

  procedure AddRockSeries(depth : double; col : TColor);
  begin
    LSeries := TBarSeries.Create(LithologyChart);
    LithologyChart.AddSeries(LSeries);
    LSeries.AddBar(0, '', clBlue);
    if col=clNone then
      LSeries.AddNullXY(0,depth,'')
    else
      LSeries.AddXY(0,depth,'',col);
    LSeries.Marks.Visible := False;
    LSeries.MultiBar := mbStacked;
    LSeries.CustomBarWidth := 80;
  end;

begin
  LithologyChart.LeftAxis.Inverted := True;
  LithologyChart.Title.Text.Text := 'Well Lithology - data-aware test';
  LithologyChart.SeriesList.Clear;
  AdoQuery1.First;
  i := 0;
  LastBot := 0;
  while not AdoQuery1.Eof do begin
    if abs(AdoQuery1.FieldByName('Strata Top').asFloat-LastBot) > 0.0005 then begin
      // create blank cross section for the missing depth range
      AddRockSeries(AdoQuery1.FieldByName('Strata Top').asFloat-LastBot, clNone);
    end;
    AddRockSeries(AdoQuery1.FieldByName('Strata Bottom').asFloat-AdoQuery1.FieldByName('Strata Top').asFloat, clRed);

    LastBot := AdoQuery1.FieldByName('Strata Bottom').asFloat;
    inc(i);
    //if i = 3 then break;

    AdoQuery1.Next;
  end;
  AdoQuery1.First;
end;
like image 703
user2455609 Avatar asked Oct 22 '22 08:10

user2455609


1 Answers

Try using mbSelfStack MultiBar style for your TBarSeries. Here it is an example:

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.View3D:=false;
  with Chart1.AddSeries(TBarSeries) as TBarSeries do
  begin
    Marks.Visible:=false;
    MultiBar:=mbSelfStack;
    FillSampleValues;
  end;
end;

With this style, the values in a single TBarSeries will be stacked one above the other

like image 199
Yeray Avatar answered Nov 01 '22 09:11

Yeray