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

Allow for animated horizontal viewport resizing when chart data increases or decreases #453

Open
wants to merge 2 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
14 changes: 13 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion hellocharts-library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -60,6 +60,7 @@ android {
abortOnError false
}

buildToolsVersion '26.0.2'
}

//Execute "gradle clean jarRelease" to cook jar.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public ColumnChartData() {

public ColumnChartData(List<Column> columns) {
setColumns(columns);
setLastDataSize();
}

/**
Expand All @@ -35,6 +36,7 @@ public ColumnChartData(ColumnChartData data) {
for (Column column : data.columns) {
this.columns.add(new Column(column));
}
setLastDataSize();
}

public static ColumnChartData generateDummyData() {
Expand Down Expand Up @@ -67,6 +69,7 @@ public void finish() {
for (Column column : columns) {
column.finish();
}
super.finish();
}

public List<Column> getColumns() {
Expand All @@ -79,6 +82,7 @@ public ColumnChartData setColumns(List<Column> columns) {
} else {
this.columns = columns;
}
setLastDataSize();
return this;
}

Expand Down Expand Up @@ -134,4 +138,12 @@ public ColumnChartData setBaseValue(float baseValue) {
return this;
}

public void setLastDataSize() {
lastDataSize = columns.size();
}

public int setNewDataSize() {
return newDataSize = columns.size();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public LineChartData() {

public LineChartData(List<Line> lines) {
setLines(lines);
setLastDataSize();
}

/**
Expand All @@ -30,6 +31,7 @@ public LineChartData(LineChartData data) {
for (Line line : data.lines) {
this.lines.add(new Line(line));
}
setLastDataSize();
}

public static LineChartData generateDummyData() {
Expand Down Expand Up @@ -59,6 +61,7 @@ public void finish() {
for (Line line : lines) {
line.finish();
}
super.finish();
}

public List<Line> getLines() {
Expand All @@ -71,6 +74,7 @@ public LineChartData setLines(List<Line> lines) {
} else {
this.lines = lines;
}
setLastDataSize();
return this;
}

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public interface ChartRenderer {

public void onChartViewportChanged();

public void onChartViewportChanged(float scale);

public void resetRenderer();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public void cancelDataAnimation() {
@Override
public void animationDataUpdate(float scale) {
getChartData().update(scale);
chartRenderer.onChartViewportChanged();
chartRenderer.onChartViewportChanged(scale);
ViewCompat.postInvalidateOnAnimation(this);
}

Expand Down
20 changes: 11 additions & 9 deletions hellocharts-samples/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down Expand Up @@ -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'
}