F# Charting


F# Charting: Bar and Column Charts

Summary: This example shows how to create bar and column charts in F#.

The input data in this example is an F# list of tuples containing continent names and total populations. The example demonstrates how to display a bar/column chart with names of continents as labels and the populations as the values. A sample bar chart is shown below.

A bar or a column chart can be created using the Chart.Column and Chart.Bar methods.

All methods are overloaded and can be called with various types of parameters. When called with a list containing just Y values, the chart automatically uses the sequence 1, 2, 3… for the X values. Alternatively, it is possible to provide a list containing both X and Y values as a tuple, which gives a way to draw 2D curves and scatter plots as well. Here are three examples:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
// On Mac OSX use FSharp.Charting.Gtk.fsx
#I "packages/FSharp.Charting.Gtk"
#load "FSharp.Charting.Gtk.fsx"

open FSharp.Charting
open System

let countryData = 
    [ "Africa", 1033043; 
      "Asia", 4166741; 
      "Europe", 732759; 
      "South America", 588649; 
      "North America", 351659; 
      "Oceania", 35838  ]
1: 
Chart.Bar countryData
No value has been returned
1: 
Chart.Column countryData
No value has been returned
1: 
Chart.Bar [ 0 .. 10 ] 
No value has been returned

When using F# Interactive, each of these examples needs to be evaluated separately. This way, F# Interactive invokes a handler that automatically shows the created chart.

The first example specifies the data source as a single list that contains two-element tuples. The first element of the tuple is the X value (category) and the second element is the population.

The second example creates a Column chart instead of a Bar chart.

The third example below shows that you may also simply give a set of Y values, rather than (X,Y) value pairs.

Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
namespace FSharp.Charting
namespace System
val countryData : (string * int) list

Full name: BarAndColumnCharts.countryData
type Chart =
  static member Area : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<'a2> * ?Color:OxyColor * ?XTitle:string * ?YTitle:string -> GenericChart
  static member Area : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<'a3> * ?Color:OxyColor * ?XTitle:string * ?YTitle:string -> GenericChart
  static member Bar : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<'a2> * ?Color:OxyColor * ?XTitle:string * ?YTitle:string -> GenericChart
  static member Bar : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<'a3> * ?Color:OxyColor * ?XTitle:string * ?YTitle:string -> GenericChart
  static member Bubble : data:seq<#value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<'a3> * ?Color:OxyColor * ?XTitle:string * ?YTitle:string * ?MarkerSize:float -> GenericChart (requires 'a3 : null)
  static member Bubble : data:seq<#value * #value * #value> * ?Name:string * ?Title:string * ?Labels:#seq<'a4> * ?Color:OxyColor * ?XTitle:string * ?YTitle:string * ?MarkerSize:float -> GenericChart (requires 'a4 : null)
  static member Candlestick : data:seq<#key * #value * #value * #value * #value> * ?Name:string * ?Title:string * ?Color:OxyColor * ?XTitle:string * ?YTitle:string -> GenericChart
  static member Column : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<'a2> * ?Color:OxyColor * ?XTitle:string * ?YTitle:string * ?ColumnWidth:float -> GenericChart
  static member Column : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<'a3> * ?Color:OxyColor * ?XTitle:string * ?YTitle:string * ?ColumnWidth:float -> GenericChart
  static member Histogram : data:seq<#value> * ?Name:string * ?Title:string * ?Color:OxyColor * ?XTitle:string * ?YTitle:string * ?LowerBound:float * ?UpperBound:float * ?Intervals:float -> GenericChart
  ...

Full name: FSharp.Charting.Chart
static member Chart.Bar : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<'a2> * ?Color:OxyPlot.OxyColor * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
static member Chart.Bar : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<'a3> * ?Color:OxyPlot.OxyColor * ?XTitle:string * ?YTitle:string -> ChartTypes.GenericChart
static member Chart.Column : data:seq<#value> * ?Name:string * ?Title:string * ?Labels:#seq<'a2> * ?Color:OxyPlot.OxyColor * ?XTitle:string * ?YTitle:string * ?ColumnWidth:float -> ChartTypes.GenericChart
static member Chart.Column : data:seq<#key * #value> * ?Name:string * ?Title:string * ?Labels:#seq<'a3> * ?Color:OxyPlot.OxyColor * ?XTitle:string * ?YTitle:string * ?ColumnWidth:float -> ChartTypes.GenericChart
Fork me on GitHub