From 26754a3230f5c47ce07dc1f99f1e7c5ecaf81234 Mon Sep 17 00:00:00 2001 From: Gabriel Ozouf Date: Tue, 6 Oct 2020 11:57:45 +0200 Subject: [PATCH] [graph] Force same grid unit on orthonormal graphs Change-Id: I2abca9eb1c4a14057323ee97e1044715f89620ff --- apps/shared/curve_view_range.h | 3 ++- apps/shared/interactive_curve_view_range.cpp | 16 ++++++++++++++++ apps/shared/interactive_curve_view_range.h | 4 ++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/apps/shared/curve_view_range.h b/apps/shared/curve_view_range.h index caa1d9ab0..ae31a94f2 100644 --- a/apps/shared/curve_view_range.h +++ b/apps/shared/curve_view_range.h @@ -27,9 +27,10 @@ public: } constexpr static float k_maxNumberOfXGridUnits = 18.0f; constexpr static float k_maxNumberOfYGridUnits = 13.0f; -private: +protected: constexpr static float k_minNumberOfXGridUnits = 7.0f; constexpr static float k_minNumberOfYGridUnits = 5.0f; +private: /* The grid units is constrained to be a number of type: k*10^n with k = 1,2 or 5 * and n a relative integer. The choice of x and y grid units depend on the * grid range.*/ diff --git a/apps/shared/interactive_curve_view_range.cpp b/apps/shared/interactive_curve_view_range.cpp index 46d347127..86953bc04 100644 --- a/apps/shared/interactive_curve_view_range.cpp +++ b/apps/shared/interactive_curve_view_range.cpp @@ -33,6 +33,22 @@ void InteractiveCurveViewRange::setYMax(float yMax) { MemoizedCurveViewRange::protectedSetYMax(yMax, k_lowerMaxFloat, k_upperMaxFloat); } +float InteractiveCurveViewRange::yGridUnit() const { + float res = MemoizedCurveViewRange::yGridUnit(); + if (m_zoomNormalize) { + /* When m_zoomNormalize is active, both xGridUnit and yGridUnit will be the + * same. To declutter the X axis, we try a unit twice as large. We check + * that it allows enough graduations on the Y axis, but if the standard + * unit would lead to too many graduations on the X axis, we force the + * larger unit anyways. */ + float numberOfUnits = (yMax() - yMin()) / res; + if (numberOfUnits > k_maxNumberOfXGridUnits || numberOfUnits / 2.f > k_minNumberOfYGridUnits) { + return 2 * res; + } + } + return res; +} + void InteractiveCurveViewRange::zoom(float ratio, float x, float y) { float xMi = xMin(); float xMa = xMax(); diff --git a/apps/shared/interactive_curve_view_range.h b/apps/shared/interactive_curve_view_range.h index 4b1c389bd..5ba3ba3a6 100644 --- a/apps/shared/interactive_curve_view_range.h +++ b/apps/shared/interactive_curve_view_range.h @@ -26,6 +26,10 @@ public: bool zoomNormalize() const { return m_zoomNormalize; } void setZoomNormalize(bool v) { m_zoomNormalize = v; } + // MemoizedCurveViewRange + float xGridUnit() const override { return m_zoomNormalize ? yGridUnit() : MemoizedCurveViewRange::xGridUnit(); } + float yGridUnit() const override; + // CurveViewWindow void setXMin(float f) override; void setXMax(float f) override;