|
@@ -43,8 +43,21 @@ import java.util.ArrayList;
|
|
|
|
|
|
public class LineGraph extends View {
|
|
|
|
|
|
+ /**
|
|
|
+ * The axis converter will be called, if the line graph loads / refreshed its axis.
|
|
|
+ */
|
|
|
public interface AxisDataConverter {
|
|
|
+ /**
|
|
|
+ * returns the x axis value for a given x coord.
|
|
|
+ * @param x double, the x coord.
|
|
|
+ * @return String, formatted value for the x position.
|
|
|
+ */
|
|
|
public String convertDataForX_Position(double x);
|
|
|
+ /**
|
|
|
+ * returns the y axis value for a given y coord.
|
|
|
+ * @param y double, the y coord.
|
|
|
+ * @return String, formatted value for the y position.
|
|
|
+ */
|
|
|
public String convertDataForY_Position(double y);
|
|
|
}
|
|
|
|
|
@@ -68,6 +81,9 @@ public class LineGraph extends View {
|
|
|
static final float leftPadding = 50, rightPadding = 16;
|
|
|
static final float sidePadding = rightPadding + leftPadding;
|
|
|
|
|
|
+ /**
|
|
|
+ * Step = Axis Period
|
|
|
+ */
|
|
|
private float xAxisStep = 4;
|
|
|
private float yAxisStep = 4;
|
|
|
|
|
@@ -84,25 +100,38 @@ public class LineGraph extends View {
|
|
|
public float getyAxisStep(){return this.yAxisStep;}
|
|
|
public float getxAxisStep(){return this.xAxisStep;}
|
|
|
|
|
|
+ /**
|
|
|
+ * The axis converter will be called if the line graph refresh / load its axis labels.
|
|
|
+ * @param conv AxisDataConverter
|
|
|
+ */
|
|
|
public void setConverter(AxisDataConverter conv){
|
|
|
this.converter = conv;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Constructor
|
|
|
+ * @param context Context
|
|
|
+ */
|
|
|
public LineGraph(Context context){
|
|
|
super(context);
|
|
|
this.mContext = context;
|
|
|
this.setWillNotDraw(false);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Constructor
|
|
|
+ * @param context Context
|
|
|
+ * @param attrs AttributeSet
|
|
|
+ */
|
|
|
public LineGraph(Context context, AttributeSet attrs) {
|
|
|
super(context, attrs);
|
|
|
this.mContext = context;
|
|
|
this.setWillNotDraw(false);
|
|
|
}
|
|
|
- public void setMinY(float minY){
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Removes all lines.
|
|
|
+ */
|
|
|
public void removeAllLines(){
|
|
|
while (lines.size() > 0){
|
|
|
lines.remove(0);
|
|
@@ -110,12 +139,23 @@ public class LineGraph extends View {
|
|
|
shouldUpdate = true;
|
|
|
//postInvalidate();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Add a line.
|
|
|
+ * @param line {@link Line Line}
|
|
|
+ */
|
|
|
public void addLine(Line line) {
|
|
|
lines.add(line);
|
|
|
shouldUpdate = true;
|
|
|
//postInvalidate();
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Adds a new point to a {@link Line lin}
|
|
|
+ * @param lineIndex the index of the line
|
|
|
+ * @param x double, the x coord.
|
|
|
+ * @param y double, the y coord.
|
|
|
+ */
|
|
|
public void addPointToLine(int lineIndex, double x, double y){
|
|
|
addPointToLine(lineIndex, (float) x, (float) y);
|
|
|
}
|
|
@@ -203,12 +243,19 @@ public class LineGraph extends View {
|
|
|
shouldUpdate = true;
|
|
|
//postInvalidate();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Resets the y axis limits
|
|
|
+ */
|
|
|
public void resetYLimits(){
|
|
|
double range = getMaxY() - getMinY();
|
|
|
setRangeY(getMinY()-range*getRangeYRatio(), getMaxY()+range*getRangeYRatio());
|
|
|
isMaxYUserSet = false;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Resets the x axis limits
|
|
|
+ */
|
|
|
public void resetXLimits(){
|
|
|
double range = getMaxX() - getMinX();
|
|
|
setRangeX(getMinX()-range*getRangeXRatio(), getMaxX()+range*getRangeXRatio());
|
|
@@ -238,22 +285,44 @@ public class LineGraph extends View {
|
|
|
public int getSize(){
|
|
|
return lines.size();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Sets the y axis range (minimal and maximal value for the y axis)
|
|
|
+ * @param min float
|
|
|
+ * @param max float
|
|
|
+ */
|
|
|
public void setRangeY(float min, float max) {
|
|
|
minY = min;
|
|
|
maxY = max;
|
|
|
isMaxYUserSet = true;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Sets the y axis range (minimal and maximal value for the y axis)
|
|
|
+ * @param min double
|
|
|
+ * @param max double
|
|
|
+ */
|
|
|
public void setRangeY(double min, double max){
|
|
|
minY = min;
|
|
|
maxY = max;
|
|
|
isMaxYUserSet = true;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Sets the x axis range (minimal and maximal value for the x axis)
|
|
|
+ * @param min float
|
|
|
+ * @param max float
|
|
|
+ */
|
|
|
public void setRangeX(float min, float max) {
|
|
|
minX = min;
|
|
|
maxX = max;
|
|
|
isMaxXUserSet = true;
|
|
|
}
|
|
|
+ /**
|
|
|
+ * Sets the x axis range (minimal and maximal value for the x axis)
|
|
|
+ * @param min double
|
|
|
+ * @param max double
|
|
|
+ */
|
|
|
public void setRangeX(double min, double max){
|
|
|
minX = min;
|
|
|
maxX = max;
|
|
@@ -325,17 +394,27 @@ public class LineGraph extends View {
|
|
|
minX = min;
|
|
|
return minX;
|
|
|
}
|
|
|
-
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns the title for the x coordinate by calling the converter.
|
|
|
+ * @param x double, the x coord.
|
|
|
+ * @return String, the title
|
|
|
+ */
|
|
|
private String getX_AxisTitle(double x){
|
|
|
if (this.converter == null)return "" + (long)x;
|
|
|
return this.converter.convertDataForX_Position(x);
|
|
|
}
|
|
|
+ /**
|
|
|
+ * Returns the title for the y coordinate by calling the converter.
|
|
|
+ * @param y double, the y coord.
|
|
|
+ * @return String, the title
|
|
|
+ */
|
|
|
private String getY_AxisTitle(double y){
|
|
|
if (this.converter == null)return "" + (long)y;
|
|
|
return this.converter.convertDataForY_Position(y);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ @Override
|
|
|
public void onDraw(Canvas ca) {
|
|
|
super.onDraw(ca);
|
|
|
|
|
@@ -447,6 +526,10 @@ public class LineGraph extends View {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Draw the x & y axis
|
|
|
+ * @param canvas Canvas
|
|
|
+ */
|
|
|
private void drawAxis(Canvas canvas){
|
|
|
//double maxX = getMaxLimX();
|
|
|
//double minX = getMinLimX();
|
|
@@ -469,7 +552,10 @@ public class LineGraph extends View {
|
|
|
this.drawAxisLabel(canvas);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ /**
|
|
|
+ * Draw the x & y axis labels.
|
|
|
+ * @param canvas Canvas
|
|
|
+ */
|
|
|
private void drawAxisLabel(Canvas canvas){
|
|
|
this.paint.setTextSize(AXIS_LABEL_FONT_SIZE * mContext.getResources().getDisplayMetrics().scaledDensity);
|
|
|
|
|
@@ -516,6 +602,10 @@ public class LineGraph extends View {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Draw the lined background.
|
|
|
+ * @param canvas Canvas
|
|
|
+ */
|
|
|
private void drawBackground(Canvas canvas){
|
|
|
|
|
|
paint.reset();
|
|
@@ -688,10 +778,17 @@ public class LineGraph extends View {
|
|
|
postInvalidate();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Set the point click listener, which will be called on clicking a point.
|
|
|
+ * @param listener OnPointClickedListener
|
|
|
+ */
|
|
|
public void setOnPointClickedListener(OnPointClickedListener listener) {
|
|
|
this.listener = listener;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * OnPointClickedListener will be called, if the user clicks a point.
|
|
|
+ */
|
|
|
public interface OnPointClickedListener {
|
|
|
abstract void onClick(int lineIndex, int pointIndex);
|
|
|
}
|