2017-06-12 69 views
-2

我想爲TGanttSeries添加一個空甘特圖系列。怎麼做 ?將一個空甘特圖添加到系列

我希望圖表上的繪圖甚至是空的。

我試着把Serie.AddGanttColor(0,0,i,SerieName [i],clBlue);但它打印30/12/1899酒吧...

這裏是我做了一個畫面:TCHART

我需要提出的是,在左側還繪製系列1(1#任務) TChart的軸。 (這裏Series Series1不包含任何繪製點)

+0

30/12/1899是數字零的日期解釋。 – Dsm

+0

這樣的範圍沒有意義。你爲什麼要創建它?對於用戶來說,它是無用的。 – Victoria

+0

我有6個différent「系列」,即使它們是空的,我也想將它們全部顯示出來... –

回答

0

您可以通過將其「Pointer.Visible」屬性設置爲「false」來隱藏「空」系列。這仍將包含該圖例中的該系列。如果您希望圖表中的這個系列爲空白(標籤仍然沿着左軸繪製),您至少需要添加一個值,並且您一定要選擇一個有助於保持圖表可讀性的值。在我的示例中,我選擇使用非空系列中的最小日期,如果所有系列都是「空」,則只需使用當前的DateTime(「Now」)即可。

另外,您希望連接圖表的「GetLegendText」事件,以便您只能提供每個系列的名稱,而不是系列名稱及其數據的某種組合。在圖例的屬性中似乎沒有有用的設置。你可以擴展它,只返回系列的名字,如果沒有,則返回一些更有意義的名字/數據組合。

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. 
+0

謝謝你的回答,是的,我明白你的意思,對不起......用你的答案,只有系列:TChart上繪製的Series2和Series3。但是我的問題是:即使TTGantSeries上沒有點,如何在垂直的Tchart軸上也打印Series1。主要目標是始終顯示相同的圖表,即使我們沒有關於系列的點數 –

+0

我已增強了代碼並更改了答案。希望這更接近你想要實現的目標。 –