-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsleep-chart.js
183 lines (160 loc) · 8.28 KB
/
sleep-chart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Create a sleep chart
*
* @copyright Copyright 2020-2021 Sleep Diary Authors <sleepdiary@pileofstuff.org>
*
* @license
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
add_export("sleep_chart",function( activities, theme, start_at_midnight, timezone ) {
const bottom = (activities.length-1) * LINE_HEIGHT + TEXT_OFFSET;
let header = [], body = [], prev_day,
headings = [
[ '6pm' , 50 , '' ],
[ 'midnight', 183.75 , ' text-anchor="middle"' ],
[ '6am' , 322.5 , ' text-anchor="middle"' ],
[ 'noon' , 461.25 , ' text-anchor="middle"' ],
],
month_backgrounds = [],
month_labels = [],
prev_month_boundary = 0,
prev_month_string = '',
prev_month = -1,
// this formatter uses real times:
time_formatter = new Intl.DateTimeFormat(undefined, { timeZone: timezone, timeStyle: 'long', }),
// these formatters use timezoneless inputs:
date_formatter_short = new Intl.DateTimeFormat(undefined, { timeZone: "Etc/GMT", weekday: "short", day: "numeric", } ),
date_formatter_long = new Intl.DateTimeFormat(undefined, { timeZone: "Etc/GMT", year: 'numeric', month: 'long', day: 'numeric', weekday: 'long', }),
month_formatter = new Intl.DateTimeFormat(undefined, { timeZone: "Etc/GMT", year: "numeric", month: 'long', } )
;
function add_month(y,n) {
month_backgrounds.push('<rect class="month month-' + (month_backgrounds.length%2) + '" x="0" y="' + prev_month_boundary + '" width="600" height="' + (y-prev_month_boundary) + '"/>');
if ( prev_month_boundary && n!=activities.length-1 ) {
month_labels.push(
'<text class="shadow" text-anchor="end" x="591" y="' + (prev_month_boundary+TEXT_OFFSET+1) + '">' + prev_month_string + '</text>' +
'<text class="month month-' + (month_backgrounds.length%2) + '" text-anchor="end" x="590" y="' + (prev_month_boundary+TEXT_OFFSET) + '">' + prev_month_string + '</text>'
);
}
}
if ( start_at_midnight ) {
const title_0 = headings[0][0];
for ( let n=1; n!=headings.length; ++n ) {
headings[n-1][0] = headings[n][0];
}
headings[headings.length-1][0] = title_0;
}
headings.push([ headings[0][0], 595, ' text-anchor="end"' ]);
for ( let n=0; n!=activities.length; ++n ) {
const day = activities[activities.length-1-n],
y = n*LINE_HEIGHT
;
if ( n && (day||prev_day) ) {
header.push('<line class="notch" x1="0" y1="'+y+'" x2="600" y2="'+y+'"/>')
}
if ( day ) {
const date = day["id"].split(/[T-]/),
date_obj = new Date(date[0],date[1]-1,date[2]),
sleeps = day.activities.filter(
a => a.record.status == "asleep" && a.record.start && a.record.end
)
;
if ( prev_month != date_obj.getFullYear()*12 + date_obj.getMonth() ) {
add_month(y,n);
prev_month = date_obj.getFullYear()*12 + date_obj.getMonth();
prev_month_string = month_formatter.format(date_obj);
prev_month_boundary = y;
}
header.push(
'<text class="date day-'+date_obj.getDay()
+ '" text-anchor="end" x="44" y="'+(y+TEXT_OFFSET)+'">'
+ date_formatter_short.format(date_obj)
+ '<title>' + date_formatter_long.format(date_obj) + '</title>'
+ '</text>'
+ sleeps.map(
a => '<rect class="chart-sleep" x="' + (45+555*a.offset_start) + '" y="' + (y+3) + '" width="' +(555*(a.offset_end-a.offset_start)) + '" height="' + (LINE_HEIGHT-6) + '"/>'
).join('')
);
body.push(
sleeps
.map(
a =>
'<rect class="chart-sleep chart-sleep-overlay" x="' + (45+555*a.offset_start) + '" y="' + (y+3) + '" width="' +(555*(a.offset_end-a.offset_start)) + '" height="' + (LINE_HEIGHT-6) + '">'
+ '<title>' + time_formatter.format(new Date(a.record.start)) + ' - ' + time_formatter.format(new Date(a.record.end)) + '</title>'
+ '</rect>'
).join('')
);
prev_day = true;
} else {
header.push(
'<text class="date day-missing'
+ '" text-anchor="end" x="30" y="'+(y+TEXT_OFFSET)+'">···'
+ '<title>no data for this day</title>'
+ '</text>'
);
prev_day = false;
}
}
add_month(activities.length*LINE_HEIGHT,prev_month_boundary/LINE_HEIGHT);
return [
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 600 ' + (activities.length*LINE_HEIGHT) + '" class="sleep-chart ' + (theme||'') + '">'
+ '<style>'
+ 'svg.sleep-chart{width:100%;height:auto}'
+ '.sleep-chart text{font-family:sans-serif;font-size:' + (LINE_HEIGHT-4) + 'px;fill:black}'
+ '.sleep-chart text.heading,.sleep-chart text.month{fill:#ddd}'
+ '.sleep-chart text.month.month-0{fill:white}'
+ '.sleep-chart text.shadow{fill:black}'
+ '.sleep-chart .notch{stroke-dasharray:4;stroke:#7F7F7F}'
+ '.sleep-chart .day-0,.day-6{font-weight:bold}'
+ '.sleep-chart .day-missing{opacity: 0.5}'
+ '.sleep-chart .chart-sleep{fill:#0000FF;stroke:#6666CC}'
+ '.sleep-chart .chart-sleep-overlay{opacity: 0.5}'
+ '.sleep-chart .month-0 { fill: white }'
+ '.sleep-chart .month-1 { fill: #AAA }'
// dark theme:
+ '.sleep-chart.dark .month-0 { fill: #3F3F3F }'
+ '.sleep-chart.dark .month-1 { fill: #2F2F2F }'
+ '.sleep-chart.dark text,.sleep-chart.dark text.heading,.sleep-chart.dark text.month{fill:white}'
+ '.sleep-chart.dark text.shadow{fill:none}'
+ '</style>'
].concat(
month_backgrounds,
header,
[
'<line class="notch" x1="50" x2="50" y1="0" y2="' + bottom + '" />' +
'<line class="notch" x1="183.75" x2="183.75" y1="0" y2="' + bottom + '" />' +
'<line class="notch" x1="322.5" x2="322.5" y1="0" y2="' + bottom + '" />' +
'<line class="notch" x1="461.25" x2="461.25" y1="0" y2="' + bottom + '" />' +
'<line class="notch" x1="50" x2="50" y1="0" y2="' + bottom + '" />' +
'<line class="notch" x1="595" x2="595" y1="0" y2="' + bottom + '" />'
],
body,
month_labels,
headings.map( heading =>
'<text class="shadow" x="' + (heading[1]+1) + '" y="' + (TEXT_OFFSET+1) + '"' + heading[2] + '>' + heading[0] + '</text>' +
'<text class="shadow" x="' + (heading[1]+1) + '" y="' + (bottom+1) + '"' + heading[2] + '>' + heading[0] + '</text>' +
'<text class="heading" x="' + heading[1] + '" y="' + TEXT_OFFSET + '"' + heading[2] + '>' + heading[0] + '</text>' +
'<text class="heading" x="' + heading[1] + '" y="' + bottom + '"' + heading[2] + '>' + heading[0] + '</text>'
),
).join('') + (
'</svg>'
);
});