mark regions in (Gantt) Chart

1.1k views Asked by At

[TeeChart 2015.14.150120]
I've a Gantt-Chart so the x axis is date based. I would need to mark time periods in the chart. So i.e. mark 2009 -2011 describing it with a certain description, 2011-2013 with another name ...

I've tried to use TColorBandTool but it had some drawbacks:

  1. I wasn't able anymore to click the Chart-Entries (even if the tool was marked as "behind"
  2. I could not show a description of the period.

So I've tried to use TColorLineTool which worked better (almost perfect) but:

  1. the text panels for differnt period where shown at the same (vertical) position, so they overlapped some times.
  2. When the last TextPanel was longer then the remaining part of the chart, it "dropped" out, the Chart was not redimensioned in order to show the Panel within the Diagram.

So, now I had another Idea: to use different series to build one line over the whole width of the chart, each series for one period to show. But I would have to show the description of these series in extra Legends (TExtraLegendTool) in oder to have space enough for the texts. But I couldn't get the TExtraLegendTool shown. I assume there is a bug in that version of TeeChart since also the demo put by the installer doesn't show that tool.
Now I'm rather at a loss how to go on. Anybody has an idea?

1

There are 1 answers

0
Yeray On

I've given a try at the first approach:

I've tried to use TColorBandTool but it had some drawbacks:

  1. I wasn't able anymore to click the Chart-Entries (even if the tool was marked as "behind"
  2. I could not show a description of the period.

And I think it works fine for me here. This is how it looks and below the code I used. Note I disabled the TColorBandTool's AllowDrag, ResizeStart and ResizeEnd properties.

Gantt with ColorBands and Annotations

var gantt1: TGanttSeries;
    greenBand, blueBand: TColorBandTool;
    greenAnnot, blueAnnot: TAnnotationTool;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.Title.Visible:=false;
  Chart1.View3D:=false;
  Chart1.Legend.Alignment:=laBottom;

  gantt1:=Chart1.AddSeries(TGanttSeries) as TGanttSeries;
  gantt1.FillSampleValues(10);

  greenBand:= Chart1.Tools.Add(TColorBandTool) as TColorBandTool;
  with greenBand do
  begin
    Color:=clGreen;
    Transparency:=50;
    Axis:=Chart1.Axes.Bottom;
    StartValue:=gantt1.StartValues[1];
    EndValue:=gantt1.EndValues[2];
    AllowDrag:=false;
    ResizeStart:=false;
    ResizeEnd:=false;
  end;

  blueBand:= Chart1.Tools.Add(TColorBandTool) as TColorBandTool;
  with blueBand do
  begin
    Color:=clBlue;
    Transparency:=50;
    Axis:=Chart1.Axes.Bottom;
    StartValue:=gantt1.StartValues[9];
    EndValue:=gantt1.EndValues[8];
    AllowDrag:=false;
    ResizeStart:=false;
    ResizeEnd:=false;
  end;

  Chart1.MarginTop:=10;

  Chart1.Draw;

  greenAnnot:=Chart1.Tools.Add(TAnnotationTool) as TAnnotationTool;
  with greenAnnot do
  begin
    Shape.Transparent:=true;
    Shape.Font.Color:=clGreen;
    Text:='Green annotation';
    Top:=Chart1.ChartRect.Top-15;
    Left:=Chart1.Axes.Bottom.CalcPosValue(greenBand.StartValue+(greenBand.EndValue-greenBand.StartValue)/2) -
          (Chart1.Canvas.TextWidth(Text) div 2);
  end;

  blueAnnot:=Chart1.Tools.Add(TAnnotationTool) as TAnnotationTool;
  with blueAnnot do
  begin
    Shape.Transparent:=true;
    Shape.Font.Color:=clBlue;
    Text:='Blue annotation';
    Top:=Chart1.ChartRect.Top-15;
    Left:=Chart1.Axes.Bottom.CalcPosValue(blueBand.StartValue+(blueBand.EndValue-blueBand.StartValue)/2) -
          (Chart1.Canvas.TextWidth(Text) div 2);
  end;
end;