plots: multiple rows #136

This commit is contained in:
nick black 2020-04-03 06:37:14 -04:00 committed by Nick Black
parent 6b74ea7718
commit 42eb18893e

View File

@ -50,19 +50,25 @@ ncplane* ncplot_plane(ncplot* n){
return n->ncp; return n->ncp;
} }
static inline bool // if we're doing domain detection, update the domain to reflect the value we
invalid_y(ncplot* n, int64_t y){ // just set. if we're not, check the result against the known ranges, and
// return -1 if the value is outside of that range.
static inline int
update_domain(ncplot* n, uint64_t x){
const int64_t val = n->slots[x % n->slotcount];
if(n->detectdomain){ if(n->detectdomain){
if(y > n->maxy){ if(val > n->maxy){
n->maxy = y; n->maxy = val;
} }
if(y < n->miny){ if(val < n->miny){
n->miny = y; n->miny = val;
} }
}else if(y > n->maxy || y < n->miny){ return 0;
return true;
} }
return false; if(val > n->maxy || val < n->miny){
return -1;
}
return 0;
} }
// if x is less than the window, return -1 // if x is less than the window, return -1
@ -102,7 +108,6 @@ window_slide(ncplot* n, uint64_t x){
static inline void static inline void
update_sample(ncplot* n, uint64_t x, int64_t y, bool reset){ update_sample(ncplot* n, uint64_t x, int64_t y, bool reset){
uint64_t idx = x/*(n->slotstart + delta)*/ % n->slotcount; uint64_t idx = x/*(n->slotstart + delta)*/ % n->slotcount;
fprintf(stderr, "WRITING %jd to IFX %ju (DELTA: %ju, SLOTX: %ju)\n", y, idx, 0, n->slotx);
if(reset){ if(reset){
n->slots[idx] = y; n->slots[idx] = y;
}else{ }else{
@ -113,16 +118,28 @@ fprintf(stderr, "WRITING %jd to IFX %ju (DELTA: %ju, SLOTX: %ju)\n", y, idx, 0,
static int static int
redraw_plot(ncplot* n){ redraw_plot(ncplot* n){
ncplane_erase(ncplot_plane(n)); // FIXME shouldn't need this ncplane_erase(ncplot_plane(n)); // FIXME shouldn't need this
for(int y = 0 ; y < 1 ; ++y){ // FIXME use available columns const int dimy = ncplane_dim_y(ncplot_plane(n));
int idx = n->slotstart; // each row is worth this much change in value
for(uint64_t x = 0 ; x < n->slotcount ; ++x){ double interval = (n->maxy - n->miny + 1) / (double)dimy;
if(n->slots[idx] > n->miny){ // FIXME prorate int idx = n->slotstart;
if(ncplane_putegc_yx(ncplot_plane(n), y, x, "", NULL) <= 0){ for(uint64_t x = 0 ; x < n->slotcount ; ++x){
int64_t gval = n->slots[idx];
if(gval < n->miny){
gval = n->miny;
}
if(gval > n->maxy){
gval = n->maxy;
}
for(int y = 0 ; y < dimy ; ++y){
if(n->miny + interval * (y + 1) <= gval){
if(ncplane_putegc_yx(ncplot_plane(n), dimy - y - 1, x, "", NULL) <= 0){
return -1; return -1;
} }
}else{
break;
} }
idx = (idx + 1) % n->slotcount;
} }
idx = (idx + 1) % n->slotcount;
} }
return 0; return 0;
} }
@ -132,24 +149,24 @@ redraw_plot(ncplot* n){
// the window are lost. The first call will place the initial window. The plot // the window are lost. The first call will place the initial window. The plot
// will be redrawn, but notcurses_render() is not called. // will be redrawn, but notcurses_render() is not called.
int ncplot_add_sample(ncplot* n, uint64_t x, int64_t y){ int ncplot_add_sample(ncplot* n, uint64_t x, int64_t y){
if(invalid_y(n, y)){
return -1;
}
if(window_slide(n, x)){ if(window_slide(n, x)){
return -1; return -1;
} }
update_sample(n, x, y, false); update_sample(n, x, y, false);
if(update_domain(n, x)){
return -1;
}
return redraw_plot(n); return redraw_plot(n);
} }
int ncplot_set_sample(ncplot* n, uint64_t x, int64_t y){ int ncplot_set_sample(ncplot* n, uint64_t x, int64_t y){
if(invalid_y(n, y)){
return -1;
}
if(window_slide(n, x)){ if(window_slide(n, x)){
return -1; return -1;
} }
update_sample(n, x, y, true); update_sample(n, x, y, true);
if(update_domain(n, x)){
return -1;
}
return redraw_plot(n); return redraw_plot(n);
} }