7
7
import android .graphics .Paint ;
8
8
import android .graphics .Path ;
9
9
import android .graphics .PointF ;
10
+ import android .graphics .Paint .Align ;
10
11
import android .util .AttributeSet ;
11
12
import android .util .Log ;
12
13
17
18
import com .github .mikephil .charting .utils .Utils ;
18
19
import com .github .mikephil .charting .utils .XLabels ;
19
20
import com .github .mikephil .charting .utils .YLabels ;
21
+ import com .github .mikephil .charting .utils .Legend .LegendPosition ;
20
22
21
23
import java .util .ArrayList ;
22
24
23
25
/**
24
- * Implementation of the RadarChart, a "spidernet"-like chart. It works best when
25
- * displaying 5-10 entries per DataSet.
26
+ * Implementation of the RadarChart, a "spidernet"-like chart. It works best
27
+ * when displaying 5-10 entries per DataSet.
26
28
*
27
29
* @author Philipp Jahoda
28
30
*/
@@ -101,13 +103,49 @@ protected void calcMinMax(boolean fixedValues) {
101
103
102
104
mYChartMin = 0 ;
103
105
}
104
-
106
+
105
107
@ Override
106
108
public void prepare () {
107
109
super .prepare ();
108
-
110
+
109
111
prepareXLabels ();
110
112
}
113
+
114
+ @ Override
115
+ protected void calculateOffsets () {
116
+
117
+ if (mLegend == null )
118
+ return ;
119
+
120
+ // setup offsets for legend
121
+ if (mLegend .getPosition () == LegendPosition .RIGHT_OF_CHART ) {
122
+
123
+ mLegend .setOffsetRight (mLegend .getMaximumEntryLength (mLegendLabelPaint ));
124
+ mLegendLabelPaint .setTextAlign (Align .LEFT );
125
+
126
+ } else if (mLegend .getPosition () == LegendPosition .BELOW_CHART_LEFT
127
+ || mLegend .getPosition () == LegendPosition .BELOW_CHART_RIGHT
128
+ || mLegend .getPosition () == LegendPosition .BELOW_CHART_CENTER ) {
129
+
130
+ mLegend .setOffsetBottom (mLegendLabelPaint .getTextSize () * 5.5f );
131
+ }
132
+
133
+ mLegend .setOffsetTop (mOffsetTop );
134
+ mLegend .setOffsetLeft (mOffsetLeft );
135
+
136
+ if (mDrawLegend ) {
137
+
138
+ mOffsetBottom = Math .max (mXLabels .mLabelWidth , mOffsetBottom );
139
+ mOffsetTop = Math .max (mXLabels .mLabelWidth , mOffsetTop );
140
+ mOffsetRight = Math .max (mXLabels .mLabelWidth , mOffsetRight );
141
+ mOffsetLeft = Math .max (mXLabels .mLabelWidth , mOffsetLeft );
142
+
143
+ mOffsetBottom = Math .max (mOffsetBottom , mLegend .getOffsetBottom ());
144
+ mOffsetRight = Math .max (mOffsetRight , mLegend .getOffsetRight () / 3 * 2 );
145
+ }
146
+
147
+ applyCalculatedOffsets ();
148
+ }
111
149
112
150
@ Override
113
151
protected void onDraw (Canvas canvas ) {
@@ -119,7 +157,7 @@ protected void onDraw(Canvas canvas) {
119
157
long starttime = System .currentTimeMillis ();
120
158
121
159
prepareYLabels ();
122
-
160
+
123
161
drawXLabels ();
124
162
125
163
drawWeb ();
@@ -137,9 +175,9 @@ protected void onDraw(Canvas canvas) {
137
175
drawLegend ();
138
176
139
177
drawDescription ();
140
-
178
+
141
179
drawMarkers ();
142
-
180
+
143
181
canvas .drawBitmap (mDrawBitmap , 0 , 0 , mDrawPaint );
144
182
145
183
Log .i (LOG_TAG , "RadarChart DrawTime: " + (System .currentTimeMillis () - starttime ) + " ms" );
@@ -243,7 +281,6 @@ protected void drawData() {
243
281
mDrawCanvas .drawPath (surface , mRenderPaint );
244
282
}
245
283
}
246
-
247
284
248
285
/**
249
286
* Calculates the required maximum y-value in order to be able to provide
@@ -308,7 +345,7 @@ private void drawYLabels() {
308
345
mSeparateTousands ), p .x + 10 , p .y - 5 , mYLabelPaint );
309
346
}
310
347
}
311
-
348
+
312
349
/**
313
350
* setup the x-axis labels
314
351
*/
@@ -324,7 +361,7 @@ private void prepareXLabels() {
324
361
325
362
mXLabels .mLabelWidth = Utils .calcTextWidth (mXLabelPaint , a .toString ());
326
363
mXLabels .mLabelHeight = Utils .calcTextWidth (mXLabelPaint , "Q" );
327
-
364
+
328
365
Log .i (LOG_TAG , "xlabels prepared, width: " + mXLabels .mLabelWidth );
329
366
}
330
367
@@ -335,26 +372,28 @@ private void drawXLabels() {
335
372
336
373
if (!mDrawXLabels )
337
374
return ;
338
-
375
+
339
376
mXLabelPaint .setTypeface (mXLabels .getTypeface ());
340
377
mXLabelPaint .setTextSize (mXLabels .getTextSize ());
341
378
mXLabelPaint .setColor (mXLabels .getTextColor ());
342
-
379
+
343
380
float sliceangle = getSliceAngle ();
344
381
345
382
// calculate the factor that is needed for transforming the value to
346
383
// pixels
347
384
float factor = getFactor ();
348
385
349
386
PointF c = getCenterOffsets ();
350
-
351
- for (int i = 0 ; i < mCurrentData .getXValCount (); i ++) {
352
-
387
+
388
+ for (int i = 0 ; i < mCurrentData .getXValCount (); i ++) {
389
+
353
390
String text = mCurrentData .getXVals ().get (i );
354
-
355
- PointF p = getPosition (c , mYChartMax * factor , sliceangle * i + mRotationAngle );
356
-
357
- mDrawCanvas .drawText (text , p .x , p .y , mXLabelPaint );
391
+
392
+ float angle = (sliceangle * i + mRotationAngle ) % 360f ;
393
+
394
+ PointF p = getPosition (c , mYChartMax * factor + mXLabels .mLabelWidth / 2f , angle );
395
+
396
+ mDrawCanvas .drawText (text , p .x , p .y + mXLabels .mLabelHeight / 2f , mXLabelPaint );
358
397
}
359
398
}
360
399
0 commit comments