label overwrites titles
Posted: Mon Jan 09, 2006 1:02 am
Hello,
I have found that if you make the chart label too big, the label can overwrite the title.
I have fixed the problem in chart.pas so that it works in my application. But it only takes into account the left axis, which is the only one I currently use. Look for "dn code added here"
procedure TChartTitle.DrawTitle;
var tmpXPosTitle : Integer;
tmpMargin : Integer;
FontH : Integer;
tmpFrameWidth : Integer;
Procedure DrawTitleLine(AIndex:Integer);
Var St : String;
APos : Integer;
Begin { draw a title text line }
St:=Text[AIndex];
APos:=AIndex*FontH+tmpFrameWidth;
if IOnTop then Inc(APos,ShapeBounds.Top)
else APos:=ShapeBounds.Bottom-FontH-APos;
if Alignment=taRightJustify then
tmpXPosTitle:=ShapeBounds.Right-ParentChart.Canvas.TextWidth(St)-(tmpMargin div 2)
else
if Alignment=taCenter then
tmpXPosTitle:=((ShapeBounds.Left+ShapeBounds.Right) div 2)-(ParentChart.Canvas.TextWidth(St) div 2);
ParentChart.Canvas.TextOut(tmpXPosTitle,APos,St);
end;
Var t : Integer;
tmpMaxWidth : Integer;
tmp : Integer;
tmpFrameVisible : Boolean;
lblHeight: Integer;
{$IFDEF CLR}
tmpR : TRect;
{$ENDIF}
Begin
if Visible and (Text.Count>0) then
begin
{ calculate title shape margin }
tmpFrameVisible:=Frame.Visible and (Frame.Color<>clTeeColor);
if tmpFrameVisible then tmpFrameWidth:=Frame.Width
else tmpFrameWidth:=0;
if Bevel<>bvNone then tmpFrameWidth:=BevelWidth;
{ apply title margins }
if not FCustomPosition then
begin
ShapeBounds:=ParentChart.ChartRect;
if IOnTop then ShapeBounds.Top:=ShapeBounds.Top+tmpFrameWidth;
end;
{ prepare title font }
With ParentChart.Canvas do
begin
AssignFont(Self.Font);
TextAlign:=TA_LEFT;
FontH:=FontHeight;
end;
{ autosize title height on number of text lines }
if IOnTop or FCustomPosition then
ShapeBounds.Bottom:=ShapeBounds.Top+Text.Count*FontH
else
ShapeBounds.Top :=ShapeBounds.Bottom-Text.Count*FontH;
{ apply margins to bottom and right sides }
if not FCustomPosition then
InflateRect(ShapeBounds,tmpFrameWidth,tmpFrameWidth);
tmpMargin:=ParentChart.Canvas.TextWidth('W');
{ resize Title to maximum Chart width }
if AdjustFrame then
begin
tmpMaxWidth:=0;
for t:=0 to Text.Count-1 do
Begin
tmp:=ParentChart.Canvas.TextWidth(Text[t]);
if tmp>tmpMaxWidth then tmpMaxWidth:=tmp;
end;
Inc(tmpMaxWidth,tmpMargin+tmpFrameWidth);
With ShapeBounds do
Case Alignment of
taLeftJustify : Right:=Left +tmpMaxWidth;
taRightJustify : Left :=Right-tmpMaxWidth;
taCenter : begin
if FCustomPosition then Right:=Left+tmpMaxWidth;
tmp:=(Left+Right) div 2;
Left :=tmp-(tmpMaxWidth div 2);
Right:=tmp+(tmpMaxWidth div 2);
end;
end;
end;
{ draw title shape }
Draw;
if Alignment=taLeftJustify then tmpXPosTitle:=ShapeBounds.Left+(tmpMargin div 2);
{ draw all Title text lines }
ParentChart.Canvas.BackMode:=cbmTransparent;
for t:=0 to Text.Count-1 do DrawTitleLine(t);
{ calculate Chart positions after drawing the titles / footers }
if not FCustomPosition then
begin
tmp:=TeeTitleFootDistance+tmpFrameWidth;
if not Transparent then
Inc(tmp,Shadow.VertSize);
// dn code added here
parentChart.canvas.assignFont((parentChart as TChart).leftAxis.LabelsFont);
lblHeight:=ParentChart.canvas.fontHeight;
if (lblHeight div 2) > tmp then
tmp:=(lblHeight div 2)+TeeTitleFootDistance;
// end of dn added code
{$IFDEF CLR}
tmpR:=ParentChart.ChartRect;
if IOnTop then
tmpR.Top:=ShapeBounds.Bottom+tmp
else
tmpR.Bottom:=ShapeBounds.Bottom-tmp-Text.Count*FontH;
ParentChart.ChartRect:=tmpR;
{$ELSE}
if IOnTop then
ParentChart.ChartRect.Top:=ShapeBounds.Bottom+tmp
else
ParentChart.ChartRect.Bottom:=ShapeBounds.Bottom-tmp-Text.Count*FontH;
{$ENDIF}
ParentChart.RecalcWidthHeight;
end;
end;
end;
I have found that if you make the chart label too big, the label can overwrite the title.
I have fixed the problem in chart.pas so that it works in my application. But it only takes into account the left axis, which is the only one I currently use. Look for "dn code added here"
procedure TChartTitle.DrawTitle;
var tmpXPosTitle : Integer;
tmpMargin : Integer;
FontH : Integer;
tmpFrameWidth : Integer;
Procedure DrawTitleLine(AIndex:Integer);
Var St : String;
APos : Integer;
Begin { draw a title text line }
St:=Text[AIndex];
APos:=AIndex*FontH+tmpFrameWidth;
if IOnTop then Inc(APos,ShapeBounds.Top)
else APos:=ShapeBounds.Bottom-FontH-APos;
if Alignment=taRightJustify then
tmpXPosTitle:=ShapeBounds.Right-ParentChart.Canvas.TextWidth(St)-(tmpMargin div 2)
else
if Alignment=taCenter then
tmpXPosTitle:=((ShapeBounds.Left+ShapeBounds.Right) div 2)-(ParentChart.Canvas.TextWidth(St) div 2);
ParentChart.Canvas.TextOut(tmpXPosTitle,APos,St);
end;
Var t : Integer;
tmpMaxWidth : Integer;
tmp : Integer;
tmpFrameVisible : Boolean;
lblHeight: Integer;
{$IFDEF CLR}
tmpR : TRect;
{$ENDIF}
Begin
if Visible and (Text.Count>0) then
begin
{ calculate title shape margin }
tmpFrameVisible:=Frame.Visible and (Frame.Color<>clTeeColor);
if tmpFrameVisible then tmpFrameWidth:=Frame.Width
else tmpFrameWidth:=0;
if Bevel<>bvNone then tmpFrameWidth:=BevelWidth;
{ apply title margins }
if not FCustomPosition then
begin
ShapeBounds:=ParentChart.ChartRect;
if IOnTop then ShapeBounds.Top:=ShapeBounds.Top+tmpFrameWidth;
end;
{ prepare title font }
With ParentChart.Canvas do
begin
AssignFont(Self.Font);
TextAlign:=TA_LEFT;
FontH:=FontHeight;
end;
{ autosize title height on number of text lines }
if IOnTop or FCustomPosition then
ShapeBounds.Bottom:=ShapeBounds.Top+Text.Count*FontH
else
ShapeBounds.Top :=ShapeBounds.Bottom-Text.Count*FontH;
{ apply margins to bottom and right sides }
if not FCustomPosition then
InflateRect(ShapeBounds,tmpFrameWidth,tmpFrameWidth);
tmpMargin:=ParentChart.Canvas.TextWidth('W');
{ resize Title to maximum Chart width }
if AdjustFrame then
begin
tmpMaxWidth:=0;
for t:=0 to Text.Count-1 do
Begin
tmp:=ParentChart.Canvas.TextWidth(Text[t]);
if tmp>tmpMaxWidth then tmpMaxWidth:=tmp;
end;
Inc(tmpMaxWidth,tmpMargin+tmpFrameWidth);
With ShapeBounds do
Case Alignment of
taLeftJustify : Right:=Left +tmpMaxWidth;
taRightJustify : Left :=Right-tmpMaxWidth;
taCenter : begin
if FCustomPosition then Right:=Left+tmpMaxWidth;
tmp:=(Left+Right) div 2;
Left :=tmp-(tmpMaxWidth div 2);
Right:=tmp+(tmpMaxWidth div 2);
end;
end;
end;
{ draw title shape }
Draw;
if Alignment=taLeftJustify then tmpXPosTitle:=ShapeBounds.Left+(tmpMargin div 2);
{ draw all Title text lines }
ParentChart.Canvas.BackMode:=cbmTransparent;
for t:=0 to Text.Count-1 do DrawTitleLine(t);
{ calculate Chart positions after drawing the titles / footers }
if not FCustomPosition then
begin
tmp:=TeeTitleFootDistance+tmpFrameWidth;
if not Transparent then
Inc(tmp,Shadow.VertSize);
// dn code added here
parentChart.canvas.assignFont((parentChart as TChart).leftAxis.LabelsFont);
lblHeight:=ParentChart.canvas.fontHeight;
if (lblHeight div 2) > tmp then
tmp:=(lblHeight div 2)+TeeTitleFootDistance;
// end of dn added code
{$IFDEF CLR}
tmpR:=ParentChart.ChartRect;
if IOnTop then
tmpR.Top:=ShapeBounds.Bottom+tmp
else
tmpR.Bottom:=ShapeBounds.Bottom-tmp-Text.Count*FontH;
ParentChart.ChartRect:=tmpR;
{$ELSE}
if IOnTop then
ParentChart.ChartRect.Top:=ShapeBounds.Bottom+tmp
else
ParentChart.ChartRect.Bottom:=ShapeBounds.Bottom-tmp-Text.Count*FontH;
{$ENDIF}
ParentChart.RecalcWidthHeight;
end;
end;
end;