diff --git a/apps/statistics/histogram_controller.cpp b/apps/statistics/histogram_controller.cpp index 42d5c52a3..e4f762bf1 100644 --- a/apps/statistics/histogram_controller.cpp +++ b/apps/statistics/histogram_controller.cpp @@ -180,31 +180,33 @@ bool HistogramController::moveSelectionHorizontally(int deltaIndex) { return false; } -void HistogramController::preinitXRangeParameters() { +void HistogramController::preinitXRangeParameters(double * xMin) { /* Compute m_store's min and max values, hold them temporarily in the * CurveViewRange, for later use by initRangeParameters and * initBarParameters. Indeed, initRangeParameters will anyway alter the * CurveViewRange. The CurveViewRange setter methods take care of the case * where minValue >= maxValue. Moreover they compute the xGridUnit, which is * used by initBarParameters. */ - float minValue = FLT_MAX; - float maxValue = -FLT_MAX; + double minValue = DBL_MAX; + double maxValue = -DBL_MAX; for (int i = 0; i < Store::k_numberOfSeries; i ++) { if (!m_store->seriesIsEmpty(i)) { - minValue = std::min(minValue, m_store->minValue(i)); - maxValue = std::max(maxValue, m_store->maxValue(i)); + minValue = std::min(minValue, m_store->minValue(i)); + maxValue = std::max(maxValue, m_store->maxValue(i)); } } + assert(xMin != nullptr); + *xMin = minValue; m_store->setXMin(minValue); m_store->setXMax(maxValue); } void HistogramController::initRangeParameters() { assert(selectedSeriesIndex() >= 0 && m_store->sumOfOccurrences(selectedSeriesIndex()) > 0); - float barWidth = m_store->barWidth(); - preinitXRangeParameters(); - float xMin = m_store->firstDrawnBarAbscissa(); - float xMax = m_store->xMax() + barWidth; + double barWidth = m_store->barWidth(); + double xMin; + preinitXRangeParameters(&xMin); + double xMax = m_store->xMax() + barWidth; /* if a bar is represented by less than one pixel, we cap xMax */ if ((xMax - xMin)/barWidth > k_maxNumberOfBarsPerWindow) { xMax = xMin + k_maxNumberOfBarsPerWindow*barWidth; @@ -244,8 +246,9 @@ void HistogramController::initYRangeParameters(int series) { void HistogramController::initBarParameters() { assert(selectedSeriesIndex() >= 0 && m_store->sumOfOccurrences(selectedSeriesIndex()) > 0); - preinitXRangeParameters(); - m_store->setFirstDrawnBarAbscissa(m_store->xMin()); + double xMin; + preinitXRangeParameters(&xMin); + m_store->setFirstDrawnBarAbscissa(xMin); double barWidth = m_store->xGridUnit(); if (barWidth <= 0.0) { barWidth = 1.0; diff --git a/apps/statistics/histogram_controller.h b/apps/statistics/histogram_controller.h index a64006a3d..813964345 100644 --- a/apps/statistics/histogram_controller.h +++ b/apps/statistics/histogram_controller.h @@ -34,7 +34,7 @@ private: void highlightSelection() override; Responder * tabController() const override; void reloadBannerView() override; - void preinitXRangeParameters(); + void preinitXRangeParameters(double * xMin); void initRangeParameters(); void initYRangeParameters(int series); void initBarParameters(); diff --git a/apps/statistics/histogram_parameter_controller.cpp b/apps/statistics/histogram_parameter_controller.cpp index 6e58a2d8e..5fb02e7b7 100644 --- a/apps/statistics/histogram_parameter_controller.cpp +++ b/apps/statistics/histogram_parameter_controller.cpp @@ -37,18 +37,20 @@ double HistogramParameterController::parameterAtIndex(int index) { return index == 0 ? m_store->barWidth() : m_store->firstDrawnBarAbscissa(); } -bool HistogramParameterController::setParameterAtIndex(int parameterIndex, double f) { +bool HistogramParameterController::setParameterAtIndex(int parameterIndex, double value) { assert(parameterIndex >= 0 && parameterIndex < k_numberOfCells); if (parameterIndex == 0) { + // Bar width + // The bar width cannot be negative - if (f <= 0.0f) { + if (value <= 0.0) { Container::activeApp()->displayWarning(I18n::Message::ForbiddenValue); return false; } // There should be at least one value in the drawn bin for (int i = 0; i < DoublePairStore::k_numberOfSeries; i++) { - if (m_store->firstDrawnBarAbscissa() <= m_store->maxValue(i)+f) { + if (m_store->firstDrawnBarAbscissa() <= m_store->maxValue(i)+value) { break; } else if (i == DoublePairStore::k_numberOfSeries - 1) { Container::activeApp()->displayWarning(I18n::Message::ForbiddenValue); @@ -58,9 +60,9 @@ bool HistogramParameterController::setParameterAtIndex(int parameterIndex, doubl // The number of bars cannot be above the max assert(DoublePairStore::k_numberOfSeries > 0); - double maxNewNumberOfBars = std::ceil((m_store->maxValue(0) - m_store->minValue(0))/f); + double maxNewNumberOfBars = std::ceil((m_store->maxValue(0) - m_store->minValue(0))/value); for (int i = 1; i < DoublePairStore::k_numberOfSeries; i++) { - double numberOfBars = std::ceil((m_store->maxValue(i) - m_store->minValue(i))/f); + double numberOfBars = std::ceil((m_store->maxValue(i) - m_store->minValue(i))/value); if (maxNewNumberOfBars < numberOfBars) { maxNewNumberOfBars = numberOfBars; } @@ -71,34 +73,36 @@ bool HistogramParameterController::setParameterAtIndex(int parameterIndex, doubl } // Set the bar width - m_store->setBarWidth(f); - } else { - // The number of bars cannot be above the max - assert(DoublePairStore::k_numberOfSeries > 0); - double maxNewNumberOfBars = ceilf((m_store->maxValue(0) - f)/m_store->barWidth()); - for (int i = 1; i < DoublePairStore::k_numberOfSeries; i++) { - double numberOfBars = ceilf((m_store->maxValue(i) - f)/m_store->barWidth()); - if (maxNewNumberOfBars < numberOfBars) { - maxNewNumberOfBars = numberOfBars; - } - } - if (maxNewNumberOfBars > Store::k_maxNumberOfBars) { - Container::activeApp()->displayWarning(I18n::Message::ForbiddenValue); - return false; - } - // There should be at least one value in the drawn bin - for (int i = 0; i < DoublePairStore::k_numberOfSeries; i++) { - if (f <= m_store->maxValue(i)+m_store->barWidth()) { - break; - } else if (i == DoublePairStore::k_numberOfSeries - 1) { - Container::activeApp()->displayWarning(I18n::Message::ForbiddenValue); - return false; - } - } - // Set the first drawn bar abscissa - m_store->setFirstDrawnBarAbscissa(f); + m_store->setBarWidth(value); + return true; } - return true; + assert(parameterIndex == 1); + // The number of bars cannot be above the max + assert(DoublePairStore::k_numberOfSeries > 0); + const double barWidth = m_store->barWidth(); + double maxNewNumberOfBars = std::ceil((m_store->maxValue(0) - value)/barWidth); + for (int i = 1; i < DoublePairStore::k_numberOfSeries; i++) { + double numberOfBars = std::ceil((m_store->maxValue(i) - value)/barWidth); + if (maxNewNumberOfBars < numberOfBars) { + maxNewNumberOfBars = numberOfBars; + } + } + if (maxNewNumberOfBars > Store::k_maxNumberOfBars) { + Container::activeApp()->displayWarning(I18n::Message::ForbiddenValue); + return false; + } + // There should be at least one value in the drawn bin + for (int i = 0; i < DoublePairStore::k_numberOfSeries; i++) { + if (value <= m_store->maxValue(i) + barWidth) { + break; + } else if (i == DoublePairStore::k_numberOfSeries - 1) { + Container::activeApp()->displayWarning(I18n::Message::ForbiddenValue); + return false; + } + } + // Set the first drawn bar abscissa + m_store->setFirstDrawnBarAbscissa(value); + return true; } HighlightCell * HistogramParameterController::reusableParameterCell(int index, int type) { diff --git a/apps/statistics/store.cpp b/apps/statistics/store.cpp index dd9e07b6d..e7d6aec32 100644 --- a/apps/statistics/store.cpp +++ b/apps/statistics/store.cpp @@ -43,8 +43,8 @@ double Store::heightOfBarAtIndex(int series, int index) const { double Store::heightOfBarAtValue(int series, double value) const { double width = barWidth(); int barNumber = std::floor((value - m_firstDrawnBarAbscissa)/width); - double lowerBound = m_firstDrawnBarAbscissa + barNumber*width; - double upperBound = m_firstDrawnBarAbscissa + (barNumber+1)*width; + double lowerBound = m_firstDrawnBarAbscissa + ((double)barNumber)*width; + double upperBound = m_firstDrawnBarAbscissa + ((double)(barNumber+1))*width; return sumOfValuesBetween(series, lowerBound, upperBound); }