From aa91ba3ab50320d9f3cf9b41a68d9c001f769df6 Mon Sep 17 00:00:00 2001 From: Vasile Vilvoiu Date: Thu, 22 Jul 2021 20:46:32 +0300 Subject: Adjust the way scale is computed for axes. Add extra digit only when error is greater than 1% of scale domain. Add unit test for Renderer (for axes related routines). Fixes #26. --- src/renderer.hpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/renderer.hpp') diff --git a/src/renderer.hpp b/src/renderer.hpp index 1a27276..9e97870 100644 --- a/src/renderer.hpp +++ b/src/renderer.hpp @@ -24,7 +24,7 @@ enum class Orientation { * Spectrogram rendering class */ class Renderer { -private: +protected: /* for all intents and purposes this should be private, but we want to unit test a few of the methods here */ const Configuration configuration_; /* configuration is cached as it contains multiple settings regarding spacing and sizing */ const std::size_t fft_count_; /* number of windows to render */ const std::unique_ptr color_map_; /* color map used for rendering */ @@ -50,6 +50,22 @@ private: std::list frequency_ticks_; std::list live_ticks_; + /** + * Return a short representation of the value (using unit prefixes like, m, k, M ...). + * @param value The value to encode. + * @param scale Scale of the value (in the numeric sense). + * @param unit Unit of the value. + * @return Short representation string. + * + * NOTE: The unit prefix is computed just from the scale, not the value itself. + * For example, ValueToShortString(0.00004, 5, "V") will yield "0.04mV", + * not "40uV" (which would otherwise be the more natural representation). + * This behaviour is purposeful, as it allows more flexibility (e.g. if + * we want the same prefix and number of decimal places for all values + * of an axis). + */ + static std::string ValueToShortString(double value, int scale, const std::string& unit); + /** * Build an array of ticks with linear spacing. * @param v_min Lowest value on the axis. @@ -74,6 +90,7 @@ private: * * NOTE: This method attempts to find some nice values for the ticks that * also have sufficient spacing for the text to fit properly. + * NOTE: This method enforces an error of <1% between tick text and tick value. */ std::list GetNiceTicks(double v_min, double v_max, const std::string& v_unit, unsigned int length_px, unsigned int min_tick_length_px, bool rotated); -- cgit v1.2.3