|
15 | 15 | * changing formatter {@link #setFormatter(ValueFormatter)}. Axis can have a name that should be displayed next to |
16 | 16 | * labels(that depends on renderer implementation), you can change name using {@link #setName(String)}, by default axis |
17 | 17 | * name is empty and therefore not displayed. |
18 | | - * |
19 | 18 | */ |
20 | 19 | public class Axis { |
21 | 20 | public static final int DEFAULT_TEXT_SIZE_SP = 12; |
22 | 21 | public static final int DEFAULT_MAX_AXIS_LABEL_CHARS = 4; |
23 | 22 |
|
24 | | - /** Axis values, each value will be used to calculate its label position. */ |
| 23 | + /** |
| 24 | + * Axis values, each value will be used to calculate its label position. |
| 25 | + */ |
25 | 26 | private List<AxisValue> values = new ArrayList<AxisValue>(); |
26 | 27 |
|
27 | | - /** Name for this axis. */ |
| 28 | + /** |
| 29 | + * Name for this axis. |
| 30 | + */ |
28 | 31 | private String name; |
29 | 32 |
|
30 | | - /** If true axis will be generated to automatically fit chart ranges. **/ |
| 33 | + /** |
| 34 | + * If true axis will be generated to automatically fit chart ranges. * |
| 35 | + */ |
31 | 36 | private boolean isAutoGenerated = true; |
32 | 37 |
|
33 | | - /** If true renderer will draw lines(grid) for this axis. */ |
| 38 | + /** |
| 39 | + * If true renderer will draw lines(grid) for this axis. |
| 40 | + */ |
34 | 41 | private boolean hasLines = false; |
35 | 42 |
|
36 | | - /** If true axis labels will be drown inside chart area. */ |
| 43 | + /** |
| 44 | + * If true axis labels will be drown inside chart area. |
| 45 | + */ |
37 | 46 | private boolean isInside = false; |
38 | 47 |
|
39 | | - /** Axis labels and name text color. */ |
| 48 | + /** |
| 49 | + * Axis labels and name text color. |
| 50 | + */ |
40 | 51 | private int textColor = Color.LTGRAY; |
41 | 52 |
|
42 | | - /** Axis grid lines color. */ |
| 53 | + /** |
| 54 | + * Axis grid lines color. |
| 55 | + */ |
43 | 56 | private int lineColor = Utils.DEFAULT_DARKEN_COLOR; |
44 | 57 |
|
45 | | - /** Text size for axis labels and name. */ |
| 58 | + /** |
| 59 | + * Text size for axis labels and name. |
| 60 | + */ |
46 | 61 | private int textSize = DEFAULT_TEXT_SIZE_SP; |
47 | 62 |
|
48 | | - /** Maximum number of characters used for this axis. Used to determine axis dimensions. */ |
| 63 | + /** |
| 64 | + * Maximum number of characters used for this axis. Used to determine axis dimensions. |
| 65 | + */ |
49 | 66 | private int maxLabelChars = DEFAULT_MAX_AXIS_LABEL_CHARS; |
50 | 67 |
|
51 | | - /** Typeface for labels and name text. */ |
| 68 | + /** |
| 69 | + * Typeface for labels and name text. |
| 70 | + */ |
52 | 71 | private Typeface typeface; |
53 | 72 |
|
54 | | - /** Formatter used to format labels. */ |
| 73 | + /** |
| 74 | + * Formatter used to format labels. |
| 75 | + */ |
55 | 76 | private ValueFormatter formatter = new SimpleValueFormatter(); |
56 | 77 |
|
57 | | - /** If true draws a line between the labels and the graph **/ |
| 78 | + /** |
| 79 | + * If true draws a line between the labels and the graph * |
| 80 | + */ |
58 | 81 | private boolean hasSeparationLine = true; |
59 | 82 |
|
60 | 83 | /** |
61 | 84 | * Creates auto-generated axis without name and with default formatter. |
62 | | - * |
| 85 | + * |
63 | 86 | * @see SimpleValueFormatter |
64 | 87 | */ |
65 | 88 | public Axis() { |
@@ -226,4 +249,55 @@ public boolean hasSeparationLine() { |
226 | 249 | return hasSeparationLine; |
227 | 250 | } |
228 | 251 |
|
| 252 | + /** |
| 253 | + * Generates Axis with values from start to stop inclusive. |
| 254 | + */ |
| 255 | + public static Axis generateAxisFromRange(float start, float stop, float step) { |
| 256 | + |
| 257 | + List<AxisValue> values = new ArrayList<AxisValue>(); |
| 258 | + for (float value = start; value <= stop; value += step) { |
| 259 | + AxisValue axisValue = new AxisValue(value); |
| 260 | + values.add(axisValue); |
| 261 | + } |
| 262 | + |
| 263 | + Axis axis = new Axis(values); |
| 264 | + return axis; |
| 265 | + } |
| 266 | + |
| 267 | + /** |
| 268 | + * Generates Axis with values from given list. |
| 269 | + */ |
| 270 | + public static Axis generateAxisFromCollection(List<Float> axisValues) { |
| 271 | + List<AxisValue> values = new ArrayList<AxisValue>(); |
| 272 | + int index = 0; |
| 273 | + for (float value : axisValues) { |
| 274 | + AxisValue axisValue = new AxisValue(value); |
| 275 | + values.add(axisValue); |
| 276 | + ++index; |
| 277 | + } |
| 278 | + |
| 279 | + Axis axis = new Axis(values); |
| 280 | + return axis; |
| 281 | + } |
| 282 | + |
| 283 | + /** |
| 284 | + * Generates Axis with values and labels from given lists, both lists must have the same size. |
| 285 | + */ |
| 286 | + public static Axis generateAxisFromCollection(List<Float> axisValues, List<String> axisValuesLabels) { |
| 287 | + if (axisValues.size() != axisValuesLabels.size()) { |
| 288 | + throw new IllegalArgumentException("Values and labels lists must have the same size!"); |
| 289 | + } |
| 290 | + |
| 291 | + List<AxisValue> values = new ArrayList<AxisValue>(); |
| 292 | + int index = 0; |
| 293 | + for (float value : axisValues) { |
| 294 | + AxisValue axisValue = new AxisValue(value, axisValuesLabels.get(index).toCharArray()); |
| 295 | + values.add(axisValue); |
| 296 | + ++index; |
| 297 | + } |
| 298 | + |
| 299 | + Axis axis = new Axis(values); |
| 300 | + return axis; |
| 301 | + } |
| 302 | + |
229 | 303 | } |
0 commit comments