Page 1 of 1

Drawing bottom axis label in red

Posted: Mon Apr 28, 2014 11:07 pm
by 10546565
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

Re: Drawing bottom axis label in red

Posted: Tue Apr 29, 2014 9:43 am
by yeray
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;