Skip to content

Commit 5ee5575

Browse files
authored
Merge pull request #237 from plotly/defaults
2 parents a2ac99d + f73dc3b commit 5ee5575

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2543
-1356
lines changed

Plotly.NET.sln

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.29709.97
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31903.59
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "project", "project", "{BF60BC93-E09B-4E5F-9D85-95A519479D54}"
77
ProjectSection(SolutionItems) = preProject
@@ -44,6 +44,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{7B09CC0A-F
4444
docs\00_1_image-export.fsx = docs\00_1_image-export.fsx
4545
docs\00_2_display-options.fsx = docs\00_2_display-options.fsx
4646
docs\00_3_chart-config.fsx = docs\00_3_chart-config.fsx
47+
docs\00_4_templates.fsx = docs\00_4_templates.fsx
48+
docs\00_5_defaults.fsx = docs\00_5_defaults.fsx
4749
docs\01_0_axis-styling.fsx = docs\01_0_axis-styling.fsx
4850
docs\01_1_errorbars.fsx = docs\01_1_errorbars.fsx
4951
docs\01_2_multiple-charts.fsx = docs\01_2_multiple-charts.fsx

docs/00_0_basics.fsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ let myTrace = Trace("scatter") // create a scatter trace
7777
myTrace?x <- [0;1;2] // set the x property (the x dimension of the data)
7878
myTrace?y <- [0;1;2] // set the y property (the y dimension of the data)
7979

80-
GenericChart.ofTraceObject myTrace // create a generic chart (layout and config are empty objects)
80+
GenericChart.ofTraceObject false myTrace // create a generic chart (layout and config are empty objects. When using useDefaults = true, default styling will be applied.)
8181
|> Chart.show
8282

8383
(**

docs/00_4_templates.fsx

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
(**
2+
---
3+
title: Chart Templates
4+
category: General
5+
categoryindex: 1
6+
index: 5
7+
---
8+
*)
9+
10+
(*** hide ***)
11+
12+
(*** condition: prepare ***)
13+
#r "nuget: Newtonsoft.JSON, 12.0.3"
14+
#r "nuget: DynamicObj"
15+
#r "../bin/Plotly.NET/netstandard2.0/Plotly.NET.dll"
16+
17+
(*** condition: ipynb ***)
18+
#if IPYNB
19+
#r "nuget: Plotly.NET, {{fsdocs-package-version}}"
20+
#r "nuget: Plotly.NET.Interactive, {{fsdocs-package-version}}"
21+
#endif // IPYNB
22+
23+
(**
24+
# Chart Templates
25+
26+
[![Binder]({{root}}img/badge-binder.svg)](https://mybinder.org/v2/gh/plotly/Plotly.NET/gh-pages?filepath={{fsdocs-source-basename}}.ipynb)&emsp;
27+
[![Script]({{root}}img/badge-script.svg)]({{root}}{{fsdocs-source-basename}}.fsx)&emsp;
28+
[![Notebook]({{root}}img/badge-notebook.svg)]({{root}}{{fsdocs-source-basename}}.ipynb)
29+
30+
## Using premade templates
31+
32+
premade templates can be accessed via the `ChartTemplates` module. In fact, the `ChartTemplates.plotly` template is always active by default (see [global defaults](./005_defaults.html))
33+
*)
34+
open Plotly.NET
35+
36+
let lightMirrored =
37+
Chart.Point([1,2])
38+
|> Chart.withTemplate ChartTemplates.lightMirrored
39+
40+
(*** condition: ipynb ***)
41+
#if IPYNB
42+
lightMirrored
43+
#endif // IPYNB
44+
45+
(***hide***)
46+
lightMirrored |> GenericChart.toChartHTML
47+
(***include-it-raw***)
48+
49+
(**
50+
51+
here are then contents of the template `plotly` which is used by default for all charts:
52+
53+
*)
54+
55+
open DynamicObj
56+
57+
(***include-output***)
58+
ChartTemplates.plotly
59+
|> DynObj.print
60+
61+
(**
62+
63+
## Creating custom templates
64+
65+
Chart Templates consist of a `Layout` object and a collection of `Trace` objects. Both are used to set default values for all possible styling options:
66+
*)
67+
68+
open Plotly.NET.TraceObjects
69+
70+
let layoutTemplate =
71+
Layout.init(
72+
Title = Title.init("I will always be there now!")
73+
)
74+
75+
let traceTemplates =
76+
[
77+
Trace2D.initScatter(
78+
Trace2DStyle.Scatter(
79+
Marker = Marker.init(Symbol = StyleParam.MarkerSymbol.ArrowLeft, Size = 20)
80+
)
81+
)
82+
]
83+
84+
let myTemplate = Template.init(layoutTemplate, traceTemplates)
85+
86+
let myTemplateExampleChart =
87+
Chart.Point([1,2])
88+
|> Chart.withTemplate myTemplate
89+
90+
(*** condition: ipynb ***)
91+
#if IPYNB
92+
myTemplateExampleChart
93+
#endif // IPYNB
94+
95+
(***hide***)
96+
myTemplateExampleChart |> GenericChart.toChartHTML
97+
(***include-it-raw***)

docs/00_5_defaults.fsx

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
(**
2+
---
3+
title: Global default values
4+
category: General
5+
categoryindex: 1
6+
index: 6
7+
---
8+
*)
9+
10+
(*** hide ***)
11+
12+
(*** condition: prepare ***)
13+
#r "nuget: Newtonsoft.JSON, 12.0.3"
14+
#r "nuget: DynamicObj"
15+
#r "../bin/Plotly.NET/netstandard2.0/Plotly.NET.dll"
16+
17+
(*** condition: ipynb ***)
18+
#if IPYNB
19+
#r "nuget: Plotly.NET, {{fsdocs-package-version}}"
20+
#r "nuget: Plotly.NET.Interactive, {{fsdocs-package-version}}"
21+
#endif // IPYNB
22+
23+
(**
24+
# Global default values
25+
26+
[![Binder]({{root}}img/badge-binder.svg)](https://mybinder.org/v2/gh/plotly/Plotly.NET/gh-pages?filepath={{fsdocs-source-basename}}.ipynb)&emsp;
27+
[![Script]({{root}}img/badge-script.svg)]({{root}}{{fsdocs-source-basename}}.fsx)&emsp;
28+
[![Notebook]({{root}}img/badge-notebook.svg)]({{root}}{{fsdocs-source-basename}}.ipynb)
29+
30+
Plotly.NET provides mutable global default values in the `Defaults` module.
31+
32+
These values are always used in Chart generation. The default values are:
33+
34+
|Value name|Value|
35+
|---|---|
36+
| DefaultWidth | `600` |
37+
| DefaultHeight | `600` |
38+
| DefaultConfig | `Config.init(Responsive = true)` |
39+
| DefaultDisplayOptions | `DisplayOptions.init()` |
40+
| DefaultTemplate | `ChartTemplates.plotly` |
41+
42+
## Changing default values
43+
44+
The following code replaces the default template from the global defaults:
45+
*)
46+
open Plotly.NET
47+
48+
let before = Chart.Point([1,2])
49+
50+
(*** condition: ipynb ***)
51+
#if IPYNB
52+
before
53+
#endif // IPYNB
54+
55+
(***hide***)
56+
before |> GenericChart.toChartHTML
57+
(***include-it-raw***)
58+
59+
Defaults.DefaultTemplate <- ChartTemplates.lightMirrored
60+
61+
let after = Chart.Point([1,2])
62+
63+
(*** condition: ipynb ***)
64+
#if IPYNB
65+
after
66+
#endif // IPYNB
67+
68+
(***hide***)
69+
after |> GenericChart.toChartHTML
70+
(***include-it-raw***)
71+
72+
73+
(**
74+
## Ignoring global defaults
75+
76+
All Chart functions have a `UseDefaults` argument, which when set to `false` will ignore all global defaults:
77+
*)
78+
79+
80+
let noDefaults = Chart.Point([1,2], UseDefaults = false)
81+
82+
(*** condition: ipynb ***)
83+
#if IPYNB
84+
noDefaults
85+
#endif // IPYNB
86+
87+
(***hide***)
88+
noDefaults |> GenericChart.toChartHTML
89+
(***include-it-raw***)

docs/03_2_3d-mesh-plots.fsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ let mesh3d =
7474
mesh3d?contour <- Contours.initXyz(Show=true)
7575
mesh3d
7676
)
77-
|> GenericChart.ofTraceObject
77+
|> GenericChart.ofTraceObject true
7878

7979
(*** condition: ipynb ***)
8080
#if IPYNB

docs/09_1_parallel-coords.fsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ let parcoords =
8686
dyn?line <- Line.init(Color=Color.fromString "blue")
8787

8888
dyn
89-
|> GenericChart.ofTraceObject
89+
|> GenericChart.ofTraceObject true
9090

9191
(*** condition: ipynb ***)
9292
#if IPYNB

docs/index.fsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ let trace =
277277
tmp?name <- "Hello from F#"
278278
tmp
279279

280-
GenericChart.ofTraceObject(trace)
280+
GenericChart.ofTraceObject true trace
281281
|> GenericChart.setLayout layout
282282

283283
(**

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"sdk": {
33
"version": "5.0.100",
4-
"rollForward": "latestFeature"
4+
"rollForward": "latestMajor"
55
}
66
}

src/Plotly.NET/ChartAPI/Chart.fs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ type Chart =
5959

6060
let trace = Trace2D.initScatter(id)
6161
trace.Remove("type") |> ignore
62-
GenericChart.ofTraceObject trace
62+
GenericChart.ofTraceObject false trace
6363
|> GenericChart.mapLayout ( fun l ->
6464
l
6565
|> Layout.AddLinearAxis(StyleParam.SubPlotId.XAxis 1,hiddenAxis())
@@ -976,10 +976,9 @@ type Chart =
976976
static member withTemplate(template: Template) =
977977
(fun (ch:GenericChart) ->
978978
ch
979-
|> GenericChart.mapLayout (fun l ->
980-
template |> DynObj.setValue l "template"
981-
l
982-
)
979+
|> GenericChart.mapLayout (
980+
Layout.style(Template = (template :> DynamicObj))
981+
)
983982
)
984983

985984
// TODO: Include withLegend & withLegendStyle

0 commit comments

Comments
 (0)