1

I am creating a tChart programmatically (Delphi2007, TeeChar 7 free edition). I'd like to set the chart dimension and maybe change the aspect ratio, but I don't get meaningful results changing Width and Height properties. I tried also changing the axis TickLength with no luck. I copied the relevant properties of TChart from a dfm file, not to forget anything meaningful. The aspect of the graph changes only when I edit X and Y max and min values, but this is not enough.

Here is my original chart and the "reformatted" one, as you can see the chart dimension is 400 x 250 for both. Is there a specific property for resizing charts? I want the axis to resize accordingly, is it possible? Thank you for your help

First Chart The same chart resized

Here is the code relevant to 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;
lib
  • 2,918
  • 3
  • 27
  • 53

1 Answers1

6

I think there is a name conflict here. You are using with fChart, and properties Height and Width of fChart. The same names are in your procedure call though, but the fChart width and height is used instead:

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

Rename the names in the procedure call, and it will work as it is supposed to.

Better still, avoid using the with keyword. See: Is Delphi “with” keyword a bad practice?.

Community
  • 1
  • 1
LU RD
  • 34,438
  • 5
  • 88
  • 296
  • Thank you very much, the error was under my nose and I couldn't see it! I thought of being on the safe side using "with" just to save some typing in setting a long list of properties , but now the evilness of "with" has shown to me! – lib Jul 31 '12 at 15:37
  • This has happened to many Delphi programmers I guess. A common practice in procedure/function calls is to start every parameter name with an `A`. This could have saved you here, but as stated in the link, avoid the "with" keyword. – LU RD Jul 31 '12 at 15:43
  • 2
    +1. I'd add a second upvote for the final sentence if I could. :-) – Ken White Jul 31 '12 at 16:46
  • +1 Boy I wish someone would kill `with` as it causes failures on so many levels. – Jeroen Wiert Pluimers Jul 31 '12 at 18:10
  • @Jeroen It's easy to kill it yourself by rejecting its use. – David Heffernan Jul 31 '12 at 22:55
  • @DavidHeffernan I do; wish everyone would to that. And yes I know about the discussion. – Jeroen Wiert Pluimers Aug 01 '12 at 19:51