[test-graphing] upgraded

This commit is contained in:
Christopher Lam 2019-06-08 23:42:54 +08:00
parent ddceb54736
commit a552d0d485

View File

@ -27,58 +27,86 @@
(use-modules (gnucash utilities)) (use-modules (gnucash utilities))
(use-modules (gnucash gnc-module)) (use-modules (gnucash gnc-module))
(debug-enable 'backtrace)
(gnc:module-load "gnucash/report/report-system" 0) (gnc:module-load "gnucash/report/report-system" 0)
(gnc:module-load "gnucash/gnome-utils" 0) ;for gnc-build-url (gnc:module-load "gnucash/gnome-utils" 0) ;for gnc-build-url
(define (simple-pie-chart) (define (simple-pie-chart)
(let ((chart (gnc:make-html-piechart))) (let ((chart (gnc:make-html-chart)))
(gnc:html-piechart-set-title! chart "Pie Chart Title") ;; the minimum chartjs-based html-chart requires the following settings
(gnc:html-piechart-set-subtitle! chart "Pie Chart SubTitle") (gnc:html-chart-set-type! chart 'pie)
(gnc:html-piechart-set-width! chart 320)
(gnc:html-piechart-set-height! chart 240) ;; title is either a string, or a list of strings
(gnc:html-piechart-set-data! chart '(25 45 30)) (gnc:html-chart-set-title! chart "Pie Chart Title")
(gnc:html-piechart-set-labels! chart '("foo" "bar" "baz")) (gnc:html-chart-set-width! chart '(pixels . 480))
(gnc:html-piechart-set-colors! chart (gnc:assign-colors 3)) (gnc:html-chart-set-height! chart '(pixels . 360))
chart
) ;; data-labels and data-series should be the same length
) (gnc:html-chart-set-data-labels! chart '("alpha" "beta" "delta"))
(gnc:html-chart-add-data-series! chart
"series-1" ;series name
'(25 45 30) ;pie ratios
(gnc:assign-colors 3)) ;colours
;; piechart doesn't need axes display:
(gnc:html-chart-set-axes-display! chart #f)
chart))
(define (simple-bar-chart stacked?) (define (simple-bar-chart stacked?)
(let ((chart (gnc:make-html-barchart)) (let ((chart (gnc:make-html-chart))
(text (gnc:make-html-text (gnc:html-markup-p "[bar goes here]")))) (colours (gnc:assign-colors 3)))
(gnc:html-barchart-set-title! chart "Bar Chart Title") (gnc:html-chart-set-title!
(gnc:html-barchart-set-subtitle! chart (gnc:html-string-sanitize "Bar Chart SubTitle")) chart (list "Bar Chart Title"
(gnc:html-barchart-append-row! chart '(25 45 30)) (gnc:html-string-sanitize "Bar Chart SubTitle")))
(gnc:html-barchart-append-row! chart '(75 55 70)) (gnc:html-chart-set-type! chart 'bar)
(gnc:html-barchart-set-width! chart 320) (gnc:html-chart-set-width! chart '(pixels . 480))
(gnc:html-barchart-set-height! chart 240) (gnc:html-chart-set-height! chart '(pixels . 360))
(gnc:html-barchart-set-row-labels! chart '("date1" "date2")) (gnc:html-chart-set-data-labels! chart '("water" "fire"))
(gnc:html-barchart-set-col-labels! chart '("dataset1" "dataset2" "dataset3")) (gnc:html-chart-add-data-series! chart
(gnc:html-barchart-set-col-colors! chart (gnc:assign-colors 3)) "dataset1"
(gnc:html-barchart-set-stacked?! chart stacked?) '(25 75)
(gnc:html-barchart-set-y-axis-label! chart "A Y Axis") (car colours))
(gnc:html-chart-add-data-series! chart
"dataset2"
'(45 55)
(cadr colours))
(gnc:html-chart-add-data-series! chart
"dataset3"
'(30 70)
(caddr colours))
(gnc:html-chart-set-stacking?! chart stacked?)
(gnc:html-chart-set-y-axis-label! chart "Y Axis")
chart)) chart))
(define (simple-scatter-chart) (define (simple-scatter-chart)
(let ((chart (gnc:make-html-scatter)) (let ((chart (gnc:make-html-chart)))
(text (gnc:make-html-text (gnc:html-markup-p "[scatter goes here]")))) (gnc:html-chart-set-type! chart 'line)
(gnc:html-scatter-set-title! chart "Scatter Title") (gnc:html-chart-set-title! chart "Scatter Title")
(gnc:html-scatter-set-subtitle! chart "Scatter SubTitle")
(gnc:html-scatter-add-datapoint! chart '(25 75)) (gnc:html-chart-set-data-labels! chart '("a" "b" "c" "d" "e"))
(gnc:html-scatter-add-datapoint! chart '(45 55)) (gnc:html-chart-set-width! chart '(pixels . 480))
(gnc:html-scatter-add-datapoint! chart '(70 30)) (gnc:html-chart-set-height! chart '(pixels . 360))
(gnc:html-scatter-set-width! chart 320)
(gnc:html-scatter-set-height! chart 240) ;; scatter plots require x/y coordinates for data points
(gnc:html-scatter-set-markercolor! chart (car (gnc:assign-colors 1))) (gnc:html-chart-add-data-series! chart
"xy-plot"
'(((x . 25) (y . 75))
((x . 45) (y . 55))
((x . 32) (y . 30)))
(gnc:assign-colors 3)
'borderWidth 1
'fill #f)
;; The following sets xaxis to linear rather than category and
;; also illustrates how to precisely select chartjs options. The
;; second parameter is a list of symbols to target a particular
;; chartJS option. See chartJS documentation.
(gnc:html-chart-set! chart '(options scales xAxes (0) type) "linear")
chart)) chart))
(define (options-generator) (define (options-generator)
(let* ((options (gnc:new-options))) (gnc:new-options))
(gnc:options-set-default-section options "Test Graphing")
options)
)
;; This is the rendering function. It accepts a database of options ;; This is the rendering function. It accepts a database of options
;; and generates an object of type <html-document>. See the file ;; and generates an object of type <html-document>. See the file
@ -88,9 +116,9 @@
;; defined above. ;; defined above.
(define (test-graphing-renderer report-obj) (define (test-graphing-renderer report-obj)
;; These are some helper functions for looking up option values. ;; These are some helper functions for looking up option values.
(define options (gnc:report-options report-obj))
(define (get-op section name) (define (get-op section name)
(gnc:lookup-option (gnc:report-options report-obj) section name)) (gnc:lookup-option options section name))
(define (op-value section name) (define (op-value section name)
(gnc:option-value (get-op section name))) (gnc:option-value (get-op section name)))
@ -124,22 +152,20 @@
document document
(gnc:make-html-text (gnc:html-markup-p "Scatter:"))) (gnc:make-html-text (gnc:html-markup-p "Scatter:")))
(gnc:html-document-add-object! document (simple-scatter-chart)) (gnc:html-document-add-object! document (simple-scatter-chart))
(gnc:html-document-add-object! (gnc:html-document-add-object!
document document
(gnc:make-html-text (gnc:make-html-text
(gnc:html-markup-p "Done."))) (gnc:html-markup-p "Done.")))
document document))
)
)
;; Here we define the actual report with gnc:define-report ;; Here we define the actual report with gnc:define-report
(gnc:define-report (gnc:define-report
;; The version of this report. ;; The version of this report.
'version 1 'version 1
;; The name of this report. This will be used, among other things, ;; The name of this report. This will be used, among other things,
;; for making its menu item in the main menu. You need to use the ;; for making its menu item in the main menu. You need to use the
;; untranslated value here! ;; untranslated value here!
@ -154,11 +180,11 @@
;; The name in the menu ;; The name in the menu
;; (only necessary if it differs from the name) ;; (only necessary if it differs from the name)
'menu-name "Sample graphs." 'menu-name "Sample graphs"
;; A tip that is used to provide additional information about the ;; A tip that is used to provide additional information about the
;; report to the user. ;; report to the user.
'menu-tip "Sample graphs." 'menu-tip "Sample graphs"
;; A path describing where to put the report in the menu system. ;; A path describing where to put the report in the menu system.
;; In this case, it's going under the utility menu. ;; In this case, it's going under the utility menu.
@ -166,7 +192,6 @@
;; The options generator function defined above. ;; The options generator function defined above.
'options-generator options-generator 'options-generator options-generator
;; 'options-generator gnc:new-options
;; The rendering function defined above. ;; The rendering function defined above.
'renderer test-graphing-renderer) 'renderer test-graphing-renderer)