Using plotly with Clojure Remix1

Translated from: https://plot.ly/javascript/

Select the plotly viewer via the :nextjournal/viewer metadata attribute, and provide the :data and :layout information.

Scatter charts

Basic line chart

^{:nextjournal/viewer :plotly}
{:data [{:x [1 2 3 4 5] :y [1 2 4 8 16]}]
 :layout {:title "My Awesome Line Chart"
          :xaxis1 {:title "year"} :yaxis1 {:title "revenue"}
          :margin {:t 50 :l 50}}}
0.1s
Clojure

Line dash styles

(def trace1
  {:x [1 2 3 4 5] :y [6 8 7 8 6]
   :mode "lines" :name "dashdot"
   :line {:dash "dashdot" :width 4}})
(def trace2
  {:x [1 2 3 4 5] :y [11 13 12 13 11]
   :mode "lines" :name "Solid"
   :line {:dash "solid" :width 4}})
(def trace3
  {:x [1 2 3 4 5] :y [16 18 17 18 16]
   :mode "lines" :name "dot"
   :line {:dash "dot" :width 4}})
(def layout
  {:title "Line Dash Styles"
   :xaxis {:range [0.75 5.25] :autorange false}
   :yaxis {:range [0 18.5] :autorange false}
   :legend {:y 0.5 :traceorder "reversed" :font {:size 16}}
   :margin {:t 50}})
^{:nextjournal/viewer :plotly}
{:data [trace1 trace2 trace3] :layout layout}
0.2s
Clojure

Fancy annotated line chart

(def xData 
  [[2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2013]
  [2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2013]
  [2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2013]
  [2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2013]])
(def yData
  [[74 82 80 74 73 72 74 70 70 66 66 69]
  [45 42 50 46 36 36 34 35 32 31 31 28]
  [13 14 20 24 20 24 24 40 35 41 43 50]
  [18 21 18 21 16 14 13 18 17 16 19 23]])
(def colors 
  ["rgba(67,67,67,1)" "rgba(115,115,115,1)"
   "rgba(49,130,189, 1)" "rgba(189,189,189,1)"])
(def lineSize 
  [2 2 4 2])
(def labels 
  ["Television" "Newspaper" "Internet" "Radio"])
(def data
  (loop [i 0 data []]
    (if (< i (count xData))
      (recur (inc i)
             (conj data
                   {:x (nth xData i) :y (nth yData i)
                    :type "scatter" :mode "lines"
                    :line {:color (nth colors i) :width (nth lineSize i)}}
                   {:x [(first (nth xData i)) (nth (nth xData i) 11)]
                    :y [(first (nth yData i)) (nth (nth yData i) 11)]
                    :type "scatter" :mode "markers"
                    :marker {:color (nth colors i) :size 12}}))
      data)))
(def annotations
  (loop [i 0 annotations
         [{:xref "paper" :yref "paper"
           :x 0.0 :y 1.05
           :xanchor "left" :yanchor "bottom"
           :text "Main Source for News"
           :font {:family "Arial" :size 30 :color "rgb(37,37,37)"}
           :showarrow false}
          {:xref "paper" :yref "paper"
           :x 0.5 :y -0.1 :xanchor "center" :yanchor "top"
           :text "Source: Pew Research Center & Storytelling with data"
           :showarrow false
           :font {:family "Arial" :size 12 :color "rgb(150,150,150)"}}]]
    (if (< i (count xData))
      (recur (inc i)
             (conj annotations
                   {:xref "paper"
                    :x 0.05 :y (first (nth yData i))
                    :xanchor "right" :yanchor "middle"
                    :text (str (nth labels i) " " (first (nth yData i)) "%")
                    :showarrow false
                    :font {:family "Arial" :size 16 :color "black"}}
                   {:xref "paper"
                    :x 0.95 :y (nth (nth yData i) 11)
                    :xanchor "left" :yanchor "middle"
                    :text (str (nth (nth yData i) 11) "%")
                    :font {:family "Arial" :size 16 :color "black"}
                    :showarrow false}))
      annotations)))
(def layout
  {:showlegend false
   :height 600 :width 600
   :xaxis {:showline true :showgrid false :showticklabels true
           :linecolor "rgb(204,204,204)" :linewidth 2
           :autotick false :ticks "outside"
           :tickcolor "rgb(204,204,204)" :tickwidth 2 :ticklen 5
           :tickfont {:family "Arial" :size 12 :color "rgb(82, 82, 82)"}}
   :yaxis {:showgrid false :zeroline false
           :showline false :showticklabels false}
   :autosize false
   :margin {:autoexpand false :l 100 :r 20 :t 100}
   :annotations annotations})
^{:nextjournal/viewer :plotly}
{:data data :layout layout}
0.8s
Clojure

Area chart

(def trace1
  {:x [1 2 3 4] :y [0 2 3 5]
   :fill "tozeroy" :type "scatter"})
(def trace2
  {:x [1 2 3 4] :y [3 5 1 7]
   :fill "tonexty" :type "scatter"})
^{:nextjournal/viewer :plotly}
{:data [trace1 trace2]
 :layout {:title "My Awesome Area Chart"
          :margin {:t 50 :l 50}}}
0.1s
Clojure
(def traces
  [{:x [1 2 3] :y [2 1 4] :stackgroup "one"}
   {:x [1 2 3] :y [1 1 2] :stackgroup "one"}
   {:x [1 2 3] :y [3 0 2] :stackgroup "one"}])
^{:nextjournal/viewer :plotly}
{:data traces
 :layout {:title "My Awesome Stacked Chart"
          :margin {:t 50 :l 50}}}
0.1s
Clojure

Tables

(def header
  [["<b>EXPENSES</b>"] ["<b>Q1</b>"] ["<b>Q2</b>"] ["<b>Q3</b>"] ["<b>Q4</b>"]])
(def values
  [["Salaries" "Office" "Merchandise" "Legal" "<b>TOTAL</b>"]
   [1200000 20000 80000 2000 12120000]
   [1300000 20000 70000 2000 130902000]
   [1300000 20000 120000 2000 131222000]
   [1400000 20000 90000 2000 14102000]])
^{:nextjournal/viewer :plotly}
{:data [{:header {:values header
                  :fill {:color "blue"}
                  :font {:family "Arial" :size 14 :color "white"}
                  :line {:color "black" :width 1}}
         :cells {:values values
                 :align "center"
                 :fill {:color [["yellow" "turquoise" "yellow"
                                 "turquoise" "magenta"]]}
                 :font {:family "Arial" :size 12 :color ["black"]}}
         :type "table"}]
 :layout {:title "My Awesome Table" :margin {:t 50}}}
0.1s
Clojure

Pie chart

^{:nextjournal/viewer :plotly}
{:data [{:values [19 26 55]
    :labels ["Residential" "Non-Residential" "Utility"]
    :type "pie"}]
 :layout {:height 400 :width 500}}
0.1s
Clojure

Bar chart

^{:nextjournal/viewer :plotly}
  {:data [{:x [1971 1969 1973 1972 1970] :y [4 3 3 3 3] :type "bar"}]
   :layout {:autosize false :width 600 :height 500
            :type "bar" :title "My Awesome Bar Chart"
            :xaxis1 {:title "Year"} :yaxis1 {:title "# of Albums"}
            :margin {:t 50 :l 50}}}
0.1s
Clojure

Gauge chart

(def level 120)
(def path
  (str "M -0.0 -0.025 L 0.0 0.025 L "
       (* 0.5 (Math/cos (/ (* (- 180 level) Math/PI) 180))) " "
       (* 0.5 (Math/sin (/ (* (- 180 level) Math/PI) 180))) " Z"))
(def data
  [{:type "scatter" :x [0] :y [0] :marker {:size 28 :color "850000"}
    :showlegend false :name "speed" :text level :hoverinfo "text+name"}
  {:values [8.34 8.34 8.34 8.34 8.34 8.34 50] :rotation 90
   :text ["TOO FAST!" "Pretty Fast" "Fast" "Average" "Slow" "Super Slow" ""]
   :textinfo "text" :textposition "inside"
   :marker {:colors ["red" "orange" "yellow" "green" "blue" "violet" "white"]}
   :labels ["151-180" "121-150" "91-120" "61-90" "31-60" "0-30" ""]
   :hoverinfo "label" :hole 0.5 :type "pie" :showlegend false}])
(def layout
  {:shapes [{:type "path" :path path
             :fillcolor "850000" :line {:color "850000"}}]
   :title "<b>Gauge</b> <br> Speed 0-100"
   :height 1000 :width 1000
   :xaxis {:zeroline false :showticklabels false
           :showgrid false :range [-1 1]}
   :yaxis {:zeroline false :showticklabels false
           :showgrid false :range [-1 1]}
   :margin {:t 80}})
^{:nextjournal/viewer :plotly}
{:data data :layout layout}
0.1s
Clojure

Arbitrary SVG Paths

(def data
  [{:x [2 1 6 6]
    :y [0.5 9 1.5 5.5]
    :text ["filled triangle" "filled Polygon"
           "Quadratic Bezier Curves" "Cubic Bezier Curves"]
    :mode "text"}])
(def layout
  {:title "Basic Arbitrary SVG Paths"
   :xaxis {:range [0 9]
           :zeroline false}
   :yaxis {:range [0 11]
           :showgrid false}
   :width 500
   :height 500
   :margin {:t 80}
   :shapes [
    ;;Quadratic Bezier Curves
    {:type "path"
     :path "M 4,4 Q 6,0 8,4"
     :line {:color "rgb(93, 164, 214)"}}
    ;;Cubic Bezier Curves
    {:type "path"
     :path "M 1,4 C 2,8 6,4 8,8"
     :line {:color "rgb(207, 114, 255)"}}
    ;;Filled Triangle
    {:type "path"
     :path "M 1 1 L 1 3 L 4 1 Z"
     :fillcolor "rgba(44, 160, 101, 0.5)"
     :line {:color "rgb(44, 160, 101)"}}
    ;;Filled Polygon
    {:type "path"
     :path "M 3,7 L2,8 L2,9 L3,10, L4,10 L5,9 L5,8 L4,7 Z"
     :fillcolor "rgba(255, 140, 184, 0.5)"
     :line {:color "rgb(255, 140, 184)"}}]})
^{:nextjournal/viewer :plotly}
{:data data :layout layout}
0.5s
Clojure
Clojure
Runtimes (1)