Skip to content

Commit 4a63a31

Browse files
committed
Merge branch 'heat-maps'
2 parents cc49f26 + ab97586 commit 4a63a31

11 files changed

Lines changed: 764 additions & 83 deletions

examples/heat-map.janet

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
###
2+
### Heat maps in 2 ways - closures vs. dataframes
3+
###
4+
5+
(import spork/charts)
6+
(import spork/gfx2d)
7+
8+
###
9+
### The closure driven way
10+
###
11+
(def cmap (get charts/color-maps :turbo))
12+
(defn distfrom [px py] (fn [x y] (let [dx (- px x) dy (- py y)] (math/sqrt (+ (* dx dx) (* dy dy))))))
13+
(def d1 (distfrom 10 10))
14+
(def d2 (distfrom 20 18))
15+
(def d3 (distfrom 37 8))
16+
(charts/heat-map-chart
17+
:width (* 2 1920) :height (* 2 1080)
18+
:num-columns (* 1 48) :num-rows (* 1 27)
19+
:cell-text-fn (fn [x y] (string/format "%d,%d" x y))
20+
:color-fn (fn [x y]
21+
(def t (min (* 0.1 (d1 x y)) (* 0.2 (d2 x y)) (* 0.03 (d3 x y))))
22+
(cmap (+ (* 0.01 (math/random)) t)))
23+
:box-gap 2
24+
:font (gfx2d/load-font "examples/fonts/Roboto-Regular.ttf" 24)
25+
:title "Heat Map Example"
26+
:save-as "tmp/heat-map.png")
27+
(print "Made heat map at tmp/heat-map.png")
28+
29+
###
30+
### The data-driven way
31+
###
32+
(def dfdata
33+
{:a (range 0 1 0.04)
34+
:b (range 0 1 0.03)
35+
:c (range 0 1 0.035)
36+
:d (range 0 2 0.029)
37+
:e (range 0.2 1.4 0.0391)})
38+
(charts/dark-mode)
39+
(charts/heat-map-chart
40+
:width 700 :height 700
41+
:data dfdata
42+
:legend :right
43+
:legend-labels (seq [x :range [0 5]] (case x 0 "" 4 "" (string/format "%.2f" (/ x 4))))
44+
:font (gfx2d/load-font "examples/fonts/Roboto-Regular.ttf" 24)
45+
:title "Heat Map"
46+
:color-map :viridis
47+
:save-as "tmp/heat-map-dataframe.png")
48+
(print "Made heat map at tmp/heat-map-dataframe.png")

examples/multi-charts.janet

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
###
2+
### See available color maps with heat charts
3+
###
4+
5+
(import spork/charts)
6+
(import spork/gfx2d)
7+
8+
(def dfdata
9+
{:a (range 0 1 0.04)
10+
:b (range 0 1 0.03)
11+
:c (range 0 1 0.035)
12+
:d (range 0 2 0.029)
13+
:e (range 0.2 1.4 0.0391)})
14+
(def font (gfx2d/load-font "examples/fonts/Roboto-Regular.ttf" 16))
15+
(defn make-a-chart
16+
[color-map]
17+
(charts/heat-map-chart
18+
:width 500 :height 500
19+
:data dfdata
20+
:legend :right
21+
:legend-labels (seq [x :range [0 5]] (case x 0 "" 4 "" (string/format "%.2f" (/ x 4))))
22+
:font font
23+
:legend-width 20
24+
:title (string ":" color-map)
25+
:color-map color-map))
26+
27+
# Make a grid of charts
28+
(def the-charts (map make-a-chart [:grayscale :turbo :viridis :magma]))
29+
(def outer-canvas (gfx2d/blank 1000 1100 4))
30+
(gfx2d/fill-rect outer-canvas 0 0 2000 2000 gfx2d/white)
31+
(def title "spork/charts color maps")
32+
(def [w h] (gfx2d/measure-text font title 3.0))
33+
(gfx2d/draw-text outer-canvas font (- 500 (div w 2)) 20 title gfx2d/black 3.0)
34+
(eachp [i chart] the-charts
35+
(def x (if (even? i) 0 500))
36+
(def y (if (> i 1) 100 600))
37+
(gfx2d/stamp outer-canvas chart x y))
38+
(gfx2d/save "tmp/chart-grid.png" outer-canvas)
39+
(print "Wrote to tmp/chart-grid.png")

0 commit comments

Comments
 (0)