metrics-graphics-rails 2.6.0 → 2.6.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -2
- data/lib/metrics-graphics-rails/version.rb +1 -1
- data/lib/metrics-graphics-rails/view_helpers.rb +45 -21
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a83deabed39bb80b745da7fb1f8300d01f557e08
|
4
|
+
data.tar.gz: 945b01a3e9af84501713a184aa7f04b4a2d8e061
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5e49f65f6b0707d3ad6b387f947b2e0d7c0095b5c87e6b7d03facd84b94ee1e139cf276c13de96f06d6e257b5e4b2eaee43cf7d624114cb2d9e625b72f988a04
|
7
|
+
data.tar.gz: 00d5b5bade199a922bc8cb7d47c11b3962bbd1eefafdaf86b05a895731a072738bcb7f9560033a4d811536e10fbc2acc4661f8c07a15fe0458f0d139ff6eb2d2
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@ metrics-graphics-rails
|
|
3
3
|
|
4
4
|
[metrics-graphics](https://github.com/mozilla/metrics-graphics) is a library optimized for concise, principled data graphics and layouts. See more in [metricsgraphicsjs.org](http://metricsgraphicsjs.org)
|
5
5
|
|
6
|
-
The **metrics-graphics-rails** provides metrics-graphics for Rails and the assets pipeline.
|
6
|
+
The **metrics-graphics-rails** provides metrics-graphics v2.6.0 for Rails and the assets pipeline.
|
7
7
|
|
8
8
|
## Usage
|
9
9
|
|
@@ -45,7 +45,7 @@ From the [metrics-graphics example](http://metricsgraphicsjs.org/):
|
|
45
45
|
|
46
46
|
This graph can be rendered in a Rails view template with this helper:
|
47
47
|
|
48
|
-
= metrics_graphic_for data, title: "Downloads", description: "This graphic shows a time-series of downloads.", width: 600, height: 250,
|
48
|
+
= metrics_graphic_for data, target: '#downloads', title: "Downloads", description: "This graphic shows a time-series of downloads.", width: 600, height: 250, x_accessor: 'date', y_accessor: 'value', time_format: '%Y-%m-%d'
|
49
49
|
|
50
50
|
where ``data`` is an Array of Hashes of points with a ``date`` and a ``value`` keys and values. Ex: ``[{date: '2014-11-01', value: 12}, {date: '2014-11-02', value: 18}]``
|
51
51
|
|
@@ -55,6 +55,20 @@ where ``data`` is an Array of Hashes of points with a ``date`` and a ``value`` k
|
|
55
55
|
|
56
56
|
`metrics_graphic_for` helper supports multiple-lined graphs generation. Just pass data as an Array of Arrays in the same format as for single-lined graphs and it should work as a charm.
|
57
57
|
|
58
|
+
### Markers
|
59
|
+
|
60
|
+
`metrics_graphic_for` helper supports markers. Just pass marker data as an Array with the `markers` option like this:
|
61
|
+
|
62
|
+
= metrics_graphic_for data, target: '#downloads', markers: markers
|
63
|
+
|
64
|
+
where ``markers`` is an Array of Hashes of markers with ``date`` and a ``label`` keys and values. Ex: ``[{date: '2014-11-01', label: 'look here!'}, {date: '2014-11-02', label: 'look there!'}]``
|
65
|
+
|
66
|
+
### Other options
|
67
|
+
|
68
|
+
Do you want to use any *metrics-graphics* option not included in `metrics_graphic_for` view helper? Just pass them as a Hash in the `:extra_options` attribute like this:
|
69
|
+
|
70
|
+
= metrics_graphic_for data, target: '#downloads', extra_options: { min_y: 100, max_y: 200 }
|
71
|
+
|
58
72
|
## Versioning
|
59
73
|
|
60
74
|
metrics-graphics-rails 2.6.0 == metrics-graphics 2.6.0
|
@@ -2,25 +2,28 @@ require 'json'
|
|
2
2
|
|
3
3
|
module MetricsGraphicsRails
|
4
4
|
module ViewHelpers
|
5
|
-
# TODO: generalize for extra options
|
6
5
|
def metrics_graphic_for(data, options = {})
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
6
|
+
@data = data
|
7
|
+
json_data = data.to_json
|
8
|
+
@target = options.fetch(:target)
|
9
|
+
@x_accessor = options.fetch(:x_accessor) { :date }
|
10
|
+
@y_accessor = options.fetch(:y_accessor) { :value }
|
11
|
+
title = options.fetch(:title)
|
12
|
+
description = options.fetch(:description) { '' }
|
13
|
+
width = options.fetch(:width) { 600 }
|
14
|
+
height = options.fetch(:height) { 250 }
|
15
|
+
@time_format = options.fetch(:time_format) { '%Y-%m-%d' }
|
16
|
+
# Markers is an array of hashes with 'date' and 'label' keys
|
17
|
+
@markers = options.fetch(:markers) { [] }
|
18
|
+
@is_multiple = data.first.is_a?(Array)
|
19
|
+
@extra_options = options[:extra_options] || {}
|
19
20
|
|
20
21
|
javascript_tag <<-SCRIPT
|
21
22
|
var data = #{json_data};
|
22
23
|
|
23
|
-
#{convert_data_js
|
24
|
+
#{convert_data_js}
|
25
|
+
|
26
|
+
#{markers}
|
24
27
|
|
25
28
|
MG.data_graphic({
|
26
29
|
title: "#{title}",
|
@@ -28,25 +31,46 @@ module MetricsGraphicsRails
|
|
28
31
|
data: data,
|
29
32
|
width: #{width},
|
30
33
|
height: #{height},
|
31
|
-
target: '#{target}',
|
34
|
+
target: '#{@target}',
|
32
35
|
#{extra_options_to_options}
|
33
|
-
|
34
|
-
|
36
|
+
#{markers_option}
|
37
|
+
x_accessor: '#{@x_accessor}',
|
38
|
+
y_accessor: '#{@y_accessor}'
|
35
39
|
});
|
36
40
|
SCRIPT
|
37
41
|
end
|
38
42
|
|
39
43
|
private
|
40
44
|
|
41
|
-
def
|
42
|
-
|
45
|
+
def extra_options_to_options
|
46
|
+
@extra_options.map{ |k,v| "#{k}: #{v}," }.join("\n ")
|
47
|
+
end
|
48
|
+
|
49
|
+
def convert_data_js
|
50
|
+
if @is_multiple
|
43
51
|
<<-CONVERT
|
44
52
|
for (var i = 0; i < data.length; i++) {
|
45
|
-
data[i] = MG.convert.date(data[i], '#{x_accessor}', '#{time_format}');
|
53
|
+
data[i] = MG.convert.date(data[i], '#{@x_accessor}', '#{@time_format}');
|
46
54
|
}
|
47
55
|
CONVERT
|
48
56
|
else
|
49
|
-
"MG.convert.date(data, '#{x_accessor}', '#{time_format}');"
|
57
|
+
"MG.convert.date(data, '#{@x_accessor}', '#{@time_format}');"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def markers
|
62
|
+
if @markers.present?
|
63
|
+
markers_json = @markers.to_json
|
64
|
+
|
65
|
+
"var markers = #{markers_json};\n\n
|
66
|
+
MG.convert.date(markers, 'date', '%Y-%m-%dT%H:%M:%S.%LZ');"
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def markers_option
|
72
|
+
if @markers.present?
|
73
|
+
"markers: markers,"
|
50
74
|
end
|
51
75
|
end
|
52
76
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: metrics-graphics-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.6.0
|
4
|
+
version: 2.6.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Gil
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|