Skip to content

Commit e9e279a

Browse files
committed
fix histogram2d docs, extend library introduction
1 parent d011f56 commit e9e279a

2 files changed

Lines changed: 84 additions & 11 deletions

File tree

docs/00_0_basics.fsx

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,30 @@ _This section is WIP._
3535
3636
### Table of contents
3737
38+
- Library design(#Library-design)
3839
- [GenericChart](#GenericChart)
3940
- [Working with GenericCharts](#Working-with-GenericCharts)
4041
- [Dynamic object style](#Dynamic-object-style)
4142
42-
## GenericChart
43-
43+
## Library design
4444
4545
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.
4646
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+
4757
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:
4858
4959
*)
5060

5161
(***do-not-eval***)
52-
[<NoComparison>]
5362
type GenericChart =
5463
| Chart of Trace * Layout * Config * DisplayOptions
5564
| MultiChart of Trace list * Layout * Config * DisplayOptions
@@ -62,23 +71,87 @@ As you can see, a `GenericChart` consists of four top level objects - `Trace` (m
6271
- `Config` is an object that configures high level properties of the chart like making all chart elements editable or the tool bar on top
6372
- `DisplayOptions` is an object that contains meta information about how the html document that contains the chart.
6473
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+
let pointChart =
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***)
6697

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+
let withTraceStyle =
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
68123

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.
70132
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),
71133
an extension of `DynamicObject` which makes it possible to set arbitraryly named and typed properties of these objects via the `?` operator.
72134
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+
73138
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:
74139
*)
75140

76141
let myTrace = 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)
144+
145+
let withDynObj = GenericChart.ofTraceObject true myTrace
146+
147+
(*** condition: ipynb ***)
148+
#if IPYNB
149+
withDynObj
150+
#endif // IPYNB
79151

80-
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***)
82155

83156
(**
84157
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:

docs/04_4_2d-histograms.fsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ A Histogram2D chart can be created using the `Chart.Histogram2D` or `Chart.Histo
6262

6363
let histogramContour =
6464
[
65-
Chart.Histogram2DContour (x,y,Line=Line.init(Width=0.))
65+
Chart.Histogram2DContour (x,y,ContourLine=Line.init(Width=0.))
6666
Chart.Point(x,y,Opacity=0.3)
6767
]
6868
|> Chart.combine

0 commit comments

Comments
 (0)