changing the color of some of the labels

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Nati
Newbie
Newbie
Posts: 37
Joined: Mon Jun 29, 2009 12:00 am

changing the color of some of the labels

Post by Nati » Mon Nov 29, 2010 12:43 pm

hi

the bottom axis contains dates. for each date i want to check if it answers a certain condition
if so, it should be blue, otherwise black.

i did the following just to check:

procedure Tfrm.Chart1GetAxisLabel(Sender: TChartAxis;
Series: TChartSeries; ValueIndex: Integer; var LabelText: String);
begin
inherited;
if not Sender.IsDateTime then exit;
if valueindex mod 2 = 0 then
sender.LabelsFont.Color := clblue
else
sender.LabelsFont.Color := clblack;
end;

but the event is not even triggered
how can i accomplish what i wrote above?
thanks

Sandra
Site Admin
Site Admin
Posts: 3132
Joined: Fri Nov 07, 2008 12:00 am

Re: changing the color of some of the labels

Post by Sandra » Mon Nov 29, 2010 4:02 pm

Hello Nati,

If you want change color of labels font alternately, you can not use LabelFont so it property change color of all labels. I recommend use custom labels as do in next example:

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
    dt: TDateTime;
    myColor: TColor;
begin
  Chart1.AddSeries(TLineSeries.Create(Self));

  for i:=0 to 10 do
  begin
    dt:=Now + i;
    Chart1[0].AddXY(dt, random, DateToStr(dt));
  end;

  Chart1.Axes.Bottom.Items.Clear;

  for i:=0 to Chart1[0].Count-1 do
  begin
    if i mod 2 <> 0 then
      myColor:=clRed
    else
      myColor:=clBlack;

    Chart1.Axes.Bottom.Items.Add(Chart1[0].XValue[i], Chart1[0].Labels[i]).Format.Font.Color:=myColor;
  end;

  Chart1.Axes.Bottom.LabelsAngle:=90;
end;
Could you please, tell us if previous code works as you want?

I hope will helps.

Thanks,
Best Regards,
Sandra Pazos / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Nati
Newbie
Newbie
Posts: 37
Joined: Mon Jun 29, 2009 12:00 am

Re: changing the color of some of the labels

Post by Nati » Tue Nov 30, 2010 7:42 am

hi

this is not exactly what i need.
the data is coming from a cds so at first i fill the data inside the cds and the graph is updated accordingly.
for each date value (x/bottom axis) i need to check if it answers a certain criteria and paint it accordingly.

thanks

Sandra
Site Admin
Site Admin
Posts: 3132
Joined: Fri Nov 07, 2008 12:00 am

Re: changing the color of some of the labels

Post by Sandra » Tue Nov 30, 2010 9:09 am

Hello Nati,

Could you please, try to arrange a simple example project we can run as-is to reproduce the problem here?

Thanks,
Best Regards,
Sandra Pazos / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Nati
Newbie
Newbie
Posts: 37
Joined: Mon Jun 29, 2009 12:00 am

Re: changing the color of some of the labels

Post by Nati » Tue Nov 30, 2010 9:34 am

actually there is no problem :) just a question

im using TDBChart which values are coming from a cds
and the question is: how can i control the color of each label in the bottom axis

Sandra
Site Admin
Site Admin
Posts: 3132
Joined: Fri Nov 07, 2008 12:00 am

Re: changing the color of some of the labels

Post by Sandra » Tue Nov 30, 2010 4:45 pm

Hello Nati,

I 'm afraid that the only way you have for controlling labels colors is using custom labels as I do in the example above.

Thanks,
Best Regards,
Sandra Pazos / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Nati
Newbie
Newbie
Posts: 37
Joined: Mon Jun 29, 2009 12:00 am

Re: changing the color of some of the labels

Post by Nati » Wed Dec 01, 2010 9:12 am

hi

thanks, it actually seems to do the trick

Sandra
Site Admin
Site Admin
Posts: 3132
Joined: Fri Nov 07, 2008 12:00 am

Re: changing the color of some of the labels

Post by Sandra » Wed Dec 01, 2010 12:42 pm

Hello Nati,

I am glad that now solution works for you :).

Thanks,
Best Regards,
Sandra Pazos / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

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

Re: changing the color of some of the labels

Post by Yeray » Fri Dec 10, 2010 5:13 pm

Hi,

However, I think that the OnGetAxisLabel event should allow you to change the labels font, and it did in v8.07.
I've added it to the defect list be investigated for future releases (TV52015315).
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

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

Re: changing the color of some of the labels

Post by Yeray » Wed Jan 05, 2011 3:07 pm

Hello,

Investigating this, it claimed to us that it is already possible with events too. You only have to use the axis OnDrawLabel event instead of the chart's OnGetAxisLabel.
Here it is an example of the usage:

Code: Select all

  private
    { Private declarations }
    procedure OnBottomAxisDawLabel(Sender:TChartAxis; var X,Y,Z:Integer; var Text:String; var DrawLabel:Boolean);
//...
uses Series;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.AddSeries(TLineSeries);
  Chart1[0].FillSampleValues(10);

  Chart1.Axes.Bottom.OnDrawLabel:=OnBottomAxisDawLabel;
end;

procedure TForm1.OnBottomAxisDawLabel(Sender:TChartAxis; var X,Y,Z:Integer; var Text:String; var DrawLabel:Boolean);
begin
  If Round(StrToFloat(Text)) mod 2 = 0 Then
    Chart1.Canvas.Font.Color:=clRed
  else
    Chart1.Canvas.Font.Color:=clGreen;
end;
So we've closed the ticket with number [TV52015315].
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

TestAlways
Advanced
Posts: 228
Joined: Tue Aug 28, 2007 12:00 am
Location: Oregon, USA

Re: changing the color of some of the labels

Post by TestAlways » Fri Jan 07, 2011 11:19 pm

Is there any way to get the X index from the X variable (which appears to be the pixels)? The text assigned to the "Text" variable can vary.

Thank you,

Ed Dressel

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

Re: changing the color of some of the labels

Post by Yeray » Mon Jan 10, 2011 4:54 pm

Hi Ed,

I'm afraid you have to loop into your series values looking for an ValueIndex that matches with the x pixel you have
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

TestAlways
Advanced
Posts: 228
Joined: Tue Aug 28, 2007 12:00 am
Location: Oregon, USA

Re: changing the color of some of the labels

Post by TestAlways » Mon Jan 10, 2011 6:00 pm

I tried the following LOC but X never equals lSrs.CalcXPos(I) (FRedLblIndexes is a TList of integer and contain the index of values that should be drawn in red):

Code: Select all

procedure TfrmDBCalcRetirementYears.ChartBottomAxisDrawLabel(Sender:TChartAxis; var X,Y,Z:Integer; var Text:String; var DrawLabel:Boolean);
var
  I: Integer;
  lChart: TChart;
  lIdx: Integer;
  lSrs: TChartSeries;
begin
  Assert(Sender.ParentChart is TChart);
  lChart := TChart(Sender.ParentChart);
  if lChart.SeriesCount > 0 then
  begin
    lSrs := lChart.Series[0];
    lIdx := -1;
    for I := 0 to  lSrs.Count - 1 do
      if lSrs.CalcXPos(I) = X then
      begin
        lIdx := I;
        break;
      end;
    if (lIdx > -1) and (FRedLblIndexes.IndexOf(X) > -1) then
      lChart.Canvas.Font.Color := clRed;
  end;
end;
What should I do differently?

Ed Dressel

TestAlways
Advanced
Posts: 228
Joined: Tue Aug 28, 2007 12:00 am
Location: Oregon, USA

Re: changing the color of some of the labels

Post by TestAlways » Tue Jan 11, 2011 4:59 pm

Attached is a demo that reproduces the problem. It appears that with a bar series, CalcXPos gives the left side of the bar and the X parameter is the center of the bar. (In the attached demo, the caption shows the X, Y position of the mouse when it is over the TChart, along with the CalcXPos and X values for Series[0]).

I am pressed to get this working.

Ed Dressel
Attachments
Draw Axis Label Position.zip
(3.01 KiB) Downloaded 807 times

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: changing the color of some of the labels

Post by Narcís » Wed Jan 12, 2011 8:57 am

Hi Ed,

You can calculate bar's center position from CalcBarBounds method implementing ChartBottomAxisDrawLabel as shown below. You'll see values in both lists coinciding now.

Code: Select all

procedure TForm1.ChartBottomAxisDrawLabel(Sender: TChartAxis; var X, Y,
  Z: Integer; var Text: String; var DrawLabel: Boolean);
var
  I     : Integer;
  lValue: Integer;
  rect  : TRect;
  XPos  : Integer;
begin
  lValue := StrToIntDef(Text, -1);
  if lValue < 0 then
  begin
    lbxXValues.Items.Clear;
    lbxCalcXPos.Items.Clear;
    for I := Series1.Count - 1 downto 0 do
    begin
      rect := Series1.CalcBarBounds(I);
      XPos := rect.Left + ((rect.Right - rect.Left) div 2);
      lbxCalcXPos.Items.Add(Format('X Pos for %d: %d', [I, XPos]));
    end;
  end
  else
    lbxXValues.Items.Add(Text + ': ' + IntToStr(X));
end;
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Post Reply