quacky-charts 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +5 -0
- data/lib/generators/quacky/templates/pie_charts.js.coffee +23 -8
- data/lib/quacky/pie_chart_builder.rb +27 -2
- data/lib/quacky/version.rb +1 -1
- data/lib/quacky-charts.rb +1 -2
- data/quacky-charts.gemspec +1 -0
- data/spec/cases/pie_chart_builder_spec.rb +47 -0
- metadata +18 -2
data/README.md
CHANGED
@@ -22,6 +22,11 @@ Add this line to the controller:
|
|
22
22
|
|
23
23
|
@chart = Quacky::PieChartBuilder.new
|
24
24
|
|
25
|
+
If you want to add some custom data (otherwise you'll get dummy data):
|
26
|
+
|
27
|
+
@chart.add_data( { "label" => "Category A", "value" => 20 } )
|
28
|
+
@chart.add_data( { "label" => "Category B", "value" => 60 } )
|
29
|
+
|
25
30
|
and in the view:
|
26
31
|
|
27
32
|
<%= @chart.draw %>
|
@@ -1,22 +1,37 @@
|
|
1
|
-
# based on the example at https://github.com/mbostock/d3/blob/master/examples/donut/donut.html
|
2
1
|
$(document).ready( ->
|
3
2
|
builder = new PieChartBuilder
|
4
3
|
builder.drawCharts()
|
5
4
|
)
|
6
5
|
|
6
|
+
# Draws D3 pie charts on all the elements with class "pie-chart."
|
7
7
|
class PieChartBuilder
|
8
|
-
|
8
|
+
|
9
|
+
# Grabs all the elements with class "pie-chart," and draws the chart.
|
9
10
|
drawCharts: ->
|
11
|
+
allCharts = $(".pie-chart")
|
12
|
+
@appendChart chart for chart in allCharts
|
13
|
+
|
14
|
+
# Takes a container, draws a chart based on data attribute.
|
15
|
+
# If no init data, draws from dummy data.
|
16
|
+
appendChart: (container) ->
|
17
|
+
|
10
18
|
w = 300
|
11
19
|
h = 300
|
12
|
-
r =
|
20
|
+
r = 150
|
13
21
|
color = d3.scale.category20c()
|
14
22
|
|
15
|
-
data
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
23
|
+
# If there is data in the container's data attribute, set it.
|
24
|
+
# Else, use some dummy data.
|
25
|
+
init_data = $(container).data()["chart"]
|
26
|
+
if init_data.length == 0
|
27
|
+
data = [{"label":"Category A", "value":20},
|
28
|
+
{"label":"Category B", "value":50},
|
29
|
+
{"label":"Category C", "value":30}]
|
30
|
+
else
|
31
|
+
data = init_data
|
32
|
+
|
33
|
+
# fancy D3 stuff ...
|
34
|
+
vis = d3.select(container).append("svg:svg").data([data]).attr("width", w).attr("height", h).append("svg:g").attr("transform", "translate(" + r + "," + r + ")")
|
20
35
|
arc = d3.svg.arc().outerRadius(r)
|
21
36
|
|
22
37
|
pie = d3.layout.pie().value( (d) ->
|
@@ -2,9 +2,34 @@ module Quacky
|
|
2
2
|
|
3
3
|
class PieChartBuilder
|
4
4
|
|
5
|
-
|
5
|
+
@data = []
|
6
|
+
|
7
|
+
def initialize(init_data = [])
|
8
|
+
@data = init_data
|
9
|
+
end
|
10
|
+
|
11
|
+
# Return a content tag that can be selected by the client-side, and drawn on.
|
12
|
+
# The data attribute of the HTML tag is @data.
|
6
13
|
def draw
|
7
|
-
"<div class='pie-chart'>".html_safe
|
14
|
+
"<div class='pie-chart' data-chart='#{@data.to_json}'></div>".html_safe
|
15
|
+
end
|
16
|
+
|
17
|
+
# Add data. If input is a hash, add it in. Otherwise (it's an array, most likely),
|
18
|
+
# set @data to input.
|
19
|
+
def add_data(input)
|
20
|
+
if input.class == Hash
|
21
|
+
@data << input
|
22
|
+
else
|
23
|
+
@data = input
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def clear_data
|
28
|
+
@data = []
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_data
|
32
|
+
@data
|
8
33
|
end
|
9
34
|
|
10
35
|
end
|
data/lib/quacky/version.rb
CHANGED
data/lib/quacky-charts.rb
CHANGED
data/quacky-charts.gemspec
CHANGED
@@ -12,6 +12,53 @@ describe "Quacky::PieChartBuilder" do
|
|
12
12
|
chart.class.should eq(Quacky::PieChartBuilder)
|
13
13
|
end
|
14
14
|
|
15
|
+
it "should have specified data if passed as an initialization variable" do
|
16
|
+
example_data_set = [ { "label" => "one", "value" => 20 }, { "label" => "two", "value" => 50 } ]
|
17
|
+
chart = Quacky::PieChartBuilder.new(example_data_set)
|
18
|
+
chart.get_data.should eq(example_data_set)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should have one data point after adding one data point" do
|
22
|
+
chart = Quacky::PieChartBuilder.new
|
23
|
+
|
24
|
+
example_data_point = { "label" => "one", "value" => 20 }
|
25
|
+
|
26
|
+
chart.add_data(example_data_point)
|
27
|
+
chart.get_data.should eq( [ example_data_point ] )
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should have two data points after adding two sequentially" do
|
31
|
+
chart = Quacky::PieChartBuilder.new
|
32
|
+
|
33
|
+
first_example_data_point = { "label" => "one", "value" => 20 }
|
34
|
+
second_example_data_point = { "label" => "two", "value" => 50 }
|
35
|
+
|
36
|
+
chart.add_data(first_example_data_point)
|
37
|
+
chart.add_data(second_example_data_point)
|
38
|
+
chart.get_data.should eq( [ first_example_data_point, second_example_data_point ] )
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should be able to accept an array of data, rather than a hash" do
|
42
|
+
chart = Quacky::PieChartBuilder.new
|
43
|
+
|
44
|
+
first_example_data_point = { "label" => "one", "value" => 20 }
|
45
|
+
second_example_data_point = { "label" => "two", "value" => 50 }
|
46
|
+
|
47
|
+
chart.add_data( [ first_example_data_point, second_example_data_point ] )
|
48
|
+
chart.get_data.should eq( [ first_example_data_point, second_example_data_point ] )
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should clear and replace data if an array is added" do
|
52
|
+
chart = Quacky::PieChartBuilder.new
|
53
|
+
|
54
|
+
first_example_data_point = { "label" => "one", "value" => 20 }
|
55
|
+
second_example_data_point = { "label" => "two", "value" => 50 }
|
56
|
+
|
57
|
+
chart.add_data(first_example_data_point)
|
58
|
+
chart.add_data( [ first_example_data_point, second_example_data_point ] )
|
59
|
+
chart.get_data.should eq( [ first_example_data_point, second_example_data_point ] )
|
60
|
+
end
|
61
|
+
|
15
62
|
it "should be able to draw and raise error" do
|
16
63
|
chart = Quacky::PieChartBuilder.new
|
17
64
|
lambda {chart.draw}.should raise_error
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: quacky-charts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -43,6 +43,22 @@ dependencies:
|
|
43
43
|
- - ~>
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: 3.2.1
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: json
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
46
62
|
description: A Rails Charting Gem
|
47
63
|
email:
|
48
64
|
- travissheppard.1988@gmail.com
|