peity_vanilla_rails 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,376 +0,0 @@
1
- /*!
2
- Peity Vanila JS 0.0.5
3
- Copyright © 2022 RailsJazz
4
- https://railsjazz.com
5
- */
6
-
7
- function _classCallCheck(instance, Constructor) {
8
- if (!(instance instanceof Constructor)) {
9
- throw new TypeError("Cannot call a class as a function");
10
- }
11
- }
12
-
13
- function _defineProperties(target, props) {
14
- for (var i = 0; i < props.length; i++) {
15
- var descriptor = props[i];
16
- descriptor.enumerable = descriptor.enumerable || false;
17
- descriptor.configurable = true;
18
- if ("value" in descriptor) descriptor.writable = true;
19
- Object.defineProperty(target, descriptor.key, descriptor);
20
- }
21
- }
22
-
23
- function _createClass(Constructor, protoProps, staticProps) {
24
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
25
- if (staticProps) _defineProperties(Constructor, staticProps);
26
- Object.defineProperty(Constructor, "prototype", {
27
- writable: false
28
- });
29
- return Constructor;
30
- }
31
-
32
- function _defineProperty(obj, key, value) {
33
- if (key in obj) {
34
- Object.defineProperty(obj, key, {
35
- value: value,
36
- enumerable: true,
37
- configurable: true,
38
- writable: true
39
- });
40
- } else {
41
- obj[key] = value;
42
- }
43
-
44
- return obj;
45
- }
46
-
47
- var isFunction = function isFunction(o) {
48
- return o !== null && typeof o === "function" && !!o.apply;
49
- };
50
- var svgElement = function svgElement(tag, attrs) {
51
- var element = document.createElementNS("http://www.w3.org/2000/svg", tag);
52
-
53
- for (var attr in attrs) {
54
- element.setAttribute(attr, attrs[attr]);
55
- }
56
-
57
- return element;
58
- };
59
- var svgSupported = "createElementNS" in document && svgElement("svg", {}).createSVGRect();
60
-
61
- var Peity = /*#__PURE__*/function () {
62
- function Peity(element, type) {
63
- var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
64
-
65
- _classCallCheck(this, Peity);
66
-
67
- this.element = element;
68
- this.type = type;
69
- this.options = Object.assign({}, Peity.defaults[type], JSON.parse(element.dataset["peity"] || "{}"), options);
70
- }
71
-
72
- _createClass(Peity, [{
73
- key: "draw",
74
- value: function draw() {
75
- var options = this.options;
76
- Peity.graphers[this.type](this);
77
- if (isFunction(options.after)) options.after.call(this, options);
78
- }
79
- }, {
80
- key: "fill",
81
- value: function fill() {
82
- var fill = this.options.fill;
83
- return isFunction(fill) ? fill : function (_, i) {
84
- return fill[i % fill.length];
85
- };
86
- }
87
- }, {
88
- key: "prepare",
89
- value: function prepare(width, height) {
90
- if (!this.svg) {
91
- this.element.style.display = "none";
92
- this.element.after(this.svg = svgElement("svg", {
93
- class: "peity"
94
- }));
95
- }
96
-
97
- this.svg.innerHTML = "";
98
- this.svg.setAttribute("width", width);
99
- this.svg.setAttribute("height", height);
100
- return this.svg;
101
- }
102
- }, {
103
- key: "values",
104
- get: function get() {
105
- return this.element.innerText.split(this.options.delimiter).map(function (value) {
106
- return parseFloat(value);
107
- });
108
- }
109
- }, {
110
- key: "mount",
111
- value: function mount() {
112
- if (!svgSupported) return;
113
- this.element.addEventListener("change", this.draw.bind(this));
114
- this.element.dataset["_peity"] = "1";
115
- this.draw();
116
- }
117
- }, {
118
- key: "unmount",
119
- value: function unmount() {
120
- this.element.removeEventListener("change", this.draw);
121
- }
122
- }], [{
123
- key: "register",
124
- value: function register(type, defaults, grapher) {
125
- Peity.defaults[type] = defaults;
126
- Peity.graphers[type] = grapher;
127
- }
128
- }]);
129
-
130
- return Peity;
131
- }();
132
-
133
- _defineProperty(Peity, "defaults", {});
134
-
135
- _defineProperty(Peity, "graphers", {});
136
-
137
- var renderer$2 = function renderer(peity) {
138
- if (!peity.options.delimiter) {
139
- var delimiter = peity.element.innerText.match(/[^0-9\.]/);
140
- peity.options.delimiter = delimiter ? delimiter[0] : ",";
141
- }
142
-
143
- var values = peity.values.map(function (n) {
144
- return n > 0 ? n : 0;
145
- });
146
-
147
- if (peity.options.delimiter == "/") {
148
- var v1 = values[0];
149
- var v2 = values[1];
150
- values = [v1, Math.max(0, v2 - v1)];
151
- }
152
-
153
- var i = 0;
154
- var length = values.length;
155
- var sum = 0;
156
-
157
- for (; i < length; i++) {
158
- sum += values[i];
159
- }
160
-
161
- if (!sum) {
162
- length = 2;
163
- sum = 1;
164
- values = [0, 1];
165
- }
166
-
167
- var diameter = peity.options.radius * 2;
168
- var svg = peity.prepare(peity.options.width || diameter, peity.options.height || diameter);
169
- var width = svg.clientWidth;
170
- var height = svg.clientHeight;
171
- var cx = width / 2;
172
- var cy = height / 2;
173
- var radius = Math.min(cx, cy);
174
- var innerRadius = peity.options.innerRadius;
175
-
176
- if (peity.type == "donut" && !innerRadius) {
177
- innerRadius = radius * 0.5;
178
- }
179
-
180
- var fill = peity.fill();
181
-
182
- var scale = function scale(value, radius) {
183
- var radians = value / sum * Math.PI * 2 - Math.PI / 2;
184
- return [radius * Math.cos(radians) + cx, radius * Math.sin(radians) + cy];
185
- };
186
-
187
- var cumulative = 0;
188
-
189
- for (i = 0; i < length; i++) {
190
- var value = values[i];
191
- var portion = value / sum;
192
- var node = void 0;
193
- if (portion == 0) continue;
194
-
195
- if (portion == 1) {
196
- if (innerRadius) {
197
- var x2 = cx - 0.01;
198
- var y1 = cy - radius;
199
- var y2 = cy - innerRadius;
200
- node = svgElement("path", {
201
- d: ["M", cx, y1, "A", radius, radius, 0, 1, 1, x2, y1, "L", x2, y2, "A", innerRadius, innerRadius, 0, 1, 0, cx, y2].join(" "),
202
- "data-value": value
203
- });
204
- } else {
205
- node = svgElement("circle", {
206
- cx: cx,
207
- cy: cy,
208
- "data-value": value,
209
- r: radius
210
- });
211
- }
212
- } else {
213
- var cumulativePlusValue = cumulative + value;
214
- var d = ["M"].concat(scale(cumulative, radius), "A", radius, radius, 0, portion > 0.5 ? 1 : 0, 1, scale(cumulativePlusValue, radius), "L");
215
-
216
- if (innerRadius) {
217
- d = d.concat(scale(cumulativePlusValue, innerRadius), "A", innerRadius, innerRadius, 0, portion > 0.5 ? 1 : 0, 0, scale(cumulative, innerRadius));
218
- } else {
219
- d.push(cx, cy);
220
- }
221
-
222
- cumulative += value;
223
- node = svgElement("path", {
224
- d: d.join(" "),
225
- "data-value": value
226
- });
227
- }
228
-
229
- node.setAttribute("fill", fill.call(peity, value, i, values));
230
- svg.append(node);
231
- }
232
- };
233
- var defaults$2 = {
234
- fill: ["#ff9900", "#fff4dd", "#ffc66e"],
235
- radius: 8
236
- };
237
-
238
- var renderer$1 = function renderer(peity) {
239
- var values = peity.values;
240
- var max = Math.max.apply(Math, peity.options.max == undefined ? values : values.concat(peity.options.max));
241
- var min = Math.min.apply(Math, peity.options.min == undefined ? values : values.concat(peity.options.min));
242
- var svg = peity.prepare(peity.options.width, peity.options.height);
243
- var width = svg.clientWidth;
244
- var height = svg.clientHeight;
245
- var diff = max - min;
246
- var padding = peity.options.padding;
247
- var fill = peity.fill();
248
-
249
- var xScale = function xScale(input) {
250
- return input * width / values.length;
251
- };
252
-
253
- var yScale = function yScale(input) {
254
- return height - (diff ? (input - min) / diff * height : 1);
255
- };
256
-
257
- for (var i = 0; i < values.length; i++) {
258
- var x = xScale(i + padding);
259
- var w = xScale(i + 1 - padding) - x;
260
- var value = values[i];
261
- var valueY = yScale(value);
262
- var y1 = valueY;
263
- var y2 = valueY;
264
- var h = void 0;
265
-
266
- if (!diff) {
267
- h = 1;
268
- } else if (value < 0) {
269
- y1 = yScale(Math.min(max, 0));
270
- } else {
271
- y2 = yScale(Math.max(min, 0));
272
- }
273
-
274
- h = y2 - y1;
275
-
276
- if (h == 0) {
277
- h = 1;
278
- if (max > 0 && diff) y1--;
279
- }
280
-
281
- svg.append(svgElement("rect", {
282
- "data-value": value,
283
- fill: fill.call(peity, value, i, values),
284
- x: x,
285
- y: y1,
286
- width: w,
287
- height: h
288
- }));
289
- }
290
- };
291
- var defaults$1 = {
292
- delimiter: ",",
293
- fill: ["#4D89F9"],
294
- height: 16,
295
- min: 0,
296
- padding: 0.1,
297
- width: 32
298
- };
299
-
300
- var renderer = function renderer(peity) {
301
- var values = peity.values;
302
- if (values.length == 1) values.push(values[0]);
303
- var max = Math.max.apply(Math, peity.options.max == undefined ? values : values.concat(peity.options.max));
304
- var min = Math.min.apply(Math, peity.options.min == undefined ? values : values.concat(peity.options.min));
305
- var svg = peity.prepare(peity.options.width, peity.options.height);
306
- var strokeWidth = peity.options.strokeWidth;
307
- var width = svg.clientWidth;
308
- var height = svg.clientHeight - strokeWidth;
309
- var diff = max - min;
310
-
311
- var xScale = function xScale(input) {
312
- return input * (width / (values.length - 1));
313
- };
314
-
315
- var yScale = function yScale(input) {
316
- var y = height;
317
-
318
- if (diff) {
319
- y -= (input - min) / diff * height;
320
- }
321
-
322
- return y + strokeWidth / 2;
323
- };
324
-
325
- var zero = yScale(Math.max(min, 0));
326
- var coords = [0, zero];
327
-
328
- for (var i = 0; i < values.length; i++) {
329
- coords.push(xScale(i), yScale(values[i]));
330
- }
331
-
332
- coords.push(width, zero);
333
-
334
- if (peity.options.fill) {
335
- svg.append(svgElement("polygon", {
336
- fill: peity.options.fill,
337
- points: coords.join(" ")
338
- }));
339
- }
340
-
341
- if (strokeWidth) {
342
- svg.append(svgElement("polyline", {
343
- fill: "none",
344
- points: coords.slice(2, coords.length - 2).join(" "),
345
- stroke: peity.options.stroke,
346
- "stroke-width": strokeWidth,
347
- "stroke-linecap": "square"
348
- }));
349
- }
350
- };
351
- var defaults = {
352
- delimiter: ",",
353
- fill: "#c6d9fd",
354
- height: 16,
355
- min: 0,
356
- stroke: "#4d89f9",
357
- strokeWidth: 1,
358
- width: 32
359
- };
360
-
361
- Peity.register("pie", defaults$2, renderer$2);
362
- Peity.register("donut", defaults$2, renderer$2);
363
- Peity.register("bar", defaults$1, renderer$1);
364
- Peity.register("line", defaults, renderer);
365
-
366
- var peity = function peity(element, type, options) {
367
- var peity = new Peity(element, type, options);
368
- peity.mount();
369
- return peity;
370
- };
371
-
372
- peity.defaults = Peity.defaults;
373
- peity.graphers = Peity.graphers;
374
-
375
- export { peity as default };
376
-