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,20 @@
1
+ module ChartJS
2
+
3
+ class Background
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 color(value)
15
+ @container['backgroundColor'] = value
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,48 @@
1
+ module ChartJS
2
+
3
+ class Border
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 color(value)
15
+ @container['borderColor'] = value
16
+ end
17
+
18
+ def width(value)
19
+ if value.is_a? Array
20
+ @container['borderWidth'] = value.map(&:to_i)
21
+ else
22
+ @container['borderWidth'] = value.to_i
23
+ end
24
+ end
25
+
26
+ def dash(value)
27
+ if value.is_a? Array
28
+ @container['borderDash'] = value.map(&:to_i)
29
+ else
30
+ raise "Dash must be an array!"
31
+ end
32
+ end
33
+
34
+ def dash_offset(value)
35
+ @container['borderDashOffset'] = value.to_i
36
+ end
37
+
38
+ def cap(value)
39
+ @container['borderCapStyle'] = value
40
+ end
41
+
42
+ def join(value)
43
+ @container['borderJoinStyle'] = value
44
+ end
45
+
46
+ end
47
+
48
+ 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,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,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,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 LineChart
10
+
11
+ def initialize(&block)
12
+ @type = "line"
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,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