66import android .graphics .Paint ;
77import android .graphics .Paint .Align ;
88import android .graphics .Path ;
9+ import android .graphics .PointF ;
10+ import android .util .Size ;
911
1012import com .github .mikephil .charting .components .LimitLine ;
1113import com .github .mikephil .charting .components .XAxis ;
1214import com .github .mikephil .charting .components .XAxis .XAxisPosition ;
15+ import com .github .mikephil .charting .utils .FSize ;
1316import com .github .mikephil .charting .utils .Transformer ;
1417import com .github .mikephil .charting .utils .Utils ;
1518import com .github .mikephil .charting .utils .ViewPortHandler ;
@@ -35,17 +38,30 @@ public void computeAxis(float xValAverageLength, List<String> xValues) {
3538 mAxisLabelPaint .setTypeface (mXAxis .getTypeface ());
3639 mAxisLabelPaint .setTextSize (mXAxis .getTextSize ());
3740
38- StringBuffer a = new StringBuffer ();
41+ StringBuilder widthText = new StringBuilder ();
3942
40- int max = ( int ) Math .round (xValAverageLength
43+ int max = Math .round (xValAverageLength
4144 + mXAxis .getSpaceBetweenLabels ());
4245
4346 for (int i = 0 ; i < max ; i ++) {
44- a .append ('h' );
47+ widthText .append ('h' );
4548 }
4649
47- mXAxis .mLabelWidth = Utils .calcTextWidth (mAxisLabelPaint , a .toString ());
48- mXAxis .mLabelHeight = Utils .calcTextHeight (mAxisLabelPaint , "Q" );
50+ final FSize labelSize = Utils .calcTextSize (mAxisLabelPaint , widthText .toString ());
51+
52+ final float labelWidth = labelSize .width ;
53+ final float labelHeight = Utils .calcTextHeight (mAxisLabelPaint , "Q" );
54+
55+ final FSize labelRotatedSize = Utils .getSizeOfRotatedRectangleByDegrees (
56+ labelWidth ,
57+ labelHeight ,
58+ mXAxis .getLabelRotationAngle ());
59+
60+ mXAxis .mLabelWidth = Math .round (labelWidth );
61+ mXAxis .mLabelHeight = Math .round (labelHeight );
62+ mXAxis .mLabelRotatedWidth = Math .round (labelRotatedSize .width );
63+ mXAxis .mLabelRotatedHeight = Math .round (labelRotatedSize .height );
64+
4965 mXAxis .setValues (xValues );
5066 }
5167
@@ -55,32 +71,38 @@ public void renderAxisLabels(Canvas c) {
5571 if (!mXAxis .isEnabled () || !mXAxis .isDrawLabelsEnabled ())
5672 return ;
5773
58- float yoffset = Utils . convertDpToPixel ( 4f );
74+ float yoffset = mXAxis . getYOffset ( );
5975
6076 mAxisLabelPaint .setTypeface (mXAxis .getTypeface ());
6177 mAxisLabelPaint .setTextSize (mXAxis .getTextSize ());
6278 mAxisLabelPaint .setColor (mXAxis .getTextColor ());
6379
6480 if (mXAxis .getPosition () == XAxisPosition .TOP ) {
6581
66- drawLabels (c , mViewPortHandler .offsetTop () - yoffset );
82+ drawLabels (c , mViewPortHandler .contentTop () - yoffset ,
83+ new PointF (0.5f , 1.0f ));
6784
68- } else if (mXAxis .getPosition () == XAxisPosition .BOTTOM ) {
85+ } else if (mXAxis .getPosition () == XAxisPosition .TOP_INSIDE ) {
6986
70- drawLabels (c , mViewPortHandler .contentBottom () + mXAxis .mLabelHeight + yoffset * 1.5f );
87+ drawLabels (c , mViewPortHandler .contentTop () + yoffset + mXAxis .mLabelRotatedHeight ,
88+ new PointF (0.5f , 1.0f ));
7189
72- } else if (mXAxis .getPosition () == XAxisPosition .BOTTOM_INSIDE ) {
90+ } else if (mXAxis .getPosition () == XAxisPosition .BOTTOM ) {
7391
74- drawLabels (c , mViewPortHandler .contentBottom () - yoffset );
92+ drawLabels (c , mViewPortHandler .contentBottom () + yoffset ,
93+ new PointF (0.5f , 0.0f ));
7594
76- } else if (mXAxis .getPosition () == XAxisPosition .TOP_INSIDE ) {
95+ } else if (mXAxis .getPosition () == XAxisPosition .BOTTOM_INSIDE ) {
7796
78- drawLabels (c , mViewPortHandler .offsetTop () + yoffset + mXAxis .mLabelHeight );
97+ drawLabels (c , mViewPortHandler .contentBottom () - yoffset - mXAxis .mLabelRotatedHeight ,
98+ new PointF (0.5f , 0.0f ));
7999
80100 } else { // BOTH SIDED
81101
82- drawLabels (c , mViewPortHandler .offsetTop () - yoffset );
83- drawLabels (c , mViewPortHandler .contentBottom () + mXAxis .mLabelHeight + yoffset * 1.6f );
102+ drawLabels (c , mViewPortHandler .contentTop () - yoffset ,
103+ new PointF (0.5f , 1.0f ));
104+ drawLabels (c , mViewPortHandler .contentBottom () + yoffset ,
105+ new PointF (0.5f , 0.0f ));
84106 }
85107 }
86108
@@ -115,7 +137,9 @@ public void renderAxisLine(Canvas c) {
115137 *
116138 * @param pos
117139 */
118- protected void drawLabels (Canvas c , float pos ) {
140+ protected void drawLabels (Canvas c , float pos , PointF anchor ) {
141+
142+ final float labelRotationAngleDegrees = mXAxis .getLabelRotationAngle ();
119143
120144 // pre allocate to save performance (dont allocate in loop)
121145 float [] position = new float [] {
@@ -150,14 +174,14 @@ protected void drawLabels(Canvas c, float pos) {
150174 }
151175 }
152176
153- drawLabel (c , label , i , position [0 ], pos );
177+ drawLabel (c , label , i , position [0 ], pos , anchor , labelRotationAngleDegrees );
154178 }
155179 }
156180 }
157181
158- protected void drawLabel (Canvas c , String label , int xIndex , float x , float y ) {
182+ protected void drawLabel (Canvas c , String label , int xIndex , float x , float y , PointF anchor , float angleDegrees ) {
159183 String formattedLabel = mXAxis .getValueFormatter ().getXValue (label , xIndex , mViewPortHandler );
160- c .drawText (formattedLabel , x , y , mAxisLabelPaint );
184+ Utils .drawText (c , formattedLabel , x , y , mAxisLabelPaint , anchor , angleDegrees );
161185 }
162186
163187 @ Override
0 commit comments