Skip to content

Commit c29bb90

Browse files
committed
Added static methods for Axis generation from collection or range #30
1 parent 2abd4e2 commit c29bb90

File tree

1 file changed

+88
-14
lines changed
  • hellocharts-library/src/lecho/lib/hellocharts/model

1 file changed

+88
-14
lines changed

hellocharts-library/src/lecho/lib/hellocharts/model/Axis.java

Lines changed: 88 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,51 +15,74 @@
1515
* changing formatter {@link #setFormatter(ValueFormatter)}. Axis can have a name that should be displayed next to
1616
* labels(that depends on renderer implementation), you can change name using {@link #setName(String)}, by default axis
1717
* name is empty and therefore not displayed.
18-
*
1918
*/
2019
public class Axis {
2120
public static final int DEFAULT_TEXT_SIZE_SP = 12;
2221
public static final int DEFAULT_MAX_AXIS_LABEL_CHARS = 4;
2322

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+
*/
2526
private List<AxisValue> values = new ArrayList<AxisValue>();
2627

27-
/** Name for this axis. */
28+
/**
29+
* Name for this axis.
30+
*/
2831
private String name;
2932

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+
*/
3136
private boolean isAutoGenerated = true;
3237

33-
/** If true renderer will draw lines(grid) for this axis. */
38+
/**
39+
* If true renderer will draw lines(grid) for this axis.
40+
*/
3441
private boolean hasLines = false;
3542

36-
/** If true axis labels will be drown inside chart area. */
43+
/**
44+
* If true axis labels will be drown inside chart area.
45+
*/
3746
private boolean isInside = false;
3847

39-
/** Axis labels and name text color. */
48+
/**
49+
* Axis labels and name text color.
50+
*/
4051
private int textColor = Color.LTGRAY;
4152

42-
/** Axis grid lines color. */
53+
/**
54+
* Axis grid lines color.
55+
*/
4356
private int lineColor = Utils.DEFAULT_DARKEN_COLOR;
4457

45-
/** Text size for axis labels and name. */
58+
/**
59+
* Text size for axis labels and name.
60+
*/
4661
private int textSize = DEFAULT_TEXT_SIZE_SP;
4762

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+
*/
4966
private int maxLabelChars = DEFAULT_MAX_AXIS_LABEL_CHARS;
5067

51-
/** Typeface for labels and name text. */
68+
/**
69+
* Typeface for labels and name text.
70+
*/
5271
private Typeface typeface;
5372

54-
/** Formatter used to format labels. */
73+
/**
74+
* Formatter used to format labels.
75+
*/
5576
private ValueFormatter formatter = new SimpleValueFormatter();
5677

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+
*/
5881
private boolean hasSeparationLine = true;
5982

6083
/**
6184
* Creates auto-generated axis without name and with default formatter.
62-
*
85+
*
6386
* @see SimpleValueFormatter
6487
*/
6588
public Axis() {
@@ -226,4 +249,55 @@ public boolean hasSeparationLine() {
226249
return hasSeparationLine;
227250
}
228251

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+
229303
}

0 commit comments

Comments
 (0)