Skip to content

Commit 9289c91

Browse files
committed
Project refectoring with Options functions
1 parent ef4f0fd commit 9289c91

24 files changed

+5846
-14243
lines changed

docs/content/bar-charts.fsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ values.
1414

1515
open FSharp.Plotly
1616

17-
let values = [19; 26; 55;]
18-
let labels = ["Residential"; "Non-Residential"; "Utility"]
17+
let yValues = [20; 14; 23;]
18+
let xValues = ["Product A"; "Product B"; "Product C";]
19+
let labels = ["27% market share"; "24% market share"; "19% market share";]
1920

2021
(*** define-output:pie1 ***)
21-
Chart.Pie(values,labels)
22+
Chart.Bar(xValues,yValues,Labels=labels,Opacity=0.3,Marker=Options.Marker(color="rgba(222,45,38,0.8)"))
23+
|> Chart.withSize(500.,500.)
2224
(*** include-it:pie1 ***)
25+
|> Chart.Show
26+

src/FSharp.Plotly.WPF/ChartWPF.fs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ module ChartWPF =
99
open System.IO
1010

1111
open FSharp.Plotly
12-
open StyleGramar
13-
open ChartArea
1412
open GenericChart
1513

1614

1715

1816
type Chart with
1917

2018

21-
static member ShowWPF (ch:GenericChart) =
19+
static member ShowWPF (ch:GenericChart<_>) =
2220
let guid = Guid.NewGuid().ToString()
2321
let html = GenericChart.toEmbeddedHTML ch
2422
ViewContainer.showHTML html

src/FSharp.Plotly/ApplyHelper.fs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
namespace FSharp.Plotly
2+
3+
open System
4+
5+
6+
module internal ApplyHelper =
7+
8+
open System.Reflection
9+
10+
/// Creates an instance of the Object according to applyStyle and applies the function..
11+
let buildApply (applyStyle:'a -> 'a) =
12+
let instance =
13+
System.Activator.CreateInstance<'a>()
14+
applyStyle instance
15+
16+
/// Applies 'applyStyle' to item option. If None it creates a new instance.
17+
let optBuildApply (applyStyle:'a -> 'a) (item:'a option) =
18+
match item with
19+
| Some item' -> applyStyle item'
20+
| None -> buildApply applyStyle
21+
22+
/// Applies Some 'applyStyle' to item. If None it returns 'item' unchanged.
23+
let optApply (applyStyle:('a -> 'a) option) (item:'a ) =
24+
match applyStyle with
25+
| Some apply -> apply item
26+
| None -> item
27+
28+
/// Returns the proptery name from quotation expression
29+
let tryGetPropertyName (expr : Microsoft.FSharp.Quotations.Expr) =
30+
match expr with
31+
| Microsoft.FSharp.Quotations.Patterns.PropertyGet (_,pInfo,_) -> Some pInfo.Name
32+
| _ -> None
33+
34+
/// Sets property value using reflection
35+
let trySetPropertyValue (o:obj) (propName:string) (value:obj) =
36+
let property = o.GetType().GetProperty(propName)
37+
try
38+
property.SetValue(o, value, null)
39+
Some o
40+
with
41+
| :? System.ArgumentException -> None
42+
| :? System.NullReferenceException -> None
43+
44+
/// Gets property value as option using reflection
45+
let tryGetPropertyValue (o:obj) (propName:string) =
46+
try
47+
Some (o.GetType().GetProperty(propName).GetValue(o, null))
48+
with
49+
| :? System.Reflection.TargetInvocationException -> None
50+
| :? System.NullReferenceException -> None
51+
52+
/// Gets property value as 'a option using reflection. Cast to 'a
53+
let tryGetPropertyValueAs<'a> (o:obj) (propName:string) =
54+
try
55+
let v = (o.GetType().GetProperty(propName).GetValue(o, null))
56+
Some (v :?> 'a)
57+
with
58+
| :? System.Reflection.TargetInvocationException -> None
59+
| :? System.NullReferenceException -> None
60+
61+
/// Updates property value by given function
62+
let tryUpdatePropertyValueFromName (o:obj) (propName:string) (f: 'a -> 'a) =
63+
let v = optBuildApply f (tryGetPropertyValueAs<'a> o propName)
64+
trySetPropertyValue o propName v
65+
//o
66+
67+
/// Updates property value by given function
68+
let tryUpdatePropertyValue (o:obj) (expr : Microsoft.FSharp.Quotations.Expr) (f: 'a -> 'a) =
69+
let propName = tryGetPropertyName expr
70+
let v = optBuildApply f (tryGetPropertyValueAs<'a> o propName.Value)
71+
trySetPropertyValue o propName.Value v
72+
//o
73+
74+
let updatePropertyValueAndIgnore (o:obj) (expr : Microsoft.FSharp.Quotations.Expr) (f: 'a -> 'a) =
75+
tryUpdatePropertyValue o expr f |> ignore
76+

0 commit comments

Comments
 (0)