gadfly361 / rid3

Reagent Interface to D3
MIT License
142 stars 6 forks source link

Adding events to pieces #12

Closed Conaws closed 4 years ago

Conaws commented 4 years ago

Working off of your scatterplot example

I want to be able to add click and hover events to the individual nodes

additionally, I'd like to be able to change the color of the node based on it's cy position

can you point me to resources on this


{:kind            :elem-with-data
           :class           "dots"
           :tag             "circle"
           :prepare-dataset prepare-dataset
           :did-mount
           (fn [node ratom]
             (let [y-scale (->y-scale ratom)
                   x-scale (->x-scale ratom)]
               (rid3-> node
                       {:cx   (fn [d]
                                (let [label (gobj/get d "label")]
                                  (x-scale label)))
                        :cy   (fn [d]
                                (let [value (gobj/get d "value")]
                                  (y-scale value)))
                        :r    4
;;; on-mouse-enter: etc

                        :fill  
;;; (if (= (gobj/get ??? "value) 0)) "nocolor" 
"#3366CC"})))}

``
Conaws commented 4 years ago

Answered first part of the question


           :class           "dots"
           :tag             "circle"
           :prepare-dataset prepare-dataset
           :did-mount
           (fn [node ratom]
             (let [y-scale (->y-scale ratom)
                   x-scale (->x-scale ratom)]
               (rid3-> node
                       (.on "mouseover" #(js/alert (gobj/get % "label")))
                       {:cx   (fn [d]
                                (let [label (gobj/get d "label")]
                                  (x-scale label)))
                        :cy   (fn [d]
                                (let [value (gobj/get d "value")]
                                  (y-scale value)))
                        :r    4
                        :fill "#3366CC"})))}```
gadfly361 commented 4 years ago

@Conaws You are correct that you should use (.on "mouseover" ...) for hover events.

For color of the circles in the scatterplot, here is a very basic example to get you going:

                        :fill (fn [d]
                                (let [value (gobj/get d "value")]
                                  (if (> value 15) "blue" "red")))

Depending on how you want to color them, you probably want to reach for d3's built in scales, which can handle colors.


color-scatter