                 (.data data)
                 (.attr "transform" #(str "translate(" (x-scale (.-x %)) "," (y-scale (.-y %)) ")")))]
 
+    ;; new bars
     (doto (-> bar
               (.enter)
               (.append "g")

(ns load-test-om.histogram
  (:require [om.core :as om :include-macros true]
            [om.dom :as dom :include-macros true]))

(defn x-axis [x-scale]
  (-> (.-svg js/d3)

        (.selectAll ".bar text")
        (.data data)
        (.attr "x" (/ (x-scale (.-dx (aget data 0))) 2))
        (.text format-label)
        (.exit)
        (.remove))

    ;; existing rects
    (-> svg
        (.selectAll "rect")
        (.data data)
        (.attr "width" (dec (x-scale (.-dx (aget data 0)))))
        (.attr "height" #(- chart-height (y-scale (.-y %))))
        (.exit)
        (.remove))

    ;; cleanup old bars
    (-> bar .exit .remove)

    (-> svg
        (.select ".x.axis")
        (.call (x-axis x-scale)))))

(defn create-x-axis [el]
  (-> (.select js/d3 el)
      (.select "svg g")
      (.append "g")
      (.attr "class" "x axis")
      (.attr "transform" (str "translate(0," chart-height ")"))))

(defn create-chart [el data-points]
  (create-x-axis el)
  (update-chart el data-points))

(defn component [load-test owner]
  (reify
    om/IDidMount
    (did-mount [_]
      (create-chart (om/get-node owner) (:data-points load-test)))

    om/IDidUpdate
    (did-update [_ _ _]
      (update-chart (om/get-node owner) (:data-points load-test)))

    om/IRender
    (render [_]
      (let [top 20
            right 10
            bottom 30
            left 30]
        (dom/div #js {:className "chart response-time-histogram"}
                 (dom/svg #js {:width (+ chart-width left right)
                               :height (+ chart-height top bottom)}
                          (dom/g #js {:transform (str "translate(" left "," top ")")})))))))
