Skip to content

Commit

Permalink
Added static methods for Axis generation from collection or range #30
Browse files Browse the repository at this point in the history
  • Loading branch information
lecho committed Dec 18, 2014
1 parent 2abd4e2 commit c29bb90
Showing 1 changed file with 88 additions and 14 deletions.
102 changes: 88 additions & 14 deletions hellocharts-library/src/lecho/lib/hellocharts/model/Axis.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<AxisValue> values = new ArrayList<AxisValue>();

/** 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() {
Expand Down Expand Up @@ -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<AxisValue> values = new ArrayList<AxisValue>();
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<Float> axisValues) {
List<AxisValue> values = new ArrayList<AxisValue>();
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<Float> axisValues, List<String> axisValuesLabels) {
if (axisValues.size() != axisValuesLabels.size()) {
throw new IllegalArgumentException("Values and labels lists must have the same size!");
}

List<AxisValue> values = new ArrayList<AxisValue>();
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;
}

}

0 comments on commit c29bb90

Please sign in to comment.