Skip to content

Commit 080f118

Browse files
committed
Update "total" value for non-percent bar meters
If "isPercentChart" of a meter is false, update its "total" value automatically in the bar meter mode. The "total" value is capped to DBL_MAX in order to ensure the division never produces NaN. The newly introduced Meter_computeSum() function will be reused by the feature "Graph meter dynamic scaling and percent graph drawing". Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
1 parent b3d2aeb commit 080f118

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

Meter.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ in the source distribution for its full text.
1010
#include "Meter.h"
1111

1212
#include <assert.h>
13+
#include <float.h>
1314
#include <limits.h> // IWYU pragma: keep
1415
#include <math.h>
1516
#include <stdlib.h>
@@ -47,6 +48,14 @@ static inline void Meter_displayBuffer(const Meter* this, RichString* out) {
4748
}
4849
}
4950

51+
static double Meter_computeSum(const Meter* this) {
52+
assert(this->curItems > 0);
53+
assert(this->values);
54+
double sum = sumPositiveValues(this->values, this->curItems);
55+
// Prevent rounding to infinity in IEEE 754
56+
return MINIMUM(DBL_MAX, sum);
57+
}
58+
5059
/* ---------- TextMeterMode ---------- */
5160

5261
static void TextMeterMode_draw(Meter* this, int x, int y, int w) {
@@ -100,6 +109,12 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) {
100109
w--;
101110
}
102111

112+
// Update the "total" if necessary
113+
if (!Meter_isPercentChart(this) && this->curItems > 0) {
114+
double sum = Meter_computeSum(this);
115+
this->total = MAXIMUM(sum, this->total);
116+
}
117+
103118
if (w < 1) {
104119
goto end;
105120
}

0 commit comments

Comments
 (0)