This project is read-only.
Excel Chart

Category:
Office Interop
Description: Creating Excel charts. Please make sure you have Excel 2010 installed on your computer.
Code:
let excelChart() = 
try
let dataX = [|0.0 .. 0.1 .. 10.|]
let dataY = [| [|for f in dataX -> cos f|];
[|for f in dataX -> sin f|] |]
// Update the excel charting object
let xlApp = new Excel.ApplicationClass()
let xlWorkBook = xlApp.Workbooks.Add()
let xlWorkSheet = xlWorkBook.Worksheets.[1] :?> Excel.Worksheet
let xlCharts = xlWorkSheet.ChartObjects() :?> Excel.ChartObjects
let xlChart = xlCharts.Add(1., 1., 460., 380.)
let myChart = xlChart.Chart
// Fill in a excel worksheet with data from dataY[][]
for i in 1 .. dataY.Length do
for j in 1 .. dataY.[i-1].Length do
xlWorkSheet.Cells.[j, i] <- dataY.[i-1].[j-1]
let xlRange = xlWorkSheet.Cells.CurrentRegion
myChart.SetSourceData(xlRange)
// Set Plot type and show chart
myChart.ChartType <- Excel.XlChartType.xlXYScatterLines
xlApp.Visible <- true
with _ -> printfn "%s" "please check your Excel installation"

Execution Result:
 

Last edited Sep 14, 2011 at 4:05 AM by ttliu2000, version 1

Comments

No comments yet.