Skip to content

Commit

Permalink
#42: Add waterfall chart
Browse files Browse the repository at this point in the history
  • Loading branch information
kMutagene committed Jun 16, 2020
1 parent 75c7a32 commit 4d93598
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 1 deletion.
98 changes: 97 additions & 1 deletion src/FSharp.Plotly/Chart.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1999,7 +1999,6 @@ type Chart =
static member Candlestick
(
stockTimeSeries: seq<System.DateTime*StockData>,
[<Optional;DefaultParameterValue(true)>]?ShowRangeSlider: bool,
[<Optional;DefaultParameterValue(null)>]?Increasing : Line,
[<Optional;DefaultParameterValue(null)>]?Decreasing : Line,
[<Optional;DefaultParameterValue(null)>]?WhiskerWidth : float,
Expand All @@ -2020,4 +2019,101 @@ type Chart =
?XCalendar = XCalendar
)
)
|> GenericChart.ofTraceObject


/// Creates a waterfall chart. Waterfall charts are special bar charts that help visualizing the cumulative effect of sequentially introduced positive or negative values
///
/// Parameters:
///
/// x : Sets the x coordinates.
///
/// y : Sets the y coordinates.
///
/// Base : Sets where the bar base is drawn (in position axis units).
///
/// Width : Sets the bar width (in position axis units).
///
/// Measure : An array containing types of values. 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.
///
/// Orientation : Sets the orientation of the bars. With "v" ("h"), the value of the each bar spans along the vertical (horizontal).
///
/// Connector : Sets the styling of the connector lines
///
/// AlignmentGroup : Set several traces linked to the same position axis or matching axes to the same alignmentgroup. This controls whether bars compute their positional range dependently or independently.
///
/// OffsetGroup : Set several traces linked to the same position axis or matching axes to the same offsetgroup where bars of the same position coordinate will line up.
///
/// Offset : Shifts the position where the bar is drawn (in position axis units). In "group" barmode, traces that set "offset" will be excluded and drawn in "overlay" mode instead.
static member Waterfall
(
x : #IConvertible seq,
y : #IConvertible seq,
[<Optional;DefaultParameterValue(null)>]?Base : IConvertible ,
[<Optional;DefaultParameterValue(null)>]?Width : float ,
[<Optional;DefaultParameterValue(null)>]?Measure : StyleParam.WaterfallMeasure seq,
[<Optional;DefaultParameterValue(null)>]?Orientation : StyleParam.Orientation,
[<Optional;DefaultParameterValue(null)>]?Connector : WaterfallConnector ,
[<Optional;DefaultParameterValue(null)>]?AlignmentGroup : string,
[<Optional;DefaultParameterValue(null)>]?OffsetGroup : string,
[<Optional;DefaultParameterValue(null)>]?Offset
) =
Trace.initWaterfall(
TraceStyle.Waterfall(x,y,
?Base = Base ,
?Width = Width ,
?Measure = Measure ,
?Orientation = Orientation ,
?Connector = Connector ,
?AlignmentGroup = AlignmentGroup,
?OffsetGroup = OffsetGroup ,
?Offset = Offset
)
)
|> GenericChart.ofTraceObject


/// Creates a waterfall chart. Waterfall charts are special bar charts that help visualizing the cumulative effect of sequentially introduced positive or negative values
///
/// Parameters:
///
/// xyMeasures : triple sequence containing x coordinates, y coordinates, and the type of measure used for each bar.
///
/// Base : Sets where the bar base is drawn (in position axis units).
///
/// Width : Sets the bar width (in position axis units).
///
/// Orientation : Sets the orientation of the bars. With "v" ("h"), the value of the each bar spans along the vertical (horizontal).
///
/// Connector : Sets the styling of the connector lines
///
/// AlignmentGroup : Set several traces linked to the same position axis or matching axes to the same alignmentgroup. This controls whether bars compute their positional range dependently or independently.
///
/// OffsetGroup : Set several traces linked to the same position axis or matching axes to the same offsetgroup where bars of the same position coordinate will line up.
///
/// Offset : Shifts the position where the bar is drawn (in position axis units). In "group" barmode, traces that set "offset" will be excluded and drawn in "overlay" mode instead.
static member Waterfall
(
xyMeasure: (#IConvertible*#IConvertible*StyleParam.WaterfallMeasure) seq,
[<Optional;DefaultParameterValue(null)>]?Base : IConvertible ,
[<Optional;DefaultParameterValue(null)>]?Width : float ,
[<Optional;DefaultParameterValue(null)>]?Orientation : StyleParam.Orientation,
[<Optional;DefaultParameterValue(null)>]?Connector : WaterfallConnector ,
[<Optional;DefaultParameterValue(null)>]?AlignmentGroup : string,
[<Optional;DefaultParameterValue(null)>]?OffsetGroup : string,
[<Optional;DefaultParameterValue(null)>]?Offset
) =
let x,y,measure = Seq.unzip3 xyMeasure
Trace.initWaterfall(
TraceStyle.Waterfall(x,y,
?Base = Base ,
?Width = Width ,
?Measure = Some measure ,
?Orientation = Orientation ,
?Connector = Connector ,
?AlignmentGroup = AlignmentGroup,
?OffsetGroup = OffsetGroup ,
?Offset = Offset
)
)
|> GenericChart.ofTraceObject
1 change: 1 addition & 0 deletions src/FSharp.Plotly/FSharp.Plotly.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<Compile Include="Dimensions.fs" />
<Compile Include="Domain.fs" />
<Compile Include="Line.fs" />
<Compile Include="WaterfallConnector.fs" />
<Compile Include="Box.fs" />
<Compile Include="Meanline.fs" />
<Compile Include="Marker.fs" />
Expand Down
15 changes: 15 additions & 0 deletions src/FSharp.Plotly/Playground.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#load "Dimensions.fs"
#load "Domain.fs"
#load "Line.fs"
#load "WaterfallConnector.fs"
#load "Box.fs"
#load "Meanline.fs"
#load "Marker.fs"
Expand Down Expand Up @@ -40,6 +41,20 @@
open FSharp.Plotly
open GenericChart


let waterfallData = [
"Sales" , 60 , StyleParam.WaterfallMeasure.Relative
"Consulting" , 80 , StyleParam.WaterfallMeasure.Relative
"Net revenue" , 0 , StyleParam.WaterfallMeasure.Total
"Purchases" , -60 , StyleParam.WaterfallMeasure.Relative
"Other expenses" , -20 , StyleParam.WaterfallMeasure.Relative
"Profit before tax" , 0 , StyleParam.WaterfallMeasure.Total
]

Chart.Waterfall(waterfallData)
|> Chart.Show


let manyPoints =
let rnd = new System.Random()
[for i = 0 to 50000 do (rnd.NextDouble(),rnd.NextDouble()) ]
Expand Down
21 changes: 21 additions & 0 deletions src/FSharp.Plotly/StyleParams.fs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,17 @@ module StyleParam =

static member convert = CategoryOrder.toString >> box

///The shape of connector lines in Waterfall charts.
[<RequireQualifiedAccess>]
type ConnectorMode =
| Spanning | Between

static member toString = function
| Spanning -> "spanning"
| Between -> "between"

static member convert = ConnectorMode.toString >> box

//--------------------------
// #D#
//--------------------------
Expand Down Expand Up @@ -1022,6 +1033,16 @@ module StyleParam =
// #W#
//--------------------------

///How to compute differences between bars in Waterfall Charts
[<RequireQualifiedAccess>]
type WaterfallMeasure =
|Relative | Total
static member toString = function
| Relative -> "relative"
| Total -> "total"

static member convert = WaterfallMeasure.toString >> box

//--------------------------
// #X#
//--------------------------
Expand Down
52 changes: 52 additions & 0 deletions src/FSharp.Plotly/Trace.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1396,4 +1396,56 @@ module Trace =

trace
)

/// Applies the styles of candlestick plot to TraceObjects
///
/// Parameters:
///
/// x : Sets the x coordinates.
///
/// y : Sets the y coordinates.
///
/// Base : Sets where the bar base is drawn (in position axis units).
///
/// Width : Sets the bar width (in position axis units).
///
/// Measure : An array containing types of values. 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.
///
/// Orientation : Sets the orientation of the bars. With "v" ("h"), the value of the each bar spans along the vertical (horizontal).
///
/// Connector : Sets the styling of the connector lines
///
/// AlignmentGroup : Set several traces linked to the same position axis or matching axes to the same alignmentgroup. This controls whether bars compute their positional range dependently or independently.
///
/// OffsetGroup : Set several traces linked to the same position axis or matching axes to the same offsetgroup where bars of the same position coordinate will line up.
///
/// Offset : Shifts the position where the bar is drawn (in position axis units). In "group" barmode, traces that set "offset" will be excluded and drawn in "overlay" mode instead.
static member Waterfall
(
x : #IConvertible seq,
y : #IConvertible seq,
?Base : IConvertible,
?Width : float,
?Measure : StyleParam.WaterfallMeasure seq,
?Orientation : StyleParam.Orientation,
?Connector : WaterfallConnector,
?AlignmentGroup : string,
?OffsetGroup : string,
?Offset

) =
(fun (trace:('T :> Trace)) ->

x |> DynObj.setValue trace "x"
y |> DynObj.setValue trace "y"
Base |> DynObj.setValueOpt trace "base"
Width |> DynObj.setValueOpt trace "width"
Measure |> DynObj.setValueOptBy trace "measure" (Seq.map StyleParam.WaterfallMeasure.convert)
Orientation |> DynObj.setValueOptBy trace "orientation" StyleParam.Orientation.convert
AlignmentGroup |> DynObj.setValueOpt trace "alignmentgroup"
Connector |> DynObj.setValueOpt trace "connector"
OffsetGroup |> DynObj.setValueOpt trace "offsetgroup"
Offset |> DynObj.setValueOpt trace "offset"

trace
)
43 changes: 43 additions & 0 deletions src/FSharp.Plotly/WaterfallConnector.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace FSharp.Plotly

/// Styles for connector lines in Waterfall Charts.
///
/// Parameters:
///
/// Line : Sets the Line style for this WaterfallConnector
///
/// Visible : Wether or not connectors are visible
///
/// ConnectorMode : Sets the shape of connector lines.
type WaterfallConnector () =
inherit DynamicObj ()

static member init
(
?Line : Line,
?Visible : bool,
?ConnectorMode : StyleParam.ConnectorMode
) =

WaterfallConnector()
|> WaterfallConnector.style
(
?Line = Line ,
?Visible = Visible ,
?ConnectorMode = ConnectorMode
)

static member style
(
?Line : Line,
?Visible : bool,
?ConnectorMode : StyleParam.ConnectorMode
) =
(fun (connector:WaterfallConnector) ->

Line |> DynObj.setValueOpt connector "line"
Visible |> DynObj.setValueOpt connector "visible"
ConnectorMode |> DynObj.setValueOptBy connector "mode" StyleParam.ConnectorMode.convert

connector
)

0 comments on commit 4d93598

Please sign in to comment.