From 61f40847cbedcba6594d063efd0abad6094c1827 Mon Sep 17 00:00:00 2001 From: Robert Jones II Date: Tue, 24 Oct 2017 19:33:37 -0400 Subject: [PATCH 1/2] change code to support the horizontal animation of the viewport on the addition or subtraction of point and column data --- .../hellocharts/model/AbstractChartData.java | 14 ++++++ .../hellocharts/model/ColumnChartData.java | 12 ++++++ .../lib/hellocharts/model/LineChartData.java | 20 +++++++++ .../renderer/BubbleChartRenderer.java | 5 +++ .../hellocharts/renderer/ChartRenderer.java | 2 + .../renderer/ColumnChartRenderer.java | 13 ++++-- .../renderer/ComboChartRenderer.java | 7 ++- .../renderer/LineChartRenderer.java | 43 +++++++++++-------- .../renderer/PieChartRenderer.java | 5 +++ .../hellocharts/view/AbstractChartView.java | 2 +- 10 files changed, 100 insertions(+), 23 deletions(-) diff --git a/hellocharts-library/src/lecho/lib/hellocharts/model/AbstractChartData.java b/hellocharts-library/src/lecho/lib/hellocharts/model/AbstractChartData.java index a3575a7a..6c07fc2a 100644 --- a/hellocharts-library/src/lecho/lib/hellocharts/model/AbstractChartData.java +++ b/hellocharts-library/src/lecho/lib/hellocharts/model/AbstractChartData.java @@ -18,6 +18,8 @@ public abstract class AbstractChartData implements ChartData { protected int valueLabelTextSize = DEFAULT_TEXT_SIZE_SP; protected Typeface valueLabelTypeface; + protected int lastDataSize, newDataSize; + /** * If true each value label will have background rectangle */ @@ -163,4 +165,16 @@ public void setValueLabelBackgroundColor(int valueLabelBackgroundColor) { this.valueLabelBackgroundColor = valueLabelBackgroundColor; } + public int getLastDataSize() { + return lastDataSize; + } + + public int getNewDataSize() { + return newDataSize; + } + + public void finish() { + lastDataSize = newDataSize; + } + } \ No newline at end of file diff --git a/hellocharts-library/src/lecho/lib/hellocharts/model/ColumnChartData.java b/hellocharts-library/src/lecho/lib/hellocharts/model/ColumnChartData.java index 42c11be0..60a4e758 100644 --- a/hellocharts-library/src/lecho/lib/hellocharts/model/ColumnChartData.java +++ b/hellocharts-library/src/lecho/lib/hellocharts/model/ColumnChartData.java @@ -22,6 +22,7 @@ public ColumnChartData() { public ColumnChartData(List columns) { setColumns(columns); + setLastDataSize(); } /** @@ -35,6 +36,7 @@ public ColumnChartData(ColumnChartData data) { for (Column column : data.columns) { this.columns.add(new Column(column)); } + setLastDataSize(); } public static ColumnChartData generateDummyData() { @@ -67,6 +69,7 @@ public void finish() { for (Column column : columns) { column.finish(); } + super.finish(); } public List getColumns() { @@ -79,6 +82,7 @@ public ColumnChartData setColumns(List columns) { } else { this.columns = columns; } + setLastDataSize(); return this; } @@ -134,4 +138,12 @@ public ColumnChartData setBaseValue(float baseValue) { return this; } + public void setLastDataSize() { + lastDataSize = columns.size(); + } + + public int setNewDataSize() { + return newDataSize = columns.size(); + } + } diff --git a/hellocharts-library/src/lecho/lib/hellocharts/model/LineChartData.java b/hellocharts-library/src/lecho/lib/hellocharts/model/LineChartData.java index df35b3e8..e102db95 100644 --- a/hellocharts-library/src/lecho/lib/hellocharts/model/LineChartData.java +++ b/hellocharts-library/src/lecho/lib/hellocharts/model/LineChartData.java @@ -18,6 +18,7 @@ public LineChartData() { public LineChartData(List lines) { setLines(lines); + setLastDataSize(); } /** @@ -30,6 +31,7 @@ public LineChartData(LineChartData data) { for (Line line : data.lines) { this.lines.add(new Line(line)); } + setLastDataSize(); } public static LineChartData generateDummyData() { @@ -59,6 +61,7 @@ public void finish() { for (Line line : lines) { line.finish(); } + super.finish(); } public List getLines() { @@ -71,6 +74,7 @@ public LineChartData setLines(List lines) { } else { this.lines = lines; } + setLastDataSize(); return this; } @@ -89,4 +93,20 @@ public LineChartData setBaseValue(float baseValue) { this.baseValue = baseValue; return this; } + + public void setLastDataSize() { + int size = 0; + for (Line line : lines) { + size = line.getValues().size() > size ? line.getValues().size() : size; + } + lastDataSize = size; + } + + public void setNewDataSize() { + int size = 0; + for (Line line : lines) { + size = line.getValues().size() > size ? line.getValues().size() : size; + } + newDataSize = size; + } } diff --git a/hellocharts-library/src/lecho/lib/hellocharts/renderer/BubbleChartRenderer.java b/hellocharts-library/src/lecho/lib/hellocharts/renderer/BubbleChartRenderer.java index 4946178d..06953d3e 100644 --- a/hellocharts-library/src/lecho/lib/hellocharts/renderer/BubbleChartRenderer.java +++ b/hellocharts-library/src/lecho/lib/hellocharts/renderer/BubbleChartRenderer.java @@ -100,6 +100,11 @@ public void onChartDataChanged() { @Override public void onChartViewportChanged() { + onChartViewportChanged(1); + } + + @Override + public void onChartViewportChanged(float scale) { if (isViewportCalculationEnabled) { calculateMaxViewport(); computator.setMaxViewport(tempMaximumViewport); diff --git a/hellocharts-library/src/lecho/lib/hellocharts/renderer/ChartRenderer.java b/hellocharts-library/src/lecho/lib/hellocharts/renderer/ChartRenderer.java index 1de14e75..b7fb57ec 100644 --- a/hellocharts-library/src/lecho/lib/hellocharts/renderer/ChartRenderer.java +++ b/hellocharts-library/src/lecho/lib/hellocharts/renderer/ChartRenderer.java @@ -16,6 +16,8 @@ public interface ChartRenderer { public void onChartViewportChanged(); + public void onChartViewportChanged(float scale); + public void resetRenderer(); /** diff --git a/hellocharts-library/src/lecho/lib/hellocharts/renderer/ColumnChartRenderer.java b/hellocharts-library/src/lecho/lib/hellocharts/renderer/ColumnChartRenderer.java index da677138..776f2f5b 100644 --- a/hellocharts-library/src/lecho/lib/hellocharts/renderer/ColumnChartRenderer.java +++ b/hellocharts-library/src/lecho/lib/hellocharts/renderer/ColumnChartRenderer.java @@ -87,8 +87,13 @@ public void onChartDataChanged() { @Override public void onChartViewportChanged() { + onChartViewportChanged(1); + } + + @Override + public void onChartViewportChanged(float scale) { if (isViewportCalculationEnabled) { - calculateMaxViewport(); + calculateMaxViewport(scale); computator.setMaxViewport(tempMaximumViewport); computator.setCurrentViewport(computator.getMaximumViewport()); } @@ -125,12 +130,14 @@ public boolean checkTouch(float touchX, float touchY) { return isTouched(); } - private void calculateMaxViewport() { + private void calculateMaxViewport(float scale) { final ColumnChartData data = dataProvider.getColumnChartData(); + int diff = data.setNewDataSize() - data.getLastDataSize(); + float right = (data.getNewDataSize() - diff) + diff * scale - 0.5f; // Column chart always has X values from 0 to numColumns-1, to add some margin on the left and right I added // extra 0.5 to the each side, that margins will be negative scaled according to number of columns, so for more // columns there will be less margin. - tempMaximumViewport.set(-0.5f, baseValue, data.getColumns().size() - 0.5f, baseValue); + tempMaximumViewport.set(-0.5f, baseValue, right, baseValue); if (data.isStacked()) { calculateMaxViewportForStacked(data); } else { diff --git a/hellocharts-library/src/lecho/lib/hellocharts/renderer/ComboChartRenderer.java b/hellocharts-library/src/lecho/lib/hellocharts/renderer/ComboChartRenderer.java index 5cb4153f..ba35f369 100644 --- a/hellocharts-library/src/lecho/lib/hellocharts/renderer/ComboChartRenderer.java +++ b/hellocharts-library/src/lecho/lib/hellocharts/renderer/ComboChartRenderer.java @@ -37,10 +37,15 @@ public void onChartDataChanged() { @Override public void onChartViewportChanged() { + onChartViewportChanged(1); + } + + @Override + public void onChartViewportChanged(float scale) { if (isViewportCalculationEnabled) { int rendererIndex = 0; for (ChartRenderer renderer : renderers) { - renderer.onChartViewportChanged(); + renderer.onChartViewportChanged(scale); if (rendererIndex == 0) { unionViewport.set(renderer.getMaximumViewport()); } else { diff --git a/hellocharts-library/src/lecho/lib/hellocharts/renderer/LineChartRenderer.java b/hellocharts-library/src/lecho/lib/hellocharts/renderer/LineChartRenderer.java index b86118eb..1b7a1762 100644 --- a/hellocharts-library/src/lecho/lib/hellocharts/renderer/LineChartRenderer.java +++ b/hellocharts-library/src/lecho/lib/hellocharts/renderer/LineChartRenderer.java @@ -88,10 +88,14 @@ public void onChartDataChanged() { onChartViewportChanged(); } - @Override public void onChartViewportChanged() { + onChartViewportChanged(1); + } + + @Override + public void onChartViewportChanged(float scale) { if (isViewportCalculationEnabled) { - calculateMaxViewport(); + calculateMaxViewport(scale); computator.setMaxViewport(tempMaximumViewport); computator.setCurrentViewport(computator.getMaximumViewport()); } @@ -172,28 +176,31 @@ public boolean checkTouch(float touchX, float touchY) { return isTouched(); } - private void calculateMaxViewport() { - tempMaximumViewport.set(Float.MAX_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MAX_VALUE); + /** + by setting the tempMaximumViewport after the new points of the viewport are calculated, + it doesn't cause the small movement in the viewport directly before animating the horizontal axis + */ + private void calculateMaxViewport(float scale) { LineChartData data = dataProvider.getLineChartData(); - + float maxX = Float.MIN_VALUE, maxY = Float.MIN_VALUE, minX = Float.MAX_VALUE, minY = Float.MAX_VALUE; for (Line line : data.getLines()) { // Calculate max and min for viewport. for (PointValue pointValue : line.getValues()) { - if (pointValue.getX() < tempMaximumViewport.left) { - tempMaximumViewport.left = pointValue.getX(); - } - if (pointValue.getX() > tempMaximumViewport.right) { - tempMaximumViewport.right = pointValue.getX(); - } - if (pointValue.getY() < tempMaximumViewport.bottom) { - tempMaximumViewport.bottom = pointValue.getY(); - } - if (pointValue.getY() > tempMaximumViewport.top) { - tempMaximumViewport.top = pointValue.getY(); - } - + maxX = pointValue.getX() > maxX ? pointValue.getX() : maxX; + maxY = pointValue.getY() > maxY ? pointValue.getY() : maxY; + minX = pointValue.getX() < minX ? pointValue.getX() : minX; + minY = pointValue.getY() < minY ? pointValue.getY() : minY; } } + tempMaximumViewport.set(minX, maxY, maxX, minY); + data.setNewDataSize(); + int diff = data.getNewDataSize() - data.getLastDataSize(); + float scaleFactor; + if (diff != 0 ) { + scaleFactor = diff > 0 ? 1.1f : 1.0f; + tempMaximumViewport.right -= diff; + tempMaximumViewport.right += diff * scaleFactor * scale; + } } private int calculateContentRectInternalMargin() { diff --git a/hellocharts-library/src/lecho/lib/hellocharts/renderer/PieChartRenderer.java b/hellocharts-library/src/lecho/lib/hellocharts/renderer/PieChartRenderer.java index 17d1f862..9d7ecf74 100644 --- a/hellocharts-library/src/lecho/lib/hellocharts/renderer/PieChartRenderer.java +++ b/hellocharts-library/src/lecho/lib/hellocharts/renderer/PieChartRenderer.java @@ -133,6 +133,11 @@ public void onChartDataChanged() { @Override public void onChartViewportChanged() { + onChartViewportChanged(1); + } + + @Override + public void onChartViewportChanged(float scale) { if (isViewportCalculationEnabled) { calculateMaxViewport(); computator.setMaxViewport(tempMaximumViewport); diff --git a/hellocharts-library/src/lecho/lib/hellocharts/view/AbstractChartView.java b/hellocharts-library/src/lecho/lib/hellocharts/view/AbstractChartView.java index f0bb9af1..57ae776c 100644 --- a/hellocharts-library/src/lecho/lib/hellocharts/view/AbstractChartView.java +++ b/hellocharts-library/src/lecho/lib/hellocharts/view/AbstractChartView.java @@ -149,7 +149,7 @@ public void cancelDataAnimation() { @Override public void animationDataUpdate(float scale) { getChartData().update(scale); - chartRenderer.onChartViewportChanged(); + chartRenderer.onChartViewportChanged(scale); ViewCompat.postInvalidateOnAnimation(this); } From f5eef22ede9884d6e947f12593dbc3d7c9cb58d0 Mon Sep 17 00:00:00 2001 From: Robert Jones II Date: Sun, 5 Nov 2017 15:22:32 -0500 Subject: [PATCH 2/2] update to use in Android Studio v3 --- build.gradle | 14 +++++++++++++- gradle/wrapper/gradle-wrapper.properties | 4 ++-- hellocharts-library/build.gradle | 3 ++- hellocharts-samples/build.gradle | 20 +++++++++++--------- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/build.gradle b/build.gradle index b4d37b7b..ce71b45b 100644 --- a/build.gradle +++ b/build.gradle @@ -2,8 +2,20 @@ buildscript { repositories { mavenCentral() + maven { + url 'https://maven.google.com/' + name 'Google' + } + google() } dependencies { - classpath 'com.android.tools.build:gradle:2.1.0' + classpath 'com.android.tools.build:gradle:3.0.0' } } + +repositories { + maven { + url 'https://maven.google.com/' + name 'Google' + } +} \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 0c44860e..90fcf3f4 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Apr 10 15:27:10 PDT 2013 +#Sun Nov 05 15:03:49 EST 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=http\://services.gradle.org/distributions/gradle-2.2.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip diff --git a/hellocharts-library/build.gradle b/hellocharts-library/build.gradle index acd91eac..c9e7bc76 100644 --- a/hellocharts-library/build.gradle +++ b/hellocharts-library/build.gradle @@ -4,7 +4,7 @@ apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn //To upload to maven central //http://zserge.com/blog/gradle-maven-publish.html -//gradle uploadArchives +// gradle uploadArchives buildscript { repositories { @@ -60,6 +60,7 @@ android { abortOnError false } + buildToolsVersion '26.0.2' } //Execute "gradle clean jarRelease" to cook jar. diff --git a/hellocharts-samples/build.gradle b/hellocharts-samples/build.gradle index 7d298448..6ffeb9b9 100644 --- a/hellocharts-samples/build.gradle +++ b/hellocharts-samples/build.gradle @@ -3,6 +3,7 @@ apply plugin: 'com.android.application' dependencies { //compile fileTree(dir: 'libs', include: '*.jar') compile project(':hellocharts-library') + //noinspection GradleCompatible compile 'com.android.support:support-v4:23.3.0' compile 'com.android.support:appcompat-v7:23.3.0' } @@ -34,15 +35,16 @@ android { assets.srcDirs = ['assets'] } } - - buildTypes { - release { + + buildTypes { + release { minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' - } - } + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } - lintOptions { - abortOnError false - } + lintOptions { + abortOnError false + } + buildToolsVersion '26.0.2' }