Skip to content

Commit

Permalink
create a cljs lib for echarts
Browse files Browse the repository at this point in the history
  • Loading branch information
kimim committed Feb 24, 2021
0 parents commit 50f3673
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
5 changes: 5 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"devDependencies": {
"shadow-cljs": "^2.3.22"
}
}
3 changes: 3 additions & 0 deletions example/public/css/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
color: green;
}
12 changes: 12 additions & 0 deletions example/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html>
<head><title>Browser Starter</title></head>
<link rel="stylesheet" href="/css/main.css">
<body>
<h1>shadow-cljs - Browser</h1>
<div id="app"></div>

<script src="/js/main.js"></script>
<script>starter.browser.init();</script>
</body>
</html>
19 changes: 19 additions & 0 deletions example/shadow-cljs.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{:source-paths
["src"]

:dependencies
[[reagent "1.0.0"]
[re-echarts "5.0.2"]]

:builds
{:app {:target :browser
:output-dir "public/js"
:asset-path "/js"

:modules
{:main
{:entries [starter.browser]}}

:devtools
{:http-root "public"
:http-port 8020}}}}
35 changes: 35 additions & 0 deletions example/src/starter/browser.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(ns starter.browser
(:require
[reagent.dom :as rdom]
[re-echarts.core :refer [ECharts]]))

(defn myechart []
[:> ECharts
{:style {:width "800px" :height "600px"}
:theme "dark"
:option
{:title {:text "Echarts is here"}
:dataset {:dimention [:Week :Value]
:source [{:Week "Mon" :Value 820} {:Week "Tue" :Value 932} {:Week "Wed" :Value 901}
{:Week "Thu" :Value 934} {:Week "Fri" :Value 1220} {:Week "Sat" :Value 820}
{:Week "Sun" :Value 990}]}
:xAxis {:type "category"}
:yAxis {:type "value"}
:series [{:type "line"
:smooth true}]}}])

;; start is called by init and after code reloading finishes
(defn ^:dev/after-load start []
(js/console.log "start")
(rdom/render [#'myechart] (.getElementById js/document "app")))

(defn ^:export init []
;; init is called ONCE when the page loads
;; this is called in the index.html and must be exported
;; so it is available even in :advanced release builds
(js/console.log "init")
(start))

;; this is called before any code is reloaded
(defn ^:dev/before-load stop []
(js/console.log "stop"))
8 changes: 8 additions & 0 deletions localrepo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
# move jar to local repo for testing purpose
export version=5.0.2
lein jar
lein pom
lein localrepo install target/re-echarts-$version.jar re-echarts $version
mkdir -p ~/.m2/repository/re-echarts/re-echarts/$version/
mv pom.xml ~/.m2/repository/re-echarts/re-echarts/$version/re-echarts-$version.pom
14 changes: 14 additions & 0 deletions project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(defproject re-echarts/re-echarts "5.0.2"
:description "React ECharts wrapper in ClojureScript"
:url "https://github.com/kimim/re-echarts"

:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}

:dependencies
;; always use "provided" for Clojure(Script)
[[org.clojure/clojurescript "1.10.520" :scope "provided"]
[reagent "1.0.0"]]

:source-paths
["src"])
1 change: 1 addition & 0 deletions src/deps.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:npm-deps {"echarts" "5.0.2"}}
16 changes: 16 additions & 0 deletions src/re_echarts/core.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(ns re-echarts.core
(:require
[react :as react]
[reagent.core :as r]
[echarts :as echarts]))

(defn ECharts [options]
(r/as-element
(let [mychart (react/useRef nil)]
(react/useEffect (fn []
(set! (.-chart js/document)
(echarts/init (.-current mychart) (.-theme options)))
(.setOption (.-chart js/document) (.-option options)))
(clj->js [options js/ResizeObserver]))
[:div {:ref mychart
:style (.-style options)}])))

0 comments on commit 50f3673

Please sign in to comment.