RGraph.funnel.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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 bar chart constructor
  14. *
  15. * @param object canvas The canvas object
  16. * @param array data The chart data
  17. */
  18. RGraph.Funnel = function (id, data)
  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 = 'funnel';
  27. this.coords = [];
  28. this.isRGraph = true;
  29. this.uid = RGraph.CreateUID();
  30. this.canvas.uid = this.canvas.uid ? this.canvas.uid : RGraph.CreateUID();
  31. this.coordsText = [];
  32. this.original_colors = [];
  33. this.firstDraw = true; // After the first draw this will be false
  34. /**
  35. * Compatibility with older browsers
  36. */
  37. //RGraph.OldBrowserCompat(this.context);
  38. // Check for support
  39. if (!this.canvas) {
  40. alert('[FUNNEL] No canvas support');
  41. return;
  42. }
  43. /**
  44. * The funnel charts properties
  45. */
  46. this.properties =
  47. {
  48. 'chart.strokestyle': 'rgba(0,0,0,0)',
  49. 'chart.gutter.left': 25,
  50. 'chart.gutter.right': 25,
  51. 'chart.gutter.top': 25,
  52. 'chart.gutter.bottom': 25,
  53. 'chart.labels': null,
  54. 'chart.labels.sticks': false,
  55. 'chart.labels.x': null,
  56. 'chart.title': '',
  57. 'chart.title.background': null,
  58. 'chart.title.hpos': null,
  59. 'chart.title.vpos': null,
  60. 'chart.title.bold': true,
  61. 'chart.title.font': null,
  62. 'chart.title.x': null,
  63. 'chart.title.y': null,
  64. 'chart.title.halign': null,
  65. 'chart.title.valign': null,
  66. 'chart.colors': ['Gradient(white:red)','Gradient(white:green)','Gradient(white:gray)','Gradient(white:blue)','Gradient(white:black)','Gradient(white:gray)','Gradient(white:pink)','Gradient(white:blue)','Gradient(white:yellow)','Gradient(white:green)','Gradient(white:red)'],
  67. 'chart.text.size': 10,
  68. 'chart.text.boxed': true,
  69. 'chart.text.halign': 'left',
  70. 'chart.text.color': 'black',
  71. 'chart.text.font': 'Arial',
  72. 'chart.contextmenu': null,
  73. 'chart.shadow': false,
  74. 'chart.shadow.color': '#666',
  75. 'chart.shadow.blur': 3,
  76. 'chart.shadow.offsetx': 3,
  77. 'chart.shadow.offsety': 3,
  78. 'chart.key': null,
  79. 'chart.key.background': 'white',
  80. 'chart.key.position': 'graph',
  81. 'chart.key.halign': 'right',
  82. 'chart.key.shadow': false,
  83. 'chart.key.shadow.color': '#666',
  84. 'chart.key.shadow.blur': 3,
  85. 'chart.key.shadow.offsetx': 2,
  86. 'chart.key.shadow.offsety': 2,
  87. 'chart.key.position.gutter.boxed': false,
  88. 'chart.key.position.x': null,
  89. 'chart.key.position.y': null,
  90. 'chart.key.color.shape': 'square',
  91. 'chart.key.rounded': true,
  92. 'chart.key.linewidth': 1,
  93. 'chart.key.colors': null,
  94. 'chart.key.interactive': false,
  95. 'chart.key.interactive.highlight.chart.stroke': 'black',
  96. 'chart.key.interactive.highlight.chart.fill': 'rgba(255,255,255,0.7)',
  97. 'chart.key.interactive.highlight.label': 'rgba(255,0,0,0.2)',
  98. 'chart.key.text.color': 'black',
  99. 'chart.tooltips': null,
  100. 'chart.tooltips.effect': 'fade',
  101. 'chart.tooltips.css.class': 'RGraph_tooltip',
  102. 'chart.tooltips.event': 'onclick',
  103. 'chart.highlight.stroke': 'rgba(0,0,0,0)',
  104. 'chart.highlight.fill': 'rgba(255,255,255,0.7)',
  105. 'chart.tooltips.highlight': true,
  106. 'chart.annotatable': false,
  107. 'chart.annotate.color': 'black',
  108. 'chart.zoom.factor': 1.5,
  109. 'chart.zoom.fade.in': true,
  110. 'chart.zoom.fade.out': true,
  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.events.click': null,
  123. 'chart.events.mousemove': null
  124. }
  125. // Store the data
  126. this.data = data;
  127. /**
  128. * Create the dollar objects so that functions can be added to them
  129. */
  130. for (var i=0; i<data.length; ++i) {
  131. this['$' + i] = {};
  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] = 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. * The function you call to draw the bar chart
  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. /**
  211. * This is new in May 2011 and facilitates indiviual gutter settings,
  212. * eg chart.gutter.left
  213. */
  214. this.gutterLeft = prop['chart.gutter.left'];
  215. this.gutterRight = prop['chart.gutter.right'];
  216. this.gutterTop = prop['chart.gutter.top'];
  217. this.gutterBottom = prop['chart.gutter.bottom'];
  218. // This stops the coords array from growing
  219. this.coords = [];
  220. /**
  221. * Stop this growing uncntrollably
  222. */
  223. this.coordsText = [];
  224. RG.DrawTitle(this, prop['chart.title'], this.gutterTop, null, prop['chart.title.size'] ? prop['chart.title.size'] : prop['chart.text.size'] + 2);
  225. this.DrawFunnel();
  226. /**
  227. * Setup the context menu if required
  228. */
  229. if (prop['chart.contextmenu']) {
  230. RG.ShowContext(this);
  231. }
  232. /**
  233. * Draw the labels on the chart
  234. */
  235. this.DrawLabels();
  236. /**
  237. * This function enables resizing
  238. */
  239. if (prop['chart.resizable']) {
  240. RG.AllowResizing(this);
  241. }
  242. /**
  243. * This installs the event listeners
  244. */
  245. RG.InstallEventListeners(this);
  246. /**
  247. * Fire the onfirstdraw event
  248. */
  249. if (this.firstDraw) {
  250. RG.fireCustomEvent(this, 'onfirstdraw');
  251. this.firstDrawFunc();
  252. this.firstDraw = false;
  253. }
  254. /**
  255. * Fire the RGraph ondraw event
  256. */
  257. RG.FireCustomEvent(this, 'ondraw');
  258. return this;
  259. };
  260. /**
  261. * This function actually draws the chart
  262. */
  263. this.drawFunnel =
  264. this.DrawFunnel = function ()
  265. {
  266. var width = ca.width - this.gutterLeft - this.gutterRight;
  267. var height = ca.height - this.gutterTop - this.gutterBottom;
  268. var total = RG.array_max(this.data);
  269. var accheight = this.gutterTop;
  270. /**
  271. * Loop through each segment to draw
  272. */
  273. // Set a shadow if it's been requested
  274. if (prop['chart.shadow']) {
  275. co.shadowColor = prop['chart.shadow.color'];
  276. co.shadowBlur = prop['chart.shadow.blur'];
  277. co.shadowOffsetX = prop['chart.shadow.offsetx'];
  278. co.shadowOffsetY = prop['chart.shadow.offsety'];
  279. }
  280. for (i=0,len=this.data.length; i<len; ++i) {
  281. var firstvalue = this.data[0];
  282. var firstwidth = (firstvalue / total) * width;
  283. var curvalue = this.data[i];
  284. var curwidth = (curvalue / total) * width;
  285. var curheight = height / this.data.length;
  286. var halfCurWidth = (curwidth / 2);
  287. var nextvalue = this.data[i + 1];
  288. var nextwidth = this.data[i + 1] ? (nextvalue / total) * width : null;
  289. var halfNextWidth = (nextwidth / 2);
  290. var center = this.gutterLeft + (firstwidth / 2);
  291. var x1 = center - halfCurWidth;
  292. var y1 = accheight;
  293. var x2 = center + halfCurWidth;
  294. var y2 = accheight;
  295. var x3 = center + halfNextWidth;
  296. var y3 = accheight + curheight;
  297. var x4 = center - halfNextWidth;
  298. var y4 = accheight + curheight;
  299. if (nextwidth && i < this.data.length - 1) {
  300. co.beginPath();
  301. co.strokeStyle = prop['chart.strokestyle'];
  302. co.fillStyle = prop['chart.colors'][i];
  303. co.moveTo(x1, y1);
  304. co.lineTo(x2, y2);
  305. co.lineTo(x3, y3);
  306. co.lineTo(x4, y4);
  307. co.closePath();
  308. /**
  309. * Store the coordinates
  310. */
  311. this.coords.push([x1, y1, x2, y2, x3, y3, x4, y4]);
  312. }
  313. // The redrawing if the shadow is on will do the stroke
  314. if (!prop['chart.shadow']) {
  315. co.stroke();
  316. }
  317. co.fill();
  318. accheight += curheight;
  319. }
  320. /**
  321. * If the shadow is enabled, redraw every segment, in order to allow for shadows going upwards
  322. */
  323. if (prop['chart.shadow']) {
  324. RG.NoShadow(this);
  325. for (i=0; i<this.coords.length; ++i) {
  326. co.strokeStyle = prop['chart.strokestyle'];
  327. co.fillStyle = prop['chart.colors'][i];
  328. co.beginPath();
  329. co.moveTo(this.coords[i][0], this.coords[i][1]);
  330. co.lineTo(this.coords[i][2], this.coords[i][3]);
  331. co.lineTo(this.coords[i][4], this.coords[i][5]);
  332. co.lineTo(this.coords[i][6], this.coords[i][7]);
  333. co.closePath();
  334. co.stroke();
  335. co.fill();
  336. }
  337. }
  338. /**
  339. * Lastly, draw the key if necessary
  340. */
  341. if (prop['chart.key'] && prop['chart.key'].length) {
  342. RG.DrawKey(this, prop['chart.key'], prop['chart.colors']);
  343. }
  344. };
  345. /**
  346. * Draws the labels
  347. */
  348. this.drawLabels =
  349. this.DrawLabels = function ()
  350. {
  351. /**
  352. * Draws the labels
  353. */
  354. if (prop['chart.labels'] && prop['chart.labels'].length > 0) {
  355. var font = prop['chart.text.font'];
  356. var size = prop['chart.text.size'];
  357. var color = prop['chart.text.color'];
  358. var labels = prop['chart.labels'];
  359. var halign = prop['chart.text.halign'] == 'left' ? 'left' : 'center';
  360. var bgcolor = prop['chart.text.boxed'] ? 'white' : null;
  361. if (typeof prop['chart.labels.x'] == 'number') {
  362. var x = prop['chart.labels.x'];
  363. } else {
  364. var x = halign == 'left' ? (this.gutterLeft - 15) : ((ca.width - this.gutterLeft - this.gutterRight) / 2) + this.gutterLeft;
  365. }
  366. for (var j=0; j<this.coords.length; ++j) { // MUST be "j"
  367. co.beginPath();
  368. // Set the color back to black
  369. co.strokeStyle = 'black';
  370. co.fillStyle = color;
  371. // Turn off any shadow
  372. RG.NoShadow(this);
  373. var label = labels[j];
  374. RG.text2(this,{'font':font,
  375. 'size':size,
  376. 'x':x,
  377. 'y':this.coords[j][1],
  378. 'text':label,
  379. 'valign':'center',
  380. 'halign':halign,
  381. 'bounding': prop['chart.text.boxed'],
  382. 'boundingFill':bgcolor,
  383. 'tag': 'labels'
  384. });
  385. if (prop['chart.labels.sticks']) {
  386. /**
  387. * Measure the text
  388. */
  389. co.font = size + 'pt ' + font;
  390. var labelWidth = co.measureText(label).width;
  391. /**
  392. * Draw the horizontal indicator line
  393. */
  394. co.beginPath();
  395. co.strokeStyle = 'gray';
  396. co.moveTo(x + labelWidth + 10, ma.round(this.coords[j][1]));
  397. co.lineTo(this.coords[j][0] - 10, ma.round(this.coords[j][1]));
  398. co.stroke();
  399. }
  400. }
  401. /**
  402. * This draws the last labels if defined
  403. */
  404. var lastLabel = labels[j];
  405. if (lastLabel) {
  406. RG.text2(this,{'font':font,
  407. 'size':size,
  408. 'x':x,
  409. 'y':this.coords[j - 1][5],
  410. 'text':lastLabel,
  411. 'valign':'center',
  412. 'halign':halign,
  413. 'bounding': prop['chart.text.boxed'],
  414. 'boundingFill':bgcolor,
  415. 'tag': 'labels'
  416. });
  417. if (prop['chart.labels.sticks']) {
  418. /**
  419. * Measure the text
  420. */
  421. co.font = size + 'pt ' + font;
  422. var labelWidth = co.measureText(lastLabel).width;
  423. /**
  424. * Draw the horizontal indicator line
  425. */
  426. co.beginPath();
  427. co.strokeStyle = 'gray';
  428. //co.moveTo(x + labelWidth + 10, ma.round(this.coords[j][1]));
  429. //co.lineTo(this.coords[j][0] - 10, ma.round(this.coords[j][1]));
  430. co.moveTo(x + labelWidth + 10, Math.round(this.coords[j - 1][7]));
  431. co.lineTo(this.coords[j - 1][6] - 10, Math.round(this.coords[j - 1][7]));
  432. co.stroke();
  433. }
  434. }
  435. }
  436. };
  437. /**
  438. * Gets the appropriate segment that has been highlighted
  439. */
  440. this.getShape =
  441. this.getSegment = function (e)
  442. {
  443. //var canvas = ca = e.target;
  444. //var co = this.context;
  445. //var prop = this.properties;
  446. var coords = this.coords;
  447. var mouseCoords = RG.getMouseXY(e);
  448. var x = mouseCoords[0];
  449. var y = mouseCoords[1];
  450. for (i=0,len=coords.length; i<len; ++i) {
  451. var segment = coords[i]
  452. // Path testing
  453. co.beginPath();
  454. co.moveTo(segment[0], segment[1]);
  455. co.lineTo(segment[2], segment[3]);
  456. co.lineTo(segment[4], segment[5]);
  457. co.lineTo(segment[6], segment[7]);
  458. co.lineTo(segment[8], segment[9]);
  459. if (co.isPointInPath(x, y)) {
  460. var tooltip = RGraph.parseTooltipText(prop['chart.tooltips'], i);
  461. return {0: this, 1: coords, 2: i, 'object': this, 'coords': segment, 'index': i, 'tooltip': tooltip};
  462. }
  463. }
  464. return null;
  465. };
  466. /**
  467. * Each object type has its own Highlight() function which highlights the appropriate shape
  468. *
  469. * @param object shape The shape to highlight
  470. */
  471. this.highlight =
  472. this.Highlight = function (shape)
  473. {
  474. if (prop['chart.tooltips.highlight']) {
  475. // Add the new highlight
  476. var coords = shape['coords'];
  477. co.beginPath();
  478. co.strokeStyle = prop['chart.highlight.stroke'];
  479. co.fillStyle = prop['chart.highlight.fill'];
  480. co.moveTo(coords[0], coords[1]);
  481. co.lineTo(coords[2], coords[3]);
  482. co.lineTo(coords[4], coords[5]);
  483. co.lineTo(coords[6], coords[7]);
  484. co.closePath();
  485. co.stroke();
  486. co.fill();
  487. }
  488. };
  489. /**
  490. * The getObjectByXY() worker method. Don't call this call:
  491. *
  492. * RGraph.ObjectRegistry.getObjectByXY(e)
  493. *
  494. * @param object e The event object
  495. */
  496. this.getObjectByXY = function (e)
  497. {
  498. var mouseXY = RGraph.getMouseXY(e);
  499. if (
  500. mouseXY[0] > prop['chart.gutter.left']
  501. && mouseXY[0] < (ca.width - prop['chart.gutter.right'])
  502. && mouseXY[1] > prop['chart.gutter.top']
  503. && mouseXY[1] < (ca.height - prop['chart.gutter.bottom'])
  504. ) {
  505. return this;
  506. }
  507. };
  508. /**
  509. * This function positions a tooltip when it is displayed
  510. *
  511. * @param obj object The chart object
  512. * @param int x The X coordinate specified for the tooltip
  513. * @param int y The Y coordinate specified for the tooltip
  514. * @param objec tooltip The tooltips DIV element
  515. */
  516. this.positionTooltip = function (obj, x, y, tooltip, idx)
  517. {
  518. var coords = obj.coords[tooltip.__index__];
  519. var x1 = coords[0];
  520. var y1 = coords[1];
  521. var x2 = coords[2];
  522. var y2 = coords[3];
  523. var x3 = coords[4];
  524. var y3 = coords[5];
  525. var x4 = coords[6];
  526. var y4 = coords[7];
  527. var coordW = x2 - x1;
  528. var coordX = x1 + (coordW / 2);
  529. var canvasXY = RG.getCanvasXY(ca);
  530. var gutterLeft = prop['chart.gutter.left'];
  531. var gutterTop = prop['chart.gutter.top'];
  532. var width = tooltip.offsetWidth;
  533. var height = tooltip.offsetHeight;
  534. // Set the top position
  535. tooltip.style.left = 0;
  536. tooltip.style.top = canvasXY[1] + y1 + ((y3 - y2) / 2) - height - 7 + 'px';
  537. // By default any overflow is hidden
  538. tooltip.style.overflow = '';
  539. // The arrow
  540. var img = new Image();
  541. img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABEAAAAFCAYAAACjKgd3AAAARUlEQVQYV2NkQAN79+797+RkhC4M5+/bd47B2dmZEVkBCgcmgcsgbAaA9GA1BCSBbhAuA/AagmwQPgMIGgIzCD0M0AMMAEFVIAa6UQgcAAAAAElFTkSuQmCC';
  542. img.style.position = 'absolute';
  543. img.id = '__rgraph_tooltip_pointer__';
  544. img.style.top = (tooltip.offsetHeight - 2) + 'px';
  545. tooltip.appendChild(img);
  546. // Reposition the tooltip if at the edges:
  547. // LEFT edge
  548. if ((canvasXY[0] + coordX - (width / 2)) < 5) {
  549. tooltip.style.left = (canvasXY[0] + coordX - (width * 0.1)) + 'px';
  550. img.style.left = ((width * 0.1) - 8.5) + 'px';
  551. // RIGHT edge
  552. } else if ((canvasXY[0] + coordX + (width / 2)) > document.body.offsetWidth) {
  553. tooltip.style.left = canvasXY[0] + coordX - (width * 0.9) + 'px';
  554. img.style.left = ((width * 0.9) - 8.5) + 'px';
  555. // Default positioning - CENTERED
  556. } else {
  557. tooltip.style.left = (canvasXY[0] + coordX - (width / 2)) + 'px';
  558. img.style.left = ((width * 0.5) - 8.5) + 'px';
  559. }
  560. };
  561. /**
  562. * This allows for easy specification of gradients
  563. */
  564. this.parseColors = function ()
  565. {
  566. // Save the original colors so that they can be restored when the canvas is reset
  567. if (this.original_colors.length === 0) {
  568. this.original_colors['chart.colors'] = RG.array_clone(prop['chart.colors']);
  569. this.original_colors['chart.key.colors'] = RG.array_clone(prop['chart.key.colors']);
  570. this.original_colors['chart.highlight.fill'] = RG.array_clone(prop['chart.highlight.fill']);
  571. this.original_colors['chart.highlight.stroke'] = RG.array_clone(prop['chart.highlight.stroke']);
  572. this.original_colors['chart.strokestyle'] = RG.array_clone(prop['chart.strokestyle']);
  573. }
  574. var colors = prop['chart.colors'];
  575. for (var i=0; i<colors.length; ++i) {
  576. colors[i] = this.parseSingleColorForHorizontalGradient(colors[i]);
  577. }
  578. var keyColors = prop['chart.key.colors'];
  579. if (keyColors) {
  580. for (var i=0; i<keyColors.length; ++i) {
  581. keyColors[i] = this.parseSingleColorForHorizontalGradient(keyColors[i]);
  582. }
  583. }
  584. prop['chart.strokestyle'] = this.parseSingleColorForVerticalGradient(prop['chart.strokestyle']);
  585. prop['chart.highlight.stroke'] = this.parseSingleColorForHorizontalGradient(prop['chart.highlight.stroke']);
  586. prop['chart.highlight.fill'] = this.parseSingleColorForHorizontalGradient(prop['chart.highlight.fill']);
  587. };
  588. /**
  589. * This parses a single color value
  590. */
  591. this.parseSingleColorForHorizontalGradient = function (color)
  592. {
  593. if (!color || typeof(color) != 'string') {
  594. return color;
  595. }
  596. if (color.match(/^gradient\((.*)\)$/i)) {
  597. var parts = RegExp.$1.split(':');
  598. // Create the gradient
  599. var grad = co.createLinearGradient(prop['chart.gutter.left'],0,ca.width - prop['chart.gutter.right'],0);
  600. var diff = 1 / (parts.length - 1);
  601. grad.addColorStop(0, RGraph.trim(parts[0]));
  602. for (var j=1; j<parts.length; ++j) {
  603. grad.addColorStop(j * diff, RG.trim(parts[j]));
  604. }
  605. }
  606. return grad ? grad : color;
  607. };
  608. /**
  609. * This parses a single color value
  610. */
  611. this.parseSingleColorForVerticalGradient = function (color)
  612. {
  613. if (!color || typeof(color) != 'string') {
  614. return color;
  615. }
  616. if (color.match(/^gradient\((.*)\)$/i)) {
  617. var parts = RegExp.$1.split(':');
  618. // Create the gradient
  619. var grad = co.createLinearGradient(0, prop['chart.gutter.top'],0,ca.height - prop['chart.gutter.bottom']);
  620. var diff = 1 / (parts.length - 1);
  621. grad.addColorStop(0, RGraph.trim(parts[0]));
  622. for (var j=1; j<parts.length; ++j) {
  623. grad.addColorStop(j * diff, RG.trim(parts[j]));
  624. }
  625. }
  626. return grad ? grad : color;
  627. };
  628. /**
  629. * This function handles highlighting an entire data-series for the interactive
  630. * key
  631. *
  632. * @param int index The index of the data series to be highlighted
  633. */
  634. this.interactiveKeyHighlight = function (index)
  635. {
  636. var coords = this.coords[index];
  637. if (coords && coords.length == 8) {
  638. var pre_linewidth = co.lineWidth;
  639. co.lineWidth = 2;
  640. co.strokeStyle = prop['chart.key.interactive.highlight.chart.stroke'];
  641. co.fillStyle = prop['chart.key.interactive.highlight.chart.fill'];
  642. co.beginPath();
  643. co.moveTo(coords[0], coords[1]);
  644. co.lineTo(coords[2], coords[3]);
  645. co.lineTo(coords[4], coords[5]);
  646. co.lineTo(coords[6], coords[7]);
  647. co.closePath();
  648. co.fill();
  649. co.stroke();
  650. // Reset the linewidth
  651. co.lineWidth = pre_linewidth;
  652. }
  653. };
  654. /**
  655. * Using a function to add events makes it easier to facilitate method chaining
  656. *
  657. * @param string type The type of even to add
  658. * @param function func
  659. */
  660. this.on = function (type, func)
  661. {
  662. if (type.substr(0,2) !== 'on') {
  663. type = 'on' + type;
  664. }
  665. this[type] = func;
  666. return this;
  667. };
  668. /**
  669. * This function runs once only
  670. * (put at the end of the file (before any effects))
  671. */
  672. this.firstDrawFunc = function ()
  673. {
  674. };
  675. /**
  676. * Always now regsiter the object
  677. */
  678. RG.Register(this);
  679. };