• Stars
    star
    142
  • Rank 258,495 (Top 6 %)
  • Language
    Clojure
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Reagent Interface to D3

rid3

Rid3: Reagent interface to d3. Pronounced like the word 'ride'.

basics

examples

clojure meetup slides based on v0.1.0-alpha-3.

To use rid3, add the following to the :dependencies vector in your project.clj file:

[rid3 "0.2.2"]

Using an older version?

The Problem

In my experience, there is a lot of boilerplate involved when trying to get reagent (i.e., react) and d3 to play nicely together. The crux of the problem is you only want to append containing g tags (or static elements) to the DOM during reagent's component-did-mount lifecycle method and not during the component-did-update lifecycle method. However, more often than not, you want d3 to modify the stuff contained in the g tag in the same manner whether or not the component just mounted or just updated.

Rid3's Solution

Rid3 exposes a single reagent component, viz, with the aim to make 80% of what you'll likely want to make with d3 easier (bar charts, pie charts, scatter plots, etc.). Rid3 attempts to achieve this by making containing g tags for you. The benefit of doing this is that you can now use the same function in the did-mount and did-update lifecycle methods. However, rid3 goes a step further and will default the did-update lifecycle method to whatever you supply as the did-mount lifecycle method.

Viz Component

The viz component takes a hash-map of the following:

key type default required?
:id string yes
:ratom reagent.core/atom yes
:svg svg yes
:main-container main-container no
:pieces [ piece ] no
:class string no
  • an :id is required to differentiate between different rid3's
  • a :ratom can be a reagent atom, reagent cursor, or a re-frame subscription. This should be used to store all of the data relevant to your rid3.

:svg

Where an svg hash-map looks like:

key type default required?
:did-mount (fn [node ratom] ...) yes
:did-update (fn [node ratom] ...) did-mount no

:main-container

Where a main-container hash-map looks like:

key type default required?
:did-mount (fn [node ratom] ...) no
:did-update (fn [node ratom] ...) did-mount no

:pieces

And where :pieces is a vector of piece hash-maps. There are four kinds of piece hash-maps:

:container for when you want to group elem or elem-with-data pieces under the same g tag.

key type default required?
:class string yes
:did-mount (fn [node ratom] ...) yes
:did-update (fn [node ratom] ...) did-mount no
:children [ piece ] no

:elem for when you want to add an element like a text or a circle.

key type default required?
:class string yes
:tag string yes
:did-mount (fn [node ratom] ...) yes
:did-update (fn [node ratom] ...) did-mount no

:elem-with-data for when you want to add a series of elements that are joined to a dataset.

key type default required?
:class string yes
:tag string yes
:did-mount (fn [node ratom] ...) yes (1)
:did-update (fn [node ratom] ...) did-mount no
:gup gup-hash-map yes (1)
:prepare-dataset (fn [ratom] ...) (fn [ratom] (-> @ratom (get :dataset) clj->js)) no
:key-fn (fn [d i] ...) no
  • :elem-with-data pieces expect what is returned by the :prepare-dataset function to be a JavaScript array. E.g. [1, 2, 3] or [{"color": "blue"}, {"color": "green"} ... ]

  • If :prepare-dataset is not provided, then (as a default):elem-with-data will convert whatever is stored in the :dataset key of your ratom to a JavaScript array.

  • Individual datums (often referred to as "d") of the JavaScript array are passed to the anonymous functions that can be used to set properties of each :elem-with-data element. E.g. (.attr node "color" (fn [d] (goog.object/get d "color")))

  • (1) You can use either :did-mount and :did-update or :gup. You cannot mix.

  • If you choose to use :gup, then you can explicitly set attributes and add transitions in the :enter-init, :enter, :update, and :exit parts of the general update pattern.

  • A gup-hash-map looks like:

{:enter-init  (fn [node ratom] ...)
 :enter       (fn [node ratom] ...)
 :update      (fn [node ratom] ...)
 :exit        (fn [node ratom] ...)}
  • Note: only use :enter-init if you want a special enter transition when the vizusualiztion first mounts, otherwise, you can ignore it, and just use :enter, :update and :exit.

:raw for when you want to either trigger some side-effect or have an escape hatch from the rid3

key type default required?
:did-mount (fn [ratom] ...) yes
:did-update (fn [ratom] ...) yes

Joy Rid3 (Minimal Example)

Just so you can get a sense of a vizcomponent, let's create a minimal example ... a circle with text on top of it.

Add rid3 to your project

Add the following to the :dependencies vector of your project.clj file.

[rid3 "0.2.2"]

Require rid3 in your namespace

(ns foo.core
  (:require
   [rid3.core :as rid3 :refer [rid3->]]))

Create an svg

(defn viz [ratom]
  [rid3/viz
   {:id    "some-id"
    :ratom ratom
    :svg   {:did-mount (fn [node ratom]
                         (rid3-> node
                                 {:width  200
                                  :height 200
                                  :style  {:background-color "grey"}}))}
    }])

Which will result in the following:

<div id="some-id">
  <svg width="200" height="200" style="background-color: grey;">
    <g class="rid3-main-container">
    </g>
  </svg>
</div>

Note: All viz components need to provide a ratom. All relevant data for the component should be stored here. If anything changes in this ratom, then rid3 will trigger a re-render of the viz component for you.

You probably noticed that rid3 added a g tag with the class .rid3-main-container inside of your svg. This is where rid3 will place all of your pieces.

You probably also noticed the use of rid3->. This is just syntactic sugar to set attributes with familiar hiccup-like attribute maps. The above is the same as doing the following:

(defn viz [ratom]
  [rid3/viz
   {:id    "some-id"
    :ratom ratom
    :svg   {:did-mount (fn [node ratom]
                         (-> node
                             (.attr "width" 200)
                             (.attr "height" 200)
                             (.style "background-color" "grey")))}
    }])

Add a circle

(defn viz [ratom]
  [rid3/viz
   {:id    "some-id"
    :ratom ratom
    :svg   {:did-mount (fn [node ratom]
                         (rid3-> node
                                 {:width  200
                                  :height 200
                                  :style  {:background-color "grey"}}))}
    ;; ATTENTION \/
    :pieces
    [{:kind      :elem
      :class     "backround"
      :tag       "circle"
      :did-mount (fn [node ratom]
                   (rid3-> node
                           {:cx 100
                            :cy 100
                            :r  50}))}]
    }])

Which will result in the following:

<div id="some-id">
  <svg width="200" height="200" style="background-color: grey;">
    <g class="rid3-main-container">
      <g class="backround">
	<circle cx="100" cy="100" r="50">
	</circle>
      </g>
    </g>
  </svg>
</div>

Please note, that there is no .append method in our did-mount function! Rid3 created a containing g tag and placed the circle inside of it for us. The node that is passed to the did-mount function is the circle itself. You can confirm this by looking at where the attributes from our did-mount function are added.

Add text

(defn viz [ratom]
  [rid3/viz
   {:id    "some-id"
    :ratom ratom
    :svg   {:did-mount (fn [node ratom]
                         (rid3-> node
                                 {:width  200
                                  :height 200
                                  :style  {:background-color "grey"}}))}
    :pieces
    [{:kind      :elem
      :class     "backround"
      :tag       "circle"
      :did-mount (fn [node ratom]
                   (rid3-> node
                           {:cx 100
                            :cy 100
                            :r  50}))}
     ;; ATTENTION \/
     {:kind      :elem
      :class     "foreground"
      :tag       "text"
      :did-mount (fn [node ratom]
                   (rid3-> node
                           {:x                  100
                            :y                  100
                            :text-anchor        "middle"
                            :alignment-baseline "middle"
                            :fill               "green"
                            :font-size          "24px"
                            :font-family        "sans-serif"}
                           (.text "RID3")))}]
    }])

Which will result in the following:

<div id="some-id">
  <svg width="200" height="200" style="background-color: grey;">
    <g class="rid3-main-container">
      <g class="backround">
	<circle cx="100" cy="100" r="50">
	</circle>
      </g>
      <g class="foreground">
	<text x="100" y="100" text-anchor="middle" alignment-baseline="middle" fill="green" font-size="24px" font-family="sans-serif">RID3
	</text>
      </g>
    </g>
  </svg>
</div>

Note: the order of pieces matters. If we reversed the order of our text and circle, we wouldn't be able to see the text (because it would be behind the circle).


Don't forget to check out the following:

basics

examples

License

The MIT License (MIT)

Copyright Β© 2017 Matthew Jaoudi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

More Repositories

1

re-pressed

re-pressed is a clojurescript library that handles keyboard events for re-frame applications.
Clojure
163
star
2

reagent-figwheel

Leiningen template for Reagent applications in Clojurescript with Figwheel. Optionally includes secretary, cljs.test, garden, less, cider, devcards, cljs-devtools, re-risk
Clojure
156
star
3

soda-ash

Soda-ash is an interface between clojurescript's Reagent and Semantic UI React
Clojure
152
star
4

flutter_test_cookbook

A community driven cookbook with recipes (i.e., examples) on how to test your flutter application.
Dart
126
star
5

baking-soda

baking-soda is an interface between clojurescript's reagent and reactstrap (i.e., bootstrap 4 react components) and react-bootstrap (i.e., bootstrap 3 react components)
Clojure
74
star
6

cljs-todomvc

List of TodoMVC examples that use Clojurescript (om, om next, reagent, re-frame, rum, quiescent, etc.)
Clojure
72
star
7

reagent-seed

Clojurescript template for making Reagent applications with Org-mode (includes figwheel, cider, devcards, doo garden, secretary)
Clojure
63
star
8

breaking-point

BREAKING-POINT lets you quickly define and subscribe to screen (i.e. window) breakpoints in your re-frame application
Clojure
39
star
9

re-surface

Add structure to a reagent or re-frame application at the page level (sticky header, sidebar, modal, etc.)
Clojure
34
star
10

re-pollsive

Re-pollsive is a clojurescript library that handles polling events for re-frame applications
Clojure
34
star
11

gadfly-blog

https://gadfly361.github.io/gadfly-blog/
HTML
4
star
12

reagent-tic-tac-toe

Tic Tac Toe built with Reagent
CSS
2
star
13

reagent-cookbook-template

Template for Reagent Cookbook recipes
Clojure
2
star
14

flutter_services_example

An example Flutter app using "Services" state-management
Dart
1
star