quacky-charts 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 = 100
20
+ r = 150
13
21
  color = d3.scale.category20c()
14
22
 
15
- data = [{"label":"one", "value":20},
16
- {"label":"two", "value":50},
17
- {"label":"three", "value":30}]
18
-
19
- vis = d3.select(".pie-chart").append("svg:svg").data([data]).attr("width", w).attr("height", h).append("svg:g").attr("transform", "translate(" + r + "," + r + ")")
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
- # return a content tag that can be selected by the client-side, and drawn on
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
@@ -1,3 +1,3 @@
1
1
  module QuackyCharts
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
data/lib/quacky-charts.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require "quacky/version"
2
2
  require "quacky/pie_chart_builder"
3
-
4
- #include ActionView::Helpers::TagHelper
3
+ require "json"
5
4
 
6
5
  module Quacky
7
6
  end
@@ -17,4 +17,5 @@ Gem::Specification.new do |gem|
17
17
 
18
18
  gem.add_development_dependency(%q<rspec>)
19
19
  gem.add_runtime_dependency(%q<coffee-rails>, ["~> 3.2.1"])
20
+ gem.add_runtime_dependency(%q<json>)
20
21
  end
@@ -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.6
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-24 00:00:00.000000000 Z
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