Skip to content
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

Feature/fix bargraph call click listener #27

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 45 additions & 19 deletions HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,21 @@
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;
Expand All @@ -39,7 +48,6 @@ public class BarGraph extends View {

private ArrayList<Bar> points = new ArrayList<Bar>();
private Paint p = new Paint();
private Path path = new Path();
private Rect r;
private boolean showBarText = true;
private int indexSelected = -1;
Expand Down Expand Up @@ -132,30 +140,31 @@ public void onDraw(Canvas ca) {

r = new Rect();

path.reset();

int count = 0;
for (Bar p : points) {
Path path = new Path();

if(p.getStackedBar()){
if (p.getStackedBar()) {
ArrayList<BarStackSegment> values = new ArrayList<BarStackSegment>(p.getStackedValues());
float prevValue = 0;
for(BarStackSegment value : values) {
for (BarStackSegment value : values) {
value.Value += prevValue;
prevValue += value.Value;
}
Collections.reverse(values);

for(BarStackSegment value : values) {
for (BarStackSegment value : values) {
r.set((int) ((padding * 2) * count + padding + barWidth * count), (int) ((getHeight() - bottomPadding - (usableHeight * (value.Value / maxValue)))), (int) ((padding * 2) * count + padding + barWidth * (count + 1)), (int) ((getHeight() - bottomPadding)));
path.addRect(new RectF(r.left - selectPadding, r.top - selectPadding, r.right + selectPadding, r.bottom + selectPadding), Path.Direction.CW);
p.setPath(path);
p.setRegion(new Region(r.left - selectPadding, r.top - selectPadding, r.right + selectPadding, r.bottom + selectPadding));
value.setPath(path);
value.setRegion(new Region(r.left - selectPadding, r.top - selectPadding, r.right + selectPadding, r.bottom + selectPadding));
this.p.setColor(value.Color);
this.p.setAlpha(255);
canvas.drawRect(r, this.p);
}
}else {
} else {
r.set((int) ((padding * 2) * count + padding + barWidth * count), (int) (getHeight() - bottomPadding - (usableHeight * (p.getValue() / maxValue))), (int) ((padding * 2) * count + padding + barWidth * (count + 1)), (int) (getHeight() - bottomPadding));
path.addRect(new RectF(r.left - selectPadding, r.top - selectPadding, r.right + selectPadding, r.bottom + selectPadding), Path.Direction.CW);
p.setPath(path);
Expand Down Expand Up @@ -202,19 +211,36 @@ public boolean onTouchEvent(@NotNull MotionEvent event) {
point.x = (int) event.getX();
point.y = (int) event.getY();

int count = 0;
for (Bar bar : points) {
Region r = new Region();
r.setPath(bar.getPath(), bar.getRegion());
if (r.contains(point.x, point.y) && event.getAction() == MotionEvent.ACTION_DOWN) {
indexSelected = count;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (r.contains(point.x, point.y) && listener != null) {
listener.onClick(indexSelected);
if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP) {
// find clicked bar index
indexSelected = -1;
for (int i = 0; i < points.size(); i++) {
Region r = new Region();

if (points.get(i).getStackedBar()) {

ArrayList<BarStackSegment> values = points.get(i).getStackedValues();
for (BarStackSegment value : values) {
r.setPath(value.getPath(), value.getRegion());
if (r.contains(point.x, point.y)) {
indexSelected = i;
}
}
}
indexSelected = -1;
else {
r.setPath(points.get(i).getPath(), points.get(i).getRegion());
if (r.contains(point.x, point.y)) {
indexSelected = i;
}
}
}
}

if (event.getAction() == MotionEvent.ACTION_UP) {
// dispatch onClick event
if ((indexSelected != -1) && (listener != null)) {
listener.onClick(indexSelected);
}
count++;
}

if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
package com.echo.holographlibrary;

import android.graphics.Path;
import android.graphics.Region;

/**
* Created by Aaron on 19/10/2014.
*/
public class BarStackSegment {
public float Value;
public int Color;
private Path path;
private Region region;

public BarStackSegment(int val, int color){
Value = val;
Color = color;
}
public Path getPath() {
return path;
}
public void setPath(Path path) {
this.path = path;
}
public Region getRegion() {
return region;
}
public void setRegion(Region region) {
this.region = region;
}
}