d3_charts 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -7
- data/lib/assets/javascripts/charts/area_chart.js.coffee +18 -30
- data/lib/assets/javascripts/charts/pie_chart.js.coffee +1 -7
- data/lib/assets/stylesheets/d3_charts.css.scss +1 -1
- data/lib/d3_charts.rb +2 -2
- data/lib/d3_charts/chart.rb +4 -4
- data/lib/d3_charts/chart/area.rb +3 -3
- data/lib/d3_charts/version.rb +1 -1
- data/lib/d3_charts/view_helpers.rb +7 -12
- data/test/d3_charts/view_helpers_test.rb +6 -6
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 05a1d163bde06ce806fc422a0568a6a2c0774151
|
4
|
+
data.tar.gz: 8c38cb21e11baf1e63dd40902c720fc1d93b47e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
=
|
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
|
-
=
|
42
|
+
= chart_tag(:pie, data, { width: 123, height: 123 })
|
43
43
|
|
44
44
|
### Area charts
|
45
45
|
|
46
|
-
=
|
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
|
-
* `
|
53
|
-
* `
|
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
|
-
=
|
57
|
+
= chart_tag(:area, data, { width: 123, height: 123 })
|
58
58
|
|
59
59
|
## TODO:
|
60
60
|
|
61
|
-
*
|
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
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
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
|
-
|
43
|
-
|
44
|
-
d3.time.
|
45
|
-
|
46
|
-
|
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
|
-
|
57
|
-
|
58
|
-
|
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(@
|
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")
|
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.
|
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()
|
data/lib/d3_charts.rb
CHANGED
@@ -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"
|
data/lib/d3_charts/chart.rb
CHANGED
@@ -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]
|
21
|
-
res[:height] = @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
|
data/lib/d3_charts/chart/area.rb
CHANGED
@@ -8,11 +8,11 @@ module D3Charts
|
|
8
8
|
|
9
9
|
def dom_data
|
10
10
|
res = {}
|
11
|
-
res[:
|
12
|
-
res[:
|
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
|
data/lib/d3_charts/version.rb
CHANGED
@@ -11,19 +11,14 @@ module D3Charts
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
#
|
14
|
+
# =====================================================================
|
15
15
|
|
16
|
-
def
|
17
|
-
|
18
|
-
|
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 '#
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
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:
|
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.
|
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.
|