-
Notifications
You must be signed in to change notification settings - Fork 81
/
02_6_table.fsx
235 lines (196 loc) · 5.97 KB
/
02_6_table.fsx
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
(**
---
title: Tables
category: Simple Charts
categoryindex: 3
index: 7
---
*)
(*** hide ***)
(*** condition: prepare ***)
#r "nuget: Newtonsoft.JSON, 12.0.3"
#r "nuget: DynamicObj"
#r "../bin/Plotly.NET/netstandard2.0/Plotly.NET.dll"
(*** condition: ipynb ***)
#if IPYNB
#r "nuget: Plotly.NET, {{fsdocs-package-version}}"
#r "nuget: Plotly.NET.Interactive, {{fsdocs-package-version}}"
#endif // IPYNB
(**
# Tables
[![Binder]({{root}}img/badge-binder.svg)](https://mybinder.org/v2/gh/plotly/Plotly.NET/gh-pages?filepath={{fsdocs-source-basename}}.ipynb) 
[![Script]({{root}}img/badge-script.svg)]({{root}}{{fsdocs-source-basename}}.fsx) 
[![Notebook]({{root}}img/badge-notebook.svg)]({{root}}{{fsdocs-source-basename}}.ipynb)
This example shows how to create tables in F#.
let's first create some data for the purpose of creating example charts:
*)
open Plotly.NET
open Plotly.NET.StyleParam
let header = ["<b>RowIndex</b>";"A";"simple";"table"]
let rows =
[
["0";"I" ;"am" ;"a"]
["1";"little";"example";"!"]
]
let table1 = Chart.Table(header, rows)
(*** condition: ipynb ***)
#if IPYNB
table1
#endif // IPYNB
(***hide***)
table1 |> GenericChart.toChartHTML
(***include-it-raw***)
(**
A little bit of styling:
*)
let table2 =
Chart.Table(
header,
rows,
//sets global header alignment
AlignHeader = [HorizontalAlign.Center],
//sets alignment for each column separately
//(The last alignment is applied to all potential following columns)
AlignCells = [HorizontalAlign.Left;HorizontalAlign.Center;HorizontalAlign.Right],
//sets global header color
ColorHeader = Color.fromString "#45546a",
//sets specific color to each header column
//ColorHeader=["#45546a";"#deebf7";"#45546a";"#deebf7"],
//sets global cell color
//ColorRows = "#deebf7",
//sets cell column colors
ColorCells = Color.fromColors [
Color.fromString "#deebf7"
Color.fromString "lightgrey"
Color.fromString "#deebf7"
Color.fromString "lightgrey"
],
//sets cell row colors
//ColorCells=[["#deebf7";"lightgrey"]],
//sets font of header
FontHeader = Font.init(FontFamily.Courier_New, Size=12., Color=Color.fromString "white"),
//sets the height of the header
HeightHeader= 30.,
//sets lines of header
LineHeader = Line.init(2.,Color.fromString "black"),
ColumnWidth = [70;50;100;70],
//defines order of columns
ColumnOrder = [1;2;3;4]
)
(*** condition: ipynb ***)
#if IPYNB
table2
#endif // IPYNB
(***hide***)
table2 |> GenericChart.toChartHTML
(***include-it-raw***)
(**
Value dependent cell coloring:
*)
let header2 = ["Identifier";"T0";"T1";"T2";"T3"]
let rowvalues =
[
[10001.;0.2;2.0;4.0;5.0]
[10002.;2.1;2.0;1.8;2.1]
[10003.;4.5;3.0;2.0;2.5]
[10004.;0.0;0.1;0.3;0.2]
[10005.;1.0;1.6;1.8;2.2]
[10006.;1.0;0.8;1.5;0.7]
[10007.;2.0;2.0;2.1;1.9]
]
|> Seq.sortBy (fun x -> x.[1])
//map color from value to hex representation
let mapColor min max value =
let proportion =
(255. * (value - min) / (max - min))
|> int
Color.fromRGB 255 (255 - proportion) proportion
//Assign a color to every cell seperately. Matrix must be transposed for correct orientation.
let cellcolor =
rowvalues
|> Seq.map (fun row ->
row
|> Seq.mapi (fun index value ->
if index = 0 then Color.fromString "white"
else mapColor 0. 5. value
)
)
|> Seq.transpose
|> Seq.map Color.fromColors
|> Color.fromColors
let table3 = Chart.Table(header2,rowvalues,ColorCells=cellcolor)
(*** condition: ipynb ***)
#if IPYNB
table3
#endif // IPYNB
(***hide***)
table3 |> GenericChart.toChartHTML
(***include-it-raw***)
(**
Sequence representation:
*)
let sequence =
[
"ATGAGACGTCGAGACTGATAGACGTCGATAGACGTCGATAGACCG"
"ATAGACTCGTGATAGACGTCGATAGACGTCGATAGAGTATAGACC"
"GTGATAGACGTCGAGAAGACGTCGATAGACGTCGATAGACGTCGA"
"TAGAGATAGACGTCGATAGACCGTATAGAAGACGTCGATAGATAG"
"ACGTCGATAGACCGTAGACGTCGATAGACGTCGATAGACCGT"
]
|> String.concat ""
let elementsPerRow = 60
let headers =
[0..elementsPerRow]
|> Seq.map (fun x ->
if x%10=0 && x <> 0 then "|"
else ""
)
let cells =
sequence
|> Seq.chunkBySize elementsPerRow
|> Seq.mapi (fun i x -> Seq.append [string (i * elementsPerRow)] (Seq.map string x))
let cellcolors =
cells
|> Seq.map (fun row ->
row
|> Seq.map (fun element ->
match element with
//colors taken from DRuMS
//(http://biomodel.uah.es/en/model4/dna/atgc.htm)
| "A" -> Color.fromHex "#5050FF"
| "T" -> Color.fromHex "#E6E600"
| "G" -> Color.fromHex "#00C000"
| "C" -> Color.fromHex "#E00000"
| "U" -> Color.fromHex "#B48100"
| _ -> Color.fromString "white"
)
)
|> Seq.transpose
|> Seq.map (fun x -> Seq.append x (seq [Color.fromString "white"]))
|> Seq.map Color.fromColors
|> Color.fromColors
let font = Font.init(FontFamily.Consolas,Size=14.)
let line = Line.init(0.,Color.fromString "white")
let chartwidth = 50. + 10. * float elementsPerRow
let table4 =
Chart.Table(
headers,
cells,
LineCells = line,
LineHeader = line,
HeightCells = 20.,
FontHeader = font,
FontCells = font,
ColumnWidth = [50;10],
AlignCells = [HorizontalAlign.Right;HorizontalAlign.Center],
ColorCells = cellcolors
)
|> Chart.withSize(chartwidth,nan)
|> Chart.withTitle "Sequence A"
(*** condition: ipynb ***)
#if IPYNB
table4
#endif // IPYNB
(***hide***)
table4 |> GenericChart.toChartHTML
(***include-it-raw***)