[budget-barchart] upgraded

This commit is contained in:
Christopher Lam 2019-01-19 22:30:36 +08:00
parent cb978aad0d
commit f7ff85b8c3

View File

@ -41,6 +41,7 @@
(define optname-running-sum (N_ "Running Sum")) (define optname-running-sum (N_ "Running Sum"))
(define optname-chart-type (N_ "Chart Type")) (define optname-chart-type (N_ "Chart Type"))
(define opthelp-chart-type (N_ "Select which chart type to use"))
(define optname-plot-width (N_ "Plot Width")) (define optname-plot-width (N_ "Plot Width"))
(define optname-plot-height (N_ "Plot Height")) (define optname-plot-height (N_ "Plot Height"))
(define optname-from-date (N_ "Start Date")) (define optname-from-date (N_ "Start Date"))
@ -52,14 +53,10 @@
;(define (options-generator inc-exp?) ;(define (options-generator inc-exp?)
(define (options-generator) (define (options-generator)
(let* ( (let* ((options (gnc:new-options))
(options (gnc:new-options)) (add-option
;; This is just a helper function for making options. (lambda (new-option)
;; See libgnucash/scm/options.scm for details. (gnc:register-option options new-option))))
(add-option
(lambda (new-option)
(gnc:register-option options new-option)))
)
;; Option to select Budget ;; Option to select Budget
(add-option (gnc:make-budget-option (add-option (gnc:make-budget-option
gnc:pagename-general optname-budget gnc:pagename-general optname-budget
@ -95,39 +92,30 @@
;; Display tab ;; Display tab
(add-option (add-option
(gnc:make-multichoice-option (gnc:make-multichoice-option
gnc:pagename-display ;; tab name gnc:pagename-display ;; tab name
optname-chart-type ;; displayed option name optname-chart-type ;; displayed option name
"b" ;; localization in the tab "b" ;; localization in the tab
(N_ "This is a multi choice option.") ;; option help text opthelp-chart-type ;; option help text
'bars ;; default selectioin 'bars ;; default selectioin
(list (list
(list->vector (vector 'bars
(list 'bars (N_ "Barchart")
(N_ "Barchart") (N_ "Show the report as a bar chart."))
(N_ "Show the report as a bar chart.") (vector 'lines
) (N_ "Linechart")
) (N_ "Show the report as a line chart.")))))
(list->vector
(list 'lines
(N_ "Linechart")
(N_ "Show the report as a line chart.")
)
)
)
)
)
(gnc:options-add-plot-size! (gnc:options-add-plot-size!
options gnc:pagename-display options gnc:pagename-display
optname-plot-width optname-plot-height "c" (cons 'percent 100.0) (cons 'percent 100.0)) optname-plot-width optname-plot-height
"c" (cons 'percent 80) (cons 'percent 80))
;; Set default page ;; Set default page
(gnc:options-set-default-section options gnc:pagename-general) (gnc:options-set-default-section options gnc:pagename-general)
;; Return options ;; Return options
options options))
))
;; For each period in the budget: ;; For each period in the budget:
@ -137,124 +125,77 @@
;; Create bar and values ;; Create bar and values
;; ;;
(define (gnc:chart-create-budget-actual budget acct running-sum chart-type width height report-start-time report-end-time) (define (gnc:chart-create-budget-actual budget acct running-sum chart-type width height report-start-time report-end-time)
(let* ( (define curr (xaccAccountGetCommodity acct))
(chart #f) (define (amount->monetary amount)
) (gnc:monetary->string
(gnc:make-gnc-monetary curr amount)))
(let ((chart (gnc:make-html-chart)))
(gnc:html-chart-set-type! chart (if (eq? chart-type 'bars) 'bar 'line))
(gnc:html-chart-set-title! chart (xaccAccountGetName acct))
(gnc:html-chart-set-width! chart width)
(gnc:html-chart-set-height! chart height)
(gnc:html-chart-set-currency-iso! chart (gnc-commodity-get-mnemonic curr))
(gnc:html-chart-set-currency-symbol! chart (gnc-commodity-get-nice-symbol curr))
(gnc:html-chart-set-y-axis-label! chart (gnc-commodity-get-mnemonic curr))
(if (eqv? chart-type 'bars) ;; disable animation; with multiple accounts selected this report
(begin ;; will create several charts, all will want to animate
;; Setup barchart (gnc:html-chart-set! chart '(options animation duration) 0)
(set! chart (gnc:make-html-barchart)) (gnc:html-chart-set! chart '(options hover animationDuration) 0)
(gnc:html-barchart-set-title! chart (xaccAccountGetName acct)) (gnc:html-chart-set! chart '(options responsiveAnimationDuration) 0)
(gnc:html-barchart-set-width! chart width)
(gnc:html-barchart-set-height! chart height)
(gnc:html-barchart-set-row-labels-rotated?! chart #t)
(gnc:html-barchart-set-col-labels!
chart (list (_ "Budget") (_ "Actual")))
(gnc:html-barchart-set-col-colors!
chart '("#0074D9" "#FF4136"))
)
;; else
(begin
;; Setup linechart
(set! chart (gnc:make-html-linechart))
(gnc:html-linechart-set-title! chart (xaccAccountGetName acct))
(gnc:html-linechart-set-width! chart width)
(gnc:html-linechart-set-height! chart height)
(gnc:html-linechart-set-row-labels-rotated?! chart #t)
(gnc:html-linechart-set-col-labels!
chart (list (_ "Budget") (_ "Actual")))
(gnc:html-linechart-set-col-colors!
chart '("#0074D9" "#FF4136"))
)
)
;; Prepair vars for running sums, and to loop though periods ;; Prepair vars for running sums, and to loop though periods
(let* ( (let* ((num-periods (gnc-budget-get-num-periods budget))
(num-periods (gnc-budget-get-num-periods budget)) (period 0)
(period 0) (bgt-sum 0)
(bgt-sum 0) (act-sum 0)
(act-sum 0) (date (gnc-budget-get-period-start-date budget period))
(date (gnc-budget-get-period-start-date budget period)) (bgt-vals '())
(bgt-vals '()) (act-vals '())
(act-vals '()) (dates-list '()))
(date-iso-string-list '())
(save-fmt (qof-date-format-get))
)
;; make sure jqplot receives the date strings in ISO format (Bug763257)
(qof-date-format-set QOF-DATE-FORMAT-ISO)
;; Loop through periods ;; Loop through periods
(while (< period num-periods) (while (< period num-periods)
;;add calc new running sums ;;add calc new running sums
(if running-sum (when running-sum
(begin (set! bgt-sum
(set! bgt-sum (+ bgt-sum (+ bgt-sum
(gnc-numeric-to-double (gnc:get-account-period-rolledup-budget-value budget acct period)))
(gnc:get-account-period-rolledup-budget-value budget acct period)))) (set! act-sum
(set! act-sum (+ act-sum (+ act-sum
(gnc-numeric-to-double (gnc-budget-get-account-period-actual-value budget acct period))))
(gnc-budget-get-account-period-actual-value budget acct period)))) (when (<= report-start-time date)
) ;; within reporting period, update the display lists
) (unless running-sum
(if (<= report-start-time date) (set! bgt-sum
;; within reporting period, update the display lists (gnc:get-account-period-rolledup-budget-value budget acct period))
(begin (set! act-sum
(if (not running-sum) (gnc-budget-get-account-period-actual-value budget acct period)))
(begin (set! bgt-vals (cons bgt-sum bgt-vals))
(set! bgt-sum (set! act-vals (cons act-sum act-vals))
(gnc-numeric-to-double (set! dates-list (cons (qof-print-date date) dates-list)))
(gnc:get-account-period-rolledup-budget-value budget acct period)))
(set! act-sum
(gnc-numeric-to-double
(gnc-budget-get-account-period-actual-value budget acct period)))
)
)
(set! bgt-vals (append bgt-vals (list bgt-sum)))
(set! act-vals (append act-vals (list act-sum)))
(set! date-iso-string-list (append date-iso-string-list (list (qof-print-date date))))
)
)
;; prepare data for next loop repetition ;; prepare data for next loop repetition
(set! period (+ period 1)) (set! period (1+ period))
(set! date (gnc-budget-get-period-start-date budget period)) (set! date (gnc-budget-get-period-start-date budget period))
(if (< report-end-time date) (if (< report-end-time date)
(set! period num-periods) ;; reporting period has ended, break the loop (set! period num-periods)))
) (set! bgt-vals (reverse bgt-vals))
) (set! act-vals (reverse act-vals))
(set! dates-list (reverse dates-list))
;; restore the date strings format (gnc:html-chart-add-data-series! chart (_ "Budget") bgt-vals "#0074D9")
(qof-date-format-set save-fmt) (gnc:html-chart-add-data-series! chart (_ "Actual") act-vals "#FF4136")
(gnc:html-chart-set-data-labels! chart dates-list)
(if (eqv? chart-type 'bars) (if running-sum
(begin (gnc:html-chart-set-title!
;; Add data to the bar chart chart
(gnc:html-barchart-append-column! chart bgt-vals) (list (xaccAccountGetName acct)
(gnc:html-barchart-append-column! chart act-vals) (format #f "Bgt: ~a Act: ~a"
(gnc:html-barchart-set-row-labels! chart date-iso-string-list) (amount->monetary bgt-sum)
(if running-sum (amount->monetary act-sum))))))
(gnc:html-barchart-set-subtitle!
chart (format #f "Bgt: ~a Act: ~a" bgt-sum act-sum)))
)
;; else
(begin
;; Add data to the line chart
(gnc:html-linechart-append-column! chart bgt-vals)
(gnc:html-linechart-append-column! chart act-vals)
(gnc:html-linechart-set-row-labels! chart date-iso-string-list)
(if running-sum
(gnc:html-linechart-set-subtitle!
chart
(format #f "Bgt: ~a Act: ~a" bgt-sum act-sum)))
)
)
)
;; Return newly created chart ;; Return newly created chart
chart chart))
))
;; 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
@ -272,20 +213,10 @@
;; This is a helper function to find out the level of the account ;; This is a helper function to find out the level of the account
;; with in the account tree ;; with in the account tree
(define (get-account-level account level) (define (get-account-level account level)
(let ( (let ((parent (gnc-account-get-parent account)))
(parent (gnc-account-get-parent account))
)
(cond (cond
( ((null? parent) level)
(null? parent) ;; exit (else (get-account-level parent (1+ level))))))
level
)
(else
(get-account-level parent (+ level 1))
)
)
)
)
(let* ( (let* (
(budget (get-option gnc:pagename-general optname-budget)) (budget (get-option gnc:pagename-general optname-budget))
@ -304,8 +235,8 @@
(get-option gnc:pagename-general optname-from-date)))) (get-option gnc:pagename-general optname-from-date))))
(to-date-t64 (gnc:time64-end-day-time (to-date-t64 (gnc:time64-end-day-time
(gnc:date-option-absolute-time (gnc:date-option-absolute-time
(get-option gnc:pagename-general optname-to-date)))) (get-option gnc:pagename-general optname-to-date)))))
)
(cond (cond
((null? accounts) ((null? accounts)
;; No accounts selected ;; No accounts selected
@ -321,33 +252,23 @@
;; Else create chart for each account ;; Else create chart for each account
(else (else
(for-each (for-each
(lambda (acct) (lambda (acct)
(if (or (if (or (and (eq? depth-limit 'all)
(and (equal? depth-limit 'all) (null? (gnc-account-get-descendants acct)))
(null? (gnc-account-get-descendants acct)) (and (not (eq? depth-limit 'all))
)
(and (not (equal? depth-limit 'all))
(<= (get-account-level acct 0) depth-limit) (<= (get-account-level acct 0) depth-limit)
(null? (gnc-account-get-descendants acct)) (null? (gnc-account-get-descendants acct)))
) (and (not (eq? depth-limit 'all))
(and (not (equal? depth-limit 'all)) (= (get-account-level acct 0) depth-limit)))
(= (get-account-level acct 0) depth-limit)
)
)
(gnc:html-document-add-object! (gnc:html-document-add-object!
document document
(gnc:chart-create-budget-actual budget acct running-sum chart-type width height from-date-t64 to-date-t64) (gnc:chart-create-budget-actual
) budget acct running-sum chart-type
) width height from-date-t64 to-date-t64))))
) accounts)))
accounts
)
)
) ;; end cond
document document))
))
;; Here we define the actual report ;; Here we define the actual report
(gnc:define-report (gnc:define-report