6 people like it.

University enrollment (CZE vs. EU)

Using World Bank type provider and FSharpChart to show average university enrollment in Czech Republic, European Union and OECD members.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
// Reference type provider and FSharpChart
#r @"WorldBank.TypeProvider.dll"
#load @"FSharpChart.fsx"

open Samples.Charting
open System.Windows.Forms.DataVisualization.Charting
open System.Drawing

let dashGrid = Grid(LineColor = Color.Gainsboro, LineDashStyle = ChartDashStyle.Dash)

/// Draws line chart from year-value pairs using specified label & color
let lineChart (data:seq<int * float>) name color =
  ( FSharpChart.Line(Array.ofSeq data, Name=name)
    |> FSharpChart.WithSeries.Style(BorderWidth = 2, Color = color) ) 
  :> ChartTypes.GenericChart
    
/// Calculate average university enrollment for EU
let avgEU =
    [ for c in WorldBank.Regions.``European Union`` do
        yield! c.``School enrollment, tertiary (% gross)`` ]
    |> Seq.groupBy fst
    |> Seq.map (fun (y, v) -> y, Seq.averageBy snd v)
    |> Array.ofSeq
    |> Array.sortBy fst

/// Calculate average university enrollment for OECD
let avgOECD =
    [ for c in WorldBank.Regions.``OECD members`` do
        yield! c.``School enrollment, tertiary (% gross)`` ]
    |> Seq.groupBy fst
    |> Seq.map (fun (y, v) -> y, Seq.averageBy snd v)
    |> Array.ofSeq
    |> Array.sortBy fst

// Generate nice line chart combining CZ and EU enrollment
FSharpChart.Combine
  [ yield lineChart avgEU "EU" Color.Blue
    yield lineChart avgOECD "OECD" Color.Goldenrod
    let cze = WorldBank.Countries.``Czech Republic``
    yield lineChart cze.``School enrollment, tertiary (% gross)`` "CZ" Color.DarkRed ]
|> FSharpChart.WithLegend(Docking = Docking.Left)
|> FSharpChart.WithArea.AxisY(MajorGrid = dashGrid) 
|> FSharpChart.WithArea.AxisX(MajorGrid = dashGrid)
namespace System
namespace System.Windows
namespace System.Windows.Forms
namespace System.Drawing
val dashGrid : obj

Full name: Script.dashGrid
type Color =
  struct
    member A : byte
    member B : byte
    member Equals : obj:obj -> bool
    member G : byte
    member GetBrightness : unit -> float32
    member GetHashCode : unit -> int
    member GetHue : unit -> float32
    member GetSaturation : unit -> float32
    member IsEmpty : bool
    member IsKnownColor : bool
    ...
  end

Full name: System.Drawing.Color
property Color.Gainsboro: Color
val lineChart : data:seq<int * float> -> name:'a -> color:'b -> 'c

Full name: Script.lineChart


 Draws line chart from year-value pairs using specified label & color
val data : seq<int * float>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val name : 'a
val color : 'a
module Array

from Microsoft.FSharp.Collections
val ofSeq : source:seq<'T> -> 'T []

Full name: Microsoft.FSharp.Collections.Array.ofSeq
val avgEU : (System.IComparable * obj) []

Full name: Script.avgEU


 Calculate average university enrollment for EU
val c : obj
module Seq

from Microsoft.FSharp.Collections
val groupBy : projection:('T -> 'Key) -> source:seq<'T> -> seq<'Key * seq<'T>> (requires equality)

Full name: Microsoft.FSharp.Collections.Seq.groupBy
val fst : tuple:('T1 * 'T2) -> 'T1

Full name: Microsoft.FSharp.Core.Operators.fst
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val y : System.IComparable
val v : seq<System.IComparable * obj>
val averageBy : projection:('T -> 'U) -> source:seq<'T> -> 'U (requires member ( + ) and member DivideByInt and member get_Zero)

Full name: Microsoft.FSharp.Collections.Seq.averageBy
val snd : tuple:('T1 * 'T2) -> 'T2

Full name: Microsoft.FSharp.Core.Operators.snd
val sortBy : projection:('T -> 'Key) -> array:'T [] -> 'T [] (requires comparison)

Full name: Microsoft.FSharp.Collections.Array.sortBy
val avgOECD : (System.IComparable * obj) []

Full name: Script.avgOECD


 Calculate average university enrollment for OECD
val c : 'a
property Color.Blue: Color
property Color.Goldenrod: Color
property Color.DarkRed: Color

More information

Link:http://fssnip.net/9a
Posted:12 years ago
Author:Tomas Petricek
Tags: type provider , world bank , fsharpchart , charting