-2

I want to add an empty gantt serie to a TGanttSeries. How to do that ?

I want that serie draw on the chart even is empty.

I tried to put Serie.AddGanttColor(0, 0, i, SerieName[i], clBlue); but it print a bar on 30/12/1899...

Here a picture of what I made : TCHART

What I need to make is to also drawing the Series1 (Task #1) on the left axis of the TChart. (Here the Series Series1 don't contain any points to draw)

1 Answers1

0

You can hide the "empty" series by setting its "Pointer.Visible" property to "false". This will still include that series in the legend. If you want the series to be blank on the chart (the label still drawn along the left-axis), you will need to add at least one value to it and you'll surely want to choose a value that helps preserve the chart's readability. In my example, I chose to use the smallest date from the non-empty series and if all the series are "empty" simply use the current DateTime ("Now").

Also, you want to connect the chart's "GetLegendText" event so you can provide only the name of each series instead of some combination of the series name and its data. There does not appear to be a useful setting in the legend's properties. You could extend this and return only the series' name when empty and some more meaningful name/data combination when not.

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, VclTee.TeeGDIPlus, VCLTee.TeEngine, Vcl.ExtCtrls, VCLTee.TeeProcs, VCLTee.Chart,
  VCLTee.Series, VCLTee.GanttCh, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Chart1: TChart;
    Series1: TGanttSeries;
    Button1: TButton;
    Series2: TGanttSeries;
    Series3: TGanttSeries;
    procedure FormCreate(Sender: TObject);
    procedure Chart1GetLegendText(Sender: TCustomAxisPanel; LegendStyle: TLegendStyle; Index: Integer; var LegendText: string);

  private
    Series : array[0..2] of TGanttSeries;
    SeriesName : array[0..2] of string;
    SeriesStart : array[0..2] of TDateTime;
    SeriesEnd : array[0..3] of TDateTime;
    SeriesColor : array[0..2] of TColor;

   procedure DrawChart;

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);

var
   i : integer;

begin
   Series[0] := Series1;
   Series[1] := Series2;
   Series[2] := Series3;

   SeriesName[0] := 'Task #1';
   SeriesName[1] := 'Task #2';
   SeriesName[2] := 'Task #3';

   SeriesStart[0] := 0;        SeriesEnd[0] := 0;
   SeriesStart[1] := Now;      SeriesEnd[1] := Now+1;
   SeriesStart[2] := Now+0.5;  SeriesEnd[2] := Now+3;

   SeriesColor[0] := clBlue;
   SeriesColor[1] := clGreen;
   SeriesColor[2] := clRed;

   for i := 0 to 2 do
      begin
         Series[i].ColorEachPoint := false;
         Series[i].SeriesColor := SeriesColor[i];
      end;


   DrawChart;
end;

procedure TForm1.DrawChart;

var
   EmptyValue : TDateTime;
   i : integer;

begin
   EmptyValue := 0;
   for i := 0 to 2 do
      begin
         if (SeriesStart[i] <> 0) and
              ( (EmptyValue = 0) or (EmptyValue > SeriesStart[i]) ) then
            EmptyValue := SeriesStart[i];
      end;
   if EmptyValue = 0 then
      EmptyValue := Now;

   for i := 0 to 2 do
      begin
         Series[i].Clear;
         if SeriesStart[i] = 0 then
            begin
               Series[i].Pointer.Visible := false;
               Series[i].AddGanttColor(EmptyValue,EmptyValue, i, SeriesName[i], SeriesColor[i])
            end
         else
            begin
               Series[i].Pointer.Visible := true;
               Series[i].AddGanttColor(SeriesStart[i],SeriesEnd[i],i,SeriesName[i], SeriesColor[i])
            end;
      end;
end;

procedure TForm1.Chart1GetLegendText(Sender: TCustomAxisPanel; LegendStyle: TLegendStyle; Index: Integer; var LegendText: string);

begin
   LegendText := SeriesName[Index];
end;

end.
Dave Olson
  • 1,435
  • 1
  • 9
  • 16
  • Thank you for your answer, and yes I understand what you mean, sorry... With your answer, only the series : Series2 and Series3 drawn on the TChart. But my question is : How to print also the Series1 on the vertical Tchart axis even if there is no points on the TTGantSeries. The main goal is to display always the same chart even if we do not have points on a serie – Nizar Ouardiane Jun 20 '17 at 06:27
  • I've enhanced the code and changed the answer. Hopefully, this is closer to what you're trying to achieve. – Dave Olson Jun 20 '17 at 13:18