chart_js 1.0.0pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +67 -0
  9. data/Rakefile +6 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/chart_js.gemspec +27 -0
  13. data/examples/bar_chart.rb +18 -0
  14. data/examples/build.rb +20 -0
  15. data/examples/debug.rb +6 -0
  16. data/examples/line_chart.rb +18 -0
  17. data/examples/radar_chart.rb +18 -0
  18. data/examples/sinatra.rb +23 -0
  19. data/lib/chart_js.rb +21 -0
  20. data/lib/chart_js/chart/bar_chart/background.rb +20 -0
  21. data/lib/chart_js/chart/bar_chart/bar_chart.rb +94 -0
  22. data/lib/chart_js/chart/bar_chart/border.rb +48 -0
  23. data/lib/chart_js/chart/bar_chart/data.rb +37 -0
  24. data/lib/chart_js/chart/bar_chart/dataset.rb +87 -0
  25. data/lib/chart_js/chart/bar_chart/dataset/background.rb +20 -0
  26. data/lib/chart_js/chart/bar_chart/dataset/border.rb +48 -0
  27. data/lib/chart_js/chart/bar_chart/dataset/line.rb +16 -0
  28. data/lib/chart_js/chart/bar_chart/dataset/point.rb +55 -0
  29. data/lib/chart_js/chart/bar_chart/dataset/point_border.rb +44 -0
  30. data/lib/chart_js/chart/bar_chart/dataset/point_hover.rb +44 -0
  31. data/lib/chart_js/chart/bar_chart/file.rb +26 -0
  32. data/lib/chart_js/chart/bar_chart/line.rb +16 -0
  33. data/lib/chart_js/chart/bar_chart/opts.rb +18 -0
  34. data/lib/chart_js/chart/bar_chart/point.rb +55 -0
  35. data/lib/chart_js/chart/bar_chart/point_border.rb +44 -0
  36. data/lib/chart_js/chart/bar_chart/point_hover.rb +44 -0
  37. data/lib/chart_js/chart/chart.rb +99 -0
  38. data/lib/chart_js/chart/charts.rb +22 -0
  39. data/lib/chart_js/chart/data.rb +37 -0
  40. data/lib/chart_js/chart/dataset.rb +87 -0
  41. data/lib/chart_js/chart/dataset/background.rb +20 -0
  42. data/lib/chart_js/chart/dataset/border.rb +48 -0
  43. data/lib/chart_js/chart/dataset/line.rb +16 -0
  44. data/lib/chart_js/chart/dataset/point.rb +55 -0
  45. data/lib/chart_js/chart/dataset/point_border.rb +44 -0
  46. data/lib/chart_js/chart/dataset/point_hover.rb +44 -0
  47. data/lib/chart_js/chart/file.rb +26 -0
  48. data/lib/chart_js/chart/line_chart.rb +99 -0
  49. data/lib/chart_js/chart/line_chart/background.rb +20 -0
  50. data/lib/chart_js/chart/line_chart/border.rb +48 -0
  51. data/lib/chart_js/chart/line_chart/data.rb +37 -0
  52. data/lib/chart_js/chart/line_chart/dataset.rb +87 -0
  53. data/lib/chart_js/chart/line_chart/dataset/background.rb +20 -0
  54. data/lib/chart_js/chart/line_chart/dataset/border.rb +48 -0
  55. data/lib/chart_js/chart/line_chart/dataset/line.rb +16 -0
  56. data/lib/chart_js/chart/line_chart/dataset/point.rb +55 -0
  57. data/lib/chart_js/chart/line_chart/dataset/point_border.rb +44 -0
  58. data/lib/chart_js/chart/line_chart/dataset/point_hover.rb +44 -0
  59. data/lib/chart_js/chart/line_chart/file.rb +26 -0
  60. data/lib/chart_js/chart/line_chart/line.rb +16 -0
  61. data/lib/chart_js/chart/line_chart/line_chart.rb +94 -0
  62. data/lib/chart_js/chart/line_chart/opts.rb +18 -0
  63. data/lib/chart_js/chart/line_chart/point.rb +55 -0
  64. data/lib/chart_js/chart/line_chart/point_border.rb +44 -0
  65. data/lib/chart_js/chart/line_chart/point_hover.rb +44 -0
  66. data/lib/chart_js/chart/opts.rb +18 -0
  67. data/lib/chart_js/chart/radar_chart/background.rb +20 -0
  68. data/lib/chart_js/chart/radar_chart/border.rb +48 -0
  69. data/lib/chart_js/chart/radar_chart/data.rb +37 -0
  70. data/lib/chart_js/chart/radar_chart/dataset.rb +87 -0
  71. data/lib/chart_js/chart/radar_chart/dataset/background.rb +20 -0
  72. data/lib/chart_js/chart/radar_chart/dataset/border.rb +48 -0
  73. data/lib/chart_js/chart/radar_chart/dataset/line.rb +16 -0
  74. data/lib/chart_js/chart/radar_chart/dataset/point.rb +55 -0
  75. data/lib/chart_js/chart/radar_chart/dataset/point_border.rb +44 -0
  76. data/lib/chart_js/chart/radar_chart/dataset/point_hover.rb +44 -0
  77. data/lib/chart_js/chart/radar_chart/file.rb +26 -0
  78. data/lib/chart_js/chart/radar_chart/line.rb +16 -0
  79. data/lib/chart_js/chart/radar_chart/opts.rb +18 -0
  80. data/lib/chart_js/chart/radar_chart/point.rb +55 -0
  81. data/lib/chart_js/chart/radar_chart/point_border.rb +44 -0
  82. data/lib/chart_js/chart/radar_chart/point_hover.rb +44 -0
  83. data/lib/chart_js/chart/radar_chart/radar_chart.rb +94 -0
  84. data/lib/chart_js/version.rb +3 -0
  85. metadata +169 -0
@@ -0,0 +1,44 @@
1
+ module ChartJS
2
+
3
+ class PointBorder
4
+
5
+ def initialize(container)
6
+ @container = container
7
+ end
8
+
9
+ def build(&block)
10
+ instance_eval(&block)
11
+ @container
12
+ end
13
+
14
+ def radius(value)
15
+ if value.is_a? Array
16
+ @container['pointHoverRadius'] = value.map(&:to_i)
17
+ else
18
+ @container['pointHoverRadius'] = value.to_i
19
+ end
20
+ end
21
+
22
+ def width(value)
23
+ if value.is_a? Array
24
+ @container['pointHoverBorderWidth'] = value.map(&:to_i)
25
+ else
26
+ @container['pointHoverBorderWidth'] = value.to_i
27
+ end
28
+ end
29
+
30
+ def color(value, type = :both)
31
+ case type
32
+ when :hover
33
+ @container['pointHoverBorderColor'] = value
34
+ when :no_hover
35
+ @container['pointBorderColor'] = value
36
+ when :both
37
+ color value, :hover
38
+ color value, :no_hover
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -0,0 +1,44 @@
1
+ module ChartJS
2
+
3
+ class PointHover
4
+
5
+ def initialize(container)
6
+ @container = container
7
+ end
8
+
9
+ def build(&block)
10
+ instance_eval(&block)
11
+ @container
12
+ end
13
+
14
+ def radius(value)
15
+ if value.is_a? Array
16
+ @container['pointHoverRadius'] = value.map(&:to_i)
17
+ else
18
+ @container['pointHoverRadius'] = value.to_i
19
+ end
20
+ end
21
+
22
+ def width(value)
23
+ if value.is_a? Array
24
+ @container['pointHoverBorderWidth'] = value.map(&:to_i)
25
+ else
26
+ @container['pointHoverBorderWidth'] = value.to_i
27
+ end
28
+ end
29
+
30
+ def color(value, type = :both)
31
+ case type
32
+ when :border
33
+ @container['pointHoverBorderColor'] = value
34
+ when :background
35
+ @container['pointHoverBackgroundColor'] = value
36
+ when :both
37
+ color value, :borer
38
+ color value, :background
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -0,0 +1,26 @@
1
+ require_relative "dataset.rb"
2
+
3
+ module ChartJS
4
+
5
+ class SaveFile
6
+
7
+ def initialize(path, html: false)
8
+ path(path)
9
+ @html = html if html
10
+ end
11
+
12
+ def path(value = nil)
13
+ return @path if value.nil?
14
+ @path = value
15
+ end
16
+
17
+ def save!(path: @path, html: @html)
18
+ File.open(path, "w") do |file|
19
+ file.write(html)
20
+ file.close
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,16 @@
1
+ module ChartJS
2
+
3
+ class Line
4
+
5
+ def initialize(container)
6
+ @container = container
7
+ end
8
+
9
+ def build(&block)
10
+ instance_eval(&block)
11
+ @container
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,18 @@
1
+ require_relative "dataset.rb"
2
+
3
+ module ChartJS
4
+
5
+ class Options
6
+
7
+ def initialize
8
+ @container = Hash.new
9
+ end
10
+
11
+ def build(&block)
12
+ instance_eval(&block)
13
+ @container
14
+ end
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,55 @@
1
+ require_relative 'point_hover.rb'
2
+ require_relative 'point_border.rb'
3
+
4
+ module ChartJS
5
+
6
+ class Point
7
+
8
+ def initialize(container)
9
+ @container = container
10
+ end
11
+
12
+ def build(&block)
13
+ instance_eval(&block)
14
+ @container
15
+ end
16
+
17
+ def hover(&block)
18
+ @container = PointHover.new(@container).build(&block)
19
+ end
20
+
21
+ def border(&block)
22
+ @container = PointBorder.new(@container).build(&block)
23
+ end
24
+
25
+ def radius(value)
26
+ if value.is_a? Array
27
+ @container['pointRadius'] = value.map(&:to_i)
28
+ else
29
+ @container['pointRadius'] = value.to_i
30
+ end
31
+ end
32
+
33
+ def hit_radius(value)
34
+ if value.is_a? Array
35
+ @container['pointHitRadius'] = value.map(&:to_i)
36
+ else
37
+ @container['pointHitRadius'] = value.to_i
38
+ end
39
+ end
40
+
41
+ def color(value, type = :both)
42
+ case type
43
+ when :border
44
+ @container['pointBorderColor'] = value
45
+ when :background
46
+ @container['pointBackgroundColor'] = value
47
+ when :both
48
+ color value, :borer
49
+ color value, :background
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,44 @@
1
+ module ChartJS
2
+
3
+ class PointBorder
4
+
5
+ def initialize(container)
6
+ @container = container
7
+ end
8
+
9
+ def build(&block)
10
+ instance_eval(&block)
11
+ @container
12
+ end
13
+
14
+ def radius(value)
15
+ if value.is_a? Array
16
+ @container['pointHoverRadius'] = value.map(&:to_i)
17
+ else
18
+ @container['pointHoverRadius'] = value.to_i
19
+ end
20
+ end
21
+
22
+ def width(value)
23
+ if value.is_a? Array
24
+ @container['pointHoverBorderWidth'] = value.map(&:to_i)
25
+ else
26
+ @container['pointHoverBorderWidth'] = value.to_i
27
+ end
28
+ end
29
+
30
+ def color(value, type = :both)
31
+ case type
32
+ when :hover
33
+ @container['pointHoverBorderColor'] = value
34
+ when :no_hover
35
+ @container['pointBorderColor'] = value
36
+ when :both
37
+ color value, :hover
38
+ color value, :no_hover
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -0,0 +1,44 @@
1
+ module ChartJS
2
+
3
+ class PointHover
4
+
5
+ def initialize(container)
6
+ @container = container
7
+ end
8
+
9
+ def build(&block)
10
+ instance_eval(&block)
11
+ @container
12
+ end
13
+
14
+ def radius(value)
15
+ if value.is_a? Array
16
+ @container['pointHoverRadius'] = value.map(&:to_i)
17
+ else
18
+ @container['pointHoverRadius'] = value.to_i
19
+ end
20
+ end
21
+
22
+ def width(value)
23
+ if value.is_a? Array
24
+ @container['pointHoverBorderWidth'] = value.map(&:to_i)
25
+ else
26
+ @container['pointHoverBorderWidth'] = value.to_i
27
+ end
28
+ end
29
+
30
+ def color(value, type = :both)
31
+ case type
32
+ when :border
33
+ @container['pointHoverBorderColor'] = value
34
+ when :background
35
+ @container['pointHoverBackgroundColor'] = value
36
+ when :both
37
+ color value, :borer
38
+ color value, :background
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -0,0 +1,94 @@
1
+ require "json"
2
+ require "securerandom"
3
+ require_relative "data.rb"
4
+ require_relative "opts.rb"
5
+ require_relative "file.rb"
6
+
7
+ module ChartJS
8
+
9
+ class RadarChart
10
+
11
+ def initialize(&block)
12
+ @type = "radar"
13
+ @data = nil
14
+ @opts = nil
15
+ @file = false
16
+ build(&block)
17
+ end
18
+
19
+ def build(&block)
20
+ instance_eval(&block)
21
+ if @file
22
+ f = SaveFile.new(@file, html: to_html)
23
+ f.save!
24
+ end
25
+ self
26
+ end
27
+
28
+ def to_h
29
+ { type: @type, data: @data.to_h, options: @opts.to_h }
30
+ end
31
+
32
+ def to_json(type = :pretty)
33
+ return JSON.pretty_generate(to_h) if type == :pretty
34
+ to_h.to_json
35
+ end
36
+
37
+ def data(&block)
38
+ return @data unless block_given?
39
+ @data = Data.new.build(&block)
40
+ end
41
+
42
+ def opts(&block)
43
+ return @opts unless block_given?
44
+ @opts = Opts.new.build(&block)
45
+ end
46
+
47
+ def cdn(version: "2.6.0", min: true)
48
+ if min
49
+ "<script src=\"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/#{version}/Chart.min.js\"></script>"
50
+ else
51
+ "<script src=\"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/#{version}/Chart.js\"></script>"
52
+ end
53
+ end
54
+
55
+ def random_color
56
+ "##{SecureRandom.hex(3)}"
57
+ end
58
+
59
+ def random_id
60
+ SecureRandom.uuid
61
+ end
62
+
63
+ def script(config: to_json, id: random_id)
64
+ "<script>
65
+ var config = #{config}
66
+ window.onload = function() {
67
+ var ctx = document.getElementById(\"#{id}\").getContext(\"2d\");
68
+ new Chart(ctx, config);
69
+ };
70
+ </script>"
71
+ end
72
+
73
+ def to_html(width: "60%", heigth: width, head: true, cdn: head, version: "2.6.0", body: true, id: random_id, script: true)
74
+ str = []
75
+ if cdn
76
+ str << "<head>#{cdn(version: version)}</head>"
77
+ end
78
+ str << "<body>" if body
79
+ str << "<div id=\"canvas-holder\" style=\"width:#{width} heigth:#{heigth}\">"
80
+ str << "<canvas id=\"#{id}\"/>"
81
+ str << "</div>"
82
+ str << "</body>" if body
83
+ str << script(id: id) if script
84
+ str.join("\n")
85
+ end
86
+
87
+ def file(path, &block)
88
+ @file = path
89
+ @file_block = block
90
+ end
91
+
92
+ end
93
+
94
+ end
@@ -0,0 +1,3 @@
1
+ module ChartJS
2
+ VERSION = "1.0.0pre"
3
+ end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chart_js
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0pre
5
+ platform: ruby
6
+ authors:
7
+ - Kent 'picat' Gruber
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - kgruber1@emich.edu
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - chart_js.gemspec
73
+ - examples/bar_chart.rb
74
+ - examples/build.rb
75
+ - examples/debug.rb
76
+ - examples/line_chart.rb
77
+ - examples/radar_chart.rb
78
+ - examples/sinatra.rb
79
+ - lib/chart_js.rb
80
+ - lib/chart_js/chart/bar_chart/background.rb
81
+ - lib/chart_js/chart/bar_chart/bar_chart.rb
82
+ - lib/chart_js/chart/bar_chart/border.rb
83
+ - lib/chart_js/chart/bar_chart/data.rb
84
+ - lib/chart_js/chart/bar_chart/dataset.rb
85
+ - lib/chart_js/chart/bar_chart/dataset/background.rb
86
+ - lib/chart_js/chart/bar_chart/dataset/border.rb
87
+ - lib/chart_js/chart/bar_chart/dataset/line.rb
88
+ - lib/chart_js/chart/bar_chart/dataset/point.rb
89
+ - lib/chart_js/chart/bar_chart/dataset/point_border.rb
90
+ - lib/chart_js/chart/bar_chart/dataset/point_hover.rb
91
+ - lib/chart_js/chart/bar_chart/file.rb
92
+ - lib/chart_js/chart/bar_chart/line.rb
93
+ - lib/chart_js/chart/bar_chart/opts.rb
94
+ - lib/chart_js/chart/bar_chart/point.rb
95
+ - lib/chart_js/chart/bar_chart/point_border.rb
96
+ - lib/chart_js/chart/bar_chart/point_hover.rb
97
+ - lib/chart_js/chart/chart.rb
98
+ - lib/chart_js/chart/charts.rb
99
+ - lib/chart_js/chart/data.rb
100
+ - lib/chart_js/chart/dataset.rb
101
+ - lib/chart_js/chart/dataset/background.rb
102
+ - lib/chart_js/chart/dataset/border.rb
103
+ - lib/chart_js/chart/dataset/line.rb
104
+ - lib/chart_js/chart/dataset/point.rb
105
+ - lib/chart_js/chart/dataset/point_border.rb
106
+ - lib/chart_js/chart/dataset/point_hover.rb
107
+ - lib/chart_js/chart/file.rb
108
+ - lib/chart_js/chart/line_chart.rb
109
+ - lib/chart_js/chart/line_chart/background.rb
110
+ - lib/chart_js/chart/line_chart/border.rb
111
+ - lib/chart_js/chart/line_chart/data.rb
112
+ - lib/chart_js/chart/line_chart/dataset.rb
113
+ - lib/chart_js/chart/line_chart/dataset/background.rb
114
+ - lib/chart_js/chart/line_chart/dataset/border.rb
115
+ - lib/chart_js/chart/line_chart/dataset/line.rb
116
+ - lib/chart_js/chart/line_chart/dataset/point.rb
117
+ - lib/chart_js/chart/line_chart/dataset/point_border.rb
118
+ - lib/chart_js/chart/line_chart/dataset/point_hover.rb
119
+ - lib/chart_js/chart/line_chart/file.rb
120
+ - lib/chart_js/chart/line_chart/line.rb
121
+ - lib/chart_js/chart/line_chart/line_chart.rb
122
+ - lib/chart_js/chart/line_chart/opts.rb
123
+ - lib/chart_js/chart/line_chart/point.rb
124
+ - lib/chart_js/chart/line_chart/point_border.rb
125
+ - lib/chart_js/chart/line_chart/point_hover.rb
126
+ - lib/chart_js/chart/opts.rb
127
+ - lib/chart_js/chart/radar_chart/background.rb
128
+ - lib/chart_js/chart/radar_chart/border.rb
129
+ - lib/chart_js/chart/radar_chart/data.rb
130
+ - lib/chart_js/chart/radar_chart/dataset.rb
131
+ - lib/chart_js/chart/radar_chart/dataset/background.rb
132
+ - lib/chart_js/chart/radar_chart/dataset/border.rb
133
+ - lib/chart_js/chart/radar_chart/dataset/line.rb
134
+ - lib/chart_js/chart/radar_chart/dataset/point.rb
135
+ - lib/chart_js/chart/radar_chart/dataset/point_border.rb
136
+ - lib/chart_js/chart/radar_chart/dataset/point_hover.rb
137
+ - lib/chart_js/chart/radar_chart/file.rb
138
+ - lib/chart_js/chart/radar_chart/line.rb
139
+ - lib/chart_js/chart/radar_chart/opts.rb
140
+ - lib/chart_js/chart/radar_chart/point.rb
141
+ - lib/chart_js/chart/radar_chart/point_border.rb
142
+ - lib/chart_js/chart/radar_chart/point_hover.rb
143
+ - lib/chart_js/chart/radar_chart/radar_chart.rb
144
+ - lib/chart_js/version.rb
145
+ homepage: https://github.com/picatz/chart_js
146
+ licenses:
147
+ - MIT
148
+ metadata: {}
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">"
161
+ - !ruby/object:Gem::Version
162
+ version: 1.3.1
163
+ requirements: []
164
+ rubyforge_project:
165
+ rubygems_version: 2.6.8
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: A simple ruby DSL to build responive charts for the web using Chart.js
169
+ test_files: []