Skip to content

Going Artistic with FSharp.Charting

July 3, 2013

Since the early days of Postscript and gnuplot people tend to demonstrate the coolness of a plotting tool by creating miscellaneous computer art “masterpieces”. So, why FSharp.Charting should be an exception?

I’ll try demonstrating compositional abilities and richness of available styling provided by FSharp.Charting by plotting two still eye-candies, reserving a motion plot for another blog post. Initial ideas for both plots are taken from Jon Harrop’s book “Visual F# 2010 for Technical Computing” and this post of Kean Walmsley.

Running the snippets requires just installing FSharp.Charting via NuGet. The first plot just demonstrates chart compositionality:

#load @"..\packages\FSharp.Charting.0.82\Fsharp.Charting.fsx"
open FSharp.Charting
open System.Drawing

let n = 16
let point i =
    let t = float(i % n) / float n * 2.0 * System.Math.PI in (sin t, cos t)

Chart.Combine(seq { for i in 0..n-1 do for j in i+1..n -> Chart.Line [point i; point j] })
|> Chart.WithTitle(Text="Artistic FSharp.Charting Sample 1",Color=Color.DarkBlue,Style=ChartTypes.TextStyle.Frame)
|> Chart.WithYAxis(Enabled=false)
|> Chart.WithXAxis(Enabled=false)

yielding the following image:
FSArt1

The second plot in addition to compositionality demonstrates extensive styling abilities of the library (backgrounds, gradient fill, etc.):

#load @"..\packages\FSharp.Charting.0.82\Fsharp.Charting.fsx"
open FSharp.Charting
open System.Drawing
open System.Windows.Forms.DataVisualization.Charting

let myBackground = ChartTypes.Background.Gradient (Color.LightSkyBlue,Color.White,GradientStyle.DiagonalRight)
let myFrame = ChartTypes.Background.Solid Color.Blue

// drawing per se...
Chart.Combine(
    seq { for x in 0.0..16.0..1024.  do 
            let repers = [x,0.; 512.,x; 512.-x,512.; 0.,512.-x; x,0.] 
            for (x0,y0),(xl,yl) in Seq.pairwise repers -> Chart.Line [(x0,y0);(xl,yl)] })
// ...the rest is pure styling
|> Chart.WithTitle(Text="Artistic FSharp.Charting Sample 2",Color=Color.DarkBlue,Style=ChartTypes.TextStyle.Emboss)
|> Chart.WithXAxis(Enabled=false)
|> Chart.WithYAxis(Enabled=false)
|> Chart.WithStyling(AreaBackground=myBackground,Background=myFrame)

Running this script plots the following:
FSArt2

Enjoy!

Advertisements

From → Uncategorized

One Comment

Trackbacks & Pingbacks

  1. F# Weekly #27 2013 | Sergey Tihon's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: