simple_d3 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 92d77777b62827b1c7cd915e91fac778e9537da8
4
+ data.tar.gz: 6cde98228fa9f5360bc9ba0a483f0c4f7dae5c31
5
+ SHA512:
6
+ metadata.gz: 8a9b92c49ec365d77d1603adf00da6393c6718464d17918cb34f6cdfa7b76d924bfe9acf8c4318807bb2a13be95581502502a35245c8cf8378987001ea90d4e8
7
+ data.tar.gz: ba06b0b90d38413d28d597aaa842817c8b9bca97a55845c4f29533cb1e6e0c6857270bf30743b800d85dca77b432dd2592916f271625ee39f65db921f7a27f11
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_d3.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 TigerWolf
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,71 @@
1
+ # SimpleD3
2
+
3
+ Helps create simple D3 graphs quickly without much code.
4
+
5
+ # Installation
6
+
7
+ Add simple d3 to your gemfile
8
+
9
+ ``gem 'simple_d3'``
10
+
11
+ Then add **d3** to your application.js
12
+
13
+ ```
14
+ ..
15
+ //= require d3
16
+ //= require_tree .
17
+ ```
18
+
19
+ Then add this to your view:
20
+
21
+ ```
22
+ <script type="text/javascript">
23
+ var data = [ 5, 10, 13, 19, 21, 25, 22, 18, 15, 13,
24
+ 11, 12, 15, 20, 18, 17, 16, 18, 23, 25 ];
25
+ </script>
26
+
27
+ <%= simple_d3("my_chart") %>
28
+ ```
29
+
30
+ ### Bar Chart
31
+
32
+ ```
33
+ <script type="text/javascript">
34
+ var data = [ 5, 10, 13, 19, 21, 25, 22, 18, 15, 13,
35
+ 11, 12, 15, 20, 18, 17, 16, 18, 23, 25 ];
36
+ </script>
37
+
38
+ <%= simple_d3_bar("my_bars",:graph) %>
39
+ ```
40
+
41
+ ### Pie Chart
42
+
43
+ ```
44
+ <script type="text/javascript">
45
+ var data = [ 5, 10, 20, 45, 6, 25 ];
46
+ </script>
47
+
48
+ <%= simple_d3_pie("my_pie",:pie) %>
49
+ ```
50
+
51
+ ### Setting Options
52
+
53
+ You can also set options, like:
54
+
55
+ ```
56
+ <%= simple_d3("my_pie3",{:type=>"pie", :width=> 1000, :height => 500}) %>
57
+ ```
58
+
59
+ Current options are:
60
+ ```
61
+ type - "bar" or "pie"
62
+ width
63
+ height
64
+ colors - array of colors
65
+ ```
66
+
67
+ See https://github.com/TigerWolf/simple_d3_demo for more examples.
68
+
69
+ ### Screenshot
70
+
71
+ ![Screenshot](docs/demo.png?raw=true "Optional Title" =450x)
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/TODO.md ADDED
@@ -0,0 +1,11 @@
1
+ TODO & Ideas
2
+ ===
3
+
4
+ Just a list of potential future additions to this gem.
5
+
6
+ * Overwriting of templates with a view
7
+ * Custom Additions/Modification to javascript
8
+ * Allow for data to be defined in controller i_var or similar
9
+ * Load data dynamically from a "live call" (Call to controller to get json data from AR model)
10
+ * Tests
11
+ * More color scales and easy "named" color settings
Binary file
@@ -0,0 +1,10 @@
1
+ require "simple_d3/version"
2
+ require "simple_d3/template"
3
+ require "simple_d3/layout_helper"
4
+
5
+ module SimpleD3
6
+
7
+ end
8
+
9
+ # lib/my_gem.rb
10
+ require 'simple_d3/engine' if defined?(Rails)
@@ -0,0 +1,5 @@
1
+ module SimpleD3
2
+ class Engine < ::Rails::Engine
3
+ require 'd3_rails'
4
+ end
5
+ end
@@ -0,0 +1,38 @@
1
+ module SimpleD3
2
+ module LayoutHelper
3
+ include ActionView::Helpers::TagHelper
4
+ def simple_d3(placeholder, settings={})
5
+ defaults = {
6
+ :type => :bar,
7
+ :width => 600,
8
+ :height => 250,
9
+ }
10
+ settings.reverse_merge!(defaults)
11
+ graph = SimpleD3::Template.template(placeholder, settings).prepend(content_tag("div", "", {id: placeholder}))
12
+
13
+ # Display without escaping html
14
+ if defined?(raw)
15
+ return raw(graph)
16
+ else
17
+ return graph
18
+ end
19
+
20
+ end
21
+
22
+ def simple_d3_bar(placeholder, settings={})
23
+ settings[:type] = "bar"
24
+ settings[:width] = 600
25
+ settings[:height] = 250
26
+ settings[:colors] = ["#deebf7","#c6dbef","#9ecae1","#6baed6","#4292c6","#2171b5","#08519c","#08306b"]
27
+ simple_d3(placeholder, settings)
28
+ end
29
+
30
+ def simple_d3_pie(placeholder, settings={})
31
+ settings[:type] = "pie"
32
+ settings[:width] = settings[:height] = 500
33
+ simple_d3(placeholder, settings)
34
+ end
35
+ end
36
+ end
37
+
38
+ ActionView::Base.send :include, SimpleD3::LayoutHelper
@@ -0,0 +1,142 @@
1
+ module SimpleD3
2
+ class Template
3
+ def self.template(placeholder, settings)
4
+ template = new(placeholder, settings)
5
+ javascript = template.send(settings[:type])
6
+ colors = settings[:colors].present? ? template.colors(settings[:colors]) : "d3.scale.category10()"
7
+
8
+ graph = <<-EOJS
9
+ <script type="text/javascript">
10
+ //Width and height
11
+ var w = #{settings[:width]};
12
+ var h = #{settings[:height]};
13
+
14
+ //Easy colors accessible via a 10-step ordinal scale
15
+ var colors = #{colors};
16
+
17
+ #{javascript}
18
+ </script>
19
+ EOJS
20
+ end
21
+
22
+ def initialize(placeholder, settings)
23
+ @placeholder = placeholder
24
+ @settings = settings
25
+ end
26
+
27
+ def colors(color_array)
28
+ colors = <<-EOJS
29
+ d3.scale.ordinal()
30
+ .range(#{color_array.to_s})
31
+ .domain(d3.range(0,#{color_array.size}))
32
+ EOJS
33
+ end
34
+
35
+ def bar
36
+ graph = <<-EOJS
37
+
38
+ var xScale = d3.scale.ordinal()
39
+ .domain(d3.range(data.length))
40
+ .rangeRoundBands([0, w], 0.05);
41
+
42
+ var yScale = d3.scale.linear()
43
+ .domain([0, d3.max(data)])
44
+ .range([0, h]);
45
+
46
+ //Create SVG element
47
+ var #{@placeholder} = d3.select("##{@placeholder}")
48
+ .append("svg")
49
+ .attr("width", w)
50
+ .attr("height", h);
51
+
52
+ //Create bars
53
+ #{@placeholder}.selectAll("rect")
54
+ .data(data)
55
+ .enter()
56
+ .append("rect")
57
+ .attr("x", function(d, i) {
58
+ return xScale(i);
59
+ })
60
+ .attr("y", function(d) {
61
+ return h - yScale(d);
62
+ })
63
+ .attr("width", xScale.rangeBand())
64
+ .attr("height", function(d) {
65
+ return yScale(d);
66
+ })
67
+ .attr("fill", function(d, i) {
68
+ return colors(i);
69
+ })
70
+ .on("mouseover", function(d) {
71
+
72
+ //Get this bar's x/y values, then augment for the tooltip
73
+ var xPosition = parseFloat(d3.select(this).attr("x")) + xScale.rangeBand() / 2;
74
+ var yPosition = parseFloat(d3.select(this).attr("y")) + 14;
75
+
76
+ //Create the tooltip label
77
+ #{@placeholder}.append("text")
78
+ .attr("id", "tooltip")
79
+ .attr("x", xPosition)
80
+ .attr("y", yPosition)
81
+ .attr("text-anchor", "middle")
82
+ .attr("font-family", "sans-serif")
83
+ .attr("font-size", "11px")
84
+ .attr("font-weight", "bold")
85
+ .attr("fill", "black")
86
+ .text(d);
87
+
88
+ })
89
+ .on("mouseout", function() {
90
+
91
+ //Remove the tooltip
92
+ d3.select("#tooltip").remove();
93
+
94
+ });
95
+
96
+ EOJS
97
+ end
98
+
99
+ def pie
100
+ pie = <<-EOJS
101
+ var outerRadius = w / 2;
102
+ var innerRadius = 0;
103
+ var arc = d3.svg.arc()
104
+ .innerRadius(innerRadius)
105
+ .outerRadius(outerRadius);
106
+
107
+ var pie = d3.layout.pie();
108
+
109
+ //Create SVG element
110
+ var #{@placeholder} = d3.select("##{@placeholder}")
111
+ .append("svg")
112
+ .attr("width", w)
113
+ .attr("height", h);
114
+
115
+ //Set up groups
116
+ var arcs = #{@placeholder}.selectAll("g.arc")
117
+ .data(pie(data))
118
+ .enter()
119
+ .append("g")
120
+ .attr("class", "arc")
121
+ .attr("transform", "translate(" + outerRadius + "," + outerRadius + ")");
122
+
123
+ //Draw arc paths
124
+ arcs.append("path")
125
+ .attr("fill", function(d, i) {
126
+ return colors(i);
127
+ })
128
+ .attr("d", arc);
129
+
130
+ //Labels
131
+ arcs.append("text")
132
+ .attr("transform", function(d) {
133
+ return "translate(" + arc.centroid(d) + ")";
134
+ })
135
+ .attr("text-anchor", "middle")
136
+ .text(function(d) {
137
+ return d.value;
138
+ });
139
+ EOJS
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleD3
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_d3/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simple_d3"
8
+ spec.version = SimpleD3::VERSION
9
+ spec.authors = ["TigerWolf"]
10
+ spec.email = ["hiddentiger@gmail.com"]
11
+ spec.summary = %q{Simple graphs with D3 in Rails}
12
+ spec.description = %q{Make simple graphs with D3 in your rails project.}
13
+ spec.homepage = "https://github.com/TigerWolf/simple_d3"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'd3_rails', '~> 3.4.9'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_d3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - TigerWolf
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: d3_rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 3.4.9
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 3.4.9
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Make simple graphs with D3 in your rails project.
56
+ email:
57
+ - hiddentiger@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - TODO.md
68
+ - docs/demo.png
69
+ - lib/simple_d3.rb
70
+ - lib/simple_d3/engine.rb
71
+ - lib/simple_d3/layout_helper.rb
72
+ - lib/simple_d3/template.rb
73
+ - lib/simple_d3/version.rb
74
+ - simple_d3.gemspec
75
+ homepage: https://github.com/TigerWolf/simple_d3
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.2.2
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Simple graphs with D3 in Rails
99
+ test_files: []