-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathGenDocInlit.hs
More file actions
151 lines (118 loc) · 3.76 KB
/
GenDocInlit.hs
File metadata and controls
151 lines (118 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
{-# LANGUAGE OverloadedStrings,TemplateHaskell, ExtendedDefaultRules #-}
{-# OPTIONS_GHC -F -pgmF inlitpp #-}
# Plotly.js Haskell bindings examples
```haskell hide top
import Inliterate.Import
import Graphics.Plotly hiding (text)
import Graphics.Plotly.Lucid
import Data.Text (Text)
import Lens.Micro
import Numeric.Datasets.Iris
import Data.Aeson
```
```html_header
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
```
This web pages shows plots generated with the plotlyhs packages, along with the
Haskell code that generated the plots. To use the plots on the generate page,
the Plotly.js source code should first be including by adding this tag to your HTML
header:
```html
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
```
Alternatively, this tag can be included in an HTML page using the `plotlyCDN`
function in `Graphics.Plotly.Lucid` (when using Lucid) or
`Graphics.Plotly.Blaze`(when using blaze-html).
### A complete & minimal example
```haskell
{-# LANGUAGE OverloadedStrings #-}
import Lucid
import Lucid.Html5
import Graphics.Plotly
import Graphics.Plotly.Lucid
import Lens.Micro
import qualified Data.Text.Lazy as T
import qualified Data.Text.Lazy.IO as T
main =
T.writeFile "test.html" $ renderText $ doctypehtml_ $ do
head_ $ do meta_ [charset_ "utf-8"]
plotlyCDN
body_ $ toHtml $ plotly "myDiv" [myTrace]
pointsData :: [(Double, Double)]
pointsData = zip [1,2,3,4] [500,3000,700,200]
myTrace
= line (aes & x .~ fst
& y .~ snd) pointsData
```
```haskell eval
plotly "p0" [line (aes & x .~ fst & y .~ snd) pointsData]
```
In the examples below, we omit all of the imports,
main function, html header and focus only
on the `Plotly` value (the argument to `toHtml`). The
`Plotly` value can be constructed with the function `plotly`
which takes two arguments: the element id of the `<div>`
for the plot (this element will be created if you call toHtml on the `Plotly`
value) and a list of traces.
### A simple plot
We use these this basic dataset:
```haskell top
pointsData :: [(Double, Double)]
pointsData = zip [1,2,3,4] [500,3000,700,200]
```
Code to generate build the `Plotly` value
(which has a `toHtml` instance), result on the right
```haskell eval twocol
plotly "div1" [line (aes & x .~ fst
& y .~ snd)
pointsData]
```
The above is quite unbearably sized & padded for this tutorial, so let's fix the
margins and the plot height
```haskell eval twocol
plotly "div2" [line (aes & x .~ fst
& y .~ snd)
pointsData]
& layout . margin ?~ thinMargins
& layout . height ?~ 300
```
### Lines and Markers
```haskell eval twocol
plotly "div3" [points (aes & x .~ fst
& y .~ snd)
pointsData]
& layout . margin ?~ thinMargins
& layout . height ?~ 300
```
```haskell eval twocol
plotly "div4" [points (aes & x .~ fst
& y .~ snd)
pointsData
& mode ?~ [Lines, Markers]]
& layout . margin ?~ thinMargins
& layout . height ?~ 300
```
### Iris plots
This plot uses the `iris` value from the datasets package
```haskell eval twocol
plotly "div6"
[points (aes & x .~ sepalLength
& y .~ sepalWidth
& color ?~ (fromEnum . irisClass)) iris]
& layout . margin ?~ thinMargins
& layout . height ?~ 300
```
### Horizontal bar plots
Using this simple dataset:
```haskell top
hbarData :: [(Text, Double)]
hbarData = [("Simon", 14.5), ("Joe", 18.9), ("Dorothy", 16.2)]
```
```haskell eval twocol
plotly "div7"
[hbars (aes & y .~ fst
& x .~ snd)
hbarData]
& layout . margin ?~ thinMargins
& layout . height ?~ 300
```