From ff16f00d25fd6cbfb74ecf94e66c830ed79c7825 Mon Sep 17 00:00:00 2001 From: fughz Date: Thu, 12 Mar 2015 23:39:03 +0900 Subject: [PATCH 1/3] [fix] fix logic of finding chlicked bar index indexSelected was updated to '-1' in the wrong timing. if event action is MotionEvent.ACTION_UP and r.contains is false, indexSelected is updated '-1'. so indexSelected can't hold correct value. --- .../com/echo/holographlibrary/BarGraph.java | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java b/HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java index e6662f6..59402d1 100644 --- a/HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java +++ b/HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java @@ -202,19 +202,24 @@ 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; + int count = 0; + for (int i = points.size() - 1; i >= 0; i--) { + Region r = new Region(); + r.setPath(points.get(i).getPath(), points.get(i).getRegion()); + if (r.contains(point.x, point.y)) { + indexSelected = i; } - indexSelected = -1; } - count++; + } + + if (event.getAction() == MotionEvent.ACTION_UP) { + // dispatch onClick event + if ((indexSelected != -1) && (listener != null)) { + listener.onClick(indexSelected); + } } if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP) { From 488c551525c3736512b589148afc074d27692518 Mon Sep 17 00:00:00 2001 From: fughz Date: Fri, 13 Mar 2015 18:27:41 +0900 Subject: [PATCH 2/3] [fix] fix to add path to wrong destination. Dawing path of BarGraph redundant because instance of variable "path" is one. so we should create variable "path" for each loop. --- HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java b/HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java index 59402d1..fa3d55b 100644 --- a/HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java +++ b/HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java @@ -39,7 +39,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,10 +131,9 @@ public void onDraw(Canvas ca) { r = new Rect(); - path.reset(); - int count = 0; for (Bar p : points) { + Path path = new Path(); if(p.getStackedBar()){ ArrayList values = new ArrayList(p.getStackedValues()); From 632eafe2c35ba7ab5eec8c40d731eec155142f1d Mon Sep 17 00:00:00 2001 From: fughz Date: Fri, 13 Mar 2015 18:48:37 +0900 Subject: [PATCH 3/3] [fix] support stacked bargraph click enable stacked bargraph click --- .../com/echo/holographlibrary/BarGraph.java | 43 ++++++++++++++----- .../holographlibrary/BarStackSegment.java | 18 ++++++++ 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java b/HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java index fa3d55b..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; @@ -135,25 +144,27 @@ public void onDraw(Canvas ca) { 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); @@ -203,12 +214,24 @@ public boolean onTouchEvent(@NotNull MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP) { // find clicked bar index indexSelected = -1; - int count = 0; - for (int i = points.size() - 1; i >= 0; i--) { + for (int i = 0; i < points.size(); i++) { Region r = new Region(); - r.setPath(points.get(i).getPath(), points.get(i).getRegion()); - if (r.contains(point.x, point.y)) { - indexSelected = i; + + 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; + } + } + } + else { + r.setPath(points.get(i).getPath(), points.get(i).getRegion()); + if (r.contains(point.x, point.y)) { + indexSelected = i; + } } } } 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; + } }