graph happiness

This commit is contained in:
Neil 2023-04-07 19:57:19 -07:00
parent 6d96736fad
commit 47e6c8139e
2 changed files with 85 additions and 14 deletions

View File

@ -26,3 +26,4 @@ struct scores {
void scores_(struct scores *);
struct scores scores(struct journal *);
int scores_is_empty(const struct scores *);
const char *scores_to_string(const struct scores *);

View File

@ -102,8 +102,8 @@ static int scan(union date32 date, const char *const buffer,
case TREE_ERROR: goto catch;
case TREE_ABSENT: *pidx = idx; break;
}
date32_to_string(date, &datestr);
printf("%s: <%.*s>\n", datestr, (int)(s1 - s0), s0);
/*date32_to_string(date, &datestr);
fprintf(stderr, "%s: <%.*s>\n", datestr, (int)(s1 - s0), s0);*/
continue;
} }
/* New score. */
@ -116,7 +116,7 @@ static int scan(union date32 date, const char *const buffer,
}
if(!(score = scorelist_array_new(&scores->list))) goto catch;
*idx = (size_t)(score - scores->list.data);
/*struct pair key, name; union date32 date; unsigned edges;*/
/*struct pair key, name; union date32 date, last; unsigned edges;*/
score->key.a = s0, score->key.b = s1;
score->name.a = 0, score->name.b = 0;
score->date.u32 = score->last.u32 = 0;
@ -126,10 +126,6 @@ static int scan(union date32 date, const char *const buffer,
datestr, (int)(s1 - s0), s0, *idx);
goto also_add_to_tree;
}
/*ws* @name0 [^\x00\n;]+ @name1 ";"
ws* @year0 natural @year1 "-" @month [0-1][0-9] "-" @day [0-1][0-9] ";"
ws*
/ "\n" => skip*/
<score_name> * { why = "name unrecognized"; goto catch; }
<score_date> * { why = "date unrecognized"; goto catch; }
<score_edges> * { why = "edges unrecognized"; goto catch; }
@ -193,17 +189,91 @@ finally:
int scores_is_empty(const struct scores *const s)
{ return !s || !s->dates.root.node; }
const char *scores_to_string(const struct scores *const s)
{ return assert(s), scorelist_array_to_string(&s->list); }
#include <stdlib.h>
int main(void) {
struct journal j = journal();
if(journal_is_empty(&j)) goto catch;
struct scores s = scores(&j);
if(scores_is_empty(&s)) goto catch;
const char *fail = 0;
struct journal jrnl = {0};
struct scores scrs = {0};
errno = 0;
jrnl = journal();
fprintf(stderr, "Journal: %s.\n", journal_to_string(&jrnl));
if(journal_is_empty(&jrnl)) { fail = "journal failed to load"; goto catch; }
scrs = scores(&jrnl);
fprintf(stderr, "Scores: %s.\n", scores_to_string(&scrs));
if(scores_is_empty(&scrs)) { fail = "scores failed to parse"; goto catch; }
#if 1
struct score_tree_iterator it = score_tree_iterator(&scrs.dates);
union line64 line;
struct score *score;
printf("set terminal pngcairo dashed transparent truecolor"
" size 840, 480 fontscale 1\n"
"set output \"score.png\"\n");
printf("$Data <<EOD\n"
"# date, key\n");
while(score_tree_next(&it)) {
line = score_tree_key(&it);
score = scrs.list.data + *score_tree_value(&it);
char datestr[12];
date32_to_string(line.date, &datestr);
printf("%s, %.*s\n", datestr,
(int)(score->key.b - score->key.a), score->key.a);
}
printf("EOD\n"
"# theozh https://stackoverflow.com/a/75466214/2472827\n"
"# get a unique list from datablock\n"
"addToList(list,col) = list.( strstrt(list,'\"'.strcol(col).'\"') \\\n"
" > 0 ? '' : ' \"'.strcol(col).'\"')\n"
"Uniqs = ''\n"
"stats $Data u (Uniqs=addToList(Uniqs,2)) nooutput\n"
"Uniq(i) = word(Uniqs,i)\n"
"getIndex(s) = sum [_i=1:words(Uniqs)] s eq word(Uniqs,_i) ? _i : 0\n"
"\n"
/*"stats $Data u 3 nooutput\n"
"cumsum = STATS_sum\n"
"stats $Data u 4 nooutput\n"
"setsum = STATS_sum\n"
"\n"*/
"myTimeFmt = \"%%Y-%%m-%%d\"\n"
"set format x myTimeFmt timedate\n"
"set xtics format myTimeFmt rotate by -30\n"
"set ylabel \"happiness CDF (days)\"\n"
"set grid\n"
"set key out reverse Left noautotitle\n"
"set style fill solid 0.5\n"
"unset border\n"
"set autoscale xfix # max? hack: can't get x to extend further\n"
"\n"
/*"set label sprintf(\"%%u cumulative words (duplicate verses counted)\","
" cumsum) center at graph 0.5, first cumsum*100/%zu offset 0,0.5\n"
"set label sprintf(\"%%u unique KJV verse words memorized\", setsum) "
"center at graph 0.5, first setsum*100/%zu offset 0,0.5\n"
"\n"*/
"plot \\\n"
" total=0 $Data u"
" (timecolumn(1,myTimeFmt)):(total=total+1) \\\n"
" w steps lc \"black\" dt 1 lw 1, \\\n"
" total=0 '' u \\\n"
" (timecolumn(1,myTimeFmt)): \\\n"
" (total=total+1,total/2.): \\\n"
" (43200): \\\n"
" (total/2.): \\\n"
" (getIndex(strcol(2))) w boxxy lc var lw 1, \\\n"
" for [i=1:words(Uniqs)] keyentry w boxxy lc i ti Uniq(i)\n");
#endif
goto finally;
catch:
perror("score");
if(!fail) fail = "score";
perror(fail);
finally:
scores_(&s);
return EXIT_SUCCESS;
scores_(&scrs);
journal_(&jrnl);
return fail ? EXIT_FAILURE : EXIT_SUCCESS;
}