Page 1 of 2
changing the color of some of the labels
Posted: Mon Nov 29, 2010 12:43 pm
by 10553605
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
Re: changing the color of some of the labels
Posted: Mon Nov 29, 2010 4:02 pm
by 10050769
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,
Re: changing the color of some of the labels
Posted: Tue Nov 30, 2010 7:42 am
by 10553605
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
Re: changing the color of some of the labels
Posted: Tue Nov 30, 2010 9:09 am
by 10050769
Hello Nati,
Could you please, try to arrange a simple example project we can run as-is to reproduce the problem here?
Thanks,
Re: changing the color of some of the labels
Posted: Tue Nov 30, 2010 9:34 am
by 10553605
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
Re: changing the color of some of the labels
Posted: Tue Nov 30, 2010 4:45 pm
by 10050769
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,
Re: changing the color of some of the labels
Posted: Wed Dec 01, 2010 9:12 am
by 10553605
hi
thanks, it actually seems to do the trick
Re: changing the color of some of the labels
Posted: Wed Dec 01, 2010 12:42 pm
by 10050769
Hello Nati,
I am glad that now solution works for you
.
Thanks,
Re: changing the color of some of the labels
Posted: Fri Dec 10, 2010 5:13 pm
by yeray
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).
Re: changing the color of some of the labels
Posted: Wed Jan 05, 2011 3:07 pm
by yeray
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].
Re: changing the color of some of the labels
Posted: Fri Jan 07, 2011 11:19 pm
by 10546565
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
Re: changing the color of some of the labels
Posted: Mon Jan 10, 2011 4:54 pm
by yeray
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
Re: changing the color of some of the labels
Posted: Mon Jan 10, 2011 6:00 pm
by 10546565
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
Re: changing the color of some of the labels
Posted: Tue Jan 11, 2011 4:59 pm
by 10546565
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
Re: changing the color of some of the labels
Posted: Wed Jan 12, 2011 8:57 am
by narcis
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;