Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Overview: Charting Libraries for F#

Visual Studio 2010

Applies to: Functional Programming

Published: January 2010

Authors: Tomas Petricek and Jon Skeet

Referenced Image

Get this book in Print, PDF, ePub and Kindle at manning.com. Use code “MSDN37b” to save 37%.

Summary: This overview discusses different technologies and libraries that can be used to create charts in F#. It contains references to other tutorials and documentation for all of the discussed technologies.

This topic contains the following sections.

This article is associated with Real World Functional Programming: With Examples in F# and C# by Tomas Petricek with Jon Skeet from Manning Publications (ISBN 9781933988924, copyright Manning Publications 2009, all rights reserved). No part of these chapters may be reproduced, stored in a retrieval system, or transmitted in any form or by any means—electronic, electrostatic, mechanical, photocopying, recording, or otherwise—without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews.

Creating plots and charts to visualize and understand data is an important aspect of working with data in F#. The requirements for a charting technology depend on whether it is used in the F# Interactive environment or whether charts are created in a standalone application:

  • An F# Interactive script requires an easy way to compare various aspects of the data or visualize results that were calculated from the data set. In this setting, plots are quickly created, changed, or recreated for a different part of the data set.

  • A standalone F# application or a library (for either desktop or web applications), needs a way of creating visually attractive charts that can be easily saved or printed.

This document introduces various options for creating charts in F#. The main focus is on the rich set of charts that can be created using the Chart Controls library. In F#, this library can be used directly (in the same way as it can be used from C#) or using an Flibrary, which can be freely downloaded as an F# sample. The library provides a more F#-friendly syntax and can be used elegantly with F# features for data processing, such as for sequence expressions.

When creating charts interactively, it is easier to use the FSharpChart library because it can create a chart with a single line of F# code. Charts that are embedded in an application may require more control over the chart presentation and user interaction. In that case, you may consider using the Chart Controls library directly.

This article also briefly looks at a few other tools that can be used to create charts from F#, including the open-source project gnuplot and Microsoft Excel. In general, it is possible to access any .NET charting library from F#, including a rich set of third-party libraries.

The first two options for charting in F# discussed below are based on the Chart Controls library. The library is available as a built-in component of .NET 4.0 or as an additional download for .NET 3.5. It can be used in F# Interactive or as a component in desktop and web applications. The final two options require some additional components to be installed (Microsoft Excel and gnuplot).

Using Chart Controls Directly

Chart Controls is a powerful managed library for creating a wide range of charts. Figure 1 demonstrates a sample financial chart created using this library. The library supports both simple charts (such as line, bar, and pie charts) and advanced statistical and financial charts (such as box plot, candlestick, and Kagi charts).

Using the library directly is a good choice when:

  • Developing a custom charting API for F# similar to FSharpChart but designed for a particular domain that easily allows users to solve more specific tasks.

  • An application requires to use the Chart Controls library's advanced features that cannot be accessed more easily using the FSharpChart library.

  • Working in a mixed environment that also uses C# or Visual Basic, which makes it possible to benefit from the designer support provided by the library.

In most cases, it is a good idea to first look at the FSharpChart library because it makes most of the usual tasks easier. However, even when using the F# wrapper most of the time, it is useful to know how Chart Controls work. The F# library just provides a thin layer on top of it and some of the Chart Controls functionality is still used directly, for example, to configure the visual properties of charts.

Figure 1. A sample chart visualizing financial data

Referenced Image

For more information about using Microsoft Chart Controls directly, see:

Using the FSharpChart Library

The F# Charting library is a layer on top of Microsoft Chart Controls that can be used more easily from F#. It is designed mainly for use from F# Interactive and makes it possible to quickly visualize data using just one line of F# code. However, it can be also used as a control in Windows Forms applications.

The library has the following features:

  • A simple chart can be created using a single line of F# code. The code to create the chart can be easily evolved to configure visual aspects of the chart or combine multiple different charts into a single chart.

  • Data displayed on a chart can be updated from F# Interactive and it is also possible to create a chart that updates itself automatically as more data becomes available.

  • The library supports all chart types from Microsoft Chart Controls. All chart types are readily available in Visual Studio's IntelliSense.

  • The chart configuration is written in the functional programming style. Thanks to static typing, the options can be explored using IntelliSense.

In general, the F# Charting library is the best option for interactive scripting and is also a good choice when embedding a simple chart in a desktop application. When changing some advanced aspect of a chart, it is always possible to access the underlying Chart Controls objects.

For more information about using the FSharpChart library see:

Using Microsoft Excel

Microsoft Excel is a widely adopted spreadsheet application with many features for data analysis. It comes with an API that can be called from .NET programs so it can be controlled from an F# program or script. Excel also provides several options for creating charts. The articles referenced below demonstrate how to invoke the Excel API from F# and how to construct a chart in Excel.

The articles discussing Excel focus on running Excel as a separate process and controlling it from F# code. Excel provides a complex ecosystem, and F# could be also used to develop Excel add-ins or to call Excel's data analysis features.

Using Excel for charting from F# has the following properties:

  • It is possible to use some of Excel's advanced data analysis or charting features.

  • An F# script or application can create a simple chart, and the propreties of the chart can be later adjusted using Excel's graphical user interface.

  • Excel charts can be easily sent to other people. Even if they are not programmers, the users can modify the chart, update its data, or change how it looks.

For more information about using Excel and creating an F# wrapper for it see:

Calling Gnuplot

Gnuplot is a cross-platform software package for creating plots of functions and data. It runs as a separate process that can be controlled from .NET applications. The F# Cross Platform project provides a wrapper for calling gnuplot using an F#-friendly interface. The article referenced below discusses both of the approaches.

Using gnuplot from F# opens up several interesting possibilities:

  • Developers who are already familiar with gnuplot can use it easily for charting when processing data in F#.

  • Calling gnuplot from F# is a cross-platform approach that works well on Mono.

  • This approach makes it possible to use F# for contributing to a larger scientific project that already uses gnuplot for visualization.

For more information about using gnuplot see:

This overview looked at various charting technologies that can be used from F#. It briefly introduced four different charting technologies and discussed in which scenarios one may be a better choice than another.

Two of the technologies are immediately available in .NET 4. Microsoft Chart Controls is a powerful managed charting library and FSharpChart wrapper is an F# API built on top of this library. The remaining two technologies that were discussed are examples of the large number of third-party tools and libraries that can be used from F#. The article discussed Excel, which is a de-facto standard in many companies, and the open-source gnuplot project. These two are just examples. As a .NET language, F# can be used with any .NET library or tool providing a .NET API.

This article is written as a companion to Real World Functional Programming: With Examples in F# and C#. Book chapters related to the content of this article are:

  • Book Chapter 4: “Exploring F# and .NET libraries” by example demonstrates how to create a simple charting application from scratch. This chapter is useful for learning F# programming with the focus on working with data.

  • Book Chapter 12: “Sequence expressions and alternative workflows” explains how to work with in-memory datasets in F# using sequence expressions and higher-order functions.

  • Book Chapter 13: “Asynchronous and data-driven programming” shows how to use asynchronous workflows to obtain data from the Internet, how to convert data to a structured format, and how to chart it using Excel.

The following MSDN documents are related to the topic of this article:

  • F# Charting Wrapper contains the download link as well as more resources about the FSharpChart library.

  • Excel Object Model Overview provides the documentation for the objects exposed by the Excel .NET API that can be called from F#.

  • Chart Controls is an MSDN section dedicated to working with Microsoft Chart Controls with many examples in C# and Visual Basic. It includes a comprehensive reference with all of the chart types and properties.

  • F# cross-platform packages and samples (CodePlex) is an open-source project that includes a wrapper for creating gnuplot charts from F#.

To download the code snippets shown in this article, go to http://code.msdn.microsoft.com/Chapter-6-Visualizing-Data-c68a2296

© 2015 Microsoft