RGraph.drawing.text.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. /**
  15. * Having this here means that the RGraph libraries can be included in any order, instead of you having
  16. * to include the common core library first.
  17. */
  18. if (typeof(RGraph) == 'undefined') RGraph = {};
  19. if (typeof(RGraph.Drawing) == 'undefined') RGraph.Drawing = {};
  20. /**
  21. * The constructor. This function sets up the object. It takes the ID (the HTML attribute) of the canvas as the
  22. * first argument, then th X position, the Y position and then the text to show
  23. *
  24. * @param string id The canvas tag ID
  25. * @param number x The X position of the text
  26. * @param number y The Y position of the text
  27. * @param number text The text to show
  28. */
  29. RGraph.Drawing.Text = function (id, x, y, text)
  30. {
  31. this.id = id;
  32. this.canvas = document.getElementById(typeof id === 'object' ? id.id : id);
  33. this.context = this.canvas.getContext ? this.canvas.getContext("2d") : null;
  34. this.colorsParsed = false;
  35. this.canvas.__object__ = this;
  36. this.x = x;
  37. this.y = y;
  38. this.text = text;
  39. this.coords = [];
  40. this.coordsText = [];
  41. /**
  42. * This defines the type of this shape
  43. */
  44. this.type = 'drawing.text';
  45. /**
  46. * This facilitates easy object identification, and should always be true
  47. */
  48. this.isRGraph = true;
  49. /**
  50. * This adds a uid to the object that you can use for identification purposes
  51. */
  52. this.uid = RGraph.CreateUID();
  53. /**
  54. * This adds a UID to the canvas for identification purposes
  55. */
  56. this.canvas.uid = this.canvas.uid ? this.canvas.uid : RGraph.CreateUID();
  57. /**
  58. * This does a few things, for example adding the .fillText() method to the canvas 2D context when
  59. * it doesn't exist. This facilitates the graphs to be still shown in older browser (though without
  60. * text obviously). You'll find the function in RGraph.common.core.js
  61. */
  62. RGraph.OldBrowserCompat(this.context);
  63. /**
  64. * Some example background properties
  65. */
  66. this.properties =
  67. {
  68. 'chart.size': 10,
  69. 'chart.font': 'Arial',
  70. 'chart.bold': false,
  71. 'chart.angle': 0,
  72. 'chart.colors': ['black'],
  73. 'chart.events.click': null,
  74. 'chart.events.mousemove': null,
  75. 'chart.highlight.stroke': '#ccc',
  76. 'chart.highlight.fill': 'rgba(255,255,255,0.7)',
  77. 'chart.tooltips': null,
  78. 'chart.tooltips.effect': 'fade',
  79. 'chart.tooltips.css.class': 'RGraph_tooltip',
  80. 'chart.tooltips.event': 'onclick',
  81. 'chart.tooltips.highlight': true,
  82. 'chart.tooltips.coords.page': false,
  83. 'chart.bounding': false,
  84. 'chart.bounding.fill': 'rgba(255,255,255,0.7)',
  85. 'chart.bounding.stroke': '#777',
  86. 'chart.bounding.shadow': false,
  87. 'chart.bounding.shadow.color': '#ccc',
  88. 'chart.bounding.shadow.blur': 3,
  89. 'chart.bounding.shadow.offsetx': 3,
  90. 'chart.bounding.shadow.offsety': 3,
  91. 'chart.marker': false,
  92. 'chart.halign': 'left',
  93. 'chart.valign': 'bottom'
  94. }
  95. /**
  96. * A simple check that the browser has canvas support
  97. */
  98. if (!this.canvas) {
  99. alert('[DRAWING.TEXT] No canvas support');
  100. return;
  101. }
  102. /**
  103. * Create the dollar object so that functions can be added to them
  104. */
  105. this.$0 = {};
  106. /**
  107. * Translate half a pixel for antialiasing purposes - but only if it hasn't beeen
  108. * done already
  109. */
  110. if (!this.canvas.__rgraph_aa_translated__) {
  111. this.context.translate(0.5,0.5);
  112. this.canvas.__rgraph_aa_translated__ = true;
  113. }
  114. ///////////////////////////////// SHORT PROPERTIES /////////////////////////////////
  115. var RG = RGraph;
  116. var ca = this.canvas;
  117. var co = ca.getContext('2d');
  118. var prop = this.properties;
  119. //////////////////////////////////// METHODS ///////////////////////////////////////
  120. /**
  121. * A setter method for setting properties.
  122. *
  123. * @param name string The name of the property to set
  124. * @param value mixed The value of the property
  125. */
  126. this.Set = function (name, value)
  127. {
  128. name = name.toLowerCase();
  129. /**
  130. * This should be done first - prepend the property name with "chart." if necessary
  131. */
  132. if (name.substr(0,6) != 'chart.') {
  133. name = 'chart.' + name;
  134. }
  135. prop[name] = value;
  136. return this;
  137. }
  138. /**
  139. * A getter method for retrieving graph properties. It can be used like this: obj.Get('chart.strokestyle');
  140. *
  141. * @param name string The name of the property to get
  142. */
  143. this.Get = function (name)
  144. {
  145. /**
  146. * This should be done first - prepend the property name with "chart." if necessary
  147. */
  148. if (name.substr(0,6) != 'chart.') {
  149. name = 'chart.' + name;
  150. }
  151. return prop[name.toLowerCase()];
  152. }
  153. /**
  154. * Draws the rectangle
  155. */
  156. this.Draw = function ()
  157. {
  158. /**
  159. * Fire the onbeforedraw event
  160. */
  161. RG.FireCustomEvent(this, 'onbeforedraw');
  162. /**
  163. * Parse the colors. This allows for simple gradient syntax
  164. */
  165. if (!this.colorsParsed) {
  166. this.parseColors();
  167. // Don't want to do this again
  168. this.colorsParsed = true;
  169. }
  170. /**
  171. * Stop the coods array from growing
  172. */
  173. this.coords = [];
  174. /**
  175. * The font, its size and whether its bold or not can be set by properties,
  176. * so now they have been (potentiall) set - measure the text
  177. */
  178. /**
  179. * Measure the text and add the width/height
  180. *
  181. * text, bold, font, size
  182. *
  183. */
  184. var dimensions = RG.MeasureText(this.text, prop['chart.text.bold'],prop['chart.text.font'], prop['chart.text.size']);
  185. // ------------- DRAW TEXT HERE -------------
  186. co.fillStyle = prop['chart.colors'][0];
  187. var ret = RG.Text2(this, {'font': prop['chart.font'],
  188. 'size': prop['chart.size'],
  189. 'x': this.x,
  190. 'y': this.y,
  191. 'text': this.text,
  192. 'bold': prop['chart.bold'],
  193. 'angle': prop['chart.angle'],
  194. 'bounding': prop['chart.bounding'],
  195. 'bounding.fill': prop['chart.bounding.fill'],
  196. 'bounding.stroke': prop['chart.bounding.stroke'],
  197. 'bounding.shadow': prop['chart.bounding.shadow'],
  198. 'bounding.shadow.color': prop['chart.bounding.shadow.color'],
  199. 'bounding.shadow.blur': prop['chart.bounding.shadow.blur'],
  200. 'bounding.shadow.offsetx': prop['chart.bounding.shadow.offsetx'],
  201. 'bounding.shadow.offsety': prop['chart.bounding.shadow.offsety'],
  202. 'marker': prop['chart.marker'],
  203. 'halign': prop['chart.halign'],
  204. 'valign': prop['chart.valign']
  205. });
  206. // store the dimensions
  207. this.coords.push({'x':ret.x, 'y':ret.y, 'width':ret.width, 'height':ret.height});
  208. /**
  209. * This installs the event listeners
  210. */
  211. RG.InstallEventListeners(this);
  212. /**
  213. * Fire the ondraw event
  214. */
  215. RG.FireCustomEvent(this, 'ondraw');
  216. return this;
  217. }
  218. /**
  219. * The getObjectByXY() worker method
  220. */
  221. this.getObjectByXY = function (e)
  222. {
  223. if (this.getShape(e)) {
  224. return this;
  225. }
  226. }
  227. /**
  228. * Not used by the class during creating the graph, but is used by event handlers
  229. * to get the coordinates (if any) of the selected bar
  230. *
  231. * @param object e The event object
  232. */
  233. this.getShape = function (e)
  234. {
  235. var prop = this.properties;
  236. var coords = this.coords;
  237. var mouseXY = RGraph.getMouseXY(e);
  238. var mouseX = mouseXY[0];
  239. var mouseY = mouseXY[1];
  240. for (var i=0,len=this.coords.length; i<len; i++) {
  241. var left = coords[i].x;
  242. var top = coords[i].y;
  243. var width = coords[i].width;
  244. var height = coords[i].height;
  245. if (mouseX >= left && mouseX <= (left + width) && mouseY >= top && mouseY <= (top + height)) {
  246. return {
  247. 0: this, 1: left, 2: top, 3: width, 4: height, 5: 0,
  248. 'object': this, 'x': left, 'y': top, 'width': width, 'height': height, 'index': 0, 'tooltip': prop['chart.tooltips'] ? prop['chart.tooltips'][0] : null
  249. };
  250. }
  251. }
  252. return null;
  253. }
  254. /**
  255. * This function positions a tooltip when it is displayed
  256. *
  257. * @param obj object The chart object
  258. * @param int x The X coordinate specified for the tooltip
  259. * @param int y The Y coordinate specified for the tooltip
  260. * @param objec tooltip The tooltips DIV element
  261. */
  262. this.positionTooltip = function (obj, x, y, tooltip, idx)
  263. {
  264. var coords = obj.coords[0];
  265. var coordX = coords.x;
  266. var coordY = coords.y;
  267. var coordW = coords.width;
  268. var coordH = coords.height;
  269. var canvasXY = RGraph.getCanvasXY(obj.canvas);
  270. var width = tooltip.offsetWidth;
  271. var height = tooltip.offsetHeight;
  272. // Set the top position
  273. tooltip.style.left = 0;
  274. tooltip.style.top = canvasXY[1] + coordY + (coordH / 2) - height + 'px';
  275. // By default any overflow is hidden
  276. tooltip.style.overflow = '';
  277. // The arrow
  278. var img = new Image();
  279. img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABEAAAAFCAYAAACjKgd3AAAARUlEQVQYV2NkQAN79+797+RkhC4M5+/bd47B2dmZEVkBCgcmgcsgbAaA9GA1BCSBbhAuA/AagmwQPgMIGgIzCD0M0AMMAEFVIAa6UQgcAAAAAElFTkSuQmCC';
  280. img.style.position = 'absolute';
  281. img.id = '__rgraph_tooltip_pointer__';
  282. img.style.top = (tooltip.offsetHeight - 2) + 'px';
  283. tooltip.appendChild(img);
  284. // Reposition the tooltip if at the edges:
  285. // LEFT edge
  286. if ((canvasXY[0] + coordX + (coordW / 2) - (width / 2)) < 10) {
  287. tooltip.style.left = (canvasXY[0] + coordX - (width * 0.1)) + (coordW / 2) + 'px';
  288. img.style.left = ((width * 0.1) - 8.5) + 'px';
  289. // RIGHT edge
  290. } else if ((canvasXY[0] + coordX + (width / 2)) > document.body.offsetWidth) {
  291. tooltip.style.left = canvasXY[0] + coordX - (width * 0.9) + (coordW / 2) + 'px';
  292. img.style.left = ((width * 0.9) - 8.5) + 'px';
  293. // Default positioning - CENTERED
  294. } else {
  295. tooltip.style.left = (canvasXY[0] + coordX + (coordW / 2) - (width * 0.5)) + 'px';
  296. img.style.left = ((width * 0.5) - 8.5) + 'px';
  297. }
  298. }
  299. /**
  300. * Each object type has its own Highlight() function which highlights the appropriate shape
  301. *
  302. * @param object shape The shape to highlight
  303. */
  304. this.Highlight = function (shape)
  305. {
  306. // Add the new highlight
  307. RG.Highlight.Rect(this, shape);
  308. }
  309. /**
  310. * This allows for easy specification of gradients
  311. */
  312. this.parseColors = function ()
  313. {
  314. /**
  315. * Parse various properties for colors
  316. */
  317. prop['chart.fillstyle'] = this.parseSingleColorForGradient(prop['chart.fillstyle']);
  318. prop['chart.strokestyle'] = this.parseSingleColorForGradient(prop['chart.strokestyle']);
  319. prop['chart.highlight.stroke'] = this.parseSingleColorForGradient(prop['chart.highlight.stroke']);
  320. prop['chart.highlight.fill'] = this.parseSingleColorForGradient(prop['chart.highlight.fill']);
  321. }
  322. /**
  323. * This parses a single color value
  324. */
  325. this.parseSingleColorForGradient = function (color)
  326. {
  327. if (!color) {
  328. return color;
  329. }
  330. if (color.match(/^gradient\((.*)\)$/i)) {
  331. var parts = RegExp.$1.split(':');
  332. // Create the gradient
  333. var grad = co.createLinearGradient(0,0,ca.width,0);
  334. var diff = 1 / (parts.length - 1);
  335. grad.addColorStop(0, RGraph.trim(parts[0]));
  336. for (var j=1,len=parts.length; j<len; ++j) {
  337. grad.addColorStop(j * diff, RG.trim(parts[j]));
  338. }
  339. }
  340. return grad ? grad : color;
  341. }
  342. /**
  343. * Objects are now always registered so that the chart is redrawn if need be.
  344. */
  345. RG.Register(this);
  346. }