Recent

Author Topic: How to align 45 degree bar chart labels to end at stick mark?  (Read 11065 times)

eclbnhan

  • New Member
  • *
  • Posts: 13
How to align 45 degree bar chart labels to end at stick mark?
« on: February 21, 2017, 08:50:38 pm »
I would like to have my bar chart labels in 45 degree and the end of the label to be aligned at the stick mark. How could I achieve this?

I can do it with Excel chart by default. However, I could not do it with TAChart. I tried many options with TAChart but still cannot make it:

        oChart.BottomAxis.Marks.LabelFont.Orientation := 450  ;
        oChart.BottomAxis.Marks.Style:= smsLabel;
        oChart.BottomAxis.Marks.Alignment := taRightJustify ; //tried taLeftJustify, taCenter
        oChart.BottomAxis.Marks.Source := oChartSource;

I tried taRightJustify, taLeftJustify, and taCenter but it seems not making any different.

FPMarkLabels45Degree.png is how the labels are showed with my TAChart. I really want it to be like ExcelMarkLabels45Degree.png.

Please help! Thank you!


donnie

  • Jr. Member
  • **
  • Posts: 72
Re: How to align 45 degree bar chart labels to end at stick mark?
« Reply #1 on: February 22, 2017, 12:02:41 pm »
You can try the setadditionalangle property. This property takes the number in radius
For example at the example below I use it to show it vertical.
Code: Pascal  [Select][+][-]
  1. AChart.BottomAxis.Marks.SetAdditionalAngle(1.57);  //3.14/2  (it is the π of the circle div with 2)
In your case you need
Code: Pascal  [Select][+][-]
  1. AChart.BottomAxis.Marks.SetAdditionalAngle(0.3925);
It is 360 /45 =8. And after that π/8 which is 3.14/8 = 0.3925
But as it seems it doesn't work. It does the same
« Last Edit: February 22, 2017, 01:31:26 pm by donnie »

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: How to align 45 degree bar chart labels to end at stick mark?
« Reply #2 on: February 22, 2017, 12:30:17 pm »
Does this really work?

Marks.SetAdditionalAngle adds an additional rotation angle to the marks labels already rotated by the font's Orientation. But the problem is that TAChart has the labels centered and rotates around the label center. Excel, however, seems to have the rotation center at the end of the string.

I've been seeking through the sources, but so far could not yet find a way to solve it. Please be patient.

eclbnhan

  • New Member
  • *
  • Posts: 13
Re: How to align 45 degree bar chart labels to end at stick mark?
« Reply #3 on: February 22, 2017, 06:11:43 pm »
You can try the setadditionalangle property. This property takes the number in radius
[...]
But as it seems it doesn't work. It does the same

My label is set at 45 degree:
  Chart1.BottomAxis.Marks.LabelFont.Orientation := 450  ;
Now if I add additional another 45 degree:
  Chart1.BottomAxis.Marks.SetAdditionalAngle(3.14/4) ;
My label is perfectly right at 90 degree, therefore, the end of the label is perfectly right at the stick mark. But it is not what I want  :(
You seem to have a good sense of humor  ::)

eclbnhan

  • New Member
  • *
  • Posts: 13
Re: How to align 45 degree bar chart labels to end at stick mark?
« Reply #4 on: February 22, 2017, 11:15:46 pm »
I think OnGetShape could do something about the Mark and the Frame (of the label). I tried: 
Code: Pascal  [Select][+][-]
  1. Chart1.BottomAxis.Marks.OnGetShape := @OnGetShapeEvent ;
  2.  
I change the color and the border of the Frame so that I can visually see it:
Code: Pascal  [Select][+][-]
  1. Chart1.BottomAxis.Marks.Frame.Style := psSolid ;
  2. Chart1.BottomAxis.Marks.Frame.Color := clRed ;
  3.  

I can move the Frame (of the label). However, I still don't know how to move the label along with the Frame. I don't know a generic formula to move/align the Frame as expected neither, but I know it can be moved.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.OnGetShapeEvent(ASender: TChartTextElement;
  2.   const ABoundingBox: TRect; var APolygon: TPointArray);
  3. begin
  4.  if length(APolygon) = 4 then begin
  5.     APolygon[0].x := APolygon[0].x + APolygon[0].x ;
  6.     APolygon[1].x := APolygon[1].x + APolygon[1].x ;
  7.     APolygon[2].x := 0 ;
  8.     APolygon[3].x := 0 ;
  9.  
  10.     //APolygon[0].y := 0 ;
  11.     //APolygon[1].y := APolygon[1].y + APolygon[1].y ;
  12.     //APolygon[2].y := APolygon[2].y + APolygon[2].y ;
  13.     //APolygon[3].y := 0 ;
  14.  end;
  15.  

BeforeOnGetShapeEvent.png shows the Frame and the Label before the event. OnGetShapeEvent.png shows the Frame that has been moved by the event.

I guess TChartTextElement should have more of something to be manipulated or Marks should have something to offset the original position of the label/frame.
 

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: How to align 45 degree bar chart labels to end at stick mark?
« Reply #5 on: February 23, 2017, 12:00:03 am »
Yes, OnGetShape might be a workaround, but it it would be tough to calculate all the offsets correctly.

In the meantime, I managed to change the center of rotation; there will be a new property, prelimarily called RotationAnchor = (traCenter, traLeft, traRight) which allows to rotate the label around the center (current setting), text start or text end. Selecting traLeft or traRight (depending on the rotation angle) gets close to your desired effect.

The reason why I don't commit this modification at the moment is that the centered rotation is still active somewhere which moves the texts to different distances from the axis.

If you want to play with the current version I am adding the modfied files as an attachment ("rotation-anchor.zip"). Make a backup copy of the originals before unzipping to the tachart directory.

The attachment "vertical_bars - rotated labels.zip" is a simple demo. Drag the scroll bar to change the text angle.

eclbnhan

  • New Member
  • *
  • Posts: 13
Re: How to align 45 degree bar chart labels to end at stick mark?
« Reply #6 on: February 23, 2017, 01:11:42 am »
Very nice work! I like the RotationAnchor. I tried traLeft, traRight, and traCenter. They line up as expected. Distance seems still working right with traCenter, but not with traLeft, traRight.

donnie

  • Jr. Member
  • **
  • Posts: 72
Re: How to align 45 degree bar chart labels to end at stick mark?
« Reply #7 on: February 23, 2017, 09:37:35 am »
sorry for the mistaken post. I saw the topic and as I had a problem before With LabelFont.Orientation I changed it to SetAdditionalAngle and worked fine. I removed labels font at all as the results wasn't the desirable.
 the
Quote
But as it seems it doesn't work. It does the same
was added after my post in order not to confuse you.sorry again

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: How to align 45 degree bar chart labels to end at stick mark?
« Reply #8 on: February 23, 2017, 11:35:28 am »
Distance seems still working right with traCenter, but not with traLeft, traRight.
That's what I was trying to explain. Please have a look at the attached version with updated demo, it should work correctly now. Please test your application with it. If ok I'll commit the modified units.

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: How to align 45 degree bar chart labels to end at stick mark?
« Reply #9 on: February 23, 2017, 07:29:25 pm »
Sorry, there were still a few bugs with marks of series. Also, I found it to be inconventient that the labels can rotate into the chart. Therefore, I added another anchor type which switches between left and right anchor such that the axis labels always keep outside the chart area. And I renamed the RotationAnchor to RotationCenter - I think this is easier to understand.

Code: Pascal  [Select][+][-]
  1. type
  2.   TChartTextRotationCenter = (rcCenter, rcEdge, rcLeft, rcRight),

The attached demo shows all usages of labels as axis labels for all axis positions, as well as series labels. Please note that in case of series labels the "outside" or "inside" direction is not always well-defined, therefore the setting raEdge is ignored, it behaves like rcLeft. It could be handled but I decided to wait until users request this rarely-used feature for series labels.
« Last Edit: February 23, 2017, 07:31:07 pm by wp »

eclbnhan

  • New Member
  • *
  • Posts: 13
Re: How to align 45 degree bar chart labels to end at stick mark?
« Reply #10 on: February 23, 2017, 08:50:20 pm »
Never mind, donnie! I was just clarifying so we all could follow along and I was joking too since we are members and like a team  :)

Please have a look at the attached version [...]
I think I need your new TAChartStrConsts as well since I got compilation errors in TAChartAxis. These identifiers are not found: @rsLeft, @rsTop, @rsRight, @rsBottom, rsHidden, rsInverted.

In the mean time. I replaced those with equivalent strings and I really love your new features. Awesome!
The "Distance" at 90 degree is perfectly right to me, but at different angle the Distance for the long label is shifted further a little bit. But I think it seems like the font problem.

Also, I found it to be inconventient that the labels can rotate into the chart.
Sorry I haven't tried the latest version from your post since Identifier DotProduct is not found and I don't know the safe way to bypass it. However, too me the labels that can rotate inside the chart when choosing rcLeft or rcRight should be considered OK. It is up to users to choose the good angle and distance since "Distance" from the beginning(left) or the end(right) of the label should be maintained correctly as well.

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: How to align 45 degree bar chart labels to end at stick mark?
« Reply #11 on: February 23, 2017, 09:02:33 pm »
The "Distance" at 90 degree is perfectly right to me, but at different angle the Distance for the long label is shifted further a little bit. But I think it seems like the font problem.
The text bounding box always has some empty space below the text (or was it above the text?). This makes it difficult to correctly find the center of the text height with respect to the characters.

since Identifier DotProduct is not found and I don't know the safe way to bypass it.
The posted units are based on the trunk version of Lazarus, and there was a lot of recent changes with TAChart. This is the implementation of DotProduct (it is in TAChartUtils):
Code: Pascal  [Select][+][-]
  1. function DotProduct(A, B: TDoublePoint): Double;
  2. begin
  3.   Result := A.X * B.X + A.Y * B.Y;
  4. end;

However, too me the labels that can rotate inside the chart when choosing rcLeft or rcRight should be considered OK. It is up to users to choose the good angle and distance since "Distance" from the beginning(left) or the end(right) of the label should be maintained correctly as well.
True. But rcLeft and rcRight have a little issue because the bounding box of the rotated originally centered text is still active somewhere, and therefore the distance to the axis title increases if the labels rotate into the chart. Just run the demo and select left or right (unfortunately I forgot to add axis titles but they would be at their usual positions at the border). Probably I won't fix this because nobody would rotate the marks into the chart.

eclbnhan

  • New Member
  • *
  • Posts: 13
Re: How to align 45 degree bar chart labels to end at stick mark?
« Reply #12 on: February 23, 2017, 09:39:13 pm »
Oh I see. Thank you! I will try the latest version of TAChart with your new feature.

The text bounding box always has some empty space below the text (or was it above the text?). This makes it difficult to correctly find the center of the text height with respect to the characters.
There should be no empty space below or above the text. TestBarChartLabelRotation.zip from my previous post is my test. The problem was there before your Rotation feature. It seems that the problem is from the effect of the font when it is displayed in an angle. I am happy with your Rotation feature.

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: How to align 45 degree bar chart labels to end at stick mark?
« Reply #13 on: February 23, 2017, 11:27:56 pm »
Committed, r54258. Since it is a new feature it will not be backported to Lazarus 1.6.4. It will be in Lazarus 1.8, though.

The property RotationCenter now is published only for the axis and series marks. Chart title and footer, and axis titles inherit it from their ancestor, but as I saw in some tests it causes some trouble, because you may rotate them out of the chart area. Therefore, the property is not accessible in these cases which probably are not of any practical use anyway.

The "official" documentation is here: http://wiki.lazarus.freepascal.org/TAChart_documentation#Rotation_of_marks

eclbnhan

  • New Member
  • *
  • Posts: 13
Re: How to align 45 degree bar chart labels to end at stick mark?
« Reply #14 on: February 24, 2017, 01:47:25 am »
It is a very nice feature to have. We love it. Thank you very much!  :)

 

TinyPortal © 2005-2018