From 4d3c868ab1059007b8c36f49302763b11e88a2b4 Mon Sep 17 00:00:00 2001 From: Aaron Date: Sun, 19 Oct 2014 15:37:38 +0100 Subject: [PATCH] Added Stacked Bar Charts It is now possible to create stacked bar charts --- .../HoloGraphLibrary-HoloGraphLibrary.iml | 82 ++++++++++-------- HoloGraphLibrary/build.gradle | 4 +- .../src/com/echo/holographlibrary/Bar.java | 16 ++++ .../com/echo/holographlibrary/BarGraph.java | 36 ++++++-- .../holographlibrary/BarStackSegment.java | 13 +++ .../HoloGraphLibrarySample.iml | 84 ++++++++++--------- HoloGraphLibrarySample/build.gradle | 4 +- .../holographlibrarysample/BarFragment.java | 8 ++ 8 files changed, 162 insertions(+), 85 deletions(-) create mode 100644 HoloGraphLibrary/src/com/echo/holographlibrary/BarStackSegment.java diff --git a/HoloGraphLibrary/HoloGraphLibrary-HoloGraphLibrary.iml b/HoloGraphLibrary/HoloGraphLibrary-HoloGraphLibrary.iml index c049e37..9726a02 100644 --- a/HoloGraphLibrary/HoloGraphLibrary-HoloGraphLibrary.iml +++ b/HoloGraphLibrary/HoloGraphLibrary-HoloGraphLibrary.iml @@ -4,10 +4,11 @@ - + - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/HoloGraphLibrary/build.gradle b/HoloGraphLibrary/build.gradle index 6b23f06..eb51b8d 100644 --- a/HoloGraphLibrary/build.gradle +++ b/HoloGraphLibrary/build.gradle @@ -3,7 +3,7 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:0.7+' + classpath 'com.android.tools.build:gradle:0.12+' } } apply plugin: 'android-library' @@ -16,7 +16,7 @@ repositories { android { compileSdkVersion 19 - buildToolsVersion "18.1.1" + buildToolsVersion '19.1.0' defaultConfig { minSdkVersion 8 diff --git a/HoloGraphLibrary/src/com/echo/holographlibrary/Bar.java b/HoloGraphLibrary/src/com/echo/holographlibrary/Bar.java index 1e4b05d..7523287 100644 --- a/HoloGraphLibrary/src/com/echo/holographlibrary/Bar.java +++ b/HoloGraphLibrary/src/com/echo/holographlibrary/Bar.java @@ -26,6 +26,8 @@ import android.graphics.Path; import android.graphics.Region; +import java.util.ArrayList; + public class Bar { private int color; @@ -33,6 +35,8 @@ public class Bar { private float value; private Path path; private Region region; + private boolean isStackedBar; + private ArrayList values = new ArrayList(); public int getColor() { return color; @@ -64,5 +68,17 @@ public Region getRegion() { public void setRegion(Region region) { this.region = region; } + public void setStackedBar(boolean stacked){ + isStackedBar = stacked; + } + public boolean getStackedBar(){ + return isStackedBar; + } + public void AddStackValue(BarStackSegment val){ + values.add(val); + } + public ArrayList getStackedValues(){ + return values; + } } diff --git a/HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java b/HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java index f2a8750..a8621c9 100644 --- a/HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java +++ b/HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java @@ -33,6 +33,7 @@ import org.jetbrains.annotations.NotNull; import java.util.ArrayList; +import java.util.Collections; public class BarGraph extends View { @@ -129,15 +130,36 @@ public void onDraw(Canvas ca) { int count = 0; for (Bar p : points) { - 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); - p.setRegion(new Region(r.left - selectPadding, r.top - selectPadding, r.right + selectPadding, r.bottom + selectPadding)); + if(p.getStackedBar()){ + ArrayList values = new ArrayList(p.getStackedValues()); + int prevValue = 0; + for(BarStackSegment value : values) { + value.Value += prevValue; + prevValue += value.Value; + } + Collections.reverse(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)); + this.p.setColor(value.Color); + this.p.setAlpha(255); + canvas.drawRect(r, this.p); + } + }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); + p.setRegion(new Region(r.left - selectPadding, r.top - selectPadding, r.right + selectPadding, r.bottom + selectPadding)); + this.p.setColor(p.getColor()); + this.p.setAlpha(255); + canvas.drawRect(r, this.p); + } + - this.p.setColor(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); if (showBarText) { diff --git a/HoloGraphLibrary/src/com/echo/holographlibrary/BarStackSegment.java b/HoloGraphLibrary/src/com/echo/holographlibrary/BarStackSegment.java new file mode 100644 index 0000000..ce409eb --- /dev/null +++ b/HoloGraphLibrary/src/com/echo/holographlibrary/BarStackSegment.java @@ -0,0 +1,13 @@ +package com.echo.holographlibrary; + +/** + * Created by Aaron on 19/10/2014. + */ +public class BarStackSegment { + public int Value; + public int Color; + public BarStackSegment(int val, int color){ + Value = val; + Color = color; + } +} diff --git a/HoloGraphLibrarySample/HoloGraphLibrarySample.iml b/HoloGraphLibrarySample/HoloGraphLibrarySample.iml index b6e7888..5d501c7 100644 --- a/HoloGraphLibrarySample/HoloGraphLibrarySample.iml +++ b/HoloGraphLibrarySample/HoloGraphLibrarySample.iml @@ -8,6 +8,7 @@ - + - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - diff --git a/HoloGraphLibrarySample/build.gradle b/HoloGraphLibrarySample/build.gradle index 660f59d..66f0bd8 100644 --- a/HoloGraphLibrarySample/build.gradle +++ b/HoloGraphLibrarySample/build.gradle @@ -3,7 +3,7 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:0.7.+' + classpath 'com.android.tools.build:gradle:0.12.+' } } @@ -17,7 +17,7 @@ repositories { android { compileSdkVersion 19 - buildToolsVersion "18.1.1" + buildToolsVersion '19.1.0' defaultConfig { minSdkVersion 8 diff --git a/HoloGraphLibrarySample/src/com/echo/holographlibrarysample/BarFragment.java b/HoloGraphLibrarySample/src/com/echo/holographlibrarysample/BarFragment.java index ee52fc7..fe97892 100644 --- a/HoloGraphLibrarySample/src/com/echo/holographlibrarysample/BarFragment.java +++ b/HoloGraphLibrarySample/src/com/echo/holographlibrarysample/BarFragment.java @@ -33,6 +33,7 @@ import com.echo.holographlibrary.Bar; import com.echo.holographlibrary.BarGraph; import com.echo.holographlibrary.BarGraph.OnBarClickedListener; +import com.echo.holographlibrary.BarStackSegment; import java.util.ArrayList; @@ -52,8 +53,15 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa d2.setColor(Color.parseColor("#FFBB33")); d2.setName("Test2"); d2.setValue(20); + Bar d3 = new Bar(); + d3.setColor(Color.parseColor("#FFBB33")); + d3.setName("Test3"); + d3.setStackedBar(true); + d3.AddStackValue(new BarStackSegment(2, Color.parseColor("#FFBB33"))); + d3.AddStackValue(new BarStackSegment(4, Color.RED)); points.add(d); points.add(d2); + points.add(d3); BarGraph g = (BarGraph)v.findViewById(R.id.bargraph); assert g != null;