drawr 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/Manifest.txt +7 -0
- data/README.txt +76 -0
- data/Rakefile +15 -0
- data/bin/drawr +0 -0
- data/lib/drawr.rb +119 -0
- data/test/test_drawr.rb +65 -0
- metadata +58 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
drawr
|
2
|
+
by Aaron Patterson
|
3
|
+
http://seattlerb.org/
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
This is a ruby wrapper around Plotr with a similar API to Gruff. You can
|
8
|
+
create graphs with a similar interface to Gruff, but offload the rendering
|
9
|
+
to the browser!
|
10
|
+
|
11
|
+
== FEATURES/PROBLEMS:
|
12
|
+
|
13
|
+
* Needs more tests!
|
14
|
+
|
15
|
+
== SYNOPSIS:
|
16
|
+
|
17
|
+
An example in rails. Your controller:
|
18
|
+
|
19
|
+
class GraphController < ApplicationController
|
20
|
+
def index
|
21
|
+
@drawr = Drawr::Pie.new
|
22
|
+
@drawr.title = "Twan"
|
23
|
+
@drawr.data("One", [1])
|
24
|
+
@drawr.data('Two', [2])
|
25
|
+
@drawr.data('Three', [2])
|
26
|
+
@drawr.data('Four', [10])
|
27
|
+
@drawr.data('Five', [6])
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
Your view:
|
32
|
+
|
33
|
+
<html>
|
34
|
+
<head>
|
35
|
+
<%= javascript_include_tag 'prototype' %>
|
36
|
+
<%= javascript_include_tag 'excanvas' %>
|
37
|
+
<%= javascript_include_tag 'Plotr' %>
|
38
|
+
</head>
|
39
|
+
<body>
|
40
|
+
<%= @drawr %>
|
41
|
+
</body>
|
42
|
+
</html>
|
43
|
+
|
44
|
+
== REQUIREMENTS:
|
45
|
+
|
46
|
+
* Plotr[http://www.solutoire.com/plotr]
|
47
|
+
|
48
|
+
== INSTALL:
|
49
|
+
|
50
|
+
* sudo gem install drawr
|
51
|
+
* require drawr in your environment.rb
|
52
|
+
|
53
|
+
== LICENSE:
|
54
|
+
|
55
|
+
(The MIT License)
|
56
|
+
|
57
|
+
Copyright (c) 2007 Aaron Patterson
|
58
|
+
|
59
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
60
|
+
a copy of this software and associated documentation files (the
|
61
|
+
'Software'), to deal in the Software without restriction, including
|
62
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
63
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
64
|
+
permit persons to whom the Software is furnished to do so, subject to
|
65
|
+
the following conditions:
|
66
|
+
|
67
|
+
The above copyright notice and this permission notice shall be
|
68
|
+
included in all copies or substantial portions of the Software.
|
69
|
+
|
70
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
71
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
72
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
73
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
74
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
75
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
76
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
require './lib/drawr.rb'
|
6
|
+
|
7
|
+
Hoe.new('drawr', Drawr::VERSION) do |p|
|
8
|
+
p.rubyforge_name = 'seattlerb'
|
9
|
+
# p.summary = 'FIX'
|
10
|
+
# p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
|
11
|
+
# p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
|
12
|
+
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
|
13
|
+
end
|
14
|
+
|
15
|
+
# vim: syntax=Ruby
|
data/bin/drawr
ADDED
File without changes
|
data/lib/drawr.rb
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
module Drawr
|
2
|
+
VERSION = '1.0.0'
|
3
|
+
class Base
|
4
|
+
attr_accessor :title
|
5
|
+
attr_accessor :hide_dots
|
6
|
+
attr_accessor :div
|
7
|
+
attr_accessor :theme
|
8
|
+
|
9
|
+
# A hash of names for the individual columns, where the key is the array
|
10
|
+
# index for the column this label represents.
|
11
|
+
attr_accessor :labels
|
12
|
+
|
13
|
+
def data(name, data_points = [], color = nil)
|
14
|
+
@data_points[name] = data_points
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize(target_width = 800)
|
18
|
+
@data_points = {}
|
19
|
+
@theme = {
|
20
|
+
:background_color => '#dbdbdb',
|
21
|
+
:color_scheme => 'red',
|
22
|
+
:padding => 'left: 30, right: 0, top: 10, bottom: 30',
|
23
|
+
}
|
24
|
+
@extra_options = {}
|
25
|
+
if target_width.is_a? String
|
26
|
+
@width, @height = target_width.split(/x/, 2)
|
27
|
+
else
|
28
|
+
@width, @height = target_width, (target_width * (3.0/4.0))
|
29
|
+
end
|
30
|
+
@div = 'plotr'
|
31
|
+
end
|
32
|
+
|
33
|
+
def write_to_div(div)
|
34
|
+
@div = div
|
35
|
+
to_s
|
36
|
+
end
|
37
|
+
|
38
|
+
def write(file); File.open(file, 'wb') { |f| f.write to_s }; end
|
39
|
+
|
40
|
+
def to_s
|
41
|
+
<<END
|
42
|
+
<div><canvas id="#{div}" height="#{@height.to_i}" width="#{@width.to_i}"></canvas></div>
|
43
|
+
<script>#{dataset}\n#{options}\n#{commands}</script>
|
44
|
+
END
|
45
|
+
end
|
46
|
+
|
47
|
+
protected
|
48
|
+
def dataset
|
49
|
+
"var dataset = {\n" +
|
50
|
+
@data_points.map { |k, v|
|
51
|
+
values = []
|
52
|
+
v.each_with_index { |value, i| values << "[#{i}, #{value}]" }
|
53
|
+
"'#{k.to_s.gsub(/'/,"\\\\'")}': [#{values.join(', ')}]"
|
54
|
+
}.join(",\n") + "\n};\n"
|
55
|
+
end
|
56
|
+
|
57
|
+
def options
|
58
|
+
opts = @extra_options.map { |k,v| "#{k}: #{v}" }
|
59
|
+
opts << "padding: { #{@theme[:padding]} }"
|
60
|
+
opts << "backgroundColor: '#{@theme[:background_color]}'"
|
61
|
+
opts << "colorScheme: '#{@theme[:color_scheme]}'"
|
62
|
+
|
63
|
+
"var options = {\n" +
|
64
|
+
opts.join(",\n") + ",\n" +
|
65
|
+
"xTicks: [\n" +
|
66
|
+
labels.keys.sort.map { |k|
|
67
|
+
"{v:#{k}, label:'#{labels[k].to_s.gsub(/'/, "\\\\'")}'}"
|
68
|
+
}.join(",\n") +
|
69
|
+
"\n]\n};\n"
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
def commands
|
74
|
+
"var chart = new Plotr.#{class_name}('#{div}', options);\n" +
|
75
|
+
"chart.addDataset(dataset);\nchart.render();\n"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
class Pie < Base
|
80
|
+
def initialize(target_width = 800)
|
81
|
+
super(target_width)
|
82
|
+
@extra_options[:strokewidth] = 1
|
83
|
+
end
|
84
|
+
|
85
|
+
def data(name, data_points = [], color = nil)
|
86
|
+
@labels ||= {}
|
87
|
+
@labels[@labels.length] = name
|
88
|
+
super(name, data_points, color)
|
89
|
+
end
|
90
|
+
|
91
|
+
protected
|
92
|
+
def dataset
|
93
|
+
<<END
|
94
|
+
var dataset = {
|
95
|
+
'#{title.to_s.gsub(/'/, "\\\\'")}': [#{@labels.map { |k,v| "[#{k}, #{@data_points[v].first}]"
|
96
|
+
}.join(', ')}]
|
97
|
+
};
|
98
|
+
END
|
99
|
+
end
|
100
|
+
|
101
|
+
def class_name
|
102
|
+
'PieChart'
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
class Line < Base
|
107
|
+
protected
|
108
|
+
def class_name
|
109
|
+
'LineChart'
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
class Bar < Base
|
114
|
+
protected
|
115
|
+
def class_name
|
116
|
+
'BarChart'
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
data/test/test_drawr.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'drawr'
|
3
|
+
|
4
|
+
class TestDrawr < Test::Unit::TestCase
|
5
|
+
def test_bar
|
6
|
+
drawr = Drawr::Bar.new(400)
|
7
|
+
drawr.title = 'Awesomeness'
|
8
|
+
drawr.data(:One, [1,2,3])
|
9
|
+
drawr.data('Two', [2,3,1])
|
10
|
+
drawr.data(:Three, [3,1,2])
|
11
|
+
drawr.labels = {
|
12
|
+
0 => 'one',
|
13
|
+
1 => 'two',
|
14
|
+
2 => 'three',
|
15
|
+
}
|
16
|
+
value = drawr.to_s
|
17
|
+
%w{ one two three }.each_with_index do |v,i|
|
18
|
+
assert(value =~ /v:#{i}, label:'#{v}'/)
|
19
|
+
end
|
20
|
+
assert(value =~ /Plotr.BarChart/)
|
21
|
+
assert(value =~ /height="300"/)
|
22
|
+
assert(value =~ /width="400"/)
|
23
|
+
assert(value =~ /'One': \[\[0, 1\], \[1, 2\], \[2, 3\]\]/)
|
24
|
+
assert(value =~ /'Two': \[\[0, 2\], \[1, 3\], \[2, 1\]\]/)
|
25
|
+
assert(value =~ /'Three': \[\[0, 3\], \[1, 1\], \[2, 2\]\]/)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_line
|
29
|
+
drawr = Drawr::Line.new(400)
|
30
|
+
drawr.title = 'Awesomeness'
|
31
|
+
drawr.data(:One, [1,2,3])
|
32
|
+
drawr.data('Two', [2,3,1])
|
33
|
+
drawr.data(:Three, [3,1,2])
|
34
|
+
drawr.labels = {
|
35
|
+
0 => 'one',
|
36
|
+
1 => 'two',
|
37
|
+
2 => 'three',
|
38
|
+
}
|
39
|
+
value = drawr.to_s
|
40
|
+
%w{ one two three }.each_with_index do |v,i|
|
41
|
+
assert(value =~ /v:#{i}, label:'#{v}'/)
|
42
|
+
end
|
43
|
+
assert(value =~ /Plotr.LineChart/)
|
44
|
+
assert(value =~ /height="300"/)
|
45
|
+
assert(value =~ /width="400"/)
|
46
|
+
assert(value =~ /'One': \[\[0, 1\], \[1, 2\], \[2, 3\]\]/)
|
47
|
+
assert(value =~ /'Two': \[\[0, 2\], \[1, 3\], \[2, 1\]\]/)
|
48
|
+
assert(value =~ /'Three': \[\[0, 3\], \[1, 1\], \[2, 2\]\]/)
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_pie
|
52
|
+
drawr = Drawr::Pie.new
|
53
|
+
drawr.title = 'Awesomeness'
|
54
|
+
drawr.data(:One, [1])
|
55
|
+
drawr.data('Two', [2])
|
56
|
+
drawr.data(:Three, [2])
|
57
|
+
|
58
|
+
value = drawr.to_s
|
59
|
+
%w{ One Two Three }.each_with_index do |v,i|
|
60
|
+
assert(value =~ /v:#{i}, label:'#{v}'/)
|
61
|
+
end
|
62
|
+
assert(value =~ /Plotr.PieChart/)
|
63
|
+
assert(value =~ /'Awesomeness': \[\[0, 1\], \[1, 2\], \[2, 2\]\]/)
|
64
|
+
end
|
65
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.0
|
3
|
+
specification_version: 1
|
4
|
+
name: drawr
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 1.0.0
|
7
|
+
date: 2007-04-24 00:00:00 -07:00
|
8
|
+
summary: The author was too lazy to write a summary
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: ryand-ruby@zenspider.com
|
12
|
+
homepage: http://www.zenspider.com/ZSS/Products/drawr/
|
13
|
+
rubyforge_project: seattlerb
|
14
|
+
description: The author was too lazy to write a description
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
-
|
22
|
+
- ">"
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 0.0.0
|
25
|
+
version:
|
26
|
+
platform: ruby
|
27
|
+
signing_key:
|
28
|
+
cert_chain:
|
29
|
+
post_install_message:
|
30
|
+
authors:
|
31
|
+
- Ryan Davis
|
32
|
+
files:
|
33
|
+
- History.txt
|
34
|
+
- Manifest.txt
|
35
|
+
- README.txt
|
36
|
+
- Rakefile
|
37
|
+
- bin/drawr
|
38
|
+
- lib/drawr.rb
|
39
|
+
- test/test_drawr.rb
|
40
|
+
test_files:
|
41
|
+
- test/test_drawr.rb
|
42
|
+
rdoc_options: []
|
43
|
+
extra_rdoc_files: []
|
44
|
+
executables:
|
45
|
+
- drawr
|
46
|
+
extensions: []
|
47
|
+
requirements: []
|
48
|
+
dependencies:
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: hoe
|
51
|
+
version_requirement:
|
52
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
53
|
+
requirements:
|
54
|
+
-
|
55
|
+
- ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: 1.2.0
|
58
|
+
version:
|