diff --git a/HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java b/HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java index e6662f6..d90dca1 100644 --- a/HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java +++ b/HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java @@ -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; @@ -39,7 +48,6 @@ public class BarGraph extends View { private ArrayList points = new ArrayList(); private Paint p = new Paint(); - private Path path = new Path(); private Rect r; private boolean showBarText = true; private int indexSelected = -1; @@ -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 values = new ArrayList(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); @@ -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 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) { diff --git a/HoloGraphLibrary/src/com/echo/holographlibrary/BarStackSegment.java b/HoloGraphLibrary/src/com/echo/holographlibrary/BarStackSegment.java index 3b9be5d..947a39b 100644 --- a/HoloGraphLibrary/src/com/echo/holographlibrary/BarStackSegment.java +++ b/HoloGraphLibrary/src/com/echo/holographlibrary/BarStackSegment.java @@ -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; + } }