Skip to content

Commit 75bf096

Browse files
committed
experimental negative value support
1 parent 4d42d6a commit 75bf096

1 file changed

Lines changed: 66 additions & 12 deletions

File tree

ttyplot.c

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ static void draw_axes(int h, int ph, int pw, double max, double min, char *unit)
245245
mvaddch(1, 2, T_UARR);
246246
mvaddch(h - 3, 2, T_LLCR);
247247

248+
248249
if (colors[AXES_COLOR] != -1)
249250
attroff(COLOR_PAIR(AXES_COLOR + 1));
250251

@@ -255,17 +256,28 @@ static void draw_axes(int h, int ph, int pw, double max, double min, char *unit)
255256
// Print scale labels
256257
if (max - min >= 0.1) {
257258
mvprintw(1, 4, "%.1f %s", max, unit);
258-
mvprintw((ph / 4) + 1, 4, "%.1f %s", min / 4 + max * 3 / 4, unit);
259-
mvprintw((ph / 2) + 1, 4, "%.1f %s", min / 2 + max / 2, unit);
260-
mvprintw((ph * 3 / 4) + 1, 4, "%.1f %s", min * 3 / 4 + max / 4, unit);
259+
260+
double label_val;
261+
262+
label_val = min / 4 + max * 3 / 4;
263+
if (fabs(label_val) < 0.01) label_val = 0.0; // Prevent -0.0
264+
mvprintw((ph / 4) + 1, 4, "%.1f %s", label_val, unit);
265+
266+
label_val = min / 2 + max / 2;
267+
if (fabs(label_val) < 0.01) label_val = 0.0; // Prevent -0.0
268+
mvprintw((ph / 2) + 1, 4, "%.1f %s", label_val, unit);
269+
270+
label_val = min * 3 / 4 + max / 4;
271+
if (fabs(label_val) < 0.01) label_val = 0.0; // Prevent -0.0
272+
mvprintw((ph * 3 / 4) + 1, 4, "%.1f %s", label_val, unit);
261273
}
262274

263275
if (colors[TEXT_COLOR] != -1)
264276
attroff(COLOR_PAIR(TEXT_COLOR + 1));
265277
}
266278

267279
static void draw_line(int x, int ph, int l1, int l2, cchar_t *c1, cchar_t *c2,
268-
cchar_t *hce, cchar_t *lce) {
280+
cchar_t *hce, cchar_t *lce, int zero_pos, double v1, double v2) {
269281
static cchar_t space = {.attr = A_REVERSE, .chars = {' ', '\0'}};
270282
cchar_t c1r = *c1, c2r = *c2;
271283
c1r.attr |= A_REVERSE;
@@ -305,14 +317,48 @@ static void draw_line(int x, int ph, int l1, int l2, cchar_t *c1, cchar_t *c2,
305317
space.attr |= COLOR_PAIR(LINE_COLOR + 1);
306318
}
307319

308-
if (l1 > l2) {
309-
mvvline_set(ph + 1 - l1, x, c1, l1 - l2);
310-
mvvline_set(ph + 1 - l2, x, &c2r, l2);
311-
} else if (l1 < l2) {
312-
mvvline_set(ph + 1 - l2, x, (c2 == hce || c2 == lce) ? &c2r : &space, l2 - l1);
313-
mvvline_set(ph + 1 - l1, x, &c2r, l1);
320+
// Handle drawing based on whether values are positive or negative
321+
if (zero_pos > 0) { // We have negative values
322+
int y1_start, y1_end, y2_start, y2_end;
323+
324+
// For value 1
325+
if (v1 >= 0) {
326+
// Positive value: draw from zero line upward
327+
y1_start = ph + 1 - l1;
328+
y1_end = ph + 1 - zero_pos;
329+
} else {
330+
// Negative value: draw from zero line downward
331+
y1_start = ph + 1 - zero_pos;
332+
y1_end = ph + 1 - l1;
333+
}
334+
335+
// For value 2
336+
if (v2 >= 0) {
337+
y2_start = ph + 1 - l2;
338+
y2_end = ph + 1 - zero_pos;
339+
} else {
340+
y2_start = ph + 1 - zero_pos;
341+
y2_end = ph + 1 - l2;
342+
}
343+
344+
// Draw the lines
345+
if (y1_start < y1_end) {
346+
mvvline_set(y1_start, x, c1, y1_end - y1_start);
347+
}
348+
if (y2_start < y2_end && l2 > 0) {
349+
mvvline_set(y2_start, x, &c2r, y2_end - y2_start);
350+
}
314351
} else {
315-
mvvline_set(ph + 1 - l2, x, &c2r, l2);
352+
// Original behavior for all positive values
353+
if (l1 > l2) {
354+
mvvline_set(ph + 1 - l1, x, c1, l1 - l2);
355+
mvvline_set(ph + 1 - l2, x, &c2r, l2);
356+
} else if (l1 < l2) {
357+
mvvline_set(ph + 1 - l2, x, (c2 == hce || c2 == lce) ? &c2r : &space, l2 - l1);
358+
mvvline_set(ph + 1 - l1, x, &c2r, l1);
359+
} else {
360+
mvvline_set(ph + 1 - l2, x, &c2r, l2);
361+
}
316362
}
317363

318364
// Reset all color attributes (COLOR_PAIR indexes are LINE_COLOR+1 through
@@ -338,6 +384,14 @@ static void plot_values(int ph, int pw, double *v1, double *v2, double max, doub
338384
int i = (n + 1) % pw;
339385
int x;
340386
int l1, l2;
387+
int zero_pos = 0;
388+
389+
// Calculate zero position if we have negative values
390+
if (min < 0 && max > 0) {
391+
zero_pos = lrint((0 - min) / (max - min) * ph);
392+
} else if (max <= 0) {
393+
zero_pos = ph; // All values are negative, zero is at top
394+
}
341395

342396
if (colors[LINE_COLOR] != -1)
343397
attron(COLOR_PAIR(LINE_COLOR + 1));
@@ -370,7 +424,7 @@ static void plot_values(int ph, int pw, double *v1, double *v2, double max, doub
370424
(v2 && v2[i] > hardmax) ? hce
371425
: (v2 && v2[i] < hardmin) ? lce
372426
: pc,
373-
hce, lce);
427+
hce, lce, zero_pos, v1[i], v2 ? v2[i] : 0);
374428
}
375429

376430
if (colors[LINE_COLOR] != -1)

0 commit comments

Comments
 (0)