This commit is contained in:
Sergey Solovyev 2011-12-02 17:32:08 +04:00
parent d2e161cfa0
commit a21f31f1e0
2 changed files with 35 additions and 7 deletions

View File

@ -8,6 +8,7 @@ package org.solovyev.android.calculator;
import android.app.Activity; import android.app.Activity;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.util.Log; import android.util.Log;
@ -98,6 +99,9 @@ public class CalculatorPlotActivity extends Activity {
} catch (ParseException e) { } catch (ParseException e) {
Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show(); Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
finish(); finish();
} catch (ArithmeticException e) {
Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
finish();
} }
} }
@ -109,6 +113,10 @@ public class CalculatorPlotActivity extends Activity {
} }
final LineChart chart = prepareChart(minValue, maxValue, expression, variable); final LineChart chart = prepareChart(minValue, maxValue, expression, variable);
// reverting boundaries (as in prepareChart() we add some cached values )
chart.getRenderer().setXAxisMin(DEFAULT_MIN_NUMBER);
chart.getRenderer().setXAxisMax(DEFAULT_MAX_NUMBER);
graphicalView = new GraphicalView(this, chart); graphicalView = new GraphicalView(this, chart);
graphicalView.addZoomListener(new ZoomListener() { graphicalView.addZoomListener(new ZoomListener() {
@Override @Override
@ -141,7 +149,8 @@ public class CalculatorPlotActivity extends Activity {
//lock all operations with history //lock all operations with history
if (pendingOperation.getObject() == this) { if (pendingOperation.getObject() == this) {
final XYMultipleSeriesRenderer dr = chart.getRenderer(); final XYMultipleSeriesRenderer dr = chart.getRenderer();
Log.d(CalculatorPlotActivity.class.getName(), "x = [" + dr.getXAxisMin() + ", " + dr.getXAxisMax() + "], y = [" + dr.getYAxisMin() + ", " + dr.getYAxisMax() + "]");
//Log.d(CalculatorPlotActivity.class.getName(), "x = [" + dr.getXAxisMin() + ", " + dr.getXAxisMax() + "], y = [" + dr.getYAxisMin() + ", " + dr.getYAxisMax() + "]");
final XYSeries realSeries = chart.getDataset().getSeriesAt(0); final XYSeries realSeries = chart.getDataset().getSeriesAt(0);
@ -152,19 +161,28 @@ public class CalculatorPlotActivity extends Activity {
imagSeries = new XYSeries(getImagFunctionName(CalculatorPlotActivity.this.expression, CalculatorPlotActivity.this.variable)); imagSeries = new XYSeries(getImagFunctionName(CalculatorPlotActivity.this.expression, CalculatorPlotActivity.this.variable));
} }
if (addXY(dr.getXAxisMin(), dr.getXAxisMax(), expression, variable, realSeries, imagSeries)) { try {
if (chart.getDataset().getSeriesCount() <= 1) { if (addXY(dr.getXAxisMin(), dr.getXAxisMax(), expression, variable, realSeries, imagSeries)) {
chart.getDataset().addSeries(imagSeries); if (chart.getDataset().getSeriesCount() <= 1) {
chart.getRenderer().addSeriesRenderer(createImagRenderer()); chart.getDataset().addSeries(imagSeries);
chart.getRenderer().addSeriesRenderer(createImagRenderer());
}
} }
} catch (ArithmeticException e) {
// todo serso: translate
Toast.makeText(CalculatorPlotActivity.this, "Arithmetic error: " + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
CalculatorPlotActivity.this.finish();
} }
graphicalView.repaint(); if (pendingOperation.getObject() == this) {
graphicalView.repaint();
}
} }
} }
} }
}); });
new Handler().postDelayed(pendingOperation.getObject(), EVAL_DELAY_MILLIS); new Handler().postDelayed(pendingOperation.getObject(), EVAL_DELAY_MILLIS);
} }
@ -197,7 +215,8 @@ public class CalculatorPlotActivity extends Activity {
renderer.addSeriesRenderer(createCommonRenderer()); renderer.addSeriesRenderer(createCommonRenderer());
renderer.setShowGrid(true); renderer.setShowGrid(true);
renderer.setXTitle(variable.getName()); renderer.setXTitle(variable.getName());
renderer.setYTitle("f(" + variable.getName() +")"); renderer.setYTitle("f(" + variable.getName() + ")");
renderer.setYAxisAlign(Paint.Align.CENTER, 0);
if (imagExists) { if (imagExists) {
renderer.addSeriesRenderer(createImagRenderer()); renderer.addSeriesRenderer(createImagRenderer());
} }

View File

@ -7,6 +7,8 @@ package org.solovyev.android.calculator.model;
import jscl.AngleUnit; import jscl.AngleUnit;
import jscl.math.Expression; import jscl.math.Expression;
import jscl.math.Generic;
import jscl.math.function.Constant;
import org.junit.Assert; import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
@ -79,6 +81,13 @@ public class CalculatorEngineTest {
public void testEvaluate() throws Exception { public void testEvaluate() throws Exception {
final CalculatorEngine cm = CalculatorEngine.instance; final CalculatorEngine cm = CalculatorEngine.instance;
Assert.assertEquals("cos(t)+10%", cm.evaluate(JsclOperation.simplify, "cos(t)+10%").getResult());
final Generic expression = cm.getEngine().simplifyGeneric("cos(t)+10%");
expression.substitute(new Constant("t"), Expression.valueOf(100d));
Assert.assertEquals("it", cm.evaluate(JsclOperation.simplify, "it").getResult());
Assert.assertEquals("10%", cm.evaluate(JsclOperation.simplify, "10%").getResult());
Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "eq(0, 1)").getResult()); Assert.assertEquals("0", cm.evaluate(JsclOperation.numeric, "eq(0, 1)").getResult());
Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "eq(1, 1)").getResult()); Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "eq(1, 1)").getResult());
Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "eq( 1, 1)").getResult()); Assert.assertEquals("1", cm.evaluate(JsclOperation.numeric, "eq( 1, 1)").getResult());