Creating Charts in Excel

From CodeCodex

#   creating a chart in excel
require 'win32ole'

#   set some parameter variables
xlColumns = 2
xlColumnClustered = 51
xlWhite = 2
xlRed = 3
xlBlue = 5
xlGray = 15

#   connect to a running instance of excel
xl = WIN32OLE.connect('Excel.Application')
wb = xl.Workbooks('mlb_stats.xls')

#   delete "MLB Scoring" chart if it already exists
xl.DisplayAlerts = false
    begin
        wb.Charts("MLB Scoring").Delete
    rescue
    end
xl.DisplayAlerts = true

#   create a new chart
mychart = wb.Charts.Add
mychart.Name = "MLB Scoring"
mychart.SetSourceData wb.Worksheets("Runs Scored and Allowed").Range("A1:C15"), xlColumns
mychart.ChartType = xlColumnClustered

#   set series names in the legend
mychart.SeriesCollection(1).Name = "Runs Scored"
mychart.SeriesCollection(2).Name = "Runs Allowed  "

#   set colors
mychart.SeriesCollection(1).Interior.ColorIndex = xlBlue
mychart.SeriesCollection(2).Interior.ColorIndex = xlRed
mychart.ChartArea.Interior.ColorIndex = xlWhite
mychart.ChartArea.Border.ColorIndex = xlBlue
mychart.PlotArea.Interior.ColorIndex = xlGray
mychart.PlotArea.Border.ColorIndex = xlWhite

#   set chart title properties
mychart.HasTitle = true
mychart.ChartTitle.Characters.Text = "American League - Runs Scored vs. Runs Allowed"
mychart.ChartTitle.Font.Name = 'Verdana'
mychart.ChartTitle.Font.Size = 16
mychart.ChartTitle.Font.Bold = true