Skip to content

Commit 3a69790

Browse files
committed
Refactor test charts for simple charts tests into FSharpTestBase
1 parent 4e6e7c8 commit 3a69790

6 files changed

Lines changed: 399 additions & 355 deletions

File tree

tests/Common/FSharpTestBase/FSharpTestBase.fsproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<EmbeddedResource Include="..\..\..\docs\img\logo.png" />
10+
<EmbeddedResource Include="..\..\..\src\Plotly.NET\plotly-2.19.1.min.js" />
11+
<EmbeddedResource Include="..\..\..\src\Plotly.NET\plotly-2.19.1.min.js.LICENSE.txt" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<Compile Include="TestUtils.fs" />
16+
<Compile Include="TestCharts\ChartDomainTestCharts.fs" />
917
<Compile Include="TestCharts\Chart2DTestCharts.fs" />
1018
</ItemGroup>
1119

tests/Common/FSharpTestBase/TestCharts/Chart2DTestCharts.fs

Lines changed: 128 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,33 @@ module Spline =
7979
Chart.Spline(x = x, y = y, Name="spline", UseDefaults = false)
8080

8181

82-
module Bubble = ()
82+
module Bubble =
8383

84-
module Range = ()
84+
let ``Simple bubble chart`` =
85+
let x = [2; 4; 6;]
86+
let y = [4; 1; 6;]
87+
let size = [19; 26; 55;]
88+
Chart.Bubble(x = x, y = y,sizes = size, UseDefaults = false)
89+
90+
module Range =
91+
92+
let ``Styled range chart`` =
93+
let rnd = System.Random(5)
94+
95+
let x = [1.; 2.; 3.; 4.; 5.; 6.; 7.; 8.; 9.; 10.; ]
96+
let y = [2.; 1.5; 5.; 1.5; 3.; 2.5; 2.5; 1.5; 3.5; 1.]
97+
98+
let yUpper = y |> List.map (fun v -> v + rnd.NextDouble())
99+
let yLower = y |> List.map (fun v -> v - rnd.NextDouble())
100+
Chart.Range(
101+
x = x,
102+
y = y,
103+
upper = yUpper,
104+
lower = yLower,
105+
mode = StyleParam.Mode.Lines_Markers,
106+
MarkerColor=Color.fromString "grey",
107+
RangeColor=Color.fromString "lightblue",
108+
UseDefaults = false)
85109

86110
module Area =
87111

@@ -161,11 +185,110 @@ module Violin = ()
161185

162186
module Histogram2DContour = ()
163187

164-
module Heatmap = ()
188+
module Heatmap =
189+
190+
let ``simple heatmap with custom colorscale`` =
191+
let matrix =
192+
[[1.;1.5;0.7;2.7];
193+
[2.;0.5;1.2;1.4];
194+
[0.1;2.6;2.4;3.0];]
195+
196+
let rownames = ["p3";"p2";"p1"]
197+
let colnames = ["Tp0";"Tp30";"Tp60";"Tp160"]
198+
199+
let colorscaleValue =
200+
StyleParam.Colorscale.Custom [(0.0,Color.fromString "#3D9970");(1.0,Color.fromString "#001f3f")]
201+
202+
Chart.Heatmap(
203+
zData = matrix,
204+
colNames = colnames,
205+
rowNames = rownames,
206+
ColorScale=colorscaleValue,
207+
ShowScale=true,
208+
UseDefaults = false
209+
)
210+
|> Chart.withSize(700,500)
211+
|> Chart.withMarginSize(Left=200.)
165212

166-
module AnnotatedHeatmap = ()
213+
214+
let ``Simple heatmal with custom colorscale and styled colorbar`` =
215+
let matrix =
216+
[[1.;1.5;0.7;2.7];
217+
[2.;0.5;1.2;1.4];
218+
[0.1;2.6;2.4;3.0];]
219+
220+
let rownames = ["p3";"p2";"p1"]
221+
let colnames = ["Tp0";"Tp30";"Tp60";"Tp160"]
222+
223+
let colorscaleValue =
224+
StyleParam.Colorscale.Custom [(0.0,Color.fromString "#3D9970");(1.0,Color.fromString "#001f3f")]
225+
226+
Chart.Heatmap(
227+
zData = matrix,
228+
colNames = colnames,
229+
rowNames = rownames,
230+
ColorScale=colorscaleValue,
231+
ShowScale=true,
232+
UseDefaults = false
233+
)
234+
|> Chart.withSize(700.,500.)
235+
|> Chart.withMarginSize(Left=200.)
236+
|> Chart.withColorBarStyle(TitleText = "Im the Colorbar")
237+
238+
239+
module AnnotatedHeatmap =
240+
241+
242+
let ``Simple annotated heatmap`` =
243+
Chart.AnnotatedHeatmap(
244+
zData = [
245+
[1..5]
246+
[6..10]
247+
[11..15]
248+
],
249+
annotationText = [
250+
["1,1";"1,2";"1,3"]
251+
["2,1";"2,2";"2,3"]
252+
["3,1";"3,2";"3,3"]
253+
],
254+
X = ["C1";"C2";"C3"],
255+
Y = ["R1";"R2";"R3"],
256+
ReverseYAxis = true,
257+
UseDefaults = false
258+
)
259+
260+
module Image =
167261

168-
module Image = ()
262+
let private colors = [
263+
[[0 ;0 ;255]; [255;255;0 ]; [0 ;0 ;255]]
264+
[[255;0 ;0 ]; [255;0 ;255]; [255;0 ;255]]
265+
[[0 ;255;0 ]; [0 ;255;255]; [255;0 ;0 ]]
266+
]
267+
268+
let ``Raw color component image chart`` =
269+
Chart.Image(Z=colors, UseDefaults = false)
270+
|> Chart.withTitle "Image chart from raw color component arrays"
271+
272+
let ``HSL image chart`` =
273+
Chart.Image(Z=colors, ColorModel=StyleParam.ColorModel.HSL, UseDefaults = false)
274+
|> Chart.withTitle "HSL color model"
275+
276+
let ``ARGB image chart`` =
277+
let argbs = [
278+
[ColorKeyword.AliceBlue ; ColorKeyword.CornSilk ; ColorKeyword.LavenderBlush ] |> List.map ARGB.fromKeyword
279+
[ColorKeyword.DarkGray ; ColorKeyword.Snow ; ColorKeyword.MidnightBlue ] |> List.map ARGB.fromKeyword
280+
[ColorKeyword.LightSteelBlue; ColorKeyword.DarkKhaki; ColorKeyword.LightAkyBlue ] |> List.map ARGB.fromKeyword
281+
]
282+
Chart.Image(z = argbs, UseDefaults = false)
283+
|> Chart.withTitle "ARGB image chart"
284+
285+
let ``Image chart from base64 string`` =
286+
let base64String = TestUtils.getLogoPNG()
287+
Chart.Image(
288+
Source=($"data:image/jpg;base64,{base64String}"),
289+
UseDefaults = false
290+
)
291+
|> Chart.withTitle "This is Plotly.NET:"
169292

170293
module Contour = ()
171294

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
module ChartDomainTestCharts
2+
3+
open Plotly.NET
4+
5+
6+
module Pie =
7+
8+
let ``Simple pie chart`` =
9+
let values = [19; 26; 55;]
10+
let labels = ["Residential"; "Non-Residential"; "Utility"]
11+
Chart.Pie(values = values, Labels = labels, UseDefaults = false)
12+
13+
let ``Styled pie chart`` =
14+
15+
let values = [19; 26; 55;]
16+
let labels = ["Residential"; "Non-Residential"; "Utility"]
17+
18+
Chart.Pie(
19+
values = values,
20+
Labels = labels,
21+
SectionColors = [
22+
Color.fromKeyword Aqua
23+
Color.fromKeyword Salmon
24+
Color.fromKeyword Tan
25+
],
26+
SectionOutlineColor = Color.fromKeyword Black,
27+
SectionOutlineWidth = 2.,
28+
MultiText = [
29+
"Some"
30+
"More"
31+
"Stuff"
32+
],
33+
MultiTextPosition = [
34+
StyleParam.TextPosition.Inside
35+
StyleParam.TextPosition.Outside
36+
StyleParam.TextPosition.Inside
37+
],
38+
Rotation = 45.,
39+
MultiPull = [0.; 0.3; 0.],
40+
UseDefaults = false
41+
)
42+
43+
module Doughnut =
44+
45+
let ``Simple doughnut chart`` =
46+
let values = [19; 26; 55;]
47+
let labels = ["Residential"; "Non-Residential"; "Utility"]
48+
Chart.Doughnut(
49+
values = values,
50+
Labels = labels,
51+
Hole=0.3,
52+
MultiText=labels,
53+
UseDefaults = false
54+
)
55+
56+
module FunnelArea = ()
57+
58+
module Sunburst = ()
59+
60+
module Treemap = ()
61+
62+
module ParralelCoord = ()
63+
64+
module ParralelCategories = ()
65+
66+
module Sankey = ()
67+
68+
module Table =
69+
70+
let ``Simple table chart`` =
71+
let header = ["<b>RowIndex</b>";"A";"simple";"table"]
72+
let rows =
73+
[
74+
["0";"I" ;"am" ;"a"]
75+
["1";"little";"example";"!"]
76+
]
77+
Chart.Table(headerValues = header, cellsValues = rows, UseDefaults = false)
78+
79+
let ``Styled table chart`` =
80+
let header = ["<b>RowIndex</b>";"A";"simple";"table"]
81+
let rows =
82+
[
83+
["0";"I" ;"am" ;"a"]
84+
["1";"little";"example";"!"]
85+
]
86+
Chart.Table(
87+
headerValues = header,
88+
cellsValues = rows,
89+
HeaderAlign = StyleParam.HorizontalAlign.Center,
90+
CellsMultiAlign = [StyleParam.HorizontalAlign.Left; StyleParam.HorizontalAlign.Center; StyleParam.HorizontalAlign.Right],
91+
HeaderFillColor = Color.fromString "#45546a",
92+
CellsFillColor = Color.fromColors [Color.fromString "#deebf7"; Color.fromString "lightgrey"; Color.fromString "#deebf7"; Color.fromString "lightgrey"],
93+
HeaderHeight = 30,
94+
HeaderOutlineColor = Color.fromString "black",
95+
HeaderOutlineWidth = 2.,
96+
MultiColumnWidth = [70.; 50.; 100.; 70.],
97+
ColumnOrder = [1; 2; 3; 4],
98+
UseDefaults = false
99+
)
100+
101+
let ``Cell color dependent table chart`` =
102+
let header2 = ["Identifier";"T0";"T1";"T2";"T3"]
103+
let rowvalues =
104+
[
105+
[10001.;0.2;2.0;4.0;5.0]
106+
[10002.;2.1;2.0;1.8;2.1]
107+
[10003.;4.5;3.0;2.0;2.5]
108+
[10004.;0.0;0.1;0.3;0.2]
109+
[10005.;1.0;1.6;1.8;2.2]
110+
[10006.;1.0;0.8;1.5;0.7]
111+
[10007.;2.0;2.0;2.1;1.9]
112+
]
113+
|> Seq.sortBy (fun x -> x.[1])
114+
115+
//map color from value to hex representation
116+
let mapColor min max value =
117+
let proportion =
118+
(255. * (value - min) / (max - min))
119+
|> int
120+
Color.fromRGB 255 (255 - proportion) proportion
121+
122+
//Assign a color to every cell seperately. Matrix must be transposed for correct orientation.
123+
let cellcolor =
124+
rowvalues
125+
|> Seq.map (fun row ->
126+
row
127+
|> Seq.mapi (fun index value ->
128+
if index = 0 then Color.fromString "white"
129+
else mapColor 0. 5. value
130+
)
131+
)
132+
|> Seq.transpose
133+
|> Seq.map Color.fromColors
134+
|> Color.fromColors
135+
136+
Chart.Table(
137+
headerValues = header2,
138+
cellsValues = rowvalues,
139+
CellsFillColor=cellcolor,
140+
UseDefaults = false
141+
)
142+
143+
144+
let ``Sequence representation table chart`` =
145+
let sequence =
146+
[
147+
"ATGAGACGTCGAGACTGATAGACGTCGATAGACGTCGATAGACCG"
148+
"ATAGACTCGTGATAGACGTCGATAGACGTCGATAGAGTATAGACC"
149+
"GTGATAGACGTCGAGAAGACGTCGATAGACGTCGATAGACGTCGA"
150+
"TAGAGATAGACGTCGATAGACCGTATAGAAGACGTCGATAGATAG"
151+
"ACGTCGATAGACCGTAGACGTCGATAGACGTCGATAGACCGT"
152+
]
153+
|> String.concat ""
154+
155+
let elementsPerRow = 60
156+
157+
let headers =
158+
[0..elementsPerRow]
159+
|> Seq.map (fun x ->
160+
if x%10=0 && x <> 0 then "|"
161+
else ""
162+
)
163+
164+
let cells =
165+
sequence
166+
|> Seq.chunkBySize elementsPerRow
167+
|> Seq.mapi (fun i x -> Seq.append [string (i * elementsPerRow)] (Seq.map string x))
168+
169+
let cellcolors =
170+
cells
171+
|> Seq.map (fun row ->
172+
row
173+
|> Seq.map (fun element ->
174+
match element with
175+
//colors taken from DRuMS
176+
//(http://biomodel.uah.es/en/model4/dna/atgc.htm)
177+
| "A" -> Color.fromString "#5050FF"
178+
| "T" -> Color.fromString "#E6E600"
179+
| "G" -> Color.fromString "#00C000"
180+
| "C" -> Color.fromString "#E00000"
181+
| "U" -> Color.fromString "#B48100"
182+
| _ -> Color.fromString "white"
183+
)
184+
)
185+
|> Seq.transpose
186+
|> Seq.map (fun x -> Seq.append x (seq [Color.fromString "white"]))
187+
|> Seq.map Color.fromColors
188+
|> Color.fromColors
189+
190+
let line = Line.init(Width = 0., Color = Color.fromString "white")
191+
let chartwidth = 50 + 10 * elementsPerRow
192+
193+
Chart.Table(
194+
headerValues = headers,
195+
cellsValues = cells,
196+
CellsOutline = line,
197+
HeaderOutline = line,
198+
CellsHeight = 20,
199+
MultiColumnWidth = [50.;10.],
200+
CellsMultiAlign = [StyleParam.HorizontalAlign.Right;StyleParam.HorizontalAlign.Center],
201+
CellsFillColor = cellcolors,
202+
UseDefaults = false
203+
)
204+
|> Chart.withSize(Width=chartwidth)
205+
|> Chart.withTitle "Sequence A"
206+
207+
module Indicator = ()
208+
209+
module Icicle = ()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module TestUtils
2+
3+
open System.Reflection
4+
open System.IO
5+
6+
let getLogoPNG() =
7+
let assembly = Assembly.GetExecutingAssembly()
8+
use str = assembly.GetManifestResourceStream($"FSharpTestBase.logo.png")
9+
use r = new BinaryReader(str)
10+
r.ReadBytes(int(str.Length))
11+
|> System.Convert.ToBase64String
12+
13+
let getFullPlotlyJS() =
14+
let assembly = Assembly.GetExecutingAssembly()
15+
use str = assembly.GetManifestResourceStream($"FSharpTestBase.plotly-{Globals.PLOTLYJS_VERSION}.min.js")
16+
use r = new StreamReader(str)
17+
r.ReadToEnd()

0 commit comments

Comments
 (0)