RGraph.bar.js 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712
  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 bar chart constructor
  17. *
  18. * @param object canvas The canvas object
  19. * @param array data The chart data
  20. */
  21. RGraph.Bar = function (id, data)
  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 = 'bar';
  29. this.max = 0;
  30. this.stackedOrGrouped = false;
  31. this.isRGraph = true;
  32. this.uid = RGraph.CreateUID();
  33. this.canvas.uid = this.canvas.uid ? this.canvas.uid : RGraph.CreateUID();
  34. this.colorsParsed = false;
  35. /**
  36. * Compatibility with older browsers
  37. */
  38. RGraph.OldBrowserCompat(this.context);
  39. // Various config type stuff
  40. this.properties = {
  41. 'chart.background.barcolor1': 'rgba(0,0,0,0)',
  42. 'chart.background.barcolor2': 'rgba(0,0,0,0)',
  43. 'chart.background.grid': true,
  44. 'chart.background.grid.color': '#ddd',
  45. 'chart.background.grid.width': 1,
  46. 'chart.background.grid.hsize': 20,
  47. 'chart.background.grid.vsize': 20,
  48. 'chart.background.grid.vlines': true,
  49. 'chart.background.grid.hlines': true,
  50. 'chart.background.grid.border': true,
  51. 'chart.background.grid.autofit':true,
  52. 'chart.background.grid.autofit.numhlines': 5,
  53. 'chart.background.grid.autofit.numvlines': 20,
  54. 'chart.background.grid.dashed': false,
  55. 'chart.background.grid.dotted': false,
  56. 'chart.background.image.stretch': true,
  57. 'chart.background.image.x': null,
  58. 'chart.background.image.y': null,
  59. 'chart.background.image.w': null,
  60. 'chart.background.image.h': null,
  61. 'chart.background.image.align': null,
  62. 'chart.numyticks': 10,
  63. 'chart.hmargin': 5,
  64. 'chart.hmargin.grouped': 1,
  65. 'chart.strokecolor': 'rgba(0,0,0,0)',
  66. 'chart.axis.color': 'black',
  67. 'chart.axis.linewidth': 1,
  68. 'chart.gutter.top': 25,
  69. 'chart.gutter.bottom': 25,
  70. 'chart.gutter.left': 25,
  71. 'chart.gutter.right': 25,
  72. 'chart.labels': null,
  73. 'chart.labels.ingraph': null,
  74. 'chart.labels.above': false,
  75. 'chart.labels.above.decimals': 0,
  76. 'chart.labels.above.size': null,
  77. 'chart.labels.above.color': null,
  78. 'chart.labels.above.angle': null,
  79. 'chart.ylabels': true,
  80. 'chart.ylabels.count': 5,
  81. 'chart.ylabels.inside': false,
  82. 'chart.xlabels.offset': 0,
  83. 'chart.xaxispos': 'bottom',
  84. 'chart.yaxispos': 'left',
  85. 'chart.text.angle': 0,
  86. 'chart.text.color': 'black', // Gradients aren't supported for this color
  87. 'chart.text.size': 10,
  88. 'chart.text.font': 'Arial',
  89. 'chart.ymin': 0,
  90. 'chart.ymax': null,
  91. 'chart.title': '',
  92. 'chart.title.font': null,
  93. 'chart.title.background': null, // Gradients aren't supported for this color
  94. 'chart.title.hpos': null,
  95. 'chart.title.vpos': null,
  96. 'chart.title.bold': true,
  97. 'chart.title.xaxis': '',
  98. 'chart.title.xaxis.bold': true,
  99. 'chart.title.xaxis.size': null,
  100. 'chart.title.xaxis.font': null,
  101. 'chart.title.yaxis': '',
  102. 'chart.title.yaxis.bold': true,
  103. 'chart.title.yaxis.size': null,
  104. 'chart.title.yaxis.font': null,
  105. 'chart.title.yaxis.color': null, // Gradients aren't supported for this color
  106. 'chart.title.xaxis.pos': null,
  107. 'chart.title.yaxis.pos': null,
  108. 'chart.title.yaxis.x': null,
  109. 'chart.title.yaxis.y': null,
  110. 'chart.title.xaxis.x': null,
  111. 'chart.title.xaxis.y': null,
  112. 'chart.title.x': null,
  113. 'chart.title.y': null,
  114. 'chart.title.halign': null,
  115. 'chart.title.valign': null,
  116. 'chart.colors': ['#01B4FF', '#0f0', '#00f', '#ff0', '#0ff', '#0f0'],
  117. 'chart.colors.sequential': false,
  118. 'chart.colors.reverse': false,
  119. 'chart.grouping': 'grouped',
  120. 'chart.variant': 'bar',
  121. 'chart.variant.sketch.verticals': true,
  122. 'chart.shadow': false,
  123. 'chart.shadow.color': '#999', // Gradients aren't supported for this color
  124. 'chart.shadow.offsetx': 3,
  125. 'chart.shadow.offsety': 3,
  126. 'chart.shadow.blur': 3,
  127. 'chart.tooltips': null,
  128. 'chart.tooltips.effect': 'fade',
  129. 'chart.tooltips.css.class': 'RGraph_tooltip',
  130. 'chart.tooltips.event': 'onclick',
  131. 'chart.tooltips.highlight': true,
  132. 'chart.highlight.stroke': 'rgba(0,0,0,0)',
  133. 'chart.highlight.fill': 'rgba(255,255,255,0.7)',
  134. 'chart.background.hbars': null,
  135. 'chart.key': null,
  136. 'chart.key.background': 'white',
  137. 'chart.key.position': 'graph',
  138. 'chart.key.shadow': false,
  139. 'chart.key.shadow.color': '#666',
  140. 'chart.key.shadow.blur': 3,
  141. 'chart.key.shadow.offsetx': 2,
  142. 'chart.key.shadow.offsety': 2,
  143. 'chart.key.position.gutter.boxed':false,
  144. 'chart.key.position.x': null,
  145. 'chart.key.position.y': null,
  146. 'chart.key.interactive': false,
  147. 'chart.key.interactive.highlight.chart.stroke':'black',
  148. 'chart.key.interactive.highlight.chart.fill':'rgba(255,255,255,0.7)',
  149. 'chart.key.interactive.highlight.label':'rgba(255,0,0,0.2)',
  150. 'chart.key.halign': 'right',
  151. 'chart.key.color.shape': 'square',
  152. 'chart.key.rounded': true,
  153. 'chart.key.text.size': 10,
  154. 'chart.key.linewidth': 1,
  155. 'chart.key.colors': null,
  156. 'chart.key.text.color': 'black',
  157. 'chart.contextmenu': null,
  158. 'chart.units.pre': '',
  159. 'chart.units.post': '',
  160. 'chart.scale.decimals': 0,
  161. 'chart.scale.point': '.',
  162. 'chart.scale.thousand': ',',
  163. 'chart.crosshairs': false,
  164. 'chart.crosshairs.color': '#333',
  165. 'chart.crosshairs.hline': true,
  166. 'chart.crosshairs.vline': true,
  167. 'chart.linewidth': 1,
  168. 'chart.annotatable': false,
  169. 'chart.annotate.color': 'black',
  170. 'chart.zoom.factor': 1.5,
  171. 'chart.zoom.fade.in': true,
  172. 'chart.zoom.fade.out': true,
  173. 'chart.zoom.hdir': 'right',
  174. 'chart.zoom.vdir': 'down',
  175. 'chart.zoom.frames': 25,
  176. 'chart.zoom.delay': 16.666,
  177. 'chart.zoom.shadow': true,
  178. 'chart.zoom.background': true,
  179. 'chart.resizable': false,
  180. 'chart.resize.handle.background': null,
  181. 'chart.adjustable': false,
  182. 'chart.noaxes': false,
  183. 'chart.noxaxis': false,
  184. 'chart.noyaxis': false,
  185. 'chart.events.click': null,
  186. 'chart.events.mousemove': null,
  187. 'chart.numxticks': null,
  188. 'chart.bevel': false
  189. }
  190. // Check for support
  191. if (!this.canvas) {
  192. alert('[BAR] No canvas support');
  193. return;
  194. }
  195. /**
  196. * Determine whether the chart will contain stacked or grouped bars
  197. */
  198. for (var i=0; i<data.length; ++i) {
  199. if (typeof(data[i]) == 'object' && !RGraph.is_null(data[i])) {
  200. this.stackedOrGrouped = true;
  201. }
  202. }
  203. /**
  204. * Create the dollar objects so that functions can be added to them
  205. */
  206. var linear_data = RGraph.array_linearize(data);
  207. for (var i=0; i<linear_data.length; ++i) {
  208. this['$' + i] = {};
  209. }
  210. // Store the data
  211. this.data = data;
  212. // Used to store the coords of the bars
  213. this.coords = [];
  214. this.coords2 = [];
  215. this.coordsText = [];
  216. /**
  217. * This linearises the data. Doing so can make it easier to pull
  218. * out the appropriate data from tooltips
  219. */
  220. this.data_arr = RGraph.array_linearize(this.data);
  221. /**
  222. * Translate half a pixel for antialiasing purposes - but only if it hasn't beeen
  223. * done already
  224. */
  225. if (!this.canvas.__rgraph_aa_translated__) {
  226. this.context.translate(0.5,0.5);
  227. this.canvas.__rgraph_aa_translated__ = true;
  228. }
  229. ///////////////////////////////// SHORT PROPERTIES /////////////////////////////////
  230. var RG = RGraph;
  231. var ca = this.canvas;
  232. var co = ca.getContext('2d');
  233. var prop = this.properties;
  234. //////////////////////////////////// METHODS ///////////////////////////////////////
  235. /**
  236. * A setter
  237. *
  238. * @param name string The name of the property to set
  239. * @param value mixed The value of the property
  240. */
  241. this.Set = function (name, value)
  242. {
  243. name = name.toLowerCase();
  244. /**
  245. * This should be done first - prepend the propertyy name with "chart." if necessary
  246. */
  247. if (name.substr(0,6) != 'chart.') {
  248. name = 'chart.' + name;
  249. }
  250. if (name == 'chart.labels.abovebar') {
  251. name = 'chart.labels.above';
  252. }
  253. if (name == 'chart.strokestyle') {
  254. name = 'chart.strokecolor';
  255. }
  256. /**
  257. * Check for xaxispos
  258. */
  259. if (name == 'chart.xaxispos' ) {
  260. if (value != 'bottom' && value != 'center' && value != 'top') {
  261. alert('[BAR] (' + this.id + ') chart.xaxispos should be top, center or bottom. Tried to set it to: ' + value + ' Changing it to center');
  262. value = 'center';
  263. }
  264. if (value == 'top') {
  265. for (var i=0; i<this.data.length; ++i) {
  266. if (typeof(this.data[i]) == 'number' && this.data[i] > 0) {
  267. alert('[BAR] The data element with index ' + i + ' should be negative');
  268. }
  269. }
  270. }
  271. }
  272. /**
  273. * lineWidth doesn't appear to like a zero setting
  274. */
  275. if (name.toLowerCase() == 'chart.linewidth' && value == 0) {
  276. value = 0.0001;
  277. }
  278. prop[name] = value;
  279. return this;
  280. }
  281. /**
  282. * A getter
  283. *
  284. * @param name string The name of the property to get
  285. */
  286. this.Get = function (name)
  287. {
  288. /**
  289. * This should be done first - prepend the property name with "chart." if necessary
  290. */
  291. if (name.substr(0,6) != 'chart.') {
  292. name = 'chart.' + name;
  293. }
  294. return prop[name];
  295. }
  296. /**
  297. * The function you call to draw the bar chart
  298. */
  299. this.Draw = function ()
  300. {
  301. // MUST be the first thing done!
  302. if (typeof(prop['chart.background.image']) == 'string') {
  303. RG.DrawBackgroundImage(this);
  304. }
  305. /**
  306. * Fire the onbeforedraw event
  307. */
  308. RG.FireCustomEvent(this, 'onbeforedraw');
  309. /**
  310. * Parse the colors. This allows for simple gradient syntax
  311. */
  312. if (!this.colorsParsed) {
  313. this.parseColors();
  314. // Don't want to do this again
  315. this.colorsParsed = true;
  316. }
  317. /**
  318. * This is new in May 2011 and facilitates indiviual gutter settings,
  319. * eg chart.gutter.left
  320. */
  321. this.gutterLeft = prop['chart.gutter.left'];
  322. this.gutterRight = prop['chart.gutter.right'];
  323. this.gutterTop = prop['chart.gutter.top'];
  324. this.gutterBottom = prop['chart.gutter.bottom'];
  325. // Cache this in a class variable as it's used rather a lot
  326. /**
  327. * Check for tooltips and alert the user that they're not supported with pyramid charts
  328. */
  329. if ( (prop['chart.variant'] == 'pyramid' || prop['chart.variant'] == 'dot')
  330. && typeof(prop['chart.tooltips']) == 'object'
  331. && prop['chart.tooltips']
  332. && prop['chart.tooltips'].length > 0) {
  333. alert('[BAR] (' + this.id + ') Sorry, tooltips are not supported with dot or pyramid charts');
  334. }
  335. /**
  336. * Stop the coords arrays from growing uncontrollably
  337. */
  338. this.coords = [];
  339. this.coords2 = [];
  340. this.coordsText = [];
  341. /**
  342. * Work out a few things. They need to be here because they depend on things you can change before you
  343. * call Draw() but after you instantiate the object
  344. */
  345. this.max = 0;
  346. this.grapharea = ca.height - this.gutterTop - this.gutterBottom;
  347. this.halfgrapharea = this.grapharea / 2;
  348. this.halfTextHeight = prop['chart.text.size'] / 2;
  349. // Progressively Draw the chart
  350. RG.background.Draw(this);
  351. //If it's a sketch chart variant, draw the axes first
  352. if (prop['chart.variant'] == 'sketch') {
  353. this.DrawAxes();
  354. this.Drawbars();
  355. } else {
  356. this.Drawbars();
  357. this.DrawAxes();
  358. }
  359. this.DrawLabels();
  360. /**
  361. * Draw the bevel if required
  362. */
  363. if (prop['chart.bevel'] || prop['chart.bevelled']) {
  364. this.DrawBevel();
  365. }
  366. // Draw the key if necessary
  367. if (prop['chart.key'] && prop['chart.key'].length) {
  368. RG.DrawKey(this, prop['chart.key'], prop['chart.colors']);
  369. }
  370. /**
  371. * Setup the context menu if required
  372. */
  373. if (prop['chart.contextmenu']) {
  374. RG.ShowContext(this);
  375. }
  376. /**
  377. * Draw "in graph" labels
  378. */
  379. if (prop['chart.labels.ingraph']) {
  380. RG.DrawInGraphLabels(this);
  381. }
  382. /**
  383. * This function enables resizing
  384. */
  385. if (prop['chart.resizable']) {
  386. RG.AllowResizing(this);
  387. }
  388. /**
  389. * This installs the event listeners
  390. */
  391. RG.InstallEventListeners(this);
  392. /**
  393. * Fire the RGraph ondraw event
  394. */
  395. RG.FireCustomEvent(this, 'ondraw');
  396. return this;
  397. }
  398. /**
  399. * Draws the charts axes
  400. */
  401. this.DrawAxes = function ()
  402. {
  403. if (prop['chart.noaxes']) {
  404. return;
  405. }
  406. var xaxispos = prop['chart.xaxispos'];
  407. var yaxispos = prop['chart.yaxispos'];
  408. var isSketch = prop['chart.variant'] == 'sketch';
  409. co.beginPath();
  410. co.strokeStyle = prop['chart.axis.color'];
  411. co.lineWidth = prop['chart.axis.linewidth'] + 0.001;
  412. if (ISSAFARI == -1) {
  413. co.lineCap = 'square';
  414. }
  415. // Draw the Y axis
  416. if (prop['chart.noyaxis'] == false) {
  417. if (yaxispos == 'right') {
  418. co.moveTo(ca.width - this.gutterRight + (isSketch ? 3 : 0), this.gutterTop - (isSketch ? 3 : 0));
  419. co.lineTo(ca.width - this.gutterRight - (isSketch ? 2 : 0), ca.height - this.gutterBottom + (isSketch ? 5 : 0));
  420. } else {
  421. co.moveTo(this.gutterLeft - (isSketch ? 2 : 0), this.gutterTop - (isSketch ? 5 : 0));
  422. co.lineTo(this.gutterLeft - (isSketch ? 1 : 0), ca.height - this.gutterBottom + (isSketch ? 5 : 0));
  423. }
  424. }
  425. // Draw the X axis
  426. if (prop['chart.noxaxis'] == false) {
  427. if (xaxispos == 'center') {
  428. co.moveTo(this.gutterLeft - (isSketch ? 5 : 0), Math.round(((ca.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop + (isSketch ? 2 : 0)));
  429. co.lineTo(ca.width - this.gutterRight + (isSketch ? 5 : 0), Math.round(((ca.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop - (isSketch ? 2 : 0)));
  430. } else if (xaxispos == 'top') {
  431. co.moveTo(this.gutterLeft - (isSketch ? 3 : 0), this.gutterTop - (isSketch ? 3 : 0));
  432. co.lineTo(ca.width - this.gutterRight + (isSketch ? 5 : 0), this.gutterTop + (isSketch ? 2 : 0));
  433. } else {
  434. co.moveTo(this.gutterLeft - (isSketch ? 5 : 0), ca.height - this.gutterBottom - (isSketch ? 2 : 0));
  435. co.lineTo(ca.width - this.gutterRight + (isSketch ? 8 : 0), ca.height - this.gutterBottom + (isSketch ? 2 : 0));
  436. }
  437. }
  438. var numYTicks = prop['chart.numyticks'];
  439. // Draw the Y tickmarks
  440. if (prop['chart.noyaxis'] == false && !isSketch) {
  441. var yTickGap = (ca.height - this.gutterTop - this.gutterBottom) / numYTicks;
  442. var xpos = yaxispos == 'left' ? this.gutterLeft : ca.width - this.gutterRight;
  443. if (this.properties['chart.numyticks'] > 0) {
  444. for (y=this.gutterTop;
  445. xaxispos == 'center' ? y <= (ca.height - this.gutterBottom) : y < (ca.height - this.gutterBottom + (xaxispos == 'top' ? 1 : 0));
  446. y += yTickGap) {
  447. if (xaxispos == 'center' && y == (this.gutterTop + (this.grapharea / 2))) continue;
  448. // X axis at the top
  449. if (xaxispos == 'top' && y == this.gutterTop) continue;
  450. co.moveTo(xpos + (yaxispos == 'left' ? 0 : 0), Math.round(y));
  451. co.lineTo(xpos + (yaxispos == 'left' ? -3 : 3), Math.round(y));
  452. }
  453. }
  454. /**
  455. * If the X axis is not being shown, draw an extra tick
  456. */
  457. if (prop['chart.noxaxis']) {
  458. if (xaxispos == 'center') {
  459. co.moveTo(xpos + (yaxispos == 'left' ? -3 : 3), Math.round(ca.height / 2));
  460. co.lineTo(xpos, Math.round(ca.height / 2));
  461. } else if (xaxispos == 'top') {
  462. co.moveTo(xpos + (yaxispos == 'left' ? -3 : 3), Math.round(this.gutterTop));
  463. co.lineTo(xpos, Math.round(this.gutterTop));
  464. } else {
  465. co.moveTo(xpos + (yaxispos == 'left' ? -3 : 3), Math.round(ca.height - this.gutterBottom));
  466. co.lineTo(xpos, Math.round(ca.height - this.gutterBottom));
  467. }
  468. }
  469. }
  470. // Draw the X tickmarks
  471. if (prop['chart.noxaxis'] == false && !isSketch) {
  472. if (typeof(prop['chart.numxticks']) == 'number') {
  473. var xTickGap = (ca.width - this.gutterLeft - this.gutterRight) / prop['chart.numxticks'];
  474. } else {
  475. var xTickGap = (ca.width - this.gutterLeft - this.gutterRight) / this.data.length;
  476. }
  477. if (xaxispos == 'bottom') {
  478. yStart = ca.height - this.gutterBottom;
  479. yEnd = (ca.height - this.gutterBottom) + 3;
  480. } else if (xaxispos == 'top') {
  481. yStart = this.gutterTop - 3;
  482. yEnd = this.gutterTop;
  483. } else if (xaxispos == 'center') {
  484. yStart = ((ca.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop + 3;
  485. yEnd = ((ca.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop - 3;
  486. }
  487. yStart = yStart;
  488. yEnd = yEnd;
  489. //////////////// X TICKS ////////////////
  490. var noEndXTick = prop['chart.noendxtick'];
  491. for (x=this.gutterLeft + (yaxispos == 'left' ? xTickGap : 0),len=(ca.width - this.gutterRight + (yaxispos == 'left' ? 5 : 0)); x<len; x+=xTickGap) {
  492. if (yaxispos == 'left' && !noEndXTick && x > this.gutterLeft) {
  493. co.moveTo(Math.round(x), yStart);
  494. co.lineTo(Math.round(x), yEnd);
  495. } else if (yaxispos == 'left' && noEndXTick && x > this.gutterLeft && x < (ca.width - this.gutterRight) ) {
  496. co.moveTo(Math.round(x), yStart);
  497. co.lineTo(Math.round(x), yEnd);
  498. } else if (yaxispos == 'right' && x < (ca.width - this.gutterRight) && !noEndXTick) {
  499. co.moveTo(Math.round(x), yStart);
  500. co.lineTo(Math.round(x), yEnd);
  501. } else if (yaxispos == 'right' && x < (ca.width - this.gutterRight) && x > (this.gutterLeft) && noEndXTick) {
  502. co.moveTo(Math.round(x), yStart);
  503. co.lineTo(Math.round(x), yEnd);
  504. }
  505. }
  506. if (prop['chart.noyaxis'] || prop['chart.numxticks'] == null) {
  507. if (typeof(prop['chart.numxticks']) == 'number' && prop['chart.numxticks'] > 0) {
  508. co.moveTo(Math.round(this.gutterLeft), yStart);
  509. co.lineTo(Math.round(this.gutterLeft), yEnd);
  510. }
  511. }
  512. //////////////// X TICKS ////////////////
  513. }
  514. /**
  515. * If the Y axis is not being shown, draw an extra tick
  516. */
  517. if (prop['chart.noyaxis'] && prop['chart.noxaxis'] == false && prop['chart.numxticks'] == null) {
  518. if (xaxispos == 'center') {
  519. co.moveTo(Math.round(this.gutterLeft), (ca.height / 2) - 3);
  520. co.lineTo(Math.round(this.gutterLeft), (ca.height / 2) + 3);
  521. } else {
  522. co.moveTo(Math.round(this.gutterLeft), ca.height - this.gutterBottom);
  523. co.lineTo(Math.round(this.gutterLeft), ca.height - this.gutterBottom + 3);
  524. }
  525. }
  526. co.stroke();
  527. }
  528. /**
  529. * Draws the bars
  530. */
  531. this.Drawbars = function ()
  532. {
  533. // Variable "caching" so the context can be accessed as a local variable
  534. var ca = this.canvas;
  535. var co = this.context;
  536. var prop = this.properties;
  537. co.lineWidth = prop['chart.linewidth'];
  538. co.strokeStyle = prop['chart.strokecolor'];
  539. co.fillStyle = prop['chart.colors'][0];
  540. var prevX = 0;
  541. var prevY = 0;
  542. var decimals = prop['chart.scale.decimals'];
  543. /**
  544. * Work out the max value
  545. */
  546. if (prop['chart.ymax']) {
  547. this.scale2 = RGraph.getScale2(this, {
  548. 'max':prop['chart.ymax'],
  549. 'strict': true,
  550. 'min':prop['chart.ymin'],
  551. 'scale.thousand':prop['chart.scale.thousand'],
  552. 'scale.point':prop['chart.scale.point'],
  553. 'scale.decimals':prop['chart.scale.decimals'],
  554. 'ylabels.count':prop['chart.ylabels.count'],
  555. 'scale.round':prop['chart.scale.round'],
  556. 'units.pre': prop['chart.units.pre'],
  557. 'units.post': prop['chart.units.post']
  558. });
  559. } else {
  560. for (i=0; i<this.data.length; ++i) {
  561. if (typeof(this.data[i]) == 'object') {
  562. var value = prop['chart.grouping'] == 'grouped' ? Number(RGraph.array_max(this.data[i], true)) : Number(RGraph.array_sum(this.data[i])) ;
  563. } else {
  564. var value = Number(this.data[i]);
  565. }
  566. this.max = Math.max(Math.abs(this.max), Math.abs(value));
  567. }
  568. this.scale2 = RGraph.getScale2(this, {
  569. 'max':this.max,
  570. 'min':prop['chart.ymin'],
  571. 'scale.thousand':prop['chart.scale.thousand'],
  572. 'scale.point':prop['chart.scale.point'],
  573. 'scale.decimals':prop['chart.scale.decimals'],
  574. 'ylabels.count':prop['chart.ylabels.count'],
  575. 'scale.round':prop['chart.scale.round'],
  576. 'units.pre': prop['chart.units.pre'],
  577. 'units.post': prop['chart.units.post']
  578. });
  579. this.max = this.scale2.max;
  580. }
  581. /**
  582. * if the chart is adjustable fix the scale so that it doesn't change.
  583. */
  584. if (prop['chart.adjustable'] && !prop['chart.ymax']) {
  585. this.Set('chart.ymax', this.scale2.max);
  586. }
  587. /**
  588. * Draw horizontal bars here
  589. */
  590. if (prop['chart.background.hbars'] && prop['chart.background.hbars'].length > 0) {
  591. RGraph.DrawBars(this);
  592. }
  593. var variant = prop['chart.variant'];
  594. /**
  595. * Draw the 3D axes is necessary
  596. */
  597. if (variant == '3d') {
  598. RG.Draw3DAxes(this);
  599. }
  600. /**
  601. * Get the variant once, and draw the bars, be they regular, stacked or grouped
  602. */
  603. // Get these variables outside of the loop
  604. var xaxispos = prop['chart.xaxispos'];
  605. var width = (ca.width - this.gutterLeft - this.gutterRight ) / this.data.length;
  606. var orig_height = height;
  607. var hmargin = prop['chart.hmargin'];
  608. var shadow = prop['chart.shadow'];
  609. var shadowColor = prop['chart.shadow.color'];
  610. var shadowBlur = prop['chart.shadow.blur'];
  611. var shadowOffsetX = prop['chart.shadow.offsetx'];
  612. var shadowOffsetY = prop['chart.shadow.offsety'];
  613. var strokeStyle = prop['chart.strokecolor'];
  614. var colors = prop['chart.colors'];
  615. var sequentialColorIndex = 0;
  616. for (i=0,len=this.data.length; i<len; i+=1) {
  617. // Work out the height
  618. //The width is up outside the loop
  619. var height = ((RGraph.array_sum(this.data[i]) < 0 ? RGraph.array_sum(this.data[i]) + this.scale2.min : RGraph.array_sum(this.data[i]) - this.scale2.min) / (this.scale2.max - this.scale2.min) ) * (ca.height - this.gutterTop - this.gutterBottom);
  620. // Half the height if the Y axis is at the center
  621. if (xaxispos == 'center') {
  622. height /= 2;
  623. }
  624. var x = (i * width) + this.gutterLeft;
  625. var y = xaxispos == 'center' ? ((ca.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop - height
  626. : ca.height - height - this.gutterBottom;
  627. // xaxispos is top
  628. if (xaxispos == 'top') {
  629. y = this.gutterTop + Math.abs(height);
  630. }
  631. // Account for negative lengths - Some browsers (eg Chrome) don't like a negative value
  632. if (height < 0) {
  633. y += height;
  634. height = Math.abs(height);
  635. }
  636. /**
  637. * Turn on the shadow if need be
  638. */
  639. if (shadow) {
  640. co.shadowColor = shadowColor;
  641. co.shadowBlur = shadowBlur;
  642. co.shadowOffsetX = shadowOffsetX;
  643. co.shadowOffsetY = shadowOffsetY;
  644. }
  645. /**
  646. * Draw the bar
  647. */
  648. co.beginPath();
  649. if (typeof(this.data[i]) == 'number') {
  650. var barWidth = width - (2 * hmargin);
  651. /**
  652. * Check for a negative bar width
  653. */
  654. if (barWidth < 0) {
  655. alert('[RGRAPH] Warning: you have a negative bar width. This may be caused by the chart.hmargin being too high or the width of the canvas not being sufficient.');
  656. }
  657. // Set the fill color
  658. co.strokeStyle = strokeStyle;
  659. co.fillStyle = colors[0];
  660. /**
  661. * Sequential colors
  662. */
  663. if (prop['chart.colors.sequential']) {
  664. co.fillStyle = colors[i];
  665. }
  666. if (variant == 'sketch') {
  667. co.lineCap = 'round';
  668. var sketchOffset = 3;
  669. co.beginPath();
  670. co.strokeStyle = colors[0];
  671. /**
  672. * Sequential colors
  673. */
  674. if (prop['chart.colors.sequential']) {
  675. co.strokeStyle = colors[i];
  676. }
  677. // Left side
  678. co.moveTo(x + hmargin + 2, y + height - 2);
  679. co.lineTo(x + hmargin - 1, y - 4);
  680. // The top
  681. co.moveTo(x + hmargin - 3, y + -2 + (this.data[i] < 0 ? height : 0));
  682. co.bezierCurveTo(
  683. x + ((hmargin + width) * 0.33),
  684. y + 15 + (this.data[i] < 0 ? height - 10: 0),
  685. x + ((hmargin + width) * 0.66),
  686. y + 5 + (this.data[i] < 0 ? height - 10 : 0),x + hmargin + width + -1, y + 0 + (this.data[i] < 0 ? height : 0)
  687. );
  688. // The right side
  689. co.moveTo(x + hmargin + width - 5, y - 5);
  690. co.lineTo(x + hmargin + width - 3, y + height - 3);
  691. if (prop['chart.variant.sketch.verticals']) {
  692. for (var r=0.2; r<=0.8; r+=0.2) {
  693. co.moveTo(x + hmargin + width + (r > 0.4 ? -1 : 3) - (r * width),y - 1);
  694. co.lineTo(x + hmargin + width - (r > 0.4 ? 1 : -1) - (r * width), y + height + (r == 0.2 ? 1 : -2));
  695. }
  696. }
  697. co.stroke();
  698. // Regular bar
  699. } else if (variant == 'bar' || variant == '3d' || variant == 'glass' || variant == 'bevel') {
  700. if (RGraph.isOld() && shadow) {
  701. this.DrawIEShadow([x + hmargin, y, barWidth, height]);
  702. }
  703. if (variant == 'glass') {
  704. RGraph.filledCurvyRect(co, x + hmargin, y, barWidth, height, 3, this.data[i] > 0, this.data[i] > 0, this.data[i] < 0, this.data[i] < 0);
  705. RGraph.strokedCurvyRect(co, x + hmargin, y, barWidth, height, 3, this.data[i] > 0, this.data[i] > 0, this.data[i] < 0, this.data[i] < 0);
  706. } else {
  707. // On 9th April 2013 these two were swapped around so that the stroke happens SECOND so that any
  708. // shadow that is cast by the fill does not overwrite the stroke
  709. co.beginPath();
  710. co.rect(x + hmargin, y, barWidth, height);
  711. co.fill();
  712. // Turn the shadow off so that the stroke doesn't cast any "extra" shadow
  713. // that would show inside the bar
  714. RG.NoShadow(this);
  715. co.beginPath();
  716. co.rect(x + hmargin, y, barWidth, height);
  717. co.stroke();
  718. }
  719. // 3D effect
  720. if (variant == '3d') {
  721. var prevStrokeStyle = co.strokeStyle;
  722. var prevFillStyle = co.fillStyle;
  723. // Draw the top
  724. co.beginPath();
  725. co.moveTo(x + hmargin, y);
  726. co.lineTo(x + hmargin + 10, y - 5);
  727. co.lineTo(x + hmargin + 10 + barWidth, y - 5);
  728. co.lineTo(x + hmargin + barWidth, y);
  729. co.closePath();
  730. co.stroke();
  731. co.fill();
  732. // Draw the right hand side
  733. co.beginPath();
  734. co.moveTo(x + hmargin + barWidth, y);
  735. co.lineTo(x + hmargin + barWidth + 10, y - 5);
  736. co.lineTo(x + hmargin + barWidth + 10, y + height - 5);
  737. co.lineTo(x + hmargin + barWidth, y + height);
  738. co.closePath();
  739. co.stroke();
  740. co.fill();
  741. // Draw the darker top section
  742. co.beginPath();
  743. co.fillStyle = 'rgba(255,255,255,0.3)';
  744. co.moveTo(x + hmargin, y);
  745. co.lineTo(x + hmargin + 10, y - 5);
  746. co.lineTo(x + hmargin + 10 + barWidth, y - 5);
  747. co.lineTo(x + hmargin + barWidth, y);
  748. co.lineTo(x + hmargin, y);
  749. co.closePath();
  750. co.stroke();
  751. co.fill();
  752. // Draw the darker right side section
  753. co.beginPath();
  754. co.fillStyle = 'rgba(0,0,0,0.4)';
  755. co.moveTo(x + hmargin + barWidth, y);
  756. co.lineTo(x + hmargin + barWidth + 10, y - 5);
  757. co.lineTo(x + hmargin + barWidth + 10, y - 5 + height);
  758. co.lineTo(x + hmargin + barWidth, y + height);
  759. co.lineTo(x + hmargin + barWidth, y);
  760. co.closePath();
  761. co.stroke();
  762. co.fill();
  763. co.strokeStyle = prevStrokeStyle;
  764. co.fillStyle = prevFillStyle;
  765. // Glass variant
  766. } else if (variant == 'glass') {
  767. var grad = co.createLinearGradient(x + hmargin,y,x + hmargin + (barWidth / 2),y);
  768. grad.addColorStop(0, 'rgba(255,255,255,0.9)');
  769. grad.addColorStop(1, 'rgba(255,255,255,0.5)');
  770. co.beginPath();
  771. co.fillStyle = grad;
  772. co.fillRect(x + hmargin + 2,y + (this.data[i] > 0 ? 2 : 0),(barWidth / 2) - 2,height - 2);
  773. co.fill();
  774. }
  775. // This bit draws the text labels that appear above the bars if requested
  776. if (prop['chart.labels.above']) {
  777. // Turn off any shadow
  778. if (shadow) {
  779. RGraph.NoShadow(this);
  780. }
  781. var yPos = y - 3;
  782. var xPos = x + hmargin + (barWidth / 2);
  783. // Account for negative bars
  784. if (this.data[i] < 0) {
  785. yPos += height + 6 + (prop['chart.text.size']);
  786. }
  787. // Account for chart.xaxispos=top
  788. if (prop['chart.xaxispos'] == 'top') {
  789. yPos = this.gutterTop + height + 6 + (typeof(prop['chart.labels.above.size']) == 'number' ? prop['chart.labels.above.size'] : prop['chart.text.size'] - 4);
  790. }
  791. // Account for chart.variant=3d
  792. if (prop['chart.variant'] == '3d') {
  793. yPos -= 3;
  794. xPos += 5;
  795. }
  796. // Angled above labels
  797. if (this.properties['chart.labels.above.angle']) {
  798. var angle = -45;
  799. var halign = 'left';
  800. var valign = 'center';
  801. } else {
  802. var angle = 0;
  803. var halign = 'center';
  804. var valign = 'bottom';
  805. }
  806. // Above labels color
  807. if (typeof this.properties['chart.labels.above.color'] == 'string') {
  808. co.fillStyle = prop['chart.labels.above.color'];
  809. } else {
  810. co.fillStyle = prop['chart.text.color'];
  811. }
  812. RGraph.Text2(this, {'font': prop['chart.text.font'],
  813. 'size': typeof(prop['chart.labels.above.size']) == 'number' ? prop['chart.labels.above.size'] : prop['chart.text.size'] - 3,
  814. 'x': xPos,
  815. 'y': yPos,
  816. 'text': RGraph.number_format(this, Number(this.data[i]).toFixed(prop['chart.labels.above.decimals']),prop['chart.units.pre'],prop['chart.units.post']),
  817. 'halign': halign,
  818. 'marker': false,
  819. 'valign': valign,
  820. 'angle': angle,
  821. 'tag': 'labels.above'
  822. });
  823. }
  824. // Dot chart
  825. } else if (variant == 'dot') {
  826. co.beginPath();
  827. co.moveTo(x + (width / 2), y);
  828. co.lineTo(x + (width / 2), y + height);
  829. co.stroke();
  830. co.beginPath();
  831. co.fillStyle = this.properties['chart.colors'][i];
  832. co.arc(x + (width / 2), y + (this.data[i] > 0 ? 0 : height), 2, 0, 6.28, 0);
  833. // Set the colour for the dots
  834. co.fillStyle = prop['chart.colors'][0];
  835. /**
  836. * Sequential colors
  837. */
  838. if (prop['chart.colors.sequential']) {
  839. co.fillStyle = colors[i];
  840. }
  841. co.stroke();
  842. co.fill();
  843. // Unknown variant type
  844. } else {
  845. alert('[BAR] Warning! Unknown chart.variant: ' + variant);
  846. }
  847. this.coords.push([x + hmargin, y, width - (2 * hmargin), height]);
  848. if (typeof this.coords2[i] == 'undefined') {
  849. this.coords2[i] = [];
  850. }
  851. this.coords2[i].push([x + hmargin, y, width - (2 * hmargin), height]);
  852. /**
  853. * Stacked bar
  854. */
  855. } else if (this.data[i] && typeof(this.data[i]) == 'object' && prop['chart.grouping'] == 'stacked') {
  856. if (this.scale2.min) {
  857. alert("[ERROR] Stacked Bar charts with a Y min are not supported");
  858. }
  859. var barWidth = width - (2 * hmargin);
  860. var redrawCoords = [];// Necessary to draw if the shadow is enabled
  861. var startY = 0;
  862. var dataset = this.data[i];
  863. /**
  864. * Check for a negative bar width
  865. */
  866. if (barWidth < 0) {
  867. alert('[RGRAPH] Warning: you have a negative bar width. This may be caused by the chart.hmargin being too high or the width of the canvas not being sufficient.');
  868. }
  869. for (j=0; j<dataset.length; ++j) {
  870. // Stacked bar chart and X axis pos in the middle - poitless since negative values are not permitted
  871. if (xaxispos == 'center') {
  872. alert("[BAR] It's pointless having the X axis position at the center on a stacked bar chart.");
  873. return;
  874. }
  875. // Negative values not permitted for the stacked chart
  876. if (this.data[i][j] < 0) {
  877. alert('[BAR] Negative values are not permitted with a stacked bar chart. Try a grouped one instead.');
  878. return;
  879. }
  880. /**
  881. * Set the fill and stroke colors
  882. */
  883. co.strokeStyle = strokeStyle
  884. co.fillStyle = colors[j];
  885. if (prop['chart.colors.reverse']) {
  886. co.fillStyle = colors[this.data[i].length - j - 1];
  887. }
  888. if (prop['chart.colors.sequential'] && colors[sequentialColorIndex]) {
  889. co.fillStyle = colors[sequentialColorIndex++];
  890. } else if (prop['chart.colors.sequential']) {
  891. co.fillStyle = colors[sequentialColorIndex - 1];
  892. }
  893. var height = (dataset[j] / this.scale2.max) * (ca.height - this.gutterTop - this.gutterBottom );
  894. // If the X axis pos is in the center, we need to half the height
  895. if (xaxispos == 'center') {
  896. height /= 2;
  897. }
  898. var totalHeight = (RGraph.array_sum(dataset) / this.scale2.max) * (ca.height - hmargin - this.gutterTop - this.gutterBottom);
  899. /**
  900. * Store the coords for tooltips
  901. */
  902. this.coords.push([x + hmargin, y, width - (2 * hmargin), height]);
  903. if (typeof this.coords2[i] == 'undefined') {
  904. this.coords2[i] = [];
  905. }
  906. this.coords2[i].push([x + hmargin, y, width - (2 * hmargin), height]);
  907. // MSIE shadow
  908. if (RGraph.isOld() && shadow) {
  909. this.DrawIEShadow([x + hmargin, y, width - (2 * hmargin), height + 1]);
  910. }
  911. if (height > 0) {
  912. co.strokeRect(x + hmargin, y, width - (2 * hmargin), height);
  913. co.fillRect(x + hmargin, y, width - (2 * hmargin), height);
  914. }
  915. if (j == 0) {
  916. var startY = y;
  917. var startX = x;
  918. }
  919. /**
  920. * Store the redraw coords if the shadow is enabled
  921. */
  922. if (shadow) {
  923. redrawCoords.push([x + hmargin, y, width - (2 * hmargin), height, co.fillStyle]);
  924. }
  925. /**
  926. * Stacked 3D effect
  927. */
  928. if (variant == '3d') {
  929. var prevFillStyle = co.fillStyle;
  930. var prevStrokeStyle = co.strokeStyle;
  931. // Draw the top side
  932. if (j == 0) {
  933. co.beginPath();
  934. co.moveTo(startX + hmargin, y);
  935. co.lineTo(startX + 10 + hmargin, y - 5);
  936. co.lineTo(startX + 10 + barWidth + hmargin, y - 5);
  937. co.lineTo(startX + barWidth + hmargin, y);
  938. co.closePath();
  939. co.fill();
  940. co.stroke();
  941. }
  942. // Draw the side section
  943. co.beginPath();
  944. co.moveTo(startX + barWidth + hmargin, y);
  945. co.lineTo(startX + barWidth + hmargin + 10, y - 5);
  946. co.lineTo(startX + barWidth + hmargin + 10, y - 5 + height);
  947. co.lineTo(startX + barWidth + hmargin , y + height);
  948. co.closePath();
  949. co.fill();
  950. co.stroke();
  951. // Draw the darker top side
  952. if (j == 0) {
  953. co.fillStyle = 'rgba(255,255,255,0.3)';
  954. co.beginPath();
  955. co.moveTo(startX + hmargin, y);
  956. co.lineTo(startX + 10 + hmargin, y - 5);
  957. co.lineTo(startX + 10 + barWidth + hmargin, y - 5);
  958. co.lineTo(startX + barWidth + hmargin, y);
  959. co.closePath();
  960. co.fill();
  961. co.stroke();
  962. }
  963. // Draw the darker side section
  964. co.fillStyle = 'rgba(0,0,0,0.4)';
  965. co.beginPath();
  966. co.moveTo(startX + barWidth + hmargin, y);
  967. co.lineTo(startX + barWidth + hmargin + 10, y - 5);
  968. co.lineTo(startX + barWidth + hmargin + 10, y - 5 + height);
  969. co.lineTo(startX + barWidth + hmargin , y + height);
  970. co.closePath();
  971. co.fill();
  972. co.stroke();
  973. co.strokeStyle = prevStrokeStyle;
  974. co.fillStyle = prevFillStyle;
  975. }
  976. y += height;
  977. }
  978. // This bit draws the text labels that appear above the bars if requested
  979. if (prop['chart.labels.above']) {
  980. // Turn off any shadow
  981. RG.NoShadow(this);
  982. // Above labels color
  983. if (typeof this.properties['chart.labels.above.color'] == 'string') {
  984. co.fillStyle = prop['chart.labels.above.color'];
  985. } else {
  986. co.fillStyle = prop['chart.text.color'];
  987. }
  988. // Angled above labels
  989. if (prop['chart.labels.above.angle']) {
  990. var angle = -45;
  991. var halign = 'left';
  992. var valign = 'center';
  993. } else {
  994. var angle = 0;
  995. var halign = 'center';
  996. var valign = 'bottom';
  997. }
  998. RGraph.Text2(this,{'font': prop['chart.text.font'],
  999. 'size': typeof(prop['chart.labels.above.size']) == 'number' ? prop['chart.labels.above.size'] : prop['chart.text.size'] - 3,
  1000. 'x': startX + (barWidth / 2) + prop['chart.hmargin'],
  1001. 'y': startY - (prop['chart.shadow'] && prop['chart.shadow.offsety'] < 0 ? 7 : 4) - (prop['chart.variant'] == '3d' ? 5 : 0),
  1002. 'text': String(prop['chart.units.pre'] + RGraph.array_sum(this.data[i]).toFixed(prop['chart.labels.above.decimals']) + prop['chart.units.post']),
  1003. 'angle': angle,
  1004. 'valign': valign,
  1005. 'halign': halign,
  1006. 'tag': 'labels.above'
  1007. });
  1008. // Turn any shadow back on
  1009. if (shadow) {
  1010. co.shadowColor = shadowColor;
  1011. co.shadowBlur = shadowBlur;
  1012. co.shadowOffsetX = shadowOffsetX;
  1013. co.shadowOffsetY = shadowOffsetY;
  1014. }
  1015. }
  1016. /**
  1017. * Redraw the bars if the shadow is enabled due to hem being drawn from the bottom up, and the
  1018. * shadow spilling over to higher up bars
  1019. */
  1020. if (shadow) {
  1021. RGraph.NoShadow(this);
  1022. for (k=0; k<redrawCoords.length; ++k) {
  1023. co.strokeStyle = strokeStyle;
  1024. co.fillStyle = redrawCoords[k][4];
  1025. co.strokeRect(redrawCoords[k][0], redrawCoords[k][1], redrawCoords[k][2], redrawCoords[k][3]);
  1026. co.fillRect(redrawCoords[k][0], redrawCoords[k][1], redrawCoords[k][2], redrawCoords[k][3]);
  1027. co.stroke();
  1028. co.fill();
  1029. }
  1030. // Reset the redraw coords to be empty
  1031. redrawCoords = [];
  1032. }
  1033. /**
  1034. * Grouped bar
  1035. */
  1036. } else if (this.data[i] && typeof(this.data[i]) == 'object' && prop['chart.grouping'] == 'grouped') {
  1037. var redrawCoords = [];
  1038. co.lineWidth = prop['chart.linewidth'];
  1039. for (j=0; j<this.data[i].length; ++j) {
  1040. // Set the fill and stroke colors
  1041. co.strokeStyle = strokeStyle;
  1042. co.fillStyle = colors[j];
  1043. /**
  1044. * Sequential colors
  1045. */
  1046. if (prop['chart.colors.sequential'] && colors[sequentialColorIndex]) {
  1047. co.fillStyle = colors[sequentialColorIndex++];
  1048. } else if (prop['chart.colors.sequential']) {
  1049. co.fillStyle = colors[sequentialColorIndex - 1];
  1050. }
  1051. var individualBarWidth = (width - (2 * hmargin)) / this.data[i].length;
  1052. var height = ((this.data[i][j] + (this.data[i][j] < 0 ? this.scale2.min : (-1 * this.scale2.min) )) / (this.scale2.max - this.scale2.min) ) * (ca.height - this.gutterTop - this.gutterBottom );
  1053. var groupedMargin = prop['chart.hmargin.grouped'];
  1054. var startX = x + hmargin + (j * individualBarWidth);
  1055. /**
  1056. * Check for a negative bar width
  1057. */
  1058. if (individualBarWidth < 0) {
  1059. alert('[RGRAPH] Warning: you have a negative bar width. This may be caused by the chart.hmargin being too high or the width of the canvas not being sufficient.');
  1060. }
  1061. // If the X axis pos is in the center, we need to half the height
  1062. if (xaxispos == 'center') {
  1063. height /= 2;
  1064. }
  1065. /**
  1066. * Determine the start positioning for the bar
  1067. */
  1068. if (xaxispos == 'top') {
  1069. var startY = this.gutterTop;
  1070. var height = Math.abs(height);
  1071. } else if (xaxispos == 'center') {
  1072. var startY = this.gutterTop + (this.grapharea / 2) - height;
  1073. } else {
  1074. var startY = ca.height - this.gutterBottom - height;
  1075. var height = Math.abs(height);
  1076. }
  1077. /**
  1078. * Draw MSIE shadow
  1079. */
  1080. if (RGraph.isOld() && shadow) {
  1081. this.DrawIEShadow([startX, startY, individualBarWidth, height]);
  1082. }
  1083. co.strokeRect(startX + groupedMargin, startY, individualBarWidth - (2 * groupedMargin), height);
  1084. co.fillRect(startX + groupedMargin, startY, individualBarWidth - (2 * groupedMargin), height);
  1085. y += height;
  1086. /**
  1087. * Grouped 3D effect
  1088. */
  1089. if (variant == '3d') {
  1090. var prevFillStyle = co.fillStyle;
  1091. var prevStrokeStyle = co.strokeStyle;
  1092. // Draw the top side
  1093. co.beginPath();
  1094. co.moveTo(startX, startY);
  1095. co.lineTo(startX + 10, startY - 5);
  1096. co.lineTo(startX + 10 + individualBarWidth, startY - 5);
  1097. co.lineTo(startX + individualBarWidth, startY);
  1098. co.closePath();
  1099. co.fill();
  1100. co.stroke();
  1101. // Draw the side section
  1102. co.beginPath();
  1103. co.moveTo(startX + individualBarWidth, startY);
  1104. co.lineTo(startX + individualBarWidth + 10, startY - 5);
  1105. co.lineTo(startX + individualBarWidth + 10, startY - 5 + height);
  1106. co.lineTo(startX + individualBarWidth , startY + height);
  1107. co.closePath();
  1108. co.fill();
  1109. co.stroke();
  1110. // Draw the darker top side
  1111. co.fillStyle = 'rgba(255,255,255,0.3)';
  1112. co.beginPath();
  1113. co.moveTo(startX, startY);
  1114. co.lineTo(startX + 10, startY - 5);
  1115. co.lineTo(startX + 10 + individualBarWidth, startY - 5);
  1116. co.lineTo(startX + individualBarWidth, startY);
  1117. co.closePath();
  1118. co.fill();
  1119. co.stroke();
  1120. // Draw the darker side section
  1121. co.fillStyle = 'rgba(0,0,0,0.4)';
  1122. co.beginPath();
  1123. co.moveTo(startX + individualBarWidth, startY);
  1124. co.lineTo(startX + individualBarWidth + 10, startY - 5);
  1125. co.lineTo(startX + individualBarWidth + 10, startY - 5 + height);
  1126. co.lineTo(startX + individualBarWidth , startY + height);
  1127. co.closePath();
  1128. co.fill();
  1129. co.stroke();
  1130. co.strokeStyle = prevStrokeStyle;
  1131. co.fillStyle = prevFillStyle;
  1132. }
  1133. if (height < 0) {
  1134. height = Math.abs(height);
  1135. startY = startY - height;
  1136. }
  1137. this.coords.push([startX + groupedMargin, startY, individualBarWidth - (2 * groupedMargin), height]);
  1138. if (typeof this.coords2[i] == 'undefined') {
  1139. this.coords2[i] = [];
  1140. }
  1141. this.coords2[i].push([startX + groupedMargin, startY, individualBarWidth - (2 * groupedMargin), height]);
  1142. // Facilitate shadows going to the left
  1143. if (prop['chart.shadow']) {
  1144. redrawCoords.push([startX + groupedMargin, startY, individualBarWidth - (2 * groupedMargin), height, co.fillStyle]);
  1145. }
  1146. // This bit draws the text labels that appear above the bars if requested
  1147. if (prop['chart.labels.above']) {
  1148. co.strokeStyle = 'rgba(0,0,0,0)';
  1149. // Turn off any shadow
  1150. if (shadow) {
  1151. RGraph.NoShadow(this);
  1152. }
  1153. var yPos = y - 3;
  1154. // Angled above labels
  1155. if (prop['chart.labels.above.angle']) {
  1156. var angle = -45;
  1157. var halign = 'left';
  1158. var valign = 'center';
  1159. } else {
  1160. var angle = 0;
  1161. var halign = 'center';
  1162. var valign = 'bottom';
  1163. // Account for negative bars
  1164. if (this.data[i][j] < 0 || prop['chart.xaxispos'] == 'top') {
  1165. yPos = startY + height + 6;
  1166. var valign = 'top';
  1167. } else {
  1168. yPos = startY;
  1169. }
  1170. }
  1171. // Above labels color
  1172. if (typeof this.properties['chart.labels.above.color'] == 'string') {
  1173. co.fillStyle = prop['chart.labels.above.color'];
  1174. } else {
  1175. co.fillStyle = prop['chart.text.color'];
  1176. }
  1177. RGraph.Text2(this, {'font': prop['chart.text.font'],
  1178. 'size': typeof(prop['chart.labels.above.size']) == 'number' ? prop['chart.labels.above.size'] : prop['chart.text.size'] - 3,
  1179. 'x': startX + (individualBarWidth / 2),
  1180. 'y': yPos - 3,
  1181. 'text': RGraph.number_format(this, this.data[i][j].toFixed(prop['chart.labels.above.decimals'])),
  1182. 'halign': halign,
  1183. 'valign': valign,
  1184. 'angle':angle,
  1185. 'tag': 'labels.above'
  1186. });
  1187. // Turn any shadow back on
  1188. if (shadow) {
  1189. co.shadowColor = shadowColor;
  1190. co.shadowBlur = shadowBlur;
  1191. co.shadowOffsetX = shadowOffsetX;
  1192. co.shadowOffsetY = shadowOffsetY;
  1193. }
  1194. }
  1195. }
  1196. /**
  1197. * Redraw the bar if shadows are going to the left
  1198. */
  1199. if (redrawCoords.length) {
  1200. RGraph.NoShadow(this);
  1201. co.lineWidth = prop['chart.linewidth'];
  1202. co.beginPath();
  1203. for (var j=0; j<redrawCoords.length; ++j) {
  1204. co.fillStyle = redrawCoords[j][4];
  1205. co.strokeStyle = prop['chart.strokecolor'];
  1206. co.fillRect(redrawCoords[j][0], redrawCoords[j][1], redrawCoords[j][2], redrawCoords[j][3]);
  1207. co.strokeRect(redrawCoords[j][0], redrawCoords[j][1], redrawCoords[j][2], redrawCoords[j][3]);
  1208. }
  1209. co.fill();
  1210. co.stroke();
  1211. redrawCoords = [];
  1212. }
  1213. } else {
  1214. this.coords.push([]);
  1215. }
  1216. co.closePath();
  1217. }
  1218. /**
  1219. * Turn off any shadow
  1220. */
  1221. RGraph.NoShadow(this);
  1222. }
  1223. /**
  1224. * Draws the labels for the graph
  1225. */
  1226. this.DrawLabels = function ()
  1227. {
  1228. // Variable "caching" so the context can be accessed as a local variable
  1229. var ca = this.canvas;
  1230. var co = this.context;
  1231. var prop = this.properties;
  1232. var context = co;
  1233. var text_angle = prop['chart.text.angle'];
  1234. var text_size = prop['chart.text.size'];
  1235. var labels = prop['chart.labels'];
  1236. // Draw the Y axis labels:
  1237. if (prop['chart.ylabels']) {
  1238. if (prop['chart.xaxispos'] == 'top') this.Drawlabels_top();
  1239. if (prop['chart.xaxispos'] == 'center') this.Drawlabels_center();
  1240. if (prop['chart.xaxispos'] == 'bottom') this.Drawlabels_bottom();
  1241. }
  1242. /**
  1243. * The X axis labels
  1244. */
  1245. if (typeof(labels) == 'object' && labels) {
  1246. var yOffset = Number(prop['chart.xlabels.offset']);
  1247. /**
  1248. * Text angle
  1249. */
  1250. if (prop['chart.text.angle'] != 0) {
  1251. var valign = 'center';
  1252. var halign = 'right';
  1253. var angle = 0 - prop['chart.text.angle'];
  1254. } else {
  1255. var valign = 'top';
  1256. var halign = 'center';
  1257. var angle = 0;
  1258. }
  1259. // Draw the X axis labels
  1260. co.fillStyle = prop['chart.text.color'];
  1261. // How wide is each bar
  1262. var barWidth = (ca.width - this.gutterRight - this.gutterLeft) / labels.length;
  1263. // Reset the xTickGap
  1264. xTickGap = (ca.width - this.gutterRight - this.gutterLeft) / labels.length
  1265. // Draw the X tickmarks
  1266. var i=0;
  1267. var font = prop['chart.text.font'];
  1268. for (x=this.gutterLeft + (xTickGap / 2); x<=ca.width - this.gutterRight; x+=xTickGap) {
  1269. RGraph.Text2(this, {'font': font,
  1270. 'size': text_size,
  1271. 'x': x,
  1272. 'y': prop['chart.xaxispos'] == 'top' ? this.gutterTop - yOffset - 5: (ca.height - this.gutterBottom) + yOffset + 3,
  1273. 'text': String(labels[i++]),
  1274. 'valign': prop['chart.xaxispos'] == 'top' ? 'bottom' : valign,
  1275. 'halign': halign,
  1276. 'tag':'label',
  1277. 'marker':false,
  1278. 'angle':angle,
  1279. 'tag': 'labels'
  1280. });
  1281. }
  1282. }
  1283. /**
  1284. * If chart.labels.above.specific is specified, draw them
  1285. */
  1286. if (prop['chart.labels.above.specific']) {
  1287. var labels = prop['chart.labels.above.specific'];
  1288. for (var i=0; i<this.coords.length; ++i) {
  1289. var xaxispos = prop['chart.xaxispos'];
  1290. var coords = this.coords[i];
  1291. var value = this.data_arr[i];
  1292. var valign = (value >=0 && xaxispos != 'top') ? 'bottom' : 'top';
  1293. var halign = 'center';
  1294. var text = labels[i];
  1295. if (text && text.toString().length > 0) {
  1296. RGraph.Text2(this, {'font': prop['chart.text.font'],
  1297. 'size': prop['chart.labels.above.size'] ? prop['chart.labels.above.size'] : prop['chart.text.size'],
  1298. 'x': coords[0] + (coords[2] / 2),
  1299. 'y': (value >=0 && xaxispos != 'top') ? coords[1] - 5 : coords[1] + coords[3] + 3,
  1300. 'text': String(labels[i]),
  1301. 'valign': valign,
  1302. 'halign': halign,
  1303. 'tag': 'labels.above'
  1304. });
  1305. }
  1306. }
  1307. }
  1308. }
  1309. /**
  1310. * Draws the X axis at the top
  1311. */
  1312. this.Drawlabels_top = function ()
  1313. {
  1314. var ca = this.canvas;
  1315. var co = this.context;
  1316. var prop = this.properties;
  1317. co.beginPath();
  1318. co.fillStyle = prop['chart.text.color'];
  1319. co.strokeStyle = 'black';
  1320. if (prop['chart.xaxispos'] == 'top') {
  1321. var context = co;
  1322. var text_size = prop['chart.text.size'];
  1323. var units_pre = prop['chart.units.pre'];
  1324. var units_post = prop['chart.units.post'];
  1325. var align = prop['chart.yaxispos'] == 'left' ? 'right' : 'left';
  1326. var font = prop['chart.text.font'];
  1327. var numYLabels = prop['chart.ylabels.count'];
  1328. var ymin = prop['chart.ymin'];
  1329. if (prop['chart.ylabels.inside'] == true) {
  1330. var xpos = prop['chart.yaxispos'] == 'left' ? this.gutterLeft + 5 : ca.width - this.gutterRight - 5;
  1331. var align = prop['chart.yaxispos'] == 'left' ? 'left' : 'right';
  1332. var boxed = true;
  1333. } else {
  1334. var xpos = prop['chart.yaxispos'] == 'left' ? this.gutterLeft - 5 : ca.width - this.gutterRight + 5;
  1335. var boxed = false;
  1336. }
  1337. /**
  1338. * Draw specific Y labels here so that the local variables can be reused
  1339. */
  1340. if (typeof(prop['chart.ylabels.specific']) == 'object' && prop['chart.ylabels.specific']) {
  1341. var labels = RGraph.array_reverse(prop['chart.ylabels.specific']);
  1342. var grapharea = ca.height - this.gutterTop - this.gutterBottom;
  1343. for (var i=0; i<labels.length; ++i) {
  1344. var y = this.gutterTop + (grapharea * (i / labels.length)) + (grapharea / labels.length);
  1345. RGraph.Text2(this, {'font': font,
  1346. 'size': text_size,
  1347. 'x': xpos,
  1348. 'y': y,
  1349. 'text': String(labels[i]),
  1350. 'valign': 'center',
  1351. 'halign': align,
  1352. 'bordered':boxed,
  1353. 'tag': 'scale'
  1354. });
  1355. }
  1356. return;
  1357. }
  1358. /**
  1359. * Draw the scale
  1360. */
  1361. var labels = this.scale2.labels;
  1362. for (var i=0; i<labels.length; ++i) {
  1363. RGraph.Text2(this, {'font': font,
  1364. 'size':text_size,
  1365. 'x':xpos,
  1366. 'y':this.gutterTop + ((this.grapharea / labels.length) * (i + 1)),
  1367. 'text': '-' + labels[i],
  1368. 'valign': 'center',
  1369. 'halign': align,
  1370. 'bordered': boxed,
  1371. 'tag':'scale'});
  1372. }
  1373. /**
  1374. * Show the minimum value if its not zero
  1375. */
  1376. if (prop['chart.ymin'] != 0 || prop['chart.noxaxis']) {
  1377. RGraph.Text2(this, {'font': font,
  1378. 'size': text_size,
  1379. 'x': xpos,
  1380. 'y': this.gutterTop,
  1381. 'text': (this.scale2.min != 0 ? '-' : '') + RGraph.number_format(this,(this.scale2.min.toFixed((prop['chart.scale.decimals']))), units_pre, units_post),
  1382. 'valign': 'center',
  1383. 'halign': align,
  1384. 'bordered': boxed,
  1385. 'tag': 'scale'});
  1386. }
  1387. }
  1388. co.fill();
  1389. }
  1390. /**
  1391. * Draws the X axis in the middle
  1392. */
  1393. this.Drawlabels_center = function ()
  1394. {
  1395. var ca = this.canvas;
  1396. var co = this.context;
  1397. var prop = this.properties;
  1398. var font = prop['chart.text.font'];
  1399. var numYLabels = prop['chart.ylabels.count'];
  1400. co.fillStyle = prop['chart.text.color'];
  1401. if (prop['chart.xaxispos'] == 'center') {
  1402. /**
  1403. * Draw the top labels
  1404. */
  1405. var text_size = prop['chart.text.size'];
  1406. var units_pre = prop['chart.units.pre'];
  1407. var units_post = prop['chart.units.post'];
  1408. var context = co;
  1409. var align = '';
  1410. var xpos = 0;
  1411. var boxed = false;
  1412. var ymin = prop['chart.ymin'];
  1413. co.fillStyle = prop['chart.text.color'];
  1414. co.strokeStyle = 'black';
  1415. if (prop['chart.ylabels.inside'] == true) {
  1416. var xpos = prop['chart.yaxispos'] == 'left' ? this.gutterLeft + 5 : ca.width - this.gutterRight - 5;
  1417. var align = prop['chart.yaxispos'] == 'left' ? 'left' : 'right';
  1418. var boxed = true;
  1419. } else {
  1420. var xpos = prop['chart.yaxispos'] == 'left' ? this.gutterLeft - 5 : ca.width - this.gutterRight + 5;
  1421. var align = prop['chart.yaxispos'] == 'left' ? 'right' : 'left';
  1422. var boxed = false;
  1423. }
  1424. /**
  1425. * Draw specific Y labels here so that the local variables can be reused
  1426. */
  1427. if (typeof(prop['chart.ylabels.specific']) == 'object' && prop['chart.ylabels.specific']) {
  1428. var labels = prop['chart.ylabels.specific'];
  1429. var grapharea = ca.height - this.gutterTop - this.gutterBottom;
  1430. // Draw the top halves labels
  1431. for (var i=0; i<labels.length; ++i) {
  1432. var y = this.gutterTop + ((grapharea / 2) / (labels.length - 1)) * i;
  1433. RGraph.Text2(this, {'font':font,
  1434. 'size':text_size,
  1435. 'x':xpos,
  1436. 'y':y,
  1437. 'text':String(labels[i]),
  1438. 'valign':'center',
  1439. 'halign':align,
  1440. 'bordered':boxed,
  1441. 'tag': 'scale'
  1442. });
  1443. }
  1444. // Draw the bottom halves labels
  1445. for (var i=labels.length-1; i>=1; --i) {
  1446. var y = this.gutterTop + (grapharea * (i / ((labels.length - 1) * 2) )) + (grapharea / 2);
  1447. RG.Text2(this, {'font':font,
  1448. 'size':text_size,
  1449. 'x':xpos,
  1450. 'y':y,
  1451. 'text':String(labels[labels.length - i - 1]),
  1452. 'valign':'center',
  1453. 'halign':align,
  1454. 'bordered':boxed,
  1455. 'tag': 'scale'
  1456. });
  1457. }
  1458. return;
  1459. }
  1460. /**
  1461. * Draw the top halfs labels
  1462. */
  1463. for (var i=0; i<this.scale2.labels.length; ++i) {
  1464. var y = this.gutterTop + this.halfgrapharea - ((this.halfgrapharea / numYLabels) * (i + 1));
  1465. var text = this.scale2.labels[i];
  1466. RG.Text2(this, {'font':font, 'size':text_size, 'x':xpos, 'y':y, 'text': text, 'valign':'center', 'halign': align, 'bordered': boxed, 'tag':'scale'});
  1467. }
  1468. /**
  1469. * Draw the bottom halfs labels
  1470. */
  1471. for (var i=(this.scale2.labels.length - 1); i>=0; --i) {
  1472. var y = this.gutterTop + ((this.halfgrapharea / numYLabels) * (i + 1)) + this.halfgrapharea;
  1473. var text = this.scale2.labels[i];
  1474. RG.Text2(this, {'font':font, 'size':text_size,'x':xpos,'y':y,'text': '-' + text,'valign':'center','halign': align,'bordered': boxed,'tag':'scale'});
  1475. }
  1476. /**
  1477. * Show the minimum value if its not zero
  1478. */
  1479. if (this.scale2.min != 0) {
  1480. RG.Text2(this, {'font':font,'size':text_size, 'x':xpos, 'y':this.gutterTop + this.halfgrapharea,'text': RGraph.number_format(this,(this.scale2.min.toFixed((prop['chart.scale.decimals']))), units_pre, units_post),'valign':'center', 'valign':'center','halign': align, 'bordered': boxed, 'tag':'scale'});
  1481. }
  1482. }
  1483. }
  1484. /**
  1485. * Draws the X axdis at the bottom (the default)
  1486. */
  1487. this.Drawlabels_bottom = function ()
  1488. {
  1489. var co = this.context;
  1490. var ca = this.canvas;
  1491. var prop = this.properties;
  1492. var text_size = prop['chart.text.size'];
  1493. var units_pre = prop['chart.units.pre'];
  1494. var units_post = prop['chart.units.post'];
  1495. var context = this.context;
  1496. var align = prop['chart.yaxispos'] == 'left' ? 'right' : 'left';
  1497. var font = prop['chart.text.font'];
  1498. var numYLabels = prop['chart.ylabels.count'];
  1499. var ymin = prop['chart.ymin'];
  1500. co.beginPath();
  1501. co.fillStyle = prop['chart.text.color'];
  1502. co.strokeStyle = 'black';
  1503. if (prop['chart.ylabels.inside'] == true) {
  1504. var xpos = prop['chart.yaxispos'] == 'left' ? this.gutterLeft + 5 : ca.width - this.gutterRight - 5;
  1505. var align = prop['chart.yaxispos'] == 'left' ? 'left' : 'right';
  1506. var boxed = true;
  1507. } else {
  1508. var xpos = prop['chart.yaxispos'] == 'left' ? this.gutterLeft - 5 : ca.width - this.gutterRight + 5;
  1509. var boxed = false;
  1510. }
  1511. /**
  1512. * Draw specific Y labels here so that the local variables can be reused
  1513. */
  1514. if (prop['chart.ylabels.specific'] && typeof(prop['chart.ylabels.specific']) == 'object') {
  1515. var labels = prop['chart.ylabels.specific'];
  1516. var grapharea = ca.height - this.gutterTop - this.gutterBottom;
  1517. for (var i=0; i<labels.length; ++i) {
  1518. var y = this.gutterTop + (grapharea * (i / (labels.length - 1)));
  1519. RGraph.Text2(this, {'font':font,
  1520. 'size':text_size,
  1521. 'x':xpos,
  1522. 'y':y,
  1523. 'text': labels[i],
  1524. 'valign':'center',
  1525. 'halign': align,
  1526. 'bordered': boxed,
  1527. 'tag':'scale'
  1528. });
  1529. }
  1530. return;
  1531. }
  1532. var gutterTop = this.gutterTop;
  1533. var halfTextHeight = this.halfTextHeight;
  1534. var scale = this.scale;
  1535. for (var i=0; i<numYLabels; ++i) {
  1536. var text = this.scale2.labels[i];
  1537. RGraph.Text2(this, {'font':font,
  1538. 'size':text_size,
  1539. 'x':xpos,
  1540. 'y':this.gutterTop + this.grapharea - ((this.grapharea / numYLabels) * (i+1)),
  1541. 'text': text,
  1542. 'valign':'center',
  1543. 'halign': align,
  1544. 'bordered': boxed,
  1545. 'tag':'scale'});
  1546. }
  1547. /**
  1548. * Show the minimum value if its not zero
  1549. */
  1550. if (prop['chart.ymin'] != 0 || prop['chart.noxaxis']) {
  1551. RG.Text2(this, {'font':font,
  1552. 'size':text_size,
  1553. 'x':xpos,
  1554. 'y':ca.height - this.gutterBottom,
  1555. 'text': RG.number_format(this,(this.scale2.min.toFixed((prop['chart.scale.decimals']))), units_pre, units_post),
  1556. 'valign':'center',
  1557. 'halign': align,
  1558. 'bordered': boxed,
  1559. 'tag':'scale'});
  1560. }
  1561. co.fill();
  1562. }
  1563. /**
  1564. * This function is used by MSIE only to manually draw the shadow
  1565. *
  1566. * @param array coords The coords for the bar
  1567. */
  1568. this.DrawIEShadow = function (coords)
  1569. {
  1570. var co = this.context;
  1571. var ca = this.canvas;
  1572. var prop = this.properties;
  1573. var prevFillStyle = co.fillStyle;
  1574. var offsetx = prop['chart.shadow.offsetx'];
  1575. var offsety = prop['chart.shadow.offsety'];
  1576. co.lineWidth = prop['chart.linewidth'];
  1577. co.fillStyle = prop['chart.shadow.color'];
  1578. co.beginPath();
  1579. // Draw shadow here
  1580. co.fillRect(coords[0] + offsetx, coords[1] + offsety, coords[2], coords[3]);
  1581. co.fill();
  1582. // Change the fillstyle back to what it was
  1583. co.fillStyle = prevFillStyle;
  1584. }
  1585. /**
  1586. * Not used by the class during creating the graph, but is used by event handlers
  1587. * to get the coordinates (if any) of the selected bar
  1588. *
  1589. * @param object e The event object
  1590. * @param object OPTIONAL You can pass in the bar object instead of the
  1591. * function using "this"
  1592. */
  1593. this.getShape =
  1594. this.getBar = function (e)
  1595. {
  1596. // This facilitates you being able to pass in the bar object as a parameter instead of
  1597. // the function getting it from itself
  1598. var obj = arguments[1] ? arguments[1] : this;
  1599. var mouseXY = RGraph.getMouseXY(e);
  1600. var mouseX = mouseXY[0];
  1601. var mouseY = mouseXY[1];
  1602. var canvas = obj.canvas;
  1603. var context = obj.context;
  1604. var coords = obj.coords
  1605. for (var i=0,len=coords.length; i<len; i+=1) {
  1606. if (obj.coords[i].length == 0) {
  1607. continue;
  1608. }
  1609. var left = coords[i][0];
  1610. var top = coords[i][1];
  1611. var width = coords[i][2];
  1612. var height = coords[i][3];
  1613. var prop = obj.properties;
  1614. if (mouseX >= left && mouseX <= (left + width) && mouseY >= top && mouseY <= (top + height)) {
  1615. if (prop['chart.tooltips']) {
  1616. var tooltip = RGraph.parseTooltipText ? RGraph.parseTooltipText(prop['chart.tooltips'], i) : prop['chart.tooltips'][i];
  1617. }
  1618. // Work out the dataset
  1619. var dataset = 0;
  1620. var idx = i;
  1621. while (idx >= (typeof(obj.data[dataset]) == 'object' && obj.data[dataset] ? obj.data[dataset].length : 1)) {
  1622. if (typeof(obj.data[dataset]) == 'number') {
  1623. idx -= 1;
  1624. } else if (obj.data[dataset]) { // Accounts for null being an object
  1625. idx -= obj.data[dataset].length;
  1626. } else {
  1627. idx -= 1;
  1628. }
  1629. dataset++;
  1630. }
  1631. if (typeof(obj.data[dataset]) == 'number') {
  1632. idx = null;
  1633. }
  1634. return {
  1635. 0: obj, 1: left, 2: top, 3: width, 4: height, 5: i,
  1636. 'object': obj, 'x': left, 'y': top, 'width': width, 'height': height, 'index': i, 'tooltip': tooltip, 'index_adjusted': idx, 'dataset': dataset
  1637. };
  1638. }
  1639. }
  1640. return null;
  1641. }
  1642. /**
  1643. * This retrives the bar based on the X coordinate only.
  1644. *
  1645. * @param object e The event object
  1646. * @param object OPTIONAL You can pass in the bar object instead of the
  1647. * function using "this"
  1648. */
  1649. this.getShapeByX = function (e)
  1650. {
  1651. var canvas = e.target;
  1652. var mouseCoords = RGraph.getMouseXY(e);
  1653. // This facilitates you being able to pass in the bar object as a parameter instead of
  1654. // the function getting it from itself
  1655. var obj = arguments[1] ? arguments[1] : this;
  1656. /**
  1657. * Loop through the bars determining if the mouse is over a bar
  1658. */
  1659. for (var i=0,len=obj.coords.length; i<len; i++) {
  1660. if (obj.coords[i].length == 0) {
  1661. continue;
  1662. }
  1663. var mouseX = mouseCoords[0];
  1664. var mouseY = mouseCoords[1];
  1665. var left = obj.coords[i][0];
  1666. var top = obj.coords[i][1];
  1667. var width = obj.coords[i][2];
  1668. var height = obj.coords[i][3];
  1669. var prop = obj.properties;
  1670. if (mouseX >= left && mouseX <= (left + width)) {
  1671. if (prop['chart.tooltips']) {
  1672. var tooltip = RGraph.parseTooltipText ? RGraph.parseTooltipText(prop['chart.tooltips'], i) : prop['chart.tooltips'][i];
  1673. }
  1674. return {
  1675. 0: obj, 1: left, 2: top, 3: width, 4: height, 5: i,
  1676. 'object': obj, 'x': left, 'y': top, 'width': width, 'height': height, 'index': i, 'tooltip': tooltip
  1677. };
  1678. }
  1679. }
  1680. return null;
  1681. }
  1682. /**
  1683. * When you click on the chart, this method can return the Y value at that point. It works for any point on the
  1684. * chart (that is inside the gutters) - not just points within the Bars.
  1685. *
  1686. * EITHER:
  1687. *
  1688. * @param object arg The event object
  1689. *
  1690. * OR:
  1691. *
  1692. * @param object arg A two element array containing the X and Y coordinates
  1693. */
  1694. this.getValue = function (arg)
  1695. {
  1696. var co = this.context;
  1697. var ca = this.canvas;
  1698. var prop = this.properties;
  1699. if (arg.length == 2) {
  1700. var mouseX = arg[0];
  1701. var mouseY = arg[1];
  1702. } else {
  1703. var mouseCoords = RGraph.getMouseXY(arg);
  1704. var mouseX = mouseCoords[0];
  1705. var mouseY = mouseCoords[1];
  1706. }
  1707. if ( mouseY < prop['chart.gutter.top']
  1708. || mouseY > (ca.height - prop['chart.gutter.bottom'])
  1709. || mouseX < prop['chart.gutter.left']
  1710. || mouseX > (ca.width - prop['chart.gutter.right'])
  1711. ) {
  1712. return null;
  1713. }
  1714. if (prop['chart.xaxispos'] == 'center') {
  1715. var value = (((this.grapharea / 2) - (mouseY - prop['chart.gutter.top'])) / this.grapharea) * (this.scale2.max - this.scale2.min)
  1716. value *= 2;
  1717. if (value >= 0) {
  1718. value += this.scale2.min;
  1719. } else {
  1720. value -= this.scale2.min;
  1721. }
  1722. } else if (prop['chart.xaxispos'] == 'top') {
  1723. var value = ((this.grapharea - (mouseY - prop['chart.gutter.top'])) / this.grapharea) * (this.scale2.max - this.scale2.min)
  1724. value = this.scale2.max - value;
  1725. value = Math.abs(value) * -1;
  1726. } else {
  1727. var value = ((this.grapharea - (mouseY - prop['chart.gutter.top'])) / this.grapharea) * (this.scale2.max - this.scale2.min)
  1728. value += this.scale2.min;
  1729. }
  1730. return value;
  1731. }
  1732. /**
  1733. * This function can be used when the canvas is clicked on (or similar - depending on the event)
  1734. * to retrieve the relevant Y coordinate for a particular value.
  1735. *
  1736. * @param int value The value to get the Y coordinate for
  1737. */
  1738. this.getYCoord = function (value)
  1739. {
  1740. if (value > this.scale2.max) {
  1741. return null;
  1742. }
  1743. var co = this.context;
  1744. var ca = this.canvas;
  1745. var prop = this.properties;
  1746. var y;
  1747. var xaxispos = prop['chart.xaxispos'];
  1748. if (xaxispos == 'top') {
  1749. // Account for negative numbers
  1750. if (value < 0) {
  1751. value = Math.abs(value);
  1752. }
  1753. y = ((value - this.scale2.min) / (this.scale2.max - this.scale2.min)) * this.grapharea;
  1754. y = y + this.gutterTop
  1755. } else if (xaxispos == 'center') {
  1756. y = ((value - this.scale2.min) / (this.scale2.max - this.scale2.min)) * (this.grapharea / 2);
  1757. y = (this.grapharea / 2) - y;
  1758. y += this.gutterTop;
  1759. } else {
  1760. if (value < this.scale2.min) {
  1761. value = this.scale2.min;
  1762. }
  1763. y = ((value - this.scale2.min) / (this.scale2.max - this.scale2.min)) * this.grapharea;
  1764. y = ca.height - this.gutterBottom - y;
  1765. }
  1766. return y;
  1767. }
  1768. /**
  1769. * Each object type has its own Highlight() function which highlights the appropriate shape
  1770. *
  1771. * @param object shape The shape to highlight
  1772. */
  1773. this.Highlight = function (shape)
  1774. {
  1775. // Add the new highlight
  1776. RGraph.Highlight.Rect(this, shape);
  1777. }
  1778. /**
  1779. * The getObjectByXY() worker method
  1780. */
  1781. this.getObjectByXY = function (e)
  1782. {
  1783. var ca = this.canvas;
  1784. var prop = this.properties;
  1785. var mouseXY = RGraph.getMouseXY(e);
  1786. if (
  1787. mouseXY[0] >= prop['chart.gutter.left']
  1788. && mouseXY[0] <= (ca.width - prop['chart.gutter.right'])
  1789. && mouseXY[1] >= prop['chart.gutter.top']
  1790. && mouseXY[1] <= (ca.height - prop['chart.gutter.bottom'])
  1791. ) {
  1792. return this;
  1793. }
  1794. }
  1795. /**
  1796. * This method handles the adjusting calculation for when the mouse is moved
  1797. *
  1798. * @param object e The event object
  1799. */
  1800. this.Adjusting_mousemove = function (e)
  1801. {
  1802. /**
  1803. * Handle adjusting for the Bar
  1804. */
  1805. if (prop['chart.adjustable'] && RG.Registry.Get('chart.adjusting') && RG.Registry.Get('chart.adjusting').uid == this.uid) {
  1806. // Rounding the value to the given number of decimals make the chart step
  1807. var value = Number(this.getValue(e));
  1808. var shape = this.getShapeByX(e);
  1809. if (shape) {
  1810. RG.Registry.Set('chart.adjusting.shape', shape);
  1811. if (this.stackedOrGrouped && prop['chart.grouping'] == 'grouped') {
  1812. var indexes = RG.sequentialIndexToGrouped(shape['index'], this.data);
  1813. if (typeof this.data[indexes[0]] == 'number') {
  1814. this.data[indexes[0]] = Number(value);
  1815. } else if (!RG.is_null(this.data[indexes[0]])) {
  1816. this.data[indexes[0]][indexes[1]] = Number(value);
  1817. }
  1818. } else if (typeof this.data[shape['index']] == 'number') {
  1819. this.data[shape['index']] = Number(value);
  1820. }
  1821. RG.RedrawCanvas(e.target);
  1822. RG.FireCustomEvent(this, 'onadjust');
  1823. }
  1824. }
  1825. }
  1826. /**
  1827. * This function positions a tooltip when it is displayed
  1828. *
  1829. * @param obj object The chart object
  1830. * @param int x The X coordinate specified for the tooltip
  1831. * @param int y The Y coordinate specified for the tooltip
  1832. * @param objec tooltip The tooltips DIV element
  1833. */
  1834. this.positionTooltip = function (obj, x, y, tooltip, idx)
  1835. {
  1836. var prop = obj.properties;
  1837. var coordX = obj.coords[tooltip.__index__][0];
  1838. var coordY = obj.coords[tooltip.__index__][1];
  1839. var coordW = obj.coords[tooltip.__index__][2];
  1840. var coordH = obj.coords[tooltip.__index__][3];
  1841. var canvasXY = RGraph.getCanvasXY(obj.canvas);
  1842. var gutterLeft = prop['chart.gutter.left'];
  1843. var gutterTop = prop['chart.gutter.top'];
  1844. var width = tooltip.offsetWidth;
  1845. var height = tooltip.offsetHeight;
  1846. var value = obj.data_arr[tooltip.__index__];
  1847. // Set the top position
  1848. tooltip.style.left = 0;
  1849. tooltip.style.top = canvasXY[1] + coordY - height - 7 + 'px';
  1850. /**
  1851. * If the tooltip is for a negative value - position it underneath the bar
  1852. */
  1853. if (value < 0) {
  1854. tooltip.style.top = canvasXY[1] + coordY + coordH + 7 + 'px';
  1855. }
  1856. // By default any overflow is hidden
  1857. tooltip.style.overflow = '';
  1858. // Inverted arrow
  1859. // data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAAFCAMAAACkeOZkAAAAK3RFWHRDcmVhdGlvbiBUaW1lAFNhdCA2IE9jdCAyMDEyIDEyOjQ5OjMyIC0wMDAw2S1RlgAAAAd0SU1FB9wKBgszM4Ed2k4AAAAJcEhZcwAACxIAAAsSAdLdfvwAAAAEZ0FNQQAAsY8L/GEFAAAACVBMVEX/AAC9vb3//+92Pom0AAAAAXRSTlMAQObYZgAAAB1JREFUeNpjYAABRgY4YGRiRDCZYBwQE8qBMEEcAANCACqByy1sAAAAAElFTkSuQmCC
  1860. // The arrow
  1861. var img = new Image();
  1862. img.style.position = 'absolute';
  1863. img.id = '__rgraph_tooltip_pointer__';
  1864. if (value >= 0) {
  1865. img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABEAAAAFCAYAAACjKgd3AAAARUlEQVQYV2NkQAN79+797+RkhC4M5+/bd47B2dmZEVkBCgcmgcsgbAaA9GA1BCSBbhAuA/AagmwQPgMIGgIzCD0M0AMMAEFVIAa6UQgcAAAAAElFTkSuQmCC';
  1866. img.style.top = (tooltip.offsetHeight - 2) + 'px';
  1867. } else {
  1868. img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAAFCAMAAACkeOZkAAAAK3RFWHRDcmVhdGlvbiBUaW1lAFNhdCA2IE9jdCAyMDEyIDEyOjQ5OjMyIC0wMDAw2S1RlgAAAAd0SU1FB9wKBgszM4Ed2k4AAAAJcEhZcwAACxIAAAsSAdLdfvwAAAAEZ0FNQQAAsY8L/GEFAAAACVBMVEX/AAC9vb3//+92Pom0AAAAAXRSTlMAQObYZgAAAB1JREFUeNpjYAABRgY4YGRiRDCZYBwQE8qBMEEcAANCACqByy1sAAAAAElFTkSuQmCC';
  1869. img.style.top = '-5px';
  1870. }
  1871. tooltip.appendChild(img);
  1872. // Reposition the tooltip if at the edges:
  1873. // LEFT edge
  1874. if ((canvasXY[0] + coordX + (coordW / 2) - (width / 2)) < 10) {
  1875. tooltip.style.left = (canvasXY[0] + coordX - (width * 0.1)) + (coordW / 2) + 'px';
  1876. img.style.left = ((width * 0.1) - 8.5) + 'px';
  1877. // RIGHT edge
  1878. } else if ((canvasXY[0] + coordX + (width / 2)) > document.body.offsetWidth) {
  1879. tooltip.style.left = canvasXY[0] + coordX - (width * 0.9) + (coordW / 2) + 'px';
  1880. img.style.left = ((width * 0.9) - 8.5) + 'px';
  1881. // Default positioning - CENTERED
  1882. } else {
  1883. tooltip.style.left = (canvasXY[0] + coordX + (coordW / 2) - (width * 0.5)) + 'px';
  1884. img.style.left = ((width * 0.5) - 8.5) + 'px';
  1885. }
  1886. }
  1887. /**
  1888. * This allows for easy specification of gradients
  1889. */
  1890. this.parseColors = function ()
  1891. {
  1892. // Set this as a local variable
  1893. var prop = this.properties;
  1894. // chart.colors
  1895. var colors = prop['chart.colors'];
  1896. if (colors) {
  1897. for (var i=0; i<colors.length; ++i) {
  1898. colors[i] = this.parseSingleColorForGradient(colors[i]);
  1899. }
  1900. }
  1901. // chart.key.colors
  1902. var colors = prop['chart.key.colors'];
  1903. if (colors) {
  1904. for (var i=0; i<colors.length; ++i) {
  1905. colors[i] = this.parseSingleColorForGradient(colors[i]);
  1906. }
  1907. }
  1908. prop['chart.crosshairs.color'] = this.parseSingleColorForGradient(prop['chart.crosshairs.color']);
  1909. prop['chart.highlight.stroke'] = this.parseSingleColorForGradient(prop['chart.highlight.stroke']);
  1910. prop['chart.highlight.fill'] = this.parseSingleColorForGradient(prop['chart.highlight.fill']);
  1911. prop['chart.text.color'] = this.parseSingleColorForGradient(prop['chart.text.color']);
  1912. prop['chart.background.barcolor1'] = this.parseSingleColorForGradient(prop['chart.background.barcolor1']);
  1913. prop['chart.background.barcolor2'] = this.parseSingleColorForGradient(prop['chart.background.barcolor2']);
  1914. prop['chart.background.grid.color'] = this.parseSingleColorForGradient(prop['chart.background.grid.color']);
  1915. prop['chart.strokecolor'] = this.parseSingleColorForGradient(prop['chart.strokecolor']);
  1916. prop['chart.axis.color'] = this.parseSingleColorForGradient(prop['chart.axis.color']);
  1917. }
  1918. /**
  1919. * This parses a single color value
  1920. */
  1921. this.parseSingleColorForGradient = function (color)
  1922. {
  1923. if (!color || typeof(color) != 'string') {
  1924. return color;
  1925. }
  1926. if (color.match(/^gradient\((.*)\)$/i)) {
  1927. var parts = RegExp.$1.split(':');
  1928. // Create the gradient
  1929. var grad = co.createLinearGradient(0,ca.height - prop['chart.gutter.bottom'], 0, prop['chart.gutter.top']);
  1930. var diff = 1 / (parts.length - 1);
  1931. grad.addColorStop(0, RG.trim(parts[0]));
  1932. for (var j=1,len=parts.length; j<len; ++j) {
  1933. grad.addColorStop(j * diff, RGraph.trim(parts[j]));
  1934. }
  1935. }
  1936. return grad ? grad : color;
  1937. }
  1938. this.DrawBevel = function ()
  1939. {
  1940. var coords = this.coords;
  1941. var coords2 = this.coords2;
  1942. var prop = this.properties;
  1943. var co = this.context;
  1944. var ca = this.canvas;
  1945. if (prop['chart.grouping'] == 'stacked') {
  1946. for (var i=0; i<coords2.length; ++i) {
  1947. if (coords2[i] && coords2[i][0] && coords2[i][0][0]) {
  1948. var x = coords2[i][0][0];
  1949. var y = coords2[i][0][1];
  1950. var w = coords2[i][0][2];
  1951. var arr = [];
  1952. for (var j=0; j<coords2[i].length; ++j) {
  1953. arr.push(coords2[i][j][3]);
  1954. }
  1955. var h = RGraph.array_sum(arr);
  1956. co.save();
  1957. co.strokeStyle = 'black';
  1958. // Clip to the rect
  1959. co.beginPath();
  1960. co.rect(x, y, w, h);
  1961. co.clip();
  1962. // Add the shadow
  1963. co.shadowColor = 'black';
  1964. co.shadowOffsetX = 0;
  1965. co.shadowOffsetY = 0;
  1966. co.shadowBlur = 20;
  1967. co.beginPath();
  1968. co.rect(x - 3, y - 3, w + 6, h + 100);
  1969. co.lineWidth = 5;
  1970. co.stroke();
  1971. co.restore();
  1972. }
  1973. }
  1974. } else {
  1975. for (var i=0; i<coords.length; ++i) {
  1976. if (coords[i]) {
  1977. var x = coords[i][0];
  1978. var y = coords[i][1];
  1979. var w = coords[i][2];
  1980. var h = coords[i][3];
  1981. var xaxispos = prop['chart.xaxispos'];
  1982. var xaxis_ycoord = ((ca.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop;
  1983. co.save();
  1984. co.strokeStyle = 'black';
  1985. // Clip to the rect
  1986. co.beginPath();
  1987. co.rect(x, y, w, h);
  1988. co.clip();
  1989. // Add the shadow
  1990. co.shadowColor = 'black';
  1991. co.shadowOffsetX = 0;
  1992. co.shadowOffsetY = 0;
  1993. co.shadowBlur = 20;
  1994. if (xaxispos == 'top' || (xaxispos == 'center' && (y + h) > xaxis_ycoord)) {
  1995. y = y - 100;
  1996. h = h + 100;
  1997. } else {
  1998. y = y;
  1999. h = h + 100;
  2000. }
  2001. co.beginPath();
  2002. co.rect(x - 3, y - 3, w + 6, h + 6);
  2003. co.lineWidth = 5;
  2004. co.stroke();
  2005. co.restore();
  2006. }
  2007. }
  2008. }
  2009. }
  2010. /**
  2011. * This function handles highlighting an entire data-series for the interactive
  2012. * key
  2013. *
  2014. * @param int index The index of the data series to be highlighted
  2015. */
  2016. this.interactiveKeyHighlight = function (index)
  2017. {
  2018. this.coords2.forEach(function (value, idx, arr)
  2019. {
  2020. if (typeof value[index] == 'object' && value[index]) {
  2021. var x = value[index][0]
  2022. var y = value[index][1]
  2023. var w = value[index][2]
  2024. var h = value[index][3]
  2025. co.fillStyle = prop['chart.key.interactive.highlight.chart.fill'];
  2026. co.strokeStyle = prop['chart.key.interactive.highlight.chart.stroke'];
  2027. co.lineWidth = 2;
  2028. co.fillRect(x, y, w, h);
  2029. co.strokeRect(x, y, w, h);
  2030. }
  2031. });
  2032. }
  2033. /**
  2034. * Register the object
  2035. */
  2036. RGraph.Register(this);
  2037. }
  2038. /*********************************************************************************************************
  2039. * This is the combined bar and Line class which makes creating bar/line combo charts a little bit easier *
  2040. /*********************************************************************************************************/
  2041. RGraph.CombinedChart = function ()
  2042. {
  2043. /**
  2044. * Create a default empty array for the objects
  2045. */
  2046. this.objects = [];
  2047. var objects = arguments;
  2048. if (RGraph.is_array(arguments[0])) {
  2049. objects = arguments[0];
  2050. }
  2051. for (var i=0; i<objects.length; ++i) {
  2052. this.objects[i] = objects[i];
  2053. /**
  2054. * Set the Line chart gutters to match the Bar chart gutters
  2055. */
  2056. this.objects[i].Set('chart.gutter.left', this.objects[0].Get('chart.gutter.left'));
  2057. this.objects[i].Set('chart.gutter.right', this.objects[0].Get('chart.gutter.right'));
  2058. this.objects[i].Set('chart.gutter.top', this.objects[0].Get('chart.gutter.top'));
  2059. this.objects[i].Set('chart.gutter.bottom', this.objects[0].Get('chart.gutter.bottom'));
  2060. if (this.objects[i].type == 'line') {
  2061. /**
  2062. * Set the line chart hmargin
  2063. */
  2064. this.objects[i].Set('chart.hmargin', ((this.objects[0].canvas.width - this.objects[0].Get('chart.gutter.right') - this.objects[0].Get('chart.gutter.left')) / this.objects[0].data.length) / 2 );
  2065. /**
  2066. * No labels, axes or grid on the Line chart
  2067. */
  2068. this.objects[i].Set('chart.noaxes', true);
  2069. this.objects[i].Set('chart.background.grid', false);
  2070. this.objects[i].Set('chart.ylabels', false);
  2071. }
  2072. /**
  2073. * Resizing
  2074. */
  2075. if (this.objects[i].Get('chart.resizable')) {
  2076. var resizable_object = this.objects[i];
  2077. }
  2078. }
  2079. /**
  2080. * Resizing
  2081. */
  2082. if (resizable_object) {
  2083. /**
  2084. * This recalculates the Line chart hmargin when the chart is resized
  2085. */
  2086. function myOnresizebeforedraw (obj)
  2087. {
  2088. var gutterLeft = obj.Get('chart.gutter.left');
  2089. var gutterRight = obj.Get('chart.gutter.right');
  2090. obj.Set('chart.hmargin', (obj.canvas.width - gutterLeft - gutterRight) / (obj.original_data[0].length * 2));
  2091. }
  2092. RGraph.AddCustomEventListener(resizable_object,
  2093. 'onresizebeforedraw',
  2094. myOnresizebeforedraw);
  2095. }
  2096. }
  2097. /**
  2098. * The Add method can be used to add methods to the CombinedChart object.
  2099. */
  2100. RGraph.CombinedChart.prototype.Add = function (obj)
  2101. {
  2102. this.objects.push(obj);
  2103. }
  2104. /**
  2105. * The Draw method goes through all of the objects drawing them (sequentially)
  2106. */
  2107. RGraph.CombinedChart.prototype.Draw = function ()
  2108. {
  2109. for (var i=0; i<this.objects.length; ++i) {
  2110. if (typeof(arguments[i]) == 'function') {
  2111. arguments[i](this.objects[i]);
  2112. } else {
  2113. this.objects[i].Draw();
  2114. }
  2115. }
  2116. }