37 people like it.

Plotting using Chart сontrol

This snippet shows how to plot data on a form using .NET 4.0 Chart control.

 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: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
(Opening declarations)

// Based on a code by James Hugard, available here: 
// http://stackoverflow.com/questions/3276357/how-do-i-plot-a-data-series-in-f 

// Create a window with Chart control and initializes
// the chart control using a given function
type Visualiser(title, style, init_fun:Chart->unit) =
  inherit Form( Text=title )
  let chart = new Chart(Dock=DockStyle.Fill)
  let area = new ChartArea(Name=title)
  let series = new Series()
  do series.ChartType <- style
  do series.ChartArea <- title
  do chart.Series.Add(series)
  do chart.ChartAreas.Add(area)
  do init_fun chart
  do base.Controls.Add(chart)
  
// Visualises a series of float values
// Returns a Form
let SeqVisualiser title style (xs: float seq) = 
  new Visualiser(title,style,fun chart -> xs |> Seq.iter (chart.Series.[0].Points.Add >> ignore))

// Visualizes a series of float pairs treated as (x,y) coordinats
// Returns a Form
let PointVisualiser title style xs =
  new Visualiser(title,style,
                  fun chart -> 
                   xs |> Seq.iter (fun (x,y) -> chart.Series.[0].Points.AddXY(x,y)|>ignore))

// Visualizes a series of float values with labels
let LabelVisualiser3D title style xs =
  new Visualiser(title,style,
   fun chart -> 
       chart.ChartAreas.[0].Area3DStyle.Enable3D <- true
       chart.ChartAreas.[0].Area3DStyle.Perspective <- 10
       xs |> Seq.iter (fun (y:string,x:float) -> 
                         let pt = chart.Series.[0].Points.Add(x)
                         pt.Label <- y))

// Plotting sin function
let V1 = SeqVisualiser "Data" (SeriesChartType.Line) (seq {-6.0..0.01..6.0}|> Seq.map sin)
V1.Show()

// Plotting bars with random values
let V2 = SeqVisualiser "Data" SeriesChartType.Bar 
           (seq { let R = new System.Random()
                 for i in [1..10] -> R.NextDouble()*3.0 })
V2.Show()
 
// Plotting a number or random (x,y)-points
let V3 = PointVisualiser "Data" SeriesChartType.Bubble
           (seq { let R = new System.Random()
                  for i in [1..10] -> (R.NextDouble()*3.0,R.NextDouble()*3.0) })
V3.Show()

// Plotting some statistics about occurence of strings in HTML page
(Opening declarations)

let http(url: string) = (fetch HTML from given URL)
let page = http "http://osys.ru/"
let count s page = Regex.Matches(page,s).Count
let os = ["Windows";"UNIX";"Linux";"DOS"] |> Seq.map (fun s -> (s,float(count s page)))

let V4 = LabelVisualiser3D "OS" SeriesChartType.Pie os
V4.Show()
open System.Drawing
open System.Windows.Forms
#r "System.Windows.Forms.DataVisualization"
open System.Windows.Forms.DataVisualization.Charting
Multiple items
type Visualiser =
  inherit Form
  new : title:string * style:SeriesChartType * init_fun:(Chart -> unit) -> Visualiser

Full name: Script.Visualiser

--------------------
new : title:string * style:SeriesChartType * init_fun:(Chart -> unit) -> Visualiser
val title : string
val style : SeriesChartType
val init_fun : (Chart -> unit)
Multiple items
type Chart =
  inherit Control
  new : unit -> Chart
  member AlignDataPointsByAxisLabel : unit -> unit + 3 overloads
  member Annotations : AnnotationCollection
  member AntiAliasing : AntiAliasingStyles with get, set
  member ApplyPaletteColors : unit -> unit
  member BackColor : Color with get, set
  member BackGradientStyle : GradientStyle with get, set
  member BackHatchStyle : ChartHatchStyle with get, set
  member BackImage : string with get, set
  member BackImageAlignment : ChartImageAlignmentStyle with get, set
  ...

Full name: System.Windows.Forms.DataVisualization.Charting.Chart

--------------------
Chart() : unit
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
Multiple items
type Form =
  inherit ContainerControl
  new : unit -> Form
  member AcceptButton : IButtonControl with get, set
  member Activate : unit -> unit
  member ActiveMdiChild : Form
  member AddOwnedForm : ownedForm:Form -> unit
  member AllowTransparency : bool with get, set
  member AutoScale : bool with get, set
  member AutoScaleBaseSize : Size with get, set
  member AutoScroll : bool with get, set
  member AutoSize : bool with get, set
  ...
  nested type ControlCollection

Full name: System.Windows.Forms.Form

--------------------
Form() : unit
namespace System.Drawing.Text
val chart : Chart
type DockStyle =
  | None = 0
  | Top = 1
  | Bottom = 2
  | Left = 3
  | Right = 4
  | Fill = 5

Full name: System.Windows.Forms.DockStyle
field DockStyle.Fill = 5
val area : ChartArea
Multiple items
type ChartArea =
  inherit ChartNamedElement
  new : unit -> ChartArea + 1 overload
  member AlignWithChartArea : string with get, set
  member AlignmentOrientation : AreaAlignmentOrientations with get, set
  member AlignmentStyle : AreaAlignmentStyles with get, set
  member Area3DStyle : ChartArea3DStyle with get, set
  member Axes : Axis[] with get, set
  member AxisX : Axis with get, set
  member AxisX2 : Axis with get, set
  member AxisY : Axis with get, set
  member AxisY2 : Axis with get, set
  ...

Full name: System.Windows.Forms.DataVisualization.Charting.ChartArea

--------------------
ChartArea() : unit
ChartArea(name: string) : unit
val series : Series
Multiple items
type Series =
  inherit DataPointCustomProperties
  new : unit -> Series + 2 overloads
  member AxisLabel : string with get, set
  member ChartArea : string with get, set
  member ChartType : SeriesChartType with get, set
  member ChartTypeName : string with get, set
  member EmptyPointStyle : DataPointCustomProperties with get, set
  member Enabled : bool with get, set
  member IsXValueIndexed : bool with get, set
  member Legend : string with get, set
  member MarkerStep : int with get, set
  ...

Full name: System.Windows.Forms.DataVisualization.Charting.Series

--------------------
Series() : unit
Series(name: string) : unit
Series(name: string, yValues: int) : unit
property Series.ChartType: SeriesChartType
property Series.ChartArea: string
property Chart.Series: SeriesCollection
System.Collections.ObjectModel.Collection.Add(item: Series) : unit
SeriesCollection.Add(name: string) : Series
property Chart.ChartAreas: ChartAreaCollection
System.Collections.ObjectModel.Collection.Add(item: ChartArea) : unit
ChartAreaCollection.Add(name: string) : ChartArea
val SeqVisualiser : title:string -> style:SeriesChartType -> xs:seq<float> -> Visualiser

Full name: Script.SeqVisualiser
val xs : seq<float>
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<_>
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<_>
module Seq

from Microsoft.FSharp.Collections
val iter : action:('T -> unit) -> source:seq<'T> -> unit

Full name: Microsoft.FSharp.Collections.Seq.iter
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val PointVisualiser : title:string -> style:SeriesChartType -> xs:seq<float * float> -> Visualiser

Full name: Script.PointVisualiser
val xs : seq<float * float>
val x : float
val y : float
val LabelVisualiser3D : title:string -> style:SeriesChartType -> xs:seq<string * float> -> Visualiser

Full name: Script.LabelVisualiser3D
val xs : seq<string * float>
val y : string
Multiple items
val string : value:'T -> string

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

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val pt : DataPoint
property DataPointCustomProperties.Label: string
val V1 : Visualiser

Full name: Script.V1
type SeriesChartType =
  | Point = 0
  | FastPoint = 1
  | Bubble = 2
  | Line = 3
  | Spline = 4
  | StepLine = 5
  | FastLine = 6
  | Bar = 7
  | StackedBar = 8
  | StackedBar100 = 9
  ...

Full name: System.Windows.Forms.DataVisualization.Charting.SeriesChartType
field SeriesChartType.Line = 3
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val sin : value:'T -> 'T (requires member Sin)

Full name: Microsoft.FSharp.Core.Operators.sin
Control.Show() : unit
Form.Show(owner: IWin32Window) : unit
val V2 : Visualiser

Full name: Script.V2
field SeriesChartType.Bar = 7
val R : System.Random
namespace System
Multiple items
type Random =
  new : unit -> Random + 1 overload
  member Next : unit -> int + 2 overloads
  member NextBytes : buffer:byte[] -> unit
  member NextDouble : unit -> float

Full name: System.Random

--------------------
System.Random() : unit
System.Random(Seed: int) : unit
val i : int
System.Random.NextDouble() : float
val V3 : Visualiser

Full name: Script.V3
field SeriesChartType.Bubble = 2
open System.IO
open System.Text.RegularExpressions
val http : url:string -> string

Full name: Script.http
val url : string
let req = System.Net.WebRequest.Create(url)
    use resp = req.GetResponse() // note 'use' = C# 'using'
    use stream = resp.GetResponseStream()
    use reader = new StreamReader(stream)
    let html = reader.ReadToEnd()
    html
val page : string

Full name: Script.page
val count : s:string -> page:string -> int

Full name: Script.count
val s : string
val page : string
Multiple items
type Regex =
  new : pattern:string -> Regex + 1 overload
  member GetGroupNames : unit -> string[]
  member GetGroupNumbers : unit -> int[]
  member GroupNameFromNumber : i:int -> string
  member GroupNumberFromName : name:string -> int
  member IsMatch : input:string -> bool + 1 overload
  member Match : input:string -> Match + 2 overloads
  member Matches : input:string -> MatchCollection + 1 overload
  member Options : RegexOptions
  member Replace : input:string * replacement:string -> string + 5 overloads
  ...

Full name: System.Text.RegularExpressions.Regex

--------------------
Regex(pattern: string) : unit
Regex(pattern: string, options: RegexOptions) : unit
Regex.Matches(input: string, pattern: string) : MatchCollection
Regex.Matches(input: string, pattern: string, options: RegexOptions) : MatchCollection
val os : seq<string * float>

Full name: Script.os
val V4 : Visualiser

Full name: Script.V4
field SeriesChartType.Pie = 17
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/2l
Posted:13 years ago
Author:Dmitry Soshnikov
Tags: plotting , chart control , visualization , charts , graphics