changing the color of some of the labels

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
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 » Wed Jan 12, 2011 3:12 pm

That assumes that Text is an index value. My application allows the users to select one for four different display types here (age, date, year, index), so this does not work.

Ed Dressel

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 3:32 pm

Hi Ed,

I'm not sure about what you are trying to achieve here then. Could you please give us more detailed information or attach a simple example project we can run "as-is" so that we can try to search for a solution to your problem?

Thanks in advance.
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

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 » Wed Jan 12, 2011 3:35 pm

The following code works, but isn't very elegant:

Code: Select all

procedure TfrmDBCalcRetirementYears.ChartBottomAxisDrawLabel(Sender:TChartAxis;
  var aX,aY,aZ:Integer; var aText:String; var aDrawLabel:Boolean);
var
  I: Integer;
  lCalcXPos: Integer;
  lChart: TChart;
  lIdx: Integer;
  lMinOffBy: Integer;
  lOffBy: Integer;
  lSrs: TChartSeries;
begin
  Assert(Sender.ParentChart is TChart);
  lChart := TChart(Sender.ParentChart);

  if SameText(lChart.BottomAxis.Title.Caption, aText) then
    exit;

  if lChart.SeriesCount > 0 then
  begin
    lSrs := lChart.Series[0];
    lIdx := -1;
    lMinOffBy := MaxInt;
    for I := lSrs.Count - 1 downto 0 do
    begin
      lCalcXPos := lSrs.CalcXPos(I);
      lOffBy := abs(aX - lCalcXPos);
      if (lOffBy < lMinOffBy) then
      begin
        lIdx := I;
        lMinOffBy := lOffBy;
      end;
    end;

    if (lIdx > -1) and (FRedLblIndexes.IndexOf(lIdx) > -1) then
      lChart.Canvas.Font.Color := clRed;
  end;
end;

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 Jan 03, 2012 3:02 pm

Regarding this issue, I have the following code that does exactly what I need:

Chart1.Axes.Bottom.Items.Clear; //deleting current labels
LastVisibleDate := 0;
i := 0;
while i < Chart1[0].Count do
begin
if i mod 15 <> 0 then
begin
inc(i);
continue;
end;
while (LastVisibleDate = Trunc(Chart1[0].XValue)) and (i < Chart1[0].Count) do
inc(i);
if <indicationTrue> then
myColor := clBlue
else
myColor := clBlack;
Chart1.Axes.Bottom.Items.Add(Chart1[0].XValue, Chart1[0].Labels).Font.Color := myColor;
LastVisibleDate := Trunc(Chart1[0].XValue);
inc(i);
end;

In short, I'm deleting the current bottom axis, scan its values and check against a certain criteria in order to select the relevant color. In general it worked great but suddenly I got the following problem you can see in the attached image: http://imageshack.us/photo/my-images/835/graphpk.jpg/

As you can see, there is a group of dates on the left side of the graph and then a lonely date on the right side of it. Without the above code, the dates are drawn nice and clean and are "spreaded" equally along the bottom axis. I noticed that the number of items of the bottom axis is different than chart1[0].Count value which represents ALL the values and not only the original ones that were drawn.

So my question is: how can I scan ONLY the dates that were originally drawn and check ONLY them against my condition in order to set the color accordingly. I'm using TeeChart 8.05 and I didn't find the mentioned event: ChartBottomAxisDrawLabel , also I remember trying other events in the past and none gave me the required result as the above code did. Not sure what happened suddenly so I guess it is related to something that applies only in certain scenarios.

Thanks

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 Jan 03, 2012 3:32 pm

I also tried scanning the Chart1.Axes.Bottom.Items.count and set the value accordingly but it seems not to do the trick, the color remains black
please advice

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 03, 2012 3:48 pm

Try the aChart.BottomAxis.OnDrawLabel event. My code for the event is below:

Code: Select all

procedure TfrmDBCalcRetirementYears.ChartBottomAxisDrawLabel(Sender:TChartAxis;
  var aX,aY,aZ:Integer; var aText:String; var aDrawLabel:Boolean);
var
  I: Integer;
  lCalcXPos: Integer;
  lChart: TChart;
  lIdx: Integer;
  lMinOffBy: Integer;
  lOffBy: Integer;
  lSrs: TChartSeries;
begin
  Assert(Sender.ParentChart is TChart);
  lChart := TChart(Sender.ParentChart);

  if SameText(lChart.BottomAxis.Title.Caption, aText) then
    exit;

  if lChart.SeriesCount > 0 then
  begin
    lSrs := lChart.Series[0];
    lIdx := -1;
    lMinOffBy := MaxInt;
    for I := lSrs.Count - 1 downto 0 do
    begin
      lCalcXPos := lSrs.CalcXPos(I);
      lOffBy := abs(aX - lCalcXPos);
      if (lOffBy < lMinOffBy) then
      begin
        lIdx := I;
        lMinOffBy := lOffBy;
      end;
    end;

    if (lIdx > -1) and (FRedLblIndexes.IndexOf(lIdx) > -1) then
      lChart.Canvas.Font.Color := clRed;
  end;
end;

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 Jan 04, 2012 8:17 am

Thanks, apparently the event cannot be accessed through the object inspector but I need to access it manually.

How can I know the value of the label (x axis)? the value is a date and I need to check it against a certain criteria, the aText is not enough, how can I know the value behind it?

Thanks!

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 Jan 04, 2012 8:39 am

I tried using your loop in order to know the value by using the lSrs.XValue[lIdx] but while the text shows 01/12/11, the value given to me from the lIdx is 13/12/11, so while 13/12/11 should be colored blue (and it is according to the code), what I see in the graph is that 01/12/11 is blue though this date should be black.

Please advice
Thanks

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 » Wed Jan 04, 2012 2:23 pm

Start simple.... Did you try creating a demo with my code?

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 Jan 04, 2012 3:44 pm

I started simple and all labels were colored as expected, then I wanted to distinguish those to be colored and for that I wanted to retrieve the date value of the label to be colored. I ran your loop exactly and got the lIdx, when I checked the value of that lIdx I saw 13/12/11 so it colored it blue (as expected) but when i saw the graph itself (and then the aText parameter) I realized that the value didn't match the aText, hence the label to be colored

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 Jan 04, 2012 4:36 pm

I think the problem is simple though I don't know how I can solve it.

While aText represents the label to be drawn, it has no direct correlation to the XValues of the series. This is the reason why the label says 01/12/11 but the correspondent XValue is 13/12/11.

In short, I need to know the date value of the label to be drawn and I'm not sure how can I do that given the fact that aText shows the date and day. Ofcourse I can rely on the fact that aText is in a fixed format and parse the relevant date out of it but I was wondering if there is a nicer way to do that

Thanks

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 » Wed Jan 04, 2012 4:58 pm

In short, I need to know the date value of the label to be drawn and I'm not sure how can I do that given the fact that aText shows the date and day. Ofcourse I can rely on the fact that aText is in a fixed format and parse the relevant date out of it but I was wondering if there is a nicer way to do that
Or a different way--keep a TStringList of labels with the associated object being an integer of the date. Use the lIdx property as I do in the example. Should work fine.

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 » Sun Jan 08, 2012 8:22 am

How can I know the relevant date per label?
How can I know which labels are visible?

Post Reply