   (did-mount [_]
              (let [{:keys [comm parsets-agg]} (om/get-state owner)
                    parsets-div (om/get-node owner "parsets-div")
-                   card-width (.-offsetWidth parsets-div)
-                   get-svg #(new-svg "#parsets" card-width height)
+                   card-width #(.-offsetWidth parsets-div)
+                   get-svg #(new-svg {:id "#parsets" :height height :width (card-width) :width-fn card-width})
                    chart #(make-parsets-chart
-                           {:height height :width card-width
+                           {:height height :width (card-width) :width-fn card-width
                             :get-parsets-agg (fn [_] (:parsets-agg (om/get-state owner)))})]
                (.attach js/Waves (om/get-node owner "parsets-div") "waves-yellow")
                (go (while (:continue? (om/get-state owner))

    (do-parsets-agg owner)
    ))

(defn- make-parsets-chart [{:keys [height width get-parsets-agg]}]
  (-> js/d3
      .parsets
      (.tension 0.8)
      (.width width)
      (.height height)
      (.dimensions (clj->js (map name (get-parsets-agg))))
      (.value (fn [d i] (. d -value)))))

(defcomponent parsets [cursor owner {:keys [agg value-path url height] :or {height 600 value-path ["doc_count"]} :as opts}]
  (init-state [_]
              (let [{:keys [es-settings ref-state]} (om/get-shared owner)]
                {:continue? true
                 :comm (chan)
                 :url url
                 :parsets-agg (map keyword (:terms agg))
                 :get-agg-terms #(:parsets-agg (om/get-state owner))
                 :make-agg-query #(->query {:agg-terms ((:get-agg-terms (om/get-state owner)))
                                            :sub-aggs (:sub agg)
                                            :filters (ref-state :filters)
                                            :default-filter (get-in (es-settings) [:default :filter])})
                 :value-path (apply vector (map keyword value-path))
                 :combined-query nil
                 :svg nil}))
  (will-mount [_]
              (do-parsets-agg owner))
  (render [_]
          (html
           [:.card {:ref "parsets-card"}
            [:.card-content
             [:div.right {:style {:max-width "50%"}}
              (let [get-agg-terms #(:parsets-agg (om/get-state owner))
                    label-string (->> (get-agg-terms)
                                      (map name)
                                      (clojure.string/join " "))]
                (om/build labels/labels cursor
                          {:opts {:labels (labels/labelize label-string)
                                  :on-label-updated! (partial update-agg-terms-with-label owner)}}))]
             [:span.card-title.black-text
              "# PARSETS [ "
              [:strong (->> (:parsets-agg (om/get-state owner))
                            (map name)
                            (clojure.string/join " > "))]
              " ]"]
             [:div#parsets {:ref "parsets-div"}]]]))
  (did-update [_ _ _]
              (if-let [filters ((:ref-state (om/get-shared owner)) :filters)]
                (let [local-combined-query (-> (om/get-state owner) :combined-query)

  [terms sub-aggs]
  (reduce (fn [agg-query a-term]
            {:aggs (merge
                    {(keyword a-term)
                     (merge {:terms
                             (merge {:field a-term}
                                    (sort-sub sub-aggs))}
                            agg-query)}
                    sub-aggs)})
          (build-sub-aggs sub-aggs) terms))

(defn- build-terms-agg [prefix terms sub-aggs]
  (let [terms-aggs (for [term terms]
                     {(str prefix (name term)) (merge {:terms (merge {:field (name term)}
                                                                     (sort-sub sub-aggs))}
                                                      (build-sub-aggs sub-aggs))})
        terms-aggs (reduce merge terms-aggs)]
    terms-aggs))

(defn- walk-buckets [de-buck bucket prefix steps value-path]
  (map #(de-buck % de-buck prefix (first steps) (rest steps) value-path)
