Recent

Author Topic: Axis Alignment problem  (Read 3557 times)

kapibara

  • Hero Member
  • *****
  • Posts: 610
Axis Alignment problem
« on: October 04, 2018, 07:57:51 pm »
It seems that modifying any properties of Chart.LeftAxis at runtime gives a runtime error if Chart.LeftAxis.Alignment is changed from default calLeft to something else.

Error:
Code: Pascal  [Select][+][-]
  1. Chart.LeftAxis.Alignment := calRight;
  2. Chart.LeftAxis.TickColor:=clGray;

But this works:
Code: Pascal  [Select][+][-]
  1. Chart.LeftAxis.Alignment := calRight;
  2. Chart.AxisList[0].TickColor:=clGray;

It doesn't matter if you change the axis alignment in object inspector or by code at runtime.

But it works to change all Axis properties at designtime in object inspector.

BottomAxis may have the same issue.
« Last Edit: October 04, 2018, 09:25:43 pm by kapibara »
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

wp

  • Hero Member
  • *****
  • Posts: 11832
Re: Axis Alignment problem
« Reply #1 on: October 04, 2018, 10:46:53 pm »
Hmm. I think this is more of a feature than a bug.

The getter of the property "LeftAxis" is

Code: Pascal  [Select][+][-]
  1. function TChart.GetAxisByAlign(AAlign: TChartAxisAlignment): TChartAxis;
  2. begin
  3.   if (BidiMode <> bdLeftToRight) then
  4.     case AAlign of
  5.       calLeft: AAlign := calRight;
  6.       calRight: AAlign := calLeft;
  7.     end;
  8.   Result := FAxisList.GetAxisByAlign(AAlign);
  9. end;

For LeftAxis, AAlign is calLeft, but of course an axis with this alignment does not exist any more after executing the first line of your code which gave it the Alignment calRight. Therefore, the result of this function is nil. In other words: you made the LeftAxis a "RightAxis" and there is no "LeftAxis" any more. Therefore,  "Chart.LeftAxis.TickColor := clGray" must crash. Of course, the error message could be more descriptive, but this is a general problem of all error messages when properties of a "nil" object are accessed.

Your second code snippet, on the other hand, is correct because the former LeftAxis still exists in the axislist at its old index 0.

When you change the Alignment in the Object Tree above the Object Inspector you'll notice that the property LeftAxis in the Object Inspector will go away.

Only when you change the Alignment in the property LeftAxis of the Object Inspector the LeftAxis remains. Probably this is intentional because if it would disappear at this moment the user would be very confused. But when you select another control in the form designer and then switch back to the chart the LeftAxis will be gone, too.
« Last Edit: October 04, 2018, 10:52:39 pm by wp »

kapibara

  • Hero Member
  • *****
  • Posts: 610
Re: Axis Alignment problem
« Reply #2 on: October 05, 2018, 12:00:55 am »
Thanks for the explanation, wp.

It's not difficult to work around this. However I think its the first time I know about that a property disappears and becomes invalid for the reason that the variable name is no longer suitable. :D  :D

Maybe if LeftAxis was called VerticalAxis and BottomAxis called HorizontalAxis the Alignment would not matter in relation to the chosen name of the variable.
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

wp

  • Hero Member
  • *****
  • Posts: 11832
Re: Axis Alignment problem
« Reply #3 on: October 05, 2018, 12:09:15 am »
However I think its the first time I know about that a property disappears and becomes invalid for the reason that the variable name is no longer suitable. :D  :D
Yes, I've never seen this either.

Maybe if LeftAxis was called VerticalAxis and BottomAxis called HorizontalAxis the Alignment would not matter in relation to the chosen name of the variable.
But there can be several vertical and horizontal axes... And for reasons of backward compatibility it is almost impossible to rename a well-established property.

 

TinyPortal © 2005-2018