@ -4,13 +4,10 @@
;; Display expenses/incomes from various accounts as a pie chart
;; by Robert Merkel (rgmerk@mira.net)
( gnc:support "report/income-or-expense-pie.scm" )
( gnc:depend "report-html.scm" )
( gnc:depend "date-utilities.scm" )
( let ( )
;; Note the options-generator has a boolean argument, which
@ -23,62 +20,66 @@
( lambda ( new-option )
( gnc:register-option options new-option ) ) ) )
( gnc:options-add-date-interval!
options "Report Options"
( N_ "From" ) ( N_ "To" )
"d" )
( add-option
( gnc:make-number-range-option
( N_ "Report Options" ) ( N_ "Maximum Slices" )
"a" ( N_ "Maximum number of slices in pie" ) 7
2 20 0 1 ) )
( add-option
( gnc:make-account-list-option
( N_ "Report Options" ) ( N_ "Accounts" )
"b"
"Select accounts to calculate income on"
( N_ "Select accounts to calculate income on" )
( lambda ( )
( gnc:filter-accountlist-type
( if is-income? ' ( income ) ' ( expense ) )
( gnc:group-get- account-li st ( gnc:get-current-group ) ) ) )
( gnc:group-get- sub accounts ( gnc:get-current-group ) ) ) )
( lambda ( account )
( let ( ( type ( gw:enum-<gnc:AccountType>-val->sym
( gnc:account- type account )
( gnc:account- get- type account )
#f ) ) )
( member type ( if is-income? ' ( income ) ' ( expense ) ) ) ) )
#t ) )
( add-option
( gnc:make-currency-option
"Report Options"
"Report Currency"
( N_ "Report Options" ) ( N_ "Report Currency" )
"c"
"Select the display value for the currency"
( N_ "Select the display value for the currency" )
( gnc:locale-default-currency ) ) )
( gnc:options-add-date-interval!
options "Report Options"
( N_ "From" ) ( N_ "To" )
"d" )
( add-option
( gnc:make-number-range-option
( N_ "Display Format" ) ( N_ "Plot Width" )
"a" ( N_ "Width of plot in pixels." ) 400
"a" ( N_ "Width of plot in pixels." ) 5 00
100 1000 0 1 ) )
( add-option
( gnc:make-number-range-option
( N_ "Display Format" ) ( N_ "Plot Height" )
"b" ( N_ "Height of plot in pixels." ) 40 0
"b" ( N_ "Height of plot in pixels." ) 25 0
100 1000 0 1 ) )
( gnc:options-set-default-section options "Report Options" )
options ) )
;; Similar arrangement to the options-generator.
( define ( income-or-expense-pie-renderer report-obj is-income? )
;; These are some helper functions for looking up option values.
( define ( get-op section name )
( gnc:lookup-option ( gnc:report-options report-obj ) section name ) )
( define ( op-value section name )
( gnc:option-value ( get-op section name ) ) )
( let* (
( report-currency ( op-value "Report Options" "Report Currency" ) )
( let* ( ( max-slices ( op-value "Report Options" "Maximum Slices" ) )
( report-currency ( op-value "Report Options" "Report Currency" ) )
( height ( op-value "Display Format" "Plot Height" ) )
( width ( op-value "Display Format" "Plot Width" ) )
( accounts ( op-value "Report Options" "Accounts" ) )
@ -99,51 +100,82 @@
account
from-date-tp
to-date-tp
# t ) ) )
# f ) ) )
( profit-collector-list
( map profit-collector-fn accounts ) )
;;; FIXME: better currency handling here
( double-list
( map ( lambda ( commodity-collector )
( abs ( gnc:numeric-to-double
( cadr ( commodity-collector 'getpair report-currency #t ) ) ) ) )
( cadr ( commodity-collector 'getpair
report-currency #t ) ) ) ) )
profit-collector-list ) )
( account-name-list ( map gnc:account-get-name accounts ) ) )
( gnc:warn "account-name-list" account-name-list )
( gnc:html-piechart-set-title! chart ( if is-income?
( N_ "Income by Account" )
( N_ "Expenses by Account" ) ) )
( gnc:html-piechart-set-subtitle! chart ( string-append
( gnc:timepair-to-datestring from-date-tp )
" " ( N_ "to" ) " "
( gnc:timepair-to-datestring to-date-tp ) ) )
( combined ( zip double-list accounts ) )
( accounts-or-names ' ( ) ) )
( set! combined
( filter ( lambda ( pair ) ( not ( = 0.0 ( car pair ) ) ) )
combined ) )
( set! combined
( sort combined
( lambda ( a b ) ( > ( car a ) ( car b ) ) ) ) )
( if ( > ( length combined ) max-slices )
( let* ( ( start ( take combined ( - max-slices 1 ) ) )
( finish ( drop combined ( - max-slices 1 ) ) )
( sum ( apply + ( unzip1 finish ) ) ) )
( set! combined
( append start
( list ( list sum ( _ "Other" ) ) ) ) ) ) )
( call-with-values ( lambda ( ) ( unzip2 combined ) )
( lambda ( ds as )
( set! double-list ds )
( set! accounts-or-names as ) ) )
( gnc:html-piechart-set-title!
chart ( if is-income?
( N_ "Income by Account" )
( N_ "Expenses by Account" ) ) )
( gnc:html-piechart-set-subtitle!
chart ( sprintf #f
( _ "%s to %s" )
( gnc:timepair-to-datestring from-date-tp )
( gnc:timepair-to-datestring to-date-tp ) ) )
( gnc:html-piechart-set-width! chart width )
( gnc:html-piechart-set-height! chart height )
( gnc:html-piechart-set-data! chart double-list )
( gnc:html-piechart-set-labels! chart account-name-list )
( gnc:html-piechart-set-labels!
chart
( map ( lambda ( a ) ( if ( string? a ) a ( gnc:account-get-full-name a ) ) )
accounts-or-names ) )
( gnc:html-piechart-set-colors! chart
( gnc:assign-colors ( length combined ) ) )
( let ( ( urls ( map ( lambda ( a )
( if ( string? a ) "" ( gnc:account-anchor-text a ) ) )
accounts-or-names ) ) )
( gnc:html-piechart-set-button-1-slice-urls! chart urls )
( gnc:html-piechart-set-button-1-legend-urls! chart urls ) )
( gnc:html-document-add-object! document chart )
document ) )
( gnc:define-report
'version 1
'name ( N_ "Income Breakdown Piechart" )
'options-generator ( lambda ( ) ( options-generator #t ) )
'renderer ( lambda ( report-obj ) ( income-or-expense-pie-renderer report-obj #t ) ) )
'renderer ( lambda ( report-obj )
( income-or-expense-pie-renderer report-obj #t ) ) )
( gnc:define-report
'version 1
'name ( N_ "Expense Breakdown Piechart" )
'options-generator ( lambda ( ) ( options-generator #f ) )
'renderer ( lambda ( report-obj ) ( income-or-expense-pie-renderer report-obj #f ) ) ) )
'renderer ( lambda ( report-obj )
( income-or-expense-pie-renderer report-obj #f ) ) ) )