graph improvements
This commit is contained in:
parent
d1677c97e6
commit
6a4d990d9c
@ -15,12 +15,9 @@ import com.actionbarsherlock.app.SherlockFragmentActivity;
|
|||||||
import com.actionbarsherlock.view.Menu;
|
import com.actionbarsherlock.view.Menu;
|
||||||
import com.actionbarsherlock.view.MenuInflater;
|
import com.actionbarsherlock.view.MenuInflater;
|
||||||
import com.actionbarsherlock.view.MenuItem;
|
import com.actionbarsherlock.view.MenuItem;
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
import javax.annotation.Nullable;
|
|
||||||
import org.solovyev.android.Android;
|
import org.solovyev.android.Android;
|
||||||
import org.solovyev.android.Threads;
|
import org.solovyev.android.Threads;
|
||||||
import org.solovyev.android.calculator.*;
|
import org.solovyev.android.calculator.*;
|
||||||
import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
|
|
||||||
import org.solovyev.android.menu.AMenuItem;
|
import org.solovyev.android.menu.AMenuItem;
|
||||||
import org.solovyev.android.menu.ActivityMenu;
|
import org.solovyev.android.menu.ActivityMenu;
|
||||||
import org.solovyev.android.menu.IdentifiableMenuItem;
|
import org.solovyev.android.menu.IdentifiableMenuItem;
|
||||||
@ -29,6 +26,8 @@ import org.solovyev.android.sherlock.menu.SherlockMenuHelper;
|
|||||||
import org.solovyev.common.JPredicate;
|
import org.solovyev.common.JPredicate;
|
||||||
import org.solovyev.common.msg.MessageType;
|
import org.solovyev.common.msg.MessageType;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -38,6 +37,8 @@ import java.util.List;
|
|||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User: serso
|
* User: serso
|
||||||
* Date: 12/30/12
|
* Date: 12/30/12
|
||||||
@ -66,7 +67,7 @@ public abstract class AbstractCalculatorPlotFragment extends CalculatorFragment
|
|||||||
private int bgColor;
|
private int bgColor;
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private PlotData plotData = new PlotData(Collections.<PlotFunction>emptyList(), false, PlotBoundaries.newDefaultInstance());
|
private PlotData plotData = new PlotData(Collections.<PlotFunction>emptyList(), false, true, PlotBoundaries.newDefaultInstance());
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private ActivityMenu<Menu, MenuItem> fragmentMenu;
|
private ActivityMenu<Menu, MenuItem> fragmentMenu;
|
||||||
@ -107,15 +108,6 @@ public abstract class AbstractCalculatorPlotFragment extends CalculatorFragment
|
|||||||
@Nullable
|
@Nullable
|
||||||
protected abstract PlotBoundaries getPlotBoundaries();
|
protected abstract PlotBoundaries getPlotBoundaries();
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPause() {
|
|
||||||
PreferenceManager.getDefaultSharedPreferences(this.getActivity()).unregisterOnSharedPreferenceChangeListener(this);
|
|
||||||
|
|
||||||
savePlotBoundaries();
|
|
||||||
|
|
||||||
super.onPause();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
@ -126,9 +118,18 @@ public abstract class AbstractCalculatorPlotFragment extends CalculatorFragment
|
|||||||
updateChart(plotData, getSherlockActivity());
|
updateChart(plotData, getSherlockActivity());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPause() {
|
||||||
|
PreferenceManager.getDefaultSharedPreferences(this.getActivity()).unregisterOnSharedPreferenceChangeListener(this);
|
||||||
|
|
||||||
|
savePlotBoundaries();
|
||||||
|
|
||||||
|
super.onPause();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||||
if (AndroidCalculatorEngine.Preferences.angleUnit.getKey().equals(key)) {
|
if (Preferences.angleUnit.getKey().equals(key)) {
|
||||||
updateChart(this.plotData, getSherlockActivity());
|
updateChart(this.plotData, getSherlockActivity());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -139,6 +140,7 @@ public abstract class AbstractCalculatorPlotFragment extends CalculatorFragment
|
|||||||
case plot_data_changed:
|
case plot_data_changed:
|
||||||
final CalculatorEventHolder.Result result = this.lastEventHolder.apply(calculatorEventData);
|
final CalculatorEventHolder.Result result = this.lastEventHolder.apply(calculatorEventData);
|
||||||
if (result.isNewAfter()) {
|
if (result.isNewAfter()) {
|
||||||
|
assert data != null;
|
||||||
onNewPlotData((PlotData) data);
|
onNewPlotData((PlotData) data);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -164,6 +166,7 @@ public abstract class AbstractCalculatorPlotFragment extends CalculatorFragment
|
|||||||
createGraphicalView(view, plotData);
|
createGraphicalView(view, plotData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert activity != null;
|
||||||
activity.invalidateOptionsMenu();
|
activity.invalidateOptionsMenu();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -8,9 +8,9 @@ import android.view.MotionEvent;
|
|||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Scroller;
|
import android.widget.Scroller;
|
||||||
import android.widget.ZoomButtonsController;
|
import android.widget.ZoomButtonsController;
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
import org.solovyev.common.math.Point2d;
|
import org.solovyev.common.math.Point2d;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -18,17 +18,19 @@ import java.util.List;
|
|||||||
public class CalculatorGraph2dView extends View implements GraphView {
|
public class CalculatorGraph2dView extends View implements GraphView {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
**********************************************************************
|
**********************************************************************
|
||||||
*
|
*
|
||||||
* CONSTANTS
|
* CONSTANTS
|
||||||
*
|
*
|
||||||
**********************************************************************
|
**********************************************************************
|
||||||
*/
|
*/
|
||||||
private static final int NO_TOUCH = -1;
|
private static final int NO_TOUCH = -1;
|
||||||
|
|
||||||
private static final float TICKS_COUNT = 15;
|
private static final float TICKS_COUNT = 15;
|
||||||
public static final int TICK_SIZE_PXS = 3;
|
public static final int TICK_SIZE_PXS = 3;
|
||||||
|
|
||||||
|
private static final float Y_TO_X_ADJUST_SCALE = 2f;
|
||||||
|
|
||||||
private static final DecimalFormat tickFormat = new DecimalFormat("##0.#####E0");
|
private static final DecimalFormat tickFormat = new DecimalFormat("##0.#####E0");
|
||||||
private static final int MAX_TICK_DIGITS = 4;
|
private static final int MAX_TICK_DIGITS = 4;
|
||||||
private static final String[] TICK_FORMATS = new String[MAX_TICK_DIGITS];
|
private static final String[] TICK_FORMATS = new String[MAX_TICK_DIGITS];
|
||||||
@ -38,6 +40,7 @@ public class CalculatorGraph2dView extends View implements GraphView {
|
|||||||
TICK_FORMATS[i] = "%." + i + "f";
|
TICK_FORMATS[i] = "%." + i + "f";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
**********************************************************************
|
**********************************************************************
|
||||||
*
|
*
|
||||||
@ -83,7 +86,9 @@ public class CalculatorGraph2dView extends View implements GraphView {
|
|||||||
@Nonnull
|
@Nonnull
|
||||||
private final GraphCalculator graphCalculator = new GraphCalculatorImpl();
|
private final GraphCalculator graphCalculator = new GraphCalculatorImpl();
|
||||||
|
|
||||||
private boolean mDrawn = false;
|
private boolean drawn = false;
|
||||||
|
|
||||||
|
private boolean adjustYAxis = false;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
**********************************************************************
|
**********************************************************************
|
||||||
@ -162,12 +167,17 @@ public class CalculatorGraph2dView extends View implements GraphView {
|
|||||||
public void invalidateGraphs() {
|
public void invalidateGraphs() {
|
||||||
graphsData.clear();
|
graphsData.clear();
|
||||||
|
|
||||||
if (mDrawn) {
|
if (drawn) {
|
||||||
mDrawn = false;
|
drawn = false;
|
||||||
invalidate();
|
invalidate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setAdjustYAxis(boolean adjustYAxis) {
|
||||||
|
this.adjustYAxis = adjustYAxis;
|
||||||
|
}
|
||||||
|
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,7 +238,12 @@ public class CalculatorGraph2dView extends View implements GraphView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void drawGraph(@Nonnull Canvas canvas) {
|
private void drawGraph(@Nonnull Canvas canvas) {
|
||||||
mDrawn = true;
|
drawn = true;
|
||||||
|
|
||||||
|
if(adjustYAxis) {
|
||||||
|
adjustYAxis();
|
||||||
|
adjustYAxis = false;
|
||||||
|
}
|
||||||
|
|
||||||
final float graphWidth = dimensions.getGWidth();
|
final float graphWidth = dimensions.getGWidth();
|
||||||
final float graphHeight = dimensions.getGHeight();
|
final float graphHeight = dimensions.getGHeight();
|
||||||
@ -300,6 +315,51 @@ public class CalculatorGraph2dView extends View implements GraphView {
|
|||||||
graphsData.setLastXMax(xMax);
|
graphsData.setLastXMax(xMax);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void adjustYAxis() {
|
||||||
|
final float xMin = dimensions.getXMin();
|
||||||
|
final float xMax = dimensions.getXMax(xMin);
|
||||||
|
|
||||||
|
float yMax = -Float.MAX_VALUE;
|
||||||
|
float yMin = Float.MAX_VALUE;
|
||||||
|
|
||||||
|
graphsData.checkBoundaries(Float.MAX_VALUE, -Float.MAX_VALUE, Float.MAX_VALUE);
|
||||||
|
|
||||||
|
final List<PlotFunction> plotFunctions = graphViewHelper.getPlotFunctions();
|
||||||
|
|
||||||
|
for (int i = 0; i < plotFunctions.size(); i++) {
|
||||||
|
final PlotFunction plotFunction = plotFunctions.get(i);
|
||||||
|
final GraphData graph = graphsData.get(i);
|
||||||
|
|
||||||
|
graphCalculator.computeGraph(plotFunction.getXyFunction(), xMin, xMax, graph, graphsData, dimensions);
|
||||||
|
|
||||||
|
final float[] ys = graph.getYs();
|
||||||
|
for (int j = 0; j < graph.getSize(); j++) {
|
||||||
|
final float y = ys[j];
|
||||||
|
if (!Float.isNaN(y)) {
|
||||||
|
yMax = Math.max(yMax, y);
|
||||||
|
yMin = Math.min(yMin, y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final float xDist = xMax - xMin;
|
||||||
|
|
||||||
|
yMax = Math.min(yMax, xDist);
|
||||||
|
yMin = Math.max(yMin, -xDist);
|
||||||
|
|
||||||
|
if (yMax - yMin > 0.00000001) {
|
||||||
|
final float yDist = yMax - yMin;
|
||||||
|
float maxYDist = Y_TO_X_ADJUST_SCALE * xDist;
|
||||||
|
if (yDist > maxYDist) {
|
||||||
|
// usually functions are symmetrical => just make a symmetry
|
||||||
|
yMax = yMax - yDist / 2 + maxYDist / 2;
|
||||||
|
yMin = yMin + yDist / 2 - maxYDist / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
dimensions.setYRange(yMin, yMax);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private TickDigits drawGridAndAxis(@Nonnull Canvas canvas) {
|
private TickDigits drawGridAndAxis(@Nonnull Canvas canvas) {
|
||||||
final TickDigits result = new TickDigits();
|
final TickDigits result = new TickDigits();
|
||||||
|
@ -203,6 +203,11 @@ public class CalculatorGraph3dView extends GLView implements GraphView {
|
|||||||
//To change body of implemented methods use File | Settings | File Templates.
|
//To change body of implemented methods use File | Settings | File Templates.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setAdjustYAxis(boolean adjustYAxis) {
|
||||||
|
// not supported
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSurfaceCreated(GL10 gl, int width, int height) {
|
public void onSurfaceCreated(GL10 gl, int width, int height) {
|
||||||
gl.glDisable(GL10.GL_DITHER);
|
gl.glDisable(GL10.GL_DITHER);
|
||||||
|
@ -4,9 +4,10 @@ import android.graphics.Bitmap;
|
|||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
import org.solovyev.android.calculator.R;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import org.solovyev.android.calculator.R;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* User: serso
|
* User: serso
|
||||||
@ -50,6 +51,7 @@ public class CalculatorPlotFragment extends AbstractCalculatorPlotFragment {
|
|||||||
final PlotBoundaries boundaries = plotData.getBoundaries();
|
final PlotBoundaries boundaries = plotData.getBoundaries();
|
||||||
graphView.setXRange(boundaries.getXMin(), boundaries.getXMax());
|
graphView.setXRange(boundaries.getXMin(), boundaries.getXMax());
|
||||||
graphView.setYRange(boundaries.getYMin(), boundaries.getYMax());
|
graphView.setYRange(boundaries.getYMin(), boundaries.getYMax());
|
||||||
|
graphView.setAdjustYAxis(plotData.isAdjustYAxis());
|
||||||
|
|
||||||
graphView.setPlotFunctions(plotData.getFunctions());
|
graphView.setPlotFunctions(plotData.getFunctions());
|
||||||
|
|
||||||
|
@ -38,17 +38,5 @@ public interface GraphView extends ZoomButtonsController.OnZoomListener, TouchHa
|
|||||||
|
|
||||||
void invalidateGraphs();
|
void invalidateGraphs();
|
||||||
|
|
||||||
/* void increaseDensity();
|
void setAdjustYAxis(boolean adjustYAxis);
|
||||||
void decreaseDensity();*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
**********************************************************************
|
|
||||||
*
|
|
||||||
* CUSTOMIZATION
|
|
||||||
*
|
|
||||||
**********************************************************************
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* void setBgColor(int color);
|
|
||||||
void setAxisColor(int color);*/
|
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,7 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
|
|||||||
private final PlotResourceManager resourceManager = new MapPlotResourceManager();
|
private final PlotResourceManager resourceManager = new MapPlotResourceManager();
|
||||||
|
|
||||||
private boolean plot3d = false;
|
private boolean plot3d = false;
|
||||||
|
private boolean adjustYAxis = true;
|
||||||
|
|
||||||
private boolean plotImag = false;
|
private boolean plotImag = false;
|
||||||
|
|
||||||
@ -40,7 +41,7 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
|
|||||||
private PlotBoundaries plotBoundaries = PlotBoundaries.newDefaultInstance();
|
private PlotBoundaries plotBoundaries = PlotBoundaries.newDefaultInstance();
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private PlotData plotData = new PlotData(Collections.<PlotFunction>emptyList(), plot3d, plotBoundaries);
|
private PlotData plotData = new PlotData(Collections.<PlotFunction>emptyList(), plot3d, true, plotBoundaries);
|
||||||
|
|
||||||
public CalculatorPlotterImpl(@Nonnull Calculator calculator) {
|
public CalculatorPlotterImpl(@Nonnull Calculator calculator) {
|
||||||
this.calculator = calculator;
|
this.calculator = calculator;
|
||||||
@ -308,6 +309,7 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
|
|||||||
if (functions.isEmpty()) {
|
if (functions.isEmpty()) {
|
||||||
// no functions => new plot => default boundaries
|
// no functions => new plot => default boundaries
|
||||||
this.plotBoundaries = PlotBoundaries.newDefaultInstance();
|
this.plotBoundaries = PlotBoundaries.newDefaultInstance();
|
||||||
|
this.adjustYAxis = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
arity = maxArity;
|
arity = maxArity;
|
||||||
@ -378,7 +380,7 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void updatePlotData() {
|
private void updatePlotData() {
|
||||||
plotData = new PlotData(getVisibleFunctions(), plot3d, plotBoundaries);
|
plotData = new PlotData(getVisibleFunctions(), plot3d, adjustYAxis, plotBoundaries);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -395,6 +397,7 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
|
|||||||
public void savePlotBoundaries(@Nonnull PlotBoundaries plotBoundaries) {
|
public void savePlotBoundaries(@Nonnull PlotBoundaries plotBoundaries) {
|
||||||
if (!this.plotBoundaries.equals(plotBoundaries)) {
|
if (!this.plotBoundaries.equals(plotBoundaries)) {
|
||||||
this.plotBoundaries = plotBoundaries;
|
this.plotBoundaries = plotBoundaries;
|
||||||
|
this.adjustYAxis = false;
|
||||||
updatePlotData();
|
updatePlotData();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -403,6 +406,7 @@ public class CalculatorPlotterImpl implements CalculatorPlotter {
|
|||||||
public void setPlotBoundaries(@Nonnull PlotBoundaries plotBoundaries) {
|
public void setPlotBoundaries(@Nonnull PlotBoundaries plotBoundaries) {
|
||||||
if (!this.plotBoundaries.equals(plotBoundaries)) {
|
if (!this.plotBoundaries.equals(plotBoundaries)) {
|
||||||
this.plotBoundaries = plotBoundaries;
|
this.plotBoundaries = plotBoundaries;
|
||||||
|
this.adjustYAxis = false;
|
||||||
firePlotDataChangedEvent();
|
firePlotDataChangedEvent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,18 +12,22 @@ import java.util.List;
|
|||||||
public class PlotData {
|
public class PlotData {
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private List<PlotFunction> functions;
|
private final List<PlotFunction> functions;
|
||||||
|
|
||||||
private boolean plot3d;
|
private final boolean plot3d;
|
||||||
|
|
||||||
|
private final boolean adjustYAxis;
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private PlotBoundaries boundaries;
|
private final PlotBoundaries boundaries;
|
||||||
|
|
||||||
public PlotData(@Nonnull List<PlotFunction> functions,
|
public PlotData(@Nonnull List<PlotFunction> functions,
|
||||||
boolean plot3d,
|
boolean plot3d,
|
||||||
|
boolean adjustYAxis,
|
||||||
@Nonnull PlotBoundaries boundaries) {
|
@Nonnull PlotBoundaries boundaries) {
|
||||||
this.functions = functions;
|
this.functions = functions;
|
||||||
this.plot3d = plot3d;
|
this.plot3d = plot3d;
|
||||||
|
this.adjustYAxis = adjustYAxis;
|
||||||
this.boundaries = boundaries;
|
this.boundaries = boundaries;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,4 +44,8 @@ public class PlotData {
|
|||||||
public PlotBoundaries getBoundaries() {
|
public PlotBoundaries getBoundaries() {
|
||||||
return boundaries;
|
return boundaries;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isAdjustYAxis() {
|
||||||
|
return adjustYAxis;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user