You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/00_0_basics.fsx
+83-10Lines changed: 83 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -35,21 +35,30 @@ _This section is WIP._
35
35
36
36
### Table of contents
37
37
38
+
- Library design(#Library-design)
38
39
-[GenericChart](#GenericChart)
39
40
-[Working with GenericCharts](#Working-with-GenericCharts)
40
41
-[Dynamic object style](#Dynamic-object-style)
41
42
42
-
## GenericChart
43
-
43
+
## Library design
44
44
45
45
Plotly.NET is a .NET wrapper for creation of [plotly charts]() written in F#. This means that, under the hood, all functionality creates JSON objects that can be rendered by plotly.
46
46
47
+
A plotly.js chart consists of 3 objects:
48
+
49
+
-`data`, which is a collection of `traces` which represent the data and chart type used to visualize the data
50
+
-`layout`, which controls the general chart layout such as axis positions and styles
51
+
-`config` high level properties of the chart like making all chart elements editable or the tool bar on top
52
+
53
+
These are mirrored in Plotly.NET's central type, `GenericChart`:
54
+
55
+
## GenericChart
56
+
47
57
The central type that gets created by all Chart constructors is `GenericChart`, which itself represents either a single chart or a multi chart (as a Discriminate Union type). It looks like this:
@@ -62,23 +71,87 @@ As you can see, a `GenericChart` consists of four top level objects - `Trace` (m
62
71
-`Config` is an object that configures high level properties of the chart like making all chart elements editable or the tool bar on top
63
72
-`DisplayOptions` is an object that contains meta information about how the html document that contains the chart.
64
73
65
-
## Working with GenericCharts
74
+
### Layers of abstraction
75
+
76
+
Plotly.NET uses multiple layers of abstractions to generate valid plotly.js JSON objects with different levels of control and complexity:
77
+
78
+
#### The Chart module
79
+
80
+
The `Chart` module provides the highest layer of abstraction. Here, plotly.js trace types are broken down to the most common and useful styling options, and combined with common layout settings.
81
+
It also provides composite charts which consist of multiple traces such as `Chart.Range`, which really is a combination of 3 scatter traces.
82
+
83
+
Here is an example on how to create a simple 2D point chart:
84
+
*)
85
+
86
+
letpointChart=
87
+
Chart.Point([1,2;3,4])
88
+
89
+
(*** condition: ipynb ***)
90
+
#if IPYNB
91
+
pointChart
92
+
#endif// IPYNB
93
+
94
+
(***hide***)
95
+
pointChart |> GenericChart.toChartHTML
96
+
(***include-it-raw***)
66
97
67
-
### Dynamic object style
98
+
(**
99
+
#### The TraceStyle modules
100
+
101
+
The TraceStyle modules offer access to all parameters supported by plotly.js for the respective trace type. If you want to create a `scatter` trace, you can use the function
102
+
`Trace2D.initScatter`, which will initialize an empty trace of type `scatter` and apply a styling function to it. This function would be `Trace2DStyle.Scatter`, which can apply all scatter related parameters to a trace.
103
+
In contrast to the `Chart` module, the parameters are named exactly the same as in plotly.js (but in PascalCase).
104
+
105
+
To create a GenericChart from a `Trace` object, you can use `GenericChart.ofTraceObject`.
106
+
Compare how many more styling options you have compared to `Chart.Point` above, but also take a look at how more verbose you have to be.
107
+
You can clearly see the advantages and disadvantages of both approaches.
108
+
*)
109
+
110
+
letwithTraceStyle=
111
+
Trace2D.initScatter(
112
+
Trace2DStyle.Scatter(
113
+
X =[1;3],
114
+
Y =[2;4]
115
+
)
116
+
)
117
+
|> GenericChart.ofTraceObject true
118
+
119
+
(*** condition: ipynb ***)
120
+
#if IPYNB
121
+
withTraceStyle
122
+
#endif// IPYNB
68
123
69
-
Plotly.NET has multiple abstraction layers to work with `GenericChart`s. The prime directive for all functions provided by this library is the construction of valid plotly JSON objects.
124
+
(***hide***)
125
+
withTraceStyle |> GenericChart.toChartHTML
126
+
(***include-it-raw***)
127
+
128
+
(**
129
+
#### Dynamic object
130
+
131
+
The prime directive for all functions provided by Plotly.NET is the construction of valid plotly JSON objects.
70
132
For this purpose, `Trace`, `Layout`, and `Config` (and many other internal objects) are inheriting from [`DynamicObj`](https://github.com/plotly/Plotly.NET/blob/dev/src/Plotly.NET/DynamicObj.fs),
71
133
an extension of `DynamicObject` which makes it possible to set arbitraryly named and typed properties of these objects via the `?` operator.
72
134
135
+
If you want to exactly mirror a plotly.js tutorial, or want to set properties that for any reason are not abstracted in Plotly.NET,
136
+
it can be useful to use the power of DynamicObj to set the parameters directly. Just make sure that the property name is exactly the same as in plotly.js (all lowercase)
137
+
73
138
So if you want to set any kind of property on one of these objects you can do it in a very declarative way like this:
74
139
*)
75
140
76
141
letmyTrace= Trace("scatter")// create a scatter trace
77
-
myTrace?x <-[0;1;2]// set the x property (the x dimension of the data)
78
-
myTrace?y <-[0;1;2]// set the y property (the y dimension of the data)
142
+
myTrace?x <-[0;3]// set the x property (the x dimension of the data)
143
+
myTrace?y <-[2;4]// set the y property (the y dimension of the data)
GenericChart.ofTraceObject false myTrace // create a generic chart (layout and config are empty objects. When using useDefaults = true, default styling will be applied.)
81
-
|> Chart.show
152
+
(***hide***)
153
+
withDynObj |> GenericChart.toChartHTML
154
+
(***include-it-raw***)
82
155
83
156
(**
84
157
lets have a look at the trace object that will be created. The relevant section of the html generated with Chart.Show is the following:
0 commit comments