Skip to content

Instantly share code, notes, and snippets.

@pmonks
Last active November 19, 2018 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmonks/360fee12745c0c95ad1fbf87b6bb66d8 to your computer and use it in GitHub Desktop.
Save pmonks/360fee12745c0c95ad1fbf87b6bb66d8 to your computer and use it in GitHub Desktop.
quil-radar-chart.clj
; Start a REPL with `lein try quil`, on JDK 1.8 (quil is not yet functional on JRE 9+)
(require '[quil.core :as q])
; Tunables (feel free to play with these)
(def window-size [1000 1000]) ; Initial size of the window
(def num-dimensions 9) ; Number of "dimensions" (spokes) to draw
(def num-circles 10) ; Number of circles in the chart
(def save-image (atom true)) ; Save the image (to a PNG) once, after it's first drawn
; Other constants (do not touch!)
(def circle-angle (* 2 Math/PI))
(def line-angles (map (partial * (/ circle-angle num-dimensions)) (range num-dimensions)))
(defn diagram-size
[]
(- (min (q/height) (q/width)) 10))
(defn draw-segment
[start-x start-y length angle]
(let [end-x (+ start-x (* length (Math/cos angle)))
end-y (+ start-y (* length (Math/sin angle)))]
(q/line start-x start-y end-x end-y)))
(defn draw-bullseye
[start-x start-y num-circles diagram-size]
(let [width-step (/ diagram-size num-circles)
circle-widths (map (partial * width-step) (map (partial + 1) (range num-circles)))]
(q/no-fill)
(doall (map #(q/ellipse start-x start-y % %) circle-widths))))
(defn draw-radar-chart
[num-circles num-dimensions diagram-size]
(q/background 255)
(q/with-translation [(/ (q/width) 2) (/ (q/height) 2)]
(draw-bullseye 0 0 num-circles diagram-size)
(doall (map (partial draw-segment 0 0 (/ diagram-size 2)) line-angles))))
(defn setup
[]
(q/frame-rate 4))
(defn settings
[]
(q/pixel-density (q/display-density)) ; Note: these are the only two things that can be set here - use :setup for everything else
(q/smooth 16))
(defn draw
[]
(draw-radar-chart num-circles num-dimensions (diagram-size))
(if @save-image
(do
(swap! save-image (constantly false))
(q/save "radar-chart.png"))))
; run sketch
(q/defsketch radar-chart
:title "Radar chart"
:features [:keep-on-top :resizable]
:size window-size
:setup setup
:settings settings
:draw draw)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment