forked from Gnucash/gnucash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html-chart.scm
488 lines (437 loc) · 21.2 KB
/
html-chart.scm
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; html-chart.scm : generate HTML programmatically, with support
;; for simple style elements.
;;
;; Added dependency on guile-json to help construct options. Migrated
;; from obsolete jquery and jqplot to modern chartjs instead in 2018
;; by Christopher Lam
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of
;; the License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, contact:
;;
;; Free Software Foundation Voice: +1-617-542-5942
;; 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
;; Boston, MA 02110-1301, USA gnu@gnu.org
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-module (gnucash report html-chart))
(use-modules (gnucash core-utils))
(use-modules (gnucash json builder)) ;for building JSON options
(use-modules (gnucash report html-utilities))
(use-modules (srfi srfi-9))
(use-modules (ice-9 match))
;; html-chart.scm
(export gnc:html-chart?)
(export gnc:make-html-chart)
(export gnc:html-chart-data)
(export gnc:html-chart-set-data!)
(export gnc:html-chart-width)
(export gnc:html-chart-set-width!)
(export gnc:html-chart-height)
(export gnc:html-chart-set-height!)
(export gnc:html-chart-type)
(export gnc:html-chart-set-type!)
(export gnc:html-chart-title)
(export gnc:html-chart-get)
(export gnc:html-chart-set!)
(export gnc:html-chart-currency-iso)
(export gnc:html-chart-set-currency-iso!)
(export gnc:html-chart-currency-symbol)
(export gnc:html-chart-set-currency-symbol!)
(export gnc:html-chart-format-style)
(export gnc:html-chart-set-format-style!)
(export gnc:html-chart-render)
(export gnc:html-chart-set-custom-x-axis-ticks?!)
(export gnc:html-chart-set-title!)
(export gnc:html-chart-set-data-labels!)
(export gnc:html-chart-set-axes-display!)
(export gnc:html-chart-set-custom-y-axis-ticks?!)
(export gnc:html-chart-clear-data-series!)
(export gnc:html-chart-set-x-axis-label!)
(export gnc:html-chart-set-stacking?!)
(export gnc:html-chart-set-grid?!)
(export gnc:html-chart-set-y-axis-label!)
(export gnc:html-chart-add-data-series!)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; utility functions for nested list handling
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; nested-alist-set! parameters are
;; lst - a nested alist e.g. (list (cons 'key1 'val1)
;; (cons 'key2 (list (cons 'key2-sub1 'val2a))))
;; path - a list of symbols or single-element list
;; e.g. '(key2 key2-sub1)
;; '(key2 key2-sub2 (0))
;; '(key3 key3-sub1 key3-sub1-sub1)
;; newval - the cdr of the innermost cons cell specified by the path
;;
;; see test-html-chart.scm for usage examples
(define (nested-alist-set! lst path newval)
(define (path->nested-alist path newval)
(let loop ((path (reverse path)) (result newval))
(match path
(() result)
((((? number? idx)) . tail)
(let ((v (make-vector (1+ idx))))
(vector-set! v idx result)
(loop tail v)))
((head . tail) (loop tail (list (cons head result)))))))
(let loop ((nested-lst lst) (path path))
(define (out-of-bound? n) (and (number? n) (>= n (vector-length nested-lst))))
(define (existing? n) (and (number? n) (pair? (vector-ref nested-lst n))))
(if (null? nested-lst) (throw 'invalid-state))
(match path
(() (throw 'invalid-state))
((((? out-of-bound? idx)) . _) (throw 'index-too-high idx))
((((? existing? idx)) . tail) (loop (vector-ref nested-lst idx) tail))
((((? number? idx)) . tail) (vector-set! nested-lst idx
(path->nested-alist tail newval)))
((head . tail)
(let ((pair (assq head nested-lst)))
(cond
((not pair) (list-cdr-set! nested-lst (1- (length nested-lst))
(path->nested-alist path newval)))
((null? tail) (set-cdr! pair newval))
(else (loop (cdr pair) tail))))))))
(define (nested-alist-get lst path)
(let loop ((nested-lst lst) (path path))
(define (out-of-bound? n) (and (number? n) (>= n (vector-length nested-lst))))
(match path
(() nested-lst)
((((? out-of-bound? idx)) . _) (throw 'index-too-high idx))
((((? number? idx)) . tail) (loop (vector-ref nested-lst idx) tail))
((head . tail)
(let ((pair (assq head nested-lst)))
(if pair
(loop (cdr pair) tail)
(throw 'invalid-path path)))))))
;; helper for setting data - guile-json expects vectors to be
;; transformed into JSON arrays; convert list to vector. if not list,
;; leave alone.
(define (list-to-vec v)
(if (list? v)
(list->vector v)
v))
;; The html-chart specifies options and data to create a chart. The
;; old chart toolkits guppi and jqplot seemed to require restrictive
;; specific formats for the options and data, and modern toolkit
;; chartjs is more permissive. Nonetheless some of the restrictions
;; remain.
;; At minimum the html-chart will require setting the following
;; fields:
;; type - one of 'bar 'line 'pie
;; width - pair
;; height - pair
(define-record-type <html-chart>
(make-html-chart width height chart-options currency-iso
currency-symbol format-style custom-x-axis-ticks? custom-y-axis-ticks?)
html-chart?
(width html-chart-width html-chart-set-width)
(height html-chart-height html-chart-set-height)
(chart-options html-chart-chart-options html-chart-set-chart-options)
(currency-iso html-chart-currency-iso html-chart-set-currency-iso)
(currency-symbol html-chart-currency-symbol html-chart-set-currency-symbol)
(format-style html-chart-format-style html-chart-set-format-style)
(custom-x-axis-ticks? html-chart-custom-x-axis-ticks?
html-chart-set-custom-x-axis-ticks?)
(custom-y-axis-ticks? html-chart-custom-y-axis-ticks?
html-chart-set-custom-y-axis-ticks?))
(define gnc:make-html-chart-internal make-html-chart)
(define gnc:html-chart? html-chart?)
(define gnc:html-chart-width html-chart-width)
(define gnc:html-chart-set-width! html-chart-set-width)
(define gnc:html-chart-height html-chart-height)
(define gnc:html-chart-set-height! html-chart-set-height)
(define gnc:html-chart-currency-iso html-chart-currency-iso)
(define gnc:html-chart-set-currency-iso! html-chart-set-currency-iso)
(define gnc:html-chart-currency-symbol html-chart-currency-symbol)
(define gnc:html-chart-set-currency-symbol! html-chart-set-currency-symbol)
(define gnc:html-chart-format-style html-chart-format-style)
(define gnc:html-chart-set-format-style! html-chart-set-format-style)
(define gnc:html-chart-custom-x-axis-ticks? html-chart-custom-x-axis-ticks?)
(define gnc:html-chart-set-custom-x-axis-ticks?! html-chart-set-custom-x-axis-ticks?)
(define gnc:html-chart-custom-y-axis-ticks? html-chart-custom-y-axis-ticks?)
(define gnc:html-chart-set-custom-y-axis-ticks?! html-chart-set-custom-y-axis-ticks?)
(define gnc:html-chart-get-options-internal html-chart-chart-options)
(define gnc:html-chart-set-options-internal! html-chart-set-chart-options)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; <html-chart> class
;; generate the <object> form for an html chart.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (gnc:make-html-chart)
(gnc:make-html-chart-internal
'(percent . 100) ;;width
'(percent . 100) ;;height
(list ;;chartjs options object
(cons 'type 'bar)
(cons 'data (list
(cons 'labels #())
(cons 'datasets #())))
(cons 'options (list
(cons 'maintainAspectRatio #f)
(cons 'animation (list
(cons 'duration 0)))
(cons 'chartArea (list
(cons 'backgroundColor "#fffdf6")))
(cons 'legend (list
(cons 'position 'right)
(cons 'reverse #f)
(cons 'labels (list
(cons 'fontColor 'black)))))
(cons 'elements (list
(cons 'line (list
(cons 'tension 0)))
(cons 'point (list
(cons 'pointStyle #f)))))
(cons 'tooltips (list
(cons 'callbacks (list
(cons 'label #f)))))
(cons 'scales (list
(cons 'xAxes (vector
(list
(cons 'display #t)
(cons 'type 'category)
(cons 'distribution 'series)
(cons 'offset #t)
(cons 'gridLines (list
(cons 'display #t)
(cons 'lineWidth 1.5)))
(cons 'scaleLabel (list
(cons 'display #t)
(cons 'labelString "")))
(cons 'ticks (list
(cons 'maxRotation 30))))
;; the following another xAxis at the top
'((position . top)
(ticks . ((display . #f)))
(gridLines . ((display . #f)
(drawTicks . #f))))
))
(cons 'yAxes (vector
(list
(cons 'stacked #f)
(cons 'display #t)
(cons 'gridLines (list
(cons 'display #t)
(cons 'lineWidth 1.5)))
(cons 'scaleLabel (list
(cons 'display 1.5)
(cons 'labelString "")))
(cons 'ticks (list
(cons 'beginAtZero #f))))
;; the following another yAxis on the right
'((position . right)
(ticks . ((display . #f)))
(gridLines . ((display . #f)
(drawTicks . #f))))
))))
(cons 'title (list
(cons 'display #t)
(cons 'fontStyle "")
(cons 'text ""))))))
"XXX" ;currency-iso
"\u00A4" ;currency-symbol
"currency";format-style
#t ;custom x-axis ticks?
#t ;custom y-axis ticks?
))
(define (gnc:html-chart-type chart)
(gnc:html-chart-get chart '(type)))
(define (gnc:html-chart-set-type! chart type)
(gnc:html-chart-set! chart '(type) type))
(define (gnc:html-chart-title chart)
(gnc:html-chart-get chart '(options title text)))
(define (gnc:html-chart-set-title! chart title)
(gnc:html-chart-set! chart '(options title text) title))
(define (gnc:html-chart-set-data-labels! chart labels)
(gnc:html-chart-set! chart '(data labels) labels))
(define (gnc:html-chart-set-axes-display! chart display?)
(gnc:html-chart-set! chart '(options scales xAxes (0) display) display?)
(gnc:html-chart-set! chart '(options scales yAxes (0) display) display?))
;; e.g.:
;; (gnc:html-chart-add-data-series! chart "label" list-of-numbers color
;; 'fill #t
;; 'urls "gnc-report:id=13#")
;;
;; chart - html-chart object
;; "label" - data series label eg. "Income"
;; data - a list-of-numbers specifying series data
;; color - a string specifying series colour
;;
;; other keys may be specified, see ChartJS documentation
;; 'fill - fill bar, or fill under line?
;; 'borderWidth - line width
;; 'urls - either a string (same url for whole data series)
;; or a list-of-string (individual url for each data point)
;; see javascript onClick event handler how they are decoded
(define* (gnc:html-chart-add-data-series! chart label data color . rest)
(let loop ((rest rest)
(newseries (list
(cons 'data (list-to-vec data))
(cons 'label label)
(cons 'backgroundColor (list-to-vec color))
(cons 'borderColor (list-to-vec color)))))
(match rest
(() (let* ((old-vec (gnc:html-chart-get chart '(data datasets)))
(old-len (vector-length old-vec))
(new-vec (make-vector (1+ old-len))))
(vector-move-left! old-vec 0 old-len new-vec 0)
(vector-set! new-vec old-len newseries)
(gnc:html-chart-set! chart '(data datasets) new-vec)))
((key val . rest) (loop rest (assq-set! newseries key (list-to-vec val)))))))
(define (gnc:html-chart-clear-data-series! chart)
(gnc:html-chart-set! chart '(data datasets) #()))
(define (gnc:html-chart-set-x-axis-label! chart label)
(gnc:html-chart-set! chart '(options scales xAxes (0) scaleLabel labelString) label))
(define (gnc:html-chart-set-stacking?! chart stack?)
(gnc:html-chart-set! chart '(options scales xAxes (0) stacked) stack?)
(gnc:html-chart-set! chart '(options scales yAxes (0) stacked) stack?))
(define (gnc:html-chart-set-grid?! chart grid?)
(gnc:html-chart-set! chart '(options scales xAxes (0) gridLines display) grid?)
(gnc:html-chart-set! chart '(options scales yAxes (0) gridLines display) grid?))
(define (gnc:html-chart-set-y-axis-label! chart label)
(gnc:html-chart-set! chart '(options scales yAxes (0) scaleLabel labelString) label))
(define (gnc:html-chart-get chart path)
(let ((options (gnc:html-chart-get-options-internal chart)))
(nested-alist-get options path)))
(define (gnc:html-chart-set! chart path val)
(let ((options (gnc:html-chart-get-options-internal chart))
(val-vec (list-to-vec val)))
(nested-alist-set! options path val-vec)
(gnc:html-chart-set-options-internal! chart options)))
(define JS-Number-to-String "
// The following snippet from MDN
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
var toLocaleStringSupportsOptions = (typeof Intl == 'object' && Intl && typeof Intl.NumberFormat == 'function');
// format a number e.g. 2.5 into monetary e.g. \"$2.50\" or other style formsty
function numformat(amount) {
if (toLocaleStringSupportsOptions) {
return amount.toLocaleString(undefined, {style:formsty, currency:curriso});
} else {
return currsym + amount.toLocaleString();
}
}
")
(define JS-setup "
function tooltipLabel(tooltipItem,data) {
var datasetLabel = data.datasets[tooltipItem.datasetIndex].label || 'Other';
var label = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
switch (typeof(label)) {
case 'number':
return datasetLabel + ': ' + numformat(label);
default:
return '';
}
}
function tooltipTitle(array,data) {
return data.labels[array[0].index]; }
// draw the background color
Chart.pluginService.register({
beforeDraw: function (chart, easing) {
if (chart.config.options.chartArea && chart.config.options.chartArea.backgroundColor) {
var ctx = chart.chart.ctx;
var chartArea = chart.chartArea;
ctx.save();
ctx.fillStyle = chart.config.options.chartArea.backgroundColor;
ctx.fillRect(chartArea.left, chartArea.top, chartArea.right - chartArea.left, chartArea.bottom - chartArea.top);
ctx.restore();
}
}
})
// copy font info from css into chartjs.
bodyStyle = window.getComputedStyle (document.querySelector ('body'));
Chart.defaults.global.defaultFontSize = parseInt (bodyStyle.fontSize);
Chart.defaults.global.defaultFontFamily = bodyStyle.fontFamily;
Chart.defaults.global.defaultFontStyle = bodyStyle.fontStyle;
titleStyle = window.getComputedStyle (document.querySelector ('h3'));
chartjsoptions.options.title.fontSize = parseInt (titleStyle.fontSize);
chartjsoptions.options.title.fontFamily = titleStyle.fontFamily;
chartjsoptions.options.title.fontStyle = titleStyle.fontStyle;
document.getElementById(chartid).onclick = function(evt) {
var activepoints = myChart.getElementAtEvent(evt);
var anchor = document.getElementById(jumpid);
switch (activepoints.length) {
case 0:
anchor.href = '';
anchor.textContent = '';
anchor.style = 'display: none';
break;
default:
var index = activepoints[0]['_index'];
var datasetIndex = activepoints[0]['_datasetIndex'];
var datasetURLs = myChart.data.datasets[datasetIndex].urls;
// console.log('index=',index,'datasetIndex=',datasetIndex);
anchor.style = 'position:absolute; top:' + (evt.clientY - 30) + 'px; left:' + (evt.clientX - 20) + 'px; display: block; padding: 5px; border-radius: 5px; background: #4E9CAF; text-align:center; color:white; z-index: 999;';
switch (typeof(datasetURLs)) {
case 'string':
anchor.href = datasetURLs;
anchor.textContent = loadstring;
break;
case 'object':
anchor.href = datasetURLs[index];
anchor.textContent = loadstring;
break;
default:
anchor.href = '';
anchor.textContent = '';
anchor.style = 'display: none';
}
}
}\n\n")
(define (get-options-string chart)
(scm->json-string
(gnc:html-chart-get-options-internal chart)
#:pretty #t))
(define (gnc:html-chart-render chart doc)
(define (size->str size)
(string-append
(number->string (cdr size))
(case (car size)
((pixels) "px")
((percent) "%"))))
(let* ((retval '())
(push (lambda (l) (set! retval (cons l retval))))
;; Use a unique chart-id for each chart. This prevents charts
;; clashing on multi-column reports
(id (symbol->string (gensym "chart"))))
(push (gnc:html-js-include "chartjs/Chart.bundle.min.js"))
;; the following hidden h3 is used to query style and copy onto chartjs
(push "<h3 style='display:none'></h3>")
(push (format #f "<div style='width:~a;height:~a;'>\n"
(size->str (gnc:html-chart-width chart))
(size->str (gnc:html-chart-height chart))))
(push (format #f "<a id='jump-~a' href='' style='display:none'></a>\n" id))
(push (format #f "<canvas id=~s></canvas>\n" id))
(push "</div>\n")
(push (format #f "<script id='script-~a'>\n" id))
(push (format #f "var curriso = ~s;\n" (gnc:html-chart-currency-iso chart)))
(push (format #f "var currsym = ~s;\n" (gnc:html-chart-currency-symbol chart)))
(push (format #f "var formsty = ~s;\n" (gnc:html-chart-format-style chart)))
(push (format #f "var chartid = ~s;\n" id))
(push (format #f "var jumpid = 'jump-~a';\n" id))
(push (format #f "var loadstring = ~s;\n" (G_ "Load")))
(push (format #f "var chartjsoptions = ~a;\n\n"
(get-options-string chart)))
(push JS-Number-to-String)
(when (gnc:html-chart-custom-y-axis-ticks? chart)
(push "chartjsoptions.options.scales.yAxes[0].ticks.callback = yAxisDisplay;\n")
(push "function yAxisDisplay(value,index,values) { return numformat(value); };\n"))
(when (gnc:html-chart-custom-x-axis-ticks? chart)
(push "chartjsoptions.options.scales.xAxes[0].ticks.callback = xAxisDisplay;\n")
(push "function xAxisDisplay(value,index,values) { return chartjsoptions.data.labels[index]; };\n"))
(push "chartjsoptions.options.tooltips.callbacks.label = tooltipLabel;\n")
(push "chartjsoptions.options.tooltips.callbacks.title = tooltipTitle;\n")
(push JS-setup)
(push "var myChart = new Chart(chartid, chartjsoptions);\n")
(push "</script>")
retval))