Using F# and XPlot for rugby league visualisations

I want an easy way to visualise rugby league match data for the team I support.

It's been a couple of years since I last played around with F# so I'm going to use XPlot which will also give me the chance to look into the impressive ionide and paket. I'm going to start with a scatter chart showing the times tries were conceded each match - I'll continue to add more visualisations as the season progresses.

The end product

rlviz.png

How to build it

The first thing to do is fire up vscode and install the ionide-fsharp and ionide-paket extensions, if you've not already done so.

Hit ctrl + shift + p to show all commands and run:

Paket: Init
Paket: Add Nuget Package -> FsLab

Then add a new .fsx file and reference the required packages:

#r "packages/FSharp.Data/lib/net40/FSharp.Data.dll"
#r "packages/XPlot.GoogleCharts/lib/net45/XPlot.GoogleCharts.dll"
#r "packages/Google.DataTable.Net.Wrapper/lib/Google.DataTable.Net.Wrapper.dll"

open System.IO  
open FSharp.Data  
open XPlot.GoogleCharts

The data has been sourced from various match reports. Starting with a subset of data this will be read in from an external JSON file, consisting of an array of rugby matches:

    {
        "opposition": {
            "name": "TheTeamName",
            "tries": [
                {"name":"ThePlayerName", "minute":1}
            ]
        }
    }

Read the JSON using the JSON Type Provider

type Matches = JsonProvider<"matches.json">

let matches =  
    Matches.GetSamples()

Points is a list of tuples of type (int, int). The first int is the rugby match, the second is the minute the try is scored.

let points =  
    [for matchIndex in 0..Seq.length matches-1 do
        let theMatch = matches |> Seq.item matchIndex
        let tries = theMatch.Opposition.Tries
        for tryIndex in 0..Seq.length tries-1 do
            let theTry = tries |> Seq.item tryIndex
            yield (matchIndex, theTry.Minute)]

Set graph options:

let options =  
    Options(
        title = "Minutes tries conceded",
        hAxis = Axis(title = "Match", gridlines = Gridlines(count=matches.Length)),
        vAxis = Axis(title = "Minute", gridlines = Gridlines(count=10), viewWindow = ViewWindow(max=80)),
        bubble = Bubble(textStyle=TextStyle(color="transparent")),
        colors = [| "red" |] )

Create a scatter chart from the points defined above and launch in browser:

let chart =  
    points
    |> Chart.Scatter
    |> Chart.WithOptions options
    |> Chart.WithLabels ["Try"]
    |> Chart.WithSize (800, 300)
    |> Chart.Show

What next?

  • Create other visualisations using the full set of data
  • Explore trends in the visualisations