[apps/stats] Fill with formula in statistics

This commit is contained in:
Léa Saviot
2018-06-01 10:30:54 +02:00
parent 5f3ea724f4
commit 5da90b5160
7 changed files with 61 additions and 22 deletions

View File

@@ -11,6 +11,7 @@ namespace Regression {
class StoreController : public Shared::StoreController {
public:
StoreController(Responder * parentResponder, Store * store, ButtonRowController * header);
void fillColumnWithFormula(Poincare::Expression * formula) {}
void willDisplayCellAtLocation(HighlightCell * cell, int i, int j) override;
private:
HighlightCell * titleCells(int index) override;

View File

@@ -64,7 +64,14 @@ void StoreController::displayFormulaInput() {
bool StoreController::textFieldDidFinishEditing(TextField * textField, const char * text, Ion::Events::Event event) {
if (textField == contentView()->formulaInputView()->textField()) {
// Handle formula input
Expression * expression = Expression::parse(textField->text());
if (expression == nullptr) {
app()->displayWarning(I18n::Message::SyntaxError);
return false;
}
contentView()->displayFormulaInput(false);
fillColumnWithFormula(expression);
delete expression;
app()->setFirstResponder(contentView());
return true;
}

View File

@@ -16,6 +16,7 @@ public:
StoreController(Responder * parentResponder, FloatPairStore * store, ButtonRowController * header);
void displayFormulaInput();
virtual void fillColumnWithFormula(Poincare::Expression * formula) = 0;
// TextFieldDelegate
bool textFieldDidFinishEditing(TextField * textField, const char * text, Ion::Events::Event event) override;

View File

@@ -1,31 +1,37 @@
#include "series_context.h"
#include <poincare/decimal.h>
#include <assert.h>
#include <string.h>
using namespace Poincare;
using namespace Shared;
namespace Statistics {
void SeriesContext::setExpressionForSymbolName(const Expression * expression, const Symbol * symbol, Context & context) {
m_parentContext->setExpressionForSymbolName(expression, symbol, context);
}
const Expression * SeriesContext::expressionForSymbol(const Symbol * symbol) {
assert(m_seriesPairIndex >= 0);
if (!symbol->isSeriesSymbol()) {
return nullptr;
if (symbol->isSeriesSymbol()) {
const char * seriesName = Symbol::textForSpecialSymbols(symbol->name());
assert(strlen(seriesName) == 2);
int series = (int)(seriesName[1] - '0') - 1;
assert(series >= 0 && series < FloatPairStore::k_numberOfSeries);
assert((seriesName[0] == 'V') || (seriesName[0] == 'N'));
int storeI = seriesName[0] == 'V' ? 0 : 1;
assert(m_seriesPairIndex >= 0);
assert(m_seriesPairIndex < m_store->numberOfPairsOfSeries(series));
Expression * result = new Decimal(m_store->get(series, storeI, m_seriesPairIndex));
assert(result != nullptr);
return result;
} else {
return m_parentContext->expressionForSymbol(symbol);
}
const char * seriesName = Symbol::textForSpecialSymbols(symbol->name());
assert(strlen(seriesName) == 2);
int series = seriesName[1] - '0';
assert(series >= 0 && series < Store::k_numberOfSeries);
assert((seriesName[0] == 'V') || (seriesName[0] == 'N'));
int storeI = seriesName[0] == 'V' ? 0 : 1;
assert(m_seriesPairIndex < store->numberOfPairsOfSeries(series));
Expression * result = new Decimal(m_store->get(series, storeI, m_seriesPairIndex));
assert(result != nullptr);
return result;
}
}

View File

@@ -8,17 +8,19 @@ namespace Statistics {
class SeriesContext : public Poincare::Context {
public:
SeriesContext(Shared::FloatPairStore * store) :
SeriesContext(Shared::FloatPairStore * store, Poincare::Context * parentContext = nullptr) :
Poincare::Context(),
m_store(store),
m_seriesPairIndex(-1)
m_seriesPairIndex(-1),
m_parentContext(parentContext)
{}
void setStorePosition(int series, int i, int j);
void setExpressionForSymbolName(const Poincare::Expression * expression, const Poincare::Symbol * symbol, Poincare::Context & context) override {}
void setSeriesPairIndex(int j) { m_seriesPairIndex = j; }
void setExpressionForSymbolName(const Poincare::Expression * expression, const Poincare::Symbol * symbol, Poincare::Context & context) override;
const Poincare::Expression * expressionForSymbol(const Poincare::Symbol * symbol) override;
private:
Shared::FloatPairStore * m_store;
int m_seriesPairIndex;
Poincare::Context * m_parentContext;
};
}

View File

@@ -1,4 +1,5 @@
#include "store_controller.h"
#include "series_context.h"
#include "app.h"
#include "../apps_container.h"
#include "../constant.h"
@@ -6,16 +7,35 @@
#include <float.h>
#include <cmath>
using namespace Poincare;
using namespace Shared;
namespace Statistics {
StoreController::StoreController(Responder * parentResponder, Store * store, ButtonRowController * header) :
Shared::StoreController(parentResponder, store, header),
m_titleCells{}
m_titleCells{},
m_store(store)
{
}
void StoreController::fillColumnWithFormula(Expression * formula) {
int currentColumn = selectedColumn();
// Fetch the series used in the formula to:
// - Make sure the current filled column is not inside
// - Compute the size of the filled in series
SeriesContext seriesContext(m_store, const_cast<AppsContainer *>(static_cast<const AppsContainer *>(app()->container()))->globalContext());
for (int j = 0; j < 2; j++) {
// Set the context
seriesContext.setSeriesPairIndex(j);
// Compute the new value using the formula
double evaluation = formula->approximateToScalar<double>(seriesContext);
setDataAtLocation(evaluation, currentColumn, j + 1);
}
selectableTableView()->reloadData();
}
void StoreController::willDisplayCellAtLocation(HighlightCell * cell, int i, int j) {
Shared::StoreController::willDisplayCellAtLocation(cell, i, j);
if (cellAtLocationIsEditable(i, j)) {

View File

@@ -11,6 +11,7 @@ namespace Statistics {
class StoreController : public Shared::StoreController {
public:
StoreController(Responder * parentResponder, Store * store, ButtonRowController * header);
void fillColumnWithFormula(Poincare::Expression * formula) override;
void willDisplayCellAtLocation(HighlightCell * cell, int i, int j) override;
private:
bool setDataAtLocation(double floatBody, int columnIndex, int rowIndex) override;
@@ -18,6 +19,7 @@ private:
View * loadView() override;
void unloadView(View * view) override;
Shared::StoreTitleCell * m_titleCells[k_numberOfTitleCells];
Store * m_store;
};
}