Skip to content

Commit acb3b9c

Browse files
committed
Add Chart.withLayoutImage(s), adapt Chart.withShape(s)
1 parent c854600 commit acb3b9c

3 files changed

Lines changed: 123 additions & 28 deletions

File tree

src/Plotly.NET/CSharpLayer/GenericChartExtensions.fs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -493,12 +493,15 @@ module GenericChartExtensions =
493493
member this.WithConfig (config:Config) =
494494
GenericChart.setConfig config this
495495

496+
[<CompiledName("WithAnnotation")>]
497+
[<Extension>]
498+
member this.WithAnnotation(annotation:Annotation, [<Optional;DefaultParameterValue(true)>]?Append:bool) =
499+
this |> Chart.withAnnotation(annotation, ?Append = Append)
500+
496501
[<CompiledName("WithAnnotations")>]
497502
[<Extension>]
498-
member this.WithAnnotations(annotations:seq<Annotation>) =
499-
this
500-
|> GenericChart.mapLayout
501-
(Layout.style (Annotations = annotations))
503+
member this.WithAnnotations(annotations:Annotation seq, [<Optional;DefaultParameterValue(true)>]?Append:bool) =
504+
this |> Chart.withAnnotations(annotations, ?Append = Append)
502505

503506
// Set the title of a Chart
504507
[<CompiledName("WithTitle")>]
@@ -574,19 +577,13 @@ module GenericChartExtensions =
574577
//(`x0`,`y0`), (`x1`,`y0`), (`x1`,`y1`), (`x0`,`y1`), (`x0`,`y0`)
575578
[<CompiledName("WithShape")>]
576579
[<Extension>]
577-
member this.WithShape(shape:Shape) =
578-
let layout =
579-
GenericChart.getLayout this
580-
|> Layout.style (Shapes=[shape])
581-
GenericChart.setLayout layout this
580+
member this.WithShape(shape:Shape, [<Optional;DefaultParameterValue(true)>]?Append:bool) =
581+
this |> Chart.withShape(shape, ?Append = Append)
582582

583583
[<CompiledName("WithShapes")>]
584584
[<Extension>]
585-
member this.WithShapes(shapes:Shape seq) =
586-
let layout =
587-
GenericChart.getLayout this
588-
|> Layout.style (Shapes=shapes)
589-
GenericChart.setLayout layout this
585+
member this.WithShapes(shapes:Shape seq, [<Optional;DefaultParameterValue(true)>]?Append:bool) =
586+
this |> Chart.withShapes(shapes, ?Append = Append)
590587

591588
// ############################################################
592589
// ####################### Apply to DisplayOptions
@@ -664,3 +661,13 @@ module GenericChartExtensions =
664661
member this.WithTernary(ternary:Ternary, [<Optional;DefaultParameterValue(null)>] ?Id) =
665662
this |> Chart.withTernary(ternary,?Id=Id)
666663

664+
665+
[<CompiledName("WithLayoutImage")>]
666+
[<Extension>]
667+
member this.WithLayoutImage(image:LayoutImage, [<Optional;DefaultParameterValue(true)>]?Append:bool) =
668+
this |> Chart.withLayoutImage(image, ?Append = Append)
669+
670+
[<CompiledName("WithLayoutImages")>]
671+
[<Extension>]
672+
member this.WithLayoutImages(images:seq<LayoutImage>, [<Optional;DefaultParameterValue(true)>]?Append:bool) =
673+
this |> Chart.withLayoutImages(images, ?Append = Append)

src/Plotly.NET/ChartAPI/Chart.fs

Lines changed: 84 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,14 @@ type Chart =
823823
|> GenericChart.mapLayout
824824
(Layout.style (Annotations = annotations'))
825825

826+
[<CompiledName("WithAnnotation")>]
827+
static member withAnnotation
828+
(
829+
annotation: Annotation,
830+
[<Optional;DefaultParameterValue(true)>] ?Append: bool
831+
) =
832+
Chart.withAnnotations([annotation], ?Append = Append)
833+
826834
// Set the title of a Chart
827835
[<CompiledName("WithTitle")>]
828836
static member withTitle(title,[<Optional;DefaultParameterValue(null)>] ?TitleFont) =
@@ -911,23 +919,45 @@ type Chart =
911919
//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
912920
//((`x0`+`x1`)/2, (`y0`+`y1`)/2)) with radius (|(`x0`+`x1`)/2 - `x0`|, |(`y0`+`y1`)/2 -`y0`)|) If "rect", a rectangle is drawn linking
913921
//(`x0`,`y0`), (`x1`,`y0`), (`x1`,`y1`), (`x0`,`y1`), (`x0`,`y0`)
914-
[<CompiledName("WithShape")>]
915-
static member withShape(shape:Shape) =
916-
(fun (ch:GenericChart) ->
917-
let layout =
918-
GenericChart.getLayout ch
919-
|> Layout.style (Shapes=[shape])
920-
GenericChart.setLayout layout ch)
922+
923+
/// <summary>
924+
///
925+
/// </summary>
926+
/// <param name="shapes">The shapes to add to the input charts layout</param>
927+
/// <param name="Append">If true, the input annotations will be appended to existing annotations, otherwise existing annotations will be removed (default: true)</param>
928+
[<CompiledName("WithShapes")>]
929+
static member withShapes
930+
(
931+
shapes:seq<Shape>,
932+
[<Optional;DefaultParameterValue(true)>] ?Append: bool
933+
) =
934+
let append = defaultArg Append true
921935

936+
fun (ch:GenericChart) ->
937+
938+
let shapes' =
922939

923-
[<CompiledName("WithShapes")>]
924-
static member withShapes(shapes:Shape seq) =
925-
(fun (ch:GenericChart) ->
926-
let layout =
927-
GenericChart.getLayout ch
928-
|> Layout.style (Shapes=shapes)
929-
GenericChart.setLayout layout ch)
940+
if append then
941+
942+
let layout = GenericChart.getLayout ch
943+
944+
layout.TryGetTypedValue<seq<Shape>>("shapes")
945+
|> Option.defaultValue Seq.empty
946+
|> Seq.append shapes
947+
948+
else shapes
949+
950+
ch
951+
|> GenericChart.mapLayout
952+
(Layout.style (Shapes = shapes'))
930953

954+
[<CompiledName("WithShape")>]
955+
static member withShape
956+
(
957+
shape: Shape,
958+
[<Optional;DefaultParameterValue(true)>] ?Append: bool
959+
) =
960+
Chart.withShapes([shape], ?Append = Append)
931961

932962
// #######################
933963
/// Create a combined chart with the given charts merged
@@ -1491,3 +1521,43 @@ type Chart =
14911521

14921522
GenericChart.setLayout updatedLayout ch
14931523
)
1524+
1525+
/// <summary>
1526+
///
1527+
/// </summary>
1528+
/// <param name="images">The images to add to the input charts layout</param>
1529+
/// <param name="Append">If true, the input images will be appended to existing annotations, otherwise existing annotations will be removed (default: true)</param>
1530+
[<CompiledName("WithImages")>]
1531+
static member withLayoutImages
1532+
(
1533+
images:seq<LayoutImage>,
1534+
[<Optional;DefaultParameterValue(true)>] ?Append: bool
1535+
) =
1536+
let append = defaultArg Append true
1537+
1538+
fun (ch:GenericChart) ->
1539+
1540+
let images' =
1541+
1542+
if append then
1543+
1544+
let layout = GenericChart.getLayout ch
1545+
1546+
layout.TryGetTypedValue<seq<LayoutImage>>("images")
1547+
|> Option.defaultValue Seq.empty
1548+
|> Seq.append images
1549+
1550+
else images
1551+
1552+
ch
1553+
|> GenericChart.mapLayout
1554+
(Layout.style (Images = images'))
1555+
1556+
[<CompiledName("WithLayoutImage")>]
1557+
static member withLayoutImage
1558+
(
1559+
image: LayoutImage,
1560+
[<Optional;DefaultParameterValue(true)>] ?Append: bool
1561+
) =
1562+
1563+
Chart.withLayoutImages([image], ?Append = Append)

src/Plotly.NET/Playground.fsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#I "Layout/ObjectAbstractions/Common"
1818

19+
#load "LayoutImage.fs"
1920
#load "Button.fs"
2021
#load "RangeSelector.fs"
2122
#load "RangeSlider.fs"
@@ -153,6 +154,23 @@ open FSharpAux
153154
open System
154155
open System.IO
155156

157+
Chart.Line([0.; 0.5; 1.; 2.; 2.2], y=[1.23; 2.5; 0.42; 3.; 1.])
158+
|> Chart.withLayoutImage(
159+
LayoutImage.init(
160+
Source="https://fsharp.org/img/logo/fsharp.svg",
161+
XRef="x",
162+
YRef="y",
163+
X=0,
164+
Y=3,
165+
SizeX=2,
166+
SizeY=2,
167+
Sizing=StyleParam.LayoutImageSizing.Stretch,
168+
Opacity=0.5,
169+
Layer=StyleParam.Layer.Below
170+
)
171+
)
172+
|> Chart.show
173+
156174
let imagebase64 =
157175
System.Convert.ToBase64String(File.ReadAllBytes(@"C:\Users\schne\Pictures\Untitled.jpg"))
158176

0 commit comments

Comments
 (0)