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,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,99 @@
|
|
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 = nil
|
13
|
+
@data = nil
|
14
|
+
@opts = nil
|
15
|
+
@file = false
|
16
|
+
build(&block) if block_given?
|
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 type(value = nil)
|
38
|
+
return @type if value.nil?
|
39
|
+
@type = value
|
40
|
+
end
|
41
|
+
|
42
|
+
def data(&block)
|
43
|
+
return @data unless block_given?
|
44
|
+
@data = Data.new.build(&block)
|
45
|
+
end
|
46
|
+
|
47
|
+
def opts(&block)
|
48
|
+
return @opts unless block_given?
|
49
|
+
@opts = Opts.new.build(&block)
|
50
|
+
end
|
51
|
+
|
52
|
+
def cdn(version: "2.6.0", min: true)
|
53
|
+
if min
|
54
|
+
"<script src=\"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/#{version}/Chart.min.js\"></script>"
|
55
|
+
else
|
56
|
+
"<script src=\"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/#{version}/Chart.js\"></script>"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def random_color
|
61
|
+
"##{SecureRandom.hex(3)}"
|
62
|
+
end
|
63
|
+
|
64
|
+
def random_id
|
65
|
+
SecureRandom.uuid
|
66
|
+
end
|
67
|
+
|
68
|
+
def script(config: to_json, id: random_id)
|
69
|
+
"<script>
|
70
|
+
var config = #{config}
|
71
|
+
window.onload = function() {
|
72
|
+
var ctx = document.getElementById(\"#{id}\").getContext(\"2d\");
|
73
|
+
new Chart(ctx, config);
|
74
|
+
};
|
75
|
+
</script>"
|
76
|
+
end
|
77
|
+
|
78
|
+
def to_html(width: "60%", heigth: width, head: true, cdn: head, version: "2.6.0", body: true, id: random_id, script: true)
|
79
|
+
str = []
|
80
|
+
if cdn
|
81
|
+
str << "<head>#{cdn(version: version)}</head>"
|
82
|
+
end
|
83
|
+
str << "<body>" if body
|
84
|
+
str << "<div id=\"canvas-holder\" style=\"width:#{width} heigth:#{heigth}\">"
|
85
|
+
str << "<canvas id=\"#{id}\"/>"
|
86
|
+
str << "</div>"
|
87
|
+
str << "</body>" if body
|
88
|
+
str << script(id: id) if script
|
89
|
+
str.join("\n")
|
90
|
+
end
|
91
|
+
|
92
|
+
def file(path, &block)
|
93
|
+
@file = path
|
94
|
+
@file_block = block
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
@@ -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,37 @@
|
|
1
|
+
require_relative "dataset.rb"
|
2
|
+
|
3
|
+
module ChartJS
|
4
|
+
|
5
|
+
class Data
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@container = Hash.new
|
9
|
+
@datasets = Hash.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def build(&block)
|
13
|
+
instance_eval(&block)
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
def labels(labels = nil)
|
18
|
+
raise "Not an array!" unless labels.is_a? Array
|
19
|
+
@container['labels'] = labels
|
20
|
+
end
|
21
|
+
|
22
|
+
def dataset(label, &block)
|
23
|
+
@datasets[label] = Dataset.new(label, &block)
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_h
|
27
|
+
cont = @container.dup
|
28
|
+
cont['datasets'] = []
|
29
|
+
@datasets.each do |_, data|
|
30
|
+
cont['datasets'] << data.to_h
|
31
|
+
end
|
32
|
+
cont
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require_relative "dataset/background.rb"
|
2
|
+
require_relative "dataset/border.rb"
|
3
|
+
require_relative "dataset/point.rb"
|
4
|
+
require_relative "dataset/line.rb"
|
5
|
+
|
6
|
+
module ChartJS
|
7
|
+
|
8
|
+
class Dataset
|
9
|
+
|
10
|
+
def initialize(label, &block)
|
11
|
+
@container = Hash.new
|
12
|
+
fill(false)
|
13
|
+
label(label)
|
14
|
+
build(&block)
|
15
|
+
end
|
16
|
+
|
17
|
+
def build(&block)
|
18
|
+
instance_eval(&block)
|
19
|
+
@container
|
20
|
+
end
|
21
|
+
|
22
|
+
def data(value)
|
23
|
+
@container['data'] = value
|
24
|
+
end
|
25
|
+
|
26
|
+
def label(value)
|
27
|
+
@container['label'] = value
|
28
|
+
end
|
29
|
+
|
30
|
+
def fill(value = true)
|
31
|
+
@container['fill'] = value
|
32
|
+
end
|
33
|
+
|
34
|
+
def axis_id(value, axis)
|
35
|
+
case axis
|
36
|
+
when :x
|
37
|
+
@container['xAxisID'] = value
|
38
|
+
when :y
|
39
|
+
@container['yAxisID'] = value
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def color(value = :random, type = :both)
|
44
|
+
if value == :random
|
45
|
+
c = "##{SecureRandom.hex(3)}"
|
46
|
+
color c, :border if type == :both || type == :border
|
47
|
+
color c, :background if type == :both || type == :background
|
48
|
+
return
|
49
|
+
end
|
50
|
+
case type
|
51
|
+
when :border
|
52
|
+
@container['borderColor'] = value
|
53
|
+
when :background
|
54
|
+
@container['backgroundColor'] = value
|
55
|
+
when :both
|
56
|
+
color value, :border
|
57
|
+
color value, :background
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def span_gaps(value = true)
|
62
|
+
@container['spanGaps'] = value
|
63
|
+
end
|
64
|
+
|
65
|
+
def background(&block)
|
66
|
+
@container = Background.new(@container).build(&block)
|
67
|
+
end
|
68
|
+
|
69
|
+
def border(&block)
|
70
|
+
@container = Border.new(@container).build(&block)
|
71
|
+
end
|
72
|
+
|
73
|
+
def point(&block)
|
74
|
+
@container = Point.new(@container).build(&block)
|
75
|
+
end
|
76
|
+
|
77
|
+
def line(&block)
|
78
|
+
@container = Line.new(@container).build(&block)
|
79
|
+
end
|
80
|
+
|
81
|
+
def to_h
|
82
|
+
@container
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|