Chart

Chart is a wrapper for the excellent ChartDirector library. It does not cover all charts; only those I need!

Supported charts: grouped bars

Installation

Installing ChartDirector

  1. Download ChartDirector for Ruby at http://www.advsofteng.com/download.html
  2. Extract the files
  3. Run ruby install.rb

Installing Chart

You can use it as gem or plugin:

sudo gem install fnando-chart --source=http://gems.github.com

or

script/plugin install git://github.com/fnando/chart.git

Usage

Just require the Chart library.

require "chart"

Bars

# set some data
data  = [3600.99, 1479.00, 900.10]

chart = Chart::Bar.new({
  :width => 400,
  :height => 240,
  :data => data,
  :margins   => [50, 25],
  :plot_area => [320, 180],
  :labels => %w(Ruby Python Erlang),
  :multicolor => false
})

chart.write("bar.png")

Result:

Bars chart

Multicolor Bars

# set some data
data  = [3600.99, 1479.00, 900.10]

chart = Chart::Bar.new({
  :width => 400,
  :height => 240,
  :legend => %w(Ruby Python Erlang),
  :data => data,
  :margins   => [50, 25],
  :plot_area => [320, 180],
  :labels => %w(Jan Feb Mar),
  :multicolor => true
})

chart.write("multicolorbar.png")

Result:

Multicolor Bars chart

Grouped Bars

# set some data
in_data  = [3600.99, 1479.00, 900.10]
out_data = [728.77, 356.98, 1100.00]

chart = Chart::GroupedBar.new({
  :width => 400,
  :height => 240,
  :colors => [Chart::GREEN, Chart::RED],
  :legend => %w(In Out),
  :data => [in_data, out_data],
  :margins   => [50, 25],
  :plot_area => [320, 180],
  :labels => %w(Jan Feb Mar)
})

chart.write("groupedbar.png")

Result:

Grouped Bars chart

Pie

# set some data
data    = [1000, 500, 300, 30]

# set the legend
legend  = ["Ruby", "Python", "Erlang", "PHP"]

chart = Chart::Pie.new({
  :width      => 400,
  :height     => 250,
  :data       => data,
  :margins    => [150, 130],
  :legend     => legend,
  :radius     => 80,
  :position   => [310, 80], # [legend_x, legend_y]
  :label_format => "${value|2,.}\n({percent}%)"
})

chart.write("pie.png")

Result:

Pie chart

To hide the pie label, just set the label_format to nil:

# set some data
data    = [1000, 500, 300, 30]

# set the legend
legend  = ["Ruby", "Python", "Erlang", "PHP"]

chart = Chart::Pie.new({
  :width      => 400,
  :height     => 250,
  :data       => data,
  :margins    => [150, 130],
  :legend     => legend,
  :radius     => 80,
  :position   => [310, 80], # [legend_x, legend_y]
  :label_format => nil
})

chart.write("pie_without_labels.png")

Result:

Pie chart without label

Line chart

# set some data
ruby_data   = [42, 49, 33, 38, 51, 46, 29, 41, 44, 57, 59, 52]
python_data = [50, 55, 47, 34, 42, 49, 63, 62, 73, 59, 56, 50]
erlang_data = [36, 28, 25, 33, 38, 20, 22, 30, 25, 33, 30, 24]

chart = Chart::Line.new({
  :width => 400,
  :height => 240,
  :data => [ruby_data, python_data, erlang_data],
  :legend => %w(Ruby Python Erlang),
  :labels => %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec),
  :margins   => [50, 25],
  :plot_area => [320, 180],
  :plot_top_margin => 10,
  # :style => %w(alternate dot dash),
  :line_width => 3,
  # :label_format => "{percent}%",
  :symbols => %w(circle circle circle)
})

chart.write("line.png")

Result:

Line chart

To set the line style, use the style attribute with the values dot, dash, alternate, solid or dotdash:

# set some data
ruby_data   = [42, 49, 33, 38, 51, 46, 29, 41, 44, 57, 59, 52]
python_data = [50, 55, 47, 34, 42, 49, 63, 62, 73, 59, 56, 50]
erlang_data = [36, 28, 25, 33, 38, 20, 22, 30, 25, 33, 30, 24]

chart = Chart::Line.new({
  :width => 400,
  :height => 240,
  :data => [ruby_data, python_data, erlang_data],
  :legend => %w(Ruby Python Erlang),
  :labels => %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec),
  :margins   => [50, 25],
  :plot_area => [320, 180],
  :plot_top_margin => 10,
  :line_width => 3,
  :style => %w(dash dot alternate)
})

Result:

Line chart with line style

To set the point label, use the label_format option or nil to hide it:

chart = Chart::Line.new({
  :width => 400,
  :height => 240,
  :data => [ruby_data],
  :legend => %w(Ruby),
  :labels => %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec),
  :margins   => [50, 25],
  :plot_area => [320, 180],
  :plot_top_margin => 10,
  :line_width => 3,
  :label_format => "{value}"
})

chart.write("line_with_labels.png")

Result:

Line chart with labels

To set the point type, use the symbols option with the values square, diamond, triangle, circle or an image:

chart = Chart::Line.new({
  :width => 400,
  :height => 240,
  :data => [ruby_data],
  :legend => %w(Ruby),
  :labels => %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec),
  :margins   => [50, 25],
  :plot_area => [320, 180],
  :plot_top_margin => 10,
  :line_width => 3,
  :symbols => [File.dirname(__FILE__) + "/user.png"]
})

chart.write("line_with_custom_symbol.png")

Result:

Line chart with custom symbol

MAINTAINER

LICENSE:

(The MIT License)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.