chart_js 1.0.0pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +67 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/chart_js.gemspec +27 -0
- data/examples/bar_chart.rb +18 -0
- data/examples/build.rb +20 -0
- data/examples/debug.rb +6 -0
- data/examples/line_chart.rb +18 -0
- data/examples/radar_chart.rb +18 -0
- data/examples/sinatra.rb +23 -0
- data/lib/chart_js.rb +21 -0
- data/lib/chart_js/chart/bar_chart/background.rb +20 -0
- data/lib/chart_js/chart/bar_chart/bar_chart.rb +94 -0
- data/lib/chart_js/chart/bar_chart/border.rb +48 -0
- data/lib/chart_js/chart/bar_chart/data.rb +37 -0
- data/lib/chart_js/chart/bar_chart/dataset.rb +87 -0
- data/lib/chart_js/chart/bar_chart/dataset/background.rb +20 -0
- data/lib/chart_js/chart/bar_chart/dataset/border.rb +48 -0
- data/lib/chart_js/chart/bar_chart/dataset/line.rb +16 -0
- data/lib/chart_js/chart/bar_chart/dataset/point.rb +55 -0
- data/lib/chart_js/chart/bar_chart/dataset/point_border.rb +44 -0
- data/lib/chart_js/chart/bar_chart/dataset/point_hover.rb +44 -0
- data/lib/chart_js/chart/bar_chart/file.rb +26 -0
- data/lib/chart_js/chart/bar_chart/line.rb +16 -0
- data/lib/chart_js/chart/bar_chart/opts.rb +18 -0
- data/lib/chart_js/chart/bar_chart/point.rb +55 -0
- data/lib/chart_js/chart/bar_chart/point_border.rb +44 -0
- data/lib/chart_js/chart/bar_chart/point_hover.rb +44 -0
- data/lib/chart_js/chart/chart.rb +99 -0
- data/lib/chart_js/chart/charts.rb +22 -0
- data/lib/chart_js/chart/data.rb +37 -0
- data/lib/chart_js/chart/dataset.rb +87 -0
- data/lib/chart_js/chart/dataset/background.rb +20 -0
- data/lib/chart_js/chart/dataset/border.rb +48 -0
- data/lib/chart_js/chart/dataset/line.rb +16 -0
- data/lib/chart_js/chart/dataset/point.rb +55 -0
- data/lib/chart_js/chart/dataset/point_border.rb +44 -0
- data/lib/chart_js/chart/dataset/point_hover.rb +44 -0
- data/lib/chart_js/chart/file.rb +26 -0
- data/lib/chart_js/chart/line_chart.rb +99 -0
- data/lib/chart_js/chart/line_chart/background.rb +20 -0
- data/lib/chart_js/chart/line_chart/border.rb +48 -0
- data/lib/chart_js/chart/line_chart/data.rb +37 -0
- data/lib/chart_js/chart/line_chart/dataset.rb +87 -0
- data/lib/chart_js/chart/line_chart/dataset/background.rb +20 -0
- data/lib/chart_js/chart/line_chart/dataset/border.rb +48 -0
- data/lib/chart_js/chart/line_chart/dataset/line.rb +16 -0
- data/lib/chart_js/chart/line_chart/dataset/point.rb +55 -0
- data/lib/chart_js/chart/line_chart/dataset/point_border.rb +44 -0
- data/lib/chart_js/chart/line_chart/dataset/point_hover.rb +44 -0
- data/lib/chart_js/chart/line_chart/file.rb +26 -0
- data/lib/chart_js/chart/line_chart/line.rb +16 -0
- data/lib/chart_js/chart/line_chart/line_chart.rb +94 -0
- data/lib/chart_js/chart/line_chart/opts.rb +18 -0
- data/lib/chart_js/chart/line_chart/point.rb +55 -0
- data/lib/chart_js/chart/line_chart/point_border.rb +44 -0
- data/lib/chart_js/chart/line_chart/point_hover.rb +44 -0
- data/lib/chart_js/chart/opts.rb +18 -0
- data/lib/chart_js/chart/radar_chart/background.rb +20 -0
- data/lib/chart_js/chart/radar_chart/border.rb +48 -0
- data/lib/chart_js/chart/radar_chart/data.rb +37 -0
- data/lib/chart_js/chart/radar_chart/dataset.rb +87 -0
- data/lib/chart_js/chart/radar_chart/dataset/background.rb +20 -0
- data/lib/chart_js/chart/radar_chart/dataset/border.rb +48 -0
- data/lib/chart_js/chart/radar_chart/dataset/line.rb +16 -0
- data/lib/chart_js/chart/radar_chart/dataset/point.rb +55 -0
- data/lib/chart_js/chart/radar_chart/dataset/point_border.rb +44 -0
- data/lib/chart_js/chart/radar_chart/dataset/point_hover.rb +44 -0
- data/lib/chart_js/chart/radar_chart/file.rb +26 -0
- data/lib/chart_js/chart/radar_chart/line.rb +16 -0
- data/lib/chart_js/chart/radar_chart/opts.rb +18 -0
- data/lib/chart_js/chart/radar_chart/point.rb +55 -0
- data/lib/chart_js/chart/radar_chart/point_border.rb +44 -0
- data/lib/chart_js/chart/radar_chart/point_hover.rb +44 -0
- data/lib/chart_js/chart/radar_chart/radar_chart.rb +94 -0
- data/lib/chart_js/version.rb +3 -0
- 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,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,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,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
|