Skip to content
Merged
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
6 changes: 5 additions & 1 deletion .idea/copyright/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 28 additions & 10 deletions androidplot-core/src/main/java/com/androidplot/xy/StepModelFit.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import com.androidplot.Region;

import java.util.Arrays;

/**
* Created by psi on 23.02.2017.
*
* Subclass of StepModel that chooses from predefined step values
*
* Depending on the currently displayed range (by value) choose increment so that
* the number of lines is closest to StepModel.value
* Subclass of StepModel that chooses from predefined step values. Depending on the currently
* displayed range (by value) choose increment so that the number of lines
* is closest to StepModel.value
*/

public class StepModelFit extends StepModel {

private double[] steps; // list of steps to choose from
Expand All @@ -28,6 +26,16 @@ public double[] getSteps() {
}

public void setSteps(double[] steps) {

// sanity checks: no null, 0 or negative
if (steps == null || steps.length == 0)
return;

for (double step : steps) {
if (step <= 0.0d)
return;
}

this.steps = steps;
}

Expand All @@ -43,15 +51,16 @@ public void setScale(Region scale) {
@Override
public double getValue() {

// no possible increments where supplied (e.g. switched into this mode without calling setSteps(...)
if (steps == null)
// no possible increments where supplied
// or no region defined
if (steps == null || scale == null || !scale.isDefined())
return super.getValue();

double curStep = steps[0];

double oldDistance = Math.abs((scale.length().doubleValue() / curStep)-super.getValue() );

// determine which step size comes closest to the desired number of steps
// since steps[] is a small array brute force search is ok
for (double step : steps) {

double newDistance = Math.abs((scale.length().doubleValue() / step)-super.getValue() );
Expand All @@ -64,4 +73,13 @@ public double getValue() {
}
return curStep;
}

@Override
public String toString() {
return "StepModelFit{" +
"steps=" + Arrays.toString(steps) +
", scale=" + scale +
", current stepping=" + getValue() +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.androidplot.xy;

import com.androidplot.Region;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class StepModelFitTest {

Region regionSmall = new Region(0,11);
Region regionBig = new Region(-111,420);
Region regionZero = new Region(0, 0);
Region regionUndef = new Region(0, null);

double[] stpSmall = {1,2,5}, stpBig = {1,10,100}, nonsense = {0};

@Before
public void setUp() throws Exception {

}

@After
public void tearDown() throws Exception {

}

@Test
public void getValue() throws Exception {

StepModelFit model = new StepModelFit(regionSmall,stpSmall,3);

assertEquals(5.0, model.getValue(), 0.0);
model.setValue(5.0);
assertEquals(2.0, model.getValue(), 0.0);
model.setValue(7.0);
assertEquals(2.0, model.getValue(), 0.0);

model.setSteps(stpBig);
assertEquals(1.0, model.getValue(), 0.0);

model.setScale(regionBig);
assertEquals(100.0, model.getValue(), 0.0);
model.setValue(1000.0);
assertEquals(1.0, model.getValue(), 0.0);

// bad parameters
model.setSteps(nonsense);
assertArrayEquals(stpBig,model.getSteps(), 0.0);

model.setScale(regionZero);
assertEquals(stpBig[0], model.getValue(), 0.0);

model.setScale(regionUndef);
model.setValue(1.1);
assertEquals(1.1, model.getValue(), 0.0);
}

}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
classpath 'com.vanniktech:gradle-android-junit-jacoco-plugin:0.5.0'
Expand Down
2 changes: 1 addition & 1 deletion demoapp-wearable/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
apply plugin: 'com.android.application'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
public class TouchZoomExampleActivity extends Activity {
private static final int SERIES_SIZE = 3000;
private static final int SERIES_ALPHA = 255;
private static final int NUM_GRIDLINES = 5;
private XYPlot plot;
private PanZoom panZoom;
private Button resetButton;
Expand All @@ -55,11 +56,11 @@ public void onClick(View view) {
plot.setUserRangeOrigin(0);

// predefine the stepping of both axis
// increment will be chosen from list to best fit 5 grid lines
// increment will be chosen from list to best fit NUM_GRIDLINES grid lines
double[] inc_domain = new double[]{10,50,100,500};
double[] inc_range = new double[]{1,5,10,20,50,100};
plot.setDomainStepModel(new StepModelFit(plot.getBounds().getxRegion(),inc_domain,5));
plot.setRangeStepModel( new StepModelFit(plot.getBounds().getyRegion(),inc_range,5));
plot.setDomainStepModel(new StepModelFit(plot.getBounds().getxRegion(),inc_domain,NUM_GRIDLINES));
plot.setRangeStepModel( new StepModelFit(plot.getBounds().getyRegion(),inc_range,NUM_GRIDLINES));


panSpinner = (Spinner) findViewById(R.id.pan_spinner);
Expand Down