Skip to content

Commit d5ce2bb

Browse files
muehlmuehl
authored andcommitted
Add autolabels
1 parent e14df8d commit d5ce2bb

File tree

4 files changed

+123
-28
lines changed

4 files changed

+123
-28
lines changed

docs/content/line-scatter-plots.fsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ open FSharp.Plotly
1818

1919
let x = [1.; 2.; 3.; 4.; 5.; 6.; 7.; 8.; 9.; 10.; ]
2020
let y' = [2.; 1.5; 5.; 1.5; 3.; 2.5; 2.5; 1.5; 3.5; 1.]
21-
21+
22+
2223
(*** define-output:line1 ***)
2324
Chart.Line(x,y',Name="line")
2425
|> Chart.withLineStyle(Width=2,Dash=StyleOption.DrawingStyle.Dot)
@@ -37,7 +38,6 @@ shown below.
3738
[ for x in 1.0 .. 100.0 -> (x, x ** 2.0) ]
3839
|> Chart.Line
3940
(*** include-it:line2 ***)
40-
|> Chart.Show
4141

4242

4343

@@ -46,5 +46,21 @@ Chart.Spline(x,y',Name="spline")
4646
|> Chart.withLineStyle(Width=2,Dash=StyleOption.DrawingStyle.Dot)
4747
|> Chart.withLineStyle(Width=6,Dash=StyleOption.DrawingStyle.Dot)
4848
(*** include-it:line3 ***)
49+
50+
51+
(**
52+
53+
## Point chart with text label
54+
The following example calls the `Chart.Point` method to generate a Scattern Plot containing X and Y values plus text labels.
55+
If `TextPosition` is set the labels are drawn otherwise only shown when hovering over the points.
56+
57+
*)
58+
59+
60+
let l = ["a";"b";"c";"d";"e";"f";"g";"h";"i";"j";]
61+
62+
(*** define-output:pointsWithLabels ***)
63+
Chart.Point(x,y',Name="points",Labels=l,TextPosition=StyleOption.TextPosition.TopRight)
64+
(*** include-it:pointsWithLabels ***)
4965
|> Chart.Show
5066

src/FSharp.Plotly/Chart.fs

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ type Chart =
2828

2929
/// Uses points to represent data points
3030
static member Point(x, y,?Name,?Showlegend,?MarkerSymbol,?Color,?Opacity,?Labels,?TextPosition,?TextFont) =
31+
// if text position or font is set than show labels (not only when hovering)
32+
let changeMode = StyleOption.ModeUtils.showText (TextPosition.IsSome || TextFont.IsSome)
33+
3134
let trace =
3235
TraceObjects.Scatter()
33-
|> Options.Scatter(X = x,Y = y, Mode = StyleOption.Markers)
36+
|> Options.Scatter(X = x,Y = y, Mode = changeMode StyleOption.Markers)
3437
|> Options.ITraceInfo(?Name=Name,?Showlegend=Showlegend,?Opacity=Opacity)
3538
|> Options.IMarker(Options.Marker(?Color=Color,?Symbol=MarkerSymbol))
3639
|> Options.ITextLabel(?Text=Labels,?Textposition=TextPosition,?Textfont=TextFont)
@@ -44,13 +47,19 @@ type Chart =
4447

4548
/// Uses lines to represent data points
4649
static member Line(x, y,?Name,?ShowMarkers,?Showlegend,?MarkerSymbol,?Color,?Opacity,?Labels,?TextPosition,?TextFont,?Dash,?Width) =
47-
let mode' =
48-
match ShowMarkers with
49-
| Some show -> if show then StyleOption.Lines_Markers else StyleOption.Lines
50-
| None -> StyleOption.Lines_Markers // default
50+
// if text position or font is set than show labels (not only when hovering)
51+
let changeMode =
52+
let isShowMarker =
53+
match ShowMarkers with
54+
| Some isShow -> isShow
55+
| None -> false
56+
StyleOption.ModeUtils.showText (TextPosition.IsSome || TextFont.IsSome)
57+
>> StyleOption.ModeUtils.showMarker (isShowMarker)
58+
59+
5160
let trace =
5261
TraceObjects.Scatter()
53-
|> Options.Scatter(X = x,Y = y, Mode = mode')
62+
|> Options.Scatter(X = x,Y = y, Mode = (changeMode StyleOption.Mode.Lines))
5463
|> Options.ITraceInfo(?Name=Name,?Showlegend=Showlegend,?Opacity=Opacity)
5564
|> Options.ILine(Options.Line(?Color=Color,?Dash=Dash,?Width=Width))
5665
|> Options.IMarker(Options.Marker(?Color=Color,?Symbol=MarkerSymbol))
@@ -65,13 +74,16 @@ type Chart =
6574

6675
/// A Line chart that plots a fitted curve through each data point in a series.
6776
static member Spline(x, y,?Name,?ShowMarkers,?Showlegend,?MarkerSymbol,?Color,?Opacity,?Labels,?TextPosition,?TextFont,?Dash,?Width,?Smoothing) =
68-
let mode' =
69-
match ShowMarkers with
70-
| Some show -> if show then StyleOption.Lines_Markers else StyleOption.Lines
71-
| None -> StyleOption.Lines_Markers // default
77+
let changeMode =
78+
let isShowMarker =
79+
match ShowMarkers with
80+
| Some isShow -> isShow
81+
| None -> false
82+
StyleOption.ModeUtils.showText (TextPosition.IsSome || TextFont.IsSome)
83+
>> StyleOption.ModeUtils.showMarker (isShowMarker)
7284
let trace =
7385
TraceObjects.Scatter()
74-
|> Options.Scatter(X = x,Y = y, Mode = mode', ?Fillcolor=Color)
86+
|> Options.Scatter(X = x,Y = y, Mode = (changeMode StyleOption.Mode.Lines), ?Fillcolor=Color)
7587
|> Options.ITraceInfo(?Name=Name,?Showlegend=Showlegend,?Opacity=Opacity)
7688
|> Options.ILine(Options.Line(?Color=Color,?Dash=Dash,?Width=Width, Shape=StyleOption.Shape.Spline, ?Smoothing=Smoothing))
7789
|> Options.IMarker(Options.Marker(?Color=Color,?Symbol=MarkerSymbol))
@@ -86,9 +98,12 @@ type Chart =
8698

8799
/// A variation of the Point chart type, where the data points are replaced by bubbles of different sizes.
88100
static member Bubble(x, y,sizes:seq<#IConvertible>,?Name,?Showlegend,?MarkerSymbol,?Color,?Opacity,?Labels,?TextPosition,?TextFont) =
101+
// if text position or font is set than show labels (not only when hovering)
102+
let changeMode = StyleOption.ModeUtils.showText (TextPosition.IsSome || TextFont.IsSome)
103+
89104
let trace =
90105
TraceObjects.Scatter()
91-
|> Options.Scatter(X = x,Y = y, Mode = StyleOption.Markers)
106+
|> Options.Scatter(X = x,Y = y, Mode = changeMode StyleOption.Markers)
92107
|> Options.ITraceInfo(?Name=Name,?Showlegend=Showlegend,?Opacity=Opacity)
93108
|> Options.IMarker(Options.Marker(?Color=Color,?Symbol=MarkerSymbol, MultiSizes=sizes))
94109
|> Options.ITextLabel(?Text=Labels,?Textposition=TextPosition,?Textfont=TextFont)
@@ -102,12 +117,18 @@ type Chart =
102117

103118
/// Displays a range of data by plotting two Y values per data point, with each Y value being drawn as a line
104119
static member Range(x, y, upper, lower,?Name,?ShowMarkers,?Showlegend,?Color,?RangeColor,?Labels,?TextPosition,?TextFont) =
105-
let mode' = match ShowMarkers with
106-
| Some show -> if show then StyleOption.Lines_Markers else StyleOption.Lines
107-
| None -> StyleOption.Lines_Markers // default
120+
// if text position or font is set than show labels (not only when hovering)
121+
let changeMode =
122+
let isShowMarker =
123+
match ShowMarkers with
124+
| Some isShow -> isShow
125+
| None -> false
126+
StyleOption.ModeUtils.showText (TextPosition.IsSome || TextFont.IsSome)
127+
>> StyleOption.ModeUtils.showMarker (isShowMarker)
128+
108129
let trace =
109130
TraceObjects.Scatter()
110-
|> Options.Scatter(X = x,Y = y, Mode = mode', ?Fillcolor=Color)
131+
|> Options.Scatter(X = x,Y = y, Mode = changeMode StyleOption.Mode.Lines, ?Fillcolor=Color)
111132
|> Options.ITraceInfo(?Name=Name,?Showlegend=Showlegend)
112133
|> Options.ILine(Options.Line(?Color=Color))
113134
|> Options.IMarker(Options.Marker(?Color=Color))
@@ -134,13 +155,18 @@ type Chart =
134155

135156
/// Emphasizes the degree of change over time and shows the relationship of the parts to a whole.
136157
static member Area(x, y,?Name,?ShowMarkers,?Showlegend,?MarkerSymbol,?Color,?Opacity,?Labels,?TextPosition,?TextFont,?Dash,?Width) =
137-
let mode' =
138-
match ShowMarkers with
139-
| Some show -> if show then StyleOption.Lines_Markers else StyleOption.Lines
140-
| None -> StyleOption.Lines // default
158+
// if text position or font is set than show labels (not only when hovering)
159+
let changeMode =
160+
let isShowMarker =
161+
match ShowMarkers with
162+
| Some isShow -> isShow
163+
| None -> false
164+
StyleOption.ModeUtils.showText (TextPosition.IsSome || TextFont.IsSome)
165+
>> StyleOption.ModeUtils.showMarker (isShowMarker)
166+
141167
let trace =
142168
TraceObjects.Scatter()
143-
|> Options.Scatter(X = x,Y = y, Mode = mode',Fill=StyleOption.ToZero_y)
169+
|> Options.Scatter(X = x,Y = y, Mode = changeMode StyleOption.Mode.Lines,Fill=StyleOption.ToZero_y)
144170
|> Options.ITraceInfo(?Name=Name,?Showlegend=Showlegend,?Opacity=Opacity)
145171
|> Options.ILine(Options.Line(?Color=Color,?Dash=Dash,?Width=Width))
146172
|> Options.IMarker(Options.Marker(?Color=Color,?Symbol=MarkerSymbol))
@@ -155,13 +181,17 @@ type Chart =
155181

156182
/// Emphasizes the degree of change over time and shows the relationship of the parts to a whole.
157183
static member SplineArea(x, y,?Name,?ShowMarkers,?Showlegend,?MarkerSymbol,?Color,?Opacity,?Labels,?TextPosition,?TextFont,?Dash,?Width,?Smoothing) =
158-
let mode' =
159-
match ShowMarkers with
160-
| Some show -> if show then StyleOption.Lines_Markers else StyleOption.Lines
161-
| None -> StyleOption.Lines // default
184+
// if text position or font is set than show labels (not only when hovering)
185+
let changeMode =
186+
let isShowMarker =
187+
match ShowMarkers with
188+
| Some isShow -> isShow
189+
| None -> false
190+
StyleOption.ModeUtils.showText (TextPosition.IsSome || TextFont.IsSome)
191+
>> StyleOption.ModeUtils.showMarker (isShowMarker)
162192
let trace =
163193
TraceObjects.Scatter()
164-
|> Options.Scatter(X = x,Y = y, Mode = mode',Fill=StyleOption.ToZero_y)
194+
|> Options.Scatter(X = x,Y = y, Mode = changeMode StyleOption.Mode.Lines,Fill=StyleOption.ToZero_y)
165195
|> Options.ITraceInfo(?Name=Name,?Showlegend=Showlegend,?Opacity=Opacity)
166196
|> Options.ILine(Options.Line(Shape=StyleOption.Shape.Spline,?Color=Color,?Dash=Dash,?Width=Width,?Smoothing=Smoothing))
167197
|> Options.IMarker(Options.Marker(?Color=Color,?Symbol=MarkerSymbol))

src/FSharp.Plotly/ChartExtensions.fs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,16 @@ module ChartExtensions =
208208
/// Create a combined chart with the given charts merged
209209
static member Combine(gCharts:seq<GenericChart>) =
210210
GenericChart.combine gCharts
211+
212+
// /// Save chart as html single page
213+
// static member SaveHtmlAs pathName (ch:GenericChart) =
214+
// let html = GenericChart.toEmbeddedHTML ch
215+
// let tempPath = Path.GetTempPath()
216+
// let file = sprintf "%s.html" guid
217+
// let path = Path.Combine(tempPath, file)
218+
// File.WriteAllText(path, html)
219+
// System.Diagnostics.Process.Start(path) |> ignore
220+
211221

212222
/// Show chart in browser
213223
static member Show (ch:GenericChart) =

src/FSharp.Plotly/StyleOption.fs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,45 @@ module StyleOption =
8888

8989
static member convert = Mode.toString >> box
9090

91+
/// Functions to manipulate StyleOption Mode
92+
module ModeUtils=
93+
94+
/// Takes the current mode and adds the Text flag
95+
let showText isShow (cmode:Mode) =
96+
if isShow then
97+
match cmode with
98+
| None -> Mode.Text
99+
| Lines -> Mode.Lines_Text
100+
| Lines_Markers -> Mode.Lines_Markers_Text
101+
| Markers -> Mode.Markers_Text
102+
| _ -> cmode
103+
else
104+
cmode
105+
/// Takes the current mode and adds the Markers flag
106+
let showMarker isShow (cmode:Mode) =
107+
if isShow then
108+
match cmode with
109+
| None -> Mode.Markers
110+
| Lines -> Mode.Lines_Markers
111+
| Lines_Text -> Mode.Lines_Markers_Text
112+
| Text -> Mode.Markers_Text
113+
| _ -> cmode
114+
else
115+
cmode
116+
117+
/// Takes the current mode and adds the Lines flag
118+
let showLines isShow (cmode:Mode) =
119+
if isShow then
120+
match cmode with
121+
| None -> Mode.Lines
122+
| Markers -> Mode.Lines_Markers
123+
| Markers_Text -> Mode.Lines_Markers_Text
124+
| Text -> Mode.Lines_Text
125+
| _ -> cmode
126+
else
127+
cmode
128+
129+
91130
/// Sets the positions of the `text` elements with respects to the (x,y) coordinates. (default: MiddleCenter)
92131
type TextPosition =
93132
| TopLeft | TopCenter | TopRight | MiddleLeft | MiddleCenter | MiddleRight | BottomLeft | BottomCenter | BottomRight

0 commit comments

Comments
 (0)