Skip to content

Commit 62f2976

Browse files
committed
Add Chart Templates
1 parent 8024272 commit 62f2976

6 files changed

Lines changed: 157 additions & 7 deletions

File tree

‎src/Plotly.NET/ChartExtensions.fs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,15 @@ module ChartExtensions =
661661
Margin.init ( ?Left=Left,?Right=Right,?Top=Top,?Bottom=Bottom,?Pad=Pad,?Autoexpand=Autoexpand )
662662
Chart.withMargin(margin)
663663

664+
[<CompiledName("WithTemplate")>]
665+
static member withTemplate(template: Template) =
666+
(fun (ch:GenericChart) ->
667+
ch
668+
|> GenericChart.mapLayout (fun l ->
669+
template |> DynObj.setValue l "template"
670+
l
671+
)
672+
)
664673

665674
// TODO: Include withLegend & withLegendStyle
666675

‎src/Plotly.NET/GenericChartExtensions.fs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,15 @@ module GenericChartExtensions =
592592
Margin.init ( ?Left=Left,?Right=Right,?Top=Top,?Bottom=Bottom,?Pad=Pad,?Autoexpand=Autoexpand )
593593
this |> Chart.withMargin(margin)
594594

595+
[<CompiledName("WithTemplate")>]
596+
[<Extension>]
597+
member this.WithTemplate(template: Template) =
598+
this
599+
|> GenericChart.mapLayout (fun l ->
600+
template |> DynObj.setValue l "template"
601+
l
602+
)
603+
595604
// TODO: Include withLegend & withLegendStyle
596605

597606
//Specifies the shape type to be drawn. If "line", a line is drawn from (`x0`,`y0`) to (`x1`,`y1`) If "circle", a circle is drawn from

‎src/Plotly.NET/Playground.fsx‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#load "StyleParams.fs"
22
#load "DynamicObj.fs"
3+
#load "Frame.fs"
34
#load "Colors.fs"
45
#load "StockData.fs"
56
#load "Font.fs"
@@ -31,6 +32,7 @@
3132
#load "Geo.fs"
3233
#load "LayoutGrid.fs"
3334
#load "Layout.fs"
35+
#load "Template.fs"
3436
#load "Config.fs"
3537
#r @"..\..\packages\Newtonsoft.Json\lib\netstandard2.0\Newtonsoft.Json.dll"
3638
#load "GenericChart.fs"
@@ -39,18 +41,30 @@
3941
#load "GenericChartExtensions.fs"
4042
#load "CandelstickExtension.fs"
4143
#load "SankeyExtension.fs"
42-
#load "Templates.fs"
4344

4445
open Plotly.NET
4546
open GenericChart
4647

48+
let myTemplate =
49+
ChartTemplates.dark
50+
|> Template.withColorWay ChartTemplates.ColorWays.plotly
51+
4752
//F# functional pipeline to compose a chart with functions
4853
//
49-
[(1,5);(2,10)]
50-
|> Chart.Point
54+
[
55+
[(1,5);(2,10)]
56+
[(2,4);(3,9)]
57+
[(3,3);(4,8)]
58+
[(4,2);(5,7)]
59+
[(5,1);(6,6)]
60+
61+
]
62+
|> List.map Chart.Line
63+
|> Chart.Combine
5164
|> Chart.withTraceName("Hello from F#",Showlegend=true)
52-
|> Chart.withY_AxisStyle("xAxis",Showgrid= false, Showline=true)
53-
|> Chart.withX_AxisStyle("yAxis",Showgrid= false, Showline=true)
65+
//|> Chart.withY_AxisStyle("xAxis",Showline=true)
66+
//|> Chart.withX_AxisStyle("yAxis",Showline=true)
67+
|> Chart.withTemplate myTemplate
5468
|> Chart.Show
5569

5670

‎src/Plotly.NET/Plotly.NET.fsproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@
5252
<Compile Include="Trace3d.fs" />
5353
<Compile Include="LayoutGrid.fs" />
5454
<Compile Include="Layout.fs" />
55+
<Compile Include="Template.fs" />
5556
<Compile Include="Config.fs" />
5657
<Compile Include="GenericChart.fs" />
5758
<Compile Include="Chart.fs" />
5859
<Compile Include="ChartExtensions.fs" />
5960
<Compile Include="GenericChartExtensions.fs" />
6061
<Compile Include="CandelstickExtension.fs" />
6162
<Compile Include="SankeyExtension.fs" />
62-
<Compile Include="Templates.fs" />
6363
<None Include="Playground.fsx" />
6464
<None Include="TestScript.fsx" />
6565
<None Include="paket.references" />

‎src/Plotly.NET/Template.fs‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
namespace Plotly.NET
2+
3+
/// Margin
4+
type Template() =
5+
inherit DynamicObj ()
6+
7+
/// Init Margin type
8+
static member init
9+
(
10+
layoutTemplate: Layout ,
11+
?TraceTemplates: Trace []
12+
) =
13+
Template()
14+
|> Template.style
15+
(
16+
layoutTemplate,
17+
?TraceTemplates = TraceTemplates
18+
)
19+
20+
static member style
21+
(
22+
layoutTemplate: Layout ,
23+
?TraceTemplates: Trace []
24+
) =
25+
(fun (template:Template) ->
26+
layoutTemplate |> DynObj.setValue template "layout"
27+
TraceTemplates |> DynObj.setValueOpt template "data"
28+
29+
template
30+
)
31+
32+
/// Applies the styles to Margin()
33+
static member mapLayoutTemplate (styleF: Layout -> Layout) (template:Template) =
34+
template.TryGetTypedValue<Layout>("layout")
35+
|> Option.map (styleF)
36+
|> DynObj.setValueOpt template "layout"
37+
template
38+
39+
/// Applies the styles to Margin()
40+
static member mapTraceTemplates (styleF: Trace [] -> Trace []) (template:Template) =
41+
template.TryGetTypedValue<Trace []>("data")
42+
|> Option.map (styleF)
43+
|> DynObj.setValueOpt template "data"
44+
template
45+
46+
static member withColorWay (colorway:string []) (template:Template) =
47+
template
48+
|> Template.mapLayoutTemplate (fun l ->
49+
colorway |> DynObj.setValue l "colorway"
50+
l
51+
)
52+
53+
module ChartTemplates =
54+
55+
module ColorWays =
56+
57+
let plotly = [|
58+
"#636efa"
59+
"#EF553B"
60+
"#00cc96"
61+
"#ab63fa"
62+
"#FFA15A"
63+
"#19d3f3"
64+
"#FF6692"
65+
"#B6E880"
66+
"#FF97FF"
67+
"#FECB52"
68+
|]
69+
70+
let light =
71+
72+
let defaultFont = Font.init (Size = 40)
73+
74+
let defaultLayout =
75+
Layout.init (
76+
Paper_bgcolor = "white",
77+
Plot_bgcolor= "white",
78+
Font = defaultFont
79+
)
80+
81+
82+
Template.init(defaultLayout, TraceTemplates = [||])
83+
84+
let dark =
85+
86+
let initDarkAxisTemplate() =
87+
Axis.LinearAxis.init(
88+
Linecolor = "rgb(204, 204, 204)",
89+
Zerolinecolor = "rgb(204, 204, 204)",
90+
Gridcolor = "rgba(204, 204, 204, 0.3)",
91+
Tickcolor = "rgba(204, 204, 204, 0.5)",
92+
Showline= true,
93+
Zeroline=true
94+
)
95+
96+
let darkLayoutTemplate =
97+
Layout.init(
98+
Paper_bgcolor = "rgb(55, 55, 61)",
99+
Plot_bgcolor= "rgb(55, 55, 61)",
100+
Font = Font.init(Color = "rgb(204, 204, 204)")
101+
)
102+
|> Layout.AddLinearAxis((StyleParam.AxisId.X 1),(initDarkAxisTemplate()))
103+
|> Layout.AddLinearAxis((StyleParam.AxisId.Y 1),(initDarkAxisTemplate()))
104+
105+
Template.init(darkLayoutTemplate)
106+
107+
let darkMirrored =
108+
dark
109+
|> Template.mapLayoutTemplate (fun l ->
110+
l.TryGetTypedValue<Axis.LinearAxis>("xaxis")
111+
|> Option.map (Axis.LinearAxis.style (Mirror=StyleParam.Mirror.AllTicks))
112+
|> DynObj.setValueOpt l "xaxis"
113+
114+
l.TryGetTypedValue<Axis.LinearAxis>("yaxis")
115+
|> Option.map (Axis.LinearAxis.style (Mirror=StyleParam.Mirror.AllTicks))
116+
|> DynObj.setValueOpt l "yaxis"
117+
118+
l
119+
)

‎src/Plotly.NET/Templates.fs‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)
Sponsor
SponsoredKunjungi sekarang
Promo