views:

111

answers:

1

Having been able to add data to an Excel spreadsheet from F# using the answer at

http://stackoverflow.com/questions/1214130/f-and-excel-integration-for-net-4-0-visual-studio-2010-beta-1

I find myself unable to figure out how to use the data inserted to create a chart (programmatically in Excel using F#). How can this be done?

I am using Excel 2007 (Office 12 component) and F# 2.0, if that is relevant.

+5  A: 

I have an example that shows how to do that in Real-World Functional Programming book. The Chapter 13 first downloads some data, then adds them to Excel and creates a graph.

The following snippet isn't a complete (working) code, because it relies on some objects constructed earlier, but it could give you an idea how to do that:

// Add new item to the charts collection
let chartobjects = (worksheet.ChartObjects() :?> ChartObjects) 
let chartobject = chartobjects.Add(400.0, 20.0, 550.0, 350.0) 

// Configure the chart using the wizard
chartobject.Chart.ChartWizard
  (Title = "Area covered by forests",
   Source = worksheet.Range("B2", "E" + endColumn),
   Gallery = XlChartType.xl3DColumn, PlotBy = XlRowCol.xlColumns,
   SeriesLabels = 1, CategoryLabels = 1,
   CategoryTitle = "", ValueTitle = "Forests (mil km^2)")

// Set graphical style of the chart
chartobject.Chart.ChartStyle <- 5
Tomas Petricek
Thanks. That's what I was looking for. And the explanation in the book is really good. The main thing missing for me when trying to translate a C# example I found elsewhere was the downcast operator :?>
Muhammad Alkarouri