• This repository has been archived on 02/Jan/2018
  • Stars
    star
    700
  • Rank 62,105 (Top 2 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 16 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ruby Google Chart API

Googlecharts

Build Status

The goal of this Gem is to make the creation of Google Charts a simple and easy task.

require 'googlecharts'
Gchart.line(  :size => '200x300', 
              :title => "example title",
              :bg => 'efefef',
              :legend => ['first data set label', 'second data set label'],
              :data => [10, 30, 120, 45, 72])

Check out the full documentation over there

This gem is fully tested using Rspec, check the rspec folder for more examples.

See at the bottom of this file who reported using this gem.

Chart Type

This gem supports the following types of charts:

  • line,
  • line_xy
  • sparkline
  • scatter
  • bar
  • venn
  • pie
  • pie_3d
  • google meter

Googlecharts also supports graphical themes and you can easily load your own.

To create a chart, simply require Gchart and call any of the existing type:

require 'gchart'
Gchart.pie

Chart Title

To add a title to a chart pass the title to your chart:

Gchart.line(:title => 'Sexy Charts!')

You can also specify the color and/or size

Gchart.line(:title => 'Sexy Charts!', :title_color => 'FF0000', :title_size => '20')

Colors

Specify a color with at least a 6-letter string of hexadecimal values in the format RRGGBB. For example:

* FF0000 = red
* 00FF00 = green
* 0000FF = blue
* 000000 = black
* FFFFFF = white

You can optionally specify transparency by appending a value between 00 and FF where 00 is completely transparent and FF completely opaque. For example:

* 0000FFFF = solid blue
* 0000FF00 = transparent blue

If you need to use multiple colors, check the doc. Usually you just need to pass :attribute => 'FF0000,00FF00'

Some charts have more options than other, make sure to refer to the documentation.

Background options:

If you don't set the background option, your graph will be transparent.

  • solid
  • gradient
  • stripes

By default, if you set a background color, the fill will be solid:

Gchart.bar(:bg => 'efefef')

However you can specify another fill type such as:

Gchart.line(:bg => {:color => 'efefef', :type => 'gradient'})

In the above code, we decided to have a gradient background, however since we only passed one color, the chart will start by the specified color and transition to white. By the default, the gradient angle is 0. Change it as follows:

Gchart.line(:title =>'bg example', :bg => {:color => 'efefef', :type => 'gradient', :angle => 90})

For a more advance use of colors, refer to http://code.google.com/apis/chart/#linear_gradient

Gchart.line(:bg => {:color => '76A4FB,1,ffffff,0', :type => 'gradient'})

The same way you set the background color, you can also set the graph background:

Gchart.line(:graph_bg => 'cccccc')

or both

Gchart.line(:bg => {:color => '76A4FB,1,ffffff,0', :type => 'gradient'}, :graph_bg => 'cccccc', :title => 'Sexy Chart')

Another type of fill is stripes http://code.google.com/apis/chart/#linear_stripes

Gchart.line(:bg => {:color => 'efefef', :type => 'stripes'})

You can customize the amount of stripes, colors and width by changing the color value.

Themes

Googlecharts comes with 4 themes: keynote, thirty7signals, pastel and greyscale. (ganked from Gruff

Gchart.line(
            :theme => :keynote, 
            :data => [[0,40,10,70,20],[41,10,80,50,40],[20,60,30,60,80],[5,23,35,10,56],[80,90,5,30,60]], 
            :title => 'keynote'
            )
  • keynote

    keynote

  • thirty7signals

    37signals

  • pastel

    pastel

  • greyscale

    greyscale

You can also use your own theme. Create a yml file using the same format as the themes located in lib/themes.yml

Load your theme(s):

  Chart::Theme.add_theme_file("#{File.dirname(__FILE__)}/fixtures/another_test_theme.yml")

And use the standard method signature to use your own theme:

  Gchart.line(:theme => :custom_theme, :data => [[0, 40, 10, 70, 20],[41, 10, 80, 50]], :title => 'greyscale')

Legend & Labels

You probably will want to use a legend or labels for your graph.

Gchart.line(:legend => 'legend label')

or Gchart.line(:legend => ['legend label 1', 'legend label 2'])

Will do the trick. You can also use the labels alias (makes more sense when using the pie charts)

chart = Gchart.pie(:labels => ['label 1', 'label 2'])

Multiple axis labels

Multiple axis labels are available for line charts, bar charts and scatter plots.

  • x = bottom x-axis

  • t = top x-axis

  • y = left y-axis

  • r = right y-axis

    Gchart.line(:axis_with_label => 'x,y,r,t')

To add labels on these axis:

Gchart.line(:axis_with_label => 'x,y,r,t',
            :axis_labels => ['Jan|July|Jan|July|Jan', '0|100', 'A|B|C', '2005|2006|2007'])

Note that each array entry could also be an array but represent the labels for the corresponding axis.

A question which comes back often is how do I only display the y axis label? Solution:

Gchart.line(
        :data => [0,20, 40, 60, 140, 230, 60],
        :axis_with_labels => 'y')

Custom axis ranges

If you want to display a custom range for an axis, you need to set the range as described in the Google charts documentation: min, max, step:

 Gchart.line( :data => [17, 17, 11, 8, 2], 
              :axis_with_labels => ['x', 'y'], 
              :axis_labels => [['J', 'F', 'M', 'A', 'M']], 
              :axis_range => [nil, [2,17,5]])

In this case, the custom axis range is only defined for y (second entry) with a minimum value of 2, max 17 and a step of 5.

This is also valid if you want to set a x axis and automatically define the y labels.

Data options

Data are passed using an array or a nested array.

Gchart.bar(:data => [1,2,4,67,100,41,234])  

Gchart.bar(:data => [[1,2,4,67,100,41,234],[45,23,67,12,67,300, 250]])

By default, the graph is drawn with your max value representing 100% of the height or width of the graph. You can change that my passing the max value.

Gchart.bar(:data => [1,2,4,67,100,41,234], :max_value => 300)
Gchart.bar(:data => [1,2,4,67,100,41,234], :max_value => 'auto')

or if you want to use the real values from your dataset:

Gchart.bar(:data => [1,2,4,67,100,41,234], :max_value => false)

You can also define a different encoding to add more granularity:

Gchart.bar(:data => [1,2,4,67,100,41,234], :encoding => 'simple') 
Gchart.bar(:data => [1,2,4,67,100,41,234], :encoding => 'extended') 
Gchart.bar(:data => [1,2,4,67,100,41,234], :encoding => 'text') 

Pies:

you have 2 type of pies:

  • Gchart.pie() the standard 2D pie _ Gchart.pie_3d() the fancy 3D pie

To set labels, you can use one of these two options:

@legend = ['Matt_fu', 'Rob_fu']
Gchart.pie_3d(:title => @title, :labels => @legend, :data => @data, :size => '400x200')
Gchart.pie_3d(:title => @title, :legend => @legend, :data => @data, :size => '400x200')

Bars:

A bar chart can accept options to set the width of the bars, spacing between bars and spacing between bar groups. To set these, you can either provide a string, array or hash.

The Google API sets these options in the order of width, spacing, and group spacing, with both spacing values being optional. So, if you provide a string or array, provide them in that order:

Gchart.bar(:data => @data, :bar_width_and_spacing => '25,6') # width of 25, spacing of 6
Gchart.bar(:data => @data, :bar_width_and_spacing => '25,6,12') # width of 25, spacing of 6, group spacing of 12
Gchart.bar(:data => @data, :bar_width_and_spacing => [25,6]) # width of 25, spacing of 6
Gchart.bar(:data => @data, :bar_width_and_spacing => 25) # width of 25

The hash lets you set these values directly, with the Google default values set for any options you don't include:

Gchart.bar(:data => @data, :bar_width_and_spacing => {:width => 19})
Gchart.bar(:data => @data, :bar_width_and_spacing => {:spacing => 10, :group_spacing => 12})

Radar:

In a Radar graph, the x-axis is circular. The points can be connected by straight lines or curved lines.
Gchart.radar(:data => @data, :curved => true)

Sparklines:

A sparkline chart has exactly the same parameters as a line chart. The only difference is that the axes lines are not drawn for sparklines by default.

Google-o-meter

A Google-o-meter has a few restrictions. It may only use a solid filled background and it may only have one label.

Record Chart PNG file in filesystem Sample :

Multi Lines Chart Sample :

chart = Gchart.new(	:type => 'line',
					:title => "example title",
					:theme => :keynote,
					:data => [[17, 17, 11, 8, 2],[10, 20, 15, 5, 7],[2, 3, 7, 9, 12]], 
					:line_colors => 'e0440e,e62ae5,287eec',
					:legend => ['courbe 1','courbe 2','courbe 3'],
					:axis_with_labels => ['x', 'y'], 
					:axis_range => [[0,100,20], [0,20,5]],
					:filename => "tmp/chart.png")
		
# Record file in filesystem
chart.file

try yourself

Gchart.bar( :data => [[1,2,4,67,100,41,234],[45,23,67,12,67,300, 250]], 
            :title => 'SD Ruby Fu level', 
            :legend => ['matt','patrick'], 
            :bg => {:color => '76A4FB', :type => 'gradient'}, 
            :bar_colors => 'ff0000,00ff00')

"http://chart.apis.google.com/chart?chs=300x200&chdl=matt|patrick&chd=s:AAANUIv,JENCN9y&chtt=SDRuby+Fu+level&chf=bg,lg,0,76A4FB,0,ffffff,1&cht=bvs&chco=ff0000,00ff00"

Gchart.pie(:data => [20,10,15,5,50], :title => 'SDRuby Fu level', :size => '400x200', :labels => ['matt', 'rob', 'patrick', 'ryan', 'jordan'])

http://chart.apis.google.com/chart?cht=p&chs=400x200&chd=s:YMSG9&chtt=SDRuby+Fu+level&chl=matt|rob|patrick|ryan|jordan

More Repositories

1

Weasel-Diesel

DSL to describe, document and test web services
Ruby
439
star
2

merb-book

Open Source Merb book
Ruby
186
star
3

acts_as_taggable_on_steroids

(OBSOLETE) This plugin is based on acts_as_taggable by DHH but includes extras
Ruby
152
star
4

filebuffer

filebuffer is a package implementing a few file-like interfaces. The implementation is backed by a byte buffer. The main purpose is to have in-memory file alternative.
Go
142
star
5

sinatra-web-api-example

Example application using Sinatra and the DSL gem to develop web APIs using a simple DSL and a small stack.
JavaScript
121
star
6

MacRuby--The-Definitive-Guide

MacRuby: The Definitive Guide - code repo
Ruby
119
star
7

goRailsYourself

A suite of useful functions needed when porting/mixing Go/Rails code.
Go
94
star
8

mimetype-fu

get the mimetype of a file directly in Ruby
Ruby
83
star
9

wd-sinatra

Weasel-Diesel Sinatra app gem, allowing you to generate/update sinatra apps using the Weasel Diesel DSL
Ruby
75
star
10

audio

This is a playground, production ready code is available at https://github.com/go-audio/
Go
74
star
11

goHtmlTemplateExample

Example setting up a Golang web server with static files, HTML templates and Angular.js
Go
67
star
12

globalite

Globalite is meant to be a breed of the best i18n /l10n plugins available for Rails.
Ruby
60
star
13

m3u8Grabber

Experimental command line tool downloading the content of a m3u8 file containing TS video segments and converting them to mkv.
Go
58
star
14

macruby_graphics

Cocoa's Core Graphics & Core Image wrapper for MacRuby (see original author's branch in the link below)
Ruby
57
star
15

ok-go

Google Assistant SDK in Go
Go
57
star
16

GC-stats-middleware

Ruby GC Stats after each request (rack middleware)
50
star
17

fire-and-forget

[Not maintained] Very dumb HTTP "client" that fires requests and doesn't care about the response.
Ruby
44
star
18

ruby-web-search

A Ruby gem that provides a way to retrieve search results via the main search engines using Ruby
Ruby
44
star
19

i18n

Basic internationalization(i18n) library for Ruby
Ruby
36
star
20

LiveTV

Simple OS X application allowing you to watch some live free TV channels (French, English, Italian).
Objective-C
33
star
21

macruby-httpwrapper

simplified http wrapper for MacRuby/RubyMotion using delegation/ruby block
Ruby
29
star
22

phileas_frog

MacRuby video game using Cocoa's CoreAnimation
Ruby
27
star
23

waveform_demo

Flutter Waveform drawing demo repo
Dart
24
star
24

go-web-api-demo

Example implementation of a web API in Go
Go
22
star
25

sm-808

Programming exercise - code the sequencer part of a Drum Machine
21
star
26

merb_babel

Merb Babel is a dead simple translation/localization tool for Merb
Ruby
19
star
27

pluzzdl

My own fork of pluzzdl
Python
17
star
28

noko-vs-builder-benchmark

Rails app to benchmark Nokogiri's XML builder vs Builder
Ruby
15
star
29

WSDSL

Due to a confusing name, this project was renamed and moved to:
Ruby
14
star
30

macruby-json

reimplementation of the ruby json gem for macruby using objc
Ruby
13
star
31

ar-backup

Active Record backup is a Rails plugin which lets you backup your database schema and content in a schema.rb file and fixtures.
Ruby
13
star
32

MacRuby-Siri

Simple demo showing how to use the voice recognition feature of OS X to implement a Siri-like app in MacRuby,
Ruby
13
star
33

macruby-doc-app

simple experiment to query the cocoa and eventually the macruby doc
Ruby
13
star
34

globalite-example

Sample app showing how to implement GlobaLite in a Rails app
Ruby
11
star
35

merb-static-pages-slice

Use markdown files to store and serve static pages (about us, contact us, etc)
Ruby
11
star
36

multibyte

Ruby Multibyte library extracted from ActiveSupport
Ruby
10
star
37

Pvwatts

Wrapper around the http://www.nrel.gov/rredc/pvwatts/ web service API
Ruby
10
star
38

sinatra-wsdsl-example

moved to https://github.com/mattetti/sinatra-web-api-example
JavaScript
10
star
39

TVFrancaise

Simple OS X application allowing you to watch most of the free French TV channels live. (works overseas)
Ruby
10
star
40

apple-remote-wrapper

fork of Martin Kahr work on an Apple Remote Wrapper
Objective-C
8
star
41

IOLI-crackme

crackme exercises with instructions to learn in a safe environment
C++
7
star
42

monkey_patcher

keep track of your monkey patches
7
star
43

google-speech

Fun with Google Cloud Platform speech recognition API
Go
7
star
44

simple_merb_example_app

simple app example, look at the branches to see how to build the app
Ruby
6
star
45

fasthttp

FastHTTP is a Ruby library suitable for use as a drop-in Net::HTTP replacement or with event frameworks like EventMachine and Rev
C
6
star
46

swx-ruby

Ruby implementation of SWX RPC
Ruby
6
star
47

cocoa

Pure Go reimplementation of some Cocoa specific features.
Go
6
star
48

dm-gvideo-adapter

DataMapper adapter for google video
Ruby
6
star
49

merb_training_site

5
star
50

Box2d

My Box2d fork which has the iPhone TestBed compiling under Xcode4
C++
5
star
51

hotocoa-roundtransparentwindow

HotCocoa(MacRuby) port of Apple's RoundTransparentWindow sample using a Nib
5
star
52

MacRuby-Chipmunk

Cocoa Framework designed to use Chipmunk Physics with MacRuby
C
5
star
53

MrStuff

MacRuby experimental wrappers
Ruby
5
star
54

merb-misc

misc stuff related to Merb
Ruby
5
star
55

herbs

Hopefully Exhaustive Ruby Benchmark Suite
Ruby
5
star
56

RubyConfX-code

The code used for my MacRuby talk at RubyConf X | demos: iVoiceControl (voice recognition), PS3 controller + MacRuby + HTML5, tokenization, headless browser and gowalla sample.
JavaScript
5
star
57

sdruby-raffle

Tiny Ruby gem to pick raffle winners
Ruby
4
star
58

merb_latest_rss_items_slice

Just a test Merb Slice displaying the latest RSS items from a feed
Ruby
4
star
59

PS3SixAxis

Use a PS3 Dualshock 3 Controller with Cocoa
JavaScript
4
star
60

plex-web-client

JavaScript
4
star
61

drumbeat

Drum beat is a Go library to handle drum beat patterns
Go
4
star
62

ma-agile

CSS
4
star
63

learning_scala

Misc stuff you might care about when learning scala
Scala
4
star
64

uuid

Go UUID package - use at your own risk
Go
4
star
65

artist_portfolio

Shoes app displaying an artist portfolio using Flickr as a source for content
Ruby
4
star
66

phare

native osx client for lighthouseapp.com
Ruby
4
star
67

mattetti.github.com.old

Matt Aimonetti's home page
JavaScript
4
star
68

couchrest-app

CouchApp for CouchRest logger middleware. Browse and analyze your logs directly from Couch
JavaScript
3
star
69

cbc

CBC/radio-canada has great content and in some cases, one might be interested in keeping a backup
Go
3
star
70

MacRuby-NSCollectionView

example for @tronathan
Ruby
3
star
71

bottetti

my irc bot
CoffeeScript
3
star
72

merb-book-tmbundle

A TextMate bundle for the merb-book bundle
3
star
73

scrapbook

Experiment designed to reflect on the organization of web scraping/data processing.
Ruby
3
star
74

go-exercises

Hopefully fun exercises in Golang. Each exercise has a test suite and your mission to make it pass!
Go
3
star
75

merb-doc

Stuff to generate docs for Merb outside of an app
Ruby
3
star
76

macruby-raffle

raffle app
Ruby
2
star
77

pastis

fresh and relaxing experimentation
Ruby
2
star
78

macruby-roundtransparentwindow

MacRuby port of Apple's RoundTransparentWindow sample
Ruby
2
star
79

sandbox

testing different things, nothing very interesting
Ruby
2
star
80

pubnub-publisher

A Ruby publisher gem for PubNub (for those who only care about publishing from Ruby)
Ruby
2
star
81

misc-Python-experiments

nothing valuable in here, just me playing with Python
Python
2
star
82

dm-websearch-adapter

2
star
83

ruby_practice

series of random exercises to practice Ruby
Ruby
2
star
84

MacRuby-ScriptingBridge-Browser

A ScriptingBridge browser for MacRuby, quite like AppleScript Editor's dictionary browser.
Objective-C
2
star
85

sigen

signature generator written in Ruby and using RMagick
Ruby
2
star
86

merbthor

2
star
87

UnityAzureSpeechSynthesis

Quick demo showing how to use Azure Speech Synthesis (tts) from Unity and loading/playing the generated file in game.
ShaderLab
2
star
88

gvideo

retrieve a google user's list of videos using his user id.
Ruby
2
star
89

LCFF

Luca's Cow Fart Filter
Go
2
star
90

http-proxy-experiment

Simple http proxy server written in Go to learn the language. The goal of this server is to proxy some defined websites with full cookie support.
Go
1
star
91

ruby-exercises

simple katas and exercises prepared for SDRuby meetups.
Ruby
1
star
92

bugsnag-go

bugsnag.com API client in Go
Go
1
star
93

paca

Selenium IDE test case converter to Go tests to be run via Agouti
Go
1
star
94

twitter_game_bot

A twitter bot
Ruby
1
star
95

revel_addons

Series of packages for the Revel Go Framework
Go
1
star
96

s2s-auth

s2s auth gem based on ActiveSupport's crypto
Ruby
1
star
97

wd_sinatra_active_record

ActiveRecord helper for Weasel Diesel Sinatra apps. See https://github.com/mattetti/wd-sinatra
Ruby
1
star
98

Andromeda

The goal is to provide a highly concurrent public facing API that dispatches requests to other APIs via different protocols (HTTP, Thrift..)
Scala
1
star
99

webserver-test-plan

Shell
1
star
100

macruby-twitter_engine

MGTwitterEngine package for MacRuby/HotCocoa
Objective-C
1
star