Page 1 of 1

How to align two charts

Posted: Thu Mar 01, 2007 2:56 pm
by 9349385
Hello!

On my form I have 2 TCharts. I need to display the two charts directly beneath each other so that the two left axes are perfectly aligned and the ChartWidth of the two charts is exactely the same.

Is this possible?

Regards,
Erik

Posted: Thu Mar 01, 2007 3:09 pm
by narcis
Hi Erik,

Yes, this is possible. You can try doing something as told here.

Posted: Thu Mar 01, 2007 3:25 pm
by 9349385
Hi!

I did try that but unfortunately it will not work because the legends of the two charts has different sizes. How can I compensate for this?

Regards,
Erik

Posted: Fri Mar 02, 2007 11:18 am
by yeray
In the following example, we made two series in two different charts, we set different legend widthes and aligned them.

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
begin
  Series1.FillSampleValues(100);
  Series2.FillSampleValues(200);

  //Creating diferent size titles for each legend
  Chart1.Legend.Title.Text.Clear;
  Chart1.Legend.Title.Text.Add('Chart1 Title xxxxxxxxxxxx');

  Chart2.Legend.Title.Text.Clear;
  Chart2.Legend.Title.Text.Add('Chart2 Title');

  LeftAlignCharts([Chart1,Chart2],100,600);

  Chart1.Draw;
  Chart2.Draw;
  RightAlignCharts
end;

//this is the method to resize the charts that Narcis suggested to you before
procedure TForm1.LeftAlignCharts(Const Charts: Array of TCustomChart; aLeft: Integer; aWidth: Integer);
var i: Integer;
begin

  for i := Low(Charts) to High(Charts) do
  // Left align charts
  With Charts[i] do
  begin
    Left := aLeft;
    Width := aWidth;
    MarginLeft := 0;
    Axes.Left.TitleSize := 15;
    Axes.Left.LabelsSize := 30;
    Axes.Left.LabelsAlign := alDefault;
  end;
end;

//This is to resize the chart which has a smaller legend.width
procedure TForm1.RightAlignCharts;
begin
     if Chart1.Legend.Width > Chart2.Legend.Width then
     begin
          Chart1.Legend.HorizMargin:=10;
          Chart2.Legend.HorizMargin := Chart1.Legend.Width - chart2.Legend.Width + Chart1.Legend.HorizMargin;
     end
     else
         begin
              Chart2.Legend.HorizMargin:=10;
              Chart1.Legend.HorizMargin := Chart2.Legend.Width - chart1.Legend.Width + Chart2.Legend.HorizMargin;
         end;
end;
I hope it helps.