Thursday, October 4, 2012

Generate chart using axlsx gem

Hello Rubies,
        I have generated the graph via axlsx gem of ruby. Follow the easy steps to generate 3D stacked bar graph.

Install axlsx gem via
   gem install axlsx

Lets, generate graph.rb file and than run ruby graph.rb

require "rubygems"
require "axlsx"

 p = Axlsx::Package.new
 wb = p.workbook

 wb.styles do |s|
    wb.add_worksheet(:name => "Bar graph demo") do |sheet|
        sheet.add_row ["A Simple Bar Chart"]
        sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A1", :end_at => "F27", :grouping => :stacked, :show_legend => false, :shape => :box, :barDir => :col) do |chart|
         chart.valAxis.title = "Volumes"
         chart.catAxis.title = "Periods"
         chart.add_series :data => [1,2,3], :labels => ['Mar', 'Apr','May'], :colors => ['92D050', '92D050', '92D050']
         chart.add_series :data => [4,2,6], :labels => ['Mar', 'Apr', 'May'], :colors => ['FFFF00', 'FFFF00','FFFF00']
       end
   end  
end

file = File.open('/home/Desktop/graph.xlsx', 'w')
p.serialize(file)

Lets invoke ruby graph.rb on console and get the stacked bar chart.

1 comment:

  1. Hi,

    It seems LibreOffice has an issue with hard coded data array. "You must use data in your sheet for charts. You cannot use hard coded values.", as quoted from axlsx wiki. And your example is not working with LibreOffice.

    ReplyDelete