Created
September 13, 2015 17:43
-
-
Save jamtur01/6de29e5082776fff3831 to your computer and use it in GitHub Desktop.
Sparklines generator modified from Jonathan A Watmough
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Sparkline Generator | |
; Copyright (c) Jonathan A Watmough. All Rights Reserved. | |
; | |
; The use and distribution terms for this software are covered by the | |
; Common Public License 1.0 (http://opensource.org/licenses/cpl1.0.txt). | |
; By using this software in any fashion, you are agreeing to be bound by | |
; the terms of this license. Do not remove this notice. | |
(import 'java.io.File) | |
(defn | |
#^{ :doc "Scale a list of numeric 'values' to a max height of 'height'." | |
:private true} | |
scale-values ([values height] | |
(let [highest (apply max (filter identity values))] | |
(map #(if % (dec (- height (/ (* % (- height 3)) highest))) nil) values)))) | |
(defn | |
#^{:doc "Make a Win-style java.awt.image.BufferedImage of 'width' x 'height'."} | |
make-buffered-image ([width height] | |
(let [image-type (. java.awt.image.BufferedImage TYPE_4BYTE_ABGR)] | |
(new java.awt.image.BufferedImage width height image-type)))) | |
(defn | |
#^{:doc "Create pixel-accurate sparkline bitmap graphic from {:width :height :values :marker}. :marker=-1 for last entry."} | |
make-sparkline ([{ w :width h :height v :values m :marker }] | |
(let [v (scale-values v h) | |
bitmap (make-buffered-image w h) | |
g (. bitmap (getGraphics)) | |
m (if (and m (< m 0)) (+ m (count v)) m) | |
step (/ (- w 3) (- (count v) 1))] | |
(do (doto g (.setColor (. java.awt.Color white)) | |
(.fillRect 0 0 w h) | |
(.setColor (. java.awt.Color gray))) | |
(dotimes [idx (dec (count v))] | |
(let [x-pos (inc (* idx step)) | |
prev-val (nth v idx) | |
this-val (nth v (inc idx))] | |
(when (and prev-val this-val) | |
(. g (drawLine x-pos (dec prev-val) (+ x-pos step) (dec this-val)))))) | |
; Draw marker if present and return the sparkline bitmap | |
(when (and m (nth v m)) | |
(let [bx (* m step) | |
by (- (nth v m) 2)] | |
(doto g (.setColor (. java.awt.Color red)) | |
(.fillOval bx by 2 2)))) | |
(. g (dispose)) | |
bitmap)))) | |
(defn | |
#^{:doc "Test code for sparklines graphic generator."} | |
test-sparklines [] | |
(let [spark (make-sparkline {:width 100 :height 30 | |
:values [1 2 10 8 2 5 8 12 14 3 4 15] | |
:marker -1 | |
})] | |
(. javax.imageio.ImageIO write spark "png" (File. "test.png")))) | |
(test-sparklines) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment