Skip to content

Commit 81de312

Browse files
committed
Add waterfall docs
1 parent 3fd898e commit 81de312

File tree

5 files changed

+122
-13
lines changed

5 files changed

+122
-13
lines changed

Plotly.NET.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{7B09CC0A-F
8686
docs\07_1_funnel.fsx = docs\07_1_funnel.fsx
8787
docs\07_2_funnel_area.fsx = docs\07_2_funnel_area.fsx
8888
docs\07_3_indicator.fsx = docs\07_3_indicator.fsx
89+
docs\07_4_waterfall.fsx = docs\07_4_waterfall.fsx
8990
docs\08_0_polar_line-scatter-plots.fsx = docs\08_0_polar_line-scatter-plots.fsx
9091
docs\08_1_polar_bar_charts.fsx = docs\08_1_polar_bar_charts.fsx
9192
docs\08_2_styling_polar_layouts.fsx = docs\08_2_styling_polar_layouts.fsx

docs/07_4_waterfall.fsx

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
(**
2+
---
3+
title: Waterfall Charts
4+
category: Finance Charts
5+
categoryindex: 7
6+
index: 5
7+
---
8+
*)
9+
10+
(*** hide ***)
11+
12+
(*** condition: prepare ***)
13+
#r "nuget: Newtonsoft.JSON, 13.0.1"
14+
#r "nuget: DynamicObj"
15+
#r "../bin/Plotly.NET/net5.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+
# Waterfall Charts
25+
26+
[![Binder]({{root}}img/badge-binder.svg)](https://mybinder.org/v2/gh/plotly/Plotly.NET/gh-pages?filepath={{fsdocs-source-basename}}.ipynb) 
27+
[![Script]({{root}}img/badge-script.svg)]({{root}}{{fsdocs-source-basename}}.fsx) 
28+
[![Notebook]({{root}}img/badge-notebook.svg)]({{root}}{{fsdocs-source-basename}}.ipynb)
29+
30+
*Summary:* This example shows how to create waterfall charts in F#.
31+
32+
Waterfall charts are special bar charts that help visualizing the cumulative effect of sequentially introduced positive or negative values.
33+
34+
In addition to the x and y values, a `WaterfallMeasure` can be passed corresponding to each xy pair (there is also a constructor overload using a tripel of x,y,measure).
35+
By default the values are considered as 'relative'. However; it is possible to use 'total' to compute the sums. Also 'absolute' could be applied to reset the computed total or to declare an initial value where needed.
36+
37+
*)
38+
39+
open Plotly.NET
40+
open Plotly.NET.TraceObjects
41+
open Plotly.NET.LayoutObjects
42+
43+
let waterfall1 =
44+
Chart.Waterfall(
45+
x = ["Sales"; "Consulting"; "Net revenue"; "Purchases"; "Other expenses"; "Profit before tax"],
46+
y = [60; 80; 0; -40; -20; 0],
47+
Measure = [
48+
StyleParam.WaterfallMeasure.Relative
49+
StyleParam.WaterfallMeasure.Relative
50+
StyleParam.WaterfallMeasure.Total
51+
StyleParam.WaterfallMeasure.Relative
52+
StyleParam.WaterfallMeasure.Relative
53+
StyleParam.WaterfallMeasure.Total
54+
]
55+
)
56+
57+
(*** condition: ipynb ***)
58+
#if IPYNB
59+
waterfall1
60+
#endif // IPYNB
61+
62+
(***hide***)
63+
waterfall1 |> GenericChart.toChartHTML
64+
(***include-it-raw***)
65+
66+
(**
67+
## Horizontal waterfall charts
68+
69+
Set the orientation argument to `Horizontal` to create a horizontal waterfall. Keep in mind to correctly assign x and y values (the vallues are switched on the axes in comparison to the chart example above)
70+
To keep better track of which measure belongs to which datum, use
71+
*)
72+
73+
let waterfall2 =
74+
Chart.Waterfall(
75+
xymeasures = [
76+
60 , "Sales" , StyleParam.WaterfallMeasure.Relative
77+
80 , "Consulting" , StyleParam.WaterfallMeasure.Relative
78+
0 , "Net revenue" , StyleParam.WaterfallMeasure.Total
79+
-40, "Purchases" , StyleParam.WaterfallMeasure.Relative
80+
-20, "Other expenses" , StyleParam.WaterfallMeasure.Relative
81+
0 , "Profit before tax" , StyleParam.WaterfallMeasure.Total
82+
],
83+
Orientation = StyleParam.Orientation.Horizontal
84+
)
85+
86+
(*** condition: ipynb ***)
87+
#if IPYNB
88+
waterfall2
89+
#endif // IPYNB
90+
91+
(***hide***)
92+
waterfall2 |> GenericChart.toChartHTML
93+
(***include-it-raw***)

src/Plotly.NET/ChartAPI/Chart2D.fs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,11 +1072,11 @@ module Chart2D =
10721072
[<Optional;DefaultParameterValue(null)>] ?TextPosition : StyleParam.TextPosition,
10731073
[<Optional;DefaultParameterValue(null)>] ?MultiTextPosition : seq<StyleParam.TextPosition>,
10741074
[<Optional;DefaultParameterValue(null)>] ?TextFont : Font,
1075-
[<Optional;DefaultParameterValue(null)>] ?Marker : Marker,
10761075
[<Optional;DefaultParameterValue(null)>] ?Connector : WaterfallConnector,
10771076
[<Optional;DefaultParameterValue(null)>] ?Measure : StyleParam.WaterfallMeasure seq,
10781077
[<Optional;DefaultParameterValue(null)>] ?AlignmentGroup : string,
10791078
[<Optional;DefaultParameterValue(null)>] ?OffsetGroup : string,
1079+
[<Optional;DefaultParameterValue(true)>] ?Orientation : StyleParam.Orientation,
10801080
[<Optional;DefaultParameterValue(true)>] ?UseDefaults : bool
10811081
) =
10821082

@@ -1107,7 +1107,8 @@ module Chart2D =
11071107
?Connector = Connector ,
11081108
?Measure = Measure ,
11091109
?AlignmentGroup = AlignmentGroup ,
1110-
?OffsetGroup = OffsetGroup
1110+
?OffsetGroup = OffsetGroup ,
1111+
?Orientation = Orientation
11111112
)
11121113
)
11131114
|> GenericChart.ofTraceObject useDefaults
@@ -1135,11 +1136,11 @@ module Chart2D =
11351136
[<Optional;DefaultParameterValue(null)>] ?TextPosition : StyleParam.TextPosition,
11361137
[<Optional;DefaultParameterValue(null)>] ?MultiTextPosition : seq<StyleParam.TextPosition>,
11371138
[<Optional;DefaultParameterValue(null)>] ?TextFont : Font,
1138-
[<Optional;DefaultParameterValue(null)>] ?Marker : Marker,
11391139
[<Optional;DefaultParameterValue(null)>] ?Connector : WaterfallConnector,
11401140
[<Optional;DefaultParameterValue(null)>] ?Measure : StyleParam.WaterfallMeasure seq,
11411141
[<Optional;DefaultParameterValue(null)>] ?AlignmentGroup : string,
11421142
[<Optional;DefaultParameterValue(null)>] ?OffsetGroup : string,
1143+
[<Optional;DefaultParameterValue(true)>] ?Orientation : StyleParam.Orientation,
11431144
[<Optional;DefaultParameterValue(true)>] ?UseDefaults : bool
11441145
) =
11451146

@@ -1164,11 +1165,11 @@ module Chart2D =
11641165
?TextPosition = TextPosition ,
11651166
?MultiTextPosition= MultiTextPosition,
11661167
?TextFont = TextFont ,
1167-
?Marker = Marker ,
11681168
?Connector = Connector ,
11691169
?Measure = Measure ,
11701170
?AlignmentGroup = AlignmentGroup ,
11711171
?OffsetGroup = OffsetGroup ,
1172+
?Orientation = Orientation ,
11721173
?UseDefaults = UseDefaults
11731174
)
11741175

@@ -1194,10 +1195,10 @@ module Chart2D =
11941195
[<Optional;DefaultParameterValue(null)>] ?TextPosition : StyleParam.TextPosition,
11951196
[<Optional;DefaultParameterValue(null)>] ?MultiTextPosition : seq<StyleParam.TextPosition>,
11961197
[<Optional;DefaultParameterValue(null)>] ?TextFont : Font,
1197-
[<Optional;DefaultParameterValue(null)>] ?Marker : Marker,
11981198
[<Optional;DefaultParameterValue(null)>] ?Connector : WaterfallConnector,
11991199
[<Optional;DefaultParameterValue(null)>] ?AlignmentGroup : string,
12001200
[<Optional;DefaultParameterValue(null)>] ?OffsetGroup : string,
1201+
[<Optional;DefaultParameterValue(true)>] ?Orientation : StyleParam.Orientation,
12011202
[<Optional;DefaultParameterValue(true)>] ?UseDefaults : bool
12021203
) =
12031204

@@ -1222,11 +1223,11 @@ module Chart2D =
12221223
?TextPosition = TextPosition ,
12231224
?MultiTextPosition= MultiTextPosition,
12241225
?TextFont = TextFont ,
1225-
?Marker = Marker ,
12261226
?Connector = Connector ,
12271227
Measure = measure ,
12281228
?AlignmentGroup = AlignmentGroup ,
12291229
?OffsetGroup = OffsetGroup ,
1230+
?Orientation = Orientation ,
12301231
?UseDefaults = UseDefaults
12311232
)
12321233

src/Plotly.NET/CommonAbstractions/StyleParams.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2740,10 +2740,11 @@ module StyleParam =
27402740
///How to compute differences between bars in Waterfall Charts
27412741
[<RequireQualifiedAccess>]
27422742
type WaterfallMeasure =
2743-
|Relative | Total
2743+
| Relative | Total | Absolute
27442744
static member toString = function
27452745
| Relative -> "relative"
27462746
| Total -> "total"
2747+
| Absolute -> "absolute"
27472748

27482749
static member convert = WaterfallMeasure.toString >> box
27492750
override this.ToString() = this |> WaterfallMeasure.toString

src/Plotly.NET/Playground.fsx

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,19 @@ Chart.Waterfall(
185185
)
186186
|> Chart.show
187187

188+
Chart.Waterfall(
189+
xymeasures = [
190+
60 , "Sales" , StyleParam.WaterfallMeasure.Relative
191+
80 , "Consulting" , StyleParam.WaterfallMeasure.Relative
192+
0 , "Net revenue" , StyleParam.WaterfallMeasure.Total
193+
-40, "Purchases" , StyleParam.WaterfallMeasure.Relative
194+
-20, "Other expenses" , StyleParam.WaterfallMeasure.Relative
195+
0 , "Profit before tax" , StyleParam.WaterfallMeasure.Total
196+
],
197+
Orientation = StyleParam.Orientation.Horizontal
198+
)
199+
|> Chart.show
200+
188201
Chart.Line([1,2; 3,4])
189202
|> Chart.show
190203

@@ -371,11 +384,11 @@ Chart.Icicle(
371384
|> Chart.combine
372385
|> Chart.show
373386

374-
let a = [4.; 4.; 4.; 4.5; 4.5; 4.5; 5.; 5.; 5.; 6.; 6.; 6.]
375-
let b = [1.; 2.; 3.; 1.; 2.; 3.; 1.; 2.; 3.; 1.; 2.; 3.]
376-
let y = [2.; 3.5; 4.; 3.; 4.5; 5.; 5.5; 6.5; 7.5; 8.; 8.5; 10.]
377387

378388
let carpets =
389+
let a = [4.; 4.; 4.; 4.5; 4.5; 4.5; 5.; 5.; 5.; 6.; 6.; 6.]
390+
let b = [1.; 2.; 3.; 1.; 2.; 3.; 1.; 2.; 3.; 1.; 2.; 3.]
391+
let y = [2.; 3.5; 4.; 3.; 4.5; 5.; 5.5; 6.5; 7.5; 8.; 8.5; 10.]
379392
[
380393
Chart.Carpet("carpet1",A = a, B = b, Y = y)
381394
Chart.Carpet("carpet2",A = (a |> List.rev) , B = (b |> List.rev), Y = (y |> List.map (fun x -> x + 10.)))
@@ -384,11 +397,11 @@ let carpets =
384397
Chart.Carpet("carpet5",A = a, B = b, Y = (y |> List.map (fun x -> x + 40.)))
385398
]
386399

387-
let aData = [4.; 5.; 5.; 6.]
388-
let bData = [1.; 1.; 2.; 3.]
389-
let sizes = [5; 10; 15; 20]
390400

391401
let carpetCharts =
402+
let aData = [4.; 5.; 5.; 6.]
403+
let bData = [1.; 1.; 2.; 3.]
404+
let sizes = [5; 10; 15; 20]
392405
[
393406
Chart.ScatterCarpet(
394407
aData,bData,

0 commit comments

Comments
 (0)