-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
YAxis.java
413 lines (343 loc) · 9.98 KB
/
YAxis.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package com.github.mikephil.charting.components;
import android.graphics.Paint;
import com.github.mikephil.charting.utils.DefaultValueFormatter;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ValueFormatter;
/**
* Class representing the y-axis labels settings and its entries. Only use the setter methods to modify it. Do not
* access public variables directly. Be aware that not all features the YLabels class provides are suitable for the
* RadarChart. Customizations that affect the value range of the axis need to be applied before setting data for the
* chart.
*
* @author Philipp Jahoda
*/
public class YAxis extends AxisBase {
/** custom formatter that is used instead of the auto-formatter if set */
protected ValueFormatter mValueFormatter;
/** the actual array of entries */
public float[] mEntries = new float[] {};
/** the number of entries the legend contains */
public int mEntryCount;
/** the number of decimal digits to use */
public int mDecimals;
/** the number of y-label entries the y-labels should have, default 6 */
private int mLabelCount = 6;
/** indicates if the top y-label entry is drawn or not */
private boolean mDrawTopYLabelEntry = true;
/** if true, the y-labels show only the minimum and maximum value */
protected boolean mShowOnlyMinMax = false;
/** flag that indicates if the axis is inverted or not */
protected boolean mInverted = false;
/** if true, the y-label entries will always start at zero */
protected boolean mStartAtZero = true;
/** if true, the set number of y-labels will be forced */
protected boolean mForceLabels = false;
/** custom minimum value this axis represents */
protected float mCustomAxisMin = Float.NaN;
/** custom maximum value this axis represents */
protected float mCustomAxisMax = Float.NaN;
/**
* axis space from the largest value to the top in percent of the total axis range
*/
protected float mSpacePercentTop = 10f;
/**
* axis space from the smallest value to the bottom in percent of the total axis range
*/
protected float mSpacePercentBottom = 10f;
public float mAxisMaximum = 0f;
public float mAxisMinimum = 0f;
/** the total range of values this axis covers */
public float mAxisRange = 0f;
/** the position of the y-labels relative to the chart */
private YAxisLabelPosition mPosition = YAxisLabelPosition.OUTSIDE_CHART;
/** enum for the position of the y-labels relative to the chart */
public enum YAxisLabelPosition {
OUTSIDE_CHART, INSIDE_CHART
}
/** the side this axis object represents */
private AxisDependency mAxisDependency;
/**
* Enum that specifies the axis a DataSet should be plotted against, either LEFT or RIGHT.
*
* @author Philipp Jahoda
*/
public enum AxisDependency {
LEFT, RIGHT
}
public YAxis() {
super();
this.mAxisDependency = AxisDependency.LEFT;
this.mYOffset = 0f;
}
public YAxis(AxisDependency position) {
super();
this.mAxisDependency = position;
this.mYOffset = 0f;
}
public AxisDependency getAxisDependency() {
return mAxisDependency;
}
/**
* returns the position of the y-labels
*/
public YAxisLabelPosition getLabelPosition() {
return mPosition;
}
/**
* sets the position of the y-labels
*
* @param pos
*/
public void setPosition(YAxisLabelPosition pos) {
mPosition = pos;
}
/**
* returns true if drawing the top y-axis label entry is enabled
*
* @return
*/
public boolean isDrawTopYLabelEntryEnabled() {
return mDrawTopYLabelEntry;
}
/**
* set this to true to enable drawing the top y-label entry. Disabling this can be helpful when the top y-label and
* left x-label interfere with each other. default: true
*
* @param enabled
*/
public void setDrawTopYLabelEntry(boolean enabled) {
mDrawTopYLabelEntry = enabled;
}
/**
* sets the number of label entries for the y-axis max = 25, min = 2, default: 6, be aware that this number is not
* fixed (if force == false) and can only be approximated.
*
* @param count
* the number of y-axis labels that sould be displayed
* @param force
* if enabled, the set label count will be forced, meaning that the exact specified count of labels will
* be drawn and evenly distributed alongside the axis - this might cause labels to have uneven values
*/
public void setLabelCount(int count, boolean force) {
if (count > 25)
count = 25;
if (count < 2)
count = 2;
mLabelCount = count;
mForceLabels = force;
}
/**
* Returns the number of label entries the y-axis should have
*
* @return
*/
public int getLabelCount() {
return mLabelCount;
}
/**
* Returns true if focing the y-label count is enabled. Default: false
*
* @return
*/
public boolean isForceLabelsEnabled() {
return mForceLabels;
}
/**
* If enabled, the YLabels will only show the minimum and maximum value of the chart. This will ignore/override the
* set label count.
*
* @param enabled
*/
public void setShowOnlyMinMax(boolean enabled) {
mShowOnlyMinMax = enabled;
}
/**
* Returns true if showing only min and max value is enabled.
*
* @return
*/
public boolean isShowOnlyMinMaxEnabled() {
return mShowOnlyMinMax;
}
/**
* If this is set to true, the y-axis is inverted which means that low values are on top of the chart, high values
* on bottom.
*
* @param enabled
*/
public void setInverted(boolean enabled) {
mInverted = enabled;
}
/**
* If this returns true, the y-axis is inverted.
*
* @return
*/
public boolean isInverted() {
return mInverted;
}
/**
* enable this to force the y-axis labels to always start at zero
*
* @param enabled
*/
public void setStartAtZero(boolean enabled) {
this.mStartAtZero = enabled;
}
/**
* returns true if the chart is set to start at zero, false otherwise
*
* @return
*/
public boolean isStartAtZeroEnabled() {
return mStartAtZero;
}
public float getAxisMinValue() {
return mCustomAxisMin;
}
/**
* Set a custom minimum value for this axis. If set, this value will not be calculated automatically depending on
* the provided data. Use resetAxisMinValue() to undo this. Do not forget to call setStartAtZero(false) if you use
* this method. Otherwise, the axis-minimum value will still be forced to 0.
*
* @param min
*/
public void setAxisMinValue(float min) {
mCustomAxisMin = min;
}
/**
* By calling this method, any custom minimum value that has been previously set is reseted, and the calculation is
* done automatically.
*/
public void resetAxisMinValue() {
mCustomAxisMin = Float.NaN;
}
public float getAxisMaxValue() {
return mCustomAxisMax;
}
/**
* Set a custom maximum value for this axis. If set, this value will not be calculated automatically depending on
* the provided data. Use resetAxisMaxValue() to undo this.
*
* @param max
*/
public void setAxisMaxValue(float max) {
mCustomAxisMax = max;
}
/**
* By calling this method, any custom maximum value that has been previously set is reseted, and the calculation is
* done automatically.
*/
public void resetAxisMaxValue() {
mCustomAxisMax = Float.NaN;
}
/**
* Sets the top axis space in percent of the full range. Default 10f
*
* @param percent
*/
public void setSpaceTop(float percent) {
mSpacePercentTop = percent;
}
/**
* Returns the top axis space in percent of the full range. Default 10f
*
* @return
*/
public float getSpaceTop() {
return mSpacePercentTop;
}
/**
* Sets the bottom axis space in percent of the full range. Default 10f
*
* @param percent
*/
public void setSpaceBottom(float percent) {
mSpacePercentBottom = percent;
}
/**
* Returns the bottom axis space in percent of the full range. Default 10f
*
* @return
*/
public float getSpaceBottom() {
return mSpacePercentBottom;
}
public float getRequiredWidthSpace(Paint p) {
p.setTextSize(mTextSize);
String label = getLongestLabel();
return (float) Utils.calcTextWidth(p, label) + getXOffset() * 2f;
}
public float getRequiredHeightSpace(Paint p) {
p.setTextSize(mTextSize);
String label = getLongestLabel();
return (float) Utils.calcTextHeight(p, label) + Utils.convertDpToPixel(2.5f) * 2f + getYOffset();
}
@Override
public String getLongestLabel() {
String longest = "";
for (int i = 0; i < mEntries.length; i++) {
String text = getFormattedLabel(i);
if (longest.length() < text.length())
longest = text;
}
return longest;
}
/**
* Returns the formatted y-label at the specified index. This will either use the auto-formatter or the custom
* formatter (if one is set).
*
* @param index
* @return
*/
public String getFormattedLabel(int index) {
if (index < 0 || index >= mEntries.length)
return "";
else
return getValueFormatter().getFormattedValue(mEntries[index]);
}
/**
* Sets the formatter to be used for drawing the values inside the chart. If no formatter is set, the chart will
* automatically determine a reasonable formatting (concerning decimals) for all the values that are drawn inside
* the chart. Use chart.getDefaultValueFormatter() to use the formatter calculated by the chart.
*
* @param f
*/
public void setValueFormatter(ValueFormatter f) {
if (f == null)
return;
else
mValueFormatter = f;
}
/**
* Returns the formatter used for drawing the values inside the chart.
*
* @return
*/
public ValueFormatter getValueFormatter() {
return mValueFormatter;
}
/**
* If this component has no ValueFormatter or is only equipped with the default one (no custom set), return true.
*
* @return
*/
public boolean needsDefaultFormatter() {
if (mValueFormatter == null)
return true;
if (mValueFormatter instanceof DefaultValueFormatter)
return true;
return false;
}
/**
* Returns true if this axis needs horizontal offset, false if no offset is needed.
*
* @return
*/
public boolean needsOffset() {
if (isEnabled() && isDrawLabelsEnabled() && getLabelPosition() == YAxisLabelPosition.OUTSIDE_CHART)
return true;
else
return false;
}
}