asciidoctor-chart 1.0.0.alpha.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,226 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Asciidoctor
4
- module Chart
5
- module C3js
6
- class ChartBuilder
7
- def self.bar data, labels, attrs
8
- chart_id = get_chart_id attrs
9
- chart_div = create_chart_div chart_id
10
- chart_generate_script = chart_bar_script chart_id, data, labels, attrs
11
- to_html chart_div, chart_generate_script
12
- end
13
-
14
- def self.line data, labels, attrs
15
- chart_id = get_chart_id attrs
16
- chart_div = create_chart_div chart_id
17
- chart_generate_script = chart_line_script chart_id, data, labels, attrs
18
- to_html chart_div, chart_generate_script
19
- end
20
-
21
- def self.step data, labels, attrs
22
- chart_id = get_chart_id attrs
23
- chart_div = create_chart_div chart_id
24
- chart_generate_script = chart_step_script chart_id, data, labels, attrs
25
- to_html chart_div, chart_generate_script
26
- end
27
-
28
- def self.spline data, labels, attrs
29
- chart_id = get_chart_id attrs
30
- chart_div = create_chart_div chart_id
31
- chart_generate_script = chart_spline_script chart_id, data, labels, attrs
32
- to_html chart_div, chart_generate_script
33
- end
34
-
35
- def self.pie raw_data, attrs
36
- chart_id = get_chart_id attrs
37
- chart_div = create_chart_div chart_id
38
- chart_generate_script = chart_pie_script chart_id, raw_data, attrs
39
- to_html chart_div, chart_generate_script
40
- end
41
-
42
- def self.create_chart_div chart_id
43
- %(<div id="#{chart_id}"></div>)
44
- end
45
-
46
- def self.get_chart_id attrs
47
- attrs.fetch('id', 'chart' + PlainRubyRandom.uuid)
48
- end
49
-
50
- def self.prepare_data raw_data
51
- labels = raw_data[0]
52
- raw_data.shift
53
- raw_data.map.with_index {|row, index| row.unshift index.to_s }
54
- [raw_data, labels]
55
- end
56
-
57
- def self.chart_bar_script chart_id, data, labels, attrs
58
- chart_height = get_chart_height attrs
59
- chart_width = get_chart_width attrs
60
- axis_x_label = get_axis_x_label attrs
61
- axis_y_label = get_axis_y_label attrs
62
- data_names = get_data_names attrs
63
- <<~EOS
64
- <script>
65
- c3.generate({
66
- bindto: '##{chart_id}',
67
- size: { height: #{chart_height}, width: #{chart_width} },
68
- data: {
69
- columns: #{data.to_s},
70
- type: 'bar',
71
- names: #{data_names.to_s}
72
- },
73
- axis: {
74
- x: {
75
- type: 'category',
76
- categories: #{labels.to_s},
77
- label: '#{axis_x_label}'
78
- },
79
- y: {
80
- label: '#{axis_y_label}'
81
- }
82
- }
83
- });
84
- </script>
85
- EOS
86
- end
87
-
88
- def self.chart_line_script chart_id, data, labels, attrs
89
- chart_height = get_chart_height attrs
90
- chart_width = get_chart_width attrs
91
- axis_x_label = get_axis_x_label attrs
92
- axis_y_label = get_axis_y_label attrs
93
- data_names = get_data_names attrs
94
- <<~EOS
95
- <script>
96
- c3.generate({
97
- bindto: '##{chart_id}',
98
- size: { height: #{chart_height}, width: #{chart_width} },
99
- data: {
100
- columns: #{data.to_s},
101
- names: #{data_names.to_s}
102
- },
103
- axis: {
104
- x: {
105
- type: 'category',
106
- categories: #{labels.to_s},
107
- label: '#{axis_x_label}'
108
- },
109
- y: {
110
- label: '#{axis_y_label}'
111
- }
112
- }
113
- });
114
- </script>
115
- EOS
116
- end
117
-
118
- def self.chart_step_script chart_id, data, labels, attrs
119
- chart_height = get_chart_height attrs
120
- chart_width = get_chart_width attrs
121
- axis_x_label = get_axis_x_label attrs
122
- axis_y_label = get_axis_y_label attrs
123
- data_names = get_data_names attrs
124
- <<~EOS
125
- <script>
126
- c3.generate({
127
- bindto: '##{chart_id}',
128
- size: { height: #{chart_height}, width: #{chart_width} },
129
- data: {
130
- columns: #{data.to_s},
131
- type: 'step',
132
- names: #{data_names.to_s}
133
- },
134
- axis: {
135
- x: {
136
- type: 'category',
137
- categories: #{labels.to_s},
138
- label: '#{axis_x_label}'
139
- },
140
- y: {
141
- label: '#{axis_y_label}'
142
- }
143
- }
144
- });
145
- </script>
146
- EOS
147
- end
148
-
149
- def self.chart_spline_script chart_id, data, labels, attrs
150
- chart_height = get_chart_height attrs
151
- chart_width = get_chart_width attrs
152
- axis_x_label = get_axis_x_label attrs
153
- axis_y_label = get_axis_y_label attrs
154
- data_names = get_data_names attrs
155
- <<~EOS
156
- <script>
157
- c3.generate({
158
- bindto: '##{chart_id}',
159
- size: { height: #{chart_height}, width: #{chart_width} },
160
- data: {
161
- columns: #{data.to_s},
162
- type: 'spline',
163
- names: #{data_names.to_s}
164
- },
165
- axis: {
166
- x: {
167
- type: 'category',
168
- categories: #{labels.to_s},
169
- label: '#{axis_x_label}'
170
- },
171
- y: {
172
- label: '#{axis_y_label}'
173
- }
174
- }
175
- });
176
- </script>
177
- EOS
178
- end
179
-
180
- def self.chart_pie_script chart_id, raw_data, attrs
181
- chart_height = get_chart_height attrs
182
- chart_width = get_chart_width attrs
183
- <<~EOS
184
- <script>
185
- c3.generate({
186
- bindto: '##{chart_id}',
187
- size: { height: #{chart_height}, width: #{chart_width} },
188
- data: {
189
- columns: #{raw_data.to_s},
190
- type: 'pie'
191
- }
192
- });
193
- </script>
194
- EOS
195
- end
196
-
197
- def self.to_html chart_div, chart_script
198
- <<~EOS
199
- #{chart_div}
200
- #{chart_script}
201
- EOS
202
- end
203
-
204
- def self.get_chart_height attrs
205
- attrs.fetch 'height', '400'
206
- end
207
-
208
- def self.get_chart_width attrs
209
- attrs.fetch 'width', '600'
210
- end
211
-
212
- def self.get_axis_x_label attrs
213
- attrs.key?('axis-x-label') ? CGI.unescapeHTML(attrs['axis-x-label']) : ''
214
- end
215
-
216
- def self.get_axis_y_label attrs
217
- attrs.key?('axis-y-label') ? CGI.unescapeHTML(attrs['axis-y-label']) : ''
218
- end
219
-
220
- def self.get_data_names attrs
221
- attrs.key?('data-names') ? CGI.unescapeHTML(attrs['data-names']) : '{}'
222
- end
223
- end
224
- end
225
- end
226
- end
@@ -1,88 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Asciidoctor
4
- module Chart
5
- module Chartist
6
- class ChartBuilder
7
- def self.bar data, labels, attrs
8
- chart_id = get_chart_id attrs
9
- chart_div = create_chart_div chart_id
10
- chart_generate_script = chart_bar_script chart_id, data, labels, attrs
11
- to_html chart_div, chart_generate_script
12
- end
13
-
14
- def self.line data, labels, attrs
15
- chart_id = get_chart_id attrs
16
- chart_div = create_chart_div chart_id
17
- chart_generate_script = chart_line_script chart_id, data, labels, attrs
18
- to_html chart_div, chart_generate_script
19
- end
20
-
21
- def self.create_chart_div chart_id
22
- %(<div id="#{chart_id}"class="ct-chart"></div>)
23
- end
24
-
25
- def self.get_chart_id attrs
26
- attrs.fetch('id', 'chart' + PlainRubyRandom.uuid)
27
- end
28
-
29
- def self.prepare_data raw_data
30
- labels = raw_data[0]
31
- raw_data.shift
32
- [raw_data, labels]
33
- end
34
-
35
- def self.chart_bar_script chart_id, data, labels, attrs
36
- chart_height = get_chart_height attrs
37
- <<~EOS
38
- <script>
39
- var options = {
40
- height: '#{chart_height}',
41
- colors: ["#72B3CC", "#8EB33B"]
42
- };
43
- var data = {
44
- labels: #{labels.to_s},
45
- series: #{data.to_s}
46
- };
47
- new Chartist.Bar('##{chart_id}', data, options);
48
- </script>
49
- EOS
50
- end
51
-
52
- def self.chart_line_script chart_id, data, labels, attrs
53
- chart_height = get_chart_height attrs
54
- chart_width = get_chart_width attrs
55
- <<~EOS
56
- <script>
57
- var options = {
58
- height: '#{chart_height}',
59
- width: '#{chart_width}',
60
- colors: ["#72B3CC", "#8EB33B"]
61
- };
62
- var data = {
63
- labels: #{labels.to_s},
64
- series: #{data.to_s}
65
- };
66
- new Chartist.Line('##{chart_id}', data, options);
67
- </script>
68
- EOS
69
- end
70
-
71
- def self.to_html chart_div, chart_script
72
- <<~EOS
73
- #{chart_div}
74
- #{chart_script}
75
- EOS
76
- end
77
-
78
- def self.get_chart_height attrs
79
- attrs.fetch 'height', '400'
80
- end
81
-
82
- def self.get_chart_width attrs
83
- attrs.fetch 'width', '600'
84
- end
85
- end
86
- end
87
- end
88
- end
@@ -1,66 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Asciidoctor
4
- module Chart
5
- module Chartjs
6
- class ChartBuilder
7
- def self.line data, labels, attrs
8
- default_colors = [{ r: 220, g: 220, b: 220 }, { r: 151, g: 187, b: 205 }]
9
- datasets = data.map do |set|
10
- color = default_colors[data.index(set) % 2]
11
- color_rgba = "rgba(#{color[:r]},#{color[:g]},#{color[:b]},1.0)"
12
- <<~EOS
13
- {
14
- fillColor: "#{color_rgba.gsub('1.0', '0.2')}",
15
- strokeColor: "#{color_rgba}",
16
- pointColor: "#{color_rgba}",
17
- pointHighlightStroke: "#{color_rgba}",
18
- pointStrokeColor: "#fff",
19
- pointHighlightFill: "#fff",
20
- data: #{set.to_s}
21
- }
22
- EOS
23
- end.join ','
24
- chart_id = attrs.fetch('id', 'chart' + PlainRubyRandom.uuid)
25
- chart_height = get_chart_height attrs
26
- chart_width = get_chart_width attrs
27
- chart_canvas = %(<div style="width:#{chart_width}px; height:#{chart_height}px"><canvas id="#{chart_id}"></canvas></div>) # rubocop:disable Layout/LineLength
28
- chart_init_ctx_script = %(var ctx = document.getElementById("#{chart_id}").getContext("2d");)
29
- chart_init_data_script = <<~EOS
30
- var data = {
31
- labels: #{labels.to_s},
32
- datasets: [
33
- #{datasets}
34
- ]
35
- };
36
- EOS
37
- chart_init_script = 'var chart = new Chart(ctx).Line(data, {responsive : true});'
38
- <<~EOS
39
- #{chart_canvas}
40
- <script>
41
- window.onload = function() {
42
- #{chart_init_ctx_script}
43
- #{chart_init_data_script}
44
- #{chart_init_script}
45
- }
46
- </script>
47
- EOS
48
- end
49
-
50
- def self.prepare_data raw_data
51
- labels = raw_data[0]
52
- raw_data.shift
53
- [raw_data, labels]
54
- end
55
-
56
- def self.get_chart_height attrs
57
- attrs.fetch 'height', '400'
58
- end
59
-
60
- def self.get_chart_width attrs
61
- attrs.fetch 'width', '600'
62
- end
63
- end
64
- end
65
- end
66
- end