2012-07-31 188 views
1

我正在以編程方式創建一個tChart(Delphi2007,TeeChar 7免費版)。我想設置圖表維度,也許更改寬高比,但我沒有得到有意義的結果更改寬度和高度屬性。我試着改變軸TickLength沒有運氣。我從dfm文件複製了TChart的相關屬​​性,不要忘記任何有意義的東西。僅當我編輯X和Y最大值和最小值時,圖形的方面纔會改變,但這還不夠。以編程方式更改TChart大小

這是我的原始圖表和「重新格式化」的圖表,因爲您可以看到兩者的圖表尺寸均爲400 x 250。有調整圖表大小的特定屬性嗎?我希望軸相應地調整大小,這可能嗎?感謝您的幫助

First Chart The same chart resized

下面是有關TChart代碼:

procedure CreateChart(parentform: TForm); 
//actually formatChart is a CreateChart anf fChart a member of my class 
begin 
    fchart:= TChart.Create(parentform); 
    fchart.Parent:= parentform; 
    fchart.AxisVisible := true; 
    fchart.AutoSize := false; 
    fChart.color := clWhite; 
    fchart.BottomAxis.Automatic := true; 
    fchart.BottomAxis.AutomaticMaximum := true; 
    fchart.BottomAxis.AutomaticMinimum := true; 
    fchart.LeftAxis.Automatic := true; 
    fchart.LeftAxis.AutomaticMaximum := true; 
    fchart.LeftAxis.AutomaticMinimum := true; 
    fchart.view3D := false; 
end 

procedure formatChart(width, height, xmin, xmax, ymin, ymax: double); 
//actually formatChart is a method anf fChart a member of my class 
begin 
    with fChart do 
    begin 
     Color := clWhite; 
     fChart.Legend.Visible := false; 
     AxisVisible := true; 
     AllowPanning := pmNone; 
     color := clWhite; 
     Title.Visible := False; 
     BottomAxis.Minimum := 0; //to avoid the error maximum must be > than min 
     BottomAxis.Maximum := xmax; 
     BottomAxis.Minimum := xmin; 
     BottomAxis.ExactDateTime := False ; 
     BottomAxis.Grid.Visible := False ; 
     BottomAxis.Increment := 5 ; 
     BottomAxis.MinorTickCount := 0; 
     BottomAxis.MinorTickLength := 5; 
     BottomAxis.Ticks.Color := clBlack ; 
     BottomAxis.TickOnLabelsOnly := False; 
     DepthAxis.Visible := False; 
     LeftAxis.Automatic := false; 
     LeftAxis.AutomaticMaximum := false; 
     LeftAxis.AutomaticMinimum := false; 
     LeftAxis.Minimum := ymin; 
     LeftAxis.Maximum := ymax; 
     LeftAxis.Minimum := ymin; 
     LeftAxis.TickLength := 5; 
     Width := round(width); 
     Height := round(height); 
     View3D := False ; 
    end; 
end; 

回答

6

我認爲這裏有一個名稱衝突。您正在使用with fChart,以及fChart的屬性HeightWidth。同樣的名稱在你的程序調用雖然,但是fChart寬度和高度來代替:

Width := Round(width); // The fChart property Width is used on both sides. 
Height := Round(height); // The fChart property Height is used on both sides. 

重命名的名稱在過程調用,並且它應該會工作。

更好的是,避免使用with關鍵字。參見:Is Delphi 「with」 keyword a bad practice?

+0

非常感謝你,錯誤是在我的鼻子下面,我看不到它!我認爲在安全方面使用「with」只是爲了節省一些打字的時間來設置一大堆屬性,但是現在,「with」的邪惡已經顯示給我! – lib 2012-07-31 15:37:38

+0

我想這已經發生在很多Delphi程序員身上。過程/函數調用中的一種常見做法是使用「A」開始每個參數名稱。這可能會將您保存在此處,但正如鏈接中所述,請避免使用「with」關鍵字。 – 2012-07-31 15:43:05

+2

+1。如果可能的話,我會爲最後一句增加第二個贊成票。 :-) – 2012-07-31 16:46:43