Google Chart CLJS Clojure

RRR*_*RRR 8 clojure clojurescript reagent re-frame

我尝试在Google Chart中调整此示例.到re-frame框架,reagent.我想基于订阅创建一个实时图表.我用一个简单的计数器= + - 1测试.

我收到了错误: Assert failed: Render must be a function, not nil (ifn? render-fun).

(defn draw-demo-chart 
   [d]
   (let [[columns vectors options chart] (r/children d)
         data (new js/google.visualization.DataTable)]
       (doall ;gotta keep the doall on maps. lazy sequence...
      (map (fn [[type name]]
            (.addColumn data type name)) columns))
      (.addRows data vectors)
      (.draw chart data options)
      (.load js/google "visualization" "1" (clj->js {:packages ["corechart" "orgchart" "calendar" "map" "geochart"]}))     
      (.setOnLoadCallback js/google draw-demo-chart)
      ))


(defn draw-demo-chart-container
    []
    (let [count    (re-frame/subscribe [:count])
          columns  (reaction [["date" "X"] ["number" "Y"]])
          vectors  (reaction (clj->js [[(new js/Date "07/11/14") 145] [(new js/Date "07/12/14") 15]
                                      [(new js/Date "07/13/14") 23] [(new js/Date "07/14/14") 234]]))
          options  (reaction (clj->js {:title (str @count)}))
          chart    (reaction (new js/google.visualization.LineChart (.getElementById js/document "linechart"))) ]
     (fn []
        [draw-demo-graph @columns @vectors @options @chart])))

(def draw-demo-graph 
       (r/create-class {:reagent-render  draw-demo-chart
                        :component-did-mount draw-demo-chart
                        :component-did-update draw-demo-chart}))
Run Code Online (Sandbox Code Playgroud)

Tim*_*ley 5

使用Google Charts API存在一些挑战:

  1. 它以异步方式加载,只能在准备就绪时使用.

我建议使用一个标志来记录API是否准备就绪,即使在组件安装完成后加载API,也会允许它进行渲染.

(defonce ready?
  (reagent/atom false))

(defonce initialize
  (do
    (js/google.charts.load (clj->js {:packages ["corechart"]}))
    (js/google.charts.setOnLoadCallback
      (fn google-visualization-loaded []
        (reset! ready? true)))))
Run Code Online (Sandbox Code Playgroud)
  1. 您需要调用drawHTML元素:

HTML元素仅在组件已安装时才存在.您可以使用a ref来方便地获取HTML元素(否则您需要在on上保存对in的引用,或者搜索它).

(defn draw-chart [chart-type data options]
  [:div
   (if @ready?
     [:div
      {:ref
       (fn [this]
         (when this
           (.draw (new (aget js/google.visualization chart-type) this)
                  (data-table data)
                  (clj->js options))))}]
     [:div "Loading..."])])
Run Code Online (Sandbox Code Playgroud)

每当任何输入发生变化时,您都会想要重绘(上面的ref例子就是这样).

  1. 设置数据源

我建议一种获取数据源的便捷方法:

(defn data-table [data]
  (cond
    (map? data) (js/google.visualization.DataTable. (clj->js data))
    (string? data) (js/google.visualization.Query. data)
    (seqable? data) (js/google.visualization.arrayToDataTable (clj->js data))))
Run Code Online (Sandbox Code Playgroud)
  1. 用它

现在,您可以使用具有无功值的图表!

[draw-chart
    "LineChart"
    @some-data
    {:title (str "Clicks as of day " @day)}]
Run Code Online (Sandbox Code Playgroud)

完整代码列表位于 https://github.com/timothypratley/google-chart-example