Added more graphing functions.

This commit is contained in:
Simon Brooke 2026-09-28 12:50:06 +01:00
parent 74afa02516
commit 5f7854cb0d
2 changed files with 31 additions and 1 deletions

View file

@ -5,7 +5,7 @@
[com.widdindustries/cljc.java-time "0.1.22"]
[org.xerial/sqlite-jdbc "3.7.15-M1"]]
:description "FIXME: write description"
:jvm-opts ["-Xmx4g"]
;; :jvm-opts ["-Xmx4g"]
:license {:name "GNU General Public License,version 2.0 or (at your option) an
y later version"
:url "https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html"}

View file

@ -122,6 +122,36 @@
(xy-chart {(format "Donations between\n%s and\n%s" start-date end-date)
[(range (count data)) (sort (map :donations data))]})))
(defn chart-donations-as-percentage-in-date-range
"Return a chart of donations received by individual accounts between
`start-date` (exclusive) and `end-date` (inclusive). Arguments should be
supplied as `java.time.LocalDate` objects, or as strings in the format
`yyyy-mm-dd`. Accounts are identified in the chart only by rank order."
[start-date end-date]
(let [data (accounts-with-donations-by-date-range start-date end-date)
donations (sort (map :donations data))
total (reduce + donations)
percentages (map #(* 100.0 (/ % total)) donations)]
(xy-chart {(format "Donations as percent\nof total donations\nbetween %s and\n%s" start-date end-date)
[(range (count data)) percentages]})))
(defn chart-accounts-with-donations-by-months-in-range
[start-date end-date]
(let [sd (cond (date? start-date) start-date
(string? start-date) (ld/parse start-date)
:else (ld/now))
ed (cond (date? end-date) end-date
(string? end-date) (ld/parse end-date)
:else (ld/now))]
(loop [s (.withDayOfMonth sd 1)
i (.withDayOfMonth (.plusMonths sd 1) 1)
m {}]
(let [data (accounts-with-donations-by-date-range s i)
m' (assoc m (format "Donations between\n%s and\n%s" s i)
[(range (count data)) (sort (map :donations data))])]
(if (ld/is-before ed i) (xy-chart m')
(recur i (.withDayOfMonth (.plusMonths i 1) 1) m'))))))
;; this does not work and I don't know why
;; (as-sql '(select 'campaign_donations
;; (fields :amount :campaign_url :created_at)