Commit 2e6dc4c90652f68f6fc799235fb4a887ecef021f
working scrolling graph and likes / comments switcing for stats graph
mix irving committed on 4/23/2018, 8:53:03 AMParent: e2016e7e072aaa1539f05778a0610bd68e812782
Files changed
app/page/statsShow.js | changed |
app/page/statsShow.js | ||
---|---|---|
@@ -13,8 +13,12 @@ | ||
13 | 13 | 'sbot.obs.connection': 'first', |
14 | 14 | 'history.sync.push': 'first' |
15 | 15 | }) |
16 | 16 | |
17 | +const COMMENTS = 'comments' | |
18 | +const LIKES = 'likes' | |
19 | +const DAY = 24 * 60 * 60 * 1000 | |
20 | + | |
17 | 21 | exports.create = (api) => { |
18 | 22 | return nest('app.page.statsShow', statsShow) |
19 | 23 | |
20 | 24 | function statsShow (location) { |
@@ -22,71 +26,53 @@ | ||
22 | 26 | blogs: MutantArray([]), |
23 | 27 | comments: Dict(), |
24 | 28 | likes: Dict() |
25 | 29 | }) |
26 | - onceTrue(api.sbot.obs.connection, server => { | |
27 | - fetchBlogs({ server, store }) | |
28 | - }) | |
30 | + onceTrue(api.sbot.obs.connection, server => fetchBlogs({ server, store })) | |
29 | 31 | |
30 | 32 | var howFarBack = Value(0) |
31 | 33 | // stats show a moving window of 30 days |
32 | - const THIRTY_DAYS = 30 * 24 * 60 * 60 * 1000 | |
33 | - | |
34 | - const COMMENTS = 'comments' | |
35 | - const LIKES = 'likes' | |
36 | 34 | var context = Struct({ |
37 | 35 | focus: Value(COMMENTS), |
38 | 36 | blog: Value(), |
39 | 37 | range: computed([howFarBack], howFarBack => { |
40 | 38 | const now = Date.now() |
39 | + const endOfDay = (Math.floor(now / DAY) + 1) * DAY | |
41 | 40 | return { |
42 | - upper: now - howFarBack * THIRTY_DAYS, | |
43 | - lower: now - (howFarBack + 1) * THIRTY_DAYS | |
41 | + upper: endOfDay - howFarBack * 30 * DAY, | |
42 | + lower: endOfDay - (howFarBack + 1) * 30 * DAY | |
44 | 43 | } |
45 | 44 | }) |
46 | 45 | }) |
47 | 46 | |
48 | - var commentsAll = computed(throttle(dictToCollection(store.comments), 1000), (msgs) => { | |
49 | - return msgs | |
50 | - .map(c => c.value) | |
51 | - .reduce((n, sofar) => [...n, ...sofar], []) | |
47 | + var foci = Struct({ | |
48 | + [COMMENTS]: computed([throttle(store.comments, 1000)], (msgs) => { | |
49 | + return flatMap(msgs, (val, key) => val) | |
50 | + }), | |
51 | + [LIKES]: computed([throttle(store.likes, 1000)], (msgs) => { | |
52 | + return flatMap(msgs, (val, key) => val) | |
53 | + }) | |
54 | + | |
52 | 55 | }) |
53 | - // var commentsAll = computed([throttle(store.comments, 1000)], (msgs) => { | |
54 | - // return flatMap(msgs, (val, key) => { | |
55 | - // console.log(key, val) | |
56 | - // return val | |
57 | - // }) | |
58 | - // }) | |
59 | 56 | |
60 | - var visibleCommentsCount = computed([commentsAll, context.range], (msgs, range) => { | |
57 | + var visibleCommentsCount = computed([foci.comments, context.range], (msgs, range) => { | |
61 | 58 | return msgs |
62 | 59 | .filter(msg => { |
63 | 60 | const ts = msg.value.timestamp |
64 | - return ts >= range.lower && ts <= range.upper | |
61 | + return ts > range.lower && ts <= range.upper | |
65 | 62 | }) |
66 | 63 | .length |
67 | 64 | }) |
68 | 65 | |
69 | - var likesAll = computed(throttle(dictToCollection(store.likes), 1000), (msgs) => { | |
66 | + var visibleLikesCount = computed([foci.likes, context.range], (msgs, range) => { | |
70 | 67 | return msgs |
71 | - .map(c => c.value) | |
72 | - .reduce((n, sofar) => [...n, ...sofar], []) | |
73 | - }) | |
74 | - | |
75 | - var visibleLikesCount = computed([likesAll, context.range], (msgs, range) => { | |
76 | - return msgs | |
77 | 68 | .filter(msg => { |
78 | 69 | const ts = msg.value.timestamp |
79 | - return ts >= range.lower && ts <= range.upper | |
70 | + return ts > range.lower && ts <= range.upper | |
80 | 71 | }) |
81 | 72 | .length |
82 | 73 | }) |
83 | 74 | |
84 | - var focused = Struct({ | |
85 | - [LIKES]: commentsAll, | |
86 | - [COMMENTS]: likesAll | |
87 | - }) | |
88 | - | |
89 | 75 | const canvas = h('canvas', { height: 200, width: 600, style: { height: '200px', width: '600px' } }) |
90 | 76 | |
91 | 77 | const displayComments = () => context.focus.set(COMMENTS) |
92 | 78 | const displayLikes = () => context.focus.set(LIKES) |
@@ -169,10 +155,8 @@ | ||
169 | 155 | ]) |
170 | 156 | |
171 | 157 | var chart = new Chart(canvas.getContext('2d'), chartConfig({ context, chartData: [] })) |
172 | 158 | |
173 | - const toDay = ts => Math.floor(ts / (24 * 60 * 60 * 1000)) | |
174 | - | |
175 | 159 | // HACK - if the focus has changed, then zero the data |
176 | 160 | // this prevents the graph from showing some confusing animations when transforming between foci |
177 | 161 | var lastFocus = context.focus() |
178 | 162 | const zeroGraphOnFocusChange = (focus) => { |
@@ -181,17 +165,18 @@ | ||
181 | 165 | chart.update() |
182 | 166 | lastFocus = focus |
183 | 167 | } |
184 | 168 | } |
185 | - const chartData = computed([context.focus, focused], (focus, focused) => { | |
169 | + const toDay = ts => Math.floor(ts / DAY) | |
170 | + const chartData = computed([context.focus, foci], (focus, foci) => { | |
186 | 171 | zeroGraphOnFocusChange(focus) |
187 | - const msgs = focused[focus] | |
172 | + const msgs = foci[focus] | |
188 | 173 | const grouped = groupBy(msgs, m => toDay(m.value.timestamp)) |
189 | 174 | |
190 | 175 | var data = Object.keys(grouped) |
191 | 176 | .map(day => { |
192 | 177 | return { |
193 | - t: day * 24 * 60 * 60 * 1000, | |
178 | + t: day * DAY, | |
194 | 179 | y: grouped[day].length |
195 | 180 | } |
196 | 181 | }) |
197 | 182 | return data |
@@ -202,29 +187,37 @@ | ||
202 | 187 | |
203 | 188 | chart.update() |
204 | 189 | }) |
205 | 190 | |
206 | - const graphHeight = computed([chartData, context.range], (data, range) => { | |
191 | + watchAll([chartData, context.range], (data, range) => { | |
192 | + // const graphHeight = computed([chartData, context.range], (data, range) => { | |
207 | 193 | const { lower, upper } = range |
208 | 194 | const slice = data |
209 | - .filter(d => d.t >= lower && d.t <= upper) | |
195 | + .filter(d => d.t > lower && d.t <= upper) | |
210 | 196 | .map(d => d.y) |
211 | 197 | .sort((a, b) => a < b) |
212 | - const localMax = slice[0] ? Math.max(slice[0], 10) : 10 | |
213 | 198 | |
214 | - return localMax + (5 - localMax % 5) | |
215 | - }) | |
216 | - graphHeight(h => { | |
199 | + var h = slice[0] | |
200 | + if (!h || h < 10) h = 10 | |
201 | + else h = h + (5 - h % 5) | |
202 | + | |
217 | 203 | chart.options.scales.yAxes[0].ticks.max = h |
218 | 204 | |
219 | 205 | chart.update() |
220 | 206 | }) |
207 | + // graphHeight(h => { | |
208 | + // chart.options.scales.yAxes[0].ticks.max = h | |
209 | + // console.log('listen', h) | |
221 | 210 | |
211 | + // chart.update() | |
212 | + // }) | |
213 | + | |
222 | 214 | context.range(range => { |
223 | 215 | const { lower, upper } = range |
224 | 216 | |
225 | - chart.options.scales.xAxes[0].time.min = new Date(lower) | |
226 | - chart.options.scales.xAxes[0].time.max = new Date(upper) | |
217 | + chart.options.scales.xAxes[0].time.min = new Date(lower + DAY / 2) | |
218 | + chart.options.scales.xAxes[0].time.max = new Date(upper - DAY / 2) | |
219 | + // the squeezing in by DAY/2 is to stop data outside range from half showing | |
227 | 220 | |
228 | 221 | chart.update() |
229 | 222 | }) |
230 | 223 | |
@@ -279,26 +272,20 @@ | ||
279 | 272 | }) |
280 | 273 | ) |
281 | 274 | } |
282 | 275 | |
283 | -function chartConfig ({ context, chartData }) { | |
276 | +// TODO rm chartData and other overly smart things which didn't work from here | |
277 | +function chartConfig ({ context }) { | |
284 | 278 | const { lower, upper } = resolve(context.range) |
285 | 279 | |
286 | - const data = resolve(chartData) || [] | |
287 | - const slice = data | |
288 | - .filter(d => d.t >= lower && d.t <= upper) | |
289 | - .map(d => d.y) | |
290 | - .sort((a, b) => a < b) | |
291 | - const localMax = slice[0] ? Math.max(slice[0], 10) : 10 | |
292 | - | |
293 | 280 | return { |
294 | 281 | type: 'bar', |
295 | 282 | data: { |
296 | 283 | datasets: [{ |
297 | 284 | backgroundColor: 'hsla(215, 57%, 60%, 1)', |
298 | 285 | // Ticktack Primary color:'hsla(215, 57%, 43%, 1)', |
299 | 286 | borderColor: 'hsla(215, 57%, 60%, 1)', |
300 | - data | |
287 | + data: [] | |
301 | 288 | }] |
302 | 289 | }, |
303 | 290 | options: { |
304 | 291 | legend: { |
Built with git-ssb-web