11 people like it.

Line chart using chart controls

The snippet shows how to use Microsoft Chart Controls (available in .NET 4.0 and for .NET 3.5) to draw a Line chart. The sample generates a 2D spline calculated using sin and cos functions.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
#r "System.Windows.Forms.DataVisualization.dll"
open System
open System.Windows.Forms
open System.Windows.Forms.DataVisualization.Charting

// Calculate X and Y coordinates using two functions
let data = 
  [ for i in 0.0 .. 0.02 .. 2.0 * Math.PI -> 
      sin i, cos i * sin i ]

// Create a chart containing a default area and show it on a form
let chart = new Chart(Dock = DockStyle.Fill)
let form = new Form(Visible = true, Width = 700, Height = 500)
chart.ChartAreas.Add(new ChartArea("MainArea"))
form.Controls.Add(chart)

// Create series and add it to the chart
let series = new Series(ChartType = SeriesChartType.Line)
chart.Series.Add(series)

// Add data to the series in a loop
for x, y in data do
  series.Points.AddXY(x, y) |> ignore
namespace System
namespace System.Windows
namespace System.Windows.Forms
namespace System.Windows.Forms.DataVisualization
namespace System.Windows.Forms.DataVisualization.Charting
val data : (float * float) list

Full name: Script.data
val i : float
type Math =
  static val PI : float
  static val E : float
  static member Abs : value:sbyte -> sbyte + 6 overloads
  static member Acos : d:float -> float
  static member Asin : d:float -> float
  static member Atan : d:float -> float
  static member Atan2 : y:float * x:float -> float
  static member BigMul : a:int * b:int -> int64
  static member Ceiling : d:decimal -> decimal + 1 overload
  static member Cos : d:float -> float
  ...

Full name: System.Math
field Math.PI = 3.14159265359
val sin : value:'T -> 'T (requires member Sin)

Full name: Microsoft.FSharp.Core.Operators.sin
val cos : value:'T -> 'T (requires member Cos)

Full name: Microsoft.FSharp.Core.Operators.cos
val chart : Chart

Full name: Script.chart
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 DockStyle =
  | None = 0
  | Top = 1
  | Bottom = 2
  | Left = 3
  | Right = 4
  | Fill = 5

Full name: System.Windows.Forms.DockStyle
field DockStyle.Fill = 5
val form : Form

Full name: Script.form
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
property Chart.ChartAreas: ChartAreaCollection
Collections.ObjectModel.Collection.Add(item: ChartArea) : unit
ChartAreaCollection.Add(name: string) : 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
property Control.Controls: Control.ControlCollection
Control.ControlCollection.Add(value: Control) : unit
val series : Series

Full name: Script.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
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
property Chart.Series: SeriesCollection
Collections.ObjectModel.Collection.Add(item: Series) : unit
SeriesCollection.Add(name: string) : Series
val x : float
val y : float
property Series.Points: DataPointCollection
DataPointCollection.AddXY(xValue: obj, [<ParamArray>] yValue: obj []) : int
DataPointCollection.AddXY(xValue: float, yValue: float) : int
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
Raw view Test code New version

More information

Link:http://fssnip.net/3d
Posted:13 years ago
Author:Tomas Petricek
Tags: charting , chart controls