Skip to content

Commit 6b32a64

Browse files
committed
#259: Fix mutating global defaults on individual chart styling
1 parent 63210a1 commit 6b32a64

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

src/Plotly.NET/ChartAPI/GenericChart.fs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -499,31 +499,52 @@ module GenericChart =
499499
/// Converts from a trace object and a layout object into GenericChart. If useDefaults = true, also sets the default Chart properties found in `Defaults`
500500
let ofTraceObject (useDefaults: bool) trace = //layout =
501501
if useDefaults then
502+
// copy default instances so we can safely manipulate the respective objects of the created chart without changing global default objects
503+
let defaultConfig = Config()
504+
Defaults.DefaultConfig.CopyDynamicPropertiesTo defaultConfig
505+
506+
let defaultDisplayOpts = DisplayOptions()
507+
Defaults.DefaultDisplayOptions.CopyDynamicPropertiesTo defaultDisplayOpts
508+
509+
let defaultTemplate = Template()
510+
Defaults.DefaultTemplate.CopyDynamicPropertiesTo defaultTemplate
511+
502512
GenericChart.Chart(
503513
trace,
504514
Layout.init (
505-
Width = Defaults.DefaultWidth,
506-
Height = Defaults.DefaultHeight,
507-
Template = (Defaults.DefaultTemplate :> DynamicObj)
515+
Width = Defaults.DefaultWidth, // no need to copy these, as they are primitives
516+
Height = Defaults.DefaultHeight, // no need to copy these, as they are primitives
517+
Template = (defaultTemplate :> DynamicObj)
508518
),
509-
Defaults.DefaultConfig,
510-
Defaults.DefaultDisplayOptions
519+
defaultConfig,
520+
defaultDisplayOpts
511521
)
512522
else
513523
GenericChart.Chart(trace, Layout(), Config(), DisplayOptions())
514524

515525
/// Converts from a list of trace objects and a layout object into GenericChart. If useDefaults = true, also sets the default Chart properties found in `Defaults`
516526
let ofTraceObjects (useDefaults: bool) traces = // layout =
517527
if useDefaults then
528+
// copy default instances so we can safely manipulate the respective objects of the created chart without changing global default objects
529+
let defaultConfig = Config()
530+
Defaults.DefaultConfig.CopyDynamicPropertiesTo defaultConfig
531+
532+
let defaultDisplayOpts = DisplayOptions()
533+
Defaults.DefaultDisplayOptions.CopyDynamicPropertiesTo defaultDisplayOpts
534+
535+
let defaultTemplate = Template()
536+
Defaults.DefaultTemplate.CopyDynamicPropertiesTo defaultTemplate
537+
518538
GenericChart.MultiChart(
519539
traces,
520540
Layout.init (
521541
Width = Defaults.DefaultWidth,
522542
Height = Defaults.DefaultHeight,
523-
Template = (Defaults.DefaultTemplate :> DynamicObj)
543+
Template = (defaultTemplate :> DynamicObj)
524544
),
525-
Defaults.DefaultConfig,
526-
Defaults.DefaultDisplayOptions
545+
defaultConfig,
546+
defaultDisplayOpts
547+
527548
)
528549
else
529550
GenericChart.MultiChart(traces, Layout(), Config(), DisplayOptions())

0 commit comments

Comments
 (0)