Tuesday, April 12, 2011

FSharpPlot: Copying charts as EMF to the clipboard

F# is a very handy tool when it comes to playing with new algorithms or data analysis. However an analysis is useless if you can’t present your results. Some days ago Don Syme published with FSharpPlot a wrapper for .Net 4.0 charting controls. It is a handy tool when analyzing data with F# Interactive and you can learn some interesting F# techniques from the sources.

FSharpPlot comes with the possibility to save charts as png file or copy them (also as png) to the clipboard (just right click the chart). That is fine, if you want to look at the chart on a computer screen. However if you want to produce printable reports you might want to resort to a vector file format like emf.

Microsoft Chart Controls come with the possibility to produce three kinds of emf-files. Examples how to save a plot can be found in the Samples for Chart Controls. But as it turns out copying an emf as metafile to the clipboard is not straightforward from the .Net Framework as described in KB article 323530.

Following this Knowledge Base article, I introduced a new private module to FSharpChart to be able to access the functionality from the native Windows API.