Skip to content

Commit

Permalink
FIrst commit
Browse files Browse the repository at this point in the history
  • Loading branch information
matth committed Jun 15, 2010
0 parents commit 2905b56
Show file tree
Hide file tree
Showing 13 changed files with 575 additions and 0 deletions.
126 changes: 126 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Chart Topper

Chart Topper is an extension to the [Sinatra DSL](http://github.com/sinatra/sinatra/) to allow you to quickly and dynamically create image based chart's and graphs.

# myapp.rb
require 'rubygems'
require 'sinatra'
require 'sinatra/chart_topper'

line "A graph of various fruit" do

size "400x225"

data "Apples", [1, 2, 3, 4, 4, 3]
data "Oranges", [4, 8, 7, 9, 8, 9]
data "Watermelon", [2, 3, 1, 5, 6, 8]
data "Peaches", [9, 9, 10, 8, 7, 9]

end

Install the software (for more info see the Installation section) and run with:

ruby mayapp.rb

View your graph at [http://localhost/a\_graph\_of\_various\_fruit.png](http://localhost/a_graph_of_various_fruit.png)

## Installation

Chart Topper is built around [Gruff](http://nubyonrails.com/pages/gruff) which is in turn built around [ImageMagick and RMagick](http://rmagick.rubyforge.org/install-osx.html).

The quickest way to get all the ImageMagick dependancy installed is to use MacPorts (OS X).

sudo port install imagemagick

Then install the gem dependancies as normal:

sudo gem install gruff rmagick

## URLs

Graph URL's are generated from the graph title. Non word characters are stripped, spaces become underscores and the whole thing is lowercased.

# Graph is addressable via /this_weeks_expenditures.png
pie "This week's expenditures" do
...
end

## Graph types

Various Gruff graph types are mapped to new Sinatra DSL methods, for a complete list see Sinatra::ChartTopper.GRAPH_TYPES

The basic rule is that any Gruff graph type can be generated via it's lowercased and underscore separated equivalent.

# Create a graph of type Gruff::AccumulatorBar
accumulator_bar "My accumulator bar graph" do
...
end

## The Gruff graph object and delegated methods

In order to make the DSL a bit nicer to use number of methods are delegated to the Gruff graph object. These can be found in Sinatra::ChartTopper.GRAPH_DELEGATIONS

dot "My dot graph" do
# Delegated to Gruff::Base.data
data "Tuesday", [2,4,6,8]
end

Direct access to the current Gruff graph object can be gained via the `graph()` method

spider "My spider chart" do
graph.theme = {
:colors => %w(orange purple green white red),
:marker_color => 'blue',
:background_colors => %w(black grey)
}
end

## Options

You can pass through options to the graph methods. Currently the only available option is `prefix` though. This will add a prefix to the generated URL.

# /my/prefix/my_graph.png
bar "My Graph", :prefix => "/my/prefix/" do
...
end

## Modular style apps

You can use the extension in modular style apps too ...

require 'sinatra/base'
require 'sinatra/chart_topper'

class MyApp < Sinatra::Base

register Sinatra::ChartTopper
bar "My Graph" do
...
end
end

## Examples

It's probably a good place to start, check out the examples directory.

## License

License:

(GPLv3)

Copyright (C) 2009 Matt Haynes

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <www.gnu.org/licenses/>






15 changes: 15 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace :test do

base = File.dirname(__FILE__)

task :all => [:cucumber, :spec]

task :spec do
sh "spec #{base}/test"
end

task :cucumber do
sh "cucumber #{base}/features"
end

end
1 change: 1 addition & 0 deletions chart_topper.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO!
32 changes: 32 additions & 0 deletions examples/modular.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require "rubygems"
require 'sinatra/base'
require File.dirname(__FILE__) + "/../lib/sinatra/chart_topper.rb"

class MyApp < Sinatra::Base

register Sinatra::ChartTopper

bar "My Bar Chart" do

size "400x225"

data "Apples", 40
data "Oranges", 50
data "Watermelon", 30
data "Peaches", 60

end

pie "My Pie Chart" do

size "400x225"

data "Apples", 40
data "Oranges", 50
data "Watermelon", 30
data "Peaches", 60

end


end
15 changes: 15 additions & 0 deletions examples/options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "rubygems"
require "sinatra"
require File.dirname(__FILE__) + "/../lib/sinatra/chart_topper.rb"

# BAR CHART WITH PREFIX /my/prefix/my_graph.png
bar "My Graph", :prefix => "/my/prefix/" do

size "400x225"

data "Apples", 40
data "Oranges", 50
data "Watermelon", 30
data "Peaches", 60

end
154 changes: 154 additions & 0 deletions examples/simple.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
require "rubygems"
require "sinatra"
require File.dirname(__FILE__) + "/../lib/sinatra/chart_topper.rb"

# ACCUMULATOR BAR CHART /a_simple_accumulator_bar_chart.png
accumulator_bar "A simple accumulator bar chart" do

size "400x225"

data "Apples", [40, 30, 20, 50, 70]

end

# AREA CHART /a_simple_area_chart.png
area "A simple area chart" do

size "400x225"

datasets = [
[:Jimmy, [25, 36, 86, 39, 25, 31, 79, 88]],
[:Charles, [80, 54, 67, 54, 68, 70, 90, 95]],
[:Julie, [22, 29, 35, 38, 36, 40, 46, 57]],
[:Jane, [95, 95, 95, 90, 85, 80, 88, 100]],
[:Philip, [90, 34, 23, 12, 78, 89, 98, 88]],
["Arthur", [5, 10, 13, 11, 6, 16, 22, 32]],
]

sample_labels = {
0 => '5/6',
1 => '5/15',
2 => '5/24',
3 => '5/30',
4 => '6/4',
5 => '6/12',
6 => '6/21',
7 => '6/28',
}

graph.labels = {
0 => '5/6',
2 => '5/15',
4 => '5/24',
6 => '5/30',
}

datasets.each do |data|
graph.data(data[0], data[1])
end

end

# BAR CHART /a_simple_bar_chart.png
bar "A simple bar chart" do

size "400x225"

data "Apples", 40
data "Oranges", 50
data "Watermelon", 30
data "Peaches", 60

end

# DOT CHART /a_simple_dot_chart.png
dot "A simple dot chart" do

size "400x225"

graph.labels = {
0 => '5/6',
1 => '5/15',
2 => '5/24',
3 => '5/30'
}

data :Art, [0, 5, 8, 15], '#990000'
data :Philosophy, [10, 3, 2, 8], '#009900'
data :Science, [2, 15, 8, 11], '#990099'

end

# LINE CHART /a_simple_line_chart.png
line "A simple line chart" do

size "400x225"

data "Apples", [1, 2, 3, 4, 4, 3]
data "Oranges", [4, 8, 7, 9, 8, 9]
data "Watermelon", [2, 3, 1, 5, 6, 8]
data "Peaches", [9, 9, 10, 8, 7, 9]

end

# PIE CHART /a_simple_pie_chart.png
pie "A simple pie chart" do

size "400x225"

data "Apples", 40
data "Oranges", 50
data "Watermelon", 30
data "Peaches", 60

end

# STACKED BAR CHART /a_simple_stacked_bar_chart.png
stacked_bar "A simple stacked bar chart" do

size "400x225"

data "Apples", [1, 2, 3, 4, 4, 3]
data "Oranges", [4, 8, 7, 9, 8, 9]
data "Watermelon", [2, 3, 1, 5, 6, 8]
data "Peaches", [9, 9, 10, 8, 7, 9]

end

# SIDE STACKED BAR CHART /a_simple_side_stacked_bar_chart.png
side_stacked_bar "A simple side stacked bar chart" do

size "400x225"

graph.labels = {
0 => '5/6',
1 => '5/15',
2 => '5/24',
3 => '5/30'
}

data "Apples", [1, 2, 3, 4]
data "Oranges", [4, 8, 7, 9]
data "Watermelon", [2, 3, 1, 5]
data "Peaches", [9, 9, 10, 9]

end

# SIDE AREA CHART /a_simple_stacked_area_chart.png
stacked_area "A simple stacked area chart" do

size "400x225"

graph.labels = {
0 => '5/6',
1 => '5/15',
2 => '5/24',
3 => '5/30'
}

data "Apples", [1, 2, 3, 4]
data "Oranges", [4, 8, 7, 9]
data "Watermelon", [2, 3, 1, 5]
data "Peaches", [9, 9, 10, 9]

end
16 changes: 16 additions & 0 deletions features/modular.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@modular
Feature: Be able to use the extension in a modular app
In order to have a useful piece of software
As a developer
I need to be able to send Gruff graphs out via Sinatra

Scenario Outline: Send out a simple gruff graph
Given I am have loaded the modular app
When I visit /my_<type>_chart.png
Then I should get a 200 response
And I should see a png graph 400x225 in size
Examples:
| type |
| bar |
| pie |

11 changes: 11 additions & 0 deletions features/options.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@options
Feature: Be able to set some options by default
In order to have a useful piece of software
As a developer
I need to be able to set various options for my graphs

Scenario: Set a prefix for all URLs
Given I am have loaded the options.rb example
When I visit /my/prefix/my_graph.png
Then I should get a 200 response
And I should see a png graph 400x225 in size
Loading

0 comments on commit 2905b56

Please sign in to comment.