             [cljs-time.format :as time-format]
             [cljs-time.coerce :as time-coerce]))
 
+
+(defn floor [ x ]
+  (.floor js/Math x))
+
+(defn pixel-snap [ t ]
+  (+ 0.5 (floor t)))
+
+
 (def dtf-axis-label (time-format/formatter "MM-dd HH:mm"))
 (def dtf-header (time-format/formatter "yyyy-MM-dd HH:mm"))
 (def x-axis-space 20)


(def pixels-per-y-label 20)

(def y-line-intervals [0.1 0.2 0.5
                       1 2 5
                       10 20 50
                       100 200 500])

(def stroke-styles
  {:grid
   {:line-width 0
    :stroke-style "#707070"
    :line-dash #js [ 2 2]}

   :grid-emphasis
   {:line-width 0

  (with-preserved-ctx ctx
    (let [ty (translate-fn-invert y-range h)
          y-interval (find-y-grid-interval h y-range)]
      (doseq [ y (if (range-contains-zero? y-range)
                   (concat (map #(* y-interval %)
                                (range (/ (:max y-range) y-interval)))
                           (map #(* (- y-interval) (+ % 1))
                                (range (- (/ (- (:min y-range)) y-interval) 1))))
                   (map #(+ (:min y-range) (* y-interval (+ % 1)))
                        (range (/ (range-magnitude y-range) y-interval))))]
        (draw-y-grid-line ctx w (pixel-snap (ty y)) y (= y 0))))))

(defn draw-series [ ctx w h data x-range ]
  (with-preserved-ctx ctx
    (set-stroke-style ctx :series-line)

    (aset ctx "font" "12px Arial")
    (let [y-range (rescale-range (s-yrange data) 1.1)]
      (unless (empty? data)
              (draw-y-grid ctx w h y-range)
              (with-preserved-ctx ctx
