RGraph.led.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /**
  2. * o------------------------------------------------------------------------------o
  3. * | This file is part of the RGraph package - you can learn more at: |
  4. * | |
  5. * | http://www.rgraph.net |
  6. * | |
  7. * | This package is licensed under the RGraph license. For all kinds of business |
  8. * | purposes there is a small one-time licensing fee to pay and for non |
  9. * | commercial purposes it is free to use. You can read the full license here: |
  10. * | |
  11. * | http://www.rgraph.net/license |
  12. * o------------------------------------------------------------------------------o
  13. */
  14. if (typeof(RGraph) == 'undefined') RGraph = {};
  15. /**
  16. * The LED lights constructor
  17. *
  18. * @param object canvas The canvas object
  19. * @param array data The chart data
  20. */
  21. RGraph.LED = function (id, text)
  22. {
  23. // Get the canvas and context objects
  24. this.id = id;
  25. this.canvas = document.getElementById(typeof id === 'object' ? id.id : id);
  26. this.context = this.canvas.getContext ? this.canvas.getContext("2d") : null;
  27. this.canvas.__object__ = this;
  28. this.type = 'led';
  29. this.isRGraph = true;
  30. this.uid = RGraph.CreateUID();
  31. this.canvas.uid = this.canvas.uid ? this.canvas.uid : RGraph.CreateUID();
  32. this.colorsParsed = 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. 'chart.dark': '#eee',
  110. 'chart.light': '#f66',
  111. 'chart.zoom.factor': 1.5,
  112. 'chart.zoom.fade.in': true,
  113. 'chart.zoom.fade.out': true,
  114. 'chart.zoom.hdir': 'right',
  115. 'chart.zoom.vdir': 'down',
  116. 'chart.zoom.frames': 25,
  117. 'chart.zoom.delay': 16.666,
  118. 'chart.zoom.shadow': true,
  119. 'chart.zoom.background': true,
  120. 'chart.zoom.action': 'zoom',
  121. 'chart.resizable': false,
  122. 'chart.resize.handle.adjust': [0,0],
  123. 'chart.resize.handle.background': null,
  124. 'chart.radius': null
  125. }
  126. // Check for support
  127. if (!this.canvas) {
  128. alert('[LED] No canvas support');
  129. return;
  130. }
  131. /*
  132. * Translate half a pixel for antialiasing purposes - but only if it hasn't beeen
  133. * done already
  134. */
  135. if (!this.canvas.__rgraph_aa_translated__) {
  136. this.context.translate(0.5,0.5);
  137. this.canvas.__rgraph_aa_translated__ = true;
  138. }
  139. ///////////////////////////////// SHORT PROPERTIES /////////////////////////////////
  140. var RG = RGraph;
  141. var ca = this.canvas;
  142. var co = ca.getContext('2d');
  143. var prop = this.properties;
  144. //////////////////////////////////// METHODS ///////////////////////////////////////
  145. /**
  146. * A setter
  147. *
  148. * @param name string The name of the property to set
  149. * @param value mixed The value of the property
  150. */
  151. this.Set = function (name, value)
  152. {
  153. name = name.toLowerCase();
  154. /**
  155. * This should be done first - prepend the propertyy name with "chart." if necessary
  156. */
  157. if (name.substr(0,6) != 'chart.') {
  158. name = 'chart.' + name;
  159. }
  160. prop[name.toLowerCase()] = value;
  161. return this;
  162. }
  163. /**
  164. * A getter
  165. *
  166. * @param name string The name of the property to get
  167. */
  168. this.Get = function (name)
  169. {
  170. /**
  171. * This should be done first - prepend the property name with "chart." if necessary
  172. */
  173. if (name.substr(0,6) != 'chart.') {
  174. name = 'chart.' + name;
  175. }
  176. return prop[name.toLowerCase()];
  177. }
  178. /**
  179. * This draws the LEDs
  180. */
  181. this.Draw = function ()
  182. {
  183. /**
  184. * Fire the onbeforedraw event
  185. */
  186. RG.FireCustomEvent(this, 'onbeforedraw');
  187. /**
  188. * Parse the colors. This allows for simple gradient syntax
  189. */
  190. if (!this.colorsParsed) {
  191. this.parseColors();
  192. // Don't want to do this again
  193. this.colorsParsed = true;
  194. }
  195. for (var l=0,len=this.text.length; l<len; l++) {
  196. this.DrawLetter(this.text.charAt(l), l);
  197. }
  198. /**
  199. * Set the title attribute on the canvas
  200. */
  201. ca.title = RG.rtrim(this.text);
  202. /**
  203. * Setup the context menu if required
  204. */
  205. if (prop['chart.contextmenu']) {
  206. RG.ShowContext(this);
  207. }
  208. /**
  209. * This function enables resizing
  210. */
  211. if (prop['chart.resizable']) {
  212. RG.AllowResizing(this);
  213. }
  214. /**
  215. * This installs the event listeners
  216. */
  217. RG.InstallEventListeners(this);
  218. /**
  219. * Fire the RGraph ondraw event
  220. */
  221. RG.FireCustomEvent(this, 'ondraw');
  222. return this;
  223. }
  224. /**
  225. * Draws a single letter
  226. *
  227. * @param string lights The lights to draw to draw
  228. * @param int index The position of the letter
  229. */
  230. this.DrawLetter = function (letter, index)
  231. {
  232. var light = prop['chart.light'];
  233. var dark = prop['chart.dark'];
  234. 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]]);
  235. /**
  236. * Now allow user specified radius of the size of the lights
  237. */
  238. if (typeof(prop['chart.radius']) == 'number') {
  239. radius = Number(prop['chart.radius']);
  240. diameter = 2 * radius;
  241. lwidth = diameter * 5;
  242. } else {
  243. var radius = ((ca.width / this.text.length) / 5) / 2;
  244. var diameter = radius * 2;
  245. var lwidth = diameter * 5;
  246. }
  247. //var lheight = diameter * 7;
  248. //if (lheight > ca.height) {
  249. // lheight = ca.height;
  250. // diameter = (lheight / 7);
  251. // radius = (diameter / 2);
  252. // lwidth = diameter * 5;
  253. //}
  254. for (var i=0; i<7; i++) {
  255. for (var j=0; j<5; j++) {
  256. var x = (j * diameter) + (index * lwidth) + radius;
  257. var y = ((i * diameter)) + radius;
  258. // Draw a circle
  259. co.fillStyle = (lights[i][j] ? light : dark);
  260. co.strokeStyle = (lights[i][j] ? '#ccc' : 'rgba(0,0,0,0)');
  261. co.beginPath();
  262. co.arc(x, y, radius, 0, 6.28, 0);
  263. co.stroke();
  264. co.fill();
  265. }
  266. }
  267. }
  268. /**
  269. * A place holder
  270. *
  271. * @param object e The event object
  272. */
  273. this.getValue = function (e)
  274. {
  275. return this.text;
  276. }
  277. /**
  278. * This allows for easy specification of gradients
  279. */
  280. this.parseColors = function ()
  281. {
  282. if (typeof(prop['chart.dark']) == 'string') prop['chart.dark'] = this.parseSingleColorForGradient(prop['chart.dark']);
  283. if (typeof(prop['chart.light']) == 'string') prop['chart.light'] = this.parseSingleColorForGradient(prop['chart.light']);
  284. }
  285. /**
  286. * This parses a single color value
  287. */
  288. this.parseSingleColorForGradient = function (color)
  289. {
  290. if (color.match(/^gradient\((.*)\)$/i)) {
  291. var parts = RegExp.$1.split(':');
  292. // Create the gradient
  293. var grad = co.createLinearGradient(0,0,0,ca.height);
  294. var diff = 1 / (parts.length - 1);
  295. grad.addColorStop(0, RG.trim(parts[0]));
  296. for (var j=1; j<parts.length; ++j) {
  297. grad.addColorStop(j * diff, RG.trim(parts[j]));
  298. }
  299. }
  300. return grad ? grad : color;
  301. }
  302. RG.Register(this);
  303. }