Skip to content

Commit 6a87f86

Browse files
committed
(closes #399) Add json generation functions for GenericChart, add json tests for all Chart2D test charts.
1 parent f2c4b66 commit 6a87f86

File tree

6 files changed

+626
-8
lines changed

6 files changed

+626
-8
lines changed

src/Plotly.NET/ChartAPI/GenericChart.fs

+57
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,22 @@ module GenericChart =
117117
Frames = []
118118
}
119119

120+
type ChartDTO =
121+
{
122+
[<JsonProperty("data")>]
123+
Data: Trace list
124+
[<JsonProperty("layout")>]
125+
Layout: Layout
126+
[<JsonProperty("config")>]
127+
Config: Config
128+
}
129+
static member create data layout config =
130+
{
131+
Data = data
132+
Layout = layout
133+
Config = config
134+
}
135+
120136
//TO-DO refactor as type with static members to remove verbose top namespace from 'GenericChart.GenericChart'
121137
type GenericChart =
122138
| Chart of Trace * Layout * Config * DisplayOptions
@@ -317,6 +333,47 @@ module GenericChart =
317333
)
318334
|> RenderView.AsString.htmlDocument
319335

336+
/// <summary>
337+
/// Serializes a GenericChart to a JSON string, representing the data and layout of the GenericChart:
338+
///
339+
/// {
340+
///
341+
/// "data": [ -serialized traces array- ] ,
342+
///
343+
/// "layout": { -serialized layout object- } ,
344+
///
345+
/// "frames": [ -empty array, not supported yet, legacy stuff- ]
346+
///
347+
/// }
348+
/// </summary>
349+
/// <param name="gChart">the chart to serialize</param>
350+
let toFigureJson gChart =
351+
gChart
352+
|> toFigure
353+
|> fun f -> JsonConvert.SerializeObject(f, Globals.JSON_CONFIG)
354+
355+
/// <summary>
356+
/// Serializes a GenericChart to a JSON string, representing the data, layout and config of the GenericChart:
357+
///
358+
/// {
359+
///
360+
/// "data": [ -serialized traces array- ] ,
361+
///
362+
/// "layout": { -serialized layout object- } ,
363+
///
364+
/// "config": { -serialized config object- }
365+
///
366+
/// }
367+
/// </summary>
368+
/// <param name="gChart">the chart to serialize</param>
369+
let toJson gChart =
370+
371+
ChartDTO.create
372+
(getTraces gChart)
373+
(getLayout gChart)
374+
(getConfig gChart)
375+
|> fun dto -> JsonConvert.SerializeObject(dto, Globals.JSON_CONFIG)
376+
320377
/// Creates a new GenericChart whose traces are the results of applying the given function to each of the trace of the GenericChart.
321378
let mapTrace f gChart =
322379
match gChart with

tests/Common/FSharpTestBase/TestCharts/Chart2DTestCharts.fs

+5-4
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ module Bar =
163163
let keys = ["Product A"; "Product B"; "Product C";]
164164
Chart.Bar(values = values, Keys = keys, UseDefaults = false)
165165

166+
module StackedBar =
167+
166168
let ``Two stacked bars chart`` =
167169
let values = [20; 14; 23;]
168170
let keys = ["Product A"; "Product B"; "Product C";]
@@ -172,15 +174,16 @@ module Bar =
172174
]
173175
|> Chart.combine
174176

175-
module StackedBar = ()
176-
177177
module Column =
178178

179179
let ``Simple column chart`` =
180180
let values = [20; 14; 23;]
181181
let keys = ["Product A"; "Product B"; "Product C";]
182182
Chart.Column(values = values, Keys = keys, UseDefaults = false)
183183

184+
module StackedColumn =
185+
186+
184187
let ``Two stacked columns chart`` =
185188
let values = [20; 14; 23;]
186189
let keys = ["Product A"; "Product B"; "Product C";]
@@ -190,8 +193,6 @@ module Column =
190193
]
191194
|> Chart.combine
192195

193-
module StackedColumn = ()
194-
195196
module Histogram =
196197

197198
let ``Simple histogram`` =

tests/Common/FSharpTestBase/TestUtils.fs

+10
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,16 @@ module HtmlCodegen =
203203
let emptyLayout chart =
204204
"var layout = {};" |> chartGeneratedContains chart
205205

206+
module JsonGen =
207+
208+
let jsonIs chart expected =
209+
let json = chart |> GenericChart.toJson
210+
Expect.equal json expected $"JSON not equal to expected value."
211+
212+
let figureJsonIs chart expected =
213+
let json = chart |> GenericChart.toFigureJson
214+
Expect.equal json expected $"JSON not equal to expected value."
215+
206216
module Objects =
207217

208218
let jsonFieldIsSetWith fieldName expected (object:#DynamicObj) =

tests/CoreTests/CoreTests/CoreTests.fsproj

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
<Compile Include="HTMLCodegen\SimpleTests.fs" />
2323
<Compile Include="HTMLCodegen\ChartLayout.fs" />
2424
<Compile Include="HTMLCodegen\MulticategoryData.fs" />
25+
<!--JSONGen-->
26+
<Compile Include="JSONGen\Chart2D.fs" />
2527
<!--Misc-->
2628
<Compile Include="ChartAPIs\WithAxis.fs" />
2729
<Compile Include="ChartAPIs\Combine.fs" />

tests/CoreTests/CoreTests/HTMLCodegen/Chart2D.fs

+4-4
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,11 @@ module StackedBar =
198198
testList "StackedBar" [
199199
testCase "Stacked bar data" ( fun () ->
200200
"""var data = [{"type":"bar","name":"old","x":[20,14,23],"y":["Product A","Product B","Product C"],"orientation":"h","marker":{"pattern":{}}},{"type":"bar","name":"new","x":[8,21,13],"y":["Product A","Product B","Product C"],"orientation":"h","marker":{"pattern":{}}}];"""
201-
|> chartGeneratedContains Bar.``Two stacked bars chart``
201+
|> chartGeneratedContains StackedBar.``Two stacked bars chart``
202202
);
203203
testCase "Stacked bar layout" ( fun () ->
204204
"var layout = {\"barmode\":\"stack\"};"
205-
|> chartGeneratedContains Bar.``Two stacked bars chart``
205+
|> chartGeneratedContains StackedBar.``Two stacked bars chart``
206206
);
207207
]
208208
]
@@ -229,11 +229,11 @@ module StackedColumn =
229229
testList "StackedColumn" [
230230
testCase "Stacked column data" ( fun () ->
231231
"""var data = [{"type":"bar","name":"old","x":["Product A","Product B","Product C"],"y":[20,14,23],"orientation":"v","marker":{"pattern":{}}},{"type":"bar","name":"new","x":["Product A","Product B","Product C"],"y":[8,21,13],"orientation":"v","marker":{"pattern":{}}}];"""
232-
|> chartGeneratedContains Column.``Two stacked columns chart``
232+
|> chartGeneratedContains StackedColumn.``Two stacked columns chart``
233233
);
234234
testCase "Stacked column layout" ( fun () ->
235235
"var layout = {\"barmode\":\"stack\"};"
236-
|> chartGeneratedContains Column.``Two stacked columns chart``
236+
|> chartGeneratedContains StackedColumn.``Two stacked columns chart``
237237
);
238238
]
239239
]

0 commit comments

Comments
 (0)