|
;;This is released under EPL (same as Clojure) and is most |
|
;;welcomed to be included to Incanter or anything else |
|
;;after some bettering (it would be nice to be able |
|
;;to push DefaultXYZDatasets into the heat-map-thing |
|
;;instead of function + xy-box |
|
|
|
(ns heatmapfromarray |
|
"show an vector-of-vectors without missing important parts of it" |
|
(use [incanter core charts])) |
|
|
|
;;an edited copy of incanter.chart/heat-map*: |
|
|
|
(defn heat-map-2d-vector* |
|
"parses an vector-of-vectors and show it as a dataset |
|
room for marvellous improvement" |
|
([data2d empty-val & options] |
|
(let [opts (when options (apply assoc {} options)) |
|
color? (if (false? (:color? opts)) false true) |
|
include-zero? (if (false? (:include-zero? opts)) false true) |
|
title (or (:title opts) "") |
|
x-label (or (:x-label opts) "rows") |
|
y-label (or (:y-label opts) "cols") |
|
z-label (or (:z-label opts) "value") |
|
theme (or (:theme opts) :default) |
|
xyz-dataset (org.jfree.data.xy.DefaultXYZDataset.) |
|
x-min 0 |
|
x-max (count data2d) |
|
y-min 0 |
|
y-max (apply max (map count data2d)) |
|
data-transposed (for [x (range x-min x-max 1) y (range y-min y-max 1)] |
|
{:x x :y y :z (get-in data2d [x y] empty-val)}) |
|
;; org.jfree-data.xy.DefaultXYZDataset wants data on the format: |
|
;;[[D x0 x1 x2 x3] [y0 y1 y2 y3 y4] [z0 z1 z2 z3]] |
|
data (into-array (map double-array [(map :x data-transposed) (map :y data-transposed) (map :z data-transposed)])) |
|
min-z (reduce min (last data)) |
|
max-z (reduce max (last data)) |
|
x-axis (doto (org.jfree.chart.axis.NumberAxis. x-label) |
|
(.setStandardTickUnits (org.jfree.chart.axis.NumberAxis/createIntegerTickUnits)) |
|
(.setLowerMargin 0.0) |
|
(.setUpperMargin 0.0) |
|
(.setAxisLinePaint java.awt.Color/white) |
|
(.setTickMarkPaint java.awt.Color/white) |
|
(.setAutoRangeIncludesZero include-zero?)) |
|
y-axis (doto (org.jfree.chart.axis.NumberAxis. y-label) |
|
(.setStandardTickUnits (org.jfree.chart.axis.NumberAxis/createIntegerTickUnits)) |
|
(.setLowerMargin 0.0) |
|
(.setUpperMargin 0.0) |
|
(.setAxisLinePaint java.awt.Color/white) |
|
(.setTickMarkPaint java.awt.Color/white) |
|
(.setAutoRangeIncludesZero include-zero?)) |
|
colors (or (:colors opts) |
|
[[0 0 127] [0 0 212] [0 42 255] [0 127 255] [0 127 255] |
|
[0 226 255] [42 255 212] [56 255 198] [255 212 0] [255 198 0] |
|
[255 169 0] [255 112 0] [255 56 0] [255 14 0] [255 42 0] |
|
[226 0 0]]) |
|
scale (if color? |
|
(org.jfree.chart.renderer.LookupPaintScale. min-z max-z java.awt.Color/white) |
|
(org.jfree.chart.renderer.GrayPaintScale. min-z max-z)) |
|
add-color (fn [idx color] |
|
(.add scale |
|
(+ min-z (* (/ idx (count colors)) (- max-z min-z))) |
|
(apply #(java.awt.Color. %1 %2 %3) color))) |
|
scale-axis (org.jfree.chart.axis.NumberAxis. z-label) |
|
legend (org.jfree.chart.title.PaintScaleLegend. scale scale-axis) |
|
renderer (org.jfree.chart.renderer.xy.XYBlockRenderer.) |
|
|
|
plot (org.jfree.chart.plot.XYPlot. xyz-dataset x-axis y-axis renderer) |
|
chart (org.jfree.chart.JFreeChart. plot)] |
|
(do |
|
(.setPaintScale renderer scale) |
|
(when color? (doseq [i (range (count colors))] |
|
(add-color i (nth colors i)))) |
|
(.addSeries xyz-dataset "Series 1" data) |
|
(.setBackgroundPaint plot java.awt.Color/lightGray) |
|
(.setDomainGridlinesVisible plot false) |
|
(.setRangeGridlinePaint plot java.awt.Color/white) |
|
(.setAxisOffset plot (org.jfree.ui.RectangleInsets. 5 5 5 5)) |
|
(.setOutlinePaint plot java.awt.Color/blue) |
|
(.removeLegend chart) |
|
(.setSubdivisionCount legend 20) |
|
(.setAxisLocation legend org.jfree.chart.axis.AxisLocation/BOTTOM_OR_LEFT) |
|
(.setAxisOffset legend 5.0) |
|
(.setMargin legend (org.jfree.ui.RectangleInsets. 5 5 5 5)) |
|
(.setFrame legend (org.jfree.chart.block.BlockBorder. java.awt.Color/red)) |
|
(.setPadding legend (org.jfree.ui.RectangleInsets. 10 10 10 10)) |
|
(.setStripWidth legend 10) |
|
(.setPosition legend org.jfree.ui.RectangleEdge/RIGHT) |
|
(.setTitle chart title) |
|
(.addSubtitle chart legend) |
|
(org.jfree.chart.ChartUtilities/applyCurrentTheme chart) |
|
(set-theme chart theme)) |
|
chart))) |
|
|
|
|
|
|
|
|
|
(comment |
|
(view (heat-map-2d-vector* [[10 10 0 1 1 1 2 2 2] |
|
[1 20 30 1 20 3 0 0 3] |
|
[1 2 10 1 10 1 2 2 2 ]] -10))) |
|
|
|
|