Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add intermediate bar and scatter examples;
- Loading branch information
Showing
11 changed files
with
928 additions
and
445 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset='utf-8'> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<script src="js/basics-dev.js"></script> | ||
<script>rid3.basics.main();</script> | ||
</body> | ||
</html> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset='utf-8'> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<script src="js/examples-dev.js"></script> | ||
<script>rid3.examples.main();</script> | ||
</body> | ||
</html> |
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
(ns rid3.bar-intermediate | ||
(:require | ||
[reagent.core :as reagent] | ||
[rid3.core :as rid3] | ||
[rid3.examples-util :as util] | ||
[goog.object :as gobj] | ||
)) | ||
|
||
(def cursor-key :bar-intermediate) | ||
|
||
(def height 140) | ||
(def width 260) | ||
|
||
(def margin {:top 8 | ||
:left 32 | ||
:right 16 | ||
:bottom 40}) | ||
|
||
(defn translate [left top] | ||
(str "translate(" | ||
(or left 0) | ||
"," | ||
(or top 0) | ||
")")) | ||
|
||
(def color | ||
(-> js/d3 | ||
(.scaleOrdinal #js ["#3366CC" | ||
"#DC3912" | ||
"#FF9900"]))) | ||
|
||
(def transition-duration 800) | ||
|
||
|
||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;; Dataset | ||
|
||
(defn ->mock-dataset [] | ||
(mapv #(hash-map :label % | ||
:value (rand-int 200)) | ||
["A" "B" "C"])) | ||
|
||
(defn prepare-dataset [ratom] | ||
(-> @ratom | ||
(get :dataset) | ||
clj->js)) | ||
|
||
|
||
|
||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;; Scales | ||
|
||
(defn ->x-scale [ratom] | ||
(let [{:keys [dataset]} @ratom | ||
labels (mapv :label dataset)] | ||
(-> js/d3 | ||
.scaleBand | ||
(.rangeRound #js [0 width]) | ||
(.padding 0.1) | ||
(.domain (clj->js labels))))) | ||
|
||
(defn ->y-scale [ratom] | ||
(let [{:keys [dataset]} @ratom | ||
values (mapv :value dataset) | ||
max-value (apply max values)] | ||
(-> js/d3 | ||
.scaleLinear | ||
(.rangeRound #js [height 0]) | ||
(.domain #js [0 max-value])))) | ||
|
||
|
||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;; Axes | ||
|
||
(defn lighten-axis [node] | ||
;; lighten path | ||
(-> node | ||
(.select "path") | ||
(.style "stroke" "lightgrey")) | ||
|
||
;; light text | ||
(-> node | ||
(.selectAll ".tick text") | ||
(.style "fill" "#404040")) | ||
|
||
;; light line | ||
(-> node | ||
(.selectAll ".tick line") | ||
(.style "stroke" "lightgrey"))) | ||
|
||
|
||
(defn x-axis-did-mount [node ratom] | ||
(let [x-scale (->x-scale ratom)] | ||
(-> node | ||
(.attr "transform" (translate 0 height)) | ||
(.call (.axisBottom js/d3 x-scale))) | ||
(lighten-axis node))) | ||
|
||
|
||
(defn ->y-axis [did-mount?] | ||
(fn [node ratom] | ||
(let [y-scale (->y-scale ratom)] | ||
|
||
;; create axis | ||
(if did-mount? | ||
(-> node | ||
(.call (-> (.axisLeft js/d3 y-scale) | ||
(.ticks 3)))) | ||
;; Add did-update version that transitions on re-render (i.e., | ||
;; when data changes) | ||
(-> node | ||
.transition | ||
(.duration transition-duration) | ||
(.call (-> (.axisLeft js/d3 y-scale) | ||
(.ticks 3))))) | ||
|
||
(lighten-axis node) | ||
))) | ||
|
||
|
||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;; Bars | ||
|
||
(defn ->bars [did-mount?] | ||
(fn [node ratom] | ||
(let [y-scale (->y-scale ratom) | ||
x-scale (->x-scale ratom)] | ||
|
||
;; common | ||
(-> node | ||
(.attr "x" (fn [d] | ||
(let [label (gobj/get d "label")] | ||
(x-scale label)))) | ||
(.attr "fill" (fn [d i] | ||
(color i))) | ||
(.attr "width" (.bandwidth x-scale))) | ||
|
||
;; only for did-mount, set have bars grow from x-axis | ||
(when did-mount? | ||
(-> node | ||
(.attr "height" 0) | ||
(.attr "y" height))) | ||
|
||
;; add transition to bars height on page load and when data | ||
;; changes | ||
(-> node | ||
.transition | ||
(.duration transition-duration) | ||
(.attr "height" (fn [d] | ||
(let [value (gobj/get d "value")] | ||
(- height | ||
(y-scale value))))) | ||
(.attr "y" (fn [d] | ||
(let [value (gobj/get d "value")] | ||
(y-scale value)))))))) | ||
|
||
|
||
|
||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
;; Example | ||
|
||
(defn example [app-state] | ||
(let [viz-ratom (reagent/cursor app-state [cursor-key])] | ||
(reset! viz-ratom {:dataset (->mock-dataset)}) | ||
(fn [app-state] | ||
[:div | ||
[:h4 "Intermediate Bar Chart"] | ||
[util/link-source (name cursor-key)] | ||
[:button | ||
{:on-click #(swap! viz-ratom assoc :dataset (->mock-dataset))} | ||
"Randomize data"] | ||
|
||
[rid3/viz | ||
{:id "bar-intermediate" | ||
:ratom viz-ratom | ||
:svg {:did-mount | ||
(fn [node ratom] | ||
(-> node | ||
(.attr "width" (+ width | ||
(get margin :left) | ||
(get margin :right))) | ||
(.attr "height" (+ height | ||
(get margin :top) | ||
(get margin :bottom)))))} | ||
|
||
:main-container {:did-mount | ||
(fn [node ratom] | ||
(-> node | ||
(.attr "transform" (translate | ||
(get margin :left) | ||
(get margin :right)))))} | ||
:pieces | ||
[{:kind :container | ||
:class "x-axis" | ||
:did-mount x-axis-did-mount} | ||
|
||
{:kind :container | ||
:class "y-axis" | ||
:did-mount (->y-axis true) | ||
:did-update (->y-axis false)} | ||
|
||
{:kind :elem-with-data | ||
:class "bars" | ||
:tag "rect" | ||
:prepare-dataset prepare-dataset | ||
:did-mount (->bars true) | ||
:did-update (->bars false) | ||
}] | ||
}] | ||
]))) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
|
||
(def margin {:top 8 | ||
:left 32 | ||
:right 8 | ||
:right 16 | ||
:bottom 40}) | ||
|
||
(defn translate [left top] | ||
|
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
Oops, something went wrong.