Drawing bottom axis label in red

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
TestAlways
Advanced
Posts: 228
Joined: Tue Aug 28, 2007 12:00 am
Location: Oregon, USA

Drawing bottom axis label in red

Post by TestAlways » Mon Apr 28, 2014 11:07 pm

2014.11.140409

In the attached demo, I have code that use to work (I believe Steema support provided similar code) to draw selected labels on the bottom axis in red. This one should draw every other label in red. It doesn't work in the latest version.

How should this be coded?

Ed Dressel
Attachments
Draw Axis Label.zip
(2.2 KiB) Downloaded 582 times

Yeray
Site Admin
Site Admin
Posts: 9612
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Drawing bottom axis label in red

Post by Yeray » Tue Apr 29, 2014 9:43 am

Hi Ed,

Instead of modifying the Canvas Font, you could modify the Bottom Axis LabelsFont in the OnDrawLabel event, making sure you also handle the "else" situation setting the default properties to the LabelsFont. Ie:

Code: Select all

    if (lIdx > -1) and (FDrawRedIndexes.IndexOf(TObject(lIdx)) > -1) then
    begin
      {lChart.Canvas.Font.Color := clRed;
      lChart.Canvas.Font.Style := [fsBold];}
      lChart.Axes.Bottom.LabelsFont.Color:=clRed;
      lChart.Axes.Bottom.LabelsFont.Style:=[fsBold];
    end
    else
    begin
      lChart.Axes.Bottom.LabelsFont.Color:=clBlack;
      lChart.Axes.Bottom.LabelsFont.Style:=[];
    end;
As an alternative, the latest TeeChart versions allow you yo to directly set the LabelsList in your Bottom Axis, instead of having to use the OnDrawLabel event. Ie:

Code: Select all

constructor TForm1.Create(aOwner: TComponent);
var
  lValue: Double;
  I, J: Integer;
  lIdx: Integer;
  aX: Integer;
  lCalcXPos: Integer;
  lMinOffBy: Integer;
  lOffBy: Integer;
  lSrs: TChartSeries;
begin
  inherited;

  Caption := TeeMsg_Version;

  FDrawRedIndexes := TList.Create;

  SEries1.Clear;
  lValue := 1000.0;
  for I := 0 to 19 do
  begin
    Series1.Add(lValue);
    lValue := lValue * 1.03;
    if (I mod 2) = 0 then
      FDrawRedIndexes.Add(TObject(I));
  end;

  //Chart1.BottomAxis.OnDrawLabel := ChartBottomAxisDrawLabel;

  Chart1.Draw;
  with Chart1.Axes.Bottom.Items do
  begin
    Automatic:=false;
    for I := 0 to Count-1 do
    begin
      lSrs := Chart1.Series[0];
      lIdx := -1;
      aX := Item[I].LabelPos;
      lMinOffBy := MaxInt;
      for J := lSrs.Count - 1 downto 0 do
      begin
        lCalcXPos := lSrs.CalcXPos(J);
        lOffBy := abs(aX - lCalcXPos);
        if (lOffBy < lMinOffBy) then
        begin
          lIdx := J;
          lMinOffBy := lOffBy;
        end;
      end;

      if (lIdx > -1) and (FDrawRedIndexes.IndexOf(TObject(lIdx)) > -1) then
      begin
        Item[I].Format.Font.Color := clRed;
        Item[I].Format.Font.Style := [fsBold];
      end;
    end;
  end;
end;
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Post Reply