|
@@ -15,8 +15,6 @@ import android.view.MotionEvent;
|
|
|
import android.view.View;
|
|
import android.view.View;
|
|
|
import android.view.WindowManager;
|
|
import android.view.WindowManager;
|
|
|
|
|
|
|
|
-import hr.tacitus.graphs.R;
|
|
|
|
|
-
|
|
|
|
|
public class BarGraphView extends View {
|
|
public class BarGraphView extends View {
|
|
|
// Spacing between axis and values, and values and description.
|
|
// Spacing between axis and values, and values and description.
|
|
|
private final static float DEFAULT_AXIS_LINE_SPACING_DP = 2.0f;
|
|
private final static float DEFAULT_AXIS_LINE_SPACING_DP = 2.0f;
|
|
@@ -24,6 +22,7 @@ public class BarGraphView extends View {
|
|
|
private final static float DEFAULT_AXIS_DESCRIPTION_FONT_SIZE_SP = 16.f;
|
|
private final static float DEFAULT_AXIS_DESCRIPTION_FONT_SIZE_SP = 16.f;
|
|
|
private final static float AVERAGE_LINE_STROKE_WIDTH = 1.f;
|
|
private final static float AVERAGE_LINE_STROKE_WIDTH = 1.f;
|
|
|
private final static float VALUE_TEXT_OFFSET = 5f;
|
|
private final static float VALUE_TEXT_OFFSET = 5f;
|
|
|
|
|
+ private final static float NO_DATA_TEXT_SIZE_SP = 18.f;
|
|
|
|
|
|
|
|
VerticalAxisRect verticalAxisRect = new VerticalAxisRect();
|
|
VerticalAxisRect verticalAxisRect = new VerticalAxisRect();
|
|
|
HorizontalAxisRect horizontalAxisRect = new HorizontalAxisRect();
|
|
HorizontalAxisRect horizontalAxisRect = new HorizontalAxisRect();
|
|
@@ -33,10 +32,12 @@ public class BarGraphView extends View {
|
|
|
// Scroll value, default is leftmost.
|
|
// Scroll value, default is leftmost.
|
|
|
private float offsetX = 0;
|
|
private float offsetX = 0;
|
|
|
private boolean drawAverageLine = false;
|
|
private boolean drawAverageLine = false;
|
|
|
|
|
+ private String noDataText;
|
|
|
|
|
|
|
|
private Paint averageLinePaint = null;
|
|
private Paint averageLinePaint = null;
|
|
|
private Paint canvasPaint = null;
|
|
private Paint canvasPaint = null;
|
|
|
private Paint axisPaint = null;
|
|
private Paint axisPaint = null;
|
|
|
|
|
+ private Paint textPaint = null;
|
|
|
|
|
|
|
|
private DisplayMetrics dm = null;
|
|
private DisplayMetrics dm = null;
|
|
|
|
|
|
|
@@ -50,6 +51,39 @@ public class BarGraphView extends View {
|
|
|
float barPaddingPx;
|
|
float barPaddingPx;
|
|
|
float ordinateMinMarkingsDistance;
|
|
float ordinateMinMarkingsDistance;
|
|
|
|
|
|
|
|
|
|
+ GestureDetector gestureDetector = new GestureDetector(this.getContext(), new OnGestureListener() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
|
|
|
|
|
+ scrollBy((int) distanceX, (int) distanceY);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean onDown(MotionEvent e) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onLongPress(MotionEvent e) {
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onShowPress(MotionEvent e) {
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean onSingleTapUp(MotionEvent e) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
public BarGraphView(Context context) {
|
|
public BarGraphView(Context context) {
|
|
|
super(context);
|
|
super(context);
|
|
|
init(context, null);
|
|
init(context, null);
|
|
@@ -70,6 +104,7 @@ public class BarGraphView extends View {
|
|
|
barPaddingPx = getDimensionFromDp(6.f);
|
|
barPaddingPx = getDimensionFromDp(6.f);
|
|
|
axisValuesFontSizePx = getDimensionFromSp(14.f);
|
|
axisValuesFontSizePx = getDimensionFromSp(14.f);
|
|
|
ordinateMinMarkingsDistance = getDimensionFromDp(40.f);
|
|
ordinateMinMarkingsDistance = getDimensionFromDp(40.f);
|
|
|
|
|
+ noDataText = "No data available.";
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void setupDisplayMetrics() {
|
|
private void setupDisplayMetrics() {
|
|
@@ -102,6 +137,9 @@ public class BarGraphView extends View {
|
|
|
horizontalAxisRect.setShowValues(a.getBoolean(R.styleable.BarGraph_showHorizontalValues, false));
|
|
horizontalAxisRect.setShowValues(a.getBoolean(R.styleable.BarGraph_showHorizontalValues, false));
|
|
|
verticalAxisRect.setShowValues(a.getBoolean(R.styleable.BarGraph_showVerticalValues, true));
|
|
verticalAxisRect.setShowValues(a.getBoolean(R.styleable.BarGraph_showVerticalValues, true));
|
|
|
|
|
|
|
|
|
|
+ if (a.hasValue(R.styleable.BarGraph_noDataText))
|
|
|
|
|
+ noDataText = a.getString(R.styleable.BarGraph_noDataText);
|
|
|
|
|
+
|
|
|
} finally {
|
|
} finally {
|
|
|
a.recycle();
|
|
a.recycle();
|
|
|
}
|
|
}
|
|
@@ -117,12 +155,18 @@ public class BarGraphView extends View {
|
|
|
canvasPaint = new Paint();
|
|
canvasPaint = new Paint();
|
|
|
canvasPaint.setStyle(Paint.Style.FILL);
|
|
canvasPaint.setStyle(Paint.Style.FILL);
|
|
|
canvasPaint.setColor(Color.WHITE);
|
|
canvasPaint.setColor(Color.WHITE);
|
|
|
- canvasPaint.setAntiAlias(true);
|
|
|
|
|
|
|
+ // canvasPaint.setAntiAlias(true);
|
|
|
|
|
|
|
|
axisPaint = new Paint();
|
|
axisPaint = new Paint();
|
|
|
axisPaint.setStyle(Paint.Style.FILL);
|
|
axisPaint.setStyle(Paint.Style.FILL);
|
|
|
axisPaint.setColor(Color.BLACK);
|
|
axisPaint.setColor(Color.BLACK);
|
|
|
axisPaint.setAntiAlias(true);
|
|
axisPaint.setAntiAlias(true);
|
|
|
|
|
+
|
|
|
|
|
+ textPaint = new Paint();
|
|
|
|
|
+ textPaint.setStyle(Paint.Style.FILL);
|
|
|
|
|
+ textPaint.setColor(Color.BLACK);
|
|
|
|
|
+ textPaint.setAntiAlias(true);
|
|
|
|
|
+ textPaint.setTextSize(getDimensionFromSp(NO_DATA_TEXT_SIZE_SP));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void init(Context context, AttributeSet attrs) {
|
|
private void init(Context context, AttributeSet attrs) {
|
|
@@ -158,15 +202,28 @@ public class BarGraphView extends View {
|
|
|
canvas.drawLine(verticalAxisRect.getWidth() + getPaddingLeft(), y1, width - getPaddingRight(), y2, averageLinePaint);
|
|
canvas.drawLine(verticalAxisRect.getWidth() + getPaddingLeft(), y1, width - getPaddingRight(), y2, averageLinePaint);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private void drawNoDataText(Canvas canvas) {
|
|
|
|
|
+ Rect drawingRect = new Rect();
|
|
|
|
|
+ getDrawingRect(drawingRect);
|
|
|
|
|
+
|
|
|
|
|
+ Rect textBounds = new Rect();
|
|
|
|
|
+ textPaint.getTextBounds(noDataText, 0, noDataText.length() - 1, textBounds);
|
|
|
|
|
+
|
|
|
|
|
+ canvas.drawText(noDataText, drawingRect.exactCenterX() - textBounds.width() / 2,
|
|
|
|
|
+ drawingRect.exactCenterY() - textBounds.height() / 2, textPaint);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
protected void onDraw(Canvas canvas) {
|
|
protected void onDraw(Canvas canvas) {
|
|
|
super.onDraw(canvas);
|
|
super.onDraw(canvas);
|
|
|
|
|
|
|
|
- if (adapter == null)
|
|
|
|
|
- return;
|
|
|
|
|
-
|
|
|
|
|
canvas.drawPaint(canvasPaint);
|
|
canvas.drawPaint(canvasPaint);
|
|
|
|
|
|
|
|
|
|
+ if (adapter == null) {
|
|
|
|
|
+ drawNoDataText(canvas);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
drawHorizontalAxis(canvas);
|
|
drawHorizontalAxis(canvas);
|
|
|
drawVerticalAxis(canvas);
|
|
drawVerticalAxis(canvas);
|
|
|
drawValues(canvas);
|
|
drawValues(canvas);
|
|
@@ -348,43 +405,11 @@ public class BarGraphView extends View {
|
|
|
return (getWidth() - verticalAxisRect.getWidth() - getPaddingLeft() - getPaddingRight());
|
|
return (getWidth() - verticalAxisRect.getWidth() - getPaddingLeft() - getPaddingRight());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- GestureDetector gd = new GestureDetector(this.getContext(), new OnGestureListener() {
|
|
|
|
|
- @Override
|
|
|
|
|
- public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
|
|
|
|
|
- scrollBy((int) distanceX, (int) distanceY);
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- public boolean onDown(MotionEvent e) {
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- public void onLongPress(MotionEvent e) {
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- public void onShowPress(MotionEvent e) {
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- public boolean onSingleTapUp(MotionEvent e) {
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
private double maxDataValue;
|
|
private double maxDataValue;
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public boolean onTouchEvent(MotionEvent event) {
|
|
public boolean onTouchEvent(MotionEvent event) {
|
|
|
- return gd.onTouchEvent(event);
|
|
|
|
|
|
|
+ return gestureDetector.onTouchEvent(event);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -433,6 +458,10 @@ public class BarGraphView extends View {
|
|
|
invalidate();
|
|
invalidate();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public BarGraphAdapter getAdapter() {
|
|
|
|
|
+ return adapter;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public void setAdapter(BarGraphAdapter adapter) {
|
|
public void setAdapter(BarGraphAdapter adapter) {
|
|
|
this.adapter = adapter;
|
|
this.adapter = adapter;
|
|
|
|
|
|
|
@@ -458,15 +487,8 @@ public class BarGraphView extends View {
|
|
|
* @return
|
|
* @return
|
|
|
*/
|
|
*/
|
|
|
private double getGraphMaxValue(double maxDataValue) {
|
|
private double getGraphMaxValue(double maxDataValue) {
|
|
|
- // odmah računamo sve potrebno, da se ne zamaramo s tim na onDraw
|
|
|
|
|
- // vrijednost za graf
|
|
|
|
|
- // zanima me kolko dekadskih znamenki ima najveća vrijednost
|
|
|
|
|
int numberOfDigits = (int) Math.floor(Math.log10(maxDataValue)) + 1;
|
|
int numberOfDigits = (int) Math.floor(Math.log10(maxDataValue)) + 1;
|
|
|
- // osnova je 10^(broj_znakova - 1)
|
|
|
|
|
int order = (int) Math.pow(10, numberOfDigits - 1);
|
|
int order = (int) Math.pow(10, numberOfDigits - 1);
|
|
|
- // pocetak je na n*order tako da je n*order najmanji veći za neki cijeli
|
|
|
|
|
- // broj n
|
|
|
|
|
- // ovaj broj nam je u biti logički maksimum
|
|
|
|
|
return ((int) (Math.ceil(maxDataValue / order))) * order;
|
|
return ((int) (Math.ceil(maxDataValue / order))) * order;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -632,6 +654,14 @@ public class BarGraphView extends View {
|
|
|
horizontalAxisRect.setDescription(desc);
|
|
horizontalAxisRect.setDescription(desc);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public String getNoDataText() {
|
|
|
|
|
+ return noDataText;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void setNoDataText(String noDataText) {
|
|
|
|
|
+ this.noDataText = noDataText;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private class BarConfig {
|
|
private class BarConfig {
|
|
|
private float padding;
|
|
private float padding;
|
|
|
private float whiteSpace;
|
|
private float whiteSpace;
|