Cambiar el tamaño de TCart programáticamente

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

Primer gráfico 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
  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;

 procedure formatChart(width, height, xmin, xmax, ymin, ymax: double);
 //actually formatChart is a method anf fChart a member of my class
   with fChart do  
       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 ;

preguntado el 31 de julio de 12 a las 15:07

1 Respuestas

I think there is a name conflict here. You are using with fChart, and properties Height y 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: ¿Es Delphi "con" la palabra clave una mala práctica?.

contestado el 23 de mayo de 17 a las 13:05

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

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

+1. I'd add a second upvote for the final sentence if I could. :-) - Ken White

+1 Boy I wish someone would kill with as it causes failures on so many levels. - Jeroen Wiert Pluimers

@Jeroen It's easy to kill it yourself by rejecting its use. - David Heffernan

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.