OCA reporting engine fork for dev and update.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
13 KiB

  1. ;
  2. /*
  3. * AshAlom Gauge Meter. Version 2.0.0
  4. * Copyright AshAlom.com All rights reserved.
  5. * https://github.com/AshAlom/GaugeMeter <- Deleted!
  6. * https://github.com/githubsrinath/GaugeMeter <- Backup original.
  7. *
  8. * Original created by Dr Ash Alom
  9. *
  10. * This is a bug fixed and modified version of the AshAlom Gauge Meter.
  11. * Copyright 2018 Michael Wolf (Mictronics)
  12. * https://github.com/mictronics/GaugeMeter
  13. *
  14. */
  15. !function ($) {
  16. $.fn.gaugeMeter = function (t) {
  17. var defaults = $.extend({
  18. id: "",
  19. percent: 0,
  20. used: null,
  21. min: null,
  22. total: null,
  23. size: 100,
  24. prepend: "",
  25. append: "",
  26. theme: "Red-Gold-Green",
  27. color: "",
  28. back: "RGBa(0,0,0,.06)",
  29. width: 3,
  30. style: "Full",
  31. stripe: "0",
  32. animationstep: 1,
  33. animate_gauge_colors: false,
  34. animate_text_colors: false,
  35. label: "",
  36. label_color: "Black",
  37. text: "",
  38. text_size: 0.22,
  39. fill: "",
  40. showvalue: false
  41. }, t);
  42. return this.each(function () {
  43. function getThemeColor(e) {
  44. var t = "#2C94E0";
  45. return e || (e = 1e-14),
  46. "Red-Gold-Green" === option.theme && (e > 0 && (t = "#d90000"), e > 10 && (t = "#e32100"), e > 20 && (t = "#f35100"), e > 30 && (t = "#ff8700"), e > 40 && (t = "#ffb800"), e > 50 && (t = "#ffd900"), e > 60 && (t = "#dcd800"), e > 70 && (t = "#a6d900"), e > 80 && (t = "#69d900"), e > 90 && (t = "#32d900")),
  47. "Green-Gold-Red" === option.theme && (e > 0 && (t = "#32d900"), e > 10 && (t = "#69d900"), e > 20 && (t = "#a6d900"), e > 30 && (t = "#dcd800"), e > 40 && (t = "#ffd900"), e > 50 && (t = "#ffb800"), e > 60 && (t = "#ff8700"), e > 70 && (t = "#f35100"), e > 80 && (t = "#e32100"), e > 90 && (t = "#d90000")),
  48. "Green-Red" === option.theme && (e > 0 && (t = "#32d900"), e > 10 && (t = "#41c900"), e > 20 && (t = "#56b300"), e > 30 && (t = "#6f9900"), e > 40 && (t = "#8a7b00"), e > 50 && (t = "#a75e00"), e > 60 && (t = "#c24000"), e > 70 && (t = "#db2600"), e > 80 && (t = "#f01000"), e > 90 && (t = "#ff0000")),
  49. "Red-Green" === option.theme && (e > 0 && (t = "#ff0000"), e > 10 && (t = "#f01000"), e > 20 && (t = "#db2600"), e > 30 && (t = "#c24000"), e > 40 && (t = "#a75e00"), e > 50 && (t = "#8a7b00"), e > 60 && (t = "#6f9900"), e > 70 && (t = "#56b300"), e > 80 && (t = "#41c900"), e > 90 && (t = "#32d900")),
  50. "DarkBlue-LightBlue" === option.theme && (e > 0 && (t = "#2c94e0"), e > 10 && (t = "#2b96e1"), e > 20 && (t = "#2b99e4"), e > 30 && (t = "#2a9ce7"), e > 40 && (t = "#28a0e9"), e > 50 && (t = "#26a4ed"), e > 60 && (t = "#25a8f0"), e > 70 && (t = "#24acf3"), e > 80 && (t = "#23aff5"), e > 90 && (t = "#21b2f7")),
  51. "LightBlue-DarkBlue" === option.theme && (e > 0 && (t = "#21b2f7"), e > 10 && (t = "#23aff5"), e > 20 && (t = "#24acf3"), e > 30 && (t = "#25a8f0"), e > 40 && (t = "#26a4ed"), e > 50 && (t = "#28a0e9"), e > 60 && (t = "#2a9ce7"), e > 70 && (t = "#2b99e4"), e > 80 && (t = "#2b96e1"), e > 90 && (t = "#2c94e0")),
  52. "DarkRed-LightRed" === option.theme && (e > 0 && (t = "#d90000"), e > 10 && (t = "#dc0000"), e > 20 && (t = "#e00000"), e > 30 && (t = "#e40000"), e > 40 && (t = "#ea0000"), e > 50 && (t = "#ee0000"), e > 60 && (t = "#f30000"), e > 70 && (t = "#f90000"), e > 80 && (t = "#fc0000"), e > 90 && (t = "#ff0000")),
  53. "LightRed-DarkRed" === option.theme && (e > 0 && (t = "#ff0000"), e > 10 && (t = "#fc0000"), e > 20 && (t = "#f90000"), e > 30 && (t = "#f30000"), e > 40 && (t = "#ee0000"), e > 50 && (t = "#ea0000"), e > 60 && (t = "#e40000"), e > 70 && (t = "#e00000"), e > 80 && (t = "#dc0000"), e > 90 && (t = "#d90000")),
  54. "DarkGreen-LightGreen" === option.theme && (e > 0 && (t = "#32d900"), e > 10 && (t = "#33db00"), e > 20 && (t = "#34df00"), e > 30 && (t = "#34e200"), e > 40 && (t = "#36e700"), e > 50 && (t = "#37ec00"), e > 60 && (t = "#38f100"), e > 70 && (t = "#38f600"), e > 80 && (t = "#39f900"), e > 90 && (t = "#3afc00")),
  55. "LightGreen-DarkGreen" === option.theme && (e > 0 && (t = "#3afc00"), e > 10 && (t = "#39f900"), e > 20 && (t = "#38f600"), e > 30 && (t = "#38f100"), e > 40 && (t = "#37ec00"), e > 50 && (t = "#36e700"), e > 60 && (t = "#34e200"), e > 70 && (t = "#34df00"), e > 80 && (t = "#33db00"), e > 90 && (t = "#32d900")),
  56. "DarkGold-LightGold" === option.theme && (e > 0 && (t = "#ffb800"), e > 10 && (t = "#ffba00"), e > 20 && (t = "#ffbd00"), e > 30 && (t = "#ffc200"), e > 40 && (t = "#ffc600"), e > 50 && (t = "#ffcb00"), e > 60 && (t = "#ffcf00"), e > 70 && (t = "#ffd400"), e > 80 && (t = "#ffd600"), e > 90 && (t = "#ffd900")),
  57. "LightGold-DarkGold" === option.theme && (e > 0 && (t = "#ffd900"), e > 10 && (t = "#ffd600"), e > 20 && (t = "#ffd400"), e > 30 && (t = "#ffcf00"), e > 40 && (t = "#ffcb00"), e > 50 && (t = "#ffc600"), e > 60 && (t = "#ffc200"), e > 70 && (t = "#ffbd00"), e > 80 && (t = "#ffba00"), e > 90 && (t = "#ffb800")),
  58. "White" === option.theme && (t = "#fff"),
  59. "Black" === option.theme && (t = "#000"),
  60. t;
  61. }
  62. /* The label below gauge. */
  63. function createLabel(t, a) {
  64. if(t.children("b").length === 0){
  65. $("<b></b>").appendTo(t).html(option.label).css({
  66. "line-height": option.size + 5 * a + "px",
  67. color: option.label_color
  68. });
  69. }
  70. }
  71. /* Prepend and append text, the gauge text or percentage value. */
  72. function createSpanTag(t) {
  73. var fgcolor = "";
  74. if (option.animate_text_colors === true){
  75. fgcolor = option.fgcolor;
  76. }
  77. var child = t.children("span");
  78. if(child.length !== 0){
  79. child.html(r).css({color: fgcolor});
  80. return;
  81. }
  82. if(option.text_size <= 0.0 || Number.isNaN(option.text_size)){
  83. option.text_size = 0.22;
  84. }
  85. if(option.text_size > 0.5){
  86. option.text_size = 0.5;
  87. }
  88. $("<span></span>").appendTo(t).html(r).css({
  89. "line-height": option.size + "px",
  90. "font-size": option.text_size * option.size + "px",
  91. color: fgcolor
  92. });
  93. }
  94. /* Get data attributes as options from div tag. Fall back to defaults when not exists. */
  95. function getDataAttr(t) {
  96. $.each(dataAttr, function (index, element) {
  97. if(t.data(element) !== undefined && t.data(element) !== null){
  98. option[element] = t.data(element);
  99. } else {
  100. option[element] = $(defaults).attr(element);
  101. }
  102. if(element === "fill"){
  103. s = option[element];
  104. }
  105. if((element === "size" ||
  106. element === "width" ||
  107. element === "animationstep" ||
  108. element === "stripe"
  109. ) && !Number.isInteger(option[element])){
  110. option[element] = parseInt(option[element]);
  111. }
  112. if(element === "text_size"){
  113. option[element] = parseFloat(option[element]);
  114. }
  115. });
  116. }
  117. /* Draws the gauge. */
  118. function drawGauge(a) {
  119. if(M < 0) M = 0;
  120. if(M > 100) M = 100;
  121. var lw = option.width < 1 || isNaN(option.width) ? option.size / 20 : option.width;
  122. g.clearRect(0, 0, b.width, b.height);
  123. g.beginPath();
  124. g.arc(m, v, x, G, k, !1);
  125. if(s){
  126. g.fillStyle = option.fill;
  127. g.fill();
  128. }
  129. g.lineWidth = lw;
  130. g.strokeStyle = option.back;
  131. option.stripe > parseInt(0) ? g.setLineDash([option.stripe], 1) : g.lineCap = "round";
  132. g.stroke();
  133. g.beginPath();
  134. g.arc(m, v, x, -I, P * a - I, !1);
  135. g.lineWidth = lw;
  136. g.strokeStyle = option.fgcolor;
  137. g.stroke();
  138. c > M && (M += z, requestAnimationFrame(function(){
  139. drawGauge(Math.min(M, c) / 100);
  140. }, p));
  141. }
  142. $(this).attr("data-id", $(this).attr("id"));
  143. var r,
  144. dataAttr = ["percent",
  145. "used",
  146. "min",
  147. "total",
  148. "size",
  149. "prepend",
  150. "append",
  151. "theme",
  152. "color",
  153. "back",
  154. "width",
  155. "style",
  156. "stripe",
  157. "animationstep",
  158. "animate_gauge_colors",
  159. "animate_text_colors",
  160. "label",
  161. "label_color",
  162. "text",
  163. "text_size",
  164. "fill",
  165. "showvalue"],
  166. option = {},
  167. c = 0,
  168. p = $(this),
  169. s = false;
  170. p.addClass("gaugeMeter");
  171. getDataAttr(p);
  172. if(Number.isInteger(option.used) && Number.isInteger(option.total)){
  173. var u = option.used;
  174. var t = option.total;
  175. if(Number.isInteger(option.min)) {
  176. if(option.min < 0) {
  177. t -= option.min;
  178. u -= option.min;
  179. }
  180. }
  181. c = u / (t / 100);
  182. } else {
  183. if(Number.isInteger(option.percent)){
  184. c = option.percent;
  185. } else {
  186. c = parseInt(defaults.percent);
  187. }
  188. }
  189. if(c < 0) c = 0;
  190. if(c > 100) c = 100;
  191. if( option.text !== "" && option.text !== null && option.text !== undefined){
  192. if(option.append !== "" && option.append !== null && option.append !== undefined){
  193. r = option.text + "<u>" + option.append + "</u>";
  194. } else {
  195. r = option.text;
  196. }
  197. if(option.prepend !== "" && option.prepend !== null && option.prepend !== undefined){
  198. r = "<s>" + option.prepend + "</s>" + r;
  199. }
  200. } else {
  201. if(defaults.showvalue === true || option.showvalue === true){
  202. r = option.used;
  203. } else {
  204. r = c.toString();
  205. }
  206. if(option.prepend !== "" && option.prepend !== null && option.prepend !== undefined){
  207. r = "<s>" + option.prepend + "</s>" + r;
  208. }
  209. if(option.append !== "" && option.append !== null && option.append !== undefined){
  210. r = r + "<u>" + option.append + "</u>";
  211. }
  212. }
  213. option.fgcolor = getThemeColor(c);
  214. if(option.color !== "" && option.color !== null && option.color !== undefined){
  215. option.fgcolor = option.color;
  216. }
  217. if(option.animate_gauge_colors === true){
  218. option.fgcolor = getThemeColor(c);
  219. }
  220. createSpanTag(p);
  221. if(option.style !== "" && option.style !== null && option.style !== undefined){
  222. createLabel(p, option.size / 13);
  223. }
  224. $(this).width(option.size + "px");
  225. var b = $("<canvas></canvas>").attr({width: option.size, height: option.size}).get(0),
  226. g = b.getContext("2d"),
  227. m = b.width / 2,
  228. v = b.height / 2,
  229. _ = 360 * option.percent,
  230. x = (_ * (Math.PI / 180), b.width / 2.5),
  231. k = 2.3 * Math.PI,
  232. G = 0,
  233. M = 0 === option.animationstep ? c : 0,
  234. z = Math.max(option.animationstep, 0),
  235. P = 2 * Math.PI,
  236. I = Math.PI / 2,
  237. R = option.style;
  238. var child = $(this).children("canvas");
  239. if(child.length !== 0){
  240. /* Replace existing canvas when new percentage was written. */
  241. child.replaceWith(b);
  242. } else {
  243. /* Initially create canvas. */
  244. $(b).appendTo($(this));
  245. }
  246. if ("Semi" === R){
  247. k = 2 * Math.PI;
  248. G = 3.13;
  249. P = 1 * Math.PI;
  250. I = Math.PI / .996;
  251. }
  252. if ("Arch" === R){
  253. k = 2.195 * Math.PI;
  254. G = 1, G = 655.99999;
  255. P = 1.4 * Math.PI;
  256. I = Math.PI / .8335;
  257. }
  258. drawGauge(M / 100);
  259. });
  260. };
  261. }
  262. (jQuery);