Skip to content

Commit

Permalink
ncplot uses exclusively non-negative samples #447
Browse files Browse the repository at this point in the history
  • Loading branch information
dankamongmen committed Apr 6, 2020
1 parent 85c568b commit 77dd998
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
6 changes: 3 additions & 3 deletions doc/man/man3/notcurses_plot.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ typedef struct ncplot_options {
uint64_t rangex;
// dependent min and max. set both equal to 0 to
// use domain autodiscovery.
int64_t miny, maxy;
uint64_t miny, maxy;
bool labelaxisd; // label dependent axis
bool exponentialy; // is dependent exponential?
bool vertical_indep; // vertical independent variable
Expand All @@ -41,8 +41,8 @@ typedef struct ncplot_options {

**struct ncplane* ncplot_plane(struct ncplot* n);**

**int ncplot_add_sample(struct ncplot* n, uint64_t x, int64_t y);**
**int ncplot_set_sample(struct ncplot* n, uint64_t x, int64_t y);**
**int ncplot_add_sample(struct ncplot* n, uint64_t x, uint64_t y);**
**int ncplot_set_sample(struct ncplot* n, uint64_t x, uint64_t y);**

**void ncplot_destroy(struct ncplot* n);**

Expand Down
6 changes: 3 additions & 3 deletions include/notcurses/notcurses.h
Original file line number Diff line number Diff line change
Expand Up @@ -2558,7 +2558,7 @@ typedef struct ncplot_options {
// resolution, the independent variable would be the range [0..3600): 3600.
// if rangex is 0, it is dynamically set to the number of columns.
uint64_t rangex;
int64_t miny, maxy; // y axis min and max. for autodiscovery, set them equal.
uint64_t miny, maxy; // y axis min and max. for autodiscovery, set them equal.
bool labelaxisd; // generate labels for the dependent axis
bool exponentialy; // is y-axis exponential? (not yet implemented)
// independent variable is vertical rather than horizontal
Expand All @@ -2576,8 +2576,8 @@ API struct ncplane* ncplot_plane(struct ncplot* n);
// x window, the x window is advanced to include x, and values passing beyond
// the window are lost. The first call will place the initial window. The plot
// will be redrawn, but notcurses_render() is not called.
API int ncplot_add_sample(struct ncplot* n, uint64_t x, int64_t y);
API int ncplot_set_sample(struct ncplot* n, uint64_t x, int64_t y);
API int ncplot_add_sample(struct ncplot* n, uint64_t x, uint64_t y);
API int ncplot_set_sample(struct ncplot* n, uint64_t x, uint64_t y);

API void ncplot_destroy(struct ncplot* n);

Expand Down
6 changes: 3 additions & 3 deletions python/src/notcurses/build_notcurses.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,15 +420,15 @@
uint64_t minchannel;
ncgridgeom_e gridtype;
uint64_t rangex;
int64_t miny, maxy;
uint64_t miny, maxy;
bool labelaxisd;
bool exponentialy;
bool vertical_indep;
} ncplot_options;
struct ncplot* ncplot_create(struct ncplane* n, const ncplot_options* opts);
struct ncplane* ncplot_plane(struct ncplot* n);
int ncplot_add_sample(struct ncplot* n, uint64_t x, int64_t y);
int ncplot_set_sample(struct ncplot* n, uint64_t x, int64_t y);
int ncplot_add_sample(struct ncplot* n, uint64_t x, uint64_t y);
int ncplot_set_sample(struct ncplot* n, uint64_t x, uint64_t y);
void ncplot_destroy(struct ncplot* n);
bool ncplane_set_scrolling(struct ncplane* n, bool scrollp);
""")
Expand Down
2 changes: 1 addition & 1 deletion src/lib/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ typedef struct ncplot {
// domain minimum and maximum. if detectdomain is true, these are
// progressively enlarged/shrunk to fit the sample set. if not, samples
// outside these bounds are counted, but the displayed range covers only this.
int64_t miny, maxy;
uint64_t miny, maxy;
// circular buffer, with the oldest element at slotstart, and slotcount
// elements. slotcount is max(columns, rangex).
int64_t* slots;
Expand Down
16 changes: 9 additions & 7 deletions src/lib/plot.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ redraw_plot(ncplot* n){
ncplane_dim_yx(ncplot_plane(n), &dimy, &dimx);
// each transition is worth this much change in value
const size_t states = wcslen(geomdata[n->gridtype].egcs);
double interval = (n->maxy - n->miny + 1) / ((double)dimy * states); // FIXME
// FIXME can we not rid ourselves of this meddlesome double?
double interval = (n->maxy - n->miny + 1) / ((double)dimy * states);
int idx = n->slotstart;
const int startx = n->labelaxisd ? PREFIXSTRLEN : 0;
if(n->labelaxisd){
Expand All @@ -28,11 +29,12 @@ redraw_plot(ncplot* n){
ncplane_putstr_yx(ncplot_plane(n), dimy - y - 1, PREFIXSTRLEN - strlen(buf), buf);
}
}
fprintf(stderr, "min: %ju max: %ju states: %zu interval: %g\n", n->miny, n->maxy, states, interval);
for(uint64_t x = startx ; x < n->slotcount + startx ; ++x){
if(x >= (unsigned)dimx){
break;
}
int64_t gval = n->slots[idx]; // clip the value at the limits of the graph
uint64_t gval = n->slots[idx]; // clip the value at the limits of the graph
if(gval < n->miny){
gval = n->miny;
}
Expand Down Expand Up @@ -140,7 +142,7 @@ ncplane* ncplot_plane(ncplot* n){
// 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];
const uint64_t val = n->slots[x % n->slotcount];
if(n->detectdomain){
if(val > n->maxy){
n->maxy = val;
Expand Down Expand Up @@ -191,8 +193,8 @@ window_slide(ncplot* n, uint64_t x){

// x must be within n's window
static inline void
update_sample(ncplot* n, uint64_t x, int64_t y, bool reset){
uint64_t idx = x/*(n->slotstart + delta)*/ % n->slotcount;
update_sample(ncplot* n, uint64_t x, uint64_t y, bool reset){
uint64_t idx = x % n->slotcount;
if(reset){
n->slots[idx] = y;
}else{
Expand All @@ -204,7 +206,7 @@ update_sample(ncplot* n, uint64_t x, int64_t y, bool reset){
// x window, the x window is advanced to include x, and values passing beyond
// the window are lost. The first call will place the initial window. The plot
// 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, uint64_t y){
if(window_slide(n, x)){
return -1;
}
Expand All @@ -215,7 +217,7 @@ int ncplot_add_sample(ncplot* n, uint64_t x, int64_t y){
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, uint64_t y){
if(window_slide(n, x)){
return -1;
}
Expand Down

0 comments on commit 77dd998

Please sign in to comment.