Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial vega-lite and charts viewers #31

Merged
merged 2 commits into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/clojure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ jobs:
- run: make install/chrome install/clojure
- run: make ci
- run: make app
if: ${{ github.event_name == 'push' }}
- uses: peaceiris/actions-gh-pages@v3
if: ${{ github.event_name == 'push' }}
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./target/pwa-release/
752 changes: 743 additions & 9 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"dependencies": {
"vega-lite": "^4.17.0",
"vega-embed": "^6.14.2",
"vega": "^5.17.3",
"csv-parse": "^4.14.2",
"react": "17.0.1",
"react-dom": "17.0.1",
Expand Down
130 changes: 130 additions & 0 deletions src/examples/data.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,135 @@
[:a {:href "https://github.com/djblue/portal"} "djblue/portal"]]
{:portal.viewer/default :portal.viewer/hiccup}))

(def line-chart
{:data {:values (map #(-> {:time % :value (Math/sin %)})
(range 0 (* 2 3.14) 0.25))}
:encoding {:x {:field "time" :type "quantitative"}
:y {:field "value" :type "quantitative"}}
:mark "line"})

(def bar-chart
{:data
{:values
[{:a "A", :b 28}
{:a "B", :b 55}
{:a "C", :b 43}
{:a "D", :b 91}
{:a "E", :b 81}
{:a "F", :b 53}]}
:mark "bar"
:encoding
{:x
{:field "a"
:type "nominal"
:axis {:labelAngle 0}}
:y {:field "b", :type "quantitative"}}})

(def pie-chart
{:description
"A simple pie chart with labels."
:data
{:values
[{:category "a", :value 4}
{:category "b", :value 6}
{:category "c", :value 10}
{:category "d", :value 3}
{:category "e", :value 7}
{:category "f", :value 8}]}
:encoding
{:theta
{:field "value"
:type "quantitative"
:stack true}
:color
{:field "category"
:type "nominal"
:legend nil}}
:layer
[{:mark {:type "arc", :outerRadius 80}}
{:mark {:type "text", :radius 90}
:encoding
{:text
{:field "category", :type "nominal"}}}]
:view {:stroke nil}})

(def tabular-data
(map #(-> {:x %
:y (#?(:clj Math/sin
:cljs js/Math.sin) %)})
(range 0 (* 2 3.14) 0.25)))

(def numerical-collection
{:x (range -3.12 3.14 0.1)
:y (map #(+ % (/ (* -1 % % %) 6))
(range -3.14 3.14 0.1))})

(def scatter-chart
{:$schema
"https://vega.github.io/schema/vega-lite/v4.json"
:description
"A scatterplot showing horsepower and miles per gallons for various cars."
:data {:url "https://vega.github.io/vega-lite/data/cars.json"}
:mark "point"
:encoding
{:x
{:field "Horsepower", :type "quantitative"}
:y
{:field "Miles_per_Gallon"
:type "quantitative"}}})

(def geographic-data
{:width "container"
:height "container"
:data {:url "https://vega.github.io/vega-lite/data/airports.csv"}
:projection {:type "albersUsa"}
:mark "circle"
:encoding
{:longitude
{:field "longitude", :type "quantitative"}
:latitude
{:field "latitude", :type "quantitative"}
:size {:value 10}}
:config {:view {:stroke "transparent"}}})

(def histogram-heatmap-2D
{:$schema
"https://vega.github.io/schema/vega-lite/v4.json"
:data {:url "https://vega.github.io/vega-lite/data/movies.json"}
:transform
[{:filter
{:and
[{:field "IMDB Rating", :valid true}
{:field "Rotten Tomatoes Rating"
:valid true}]}}]
:mark "rect"
:width "container"
:height "container"
:encoding
{:x
{:bin {:maxbins 60}
:field "IMDB Rating"
:type "quantitative"}
:y
{:bin {:maxbins 40}
:field "Rotten Tomatoes Rating"
:type "quantitative"}
:color
{:aggregate "count", :type "quantitative"}}
:config {:view {:stroke "transparent"}}})

(def data-visualization
{::vega-lite
{::line-chart line-chart
::pie-chart pie-chart
::bar-chart bar-chart
::scatter-chart scatter-chart
::histogram-heatmap-2D histogram-heatmap-2D
::geographic-data geographic-data}
::portal-charts
{::tabular-data tabular-data
::numerical-collection numerical-collection}})

(def data
{::platform-data platform-data
::hacker-news hn/stories
Expand All @@ -93,4 +222,5 @@
::themes c/themes
::clojure-data clojure-data
::hiccup hiccup
::data-visualization data-visualization
::string-data string-data})
6 changes: 6 additions & 0 deletions src/portal/ui/app.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[portal.ui.inspector :as ins :refer [inspector]]
[portal.ui.state :refer [state tap-state]]
[portal.ui.styled :as s]
[portal.ui.viewer.charts :as charts]
[portal.ui.viewer.csv :as csv]
[portal.ui.viewer.diff :as diff]
[portal.ui.viewer.edn :as edn]
Expand All @@ -18,6 +19,7 @@
[portal.ui.viewer.text :as text]
[portal.ui.viewer.transit :as transit]
[portal.ui.viewer.tree :as tree]
[portal.ui.viewer.vega-lite :as vega-lite]
[reagent.core :as r]))

(defn filter-data [settings value]
Expand Down Expand Up @@ -130,6 +132,10 @@

(def viewers
[ex/viewer
vega-lite/viewer
charts/line-chart
charts/scatter-chart
charts/histogram-chart
image/viewer
{:name :portal.viewer/map :predicate map? :component ins/inspect-map}
{:name :portal.viewer/coll :predicate coll? :component ins/inspect-coll}
Expand Down
2 changes: 1 addition & 1 deletion src/portal/ui/styled.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

(def exclude? #{:opacity :z-index})

(defn- style->css [style]
(defn style->css [style]
(reduce-kv
(fn [css k v]
(str
Expand Down
83 changes: 83 additions & 0 deletions src/portal/ui/viewer/charts.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
(ns portal.ui.viewer.charts
(:require [clojure.spec.alpha :as sp]
[portal.colors :as c]
[portal.ui.viewer.vega-lite :as v]))

;; collection of maps of [{:x 0 :y 0} ...] maps
(sp/def :tabular/x number?)
(sp/def :tabular/y number?)
(sp/def ::data
(sp/keys :req-un [:tabular/x :tabular/y]))
(sp/def ::tabular-data
(sp/coll-of ::data :min-count 2))

;; :x [0 1 2 ...] :y [0 1 2 ...]
(sp/def :numerical-coll/x
(sp/coll-of number? :min-count 2))
(sp/def :numerical-coll/y
(sp/coll-of number? :min-count 2))
(sp/def ::numerical-collection
(sp/keys :req-un [:numerical-coll/x :numerical-coll/y]))

(defn normalize-data
"Normalize data to conform to vega-lite specification"
[data]
(cond
(sp/valid? ::tabular-data data)
data
(sp/valid? ::numerical-collection data)
(for [[x y] (map vector (:x data) (:y data))] {:x x :y y})))

(defn line-chart-viewer
[settings value]
[v/vega-lite-viewer
settings
{:data
{:values (normalize-data value)}
:encoding
{:x {:field "x" :type "quantitative"}
:y {:field "y" :type "quantitative"}
:color {:value (::c/number settings)}}
:mark "line"
:selection {:grid {:type "interval" :bind "scales"}}}])

(defn scatter-chart-viewer
[settings value]
[v/vega-lite-viewer
settings
{:data {:values (normalize-data value)}
:encoding
{:x {:field "x" :type "quantitative"}
:y {:field "y" :type "quantitative"}
:color {:value (::c/number settings)}
:tooltip
[{:field "x" :type "quantitative"}
{:field "y" :type "quantitative"}]}
:mark "circle"
:selection {:grid {:type "interval" :bind "scales"}}}])

(defn histogram-chart-viewer
[settings value]
[v/vega-lite-viewer
settings
{:data {:values (normalize-data value)}
:mark "bar"
:encoding
{:x {:bin true :field "x" :type "quantitative"}
:y {:aggregate "count" :type "quantitative"}
:color {:value (::c/number settings)}}}])

(def line-chart
{:predicate (partial sp/valid? (sp/or :tabular ::tabular-data ::numerical-collection ::numerical-collection))
:component line-chart-viewer
:name :portal.viewer/line-chart})

(def scatter-chart
{:predicate (partial sp/valid? (sp/or :tabular ::tabular-data ::numerical-collection ::numerical-collection))
:component scatter-chart-viewer
:name :portal.viewer/scatter-chart})

(def histogram-chart
{:predicate (partial sp/valid? (sp/or :tabular ::tabular-data ::numerical-collection ::numerical-collection))
:component histogram-chart-viewer
:name :portal.viewer/histogram-chart})
Loading