Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
66 lines (59 sloc) 2.3 KB
(ns foreclojure.graphs
(:use compojure.core
(foreclojure utils)
(amalloy.utils [transform :only [with-adjustments]]
[reorder :only [reorder]]))
(:require (incanter [charts :as chart]
[core :as incanter]
[stats :as stats]))
(:import ( ByteArrayInputStream
(defn un-group
"Turn a compact set of [data-point num-repetitions] pairs into a
bunch of repeated data points so that incanter will make a histogram
of them."
(mapcat (partial apply (reorder repeat))
(defn fetch-score-frequencies [problem-id]
(into {}
(for [[k v] (:scores
(fetch-one :problems
:where {:_id problem-id}
:only [:scores])))]
[(Integer/parseInt (name k)), v])))
(defn make-problem-plot [id best curr]
(let [freqs (fetch-score-frequencies id)
chart-data (un-group freqs)
[best curr] [(or best curr) (or curr best)]
chart (chart/histogram chart-data
:title (str "League scores: problem " id)
:x-label "Solution length"
:y-label "How often"
:nbins 25)]
(when best
(chart/add-pointer chart best 0
:text "your best"
:angle :ne))
(when (and curr (not= curr best))
(chart/add-pointer chart curr 0
:text "this"
:angle :ne))
(when-not (> (count freqs) 1)
(chart/add-text chart best (freqs best 0)
"Very little golfing data - chart may suck"))
(doto chart (chart/set-theme :bw))))
(defn serve-plot [plot]
(let [out (ByteArrayOutputStream.)
in (do
(incanter/save plot out)
(ByteArrayInputStream. (.toByteArray out)))]
{:response 200
:headers {"Content-Type" "image/png"}
:body in}))
(defroutes graph-routes
(GET "/leagues/golf/:id" [id best curr]
(with-adjustments #(when (seq %) (Integer/parseInt %)) [id best curr]
(serve-plot (make-problem-plot id best curr)))))
Jump to Line
Something went wrong with that request. Please try again.