From c29bb906427416b516b26d897c69574522b64397 Mon Sep 17 00:00:00 2001 From: Lecho Date: Thu, 18 Dec 2014 20:39:06 +0100 Subject: [PATCH] Added static methods for Axis generation from collection or range #30 --- .../src/lecho/lib/hellocharts/model/Axis.java | 102 +++++++++++++++--- 1 file changed, 88 insertions(+), 14 deletions(-) diff --git a/hellocharts-library/src/lecho/lib/hellocharts/model/Axis.java b/hellocharts-library/src/lecho/lib/hellocharts/model/Axis.java index fdf26eaf..fbf90847 100644 --- a/hellocharts-library/src/lecho/lib/hellocharts/model/Axis.java +++ b/hellocharts-library/src/lecho/lib/hellocharts/model/Axis.java @@ -15,51 +15,74 @@ * changing formatter {@link #setFormatter(ValueFormatter)}. Axis can have a name that should be displayed next to * labels(that depends on renderer implementation), you can change name using {@link #setName(String)}, by default axis * name is empty and therefore not displayed. - * */ public class Axis { public static final int DEFAULT_TEXT_SIZE_SP = 12; public static final int DEFAULT_MAX_AXIS_LABEL_CHARS = 4; - /** Axis values, each value will be used to calculate its label position. */ + /** + * Axis values, each value will be used to calculate its label position. + */ private List values = new ArrayList(); - /** Name for this axis. */ + /** + * Name for this axis. + */ private String name; - /** If true axis will be generated to automatically fit chart ranges. **/ + /** + * If true axis will be generated to automatically fit chart ranges. * + */ private boolean isAutoGenerated = true; - /** If true renderer will draw lines(grid) for this axis. */ + /** + * If true renderer will draw lines(grid) for this axis. + */ private boolean hasLines = false; - /** If true axis labels will be drown inside chart area. */ + /** + * If true axis labels will be drown inside chart area. + */ private boolean isInside = false; - /** Axis labels and name text color. */ + /** + * Axis labels and name text color. + */ private int textColor = Color.LTGRAY; - /** Axis grid lines color. */ + /** + * Axis grid lines color. + */ private int lineColor = Utils.DEFAULT_DARKEN_COLOR; - /** Text size for axis labels and name. */ + /** + * Text size for axis labels and name. + */ private int textSize = DEFAULT_TEXT_SIZE_SP; - /** Maximum number of characters used for this axis. Used to determine axis dimensions. */ + /** + * Maximum number of characters used for this axis. Used to determine axis dimensions. + */ private int maxLabelChars = DEFAULT_MAX_AXIS_LABEL_CHARS; - /** Typeface for labels and name text. */ + /** + * Typeface for labels and name text. + */ private Typeface typeface; - /** Formatter used to format labels. */ + /** + * Formatter used to format labels. + */ private ValueFormatter formatter = new SimpleValueFormatter(); - /** If true draws a line between the labels and the graph **/ + /** + * If true draws a line between the labels and the graph * + */ private boolean hasSeparationLine = true; /** * Creates auto-generated axis without name and with default formatter. - * + * * @see SimpleValueFormatter */ public Axis() { @@ -226,4 +249,55 @@ public boolean hasSeparationLine() { return hasSeparationLine; } + /** + * Generates Axis with values from start to stop inclusive. + */ + public static Axis generateAxisFromRange(float start, float stop, float step) { + + List values = new ArrayList(); + for (float value = start; value <= stop; value += step) { + AxisValue axisValue = new AxisValue(value); + values.add(axisValue); + } + + Axis axis = new Axis(values); + return axis; + } + + /** + * Generates Axis with values from given list. + */ + public static Axis generateAxisFromCollection(List axisValues) { + List values = new ArrayList(); + int index = 0; + for (float value : axisValues) { + AxisValue axisValue = new AxisValue(value); + values.add(axisValue); + ++index; + } + + Axis axis = new Axis(values); + return axis; + } + + /** + * Generates Axis with values and labels from given lists, both lists must have the same size. + */ + public static Axis generateAxisFromCollection(List axisValues, List axisValuesLabels) { + if (axisValues.size() != axisValuesLabels.size()) { + throw new IllegalArgumentException("Values and labels lists must have the same size!"); + } + + List values = new ArrayList(); + int index = 0; + for (float value : axisValues) { + AxisValue axisValue = new AxisValue(value, axisValuesLabels.get(index).toCharArray()); + values.add(axisValue); + ++index; + } + + Axis axis = new Axis(values); + return axis; + } + }