001  (ns org.clojars.punit-naik.chart-actions)
002  
003  (defn add-tooltip
004    [vl-spec
005     {:keys [x-fld-name x-fld-type x-fld-title]
006      :or {x-fld-name "x"
007           x-fld-type "temporal"
008           x-fld-title "Time"}}
009     {:keys [y-fld-name y-fld-type y-fld-title]
010      :or {y-fld-name "y"
011           y-fld-type "quantitative"
012           y-fld-title "Count"}}
013     {:keys [stack-fld-name stack-fld-type stack-fld-title]
014      :or {stack-fld-name "label"
015           stack-fld-type "nominal"
016           stack-fld-title "Labels"}
017      :as stack-info}]
018    (update
019     vl-spec
020     :encoding
021     assoc
022     :tooltip
023     (cond-> [{:field x-fld-name :title x-fld-title :type x-fld-type}
024              {:field y-fld-name :title y-fld-title :type y-fld-type}]
025       (seq stack-info)
026       (conj {:field stack-fld-name :title stack-fld-title :type stack-fld-type}))))
027  
028  (defn increase-hover-area
029    "Adds a selection in the `spec` to increase the hover area of a point on the chart
030     So that tooltips show even if the cursor is not exactly on the point"
031    [spec]
032    (update spec :selection assoc :hover {:nearest true :type "single" :empty "none" :on "mouseover" :clear "mouseout"}))
033  
034  (defn select-on-click
035    "Selects data only belonging to a particular label which was clicked
036     and fades out others from the legend bar"
037    [spec {:keys [select-fld select-type select-bind select-name]
038           :or {select-fld "label"
039                select-type "multi"
040                select-bind "legend"
041                select-name :A}}]
042    (-> spec
043        ;; Selection
044        (update :selection assoc select-name {:type select-type :fields [select-fld] :bind select-bind})
045        ;; Selection Action
046        (update :encoding assoc :opacity {:condition {:selection (name select-name) :value 1} :value 0.05})))
047  
048  (defn zoom-on-scroll
049    "Zooms in/out the graph"
050    [spec]
051    (update spec :selection assoc :grid {:type "interval" :bind "scales"}))