Skip to content

Commit 3c6cd67

Browse files
committed
Add Sunburst Chart
1 parent 646c9c5 commit 3c6cd67

File tree

5 files changed

+119
-5
lines changed

5 files changed

+119
-5
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Chart.Grid: Uses the grid object of plotly.js to generate various types of subplot grids
88
* Chart.SingleStack: Basically Chart.Grid with one Column
99
* Add Chart.withColorBar and Chart.withColorBarStyle to change the appearance of colorbars
10-
10+
* [Add Sunburst Chart]()
1111

1212
### 1.2.2 - Apr 9 2020
1313
* [Opening Charts is now more or less OS agnostic](https://github.com/muehlhaus/FSharp.Plotly/commit/f6e3dceade085e43e7e56b478b9cf7b533a4fe55)

src/FSharp.Plotly/Chart.fs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,3 +1267,52 @@ type Chart =
12671267
)
12681268
)
12691269
|> GenericChart.ofTraceObject
1270+
1271+
/// Creates a sunburst chart. Visualize hierarchical data spanning outward radially from root to leaves.
1272+
/// Applies the styles of sundburst plot to TraceObjects
1273+
///
1274+
/// Parameters:
1275+
///
1276+
/// labels: Sets the labels of each of the sectors.
1277+
///
1278+
/// parents: Sets the parent sectors for each of the sectors. Empty string items '' are understood to reference the root node in the hierarchy. If `ids` is filled, `parents` items are understood to be "ids" themselves. When `ids` is not set, plotly attempts to find matching items in `labels`, but beware they must be unique.
1279+
///
1280+
/// Ids: Assigns id labels to each datum. These ids for object constancy of data points during animation.
1281+
///
1282+
/// Values: Sets the values associated with each of the sectors. Use with `branchvalues` to determine how the values are summed.
1283+
///
1284+
/// Text: Sets text elements associated with each sector. If trace `textinfo` contains a "text" flag, these elements will be seen on the chart. If trace `hoverinfo` contains a "text" flag and "hovertext" is not set, these elements will be seen in the hover labels.
1285+
///
1286+
/// Branchvalues: Determines how the items in `values` are summed. When set to "total", items in `values` are taken to be value of all its descendants. When set to "remainder", items in `values` corresponding to the root and the branches sectors are taken to be the extra part not part of the sum of the values at their leaves.
1287+
///
1288+
/// Level: Sets the level from which this trace hierarchy is rendered. Set `level` to `''` to start from the root node in the hierarchy. Must be an "id" if `ids` is filled in, otherwise plotly attempts to find a matching item in `labels`.
1289+
///
1290+
/// Maxdepth: Sets the number of rendered sectors from any given `level`. Set `maxdepth` to "-1" to render all the levels in the hierarchy.
1291+
///
1292+
/// Colorbar: Sets the Colorbar for the chart
1293+
///
1294+
///Colors: Sets the color of each sector of this trace. If not specified, the default trace color set is used to pick the sector colors.
1295+
static member Sunburst(labels,parents,
1296+
[<Optional;DefaultParameterValue(null)>]?Ids,
1297+
[<Optional;DefaultParameterValue(null)>]?Values ,
1298+
[<Optional;DefaultParameterValue(null)>]?Text ,
1299+
[<Optional;DefaultParameterValue(null)>]?Branchvalues ,
1300+
[<Optional;DefaultParameterValue(null)>]?Level ,
1301+
[<Optional;DefaultParameterValue(null)>]?Maxdepth ,
1302+
[<Optional;DefaultParameterValue(null)>]?Colors: seq<string>,
1303+
[<Optional;DefaultParameterValue(null)>]?Colorbar:Colorbar
1304+
) =
1305+
Trace.initSunburst(
1306+
TraceStyle.Sunburst(
1307+
labels = labels,
1308+
parents = parents,
1309+
?Ids = Ids,
1310+
?Values = Values,
1311+
?Text = Text,
1312+
?Branchvalues = Branchvalues,
1313+
?Level = Level,
1314+
?Maxdepth = Maxdepth
1315+
)
1316+
)
1317+
|> TraceStyle.Marker(?Colors=Colors,?Colorbar=Colorbar)
1318+
|> GenericChart.ofTraceObject

src/FSharp.Plotly/Playground.fsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@
3737
open FSharp.Plotly
3838
open GenericChart
3939

40+
//Sunbursst example from plotly docs: https://plotly.com/javascript/sunburst-charts
41+
Chart.Sunburst(
42+
["Eve"; "Cain"; "Seth"; "Enos"; "Noam"; "Abel"; "Awan"; "Enoch"; "Azura"],
43+
[""; "Eve"; "Eve"; "Seth"; "Seth"; "Eve"; "Eve"; "Awan"; "Eve" ],
44+
Values = [10.; 14.; 12.; 10.; 2.; 6.; 6.; 4.; 4.]
45+
)
46+
|> Chart.withTitle "Sunburst test"
47+
|> Chart.Show
48+
4049

4150
let grid ((gCharts:seq<#seq<GenericChart>>),sharedAxes:bool,xGap,yGap) =
4251

src/FSharp.Plotly/StyleParams.fs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ module StyleParam =
133133

134134
static member convert = Barmode.toString >> box
135135

136+
[<RequireQualifiedAccess>]
137+
type BranchValues =
138+
| Remainder
139+
| Total
140+
141+
static member toString = function
142+
| Remainder -> "remainder"
143+
| Total -> "total"
144+
145+
static member convert = BranchValues.toString >> box
146+
136147
//--------------------------
137148
// #C#
138149
//--------------------------

src/FSharp.Plotly/Trace.fs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,12 @@ module Trace =
419419
trace
420420
)
421421

422-
// #################################
423-
// # Charts
424-
422+
//#############################################################################################################################################
423+
//# Chart trace style abstractions
424+
//#############################################################################################################################################
425+
426+
//-------------------------------------------------------------------------------------------------------------------------------------------------
427+
//Simple
425428

426429
// Applies the styles of scatter plot to TraceObjects
427430
static member Scatter
@@ -1165,4 +1168,46 @@ module Trace =
11651168
ColumnOrder |> DynObj.setValueOpt trace "columnorder"
11661169
// out ->
11671170
trace
1168-
)
1171+
)
1172+
1173+
/// Applies the styles of sundburst plot to TraceObjects
1174+
///
1175+
/// Parameters:
1176+
///
1177+
/// labels: Sets the labels of each of the sectors.
1178+
///
1179+
/// parents: Sets the parent sectors for each of the sectors. Empty string items '' are understood to reference the root node in the hierarchy. If `ids` is filled, `parents` items are understood to be "ids" themselves. When `ids` is not set, plotly attempts to find matching items in `labels`, but beware they must be unique.
1180+
///
1181+
/// Ids: Assigns id labels to each datum. These ids for object constancy of data points during animation.
1182+
///
1183+
/// Values: Sets the values associated with each of the sectors. Use with `branchvalues` to determine how the values are summed.
1184+
///
1185+
/// Text: Sets text elements associated with each sector. If trace `textinfo` contains a "text" flag, these elements will be seen on the chart. If trace `hoverinfo` contains a "text" flag and "hovertext" is not set, these elements will be seen in the hover labels.
1186+
///
1187+
/// Branchvalues: Determines how the items in `values` are summed. When set to "total", items in `values` are taken to be value of all its descendants. When set to "remainder", items in `values` corresponding to the root and the branches sectors are taken to be the extra part not part of the sum of the values at their leaves.
1188+
///
1189+
/// Level: Sets the level from which this trace hierarchy is rendered. Set `level` to `''` to start from the root node in the hierarchy. Must be an "id" if `ids` is filled in, otherwise plotly attempts to find a matching item in `labels`.
1190+
///
1191+
/// Maxdepth: Sets the number of rendered sectors from any given `level`. Set `maxdepth` to "-1" to render all the levels in the hierarchy.
1192+
static member Sunburst
1193+
(
1194+
labels : seq<#IConvertible>,
1195+
parents : seq<#IConvertible>,
1196+
?Ids : seq<string>,
1197+
?Values : seq<float>,
1198+
?Text : seq<string>,
1199+
?Branchvalues : StyleParam.BranchValues,
1200+
?Level ,
1201+
?Maxdepth : int
1202+
) =
1203+
(fun (trace:('T :> Trace)) ->
1204+
labels |> DynObj.setValue trace "labels"
1205+
parents |> DynObj.setValue trace "parents"
1206+
Ids |> DynObj.setValueOpt trace "ids"
1207+
Values |> DynObj.setValueOpt trace "values"
1208+
Text |> DynObj.setValueOpt trace "text"
1209+
Branchvalues |> DynObj.setValueOptBy trace "branchvalues" StyleParam.BranchValues.convert
1210+
Level |> DynObj.setValueOpt trace "level"
1211+
Maxdepth |> DynObj.setValueOpt trace "maxdepth"
1212+
trace
1213+
)

0 commit comments

Comments
 (0)