Commits

Steffen Dienst committed 76c36cc

[refactor] extract functions needed for interactive heatmap charts
(using sliders for data or manipulating the color scale)

Comments (0)

Files changed (1)

src/chart_utils/jfreechart.clj

   (java.awt.Color. r g b))
 
 (defn fixed-color-scale [color-scale values]
-  (map #(vector % (tripel2color (color-scale %))) values))
+  (map #(vector % (tripel2color (color-scale %))) values))
+
 (comment 
   (let [f (create-color-scale [0 [0 0 0]] [10 [255 0 0]] [20 [0 255 255]])]
     (map (comp tripel2color f) (range 21))
   and z calculated by applying f to the combinations of x and y with step widths
   x-step and y-step."
   ([f x-min x-max y-min y-max x-step y-step]
-     (let [x-vals (range x-min x-max (/ (- x-max x-min) x-step))
-           y-vals (range y-min y-max (/ (- y-max y-min) y-step))
-           xyz (for [_x x-vals _y y-vals] [_x _y (f _x _y)])
+     (let [x-vals (range x-min x-max (/ (- x-max x-min) x-step)) 
+           y-vals (range y-min y-max (/ (- y-max y-min) y-step)) 
+           xyz (for [_x x-vals _y y-vals] [_x _y (f _x _y)]) 
            transpose #(list (conj (first %1) (first %2))
                             (conj (second %1) (second %2))
                             (conj (nth %1 2) (nth %2 2)))]
        (reduce transpose [[] [] []] xyz))))
+
+(defn create-3d-dataset [^"[[D" data label]
+  (doto (org.jfree.data.xy.DefaultXYZDataset.)
+    (.addSeries label data)))
+
+(defn create-paint-scale [colors min-z max-z ^java.awt.Color default-color]
+  {:pre [(every? number? (map first colors)) (every? (partial instance? java.awt.Color) (map second colors))]}
+  (let [scale (org.jfree.chart.renderer.LookupPaintScale. min-z max-z default-color)]
+    (doseq [[i color] colors]
+      (.add scale i color))
+    scale))
+
+(defn set-paint-scale [chart scale]
+  (let [r (.. chart getPlot getRenderer)
+        l (.. chart getSubtitles (get 0))] 
+    (when (instance? org.jfree.chart.renderer.xy.XYBlockRenderer r) 
+      (.setPaintScale r scale))
+    (when (instance? org.jfree.chart.title.PaintScaleLegend l) 
+      (.setScale l scale))))
 
 (defn heat-map*
   ([function x-min x-max y-min y-max & options]
      x-step (or (:x-step opts) 100)
      y-step (or (:y-step opts) 100)
 	   theme (or (:theme opts) :default)
-	   xyz-dataset (org.jfree.data.xy.DefaultXYZDataset.)
 	   data (into-array (map double-array 
 				 (grid-apply function x-min x-max y-min y-max x-step y-step)))
+	   xyz-dataset (create-3d-dataset data "Series 1") 
 	   min-z (or (:z-min opts) (reduce min (last data)))
 	   max-z (or (:z-max opts) (reduce max (last data)))
 	   x-axis (doto (org.jfree.chart.axis.NumberAxis. x-label)
                    [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/cyan)
+            (create-paint-scale colors min-z max-z java.awt.Color/cyan)
             (org.jfree.chart.renderer.GrayPaintScale. min-z max-z))
 	   scale-axis (org.jfree.chart.axis.NumberAxis. z-label)
 	   legend (org.jfree.chart.title.PaintScaleLegend. scale scale-axis)
 	   chart (org.jfree.chart.JFreeChart. plot)]
        (do
 	(.setPaintScale renderer scale)
-	(when color? (doseq [[i color] colors]
-		       (.add scale i color)))
-	(.addSeries xyz-dataset "Series 1" data)
 	(.setBackgroundPaint plot java.awt.Color/lightGray)
 	(.setDomainGridlinesVisible plot false)
 	(.setRangeGridlinePaint plot java.awt.Color/white)