Skip to content

Commit 6a2df7b

Browse files
committed
Graph meter dynamic scaling and percent graph drawing
Implement dynamic scaling for Graph meter mode, and separate it from "100% graph" drawing. This is controlled by the "isPercentChart" property of a MeterClass. If "isPercentChart" is true, the meter would be drawn as a "100% graph". Graph meters now expect the "total" value may change, and the newly changed "total" value no longer affects the percent graph drawings of earlier meter values. If "isPercentChart" is false, the meter would be drawn with a dynamic scale. Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
1 parent 080f118 commit 6a2df7b

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

Meter.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,12 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
230230
}
231231
w -= captionLen;
232232

233+
// Prepare parameters for drawing
233234
assert(this->h >= 1);
234235
int h = this->h;
235236

237+
bool isPercentChart = Meter_isPercentChart(this);
238+
236239
GraphData* data = &this->drawData;
237240

238241
// Expand the graph data buffer if necessary
@@ -261,8 +264,10 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
261264

262265
data->values[nValues - 1] = 0.0;
263266
if (this->curItems > 0) {
264-
assert(this->values);
265-
data->values[nValues - 1] = sumPositiveValues(this->values, this->curItems);
267+
data->values[nValues - 1] = Meter_computeSum(this);
268+
if (isPercentChart && this->total > 0.0) {
269+
data->values[nValues - 1] /= this->total;
270+
}
266271
}
267272
}
268273

@@ -292,10 +297,19 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
292297
}
293298
size_t i = nValues - (size_t)w * 2;
294299

300+
// Determine the graph scale
301+
double total = 1.0;
302+
if (!isPercentChart) {
303+
for (size_t j = i; j < nValues; j++) {
304+
total = MAXIMUM(data->values[j], total);
305+
}
306+
assert(total <= DBL_MAX);
307+
}
308+
assert(total >= 1.0);
309+
295310
// Draw the actual graph
296311
for (int col = 0; i < nValues - 1; i += 2, col++) {
297312
int pix = GraphMeterMode_pixPerRow * h;
298-
double total = MAXIMUM(this->total, 1);
299313
int v1 = (int) lround(CLAMP(data->values[i] / total * pix, 1.0, pix));
300314
int v2 = (int) lround(CLAMP(data->values[i + 1] / total * pix, 1.0, pix));
301315

0 commit comments

Comments
 (0)