peity_vanilla_rails 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3c48643fc2950ad8495f99384593c7fd1ee26732b64c9df86b62781b738a25ce
4
+ data.tar.gz: c6ef11ccda4de91fd692c17902255a719b64cfb8038dbd0aefa02ffb13b107ab
5
+ SHA512:
6
+ metadata.gz: 77de0c81a7259fb311f37804511e85654e1bd91eca5dc3a487d2b49a8a5b433a2100eaa6af5ff5c012070188316e60f3af5e9079f8902377e901efd38777b7dd
7
+ data.tar.gz: eab3b26b503ebf900d799a8cbdca94aee4acc85bc57fbba4144a9be393d03c889c0802d47931e9decfc44025d11161a3e5faab25db16c2a75933fef19276a6d6
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2022
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,103 @@
1
+ # Peity Vanilla Rails
2
+
3
+ Sparklines are small but intense charts. This gem is a wrapper around [peity_vanilla](https://github.com/railsjazz/peity_vanilla) library. You can generate simple but informative charts with vanilla JS.
4
+
5
+ ![Sparklines Rails](docs/sparklines.png)
6
+
7
+ ## Usage
8
+
9
+ 1. Add to `application.js`
10
+
11
+ ```javascript
12
+ //= require peity_vanilla.js
13
+ ```
14
+
15
+ 2. Add charts in your code:
16
+
17
+ ```ruby
18
+ <h3>Line</h3>
19
+ <%= peity_line_chart([115,123,234,-113,-43,-223,127,332,152,233]) %>
20
+ <%= peity_line_chart(100.times.map{rand(100) * [1,-1].sample}, options: { width: 240, fill: 'lightgreen', stroke: 'blue' }) %>
21
+
22
+ <h3>Bar</h3>
23
+ <%= peity_bar_chart([115,123,234,-113,-43,-223,127,332,152,233]) %>
24
+ <%= peity_bar_chart('115,123,234,-132,152,233') %>
25
+ <%= peity_bar_chart(50.times.map{rand(100) }, options: { width: 240, fill: ['orange'], height: 30, padding: -0.1 }) %>
26
+
27
+ <h3>Pie</h3>
28
+ <%= peity_pie_chart "2/3" %>
29
+ <%= peity_pie_chart [3,10] %>
30
+ <%= peity_pie_chart [3,10], options: { fill: ["red", "#eeeeee"], radius: 10 } %>
31
+ <%= peity_pie_chart [236,300] %>
32
+
33
+ <h3>Donut</h3>
34
+ <%= peity_donut_chart "2/3" %>
35
+ <%= peity_donut_chart [6,20] %>
36
+ <%= peity_donut_chart [236,300] %>
37
+ ```
38
+
39
+ ## Installation
40
+ Add this line to your application's Gemfile:
41
+
42
+ ```ruby
43
+ gem "peity_vanilla_rails"
44
+ ```
45
+
46
+ And then execute:
47
+ ```bash
48
+ $ bundle
49
+ ```
50
+
51
+ ## Default Options
52
+
53
+ More information here: https://github.com/railsjazz/peity_vanilla.
54
+
55
+ You can pass in `options` any of the attributes.
56
+
57
+ ```html
58
+ <script>
59
+ peity.defaults.pie = {
60
+ delimiter: null,
61
+ fill: ["#58508d", "#ffa600", "#ff6361"],
62
+ height: null,
63
+ radius: 8,
64
+ width: null
65
+ }
66
+
67
+ peity.defaults.donut = {
68
+ delimiter: null,
69
+ fill: ["#ff9900", "#fff4dd", "#ffd592"],
70
+ height: null,
71
+ innerRadius: null,
72
+ radius: 8,
73
+ width: null
74
+ }
75
+
76
+ peity.defaults.line = {
77
+ delimiter: ",",
78
+ fill: "#fff4dd",
79
+ height: 16,
80
+ max: null,
81
+ min: 0,
82
+ stroke: "#ffa600",
83
+ strokeWidth: 1,
84
+ width: 32
85
+ }
86
+
87
+ peity.defaults.bar = {
88
+ delimiter: ",",
89
+ fill: ["#4d89f9"],
90
+ height: 16,
91
+ max: null,
92
+ min: 0,
93
+ padding: 0.1,
94
+ width: 32
95
+ }
96
+ </script>
97
+ ```
98
+
99
+ ## Contributing
100
+ Contribution directions go here.
101
+
102
+ ## License
103
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,2 @@
1
+ !function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var i,n=e();for(i in n)("object"==typeof exports?exports:t)[i]=n[i]}}(self,()=>(()=>{"use strict";function a(t,e,i){var n;if(s)return n=(t=t).dataset._peity,n=new r(t,e,Object.assign({},a.defaults[e],JSON.parse(t.dataset.peity||"{}"),i)),t.addEventListener("change",function(){n.draw()}),(t.dataset._peity=n).draw(),t}function r(t,e,i){this.$el=t,this.type=e,this.opts=i}var t=r.prototype,M=t.svgElement=function(t,e){var i,n=document.createElementNS("http://www.w3.org/2000/svg",t);for(i in e)n.setAttribute(i,e[i]);return n},s="createElementNS"in document&&M("svg",{}).createSVGRect();return t.draw=function(){var t=this.opts;a.graphers[this.type].call(this,t),t.after&&t.after.call(this,t)},t.isFunction=function(t){return null!==t&&"function"==typeof t&&!!t.apply},t.fill=function(){var i=this.opts.fill;return t.isFunction(i)?i:function(t,e){return i[e%i.length]}},t.prepare=function(t,e){return this.$svg||(this.$el.style.display="none",this.$el.after(this.$svg=M("svg",{class:"peity"}))),this.$svg.innerHTML="",this.$svg.setAttribute("width",t),this.$svg.setAttribute("height",e),this.$svg},t.values=function(){return this.$el.innerText.split(this.opts.delimiter).map(function(t){return parseFloat(t)})},a.defaults={},a.graphers={},a.register=function(t,e,i){this.defaults[t]=e,this.graphers[t]=i},a.register("pie",{fill:["#ff9900","#fff4dd","#ffc66e"],radius:8},function(t){t.delimiter||(l=this.$el.innerText.match(/[^0-9\.]/),t.delimiter=l?l[0]:",");for(var e=this.values().map(function(t){return 0<t?t:0}),i=("/"==t.delimiter&&(l=e[0],r=e[1],e=[l,Math.max(0,r-l)]),0),n=e.length,a=0;i<n;i++)a+=e[i];a||(n=2,e=[0,a=1]);for(var r=2*t.radius,s=this.prepare(t.width||r,t.height||r),l=s.clientWidth,r=s.clientHeight,h=l/2,o=r/2,u=Math.min(h,o),c=t.innerRadius,p=("donut"!=this.type||c||(c=.5*u),Math.PI),f=this.fill(),d=this.scale=function(t,e){t=t/a*p*2-p/2;return[e*Math.cos(t)+h,e*Math.sin(t)+o]},g=0,i=0;i<n;i++){var m,v,y=e[i],x=y/a;0!=x&&((x=1==x?c?M("path",{d:["M",h,v=o-u,"A",u,u,0,1,1,m=h-.01,v,"L",m,v=o-c,"A",c,c,0,1,0,h,v].join(" "),"data-value":y}):M("circle",{cx:h,cy:o,"data-value":y,r:u}):(m=g+y,v=["M"].concat(d(g,u),"A",u,u,0,.5<x?1:0,1,d(m,u),"L"),c?v=v.concat(d(m,c),"A",c,c,0,.5<x?1:0,0,d(g,c)):v.push(h,o),g+=y,M("path",{d:v.join(" "),"data-value":y}))).setAttribute("fill",f.call(this,y,i,e)),s.append(x))}}),a.register("donut",Object.assign(!0,{},a.defaults.pie),function(t){a.graphers.pie.call(this,t)}),a.register("bar",{delimiter:",",fill:["#4D89F9"],height:16,min:0,padding:.1,width:32},function(t){for(var e=this.values(),i=Math.max.apply(Math,null==t.max?e:e.concat(t.max)),n=Math.min.apply(Math,null==t.min?e:e.concat(t.min)),a=this.prepare(t.width,t.height),r=a.clientWidth,s=a.clientHeight,l=i-n,h=t.padding,o=this.fill(),u=this.x=function(t){return t*r/e.length},c=this.y=function(t){return s-(l?(t-n)/l*s:1)},p=0;p<e.length;p++){var f,d=u(p+h),g=u(p+1-h)-d,m=e[p],v=c(m),y=v;l?m<0?y=c(Math.min(i,0)):v=c(Math.max(n,0)):f=1,0==(f=v-y)&&(f=1,0<i&&l&&y--),a.append(M("rect",{"data-value":m,fill:o.call(this,m,p,e),x:d,y:y,width:g,height:f}))}}),a.register("line",{delimiter:",",fill:"#c6d9fd",height:16,min:0,stroke:"#4d89f9",strokeWidth:1,width:32},function(t){for(var e=this.values(),i=(1==e.length&&e.push(e[0]),Math.max.apply(Math,null==t.max?e:e.concat(t.max))),n=Math.min.apply(Math,null==t.min?e:e.concat(t.min)),a=this.prepare(t.width,t.height),r=t.strokeWidth,s=a.clientWidth,l=a.clientHeight-r,h=i-n,o=this.x=function(t){return t*(s/(e.length-1))},u=this.y=function(t){var e=l;return h&&(e-=(t-n)/h*l),e+r/2},i=u(Math.max(n,0)),c=[0,i],p=0;p<e.length;p++)c.push(o(p),u(e[p]));c.push(s,i),t.fill&&a.append(M("polygon",{fill:t.fill,points:c.join(" ")})),r&&a.append(M("polyline",{fill:"none",points:c.slice(2,c.length-2).join(" "),stroke:t.stroke,"stroke-width":r,"stroke-linecap":"square"}))}),window.peity=a,{}})());
2
+ //# sourceMappingURL=peity_vanilla.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["webpack://piety_vanilla/webpack/universalModuleDefinition","webpack://piety_vanilla/./src/index.js"],"names":["root","factory","exports","module","define","amd","i","a","self","peity","element","type","options","chart","svgSupported","$this","dataset","Peity","Object","assign","defaults","JSON","parse","addEventListener","draw","$el","opts","this","PeityPrototype","prototype","svgElement","tag","attrs","attr","document","createElementNS","setAttribute","createSVGRect","graphers","call","after","isFunction","o","apply","fill","_","length","prepare","width","height","$svg","style","display","innerHTML","values","innerText","split","delimiter","map","value","parseFloat","register","grapher","radius","match","n","v1","v2","Math","max","sum","diameter","clientWidth","clientHeight","cx","cy","min","innerRadius","pi","PI","scale","radians","cos","sin","cumulative","cumulativePlusValue","d","portion","$node","y1","x2","y2","join","r","concat","push","append","pie","padding","undefined","diff","xScale","x","input","yScale","y","h","w","valueY","stroke","strokeWidth","zero","coords","points","slice","window"],"mappings":"CAAA,SAA2CA,EAAMC,GAChD,GAAsB,iBAAZC,SAA0C,iBAAXC,OACxCA,OAAOD,QAAUD,SACb,GAAqB,mBAAXG,QAAyBA,OAAOC,IAC9CD,OAAO,GAAIH,OACP,CACJ,IACQK,EADJC,EAAIN,IACR,IAAQK,KAAKC,GAAuB,iBAAZL,QAAuBA,QAAUF,GAAMM,GAAKC,EAAED,IAPxE,CASGE,KAAM,K,kBCPG,SAARC,EAAiBC,EAASC,EAAMC,GAClC,IAIEC,EAJF,GAAIC,EAiBF,OAfID,GADAE,EAAQL,GACMM,QAAgB,OAElCH,EAAQ,IAAII,EACVF,EACAJ,EACAO,OAAOC,OAAO,GACZV,EAAMW,SAAST,GACfU,KAAKC,MAAMP,EAAMC,QAAe,OAAK,MACrCJ,IAGJG,EAAMQ,iBAAiB,SAAU,WAAaV,EAAMW,UACpDT,EAAMC,QAAgB,OAAIH,GAEpBW,OACCT,EAIC,SAARE,EAAiBQ,EAAKd,EAAMe,GAC9BC,KAAKF,IAAMA,EACXE,KAAKhB,KAAOA,EACZgB,KAAKD,KAAOA,E,IAGVE,EAAiBX,EAAMY,UAEvBC,EAAaF,EAAeE,WAAa,SAASC,EAAKC,GACzD,IACQC,EADJvB,EAAUwB,SAASC,gBAAgB,6BAA8BJ,GACrE,IAAQE,KAAQD,EACdtB,EAAQ0B,aAAaH,EAAMD,EAAMC,IAEnC,OA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B+B,MAAZzE,EAAKkD,IAAmBtB,EAASA,EAAOwC,OAAOpE,EAAKkD,MAE/E1B,EAAOvB,KAAKoB,QAAQrB,EAAKsB,MAAOtB,EAAKuB,QACrC6D,EAAcpF,EAAKoF,YACnB9D,EAAQE,EAAKsB,YACbvB,EAASC,EAAKuB,aAAeqC,EAC7BV,EAAO/B,EAAMO,EAEbyB,EAAS1E,KAAK2E,EAAI,SAASC,GAC7B,OAAOA,GAASvD,GAASM,EAAOR,OAAS,KAGvC0D,EAAS7E,KAAK8E,EAAI,SAASF,GAC7B,IAAIE,EAAIxD,EAMR,OAJImD,IACFK,IAAOF,EAAQ3B,GAAOwB,EAAQnD,GAGzBwD,EAAIK,EAAc,GAGvBC,EAAOP,EAAOpC,KAAKC,IAAIO,EAAK,IAC5BoC,EAAS,CAAC,EAAGD,GAERzG,EAAI,EAAGA,EAAIgD,EAAOR,OAAQxC,IACjC0G,EAAOjB,KACLM,EAAO/F,GACPkG,EAAOlD,EAAOhD,KAIlB0G,EAAOjB,KAAK/C,EAAO+D,GAEfrF,EAAKkB,MACPM,EAAK8C,OACHlE,EAAW,UAAW,CACpBc,KAAMlB,EAAKkB,KACXqE,OAAQD,EAAOpB,KAAK,QAKtBkB,GACF5D,EAAK8C,OACHlE,EAAW,WAAY,CACrBc,KAAM,OACNqE,OAAQD,EAAOE,MAAM,EAAGF,EAAOlE,OAAS,GAAG8C,KAAK,KAChDiB,OAAQnF,EAAKmF,OACb,eAAgBC,EAChB,iBAAkB,cAQ5BK,OAAO1G,MAAQA,E","file":"peity_vanilla.js","sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse {\n\t\tvar a = factory();\n\t\tfor(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];\n\t}\n})(self, () => {\nreturn ","\"use strict\";\r\n\r\nvar peity = function(element, type, options) {\r\n if (svgSupported) {\r\n var $this = element\r\n var chart = $this.dataset['_peity']\r\n\r\n chart = new Peity(\r\n $this,\r\n type,\r\n Object.assign({},\r\n peity.defaults[type],\r\n JSON.parse($this.dataset['peity'] || '{}'),\r\n options)\r\n )\r\n\r\n $this.addEventListener(\"change\", function() { chart.draw() })\r\n $this.dataset['_peity'] = chart\r\n\r\n chart.draw()\r\n return $this;\r\n }\r\n};\r\n\r\nvar Peity = function($el, type, opts) {\r\n this.$el = $el\r\n this.type = type\r\n this.opts = opts\r\n}\r\n\r\nvar PeityPrototype = Peity.prototype\r\n\r\nvar svgElement = PeityPrototype.svgElement = function(tag, attrs) {\r\n var element = document.createElementNS('http://www.w3.org/2000/svg', tag)\r\n for(var attr in attrs) {\r\n element.setAttribute(attr, attrs[attr])\r\n }\r\n return element;\r\n}\r\n\r\n// https://gist.github.com/madrobby/3201472\r\nvar svgSupported = 'createElementNS' in document && svgElement('svg', {}).createSVGRect()\r\n\r\nPeityPrototype.draw = function() {\r\n var opts = this.opts\r\n peity.graphers[this.type].call(this, opts)\r\n if (opts.after) opts.after.call(this, opts)\r\n}\r\n\r\nPeityPrototype.isFunction = function(o) { return null !== o && \"function\" === typeof o && !!o.apply; }\r\n\r\nPeityPrototype.fill = function() {\r\n var fill = this.opts.fill\r\n\r\n return PeityPrototype.isFunction(fill)\r\n ? fill\r\n : function(_, i) { return fill[i % fill.length] }\r\n}\r\n\r\nPeityPrototype.prepare = function(width, height) {\r\n if (!this.$svg) {\r\n this.$el.style.display = 'none'\r\n this.$el.after(\r\n this.$svg = svgElement('svg', {\r\n \"class\": \"peity\"\r\n })\r\n )\r\n }\r\n\r\n this.$svg.innerHTML = \"\";\r\n this.$svg.setAttribute(\"width\", width)\r\n this.$svg.setAttribute(\"height\", height)\r\n\r\n return this.$svg;\r\n}\r\n\r\nPeityPrototype.values = function() {\r\n return this.$el.innerText.split(this.opts.delimiter).map(function(value) {\r\n return parseFloat(value)\r\n })\r\n}\r\n\r\npeity.defaults = {}\r\npeity.graphers = {}\r\n\r\npeity.register = function(type, defaults, grapher) {\r\n this.defaults[type] = defaults\r\n this.graphers[type] = grapher\r\n}\r\n\r\npeity.register(\r\n 'pie',\r\n {\r\n fill: ['#ff9900', '#fff4dd', '#ffc66e'],\r\n radius: 8\r\n },\r\n function(opts) {\r\n if (!opts.delimiter) {\r\n var delimiter = this.$el.innerText.match(/[^0-9\\.]/)\r\n opts.delimiter = delimiter ? delimiter[0] : \",\"\r\n }\r\n\r\n var values = this.values().map(function(n) {\r\n return n > 0 ? n : 0\r\n });\r\n\r\n if (opts.delimiter == \"/\") {\r\n var v1 = values[0]\r\n var v2 = values[1]\r\n values = [v1, Math.max(0, v2 - v1)]\r\n }\r\n\r\n var i = 0\r\n var length = values.length\r\n var sum = 0\r\n\r\n for (; i < length; i++) {\r\n sum += values[i]\r\n }\r\n\r\n if (!sum) {\r\n length = 2\r\n sum = 1\r\n values = [0, 1]\r\n }\r\n\r\n var diameter = opts.radius * 2\r\n\r\n var $svg = this.prepare(\r\n opts.width || diameter,\r\n opts.height || diameter\r\n )\r\n\r\n var width = $svg.clientWidth\r\n , height = $svg.clientHeight\r\n , cx = width / 2\r\n , cy = height / 2\r\n\r\n var radius = Math.min(cx, cy)\r\n , innerRadius = opts.innerRadius\r\n\r\n if (this.type == 'donut' && !innerRadius) {\r\n innerRadius = radius * 0.5\r\n }\r\n\r\n var pi = Math.PI\r\n var fill = this.fill()\r\n\r\n var scale = this.scale = function(value, radius) {\r\n var radians = value / sum * pi * 2 - pi / 2\r\n\r\n return [\r\n radius * Math.cos(radians) + cx,\r\n radius * Math.sin(radians) + cy\r\n ]\r\n }\r\n\r\n var cumulative = 0\r\n\r\n for (i = 0; i < length; i++) {\r\n var value = values[i]\r\n , portion = value / sum\r\n , $node\r\n\r\n if (portion == 0) continue\r\n\r\n if (portion == 1) {\r\n if (innerRadius) {\r\n var x2 = cx - 0.01\r\n , y1 = cy - radius\r\n , y2 = cy - innerRadius\r\n\r\n $node = svgElement('path', {\r\n d: [\r\n 'M', cx, y1,\r\n 'A', radius, radius, 0, 1, 1, x2, y1,\r\n 'L', x2, y2,\r\n 'A', innerRadius, innerRadius, 0, 1, 0, cx, y2\r\n ].join(' '),\r\n 'data-value': value,\r\n })\r\n } else {\r\n $node = svgElement('circle', {\r\n cx: cx,\r\n cy: cy,\r\n 'data-value': value,\r\n r: radius\r\n })\r\n }\r\n } else {\r\n var cumulativePlusValue = cumulative + value\r\n\r\n var d = ['M'].concat(\r\n scale(cumulative, radius),\r\n 'A', radius, radius, 0, portion > 0.5 ? 1 : 0, 1,\r\n scale(cumulativePlusValue, radius),\r\n 'L'\r\n )\r\n\r\n if (innerRadius) {\r\n d = d.concat(\r\n scale(cumulativePlusValue, innerRadius),\r\n 'A', innerRadius, innerRadius, 0, portion > 0.5 ? 1 : 0, 0,\r\n scale(cumulative, innerRadius)\r\n )\r\n } else {\r\n d.push(cx, cy)\r\n }\r\n\r\n cumulative += value\r\n\r\n $node = svgElement('path', {\r\n d: d.join(\" \"),\r\n 'data-value': value,\r\n })\r\n }\r\n\r\n $node.setAttribute('fill', fill.call(this, value, i, values))\r\n\r\n $svg.append($node)\r\n }\r\n }\r\n)\r\n\r\npeity.register(\r\n 'donut',\r\n Object.assign(true, {}, peity.defaults.pie),\r\n function(opts) {\r\n peity.graphers.pie.call(this, opts)\r\n }\r\n)\r\n\r\npeity.register(\r\n 'bar',\r\n {\r\n delimiter: \",\",\r\n fill: [\"#4D89F9\"],\r\n height: 16,\r\n min: 0,\r\n padding: 0.1,\r\n width: 32\r\n },\r\n function(opts) {\r\n var values = this.values()\r\n , max = Math.max.apply(Math, opts.max == undefined ? values : values.concat(opts.max))\r\n , min = Math.min.apply(Math, opts.min == undefined ? values : values.concat(opts.min))\r\n\r\n var $svg = this.prepare(opts.width, opts.height)\r\n , width = $svg.clientWidth\r\n , height = $svg.clientHeight\r\n , diff = max - min\r\n , padding = opts.padding\r\n , fill = this.fill()\r\n\r\n var xScale = this.x = function(input) {\r\n return input * width / values.length\r\n }\r\n\r\n var yScale = this.y = function(input) {\r\n return height - (\r\n diff\r\n ? ((input - min) / diff) * height\r\n : 1\r\n )\r\n }\r\n\r\n for (var i = 0; i < values.length; i++) {\r\n var x = xScale(i + padding)\r\n , w = xScale(i + 1 - padding) - x\r\n , value = values[i]\r\n , valueY = yScale(value)\r\n , y1 = valueY\r\n , y2 = valueY\r\n , h\r\n\r\n if (!diff) {\r\n h = 1\r\n } else if (value < 0) {\r\n y1 = yScale(Math.min(max, 0))\r\n } else {\r\n y2 = yScale(Math.max(min, 0))\r\n }\r\n\r\n h = y2 - y1\r\n\r\n if (h == 0) {\r\n h = 1\r\n if (max > 0 && diff) y1--\r\n }\r\n\r\n $svg.append(\r\n svgElement('rect', {\r\n 'data-value': value,\r\n fill: fill.call(this, value, i, values),\r\n x: x,\r\n y: y1,\r\n width: w,\r\n height: h\r\n })\r\n )\r\n }\r\n }\r\n);\r\n\r\npeity.register(\r\n \"line\",\r\n {\r\n delimiter: \",\",\r\n fill: \"#c6d9fd\",\r\n height: 16,\r\n min: 0,\r\n stroke: \"#4d89f9\",\r\n strokeWidth: 1,\r\n width: 32\r\n },\r\n function(opts) {\r\n var values = this.values()\r\n if (values.length == 1) values.push(values[0])\r\n var max = Math.max.apply(Math, opts.max == undefined ? values : values.concat(opts.max))\r\n , min = Math.min.apply(Math, opts.min == undefined ? values : values.concat(opts.min))\r\n\r\n var $svg = this.prepare(opts.width, opts.height)\r\n , strokeWidth = opts.strokeWidth\r\n , width = $svg.clientWidth\r\n , height = $svg.clientHeight - strokeWidth\r\n , diff = max - min\r\n\r\n var xScale = this.x = function(input) {\r\n return input * (width / (values.length - 1))\r\n }\r\n\r\n var yScale = this.y = function(input) {\r\n var y = height\r\n\r\n if (diff) {\r\n y -= ((input - min) / diff) * height\r\n }\r\n\r\n return y + strokeWidth / 2\r\n }\r\n\r\n var zero = yScale(Math.max(min, 0))\r\n , coords = [0, zero]\r\n\r\n for (var i = 0; i < values.length; i++) {\r\n coords.push(\r\n xScale(i),\r\n yScale(values[i])\r\n )\r\n }\r\n\r\n coords.push(width, zero)\r\n\r\n if (opts.fill) {\r\n $svg.append(\r\n svgElement('polygon', {\r\n fill: opts.fill,\r\n points: coords.join(' ')\r\n })\r\n )\r\n }\r\n\r\n if (strokeWidth) {\r\n $svg.append(\r\n svgElement('polyline', {\r\n fill: 'none',\r\n points: coords.slice(2, coords.length - 2).join(' '),\r\n stroke: opts.stroke,\r\n 'stroke-width': strokeWidth,\r\n 'stroke-linecap': 'square'\r\n })\r\n )\r\n }\r\n }\r\n);\r\n\r\n// rework?\r\nwindow.peity = peity;"],"sourceRoot":""}
@@ -0,0 +1,22 @@
1
+ module PeityVanillaRails
2
+ class Railtie < ::Rails::Engine
3
+ initializer "peity_vanilla_rails.importmap", before: "importmap" do |app|
4
+ if defined?(Importmap)
5
+ app.config.importmap.cache_sweepers << root.join("app/assets/javascripts")
6
+ end
7
+ end
8
+
9
+ PRECOMPILE_ASSETS = Dir[root.join("app/assets/javascripts/**/*")]
10
+ initializer 'peity_vanilla_rails.assets' do |app|
11
+ if app.config.respond_to?(:assets)
12
+ app.config.assets.precompile += PRECOMPILE_ASSETS
13
+ end
14
+ end
15
+
16
+ initializer 'peity_vanilla_rails.helpers', before: :load_config_initializers do
17
+ ActiveSupport.on_load :action_view do
18
+ include PeityVanillaRails::Helpers
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,36 @@
1
+ module PeityVanillaRails
2
+ module Helpers
3
+
4
+ def peity_line_chart(data, options: {})
5
+ peity_chart(data, "line", ",", options: options)
6
+ end
7
+
8
+ def peity_bar_chart(data, options: {})
9
+ peity_chart(data, "bar", ",", options: options)
10
+ end
11
+
12
+ def peity_pie_chart(data, options: {})
13
+ peity_chart(data, "pie", "/", options: options)
14
+ end
15
+
16
+ def peity_donut_chart(data, options: {})
17
+ peity_chart(data, "donut", "/", options: options)
18
+ end
19
+
20
+ def peity_chart(value, type, delimiter, options: {})
21
+ value = value.is_a?(Array) ? value.join(delimiter) : value
22
+ id = "peity_charts_#{Digest::SHA1.hexdigest([Time.now, rand].join)}"
23
+ [
24
+ tag.span(class: 'peity_charts', id: id, "data-peity" => options.to_json) do
25
+ value
26
+ end,
27
+ tag.script do
28
+ %Q{
29
+ peity(document.getElementById("#{id}"), "#{type}");
30
+ }.html_safe
31
+ end
32
+ ].join.html_safe
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module PeityVanillaRails
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "peity_vanilla_rails/version"
2
+ require "peity_vanilla_rails/helpers"
3
+ require "peity_vanilla_rails/engine"
4
+
5
+ module PeityVanillaRails
6
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :peity_vanilla_rails do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: peity_vanilla_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Igor Kasyanchuk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-04-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: puma
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sprockets-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Sparklines are small but intense charts.
70
+ email:
71
+ - igorkasyanchuk@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - MIT-LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - app/assets/javascripts/peity_vanilla.js
80
+ - app/assets/javascripts/peity_vanilla.js.map
81
+ - lib/peity_vanilla_rails.rb
82
+ - lib/peity_vanilla_rails/engine.rb
83
+ - lib/peity_vanilla_rails/helpers.rb
84
+ - lib/peity_vanilla_rails/version.rb
85
+ - lib/tasks/peity_vanilla_rails_tasks.rake
86
+ homepage: https://github.com/railsjazz/peity_vanilla_rails
87
+ licenses:
88
+ - MIT
89
+ metadata:
90
+ homepage_uri: https://github.com/railsjazz/peity_vanilla_rails
91
+ source_code_uri: https://github.com/railsjazz/peity_vanilla_rails
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubygems_version: 3.2.3
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Sparklines are small but intense charts.
111
+ test_files: []