Skip to content

fix scaling issues on bar graph #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Binary file modified HoloGraphLibrary/res/drawable-xhdpi/popup_black.9.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions HoloGraphLibrary/src/com/echo/holographlibrary/Bar.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
import android.graphics.Region;

public class Bar {

private int color;
private int color = -1;
private String name;
private float value;
private Path path;
Expand Down
52 changes: 28 additions & 24 deletions HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,23 @@
package com.echo.holographlibrary;

import android.content.Context;
import android.graphics.*;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.drawable.NinePatchDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class BarGraph extends View {

public class BarGraph extends Graph {
private ArrayList<Bar> points = new ArrayList<Bar>();
private Paint p = new Paint();
private Path path = new Path();
Expand All @@ -45,7 +50,7 @@ public class BarGraph extends View {
private OnBarClickedListener listener;
private Bitmap fullImage;
private boolean shouldUpdate = false;
private String unit = "$";
private String unit = "";
private Boolean append = false;
private Rect r2 = new Rect();
private Rect r3 = new Rect();
Expand All @@ -64,6 +69,7 @@ public void setShowBarText(boolean show) {

public void setBars(ArrayList<Bar> points) {
this.points = points;
shouldUpdate = true;
postInvalidate();
}

Expand Down Expand Up @@ -96,26 +102,25 @@ public void onDraw(Canvas ca) {
NinePatchDrawable popup = (NinePatchDrawable) this.getResources().getDrawable(R.drawable.popup_black);

float maxValue = 0;
float padding = 7;
int selectPadding = 4;
float bottomPadding = 40;
float padding = convertToPx(7, DP);
int selectPadding = (int) convertToPx(4, DP);
float bottomPadding = convertToPx(40, DP);

float usableHeight;
if (showBarText) {
this.p.setTextSize(40);
this.p.getTextBounds(unit, 0, 1, r3);
usableHeight = getHeight() - bottomPadding - Math.abs(r3.top - r3.bottom) - 26;
this.p.setTextSize(convertToPx(20, SP));
this.p.getTextBounds(unit+"1", 0, unit.length()+1, r3);
usableHeight = getHeight() - bottomPadding - Math.abs(r3.top - r3.bottom) - convertToPx(30, DP);
} else {
usableHeight = getHeight() - bottomPadding;
}


p.setColor(Color.BLACK);
p.setStrokeWidth(2);
p.setStrokeWidth(convertToPx(2, DP));
p.setAlpha(50);
p.setAntiAlias(true);

canvas.drawLine(0, getHeight() - bottomPadding + 10, getWidth(), getHeight() - bottomPadding + 10, p);
canvas.drawLine(0, getHeight() - bottomPadding + convertToPx(10, DP), getWidth(), getHeight() - bottomPadding + convertToPx(10, DP), p);

float barWidth = (getWidth() - (padding * 2) * points.size()) / points.size();

Expand All @@ -135,22 +140,21 @@ public void onDraw(Canvas ca) {
p.setPath(path);
p.setRegion(new Region(r.left - selectPadding, r.top - selectPadding, r.right + selectPadding, r.bottom + selectPadding));

this.p.setColor(p.getColor());
this.p.setColor(p.getColor() == -1 ? DEFAULT_COLORS.get(count % DEFAULT_COLORS.size()) : p.getColor());
this.p.setAlpha(255);
canvas.drawRect(r, this.p);
this.p.setTextSize(20);
canvas.drawText(p.getName(), (int) (((r.left + r.right) / 2) - (this.p.measureText(p.getName()) / 2)), getHeight() - 5, this.p);
this.p.setTextSize(convertToPx(20, SP));
canvas.drawText(p.getName(), (int) (((r.left + r.right) / 2) - (this.p.measureText(p.getName()) / 2)), getHeight() - convertToPx(5, DP), this.p);
if (showBarText) {
this.p.setTextSize(40);
this.p.setTextSize(convertToPx(20, SP));
this.p.setColor(Color.WHITE);
this.p.getTextBounds(unit + p.getValue(), 0, 1, r2);
if (popup != null)
popup.setBounds((int) (((r.left + r.right) / 2) - (this.p.measureText(unit + p.getValue()) / 2)) - 14, r.top + (r2.top - r2.bottom) - 26, (int) (((r.left + r.right) / 2) + (this.p.measureText(unit + p.getValue()) / 2)) + 14, r.top);
popup.setBounds((int) (((r.left + r.right) / 2) - (this.p.measureText(unit + p.getValue()) / 2)) - (int) convertToPx(14, DP), r.top + (r2.top - r2.bottom) - (int) convertToPx(30, DP), (int) (((r.left + r.right) / 2) + (this.p.measureText(unit + p.getValue()) / 2)) + (int) convertToPx(14, DP), r.top);
popup.draw(canvas);
if (isAppended())
canvas.drawText(p.getValue() + unit, (int) (((r.left + r.right) / 2) - (this.p.measureText(unit + p.getValue()) / 2)), r.top - 20, this.p);
canvas.drawText(p.getValue() + unit, (int) (((r.left + r.right) / 2) - (this.p.measureText(unit + p.getValue()) / 2)), r.top - convertToPx(20, DP), this.p);
else
canvas.drawText(unit + p.getValue(), (int) (((r.left + r.right) / 2) - (this.p.measureText(unit + p.getValue()) / 2)), r.top - 20, this.p);
canvas.drawText(unit + p.getValue(), (int) (((r.left + r.right) / 2) - (this.p.measureText(unit + p.getValue()) / 2)), r.top - convertToPx(20, DP), this.p);
}
if (indexSelected == count && listener != null) {
this.p.setColor(Color.parseColor("#33B5E5"));
Expand All @@ -168,7 +172,7 @@ public void onDraw(Canvas ca) {
}

@Override
public boolean onTouchEvent(@NotNull MotionEvent event) {
public boolean onTouchEvent(MotionEvent event) {

Point point = new Point();
point.x = (int) event.getX();
Expand Down
27 changes: 27 additions & 0 deletions HoloGraphLibrary/src/com/echo/holographlibrary/Graph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.echo.holographlibrary;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import java.util.Arrays;
import java.util.List;

class Graph extends View {
static final List<Integer> DEFAULT_COLORS = Arrays.asList(0xffcc5c57, 0xff5f6ec2, 0xfff9db00, 0xffb7cf47, 0xfff48935, 0xff4ba5e2, 0xff99cc00, 0xffffbb33, 0xffaa66cc);
static final int DP = TypedValue.COMPLEX_UNIT_DIP;
static final int SP = TypedValue.COMPLEX_UNIT_SP;

public Graph(Context context){
super(context);
}

public Graph(Context context, AttributeSet attrs) {
super(context, attrs);
}

float convertToPx(int value, int unit) {
return TypedValue.applyDimension(unit, value, getContext().getResources().getDisplayMetrics());
}
}
Empty file modified HoloGraphLibrary/src/com/echo/holographlibrary/Line.java
100644 → 100755
Empty file.
Loading