RGraph.led.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. // version: 2014-06-26
  2. /**
  3. * o--------------------------------------------------------------------------------o
  4. * | This file is part of the RGraph package. RGraph is Free Software, licensed |
  5. * | under the MIT license - so it's free to use for all purposes. If you want to |
  6. * | donate to help keep the project going then you can do so here: |
  7. * | |
  8. * | http://www.rgraph.net/donate |
  9. * o--------------------------------------------------------------------------------o
  10. */
  11. RGraph = window.RGraph || {isRGraph: true};
  12. /**
  13. * The LED lights constructor
  14. *
  15. * @param object canvas The canvas object
  16. * @param array data The chart data
  17. */
  18. RGraph.LED = function (id, text)
  19. {
  20. var tmp = RGraph.getCanvasTag(id);
  21. // Get the canvas and context objects
  22. this.id = tmp[0];
  23. this.canvas = tmp[1];
  24. this.context = this.canvas.getContext ? this.canvas.getContext("2d", {alpha: (typeof id === 'object' && id.alpha === false) ? false : true}) : null;
  25. this.canvas.__object__ = this;
  26. this.type = 'led';
  27. this.isRGraph = true;
  28. this.uid = RGraph.CreateUID();
  29. this.canvas.uid = this.canvas.uid ? this.canvas.uid : RGraph.CreateUID();
  30. this.colorsParsed = false;
  31. this.original_colors = [];
  32. this.firstDraw = true; // After the first draw this will be false
  33. /**
  34. * Compatibility with older browsers
  35. */
  36. //RGraph.OldBrowserCompat(this.context);
  37. /**
  38. * Set the string that is to be displayed
  39. */
  40. this.text = text.toLowerCase();
  41. /**
  42. * The letters and numbers
  43. */
  44. this.lights = {
  45. '#': [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]],
  46. '-': [[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1],[0,0,0,0],[0,0,0,0],[0,0,0,0]],
  47. '_': [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]],
  48. '*': [[0,0,0,0],[0,0,0,0],[1,0,0,1],[0,1,1,0],[0,1,1,0],[1,0,0,1],[0,0,0,0]],
  49. '<': [[0,0,0,1],[0,0,1,0],[0,1,0,0],[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]],
  50. '>': [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1],[0,0,1,0],[0,1,0,0],[1,0,0,0]],
  51. '%': [[0,0,0,0],[0,0,0,0],[1,0,0,1],[0,0,1,0],[0,1,0,0],[1,0,0,1],[0,0,0,0]],
  52. '¡': [[0,0,1,0],[0,0,0,0],[0,0,1,0],[0,0,1,0],[0,0,1,0],[0,0,1,0],[0,0,1,0]],
  53. '!': [[0,0,1,0],[0,0,1,0],[0,0,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0],[0,0,1,0]],
  54. '¿': [[0,0,1,0],[0,0,0,0],[0,0,1,0],[0,1,1,0],[1,0,0,0],[1,0,0,0],[0,1,1,1]],
  55. '?': [[1,1,1,0],[0,0,0,1],[0,0,0,1],[0,1,1,0],[0,1,0,0],[0,0,0,0],[0,1,0,0]],
  56. '+': [[0,0,0,0],[0,0,0,0],[0,1,0,0],[1,1,1,0],[0,1,0,0],[0,0,0,0],[0,0,0,0]],
  57. '/': [[0,0,0,0],[0,0,0,1],[0,0,1,0],[0,1,0,0],[1,0,0,0],[0,0,0,0],[0,0,0,0]],
  58. ',': [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,1,0],[0,1,0,0]],
  59. '.': [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,1,0,0]],
  60. ';': [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,1,0],[0,0,0,0],[0,0,1,0],[0,1,0,0]],
  61. ':': [[0,0,0,0],[0,0,0,0],[0,1,0,0],[0,0,0,0],[0,1,0,0],[0,0,0,0],[0,0,0,0]],
  62. '"': [[1,0,1,0],[1,0,1,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],
  63. '[': [[1,1,1,0],[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,1,1,0]],
  64. ']': [[0,1,1,1],[0,0,0,1],[0,0,0,1],[0,0,0,1],[0,0,0,1],[0,0,0,1],[0,1,1,1]],
  65. '(': [[0,0,1,1],[0,1,1,0],[0,1,0,0],[0,1,0,0],[0,1,0,0],[0,1,1,0],[0,0,1,1]],
  66. ')': [[1,1,0,0],[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,1,0],[0,1,1,0],[1,1,0,0]],
  67. 'R': [[1,0,0,0],[1,1,0,0],[1,1,1,0],[1,1,1,1],[1,1,1,0],[1,1,0,0],[1,0,0,0]],
  68. 'L': [[0,0,0,1],[0,0,1,1],[0,1,1,1],[1,1,1,1],[0,1,1,1],[0,0,1,1],[0,0,0,1]],
  69. 'a': [[0,1,1,0],[1,0,0,1],[1,0,0,1],[1,1,1,1],[1,0,0,1],[1,0,0,1],[1,0,0,1]],
  70. 'b': [[1,1,1,0],[1,0,0,1],[1,0,0,1],[1,1,1,0],[1,0,0,1],[1,0,0,1],[1,1,1,0]],
  71. 'c': [[0,1,1,0],[1,0,0,1],[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,0,0,1],[0,1,1,0]],
  72. 'd': [[1,1,1,0],[1,0,0,1],[1,0,0,1],[1,0,0,1],[1,0,0,1],[1,0,0,1],[1,1,1,0]],
  73. 'e': [[1,1,1,1],[1,0,0,0],[1,0,0,0],[1,1,1,0],[1,0,0,0],[1,0,0,0],[1,1,1,1]],
  74. 'f': [[1,1,1,1],[1,0,0,0],[1,0,0,0],[1,1,1,0],[1,0,0,0],[1,0,0,0],[1,0,0,0]],
  75. 'g': [[0,1,1,0],[1,0,0,1],[1,0,0,0],[1,0,1,1],[1,0,0,1],[1,0,0,1],[0,1,1,0]],
  76. 'h': [[1,0,0,1],[1,0,0,1],[1,0,0,1],[1,1,1,1],[1,0,0,1],[1,0,0,1],[1,0,0,1]],
  77. 'i': [[0,1,1,1],[0,0,1,0],[0,0,1,0],[0,0,1,0],[0,0,1,0],[0,0,1,0],[0,1,1,1]],
  78. 'j': [[0,1,1,1],[0,0,1,0],[0,0,1,0],[0,0,1,0],[0,0,1,0],[0,0,1,0],[0,1,0,0]],
  79. 'k': [[1,0,0,1],[1,0,0,1],[1,0,1,0],[1,1,0,0],[1,0,1,0],[1,0,0,1],[1,0,0,1]],
  80. 'l': [[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,1,1,1]],
  81. 'm': [[1,0,0,1],[1,1,1,1],[1,0,0,1],[1,0,0,1],[1,0,0,1],[1,0,0,1],[1,0,0,1]],
  82. 'n': [[1,0,0,1],[1,1,0,1],[1,0,1,1],[1,0,0,1],[1,0,0,1],[1,0,0,1],[1,0,0,1]],
  83. 'o': [[0,1,1,0],[1,0,0,1],[1,0,0,1],[1,0,0,1],[1,0,0,1],[1,0,0,1],[0,1,1,0]],
  84. 'p': [[1,1,1,0],[1,0,0,1],[1,0,0,1],[1,1,1,0],[1,0,0,0],[1,0,0,0],[1,0,0,0]],
  85. 'q': [[0,1,1,0],[1,0,0,1],[1,0,0,1],[1,0,0,1],[1,0,0,1],[1,0,1,1],[0,1,1,1]],
  86. 'r': [[1,1,1,0],[1,0,0,1],[1,0,0,1],[1,1,1,0],[1,0,1,0],[1,0,0,1],[1,0,0,1]],
  87. 's': [[0,1,1,0],[1,0,0,1],[1,0,0,0],[0,1,1,0],[0,0,0,1],[1,0,0,1],[0,1,1,0]],
  88. 't': [[1,1,1,0],[0,1,0,0],[0,1,0,0],[0,1,0,0],[0,1,0,0],[0,1,0,0],[0,1,0,0]],
  89. 'u': [[1,0,0,1],[1,0,0,1],[1,0,0,1],[1,0,0,1],[1,0,0,1],[1,0,0,1],[0,1,1,0]],
  90. 'v': [[1,0,1],[1,0,1],[1,0,1],[1,0,1],[1,0,1],[0,1,0],[0,1,0]],
  91. 'w': [[1,0,0,1],[1,0,0,1],[1,0,0,1],[1,0,0,1],[1,0,0,1],[1,1,1,1],[0,1,1,0]],
  92. 'x': [[0,1,0,1],[0,1,0,1],[0,1,0,1],[0,0,1,0],[0,1,0,1],[0,1,0,1],[0,1,0,1]],
  93. 'y': [[0,1,0,1],[0,1,0,1],[0,0,1,0],[0,0,1,0],[0,0,1,0],[0,0,1,0],[0,0,1,0]],
  94. 'z': [[1,1,1,1],[0,0,0,1],[0,0,1,0],[0,0,1,0],[0,1,0,0],[1,0,0,0],[1,1,1,1]],
  95. ' ': [[],[],[],[],[], [], []],
  96. '0': [[0,1,1,0],[1,0,0,1],[1,0,0,1],[1,0,0,1],[1,0,0,1],[1,0,0,1],[0,1,1,0]],
  97. '1': [[0,0,1,0],[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,1,0],[0,0,1,0],[0,1,1,1]],
  98. '2': [[0,1,1,0],[1,0,0,1],[0,0,0,1],[0,0,1,0],[0,1,,0],[1,0,0,0],[1,1,1,1]],
  99. '3': [[0,1,1,0],[1,0,0,1],[0,0,0,1],[0,1,1,0],[0,0,0,1],[1,0,0,1],[0,1,1,0]],
  100. '4': [[1,0,0,0],[1,0,0,0],[1,0,1,0],[1,0,1,0],[1,1,1,1],[0,0,1,0],[0,0,1,0]],
  101. '5': [[1,1,1,1],[1,0,0,0],[1,0,0,0],[1,1,1,0],[0,0,0,1],[1,0,0,1],[0,1,1,0]],
  102. '6': [[0,1,1,0],[1,0,0,1],[1,0,0,0],[1,1,1,0],[1,0,0,1],[1,0,0,1],[0,1,1,0]],
  103. '7': [[1,1,1,1],[0,0,0,1],[0,0,0,1],[0,0,1,0],[0,1,0,0],[0,1,0,0],[0,1,0,0]],
  104. '8': [[0,1,1,0],[1,0,0,1],[1,0,0,1],[0,1,1,0],[1,0,0,1],[1,0,0,1],[0,1,1,0]],
  105. '9': [[0,1,1,1],[1,0,0,1],[1,0,0,1],[0,1,1,1],[0,0,0,1],[0,0,0,1],[0,0,0,1]]
  106. }
  107. // Various config type stuff
  108. this.properties =
  109. {
  110. 'chart.dark': '#eee',
  111. 'chart.light': '#f66',
  112. 'chart.tooltips': null,
  113. 'chart.zoom.factor': 1.5,
  114. 'chart.zoom.fade.in': true,
  115. 'chart.zoom.fade.out': true,
  116. 'chart.zoom.hdir': 'right',
  117. 'chart.zoom.vdir': 'down',
  118. 'chart.zoom.frames': 25,
  119. 'chart.zoom.delay': 16.666,
  120. 'chart.zoom.shadow': true,
  121. 'chart.zoom.background': true,
  122. 'chart.zoom.action': 'zoom',
  123. 'chart.resizable': false,
  124. 'chart.resize.handle.adjust': [0,0],
  125. 'chart.resize.handle.background': null,
  126. 'chart.radius': null
  127. }
  128. // Check for support
  129. if (!this.canvas) {
  130. alert('[LED] No canvas support');
  131. return;
  132. }
  133. /*
  134. * Translate half a pixel for antialiasing purposes - but only if it hasn't beeen
  135. * done already
  136. */
  137. if (!this.canvas.__rgraph_aa_translated__) {
  138. this.context.translate(0.5,0.5);
  139. this.canvas.__rgraph_aa_translated__ = true;
  140. }
  141. // Short variable names
  142. var RG = RGraph;
  143. var ca = this.canvas;
  144. var co = ca.getContext('2d');
  145. var prop = this.properties;
  146. var jq = jQuery;
  147. var pa = RG.Path;
  148. var win = window;
  149. var doc = document;
  150. var ma = Math;
  151. /**
  152. * "Decorate" the object with the generic effects if the effects library has been included
  153. */
  154. if (RG.Effects && typeof RG.Effects.decorate === 'function') {
  155. RG.Effects.decorate(this);
  156. }
  157. /**
  158. * A setter
  159. *
  160. * @param name string The name of the property to set
  161. * @param value mixed The value of the property
  162. */
  163. this.set =
  164. this.Set = function (name, value)
  165. {
  166. name = name.toLowerCase();
  167. /**
  168. * This should be done first - prepend the propertyy name with "chart." if necessary
  169. */
  170. if (name.substr(0,6) != 'chart.') {
  171. name = 'chart.' + name;
  172. }
  173. prop[name.toLowerCase()] = value;
  174. return this;
  175. };
  176. /**
  177. * A getter
  178. *
  179. * @param name string The name of the property to get
  180. */
  181. this.get =
  182. this.Get = function (name)
  183. {
  184. /**
  185. * This should be done first - prepend the property name with "chart." if necessary
  186. */
  187. if (name.substr(0,6) != 'chart.') {
  188. name = 'chart.' + name;
  189. }
  190. return prop[name.toLowerCase()];
  191. };
  192. /**
  193. * This draws the LEDs
  194. */
  195. this.draw =
  196. this.Draw = function ()
  197. {
  198. /**
  199. * Fire the onbeforedraw event
  200. */
  201. RG.FireCustomEvent(this, 'onbeforedraw');
  202. /**
  203. * Parse the colors. This allows for simple gradient syntax
  204. */
  205. if (!this.colorsParsed) {
  206. this.parseColors();
  207. // Don't want to do this again
  208. this.colorsParsed = true;
  209. }
  210. for (var l=0,len=this.text.length; l<len; l++) {
  211. this.DrawLetter(this.text.charAt(l), l);
  212. }
  213. /**
  214. * Set the title attribute on the canvas
  215. */
  216. ca.title = RG.rtrim(this.text);
  217. /**
  218. * Setup the context menu if required
  219. */
  220. if (prop['chart.contextmenu']) {
  221. RG.ShowContext(this);
  222. }
  223. /**
  224. * This function enables resizing
  225. */
  226. if (prop['chart.resizable']) {
  227. RG.AllowResizing(this);
  228. }
  229. /**
  230. * This installs the event listeners
  231. */
  232. RG.InstallEventListeners(this);
  233. /**
  234. * Fire the onfirstdraw event
  235. */
  236. if (this.firstDraw) {
  237. RG.fireCustomEvent(this, 'onfirstdraw');
  238. this.firstDrawFunc();
  239. this.firstDraw = false;
  240. }
  241. /**
  242. * Fire the RGraph ondraw event
  243. */
  244. RG.FireCustomEvent(this, 'ondraw');
  245. return this;
  246. };
  247. /**
  248. * Draws a single letter
  249. *
  250. * @param string lights The lights to draw to draw
  251. * @param int index The position of the letter
  252. */
  253. this.drawLetter =
  254. this.DrawLetter = function (letter, index)
  255. {
  256. var light = prop['chart.light'];
  257. var dark = prop['chart.dark'];
  258. var lights = (this.lights[letter] ? this.lights[letter] : [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]);
  259. /**
  260. * Now allow user specified radius of the size of the lights
  261. */
  262. if (typeof(prop['chart.radius']) == 'number') {
  263. radius = Number(prop['chart.radius']);
  264. diameter = 2 * radius;
  265. lwidth = diameter * 5;
  266. } else {
  267. var radius = ((ca.width / this.text.length) / 5) / 2;
  268. var diameter = radius * 2;
  269. var lwidth = diameter * 5;
  270. }
  271. //var lheight = diameter * 7;
  272. //if (lheight > ca.height) {
  273. // lheight = ca.height;
  274. // diameter = (lheight / 7);
  275. // radius = (diameter / 2);
  276. // lwidth = diameter * 5;
  277. //}
  278. for (var i=0; i<7; i++) {
  279. for (var j=0; j<5; j++) {
  280. var x = (j * diameter) + (index * lwidth) + radius;
  281. var y = ((i * diameter)) + radius;
  282. // Draw a circle
  283. co.fillStyle = (lights[i][j] ? light : dark);
  284. co.strokeStyle = (lights[i][j] ? '#ccc' : 'rgba(0,0,0,0)');
  285. co.beginPath();
  286. co.arc(x, y, radius, 0, RG.TWOPI, 0);
  287. co.stroke();
  288. co.fill();
  289. }
  290. }
  291. };
  292. /**
  293. * A place holder
  294. *
  295. * @param object e The event object
  296. */
  297. this.getValue = function (e)
  298. {
  299. return this.text;
  300. };
  301. /**
  302. * This allows for easy specification of gradients
  303. */
  304. this.parseColors = function ()
  305. {
  306. // Save the original colors so that they can be restored when the canvas is reset
  307. if (this.original_colors.length === 0) {
  308. this.original_colors['chart.dark'] = RG.array_clone(prop['chart.dark']);
  309. this.original_colors['chart.light'] = RG.array_clone(prop['chart.light']);
  310. }
  311. prop['chart.dark'] = this.parseSingleColorForGradient(prop['chart.dark']);
  312. prop['chart.light'] = this.parseSingleColorForGradient(prop['chart.light']);
  313. };
  314. /**
  315. * This parses a single color value
  316. */
  317. this.parseSingleColorForGradient = function (color)
  318. {
  319. if (typeof color === 'string' && color.match(/^gradient\((.*)\)$/i)) {
  320. var parts = RegExp.$1.split(':');
  321. // Create the gradient
  322. var grad = co.createLinearGradient(0,0,0,ca.height);
  323. var diff = 1 / (parts.length - 1);
  324. grad.addColorStop(0, RG.trim(parts[0]));
  325. for (var j=1; j<parts.length; ++j) {
  326. grad.addColorStop(j * diff, RG.trim(parts[j]));
  327. }
  328. }
  329. return grad ? grad : color;
  330. };
  331. /**
  332. * Using a function to add events makes it easier to facilitate method chaining
  333. *
  334. * @param string type The type of even to add
  335. * @param function func
  336. */
  337. this.on = function (type, func)
  338. {
  339. if (type.substr(0,2) !== 'on') {
  340. type = 'on' + type;
  341. }
  342. this[type] = func;
  343. return this;
  344. };
  345. /**
  346. * This function runs once only
  347. * (put at the end of the file (before any effects))
  348. */
  349. this.firstDrawFunc = function ()
  350. {
  351. };
  352. /**
  353. * A placeholder
  354. */
  355. this.getObjectByXY = function ()
  356. {
  357. }
  358. RG.Register(this);
  359. };