我是 ClojureScript 的新手,我正在尝试使用新数据更新图表。我正在使用 Chartist 和 Reagent。第一次渲染图表可以正常工作,但是如果我更改了 chartist 函数的数据,图表就不会再次渲染。我已经用 atom 尝试过不同的东西,并尝试使用 chartist 的更新功能。这是chartist更新api的链接
到目前为止,这是我的代码:
(ns example.core
(:require [clojure.browser.repl :as repl]
[reagent.core :as reagent]
[cljsjs.chartist]
))
(defonce conn
(repl/connect "http://localhost:9000/repl"))
(defonce results(reagent/atom [1 1 6 15 25] ))
(defonce timespan (reagent/atom ["Mar-2012" "Jun-2012" "Nov-2012" "Oct-2013" "Nov-2014"]))
(def global-state (reagent/atom 0))
(defn show-chart
[resultsInChart timespaneInChart]
(let [chart-data {:labels timespaneInChart
:series [resultsInChart]}
options {:width "700px"
:height "380px"}]
(js/Chartist.Line. ".ct-chart" (clj->js chart-data) (clj->js options))))
(defn chart-component
[]
(let [some global-state]
(reagent/create-class
{:component-did-mount #(show-chart @results @timespan)
:component-did-update #(show-chart @results @timespan)
:display-name "chart-component"
:reagent-render (fn []
[:div {:class "ct-chart ct-perfect-fourth"}])})))
(defn home-page []
[:div
[:div
[:p [:button {:on-click #(reset! results [1 1 6 15 5]
)} "Change last Value to 5"]
]
]
]
)
(defn run []
(reagent/render [chart-component](.getElementById js/document "myChart"))
(reagent/render [home-page](.getElementById js/document "app"))
)
如果有人可以提供帮助,那就太好了。
更新:我通过在我的项目中添加无花果来修复它,不知何故这解决了问题并且图表得到更新。最后我还使用了component-did-update下Chartist的更新功能。这是无花果的链接https://github.com/bhauman/lein-figwheel