Page 1 of 1

TeeCreateBitmap and axis labels location

Posted: Wed Jun 29, 2005 1:47 pm
by 5890529
I'm using TeeChart5.02 VCL.
When I save the chart to bitmap using TeeCreateBitmap, The left axis title overwrites its labels. I tried settings LabelsSize to zero by code, but it didn't help.
Remarks:
* The graph is not visible to the user. It is created, edited using code, and then saved to bmp file.
* I saved the chart to *.tee file and there it looked ok.

Can you help?

Posted: Wed Jun 29, 2005 2:39 pm
by narcis
Hi Mercury,

It works fine here using the same version as you and the code below.

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
var
  tmpBmp: TBitMap;
  tmpAreaSeries:TAreaSeries;
  tmpChart: TChart;
  i: Integer;
begin
  tmpChart:=TChart.Create(self);

  tmpAreaSeries:=TAreaSeries.Create(self);
  With tmpAreaSeries do
  begin
    ParentChart:=tmpChart;
    Randomize;
    for i:=0 to 100 do tmpAreaSeries.Add(random(100000000));
  end;

  tmpChart.LeftAxis.Title.Caption:='MyTitle';

  tmpBmp:=tmpChart.TeeCreateBitmap(clWhite,Rect(0,0,tmpChart.Width,tmpChart.Height));
  try
    tmpBmp.SaveToFile('C:\Temp\chart1.bmp');
  finally
    tmpBmp.Free;
  end;
end;
Could you please send us an example we can run "as-is" or modify the code so that we can reproduce the problem here? You can post your files at [url]news://www.steema.net/steema.public.attachments[/url] newsgroup.
I tried settings LabelsSize to zero by code, but it didn't help.
You should increase LabelsSize not setting it to zero, you could try with quite a big value.

To reproduce the problem...

Posted: Thu Jun 30, 2005 12:42 pm
by 5890529
To reproduce the problem...
tmpChart.LeftAxis.Maximum := 5;
tmpChart.LeftAxis.Increment := 0.5;

To reproduce the problem...

Posted: Thu Jun 30, 2005 12:47 pm
by 5890529
...And add values between 0 and 5.

Posted: Thu Jun 30, 2005 2:54 pm
by narcis
Hi Mercury,

I've been able to reproduce the problem here. I have been able to overcome it using LabelsSize so the working code would be:

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
var
  tmpBmp: TBitMap;
  tmpAreaSeries:TAreaSeries;
  tmpChart: TChart;
  i: Integer;
begin
  tmpChart:=TChart.Create(self);

  tmpAreaSeries:=TAreaSeries.Create(self);
  With tmpAreaSeries do
  begin
    ParentChart:=tmpChart;
    Randomize;
    for i:=0 to 100 do tmpAreaSeries.Add(random(5));
  end;

  With tmpChart.LeftAxis do
  begin
    Title.Caption:='MyTitle';
    Maximum := 5;
    Increment := 0.5;
    LabelsSize:=20;
  end;

  tmpBmp:=tmpChart.TeeCreateBitmap(clWhite,Rect(0,0,tmpChart.Width,tmpChart.Height));
  try
    tmpBmp.SaveToFile('C:\Temp\chart1.bmp');
  finally
    tmpBmp.Free;
  end;
end;

Posted: Wed Jul 06, 2005 6:00 pm
by 5890529
We don't want to use LabelsSize other than zero because then we need to calculate the exact size of the text that will be displayed on the axis in order to avoid overwriting it and do it everytime the graph will be resized.
Anyway I found one way that works by using powers of ten:
//-------------------------------------------------------------
procedure FillNiceNumbersArray;
const
MIN_NICE_NUMBER = 0.001;
MAX_NICE_NUMBER = 1000000000;
var
i: Integer;
nVal: Double;
begin
//fill an array with 0.001, 0.01, 0.1, 1, 10, 100, 1000, ...
G_NiceNumbersArray := nil;
nVal := MIN_NICE_NUMBER;
repeat
SetLength(G_NiceNumbersArray, Length(G_NiceNumbersArray)+1);
G_NiceNumbersArray[Length(G_NiceNumbersArray)-1] := nVal;
nVal := nVal * 10;
until nVal > MAX_NICE_NUMBER;
end;
//-------------------------------------------------------------
function GetNiceMaxAxisVal(nVal: Double): Double;
var
i :integer;
begin
Result := nVal;
try
for i := Low(G_NiceNumbersArray) to High(G_NiceNumbersArray) do
begin
Result := G_NiceNumbersArray;
if (nVal < G_NiceNumbersArray) then
BREAK;
end;
except
end;
end;
//-------------------------------------------------------------
...
m_GraphChart.LeftAxis.Automatic := False;
m_GraphChart.LeftAxis.AutomaticMinimum := False;
m_GraphChart.LeftAxis.AutomaticMaximum := False;
m_GraphChart.LeftAxis.Minimum := 0;
if bUserWantedASpecificMaxYaxisValue then
begin
m_GraphChart.LeftAxis.Maximum := WantedYaxisValue;
m_GraphChart.LeftAxis.Increment := GetScale(WantedYaxisValue) / 10;
end
else
begin
fMax := 0;
for j := 0 to (m_GraphChart.SeriesCount - 1) do
begin
fMax := Max(fMax, m_GraphChart.Series[j].YValues.MaxValue);
end;
if fMax > 0 then
begin
m_GraphChart.LeftAxis.Maximum := GetNiceMaxAxisVal(fMax);
m_GraphChart.LeftAxis.Increment := m_GraphChart.LeftAxis.Maximum / 10;
end;
end;
...
//-------------------------------------------------------------

Posted: Wed Jul 06, 2005 6:38 pm
by 5890529
One mistake on the code...
GetScale(WantedYaxisValue) / 10;
should be replaced with
GetNiceMaxAxisVal(WantedYaxisValue) / 10;