TeeChart X Axis Label Formatting

2019-09-18 01:06发布

问题:

I’ve got my TeeChart looking pretty good, the grid lines / ticks appear where I wish and so forth (the X Axis LabelStyle is set to talAuto). Now I need to format the X Axis labels so they display a time offset rather than the data point number.

I’ve changed the LabelStyle to talMark and am handling the chart’s OnGetAxisLabel. The problem is that my event handler is called for every X Axis point rather than just the points which displayed labels when I had LabelStyle set to talAuto.

How can I set things up so my event handler is only called for the labels which were drawn when LabelStyle was talAuto?

I’m using C++ Builder XE3 and the version of TeeChart included with IDE.

UPDATE inspired by Yeray's comment:

With LabelStyle set to talAuto I always get ValueIndex of -1 passed to my event handler. In looking at the LabelText, I see values that I would have expected in ValueIndex, but I also get an equal number of incorrect values (ignoring non-X Axis values). For example...

My chart displays 5 X-Axis values: 200, 400, 600, 800, 1000. My event handler is called 10 times, in this order:

ValueIndex      LabelText
    -1              937.5
    -1                0
    -1          240,000
    -1              200
    -1          239,800
    -1            1,000
    -1              800
    -1              600
    -1              400
    -1              200

I have no idea where the first 5 values came from or why my event handler was called.

NOTE: I removed the C++ Builder tag because this question might be answered by anyone using TeeChart regardless of their language; the fact that I'm using BCB is not important to the question.

回答1:

I’ve changed the LabelStyle to talMark and am handling the chart’s OnGetAxisLabel. The problem is that my event handler is called for every X Axis point rather than just the points which displayed labels when I had LabelStyle set to talAuto.

Right, using the talMark LabelStyle, the axis will draw as many labels as points in the series. You can use the talAuto LabelStyle to get the number of labels you wish and you can still format the labels in the OnGetAxisLabel event.


UPDATE:

When I use talAuto, ValueIndex is always -1 so I have no idea which data point is associated with the event.

When talAuto is set and the series has labels, it behaves like talText and talMark: these strings are used in the bottom axis and the ValueIndex in OnGetAxisLabel event can be used. When talAuto is set but the series has no label, it behaves like talValue: the bottom axis calculates the labels to show in function of the Minimum, Maximum and Increment. The ValueIndex in OnGetAxisLabel event can't be used because the axis labels don't correspond to a series point.

I guess you don't have labels in your series. Otherwise, changing from talAuto to talMark shouldn't change anything.

So you have to decide between one or the other. Maybe you can use talValue (or talAuto without labels) to get the number of labels you wish, and extract the info you need from the string that is going to be drawn.


Alternatively, it could be easier to use CustomLabels. They will allow you to control both the positions and the text of the axis labels without needing any event. In example:

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
begin
  Chart1.View3D:=false;

  Chart1.AddSeries(TBarSeries).FillSampleValues;

  Chart1.Axes.Bottom.Items.Clear;
  for i:=0 to Chart1[0].Count-1 do
    Chart1.Axes.Bottom.Items.Add(Chart1[0].XValue[i], 'label ' + IntToStr(i));
end;