Search code examples
delphidelphi-2007teechart

Change TChart Size programmatically


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;

Solution

  • 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?.