clojurescript - 重新构造: nvd3 graph doesn't respond to when its component's subscriptions are updated

标签 clojurescript reagent

我正在使用 re-frame cljs 框架,该框架使用 reagent 作为其 View 库。我有一个 nvd3 图形组件,我希望在其订阅更新时更新该组件。

不幸的是,在首次调用 :component-did-mount 后,图表永远不会自行更新。 :component-will-update 在初始渲染后永远不会再次调用。

我希望图表能够在订阅通知其正在监​​听的数据发生更改的组件时自行更新。

这是图形容器组件:

(defn weight-graph-container
  []
  (let [weight  (subscribe [:weight-change])
        bodyfat (subscribe [:bodyfat-change])
        weight-amount (reaction (get @weight :amount))
        weight-unit   (reaction (get @weight :unit))
        bf-percentage (reaction (get @bodyfat :percentage))
        lbm           (reaction (lib/lbm @weight-amount @bf-percentage))
        fat-mass      (reaction (- @weight-amount @lbm))]
    (reagent/create-class {:reagent-render weight-graph
                           :component-did-mount (draw-weight-graph @lbm @fat-mass "lb")
                           :display-name "weight-graph"
                           :component-did-update (draw-weight-graph @lbm @fat-mass "lb")})))

这是图形组件:

(defn draw-weight-graph [lbm fat-mass unit]
  (.addGraph js/nv (fn []
                     (let [chart (.. js/nv -models pieChart
                                     (x #(.-label %))
                                     (y #(.-value %))
                                     (showLabels true))]
                       (let [weight-data [{:label "LBM" :value lbm} {:label "Fat Mass" :value fat-mass}]]
                         (.. js/d3 (select "#weight-graph svg")
                                   (datum (clj->js weight-data))
                                   (call chart)))))))

最后,这是图形渲染到的组件:

(defn weight-graph []
  [:section#weight-graph
   [:svg]])

我错过了什么?感谢您的帮助。

最佳答案

以下代码可以解决您的问题:

(defn draw-weight-graph
  [d]
  (let [[lbm fat-mass unit] (reagent/children d)]
    (.addGraph js/nv (fn []
                       (let [chart (.. js/nv -models pieChart
                                       (x #(.-label %))
                                       (y #(.-value %))
                                       (showLabels true))]
                         (let [weight-data [{:label "LBM" :value lbm} {:label "Fat Mass" :value fat-mass}]]
                           (.. js/d3 (select "#weight-graph svg")
                               (datum (clj->js weight-data))
                               (call chart))))))))

(def graph-component (reagent/create-class {:reagent-render weight-graph
                                            :component-did-mount draw-weight-graph
                                            :display-name "weight-graph"
                                            :component-did-update draw-weight-graph}))

(defn weight-graph-container
  []
  (let [weight  (subscribe [:weight-change])
        bodyfat (subscribe [:bodyfat-change])
        weight-amount (reaction (get @weight :amount))
        weight-unit   (reaction (get @weight :unit))
        bf-percentage (reaction (get @bodyfat :percentage))
        lbm           (reaction (lib/lbm @weight-amount @bf-percentage))
        fat-mass      (reaction (- @weight-amount @lbm))]
  (fn []
     [graph-component @lbm @fat-mass "lb"])))

关于clojurescript - 重新构造: nvd3 graph doesn't respond to when its component's subscriptions are updated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31520217/

相关文章:

clojurescript - 在 Clojurescript 中,如何获取 UUID 的字符串部分?

postgresql - 如何使用 Clojure 将 PostgreSQL 函数应用于查询?

clojure - 如何从ClojureScript中的<input type ='file'…/>获取文件

ClojureScript - 获取复选框元素值

json - 如何解码,然后格式化,标记的 LocalDateTime 值

node.js - 如何将 Reagent 添加到 Node JS API

clojurescript - 无法在 OM 中显示两个组件

clojure - 在 Reagent/Clojurescript 中保持客户端状态最新

javascript - 将秘书 URL 参数广播到试剂组件的正确方法是什么

clojurescript - 网页未更新