d3_charts 0.0.1 → 0.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 986ef08364eb025e3a50c7b33df2f68c813b6f8f
4
- data.tar.gz: c8f4ab0880928e253c2d0ee5595b952c76d3a406
3
+ metadata.gz: 05a1d163bde06ce806fc422a0568a6a2c0774151
4
+ data.tar.gz: 8c38cb21e11baf1e63dd40902c720fc1d93b47e2
5
5
  SHA512:
6
- metadata.gz: a21b4a77fed16e6ad4a9aa05c8a8ee24f431d47d8db7045e731b464543c2759b43019c1ef8ae8a4aecfab09e8940030efcbe6d094ccddfd5c59d166637284487
7
- data.tar.gz: 990b44cd4b1129764e0b9d6c1bd1bb250b76b683c77d842a608e1947720c0af02399dda410f21bc7d6034873adf9b56797acdae5d5466197f7526741a80b20ba
6
+ metadata.gz: 0de7aa28fd88ec23dde235a08424398837d36b996c92e8e1b964f2a16746197b75422df5ef858fcdd6cd2e527c68ac85d1db96caaae5430117197b8269e88f9c
7
+ data.tar.gz: 83e66931f3b43daeab83cc59905534f1d6e1003d27baee0719f84888f895f88813ed9a3de85d4cea2f51efdea559c5aa60ff67835ffa94393506d4fc9e3acaef
data/README.md CHANGED
@@ -30,7 +30,7 @@ To add some basic colouring of pie charts, add the the following in `application
30
30
 
31
31
  ### Pie charts
32
32
 
33
- = pie_chart(data, options)
33
+ = chart_tag(:pie, data, options)
34
34
 
35
35
  Available `options` are:
36
36
 
@@ -39,26 +39,26 @@ Available `options` are:
39
39
 
40
40
  Which are passed in like so:
41
41
 
42
- = pie_chart(data, { width: 123, height: 123 })
42
+ = chart_tag(:pie, data, { width: 123, height: 123 })
43
43
 
44
44
  ### Area charts
45
45
 
46
- = area_chart(data, options)
46
+ = chart_tag(:area, data, options)
47
47
 
48
48
  Available `options` are:
49
49
 
50
50
  * `width`, defaults to 1000
51
51
  * `height`, defaults to 1000
52
- * `margin`, defaults to 25
53
- * `format`, the strftime format of the dates, defaults to `%d/%m/%y`
52
+ * `date_format`, the strftime format of datums as rendered in the chart, defaults to `%Y-%m-%d`
53
+ * `data_date_format`, the strftime format of datums as specified in the data, defaults to `%Y-%m-%d`
54
54
 
55
55
  Which are passed in like so:
56
56
 
57
- = area_chart(data, { width: 123, height: 123, margin: 25 })
57
+ = chart_tag(:area, data, { width: 123, height: 123 })
58
58
 
59
59
  ## TODO:
60
60
 
61
- * Add more charts:
61
+ * More charts:
62
62
  * Line Chart
63
63
  * Histogram Chart
64
64
  * Vertical Bar Chart
@@ -10,9 +10,9 @@
10
10
  debug: false
11
11
  width: 1000
12
12
  height: 1000
13
- margin: 40
14
- ticks: 'days'
15
- format: "%d/%m/%y"
13
+ ticks: 'months'
14
+ data_date_format: "%Y-%m-%d"
15
+ date_format: "%Y-%m-%d"
16
16
 
17
17
  # ---------------------------------------------------------------------
18
18
 
@@ -33,43 +33,37 @@
33
33
 
34
34
  # ---------------------------------------------------------------------
35
35
 
36
- get_data: -> @$element.data 'chart-data'
37
- get_width: -> @$element.data('width') || @options.width
36
+ get_data: -> @$element.data 'chart-data' || []
37
+ get_data_date_format: -> @$element.data('data-date-format') || @options.data_date_format
38
+ get_date_format: -> @$element.data('date-format') || @options.date_format
38
39
  get_height: -> @$element.data('height') || @options.height
39
- get_margin: -> @$element.data('margin') || @options.margin
40
- get_format: -> @$element.data('format') || @options.format
41
40
  get_ticks: ->
42
- format = @$element.data('format') || @options.format
43
- if /%d|%e/.test(format)
44
- d3.time.days
45
- else if /%m|%B|%b|%h/.test(format)
46
- d3.time.months
47
- alert format
48
- else if /%y|%Y/.test(format)
49
- d3.time.years
41
+ switch @$element.data('ticks') || @options.ticks
42
+ when 'days' then d3.time.days
43
+ when 'months' then d3.time.months
44
+ when 'years' then d3.time.years
45
+ get_width: -> @$element.data('width') || @options.width
50
46
 
51
47
  # ---------------------------------------------------------------------
52
48
 
53
49
  render: ->
54
50
  data = @get_data()
55
51
 
56
- console.log data
57
-
58
- width = @get_width() - @get_margin()*2
59
- height = @get_height() - @get_margin()*2
60
- parseDate = d3.time.format(@get_format()).parse
52
+ width = @get_width()
53
+ height = @get_height()
54
+ parseDate = d3.time.format(@get_data_date_format()).parse
61
55
 
62
56
  x = d3.time.scale().range([0, width])
63
57
  y = d3.scale.linear().range([height, 0])
64
58
 
65
- xAxis = d3.svg.axis().scale(x).orient("bottom").ticks(@get_ticks(), 1).tickSize(0).tickPadding(5).tickFormat(d3.time.format(@get_format()))
59
+ xAxis = d3.svg.axis().scale(x).orient("bottom").ticks(@get_ticks(), 1).tickSize(0).tickPadding(5).tickFormat(d3.time.format(@get_date_format()))
66
60
  yAxis = d3.svg.axis().scale(y).orient("left").ticks(2).tickSize(0).tickPadding(5)
67
61
 
68
62
  area = d3.svg.area().interpolate("basis").x((d) -> x d.label).y0(height).y1((d) -> y d.value)
69
- svg = d3.select(@$element[0]).append("svg").attr("width", @get_width()).attr("height", @get_height()).attr("viewBox", "0 0 #{@get_height()} #{@get_width()}").attr("xmlns", "http://www.w3.org/2000/svg").append("g").attr("transform", "translate(#{@get_margin()},#{@get_margin()})")
63
+ svg = d3.select(@$element[0]).append("svg").attr("width", @get_width()).attr("height", @get_height()).attr("viewBox", "0 0 #{@get_height()} #{@get_width()}").attr("xmlns", "http://www.w3.org/2000/svg").append("g")
70
64
 
71
65
  data.forEach (d) ->
72
- d.label = parseDate(d.label)
66
+ d.label = parseDate(d.date)
73
67
  d.value = +d.value
74
68
 
75
69
  x.domain d3.extent(data, (d) -> d.label)
@@ -94,10 +88,4 @@
94
88
 
95
89
  # =====================================================================
96
90
 
97
- $ ->
98
-
99
- $('.chart.container.area').chart_area()
100
-
101
- # make sure the plugin is correctly rebound to new elements
102
- $('body').on 'dom_update', (e) ->
103
- $('.chart.container.area').chart_area()
91
+ $ -> $('.chart.container.area').chart_area()
@@ -59,10 +59,4 @@
59
59
 
60
60
  # =====================================================================
61
61
 
62
- $ ->
63
-
64
- $('.chart.container.pie').chart_pie()
65
-
66
- # make sure the plugin is correctly rebound to new elements
67
- $('body').on 'dom_update', (e) ->
68
- $('.chart.container.pie').chart_pie()
62
+ $ -> $('.chart.container.pie').chart_pie()
@@ -10,6 +10,6 @@ $colors: (aliceblue, antiquewhite, aqua, aquamarine, azure, beige, bisque, black
10
10
 
11
11
  .chart.container.area {
12
12
  svg {
13
- // padding: 25px;
13
+ overflow: visible;
14
14
  }
15
15
  }
@@ -1,8 +1,8 @@
1
1
  require "d3_charts/chart"
2
2
  require "d3_charts/chart/pie"
3
3
  require "d3_charts/chart/area"
4
- require "d3_charts/railtie" if defined?(Rails)
4
+ require "d3_charts/railtie" if defined?(Rails::Railtie)
5
5
  require "d3_charts/engine"
6
6
  require "d3_charts/version"
7
7
 
8
- require "rails-assets-d3"
8
+ require "rails-assets-d3"
@@ -17,14 +17,14 @@ module D3Charts
17
17
  def dom_data
18
18
  res = {}
19
19
  res[:chart_data] = @chart_data.to_json
20
- res[:width] = @options[:width] if @options[:width]
21
- res[:height] = @options[:height] if @options[:height]
20
+ res[:width] = @options[:width]
21
+ res[:height] = @options[:height]
22
22
  res
23
23
  end
24
24
 
25
25
  def tag
26
- content_tag(:div, nil, { class: dom_class, data: dom_data })
26
+ content_tag(:div, nil, { class: dom_class, data: dom_data.delete_if{ |k, v| v.blank? } })
27
27
  end
28
28
 
29
29
  end
30
- end
30
+ end
@@ -8,11 +8,11 @@ module D3Charts
8
8
 
9
9
  def dom_data
10
10
  res = {}
11
- res[:margin] = @options[:margin] if @options[:margin]
12
- res[:format] = @options[:format] if @options[:format]
11
+ res[:date_format] = @options[:date_format]
12
+ res[:data_date_format] = @options[:data_date_format]
13
13
  super.merge(res)
14
14
  end
15
15
 
16
16
  end
17
17
  end
18
- end
18
+ end
@@ -1,5 +1,5 @@
1
1
  module D3
2
2
  module Charts
3
- VERSION = "0.0.1"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -11,19 +11,14 @@ module D3Charts
11
11
  end
12
12
  end
13
13
 
14
- # ---------------------------------------------------------------------
14
+ # =====================================================================
15
15
 
16
- def pie_chart chart_data, options={}
17
- Chart::Pie.new(chart_data, options).tag.html_safe
18
- end
19
-
20
- # ---------------------------------------------------------------------
21
-
22
- def area_chart chart_data, options={}
23
- Chart::Area.new(chart_data, options).tag.html_safe
16
+ def chart_tag chart_type, chart_data, options={}
17
+ case chart_type
18
+ when :pie then Chart::Pie.new(chart_data, options).tag.html_safe
19
+ when :area then Chart::Area.new(chart_data, options).tag.html_safe
20
+ end
24
21
  end
25
22
 
26
- # ---------------------------------------------------------------------
27
-
28
23
  end
29
- end
24
+ end
@@ -10,16 +10,16 @@ module D3Charts
10
10
 
11
11
  # =====================================================================
12
12
 
13
- describe '#pie_chart' do
13
+ describe '#chart_tag' do
14
14
  let(:data) { [{ value: 125, label: 'foo' }, { value: 325, label: 'bar' }] }
15
15
 
16
16
  it 'wraps the chart in a div' do
17
- pie_chart(data).must_match Regexp.new("<div.*?></div>")
17
+ chart_tag(:pie, data).must_match Regexp.new("<div.*?></div>")
18
18
  end
19
19
 
20
20
  it 'returns the correct data' do
21
21
  skip
22
- pie_chart(data).must_include "data-chart-data=\"#{data}\">"
22
+ chart_tag(:pie, data).must_include "data-chart-data=\"#{data}\">"
23
23
  end
24
24
  end
25
25
 
@@ -29,16 +29,16 @@ module D3Charts
29
29
  let(:data) { [{ date: '2014-08-02', value: 100 }, { date: '2014-09-02', value: 200 }] }
30
30
 
31
31
  it 'wraps the chart in a div' do
32
- area_chart(data).must_match Regexp.new("<div.*?></div>")
32
+ chart_tag(:area, data).must_match Regexp.new("<div.*?></div>")
33
33
  end
34
34
 
35
35
  it 'returns the correct data' do
36
36
  skip
37
- area_chart(data).must_include "data-chart-data=\"#{data}\">"
37
+ chart_tag(:area, data).must_include "data-chart-data=\"#{data}\">"
38
38
  end
39
39
  end
40
40
 
41
41
  # ---------------------------------------------------------------------
42
42
 
43
43
  end
44
- end
44
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: d3_charts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Celizna
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-02 00:00:00.000000000 Z
11
+ date: 2015-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -157,7 +157,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
157
  version: '0'
158
158
  requirements: []
159
159
  rubyforge_project:
160
- rubygems_version: 2.2.2
160
+ rubygems_version: 2.4.8
161
161
  signing_key:
162
162
  specification_version: 4
163
163
  summary: Rails helpers for building SVG charts using the d3 library.