| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067 |
- // version: 2014-06-26
- /**
- * o--------------------------------------------------------------------------------o
- * | This file is part of the RGraph package. RGraph is Free Software, licensed |
- * | under the MIT license - so it's free to use for all purposes. If you want to |
- * | donate to help keep the project going then you can do so here: |
- * | |
- * | http://www.rgraph.net/donate |
- * o--------------------------------------------------------------------------------o
- */
- RGraph = window.RGraph || {isRGraph: true};
- /**
- * The bar chart constructor
- *
- * @param object canvas The canvas object
- * @param array data The chart data
- */
- RGraph.Bar = function (conf)
- {
- /**
- * Allow for object config style
- */
- if (typeof conf === 'object' && typeof conf.data === 'object') {
- var id = conf.id
- var canvas = document.getElementById(id);
- var data = conf.data;
- var parseConfObjectForOptions = true; // Set this so the config is parsed (at the end of the constructor)
- } else {
- var id = conf;
- var canvas = document.getElementById(id);
- var data = arguments[1];
- }
- // Get the canvas and context objects
- this.id = id;
- this.canvas = canvas;
- this.context = this.canvas.getContext ? this.canvas.getContext("2d", {alpha: (typeof id === 'object' && id.alpha === false) ? false : true}) : null;
- this.canvas.__object__ = this;
- this.type = 'bar';
- this.max = 0;
- this.stackedOrGrouped = false;
- this.isRGraph = true;
- this.uid = RGraph.CreateUID();
- this.canvas.uid = this.canvas.uid ? this.canvas.uid : RGraph.CreateUID();
- this.colorsParsed = false;
- this.original_colors = [];
- this.cachedBackgroundCanvas = null;
- this.firstDraw = true; // After the first draw this will be false
- /**
- * Compatibility with older browsers
- */
- //RGraph.OldBrowserCompat(this.context);
- // Various config type stuff
- this.properties =
- {
- 'chart.background.barcolor1': 'rgba(0,0,0,0)',
- 'chart.background.barcolor2': 'rgba(0,0,0,0)',
- 'chart.background.grid': true,
- 'chart.background.grid.color': '#ddd',
- 'chart.background.grid.width': 1,
- 'chart.background.grid.hsize': 20,
- 'chart.background.grid.vsize': 20,
- 'chart.background.grid.vlines': true,
- 'chart.background.grid.hlines': true,
- 'chart.background.grid.border': true,
- 'chart.background.grid.autofit':true,
- 'chart.background.grid.autofit.numhlines': 5,
- 'chart.background.grid.autofit.numvlines': 20,
- 'chart.background.grid.dashed': false,
- 'chart.background.grid.dotted': false,
- 'chart.background.image.stretch': true,
- 'chart.background.image.x': null,
- 'chart.background.image.y': null,
- 'chart.background.image.w': null,
- 'chart.background.image.h': null,
- 'chart.background.image.align': null,
- 'chart.background.color': null,
- 'chart.numyticks': 10,
- 'chart.hmargin': 5,
- 'chart.hmargin.grouped': 1,
- 'chart.strokecolor': 'white',
- 'chart.axis.color': 'black',
- 'chart.axis.linewidth': 1,
- 'chart.gutter.top': 25,
- 'chart.gutter.bottom': 25,
- 'chart.gutter.left': 25,
- 'chart.gutter.right': 25,
- 'chart.labels': null,
- 'chart.labels.ingraph': null,
- 'chart.labels.above': false,
- 'chart.labels.above.decimals': 0,
- 'chart.labels.above.size': null,
- 'chart.labels.above.color': null,
- 'chart.labels.above.angle': null,
- 'chart.labels.above.offset': 4,
- 'chart.ylabels': true,
- 'chart.ylabels.count': 5,
- 'chart.ylabels.inside': false,
- 'chart.xlabels.offset': 0,
- 'chart.xaxispos': 'bottom',
- 'chart.yaxispos': 'left',
- 'chart.text.angle': 0,
- 'chart.text.color': 'black', // Gradients aren't supported for this color
- 'chart.text.size': 10,
- 'chart.text.font': 'Arial',
- 'chart.ymin': 0,
- 'chart.ymax': null,
- 'chart.title': '',
- 'chart.title.font': null,
- 'chart.title.background': null, // Gradients aren't supported for this color
- 'chart.title.hpos': null,
- 'chart.title.vpos': null,
- 'chart.title.bold': true,
- 'chart.title.xaxis': '',
- 'chart.title.xaxis.bold': true,
- 'chart.title.xaxis.size': null,
- 'chart.title.xaxis.font': null,
- 'chart.title.yaxis': '',
- 'chart.title.yaxis.bold': true,
- 'chart.title.yaxis.size': null,
- 'chart.title.yaxis.font': null,
- 'chart.title.yaxis.color': null, // Gradients aren't supported for this color
- 'chart.title.xaxis.pos': null,
- 'chart.title.yaxis.pos': null,
- 'chart.title.yaxis.x': null,
- 'chart.title.yaxis.y': null,
- 'chart.title.xaxis.x': null,
- 'chart.title.xaxis.y': null,
- 'chart.title.x': null,
- 'chart.title.y': null,
- 'chart.title.halign': null,
- 'chart.title.valign': null,
- 'chart.colors': [
- 'Gradient(#F9D5C9:#E65F2D:#E65F2D:#E65F2D)',
- 'Gradient(#F7DCD1:#D4592A:#D4592A:#D4592A)',
- 'Gradient(#DEE5EA:#B5C3CE:#B5C3CE:#B5C3CE)',
- 'Gradient(#E5E5E3:#545451:#545451:#545451)',
- 'Gradient(#F6E5D2:#E9C294:#E9C294:#E9C294)',
- 'Gradient(#F5EAD3:#D6AA4E:#D6AA4E:#D6AA4E)'
- ],
- 'chart.colors.sequential': false,
- 'chart.colors.reverse': false,
- 'chart.grouping': 'grouped',
- 'chart.variant': 'bar',
- 'chart.variant.sketch.verticals': true,
- 'chart.shadow': true,
- 'chart.shadow.color': '#aaa', // Gradients aren't supported for this color
- 'chart.shadow.offsetx': 0,
- 'chart.shadow.offsety': 0,
- 'chart.shadow.blur': 15,
- 'chart.tooltips': null,
- 'chart.tooltips.effect': 'fade',
- 'chart.tooltips.css.class': 'RGraph_tooltip',
- 'chart.tooltips.event': 'onclick',
- 'chart.tooltips.highlight': true,
- 'chart.highlight.stroke': 'rgba(0,0,0,0)',
- 'chart.highlight.fill': 'rgba(255,255,255,0.7)',
- 'chart.background.hbars': null,
- 'chart.key': null,
- 'chart.key.background': 'white',
- 'chart.key.position': 'graph',
- 'chart.key.shadow': false,
- 'chart.key.shadow.color': '#666',
- 'chart.key.shadow.blur': 3,
- 'chart.key.shadow.offsetx': 2,
- 'chart.key.shadow.offsety': 2,
- 'chart.key.position.gutter.boxed':false,
- 'chart.key.position.x': null,
- 'chart.key.position.y': null,
- 'chart.key.interactive': false,
- 'chart.key.interactive.highlight.chart.stroke':'black',
- 'chart.key.interactive.highlight.chart.fill':'rgba(255,255,255,0.7)',
- 'chart.key.interactive.highlight.label':'rgba(255,0,0,0.2)',
- 'chart.key.halign': 'right',
- 'chart.key.color.shape': 'square',
- 'chart.key.rounded': true,
- 'chart.key.text.size': 10,
- 'chart.key.linewidth': 1,
- 'chart.key.colors': null,
- 'chart.key.text.color': 'black',
- 'chart.contextmenu': null,
- 'chart.units.pre': '',
- 'chart.units.post': '',
- 'chart.scale.decimals': 0,
- 'chart.scale.point': '.',
- 'chart.scale.thousand': ',',
- 'chart.crosshairs': false,
- 'chart.crosshairs.color': '#333',
- 'chart.crosshairs.hline': true,
- 'chart.crosshairs.vline': true,
- 'chart.linewidth': 1,
- 'chart.annotatable': false,
- 'chart.annotate.color': 'black',
- 'chart.zoom.factor': 1.5,
- 'chart.zoom.fade.in': true,
- 'chart.zoom.fade.out': true,
- 'chart.zoom.hdir': 'right',
- 'chart.zoom.vdir': 'down',
- 'chart.zoom.frames': 25,
- 'chart.zoom.delay': 16.666,
- 'chart.zoom.shadow': true,
- 'chart.zoom.background': true,
- 'chart.resizable': false,
- 'chart.resize.handle.background': null,
- 'chart.adjustable': false,
- 'chart.noaxes': false,
- 'chart.noxaxis': false,
- 'chart.noyaxis': false,
- 'chart.events.click': null,
- 'chart.events.mousemove': null,
- 'chart.numxticks': null,
- 'chart.bevel': false
- }
- // Check for support
- if (!this.canvas) {
- alert('[BAR] No canvas support');
- return;
- }
- /**
- * Determine whether the chart will contain stacked or grouped bars
- */
- for (var i=0; i<data.length; ++i) {
- if (typeof data[i] === 'object' && !RGraph.is_null(data[i])) {
- this.stackedOrGrouped = true;
- }
- }
- /**
- * Create the dollar objects so that functions can be added to them
- */
- var linear_data = RGraph.array_linearize(data);
- for (var i=0; i<linear_data.length; ++i) {
- this['$' + i] = {};
- }
- // Store the data
- this.data = data;
-
- // Used to store the coords of the bars
- this.coords = [];
- this.coords2 = [];
- this.coordsText = [];
- /**
- * This linearises the data. Doing so can make it easier to pull
- * out the appropriate data from tooltips
- */
- this.data_arr = RGraph.array_linearize(this.data);
- /**
- * Translate half a pixel for antialiasing purposes - but only if it hasn't beeen
- * done already
- */
- if (!this.canvas.__rgraph_aa_translated__) {
- this.context.translate(0.5,0.5);
- this.canvas.__rgraph_aa_translated__ = true;
- }
- // Short variable names
- var RG = RGraph;
- var ca = this.canvas;
- var co = ca.getContext('2d');
- var prop = this.properties;
- var jq = jQuery;
- var pa = RG.Path;
- var win = window;
- var doc = document;
- var ma = Math;
-
-
-
- /**
- * "Decorate" the object with the generic effects if the effects library has been included
- */
- if (RG.Effects && typeof RG.Effects.decorate === 'function') {
- RG.Effects.decorate(this);
- }
- /**
- * A setter
- *
- * @param name string The name of the property to set
- * @param value mixed The value of the property
- */
- this.set =
- this.Set = function (name, value)
- {
- name = name.toLowerCase();
- /**
- * This should be done first - prepend the propertyy name with "chart." if necessary
- */
- if (name.substr(0,6) != 'chart.') {
- name = 'chart.' + name;
- }
-
- if (name == 'chart.labels.abovebar') {
- name = 'chart.labels.above';
- }
-
- if (name == 'chart.strokestyle') {
- name = 'chart.strokecolor';
- }
-
- /**
- * Check for xaxispos
- */
- if (name == 'chart.xaxispos' ) {
- if (value != 'bottom' && value != 'center' && value != 'top') {
- alert('[BAR] (' + this.id + ') chart.xaxispos should be top, center or bottom. Tried to set it to: ' + value + ' Changing it to center');
- value = 'center';
- }
-
- if (value == 'top') {
- for (var i=0; i<this.data.length; ++i) {
- if (typeof(this.data[i]) == 'number' && this.data[i] > 0) {
- alert('[BAR] The data element with index ' + i + ' should be negative');
- }
- }
- }
- }
-
- /**
- * lineWidth doesn't appear to like a zero setting
- */
- if (name.toLowerCase() == 'chart.linewidth' && value == 0) {
- value = 0.0001;
- }
-
- prop[name] = value;
-
- return this;
- };
- /**
- * A getter
- *
- * @param name string The name of the property to get
- */
- this.get =
- this.Get = function (name)
- {
- /**
- * This should be done first - prepend the property name with "chart." if necessary
- */
- if (name.substr(0,6) != 'chart.') {
- name = 'chart.' + name;
- }
-
- return prop[name];
- };
- /**
- * The function you call to draw the bar chart
- */
- this.draw =
- this.Draw = function ()
- {
- // MUST be the first thing done!
- if (typeof(prop['chart.background.image']) == 'string') {
- RG.DrawBackgroundImage(this);
- }
-
- /**
- * Fire the onbeforedraw event
- */
- RG.FireCustomEvent(this, 'onbeforedraw');
-
-
-
- /**
- * Parse the colors. This allows for simple gradient syntax
- */
- if (!this.colorsParsed) {
- this.parseColors();
-
- // Don't want to do this again
- this.colorsParsed = true;
- }
-
-
-
- /**
- * This is new in May 2011 and facilitates indiviual gutter settings,
- * eg chart.gutter.left
- */
- this.gutterLeft = prop['chart.gutter.left'];
- this.gutterRight = prop['chart.gutter.right'];
- this.gutterTop = prop['chart.gutter.top'];
- this.gutterBottom = prop['chart.gutter.bottom'];
-
- // Cache this in a class variable as it's used rather a lot
-
- /**
- * Check for tooltips and alert the user that they're not supported with pyramid charts
- */
- if ( (prop['chart.variant'] == 'pyramid' || prop['chart.variant'] == 'dot')
- && typeof(prop['chart.tooltips']) == 'object'
- && prop['chart.tooltips']
- && prop['chart.tooltips'].length > 0) {
-
- alert('[BAR] (' + this.id + ') Sorry, tooltips are not supported with dot or pyramid charts');
- }
-
- /**
- * Stop the coords arrays from growing uncontrollably
- */
- this.coords = [];
- this.coords2 = [];
- this.coordsText = [];
-
- /**
- * Work out a few things. They need to be here because they depend on things you can change before you
- * call Draw() but after you instantiate the object
- */
- this.max = 0;
- this.grapharea = ca.height - this.gutterTop - this.gutterBottom;
- this.halfgrapharea = this.grapharea / 2;
- this.halfTextHeight = prop['chart.text.size'] / 2;
-
- // Now draw the background on to the main canvas
- RG.background.Draw(this);
-
-
-
- //If it's a sketch chart variant, draw the axes first
- if (prop['chart.variant'] == 'sketch') {
- this.DrawAxes();
- this.Drawbars();
- } else {
- this.Drawbars();
- this.DrawAxes();
- }
-
- this.DrawLabels();
-
- /**
- * Draw the bevel if required
- */
- if (prop['chart.bevel'] || prop['chart.bevelled']) {
- this.DrawBevel();
- }
-
-
- // Draw the key if necessary
- if (prop['chart.key'] && prop['chart.key'].length) {
- RG.DrawKey(this, prop['chart.key'], prop['chart.colors']);
- }
-
-
- /**
- * Setup the context menu if required
- */
- if (prop['chart.contextmenu']) {
- RG.ShowContext(this);
- }
-
-
- /**
- * Draw "in graph" labels
- */
- if (prop['chart.labels.ingraph']) {
- RG.DrawInGraphLabels(this);
- }
-
-
- /**
- * This function enables resizing
- */
- if (prop['chart.resizable']) {
- RG.AllowResizing(this);
- }
-
-
- /**
- * This installs the event listeners
- */
- RG.InstallEventListeners(this);
-
-
- /**
- * Fire the onfirstdraw event
- */
- if (this.firstDraw) {
- RG.fireCustomEvent(this, 'onfirstdraw');
- this.firstDrawFunc();
- this.firstDraw = false;
- }
- /**
- * Fire the RGraph ondraw event
- */
- RG.fireCustomEvent(this, 'ondraw');
-
- return this;
- };
- /**
- * Draws the charts axes
- */
- this.drawAxes =
- this.DrawAxes = function ()
- {
- if (prop['chart.noaxes']) {
- return;
- }
-
- var xaxispos = prop['chart.xaxispos'];
- var yaxispos = prop['chart.yaxispos'];
- var isSketch = prop['chart.variant'] == 'sketch';
-
- co.beginPath();
- co.strokeStyle = prop['chart.axis.color'];
- co.lineWidth = prop['chart.axis.linewidth'] + 0.001;
-
- if (RG.ISSAFARI == -1) {
- co.lineCap = 'square';
- }
-
-
- // Draw the Y axis
- if (prop['chart.noyaxis'] == false) {
- if (yaxispos == 'right') {
- co.moveTo(ca.width - this.gutterRight + (isSketch ? 3 : 0), this.gutterTop - (isSketch ? 3 : 0));
- co.lineTo(ca.width - this.gutterRight - (isSketch ? 2 : 0), ca.height - this.gutterBottom + (isSketch ? 5 : 0));
- } else {
- co.moveTo(this.gutterLeft - (isSketch ? 2 : 0), this.gutterTop - (isSketch ? 5 : 0));
- co.lineTo(this.gutterLeft - (isSketch ? 1 : 0), ca.height - this.gutterBottom + (isSketch ? 5 : 0));
- }
- }
-
- // Draw the X axis
- if (prop['chart.noxaxis'] == false) {
- if (xaxispos == 'center') {
- co.moveTo(this.gutterLeft - (isSketch ? 5 : 0), Math.round(((ca.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop + (isSketch ? 2 : 0)));
- co.lineTo(ca.width - this.gutterRight + (isSketch ? 5 : 0), Math.round(((ca.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop - (isSketch ? 2 : 0)));
- } else if (xaxispos == 'top') {
- co.moveTo(this.gutterLeft - (isSketch ? 3 : 0), this.gutterTop - (isSketch ? 3 : 0));
- co.lineTo(ca.width - this.gutterRight + (isSketch ? 5 : 0), this.gutterTop + (isSketch ? 2 : 0));
- } else {
- co.moveTo(this.gutterLeft - (isSketch ? 5 : 0), ca.height - this.gutterBottom - (isSketch ? 2 : 0));
- co.lineTo(ca.width - this.gutterRight + (isSketch ? 8 : 0), ca.height - this.gutterBottom + (isSketch ? 2 : 0));
- }
- }
-
- var numYTicks = prop['chart.numyticks'];
-
- // Draw the Y tickmarks
- if (prop['chart.noyaxis'] == false && !isSketch) {
- var yTickGap = (ca.height - this.gutterTop - this.gutterBottom) / numYTicks;
- var xpos = yaxispos == 'left' ? this.gutterLeft : ca.width - this.gutterRight;
-
- if (this.properties['chart.numyticks'] > 0) {
- for (y=this.gutterTop;
- xaxispos == 'center' ? y <= (ca.height - this.gutterBottom) : y < (ca.height - this.gutterBottom + (xaxispos == 'top' ? 1 : 0));
- y += yTickGap) {
-
- if (xaxispos == 'center' && y == (this.gutterTop + (this.grapharea / 2))) continue;
-
- // X axis at the top
- if (xaxispos == 'top' && y == this.gutterTop) continue;
-
- co.moveTo(xpos + (yaxispos == 'left' ? 0 : 0), Math.round(y));
- co.lineTo(xpos + (yaxispos == 'left' ? -3 : 3), Math.round(y));
- }
- }
-
- /**
- * If the X axis is not being shown, draw an extra tick
- */
- if (prop['chart.noxaxis']) {
- if (xaxispos == 'center') {
- co.moveTo(xpos + (yaxispos == 'left' ? -3 : 3), Math.round(ca.height / 2));
- co.lineTo(xpos, Math.round(ca.height / 2));
- } else if (xaxispos == 'top') {
- co.moveTo(xpos + (yaxispos == 'left' ? -3 : 3), Math.round(this.gutterTop));
- co.lineTo(xpos, Math.round(this.gutterTop));
- } else {
- co.moveTo(xpos + (yaxispos == 'left' ? -3 : 3), Math.round(ca.height - this.gutterBottom));
- co.lineTo(xpos, Math.round(ca.height - this.gutterBottom));
- }
- }
- }
-
-
- // Draw the X tickmarks
- if (prop['chart.noxaxis'] == false && !isSketch) {
-
- if (typeof(prop['chart.numxticks']) == 'number') {
- var xTickGap = (ca.width - this.gutterLeft - this.gutterRight) / prop['chart.numxticks'];
- } else {
- var xTickGap = (ca.width - this.gutterLeft - this.gutterRight) / this.data.length;
- }
-
- if (xaxispos == 'bottom') {
- yStart = ca.height - this.gutterBottom;
- yEnd = (ca.height - this.gutterBottom) + 3;
- } else if (xaxispos == 'top') {
- yStart = this.gutterTop - 3;
- yEnd = this.gutterTop;
- } else if (xaxispos == 'center') {
- yStart = ((ca.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop + 3;
- yEnd = ((ca.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop - 3;
- }
-
- yStart = yStart;
- yEnd = yEnd;
-
- //////////////// X TICKS ////////////////
- var noEndXTick = prop['chart.noendxtick'];
-
- for (x=this.gutterLeft + (yaxispos == 'left' ? xTickGap : 0),len=(ca.width - this.gutterRight + (yaxispos == 'left' ? 5 : 0)); x<len; x+=xTickGap) {
-
- if (yaxispos == 'left' && !noEndXTick && x > this.gutterLeft) {
- co.moveTo(Math.round(x), yStart);
- co.lineTo(Math.round(x), yEnd);
-
- } else if (yaxispos == 'left' && noEndXTick && x > this.gutterLeft && x < (ca.width - this.gutterRight) ) {
- co.moveTo(Math.round(x), yStart);
- co.lineTo(Math.round(x), yEnd);
-
- } else if (yaxispos == 'right' && x < (ca.width - this.gutterRight) && !noEndXTick) {
- co.moveTo(Math.round(x), yStart);
- co.lineTo(Math.round(x), yEnd);
-
- } else if (yaxispos == 'right' && x < (ca.width - this.gutterRight) && x > (this.gutterLeft) && noEndXTick) {
- co.moveTo(Math.round(x), yStart);
- co.lineTo(Math.round(x), yEnd);
- }
- }
-
- if (prop['chart.noyaxis'] || prop['chart.numxticks'] == null) {
- if (typeof(prop['chart.numxticks']) == 'number' && prop['chart.numxticks'] > 0) {
- co.moveTo(Math.round(this.gutterLeft), yStart);
- co.lineTo(Math.round(this.gutterLeft), yEnd);
- }
- }
-
- //////////////// X TICKS ////////////////
- }
-
- /**
- * If the Y axis is not being shown, draw an extra tick
- */
- if (prop['chart.noyaxis'] && prop['chart.noxaxis'] == false && prop['chart.numxticks'] == null) {
- if (xaxispos == 'center') {
- co.moveTo(Math.round(this.gutterLeft), (ca.height / 2) - 3);
- co.lineTo(Math.round(this.gutterLeft), (ca.height / 2) + 3);
- } else {
- co.moveTo(Math.round(this.gutterLeft), ca.height - this.gutterBottom);
- co.lineTo(Math.round(this.gutterLeft), ca.height - this.gutterBottom + 3);
- }
- }
-
- co.stroke();
- };
-
-
-
- /**
- * Draws the bars
- */
- this.drawbars =
- this.Drawbars = function ()
- {
- // Variable "caching" so the context can be accessed as a local variable
- //var ca = this.canvas;
- //var co = this.context;
- //var prop = this.properties;
-
- co.lineWidth = prop['chart.linewidth'];
- co.strokeStyle = prop['chart.strokecolor'];
- co.fillStyle = prop['chart.colors'][0];
- var prevX = 0;
- var prevY = 0;
- var decimals = prop['chart.scale.decimals'];
-
- /**
- * Work out the max value
- */
- if (prop['chart.ymax']) {
-
- this.scale2 = RGraph.getScale2(this, {
- 'max':prop['chart.ymax'],
- 'strict': true,
- 'min':prop['chart.ymin'],
- 'scale.thousand':prop['chart.scale.thousand'],
- 'scale.point':prop['chart.scale.point'],
- 'scale.decimals':prop['chart.scale.decimals'],
- 'ylabels.count':prop['chart.ylabels.count'],
- 'scale.round':prop['chart.scale.round'],
- 'units.pre': prop['chart.units.pre'],
- 'units.post': prop['chart.units.post']
- });
- } else {
-
- for (i=0; i<this.data.length; ++i) {
- if (typeof(this.data[i]) == 'object') {
- var value = prop['chart.grouping'] == 'grouped' ? Number(RG.array_max(this.data[i], true)) : Number(RG.array_sum(this.data[i]));
-
- } else {
- var value = Number(this.data[i]);
- }
-
- this.max = Math.max(Math.abs(this.max), Math.abs(value));
- }
-
- this.scale2 = RGraph.getScale2(this, {
- 'max':this.max,
- 'min':prop['chart.ymin'],
- 'scale.thousand':prop['chart.scale.thousand'],
- 'scale.point':prop['chart.scale.point'],
- 'scale.decimals':prop['chart.scale.decimals'],
- 'ylabels.count':prop['chart.ylabels.count'],
- 'scale.round':prop['chart.scale.round'],
- 'units.pre': prop['chart.units.pre'],
- 'units.post': prop['chart.units.post']
- });
-
- this.max = this.scale2.max;
- }
- /**
- * if the chart is adjustable fix the scale so that it doesn't change.
- */
- if (prop['chart.adjustable'] && !prop['chart.ymax']) {
- this.Set('chart.ymax', this.scale2.max);
- }
-
- /**
- * Draw horizontal bars here
- */
- if (prop['chart.background.hbars'] && prop['chart.background.hbars'].length > 0) {
- RGraph.DrawBars(this);
- }
-
- var variant = prop['chart.variant'];
-
- /**
- * Draw the 3D axes is necessary
- */
- if (variant == '3d') {
- RG.Draw3DAxes(this);
- }
-
- /**
- * Get the variant once, and draw the bars, be they regular, stacked or grouped
- */
-
- // Get these variables outside of the loop
- var xaxispos = prop['chart.xaxispos'];
- var width = (ca.width - this.gutterLeft - this.gutterRight ) / this.data.length;
- var orig_height = height;
- var hmargin = prop['chart.hmargin'];
- var shadow = prop['chart.shadow'];
- var shadowColor = prop['chart.shadow.color'];
- var shadowBlur = prop['chart.shadow.blur'];
- var shadowOffsetX = prop['chart.shadow.offsetx'];
- var shadowOffsetY = prop['chart.shadow.offsety'];
- var strokeStyle = prop['chart.strokecolor'];
- var colors = prop['chart.colors'];
- var sequentialColorIndex = 0;
-
- for (i=0,len=this.data.length; i<len; i+=1) {
-
- // Work out the height
- //The width is up outside the loop
- 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);
-
- // Half the height if the Y axis is at the center
- if (xaxispos == 'center') {
- height /= 2;
- }
-
- var x = (i * width) + this.gutterLeft;
- var y = xaxispos == 'center' ? ((ca.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop - height
- : ca.height - height - this.gutterBottom;
-
- // xaxispos is top
- if (xaxispos == 'top') {
- y = this.gutterTop + Math.abs(height);
- }
-
-
- // Account for negative lengths - Some browsers (eg Chrome) don't like a negative value
- if (height < 0) {
- y += height;
- height = Math.abs(height);
- }
-
- /**
- * Turn on the shadow if need be
- */
- if (shadow) {
- co.shadowColor = shadowColor;
- co.shadowBlur = shadowBlur;
- co.shadowOffsetX = shadowOffsetX;
- co.shadowOffsetY = shadowOffsetY;
- }
- /**
- * Draw the bar
- */
- co.beginPath();
- if (typeof(this.data[i]) == 'number') {
- var barWidth = width - (2 * hmargin);
-
- /**
- * Check for a negative bar width
- */
- if (barWidth < 0) {
- 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.');
- }
-
- // Set the fill color
- co.strokeStyle = strokeStyle;
- co.fillStyle = colors[0];
-
- /**
- * Sequential colors
- */
- if (prop['chart.colors.sequential']) {
- co.fillStyle = colors[i];
- }
- if (variant == 'sketch') {
-
- co.lineCap = 'round';
-
- var sketchOffset = 3;
-
- co.beginPath();
-
- co.strokeStyle = colors[0];
-
- /**
- * Sequential colors
- */
- if (prop['chart.colors.sequential']) {
- co.strokeStyle = colors[i];
- }
-
- // Left side
- co.moveTo(x + hmargin + 2, y + height - 2);
- co.lineTo(x + hmargin - 1, y - 4);
-
- // The top
- co.moveTo(x + hmargin - 3, y + -2 + (this.data[i] < 0 ? height : 0));
- co.bezierCurveTo(
- x + ((hmargin + width) * 0.33),
- y + 15 + (this.data[i] < 0 ? height - 10: 0),
- x + ((hmargin + width) * 0.66),
- y + 5 + (this.data[i] < 0 ? height - 10 : 0),x + hmargin + width + -1, y + 0 + (this.data[i] < 0 ? height : 0)
- );
-
-
- // The right side
- co.moveTo(x + hmargin + width - 5, y - 5);
- co.lineTo(x + hmargin + width - 3, y + height - 3);
-
- if (prop['chart.variant.sketch.verticals']) {
- for (var r=0.2; r<=0.8; r+=0.2) {
- co.moveTo(x + hmargin + width + (r > 0.4 ? -1 : 3) - (r * width),y - 1);
- co.lineTo(x + hmargin + width - (r > 0.4 ? 1 : -1) - (r * width), y + height + (r == 0.2 ? 1 : -2));
- }
- }
-
- co.stroke();
-
- // Regular bar
- } else if (variant == 'bar' || variant == '3d' || variant == 'glass' || variant == 'bevel') {
- if (RGraph.ISOLD && shadow) {
- this.DrawIEShadow([x + hmargin, y, barWidth, height]);
- }
-
- if (variant == 'glass') {
- 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);
- 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);
- } else {
- // On 9th April 2013 these two were swapped around so that the stroke happens SECOND so that any
- // shadow that is cast by the fill does not overwrite the stroke
- co.beginPath();
- co.rect(x + hmargin, y, barWidth, height);
- co.fill();
-
- // Turn the shadow off so that the stroke doesn't cast any "extra" shadow
- // that would show inside the bar
- RG.NoShadow(this);
-
- co.beginPath();
- co.rect(x + hmargin, y, barWidth, height);
- co.stroke();
- }
-
- // 3D effect
- if (variant == '3d') {
-
- var prevStrokeStyle = co.strokeStyle;
- var prevFillStyle = co.fillStyle;
-
- // Draw the top
- co.beginPath();
- co.moveTo(x + hmargin, y);
- co.lineTo(x + hmargin + 10, y - 5);
- co.lineTo(x + hmargin + 10 + barWidth, y - 5);
- co.lineTo(x + hmargin + barWidth, y);
- co.closePath();
-
- co.stroke();
- co.fill();
-
- // Draw the right hand side
- co.beginPath();
- co.moveTo(x + hmargin + barWidth, y);
- co.lineTo(x + hmargin + barWidth + 10, y - 5);
- co.lineTo(x + hmargin + barWidth + 10, y + height - 5);
- co.lineTo(x + hmargin + barWidth, y + height);
- co.closePath();
-
- co.stroke();
- co.fill();
-
- // Draw the darker top section
- co.beginPath();
- co.fillStyle = 'rgba(255,255,255,0.3)';
- co.moveTo(x + hmargin, y);
- co.lineTo(x + hmargin + 10, y - 5);
- co.lineTo(x + hmargin + 10 + barWidth, y - 5);
- co.lineTo(x + hmargin + barWidth, y);
- co.lineTo(x + hmargin, y);
- co.closePath();
-
- co.stroke();
- co.fill();
-
- // Draw the darker right side section
- co.beginPath();
- co.fillStyle = 'rgba(0,0,0,0.4)';
- co.moveTo(x + hmargin + barWidth, y);
- co.lineTo(x + hmargin + barWidth + 10, y - 5);
- co.lineTo(x + hmargin + barWidth + 10, y - 5 + height);
- co.lineTo(x + hmargin + barWidth, y + height);
- co.lineTo(x + hmargin + barWidth, y);
- co.closePath();
-
- co.stroke();
- co.fill();
-
- co.strokeStyle = prevStrokeStyle;
- co.fillStyle = prevFillStyle;
-
- // Glass variant
- } else if (variant == 'glass') {
-
- var grad = co.createLinearGradient(x + hmargin,y,x + hmargin + (barWidth / 2),y);
- grad.addColorStop(0, 'rgba(255,255,255,0.9)');
- grad.addColorStop(1, 'rgba(255,255,255,0.5)');
-
- co.beginPath();
- co.fillStyle = grad;
- co.fillRect(x + hmargin + 2,y + (this.data[i] > 0 ? 2 : 0),(barWidth / 2) - 2,height - 2);
- co.fill();
- }
-
- // Dot chart
- } else if (variant == 'dot') {
-
- co.beginPath();
- co.moveTo(x + (width / 2), y);
- co.lineTo(x + (width / 2), y + height);
- co.stroke();
-
- co.beginPath();
- co.fillStyle = this.properties['chart.colors'][i];
- co.arc(x + (width / 2), y + (this.data[i] > 0 ? 0 : height), 2, 0, 6.28, 0);
-
- // Set the colour for the dots
- co.fillStyle = prop['chart.colors'][0];
-
- /**
- * Sequential colors
- */
- if (prop['chart.colors.sequential']) {
- co.fillStyle = colors[i];
- }
-
- co.stroke();
- co.fill();
-
-
- // Unknown variant type
- } else {
- alert('[BAR] Warning! Unknown chart.variant: ' + variant);
- }
- this.coords.push([x + hmargin, y, width - (2 * hmargin), height]);
-
- if (typeof this.coords2[i] == 'undefined') {
- this.coords2[i] = [];
- }
- this.coords2[i].push([x + hmargin, y, width - (2 * hmargin), height]);
-
-
- /**
- * Stacked bar
- */
- } else if (this.data[i] && typeof(this.data[i]) == 'object' && prop['chart.grouping'] == 'stacked') {
-
- if (this.scale2.min) {
- alert("[ERROR] Stacked Bar charts with a Y min are not supported");
- }
-
- var barWidth = width - (2 * hmargin);
- var redrawCoords = [];// Necessary to draw if the shadow is enabled
- var startY = 0;
- var dataset = this.data[i];
-
- /**
- * Check for a negative bar width
- */
- if (barWidth < 0) {
- 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.');
- }
-
- for (j=0; j<dataset.length; ++j) {
-
- // Stacked bar chart and X axis pos in the middle - poitless since negative values are not permitted
- if (xaxispos == 'center') {
- alert("[BAR] It's pointless having the X axis position at the center on a stacked bar chart.");
- return;
- }
-
- // Negative values not permitted for the stacked chart
- if (this.data[i][j] < 0) {
- alert('[BAR] Negative values are not permitted with a stacked bar chart. Try a grouped one instead.');
- return;
- }
-
- /**
- * Set the fill and stroke colors
- */
- co.strokeStyle = strokeStyle
- co.fillStyle = colors[j];
-
- if (prop['chart.colors.reverse']) {
- co.fillStyle = colors[this.data[i].length - j - 1];
- }
-
- if (prop['chart.colors.sequential'] && colors[sequentialColorIndex]) {
- co.fillStyle = colors[sequentialColorIndex++];
- } else if (prop['chart.colors.sequential']) {
- co.fillStyle = colors[sequentialColorIndex - 1];
- }
-
- var height = (dataset[j] / this.scale2.max) * (ca.height - this.gutterTop - this.gutterBottom );
- // If the X axis pos is in the center, we need to half the height
- if (xaxispos == 'center') {
- height /= 2;
- }
-
- var totalHeight = (RGraph.array_sum(dataset) / this.scale2.max) * (ca.height - hmargin - this.gutterTop - this.gutterBottom);
-
- /**
- * Store the coords for tooltips
- */
- this.coords.push([x + hmargin, y, width - (2 * hmargin), height]);
- if (typeof this.coords2[i] == 'undefined') {
- this.coords2[i] = [];
- }
- this.coords2[i].push([x + hmargin, y, width - (2 * hmargin), height]);
-
- // MSIE shadow
- if (RGraph.ISOLD && shadow) {
- this.DrawIEShadow([x + hmargin, y, width - (2 * hmargin), height + 1]);
- }
-
- if (height > 0) {
- co.strokeRect(x + hmargin, y, width - (2 * hmargin), height);
- co.fillRect(x + hmargin, y, width - (2 * hmargin), height);
- }
-
- if (j == 0) {
- var startY = y;
- var startX = x;
- }
-
- /**
- * Store the redraw coords if the shadow is enabled
- */
- if (shadow) {
- redrawCoords.push([x + hmargin, y, width - (2 * hmargin), height, co.fillStyle]);
- }
-
- /**
- * Stacked 3D effect
- */
- if (variant == '3d') {
-
- var prevFillStyle = co.fillStyle;
- var prevStrokeStyle = co.strokeStyle;
-
-
- // Draw the top side
- if (j == 0) {
- co.beginPath();
- co.moveTo(startX + hmargin, y);
- co.lineTo(startX + 10 + hmargin, y - 5);
- co.lineTo(startX + 10 + barWidth + hmargin, y - 5);
- co.lineTo(startX + barWidth + hmargin, y);
- co.closePath();
-
- co.fill();
- co.stroke();
- }
-
- // Draw the side section
- co.beginPath();
- co.moveTo(startX + barWidth + hmargin, y);
- co.lineTo(startX + barWidth + hmargin + 10, y - 5);
- co.lineTo(startX + barWidth + hmargin + 10, y - 5 + height);
- co.lineTo(startX + barWidth + hmargin , y + height);
- co.closePath();
-
- co.fill();
- co.stroke();
-
- // Draw the darker top side
- if (j == 0) {
- co.fillStyle = 'rgba(255,255,255,0.3)';
- co.beginPath();
- co.moveTo(startX + hmargin, y);
- co.lineTo(startX + 10 + hmargin, y - 5);
- co.lineTo(startX + 10 + barWidth + hmargin, y - 5);
- co.lineTo(startX + barWidth + hmargin, y);
- co.closePath();
-
- co.fill();
- co.stroke();
- }
-
- // Draw the darker side section
- co.fillStyle = 'rgba(0,0,0,0.4)';
- co.beginPath();
- co.moveTo(startX + barWidth + hmargin, y);
- co.lineTo(startX + barWidth + hmargin + 10, y - 5);
- co.lineTo(startX + barWidth + hmargin + 10, y - 5 + height);
- co.lineTo(startX + barWidth + hmargin , y + height);
- co.closePath();
-
- co.fill();
- co.stroke();
-
- co.strokeStyle = prevStrokeStyle;
- co.fillStyle = prevFillStyle;
- }
-
- y += height;
- }
-
-
- /**
- * Redraw the bars if the shadow is enabled due to hem being drawn from the bottom up, and the
- * shadow spilling over to higher up bars
- */
- if (shadow) {
-
- RGraph.NoShadow(this);
-
- for (k=0; k<redrawCoords.length; ++k) {
- co.strokeStyle = strokeStyle;
- co.fillStyle = redrawCoords[k][4];
- co.strokeRect(redrawCoords[k][0], redrawCoords[k][1], redrawCoords[k][2], redrawCoords[k][3]);
- co.fillRect(redrawCoords[k][0], redrawCoords[k][1], redrawCoords[k][2], redrawCoords[k][3]);
-
- co.stroke();
- co.fill();
- }
-
- // Reset the redraw coords to be empty
- redrawCoords = [];
- }
- /**
- * Grouped bar
- */
- } else if (this.data[i] && typeof(this.data[i]) == 'object' && prop['chart.grouping'] == 'grouped') {
-
- var redrawCoords = [];
- co.lineWidth = prop['chart.linewidth'];
-
- for (j=0; j<this.data[i].length; ++j) {
-
- // Set the fill and stroke colors
- co.strokeStyle = strokeStyle;
- co.fillStyle = colors[j];
-
- /**
- * Sequential colors
- */
- if (prop['chart.colors.sequential'] && colors[sequentialColorIndex]) {
- co.fillStyle = colors[sequentialColorIndex++];
- } else if (prop['chart.colors.sequential']) {
- co.fillStyle = colors[sequentialColorIndex - 1];
- }
-
- var individualBarWidth = (width - (2 * hmargin)) / this.data[i].length;
- 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 );
- var groupedMargin = prop['chart.hmargin.grouped'];
- var startX = x + hmargin + (j * individualBarWidth);
-
- /**
- * Check for a negative bar width
- */
- if (individualBarWidth < 0) {
- 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.');
- }
-
- // If the X axis pos is in the center, we need to half the height
- if (xaxispos == 'center') {
- height /= 2;
- }
-
- /**
- * Determine the start positioning for the bar
- */
- if (xaxispos == 'top') {
- var startY = this.gutterTop;
- var height = Math.abs(height);
-
- } else if (xaxispos == 'center') {
- var startY = this.gutterTop + (this.grapharea / 2) - height;
-
- } else {
- var startY = ca.height - this.gutterBottom - height;
- var height = Math.abs(height);
- }
-
- /**
- * Draw MSIE shadow
- */
- if (RGraph.ISOLD && shadow) {
- this.DrawIEShadow([startX, startY, individualBarWidth, height]);
- }
-
- co.strokeRect(startX + groupedMargin, startY, individualBarWidth - (2 * groupedMargin), height);
- co.fillRect(startX + groupedMargin, startY, individualBarWidth - (2 * groupedMargin), height);
- y += height;
-
-
-
- /**
- * Grouped 3D effect
- */
- if (variant == '3d') {
- var prevFillStyle = co.fillStyle;
- var prevStrokeStyle = co.strokeStyle;
-
- // Draw the top side
- co.beginPath();
- co.moveTo(startX, startY);
- co.lineTo(startX + 10, startY - 5);
- co.lineTo(startX + 10 + individualBarWidth, startY - 5);
- co.lineTo(startX + individualBarWidth, startY);
- co.closePath();
-
- co.fill();
- co.stroke();
-
- // Draw the side section
- co.beginPath();
- co.moveTo(startX + individualBarWidth, startY);
- co.lineTo(startX + individualBarWidth + 10, startY - 5);
- co.lineTo(startX + individualBarWidth + 10, startY - 5 + height);
- co.lineTo(startX + individualBarWidth , startY + height);
- co.closePath();
-
- co.fill();
- co.stroke();
-
-
- // Draw the darker top side
- co.fillStyle = 'rgba(255,255,255,0.3)';
- co.beginPath();
- co.moveTo(startX, startY);
- co.lineTo(startX + 10, startY - 5);
- co.lineTo(startX + 10 + individualBarWidth, startY - 5);
- co.lineTo(startX + individualBarWidth, startY);
- co.closePath();
-
- co.fill();
- co.stroke();
-
- // Draw the darker side section
- co.fillStyle = 'rgba(0,0,0,0.4)';
- co.beginPath();
- co.moveTo(startX + individualBarWidth, startY);
- co.lineTo(startX + individualBarWidth + 10, startY - 5);
- co.lineTo(startX + individualBarWidth + 10, startY - 5 + height);
- co.lineTo(startX + individualBarWidth , startY + height);
- co.closePath();
-
- co.fill();
- co.stroke();
-
- co.strokeStyle = prevStrokeStyle;
- co.fillStyle = prevFillStyle;
- }
-
- if (height < 0) {
- height = Math.abs(height);
- startY = startY - height;
- }
-
- this.coords.push([startX + groupedMargin, startY, individualBarWidth - (2 * groupedMargin), height]);
- if (typeof this.coords2[i] == 'undefined') {
- this.coords2[i] = [];
- }
- this.coords2[i].push([startX + groupedMargin, startY, individualBarWidth - (2 * groupedMargin), height]);
-
- // Facilitate shadows going to the left
- if (prop['chart.shadow']) {
- redrawCoords.push([startX + groupedMargin, startY, individualBarWidth - (2 * groupedMargin), height, co.fillStyle]);
- }
- }
- /**
- * Redraw the bar if shadows are going to the left
- */
- if (redrawCoords.length) {
-
- RGraph.NoShadow(this);
-
- co.lineWidth = prop['chart.linewidth'];
-
- co.beginPath();
- for (var j=0; j<redrawCoords.length; ++j) {
-
- co.fillStyle = redrawCoords[j][4];
- co.strokeStyle = prop['chart.strokecolor'];
-
- co.fillRect(redrawCoords[j][0], redrawCoords[j][1], redrawCoords[j][2], redrawCoords[j][3]);
- co.strokeRect(redrawCoords[j][0], redrawCoords[j][1], redrawCoords[j][2], redrawCoords[j][3]);
- }
- co.fill();
- co.stroke();
-
- redrawCoords = [];
- }
- } else {
- this.coords.push([]);
- }
-
- co.closePath();
- }
-
- /**
- * Turn off any shadow
- */
- RGraph.NoShadow(this);
- };
-
-
-
- /**
- * Draws the labels for the graph
- */
- this.drawLabels =
- this.DrawLabels = function ()
- {
- // Variable "caching" so the context can be accessed as a local variable
- // TODO Doesn't really need to be done as the variables come from the constructor
- var ca = this.canvas;
- var co = this.context;
- var prop = this.properties;
- var context = co;
-
- var text_angle = prop['chart.text.angle'];
- var text_size = prop['chart.text.size'];
- var labels = prop['chart.labels'];
-
-
- // Draw the Y axis labels:
- if (prop['chart.ylabels']) {
- if (prop['chart.xaxispos'] == 'top') this.Drawlabels_top();
- if (prop['chart.xaxispos'] == 'center') this.Drawlabels_center();
- if (prop['chart.xaxispos'] == 'bottom') this.Drawlabels_bottom();
- }
-
- /**
- * The X axis labels
- */
- if (typeof(labels) == 'object' && labels) {
-
- var yOffset = Number(prop['chart.xlabels.offset']);
-
- /**
- * Text angle
- */
- if (prop['chart.text.angle'] != 0) {
- var valign = 'center';
- var halign = 'right';
- var angle = 0 - prop['chart.text.angle'];
- } else {
- var valign = 'top';
- var halign = 'center';
- var angle = 0;
- }
-
- // Draw the X axis labels
- co.fillStyle = prop['chart.text.color'];
-
- // How wide is each bar
- var barWidth = (ca.width - this.gutterRight - this.gutterLeft) / labels.length;
-
- // Reset the xTickGap
- xTickGap = (ca.width - this.gutterRight - this.gutterLeft) / labels.length
-
- // Draw the X tickmarks
- var i=0;
- var font = prop['chart.text.font'];
-
- for (x=this.gutterLeft + (xTickGap / 2); x<=ca.width - this.gutterRight; x+=xTickGap) {
-
- RGraph.Text2(this, {'font': font,
- 'size': text_size,
- 'x': x,
- 'y': prop['chart.xaxispos'] == 'top' ? this.gutterTop - yOffset - 5: (ca.height - this.gutterBottom) + yOffset + 3,
- 'text': String(labels[i++]),
- 'valign': prop['chart.xaxispos'] == 'top' ? 'bottom' : valign,
- 'halign': halign,
- 'tag':'label',
- 'marker':false,
- 'angle':angle,
- 'tag': 'labels'
- });
- }
- }
-
- /**
- * Draw above labels
- */
- this.drawAboveLabels();
- };
-
-
-
- /**
- * Draws the X axis at the top
- */
- this.drawlabels_top =
- this.Drawlabels_top = function ()
- {
- var ca = this.canvas;
- var co = this.context;
- var prop = this.properties;
-
- co.beginPath();
- co.fillStyle = prop['chart.text.color'];
- co.strokeStyle = 'black';
-
- if (prop['chart.xaxispos'] == 'top') {
-
- var context = co;
- var text_size = prop['chart.text.size'];
- var units_pre = prop['chart.units.pre'];
- var units_post = prop['chart.units.post'];
- var align = prop['chart.yaxispos'] == 'left' ? 'right' : 'left';
- var font = prop['chart.text.font'];
- var numYLabels = prop['chart.ylabels.count'];
- var ymin = prop['chart.ymin'];
-
- if (prop['chart.ylabels.inside'] == true) {
- var xpos = prop['chart.yaxispos'] == 'left' ? this.gutterLeft + 5 : ca.width - this.gutterRight - 5;
- var align = prop['chart.yaxispos'] == 'left' ? 'left' : 'right';
- var boxed = true;
- } else {
- var xpos = prop['chart.yaxispos'] == 'left' ? this.gutterLeft - 5 : ca.width - this.gutterRight + 5;
- var boxed = false;
- }
-
- /**
- * Draw specific Y labels here so that the local variables can be reused
- */
- if (typeof(prop['chart.ylabels.specific']) == 'object' && prop['chart.ylabels.specific']) {
-
- var labels = RGraph.array_reverse(prop['chart.ylabels.specific']);
- var grapharea = ca.height - this.gutterTop - this.gutterBottom;
-
- for (var i=0; i<labels.length; ++i) {
-
- var y = this.gutterTop + (grapharea * (i / labels.length)) + (grapharea / labels.length);
-
- RGraph.Text2(this, {'font': font,
- 'size': text_size,
- 'x': xpos,
- 'y': y,
- 'text': String(labels[i]),
- 'valign': 'center',
- 'halign': align,
- 'bordered':boxed,
- 'tag': 'scale'
- });
- }
-
- return;
- }
-
-
-
-
-
-
-
- /**
- * Draw the scale
- */
- var labels = this.scale2.labels;
- for (var i=0; i<labels.length; ++i) {
- RGraph.Text2(this, {'font': font,
- 'size':text_size,
- 'x':xpos,
- 'y':this.gutterTop + ((this.grapharea / labels.length) * (i + 1)),
- 'text': '-' + labels[i],
- 'valign': 'center',
- 'halign': align,
- 'bordered': boxed,
- 'tag':'scale'});
- }
-
-
-
-
-
-
-
-
- /**
- * Show the minimum value if its not zero
- */
- if (prop['chart.ymin'] != 0 || prop['chart.noxaxis'] || prop['chart.scale.zerostart']) {
-
- RGraph.Text2(this, {'font': font,
- 'size': text_size,
- 'x': xpos,
- 'y': this.gutterTop,
- 'text': (this.scale2.min != 0 ? '-' : '') + RGraph.number_format(this,(this.scale2.min.toFixed((prop['chart.scale.decimals']))), units_pre, units_post),
- 'valign': 'center',
- 'halign': align,
- 'bordered': boxed,
- 'tag': 'scale'});
- }
-
- }
-
- co.fill();
- };
-
-
-
- /**
- * Draws the X axis in the middle
- */
- this.drawlabels_center =
- this.Drawlabels_center = function ()
- {
- var ca = this.canvas;
- var co = this.context;
- var prop = this.properties;
-
- var font = prop['chart.text.font'];
- var numYLabels = prop['chart.ylabels.count'];
-
- co.fillStyle = prop['chart.text.color'];
-
- if (prop['chart.xaxispos'] == 'center') {
-
- /**
- * Draw the top labels
- */
- var text_size = prop['chart.text.size'];
- var units_pre = prop['chart.units.pre'];
- var units_post = prop['chart.units.post'];
- var context = co;
- var align = '';
- var xpos = 0;
- var boxed = false;
- var ymin = prop['chart.ymin'];
-
- co.fillStyle = prop['chart.text.color'];
- co.strokeStyle = 'black';
-
- if (prop['chart.ylabels.inside'] == true) {
- var xpos = prop['chart.yaxispos'] == 'left' ? this.gutterLeft + 5 : ca.width - this.gutterRight - 5;
- var align = prop['chart.yaxispos'] == 'left' ? 'left' : 'right';
- var boxed = true;
- } else {
- var xpos = prop['chart.yaxispos'] == 'left' ? this.gutterLeft - 5 : ca.width - this.gutterRight + 5;
- var align = prop['chart.yaxispos'] == 'left' ? 'right' : 'left';
- var boxed = false;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- /**
- * Draw specific Y labels here so that the local variables can be reused
- */
- if (typeof(prop['chart.ylabels.specific']) == 'object' && prop['chart.ylabels.specific']) {
-
- var labels = prop['chart.ylabels.specific'];
- var grapharea = ca.height - this.gutterTop - this.gutterBottom;
-
- // Draw the top halves labels
- for (var i=0; i<labels.length; ++i) {
-
- var y = this.gutterTop + ((grapharea / 2) / (labels.length - 1)) * i;
-
- RGraph.Text2(this, {'font':font,
- 'size':text_size,
- 'x':xpos,
- 'y':y,
- 'text':String(labels[i]),
- 'valign':'center',
- 'halign':align,
- 'bordered':boxed,
- 'tag': 'scale'
- });
- }
-
- // Draw the bottom halves labels
- for (var i=labels.length-1; i>=1; --i) {
-
- var y = this.gutterTop + (grapharea * (i / ((labels.length - 1) * 2) )) + (grapharea / 2);
-
- RG.Text2(this, {'font':font,
- 'size':text_size,
- 'x':xpos,
- 'y':y,
- 'text':String(labels[labels.length - i - 1]),
- 'valign':'center',
- 'halign':align,
- 'bordered':boxed,
- 'tag': 'scale'
- });
- }
-
- return;
- }
-
-
-
-
-
-
-
-
-
-
- /**
- * Draw the top halfs labels
- */
- for (var i=0; i<this.scale2.labels.length; ++i) {
- var y = this.gutterTop + this.halfgrapharea - ((this.halfgrapharea / numYLabels) * (i + 1));
- var text = this.scale2.labels[i];
- RG.Text2(this, {'font':font, 'size':text_size, 'x':xpos, 'y':y, 'text': text, 'valign':'center', 'halign': align, 'bordered': boxed, 'tag':'scale'});
- }
-
- /**
- * Draw the bottom halfs labels
- */
- for (var i=(this.scale2.labels.length - 1); i>=0; --i) {
- var y = this.gutterTop + ((this.halfgrapharea / numYLabels) * (i + 1)) + this.halfgrapharea;
- var text = this.scale2.labels[i];
- RG.Text2(this, {'font':font, 'size':text_size,'x':xpos,'y':y,'text': '-' + text,'valign':'center','halign': align,'bordered': boxed,'tag':'scale'});
- }
-
-
-
-
-
- /**
- * Show the minimum value if its not zero
- */
- if (this.scale2.min != 0 || prop['chart.scale.zerostart']) {
- 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'});
- }
- }
- };
-
-
-
-
- /**
- * Draws the X axdis at the bottom (the default)
- */
- this.drawlabels_bottom =
- this.Drawlabels_bottom = function ()
- {
- var co = this.context;
- var ca = this.canvas;
- var prop = this.properties;
-
- var text_size = prop['chart.text.size'];
- var units_pre = prop['chart.units.pre'];
- var units_post = prop['chart.units.post'];
- var context = this.context;
- var align = prop['chart.yaxispos'] == 'left' ? 'right' : 'left';
- var font = prop['chart.text.font'];
- var numYLabels = prop['chart.ylabels.count'];
- var ymin = prop['chart.ymin'];
-
- co.beginPath();
- co.fillStyle = prop['chart.text.color'];
- co.strokeStyle = 'black';
-
- if (prop['chart.ylabels.inside'] == true) {
- var xpos = prop['chart.yaxispos'] == 'left' ? this.gutterLeft + 5 : ca.width - this.gutterRight - 5;
- var align = prop['chart.yaxispos'] == 'left' ? 'left' : 'right';
- var boxed = true;
- } else {
- var xpos = prop['chart.yaxispos'] == 'left' ? this.gutterLeft - 5 : ca.width - this.gutterRight + 5;
- var boxed = false;
- }
-
- /**
- * Draw specific Y labels here so that the local variables can be reused
- */
- if (prop['chart.ylabels.specific'] && typeof(prop['chart.ylabels.specific']) == 'object') {
-
- var labels = prop['chart.ylabels.specific'];
- var grapharea = ca.height - this.gutterTop - this.gutterBottom;
-
- for (var i=0; i<labels.length; ++i) {
- var y = this.gutterTop + (grapharea * (i / (labels.length - 1)));
-
- RGraph.Text2(this, {'font':font,
- 'size':text_size,
- 'x':xpos,
- 'y':y,
- 'text': labels[i],
- 'valign':'center',
- 'halign': align,
- 'bordered': boxed,
- 'tag':'scale'
- });
- }
-
- return;
- }
-
- var gutterTop = this.gutterTop;
- var halfTextHeight = this.halfTextHeight;
- var scale = this.scale;
-
-
- for (var i=0; i<numYLabels; ++i) {
- var text = this.scale2.labels[i];
- RGraph.Text2(this, {'font':font,
- 'size':text_size,
- 'x':xpos,
- 'y':this.gutterTop + this.grapharea - ((this.grapharea / numYLabels) * (i+1)),
- 'text': text,
- 'valign':'center',
- 'halign': align,
- 'bordered': boxed,
- 'tag':'scale'});
- }
-
-
- /**
- * Show the minimum value if its not zero
- */
- if (prop['chart.ymin'] != 0 || prop['chart.noxaxis'] || prop['chart.scale.zerostart']) {
- RG.Text2(this, {'font':font,
- 'size':text_size,
- 'x':xpos,
- 'y':ca.height - this.gutterBottom,
- 'text': RG.number_format(this,(this.scale2.min.toFixed((prop['chart.scale.decimals']))), units_pre, units_post),
- 'valign':'center',
- 'halign': align,
- 'bordered': boxed,
- 'tag':'scale'});
- }
-
- co.fill();
- };
-
-
- /**
- * This function is used by MSIE only to manually draw the shadow
- *
- * @param array coords The coords for the bar
- */
- this.drawIEShadow =
- this.DrawIEShadow = function (coords)
- {
- var co = this.context;
- var ca = this.canvas;
- var prop = this.properties;
-
- var prevFillStyle = co.fillStyle;
- var offsetx = prop['chart.shadow.offsetx'];
- var offsety = prop['chart.shadow.offsety'];
-
- co.lineWidth = prop['chart.linewidth'];
- co.fillStyle = prop['chart.shadow.color'];
- co.beginPath();
-
- // Draw shadow here
- co.fillRect(coords[0] + offsetx, coords[1] + offsety, coords[2], coords[3]);
-
- co.fill();
-
- // Change the fillstyle back to what it was
- co.fillStyle = prevFillStyle;
- };
-
-
- /**
- * Not used by the class during creating the graph, but is used by event handlers
- * to get the coordinates (if any) of the selected bar
- *
- * @param object e The event object
- * @param object OPTIONAL You can pass in the bar object instead of the
- * function using "this"
- */
- this.getShape =
- this.getBar = function (e)
- {
- // This facilitates you being able to pass in the bar object as a parameter instead of
- // the function getting it from itself
- var obj = arguments[1] ? arguments[1] : this;
-
- var mouseXY = RGraph.getMouseXY(e);
- var mouseX = mouseXY[0];
- var mouseY = mouseXY[1];
- var canvas = obj.canvas;
- var context = obj.context;
- var coords = obj.coords
- for (var i=0,len=coords.length; i<len; i+=1) {
-
- if (obj.coords[i].length == 0) {
- continue;
- }
- var left = coords[i][0];
- var top = coords[i][1];
- var width = coords[i][2];
- var height = coords[i][3];
- var prop = obj.properties;
-
- if (mouseX >= left && mouseX <= (left + width) && mouseY >= top && mouseY <= (top + height)) {
- if (prop['chart.tooltips']) {
- var tooltip = RGraph.parseTooltipText ? RGraph.parseTooltipText(prop['chart.tooltips'], i) : prop['chart.tooltips'][i];
- }
-
- // Work out the dataset
- var dataset = 0;
- var idx = i;
- while (idx >= (typeof(obj.data[dataset]) == 'object' && obj.data[dataset] ? obj.data[dataset].length : 1)) {
- if (typeof(obj.data[dataset]) == 'number') {
- idx -= 1;
- } else if (obj.data[dataset]) { // Accounts for null being an object
- idx -= obj.data[dataset].length;
- } else {
- idx -= 1;
- }
- dataset++;
- }
- if (typeof(obj.data[dataset]) == 'number') {
- idx = null;
- }
-
-
- return {
- 0: obj, 1: left, 2: top, 3: width, 4: height, 5: i,
- 'object': obj, 'x': left, 'y': top, 'width': width, 'height': height, 'index': i, 'tooltip': tooltip, 'index_adjusted': idx, 'dataset': dataset
- };
- }
- }
-
- return null;
- };
- /**
- * This retrives the bar based on the X coordinate only.
- *
- * @param object e The event object
- * @param object OPTIONAL You can pass in the bar object instead of the
- * function using "this"
- */
- this.getShapeByX = function (e)
- {
- var canvas = e.target;
- var mouseCoords = RGraph.getMouseXY(e);
-
-
- // This facilitates you being able to pass in the bar object as a parameter instead of
- // the function getting it from itself
- var obj = arguments[1] ? arguments[1] : this;
-
-
- /**
- * Loop through the bars determining if the mouse is over a bar
- */
- for (var i=0,len=obj.coords.length; i<len; i++) {
- if (obj.coords[i].length == 0) {
- continue;
- }
- var mouseX = mouseCoords[0];
- var mouseY = mouseCoords[1];
- var left = obj.coords[i][0];
- var top = obj.coords[i][1];
- var width = obj.coords[i][2];
- var height = obj.coords[i][3];
- var prop = obj.properties;
-
- if (mouseX >= left && mouseX <= (left + width)) {
-
- if (prop['chart.tooltips']) {
- var tooltip = RGraph.parseTooltipText ? RGraph.parseTooltipText(prop['chart.tooltips'], i) : prop['chart.tooltips'][i];
- }
-
-
- return {
- 0: obj, 1: left, 2: top, 3: width, 4: height, 5: i,
- 'object': obj, 'x': left, 'y': top, 'width': width, 'height': height, 'index': i, 'tooltip': tooltip
- };
- }
- }
-
- return null;
- };
-
-
- /**
- * When you click on the chart, this method can return the Y value at that point. It works for any point on the
- * chart (that is inside the gutters) - not just points within the Bars.
- *
- * EITHER:
- *
- * @param object arg The event object
- *
- * OR:
- *
- * @param object arg A two element array containing the X and Y coordinates
- */
- this.getValue = function (arg)
- {
- var co = this.context;
- var ca = this.canvas;
- var prop = this.properties;
-
- if (arg.length == 2) {
- var mouseX = arg[0];
- var mouseY = arg[1];
- } else {
- var mouseCoords = RGraph.getMouseXY(arg);
- var mouseX = mouseCoords[0];
- var mouseY = mouseCoords[1];
- }
-
- if ( mouseY < prop['chart.gutter.top']
- || mouseY > (ca.height - prop['chart.gutter.bottom'])
- || mouseX < prop['chart.gutter.left']
- || mouseX > (ca.width - prop['chart.gutter.right'])
- ) {
- return null;
- }
-
- if (prop['chart.xaxispos'] == 'center') {
- var value = (((this.grapharea / 2) - (mouseY - prop['chart.gutter.top'])) / this.grapharea) * (this.scale2.max - this.scale2.min)
- value *= 2;
-
- if (value >= 0) {
- value += this.scale2.min;
- } else {
- value -= this.scale2.min;
- }
-
- } else if (prop['chart.xaxispos'] == 'top') {
- var value = ((this.grapharea - (mouseY - prop['chart.gutter.top'])) / this.grapharea) * (this.scale2.max - this.scale2.min)
- value = this.scale2.max - value;
- value = Math.abs(value) * -1;
- } else {
- var value = ((this.grapharea - (mouseY - prop['chart.gutter.top'])) / this.grapharea) * (this.scale2.max - this.scale2.min)
- value += this.scale2.min;
- }
-
- return value;
- };
-
-
- /**
- * This function can be used when the canvas is clicked on (or similar - depending on the event)
- * to retrieve the relevant Y coordinate for a particular value.
- *
- * @param int value The value to get the Y coordinate for
- */
- this.getYCoord = function (value)
- {
- if (value > this.scale2.max) {
- return null;
- }
-
- var co = this.context;
- var ca = this.canvas;
- var prop = this.properties;
-
- var y;
- var xaxispos = prop['chart.xaxispos'];
-
- if (xaxispos == 'top') {
-
- // Account for negative numbers
- if (value < 0) {
- value = Math.abs(value);
- }
-
- y = ((value - this.scale2.min) / (this.scale2.max - this.scale2.min)) * this.grapharea;
- y = y + this.gutterTop
-
- } else if (xaxispos == 'center') {
-
- y = ((value - this.scale2.min) / (this.scale2.max - this.scale2.min)) * (this.grapharea / 2);
- y = (this.grapharea / 2) - y;
- y += this.gutterTop;
-
- } else {
-
- if (value < this.scale2.min) {
- value = this.scale2.min;
- }
-
- y = ((value - this.scale2.min) / (this.scale2.max - this.scale2.min)) * this.grapharea;
-
- y = ca.height - this.gutterBottom - y;
- }
-
- return y;
- };
-
-
-
- /**
- * Each object type has its own Highlight() function which highlights the appropriate shape
- *
- * @param object shape The shape to highlight
- */
- this.highlight =
- this.Highlight = function (shape)
- {
- // Add the new highlight
- RGraph.Highlight.Rect(this, shape);
- };
-
-
-
- /**
- * The getObjectByXY() worker method
- */
- this.getObjectByXY = function (e)
- {
- var ca = this.canvas;
- var prop = this.properties;
-
- var mouseXY = RGraph.getMouseXY(e);
-
- if (
- mouseXY[0] >= prop['chart.gutter.left']
- && mouseXY[0] <= (ca.width - prop['chart.gutter.right'])
- && mouseXY[1] >= prop['chart.gutter.top']
- && mouseXY[1] <= (ca.height - prop['chart.gutter.bottom'])
- ) {
-
- return this;
- }
- };
- /**
- * This method handles the adjusting calculation for when the mouse is moved
- *
- * @param object e The event object
- */
- this.adjusting_mousemove =
- this.Adjusting_mousemove = function (e)
- {
- /**
- * Handle adjusting for the Bar
- */
- if (prop['chart.adjustable'] && RG.Registry.Get('chart.adjusting') && RG.Registry.Get('chart.adjusting').uid == this.uid) {
-
- // Rounding the value to the given number of decimals make the chart step
- var value = Number(this.getValue(e));
- var shape = this.getShapeByX(e);
- if (shape) {
- RG.Registry.Set('chart.adjusting.shape', shape);
- if (this.stackedOrGrouped && prop['chart.grouping'] == 'grouped') {
- var indexes = RG.sequentialIndexToGrouped(shape['index'], this.data);
- if (typeof this.data[indexes[0]] == 'number') {
- this.data[indexes[0]] = Number(value);
- } else if (!RG.is_null(this.data[indexes[0]])) {
- this.data[indexes[0]][indexes[1]] = Number(value);
- }
- } else if (typeof this.data[shape['index']] == 'number') {
- this.data[shape['index']] = Number(value);
- }
-
- RG.redrawCanvas(e.target);
- RG.fireCustomEvent(this, 'onadjust');
- }
- }
- };
- /**
- * This function positions a tooltip when it is displayed
- *
- * @param obj object The chart object
- * @param int x The X coordinate specified for the tooltip
- * @param int y The Y coordinate specified for the tooltip
- * @param objec tooltip The tooltips DIV element
- */
- this.positionTooltip = function (obj, x, y, tooltip, idx)
- {
- var prop = obj.properties;
- var coordX = obj.coords[tooltip.__index__][0];
- var coordY = obj.coords[tooltip.__index__][1];
- var coordW = obj.coords[tooltip.__index__][2];
- var coordH = obj.coords[tooltip.__index__][3];
- var canvasXY = RGraph.getCanvasXY(obj.canvas);
- var gutterLeft = prop['chart.gutter.left'];
- var gutterTop = prop['chart.gutter.top'];
- var width = tooltip.offsetWidth;
- var height = tooltip.offsetHeight;
- var value = obj.data_arr[tooltip.__index__];
-
-
- // Set the top position
- tooltip.style.left = 0;
- tooltip.style.top = canvasXY[1] + coordY - height - 7 + 'px';
-
- /**
- * If the tooltip is for a negative value - position it underneath the bar
- */
- if (value < 0) {
- tooltip.style.top = canvasXY[1] + coordY + coordH + 7 + 'px';
- }
-
-
- // By default any overflow is hidden
- tooltip.style.overflow = '';
-
- // Inverted arrow
- // data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAAFCAMAAACkeOZkAAAAK3RFWHRDcmVhdGlvbiBUaW1lAFNhdCA2IE9jdCAyMDEyIDEyOjQ5OjMyIC0wMDAw2S1RlgAAAAd0SU1FB9wKBgszM4Ed2k4AAAAJcEhZcwAACxIAAAsSAdLdfvwAAAAEZ0FNQQAAsY8L/GEFAAAACVBMVEX/AAC9vb3//+92Pom0AAAAAXRSTlMAQObYZgAAAB1JREFUeNpjYAABRgY4YGRiRDCZYBwQE8qBMEEcAANCACqByy1sAAAAAElFTkSuQmCC
-
- // The arrow
- var img = new Image();
- img.style.position = 'absolute';
- img.id = '__rgraph_tooltip_pointer__';
- if (value >= 0) {
- img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABEAAAAFCAYAAACjKgd3AAAARUlEQVQYV2NkQAN79+797+RkhC4M5+/bd47B2dmZEVkBCgcmgcsgbAaA9GA1BCSBbhAuA/AagmwQPgMIGgIzCD0M0AMMAEFVIAa6UQgcAAAAAElFTkSuQmCC';
- img.style.top = (tooltip.offsetHeight - 2) + 'px';
- } else {
- img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAAFCAMAAACkeOZkAAAAK3RFWHRDcmVhdGlvbiBUaW1lAFNhdCA2IE9jdCAyMDEyIDEyOjQ5OjMyIC0wMDAw2S1RlgAAAAd0SU1FB9wKBgszM4Ed2k4AAAAJcEhZcwAACxIAAAsSAdLdfvwAAAAEZ0FNQQAAsY8L/GEFAAAACVBMVEX/AAC9vb3//+92Pom0AAAAAXRSTlMAQObYZgAAAB1JREFUeNpjYAABRgY4YGRiRDCZYBwQE8qBMEEcAANCACqByy1sAAAAAElFTkSuQmCC';
- img.style.top = '-5px';
- }
-
- tooltip.appendChild(img);
-
- // Reposition the tooltip if at the edges:
-
- // LEFT edge
- if ((canvasXY[0] + coordX + (coordW / 2) - (width / 2)) < 10) {
- tooltip.style.left = (canvasXY[0] + coordX - (width * 0.1)) + (coordW / 2) + 'px';
- img.style.left = ((width * 0.1) - 8.5) + 'px';
-
- // RIGHT edge
- } else if ((canvasXY[0] + coordX + (width / 2)) > doc.body.offsetWidth) {
- tooltip.style.left = canvasXY[0] + coordX - (width * 0.9) + (coordW / 2) + 'px';
- img.style.left = ((width * 0.9) - 8.5) + 'px';
-
- // Default positioning - CENTERED
- } else {
- tooltip.style.left = (canvasXY[0] + coordX + (coordW / 2) - (width * 0.5)) + 'px';
- img.style.left = ((width * 0.5) - 8.5) + 'px';
- }
- };
- /**
- * This allows for easy specification of gradients
- */
- this.parseColors = function ()
- {
- // Save the original colors so that they can be restored when the canvas is reset
- if (this.original_colors.length === 0) {
- this.original_colors['chart.colors'] = RGraph.array_clone(prop['chart.colors']);
- this.original_colors['chart.key.colors'] = RGraph.array_clone(prop['chart.key.colors']);
- this.original_colors['chart.crosshairs.color'] = prop['chart.crosshairs.color'];
- this.original_colors['chart.highlight.stroke'] = prop['chart.highlight.stroke'];
- this.original_colors['chart.highlight.fill'] = prop['chart.highlight.fill'];
- this.original_colors['chart.text.color'] = prop['chart.text.color'];
- this.original_colors['chart.background.barcolor1'] = prop['chart.background.barcolor1'];
- this.original_colors['chart.background.barcolor2'] = prop['chart.background.barcolor2'];
- this.original_colors['chart.background.grid.color'] = prop['chart.background.grid.color'];
- this.original_colors['chart.background.color'] = prop['chart.background.color'];
- this.original_colors['chart.strokecolor'] = prop['chart.strokecolor'];
- this.original_colors['chart.axis.color'] = prop['chart.axis.color'];
- }
-
-
- // chart.colors
- var colors = prop['chart.colors'];
- if (colors) {
- for (var i=0; i<colors.length; ++i) {
- colors[i] = this.parseSingleColorForGradient(colors[i]);
- }
- }
-
- // chart.key.colors
- var colors = prop['chart.key.colors'];
- if (colors) {
- for (var i=0; i<colors.length; ++i) {
- colors[i] = this.parseSingleColorForGradient(colors[i]);
- }
- }
-
- prop['chart.crosshairs.color'] = this.parseSingleColorForGradient(prop['chart.crosshairs.color']);
- prop['chart.highlight.stroke'] = this.parseSingleColorForGradient(prop['chart.highlight.stroke']);
- prop['chart.highlight.fill'] = this.parseSingleColorForGradient(prop['chart.highlight.fill']);
- prop['chart.text.color'] = this.parseSingleColorForGradient(prop['chart.text.color']);
- prop['chart.background.barcolor1'] = this.parseSingleColorForGradient(prop['chart.background.barcolor1']);
- prop['chart.background.barcolor2'] = this.parseSingleColorForGradient(prop['chart.background.barcolor2']);
- prop['chart.background.grid.color'] = this.parseSingleColorForGradient(prop['chart.background.grid.color']);
- prop['chart.background.color'] = this.parseSingleColorForGradient(prop['chart.background.color']);
- prop['chart.strokecolor'] = this.parseSingleColorForGradient(prop['chart.strokecolor']);
- prop['chart.axis.color'] = this.parseSingleColorForGradient(prop['chart.axis.color']);
- };
-
-
-
- /**
- * This parses a single color value
- */
- this.parseSingleColorForGradient = function (color)
- {
- if (!color || typeof(color) != 'string') {
- return color;
- }
-
- if (color.match(/^gradient\((.*)\)$/i)) {
-
- var parts = RegExp.$1.split(':');
-
- // Create the gradient
- var grad = co.createLinearGradient(0,ca.height - prop['chart.gutter.bottom'], 0, prop['chart.gutter.top']);
-
- var diff = 1 / (parts.length - 1);
-
- grad.addColorStop(0, RG.trim(parts[0]));
-
- for (var j=1,len=parts.length; j<len; ++j) {
- grad.addColorStop(j * diff, RGraph.trim(parts[j]));
- }
- }
-
- return grad ? grad : color;
- };
-
-
-
- this.drawBevel =
- this.DrawBevel = function ()
- {
- var coords = this.coords;
- var coords2 = this.coords2;
-
- var prop = this.properties;
- var co = this.context;
- var ca = this.canvas;
-
- if (prop['chart.grouping'] == 'stacked') {
- for (var i=0; i<coords2.length; ++i) {
- if (coords2[i] && coords2[i][0] && coords2[i][0][0]) {
-
- var x = coords2[i][0][0];
- var y = coords2[i][0][1];
- var w = coords2[i][0][2];
-
- var arr = [];
- for (var j=0; j<coords2[i].length; ++j) {
- arr.push(coords2[i][j][3]);
- }
- var h = RGraph.array_sum(arr);
-
-
- co.save();
-
- co.strokeStyle = 'black';
-
- // Clip to the rect
- co.beginPath();
- co.rect(x, y, w, h);
- co.clip();
-
- // Add the shadow
- co.shadowColor = 'black';
- co.shadowOffsetX = 0;
- co.shadowOffsetY = 0;
- co.shadowBlur = 20;
-
- co.beginPath();
- co.rect(x - 3, y - 3, w + 6, h + 100);
- co.lineWidth = 5;
- co.stroke();
- co.restore();
- }
- }
- } else {
-
- for (var i=0; i<coords.length; ++i) {
- if (coords[i]) {
-
- var x = coords[i][0];
- var y = coords[i][1];
- var w = coords[i][2];
- var h = coords[i][3];
-
- var xaxispos = prop['chart.xaxispos'];
- var xaxis_ycoord = ((ca.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop;
-
-
- co.save();
-
- co.strokeStyle = 'black';
-
- // Clip to the rect
- co.beginPath();
- co.rect(x, y, w, h);
-
- co.clip();
-
- // Add the shadow
- co.shadowColor = 'black';
- co.shadowOffsetX = 0;
- co.shadowOffsetY = 0;
- co.shadowBlur = 20;
-
- if (xaxispos == 'top' || (xaxispos == 'center' && (y + h) > xaxis_ycoord)) {
- y = y - 100;
- h = h + 100;
- } else {
- y = y;
- h = h + 100;
- }
-
- co.beginPath();
- co.rect(x - 3, y - 3, w + 6, h + 6);
- co.lineWidth = 5;
- co.stroke();
- co.restore();
- }
- }
- }
- };
- /**
- * This function handles highlighting an entire data-series for the interactive
- * key
- *
- * @param int index The index of the data series to be highlighted
- */
- this.interactiveKeyHighlight = function (index)
- {
- this.coords2.forEach(function (value, idx, arr)
- {
- if (typeof value[index] == 'object' && value[index]) {
- var x = value[index][0]
- var y = value[index][1]
- var w = value[index][2]
- var h = value[index][3]
-
- co.fillStyle = prop['chart.key.interactive.highlight.chart.fill'];
- co.strokeStyle = prop['chart.key.interactive.highlight.chart.stroke'];
- co.lineWidth = 2;
- co.strokeRect(x, y, w, h);
- co.fillRect(x, y, w, h);
- }
- });
- };
- /**
- * Using a function to add events makes it easier to facilitate method chaining
- *
- * @param string type The type of even to add
- * @param function func
- */
- this.on = function (type, func)
- {
- if (type.substr(0,2) !== 'on') {
- type = 'on' + type;
- }
-
- this[type] = func;
-
- return this;
- };
-
-
-
-
- /**
- * Draws the above labels
- */
- this.drawAboveLabels = function ()
- {
- var labels = prop['chart.labels.above'];
- var specific = prop['chart.labels.above.specific'];
- var color = prop['chart.labels.above.color'];
- var decimals = prop['chart.labels.above.decimals'];
- var size = prop['chart.labels.above.size'];
- var angle = -1 * prop['chart.labels.above.angle'];
- var coords = this.coords;
- var coords2 = this.coords2;
- var data = this.data;
- var ldata = RG.arrayLinearize(this.data);
- var offset = prop['chart.labels.above.offset'];
- var text_font = prop['chart.text.font'];
- var text_size = prop['chart.text.size'];
- var grouping = prop['chart.grouping']
- // Turn off any shadow
- RG.noShadow(this);
- // Color
- co.fillStyle = typeof color === 'string' ? color : prop['chart.text.color'];
- // This bit draws the text labels that appear above the bars if requested
- if (labels && grouping === 'grouped') {
- for (var i=0,len=data.length,sequentialIndex=0; i<len; i+=1) {
- // Alignment for regular, positive bars
- if (typeof data[i] === 'number' && data[i] >= 0) {
-
- var angle = angle;
- var halign = (angle ? 'left' : 'center');
- var valign = angle !== 0 ? 'center' : 'bottom';
- RG.text2(this, {'font': text_font,
- 'size': typeof size === 'number' ? size : text_size - 3,
- 'x': coords2[i][0][0] + (coords2[i][0][2] / 2),
- 'y': coords2[i][0][1] - offset,
- 'text': specific ? (specific[sequentialIndex] || '') : RG.numberFormat(this, Number(typeof data[i] === 'object' ? data[i][0] : data[i]).toFixed(decimals)), // TODO Units?
- 'halign': halign,
- 'valign': valign,
- 'angle': angle,
- 'marker': false,
- 'bounding': false,
- 'tag': 'labels.above'
- });
- sequentialIndex++;
- // Alignment for regular, negative bars
- } else if (typeof data[i] === 'number' && data[i] < 0) {
- var angle = angle;
- var halign = angle ? 'right' : 'center';
- var valign = angle !== 0 ? 'center' : 'top';
- RG.text2(this, {'font': text_font,
- 'size': typeof size === 'number' ? size : text_size - 3,
- 'x': coords2[i][0][0] + (coords2[i][0][2] / 2),
- 'y': coords2[i][0][1] + coords2[i][0][3] + offset,
- 'text': specific ? (specific[sequentialIndex] || '') : RG.numberFormat(this, Number(typeof data[i] === 'object' ? data[i][0] : data[i]).toFixed(decimals)), // TODO Units?
- 'halign': halign,
- 'valign': valign,
- 'angle': angle,
- 'bounding': false,
- 'marker': false,
- 'tag': 'labels.above'
- });
-
- sequentialIndex++;
- // Alignment for grouped bars
- } else if (typeof data[i] === 'object') {
-
- for (var j=0,len2=data[i].length; j<len2; j+=1) {
- var angle = angle;
- var halign = data[i][j] < 0 ? 'right' : 'left';
- halign = angle === 0 ? 'center' : halign;
- var valign = data[i][j] < 0 ? 'top' : 'bottom';
- valign = angle != 0 ? 'center' : valign;
- RG.text2(this, {'font': text_font,
- 'size': typeof size === 'number' ? size : text_size - 3,
- 'x': coords2[i][j][0] + (coords2[i][j][2] / 2),
- 'y': coords2[i][j][1] + (data[i][j] < 0 ? coords2[i][j][3] + offset: -offset),
- 'text': specific ? (specific[sequentialIndex] || '') : RG.numberFormat(this, Number(data[i][j]).toFixed(decimals)), // TODO Units?
- 'halign': halign,
- 'valign': valign,
- 'angle': angle,
- 'bounding': false,
- 'marker': false,
- 'tag': 'labels.above'
- });
- sequentialIndex++;
- }
- }
- }
- /**
- * STACKED bars
- */
- } else if (labels && grouping === 'stacked') {
- for (var i=0,len=data.length,sequentialIndex=0; i<len; i+=1) {
- if (typeof data[i] === 'object') {
- var angle = angle;
- var halign = angle != 0 ? 'left' : 'center';
- var valign = angle != 0 ? 'center' : 'bottom';
- RG.text2(this, {'font': text_font,
- 'size': typeof size === 'number' ? size : text_size - 3,
- 'x': coords2[i][0][0] + (coords2[i][0][2] / 2),
- 'y': coords2[i][0][1] + (data[i][0] < 0 ? coords2[i][0][3] : 0) - offset,
- 'text': specific ? (specific[sequentialIndex] || '') : RG.numberFormat(this, Number(RG.arraySum(data[i])).toFixed(decimals)), // TODO Units?
- 'halign': halign,
- 'valign': valign,
- 'angle': angle,
- 'bounding': false,
- 'marker': false,
- 'tag': 'labels.above'
- });
- sequentialIndex += data[i].length;
-
- /**
- * Regular numbers but in a stacked grouping
- */
- } else {
- var angle = angle;
- var halign = angle != 0 ? 'left' : 'center';
- var valign = angle != 0 ? 'center' : 'bottom';
- RG.text2(this, {'font': text_font,
- 'size': typeof size === 'number' ? size : text_size - 3,
- 'x': coords2[i][0][0] + (coords2[i][0][2] / 2),
- 'y': coords2[i][0][1] + (data[i][0] < 0 ? coords2[i][0][3] : 0) - offset,
- 'text': specific ? (specific[sequentialIndex] || '') : RG.numberFormat(this, Number(data[i]).toFixed(decimals)), // TODO Units?
- 'halign': halign,
- 'valign': valign,
- 'angle': angle,
- 'bounding': false,
- 'marker': false,
- 'tag': 'labels.above'
- });
-
- sequentialIndex++;
- }
- }
- }
- };
- /**
- * This function runs once only
- */
- this.firstDrawFunc = function ()
- {
- };
- /**
- * Bar chart Wave effect This effect defaults to 30 frames - which is
- * approximately half a second. This the prior, older implementation
- * of the Wave effect. It can be slower due to the many timers set
- *
- * @param object obj The chart object
- */
- this.waveOld = function ()
- {
- var obj = this;
- var opt = arguments[0] ? arguments[0] : {};
- opt.frames = opt.frames ? opt.frames : 15;
- opt.delay = opt.delay || 50;
- var callback = arguments[1] ? arguments[1] : function () {};
- var original_data = [];
- var frame = [];
- var length = obj.data.length;
- obj.draw();
- //var scale = RGraph.getScale2(obj, {'max':obj.max});
- obj.Set('chart.ymax', obj.scale2.max);
- RG.clear(obj.canvas);
-
- for (var i=0,len=length; i<len; ++i) {
- (function (idx)
- {
- original_data[idx] = obj.data[idx];
- obj.data[idx] = typeof obj.data[idx] === 'object' ? [] : 0;
- frame[idx] = typeof obj.data[idx] === 'object' ? [] : 0;
- setTimeout(function () {iterator(idx, opt.frames);}, opt.delay * idx)
- })(i);
- }
- return this;
- function iterator (idx, frames)
- {
- if (frame[idx] <= frames) {
-
- // Update the data point
- if (typeof obj.data[idx] === 'number') {
- obj.data[idx] = (frame[idx] / frames) * original_data[idx]
-
- } else if (typeof obj.data[idx] === 'object') {
- for (var k=0,len=original_data[idx].length; k<len; ++k) {
- obj.data[idx][k] = (frame[idx] / frames) * original_data[idx][k];
- }
- }
-
- RG.clear(obj.canvas);
- RG.redrawCanvas(obj.canvas);
-
- ++frame[idx];
- RG.Effects.updateCanvas(function () {iterator(idx, frames);});
-
- } else if (idx === (length - 1) ) {
- callback(obj);
- }
- }
- };
- /**
- * (new) Bar chart Wave effect. This is a rewrite that should be smoother
- * because it just uses a single loop and not setTimeout
- *
- * @param object OPTIONAL An object map of options. You specify 'frames' here to give the number of frames in the effect
- * @param function OPTIONAL A function that will be called when the effect is complete
- */
- this.wave = function ()
- {
- var obj = this;
- var opt = arguments[0] || {};
- opt.frames = opt.frames || 60;
- opt.startFrames = [];
- opt.counters = [];
- var framesperbar = opt.frames / 3;
- var frame = -1;
- var callback = arguments[1] || function () {};
- var original = RG.arrayClone(obj.data);
- for (var i=0,len=obj.data.length; i<len; i+=1) {
- opt.startFrames[i] = ((opt.frames / 2) / (obj.data.length - 1)) * i;
-
- if (typeof obj.data[i] === 'object' && obj.data[i]) {
- opt.counters[i] = [];
- for (var j=0; j<obj.data[i].length; j++) {
- opt.counters[i][j] = 0;
- }
- } else {
- opt.counters[i] = 0;
- }
- }
- /**
- * This stops the chart from jumping
- */
- obj.draw();
- obj.Set('ymax', obj.scale2.max);
- RG.clear(obj.canvas);
- function iterator ()
- {
- ++frame;
- for (var i=0,len=obj.data.length; i<len; i+=1) {
- if (frame > opt.startFrames[i]) {
- if (typeof obj.data[i] === 'number') {
- obj.data[i] = ma.min(
- ma.abs(original[i]),
- ma.abs(original[i] * ( (opt.counters[i]++) / framesperbar))
- );
- // Make the number negative if the original was
- if (original[i] < 0) {
- obj.data[i] *= -1;
- }
- } else if (!RG.isNull(obj.data[i])) {
- for (var j=0,len2=obj.data[i].length; j<len2; j+=1) {
- obj.data[i][j] = ma.min(
- ma.abs(original[i][j]),
- ma.abs(original[i][j] * ( (opt.counters[i][j]++) / framesperbar))
- );
- // Make the number negative if the original was
- if (original[i][j] < 0) {
- obj.data[i][j] *= -1;
- }
- }
- }
- } else {
- obj.data[i] = typeof obj.data[i] === 'object' && obj.data[i] ? RG.arrayPad([], obj.data[i].length, 0) : (RG.isNull(obj.data[i]) ? null : 0);
- }
- }
- if (frame >= opt.frames) {
- callback(obj);
- } else {
- RG.redrawCanvas(obj.canvas);
- RG.Effects.updateCanvas(iterator);
- }
- }
-
- iterator();
- return this;
- };
- /**
- * Grow
- *
- * The Bar chart Grow effect gradually increases the values of the bars
- *
- * @param object An object of options - eg: {frames: 30}
- * @param function A function to call when the effect is complete
- */
- this.grow = function ()
- {
- // Callback
- var opt = arguments[0] || {};
- var frames = opt.frames || 30;
- var frame = 0;
- var callback = arguments[1] || function () {};
- var obj = this;
-
- // Save the data
- obj.original_data = RGraph.array_clone(obj.data);
-
-
- // Stop the scale from changing by setting chart.ymax (if it's not already set)
- if (obj.Get('chart.ymax') == null) {
-
- var ymax = 0;
-
- for (var i=0; i<obj.data.length; ++i) {
- if (RG.is_array(obj.data[i]) && obj.Get('chart.grouping') == 'stacked') {
- ymax = ma.max(ymax, ma.abs(RG.array_sum(obj.data[i])));
-
- } else if (RG.is_array(obj.data[i]) && obj.Get('chart.grouping') == 'grouped') {
- ymax = ma.max(ymax, ma.abs(RG.array_max(obj.data[i])));
- } else {
- ymax = ma.max(ymax, ma.abs(obj.data[i]));
- }
- }
-
- var scale = RGraph.getScale2(obj, {'max':ymax});
- obj.Set('chart.ymax', scale.max);
- }
- var iterator = function ()
- {
- var easingMultiplier = RG.Effects.getEasingMultiplier(frames, frame);
- // Alter the Bar chart data depending on the frame
- for (var j=0,len=obj.original_data.length; j<len; ++j) {
- if (typeof obj.data[j] === 'object') {
- for (var k=0,len2=obj.data[j].length; k<len2; ++k) {
- obj.data[j][k] = easingMultiplier * obj.original_data[j][k];
- }
- } else {
- obj.data[j] = easingMultiplier * obj.original_data[j];
- }
- }
- //RGraph.clear(obj.canvas);
- RGraph.redrawCanvas(obj.canvas);
- if (frame < frames) {
- frame += 1;
-
- RG.Effects.updateCanvas(iterator);
- // Call the callback function
- } else {
- callback(obj);
- }
- };
-
- iterator();
-
- return this;
- };
- /**
- * Register the object
- */
- RG.register(this);
- /**
- * This the 'end' of the constructor so if the first argument actually
- * contains configuration dta as well - handle that.
- */
- if (parseConfObjectForOptions) {
- RG.parseObjectStyleConfig(this, conf);
- return this;
- }
- };
- /*********************************************************************************************************
- * This is the combined bar and Line class which makes creating bar/line combo charts a little bit easier *
- /*********************************************************************************************************/
- RGraph.CombinedChart = function ()
- {
- /**
- * Create a default empty array for the objects
- */
- this.objects = [];
-
- var objects = arguments;
- if (RGraph.is_array(arguments[0])) {
- objects = arguments[0];
- }
- for (var i=0; i<objects.length; ++i) {
- this.objects[i] = objects[i];
- /**
- * Set the Line chart gutters to match the Bar chart gutters
- */
- this.objects[i].Set('chart.gutter.left', this.objects[0].Get('chart.gutter.left'));
- this.objects[i].Set('chart.gutter.right', this.objects[0].Get('chart.gutter.right'));
- this.objects[i].Set('chart.gutter.top', this.objects[0].Get('chart.gutter.top'));
- this.objects[i].Set('chart.gutter.bottom', this.objects[0].Get('chart.gutter.bottom'));
- if (this.objects[i].type == 'line') {
-
- /**
- * Set the line chart hmargin
- */
- 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 );
-
-
- /**
- * No labels, axes or grid on the Line chart
- */
- this.objects[i].Set('chart.noaxes', true);
- this.objects[i].Set('chart.background.grid', false);
- this.objects[i].Set('chart.ylabels', false);
- }
- /**
- * Resizing
- */
- if (this.objects[i].Get('chart.resizable')) {
- var resizable_object = this.objects[i];
- }
- }
- /**
- * Resizing
- */
- if (resizable_object) {
- /**
- * This recalculates the Line chart hmargin when the chart is resized
- */
- function myOnresizebeforedraw (obj)
- {
- var gutterLeft = obj.Get('chart.gutter.left');
- var gutterRight = obj.Get('chart.gutter.right');
-
- obj.Set('chart.hmargin', (obj.canvas.width - gutterLeft - gutterRight) / (obj.original_data[0].length * 2));
- }
- RGraph.AddCustomEventListener(resizable_object,
- 'onresizebeforedraw',
- myOnresizebeforedraw);
- }
- };
- /**
- * The Add method can be used to add methods to the CombinedChart object.
- */
- RGraph.CombinedChart.prototype.add =
- RGraph.CombinedChart.prototype.Add = function (obj)
- {
- this.objects.push(obj);
- };
-
- /**
- * The Draw method goes through all of the objects drawing them (sequentially)
- */
- RGraph.CombinedChart.prototype.draw =
- RGraph.CombinedChart.prototype.Draw = function ()
- {
- for (var i=0; i<this.objects.length; ++i) {
- this.objects[i].Draw();
- }
- };
|