highcharts.src.js 339 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649
  1. // ==ClosureCompiler==
  2. // @compilation_level SIMPLE_OPTIMIZATIONS
  3. /**
  4. * @license Highcharts JS v2.2.3 (2012-05-07)
  5. *
  6. * (c) 2009-2011 Torstein Hønsi
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. // JSLint options:
  11. /*global Highcharts, document, window, navigator, setInterval, clearInterval, clearTimeout, setTimeout, location, jQuery, $, console */
  12. (function () {
  13. // encapsulated variables
  14. var UNDEFINED,
  15. doc = document,
  16. win = window,
  17. math = Math,
  18. mathRound = math.round,
  19. mathFloor = math.floor,
  20. mathCeil = math.ceil,
  21. mathMax = math.max,
  22. mathMin = math.min,
  23. mathAbs = math.abs,
  24. mathCos = math.cos,
  25. mathSin = math.sin,
  26. mathPI = math.PI,
  27. deg2rad = mathPI * 2 / 360,
  28. // some variables
  29. userAgent = navigator.userAgent,
  30. isIE = /msie/i.test(userAgent) && !win.opera,
  31. docMode8 = doc.documentMode === 8,
  32. isWebKit = /AppleWebKit/.test(userAgent),
  33. isFirefox = /Firefox/.test(userAgent),
  34. SVG_NS = 'http://www.w3.org/2000/svg',
  35. hasSVG = !!doc.createElementNS && !!doc.createElementNS(SVG_NS, 'svg').createSVGRect,
  36. hasBidiBug = isFirefox && parseInt(userAgent.split('Firefox/')[1], 10) < 4, // issue #38
  37. useCanVG = !hasSVG && !isIE && !!doc.createElement('canvas').getContext,
  38. Renderer,
  39. hasTouch = doc.documentElement.ontouchstart !== UNDEFINED,
  40. symbolSizes = {},
  41. idCounter = 0,
  42. garbageBin,
  43. defaultOptions,
  44. dateFormat, // function
  45. globalAnimation,
  46. pathAnim,
  47. timeUnits,
  48. // some constants for frequently used strings
  49. DIV = 'div',
  50. ABSOLUTE = 'absolute',
  51. RELATIVE = 'relative',
  52. HIDDEN = 'hidden',
  53. PREFIX = 'highcharts-',
  54. VISIBLE = 'visible',
  55. PX = 'px',
  56. NONE = 'none',
  57. M = 'M',
  58. L = 'L',
  59. /*
  60. * Empirical lowest possible opacities for TRACKER_FILL
  61. * IE6: 0.002
  62. * IE7: 0.002
  63. * IE8: 0.002
  64. * IE9: 0.00000000001 (unlimited)
  65. * FF: 0.00000000001 (unlimited)
  66. * Chrome: 0.000001
  67. * Safari: 0.000001
  68. * Opera: 0.00000000001 (unlimited)
  69. */
  70. TRACKER_FILL = 'rgba(192,192,192,' + (hasSVG ? 0.000001 : 0.002) + ')', // invisible but clickable
  71. //TRACKER_FILL = 'rgba(192,192,192,0.5)',
  72. NORMAL_STATE = '',
  73. HOVER_STATE = 'hover',
  74. SELECT_STATE = 'select',
  75. MILLISECOND = 'millisecond',
  76. SECOND = 'second',
  77. MINUTE = 'minute',
  78. HOUR = 'hour',
  79. DAY = 'day',
  80. WEEK = 'week',
  81. MONTH = 'month',
  82. YEAR = 'year',
  83. // constants for attributes
  84. FILL = 'fill',
  85. LINEAR_GRADIENT = 'linearGradient',
  86. STOPS = 'stops',
  87. STROKE = 'stroke',
  88. STROKE_WIDTH = 'stroke-width',
  89. // time methods, changed based on whether or not UTC is used
  90. makeTime,
  91. getMinutes,
  92. getHours,
  93. getDay,
  94. getDate,
  95. getMonth,
  96. getFullYear,
  97. setMinutes,
  98. setHours,
  99. setDate,
  100. setMonth,
  101. setFullYear,
  102. // check for a custom HighchartsAdapter defined prior to this file
  103. globalAdapter = win.HighchartsAdapter,
  104. adapter = globalAdapter || {},
  105. // Utility functions. If the HighchartsAdapter is not defined, adapter is an empty object
  106. // and all the utility functions will be null. In that case they are populated by the
  107. // default adapters below.
  108. getScript = adapter.getScript,
  109. each = adapter.each,
  110. grep = adapter.grep,
  111. offset = adapter.offset,
  112. map = adapter.map,
  113. merge = adapter.merge,
  114. addEvent = adapter.addEvent,
  115. removeEvent = adapter.removeEvent,
  116. fireEvent = adapter.fireEvent,
  117. washMouseEvent = adapter.washMouseEvent,
  118. animate = adapter.animate,
  119. stop = adapter.stop,
  120. // lookup over the types and the associated classes
  121. seriesTypes = {};
  122. // The Highcharts namespace
  123. win.Highcharts = {};
  124. /**
  125. * Extend an object with the members of another
  126. * @param {Object} a The object to be extended
  127. * @param {Object} b The object to add to the first one
  128. */
  129. function extend(a, b) {
  130. var n;
  131. if (!a) {
  132. a = {};
  133. }
  134. for (n in b) {
  135. a[n] = b[n];
  136. }
  137. return a;
  138. }
  139. /**
  140. * Take an array and turn into a hash with even number arguments as keys and odd numbers as
  141. * values. Allows creating constants for commonly used style properties, attributes etc.
  142. * Avoid it in performance critical situations like looping
  143. */
  144. function hash() {
  145. var i = 0,
  146. args = arguments,
  147. length = args.length,
  148. obj = {};
  149. for (; i < length; i++) {
  150. obj[args[i++]] = args[i];
  151. }
  152. return obj;
  153. }
  154. /**
  155. * Shortcut for parseInt
  156. * @param {Object} s
  157. * @param {Number} mag Magnitude
  158. */
  159. function pInt(s, mag) {
  160. return parseInt(s, mag || 10);
  161. }
  162. /**
  163. * Check for string
  164. * @param {Object} s
  165. */
  166. function isString(s) {
  167. return typeof s === 'string';
  168. }
  169. /**
  170. * Check for object
  171. * @param {Object} obj
  172. */
  173. function isObject(obj) {
  174. return typeof obj === 'object';
  175. }
  176. /**
  177. * Check for array
  178. * @param {Object} obj
  179. */
  180. function isArray(obj) {
  181. return Object.prototype.toString.call(obj) === '[object Array]';
  182. }
  183. /**
  184. * Check for number
  185. * @param {Object} n
  186. */
  187. function isNumber(n) {
  188. return typeof n === 'number';
  189. }
  190. function log2lin(num) {
  191. return math.log(num) / math.LN10;
  192. }
  193. function lin2log(num) {
  194. return math.pow(10, num);
  195. }
  196. /**
  197. * Remove last occurence of an item from an array
  198. * @param {Array} arr
  199. * @param {Mixed} item
  200. */
  201. function erase(arr, item) {
  202. var i = arr.length;
  203. while (i--) {
  204. if (arr[i] === item) {
  205. arr.splice(i, 1);
  206. break;
  207. }
  208. }
  209. //return arr;
  210. }
  211. /**
  212. * Returns true if the object is not null or undefined. Like MooTools' $.defined.
  213. * @param {Object} obj
  214. */
  215. function defined(obj) {
  216. return obj !== UNDEFINED && obj !== null;
  217. }
  218. /**
  219. * Set or get an attribute or an object of attributes. Can't use jQuery attr because
  220. * it attempts to set expando properties on the SVG element, which is not allowed.
  221. *
  222. * @param {Object} elem The DOM element to receive the attribute(s)
  223. * @param {String|Object} prop The property or an abject of key-value pairs
  224. * @param {String} value The value if a single property is set
  225. */
  226. function attr(elem, prop, value) {
  227. var key,
  228. setAttribute = 'setAttribute',
  229. ret;
  230. // if the prop is a string
  231. if (isString(prop)) {
  232. // set the value
  233. if (defined(value)) {
  234. elem[setAttribute](prop, value);
  235. // get the value
  236. } else if (elem && elem.getAttribute) { // elem not defined when printing pie demo...
  237. ret = elem.getAttribute(prop);
  238. }
  239. // else if prop is defined, it is a hash of key/value pairs
  240. } else if (defined(prop) && isObject(prop)) {
  241. for (key in prop) {
  242. elem[setAttribute](key, prop[key]);
  243. }
  244. }
  245. return ret;
  246. }
  247. /**
  248. * Check if an element is an array, and if not, make it into an array. Like
  249. * MooTools' $.splat.
  250. */
  251. function splat(obj) {
  252. return isArray(obj) ? obj : [obj];
  253. }
  254. /**
  255. * Return the first value that is defined. Like MooTools' $.pick.
  256. */
  257. function pick() {
  258. var args = arguments,
  259. i,
  260. arg,
  261. length = args.length;
  262. for (i = 0; i < length; i++) {
  263. arg = args[i];
  264. if (typeof arg !== 'undefined' && arg !== null) {
  265. return arg;
  266. }
  267. }
  268. }
  269. /**
  270. * Set CSS on a given element
  271. * @param {Object} el
  272. * @param {Object} styles Style object with camel case property names
  273. */
  274. function css(el, styles) {
  275. if (isIE) {
  276. if (styles && styles.opacity !== UNDEFINED) {
  277. styles.filter = 'alpha(opacity=' + (styles.opacity * 100) + ')';
  278. }
  279. }
  280. extend(el.style, styles);
  281. }
  282. /**
  283. * Utility function to create element with attributes and styles
  284. * @param {Object} tag
  285. * @param {Object} attribs
  286. * @param {Object} styles
  287. * @param {Object} parent
  288. * @param {Object} nopad
  289. */
  290. function createElement(tag, attribs, styles, parent, nopad) {
  291. var el = doc.createElement(tag);
  292. if (attribs) {
  293. extend(el, attribs);
  294. }
  295. if (nopad) {
  296. css(el, {padding: 0, border: NONE, margin: 0});
  297. }
  298. if (styles) {
  299. css(el, styles);
  300. }
  301. if (parent) {
  302. parent.appendChild(el);
  303. }
  304. return el;
  305. }
  306. /**
  307. * Extend a prototyped class by new members
  308. * @param {Object} parent
  309. * @param {Object} members
  310. */
  311. function extendClass(parent, members) {
  312. var object = function () {};
  313. object.prototype = new parent();
  314. extend(object.prototype, members);
  315. return object;
  316. }
  317. /**
  318. * Format a number and return a string based on input settings
  319. * @param {Number} number The input number to format
  320. * @param {Number} decimals The amount of decimals
  321. * @param {String} decPoint The decimal point, defaults to the one given in the lang options
  322. * @param {String} thousandsSep The thousands separator, defaults to the one given in the lang options
  323. */
  324. function numberFormat(number, decimals, decPoint, thousandsSep) {
  325. var lang = defaultOptions.lang,
  326. // http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_number_format/
  327. n = number,
  328. c = isNaN(decimals = mathAbs(decimals)) ? 2 : decimals,
  329. d = decPoint === undefined ? lang.decimalPoint : decPoint,
  330. t = thousandsSep === undefined ? lang.thousandsSep : thousandsSep,
  331. s = n < 0 ? "-" : "",
  332. i = String(pInt(n = mathAbs(+n || 0).toFixed(c))),
  333. j = i.length > 3 ? i.length % 3 : 0;
  334. return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) +
  335. (c ? d + mathAbs(n - i).toFixed(c).slice(2) : "");
  336. }
  337. /**
  338. * Pad a string to a given length by adding 0 to the beginning
  339. * @param {Number} number
  340. * @param {Number} length
  341. */
  342. function pad(number, length) {
  343. // Create an array of the remaining length +1 and join it with 0's
  344. return new Array((length || 2) + 1 - String(number).length).join(0) + number;
  345. }
  346. /**
  347. * Based on http://www.php.net/manual/en/function.strftime.php
  348. * @param {String} format
  349. * @param {Number} timestamp
  350. * @param {Boolean} capitalize
  351. */
  352. dateFormat = function (format, timestamp, capitalize) {
  353. if (!defined(timestamp) || isNaN(timestamp)) {
  354. return 'Invalid date';
  355. }
  356. format = pick(format, '%Y-%m-%d %H:%M:%S');
  357. var date = new Date(timestamp),
  358. key, // used in for constuct below
  359. // get the basic time values
  360. hours = date[getHours](),
  361. day = date[getDay](),
  362. dayOfMonth = date[getDate](),
  363. month = date[getMonth](),
  364. fullYear = date[getFullYear](),
  365. lang = defaultOptions.lang,
  366. langWeekdays = lang.weekdays,
  367. /* // uncomment this and the 'W' format key below to enable week numbers
  368. weekNumber = function () {
  369. var clone = new Date(date.valueOf()),
  370. day = clone[getDay]() == 0 ? 7 : clone[getDay](),
  371. dayNumber;
  372. clone.setDate(clone[getDate]() + 4 - day);
  373. dayNumber = mathFloor((clone.getTime() - new Date(clone[getFullYear](), 0, 1, -6)) / 86400000);
  374. return 1 + mathFloor(dayNumber / 7);
  375. },
  376. */
  377. // list all format keys
  378. replacements = {
  379. // Day
  380. 'a': langWeekdays[day].substr(0, 3), // Short weekday, like 'Mon'
  381. 'A': langWeekdays[day], // Long weekday, like 'Monday'
  382. 'd': pad(dayOfMonth), // Two digit day of the month, 01 to 31
  383. 'e': dayOfMonth, // Day of the month, 1 through 31
  384. // Week (none implemented)
  385. //'W': weekNumber(),
  386. // Month
  387. 'b': lang.shortMonths[month], // Short month, like 'Jan'
  388. 'B': lang.months[month], // Long month, like 'January'
  389. 'm': pad(month + 1), // Two digit month number, 01 through 12
  390. // Year
  391. 'y': fullYear.toString().substr(2, 2), // Two digits year, like 09 for 2009
  392. 'Y': fullYear, // Four digits year, like 2009
  393. // Time
  394. 'H': pad(hours), // Two digits hours in 24h format, 00 through 23
  395. 'I': pad((hours % 12) || 12), // Two digits hours in 12h format, 00 through 11
  396. 'l': (hours % 12) || 12, // Hours in 12h format, 1 through 12
  397. 'M': pad(date[getMinutes]()), // Two digits minutes, 00 through 59
  398. 'p': hours < 12 ? 'AM' : 'PM', // Upper case AM or PM
  399. 'P': hours < 12 ? 'am' : 'pm', // Lower case AM or PM
  400. 'S': pad(date.getSeconds()), // Two digits seconds, 00 through 59
  401. 'L': pad(mathRound(timestamp % 1000), 3) // Milliseconds (naming from Ruby)
  402. };
  403. // do the replaces
  404. for (key in replacements) {
  405. format = format.replace('%' + key, replacements[key]);
  406. }
  407. // Optionally capitalize the string and return
  408. return capitalize ? format.substr(0, 1).toUpperCase() + format.substr(1) : format;
  409. };
  410. /**
  411. * Take an interval and normalize it to multiples of 1, 2, 2.5 and 5
  412. * @param {Number} interval
  413. * @param {Array} multiples
  414. * @param {Number} magnitude
  415. * @param {Object} options
  416. */
  417. function normalizeTickInterval(interval, multiples, magnitude, options) {
  418. var normalized, i;
  419. // round to a tenfold of 1, 2, 2.5 or 5
  420. magnitude = pick(magnitude, 1);
  421. normalized = interval / magnitude;
  422. // multiples for a linear scale
  423. if (!multiples) {
  424. multiples = [1, 2, 2.5, 5, 10];
  425. // the allowDecimals option
  426. if (options && options.allowDecimals === false) {
  427. if (magnitude === 1) {
  428. multiples = [1, 2, 5, 10];
  429. } else if (magnitude <= 0.1) {
  430. multiples = [1 / magnitude];
  431. }
  432. }
  433. }
  434. // normalize the interval to the nearest multiple
  435. for (i = 0; i < multiples.length; i++) {
  436. interval = multiples[i];
  437. if (normalized <= (multiples[i] + (multiples[i + 1] || multiples[i])) / 2) {
  438. break;
  439. }
  440. }
  441. // multiply back to the correct magnitude
  442. interval *= magnitude;
  443. return interval;
  444. }
  445. /**
  446. * Get a normalized tick interval for dates. Returns a configuration object with
  447. * unit range (interval), count and name. Used to prepare data for getTimeTicks.
  448. * Previously this logic was part of getTimeTicks, but as getTimeTicks now runs
  449. * of segments in stock charts, the normalizing logic was extracted in order to
  450. * prevent it for running over again for each segment having the same interval.
  451. * #662, #697.
  452. */
  453. function normalizeTimeTickInterval(tickInterval, unitsOption) {
  454. var units = unitsOption || [[
  455. MILLISECOND, // unit name
  456. [1, 2, 5, 10, 20, 25, 50, 100, 200, 500] // allowed multiples
  457. ], [
  458. SECOND,
  459. [1, 2, 5, 10, 15, 30]
  460. ], [
  461. MINUTE,
  462. [1, 2, 5, 10, 15, 30]
  463. ], [
  464. HOUR,
  465. [1, 2, 3, 4, 6, 8, 12]
  466. ], [
  467. DAY,
  468. [1, 2]
  469. ], [
  470. WEEK,
  471. [1, 2]
  472. ], [
  473. MONTH,
  474. [1, 2, 3, 4, 6]
  475. ], [
  476. YEAR,
  477. null
  478. ]],
  479. unit = units[units.length - 1], // default unit is years
  480. interval = timeUnits[unit[0]],
  481. multiples = unit[1],
  482. count,
  483. i;
  484. // loop through the units to find the one that best fits the tickInterval
  485. for (i = 0; i < units.length; i++) {
  486. unit = units[i];
  487. interval = timeUnits[unit[0]];
  488. multiples = unit[1];
  489. if (units[i + 1]) {
  490. // lessThan is in the middle between the highest multiple and the next unit.
  491. var lessThan = (interval * multiples[multiples.length - 1] +
  492. timeUnits[units[i + 1][0]]) / 2;
  493. // break and keep the current unit
  494. if (tickInterval <= lessThan) {
  495. break;
  496. }
  497. }
  498. }
  499. // prevent 2.5 years intervals, though 25, 250 etc. are allowed
  500. if (interval === timeUnits[YEAR] && tickInterval < 5 * interval) {
  501. multiples = [1, 2, 5];
  502. }
  503. // prevent 2.5 years intervals, though 25, 250 etc. are allowed
  504. if (interval === timeUnits[YEAR] && tickInterval < 5 * interval) {
  505. multiples = [1, 2, 5];
  506. }
  507. // get the count
  508. count = normalizeTickInterval(tickInterval / interval, multiples);
  509. return {
  510. unitRange: interval,
  511. count: count,
  512. unitName: unit[0]
  513. };
  514. }
  515. /**
  516. * Set the tick positions to a time unit that makes sense, for example
  517. * on the first of each month or on every Monday. Return an array
  518. * with the time positions. Used in datetime axes as well as for grouping
  519. * data on a datetime axis.
  520. *
  521. * @param {Object} normalizedInterval The interval in axis values (ms) and the count
  522. * @param {Number} min The minimum in axis values
  523. * @param {Number} max The maximum in axis values
  524. * @param {Number} startOfWeek
  525. */
  526. function getTimeTicks(normalizedInterval, min, max, startOfWeek) {
  527. var tickPositions = [],
  528. i,
  529. higherRanks = {},
  530. useUTC = defaultOptions.global.useUTC,
  531. minYear, // used in months and years as a basis for Date.UTC()
  532. minDate = new Date(min),
  533. interval = normalizedInterval.unitRange,
  534. count = normalizedInterval.count;
  535. if (interval >= timeUnits[SECOND]) { // second
  536. minDate.setMilliseconds(0);
  537. minDate.setSeconds(interval >= timeUnits[MINUTE] ? 0 :
  538. count * mathFloor(minDate.getSeconds() / count));
  539. }
  540. if (interval >= timeUnits[MINUTE]) { // minute
  541. minDate[setMinutes](interval >= timeUnits[HOUR] ? 0 :
  542. count * mathFloor(minDate[getMinutes]() / count));
  543. }
  544. if (interval >= timeUnits[HOUR]) { // hour
  545. minDate[setHours](interval >= timeUnits[DAY] ? 0 :
  546. count * mathFloor(minDate[getHours]() / count));
  547. }
  548. if (interval >= timeUnits[DAY]) { // day
  549. minDate[setDate](interval >= timeUnits[MONTH] ? 1 :
  550. count * mathFloor(minDate[getDate]() / count));
  551. }
  552. if (interval >= timeUnits[MONTH]) { // month
  553. minDate[setMonth](interval >= timeUnits[YEAR] ? 0 :
  554. count * mathFloor(minDate[getMonth]() / count));
  555. minYear = minDate[getFullYear]();
  556. }
  557. if (interval >= timeUnits[YEAR]) { // year
  558. minYear -= minYear % count;
  559. minDate[setFullYear](minYear);
  560. }
  561. // week is a special case that runs outside the hierarchy
  562. if (interval === timeUnits[WEEK]) {
  563. // get start of current week, independent of count
  564. minDate[setDate](minDate[getDate]() - minDate[getDay]() +
  565. pick(startOfWeek, 1));
  566. }
  567. // get tick positions
  568. i = 1;
  569. minYear = minDate[getFullYear]();
  570. var time = minDate.getTime(),
  571. minMonth = minDate[getMonth](),
  572. minDateDate = minDate[getDate]();
  573. // iterate and add tick positions at appropriate values
  574. while (time < max) {
  575. tickPositions.push(time);
  576. // if the interval is years, use Date.UTC to increase years
  577. if (interval === timeUnits[YEAR]) {
  578. time = makeTime(minYear + i * count, 0);
  579. // if the interval is months, use Date.UTC to increase months
  580. } else if (interval === timeUnits[MONTH]) {
  581. time = makeTime(minYear, minMonth + i * count);
  582. // if we're using global time, the interval is not fixed as it jumps
  583. // one hour at the DST crossover
  584. } else if (!useUTC && (interval === timeUnits[DAY] || interval === timeUnits[WEEK])) {
  585. time = makeTime(minYear, minMonth, minDateDate +
  586. i * count * (interval === timeUnits[DAY] ? 1 : 7));
  587. // else, the interval is fixed and we use simple addition
  588. } else {
  589. time += interval * count;
  590. // mark new days if the time is dividable by day
  591. if (interval <= timeUnits[HOUR] && time % timeUnits[DAY] === 0) {
  592. higherRanks[time] = DAY;
  593. }
  594. }
  595. i++;
  596. }
  597. // push the last time
  598. tickPositions.push(time);
  599. // record information on the chosen unit - for dynamic label formatter
  600. tickPositions.info = extend(normalizedInterval, {
  601. higherRanks: higherRanks,
  602. totalRange: interval * count
  603. });
  604. return tickPositions;
  605. }
  606. /**
  607. * Helper class that contains variuos counters that are local to the chart.
  608. */
  609. function ChartCounters() {
  610. this.color = 0;
  611. this.symbol = 0;
  612. }
  613. ChartCounters.prototype = {
  614. /**
  615. * Wraps the color counter if it reaches the specified length.
  616. */
  617. wrapColor: function (length) {
  618. if (this.color >= length) {
  619. this.color = 0;
  620. }
  621. },
  622. /**
  623. * Wraps the symbol counter if it reaches the specified length.
  624. */
  625. wrapSymbol: function (length) {
  626. if (this.symbol >= length) {
  627. this.symbol = 0;
  628. }
  629. }
  630. };
  631. /**
  632. * Utility method extracted from Tooltip code that places a tooltip in a chart without spilling over
  633. * and not covering the point it self.
  634. */
  635. function placeBox(boxWidth, boxHeight, outerLeft, outerTop, outerWidth, outerHeight, point, distance, preferRight) {
  636. // keep the box within the chart area
  637. var pointX = point.x,
  638. pointY = point.y,
  639. x = pointX + outerLeft + (preferRight ? distance : -boxWidth - distance),
  640. y = pointY - boxHeight + outerTop + 15, // 15 means the point is 15 pixels up from the bottom of the tooltip
  641. alignedRight;
  642. // it is too far to the left, adjust it
  643. if (x < 7) {
  644. x = outerLeft + pointX + distance;
  645. }
  646. // Test to see if the tooltip is too far to the right,
  647. // if it is, move it back to be inside and then up to not cover the point.
  648. if ((x + boxWidth) > (outerLeft + outerWidth)) {
  649. x -= (x + boxWidth) - (outerLeft + outerWidth);
  650. y = pointY - boxHeight + outerTop - distance;
  651. alignedRight = true;
  652. }
  653. // if it is now above the plot area, align it to the top of the plot area
  654. if (y < outerTop + 5) {
  655. y = outerTop + 5;
  656. // If the tooltip is still covering the point, move it below instead
  657. if (alignedRight && pointY >= y && pointY <= (y + boxHeight)) {
  658. y = pointY + outerTop + distance; // below
  659. }
  660. }
  661. // Now if the tooltip is below the chart, move it up. It's better to cover the
  662. // point than to disappear outside the chart. #834.
  663. if (y + boxHeight > outerTop + outerHeight) {
  664. y = outerTop + outerHeight - boxHeight - distance; // below
  665. }
  666. return {x: x, y: y};
  667. }
  668. /**
  669. * Utility method that sorts an object array and keeping the order of equal items.
  670. * ECMA script standard does not specify the behaviour when items are equal.
  671. */
  672. function stableSort(arr, sortFunction) {
  673. var length = arr.length,
  674. sortValue,
  675. i;
  676. // Add index to each item
  677. for (i = 0; i < length; i++) {
  678. arr[i].ss_i = i; // stable sort index
  679. }
  680. arr.sort(function (a, b) {
  681. sortValue = sortFunction(a, b);
  682. return sortValue === 0 ? a.ss_i - b.ss_i : sortValue;
  683. });
  684. // Remove index from items
  685. for (i = 0; i < length; i++) {
  686. delete arr[i].ss_i; // stable sort index
  687. }
  688. }
  689. /**
  690. * Non-recursive method to find the lowest member of an array. Math.min raises a maximum
  691. * call stack size exceeded error in Chrome when trying to apply more than 150.000 points. This
  692. * method is slightly slower, but safe.
  693. */
  694. function arrayMin(data) {
  695. var i = data.length,
  696. min = data[0];
  697. while (i--) {
  698. if (data[i] < min) {
  699. min = data[i];
  700. }
  701. }
  702. return min;
  703. }
  704. /**
  705. * Non-recursive method to find the lowest member of an array. Math.min raises a maximum
  706. * call stack size exceeded error in Chrome when trying to apply more than 150.000 points. This
  707. * method is slightly slower, but safe.
  708. */
  709. function arrayMax(data) {
  710. var i = data.length,
  711. max = data[0];
  712. while (i--) {
  713. if (data[i] > max) {
  714. max = data[i];
  715. }
  716. }
  717. return max;
  718. }
  719. /**
  720. * Utility method that destroys any SVGElement or VMLElement that are properties on the given object.
  721. * It loops all properties and invokes destroy if there is a destroy method. The property is
  722. * then delete'ed.
  723. */
  724. function destroyObjectProperties(obj) {
  725. var n;
  726. for (n in obj) {
  727. // If the object is non-null and destroy is defined
  728. if (obj[n] && obj[n].destroy) {
  729. // Invoke the destroy
  730. obj[n].destroy();
  731. }
  732. // Delete the property from the object.
  733. delete obj[n];
  734. }
  735. }
  736. /**
  737. * Discard an element by moving it to the bin and delete
  738. * @param {Object} The HTML node to discard
  739. */
  740. function discardElement(element) {
  741. // create a garbage bin element, not part of the DOM
  742. if (!garbageBin) {
  743. garbageBin = createElement(DIV);
  744. }
  745. // move the node and empty bin
  746. if (element) {
  747. garbageBin.appendChild(element);
  748. }
  749. garbageBin.innerHTML = '';
  750. }
  751. /**
  752. * Provide error messages for debugging, with links to online explanation
  753. */
  754. function error(code, stop) {
  755. var msg = 'Highcharts error #' + code + ': www.highcharts.com/errors/' + code;
  756. if (stop) {
  757. throw msg;
  758. } else if (win.console) {
  759. console.log(msg);
  760. }
  761. }
  762. /**
  763. * Fix JS round off float errors
  764. * @param {Number} num
  765. */
  766. function correctFloat(num) {
  767. return parseFloat(
  768. num.toPrecision(14)
  769. );
  770. }
  771. /**
  772. * The time unit lookup
  773. */
  774. /*jslint white: true*/
  775. timeUnits = hash(
  776. MILLISECOND, 1,
  777. SECOND, 1000,
  778. MINUTE, 60000,
  779. HOUR, 3600000,
  780. DAY, 24 * 3600000,
  781. WEEK, 7 * 24 * 3600000,
  782. MONTH, 30 * 24 * 3600000,
  783. YEAR, 31556952000
  784. );
  785. /*jslint white: false*/
  786. /**
  787. * Path interpolation algorithm used across adapters
  788. */
  789. pathAnim = {
  790. /**
  791. * Prepare start and end values so that the path can be animated one to one
  792. */
  793. init: function (elem, fromD, toD) {
  794. fromD = fromD || '';
  795. var shift = elem.shift,
  796. bezier = fromD.indexOf('C') > -1,
  797. numParams = bezier ? 7 : 3,
  798. endLength,
  799. slice,
  800. i,
  801. start = fromD.split(' '),
  802. end = [].concat(toD), // copy
  803. startBaseLine,
  804. endBaseLine,
  805. sixify = function (arr) { // in splines make move points have six parameters like bezier curves
  806. i = arr.length;
  807. while (i--) {
  808. if (arr[i] === M) {
  809. arr.splice(i + 1, 0, arr[i + 1], arr[i + 2], arr[i + 1], arr[i + 2]);
  810. }
  811. }
  812. };
  813. if (bezier) {
  814. sixify(start);
  815. sixify(end);
  816. }
  817. // pull out the base lines before padding
  818. if (elem.isArea) {
  819. startBaseLine = start.splice(start.length - 6, 6);
  820. endBaseLine = end.splice(end.length - 6, 6);
  821. }
  822. // if shifting points, prepend a dummy point to the end path
  823. if (shift <= end.length / numParams) {
  824. while (shift--) {
  825. end = [].concat(end).splice(0, numParams).concat(end);
  826. }
  827. }
  828. elem.shift = 0; // reset for following animations
  829. // copy and append last point until the length matches the end length
  830. if (start.length) {
  831. endLength = end.length;
  832. while (start.length < endLength) {
  833. //bezier && sixify(start);
  834. slice = [].concat(start).splice(start.length - numParams, numParams);
  835. if (bezier) { // disable first control point
  836. slice[numParams - 6] = slice[numParams - 2];
  837. slice[numParams - 5] = slice[numParams - 1];
  838. }
  839. start = start.concat(slice);
  840. }
  841. }
  842. if (startBaseLine) { // append the base lines for areas
  843. start = start.concat(startBaseLine);
  844. end = end.concat(endBaseLine);
  845. }
  846. return [start, end];
  847. },
  848. /**
  849. * Interpolate each value of the path and return the array
  850. */
  851. step: function (start, end, pos, complete) {
  852. var ret = [],
  853. i = start.length,
  854. startVal;
  855. if (pos === 1) { // land on the final path without adjustment points appended in the ends
  856. ret = complete;
  857. } else if (i === end.length && pos < 1) {
  858. while (i--) {
  859. startVal = parseFloat(start[i]);
  860. ret[i] =
  861. isNaN(startVal) ? // a letter instruction like M or L
  862. start[i] :
  863. pos * (parseFloat(end[i] - startVal)) + startVal;
  864. }
  865. } else { // if animation is finished or length not matching, land on right value
  866. ret = end;
  867. }
  868. return ret;
  869. }
  870. };
  871. /**
  872. * Set the global animation to either a given value, or fall back to the
  873. * given chart's animation option
  874. * @param {Object} animation
  875. * @param {Object} chart
  876. */
  877. function setAnimation(animation, chart) {
  878. globalAnimation = pick(animation, chart.animation);
  879. }
  880. /*
  881. * Define the adapter for frameworks. If an external adapter is not defined,
  882. * Highcharts reverts to the built-in jQuery adapter.
  883. */
  884. if (globalAdapter && globalAdapter.init) {
  885. // Initialize the adapter with the pathAnim object that takes care
  886. // of path animations.
  887. globalAdapter.init(pathAnim);
  888. }
  889. if (!globalAdapter && win.jQuery) {
  890. var jQ = jQuery;
  891. /**
  892. * Downloads a script and executes a callback when done.
  893. * @param {String} scriptLocation
  894. * @param {Function} callback
  895. */
  896. getScript = jQ.getScript;
  897. /**
  898. * Utility for iterating over an array. Parameters are reversed compared to jQuery.
  899. * @param {Array} arr
  900. * @param {Function} fn
  901. */
  902. each = function (arr, fn) {
  903. var i = 0,
  904. len = arr.length;
  905. for (; i < len; i++) {
  906. if (fn.call(arr[i], arr[i], i, arr) === false) {
  907. return i;
  908. }
  909. }
  910. };
  911. /**
  912. * Filter an array
  913. */
  914. grep = jQ.grep;
  915. /**
  916. * Map an array
  917. * @param {Array} arr
  918. * @param {Function} fn
  919. */
  920. map = function (arr, fn) {
  921. //return jQuery.map(arr, fn);
  922. var results = [],
  923. i = 0,
  924. len = arr.length;
  925. for (; i < len; i++) {
  926. results[i] = fn.call(arr[i], arr[i], i, arr);
  927. }
  928. return results;
  929. };
  930. /**
  931. * Deep merge two objects and return a third object
  932. */
  933. merge = function () {
  934. var args = arguments;
  935. return jQ.extend(true, null, args[0], args[1], args[2], args[3]);
  936. };
  937. /**
  938. * Get the position of an element relative to the top left of the page
  939. */
  940. offset = function (el) {
  941. return jQ(el).offset();
  942. };
  943. /**
  944. * Add an event listener
  945. * @param {Object} el A HTML element or custom object
  946. * @param {String} event The event type
  947. * @param {Function} fn The event handler
  948. */
  949. addEvent = function (el, event, fn) {
  950. jQ(el).bind(event, fn);
  951. };
  952. /**
  953. * Remove event added with addEvent
  954. * @param {Object} el The object
  955. * @param {String} eventType The event type. Leave blank to remove all events.
  956. * @param {Function} handler The function to remove
  957. */
  958. removeEvent = function (el, eventType, handler) {
  959. // workaround for jQuery issue with unbinding custom events:
  960. // http://forum.jquery.com/topic/javascript-error-when-unbinding-a-custom-event-using-jquery-1-4-2
  961. var func = doc.removeEventListener ? 'removeEventListener' : 'detachEvent';
  962. if (doc[func] && !el[func]) {
  963. el[func] = function () {};
  964. }
  965. jQ(el).unbind(eventType, handler);
  966. };
  967. /**
  968. * Fire an event on a custom object
  969. * @param {Object} el
  970. * @param {String} type
  971. * @param {Object} eventArguments
  972. * @param {Function} defaultFunction
  973. */
  974. fireEvent = function (el, type, eventArguments, defaultFunction) {
  975. var event = jQ.Event(type),
  976. detachedType = 'detached' + type,
  977. defaultPrevented;
  978. extend(event, eventArguments);
  979. // Prevent jQuery from triggering the object method that is named the
  980. // same as the event. For example, if the event is 'select', jQuery
  981. // attempts calling el.select and it goes into a loop.
  982. if (el[type]) {
  983. el[detachedType] = el[type];
  984. el[type] = null;
  985. }
  986. // Wrap preventDefault and stopPropagation in try/catch blocks in
  987. // order to prevent JS errors when cancelling events on non-DOM
  988. // objects. #615.
  989. each(['preventDefault', 'stopPropagation'], function (fn) {
  990. var base = event[fn];
  991. event[fn] = function () {
  992. try {
  993. base.call(event);
  994. } catch (e) {
  995. if (fn === 'preventDefault') {
  996. defaultPrevented = true;
  997. }
  998. }
  999. };
  1000. });
  1001. // trigger it
  1002. jQ(el).trigger(event);
  1003. // attach the method
  1004. if (el[detachedType]) {
  1005. el[type] = el[detachedType];
  1006. el[detachedType] = null;
  1007. }
  1008. if (defaultFunction && !event.isDefaultPrevented() && !defaultPrevented) {
  1009. defaultFunction(event);
  1010. }
  1011. };
  1012. /**
  1013. * Extension method needed for MooTools
  1014. */
  1015. washMouseEvent = function (e) {
  1016. return e;
  1017. };
  1018. /**
  1019. * Animate a HTML element or SVG element wrapper
  1020. * @param {Object} el
  1021. * @param {Object} params
  1022. * @param {Object} options jQuery-like animation options: duration, easing, callback
  1023. */
  1024. animate = function (el, params, options) {
  1025. var $el = jQ(el);
  1026. if (params.d) {
  1027. el.toD = params.d; // keep the array form for paths, used in jQ.fx.step.d
  1028. params.d = 1; // because in jQuery, animating to an array has a different meaning
  1029. }
  1030. $el.stop();
  1031. $el.animate(params, options);
  1032. };
  1033. /**
  1034. * Stop running animation
  1035. */
  1036. stop = function (el) {
  1037. jQ(el).stop();
  1038. };
  1039. //=== Extend jQuery on init
  1040. /*jslint unparam: true*//* allow unused param x in this function */
  1041. jQ.extend(jQ.easing, {
  1042. easeOutQuad: function (x, t, b, c, d) {
  1043. return -c * (t /= d) * (t - 2) + b;
  1044. }
  1045. });
  1046. /*jslint unparam: false*/
  1047. // extend the animate function to allow SVG animations
  1048. var jFx = jQuery.fx,
  1049. jStep = jFx.step;
  1050. // extend some methods to check for elem.attr, which means it is a Highcharts SVG object
  1051. each(['cur', '_default', 'width', 'height'], function (fn, i) {
  1052. var obj = i ? jStep : jFx.prototype, // 'cur', the getter' relates to jFx.prototype
  1053. base = obj[fn],
  1054. elem;
  1055. if (base) { // step.width and step.height don't exist in jQuery < 1.7
  1056. // create the extended function replacement
  1057. obj[fn] = function (fx) {
  1058. // jFx.prototype.cur does not use fx argument
  1059. fx = i ? fx : this;
  1060. // shortcut
  1061. elem = fx.elem;
  1062. // jFX.prototype.cur returns the current value. The other ones are setters
  1063. // and returning a value has no effect.
  1064. return elem.attr ? // is SVG element wrapper
  1065. elem.attr(fx.prop, fx.now) : // apply the SVG wrapper's method
  1066. base.apply(this, arguments); // use jQuery's built-in method
  1067. };
  1068. }
  1069. });
  1070. // animate paths
  1071. jStep.d = function (fx) {
  1072. var elem = fx.elem;
  1073. // Normally start and end should be set in state == 0, but sometimes,
  1074. // for reasons unknown, this doesn't happen. Perhaps state == 0 is skipped
  1075. // in these cases
  1076. if (!fx.started) {
  1077. var ends = pathAnim.init(elem, elem.d, elem.toD);
  1078. fx.start = ends[0];
  1079. fx.end = ends[1];
  1080. fx.started = true;
  1081. }
  1082. // interpolate each value of the path
  1083. elem.attr('d', pathAnim.step(fx.start, fx.end, fx.pos, elem.toD));
  1084. };
  1085. }
  1086. /* ****************************************************************************
  1087. * Handle the options *
  1088. *****************************************************************************/
  1089. var
  1090. defaultLabelOptions = {
  1091. enabled: true,
  1092. // rotation: 0,
  1093. align: 'center',
  1094. x: 0,
  1095. y: 15,
  1096. /*formatter: function () {
  1097. return this.value;
  1098. },*/
  1099. style: {
  1100. color: '#666',
  1101. fontSize: '11px',
  1102. lineHeight: '14px'
  1103. }
  1104. };
  1105. defaultOptions = {
  1106. colors: ['#4572A7', '#AA4643', '#89A54E', '#80699B', '#3D96AE',
  1107. '#DB843D', '#92A8CD', '#A47D7C', '#B5CA92'],
  1108. symbols: ['circle', 'diamond', 'square', 'triangle', 'triangle-down'],
  1109. lang: {
  1110. loading: 'Loading...',
  1111. months: ['January', 'February', 'March', 'April', 'May', 'June', 'July',
  1112. 'August', 'September', 'October', 'November', 'December'],
  1113. shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  1114. weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
  1115. decimalPoint: '.',
  1116. resetZoom: 'Reset zoom',
  1117. resetZoomTitle: 'Reset zoom level 1:1',
  1118. thousandsSep: ','
  1119. },
  1120. global: {
  1121. useUTC: true,
  1122. canvasToolsURL: 'http://code.highcharts.com/2.2.3/modules/canvas-tools.js'
  1123. },
  1124. chart: {
  1125. //animation: true,
  1126. //alignTicks: false,
  1127. //reflow: true,
  1128. //className: null,
  1129. //events: { load, selection },
  1130. //margin: [null],
  1131. //marginTop: null,
  1132. //marginRight: null,
  1133. //marginBottom: null,
  1134. //marginLeft: null,
  1135. borderColor: '#4572A7',
  1136. //borderWidth: 0,
  1137. borderRadius: 5,
  1138. defaultSeriesType: 'line',
  1139. ignoreHiddenSeries: true,
  1140. //inverted: false,
  1141. //shadow: false,
  1142. spacingTop: 10,
  1143. spacingRight: 10,
  1144. spacingBottom: 15,
  1145. spacingLeft: 10,
  1146. style: {
  1147. fontFamily: '"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif', // default font
  1148. fontSize: '12px'
  1149. },
  1150. backgroundColor: '#FFFFFF',
  1151. //plotBackgroundColor: null,
  1152. plotBorderColor: '#C0C0C0',
  1153. //plotBorderWidth: 0,
  1154. //plotShadow: false,
  1155. //zoomType: ''
  1156. resetZoomButton: {
  1157. theme: {
  1158. zIndex: 20
  1159. },
  1160. position: {
  1161. align: 'right',
  1162. x: -10,
  1163. //verticalAlign: 'top',
  1164. y: 10
  1165. }
  1166. // relativeTo: 'plot'
  1167. }
  1168. },
  1169. title: {
  1170. text: 'Chart title',
  1171. align: 'center',
  1172. // floating: false,
  1173. // margin: 15,
  1174. // x: 0,
  1175. // verticalAlign: 'top',
  1176. y: 15,
  1177. style: {
  1178. color: '#3E576F',
  1179. fontSize: '16px'
  1180. }
  1181. },
  1182. subtitle: {
  1183. text: '',
  1184. align: 'center',
  1185. // floating: false
  1186. // x: 0,
  1187. // verticalAlign: 'top',
  1188. y: 30,
  1189. style: {
  1190. color: '#6D869F'
  1191. }
  1192. },
  1193. plotOptions: {
  1194. line: { // base series options
  1195. allowPointSelect: false,
  1196. showCheckbox: false,
  1197. animation: {
  1198. duration: 1000
  1199. },
  1200. //connectNulls: false,
  1201. //cursor: 'default',
  1202. //clip: true,
  1203. //dashStyle: null,
  1204. //enableMouseTracking: true,
  1205. events: {},
  1206. //legendIndex: 0,
  1207. lineWidth: 2,
  1208. shadow: true,
  1209. // stacking: null,
  1210. marker: {
  1211. enabled: true,
  1212. //symbol: null,
  1213. lineWidth: 0,
  1214. radius: 4,
  1215. lineColor: '#FFFFFF',
  1216. //fillColor: null,
  1217. states: { // states for a single point
  1218. hover: {
  1219. //radius: base + 2
  1220. },
  1221. select: {
  1222. fillColor: '#FFFFFF',
  1223. lineColor: '#000000',
  1224. lineWidth: 2
  1225. }
  1226. }
  1227. },
  1228. point: {
  1229. events: {}
  1230. },
  1231. dataLabels: merge(defaultLabelOptions, {
  1232. enabled: false,
  1233. y: -6,
  1234. formatter: function () {
  1235. return this.y;
  1236. }
  1237. // backgroundColor: undefined,
  1238. // borderColor: undefined,
  1239. // borderRadius: undefined,
  1240. // borderWidth: undefined,
  1241. // padding: 3,
  1242. // shadow: false
  1243. }),
  1244. cropThreshold: 300, // draw points outside the plot area when the number of points is less than this
  1245. pointRange: 0,
  1246. //pointStart: 0,
  1247. //pointInterval: 1,
  1248. showInLegend: true,
  1249. states: { // states for the entire series
  1250. hover: {
  1251. //enabled: false,
  1252. //lineWidth: base + 1,
  1253. marker: {
  1254. // lineWidth: base + 1,
  1255. // radius: base + 1
  1256. }
  1257. },
  1258. select: {
  1259. marker: {}
  1260. }
  1261. },
  1262. stickyTracking: true
  1263. //tooltip: {
  1264. //pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b>'
  1265. //valueDecimals: null,
  1266. //xDateFormat: '%A, %b %e, %Y',
  1267. //valuePrefix: '',
  1268. //ySuffix: ''
  1269. //}
  1270. // turboThreshold: 1000
  1271. // zIndex: null
  1272. }
  1273. },
  1274. labels: {
  1275. //items: [],
  1276. style: {
  1277. //font: defaultFont,
  1278. position: ABSOLUTE,
  1279. color: '#3E576F'
  1280. }
  1281. },
  1282. legend: {
  1283. enabled: true,
  1284. align: 'center',
  1285. //floating: false,
  1286. layout: 'horizontal',
  1287. labelFormatter: function () {
  1288. return this.name;
  1289. },
  1290. borderWidth: 1,
  1291. borderColor: '#909090',
  1292. borderRadius: 5,
  1293. // margin: 10,
  1294. // reversed: false,
  1295. shadow: false,
  1296. // backgroundColor: null,
  1297. style: {
  1298. padding: '5px'
  1299. },
  1300. itemStyle: {
  1301. cursor: 'pointer',
  1302. color: '#3E576F'
  1303. },
  1304. itemHoverStyle: {
  1305. //cursor: 'pointer', removed as of #601
  1306. color: '#000000'
  1307. },
  1308. itemHiddenStyle: {
  1309. color: '#C0C0C0'
  1310. },
  1311. itemCheckboxStyle: {
  1312. position: ABSOLUTE,
  1313. width: '13px', // for IE precision
  1314. height: '13px'
  1315. },
  1316. // itemWidth: undefined,
  1317. symbolWidth: 16,
  1318. symbolPadding: 5,
  1319. verticalAlign: 'bottom',
  1320. // width: undefined,
  1321. x: 0,
  1322. y: 0
  1323. },
  1324. loading: {
  1325. // hideDuration: 100,
  1326. labelStyle: {
  1327. fontWeight: 'bold',
  1328. position: RELATIVE,
  1329. top: '1em'
  1330. },
  1331. // showDuration: 0,
  1332. style: {
  1333. position: ABSOLUTE,
  1334. backgroundColor: 'white',
  1335. opacity: 0.5,
  1336. textAlign: 'center'
  1337. }
  1338. },
  1339. tooltip: {
  1340. enabled: true,
  1341. //crosshairs: null,
  1342. backgroundColor: 'rgba(255, 255, 255, .85)',
  1343. borderWidth: 2,
  1344. borderRadius: 5,
  1345. //formatter: defaultFormatter,
  1346. headerFormat: '<span style="font-size: 10px">{point.key}</span><br/>',
  1347. pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b><br/>',
  1348. shadow: true,
  1349. shared: useCanVG,
  1350. snap: hasTouch ? 25 : 10,
  1351. style: {
  1352. color: '#333333',
  1353. fontSize: '12px',
  1354. padding: '5px',
  1355. whiteSpace: 'nowrap'
  1356. }
  1357. //xDateFormat: '%A, %b %e, %Y',
  1358. //valueDecimals: null,
  1359. //valuePrefix: '',
  1360. //valueSuffix: ''
  1361. },
  1362. credits: {
  1363. enabled: true,
  1364. text: 'Highcharts.com',
  1365. href: 'http://www.highcharts.com',
  1366. position: {
  1367. align: 'right',
  1368. x: -10,
  1369. verticalAlign: 'bottom',
  1370. y: -5
  1371. },
  1372. style: {
  1373. cursor: 'pointer',
  1374. color: '#909090',
  1375. fontSize: '10px'
  1376. }
  1377. }
  1378. };
  1379. // Axis defaults
  1380. /*jslint white: true*/
  1381. var defaultXAxisOptions = {
  1382. // allowDecimals: null,
  1383. // alternateGridColor: null,
  1384. // categories: [],
  1385. dateTimeLabelFormats: hash(
  1386. MILLISECOND, '%H:%M:%S.%L',
  1387. SECOND, '%H:%M:%S',
  1388. MINUTE, '%H:%M',
  1389. HOUR, '%H:%M',
  1390. DAY, '%e. %b',
  1391. WEEK, '%e. %b',
  1392. MONTH, '%b \'%y',
  1393. YEAR, '%Y'
  1394. ),
  1395. endOnTick: false,
  1396. gridLineColor: '#C0C0C0',
  1397. // gridLineDashStyle: 'solid',
  1398. // gridLineWidth: 0,
  1399. // reversed: false,
  1400. labels: defaultLabelOptions,
  1401. // { step: null },
  1402. lineColor: '#C0D0E0',
  1403. lineWidth: 1,
  1404. //linkedTo: null,
  1405. max: null,
  1406. min: null,
  1407. minPadding: 0.01,
  1408. maxPadding: 0.01,
  1409. //minRange: null,
  1410. minorGridLineColor: '#E0E0E0',
  1411. // minorGridLineDashStyle: null,
  1412. minorGridLineWidth: 1,
  1413. minorTickColor: '#A0A0A0',
  1414. //minorTickInterval: null,
  1415. minorTickLength: 2,
  1416. minorTickPosition: 'outside', // inside or outside
  1417. //minorTickWidth: 0,
  1418. //opposite: false,
  1419. //offset: 0,
  1420. //plotBands: [{
  1421. // events: {},
  1422. // zIndex: 1,
  1423. // labels: { align, x, verticalAlign, y, style, rotation, textAlign }
  1424. //}],
  1425. //plotLines: [{
  1426. // events: {}
  1427. // dashStyle: {}
  1428. // zIndex:
  1429. // labels: { align, x, verticalAlign, y, style, rotation, textAlign }
  1430. //}],
  1431. //reversed: false,
  1432. // showFirstLabel: true,
  1433. // showLastLabel: true,
  1434. startOfWeek: 1,
  1435. startOnTick: false,
  1436. tickColor: '#C0D0E0',
  1437. //tickInterval: null,
  1438. tickLength: 5,
  1439. tickmarkPlacement: 'between', // on or between
  1440. tickPixelInterval: 100,
  1441. tickPosition: 'outside',
  1442. tickWidth: 1,
  1443. title: {
  1444. //text: null,
  1445. align: 'middle', // low, middle or high
  1446. //margin: 0 for horizontal, 10 for vertical axes,
  1447. //rotation: 0,
  1448. //side: 'outside',
  1449. style: {
  1450. color: '#6D869F',
  1451. //font: defaultFont.replace('normal', 'bold')
  1452. fontWeight: 'bold'
  1453. }
  1454. //x: 0,
  1455. //y: 0
  1456. },
  1457. type: 'linear' // linear, logarithmic or datetime
  1458. },
  1459. defaultYAxisOptions = merge(defaultXAxisOptions, {
  1460. endOnTick: true,
  1461. gridLineWidth: 1,
  1462. tickPixelInterval: 72,
  1463. showLastLabel: true,
  1464. labels: {
  1465. align: 'right',
  1466. x: -8,
  1467. y: 3
  1468. },
  1469. lineWidth: 0,
  1470. maxPadding: 0.05,
  1471. minPadding: 0.05,
  1472. startOnTick: true,
  1473. tickWidth: 0,
  1474. title: {
  1475. rotation: 270,
  1476. text: 'Y-values'
  1477. },
  1478. stackLabels: {
  1479. enabled: false,
  1480. //align: dynamic,
  1481. //y: dynamic,
  1482. //x: dynamic,
  1483. //verticalAlign: dynamic,
  1484. //textAlign: dynamic,
  1485. //rotation: 0,
  1486. formatter: function () {
  1487. return this.total;
  1488. },
  1489. style: defaultLabelOptions.style
  1490. }
  1491. }),
  1492. defaultLeftAxisOptions = {
  1493. labels: {
  1494. align: 'right',
  1495. x: -8,
  1496. y: null
  1497. },
  1498. title: {
  1499. rotation: 270
  1500. }
  1501. },
  1502. defaultRightAxisOptions = {
  1503. labels: {
  1504. align: 'left',
  1505. x: 8,
  1506. y: null
  1507. },
  1508. title: {
  1509. rotation: 90
  1510. }
  1511. },
  1512. defaultBottomAxisOptions = { // horizontal axis
  1513. labels: {
  1514. align: 'center',
  1515. x: 0,
  1516. y: 14
  1517. // overflow: undefined // docs - can be 'justify'
  1518. // staggerLines: null
  1519. },
  1520. title: {
  1521. rotation: 0
  1522. }
  1523. },
  1524. defaultTopAxisOptions = merge(defaultBottomAxisOptions, {
  1525. labels: {
  1526. y: -5,
  1527. overflow: 'justify'
  1528. // staggerLines: null
  1529. }
  1530. });
  1531. /*jslint white: false*/
  1532. // Series defaults
  1533. var defaultPlotOptions = defaultOptions.plotOptions,
  1534. defaultSeriesOptions = defaultPlotOptions.line;
  1535. //defaultPlotOptions.line = merge(defaultSeriesOptions);
  1536. defaultPlotOptions.spline = merge(defaultSeriesOptions);
  1537. defaultPlotOptions.scatter = merge(defaultSeriesOptions, {
  1538. lineWidth: 0,
  1539. states: {
  1540. hover: {
  1541. lineWidth: 0
  1542. }
  1543. },
  1544. tooltip: {
  1545. headerFormat: '<span style="font-size: 10px; color:{series.color}">{series.name}</span><br/>',
  1546. pointFormat: 'x: <b>{point.x}</b><br/>y: <b>{point.y}</b><br/>'
  1547. }
  1548. });
  1549. defaultPlotOptions.area = merge(defaultSeriesOptions, {
  1550. threshold: 0
  1551. // lineColor: null, // overrides color, but lets fillColor be unaltered
  1552. // fillOpacity: 0.75,
  1553. // fillColor: null
  1554. });
  1555. defaultPlotOptions.areaspline = merge(defaultPlotOptions.area);
  1556. defaultPlotOptions.column = merge(defaultSeriesOptions, {
  1557. borderColor: '#FFFFFF',
  1558. borderWidth: 1,
  1559. borderRadius: 0,
  1560. //colorByPoint: undefined,
  1561. groupPadding: 0.2,
  1562. marker: null, // point options are specified in the base options
  1563. pointPadding: 0.1,
  1564. //pointWidth: null,
  1565. minPointLength: 0,
  1566. cropThreshold: 50, // when there are more points, they will not animate out of the chart on xAxis.setExtremes
  1567. pointRange: null, // null means auto, meaning 1 in a categorized axis and least distance between points if not categories
  1568. states: {
  1569. hover: {
  1570. brightness: 0.1,
  1571. shadow: false
  1572. },
  1573. select: {
  1574. color: '#C0C0C0',
  1575. borderColor: '#000000',
  1576. shadow: false
  1577. }
  1578. },
  1579. dataLabels: {
  1580. y: null,
  1581. verticalAlign: null
  1582. },
  1583. threshold: 0
  1584. });
  1585. defaultPlotOptions.bar = merge(defaultPlotOptions.column, {
  1586. dataLabels: {
  1587. align: 'left',
  1588. x: 5,
  1589. y: null,
  1590. verticalAlign: 'middle'
  1591. }
  1592. });
  1593. defaultPlotOptions.pie = merge(defaultSeriesOptions, {
  1594. //dragType: '', // n/a
  1595. borderColor: '#FFFFFF',
  1596. borderWidth: 1,
  1597. center: ['50%', '50%'],
  1598. colorByPoint: true, // always true for pies
  1599. dataLabels: {
  1600. // align: null,
  1601. // connectorWidth: 1,
  1602. // connectorColor: point.color,
  1603. // connectorPadding: 5,
  1604. distance: 30,
  1605. enabled: true,
  1606. formatter: function () {
  1607. return this.point.name;
  1608. },
  1609. // softConnector: true,
  1610. y: 5
  1611. },
  1612. //innerSize: 0,
  1613. legendType: 'point',
  1614. marker: null, // point options are specified in the base options
  1615. size: '75%',
  1616. showInLegend: false,
  1617. slicedOffset: 10,
  1618. states: {
  1619. hover: {
  1620. brightness: 0.1,
  1621. shadow: false
  1622. }
  1623. }
  1624. });
  1625. // set the default time methods
  1626. setTimeMethods();
  1627. /**
  1628. * Set the time methods globally based on the useUTC option. Time method can be either
  1629. * local time or UTC (default).
  1630. */
  1631. function setTimeMethods() {
  1632. var useUTC = defaultOptions.global.useUTC,
  1633. GET = useUTC ? 'getUTC' : 'get',
  1634. SET = useUTC ? 'setUTC' : 'set';
  1635. makeTime = useUTC ? Date.UTC : function (year, month, date, hours, minutes, seconds) {
  1636. return new Date(
  1637. year,
  1638. month,
  1639. pick(date, 1),
  1640. pick(hours, 0),
  1641. pick(minutes, 0),
  1642. pick(seconds, 0)
  1643. ).getTime();
  1644. };
  1645. getMinutes = GET + 'Minutes';
  1646. getHours = GET + 'Hours';
  1647. getDay = GET + 'Day';
  1648. getDate = GET + 'Date';
  1649. getMonth = GET + 'Month';
  1650. getFullYear = GET + 'FullYear';
  1651. setMinutes = SET + 'Minutes';
  1652. setHours = SET + 'Hours';
  1653. setDate = SET + 'Date';
  1654. setMonth = SET + 'Month';
  1655. setFullYear = SET + 'FullYear';
  1656. }
  1657. /**
  1658. * Merge the default options with custom options and return the new options structure
  1659. * @param {Object} options The new custom options
  1660. */
  1661. function setOptions(options) {
  1662. // Pull out axis options and apply them to the respective default axis options
  1663. defaultXAxisOptions = merge(defaultXAxisOptions, options.xAxis);
  1664. defaultYAxisOptions = merge(defaultYAxisOptions, options.yAxis);
  1665. options.xAxis = options.yAxis = UNDEFINED;
  1666. // Merge in the default options
  1667. defaultOptions = merge(defaultOptions, options);
  1668. // Apply UTC
  1669. setTimeMethods();
  1670. return defaultOptions;
  1671. }
  1672. /**
  1673. * Get the updated default options. Merely exposing defaultOptions for outside modules
  1674. * isn't enough because the setOptions method creates a new object.
  1675. */
  1676. function getOptions() {
  1677. return defaultOptions;
  1678. }
  1679. /**
  1680. * Handle color operations. The object methods are chainable.
  1681. * @param {String} input The input color in either rbga or hex format
  1682. */
  1683. var Color = function (input) {
  1684. // declare variables
  1685. var rgba = [], result;
  1686. /**
  1687. * Parse the input color to rgba array
  1688. * @param {String} input
  1689. */
  1690. function init(input) {
  1691. // rgba
  1692. result = /rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]?(?:\.[0-9]+)?)\s*\)/.exec(input);
  1693. if (result) {
  1694. rgba = [pInt(result[1]), pInt(result[2]), pInt(result[3]), parseFloat(result[4], 10)];
  1695. } else { // hex
  1696. result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(input);
  1697. if (result) {
  1698. rgba = [pInt(result[1], 16), pInt(result[2], 16), pInt(result[3], 16), 1];
  1699. }
  1700. }
  1701. }
  1702. /**
  1703. * Return the color a specified format
  1704. * @param {String} format
  1705. */
  1706. function get(format) {
  1707. var ret;
  1708. // it's NaN if gradient colors on a column chart
  1709. if (rgba && !isNaN(rgba[0])) {
  1710. if (format === 'rgb') {
  1711. ret = 'rgb(' + rgba[0] + ',' + rgba[1] + ',' + rgba[2] + ')';
  1712. } else if (format === 'a') {
  1713. ret = rgba[3];
  1714. } else {
  1715. ret = 'rgba(' + rgba.join(',') + ')';
  1716. }
  1717. } else {
  1718. ret = input;
  1719. }
  1720. return ret;
  1721. }
  1722. /**
  1723. * Brighten the color
  1724. * @param {Number} alpha
  1725. */
  1726. function brighten(alpha) {
  1727. if (isNumber(alpha) && alpha !== 0) {
  1728. var i;
  1729. for (i = 0; i < 3; i++) {
  1730. rgba[i] += pInt(alpha * 255);
  1731. if (rgba[i] < 0) {
  1732. rgba[i] = 0;
  1733. }
  1734. if (rgba[i] > 255) {
  1735. rgba[i] = 255;
  1736. }
  1737. }
  1738. }
  1739. return this;
  1740. }
  1741. /**
  1742. * Set the color's opacity to a given alpha value
  1743. * @param {Number} alpha
  1744. */
  1745. function setOpacity(alpha) {
  1746. rgba[3] = alpha;
  1747. return this;
  1748. }
  1749. // initialize: parse the input
  1750. init(input);
  1751. // public methods
  1752. return {
  1753. get: get,
  1754. brighten: brighten,
  1755. setOpacity: setOpacity
  1756. };
  1757. };
  1758. /**
  1759. * A wrapper object for SVG elements
  1760. */
  1761. function SVGElement() {}
  1762. SVGElement.prototype = {
  1763. /**
  1764. * Initialize the SVG renderer
  1765. * @param {Object} renderer
  1766. * @param {String} nodeName
  1767. */
  1768. init: function (renderer, nodeName) {
  1769. var wrapper = this;
  1770. wrapper.element = nodeName === 'span' ?
  1771. createElement(nodeName) :
  1772. doc.createElementNS(SVG_NS, nodeName);
  1773. wrapper.renderer = renderer;
  1774. /**
  1775. * A collection of attribute setters. These methods, if defined, are called right before a certain
  1776. * attribute is set on an element wrapper. Returning false prevents the default attribute
  1777. * setter to run. Returning a value causes the default setter to set that value. Used in
  1778. * Renderer.label.
  1779. */
  1780. wrapper.attrSetters = {};
  1781. },
  1782. /**
  1783. * Animate a given attribute
  1784. * @param {Object} params
  1785. * @param {Number} options The same options as in jQuery animation
  1786. * @param {Function} complete Function to perform at the end of animation
  1787. */
  1788. animate: function (params, options, complete) {
  1789. var animOptions = pick(options, globalAnimation, true);
  1790. stop(this); // stop regardless of animation actually running, or reverting to .attr (#607)
  1791. if (animOptions) {
  1792. animOptions = merge(animOptions);
  1793. if (complete) { // allows using a callback with the global animation without overwriting it
  1794. animOptions.complete = complete;
  1795. }
  1796. animate(this, params, animOptions);
  1797. } else {
  1798. this.attr(params);
  1799. if (complete) {
  1800. complete();
  1801. }
  1802. }
  1803. },
  1804. /**
  1805. * Set or get a given attribute
  1806. * @param {Object|String} hash
  1807. * @param {Mixed|Undefined} val
  1808. */
  1809. attr: function (hash, val) {
  1810. var wrapper = this,
  1811. key,
  1812. value,
  1813. result,
  1814. i,
  1815. child,
  1816. element = wrapper.element,
  1817. nodeName = element.nodeName,
  1818. renderer = wrapper.renderer,
  1819. skipAttr,
  1820. titleNode,
  1821. attrSetters = wrapper.attrSetters,
  1822. shadows = wrapper.shadows,
  1823. hasSetSymbolSize,
  1824. ret = wrapper;
  1825. // single key-value pair
  1826. if (isString(hash) && defined(val)) {
  1827. key = hash;
  1828. hash = {};
  1829. hash[key] = val;
  1830. }
  1831. // used as a getter: first argument is a string, second is undefined
  1832. if (isString(hash)) {
  1833. key = hash;
  1834. if (nodeName === 'circle') {
  1835. key = { x: 'cx', y: 'cy' }[key] || key;
  1836. } else if (key === 'strokeWidth') {
  1837. key = 'stroke-width';
  1838. }
  1839. ret = attr(element, key) || wrapper[key] || 0;
  1840. if (key !== 'd' && key !== 'visibility') { // 'd' is string in animation step
  1841. ret = parseFloat(ret);
  1842. }
  1843. // setter
  1844. } else {
  1845. for (key in hash) {
  1846. skipAttr = false; // reset
  1847. value = hash[key];
  1848. // check for a specific attribute setter
  1849. result = attrSetters[key] && attrSetters[key](value, key);
  1850. if (result !== false) {
  1851. if (result !== UNDEFINED) {
  1852. value = result; // the attribute setter has returned a new value to set
  1853. }
  1854. // paths
  1855. if (key === 'd') {
  1856. if (value && value.join) { // join path
  1857. value = value.join(' ');
  1858. }
  1859. if (/(NaN| {2}|^$)/.test(value)) {
  1860. value = 'M 0 0';
  1861. }
  1862. wrapper.d = value; // shortcut for animations
  1863. // update child tspans x values
  1864. } else if (key === 'x' && nodeName === 'text') {
  1865. for (i = 0; i < element.childNodes.length; i++) {
  1866. child = element.childNodes[i];
  1867. // if the x values are equal, the tspan represents a linebreak
  1868. if (attr(child, 'x') === attr(element, 'x')) {
  1869. //child.setAttribute('x', value);
  1870. attr(child, 'x', value);
  1871. }
  1872. }
  1873. if (wrapper.rotation) {
  1874. attr(element, 'transform', 'rotate(' + wrapper.rotation + ' ' + value + ' ' +
  1875. pInt(hash.y || attr(element, 'y')) + ')');
  1876. }
  1877. // apply gradients
  1878. } else if (key === 'fill') {
  1879. value = renderer.color(value, element, key);
  1880. // circle x and y
  1881. } else if (nodeName === 'circle' && (key === 'x' || key === 'y')) {
  1882. key = { x: 'cx', y: 'cy' }[key] || key;
  1883. // rectangle border radius
  1884. } else if (nodeName === 'rect' && key === 'r') {
  1885. attr(element, {
  1886. rx: value,
  1887. ry: value
  1888. });
  1889. skipAttr = true;
  1890. // translation and text rotation
  1891. } else if (key === 'translateX' || key === 'translateY' || key === 'rotation' || key === 'verticalAlign') {
  1892. wrapper[key] = value;
  1893. wrapper.updateTransform();
  1894. skipAttr = true;
  1895. // apply opacity as subnode (required by legacy WebKit and Batik)
  1896. } else if (key === 'stroke') {
  1897. value = renderer.color(value, element, key);
  1898. // emulate VML's dashstyle implementation
  1899. } else if (key === 'dashstyle') {
  1900. key = 'stroke-dasharray';
  1901. value = value && value.toLowerCase();
  1902. if (value === 'solid') {
  1903. value = NONE;
  1904. } else if (value) {
  1905. value = value
  1906. .replace('shortdashdotdot', '3,1,1,1,1,1,')
  1907. .replace('shortdashdot', '3,1,1,1')
  1908. .replace('shortdot', '1,1,')
  1909. .replace('shortdash', '3,1,')
  1910. .replace('longdash', '8,3,')
  1911. .replace(/dot/g, '1,3,')
  1912. .replace('dash', '4,3,')
  1913. .replace(/,$/, '')
  1914. .split(','); // ending comma
  1915. i = value.length;
  1916. while (i--) {
  1917. value[i] = pInt(value[i]) * hash['stroke-width'];
  1918. }
  1919. value = value.join(',');
  1920. }
  1921. // special
  1922. } else if (key === 'isTracker') {
  1923. wrapper[key] = value;
  1924. // IE9/MooTools combo: MooTools returns objects instead of numbers and IE9 Beta 2
  1925. // is unable to cast them. Test again with final IE9.
  1926. } else if (key === 'width') {
  1927. value = pInt(value);
  1928. // Text alignment
  1929. } else if (key === 'align') {
  1930. key = 'text-anchor';
  1931. value = { left: 'start', center: 'middle', right: 'end' }[value];
  1932. // Title requires a subnode, #431
  1933. } else if (key === 'title') {
  1934. titleNode = element.getElementsByTagName('title')[0];
  1935. if (!titleNode) {
  1936. titleNode = doc.createElementNS(SVG_NS, 'title');
  1937. element.appendChild(titleNode);
  1938. }
  1939. titleNode.textContent = value;
  1940. }
  1941. // jQuery animate changes case
  1942. if (key === 'strokeWidth') {
  1943. key = 'stroke-width';
  1944. }
  1945. // Chrome/Win < 6 bug (http://code.google.com/p/chromium/issues/detail?id=15461)
  1946. if (isWebKit && key === 'stroke-width' && value === 0) {
  1947. value = 0.000001;
  1948. }
  1949. // symbols
  1950. if (wrapper.symbolName && /^(x|y|width|height|r|start|end|innerR|anchorX|anchorY)/.test(key)) {
  1951. if (!hasSetSymbolSize) {
  1952. wrapper.symbolAttr(hash);
  1953. hasSetSymbolSize = true;
  1954. }
  1955. skipAttr = true;
  1956. }
  1957. // let the shadow follow the main element
  1958. if (shadows && /^(width|height|visibility|x|y|d|transform)$/.test(key)) {
  1959. i = shadows.length;
  1960. while (i--) {
  1961. attr(shadows[i], key, value);
  1962. }
  1963. }
  1964. // validate heights
  1965. if ((key === 'width' || key === 'height') && nodeName === 'rect' && value < 0) {
  1966. value = 0;
  1967. }
  1968. if (key === 'text') {
  1969. // only one node allowed
  1970. wrapper.textStr = value;
  1971. if (wrapper.added) {
  1972. renderer.buildText(wrapper);
  1973. }
  1974. } else if (!skipAttr) {
  1975. attr(element, key, value);
  1976. }
  1977. }
  1978. }
  1979. }
  1980. // Workaround for our #732, WebKit's issue https://bugs.webkit.org/show_bug.cgi?id=78385
  1981. // TODO: If the WebKit team fix this bug before the final release of Chrome 18, remove the workaround.
  1982. if (isWebKit && /Chrome\/(18|19)/.test(userAgent)) {
  1983. if (nodeName === 'text' && (hash.x !== UNDEFINED || hash.y !== UNDEFINED)) {
  1984. var parent = element.parentNode,
  1985. next = element.nextSibling;
  1986. if (parent) {
  1987. parent.removeChild(element);
  1988. if (next) {
  1989. parent.insertBefore(element, next);
  1990. } else {
  1991. parent.appendChild(element);
  1992. }
  1993. }
  1994. }
  1995. }
  1996. // End of workaround for #732
  1997. return ret;
  1998. },
  1999. /**
  2000. * If one of the symbol size affecting parameters are changed,
  2001. * check all the others only once for each call to an element's
  2002. * .attr() method
  2003. * @param {Object} hash
  2004. */
  2005. symbolAttr: function (hash) {
  2006. var wrapper = this;
  2007. each(['x', 'y', 'r', 'start', 'end', 'width', 'height', 'innerR', 'anchorX', 'anchorY'], function (key) {
  2008. wrapper[key] = pick(hash[key], wrapper[key]);
  2009. });
  2010. wrapper.attr({
  2011. d: wrapper.renderer.symbols[wrapper.symbolName](wrapper.x, wrapper.y, wrapper.width, wrapper.height, wrapper)
  2012. });
  2013. },
  2014. /**
  2015. * Apply a clipping path to this object
  2016. * @param {String} id
  2017. */
  2018. clip: function (clipRect) {
  2019. return this.attr('clip-path', 'url(' + this.renderer.url + '#' + clipRect.id + ')');
  2020. },
  2021. /**
  2022. * Calculate the coordinates needed for drawing a rectangle crisply and return the
  2023. * calculated attributes
  2024. * @param {Number} strokeWidth
  2025. * @param {Number} x
  2026. * @param {Number} y
  2027. * @param {Number} width
  2028. * @param {Number} height
  2029. */
  2030. crisp: function (strokeWidth, x, y, width, height) {
  2031. var wrapper = this,
  2032. key,
  2033. attribs = {},
  2034. values = {},
  2035. normalizer;
  2036. strokeWidth = strokeWidth || wrapper.strokeWidth || (wrapper.attr && wrapper.attr('stroke-width')) || 0;
  2037. normalizer = mathRound(strokeWidth) % 2 / 2; // mathRound because strokeWidth can sometimes have roundoff errors
  2038. // normalize for crisp edges
  2039. values.x = mathFloor(x || wrapper.x || 0) + normalizer;
  2040. values.y = mathFloor(y || wrapper.y || 0) + normalizer;
  2041. values.width = mathFloor((width || wrapper.width || 0) - 2 * normalizer);
  2042. values.height = mathFloor((height || wrapper.height || 0) - 2 * normalizer);
  2043. values.strokeWidth = strokeWidth;
  2044. for (key in values) {
  2045. if (wrapper[key] !== values[key]) { // only set attribute if changed
  2046. wrapper[key] = attribs[key] = values[key];
  2047. }
  2048. }
  2049. return attribs;
  2050. },
  2051. /**
  2052. * Set styles for the element
  2053. * @param {Object} styles
  2054. */
  2055. css: function (styles) {
  2056. /*jslint unparam: true*//* allow unused param a in the regexp function below */
  2057. var elemWrapper = this,
  2058. elem = elemWrapper.element,
  2059. textWidth = styles && styles.width && elem.nodeName === 'text',
  2060. n,
  2061. serializedCss = '',
  2062. hyphenate = function (a, b) { return '-' + b.toLowerCase(); };
  2063. /*jslint unparam: false*/
  2064. // convert legacy
  2065. if (styles && styles.color) {
  2066. styles.fill = styles.color;
  2067. }
  2068. // Merge the new styles with the old ones
  2069. styles = extend(
  2070. elemWrapper.styles,
  2071. styles
  2072. );
  2073. // store object
  2074. elemWrapper.styles = styles;
  2075. // serialize and set style attribute
  2076. if (isIE && !hasSVG) { // legacy IE doesn't support setting style attribute
  2077. if (textWidth) {
  2078. delete styles.width;
  2079. }
  2080. css(elemWrapper.element, styles);
  2081. } else {
  2082. for (n in styles) {
  2083. serializedCss += n.replace(/([A-Z])/g, hyphenate) + ':' + styles[n] + ';';
  2084. }
  2085. elemWrapper.attr({
  2086. style: serializedCss
  2087. });
  2088. }
  2089. // re-build text
  2090. if (textWidth && elemWrapper.added) {
  2091. elemWrapper.renderer.buildText(elemWrapper);
  2092. }
  2093. return elemWrapper;
  2094. },
  2095. /**
  2096. * Add an event listener
  2097. * @param {String} eventType
  2098. * @param {Function} handler
  2099. */
  2100. on: function (eventType, handler) {
  2101. var fn = handler;
  2102. // touch
  2103. if (hasTouch && eventType === 'click') {
  2104. eventType = 'touchstart';
  2105. fn = function (e) {
  2106. e.preventDefault();
  2107. handler();
  2108. };
  2109. }
  2110. // simplest possible event model for internal use
  2111. this.element['on' + eventType] = fn;
  2112. return this;
  2113. },
  2114. /**
  2115. * Move an object and its children by x and y values
  2116. * @param {Number} x
  2117. * @param {Number} y
  2118. */
  2119. translate: function (x, y) {
  2120. return this.attr({
  2121. translateX: x,
  2122. translateY: y
  2123. });
  2124. },
  2125. /**
  2126. * Invert a group, rotate and flip
  2127. */
  2128. invert: function () {
  2129. var wrapper = this;
  2130. wrapper.inverted = true;
  2131. wrapper.updateTransform();
  2132. return wrapper;
  2133. },
  2134. /**
  2135. * Apply CSS to HTML elements. This is used in text within SVG rendering and
  2136. * by the VML renderer
  2137. */
  2138. htmlCss: function (styles) {
  2139. var wrapper = this,
  2140. element = wrapper.element,
  2141. textWidth = styles && element.tagName === 'SPAN' && styles.width;
  2142. if (textWidth) {
  2143. delete styles.width;
  2144. wrapper.textWidth = textWidth;
  2145. wrapper.updateTransform();
  2146. }
  2147. wrapper.styles = extend(wrapper.styles, styles);
  2148. css(wrapper.element, styles);
  2149. return wrapper;
  2150. },
  2151. /**
  2152. * VML and useHTML method for calculating the bounding box based on offsets
  2153. * @param {Boolean} refresh Whether to force a fresh value from the DOM or to
  2154. * use the cached value
  2155. *
  2156. * @return {Object} A hash containing values for x, y, width and height
  2157. */
  2158. htmlGetBBox: function (refresh) {
  2159. var wrapper = this,
  2160. element = wrapper.element,
  2161. bBox = wrapper.bBox;
  2162. // faking getBBox in exported SVG in legacy IE
  2163. if (!bBox || refresh) {
  2164. // faking getBBox in exported SVG in legacy IE
  2165. if (element.nodeName === 'text') {
  2166. element.style.position = ABSOLUTE;
  2167. }
  2168. bBox = wrapper.bBox = {
  2169. x: element.offsetLeft,
  2170. y: element.offsetTop,
  2171. width: element.offsetWidth,
  2172. height: element.offsetHeight
  2173. };
  2174. }
  2175. return bBox;
  2176. },
  2177. /**
  2178. * VML override private method to update elements based on internal
  2179. * properties based on SVG transform
  2180. */
  2181. htmlUpdateTransform: function () {
  2182. // aligning non added elements is expensive
  2183. if (!this.added) {
  2184. this.alignOnAdd = true;
  2185. return;
  2186. }
  2187. var wrapper = this,
  2188. renderer = wrapper.renderer,
  2189. elem = wrapper.element,
  2190. translateX = wrapper.translateX || 0,
  2191. translateY = wrapper.translateY || 0,
  2192. x = wrapper.x || 0,
  2193. y = wrapper.y || 0,
  2194. align = wrapper.textAlign || 'left',
  2195. alignCorrection = { left: 0, center: 0.5, right: 1 }[align],
  2196. nonLeft = align && align !== 'left',
  2197. shadows = wrapper.shadows;
  2198. // apply translate
  2199. if (translateX || translateY) {
  2200. css(elem, {
  2201. marginLeft: translateX,
  2202. marginTop: translateY
  2203. });
  2204. if (shadows) { // used in labels/tooltip
  2205. each(shadows, function (shadow) {
  2206. css(shadow, {
  2207. marginLeft: translateX + 1,
  2208. marginTop: translateY + 1
  2209. });
  2210. });
  2211. }
  2212. }
  2213. // apply inversion
  2214. if (wrapper.inverted) { // wrapper is a group
  2215. each(elem.childNodes, function (child) {
  2216. renderer.invertChild(child, elem);
  2217. });
  2218. }
  2219. if (elem.tagName === 'SPAN') {
  2220. var width, height,
  2221. rotation = wrapper.rotation,
  2222. baseline,
  2223. radians = 0,
  2224. costheta = 1,
  2225. sintheta = 0,
  2226. quad,
  2227. textWidth = pInt(wrapper.textWidth),
  2228. xCorr = wrapper.xCorr || 0,
  2229. yCorr = wrapper.yCorr || 0,
  2230. currentTextTransform = [rotation, align, elem.innerHTML, wrapper.textWidth].join(',');
  2231. if (currentTextTransform !== wrapper.cTT) { // do the calculations and DOM access only if properties changed
  2232. if (defined(rotation)) {
  2233. radians = rotation * deg2rad; // deg to rad
  2234. costheta = mathCos(radians);
  2235. sintheta = mathSin(radians);
  2236. // Adjust for alignment and rotation. Rotation of useHTML content is not yet implemented
  2237. // but it can probably be implemented for Firefox 3.5+ on user request. FF3.5+
  2238. // has support for CSS3 transform. The getBBox method also needs to be updated
  2239. // to compensate for the rotation, like it currently does for SVG.
  2240. // Test case: http://highcharts.com/tests/?file=text-rotation
  2241. css(elem, {
  2242. filter: rotation ? ['progid:DXImageTransform.Microsoft.Matrix(M11=', costheta,
  2243. ', M12=', -sintheta, ', M21=', sintheta, ', M22=', costheta,
  2244. ', sizingMethod=\'auto expand\')'].join('') : NONE
  2245. });
  2246. }
  2247. width = pick(wrapper.elemWidth, elem.offsetWidth);
  2248. height = pick(wrapper.elemHeight, elem.offsetHeight);
  2249. // update textWidth
  2250. if (width > textWidth) {
  2251. css(elem, {
  2252. width: textWidth + PX,
  2253. display: 'block',
  2254. whiteSpace: 'normal'
  2255. });
  2256. width = textWidth;
  2257. }
  2258. // correct x and y
  2259. baseline = renderer.fontMetrics(elem.style.fontSize).b;
  2260. xCorr = costheta < 0 && -width;
  2261. yCorr = sintheta < 0 && -height;
  2262. // correct for baseline and corners spilling out after rotation
  2263. quad = costheta * sintheta < 0;
  2264. xCorr += sintheta * baseline * (quad ? 1 - alignCorrection : alignCorrection);
  2265. yCorr -= costheta * baseline * (rotation ? (quad ? alignCorrection : 1 - alignCorrection) : 1);
  2266. // correct for the length/height of the text
  2267. if (nonLeft) {
  2268. xCorr -= width * alignCorrection * (costheta < 0 ? -1 : 1);
  2269. if (rotation) {
  2270. yCorr -= height * alignCorrection * (sintheta < 0 ? -1 : 1);
  2271. }
  2272. css(elem, {
  2273. textAlign: align
  2274. });
  2275. }
  2276. // record correction
  2277. wrapper.xCorr = xCorr;
  2278. wrapper.yCorr = yCorr;
  2279. }
  2280. // apply position with correction
  2281. css(elem, {
  2282. left: (x + xCorr) + PX,
  2283. top: (y + yCorr) + PX
  2284. });
  2285. // record current text transform
  2286. wrapper.cTT = currentTextTransform;
  2287. }
  2288. },
  2289. /**
  2290. * Private method to update the transform attribute based on internal
  2291. * properties
  2292. */
  2293. updateTransform: function () {
  2294. var wrapper = this,
  2295. translateX = wrapper.translateX || 0,
  2296. translateY = wrapper.translateY || 0,
  2297. inverted = wrapper.inverted,
  2298. rotation = wrapper.rotation,
  2299. transform = [];
  2300. // flipping affects translate as adjustment for flipping around the group's axis
  2301. if (inverted) {
  2302. translateX += wrapper.attr('width');
  2303. translateY += wrapper.attr('height');
  2304. }
  2305. // apply translate
  2306. if (translateX || translateY) {
  2307. transform.push('translate(' + translateX + ',' + translateY + ')');
  2308. }
  2309. // apply rotation
  2310. if (inverted) {
  2311. transform.push('rotate(90) scale(-1,1)');
  2312. } else if (rotation) { // text rotation
  2313. transform.push('rotate(' + rotation + ' ' + wrapper.x + ' ' + wrapper.y + ')');
  2314. }
  2315. if (transform.length) {
  2316. attr(wrapper.element, 'transform', transform.join(' '));
  2317. }
  2318. },
  2319. /**
  2320. * Bring the element to the front
  2321. */
  2322. toFront: function () {
  2323. var element = this.element;
  2324. element.parentNode.appendChild(element);
  2325. return this;
  2326. },
  2327. /**
  2328. * Break down alignment options like align, verticalAlign, x and y
  2329. * to x and y relative to the chart.
  2330. *
  2331. * @param {Object} alignOptions
  2332. * @param {Boolean} alignByTranslate
  2333. * @param {Object} box The box to align to, needs a width and height
  2334. *
  2335. */
  2336. align: function (alignOptions, alignByTranslate, box) {
  2337. var elemWrapper = this;
  2338. if (!alignOptions) { // called on resize
  2339. alignOptions = elemWrapper.alignOptions;
  2340. alignByTranslate = elemWrapper.alignByTranslate;
  2341. } else { // first call on instanciate
  2342. elemWrapper.alignOptions = alignOptions;
  2343. elemWrapper.alignByTranslate = alignByTranslate;
  2344. if (!box) { // boxes other than renderer handle this internally
  2345. elemWrapper.renderer.alignedObjects.push(elemWrapper);
  2346. }
  2347. }
  2348. box = pick(box, elemWrapper.renderer);
  2349. var align = alignOptions.align,
  2350. vAlign = alignOptions.verticalAlign,
  2351. x = (box.x || 0) + (alignOptions.x || 0), // default: left align
  2352. y = (box.y || 0) + (alignOptions.y || 0), // default: top align
  2353. attribs = {};
  2354. // align
  2355. if (/^(right|center)$/.test(align)) {
  2356. x += (box.width - (alignOptions.width || 0)) /
  2357. { right: 1, center: 2 }[align];
  2358. }
  2359. attribs[alignByTranslate ? 'translateX' : 'x'] = mathRound(x);
  2360. // vertical align
  2361. if (/^(bottom|middle)$/.test(vAlign)) {
  2362. y += (box.height - (alignOptions.height || 0)) /
  2363. ({ bottom: 1, middle: 2 }[vAlign] || 1);
  2364. }
  2365. attribs[alignByTranslate ? 'translateY' : 'y'] = mathRound(y);
  2366. // animate only if already placed
  2367. elemWrapper[elemWrapper.placed ? 'animate' : 'attr'](attribs);
  2368. elemWrapper.placed = true;
  2369. elemWrapper.alignAttr = attribs;
  2370. return elemWrapper;
  2371. },
  2372. /**
  2373. * Get the bounding box (width, height, x and y) for the element
  2374. */
  2375. getBBox: function (refresh) {
  2376. var wrapper = this,
  2377. bBox,
  2378. width,
  2379. height,
  2380. rotation = wrapper.rotation,
  2381. element = wrapper.element,
  2382. rad = rotation * deg2rad;
  2383. // SVG elements
  2384. if (element.namespaceURI === SVG_NS) {
  2385. try { // Fails in Firefox if the container has display: none.
  2386. bBox = element.getBBox ?
  2387. // SVG: use extend because IE9 is not allowed to change width and height in case
  2388. // of rotation (below)
  2389. extend({}, element.getBBox()) :
  2390. // Canvas renderer: // TODO: can this be removed now that we're checking for the SVG NS?
  2391. {
  2392. width: element.offsetWidth,
  2393. height: element.offsetHeight
  2394. };
  2395. } catch (e) {}
  2396. // If the bBox is not set, the try-catch block above failed. The other condition
  2397. // is for Opera that returns a width of -Infinity on hidden elements.
  2398. if (!bBox || bBox.width < 0) {
  2399. bBox = { width: 0, height: 0 };
  2400. }
  2401. width = bBox.width;
  2402. height = bBox.height;
  2403. // adjust for rotated text
  2404. if (rotation) {
  2405. bBox.width = mathAbs(height * mathSin(rad)) + mathAbs(width * mathCos(rad));
  2406. bBox.height = mathAbs(height * mathCos(rad)) + mathAbs(width * mathSin(rad));
  2407. }
  2408. // VML Renderer or useHTML within SVG
  2409. } else {
  2410. bBox = wrapper.htmlGetBBox(refresh);
  2411. }
  2412. return bBox;
  2413. },
  2414. /**
  2415. * Show the element
  2416. */
  2417. show: function () {
  2418. return this.attr({ visibility: VISIBLE });
  2419. },
  2420. /**
  2421. * Hide the element
  2422. */
  2423. hide: function () {
  2424. return this.attr({ visibility: HIDDEN });
  2425. },
  2426. /**
  2427. * Add the element
  2428. * @param {Object|Undefined} parent Can be an element, an element wrapper or undefined
  2429. * to append the element to the renderer.box.
  2430. */
  2431. add: function (parent) {
  2432. var renderer = this.renderer,
  2433. parentWrapper = parent || renderer,
  2434. parentNode = parentWrapper.element || renderer.box,
  2435. childNodes = parentNode.childNodes,
  2436. element = this.element,
  2437. zIndex = attr(element, 'zIndex'),
  2438. otherElement,
  2439. otherZIndex,
  2440. i,
  2441. inserted;
  2442. // mark as inverted
  2443. this.parentInverted = parent && parent.inverted;
  2444. // build formatted text
  2445. if (this.textStr !== undefined) {
  2446. renderer.buildText(this);
  2447. }
  2448. // mark the container as having z indexed children
  2449. if (zIndex) {
  2450. parentWrapper.handleZ = true;
  2451. zIndex = pInt(zIndex);
  2452. }
  2453. // insert according to this and other elements' zIndex
  2454. if (parentWrapper.handleZ) { // this element or any of its siblings has a z index
  2455. for (i = 0; i < childNodes.length; i++) {
  2456. otherElement = childNodes[i];
  2457. otherZIndex = attr(otherElement, 'zIndex');
  2458. if (otherElement !== element && (
  2459. // insert before the first element with a higher zIndex
  2460. pInt(otherZIndex) > zIndex ||
  2461. // if no zIndex given, insert before the first element with a zIndex
  2462. (!defined(zIndex) && defined(otherZIndex))
  2463. )) {
  2464. parentNode.insertBefore(element, otherElement);
  2465. inserted = true;
  2466. break;
  2467. }
  2468. }
  2469. }
  2470. // default: append at the end
  2471. if (!inserted) {
  2472. parentNode.appendChild(element);
  2473. }
  2474. // mark as added
  2475. this.added = true;
  2476. // fire an event for internal hooks
  2477. fireEvent(this, 'add');
  2478. return this;
  2479. },
  2480. /**
  2481. * Removes a child either by removeChild or move to garbageBin.
  2482. * Issue 490; in VML removeChild results in Orphaned nodes according to sIEve, discardElement does not.
  2483. */
  2484. safeRemoveChild: function (element) {
  2485. var parentNode = element.parentNode;
  2486. if (parentNode) {
  2487. parentNode.removeChild(element);
  2488. }
  2489. },
  2490. /**
  2491. * Destroy the element and element wrapper
  2492. */
  2493. destroy: function () {
  2494. var wrapper = this,
  2495. element = wrapper.element || {},
  2496. shadows = wrapper.shadows,
  2497. box = wrapper.box,
  2498. key,
  2499. i;
  2500. // remove events
  2501. element.onclick = element.onmouseout = element.onmouseover = element.onmousemove = null;
  2502. stop(wrapper); // stop running animations
  2503. if (wrapper.clipPath) {
  2504. wrapper.clipPath = wrapper.clipPath.destroy();
  2505. }
  2506. // Destroy stops in case this is a gradient object
  2507. if (wrapper.stops) {
  2508. for (i = 0; i < wrapper.stops.length; i++) {
  2509. wrapper.stops[i] = wrapper.stops[i].destroy();
  2510. }
  2511. wrapper.stops = null;
  2512. }
  2513. // remove element
  2514. wrapper.safeRemoveChild(element);
  2515. // destroy shadows
  2516. if (shadows) {
  2517. each(shadows, function (shadow) {
  2518. wrapper.safeRemoveChild(shadow);
  2519. });
  2520. }
  2521. // destroy label box
  2522. if (box) {
  2523. box.destroy();
  2524. }
  2525. // remove from alignObjects
  2526. erase(wrapper.renderer.alignedObjects, wrapper);
  2527. for (key in wrapper) {
  2528. delete wrapper[key];
  2529. }
  2530. return null;
  2531. },
  2532. /**
  2533. * Empty a group element
  2534. */
  2535. empty: function () {
  2536. var element = this.element,
  2537. childNodes = element.childNodes,
  2538. i = childNodes.length;
  2539. while (i--) {
  2540. element.removeChild(childNodes[i]);
  2541. }
  2542. },
  2543. /**
  2544. * Add a shadow to the element. Must be done after the element is added to the DOM
  2545. * @param {Boolean} apply
  2546. */
  2547. shadow: function (apply, group) {
  2548. var shadows = [],
  2549. i,
  2550. shadow,
  2551. element = this.element,
  2552. // compensate for inverted plot area
  2553. transform = this.parentInverted ? '(-1,-1)' : '(1,1)';
  2554. if (apply) {
  2555. for (i = 1; i <= 3; i++) {
  2556. shadow = element.cloneNode(0);
  2557. attr(shadow, {
  2558. 'isShadow': 'true',
  2559. 'stroke': 'rgb(0, 0, 0)',
  2560. 'stroke-opacity': 0.05 * i,
  2561. 'stroke-width': 7 - 2 * i,
  2562. 'transform': 'translate' + transform,
  2563. 'fill': NONE
  2564. });
  2565. if (group) {
  2566. group.element.appendChild(shadow);
  2567. } else {
  2568. element.parentNode.insertBefore(shadow, element);
  2569. }
  2570. shadows.push(shadow);
  2571. }
  2572. this.shadows = shadows;
  2573. }
  2574. return this;
  2575. }
  2576. };
  2577. /**
  2578. * The default SVG renderer
  2579. */
  2580. var SVGRenderer = function () {
  2581. this.init.apply(this, arguments);
  2582. };
  2583. SVGRenderer.prototype = {
  2584. Element: SVGElement,
  2585. /**
  2586. * Initialize the SVGRenderer
  2587. * @param {Object} container
  2588. * @param {Number} width
  2589. * @param {Number} height
  2590. * @param {Boolean} forExport
  2591. */
  2592. init: function (container, width, height, forExport) {
  2593. var renderer = this,
  2594. loc = location,
  2595. boxWrapper;
  2596. boxWrapper = renderer.createElement('svg')
  2597. .attr({
  2598. xmlns: SVG_NS,
  2599. version: '1.1'
  2600. });
  2601. container.appendChild(boxWrapper.element);
  2602. // object properties
  2603. renderer.isSVG = true;
  2604. renderer.box = boxWrapper.element;
  2605. renderer.boxWrapper = boxWrapper;
  2606. renderer.alignedObjects = [];
  2607. renderer.url = isIE ? '' : loc.href.replace(/#.*?$/, '')
  2608. .replace(/([\('\)])/g, '\\$1'); // Page url used for internal references. #24, #672.
  2609. renderer.defs = this.createElement('defs').add();
  2610. renderer.forExport = forExport;
  2611. renderer.gradients = {}; // Object where gradient SvgElements are stored
  2612. renderer.setSize(width, height, false);
  2613. // Issue 110 workaround:
  2614. // In Firefox, if a div is positioned by percentage, its pixel position may land
  2615. // between pixels. The container itself doesn't display this, but an SVG element
  2616. // inside this container will be drawn at subpixel precision. In order to draw
  2617. // sharp lines, this must be compensated for. This doesn't seem to work inside
  2618. // iframes though (like in jsFiddle).
  2619. var subPixelFix, rect;
  2620. if (isFirefox && container.getBoundingClientRect) {
  2621. renderer.subPixelFix = subPixelFix = function () {
  2622. css(container, { left: 0, top: 0 });
  2623. rect = container.getBoundingClientRect();
  2624. css(container, {
  2625. left: (-(rect.left - pInt(rect.left))) + PX,
  2626. top: (-(rect.top - pInt(rect.top))) + PX
  2627. });
  2628. };
  2629. // run the fix now
  2630. subPixelFix();
  2631. // run it on resize
  2632. addEvent(win, 'resize', subPixelFix);
  2633. }
  2634. },
  2635. /**
  2636. * Destroys the renderer and its allocated members.
  2637. */
  2638. destroy: function () {
  2639. var renderer = this,
  2640. rendererDefs = renderer.defs;
  2641. renderer.box = null;
  2642. renderer.boxWrapper = renderer.boxWrapper.destroy();
  2643. // Call destroy on all gradient elements
  2644. destroyObjectProperties(renderer.gradients || {});
  2645. renderer.gradients = null;
  2646. // Defs are null in VMLRenderer
  2647. // Otherwise, destroy them here.
  2648. if (rendererDefs) {
  2649. renderer.defs = rendererDefs.destroy();
  2650. }
  2651. // Remove sub pixel fix handler
  2652. removeEvent(win, 'resize', renderer.subPixelFix);
  2653. renderer.alignedObjects = null;
  2654. return null;
  2655. },
  2656. /**
  2657. * Create a wrapper for an SVG element
  2658. * @param {Object} nodeName
  2659. */
  2660. createElement: function (nodeName) {
  2661. var wrapper = new this.Element();
  2662. wrapper.init(this, nodeName);
  2663. return wrapper;
  2664. },
  2665. /**
  2666. * Dummy function for use in canvas renderer
  2667. */
  2668. draw: function () {},
  2669. /**
  2670. * Parse a simple HTML string into SVG tspans
  2671. *
  2672. * @param {Object} textNode The parent text SVG node
  2673. */
  2674. buildText: function (wrapper) {
  2675. var textNode = wrapper.element,
  2676. lines = pick(wrapper.textStr, '').toString()
  2677. .replace(/<(b|strong)>/g, '<span style="font-weight:bold">')
  2678. .replace(/<(i|em)>/g, '<span style="font-style:italic">')
  2679. .replace(/<a/g, '<span')
  2680. .replace(/<\/(b|strong|i|em|a)>/g, '</span>')
  2681. .split(/<br.*?>/g),
  2682. childNodes = textNode.childNodes,
  2683. styleRegex = /style="([^"]+)"/,
  2684. hrefRegex = /href="([^"]+)"/,
  2685. parentX = attr(textNode, 'x'),
  2686. textStyles = wrapper.styles,
  2687. width = textStyles && pInt(textStyles.width),
  2688. textLineHeight = textStyles && textStyles.lineHeight,
  2689. lastLine,
  2690. GET_COMPUTED_STYLE = 'getComputedStyle',
  2691. i = childNodes.length,
  2692. linePositions = [];
  2693. // Needed in IE9 because it doesn't report tspan's offsetHeight (#893)
  2694. function getLineHeightByBBox(lineNo) {
  2695. linePositions[lineNo] = textNode.getBBox().height;
  2696. return mathRound(linePositions[lineNo] - (linePositions[lineNo - 1] || 0));
  2697. }
  2698. // remove old text
  2699. while (i--) {
  2700. textNode.removeChild(childNodes[i]);
  2701. }
  2702. if (width && !wrapper.added) {
  2703. this.box.appendChild(textNode); // attach it to the DOM to read offset width
  2704. }
  2705. // remove empty line at end
  2706. if (lines[lines.length - 1] === '') {
  2707. lines.pop();
  2708. }
  2709. // build the lines
  2710. each(lines, function (line, lineNo) {
  2711. var spans, spanNo = 0, lineHeight;
  2712. line = line.replace(/<span/g, '|||<span').replace(/<\/span>/g, '</span>|||');
  2713. spans = line.split('|||');
  2714. each(spans, function (span) {
  2715. if (span !== '' || spans.length === 1) {
  2716. var attributes = {},
  2717. tspan = doc.createElementNS(SVG_NS, 'tspan');
  2718. if (styleRegex.test(span)) {
  2719. attr(
  2720. tspan,
  2721. 'style',
  2722. span.match(styleRegex)[1].replace(/(;| |^)color([ :])/, '$1fill$2')
  2723. );
  2724. }
  2725. if (hrefRegex.test(span)) {
  2726. attr(tspan, 'onclick', 'location.href=\"' + span.match(hrefRegex)[1] + '\"');
  2727. css(tspan, { cursor: 'pointer' });
  2728. }
  2729. span = (span.replace(/<(.|\n)*?>/g, '') || ' ')
  2730. .replace(/&lt;/g, '<')
  2731. .replace(/&gt;/g, '>');
  2732. // issue #38 workaround.
  2733. /*if (reverse) {
  2734. arr = [];
  2735. i = span.length;
  2736. while (i--) {
  2737. arr.push(span.charAt(i));
  2738. }
  2739. span = arr.join('');
  2740. }*/
  2741. // add the text node
  2742. tspan.appendChild(doc.createTextNode(span));
  2743. if (!spanNo) { // first span in a line, align it to the left
  2744. attributes.x = parentX;
  2745. } else {
  2746. // Firefox ignores spaces at the front or end of the tspan
  2747. attributes.dx = 3; // space
  2748. }
  2749. // first span on subsequent line, add the line height
  2750. if (!spanNo) {
  2751. if (lineNo) {
  2752. // allow getting the right offset height in exporting in IE
  2753. if (!hasSVG && wrapper.renderer.forExport) {
  2754. css(tspan, { display: 'block' });
  2755. }
  2756. // Webkit and opera sometimes return 'normal' as the line height. In that
  2757. // case, webkit uses offsetHeight, while Opera falls back to 18
  2758. lineHeight = win[GET_COMPUTED_STYLE] &&
  2759. pInt(win[GET_COMPUTED_STYLE](lastLine, null).getPropertyValue('line-height'));
  2760. if (!lineHeight || isNaN(lineHeight)) {
  2761. lineHeight = textLineHeight || lastLine.offsetHeight || getLineHeightByBBox(lineNo) || 18;
  2762. }
  2763. attr(tspan, 'dy', lineHeight);
  2764. }
  2765. lastLine = tspan; // record for use in next line
  2766. }
  2767. // add attributes
  2768. attr(tspan, attributes);
  2769. // append it
  2770. textNode.appendChild(tspan);
  2771. spanNo++;
  2772. // check width and apply soft breaks
  2773. if (width) {
  2774. var words = span.replace(/-/g, '- ').split(' '),
  2775. tooLong,
  2776. actualWidth,
  2777. rest = [];
  2778. while (words.length || rest.length) {
  2779. actualWidth = wrapper.getBBox().width;
  2780. tooLong = actualWidth > width;
  2781. if (!tooLong || words.length === 1) { // new line needed
  2782. words = rest;
  2783. rest = [];
  2784. if (words.length) {
  2785. tspan = doc.createElementNS(SVG_NS, 'tspan');
  2786. attr(tspan, {
  2787. dy: textLineHeight || 16,
  2788. x: parentX
  2789. });
  2790. textNode.appendChild(tspan);
  2791. if (actualWidth > width) { // a single word is pressing it out
  2792. width = actualWidth;
  2793. }
  2794. }
  2795. } else { // append to existing line tspan
  2796. tspan.removeChild(tspan.firstChild);
  2797. rest.unshift(words.pop());
  2798. }
  2799. if (words.length) {
  2800. tspan.appendChild(doc.createTextNode(words.join(' ').replace(/- /g, '-')));
  2801. }
  2802. }
  2803. }
  2804. }
  2805. });
  2806. });
  2807. },
  2808. /**
  2809. * Create a button with preset states
  2810. * @param {String} text
  2811. * @param {Number} x
  2812. * @param {Number} y
  2813. * @param {Function} callback
  2814. * @param {Object} normalState
  2815. * @param {Object} hoverState
  2816. * @param {Object} pressedState
  2817. */
  2818. button: function (text, x, y, callback, normalState, hoverState, pressedState) {
  2819. var label = this.label(text, x, y),
  2820. curState = 0,
  2821. stateOptions,
  2822. stateStyle,
  2823. normalStyle,
  2824. hoverStyle,
  2825. pressedStyle,
  2826. STYLE = 'style',
  2827. verticalGradient = { x1: 0, y1: 0, x2: 0, y2: 1 };
  2828. // prepare the attributes
  2829. /*jslint white: true*/
  2830. normalState = merge(hash(
  2831. STROKE_WIDTH, 1,
  2832. STROKE, '#999',
  2833. FILL, hash(
  2834. LINEAR_GRADIENT, verticalGradient,
  2835. STOPS, [
  2836. [0, '#FFF'],
  2837. [1, '#DDD']
  2838. ]
  2839. ),
  2840. 'r', 3,
  2841. 'padding', 3,
  2842. STYLE, hash(
  2843. 'color', 'black'
  2844. )
  2845. ), normalState);
  2846. /*jslint white: false*/
  2847. normalStyle = normalState[STYLE];
  2848. delete normalState[STYLE];
  2849. /*jslint white: true*/
  2850. hoverState = merge(normalState, hash(
  2851. STROKE, '#68A',
  2852. FILL, hash(
  2853. LINEAR_GRADIENT, verticalGradient,
  2854. STOPS, [
  2855. [0, '#FFF'],
  2856. [1, '#ACF']
  2857. ]
  2858. )
  2859. ), hoverState);
  2860. /*jslint white: false*/
  2861. hoverStyle = hoverState[STYLE];
  2862. delete hoverState[STYLE];
  2863. /*jslint white: true*/
  2864. pressedState = merge(normalState, hash(
  2865. STROKE, '#68A',
  2866. FILL, hash(
  2867. LINEAR_GRADIENT, verticalGradient,
  2868. STOPS, [
  2869. [0, '#9BD'],
  2870. [1, '#CDF']
  2871. ]
  2872. )
  2873. ), pressedState);
  2874. /*jslint white: false*/
  2875. pressedStyle = pressedState[STYLE];
  2876. delete pressedState[STYLE];
  2877. // add the events
  2878. addEvent(label.element, 'mouseenter', function () {
  2879. label.attr(hoverState)
  2880. .css(hoverStyle);
  2881. });
  2882. addEvent(label.element, 'mouseleave', function () {
  2883. stateOptions = [normalState, hoverState, pressedState][curState];
  2884. stateStyle = [normalStyle, hoverStyle, pressedStyle][curState];
  2885. label.attr(stateOptions)
  2886. .css(stateStyle);
  2887. });
  2888. label.setState = function (state) {
  2889. curState = state;
  2890. if (!state) {
  2891. label.attr(normalState)
  2892. .css(normalStyle);
  2893. } else if (state === 2) {
  2894. label.attr(pressedState)
  2895. .css(pressedStyle);
  2896. }
  2897. };
  2898. return label
  2899. .on('click', function () {
  2900. callback.call(label);
  2901. })
  2902. .attr(normalState)
  2903. .css(extend({ cursor: 'default' }, normalStyle));
  2904. },
  2905. /**
  2906. * Make a straight line crisper by not spilling out to neighbour pixels
  2907. * @param {Array} points
  2908. * @param {Number} width
  2909. */
  2910. crispLine: function (points, width) {
  2911. // points format: [M, 0, 0, L, 100, 0]
  2912. // normalize to a crisp line
  2913. if (points[1] === points[4]) {
  2914. points[1] = points[4] = mathRound(points[1]) + (width % 2 / 2);
  2915. }
  2916. if (points[2] === points[5]) {
  2917. points[2] = points[5] = mathRound(points[2]) + (width % 2 / 2);
  2918. }
  2919. return points;
  2920. },
  2921. /**
  2922. * Draw a path
  2923. * @param {Array} path An SVG path in array form
  2924. */
  2925. path: function (path) {
  2926. return this.createElement('path').attr({
  2927. d: path,
  2928. fill: NONE
  2929. });
  2930. },
  2931. /**
  2932. * Draw and return an SVG circle
  2933. * @param {Number} x The x position
  2934. * @param {Number} y The y position
  2935. * @param {Number} r The radius
  2936. */
  2937. circle: function (x, y, r) {
  2938. var attr = isObject(x) ?
  2939. x :
  2940. {
  2941. x: x,
  2942. y: y,
  2943. r: r
  2944. };
  2945. return this.createElement('circle').attr(attr);
  2946. },
  2947. /**
  2948. * Draw and return an arc
  2949. * @param {Number} x X position
  2950. * @param {Number} y Y position
  2951. * @param {Number} r Radius
  2952. * @param {Number} innerR Inner radius like used in donut charts
  2953. * @param {Number} start Starting angle
  2954. * @param {Number} end Ending angle
  2955. */
  2956. arc: function (x, y, r, innerR, start, end) {
  2957. // arcs are defined as symbols for the ability to set
  2958. // attributes in attr and animate
  2959. if (isObject(x)) {
  2960. y = x.y;
  2961. r = x.r;
  2962. innerR = x.innerR;
  2963. start = x.start;
  2964. end = x.end;
  2965. x = x.x;
  2966. }
  2967. return this.symbol('arc', x || 0, y || 0, r || 0, r || 0, {
  2968. innerR: innerR || 0,
  2969. start: start || 0,
  2970. end: end || 0
  2971. });
  2972. },
  2973. /**
  2974. * Draw and return a rectangle
  2975. * @param {Number} x Left position
  2976. * @param {Number} y Top position
  2977. * @param {Number} width
  2978. * @param {Number} height
  2979. * @param {Number} r Border corner radius
  2980. * @param {Number} strokeWidth A stroke width can be supplied to allow crisp drawing
  2981. */
  2982. rect: function (x, y, width, height, r, strokeWidth) {
  2983. if (isObject(x)) {
  2984. y = x.y;
  2985. width = x.width;
  2986. height = x.height;
  2987. r = x.r;
  2988. strokeWidth = x.strokeWidth;
  2989. x = x.x;
  2990. }
  2991. var wrapper = this.createElement('rect').attr({
  2992. rx: r,
  2993. ry: r,
  2994. fill: NONE
  2995. });
  2996. return wrapper.attr(wrapper.crisp(strokeWidth, x, y, mathMax(width, 0), mathMax(height, 0)));
  2997. },
  2998. /**
  2999. * Resize the box and re-align all aligned elements
  3000. * @param {Object} width
  3001. * @param {Object} height
  3002. * @param {Boolean} animate
  3003. *
  3004. */
  3005. setSize: function (width, height, animate) {
  3006. var renderer = this,
  3007. alignedObjects = renderer.alignedObjects,
  3008. i = alignedObjects.length;
  3009. renderer.width = width;
  3010. renderer.height = height;
  3011. renderer.boxWrapper[pick(animate, true) ? 'animate' : 'attr']({
  3012. width: width,
  3013. height: height
  3014. });
  3015. while (i--) {
  3016. alignedObjects[i].align();
  3017. }
  3018. },
  3019. /**
  3020. * Create a group
  3021. * @param {String} name The group will be given a class name of 'highcharts-{name}'.
  3022. * This can be used for styling and scripting.
  3023. */
  3024. g: function (name) {
  3025. var elem = this.createElement('g');
  3026. return defined(name) ? elem.attr({ 'class': PREFIX + name }) : elem;
  3027. },
  3028. /**
  3029. * Display an image
  3030. * @param {String} src
  3031. * @param {Number} x
  3032. * @param {Number} y
  3033. * @param {Number} width
  3034. * @param {Number} height
  3035. */
  3036. image: function (src, x, y, width, height) {
  3037. var attribs = {
  3038. preserveAspectRatio: NONE
  3039. },
  3040. elemWrapper;
  3041. // optional properties
  3042. if (arguments.length > 1) {
  3043. extend(attribs, {
  3044. x: x,
  3045. y: y,
  3046. width: width,
  3047. height: height
  3048. });
  3049. }
  3050. elemWrapper = this.createElement('image').attr(attribs);
  3051. // set the href in the xlink namespace
  3052. if (elemWrapper.element.setAttributeNS) {
  3053. elemWrapper.element.setAttributeNS('http://www.w3.org/1999/xlink',
  3054. 'href', src);
  3055. } else {
  3056. // could be exporting in IE
  3057. // using href throws "not supported" in ie7 and under, requries regex shim to fix later
  3058. elemWrapper.element.setAttribute('hc-svg-href', src);
  3059. }
  3060. return elemWrapper;
  3061. },
  3062. /**
  3063. * Draw a symbol out of pre-defined shape paths from the namespace 'symbol' object.
  3064. *
  3065. * @param {Object} symbol
  3066. * @param {Object} x
  3067. * @param {Object} y
  3068. * @param {Object} radius
  3069. * @param {Object} options
  3070. */
  3071. symbol: function (symbol, x, y, width, height, options) {
  3072. var obj,
  3073. // get the symbol definition function
  3074. symbolFn = this.symbols[symbol],
  3075. // check if there's a path defined for this symbol
  3076. path = symbolFn && symbolFn(
  3077. mathRound(x),
  3078. mathRound(y),
  3079. width,
  3080. height,
  3081. options
  3082. ),
  3083. imageRegex = /^url\((.*?)\)$/,
  3084. imageSrc,
  3085. imageSize,
  3086. centerImage;
  3087. if (path) {
  3088. obj = this.path(path);
  3089. // expando properties for use in animate and attr
  3090. extend(obj, {
  3091. symbolName: symbol,
  3092. x: x,
  3093. y: y,
  3094. width: width,
  3095. height: height
  3096. });
  3097. if (options) {
  3098. extend(obj, options);
  3099. }
  3100. // image symbols
  3101. } else if (imageRegex.test(symbol)) {
  3102. // On image load, set the size and position
  3103. centerImage = function (img, size) {
  3104. img.attr({
  3105. width: size[0],
  3106. height: size[1]
  3107. });
  3108. if (!img.alignByTranslate) { // #185
  3109. img.translate(
  3110. -mathRound(size[0] / 2),
  3111. -mathRound(size[1] / 2)
  3112. );
  3113. }
  3114. };
  3115. imageSrc = symbol.match(imageRegex)[1];
  3116. imageSize = symbolSizes[imageSrc];
  3117. // create the image synchronously, add attribs async
  3118. obj = this.image(imageSrc)
  3119. .attr({
  3120. x: x,
  3121. y: y
  3122. });
  3123. if (imageSize) {
  3124. centerImage(obj, imageSize);
  3125. } else {
  3126. // initialize image to be 0 size so export will still function if there's no cached sizes
  3127. obj.attr({ width: 0, height: 0 });
  3128. // create a dummy JavaScript image to get the width and height
  3129. createElement('img', {
  3130. onload: function () {
  3131. var img = this;
  3132. centerImage(obj, symbolSizes[imageSrc] = [img.width, img.height]);
  3133. },
  3134. src: imageSrc
  3135. });
  3136. }
  3137. }
  3138. return obj;
  3139. },
  3140. /**
  3141. * An extendable collection of functions for defining symbol paths.
  3142. */
  3143. symbols: {
  3144. 'circle': function (x, y, w, h) {
  3145. var cpw = 0.166 * w;
  3146. return [
  3147. M, x + w / 2, y,
  3148. 'C', x + w + cpw, y, x + w + cpw, y + h, x + w / 2, y + h,
  3149. 'C', x - cpw, y + h, x - cpw, y, x + w / 2, y,
  3150. 'Z'
  3151. ];
  3152. },
  3153. 'square': function (x, y, w, h) {
  3154. return [
  3155. M, x, y,
  3156. L, x + w, y,
  3157. x + w, y + h,
  3158. x, y + h,
  3159. 'Z'
  3160. ];
  3161. },
  3162. 'triangle': function (x, y, w, h) {
  3163. return [
  3164. M, x + w / 2, y,
  3165. L, x + w, y + h,
  3166. x, y + h,
  3167. 'Z'
  3168. ];
  3169. },
  3170. 'triangle-down': function (x, y, w, h) {
  3171. return [
  3172. M, x, y,
  3173. L, x + w, y,
  3174. x + w / 2, y + h,
  3175. 'Z'
  3176. ];
  3177. },
  3178. 'diamond': function (x, y, w, h) {
  3179. return [
  3180. M, x + w / 2, y,
  3181. L, x + w, y + h / 2,
  3182. x + w / 2, y + h,
  3183. x, y + h / 2,
  3184. 'Z'
  3185. ];
  3186. },
  3187. 'arc': function (x, y, w, h, options) {
  3188. var start = options.start,
  3189. radius = options.r || w || h,
  3190. end = options.end - 0.000001, // to prevent cos and sin of start and end from becoming equal on 360 arcs
  3191. innerRadius = options.innerR,
  3192. cosStart = mathCos(start),
  3193. sinStart = mathSin(start),
  3194. cosEnd = mathCos(end),
  3195. sinEnd = mathSin(end),
  3196. longArc = options.end - start < mathPI ? 0 : 1;
  3197. return [
  3198. M,
  3199. x + radius * cosStart,
  3200. y + radius * sinStart,
  3201. 'A', // arcTo
  3202. radius, // x radius
  3203. radius, // y radius
  3204. 0, // slanting
  3205. longArc, // long or short arc
  3206. 1, // clockwise
  3207. x + radius * cosEnd,
  3208. y + radius * sinEnd,
  3209. L,
  3210. x + innerRadius * cosEnd,
  3211. y + innerRadius * sinEnd,
  3212. 'A', // arcTo
  3213. innerRadius, // x radius
  3214. innerRadius, // y radius
  3215. 0, // slanting
  3216. longArc, // long or short arc
  3217. 0, // clockwise
  3218. x + innerRadius * cosStart,
  3219. y + innerRadius * sinStart,
  3220. 'Z' // close
  3221. ];
  3222. }
  3223. },
  3224. /**
  3225. * Define a clipping rectangle
  3226. * @param {String} id
  3227. * @param {Number} x
  3228. * @param {Number} y
  3229. * @param {Number} width
  3230. * @param {Number} height
  3231. */
  3232. clipRect: function (x, y, width, height) {
  3233. var wrapper,
  3234. id = PREFIX + idCounter++,
  3235. clipPath = this.createElement('clipPath').attr({
  3236. id: id
  3237. }).add(this.defs);
  3238. wrapper = this.rect(x, y, width, height, 0).add(clipPath);
  3239. wrapper.id = id;
  3240. wrapper.clipPath = clipPath;
  3241. return wrapper;
  3242. },
  3243. /**
  3244. * Take a color and return it if it's a string, make it a gradient if it's a
  3245. * gradient configuration object. Prior to Highstock, an array was used to define
  3246. * a linear gradient with pixel positions relative to the SVG. In newer versions
  3247. * we change the coordinates to apply relative to the shape, using coordinates
  3248. * 0-1 within the shape. To preserve backwards compatibility, linearGradient
  3249. * in this definition is an object of x1, y1, x2 and y2.
  3250. *
  3251. * @param {Object} color The color or config object
  3252. */
  3253. color: function (color, elem, prop) {
  3254. var colorObject,
  3255. regexRgba = /^rgba/;
  3256. if (color && color.linearGradient) {
  3257. var renderer = this,
  3258. linearGradient = color[LINEAR_GRADIENT],
  3259. relativeToShape = !isArray(linearGradient), // keep backwards compatibility
  3260. id,
  3261. gradients = renderer.gradients,
  3262. gradientObject,
  3263. x1 = linearGradient.x1 || linearGradient[0] || 0,
  3264. y1 = linearGradient.y1 || linearGradient[1] || 0,
  3265. x2 = linearGradient.x2 || linearGradient[2] || 0,
  3266. y2 = linearGradient.y2 || linearGradient[3] || 0,
  3267. stopColor,
  3268. stopOpacity,
  3269. // Create a unique key in order to reuse gradient objects. #671.
  3270. key = [relativeToShape, x1, y1, x2, y2, color.stops.join(',')].join(',');
  3271. // If the gradient with the same setup is already created, reuse it
  3272. if (gradients[key]) {
  3273. id = attr(gradients[key].element, 'id');
  3274. // If not, create a new one and keep the reference.
  3275. } else {
  3276. id = PREFIX + idCounter++;
  3277. gradientObject = renderer.createElement(LINEAR_GRADIENT)
  3278. .attr(extend({
  3279. id: id,
  3280. x1: x1,
  3281. y1: y1,
  3282. x2: x2,
  3283. y2: y2
  3284. }, relativeToShape ? null : { gradientUnits: 'userSpaceOnUse' }))
  3285. .add(renderer.defs);
  3286. // The gradient needs to keep a list of stops to be able to destroy them
  3287. gradientObject.stops = [];
  3288. each(color.stops, function (stop) {
  3289. var stopObject;
  3290. if (regexRgba.test(stop[1])) {
  3291. colorObject = Color(stop[1]);
  3292. stopColor = colorObject.get('rgb');
  3293. stopOpacity = colorObject.get('a');
  3294. } else {
  3295. stopColor = stop[1];
  3296. stopOpacity = 1;
  3297. }
  3298. stopObject = renderer.createElement('stop').attr({
  3299. offset: stop[0],
  3300. 'stop-color': stopColor,
  3301. 'stop-opacity': stopOpacity
  3302. }).add(gradientObject);
  3303. // Add the stop element to the gradient
  3304. gradientObject.stops.push(stopObject);
  3305. });
  3306. // Keep a reference to the gradient object so it is possible to reuse it and
  3307. // destroy it later
  3308. gradients[key] = gradientObject;
  3309. }
  3310. return 'url(' + this.url + '#' + id + ')';
  3311. // Webkit and Batik can't show rgba.
  3312. } else if (regexRgba.test(color)) {
  3313. colorObject = Color(color);
  3314. attr(elem, prop + '-opacity', colorObject.get('a'));
  3315. return colorObject.get('rgb');
  3316. } else {
  3317. // Remove the opacity attribute added above. Does not throw if the attribute is not there.
  3318. elem.removeAttribute(prop + '-opacity');
  3319. return color;
  3320. }
  3321. },
  3322. /**
  3323. * Add text to the SVG object
  3324. * @param {String} str
  3325. * @param {Number} x Left position
  3326. * @param {Number} y Top position
  3327. * @param {Boolean} useHTML Use HTML to render the text
  3328. */
  3329. text: function (str, x, y, useHTML) {
  3330. // declare variables
  3331. var renderer = this,
  3332. defaultChartStyle = defaultOptions.chart.style,
  3333. wrapper;
  3334. if (useHTML && !renderer.forExport) {
  3335. return renderer.html(str, x, y);
  3336. }
  3337. x = mathRound(pick(x, 0));
  3338. y = mathRound(pick(y, 0));
  3339. wrapper = renderer.createElement('text')
  3340. .attr({
  3341. x: x,
  3342. y: y,
  3343. text: str
  3344. })
  3345. .css({
  3346. fontFamily: defaultChartStyle.fontFamily,
  3347. fontSize: defaultChartStyle.fontSize
  3348. });
  3349. wrapper.x = x;
  3350. wrapper.y = y;
  3351. return wrapper;
  3352. },
  3353. /**
  3354. * Create HTML text node. This is used by the VML renderer as well as the SVG
  3355. * renderer through the useHTML option.
  3356. *
  3357. * @param {String} str
  3358. * @param {Number} x
  3359. * @param {Number} y
  3360. */
  3361. html: function (str, x, y) {
  3362. var defaultChartStyle = defaultOptions.chart.style,
  3363. wrapper = this.createElement('span'),
  3364. attrSetters = wrapper.attrSetters,
  3365. element = wrapper.element,
  3366. renderer = wrapper.renderer;
  3367. // Text setter
  3368. attrSetters.text = function (value) {
  3369. element.innerHTML = value;
  3370. return false;
  3371. };
  3372. // Various setters which rely on update transform
  3373. attrSetters.x = attrSetters.y = attrSetters.align = function (value, key) {
  3374. if (key === 'align') {
  3375. key = 'textAlign'; // Do not overwrite the SVGElement.align method. Same as VML.
  3376. }
  3377. wrapper[key] = value;
  3378. wrapper.htmlUpdateTransform();
  3379. return false;
  3380. };
  3381. // Set the default attributes
  3382. wrapper.attr({
  3383. text: str,
  3384. x: mathRound(x),
  3385. y: mathRound(y)
  3386. })
  3387. .css({
  3388. position: ABSOLUTE,
  3389. whiteSpace: 'nowrap',
  3390. fontFamily: defaultChartStyle.fontFamily,
  3391. fontSize: defaultChartStyle.fontSize
  3392. });
  3393. // Use the HTML specific .css method
  3394. wrapper.css = wrapper.htmlCss;
  3395. // This is specific for HTML within SVG
  3396. if (renderer.isSVG) {
  3397. wrapper.add = function (svgGroupWrapper) {
  3398. var htmlGroup,
  3399. htmlGroupStyle,
  3400. container = renderer.box.parentNode;
  3401. // Create a mock group to hold the HTML elements
  3402. if (svgGroupWrapper) {
  3403. htmlGroup = svgGroupWrapper.div;
  3404. if (!htmlGroup) {
  3405. htmlGroup = svgGroupWrapper.div = createElement(DIV, {
  3406. className: attr(svgGroupWrapper.element, 'class')
  3407. }, {
  3408. position: ABSOLUTE,
  3409. left: svgGroupWrapper.attr('translateX') + PX,
  3410. top: svgGroupWrapper.attr('translateY') + PX
  3411. }, container);
  3412. // Ensure dynamic updating position
  3413. htmlGroupStyle = htmlGroup.style;
  3414. extend(svgGroupWrapper.attrSetters, {
  3415. translateX: function (value) {
  3416. htmlGroupStyle.left = value + PX;
  3417. },
  3418. translateY: function (value) {
  3419. htmlGroupStyle.top = value + PX;
  3420. },
  3421. visibility: function (value, key) {
  3422. htmlGroupStyle[key] = value;
  3423. }
  3424. });
  3425. }
  3426. } else {
  3427. htmlGroup = container;
  3428. }
  3429. htmlGroup.appendChild(element);
  3430. // Shared with VML:
  3431. wrapper.added = true;
  3432. if (wrapper.alignOnAdd) {
  3433. wrapper.htmlUpdateTransform();
  3434. }
  3435. return wrapper;
  3436. };
  3437. }
  3438. return wrapper;
  3439. },
  3440. /**
  3441. * Utility to return the baseline offset and total line height from the font size
  3442. */
  3443. fontMetrics: function (fontSize) {
  3444. fontSize = pInt(fontSize || 11);
  3445. // Empirical values found by comparing font size and bounding box height.
  3446. // Applies to the default font family. http://jsfiddle.net/highcharts/7xvn7/
  3447. var lineHeight = fontSize < 24 ? fontSize + 4 : mathRound(fontSize * 1.2),
  3448. baseline = mathRound(lineHeight * 0.8);
  3449. return {
  3450. h: lineHeight,
  3451. b: baseline
  3452. };
  3453. },
  3454. /**
  3455. * Add a label, a text item that can hold a colored or gradient background
  3456. * as well as a border and shadow.
  3457. * @param {string} str
  3458. * @param {Number} x
  3459. * @param {Number} y
  3460. * @param {String} shape
  3461. * @param {Number} anchorX In case the shape has a pointer, like a flag, this is the
  3462. * coordinates it should be pinned to
  3463. * @param {Number} anchorY
  3464. * @param {Boolean} baseline Whether to position the label relative to the text baseline,
  3465. * like renderer.text, or to the upper border of the rectangle.
  3466. * @param {String} className Class name for the group
  3467. */
  3468. label: function (str, x, y, shape, anchorX, anchorY, useHTML, baseline, className) {
  3469. var renderer = this,
  3470. wrapper = renderer.g(className),
  3471. text = renderer.text('', 0, 0, useHTML)
  3472. .attr({
  3473. zIndex: 1
  3474. })
  3475. .add(wrapper),
  3476. box,
  3477. bBox,
  3478. alignFactor = 0,
  3479. padding = 3,
  3480. width,
  3481. height,
  3482. wrapperX,
  3483. wrapperY,
  3484. crispAdjust = 0,
  3485. deferredAttr = {},
  3486. baselineOffset,
  3487. attrSetters = wrapper.attrSetters;
  3488. /**
  3489. * This function runs after the label is added to the DOM (when the bounding box is
  3490. * available), and after the text of the label is updated to detect the new bounding
  3491. * box and reflect it in the border box.
  3492. */
  3493. function updateBoxSize() {
  3494. var boxY,
  3495. style = text.element.style;
  3496. bBox = (width === undefined || height === undefined || wrapper.styles.textAlign) &&
  3497. text.getBBox(true);
  3498. wrapper.width = (width || bBox.width || 0) + 2 * padding;
  3499. wrapper.height = (height || bBox.height || 0) + 2 * padding;
  3500. // update the label-scoped y offset
  3501. baselineOffset = padding + renderer.fontMetrics(style && style.fontSize).b;
  3502. // create the border box if it is not already present
  3503. if (!box) {
  3504. boxY = baseline ? -baselineOffset : 0;
  3505. wrapper.box = box = shape ?
  3506. renderer.symbol(shape, -alignFactor * padding, boxY, wrapper.width, wrapper.height) :
  3507. renderer.rect(-alignFactor * padding, boxY, wrapper.width, wrapper.height, 0, deferredAttr[STROKE_WIDTH]);
  3508. box.add(wrapper);
  3509. }
  3510. // apply the box attributes
  3511. box.attr(merge({
  3512. width: wrapper.width,
  3513. height: wrapper.height
  3514. }, deferredAttr));
  3515. deferredAttr = null;
  3516. }
  3517. /**
  3518. * This function runs after setting text or padding, but only if padding is changed
  3519. */
  3520. function updateTextPadding() {
  3521. var styles = wrapper.styles,
  3522. textAlign = styles && styles.textAlign,
  3523. x = padding * (1 - alignFactor),
  3524. y;
  3525. // determin y based on the baseline
  3526. y = baseline ? 0 : baselineOffset;
  3527. // compensate for alignment
  3528. if (defined(width) && (textAlign === 'center' || textAlign === 'right')) {
  3529. x += { center: 0.5, right: 1 }[textAlign] * (width - bBox.width);
  3530. }
  3531. // update if anything changed
  3532. if (x !== text.x || y !== text.y) {
  3533. text.attr({
  3534. x: x,
  3535. y: y
  3536. });
  3537. }
  3538. // record current values
  3539. text.x = x;
  3540. text.y = y;
  3541. }
  3542. /**
  3543. * Set a box attribute, or defer it if the box is not yet created
  3544. * @param {Object} key
  3545. * @param {Object} value
  3546. */
  3547. function boxAttr(key, value) {
  3548. if (box) {
  3549. box.attr(key, value);
  3550. } else {
  3551. deferredAttr[key] = value;
  3552. }
  3553. }
  3554. function getSizeAfterAdd() {
  3555. wrapper.attr({
  3556. text: str, // alignment is available now
  3557. x: x,
  3558. y: y,
  3559. anchorX: anchorX,
  3560. anchorY: anchorY
  3561. });
  3562. }
  3563. /**
  3564. * After the text element is added, get the desired size of the border box
  3565. * and add it before the text in the DOM.
  3566. */
  3567. addEvent(wrapper, 'add', getSizeAfterAdd);
  3568. /*
  3569. * Add specific attribute setters.
  3570. */
  3571. // only change local variables
  3572. attrSetters.width = function (value) {
  3573. width = value;
  3574. return false;
  3575. };
  3576. attrSetters.height = function (value) {
  3577. height = value;
  3578. return false;
  3579. };
  3580. attrSetters.padding = function (value) {
  3581. if (defined(value) && value !== padding) {
  3582. padding = value;
  3583. updateTextPadding();
  3584. }
  3585. return false;
  3586. };
  3587. // change local variable and set attribue as well
  3588. attrSetters.align = function (value) {
  3589. alignFactor = { left: 0, center: 0.5, right: 1 }[value];
  3590. return false; // prevent setting text-anchor on the group
  3591. };
  3592. // apply these to the box and the text alike
  3593. attrSetters.text = function (value, key) {
  3594. text.attr(key, value);
  3595. updateBoxSize();
  3596. updateTextPadding();
  3597. return false;
  3598. };
  3599. // apply these to the box but not to the text
  3600. attrSetters[STROKE_WIDTH] = function (value, key) {
  3601. crispAdjust = value % 2 / 2;
  3602. boxAttr(key, value);
  3603. return false;
  3604. };
  3605. attrSetters.stroke = attrSetters.fill = attrSetters.r = function (value, key) {
  3606. boxAttr(key, value);
  3607. return false;
  3608. };
  3609. attrSetters.anchorX = function (value, key) {
  3610. anchorX = value;
  3611. boxAttr(key, value + crispAdjust - wrapperX);
  3612. return false;
  3613. };
  3614. attrSetters.anchorY = function (value, key) {
  3615. anchorY = value;
  3616. boxAttr(key, value - wrapperY);
  3617. return false;
  3618. };
  3619. // rename attributes
  3620. attrSetters.x = function (value) {
  3621. wrapper.x = value; // for animation getter
  3622. value -= alignFactor * ((width || bBox.width) + padding);
  3623. wrapperX = mathRound(value);
  3624. wrapper.attr('translateX', wrapperX);
  3625. return false;
  3626. };
  3627. attrSetters.y = function (value) {
  3628. wrapperY = wrapper.y = mathRound(value);
  3629. wrapper.attr('translateY', value);
  3630. return false;
  3631. };
  3632. // Redirect certain methods to either the box or the text
  3633. var baseCss = wrapper.css;
  3634. return extend(wrapper, {
  3635. /**
  3636. * Pick up some properties and apply them to the text instead of the wrapper
  3637. */
  3638. css: function (styles) {
  3639. if (styles) {
  3640. var textStyles = {};
  3641. styles = merge({}, styles); // create a copy to avoid altering the original object (#537)
  3642. each(['fontSize', 'fontWeight', 'fontFamily', 'color', 'lineHeight', 'width'], function (prop) {
  3643. if (styles[prop] !== UNDEFINED) {
  3644. textStyles[prop] = styles[prop];
  3645. delete styles[prop];
  3646. }
  3647. });
  3648. text.css(textStyles);
  3649. }
  3650. return baseCss.call(wrapper, styles);
  3651. },
  3652. /**
  3653. * Return the bounding box of the box, not the group
  3654. */
  3655. getBBox: function () {
  3656. return box.getBBox();
  3657. },
  3658. /**
  3659. * Apply the shadow to the box
  3660. */
  3661. shadow: function (b) {
  3662. box.shadow(b);
  3663. return wrapper;
  3664. },
  3665. /**
  3666. * Destroy and release memory.
  3667. */
  3668. destroy: function () {
  3669. removeEvent(wrapper, 'add', getSizeAfterAdd);
  3670. // Added by button implementation
  3671. removeEvent(wrapper.element, 'mouseenter');
  3672. removeEvent(wrapper.element, 'mouseleave');
  3673. if (text) {
  3674. // Destroy the text element
  3675. text = text.destroy();
  3676. }
  3677. // Call base implementation to destroy the rest
  3678. SVGElement.prototype.destroy.call(wrapper);
  3679. }
  3680. });
  3681. }
  3682. }; // end SVGRenderer
  3683. // general renderer
  3684. Renderer = SVGRenderer;
  3685. /* ****************************************************************************
  3686. * *
  3687. * START OF INTERNET EXPLORER <= 8 SPECIFIC CODE *
  3688. * *
  3689. * For applications and websites that don't need IE support, like platform *
  3690. * targeted mobile apps and web apps, this code can be removed. *
  3691. * *
  3692. *****************************************************************************/
  3693. /**
  3694. * @constructor
  3695. */
  3696. var VMLRenderer;
  3697. if (!hasSVG && !useCanVG) {
  3698. /**
  3699. * The VML element wrapper.
  3700. */
  3701. var VMLElement = {
  3702. /**
  3703. * Initialize a new VML element wrapper. It builds the markup as a string
  3704. * to minimize DOM traffic.
  3705. * @param {Object} renderer
  3706. * @param {Object} nodeName
  3707. */
  3708. init: function (renderer, nodeName) {
  3709. var wrapper = this,
  3710. markup = ['<', nodeName, ' filled="f" stroked="f"'],
  3711. style = ['position: ', ABSOLUTE, ';'];
  3712. // divs and shapes need size
  3713. if (nodeName === 'shape' || nodeName === DIV) {
  3714. style.push('left:0;top:0;width:10px;height:10px;');
  3715. }
  3716. if (docMode8) {
  3717. style.push('visibility: ', nodeName === DIV ? HIDDEN : VISIBLE);
  3718. }
  3719. markup.push(' style="', style.join(''), '"/>');
  3720. // create element with default attributes and style
  3721. if (nodeName) {
  3722. markup = nodeName === DIV || nodeName === 'span' || nodeName === 'img' ?
  3723. markup.join('')
  3724. : renderer.prepVML(markup);
  3725. wrapper.element = createElement(markup);
  3726. }
  3727. wrapper.renderer = renderer;
  3728. wrapper.attrSetters = {};
  3729. },
  3730. /**
  3731. * Add the node to the given parent
  3732. * @param {Object} parent
  3733. */
  3734. add: function (parent) {
  3735. var wrapper = this,
  3736. renderer = wrapper.renderer,
  3737. element = wrapper.element,
  3738. box = renderer.box,
  3739. inverted = parent && parent.inverted,
  3740. // get the parent node
  3741. parentNode = parent ?
  3742. parent.element || parent :
  3743. box;
  3744. // if the parent group is inverted, apply inversion on all children
  3745. if (inverted) { // only on groups
  3746. renderer.invertChild(element, parentNode);
  3747. }
  3748. // issue #140 workaround - related to #61 and #74
  3749. if (docMode8 && parentNode.gVis === HIDDEN) {
  3750. css(element, { visibility: HIDDEN });
  3751. }
  3752. // append it
  3753. parentNode.appendChild(element);
  3754. // align text after adding to be able to read offset
  3755. wrapper.added = true;
  3756. if (wrapper.alignOnAdd && !wrapper.deferUpdateTransform) {
  3757. wrapper.updateTransform();
  3758. }
  3759. // fire an event for internal hooks
  3760. fireEvent(wrapper, 'add');
  3761. return wrapper;
  3762. },
  3763. /**
  3764. * In IE8 documentMode 8, we need to recursively set the visibility down in the DOM
  3765. * tree for nested groups. Related to #61, #586.
  3766. */
  3767. toggleChildren: function (element, visibility) {
  3768. var childNodes = element.childNodes,
  3769. i = childNodes.length;
  3770. while (i--) {
  3771. // apply the visibility
  3772. css(childNodes[i], { visibility: visibility });
  3773. // we have a nested group, apply it to its children again
  3774. if (childNodes[i].nodeName === 'DIV') {
  3775. this.toggleChildren(childNodes[i], visibility);
  3776. }
  3777. }
  3778. },
  3779. /**
  3780. * VML always uses htmlUpdateTransform
  3781. */
  3782. updateTransform: SVGElement.prototype.htmlUpdateTransform,
  3783. /**
  3784. * Get or set attributes
  3785. */
  3786. attr: function (hash, val) {
  3787. var wrapper = this,
  3788. key,
  3789. value,
  3790. i,
  3791. result,
  3792. element = wrapper.element || {},
  3793. elemStyle = element.style,
  3794. nodeName = element.nodeName,
  3795. renderer = wrapper.renderer,
  3796. symbolName = wrapper.symbolName,
  3797. hasSetSymbolSize,
  3798. shadows = wrapper.shadows,
  3799. skipAttr,
  3800. attrSetters = wrapper.attrSetters,
  3801. ret = wrapper;
  3802. // single key-value pair
  3803. if (isString(hash) && defined(val)) {
  3804. key = hash;
  3805. hash = {};
  3806. hash[key] = val;
  3807. }
  3808. // used as a getter, val is undefined
  3809. if (isString(hash)) {
  3810. key = hash;
  3811. if (key === 'strokeWidth' || key === 'stroke-width') {
  3812. ret = wrapper.strokeweight;
  3813. } else {
  3814. ret = wrapper[key];
  3815. }
  3816. // setter
  3817. } else {
  3818. for (key in hash) {
  3819. value = hash[key];
  3820. skipAttr = false;
  3821. // check for a specific attribute setter
  3822. result = attrSetters[key] && attrSetters[key](value, key);
  3823. if (result !== false && value !== null) { // #620
  3824. if (result !== UNDEFINED) {
  3825. value = result; // the attribute setter has returned a new value to set
  3826. }
  3827. // prepare paths
  3828. // symbols
  3829. if (symbolName && /^(x|y|r|start|end|width|height|innerR|anchorX|anchorY)/.test(key)) {
  3830. // if one of the symbol size affecting parameters are changed,
  3831. // check all the others only once for each call to an element's
  3832. // .attr() method
  3833. if (!hasSetSymbolSize) {
  3834. wrapper.symbolAttr(hash);
  3835. hasSetSymbolSize = true;
  3836. }
  3837. skipAttr = true;
  3838. } else if (key === 'd') {
  3839. value = value || [];
  3840. wrapper.d = value.join(' '); // used in getter for animation
  3841. // convert paths
  3842. i = value.length;
  3843. var convertedPath = [];
  3844. while (i--) {
  3845. // Multiply by 10 to allow subpixel precision.
  3846. // Substracting half a pixel seems to make the coordinates
  3847. // align with SVG, but this hasn't been tested thoroughly
  3848. if (isNumber(value[i])) {
  3849. convertedPath[i] = mathRound(value[i] * 10) - 5;
  3850. } else if (value[i] === 'Z') { // close the path
  3851. convertedPath[i] = 'x';
  3852. } else {
  3853. convertedPath[i] = value[i];
  3854. }
  3855. }
  3856. value = convertedPath.join(' ') || 'x';
  3857. element.path = value;
  3858. // update shadows
  3859. if (shadows) {
  3860. i = shadows.length;
  3861. while (i--) {
  3862. shadows[i].path = value;
  3863. }
  3864. }
  3865. skipAttr = true;
  3866. // directly mapped to css
  3867. } else if (key === 'zIndex' || key === 'visibility') {
  3868. // workaround for #61 and #586
  3869. if (docMode8 && key === 'visibility' && nodeName === 'DIV') {
  3870. element.gVis = value;
  3871. wrapper.toggleChildren(element, value);
  3872. if (value === VISIBLE) { // #74
  3873. value = null;
  3874. }
  3875. }
  3876. if (value) {
  3877. elemStyle[key] = value;
  3878. }
  3879. skipAttr = true;
  3880. // width and height
  3881. } else if (key === 'width' || key === 'height') {
  3882. value = mathMax(0, value); // don't set width or height below zero (#311)
  3883. this[key] = value; // used in getter
  3884. // clipping rectangle special
  3885. if (wrapper.updateClipping) {
  3886. wrapper[key] = value;
  3887. wrapper.updateClipping();
  3888. } else {
  3889. // normal
  3890. elemStyle[key] = value;
  3891. }
  3892. skipAttr = true;
  3893. // x and y
  3894. } else if (key === 'x' || key === 'y') {
  3895. wrapper[key] = value; // used in getter
  3896. elemStyle[{ x: 'left', y: 'top' }[key]] = value;
  3897. // class name
  3898. } else if (key === 'class') {
  3899. // IE8 Standards mode has problems retrieving the className
  3900. element.className = value;
  3901. // stroke
  3902. } else if (key === 'stroke') {
  3903. value = renderer.color(value, element, key);
  3904. key = 'strokecolor';
  3905. // stroke width
  3906. } else if (key === 'stroke-width' || key === 'strokeWidth') {
  3907. element.stroked = value ? true : false;
  3908. key = 'strokeweight';
  3909. wrapper[key] = value; // used in getter, issue #113
  3910. if (isNumber(value)) {
  3911. value += PX;
  3912. }
  3913. // dashStyle
  3914. } else if (key === 'dashstyle') {
  3915. var strokeElem = element.getElementsByTagName('stroke')[0] ||
  3916. createElement(renderer.prepVML(['<stroke/>']), null, null, element);
  3917. strokeElem[key] = value || 'solid';
  3918. wrapper.dashstyle = value; /* because changing stroke-width will change the dash length
  3919. and cause an epileptic effect */
  3920. skipAttr = true;
  3921. // fill
  3922. } else if (key === 'fill') {
  3923. if (nodeName === 'SPAN') { // text color
  3924. elemStyle.color = value;
  3925. } else {
  3926. element.filled = value !== NONE ? true : false;
  3927. value = renderer.color(value, element, key);
  3928. key = 'fillcolor';
  3929. }
  3930. // translation for animation
  3931. } else if (key === 'translateX' || key === 'translateY' || key === 'rotation') {
  3932. wrapper[key] = value;
  3933. wrapper.updateTransform();
  3934. skipAttr = true;
  3935. // text for rotated and non-rotated elements
  3936. } else if (key === 'text') {
  3937. this.bBox = null;
  3938. element.innerHTML = value;
  3939. skipAttr = true;
  3940. }
  3941. // let the shadow follow the main element
  3942. if (shadows && key === 'visibility') {
  3943. i = shadows.length;
  3944. while (i--) {
  3945. shadows[i].style[key] = value;
  3946. }
  3947. }
  3948. if (!skipAttr) {
  3949. if (docMode8) { // IE8 setAttribute bug
  3950. element[key] = value;
  3951. } else {
  3952. attr(element, key, value);
  3953. }
  3954. }
  3955. }
  3956. }
  3957. }
  3958. return ret;
  3959. },
  3960. /**
  3961. * Set the element's clipping to a predefined rectangle
  3962. *
  3963. * @param {String} id The id of the clip rectangle
  3964. */
  3965. clip: function (clipRect) {
  3966. var wrapper = this,
  3967. clipMembers = clipRect.members,
  3968. element = wrapper.element;
  3969. clipMembers.push(wrapper);
  3970. wrapper.destroyClip = function () {
  3971. erase(clipMembers, wrapper);
  3972. };
  3973. // Issue #863 workaround - related to #140, #61, #74
  3974. if (element.parentNode.className === 'highcharts-tracker' && !docMode8) {
  3975. css(element, { visibility: HIDDEN });
  3976. }
  3977. return wrapper.css(clipRect.getCSS(wrapper.inverted));
  3978. },
  3979. /**
  3980. * Set styles for the element
  3981. * @param {Object} styles
  3982. */
  3983. css: SVGElement.prototype.htmlCss,
  3984. /**
  3985. * Removes a child either by removeChild or move to garbageBin.
  3986. * Issue 490; in VML removeChild results in Orphaned nodes according to sIEve, discardElement does not.
  3987. */
  3988. safeRemoveChild: function (element) {
  3989. // discardElement will detach the node from its parent before attaching it
  3990. // to the garbage bin. Therefore it is important that the node is attached and have parent.
  3991. var parentNode = element.parentNode;
  3992. if (parentNode) {
  3993. discardElement(element);
  3994. }
  3995. },
  3996. /**
  3997. * Extend element.destroy by removing it from the clip members array
  3998. */
  3999. destroy: function () {
  4000. var wrapper = this;
  4001. if (wrapper.destroyClip) {
  4002. wrapper.destroyClip();
  4003. }
  4004. return SVGElement.prototype.destroy.apply(wrapper);
  4005. },
  4006. /**
  4007. * Remove all child nodes of a group, except the v:group element
  4008. */
  4009. empty: function () {
  4010. var element = this.element,
  4011. childNodes = element.childNodes,
  4012. i = childNodes.length,
  4013. node;
  4014. while (i--) {
  4015. node = childNodes[i];
  4016. node.parentNode.removeChild(node);
  4017. }
  4018. },
  4019. /**
  4020. * Add an event listener. VML override for normalizing event parameters.
  4021. * @param {String} eventType
  4022. * @param {Function} handler
  4023. */
  4024. on: function (eventType, handler) {
  4025. // simplest possible event model for internal use
  4026. this.element['on' + eventType] = function () {
  4027. var evt = win.event;
  4028. evt.target = evt.srcElement;
  4029. handler(evt);
  4030. };
  4031. return this;
  4032. },
  4033. /**
  4034. * Apply a drop shadow by copying elements and giving them different strokes
  4035. * @param {Boolean} apply
  4036. */
  4037. shadow: function (apply, group) {
  4038. var shadows = [],
  4039. i,
  4040. element = this.element,
  4041. renderer = this.renderer,
  4042. shadow,
  4043. elemStyle = element.style,
  4044. markup,
  4045. path = element.path;
  4046. // some times empty paths are not strings
  4047. if (path && typeof path.value !== 'string') {
  4048. path = 'x';
  4049. }
  4050. if (apply) {
  4051. for (i = 1; i <= 3; i++) {
  4052. markup = ['<shape isShadow="true" strokeweight="', (7 - 2 * i),
  4053. '" filled="false" path="', path,
  4054. '" coordsize="100,100" style="', element.style.cssText, '" />'];
  4055. shadow = createElement(renderer.prepVML(markup),
  4056. null, {
  4057. left: pInt(elemStyle.left) + 1,
  4058. top: pInt(elemStyle.top) + 1
  4059. }
  4060. );
  4061. // apply the opacity
  4062. markup = ['<stroke color="black" opacity="', (0.05 * i), '"/>'];
  4063. createElement(renderer.prepVML(markup), null, null, shadow);
  4064. // insert it
  4065. if (group) {
  4066. group.element.appendChild(shadow);
  4067. } else {
  4068. element.parentNode.insertBefore(shadow, element);
  4069. }
  4070. // record it
  4071. shadows.push(shadow);
  4072. }
  4073. this.shadows = shadows;
  4074. }
  4075. return this;
  4076. }
  4077. };
  4078. VMLElement = extendClass(SVGElement, VMLElement);
  4079. /**
  4080. * The VML renderer
  4081. */
  4082. var VMLRendererExtension = { // inherit SVGRenderer
  4083. Element: VMLElement,
  4084. isIE8: userAgent.indexOf('MSIE 8.0') > -1,
  4085. /**
  4086. * Initialize the VMLRenderer
  4087. * @param {Object} container
  4088. * @param {Number} width
  4089. * @param {Number} height
  4090. */
  4091. init: function (container, width, height) {
  4092. var renderer = this,
  4093. boxWrapper,
  4094. box;
  4095. renderer.alignedObjects = [];
  4096. boxWrapper = renderer.createElement(DIV);
  4097. box = boxWrapper.element;
  4098. box.style.position = RELATIVE; // for freeform drawing using renderer directly
  4099. container.appendChild(boxWrapper.element);
  4100. // generate the containing box
  4101. renderer.box = box;
  4102. renderer.boxWrapper = boxWrapper;
  4103. renderer.setSize(width, height, false);
  4104. // The only way to make IE6 and IE7 print is to use a global namespace. However,
  4105. // with IE8 the only way to make the dynamic shapes visible in screen and print mode
  4106. // seems to be to add the xmlns attribute and the behaviour style inline.
  4107. if (!doc.namespaces.hcv) {
  4108. doc.namespaces.add('hcv', 'urn:schemas-microsoft-com:vml');
  4109. // setup default css
  4110. doc.createStyleSheet().cssText =
  4111. 'hcv\\:fill, hcv\\:path, hcv\\:shape, hcv\\:stroke' +
  4112. '{ behavior:url(#default#VML); display: inline-block; } ';
  4113. }
  4114. },
  4115. /**
  4116. * Define a clipping rectangle. In VML it is accomplished by storing the values
  4117. * for setting the CSS style to all associated members.
  4118. *
  4119. * @param {Number} x
  4120. * @param {Number} y
  4121. * @param {Number} width
  4122. * @param {Number} height
  4123. */
  4124. clipRect: function (x, y, width, height) {
  4125. // create a dummy element
  4126. var clipRect = this.createElement();
  4127. // mimic a rectangle with its style object for automatic updating in attr
  4128. return extend(clipRect, {
  4129. members: [],
  4130. left: x,
  4131. top: y,
  4132. width: width,
  4133. height: height,
  4134. getCSS: function (inverted) {
  4135. var rect = this,//clipRect.element.style,
  4136. top = rect.top,
  4137. left = rect.left,
  4138. right = left + rect.width,
  4139. bottom = top + rect.height,
  4140. ret = {
  4141. clip: 'rect(' +
  4142. mathRound(inverted ? left : top) + 'px,' +
  4143. mathRound(inverted ? bottom : right) + 'px,' +
  4144. mathRound(inverted ? right : bottom) + 'px,' +
  4145. mathRound(inverted ? top : left) + 'px)'
  4146. };
  4147. // issue 74 workaround
  4148. if (!inverted && docMode8) {
  4149. extend(ret, {
  4150. width: right + PX,
  4151. height: bottom + PX
  4152. });
  4153. }
  4154. return ret;
  4155. },
  4156. // used in attr and animation to update the clipping of all members
  4157. updateClipping: function () {
  4158. each(clipRect.members, function (member) {
  4159. member.css(clipRect.getCSS(member.inverted));
  4160. });
  4161. }
  4162. });
  4163. },
  4164. /**
  4165. * Take a color and return it if it's a string, make it a gradient if it's a
  4166. * gradient configuration object, and apply opacity.
  4167. *
  4168. * @param {Object} color The color or config object
  4169. */
  4170. color: function (color, elem, prop) {
  4171. var colorObject,
  4172. regexRgba = /^rgba/,
  4173. markup;
  4174. if (color && color[LINEAR_GRADIENT]) {
  4175. var stopColor,
  4176. stopOpacity,
  4177. linearGradient = color[LINEAR_GRADIENT],
  4178. x1 = linearGradient.x1 || linearGradient[0] || 0,
  4179. y1 = linearGradient.y1 || linearGradient[1] || 0,
  4180. x2 = linearGradient.x2 || linearGradient[2] || 0,
  4181. y2 = linearGradient.y2 || linearGradient[3] || 0,
  4182. angle,
  4183. color1,
  4184. opacity1,
  4185. color2,
  4186. opacity2;
  4187. each(color.stops, function (stop, i) {
  4188. if (regexRgba.test(stop[1])) {
  4189. colorObject = Color(stop[1]);
  4190. stopColor = colorObject.get('rgb');
  4191. stopOpacity = colorObject.get('a');
  4192. } else {
  4193. stopColor = stop[1];
  4194. stopOpacity = 1;
  4195. }
  4196. if (!i) { // first
  4197. color1 = stopColor;
  4198. opacity1 = stopOpacity;
  4199. } else {
  4200. color2 = stopColor;
  4201. opacity2 = stopOpacity;
  4202. }
  4203. });
  4204. // Apply the gradient to fills only.
  4205. if (prop === 'fill') {
  4206. // calculate the angle based on the linear vector
  4207. angle = 90 - math.atan(
  4208. (y2 - y1) / // y vector
  4209. (x2 - x1) // x vector
  4210. ) * 180 / mathPI;
  4211. // when colors attribute is used, the meanings of opacity and o:opacity2
  4212. // are reversed.
  4213. markup = ['<fill colors="0% ', color1, ',100% ', color2, '" angle="', angle,
  4214. '" opacity="', opacity2, '" o:opacity2="', opacity1,
  4215. '" type="gradient" focus="100%" method="sigma" />'];
  4216. createElement(this.prepVML(markup), null, null, elem);
  4217. // Gradients are not supported for VML stroke, return the first color. #722.
  4218. } else {
  4219. return stopColor;
  4220. }
  4221. // if the color is an rgba color, split it and add a fill node
  4222. // to hold the opacity component
  4223. } else if (regexRgba.test(color) && elem.tagName !== 'IMG') {
  4224. colorObject = Color(color);
  4225. markup = ['<', prop, ' opacity="', colorObject.get('a'), '"/>'];
  4226. createElement(this.prepVML(markup), null, null, elem);
  4227. return colorObject.get('rgb');
  4228. } else {
  4229. var strokeNodes = elem.getElementsByTagName(prop);
  4230. if (strokeNodes.length) {
  4231. strokeNodes[0].opacity = 1;
  4232. }
  4233. return color;
  4234. }
  4235. },
  4236. /**
  4237. * Take a VML string and prepare it for either IE8 or IE6/IE7.
  4238. * @param {Array} markup A string array of the VML markup to prepare
  4239. */
  4240. prepVML: function (markup) {
  4241. var vmlStyle = 'display:inline-block;behavior:url(#default#VML);',
  4242. isIE8 = this.isIE8;
  4243. markup = markup.join('');
  4244. if (isIE8) { // add xmlns and style inline
  4245. markup = markup.replace('/>', ' xmlns="urn:schemas-microsoft-com:vml" />');
  4246. if (markup.indexOf('style="') === -1) {
  4247. markup = markup.replace('/>', ' style="' + vmlStyle + '" />');
  4248. } else {
  4249. markup = markup.replace('style="', 'style="' + vmlStyle);
  4250. }
  4251. } else { // add namespace
  4252. markup = markup.replace('<', '<hcv:');
  4253. }
  4254. return markup;
  4255. },
  4256. /**
  4257. * Create rotated and aligned text
  4258. * @param {String} str
  4259. * @param {Number} x
  4260. * @param {Number} y
  4261. */
  4262. text: SVGRenderer.prototype.html,
  4263. /**
  4264. * Create and return a path element
  4265. * @param {Array} path
  4266. */
  4267. path: function (path) {
  4268. // create the shape
  4269. return this.createElement('shape').attr({
  4270. // subpixel precision down to 0.1 (width and height = 10px)
  4271. coordsize: '100 100',
  4272. d: path
  4273. });
  4274. },
  4275. /**
  4276. * Create and return a circle element. In VML circles are implemented as
  4277. * shapes, which is faster than v:oval
  4278. * @param {Number} x
  4279. * @param {Number} y
  4280. * @param {Number} r
  4281. */
  4282. circle: function (x, y, r) {
  4283. return this.symbol('circle').attr({ x: x - r, y: y - r, width: 2 * r, height: 2 * r });
  4284. },
  4285. /**
  4286. * Create a group using an outer div and an inner v:group to allow rotating
  4287. * and flipping. A simple v:group would have problems with positioning
  4288. * child HTML elements and CSS clip.
  4289. *
  4290. * @param {String} name The name of the group
  4291. */
  4292. g: function (name) {
  4293. var wrapper,
  4294. attribs;
  4295. // set the class name
  4296. if (name) {
  4297. attribs = { 'className': PREFIX + name, 'class': PREFIX + name };
  4298. }
  4299. // the div to hold HTML and clipping
  4300. wrapper = this.createElement(DIV).attr(attribs);
  4301. return wrapper;
  4302. },
  4303. /**
  4304. * VML override to create a regular HTML image
  4305. * @param {String} src
  4306. * @param {Number} x
  4307. * @param {Number} y
  4308. * @param {Number} width
  4309. * @param {Number} height
  4310. */
  4311. image: function (src, x, y, width, height) {
  4312. var obj = this.createElement('img')
  4313. .attr({ src: src });
  4314. if (arguments.length > 1) {
  4315. obj.css({
  4316. left: x,
  4317. top: y,
  4318. width: width,
  4319. height: height
  4320. });
  4321. }
  4322. return obj;
  4323. },
  4324. /**
  4325. * VML uses a shape for rect to overcome bugs and rotation problems
  4326. */
  4327. rect: function (x, y, width, height, r, strokeWidth) {
  4328. if (isObject(x)) {
  4329. y = x.y;
  4330. width = x.width;
  4331. height = x.height;
  4332. strokeWidth = x.strokeWidth;
  4333. x = x.x;
  4334. }
  4335. var wrapper = this.symbol('rect');
  4336. wrapper.r = r;
  4337. return wrapper.attr(wrapper.crisp(strokeWidth, x, y, mathMax(width, 0), mathMax(height, 0)));
  4338. },
  4339. /**
  4340. * In the VML renderer, each child of an inverted div (group) is inverted
  4341. * @param {Object} element
  4342. * @param {Object} parentNode
  4343. */
  4344. invertChild: function (element, parentNode) {
  4345. var parentStyle = parentNode.style;
  4346. css(element, {
  4347. flip: 'x',
  4348. left: pInt(parentStyle.width) - 10,
  4349. top: pInt(parentStyle.height) - 10,
  4350. rotation: -90
  4351. });
  4352. },
  4353. /**
  4354. * Symbol definitions that override the parent SVG renderer's symbols
  4355. *
  4356. */
  4357. symbols: {
  4358. // VML specific arc function
  4359. arc: function (x, y, w, h, options) {
  4360. var start = options.start,
  4361. end = options.end,
  4362. radius = options.r || w || h,
  4363. cosStart = mathCos(start),
  4364. sinStart = mathSin(start),
  4365. cosEnd = mathCos(end),
  4366. sinEnd = mathSin(end),
  4367. innerRadius = options.innerR,
  4368. circleCorrection = 0.08 / radius, // #760
  4369. innerCorrection = (innerRadius && 0.25 / innerRadius) || 0;
  4370. if (end - start === 0) { // no angle, don't show it.
  4371. return ['x'];
  4372. } else if (2 * mathPI - end + start < circleCorrection) { // full circle
  4373. // empirical correction found by trying out the limits for different radii
  4374. cosEnd = -circleCorrection;
  4375. } else if (end - start < innerCorrection) { // issue #186, another mysterious VML arc problem
  4376. cosEnd = mathCos(start + innerCorrection);
  4377. }
  4378. return [
  4379. 'wa', // clockwise arc to
  4380. x - radius, // left
  4381. y - radius, // top
  4382. x + radius, // right
  4383. y + radius, // bottom
  4384. x + radius * cosStart, // start x
  4385. y + radius * sinStart, // start y
  4386. x + radius * cosEnd, // end x
  4387. y + radius * sinEnd, // end y
  4388. 'at', // anti clockwise arc to
  4389. x - innerRadius, // left
  4390. y - innerRadius, // top
  4391. x + innerRadius, // right
  4392. y + innerRadius, // bottom
  4393. x + innerRadius * cosEnd, // start x
  4394. y + innerRadius * sinEnd, // start y
  4395. x + innerRadius * cosStart, // end x
  4396. y + innerRadius * sinStart, // end y
  4397. 'x', // finish path
  4398. 'e' // close
  4399. ];
  4400. },
  4401. // Add circle symbol path. This performs significantly faster than v:oval.
  4402. circle: function (x, y, w, h) {
  4403. return [
  4404. 'wa', // clockwisearcto
  4405. x, // left
  4406. y, // top
  4407. x + w, // right
  4408. y + h, // bottom
  4409. x + w, // start x
  4410. y + h / 2, // start y
  4411. x + w, // end x
  4412. y + h / 2, // end y
  4413. //'x', // finish path
  4414. 'e' // close
  4415. ];
  4416. },
  4417. /**
  4418. * Add rectangle symbol path which eases rotation and omits arcsize problems
  4419. * compared to the built-in VML roundrect shape
  4420. *
  4421. * @param {Number} left Left position
  4422. * @param {Number} top Top position
  4423. * @param {Number} r Border radius
  4424. * @param {Object} options Width and height
  4425. */
  4426. rect: function (left, top, width, height, options) {
  4427. /*for (var n in r) {
  4428. logTime && console .log(n)
  4429. }*/
  4430. if (!defined(options)) {
  4431. return [];
  4432. }
  4433. var right = left + width,
  4434. bottom = top + height,
  4435. r = mathMin(options.r || 0, width, height);
  4436. return [
  4437. M,
  4438. left + r, top,
  4439. L,
  4440. right - r, top,
  4441. 'wa',
  4442. right - 2 * r, top,
  4443. right, top + 2 * r,
  4444. right - r, top,
  4445. right, top + r,
  4446. L,
  4447. right, bottom - r,
  4448. 'wa',
  4449. right - 2 * r, bottom - 2 * r,
  4450. right, bottom,
  4451. right, bottom - r,
  4452. right - r, bottom,
  4453. L,
  4454. left + r, bottom,
  4455. 'wa',
  4456. left, bottom - 2 * r,
  4457. left + 2 * r, bottom,
  4458. left + r, bottom,
  4459. left, bottom - r,
  4460. L,
  4461. left, top + r,
  4462. 'wa',
  4463. left, top,
  4464. left + 2 * r, top + 2 * r,
  4465. left, top + r,
  4466. left + r, top,
  4467. 'x',
  4468. 'e'
  4469. ];
  4470. }
  4471. }
  4472. };
  4473. VMLRenderer = function () {
  4474. this.init.apply(this, arguments);
  4475. };
  4476. VMLRenderer.prototype = merge(SVGRenderer.prototype, VMLRendererExtension);
  4477. // general renderer
  4478. Renderer = VMLRenderer;
  4479. }
  4480. /* ****************************************************************************
  4481. * *
  4482. * END OF INTERNET EXPLORER <= 8 SPECIFIC CODE *
  4483. * *
  4484. *****************************************************************************/
  4485. /* ****************************************************************************
  4486. * *
  4487. * START OF ANDROID < 3 SPECIFIC CODE. THIS CAN BE REMOVED IF YOU'RE NOT *
  4488. * TARGETING THAT SYSTEM. *
  4489. * *
  4490. *****************************************************************************/
  4491. var CanVGRenderer,
  4492. CanVGController;
  4493. if (useCanVG) {
  4494. /**
  4495. * The CanVGRenderer is empty from start to keep the source footprint small.
  4496. * When requested, the CanVGController downloads the rest of the source packaged
  4497. * together with the canvg library.
  4498. */
  4499. CanVGRenderer = function () {
  4500. // Empty constructor
  4501. };
  4502. /**
  4503. * Handles on demand download of canvg rendering support.
  4504. */
  4505. CanVGController = (function () {
  4506. // List of renderering calls
  4507. var deferredRenderCalls = [];
  4508. /**
  4509. * When downloaded, we are ready to draw deferred charts.
  4510. */
  4511. function drawDeferred() {
  4512. var callLength = deferredRenderCalls.length,
  4513. callIndex;
  4514. // Draw all pending render calls
  4515. for (callIndex = 0; callIndex < callLength; callIndex++) {
  4516. deferredRenderCalls[callIndex]();
  4517. }
  4518. // Clear the list
  4519. deferredRenderCalls = [];
  4520. }
  4521. return {
  4522. push: function (func, scriptLocation) {
  4523. // Only get the script once
  4524. if (deferredRenderCalls.length === 0) {
  4525. getScript(scriptLocation, drawDeferred);
  4526. }
  4527. // Register render call
  4528. deferredRenderCalls.push(func);
  4529. }
  4530. };
  4531. }());
  4532. } // end CanVGRenderer
  4533. /* ****************************************************************************
  4534. * *
  4535. * END OF ANDROID < 3 SPECIFIC CODE *
  4536. * *
  4537. *****************************************************************************/
  4538. /**
  4539. * General renderer
  4540. */
  4541. Renderer = VMLRenderer || CanVGRenderer || SVGRenderer;
  4542. /**
  4543. * The chart class
  4544. * @param {Object} options
  4545. * @param {Function} callback Function to run when the chart has loaded
  4546. */
  4547. function Chart(userOptions, callback) {
  4548. // Handle regular options
  4549. var options,
  4550. seriesOptions = userOptions.series; // skip merging data points to increase performance
  4551. userOptions.series = null;
  4552. options = merge(defaultOptions, userOptions); // do the merge
  4553. options.series = userOptions.series = seriesOptions; // set back the series data
  4554. var optionsChart = options.chart,
  4555. optionsMargin = optionsChart.margin,
  4556. margin = isObject(optionsMargin) ?
  4557. optionsMargin :
  4558. [optionsMargin, optionsMargin, optionsMargin, optionsMargin],
  4559. optionsMarginTop = pick(optionsChart.marginTop, margin[0]),
  4560. optionsMarginRight = pick(optionsChart.marginRight, margin[1]),
  4561. optionsMarginBottom = pick(optionsChart.marginBottom, margin[2]),
  4562. optionsMarginLeft = pick(optionsChart.marginLeft, margin[3]),
  4563. spacingTop = optionsChart.spacingTop,
  4564. spacingRight = optionsChart.spacingRight,
  4565. spacingBottom = optionsChart.spacingBottom,
  4566. spacingLeft = optionsChart.spacingLeft,
  4567. spacingBox,
  4568. chartTitleOptions,
  4569. chartSubtitleOptions,
  4570. plotTop,
  4571. marginRight,
  4572. marginBottom,
  4573. plotLeft,
  4574. axisOffset,
  4575. renderTo,
  4576. renderToClone,
  4577. container,
  4578. containerId,
  4579. containerWidth,
  4580. containerHeight,
  4581. chartWidth,
  4582. chartHeight,
  4583. oldChartWidth,
  4584. oldChartHeight,
  4585. chartBackground,
  4586. plotBackground,
  4587. plotBGImage,
  4588. plotBorder,
  4589. chart = this,
  4590. chartEvents = optionsChart.events,
  4591. runChartClick = chartEvents && !!chartEvents.click,
  4592. eventType,
  4593. isInsidePlot, // function
  4594. tooltip,
  4595. mouseIsDown,
  4596. loadingDiv,
  4597. loadingSpan,
  4598. loadingShown,
  4599. plotHeight,
  4600. plotWidth,
  4601. tracker,
  4602. trackerGroup,
  4603. legend,
  4604. legendWidth,
  4605. legendHeight,
  4606. chartPosition,
  4607. hasCartesianSeries = optionsChart.showAxes,
  4608. isResizing = 0,
  4609. axes = [],
  4610. maxTicks, // handle the greatest amount of ticks on grouped axes
  4611. series = [],
  4612. inverted,
  4613. renderer,
  4614. tooltipTick,
  4615. tooltipInterval,
  4616. hoverX,
  4617. drawChartBox, // function
  4618. getMargins, // function
  4619. resetMargins, // function
  4620. setChartSize, // function
  4621. resize,
  4622. zoom, // function
  4623. zoomOut; // function
  4624. /**
  4625. * Create a new axis object
  4626. * @param {Object} options
  4627. */
  4628. function Axis(userOptions) {
  4629. // Define variables
  4630. var isXAxis = userOptions.isX,
  4631. opposite = userOptions.opposite, // needed in setOptions
  4632. horiz = inverted ? !isXAxis : isXAxis,
  4633. side = horiz ?
  4634. (opposite ? 0 : 2) : // top : bottom
  4635. (opposite ? 1 : 3), // right : left
  4636. stacks = {},
  4637. options = merge(
  4638. isXAxis ? defaultXAxisOptions : defaultYAxisOptions,
  4639. [defaultTopAxisOptions, defaultRightAxisOptions,
  4640. defaultBottomAxisOptions, defaultLeftAxisOptions][side],
  4641. userOptions
  4642. ),
  4643. axis = this,
  4644. axisTitle,
  4645. type = options.type,
  4646. isDatetimeAxis = type === 'datetime',
  4647. isLog = type === 'logarithmic',
  4648. offset = options.offset || 0,
  4649. xOrY = isXAxis ? 'x' : 'y',
  4650. axisLength = 0,
  4651. oldAxisLength,
  4652. transA, // translation factor
  4653. transB, // translation addend
  4654. oldTransA, // used for prerendering
  4655. axisLeft,
  4656. axisTop,
  4657. axisWidth,
  4658. axisHeight,
  4659. axisBottom,
  4660. axisRight,
  4661. translate, // fn
  4662. setAxisTranslation, // fn
  4663. getPlotLinePath, // fn
  4664. axisGroup,
  4665. gridGroup,
  4666. axisLine,
  4667. dataMin,
  4668. dataMax,
  4669. minRange = options.minRange || options.maxZoom,
  4670. range = options.range,
  4671. userMin,
  4672. userMax,
  4673. oldUserMin,
  4674. oldUserMax,
  4675. max = null,
  4676. min = null,
  4677. oldMin,
  4678. oldMax,
  4679. minPadding = options.minPadding,
  4680. maxPadding = options.maxPadding,
  4681. minPixelPadding = 0,
  4682. isLinked = defined(options.linkedTo),
  4683. linkedParent,
  4684. ignoreMinPadding, // can be set to true by a column or bar series
  4685. ignoreMaxPadding,
  4686. usePercentage,
  4687. events = options.events,
  4688. eventType,
  4689. plotLinesAndBands = [],
  4690. tickInterval,
  4691. minorTickInterval,
  4692. magnitude,
  4693. tickPositions, // array containing predefined positions
  4694. tickPositioner = options.tickPositioner,
  4695. ticks = {},
  4696. minorTicks = {},
  4697. alternateBands = {},
  4698. tickAmount,
  4699. labelOffset,
  4700. axisTitleMargin,// = options.title.margin,
  4701. categories = options.categories,
  4702. labelFormatter = options.labels.formatter || // can be overwritten by dynamic format
  4703. function () {
  4704. var value = this.value,
  4705. dateTimeLabelFormat = this.dateTimeLabelFormat,
  4706. ret;
  4707. if (dateTimeLabelFormat) { // datetime axis
  4708. ret = dateFormat(dateTimeLabelFormat, value);
  4709. } else if (tickInterval % 1000000 === 0) { // use M abbreviation
  4710. ret = (value / 1000000) + 'M';
  4711. } else if (tickInterval % 1000 === 0) { // use k abbreviation
  4712. ret = (value / 1000) + 'k';
  4713. } else if (!categories && value >= 1000) { // add thousands separators
  4714. ret = numberFormat(value, 0);
  4715. } else { // strings (categories) and small numbers
  4716. ret = value;
  4717. }
  4718. return ret;
  4719. },
  4720. staggerLines = horiz && options.labels.staggerLines,
  4721. reversed = options.reversed,
  4722. tickmarkOffset = (categories && options.tickmarkPlacement === 'between') ? 0.5 : 0;
  4723. /**
  4724. * The Tick class
  4725. */
  4726. function Tick(pos, type) {
  4727. var tick = this;
  4728. tick.pos = pos;
  4729. tick.type = type || '';
  4730. tick.isNew = true;
  4731. if (!type) {
  4732. tick.addLabel();
  4733. }
  4734. }
  4735. Tick.prototype = {
  4736. /**
  4737. * Write the tick label
  4738. */
  4739. addLabel: function () {
  4740. var tick = this,
  4741. pos = tick.pos,
  4742. labelOptions = options.labels,
  4743. str,
  4744. width = (categories && horiz && categories.length &&
  4745. !labelOptions.step && !labelOptions.staggerLines &&
  4746. !labelOptions.rotation &&
  4747. plotWidth / categories.length) ||
  4748. (!horiz && plotWidth / 2),
  4749. isFirst = pos === tickPositions[0],
  4750. isLast = pos === tickPositions[tickPositions.length - 1],
  4751. css,
  4752. value = categories && defined(categories[pos]) ? categories[pos] : pos,
  4753. label = tick.label,
  4754. tickPositionInfo = tickPositions.info,
  4755. dateTimeLabelFormat;
  4756. // Set the datetime label format. If a higher rank is set for this position, use that. If not,
  4757. // use the general format.
  4758. if (isDatetimeAxis && tickPositionInfo) {
  4759. dateTimeLabelFormat = options.dateTimeLabelFormats[tickPositionInfo.higherRanks[pos] || tickPositionInfo.unitName];
  4760. }
  4761. // set properties for access in render method
  4762. tick.isFirst = isFirst;
  4763. tick.isLast = isLast;
  4764. // get the string
  4765. str = labelFormatter.call({
  4766. axis: axis,
  4767. chart: chart,
  4768. isFirst: isFirst,
  4769. isLast: isLast,
  4770. dateTimeLabelFormat: dateTimeLabelFormat,
  4771. value: isLog ? correctFloat(lin2log(value)) : value
  4772. });
  4773. // prepare CSS
  4774. css = width && { width: mathMax(1, mathRound(width - 2 * (labelOptions.padding || 10))) + PX };
  4775. css = extend(css, labelOptions.style);
  4776. // first call
  4777. if (!defined(label)) {
  4778. tick.label =
  4779. defined(str) && labelOptions.enabled ?
  4780. renderer.text(
  4781. str,
  4782. 0,
  4783. 0,
  4784. labelOptions.useHTML
  4785. )
  4786. .attr({
  4787. align: labelOptions.align,
  4788. rotation: labelOptions.rotation
  4789. })
  4790. // without position absolute, IE export sometimes is wrong
  4791. .css(css)
  4792. .add(axisGroup) :
  4793. null;
  4794. // update
  4795. } else if (label) {
  4796. label.attr({
  4797. text: str
  4798. })
  4799. .css(css);
  4800. }
  4801. },
  4802. /**
  4803. * Get the offset height or width of the label
  4804. */
  4805. getLabelSize: function () {
  4806. var label = this.label;
  4807. return label ?
  4808. ((this.labelBBox = label.getBBox(true)))[horiz ? 'height' : 'width'] :
  4809. 0;
  4810. },
  4811. /**
  4812. * Find how far the labels extend to the right and left of the tick's x position. Used for anti-collision
  4813. * detection with overflow logic.
  4814. */
  4815. getLabelSides: function () {
  4816. var bBox = this.labelBBox, // assume getLabelSize has run at this point
  4817. labelOptions = options.labels,
  4818. width = bBox.width,
  4819. leftSide = width * { left: 0, center: 0.5, right: 1 }[labelOptions.align] - labelOptions.x;
  4820. return [-leftSide, width - leftSide];
  4821. },
  4822. /**
  4823. * Handle the label overflow by adjusting the labels to the left and right edge, or
  4824. * hide them if they collide into the neighbour label.
  4825. */
  4826. handleOverflow: function (index) {
  4827. var show = true,
  4828. isFirst = this.isFirst,
  4829. isLast = this.isLast,
  4830. label = this.label,
  4831. x = label.x;
  4832. if (isFirst || isLast) {
  4833. var sides = this.getLabelSides(),
  4834. leftSide = sides[0],
  4835. rightSide = sides[1],
  4836. plotLeft = chart.plotLeft,
  4837. plotRight = plotLeft + axis.len,
  4838. neighbour = ticks[tickPositions[index + (isFirst ? 1 : -1)]],
  4839. neighbourEdge = neighbour && neighbour.label.x + neighbour.getLabelSides()[isFirst ? 0 : 1];
  4840. if ((isFirst && !reversed) || (isLast && reversed)) {
  4841. // Is the label spilling out to the left of the plot area?
  4842. if (x + leftSide < plotLeft) {
  4843. // Align it to plot left
  4844. x = plotLeft - leftSide;
  4845. // Hide it if it now overlaps the neighbour label
  4846. if (neighbour && x + rightSide > neighbourEdge) {
  4847. show = false;
  4848. }
  4849. }
  4850. } else {
  4851. // Is the label spilling out to the right of the plot area?
  4852. if (x + rightSide > plotRight) {
  4853. // Align it to plot right
  4854. x = plotRight - rightSide;
  4855. // Hide it if it now overlaps the neighbour label
  4856. if (neighbour && x + leftSide < neighbourEdge) {
  4857. show = false;
  4858. }
  4859. }
  4860. }
  4861. // Set the modified x position of the label
  4862. label.x = x;
  4863. }
  4864. return show;
  4865. },
  4866. /**
  4867. * Put everything in place
  4868. *
  4869. * @param index {Number}
  4870. * @param old {Boolean} Use old coordinates to prepare an animation into new position
  4871. */
  4872. render: function (index, old) {
  4873. var tick = this,
  4874. type = tick.type,
  4875. label = tick.label,
  4876. pos = tick.pos,
  4877. labelOptions = options.labels,
  4878. gridLine = tick.gridLine,
  4879. gridPrefix = type ? type + 'Grid' : 'grid',
  4880. tickPrefix = type ? type + 'Tick' : 'tick',
  4881. gridLineWidth = options[gridPrefix + 'LineWidth'],
  4882. gridLineColor = options[gridPrefix + 'LineColor'],
  4883. dashStyle = options[gridPrefix + 'LineDashStyle'],
  4884. tickLength = options[tickPrefix + 'Length'],
  4885. tickWidth = options[tickPrefix + 'Width'] || 0,
  4886. tickColor = options[tickPrefix + 'Color'],
  4887. tickPosition = options[tickPrefix + 'Position'],
  4888. gridLinePath,
  4889. mark = tick.mark,
  4890. markPath,
  4891. step = labelOptions.step,
  4892. cHeight = (old && oldChartHeight) || chartHeight,
  4893. attribs,
  4894. show = true,
  4895. x,
  4896. y;
  4897. // get x and y position for ticks and labels
  4898. x = horiz ?
  4899. translate(pos + tickmarkOffset, null, null, old) + transB :
  4900. axisLeft + offset + (opposite ? ((old && oldChartWidth) || chartWidth) - axisRight - axisLeft : 0);
  4901. y = horiz ?
  4902. cHeight - axisBottom + offset - (opposite ? axisHeight : 0) :
  4903. cHeight - translate(pos + tickmarkOffset, null, null, old) - transB;
  4904. // create the grid line
  4905. if (gridLineWidth) {
  4906. gridLinePath = getPlotLinePath(pos + tickmarkOffset, gridLineWidth, old);
  4907. if (gridLine === UNDEFINED) {
  4908. attribs = {
  4909. stroke: gridLineColor,
  4910. 'stroke-width': gridLineWidth
  4911. };
  4912. if (dashStyle) {
  4913. attribs.dashstyle = dashStyle;
  4914. }
  4915. if (!type) {
  4916. attribs.zIndex = 1;
  4917. }
  4918. tick.gridLine = gridLine =
  4919. gridLineWidth ?
  4920. renderer.path(gridLinePath)
  4921. .attr(attribs).add(gridGroup) :
  4922. null;
  4923. }
  4924. // If the parameter 'old' is set, the current call will be followed
  4925. // by another call, therefore do not do any animations this time
  4926. if (!old && gridLine && gridLinePath) {
  4927. gridLine[tick.isNew ? 'attr' : 'animate']({
  4928. d: gridLinePath
  4929. });
  4930. }
  4931. }
  4932. // create the tick mark
  4933. if (tickWidth) {
  4934. // negate the length
  4935. if (tickPosition === 'inside') {
  4936. tickLength = -tickLength;
  4937. }
  4938. if (opposite) {
  4939. tickLength = -tickLength;
  4940. }
  4941. markPath = renderer.crispLine([
  4942. M,
  4943. x,
  4944. y,
  4945. L,
  4946. x + (horiz ? 0 : -tickLength),
  4947. y + (horiz ? tickLength : 0)
  4948. ], tickWidth);
  4949. if (mark) { // updating
  4950. mark.animate({
  4951. d: markPath
  4952. });
  4953. } else { // first time
  4954. tick.mark = renderer.path(
  4955. markPath
  4956. ).attr({
  4957. stroke: tickColor,
  4958. 'stroke-width': tickWidth
  4959. }).add(axisGroup);
  4960. }
  4961. }
  4962. // the label is created on init - now move it into place
  4963. if (label && !isNaN(x)) {
  4964. x = x + labelOptions.x - (tickmarkOffset && horiz ?
  4965. tickmarkOffset * transA * (reversed ? -1 : 1) : 0);
  4966. y = y + labelOptions.y - (tickmarkOffset && !horiz ?
  4967. tickmarkOffset * transA * (reversed ? 1 : -1) : 0);
  4968. // vertically centered
  4969. if (!defined(labelOptions.y)) {
  4970. y += pInt(label.styles.lineHeight) * 0.9 - label.getBBox().height / 2;
  4971. }
  4972. // correct for staggered labels
  4973. if (staggerLines) {
  4974. y += (index / (step || 1) % staggerLines) * 16;
  4975. }
  4976. // Cache x and y to be able to read final position before animation
  4977. label.x = x;
  4978. label.y = y;
  4979. // apply show first and show last
  4980. if ((tick.isFirst && !pick(options.showFirstLabel, 1)) ||
  4981. (tick.isLast && !pick(options.showLastLabel, 1))) {
  4982. show = false;
  4983. // Handle label overflow and show or hide accordingly
  4984. } else if (!staggerLines && horiz && labelOptions.overflow === 'justify' && !tick.handleOverflow(index)) {
  4985. show = false;
  4986. }
  4987. // apply step
  4988. if (step && index % step) {
  4989. // show those indices dividable by step
  4990. show = false;
  4991. }
  4992. // Set the new position, and show or hide
  4993. if (show) {
  4994. label[tick.isNew ? 'attr' : 'animate']({
  4995. x: label.x,
  4996. y: label.y
  4997. });
  4998. label.show();
  4999. tick.isNew = false;
  5000. } else {
  5001. label.hide();
  5002. }
  5003. }
  5004. },
  5005. /**
  5006. * Destructor for the tick prototype
  5007. */
  5008. destroy: function () {
  5009. destroyObjectProperties(this);
  5010. }
  5011. };
  5012. /**
  5013. * The object wrapper for plot lines and plot bands
  5014. * @param {Object} options
  5015. */
  5016. function PlotLineOrBand(options) {
  5017. var plotLine = this;
  5018. if (options) {
  5019. plotLine.options = options;
  5020. plotLine.id = options.id;
  5021. }
  5022. //plotLine.render()
  5023. return plotLine;
  5024. }
  5025. PlotLineOrBand.prototype = {
  5026. /**
  5027. * Render the plot line or plot band. If it is already existing,
  5028. * move it.
  5029. */
  5030. render: function () {
  5031. var plotLine = this,
  5032. halfPointRange = (axis.pointRange || 0) / 2,
  5033. options = plotLine.options,
  5034. optionsLabel = options.label,
  5035. label = plotLine.label,
  5036. width = options.width,
  5037. to = options.to,
  5038. from = options.from,
  5039. value = options.value,
  5040. toPath, // bands only
  5041. dashStyle = options.dashStyle,
  5042. svgElem = plotLine.svgElem,
  5043. path = [],
  5044. addEvent,
  5045. eventType,
  5046. xs,
  5047. ys,
  5048. x,
  5049. y,
  5050. color = options.color,
  5051. zIndex = options.zIndex,
  5052. events = options.events,
  5053. attribs;
  5054. // logarithmic conversion
  5055. if (isLog) {
  5056. from = log2lin(from);
  5057. to = log2lin(to);
  5058. value = log2lin(value);
  5059. }
  5060. // plot line
  5061. if (width) {
  5062. path = getPlotLinePath(value, width);
  5063. attribs = {
  5064. stroke: color,
  5065. 'stroke-width': width
  5066. };
  5067. if (dashStyle) {
  5068. attribs.dashstyle = dashStyle;
  5069. }
  5070. } else if (defined(from) && defined(to)) { // plot band
  5071. // keep within plot area
  5072. from = mathMax(from, min - halfPointRange);
  5073. to = mathMin(to, max + halfPointRange);
  5074. toPath = getPlotLinePath(to);
  5075. path = getPlotLinePath(from);
  5076. if (path && toPath) {
  5077. path.push(
  5078. toPath[4],
  5079. toPath[5],
  5080. toPath[1],
  5081. toPath[2]
  5082. );
  5083. } else { // outside the axis area
  5084. path = null;
  5085. }
  5086. attribs = {
  5087. fill: color
  5088. };
  5089. } else {
  5090. return;
  5091. }
  5092. // zIndex
  5093. if (defined(zIndex)) {
  5094. attribs.zIndex = zIndex;
  5095. }
  5096. // common for lines and bands
  5097. if (svgElem) {
  5098. if (path) {
  5099. svgElem.animate({
  5100. d: path
  5101. }, null, svgElem.onGetPath);
  5102. } else {
  5103. svgElem.hide();
  5104. svgElem.onGetPath = function () {
  5105. svgElem.show();
  5106. };
  5107. }
  5108. } else if (path && path.length) {
  5109. plotLine.svgElem = svgElem = renderer.path(path)
  5110. .attr(attribs).add();
  5111. // events
  5112. if (events) {
  5113. addEvent = function (eventType) {
  5114. svgElem.on(eventType, function (e) {
  5115. events[eventType].apply(plotLine, [e]);
  5116. });
  5117. };
  5118. for (eventType in events) {
  5119. addEvent(eventType);
  5120. }
  5121. }
  5122. }
  5123. // the plot band/line label
  5124. if (optionsLabel && defined(optionsLabel.text) && path && path.length && axisWidth > 0 && axisHeight > 0) {
  5125. // apply defaults
  5126. optionsLabel = merge({
  5127. align: horiz && toPath && 'center',
  5128. x: horiz ? !toPath && 4 : 10,
  5129. verticalAlign : !horiz && toPath && 'middle',
  5130. y: horiz ? toPath ? 16 : 10 : toPath ? 6 : -4,
  5131. rotation: horiz && !toPath && 90
  5132. }, optionsLabel);
  5133. // add the SVG element
  5134. if (!label) {
  5135. plotLine.label = label = renderer.text(
  5136. optionsLabel.text,
  5137. 0,
  5138. 0
  5139. )
  5140. .attr({
  5141. align: optionsLabel.textAlign || optionsLabel.align,
  5142. rotation: optionsLabel.rotation,
  5143. zIndex: zIndex
  5144. })
  5145. .css(optionsLabel.style)
  5146. .add();
  5147. }
  5148. // get the bounding box and align the label
  5149. xs = [path[1], path[4], pick(path[6], path[1])];
  5150. ys = [path[2], path[5], pick(path[7], path[2])];
  5151. x = arrayMin(xs);
  5152. y = arrayMin(ys);
  5153. label.align(optionsLabel, false, {
  5154. x: x,
  5155. y: y,
  5156. width: arrayMax(xs) - x,
  5157. height: arrayMax(ys) - y
  5158. });
  5159. label.show();
  5160. } else if (label) { // move out of sight
  5161. label.hide();
  5162. }
  5163. // chainable
  5164. return plotLine;
  5165. },
  5166. /**
  5167. * Remove the plot line or band
  5168. */
  5169. destroy: function () {
  5170. var obj = this;
  5171. destroyObjectProperties(obj);
  5172. // remove it from the lookup
  5173. erase(plotLinesAndBands, obj);
  5174. }
  5175. };
  5176. /**
  5177. * The class for stack items
  5178. */
  5179. function StackItem(options, isNegative, x, stackOption) {
  5180. var stackItem = this;
  5181. // Tells if the stack is negative
  5182. stackItem.isNegative = isNegative;
  5183. // Save the options to be able to style the label
  5184. stackItem.options = options;
  5185. // Save the x value to be able to position the label later
  5186. stackItem.x = x;
  5187. // Save the stack option on the series configuration object
  5188. stackItem.stack = stackOption;
  5189. // The align options and text align varies on whether the stack is negative and
  5190. // if the chart is inverted or not.
  5191. // First test the user supplied value, then use the dynamic.
  5192. stackItem.alignOptions = {
  5193. align: options.align || (inverted ? (isNegative ? 'left' : 'right') : 'center'),
  5194. verticalAlign: options.verticalAlign || (inverted ? 'middle' : (isNegative ? 'bottom' : 'top')),
  5195. y: pick(options.y, inverted ? 4 : (isNegative ? 14 : -6)),
  5196. x: pick(options.x, inverted ? (isNegative ? -6 : 6) : 0)
  5197. };
  5198. stackItem.textAlign = options.textAlign || (inverted ? (isNegative ? 'right' : 'left') : 'center');
  5199. }
  5200. StackItem.prototype = {
  5201. destroy: function () {
  5202. destroyObjectProperties(this);
  5203. },
  5204. /**
  5205. * Sets the total of this stack. Should be called when a serie is hidden or shown
  5206. * since that will affect the total of other stacks.
  5207. */
  5208. setTotal: function (total) {
  5209. this.total = total;
  5210. this.cum = total;
  5211. },
  5212. /**
  5213. * Renders the stack total label and adds it to the stack label group.
  5214. */
  5215. render: function (group) {
  5216. var stackItem = this, // aliased this
  5217. str = stackItem.options.formatter.call(stackItem); // format the text in the label
  5218. // Change the text to reflect the new total and set visibility to hidden in case the serie is hidden
  5219. if (stackItem.label) {
  5220. stackItem.label.attr({text: str, visibility: HIDDEN});
  5221. // Create new label
  5222. } else {
  5223. stackItem.label =
  5224. chart.renderer.text(str, 0, 0) // dummy positions, actual position updated with setOffset method in columnseries
  5225. .css(stackItem.options.style) // apply style
  5226. .attr({align: stackItem.textAlign, // fix the text-anchor
  5227. rotation: stackItem.options.rotation, // rotation
  5228. visibility: HIDDEN }) // hidden until setOffset is called
  5229. .add(group); // add to the labels-group
  5230. }
  5231. },
  5232. /**
  5233. * Sets the offset that the stack has from the x value and repositions the label.
  5234. */
  5235. setOffset: function (xOffset, xWidth) {
  5236. var stackItem = this, // aliased this
  5237. neg = stackItem.isNegative, // special treatment is needed for negative stacks
  5238. y = axis.translate(stackItem.total, 0, 0, 0, 1), // stack value translated mapped to chart coordinates
  5239. yZero = axis.translate(0), // stack origin
  5240. h = mathAbs(y - yZero), // stack height
  5241. x = chart.xAxis[0].translate(stackItem.x) + xOffset, // stack x position
  5242. plotHeight = chart.plotHeight,
  5243. stackBox = { // this is the box for the complete stack
  5244. x: inverted ? (neg ? y : y - h) : x,
  5245. y: inverted ? plotHeight - x - xWidth : (neg ? (plotHeight - y - h) : plotHeight - y),
  5246. width: inverted ? h : xWidth,
  5247. height: inverted ? xWidth : h
  5248. };
  5249. if (stackItem.label) {
  5250. stackItem.label
  5251. .align(stackItem.alignOptions, null, stackBox) // align the label to the box
  5252. .attr({visibility: VISIBLE}); // set visibility
  5253. }
  5254. }
  5255. };
  5256. /**
  5257. * Get the minimum and maximum for the series of each axis
  5258. */
  5259. function getSeriesExtremes() {
  5260. var posStack = [],
  5261. negStack = [],
  5262. i;
  5263. // reset dataMin and dataMax in case we're redrawing
  5264. dataMin = dataMax = null;
  5265. // loop through this axis' series
  5266. each(axis.series, function (series) {
  5267. if (series.visible || !optionsChart.ignoreHiddenSeries) {
  5268. var seriesOptions = series.options,
  5269. stacking,
  5270. posPointStack,
  5271. negPointStack,
  5272. stackKey,
  5273. stackOption,
  5274. negKey,
  5275. xData,
  5276. yData,
  5277. x,
  5278. y,
  5279. threshold = seriesOptions.threshold,
  5280. yDataLength,
  5281. activeYData = [],
  5282. activeCounter = 0;
  5283. // Validate threshold in logarithmic axes
  5284. if (isLog && threshold <= 0) {
  5285. threshold = seriesOptions.threshold = null;
  5286. }
  5287. // Get dataMin and dataMax for X axes
  5288. if (isXAxis) {
  5289. xData = series.xData;
  5290. if (xData.length) {
  5291. dataMin = mathMin(pick(dataMin, xData[0]), arrayMin(xData));
  5292. dataMax = mathMax(pick(dataMax, xData[0]), arrayMax(xData));
  5293. }
  5294. // Get dataMin and dataMax for Y axes, as well as handle stacking and processed data
  5295. } else {
  5296. var isNegative,
  5297. pointStack,
  5298. key,
  5299. cropped = series.cropped,
  5300. xExtremes = series.xAxis.getExtremes(),
  5301. //findPointRange,
  5302. //pointRange,
  5303. j,
  5304. hasModifyValue = !!series.modifyValue;
  5305. // Handle stacking
  5306. stacking = seriesOptions.stacking;
  5307. usePercentage = stacking === 'percent';
  5308. // create a stack for this particular series type
  5309. if (stacking) {
  5310. stackOption = seriesOptions.stack;
  5311. stackKey = series.type + pick(stackOption, '');
  5312. negKey = '-' + stackKey;
  5313. series.stackKey = stackKey; // used in translate
  5314. posPointStack = posStack[stackKey] || []; // contains the total values for each x
  5315. posStack[stackKey] = posPointStack;
  5316. negPointStack = negStack[negKey] || [];
  5317. negStack[negKey] = negPointStack;
  5318. }
  5319. if (usePercentage) {
  5320. dataMin = 0;
  5321. dataMax = 99;
  5322. }
  5323. // processData can alter series.pointRange, so this goes after
  5324. //findPointRange = series.pointRange === null;
  5325. xData = series.processedXData;
  5326. yData = series.processedYData;
  5327. yDataLength = yData.length;
  5328. // loop over the non-null y values and read them into a local array
  5329. for (i = 0; i < yDataLength; i++) {
  5330. x = xData[i];
  5331. y = yData[i];
  5332. if (y !== null && y !== UNDEFINED) {
  5333. // read stacked values into a stack based on the x value,
  5334. // the sign of y and the stack key
  5335. if (stacking) {
  5336. isNegative = y < threshold;
  5337. pointStack = isNegative ? negPointStack : posPointStack;
  5338. key = isNegative ? negKey : stackKey;
  5339. y = pointStack[x] =
  5340. defined(pointStack[x]) ?
  5341. pointStack[x] + y : y;
  5342. // add the series
  5343. if (!stacks[key]) {
  5344. stacks[key] = {};
  5345. }
  5346. // If the StackItem is there, just update the values,
  5347. // if not, create one first
  5348. if (!stacks[key][x]) {
  5349. stacks[key][x] = new StackItem(options.stackLabels, isNegative, x, stackOption);
  5350. }
  5351. stacks[key][x].setTotal(y);
  5352. // general hook, used for Highstock compare values feature
  5353. } else if (hasModifyValue) {
  5354. y = series.modifyValue(y);
  5355. }
  5356. // get the smallest distance between points
  5357. /*if (i) {
  5358. distance = mathAbs(xData[i] - xData[i - 1]);
  5359. pointRange = pointRange === UNDEFINED ? distance : mathMin(distance, pointRange);
  5360. }*/
  5361. // for points within the visible range, including the first point outside the
  5362. // visible range, consider y extremes
  5363. if (cropped || ((xData[i + 1] || x) >= xExtremes.min && (xData[i - 1] || x) <= xExtremes.max)) {
  5364. j = y.length;
  5365. if (j) { // array, like ohlc data
  5366. while (j--) {
  5367. if (y[j] !== null) {
  5368. activeYData[activeCounter++] = y[j];
  5369. }
  5370. }
  5371. } else {
  5372. activeYData[activeCounter++] = y;
  5373. }
  5374. }
  5375. }
  5376. }
  5377. // record the least unit distance
  5378. /*if (findPointRange) {
  5379. series.pointRange = pointRange || 1;
  5380. }
  5381. series.closestPointRange = pointRange;*/
  5382. // Get the dataMin and dataMax so far. If percentage is used, the min and max are
  5383. // always 0 and 100. If the length of activeYData is 0, continue with null values.
  5384. if (!usePercentage && activeYData.length) {
  5385. dataMin = mathMin(pick(dataMin, activeYData[0]), arrayMin(activeYData));
  5386. dataMax = mathMax(pick(dataMax, activeYData[0]), arrayMax(activeYData));
  5387. }
  5388. // Adjust to threshold
  5389. if (defined(threshold)) {
  5390. if (dataMin >= threshold) {
  5391. dataMin = threshold;
  5392. ignoreMinPadding = true;
  5393. } else if (dataMax < threshold) {
  5394. dataMax = threshold;
  5395. ignoreMaxPadding = true;
  5396. }
  5397. }
  5398. }
  5399. }
  5400. });
  5401. }
  5402. /**
  5403. * Translate from axis value to pixel position on the chart, or back
  5404. *
  5405. */
  5406. translate = function (val, backwards, cvsCoord, old, handleLog) {
  5407. var sign = 1,
  5408. cvsOffset = 0,
  5409. localA = old ? oldTransA : transA,
  5410. localMin = old ? oldMin : min,
  5411. returnValue,
  5412. postTranslate = options.ordinal || (isLog && handleLog);
  5413. if (!localA) {
  5414. localA = transA;
  5415. }
  5416. if (cvsCoord) {
  5417. sign *= -1; // canvas coordinates inverts the value
  5418. cvsOffset = axisLength;
  5419. }
  5420. if (reversed) { // reversed axis
  5421. sign *= -1;
  5422. cvsOffset -= sign * axisLength;
  5423. }
  5424. if (backwards) { // reverse translation
  5425. if (reversed) {
  5426. val = axisLength - val;
  5427. }
  5428. returnValue = val / localA + localMin; // from chart pixel to value
  5429. if (postTranslate) { // log and ordinal axes
  5430. returnValue = axis.lin2val(returnValue);
  5431. }
  5432. } else { // normal translation, from axis value to pixel, relative to plot
  5433. if (postTranslate) { // log and ordinal axes
  5434. val = axis.val2lin(val);
  5435. }
  5436. returnValue = sign * (val - localMin) * localA + cvsOffset + (sign * minPixelPadding);
  5437. }
  5438. return returnValue;
  5439. };
  5440. /**
  5441. * Create the path for a plot line that goes from the given value on
  5442. * this axis, across the plot to the opposite side
  5443. * @param {Number} value
  5444. * @param {Number} lineWidth Used for calculation crisp line
  5445. * @param {Number] old Use old coordinates (for resizing and rescaling)
  5446. */
  5447. getPlotLinePath = function (value, lineWidth, old) {
  5448. var x1,
  5449. y1,
  5450. x2,
  5451. y2,
  5452. translatedValue = translate(value, null, null, old),
  5453. cHeight = (old && oldChartHeight) || chartHeight,
  5454. cWidth = (old && oldChartWidth) || chartWidth,
  5455. skip;
  5456. x1 = x2 = mathRound(translatedValue + transB);
  5457. y1 = y2 = mathRound(cHeight - translatedValue - transB);
  5458. if (isNaN(translatedValue)) { // no min or max
  5459. skip = true;
  5460. } else if (horiz) {
  5461. y1 = axisTop;
  5462. y2 = cHeight - axisBottom;
  5463. if (x1 < axisLeft || x1 > axisLeft + axisWidth) {
  5464. skip = true;
  5465. }
  5466. } else {
  5467. x1 = axisLeft;
  5468. x2 = cWidth - axisRight;
  5469. if (y1 < axisTop || y1 > axisTop + axisHeight) {
  5470. skip = true;
  5471. }
  5472. }
  5473. return skip ?
  5474. null :
  5475. renderer.crispLine([M, x1, y1, L, x2, y2], lineWidth || 0);
  5476. };
  5477. /**
  5478. * Set the tick positions of a linear axis to round values like whole tens or every five.
  5479. */
  5480. function getLinearTickPositions(tickInterval, min, max) {
  5481. var pos,
  5482. lastPos,
  5483. roundedMin = correctFloat(mathFloor(min / tickInterval) * tickInterval),
  5484. roundedMax = correctFloat(mathCeil(max / tickInterval) * tickInterval),
  5485. tickPositions = [];
  5486. // Populate the intermediate values
  5487. pos = roundedMin;
  5488. while (pos <= roundedMax) {
  5489. // Place the tick on the rounded value
  5490. tickPositions.push(pos);
  5491. // Always add the raw tickInterval, not the corrected one.
  5492. pos = correctFloat(pos + tickInterval);
  5493. // If the interval is not big enough in the current min - max range to actually increase
  5494. // the loop variable, we need to break out to prevent endless loop. Issue #619
  5495. if (pos === lastPos) {
  5496. break;
  5497. }
  5498. // Record the last value
  5499. lastPos = pos;
  5500. }
  5501. return tickPositions;
  5502. }
  5503. /**
  5504. * Set the tick positions of a logarithmic axis
  5505. */
  5506. function getLogTickPositions(interval, min, max, minor) {
  5507. // Since we use this method for both major and minor ticks,
  5508. // use a local variable and return the result
  5509. var positions = [];
  5510. // Reset
  5511. if (!minor) {
  5512. axis._minorAutoInterval = null;
  5513. }
  5514. // First case: All ticks fall on whole logarithms: 1, 10, 100 etc.
  5515. if (interval >= 0.5) {
  5516. interval = mathRound(interval);
  5517. positions = getLinearTickPositions(interval, min, max);
  5518. // Second case: We need intermediary ticks. For example
  5519. // 1, 2, 4, 6, 8, 10, 20, 40 etc.
  5520. } else if (interval >= 0.08) {
  5521. var roundedMin = mathFloor(min),
  5522. intermediate,
  5523. i,
  5524. j,
  5525. len,
  5526. pos,
  5527. lastPos,
  5528. break2;
  5529. if (interval > 0.3) {
  5530. intermediate = [1, 2, 4];
  5531. } else if (interval > 0.15) { // 0.2 equals five minor ticks per 1, 10, 100 etc
  5532. intermediate = [1, 2, 4, 6, 8];
  5533. } else { // 0.1 equals ten minor ticks per 1, 10, 100 etc
  5534. intermediate = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  5535. }
  5536. for (i = roundedMin; i < max + 1 && !break2; i++) {
  5537. len = intermediate.length;
  5538. for (j = 0; j < len && !break2; j++) {
  5539. pos = log2lin(lin2log(i) * intermediate[j]);
  5540. if (pos > min) {
  5541. positions.push(lastPos);
  5542. }
  5543. if (lastPos > max) {
  5544. break2 = true;
  5545. }
  5546. lastPos = pos;
  5547. }
  5548. }
  5549. // Third case: We are so deep in between whole logarithmic values that
  5550. // we might as well handle the tick positions like a linear axis. For
  5551. // example 1.01, 1.02, 1.03, 1.04.
  5552. } else {
  5553. var realMin = lin2log(min),
  5554. realMax = lin2log(max),
  5555. tickIntervalOption = options[minor ? 'minorTickInterval' : 'tickInterval'],
  5556. filteredTickIntervalOption = tickIntervalOption === 'auto' ? null : tickIntervalOption,
  5557. tickPixelIntervalOption = options.tickPixelInterval / (minor ? 5 : 1),
  5558. totalPixelLength = minor ? axisLength / tickPositions.length : axisLength;
  5559. interval = pick(
  5560. filteredTickIntervalOption,
  5561. axis._minorAutoInterval,
  5562. (realMax - realMin) * tickPixelIntervalOption / (totalPixelLength || 1)
  5563. );
  5564. interval = normalizeTickInterval(
  5565. interval,
  5566. null,
  5567. math.pow(10, mathFloor(math.log(interval) / math.LN10))
  5568. );
  5569. positions = map(getLinearTickPositions(
  5570. interval,
  5571. realMin,
  5572. realMax
  5573. ), log2lin);
  5574. if (!minor) {
  5575. axis._minorAutoInterval = interval / 5;
  5576. }
  5577. }
  5578. // Set the axis-level tickInterval variable
  5579. if (!minor) {
  5580. tickInterval = interval;
  5581. }
  5582. return positions;
  5583. }
  5584. /**
  5585. * Return the minor tick positions. For logarithmic axes, reuse the same logic
  5586. * as for major ticks.
  5587. */
  5588. function getMinorTickPositions() {
  5589. var minorTickPositions = [],
  5590. pos,
  5591. i,
  5592. len;
  5593. if (isLog) {
  5594. len = tickPositions.length;
  5595. for (i = 1; i < len; i++) {
  5596. minorTickPositions = minorTickPositions.concat(
  5597. getLogTickPositions(minorTickInterval, tickPositions[i - 1], tickPositions[i], true)
  5598. );
  5599. }
  5600. } else {
  5601. for (pos = min + (tickPositions[0] - min) % minorTickInterval; pos <= max; pos += minorTickInterval) {
  5602. minorTickPositions.push(pos);
  5603. }
  5604. }
  5605. return minorTickPositions;
  5606. }
  5607. /**
  5608. * Adjust the min and max for the minimum range. Keep in mind that the series data is
  5609. * not yet processed, so we don't have information on data cropping and grouping, or
  5610. * updated axis.pointRange or series.pointRange. The data can't be processed until
  5611. * we have finally established min and max.
  5612. */
  5613. function adjustForMinRange() {
  5614. var zoomOffset,
  5615. spaceAvailable = dataMax - dataMin >= minRange,
  5616. closestDataRange,
  5617. i,
  5618. distance,
  5619. xData,
  5620. loopLength,
  5621. minArgs,
  5622. maxArgs;
  5623. // Set the automatic minimum range based on the closest point distance
  5624. if (isXAxis && minRange === UNDEFINED && !isLog) {
  5625. if (defined(options.min) || defined(options.max)) {
  5626. minRange = null; // don't do this again
  5627. } else {
  5628. // Find the closest distance between raw data points, as opposed to
  5629. // closestPointRange that applies to processed points (cropped and grouped)
  5630. each(axis.series, function (series) {
  5631. xData = series.xData;
  5632. loopLength = series.xIncrement ? 1 : xData.length - 1;
  5633. for (i = loopLength; i > 0; i--) {
  5634. distance = xData[i] - xData[i - 1];
  5635. if (closestDataRange === UNDEFINED || distance < closestDataRange) {
  5636. closestDataRange = distance;
  5637. }
  5638. }
  5639. });
  5640. minRange = mathMin(closestDataRange * 5, dataMax - dataMin);
  5641. }
  5642. // A hook for resetting the minRange in series.setData (#878)
  5643. // TODO: remove this in protofy, where xAxis.minRange can be set directly
  5644. axis.setMinRange = function (newMinRange) {
  5645. minRange = newMinRange;
  5646. };
  5647. }
  5648. // if minRange is exceeded, adjust
  5649. if (max - min < minRange) {
  5650. zoomOffset = (minRange - max + min) / 2;
  5651. // if min and max options have been set, don't go beyond it
  5652. minArgs = [min - zoomOffset, pick(options.min, min - zoomOffset)];
  5653. if (spaceAvailable) { // if space is available, stay within the data range
  5654. minArgs[2] = dataMin;
  5655. }
  5656. min = arrayMax(minArgs);
  5657. maxArgs = [min + minRange, pick(options.max, min + minRange)];
  5658. if (spaceAvailable) { // if space is availabe, stay within the data range
  5659. maxArgs[2] = dataMax;
  5660. }
  5661. max = arrayMin(maxArgs);
  5662. // now if the max is adjusted, adjust the min back
  5663. if (max - min < minRange) {
  5664. minArgs[0] = max - minRange;
  5665. minArgs[1] = pick(options.min, max - minRange);
  5666. min = arrayMax(minArgs);
  5667. }
  5668. }
  5669. }
  5670. /**
  5671. * Set the tick positions to round values and optionally extend the extremes
  5672. * to the nearest tick
  5673. */
  5674. function setTickPositions(secondPass) {
  5675. var length,
  5676. linkedParentExtremes,
  5677. tickIntervalOption = options.tickInterval,
  5678. tickPixelIntervalOption = options.tickPixelInterval;
  5679. // linked axis gets the extremes from the parent axis
  5680. if (isLinked) {
  5681. linkedParent = chart[isXAxis ? 'xAxis' : 'yAxis'][options.linkedTo];
  5682. linkedParentExtremes = linkedParent.getExtremes();
  5683. min = pick(linkedParentExtremes.min, linkedParentExtremes.dataMin);
  5684. max = pick(linkedParentExtremes.max, linkedParentExtremes.dataMax);
  5685. if (options.type !== linkedParent.options.type) {
  5686. error(11, 1); // Can't link axes of different type
  5687. }
  5688. } else { // initial min and max from the extreme data values
  5689. min = pick(userMin, options.min, dataMin);
  5690. max = pick(userMax, options.max, dataMax);
  5691. }
  5692. if (isLog) {
  5693. if (!secondPass && mathMin(min, dataMin) <= 0) {
  5694. error(10, 1); // Can't plot negative values on log axis
  5695. }
  5696. min = log2lin(min);
  5697. max = log2lin(max);
  5698. }
  5699. // handle zoomed range
  5700. if (range) {
  5701. userMin = min = mathMax(min, max - range); // #618
  5702. userMax = max;
  5703. if (secondPass) {
  5704. range = null; // don't use it when running setExtremes
  5705. }
  5706. }
  5707. // adjust min and max for the minimum range
  5708. adjustForMinRange();
  5709. // pad the values to get clear of the chart's edges
  5710. if (!categories && !usePercentage && !isLinked && defined(min) && defined(max)) {
  5711. length = (max - min) || 1;
  5712. if (!defined(options.min) && !defined(userMin) && minPadding && (dataMin < 0 || !ignoreMinPadding)) {
  5713. min -= length * minPadding;
  5714. }
  5715. if (!defined(options.max) && !defined(userMax) && maxPadding && (dataMax > 0 || !ignoreMaxPadding)) {
  5716. max += length * maxPadding;
  5717. }
  5718. }
  5719. // get tickInterval
  5720. if (min === max || min === undefined || max === undefined) {
  5721. tickInterval = 1;
  5722. } else if (isLinked && !tickIntervalOption &&
  5723. tickPixelIntervalOption === linkedParent.options.tickPixelInterval) {
  5724. tickInterval = linkedParent.tickInterval;
  5725. } else {
  5726. tickInterval = pick(
  5727. tickIntervalOption,
  5728. categories ? // for categoried axis, 1 is default, for linear axis use tickPix
  5729. 1 :
  5730. (max - min) * tickPixelIntervalOption / (axisLength || 1)
  5731. );
  5732. }
  5733. // Now we're finished detecting min and max, crop and group series data. This
  5734. // is in turn needed in order to find tick positions in ordinal axes.
  5735. if (isXAxis && !secondPass) {
  5736. each(axis.series, function (series) {
  5737. series.processData(min !== oldMin || max !== oldMax);
  5738. });
  5739. }
  5740. // set the translation factor used in translate function
  5741. setAxisTranslation();
  5742. // hook for ordinal axes. To do: merge with below
  5743. if (axis.beforeSetTickPositions) {
  5744. axis.beforeSetTickPositions();
  5745. }
  5746. // hook for extensions, used in Highstock ordinal axes
  5747. if (axis.postProcessTickInterval) {
  5748. tickInterval = axis.postProcessTickInterval(tickInterval);
  5749. }
  5750. // for linear axes, get magnitude and normalize the interval
  5751. if (!isDatetimeAxis && !isLog) { // linear
  5752. magnitude = math.pow(10, mathFloor(math.log(tickInterval) / math.LN10));
  5753. if (!defined(options.tickInterval)) {
  5754. tickInterval = normalizeTickInterval(tickInterval, null, magnitude, options);
  5755. }
  5756. }
  5757. // record the tick interval for linked axis
  5758. axis.tickInterval = tickInterval;
  5759. // get minorTickInterval
  5760. minorTickInterval = options.minorTickInterval === 'auto' && tickInterval ?
  5761. tickInterval / 5 : options.minorTickInterval;
  5762. // find the tick positions
  5763. tickPositions = options.tickPositions || (tickPositioner && tickPositioner.apply(axis, [min, max]));
  5764. if (!tickPositions) {
  5765. if (isDatetimeAxis) {
  5766. tickPositions = (axis.getNonLinearTimeTicks || getTimeTicks)(
  5767. normalizeTimeTickInterval(tickInterval, options.units),
  5768. min,
  5769. max,
  5770. options.startOfWeek,
  5771. axis.ordinalPositions,
  5772. axis.closestPointRange,
  5773. true
  5774. );
  5775. } else if (isLog) {
  5776. tickPositions = getLogTickPositions(tickInterval, min, max);
  5777. } else {
  5778. tickPositions = getLinearTickPositions(tickInterval, min, max);
  5779. }
  5780. }
  5781. if (!isLinked) {
  5782. // reset min/max or remove extremes based on start/end on tick
  5783. var roundedMin = tickPositions[0],
  5784. roundedMax = tickPositions[tickPositions.length - 1];
  5785. if (options.startOnTick) {
  5786. min = roundedMin;
  5787. } else if (min > roundedMin) {
  5788. tickPositions.shift();
  5789. }
  5790. if (options.endOnTick) {
  5791. max = roundedMax;
  5792. } else if (max < roundedMax) {
  5793. tickPositions.pop();
  5794. }
  5795. }
  5796. }
  5797. /**
  5798. * Set the max ticks of either the x and y axis collection. #840.
  5799. */
  5800. axis.setMaxTicks = function () {
  5801. // record the greatest number of ticks for multi axis
  5802. if (!maxTicks) { // first call, or maxTicks have been reset after a zoom operation
  5803. maxTicks = {
  5804. x: 0,
  5805. y: 0
  5806. };
  5807. }
  5808. if (!isLinked && !isDatetimeAxis && tickPositions.length > maxTicks[xOrY] && options.alignTicks !== false) {
  5809. maxTicks[xOrY] = tickPositions.length;
  5810. }
  5811. };
  5812. /**
  5813. * When using multiple axes, adjust the number of ticks to match the highest
  5814. * number of ticks in that group
  5815. */
  5816. function adjustTickAmount() {
  5817. if (maxTicks && maxTicks[xOrY] && !isDatetimeAxis && !categories && !isLinked && options.alignTicks !== false) { // only apply to linear scale
  5818. var oldTickAmount = tickAmount,
  5819. calculatedTickAmount = tickPositions.length;
  5820. // set the axis-level tickAmount to use below
  5821. tickAmount = maxTicks[xOrY];
  5822. if (calculatedTickAmount < tickAmount) {
  5823. while (tickPositions.length < tickAmount) {
  5824. tickPositions.push(correctFloat(
  5825. tickPositions[tickPositions.length - 1] + tickInterval
  5826. ));
  5827. }
  5828. transA *= (calculatedTickAmount - 1) / (tickAmount - 1);
  5829. max = tickPositions[tickPositions.length - 1];
  5830. }
  5831. if (defined(oldTickAmount) && tickAmount !== oldTickAmount) {
  5832. axis.isDirty = true;
  5833. }
  5834. }
  5835. }
  5836. /**
  5837. * Set the scale based on data min and max, user set min and max or options
  5838. *
  5839. */
  5840. function setScale() {
  5841. var type,
  5842. i,
  5843. isDirtyData,
  5844. isDirtyAxisLength;
  5845. oldMin = min;
  5846. oldMax = max;
  5847. oldAxisLength = axisLength;
  5848. // set the new axisLength
  5849. axis.setAxisSize();
  5850. //axisLength = horiz ? axisWidth : axisHeight;
  5851. isDirtyAxisLength = axisLength !== oldAxisLength;
  5852. // is there new data?
  5853. each(axis.series, function (series) {
  5854. if (series.isDirtyData || series.isDirty ||
  5855. series.xAxis.isDirty) { // when x axis is dirty, we need new data extremes for y as well
  5856. isDirtyData = true;
  5857. }
  5858. });
  5859. // do we really need to go through all this?
  5860. if (isDirtyAxisLength || isDirtyData || isLinked ||
  5861. userMin !== oldUserMin || userMax !== oldUserMax) {
  5862. // get data extremes if needed
  5863. getSeriesExtremes();
  5864. // get fixed positions based on tickInterval
  5865. setTickPositions();
  5866. // record old values to decide whether a rescale is necessary later on (#540)
  5867. oldUserMin = userMin;
  5868. oldUserMax = userMax;
  5869. // reset stacks
  5870. if (!isXAxis) {
  5871. for (type in stacks) {
  5872. for (i in stacks[type]) {
  5873. stacks[type][i].cum = stacks[type][i].total;
  5874. }
  5875. }
  5876. }
  5877. // Mark as dirty if it is not already set to dirty and extremes have changed. #595.
  5878. if (!axis.isDirty) {
  5879. axis.isDirty = isDirtyAxisLength || min !== oldMin || max !== oldMax;
  5880. }
  5881. }
  5882. axis.setMaxTicks();
  5883. }
  5884. /**
  5885. * Set the extremes and optionally redraw
  5886. * @param {Number} newMin
  5887. * @param {Number} newMax
  5888. * @param {Boolean} redraw
  5889. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  5890. * configuration
  5891. * @param {Object} eventArguments
  5892. *
  5893. */
  5894. function setExtremes(newMin, newMax, redraw, animation, eventArguments) {
  5895. redraw = pick(redraw, true); // defaults to true
  5896. // Extend the arguments with min and max
  5897. eventArguments = extend(eventArguments, {
  5898. min: newMin,
  5899. max: newMax
  5900. });
  5901. // Fire the event
  5902. fireEvent(axis, 'setExtremes', eventArguments, function () { // the default event handler
  5903. userMin = newMin;
  5904. userMax = newMax;
  5905. // Mark for running afterSetExtremes
  5906. axis.isDirtyExtremes = true;
  5907. // redraw
  5908. if (redraw) {
  5909. chart.redraw(animation);
  5910. }
  5911. });
  5912. }
  5913. /**
  5914. * Update translation information
  5915. */
  5916. setAxisTranslation = function () {
  5917. var range = max - min,
  5918. pointRange = 0,
  5919. closestPointRange,
  5920. seriesClosestPointRange;
  5921. // adjust translation for padding
  5922. if (isXAxis) {
  5923. if (isLinked) {
  5924. pointRange = linkedParent.pointRange;
  5925. } else {
  5926. each(axis.series, function (series) {
  5927. pointRange = mathMax(pointRange, series.pointRange);
  5928. seriesClosestPointRange = series.closestPointRange;
  5929. if (!series.noSharedTooltip && defined(seriesClosestPointRange)) {
  5930. closestPointRange = defined(closestPointRange) ?
  5931. mathMin(closestPointRange, seriesClosestPointRange) :
  5932. seriesClosestPointRange;
  5933. }
  5934. });
  5935. }
  5936. // pointRange means the width reserved for each point, like in a column chart
  5937. axis.pointRange = pointRange;
  5938. // closestPointRange means the closest distance between points. In columns
  5939. // it is mostly equal to pointRange, but in lines pointRange is 0 while closestPointRange
  5940. // is some other value
  5941. axis.closestPointRange = closestPointRange;
  5942. }
  5943. // secondary values
  5944. oldTransA = transA;
  5945. axis.translationSlope = transA = axisLength / ((range + pointRange) || 1);
  5946. transB = horiz ? axisLeft : axisBottom; // translation addend
  5947. minPixelPadding = transA * (pointRange / 2);
  5948. };
  5949. /**
  5950. * Update the axis metrics
  5951. */
  5952. function setAxisSize() {
  5953. var offsetLeft = options.offsetLeft || 0,
  5954. offsetRight = options.offsetRight || 0;
  5955. // basic values
  5956. axisLeft = pick(options.left, plotLeft + offsetLeft);
  5957. axisTop = pick(options.top, plotTop);
  5958. axisWidth = pick(options.width, plotWidth - offsetLeft + offsetRight);
  5959. axisHeight = pick(options.height, plotHeight);
  5960. axisBottom = chartHeight - axisHeight - axisTop;
  5961. axisRight = chartWidth - axisWidth - axisLeft;
  5962. axisLength = mathMax(horiz ? axisWidth : axisHeight, 0); // mathMax fixes #905
  5963. // expose to use in Series object and navigator
  5964. axis.left = axisLeft;
  5965. axis.top = axisTop;
  5966. axis.len = axisLength;
  5967. }
  5968. /**
  5969. * Get the actual axis extremes
  5970. */
  5971. function getExtremes() {
  5972. return {
  5973. min: isLog ? correctFloat(lin2log(min)) : min,
  5974. max: isLog ? correctFloat(lin2log(max)) : max,
  5975. dataMin: dataMin,
  5976. dataMax: dataMax,
  5977. userMin: userMin,
  5978. userMax: userMax
  5979. };
  5980. }
  5981. /**
  5982. * Get the zero plane either based on zero or on the min or max value.
  5983. * Used in bar and area plots
  5984. */
  5985. function getThreshold(threshold) {
  5986. var realMin = isLog ? lin2log(min) : min,
  5987. realMax = isLog ? lin2log(max) : max;
  5988. if (realMin > threshold || threshold === null) {
  5989. threshold = realMin;
  5990. } else if (realMax < threshold) {
  5991. threshold = realMax;
  5992. }
  5993. return translate(threshold, 0, 1, 0, 1);
  5994. }
  5995. /**
  5996. * Add a plot band or plot line after render time
  5997. *
  5998. * @param options {Object} The plotBand or plotLine configuration object
  5999. */
  6000. function addPlotBandOrLine(options) {
  6001. var obj = new PlotLineOrBand(options).render();
  6002. plotLinesAndBands.push(obj);
  6003. return obj;
  6004. }
  6005. /**
  6006. * Render the tick labels to a preliminary position to get their sizes
  6007. */
  6008. function getOffset() {
  6009. var hasData = axis.series.length && defined(min) && defined(max),
  6010. showAxis = hasData || pick(options.showEmpty, true),
  6011. titleOffset = 0,
  6012. titleOffsetOption,
  6013. titleMargin = 0,
  6014. axisTitleOptions = options.title,
  6015. labelOptions = options.labels,
  6016. directionFactor = [-1, 1, 1, -1][side],
  6017. n;
  6018. if (!axisGroup) {
  6019. axisGroup = renderer.g('axis')
  6020. .attr({ zIndex: 7 })
  6021. .add();
  6022. gridGroup = renderer.g('grid')
  6023. .attr({ zIndex: options.gridZIndex || 1 })
  6024. .add();
  6025. }
  6026. labelOffset = 0; // reset
  6027. if (hasData || isLinked) {
  6028. each(tickPositions, function (pos) {
  6029. if (!ticks[pos]) {
  6030. ticks[pos] = new Tick(pos);
  6031. } else {
  6032. ticks[pos].addLabel(); // update labels depending on tick interval
  6033. }
  6034. });
  6035. each(tickPositions, function (pos) {
  6036. // left side must be align: right and right side must have align: left for labels
  6037. if (side === 0 || side === 2 || { 1: 'left', 3: 'right' }[side] === labelOptions.align) {
  6038. // get the highest offset
  6039. labelOffset = mathMax(
  6040. ticks[pos].getLabelSize(),
  6041. labelOffset
  6042. );
  6043. }
  6044. });
  6045. if (staggerLines) {
  6046. labelOffset += (staggerLines - 1) * 16;
  6047. }
  6048. } else { // doesn't have data
  6049. for (n in ticks) {
  6050. ticks[n].destroy();
  6051. delete ticks[n];
  6052. }
  6053. }
  6054. if (axisTitleOptions && axisTitleOptions.text) {
  6055. if (!axisTitle) {
  6056. axisTitle = axis.axisTitle = renderer.text(
  6057. axisTitleOptions.text,
  6058. 0,
  6059. 0,
  6060. axisTitleOptions.useHTML
  6061. )
  6062. .attr({
  6063. zIndex: 7,
  6064. rotation: axisTitleOptions.rotation || 0,
  6065. align:
  6066. axisTitleOptions.textAlign ||
  6067. { low: 'left', middle: 'center', high: 'right' }[axisTitleOptions.align]
  6068. })
  6069. .css(axisTitleOptions.style)
  6070. .add();
  6071. axisTitle.isNew = true;
  6072. }
  6073. if (showAxis) {
  6074. titleOffset = axisTitle.getBBox()[horiz ? 'height' : 'width'];
  6075. titleMargin = pick(axisTitleOptions.margin, horiz ? 5 : 10);
  6076. titleOffsetOption = axisTitleOptions.offset;
  6077. }
  6078. // hide or show the title depending on whether showEmpty is set
  6079. axisTitle[showAxis ? 'show' : 'hide']();
  6080. }
  6081. // handle automatic or user set offset
  6082. offset = directionFactor * pick(options.offset, axisOffset[side]);
  6083. axisTitleMargin =
  6084. pick(titleOffsetOption,
  6085. labelOffset + titleMargin +
  6086. (side !== 2 && labelOffset && directionFactor * options.labels[horiz ? 'y' : 'x'])
  6087. );
  6088. axisOffset[side] = mathMax(
  6089. axisOffset[side],
  6090. axisTitleMargin + titleOffset + directionFactor * offset
  6091. );
  6092. }
  6093. /**
  6094. * Render the axis
  6095. */
  6096. function render() {
  6097. var axisTitleOptions = options.title,
  6098. stackLabelOptions = options.stackLabels,
  6099. alternateGridColor = options.alternateGridColor,
  6100. lineWidth = options.lineWidth,
  6101. lineLeft,
  6102. lineTop,
  6103. linePath,
  6104. hasRendered = chart.hasRendered,
  6105. slideInTicks = hasRendered && defined(oldMin) && !isNaN(oldMin),
  6106. hasData = axis.series.length && defined(min) && defined(max),
  6107. showAxis = hasData || pick(options.showEmpty, true),
  6108. from,
  6109. to;
  6110. // If the series has data draw the ticks. Else only the line and title
  6111. if (hasData || isLinked) {
  6112. // minor ticks
  6113. if (minorTickInterval && !categories) {
  6114. each(getMinorTickPositions(), function (pos) {
  6115. if (!minorTicks[pos]) {
  6116. minorTicks[pos] = new Tick(pos, 'minor');
  6117. }
  6118. // render new ticks in old position
  6119. if (slideInTicks && minorTicks[pos].isNew) {
  6120. minorTicks[pos].render(null, true);
  6121. }
  6122. minorTicks[pos].isActive = true;
  6123. minorTicks[pos].render();
  6124. });
  6125. }
  6126. // Major ticks. Pull out the first item and render it last so that
  6127. // we can get the position of the neighbour label. #808.
  6128. each(tickPositions.slice(1).concat([tickPositions[0]]), function (pos, i) {
  6129. // Reorganize the indices
  6130. i = (i === tickPositions.length - 1) ? 0 : i + 1;
  6131. // linked axes need an extra check to find out if
  6132. if (!isLinked || (pos >= min && pos <= max)) {
  6133. if (!ticks[pos]) {
  6134. ticks[pos] = new Tick(pos);
  6135. }
  6136. // render new ticks in old position
  6137. if (slideInTicks && ticks[pos].isNew) {
  6138. ticks[pos].render(i, true);
  6139. }
  6140. ticks[pos].isActive = true;
  6141. ticks[pos].render(i);
  6142. }
  6143. });
  6144. // alternate grid color
  6145. if (alternateGridColor) {
  6146. each(tickPositions, function (pos, i) {
  6147. if (i % 2 === 0 && pos < max) {
  6148. if (!alternateBands[pos]) {
  6149. alternateBands[pos] = new PlotLineOrBand();
  6150. }
  6151. from = pos;
  6152. to = tickPositions[i + 1] !== UNDEFINED ? tickPositions[i + 1] : max;
  6153. alternateBands[pos].options = {
  6154. from: isLog ? lin2log(from) : from,
  6155. to: isLog ? lin2log(to) : to,
  6156. color: alternateGridColor
  6157. };
  6158. alternateBands[pos].render();
  6159. alternateBands[pos].isActive = true;
  6160. }
  6161. });
  6162. }
  6163. // custom plot lines and bands
  6164. if (!axis._addedPlotLB) { // only first time
  6165. each((options.plotLines || []).concat(options.plotBands || []), function (plotLineOptions) {
  6166. //plotLinesAndBands.push(new PlotLineOrBand(plotLineOptions).render());
  6167. addPlotBandOrLine(plotLineOptions);
  6168. });
  6169. axis._addedPlotLB = true;
  6170. }
  6171. } // end if hasData
  6172. // remove inactive ticks
  6173. each([ticks, minorTicks, alternateBands], function (coll) {
  6174. var pos;
  6175. for (pos in coll) {
  6176. if (!coll[pos].isActive) {
  6177. coll[pos].destroy();
  6178. delete coll[pos];
  6179. } else {
  6180. coll[pos].isActive = false; // reset
  6181. }
  6182. }
  6183. });
  6184. // Static items. As the axis group is cleared on subsequent calls
  6185. // to render, these items are added outside the group.
  6186. // axis line
  6187. if (lineWidth) {
  6188. lineLeft = axisLeft + (opposite ? axisWidth : 0) + offset;
  6189. lineTop = chartHeight - axisBottom - (opposite ? axisHeight : 0) + offset;
  6190. linePath = renderer.crispLine([
  6191. M,
  6192. horiz ?
  6193. axisLeft :
  6194. lineLeft,
  6195. horiz ?
  6196. lineTop :
  6197. axisTop,
  6198. L,
  6199. horiz ?
  6200. chartWidth - axisRight :
  6201. lineLeft,
  6202. horiz ?
  6203. lineTop :
  6204. chartHeight - axisBottom
  6205. ], lineWidth);
  6206. if (!axisLine) {
  6207. axisLine = renderer.path(linePath)
  6208. .attr({
  6209. stroke: options.lineColor,
  6210. 'stroke-width': lineWidth,
  6211. zIndex: 7
  6212. })
  6213. .add();
  6214. } else {
  6215. axisLine.animate({ d: linePath });
  6216. }
  6217. // show or hide the line depending on options.showEmpty
  6218. axisLine[showAxis ? 'show' : 'hide']();
  6219. }
  6220. if (axisTitle && showAxis) {
  6221. // compute anchor points for each of the title align options
  6222. var margin = horiz ? axisLeft : axisTop,
  6223. fontSize = pInt(axisTitleOptions.style.fontSize || 12),
  6224. // the position in the length direction of the axis
  6225. alongAxis = {
  6226. low: margin + (horiz ? 0 : axisLength),
  6227. middle: margin + axisLength / 2,
  6228. high: margin + (horiz ? axisLength : 0)
  6229. }[axisTitleOptions.align],
  6230. // the position in the perpendicular direction of the axis
  6231. offAxis = (horiz ? axisTop + axisHeight : axisLeft) +
  6232. (horiz ? 1 : -1) * // horizontal axis reverses the margin
  6233. (opposite ? -1 : 1) * // so does opposite axes
  6234. axisTitleMargin +
  6235. (side === 2 ? fontSize : 0);
  6236. axisTitle[axisTitle.isNew ? 'attr' : 'animate']({
  6237. x: horiz ?
  6238. alongAxis :
  6239. offAxis + (opposite ? axisWidth : 0) + offset +
  6240. (axisTitleOptions.x || 0), // x
  6241. y: horiz ?
  6242. offAxis - (opposite ? axisHeight : 0) + offset :
  6243. alongAxis + (axisTitleOptions.y || 0) // y
  6244. });
  6245. axisTitle.isNew = false;
  6246. }
  6247. // Stacked totals:
  6248. if (stackLabelOptions && stackLabelOptions.enabled) {
  6249. var stackKey, oneStack, stackCategory,
  6250. stackTotalGroup = axis.stackTotalGroup;
  6251. // Create a separate group for the stack total labels
  6252. if (!stackTotalGroup) {
  6253. axis.stackTotalGroup = stackTotalGroup =
  6254. renderer.g('stack-labels')
  6255. .attr({
  6256. visibility: VISIBLE,
  6257. zIndex: 6
  6258. })
  6259. .add();
  6260. }
  6261. // plotLeft/Top will change when y axis gets wider so we need to translate the
  6262. // stackTotalGroup at every render call. See bug #506 and #516
  6263. stackTotalGroup.translate(plotLeft, plotTop);
  6264. // Render each stack total
  6265. for (stackKey in stacks) {
  6266. oneStack = stacks[stackKey];
  6267. for (stackCategory in oneStack) {
  6268. oneStack[stackCategory].render(stackTotalGroup);
  6269. }
  6270. }
  6271. }
  6272. // End stacked totals
  6273. axis.isDirty = false;
  6274. }
  6275. /**
  6276. * Remove a plot band or plot line from the chart by id
  6277. * @param {Object} id
  6278. */
  6279. function removePlotBandOrLine(id) {
  6280. var i = plotLinesAndBands.length;
  6281. while (i--) {
  6282. if (plotLinesAndBands[i].id === id) {
  6283. plotLinesAndBands[i].destroy();
  6284. }
  6285. }
  6286. }
  6287. /**
  6288. * Update the axis title by options
  6289. */
  6290. function setTitle(newTitleOptions, redraw) {
  6291. options.title = merge(options.title, newTitleOptions);
  6292. axisTitle = axisTitle.destroy();
  6293. axis.isDirty = true;
  6294. if (pick(redraw, true)) {
  6295. chart.redraw();
  6296. }
  6297. }
  6298. /**
  6299. * Redraw the axis to reflect changes in the data or axis extremes
  6300. */
  6301. function redraw() {
  6302. // hide tooltip and hover states
  6303. if (tracker.resetTracker) {
  6304. tracker.resetTracker(true);
  6305. }
  6306. // render the axis
  6307. render();
  6308. // move plot lines and bands
  6309. each(plotLinesAndBands, function (plotLine) {
  6310. plotLine.render();
  6311. });
  6312. // mark associated series as dirty and ready for redraw
  6313. each(axis.series, function (series) {
  6314. series.isDirty = true;
  6315. });
  6316. }
  6317. /**
  6318. * Set new axis categories and optionally redraw
  6319. * @param {Array} newCategories
  6320. * @param {Boolean} doRedraw
  6321. */
  6322. function setCategories(newCategories, doRedraw) {
  6323. // set the categories
  6324. axis.categories = userOptions.categories = categories = newCategories;
  6325. // force reindexing tooltips
  6326. each(axis.series, function (series) {
  6327. series.translate();
  6328. series.setTooltipPoints(true);
  6329. });
  6330. // optionally redraw
  6331. axis.isDirty = true;
  6332. if (pick(doRedraw, true)) {
  6333. chart.redraw();
  6334. }
  6335. }
  6336. /**
  6337. * Destroys an Axis instance.
  6338. */
  6339. function destroy() {
  6340. var stackKey;
  6341. // Remove the events
  6342. removeEvent(axis);
  6343. // Destroy each stack total
  6344. for (stackKey in stacks) {
  6345. destroyObjectProperties(stacks[stackKey]);
  6346. stacks[stackKey] = null;
  6347. }
  6348. // Destroy stack total group
  6349. if (axis.stackTotalGroup) {
  6350. axis.stackTotalGroup = axis.stackTotalGroup.destroy();
  6351. }
  6352. // Destroy collections
  6353. each([ticks, minorTicks, alternateBands, plotLinesAndBands], function (coll) {
  6354. destroyObjectProperties(coll);
  6355. });
  6356. // Destroy local variables
  6357. each([axisLine, axisGroup, gridGroup, axisTitle], function (obj) {
  6358. if (obj) {
  6359. obj.destroy();
  6360. }
  6361. });
  6362. axisLine = axisGroup = gridGroup = axisTitle = null;
  6363. }
  6364. // Run Axis
  6365. // Register
  6366. axes.push(axis);
  6367. chart[isXAxis ? 'xAxis' : 'yAxis'].push(axis);
  6368. // inverted charts have reversed xAxes as default
  6369. if (inverted && isXAxis && reversed === UNDEFINED) {
  6370. reversed = true;
  6371. }
  6372. // expose some variables
  6373. extend(axis, {
  6374. addPlotBand: addPlotBandOrLine,
  6375. addPlotLine: addPlotBandOrLine,
  6376. adjustTickAmount: adjustTickAmount,
  6377. categories: categories,
  6378. getExtremes: getExtremes,
  6379. getPlotLinePath: getPlotLinePath,
  6380. getThreshold: getThreshold,
  6381. isXAxis: isXAxis,
  6382. options: options,
  6383. plotLinesAndBands: plotLinesAndBands,
  6384. getOffset: getOffset,
  6385. render: render,
  6386. setAxisSize: setAxisSize,
  6387. setAxisTranslation: setAxisTranslation,
  6388. setCategories: setCategories,
  6389. setExtremes: setExtremes,
  6390. setScale: setScale,
  6391. setTickPositions: setTickPositions,
  6392. translate: translate,
  6393. redraw: redraw,
  6394. removePlotBand: removePlotBandOrLine,
  6395. removePlotLine: removePlotBandOrLine,
  6396. reversed: reversed,
  6397. setTitle: setTitle,
  6398. series: [], // populated by Series
  6399. stacks: stacks,
  6400. destroy: destroy
  6401. });
  6402. // register event listeners
  6403. for (eventType in events) {
  6404. addEvent(axis, eventType, events[eventType]);
  6405. }
  6406. // extend logarithmic axis
  6407. if (isLog) {
  6408. axis.val2lin = log2lin;
  6409. axis.lin2val = lin2log;
  6410. }
  6411. } // end Axis
  6412. /**
  6413. * The tooltip object
  6414. * @param {Object} options Tooltip options
  6415. */
  6416. function Tooltip(options) {
  6417. var currentSeries,
  6418. borderWidth = options.borderWidth,
  6419. crosshairsOptions = options.crosshairs,
  6420. crosshairs = [],
  6421. style = options.style,
  6422. shared = options.shared,
  6423. padding = pInt(style.padding),
  6424. tooltipIsHidden = true,
  6425. currentX = 0,
  6426. currentY = 0;
  6427. // remove padding CSS and apply padding on box instead
  6428. style.padding = 0;
  6429. // create the label
  6430. var label = renderer.label('', 0, 0, null, null, null, options.useHTML, null, 'tooltip')
  6431. .attr({
  6432. padding: padding,
  6433. fill: options.backgroundColor,
  6434. 'stroke-width': borderWidth,
  6435. r: options.borderRadius,
  6436. zIndex: 8
  6437. })
  6438. .css(style)
  6439. .hide()
  6440. .add();
  6441. // When using canVG the shadow shows up as a gray circle
  6442. // even if the tooltip is hidden.
  6443. if (!useCanVG) {
  6444. label.shadow(options.shadow);
  6445. }
  6446. /**
  6447. * Destroy the tooltip and its elements.
  6448. */
  6449. function destroy() {
  6450. each(crosshairs, function (crosshair) {
  6451. if (crosshair) {
  6452. crosshair.destroy();
  6453. }
  6454. });
  6455. // Destroy and clear local variables
  6456. if (label) {
  6457. label = label.destroy();
  6458. }
  6459. }
  6460. /**
  6461. * In case no user defined formatter is given, this will be used
  6462. */
  6463. function defaultFormatter() {
  6464. var pThis = this,
  6465. items = pThis.points || splat(pThis),
  6466. series = items[0].series,
  6467. s;
  6468. // build the header
  6469. s = [series.tooltipHeaderFormatter(items[0].key)];
  6470. // build the values
  6471. each(items, function (item) {
  6472. series = item.series;
  6473. s.push((series.tooltipFormatter && series.tooltipFormatter(item)) ||
  6474. item.point.tooltipFormatter(series.tooltipOptions.pointFormat));
  6475. });
  6476. // footer
  6477. s.push(options.footerFormat || '');
  6478. return s.join('');
  6479. }
  6480. /**
  6481. * Provide a soft movement for the tooltip
  6482. *
  6483. * @param {Number} finalX
  6484. * @param {Number} finalY
  6485. */
  6486. function move(finalX, finalY) {
  6487. // get intermediate values for animation
  6488. currentX = tooltipIsHidden ? finalX : (2 * currentX + finalX) / 3;
  6489. currentY = tooltipIsHidden ? finalY : (currentY + finalY) / 2;
  6490. // move to the intermediate value
  6491. label.attr({ x: currentX, y: currentY });
  6492. // run on next tick of the mouse tracker
  6493. if (mathAbs(finalX - currentX) > 1 || mathAbs(finalY - currentY) > 1) {
  6494. tooltipTick = function () {
  6495. move(finalX, finalY);
  6496. };
  6497. } else {
  6498. tooltipTick = null;
  6499. }
  6500. }
  6501. /**
  6502. * Hide the tooltip
  6503. */
  6504. function hide() {
  6505. if (!tooltipIsHidden) {
  6506. var hoverPoints = chart.hoverPoints;
  6507. label.hide();
  6508. // hide previous hoverPoints and set new
  6509. if (hoverPoints) {
  6510. each(hoverPoints, function (point) {
  6511. point.setState();
  6512. });
  6513. }
  6514. chart.hoverPoints = null;
  6515. tooltipIsHidden = true;
  6516. }
  6517. }
  6518. /**
  6519. * Hide the crosshairs
  6520. */
  6521. function hideCrosshairs() {
  6522. each(crosshairs, function (crosshair) {
  6523. if (crosshair) {
  6524. crosshair.hide();
  6525. }
  6526. });
  6527. }
  6528. /**
  6529. * Refresh the tooltip's text and position.
  6530. * @param {Object} point
  6531. *
  6532. */
  6533. function refresh(point) {
  6534. var x,
  6535. y,
  6536. show,
  6537. plotX,
  6538. plotY,
  6539. textConfig = {},
  6540. text,
  6541. pointConfig = [],
  6542. tooltipPos = point.tooltipPos,
  6543. formatter = options.formatter || defaultFormatter,
  6544. hoverPoints = chart.hoverPoints,
  6545. placedTooltipPoint,
  6546. borderColor;
  6547. // shared tooltip, array is sent over
  6548. if (shared && !(point.series && point.series.noSharedTooltip)) {
  6549. plotY = 0;
  6550. // hide previous hoverPoints and set new
  6551. if (hoverPoints) {
  6552. each(hoverPoints, function (point) {
  6553. point.setState();
  6554. });
  6555. }
  6556. chart.hoverPoints = point;
  6557. each(point, function (item) {
  6558. item.setState(HOVER_STATE);
  6559. plotY += item.plotY; // for average
  6560. pointConfig.push(item.getLabelConfig());
  6561. });
  6562. plotX = point[0].plotX;
  6563. plotY = mathRound(plotY) / point.length; // mathRound because Opera 10 has problems here
  6564. textConfig = {
  6565. x: point[0].category
  6566. };
  6567. textConfig.points = pointConfig;
  6568. point = point[0];
  6569. // single point tooltip
  6570. } else {
  6571. textConfig = point.getLabelConfig();
  6572. }
  6573. text = formatter.call(textConfig);
  6574. // register the current series
  6575. currentSeries = point.series;
  6576. // get the reference point coordinates (pie charts use tooltipPos)
  6577. plotX = pick(plotX, point.plotX);
  6578. plotY = pick(plotY, point.plotY);
  6579. x = mathRound(tooltipPos ? tooltipPos[0] : (inverted ? plotWidth - plotY : plotX));
  6580. y = mathRound(tooltipPos ? tooltipPos[1] : (inverted ? plotHeight - plotX : plotY));
  6581. // For line type series, hide tooltip if the point falls outside the plot
  6582. show = shared || !currentSeries.isCartesian || currentSeries.tooltipOutsidePlot || isInsidePlot(x, y);
  6583. // update the inner HTML
  6584. if (text === false || !show) {
  6585. hide();
  6586. } else {
  6587. // show it
  6588. if (tooltipIsHidden) {
  6589. label.show();
  6590. tooltipIsHidden = false;
  6591. }
  6592. // update text
  6593. label.attr({
  6594. text: text
  6595. });
  6596. // set the stroke color of the box
  6597. borderColor = options.borderColor || point.color || currentSeries.color || '#606060';
  6598. label.attr({
  6599. stroke: borderColor
  6600. });
  6601. placedTooltipPoint = placeBox(
  6602. label.width,
  6603. label.height,
  6604. plotLeft,
  6605. plotTop,
  6606. plotWidth,
  6607. plotHeight,
  6608. {x: x, y: y},
  6609. pick(options.distance, 12),
  6610. inverted
  6611. );
  6612. // do the move
  6613. move(mathRound(placedTooltipPoint.x), mathRound(placedTooltipPoint.y));
  6614. }
  6615. // crosshairs
  6616. if (crosshairsOptions) {
  6617. crosshairsOptions = splat(crosshairsOptions); // [x, y]
  6618. var path,
  6619. i = crosshairsOptions.length,
  6620. attribs,
  6621. axis;
  6622. while (i--) {
  6623. axis = point.series[i ? 'yAxis' : 'xAxis'];
  6624. if (crosshairsOptions[i] && axis) {
  6625. path = axis.getPlotLinePath(
  6626. i ? pick(point.stackY, point.y) : point.x, // #814
  6627. 1
  6628. );
  6629. if (crosshairs[i]) {
  6630. crosshairs[i].attr({ d: path, visibility: VISIBLE });
  6631. } else {
  6632. attribs = {
  6633. 'stroke-width': crosshairsOptions[i].width || 1,
  6634. stroke: crosshairsOptions[i].color || '#C0C0C0',
  6635. zIndex: crosshairsOptions[i].zIndex || 2
  6636. };
  6637. if (crosshairsOptions[i].dashStyle) {
  6638. attribs.dashstyle = crosshairsOptions[i].dashStyle;
  6639. }
  6640. crosshairs[i] = renderer.path(path)
  6641. .attr(attribs)
  6642. .add();
  6643. }
  6644. }
  6645. }
  6646. }
  6647. fireEvent(chart, 'tooltipRefresh', {
  6648. text: text,
  6649. x: x + plotLeft,
  6650. y: y + plotTop,
  6651. borderColor: borderColor
  6652. });
  6653. }
  6654. // public members
  6655. return {
  6656. shared: shared,
  6657. refresh: refresh,
  6658. hide: hide,
  6659. hideCrosshairs: hideCrosshairs,
  6660. destroy: destroy
  6661. };
  6662. }
  6663. /**
  6664. * The mouse tracker object
  6665. * @param {Object} options
  6666. */
  6667. function MouseTracker(options) {
  6668. var mouseDownX,
  6669. mouseDownY,
  6670. hasDragged,
  6671. selectionMarker,
  6672. zoomType = useCanVG ? '' : optionsChart.zoomType,
  6673. zoomX = /x/.test(zoomType),
  6674. zoomY = /y/.test(zoomType),
  6675. zoomHor = (zoomX && !inverted) || (zoomY && inverted),
  6676. zoomVert = (zoomY && !inverted) || (zoomX && inverted);
  6677. /**
  6678. * Add crossbrowser support for chartX and chartY
  6679. * @param {Object} e The event object in standard browsers
  6680. */
  6681. function normalizeMouseEvent(e) {
  6682. var ePos,
  6683. chartX,
  6684. chartY;
  6685. // common IE normalizing
  6686. e = e || win.event;
  6687. if (!e.target) {
  6688. e.target = e.srcElement;
  6689. }
  6690. // jQuery only copies over some properties. IE needs e.x and iOS needs touches.
  6691. if (e.originalEvent) {
  6692. e = e.originalEvent;
  6693. }
  6694. // The same for MooTools. It renames e.pageX to e.page.x. #445.
  6695. if (e.event) {
  6696. e = e.event;
  6697. }
  6698. // iOS
  6699. ePos = e.touches ? e.touches.item(0) : e;
  6700. // get mouse position
  6701. chartPosition = offset(container);
  6702. // chartX and chartY
  6703. if (ePos.pageX === UNDEFINED) { // IE < 9. #886.
  6704. chartX = e.x;
  6705. chartY = e.y;
  6706. } else {
  6707. chartX = ePos.pageX - chartPosition.left;
  6708. chartY = ePos.pageY - chartPosition.top;
  6709. }
  6710. return extend(e, {
  6711. chartX: mathRound(chartX),
  6712. chartY: mathRound(chartY)
  6713. });
  6714. }
  6715. /**
  6716. * Get the click position in terms of axis values.
  6717. *
  6718. * @param {Object} e A mouse event
  6719. */
  6720. function getMouseCoordinates(e) {
  6721. var coordinates = {
  6722. xAxis: [],
  6723. yAxis: []
  6724. };
  6725. each(axes, function (axis) {
  6726. var translate = axis.translate,
  6727. isXAxis = axis.isXAxis,
  6728. isHorizontal = inverted ? !isXAxis : isXAxis;
  6729. coordinates[isXAxis ? 'xAxis' : 'yAxis'].push({
  6730. axis: axis,
  6731. value: translate(
  6732. isHorizontal ?
  6733. e.chartX - plotLeft :
  6734. plotHeight - e.chartY + plotTop,
  6735. true
  6736. )
  6737. });
  6738. });
  6739. return coordinates;
  6740. }
  6741. /**
  6742. * With line type charts with a single tracker, get the point closest to the mouse
  6743. */
  6744. function onmousemove(e) {
  6745. var point,
  6746. points,
  6747. hoverPoint = chart.hoverPoint,
  6748. hoverSeries = chart.hoverSeries,
  6749. i,
  6750. j,
  6751. distance = chartWidth,
  6752. // the index in the tooltipPoints array, corresponding to pixel position in plot area
  6753. index = inverted ? plotHeight + plotTop - e.chartY : e.chartX - plotLeft;
  6754. // shared tooltip
  6755. if (tooltip && options.shared && !(hoverSeries && hoverSeries.noSharedTooltip)) {
  6756. points = [];
  6757. // loop over all series and find the ones with points closest to the mouse
  6758. i = series.length;
  6759. for (j = 0; j < i; j++) {
  6760. if (series[j].visible &&
  6761. series[j].options.enableMouseTracking !== false &&
  6762. !series[j].noSharedTooltip && series[j].tooltipPoints.length) {
  6763. point = series[j].tooltipPoints[index];
  6764. point._dist = mathAbs(index - point.plotX);
  6765. distance = mathMin(distance, point._dist);
  6766. points.push(point);
  6767. }
  6768. }
  6769. // remove furthest points
  6770. i = points.length;
  6771. while (i--) {
  6772. if (points[i]._dist > distance) {
  6773. points.splice(i, 1);
  6774. }
  6775. }
  6776. // refresh the tooltip if necessary
  6777. if (points.length && (points[0].plotX !== hoverX)) {
  6778. tooltip.refresh(points);
  6779. hoverX = points[0].plotX;
  6780. }
  6781. }
  6782. // separate tooltip and general mouse events
  6783. if (hoverSeries && hoverSeries.tracker) { // only use for line-type series with common tracker
  6784. // get the point
  6785. point = hoverSeries.tooltipPoints[index];
  6786. // a new point is hovered, refresh the tooltip
  6787. if (point && point !== hoverPoint) {
  6788. // trigger the events
  6789. point.onMouseOver();
  6790. }
  6791. }
  6792. }
  6793. /**
  6794. * Reset the tracking by hiding the tooltip, the hover series state and the hover point
  6795. */
  6796. function resetTracker(allowMove) {
  6797. var hoverSeries = chart.hoverSeries,
  6798. hoverPoint = chart.hoverPoint,
  6799. tooltipPoints = chart.hoverPoints || hoverPoint;
  6800. // Just move the tooltip, #349
  6801. if (allowMove && tooltip && tooltipPoints) {
  6802. tooltip.refresh(tooltipPoints);
  6803. // Full reset
  6804. } else {
  6805. if (hoverPoint) {
  6806. hoverPoint.onMouseOut();
  6807. }
  6808. if (hoverSeries) {
  6809. hoverSeries.onMouseOut();
  6810. }
  6811. if (tooltip) {
  6812. tooltip.hide();
  6813. tooltip.hideCrosshairs();
  6814. }
  6815. hoverX = null;
  6816. }
  6817. }
  6818. /**
  6819. * Mouse up or outside the plot area
  6820. */
  6821. function drop() {
  6822. if (selectionMarker) {
  6823. var selectionData = {
  6824. xAxis: [],
  6825. yAxis: []
  6826. },
  6827. selectionBox = selectionMarker.getBBox(),
  6828. selectionLeft = selectionBox.x - plotLeft,
  6829. selectionTop = selectionBox.y - plotTop,
  6830. runZoom;
  6831. // a selection has been made
  6832. if (hasDragged) {
  6833. // record each axis' min and max
  6834. each(axes, function (axis) {
  6835. if (axis.options.zoomEnabled !== false) {
  6836. var translate = axis.translate,
  6837. isXAxis = axis.isXAxis,
  6838. isHorizontal = inverted ? !isXAxis : isXAxis,
  6839. selectionMin = translate(
  6840. isHorizontal ?
  6841. selectionLeft :
  6842. plotHeight - selectionTop - selectionBox.height,
  6843. true,
  6844. 0,
  6845. 0,
  6846. 1
  6847. ),
  6848. selectionMax = translate(
  6849. isHorizontal ?
  6850. selectionLeft + selectionBox.width :
  6851. plotHeight - selectionTop,
  6852. true,
  6853. 0,
  6854. 0,
  6855. 1
  6856. );
  6857. if (!isNaN(selectionMin) && !isNaN(selectionMax)) { // #859
  6858. selectionData[isXAxis ? 'xAxis' : 'yAxis'].push({
  6859. axis: axis,
  6860. min: mathMin(selectionMin, selectionMax), // for reversed axes,
  6861. max: mathMax(selectionMin, selectionMax)
  6862. });
  6863. runZoom = true;
  6864. }
  6865. }
  6866. });
  6867. if (runZoom) {
  6868. fireEvent(chart, 'selection', selectionData, zoom);
  6869. }
  6870. }
  6871. selectionMarker = selectionMarker.destroy();
  6872. }
  6873. if (chart) { // it may be destroyed on mouse up - #877
  6874. css(container, { cursor: 'auto' });
  6875. chart.cancelClick = hasDragged; // #370
  6876. chart.mouseIsDown = mouseIsDown = hasDragged = false;
  6877. }
  6878. removeEvent(doc, hasTouch ? 'touchend' : 'mouseup', drop);
  6879. }
  6880. /**
  6881. * Special handler for mouse move that will hide the tooltip when the mouse leaves the plotarea.
  6882. */
  6883. function hideTooltipOnMouseMove(e) {
  6884. // Get e.pageX and e.pageY back in MooTools
  6885. washMouseEvent(e);
  6886. // If we're outside, hide the tooltip
  6887. if (chartPosition &&
  6888. !isInsidePlot(e.pageX - chartPosition.left - plotLeft,
  6889. e.pageY - chartPosition.top - plotTop)) {
  6890. resetTracker();
  6891. }
  6892. }
  6893. /**
  6894. * When mouse leaves the container, hide the tooltip.
  6895. */
  6896. function hideTooltipOnMouseLeave() {
  6897. resetTracker();
  6898. chartPosition = null; // also reset the chart position, used in #149 fix
  6899. }
  6900. /**
  6901. * Set the JS events on the container element
  6902. */
  6903. function setDOMEvents() {
  6904. var lastWasOutsidePlot = true;
  6905. /*
  6906. * Record the starting position of a dragoperation
  6907. */
  6908. container.onmousedown = function (e) {
  6909. e = normalizeMouseEvent(e);
  6910. // issue #295, dragging not always working in Firefox
  6911. if (!hasTouch && e.preventDefault) {
  6912. e.preventDefault();
  6913. }
  6914. // record the start position
  6915. chart.mouseIsDown = mouseIsDown = true;
  6916. chart.cancelClick = false;
  6917. chart.mouseDownX = mouseDownX = e.chartX;
  6918. mouseDownY = e.chartY;
  6919. addEvent(doc, hasTouch ? 'touchend' : 'mouseup', drop);
  6920. };
  6921. // The mousemove, touchmove and touchstart event handler
  6922. var mouseMove = function (e) {
  6923. // let the system handle multitouch operations like two finger scroll
  6924. // and pinching
  6925. if (e && e.touches && e.touches.length > 1) {
  6926. return;
  6927. }
  6928. // normalize
  6929. e = normalizeMouseEvent(e);
  6930. if (!hasTouch) { // not for touch devices
  6931. e.returnValue = false;
  6932. }
  6933. var chartX = e.chartX,
  6934. chartY = e.chartY,
  6935. isOutsidePlot = !isInsidePlot(chartX - plotLeft, chartY - plotTop);
  6936. // on touch devices, only trigger click if a handler is defined
  6937. if (hasTouch && e.type === 'touchstart') {
  6938. if (attr(e.target, 'isTracker')) {
  6939. if (!chart.runTrackerClick) {
  6940. e.preventDefault();
  6941. }
  6942. } else if (!runChartClick && !isOutsidePlot) {
  6943. e.preventDefault();
  6944. }
  6945. }
  6946. // cancel on mouse outside
  6947. if (isOutsidePlot) {
  6948. /*if (!lastWasOutsidePlot) {
  6949. // reset the tracker
  6950. resetTracker();
  6951. }*/
  6952. // drop the selection if any and reset mouseIsDown and hasDragged
  6953. //drop();
  6954. if (chartX < plotLeft) {
  6955. chartX = plotLeft;
  6956. } else if (chartX > plotLeft + plotWidth) {
  6957. chartX = plotLeft + plotWidth;
  6958. }
  6959. if (chartY < plotTop) {
  6960. chartY = plotTop;
  6961. } else if (chartY > plotTop + plotHeight) {
  6962. chartY = plotTop + plotHeight;
  6963. }
  6964. }
  6965. if (mouseIsDown && e.type !== 'touchstart') { // make selection
  6966. // determine if the mouse has moved more than 10px
  6967. hasDragged = Math.sqrt(
  6968. Math.pow(mouseDownX - chartX, 2) +
  6969. Math.pow(mouseDownY - chartY, 2)
  6970. );
  6971. if (hasDragged > 10) {
  6972. var clickedInside = isInsidePlot(mouseDownX - plotLeft, mouseDownY - plotTop);
  6973. // make a selection
  6974. if (hasCartesianSeries && (zoomX || zoomY) && clickedInside) {
  6975. if (!selectionMarker) {
  6976. selectionMarker = renderer.rect(
  6977. plotLeft,
  6978. plotTop,
  6979. zoomHor ? 1 : plotWidth,
  6980. zoomVert ? 1 : plotHeight,
  6981. 0
  6982. )
  6983. .attr({
  6984. fill: optionsChart.selectionMarkerFill || 'rgba(69,114,167,0.25)',
  6985. zIndex: 7
  6986. })
  6987. .add();
  6988. }
  6989. }
  6990. // adjust the width of the selection marker
  6991. if (selectionMarker && zoomHor) {
  6992. var xSize = chartX - mouseDownX;
  6993. selectionMarker.attr({
  6994. width: mathAbs(xSize),
  6995. x: (xSize > 0 ? 0 : xSize) + mouseDownX
  6996. });
  6997. }
  6998. // adjust the height of the selection marker
  6999. if (selectionMarker && zoomVert) {
  7000. var ySize = chartY - mouseDownY;
  7001. selectionMarker.attr({
  7002. height: mathAbs(ySize),
  7003. y: (ySize > 0 ? 0 : ySize) + mouseDownY
  7004. });
  7005. }
  7006. // panning
  7007. if (clickedInside && !selectionMarker && optionsChart.panning) {
  7008. chart.pan(chartX);
  7009. }
  7010. }
  7011. } else if (!isOutsidePlot) {
  7012. // show the tooltip
  7013. onmousemove(e);
  7014. }
  7015. lastWasOutsidePlot = isOutsidePlot;
  7016. // when outside plot, allow touch-drag by returning true
  7017. return isOutsidePlot || !hasCartesianSeries;
  7018. };
  7019. /*
  7020. * When the mouse enters the container, run mouseMove
  7021. */
  7022. container.onmousemove = mouseMove;
  7023. /*
  7024. * When the mouse leaves the container, hide the tracking (tooltip).
  7025. */
  7026. addEvent(container, 'mouseleave', hideTooltipOnMouseLeave);
  7027. // issue #149 workaround
  7028. // The mouseleave event above does not always fire. Whenever the mouse is moving
  7029. // outside the plotarea, hide the tooltip
  7030. addEvent(doc, 'mousemove', hideTooltipOnMouseMove);
  7031. container.ontouchstart = function (e) {
  7032. // For touch devices, use touchmove to zoom
  7033. if (zoomX || zoomY) {
  7034. container.onmousedown(e);
  7035. }
  7036. // Show tooltip and prevent the lower mouse pseudo event
  7037. mouseMove(e);
  7038. };
  7039. /*
  7040. * Allow dragging the finger over the chart to read the values on touch
  7041. * devices
  7042. */
  7043. container.ontouchmove = mouseMove;
  7044. /*
  7045. * Allow dragging the finger over the chart to read the values on touch
  7046. * devices
  7047. */
  7048. container.ontouchend = function () {
  7049. if (hasDragged) {
  7050. resetTracker();
  7051. }
  7052. };
  7053. // MooTools 1.2.3 doesn't fire this in IE when using addEvent
  7054. container.onclick = function (e) {
  7055. var hoverPoint = chart.hoverPoint;
  7056. e = normalizeMouseEvent(e);
  7057. e.cancelBubble = true; // IE specific
  7058. if (!chart.cancelClick) {
  7059. // Detect clicks on trackers or tracker groups, #783
  7060. if (hoverPoint && (attr(e.target, 'isTracker') || attr(e.target.parentNode, 'isTracker'))) {
  7061. var plotX = hoverPoint.plotX,
  7062. plotY = hoverPoint.plotY;
  7063. // add page position info
  7064. extend(hoverPoint, {
  7065. pageX: chartPosition.left + plotLeft +
  7066. (inverted ? plotWidth - plotY : plotX),
  7067. pageY: chartPosition.top + plotTop +
  7068. (inverted ? plotHeight - plotX : plotY)
  7069. });
  7070. // the series click event
  7071. fireEvent(hoverPoint.series, 'click', extend(e, {
  7072. point: hoverPoint
  7073. }));
  7074. // the point click event
  7075. hoverPoint.firePointEvent('click', e);
  7076. } else {
  7077. extend(e, getMouseCoordinates(e));
  7078. // fire a click event in the chart
  7079. if (isInsidePlot(e.chartX - plotLeft, e.chartY - plotTop)) {
  7080. fireEvent(chart, 'click', e);
  7081. }
  7082. }
  7083. }
  7084. };
  7085. }
  7086. /**
  7087. * Destroys the MouseTracker object and disconnects DOM events.
  7088. */
  7089. function destroy() {
  7090. // Destroy the tracker group element
  7091. if (chart.trackerGroup) {
  7092. chart.trackerGroup = trackerGroup = chart.trackerGroup.destroy();
  7093. }
  7094. removeEvent(container, 'mouseleave', hideTooltipOnMouseLeave);
  7095. removeEvent(doc, 'mousemove', hideTooltipOnMouseMove);
  7096. container.onclick = container.onmousedown = container.onmousemove = container.ontouchstart = container.ontouchend = container.ontouchmove = null;
  7097. }
  7098. // Run MouseTracker
  7099. if (!trackerGroup) {
  7100. chart.trackerGroup = trackerGroup = renderer.g('tracker')
  7101. .attr({ zIndex: 9 })
  7102. .add();
  7103. }
  7104. if (options.enabled) {
  7105. chart.tooltip = tooltip = Tooltip(options);
  7106. // set the fixed interval ticking for the smooth tooltip
  7107. tooltipInterval = setInterval(function () {
  7108. if (tooltipTick) {
  7109. tooltipTick();
  7110. }
  7111. }, 32);
  7112. }
  7113. setDOMEvents();
  7114. // expose properties
  7115. extend(this, {
  7116. zoomX: zoomX,
  7117. zoomY: zoomY,
  7118. resetTracker: resetTracker,
  7119. normalizeMouseEvent: normalizeMouseEvent,
  7120. destroy: destroy
  7121. });
  7122. }
  7123. /**
  7124. * The overview of the chart's series
  7125. */
  7126. var Legend = function () {
  7127. var options = chart.options.legend;
  7128. if (!options.enabled) {
  7129. return;
  7130. }
  7131. var horizontal = options.layout === 'horizontal',
  7132. symbolWidth = options.symbolWidth,
  7133. symbolPadding = options.symbolPadding,
  7134. allItems,
  7135. style = options.style,
  7136. itemStyle = options.itemStyle,
  7137. itemHoverStyle = options.itemHoverStyle,
  7138. itemHiddenStyle = merge(itemStyle, options.itemHiddenStyle),
  7139. padding = options.padding || pInt(style.padding),
  7140. ltr = !options.rtl,
  7141. itemMarginTop = options.itemMarginTop || 0,
  7142. itemMarginBottom = options.itemMarginBottom || 0,
  7143. y = 18,
  7144. maxItemWidth = 0,
  7145. initialItemX = 4 + padding + symbolWidth + symbolPadding,
  7146. initialItemY = padding + itemMarginTop + y - 5, // 5 is the number of pixels above the text
  7147. itemX,
  7148. itemY,
  7149. lastItemY,
  7150. itemHeight = 0,
  7151. box,
  7152. legendBorderWidth = options.borderWidth,
  7153. legendBackgroundColor = options.backgroundColor,
  7154. legendGroup,
  7155. offsetWidth,
  7156. widthOption = options.width,
  7157. series = chart.series,
  7158. reversedLegend = options.reversed;
  7159. /**
  7160. * Set the colors for the legend item
  7161. * @param {Object} item A Series or Point instance
  7162. * @param {Object} visible Dimmed or colored
  7163. */
  7164. function colorizeItem(item, visible) {
  7165. var legendItem = item.legendItem,
  7166. legendLine = item.legendLine,
  7167. legendSymbol = item.legendSymbol,
  7168. hiddenColor = itemHiddenStyle.color,
  7169. textColor = visible ? options.itemStyle.color : hiddenColor,
  7170. symbolColor = visible ? item.color : hiddenColor;
  7171. if (legendItem) {
  7172. legendItem.css({ fill: textColor });
  7173. }
  7174. if (legendLine) {
  7175. legendLine.attr({ stroke: symbolColor });
  7176. }
  7177. if (legendSymbol) {
  7178. legendSymbol.attr({
  7179. stroke: symbolColor,
  7180. fill: symbolColor
  7181. });
  7182. }
  7183. }
  7184. /**
  7185. * Position the legend item
  7186. * @param {Object} item A Series or Point instance
  7187. * @param {Object} visible Dimmed or colored
  7188. */
  7189. function positionItem(item) {
  7190. var legendItem = item.legendItem,
  7191. legendLine = item.legendLine,
  7192. legendItemPos = item._legendItemPos,
  7193. itemX = legendItemPos[0],
  7194. itemY = legendItemPos[1],
  7195. legendSymbol = item.legendSymbol,
  7196. symbolX,
  7197. checkbox = item.checkbox;
  7198. if (legendItem) {
  7199. legendItem.attr({
  7200. x: ltr ? itemX : legendWidth - itemX,
  7201. y: itemY
  7202. });
  7203. }
  7204. if (legendLine) {
  7205. legendLine.translate(
  7206. ltr ? itemX : legendWidth - itemX,
  7207. itemY - 4
  7208. );
  7209. }
  7210. if (legendSymbol) {
  7211. symbolX = itemX + legendSymbol.xOff;
  7212. legendSymbol.attr({
  7213. x: ltr ? symbolX : legendWidth - symbolX,
  7214. y: itemY + legendSymbol.yOff
  7215. });
  7216. }
  7217. if (checkbox) {
  7218. checkbox.x = itemX;
  7219. checkbox.y = itemY;
  7220. }
  7221. }
  7222. /**
  7223. * Destroy a single legend item
  7224. * @param {Object} item The series or point
  7225. */
  7226. function destroyItem(item) {
  7227. var checkbox = item.checkbox;
  7228. // destroy SVG elements
  7229. each(['legendItem', 'legendLine', 'legendSymbol'], function (key) {
  7230. if (item[key]) {
  7231. item[key].destroy();
  7232. }
  7233. });
  7234. if (checkbox) {
  7235. discardElement(item.checkbox);
  7236. }
  7237. }
  7238. /**
  7239. * Destroys the legend.
  7240. */
  7241. function destroy() {
  7242. if (box) {
  7243. box = box.destroy();
  7244. }
  7245. if (legendGroup) {
  7246. legendGroup = legendGroup.destroy();
  7247. }
  7248. }
  7249. /**
  7250. * Position the checkboxes after the width is determined
  7251. */
  7252. function positionCheckboxes() {
  7253. each(allItems, function (item) {
  7254. var checkbox = item.checkbox,
  7255. alignAttr = legendGroup.alignAttr;
  7256. if (checkbox) {
  7257. css(checkbox, {
  7258. left: (alignAttr.translateX + item.legendItemWidth + checkbox.x - 40) + PX,
  7259. top: (alignAttr.translateY + checkbox.y - 11) + PX
  7260. });
  7261. }
  7262. });
  7263. }
  7264. /**
  7265. * Render a single specific legend item
  7266. * @param {Object} item A series or point
  7267. */
  7268. function renderItem(item) {
  7269. var bBox,
  7270. itemWidth,
  7271. legendSymbol,
  7272. symbolX,
  7273. symbolY,
  7274. simpleSymbol,
  7275. radius,
  7276. li = item.legendItem,
  7277. series = item.series || item,
  7278. itemOptions = series.options,
  7279. strokeWidth = (itemOptions && itemOptions.borderWidth) || 0;
  7280. if (!li) { // generate it once, later move it
  7281. // let these series types use a simple symbol
  7282. simpleSymbol = /^(bar|pie|area|column)$/.test(series.type);
  7283. // generate the list item text
  7284. item.legendItem = li = renderer.text(
  7285. options.labelFormatter.call(item),
  7286. 0,
  7287. 0,
  7288. options.useHTML
  7289. )
  7290. .css(item.visible ? itemStyle : itemHiddenStyle)
  7291. .on('mouseover', function () {
  7292. item.setState(HOVER_STATE);
  7293. li.css(itemHoverStyle);
  7294. })
  7295. .on('mouseout', function () {
  7296. li.css(item.visible ? itemStyle : itemHiddenStyle);
  7297. item.setState();
  7298. })
  7299. .on('click', function () {
  7300. var strLegendItemClick = 'legendItemClick',
  7301. fnLegendItemClick = function () {
  7302. item.setVisible();
  7303. };
  7304. // click the name or symbol
  7305. if (item.firePointEvent) { // point
  7306. item.firePointEvent(strLegendItemClick, null, fnLegendItemClick);
  7307. } else {
  7308. fireEvent(item, strLegendItemClick, null, fnLegendItemClick);
  7309. }
  7310. })
  7311. .attr({
  7312. align: ltr ? 'left' : 'right',
  7313. zIndex: 2
  7314. })
  7315. .add(legendGroup);
  7316. // draw the line
  7317. if (!simpleSymbol && itemOptions && itemOptions.lineWidth) {
  7318. var attrs = {
  7319. 'stroke-width': itemOptions.lineWidth,
  7320. zIndex: 2
  7321. };
  7322. if (itemOptions.dashStyle) {
  7323. attrs.dashstyle = itemOptions.dashStyle;
  7324. }
  7325. item.legendLine = renderer.path([
  7326. M,
  7327. (-symbolWidth - symbolPadding) * (ltr ? 1 : -1),
  7328. 0,
  7329. L,
  7330. (-symbolPadding) * (ltr ? 1 : -1),
  7331. 0
  7332. ])
  7333. .attr(attrs)
  7334. .add(legendGroup);
  7335. }
  7336. // draw a simple symbol
  7337. if (simpleSymbol) { // bar|pie|area|column
  7338. legendSymbol = renderer.rect(
  7339. (symbolX = -symbolWidth - symbolPadding),
  7340. (symbolY = -11),
  7341. symbolWidth,
  7342. 12,
  7343. 2
  7344. ).attr({
  7345. //'stroke-width': 0,
  7346. zIndex: 3
  7347. }).add(legendGroup);
  7348. if (!ltr) {
  7349. symbolX += symbolWidth;
  7350. }
  7351. } else if (itemOptions && itemOptions.marker && itemOptions.marker.enabled) { // draw the marker
  7352. radius = itemOptions.marker.radius;
  7353. legendSymbol = renderer.symbol(
  7354. item.symbol,
  7355. (symbolX = -symbolWidth / 2 - symbolPadding - radius),
  7356. (symbolY = -4 - radius),
  7357. 2 * radius,
  7358. 2 * radius
  7359. )
  7360. .attr(item.pointAttr[NORMAL_STATE])
  7361. .attr({ zIndex: 3 })
  7362. .add(legendGroup);
  7363. if (!ltr) {
  7364. symbolX += symbolWidth / 2;
  7365. }
  7366. }
  7367. if (legendSymbol) {
  7368. legendSymbol.xOff = symbolX + (strokeWidth % 2 / 2);
  7369. legendSymbol.yOff = symbolY + (strokeWidth % 2 / 2);
  7370. }
  7371. item.legendSymbol = legendSymbol;
  7372. // colorize the items
  7373. colorizeItem(item, item.visible);
  7374. // add the HTML checkbox on top
  7375. if (itemOptions && itemOptions.showCheckbox) {
  7376. item.checkbox = createElement('input', {
  7377. type: 'checkbox',
  7378. checked: item.selected,
  7379. defaultChecked: item.selected // required by IE7
  7380. }, options.itemCheckboxStyle, container);
  7381. addEvent(item.checkbox, 'click', function (event) {
  7382. var target = event.target;
  7383. fireEvent(item, 'checkboxClick', {
  7384. checked: target.checked
  7385. },
  7386. function () {
  7387. item.select();
  7388. }
  7389. );
  7390. });
  7391. }
  7392. }
  7393. // calculate the positions for the next line
  7394. bBox = li.getBBox();
  7395. itemWidth = item.legendItemWidth =
  7396. options.itemWidth || symbolWidth + symbolPadding + bBox.width + padding;
  7397. itemHeight = bBox.height;
  7398. // if the item exceeds the width, start a new line
  7399. if (horizontal && itemX - initialItemX + itemWidth >
  7400. (widthOption || (chartWidth - 2 * padding - initialItemX))) {
  7401. itemX = initialItemX;
  7402. itemY += itemMarginTop + itemHeight + itemMarginBottom;
  7403. }
  7404. // If the item exceeds the height, start a new column
  7405. if (!horizontal && itemY + options.y + itemHeight > chartHeight - spacingTop - spacingBottom) {
  7406. itemY = initialItemY;
  7407. itemX += maxItemWidth;
  7408. maxItemWidth = 0;
  7409. }
  7410. // Set the edge positions
  7411. maxItemWidth = mathMax(maxItemWidth, itemWidth);
  7412. lastItemY = mathMax(lastItemY, itemY + itemMarginBottom);
  7413. // cache the position of the newly generated or reordered items
  7414. item._legendItemPos = [itemX, itemY];
  7415. // advance
  7416. if (horizontal) {
  7417. itemX += itemWidth;
  7418. } else {
  7419. itemY += itemMarginTop + itemHeight + itemMarginBottom;
  7420. }
  7421. // the width of the widest item
  7422. offsetWidth = widthOption || mathMax(
  7423. (itemX - initialItemX) + (horizontal ? 0 : itemWidth),
  7424. offsetWidth
  7425. );
  7426. }
  7427. /**
  7428. * Render the legend. This method can be called both before and after
  7429. * chart.render. If called after, it will only rearrange items instead
  7430. * of creating new ones.
  7431. */
  7432. function renderLegend() {
  7433. itemX = initialItemX;
  7434. itemY = initialItemY;
  7435. offsetWidth = 0;
  7436. lastItemY = 0;
  7437. if (!legendGroup) {
  7438. legendGroup = renderer.g('legend')
  7439. // #414, #759. Trackers will be drawn above the legend, but we have
  7440. // to sacrifice that because tooltips need to be above the legend
  7441. // and trackers above tooltips
  7442. .attr({ zIndex: 7 })
  7443. .add();
  7444. }
  7445. // add each series or point
  7446. allItems = [];
  7447. each(series, function (serie) {
  7448. var seriesOptions = serie.options;
  7449. if (!seriesOptions.showInLegend) {
  7450. return;
  7451. }
  7452. // use points or series for the legend item depending on legendType
  7453. allItems = allItems.concat(
  7454. serie.legendItems ||
  7455. (seriesOptions.legendType === 'point' ?
  7456. serie.data :
  7457. serie)
  7458. );
  7459. });
  7460. // sort by legendIndex
  7461. stableSort(allItems, function (a, b) {
  7462. return (a.options.legendIndex || 0) - (b.options.legendIndex || 0);
  7463. });
  7464. // reversed legend
  7465. if (reversedLegend) {
  7466. allItems.reverse();
  7467. }
  7468. // render the items
  7469. each(allItems, renderItem);
  7470. // Draw the border
  7471. legendWidth = widthOption || offsetWidth;
  7472. legendHeight = lastItemY - y + itemHeight;
  7473. if (legendBorderWidth || legendBackgroundColor) {
  7474. legendWidth += 2 * padding;
  7475. legendHeight += 2 * padding;
  7476. if (!box) {
  7477. box = renderer.rect(
  7478. 0,
  7479. 0,
  7480. legendWidth,
  7481. legendHeight,
  7482. options.borderRadius,
  7483. legendBorderWidth || 0
  7484. ).attr({
  7485. stroke: options.borderColor,
  7486. 'stroke-width': legendBorderWidth || 0,
  7487. fill: legendBackgroundColor || NONE
  7488. })
  7489. .add(legendGroup)
  7490. .shadow(options.shadow);
  7491. box.isNew = true;
  7492. } else if (legendWidth > 0 && legendHeight > 0) {
  7493. box[box.isNew ? 'attr' : 'animate'](
  7494. box.crisp(null, null, null, legendWidth, legendHeight)
  7495. );
  7496. box.isNew = false;
  7497. }
  7498. // hide the border if no items
  7499. box[allItems.length ? 'show' : 'hide']();
  7500. }
  7501. // Now that the legend width and height are extablished, put the items in the
  7502. // final position
  7503. each(allItems, positionItem);
  7504. // 1.x compatibility: positioning based on style
  7505. var props = ['left', 'right', 'top', 'bottom'],
  7506. prop,
  7507. i = 4;
  7508. while (i--) {
  7509. prop = props[i];
  7510. if (style[prop] && style[prop] !== 'auto') {
  7511. options[i < 2 ? 'align' : 'verticalAlign'] = prop;
  7512. options[i < 2 ? 'x' : 'y'] = pInt(style[prop]) * (i % 2 ? -1 : 1);
  7513. }
  7514. }
  7515. if (allItems.length) {
  7516. legendGroup.align(extend(options, {
  7517. width: legendWidth,
  7518. height: legendHeight
  7519. }), true, spacingBox);
  7520. }
  7521. if (!isResizing) {
  7522. positionCheckboxes();
  7523. }
  7524. }
  7525. // run legend
  7526. renderLegend();
  7527. // move checkboxes
  7528. addEvent(chart, 'endResize', positionCheckboxes);
  7529. // expose
  7530. return {
  7531. colorizeItem: colorizeItem,
  7532. destroyItem: destroyItem,
  7533. renderLegend: renderLegend,
  7534. destroy: destroy
  7535. };
  7536. };
  7537. /**
  7538. * Initialize an individual series, called internally before render time
  7539. */
  7540. function initSeries(options) {
  7541. var type = options.type || optionsChart.type || optionsChart.defaultSeriesType,
  7542. typeClass = seriesTypes[type],
  7543. serie,
  7544. hasRendered = chart.hasRendered;
  7545. // an inverted chart can't take a column series and vice versa
  7546. if (hasRendered) {
  7547. if (inverted && type === 'column') {
  7548. typeClass = seriesTypes.bar;
  7549. } else if (!inverted && type === 'bar') {
  7550. typeClass = seriesTypes.column;
  7551. }
  7552. }
  7553. serie = new typeClass();
  7554. serie.init(chart, options);
  7555. // set internal chart properties
  7556. if (!hasRendered && serie.inverted) {
  7557. inverted = true;
  7558. }
  7559. if (serie.isCartesian) {
  7560. hasCartesianSeries = serie.isCartesian;
  7561. }
  7562. series.push(serie);
  7563. return serie;
  7564. }
  7565. /**
  7566. * Add a series dynamically after time
  7567. *
  7568. * @param {Object} options The config options
  7569. * @param {Boolean} redraw Whether to redraw the chart after adding. Defaults to true.
  7570. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  7571. * configuration
  7572. *
  7573. * @return {Object} series The newly created series object
  7574. */
  7575. function addSeries(options, redraw, animation) {
  7576. var series;
  7577. if (options) {
  7578. setAnimation(animation, chart);
  7579. redraw = pick(redraw, true); // defaults to true
  7580. fireEvent(chart, 'addSeries', { options: options }, function () {
  7581. series = initSeries(options);
  7582. series.isDirty = true;
  7583. chart.isDirtyLegend = true; // the series array is out of sync with the display
  7584. if (redraw) {
  7585. chart.redraw();
  7586. }
  7587. });
  7588. }
  7589. return series;
  7590. }
  7591. /**
  7592. * Check whether a given point is within the plot area
  7593. *
  7594. * @param {Number} x Pixel x relative to the plot area
  7595. * @param {Number} y Pixel y relative to the plot area
  7596. */
  7597. isInsidePlot = function (x, y) {
  7598. return x >= 0 &&
  7599. x <= plotWidth &&
  7600. y >= 0 &&
  7601. y <= plotHeight;
  7602. };
  7603. /**
  7604. * Adjust all axes tick amounts
  7605. */
  7606. function adjustTickAmounts() {
  7607. if (optionsChart.alignTicks !== false) {
  7608. each(axes, function (axis) {
  7609. axis.adjustTickAmount();
  7610. });
  7611. }
  7612. maxTicks = null;
  7613. }
  7614. /**
  7615. * Redraw legend, axes or series based on updated data
  7616. *
  7617. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  7618. * configuration
  7619. */
  7620. function redraw(animation) {
  7621. var redrawLegend = chart.isDirtyLegend,
  7622. hasStackedSeries,
  7623. isDirtyBox = chart.isDirtyBox, // todo: check if it has actually changed?
  7624. seriesLength = series.length,
  7625. i = seriesLength,
  7626. clipRect = chart.clipRect,
  7627. serie;
  7628. setAnimation(animation, chart);
  7629. // link stacked series
  7630. while (i--) {
  7631. serie = series[i];
  7632. if (serie.isDirty && serie.options.stacking) {
  7633. hasStackedSeries = true;
  7634. break;
  7635. }
  7636. }
  7637. if (hasStackedSeries) { // mark others as dirty
  7638. i = seriesLength;
  7639. while (i--) {
  7640. serie = series[i];
  7641. if (serie.options.stacking) {
  7642. serie.isDirty = true;
  7643. }
  7644. }
  7645. }
  7646. // handle updated data in the series
  7647. each(series, function (serie) {
  7648. if (serie.isDirty) { // prepare the data so axis can read it
  7649. if (serie.options.legendType === 'point') {
  7650. redrawLegend = true;
  7651. }
  7652. }
  7653. });
  7654. // handle added or removed series
  7655. if (redrawLegend && legend.renderLegend) { // series or pie points are added or removed
  7656. // draw legend graphics
  7657. legend.renderLegend();
  7658. chart.isDirtyLegend = false;
  7659. }
  7660. if (hasCartesianSeries) {
  7661. if (!isResizing) {
  7662. // reset maxTicks
  7663. maxTicks = null;
  7664. // set axes scales
  7665. each(axes, function (axis) {
  7666. axis.setScale();
  7667. });
  7668. }
  7669. adjustTickAmounts();
  7670. getMargins();
  7671. // redraw axes
  7672. each(axes, function (axis) {
  7673. // Fire 'afterSetExtremes' only if extremes are set
  7674. if (axis.isDirtyExtremes) { // #821
  7675. axis.isDirtyExtremes = false;
  7676. fireEvent(axis, 'afterSetExtremes', axis.getExtremes()); // #747, #751
  7677. }
  7678. if (axis.isDirty || isDirtyBox || hasStackedSeries) {
  7679. axis.redraw();
  7680. isDirtyBox = true; // #792
  7681. }
  7682. });
  7683. }
  7684. // the plot areas size has changed
  7685. if (isDirtyBox) {
  7686. drawChartBox();
  7687. // move clip rect
  7688. if (clipRect) {
  7689. stop(clipRect);
  7690. clipRect.animate({ // for chart resize
  7691. width: chart.plotSizeX,
  7692. height: chart.plotSizeY + 1
  7693. });
  7694. }
  7695. }
  7696. // redraw affected series
  7697. each(series, function (serie) {
  7698. if (serie.isDirty && serie.visible &&
  7699. (!serie.isCartesian || serie.xAxis)) { // issue #153
  7700. serie.redraw();
  7701. }
  7702. });
  7703. // move tooltip or reset
  7704. if (tracker && tracker.resetTracker) {
  7705. tracker.resetTracker(true);
  7706. }
  7707. // redraw if canvas
  7708. renderer.draw();
  7709. // fire the event
  7710. fireEvent(chart, 'redraw'); // jQuery breaks this when calling it from addEvent. Overwrites chart.redraw
  7711. }
  7712. /**
  7713. * Dim the chart and show a loading text or symbol
  7714. * @param {String} str An optional text to show in the loading label instead of the default one
  7715. */
  7716. function showLoading(str) {
  7717. var loadingOptions = options.loading;
  7718. // create the layer at the first call
  7719. if (!loadingDiv) {
  7720. loadingDiv = createElement(DIV, {
  7721. className: PREFIX + 'loading'
  7722. }, extend(loadingOptions.style, {
  7723. left: plotLeft + PX,
  7724. top: plotTop + PX,
  7725. width: plotWidth + PX,
  7726. height: plotHeight + PX,
  7727. zIndex: 10,
  7728. display: NONE
  7729. }), container);
  7730. loadingSpan = createElement(
  7731. 'span',
  7732. null,
  7733. loadingOptions.labelStyle,
  7734. loadingDiv
  7735. );
  7736. }
  7737. // update text
  7738. loadingSpan.innerHTML = str || options.lang.loading;
  7739. // show it
  7740. if (!loadingShown) {
  7741. css(loadingDiv, { opacity: 0, display: '' });
  7742. animate(loadingDiv, {
  7743. opacity: loadingOptions.style.opacity
  7744. }, {
  7745. duration: loadingOptions.showDuration || 0
  7746. });
  7747. loadingShown = true;
  7748. }
  7749. }
  7750. /**
  7751. * Hide the loading layer
  7752. */
  7753. function hideLoading() {
  7754. if (loadingDiv) {
  7755. animate(loadingDiv, {
  7756. opacity: 0
  7757. }, {
  7758. duration: options.loading.hideDuration || 100,
  7759. complete: function () {
  7760. css(loadingDiv, { display: NONE });
  7761. }
  7762. });
  7763. }
  7764. loadingShown = false;
  7765. }
  7766. /**
  7767. * Get an axis, series or point object by id.
  7768. * @param id {String} The id as given in the configuration options
  7769. */
  7770. function get(id) {
  7771. var i,
  7772. j,
  7773. points;
  7774. // search axes
  7775. for (i = 0; i < axes.length; i++) {
  7776. if (axes[i].options.id === id) {
  7777. return axes[i];
  7778. }
  7779. }
  7780. // search series
  7781. for (i = 0; i < series.length; i++) {
  7782. if (series[i].options.id === id) {
  7783. return series[i];
  7784. }
  7785. }
  7786. // search points
  7787. for (i = 0; i < series.length; i++) {
  7788. points = series[i].points || [];
  7789. for (j = 0; j < points.length; j++) {
  7790. if (points[j].id === id) {
  7791. return points[j];
  7792. }
  7793. }
  7794. }
  7795. return null;
  7796. }
  7797. /**
  7798. * Create the Axis instances based on the config options
  7799. */
  7800. function getAxes() {
  7801. var xAxisOptions = options.xAxis || {},
  7802. yAxisOptions = options.yAxis || {},
  7803. optionsArray,
  7804. axis;
  7805. // make sure the options are arrays and add some members
  7806. xAxisOptions = splat(xAxisOptions);
  7807. each(xAxisOptions, function (axis, i) {
  7808. axis.index = i;
  7809. axis.isX = true;
  7810. });
  7811. yAxisOptions = splat(yAxisOptions);
  7812. each(yAxisOptions, function (axis, i) {
  7813. axis.index = i;
  7814. });
  7815. // concatenate all axis options into one array
  7816. optionsArray = xAxisOptions.concat(yAxisOptions);
  7817. each(optionsArray, function (axisOptions) {
  7818. axis = new Axis(axisOptions);
  7819. });
  7820. adjustTickAmounts();
  7821. }
  7822. /**
  7823. * Get the currently selected points from all series
  7824. */
  7825. function getSelectedPoints() {
  7826. var points = [];
  7827. each(series, function (serie) {
  7828. points = points.concat(grep(serie.points, function (point) {
  7829. return point.selected;
  7830. }));
  7831. });
  7832. return points;
  7833. }
  7834. /**
  7835. * Get the currently selected series
  7836. */
  7837. function getSelectedSeries() {
  7838. return grep(series, function (serie) {
  7839. return serie.selected;
  7840. });
  7841. }
  7842. /**
  7843. * Display the zoom button
  7844. */
  7845. function showResetZoom() {
  7846. var lang = defaultOptions.lang,
  7847. btnOptions = optionsChart.resetZoomButton,
  7848. theme = btnOptions.theme,
  7849. states = theme.states,
  7850. box = btnOptions.relativeTo === 'chart' ? null : {
  7851. x: plotLeft,
  7852. y: plotTop,
  7853. width: plotWidth,
  7854. height: plotHeight
  7855. };
  7856. chart.resetZoomButton = renderer.button(lang.resetZoom, null, null, zoomOut, theme, states && states.hover)
  7857. .attr({
  7858. align: btnOptions.position.align,
  7859. title: lang.resetZoomTitle
  7860. })
  7861. .add()
  7862. .align(btnOptions.position, false, box);
  7863. }
  7864. /**
  7865. * Zoom out to 1:1
  7866. */
  7867. zoomOut = function () {
  7868. var resetZoomButton = chart.resetZoomButton;
  7869. fireEvent(chart, 'selection', { resetSelection: true }, zoom);
  7870. if (resetZoomButton) {
  7871. chart.resetZoomButton = resetZoomButton.destroy();
  7872. }
  7873. };
  7874. /**
  7875. * Zoom into a given portion of the chart given by axis coordinates
  7876. * @param {Object} event
  7877. */
  7878. zoom = function (event) {
  7879. // add button to reset selection
  7880. var hasZoomed;
  7881. if (chart.resetZoomEnabled !== false && !chart.resetZoomButton) { // hook for Stock charts etc.
  7882. showResetZoom();
  7883. }
  7884. // if zoom is called with no arguments, reset the axes
  7885. if (!event || event.resetSelection) {
  7886. each(axes, function (axis) {
  7887. if (axis.options.zoomEnabled !== false) {
  7888. axis.setExtremes(null, null, false);
  7889. hasZoomed = true;
  7890. }
  7891. });
  7892. } else { // else, zoom in on all axes
  7893. each(event.xAxis.concat(event.yAxis), function (axisData) {
  7894. var axis = axisData.axis;
  7895. // don't zoom more than minRange
  7896. if (chart.tracker[axis.isXAxis ? 'zoomX' : 'zoomY']) {
  7897. axis.setExtremes(axisData.min, axisData.max, false);
  7898. hasZoomed = true;
  7899. }
  7900. });
  7901. }
  7902. // Redraw
  7903. if (hasZoomed) {
  7904. redraw(
  7905. pick(optionsChart.animation, chart.pointCount < 100) // animation
  7906. );
  7907. }
  7908. };
  7909. /**
  7910. * Pan the chart by dragging the mouse across the pane. This function is called
  7911. * on mouse move, and the distance to pan is computed from chartX compared to
  7912. * the first chartX position in the dragging operation.
  7913. */
  7914. chart.pan = function (chartX) {
  7915. var xAxis = chart.xAxis[0],
  7916. mouseDownX = chart.mouseDownX,
  7917. halfPointRange = xAxis.pointRange / 2,
  7918. extremes = xAxis.getExtremes(),
  7919. newMin = xAxis.translate(mouseDownX - chartX, true) + halfPointRange,
  7920. newMax = xAxis.translate(mouseDownX + plotWidth - chartX, true) - halfPointRange,
  7921. hoverPoints = chart.hoverPoints;
  7922. // remove active points for shared tooltip
  7923. if (hoverPoints) {
  7924. each(hoverPoints, function (point) {
  7925. point.setState();
  7926. });
  7927. }
  7928. if (newMin > mathMin(extremes.dataMin, extremes.min) && newMax < mathMax(extremes.dataMax, extremes.max)) {
  7929. xAxis.setExtremes(newMin, newMax, true, false);
  7930. }
  7931. chart.mouseDownX = chartX; // set new reference for next run
  7932. css(container, { cursor: 'move' });
  7933. };
  7934. /**
  7935. * Show the title and subtitle of the chart
  7936. *
  7937. * @param titleOptions {Object} New title options
  7938. * @param subtitleOptions {Object} New subtitle options
  7939. *
  7940. */
  7941. function setTitle(titleOptions, subtitleOptions) {
  7942. chartTitleOptions = merge(options.title, titleOptions);
  7943. chartSubtitleOptions = merge(options.subtitle, subtitleOptions);
  7944. // add title and subtitle
  7945. each([
  7946. ['title', titleOptions, chartTitleOptions],
  7947. ['subtitle', subtitleOptions, chartSubtitleOptions]
  7948. ], function (arr) {
  7949. var name = arr[0],
  7950. title = chart[name],
  7951. titleOptions = arr[1],
  7952. chartTitleOptions = arr[2];
  7953. if (title && titleOptions) {
  7954. title = title.destroy(); // remove old
  7955. }
  7956. if (chartTitleOptions && chartTitleOptions.text && !title) {
  7957. chart[name] = renderer.text(
  7958. chartTitleOptions.text,
  7959. 0,
  7960. 0,
  7961. chartTitleOptions.useHTML
  7962. )
  7963. .attr({
  7964. align: chartTitleOptions.align,
  7965. 'class': PREFIX + name,
  7966. zIndex: chartTitleOptions.zIndex || 4
  7967. })
  7968. .css(chartTitleOptions.style)
  7969. .add()
  7970. .align(chartTitleOptions, false, spacingBox);
  7971. }
  7972. });
  7973. }
  7974. /**
  7975. * Get chart width and height according to options and container size
  7976. */
  7977. function getChartSize() {
  7978. containerWidth = (renderToClone || renderTo).offsetWidth;
  7979. containerHeight = (renderToClone || renderTo).offsetHeight;
  7980. chart.chartWidth = chartWidth = optionsChart.width || containerWidth || 600;
  7981. chart.chartHeight = chartHeight = optionsChart.height ||
  7982. // the offsetHeight of an empty container is 0 in standard browsers, but 19 in IE7:
  7983. (containerHeight > 19 ? containerHeight : 400);
  7984. }
  7985. /**
  7986. * Get the containing element, determine the size and create the inner container
  7987. * div to hold the chart
  7988. */
  7989. function getContainer() {
  7990. renderTo = optionsChart.renderTo;
  7991. containerId = PREFIX + idCounter++;
  7992. if (isString(renderTo)) {
  7993. renderTo = doc.getElementById(renderTo);
  7994. }
  7995. // Display an error if the renderTo is wrong
  7996. if (!renderTo) {
  7997. error(13, true);
  7998. }
  7999. // remove previous chart
  8000. renderTo.innerHTML = '';
  8001. // If the container doesn't have an offsetWidth, it ha s or is a child of a node
  8002. // that has display:none. We need to temporarily move it out to a visible
  8003. // state to determine the size, else the legend and tooltips won't render
  8004. // properly
  8005. if (!renderTo.offsetWidth) {
  8006. renderToClone = renderTo.cloneNode(0);
  8007. css(renderToClone, {
  8008. position: ABSOLUTE,
  8009. top: '-9999px',
  8010. display: 'block'
  8011. });
  8012. doc.body.appendChild(renderToClone);
  8013. }
  8014. // get the width and height
  8015. getChartSize();
  8016. // create the inner container
  8017. chart.container = container = createElement(DIV, {
  8018. className: PREFIX + 'container' +
  8019. (optionsChart.className ? ' ' + optionsChart.className : ''),
  8020. id: containerId
  8021. }, extend({
  8022. position: RELATIVE,
  8023. overflow: HIDDEN, // needed for context menu (avoid scrollbars) and
  8024. // content overflow in IE
  8025. width: chartWidth + PX,
  8026. height: chartHeight + PX,
  8027. textAlign: 'left',
  8028. lineHeight: 'normal' // #427
  8029. }, optionsChart.style),
  8030. renderToClone || renderTo
  8031. );
  8032. chart.renderer = renderer =
  8033. optionsChart.forExport ? // force SVG, used for SVG export
  8034. new SVGRenderer(container, chartWidth, chartHeight, true) :
  8035. new Renderer(container, chartWidth, chartHeight);
  8036. if (useCanVG) {
  8037. // If we need canvg library, extend and configure the renderer
  8038. // to get the tracker for translating mouse events
  8039. renderer.create(chart, container, chartWidth, chartHeight);
  8040. }
  8041. }
  8042. /**
  8043. * Calculate margins by rendering axis labels in a preliminary position. Title,
  8044. * subtitle and legend have already been rendered at this stage, but will be
  8045. * moved into their final positions
  8046. */
  8047. getMargins = function () {
  8048. var legendOptions = options.legend,
  8049. legendMargin = pick(legendOptions.margin, 10),
  8050. legendX = legendOptions.x,
  8051. legendY = legendOptions.y,
  8052. align = legendOptions.align,
  8053. verticalAlign = legendOptions.verticalAlign,
  8054. titleOffset;
  8055. resetMargins();
  8056. // adjust for title and subtitle
  8057. if ((chart.title || chart.subtitle) && !defined(optionsMarginTop)) {
  8058. titleOffset = mathMax(
  8059. (chart.title && !chartTitleOptions.floating && !chartTitleOptions.verticalAlign && chartTitleOptions.y) || 0,
  8060. (chart.subtitle && !chartSubtitleOptions.floating && !chartSubtitleOptions.verticalAlign && chartSubtitleOptions.y) || 0
  8061. );
  8062. if (titleOffset) {
  8063. plotTop = mathMax(plotTop, titleOffset + pick(chartTitleOptions.margin, 15) + spacingTop);
  8064. }
  8065. }
  8066. // adjust for legend
  8067. if (legendOptions.enabled && !legendOptions.floating) {
  8068. if (align === 'right') { // horizontal alignment handled first
  8069. if (!defined(optionsMarginRight)) {
  8070. marginRight = mathMax(
  8071. marginRight,
  8072. legendWidth - legendX + legendMargin + spacingRight
  8073. );
  8074. }
  8075. } else if (align === 'left') {
  8076. if (!defined(optionsMarginLeft)) {
  8077. plotLeft = mathMax(
  8078. plotLeft,
  8079. legendWidth + legendX + legendMargin + spacingLeft
  8080. );
  8081. }
  8082. } else if (verticalAlign === 'top') {
  8083. if (!defined(optionsMarginTop)) {
  8084. plotTop = mathMax(
  8085. plotTop,
  8086. legendHeight + legendY + legendMargin + spacingTop
  8087. );
  8088. }
  8089. } else if (verticalAlign === 'bottom') {
  8090. if (!defined(optionsMarginBottom)) {
  8091. marginBottom = mathMax(
  8092. marginBottom,
  8093. legendHeight - legendY + legendMargin + spacingBottom
  8094. );
  8095. }
  8096. }
  8097. }
  8098. // adjust for scroller
  8099. if (chart.extraBottomMargin) {
  8100. marginBottom += chart.extraBottomMargin;
  8101. }
  8102. if (chart.extraTopMargin) {
  8103. plotTop += chart.extraTopMargin;
  8104. }
  8105. // pre-render axes to get labels offset width
  8106. if (hasCartesianSeries) {
  8107. each(axes, function (axis) {
  8108. axis.getOffset();
  8109. });
  8110. }
  8111. if (!defined(optionsMarginLeft)) {
  8112. plotLeft += axisOffset[3];
  8113. }
  8114. if (!defined(optionsMarginTop)) {
  8115. plotTop += axisOffset[0];
  8116. }
  8117. if (!defined(optionsMarginBottom)) {
  8118. marginBottom += axisOffset[2];
  8119. }
  8120. if (!defined(optionsMarginRight)) {
  8121. marginRight += axisOffset[1];
  8122. }
  8123. setChartSize();
  8124. };
  8125. /**
  8126. * Add the event handlers necessary for auto resizing
  8127. *
  8128. */
  8129. function initReflow() {
  8130. var reflowTimeout;
  8131. function reflow(e) {
  8132. var width = optionsChart.width || renderTo.offsetWidth,
  8133. height = optionsChart.height || renderTo.offsetHeight,
  8134. target = e ? e.target : win; // #805 - MooTools doesn't supply e
  8135. // Width and height checks for display:none. Target is doc in IE8 and Opera,
  8136. // win in Firefox, Chrome and IE9.
  8137. if (width && height && (target === win || target === doc)) {
  8138. if (width !== containerWidth || height !== containerHeight) {
  8139. clearTimeout(reflowTimeout);
  8140. reflowTimeout = setTimeout(function () {
  8141. resize(width, height, false);
  8142. }, 100);
  8143. }
  8144. containerWidth = width;
  8145. containerHeight = height;
  8146. }
  8147. }
  8148. addEvent(win, 'resize', reflow);
  8149. addEvent(chart, 'destroy', function () {
  8150. removeEvent(win, 'resize', reflow);
  8151. });
  8152. }
  8153. /**
  8154. * Fires endResize event on chart instance.
  8155. */
  8156. function fireEndResize() {
  8157. if (chart) {
  8158. fireEvent(chart, 'endResize', null, function () {
  8159. isResizing -= 1;
  8160. });
  8161. }
  8162. }
  8163. /**
  8164. * Resize the chart to a given width and height
  8165. * @param {Number} width
  8166. * @param {Number} height
  8167. * @param {Object|Boolean} animation
  8168. */
  8169. resize = function (width, height, animation) {
  8170. var chartTitle = chart.title,
  8171. chartSubtitle = chart.subtitle;
  8172. isResizing += 1;
  8173. // set the animation for the current process
  8174. setAnimation(animation, chart);
  8175. oldChartHeight = chartHeight;
  8176. oldChartWidth = chartWidth;
  8177. if (defined(width)) {
  8178. chart.chartWidth = chartWidth = mathRound(width);
  8179. }
  8180. if (defined(height)) {
  8181. chart.chartHeight = chartHeight = mathRound(height);
  8182. }
  8183. css(container, {
  8184. width: chartWidth + PX,
  8185. height: chartHeight + PX
  8186. });
  8187. renderer.setSize(chartWidth, chartHeight, animation);
  8188. // update axis lengths for more correct tick intervals:
  8189. plotWidth = chartWidth - plotLeft - marginRight;
  8190. plotHeight = chartHeight - plotTop - marginBottom;
  8191. // handle axes
  8192. maxTicks = null;
  8193. each(axes, function (axis) {
  8194. axis.isDirty = true;
  8195. axis.setScale();
  8196. });
  8197. // make sure non-cartesian series are also handled
  8198. each(series, function (serie) {
  8199. serie.isDirty = true;
  8200. });
  8201. chart.isDirtyLegend = true; // force legend redraw
  8202. chart.isDirtyBox = true; // force redraw of plot and chart border
  8203. getMargins();
  8204. // move titles
  8205. if (chartTitle) {
  8206. chartTitle.align(null, null, spacingBox);
  8207. }
  8208. if (chartSubtitle) {
  8209. chartSubtitle.align(null, null, spacingBox);
  8210. }
  8211. redraw(animation);
  8212. oldChartHeight = null;
  8213. fireEvent(chart, 'resize');
  8214. // fire endResize and set isResizing back
  8215. // If animation is disabled, fire without delay
  8216. if (globalAnimation === false) {
  8217. fireEndResize();
  8218. } else { // else set a timeout with the animation duration
  8219. setTimeout(fireEndResize, (globalAnimation && globalAnimation.duration) || 500);
  8220. }
  8221. };
  8222. /**
  8223. * Set the public chart properties. This is done before and after the pre-render
  8224. * to determine margin sizes
  8225. */
  8226. setChartSize = function () {
  8227. chart.plotLeft = plotLeft = mathRound(plotLeft);
  8228. chart.plotTop = plotTop = mathRound(plotTop);
  8229. chart.plotWidth = plotWidth = mathRound(chartWidth - plotLeft - marginRight);
  8230. chart.plotHeight = plotHeight = mathRound(chartHeight - plotTop - marginBottom);
  8231. chart.plotSizeX = inverted ? plotHeight : plotWidth;
  8232. chart.plotSizeY = inverted ? plotWidth : plotHeight;
  8233. spacingBox = {
  8234. x: spacingLeft,
  8235. y: spacingTop,
  8236. width: chartWidth - spacingLeft - spacingRight,
  8237. height: chartHeight - spacingTop - spacingBottom
  8238. };
  8239. each(axes, function (axis) {
  8240. axis.setAxisSize();
  8241. axis.setAxisTranslation();
  8242. });
  8243. };
  8244. /**
  8245. * Initial margins before auto size margins are applied
  8246. */
  8247. resetMargins = function () {
  8248. plotTop = pick(optionsMarginTop, spacingTop);
  8249. marginRight = pick(optionsMarginRight, spacingRight);
  8250. marginBottom = pick(optionsMarginBottom, spacingBottom);
  8251. plotLeft = pick(optionsMarginLeft, spacingLeft);
  8252. axisOffset = [0, 0, 0, 0]; // top, right, bottom, left
  8253. };
  8254. /**
  8255. * Draw the borders and backgrounds for chart and plot area
  8256. */
  8257. drawChartBox = function () {
  8258. var chartBorderWidth = optionsChart.borderWidth || 0,
  8259. chartBackgroundColor = optionsChart.backgroundColor,
  8260. plotBackgroundColor = optionsChart.plotBackgroundColor,
  8261. plotBackgroundImage = optionsChart.plotBackgroundImage,
  8262. mgn,
  8263. plotSize = {
  8264. x: plotLeft,
  8265. y: plotTop,
  8266. width: plotWidth,
  8267. height: plotHeight
  8268. };
  8269. // Chart area
  8270. mgn = chartBorderWidth + (optionsChart.shadow ? 8 : 0);
  8271. if (chartBorderWidth || chartBackgroundColor) {
  8272. if (!chartBackground) {
  8273. chartBackground = renderer.rect(mgn / 2, mgn / 2, chartWidth - mgn, chartHeight - mgn,
  8274. optionsChart.borderRadius, chartBorderWidth)
  8275. .attr({
  8276. stroke: optionsChart.borderColor,
  8277. 'stroke-width': chartBorderWidth,
  8278. fill: chartBackgroundColor || NONE
  8279. })
  8280. .add()
  8281. .shadow(optionsChart.shadow);
  8282. } else { // resize
  8283. chartBackground.animate(
  8284. chartBackground.crisp(null, null, null, chartWidth - mgn, chartHeight - mgn)
  8285. );
  8286. }
  8287. }
  8288. // Plot background
  8289. if (plotBackgroundColor) {
  8290. if (!plotBackground) {
  8291. plotBackground = renderer.rect(plotLeft, plotTop, plotWidth, plotHeight, 0)
  8292. .attr({
  8293. fill: plotBackgroundColor
  8294. })
  8295. .add()
  8296. .shadow(optionsChart.plotShadow);
  8297. } else {
  8298. plotBackground.animate(plotSize);
  8299. }
  8300. }
  8301. if (plotBackgroundImage) {
  8302. if (!plotBGImage) {
  8303. plotBGImage = renderer.image(plotBackgroundImage, plotLeft, plotTop, plotWidth, plotHeight)
  8304. .add();
  8305. } else {
  8306. plotBGImage.animate(plotSize);
  8307. }
  8308. }
  8309. // Plot area border
  8310. if (optionsChart.plotBorderWidth) {
  8311. if (!plotBorder) {
  8312. plotBorder = renderer.rect(plotLeft, plotTop, plotWidth, plotHeight, 0, optionsChart.plotBorderWidth)
  8313. .attr({
  8314. stroke: optionsChart.plotBorderColor,
  8315. 'stroke-width': optionsChart.plotBorderWidth,
  8316. zIndex: 4
  8317. })
  8318. .add();
  8319. } else {
  8320. plotBorder.animate(
  8321. plotBorder.crisp(null, plotLeft, plotTop, plotWidth, plotHeight)
  8322. );
  8323. }
  8324. }
  8325. // reset
  8326. chart.isDirtyBox = false;
  8327. };
  8328. /**
  8329. * Detect whether the chart is inverted, either by setting the chart.inverted option
  8330. * or adding a bar series to the configuration options
  8331. */
  8332. function setInverted() {
  8333. var BAR = 'bar',
  8334. isInverted = (
  8335. inverted || // it is set before
  8336. optionsChart.inverted ||
  8337. optionsChart.type === BAR || // default series type
  8338. optionsChart.defaultSeriesType === BAR // backwards compatible
  8339. ),
  8340. seriesOptions = options.series,
  8341. i = seriesOptions && seriesOptions.length;
  8342. // check if a bar series is present in the config options
  8343. while (!isInverted && i--) {
  8344. if (seriesOptions[i].type === BAR) {
  8345. isInverted = true;
  8346. }
  8347. }
  8348. // set the chart property and the chart scope variable
  8349. chart.inverted = inverted = isInverted;
  8350. }
  8351. /**
  8352. * Render all graphics for the chart
  8353. */
  8354. function render() {
  8355. var labels = options.labels,
  8356. credits = options.credits,
  8357. creditsHref;
  8358. // Title
  8359. setTitle();
  8360. // Legend
  8361. legend = chart.legend = new Legend();
  8362. // Get margins by pre-rendering axes
  8363. // set axes scales
  8364. each(axes, function (axis) {
  8365. axis.setScale();
  8366. });
  8367. getMargins();
  8368. maxTicks = null; // reset for second pass
  8369. each(axes, function (axis) {
  8370. axis.setTickPositions(true); // update to reflect the new margins
  8371. axis.setMaxTicks();
  8372. });
  8373. adjustTickAmounts();
  8374. getMargins(); // second pass to check for new labels
  8375. // Draw the borders and backgrounds
  8376. drawChartBox();
  8377. // Axes
  8378. if (hasCartesianSeries) {
  8379. each(axes, function (axis) {
  8380. axis.render();
  8381. });
  8382. }
  8383. // The series
  8384. if (!chart.seriesGroup) {
  8385. chart.seriesGroup = renderer.g('series-group')
  8386. .attr({ zIndex: 3 })
  8387. .add();
  8388. }
  8389. each(series, function (serie) {
  8390. serie.translate();
  8391. serie.setTooltipPoints();
  8392. serie.render();
  8393. });
  8394. // Labels
  8395. if (labels.items) {
  8396. each(labels.items, function () {
  8397. var style = extend(labels.style, this.style),
  8398. x = pInt(style.left) + plotLeft,
  8399. y = pInt(style.top) + plotTop + 12;
  8400. // delete to prevent rewriting in IE
  8401. delete style.left;
  8402. delete style.top;
  8403. renderer.text(
  8404. this.html,
  8405. x,
  8406. y
  8407. )
  8408. .attr({ zIndex: 2 })
  8409. .css(style)
  8410. .add();
  8411. });
  8412. }
  8413. // Credits
  8414. if (credits.enabled && !chart.credits) {
  8415. creditsHref = credits.href;
  8416. chart.credits = renderer.text(
  8417. credits.text,
  8418. 0,
  8419. 0
  8420. )
  8421. .on('click', function () {
  8422. if (creditsHref) {
  8423. location.href = creditsHref;
  8424. }
  8425. })
  8426. .attr({
  8427. align: credits.position.align,
  8428. zIndex: 8
  8429. })
  8430. .css(credits.style)
  8431. .add()
  8432. .align(credits.position);
  8433. }
  8434. // Set flag
  8435. chart.hasRendered = true;
  8436. }
  8437. /**
  8438. * Clean up memory usage
  8439. */
  8440. function destroy() {
  8441. var i,
  8442. parentNode = container && container.parentNode;
  8443. // If the chart is destroyed already, do nothing.
  8444. // This will happen if if a script invokes chart.destroy and
  8445. // then it will be called again on win.unload
  8446. if (chart === null) {
  8447. return;
  8448. }
  8449. // fire the chart.destoy event
  8450. fireEvent(chart, 'destroy');
  8451. // remove events
  8452. removeEvent(chart);
  8453. // ==== Destroy collections:
  8454. // Destroy axes
  8455. i = axes.length;
  8456. while (i--) {
  8457. axes[i] = axes[i].destroy();
  8458. }
  8459. // Destroy each series
  8460. i = series.length;
  8461. while (i--) {
  8462. series[i] = series[i].destroy();
  8463. }
  8464. // ==== Destroy chart properties:
  8465. each(['title', 'subtitle', 'seriesGroup', 'clipRect', 'credits', 'tracker', 'scroller', 'rangeSelector'], function (name) {
  8466. var prop = chart[name];
  8467. if (prop) {
  8468. chart[name] = prop.destroy();
  8469. }
  8470. });
  8471. // ==== Destroy local variables:
  8472. each([chartBackground, plotBorder, plotBackground, legend, tooltip, renderer, tracker], function (obj) {
  8473. if (obj && obj.destroy) {
  8474. obj.destroy();
  8475. }
  8476. });
  8477. chartBackground = plotBorder = plotBackground = legend = tooltip = renderer = tracker = null;
  8478. // remove container and all SVG
  8479. if (container) { // can break in IE when destroyed before finished loading
  8480. container.innerHTML = '';
  8481. removeEvent(container);
  8482. if (parentNode) {
  8483. discardElement(container);
  8484. }
  8485. // IE6 leak
  8486. container = null;
  8487. }
  8488. // memory and CPU leak
  8489. clearInterval(tooltipInterval);
  8490. // clean it all up
  8491. for (i in chart) {
  8492. delete chart[i];
  8493. }
  8494. chart = null;
  8495. options = null;
  8496. }
  8497. /**
  8498. * Prepare for first rendering after all data are loaded
  8499. */
  8500. function firstRender() {
  8501. // VML namespaces can't be added until after complete. Listening
  8502. // for Perini's doScroll hack is not enough.
  8503. var ONREADYSTATECHANGE = 'onreadystatechange',
  8504. COMPLETE = 'complete';
  8505. // Note: in spite of JSLint's complaints, win == win.top is required
  8506. /*jslint eqeq: true*/
  8507. if ((!hasSVG && (win == win.top && doc.readyState !== COMPLETE)) || (useCanVG && !win.canvg)) {
  8508. /*jslint eqeq: false*/
  8509. if (useCanVG) {
  8510. // Delay rendering until canvg library is downloaded and ready
  8511. CanVGController.push(firstRender, options.global.canvasToolsURL);
  8512. } else {
  8513. doc.attachEvent(ONREADYSTATECHANGE, function () {
  8514. doc.detachEvent(ONREADYSTATECHANGE, firstRender);
  8515. if (doc.readyState === COMPLETE) {
  8516. firstRender();
  8517. }
  8518. });
  8519. }
  8520. return;
  8521. }
  8522. // create the container
  8523. getContainer();
  8524. // Run an early event after the container and renderer are established
  8525. fireEvent(chart, 'init');
  8526. // Initialize range selector for stock charts
  8527. if (Highcharts.RangeSelector && options.rangeSelector.enabled) {
  8528. chart.rangeSelector = new Highcharts.RangeSelector(chart);
  8529. }
  8530. resetMargins();
  8531. setChartSize();
  8532. // Set the common inversion and transformation for inverted series after initSeries
  8533. setInverted();
  8534. // get axes
  8535. getAxes();
  8536. // Initialize the series
  8537. each(options.series || [], function (serieOptions) {
  8538. initSeries(serieOptions);
  8539. });
  8540. // Run an event where series and axes can be added
  8541. //fireEvent(chart, 'beforeRender');
  8542. // Initialize scroller for stock charts
  8543. if (Highcharts.Scroller && (options.navigator.enabled || options.scrollbar.enabled)) {
  8544. chart.scroller = new Highcharts.Scroller(chart);
  8545. }
  8546. chart.render = render;
  8547. // depends on inverted and on margins being set
  8548. chart.tracker = tracker = new MouseTracker(options.tooltip);
  8549. render();
  8550. // add canvas
  8551. renderer.draw();
  8552. // run callbacks
  8553. if (callback) {
  8554. callback.apply(chart, [chart]);
  8555. }
  8556. each(chart.callbacks, function (fn) {
  8557. fn.apply(chart, [chart]);
  8558. });
  8559. // If the chart was rendered outside the top container, put it back in
  8560. if (renderToClone) {
  8561. renderTo.appendChild(container);
  8562. discardElement(renderToClone);
  8563. }
  8564. fireEvent(chart, 'load');
  8565. }
  8566. // Run chart
  8567. // Set up auto resize
  8568. if (optionsChart.reflow !== false) {
  8569. addEvent(chart, 'load', initReflow);
  8570. }
  8571. // Chart event handlers
  8572. if (chartEvents) {
  8573. for (eventType in chartEvents) {
  8574. addEvent(chart, eventType, chartEvents[eventType]);
  8575. }
  8576. }
  8577. chart.options = options;
  8578. chart.series = series;
  8579. chart.xAxis = [];
  8580. chart.yAxis = [];
  8581. // Expose methods and variables
  8582. chart.addSeries = addSeries;
  8583. chart.animation = useCanVG ? false : pick(optionsChart.animation, true);
  8584. chart.Axis = Axis;
  8585. chart.destroy = destroy;
  8586. chart.get = get;
  8587. chart.getSelectedPoints = getSelectedPoints;
  8588. chart.getSelectedSeries = getSelectedSeries;
  8589. chart.hideLoading = hideLoading;
  8590. chart.initSeries = initSeries;
  8591. chart.isInsidePlot = isInsidePlot;
  8592. chart.redraw = redraw;
  8593. chart.setSize = resize;
  8594. chart.setTitle = setTitle;
  8595. chart.showLoading = showLoading;
  8596. chart.pointCount = 0;
  8597. chart.counters = new ChartCounters();
  8598. /*
  8599. if ($) $(function () {
  8600. $container = $('#container');
  8601. var origChartWidth,
  8602. origChartHeight;
  8603. if ($container) {
  8604. $('<button>+</button>')
  8605. .insertBefore($container)
  8606. .click(function () {
  8607. if (origChartWidth === UNDEFINED) {
  8608. origChartWidth = chartWidth;
  8609. origChartHeight = chartHeight;
  8610. }
  8611. chart.resize(chartWidth *= 1.1, chartHeight *= 1.1);
  8612. });
  8613. $('<button>-</button>')
  8614. .insertBefore($container)
  8615. .click(function () {
  8616. if (origChartWidth === UNDEFINED) {
  8617. origChartWidth = chartWidth;
  8618. origChartHeight = chartHeight;
  8619. }
  8620. chart.resize(chartWidth *= 0.9, chartHeight *= 0.9);
  8621. });
  8622. $('<button>1:1</button>')
  8623. .insertBefore($container)
  8624. .click(function () {
  8625. if (origChartWidth === UNDEFINED) {
  8626. origChartWidth = chartWidth;
  8627. origChartHeight = chartHeight;
  8628. }
  8629. chart.resize(origChartWidth, origChartHeight);
  8630. });
  8631. }
  8632. })
  8633. */
  8634. firstRender();
  8635. } // end Chart
  8636. // Hook for exporting module
  8637. Chart.prototype.callbacks = [];
  8638. /**
  8639. * The Point object and prototype. Inheritable and used as base for PiePoint
  8640. */
  8641. var Point = function () {};
  8642. Point.prototype = {
  8643. /**
  8644. * Initialize the point
  8645. * @param {Object} series The series object containing this point
  8646. * @param {Object} options The data in either number, array or object format
  8647. */
  8648. init: function (series, options, x) {
  8649. var point = this,
  8650. counters = series.chart.counters,
  8651. defaultColors;
  8652. point.series = series;
  8653. point.applyOptions(options, x);
  8654. point.pointAttr = {};
  8655. if (series.options.colorByPoint) {
  8656. defaultColors = series.chart.options.colors;
  8657. if (!point.options) {
  8658. point.options = {};
  8659. }
  8660. point.color = point.options.color = point.color || defaultColors[counters.color++];
  8661. // loop back to zero
  8662. counters.wrapColor(defaultColors.length);
  8663. }
  8664. series.chart.pointCount++;
  8665. return point;
  8666. },
  8667. /**
  8668. * Apply the options containing the x and y data and possible some extra properties.
  8669. * This is called on point init or from point.update.
  8670. *
  8671. * @param {Object} options
  8672. */
  8673. applyOptions: function (options, x) {
  8674. var point = this,
  8675. series = point.series,
  8676. optionsType = typeof options;
  8677. point.config = options;
  8678. // onedimensional array input
  8679. if (optionsType === 'number' || options === null) {
  8680. point.y = options;
  8681. } else if (typeof options[0] === 'number') { // two-dimentional array
  8682. point.x = options[0];
  8683. point.y = options[1];
  8684. } else if (optionsType === 'object' && typeof options.length !== 'number') { // object input
  8685. // copy options directly to point
  8686. extend(point, options);
  8687. point.options = options;
  8688. // This is the fastest way to detect if there are individual point dataLabels that need
  8689. // to be considered in drawDataLabels. These can only occur in object configs.
  8690. if (options.dataLabels) {
  8691. series._hasPointLabels = true;
  8692. }
  8693. } else if (typeof options[0] === 'string') { // categorized data with name in first position
  8694. point.name = options[0];
  8695. point.y = options[1];
  8696. }
  8697. /*
  8698. * If no x is set by now, get auto incremented value. All points must have an
  8699. * x value, however the y value can be null to create a gap in the series
  8700. */
  8701. // todo: skip this? It is only used in applyOptions, in translate it should not be used
  8702. if (point.x === UNDEFINED) {
  8703. point.x = x === UNDEFINED ? series.autoIncrement() : x;
  8704. }
  8705. },
  8706. /**
  8707. * Destroy a point to clear memory. Its reference still stays in series.data.
  8708. */
  8709. destroy: function () {
  8710. var point = this,
  8711. series = point.series,
  8712. chart = series.chart,
  8713. hoverPoints = chart.hoverPoints,
  8714. prop;
  8715. chart.pointCount--;
  8716. if (hoverPoints) {
  8717. point.setState();
  8718. erase(hoverPoints, point);
  8719. if (!hoverPoints.length) {
  8720. chart.hoverPoints = null;
  8721. }
  8722. }
  8723. if (point === chart.hoverPoint) {
  8724. point.onMouseOut();
  8725. }
  8726. // remove all events
  8727. if (point.graphic || point.dataLabel) { // removeEvent and destroyElements are performance expensive
  8728. removeEvent(point);
  8729. point.destroyElements();
  8730. }
  8731. if (point.legendItem) { // pies have legend items
  8732. chart.legend.destroyItem(point);
  8733. }
  8734. for (prop in point) {
  8735. point[prop] = null;
  8736. }
  8737. },
  8738. /**
  8739. * Destroy SVG elements associated with the point
  8740. */
  8741. destroyElements: function () {
  8742. var point = this,
  8743. props = ['graphic', 'tracker', 'dataLabel', 'group', 'connector', 'shadowGroup'],
  8744. prop,
  8745. i = 6;
  8746. while (i--) {
  8747. prop = props[i];
  8748. if (point[prop]) {
  8749. point[prop] = point[prop].destroy();
  8750. }
  8751. }
  8752. },
  8753. /**
  8754. * Return the configuration hash needed for the data label and tooltip formatters
  8755. */
  8756. getLabelConfig: function () {
  8757. var point = this;
  8758. return {
  8759. x: point.category,
  8760. y: point.y,
  8761. key: point.name || point.category,
  8762. series: point.series,
  8763. point: point,
  8764. percentage: point.percentage,
  8765. total: point.total || point.stackTotal
  8766. };
  8767. },
  8768. /**
  8769. * Toggle the selection status of a point
  8770. * @param {Boolean} selected Whether to select or unselect the point.
  8771. * @param {Boolean} accumulate Whether to add to the previous selection. By default,
  8772. * this happens if the control key (Cmd on Mac) was pressed during clicking.
  8773. */
  8774. select: function (selected, accumulate) {
  8775. var point = this,
  8776. series = point.series,
  8777. chart = series.chart;
  8778. selected = pick(selected, !point.selected);
  8779. // fire the event with the defalut handler
  8780. point.firePointEvent(selected ? 'select' : 'unselect', { accumulate: accumulate }, function () {
  8781. point.selected = selected;
  8782. point.setState(selected && SELECT_STATE);
  8783. // unselect all other points unless Ctrl or Cmd + click
  8784. if (!accumulate) {
  8785. each(chart.getSelectedPoints(), function (loopPoint) {
  8786. if (loopPoint.selected && loopPoint !== point) {
  8787. loopPoint.selected = false;
  8788. loopPoint.setState(NORMAL_STATE);
  8789. loopPoint.firePointEvent('unselect');
  8790. }
  8791. });
  8792. }
  8793. });
  8794. },
  8795. onMouseOver: function () {
  8796. var point = this,
  8797. series = point.series,
  8798. chart = series.chart,
  8799. tooltip = chart.tooltip,
  8800. hoverPoint = chart.hoverPoint;
  8801. // set normal state to previous series
  8802. if (hoverPoint && hoverPoint !== point) {
  8803. hoverPoint.onMouseOut();
  8804. }
  8805. // trigger the event
  8806. point.firePointEvent('mouseOver');
  8807. // update the tooltip
  8808. if (tooltip && (!tooltip.shared || series.noSharedTooltip)) {
  8809. tooltip.refresh(point);
  8810. }
  8811. // hover this
  8812. point.setState(HOVER_STATE);
  8813. chart.hoverPoint = point;
  8814. },
  8815. onMouseOut: function () {
  8816. var point = this;
  8817. point.firePointEvent('mouseOut');
  8818. point.setState();
  8819. point.series.chart.hoverPoint = null;
  8820. },
  8821. /**
  8822. * Extendable method for formatting each point's tooltip line
  8823. *
  8824. * @return {String} A string to be concatenated in to the common tooltip text
  8825. */
  8826. tooltipFormatter: function (pointFormat) {
  8827. var point = this,
  8828. series = point.series,
  8829. seriesTooltipOptions = series.tooltipOptions,
  8830. split = String(point.y).split('.'),
  8831. originalDecimals = split[1] ? split[1].length : 0,
  8832. match = pointFormat.match(/\{(series|point)\.[a-zA-Z]+\}/g),
  8833. splitter = /[{\.}]/,
  8834. obj,
  8835. key,
  8836. replacement,
  8837. parts,
  8838. prop,
  8839. i;
  8840. // loop over the variables defined on the form {series.name}, {point.y} etc
  8841. for (i in match) {
  8842. key = match[i];
  8843. if (isString(key) && key !== pointFormat) { // IE matches more than just the variables
  8844. // Split it further into parts
  8845. parts = (' ' + key).split(splitter); // add empty string because IE and the rest handles it differently
  8846. obj = { 'point': point, 'series': series }[parts[1]];
  8847. prop = parts[2];
  8848. // Add some preformatting
  8849. if (obj === point && (prop === 'y' || prop === 'open' || prop === 'high' ||
  8850. prop === 'low' || prop === 'close')) {
  8851. replacement = (seriesTooltipOptions.valuePrefix || seriesTooltipOptions.yPrefix || '') +
  8852. numberFormat(point[prop], pick(seriesTooltipOptions.valueDecimals, seriesTooltipOptions.yDecimals, originalDecimals)) +
  8853. (seriesTooltipOptions.valueSuffix || seriesTooltipOptions.ySuffix || '');
  8854. // Automatic replacement
  8855. } else {
  8856. replacement = obj[prop];
  8857. }
  8858. pointFormat = pointFormat.replace(key, replacement);
  8859. }
  8860. }
  8861. return pointFormat;
  8862. },
  8863. /**
  8864. * Update the point with new options (typically x/y data) and optionally redraw the series.
  8865. *
  8866. * @param {Object} options Point options as defined in the series.data array
  8867. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  8868. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  8869. * configuration
  8870. *
  8871. */
  8872. update: function (options, redraw, animation) {
  8873. var point = this,
  8874. series = point.series,
  8875. graphic = point.graphic,
  8876. i,
  8877. data = series.data,
  8878. dataLength = data.length,
  8879. chart = series.chart;
  8880. redraw = pick(redraw, true);
  8881. // fire the event with a default handler of doing the update
  8882. point.firePointEvent('update', { options: options }, function () {
  8883. point.applyOptions(options);
  8884. // update visuals
  8885. if (isObject(options)) {
  8886. series.getAttribs();
  8887. if (graphic) {
  8888. graphic.attr(point.pointAttr[series.state]);
  8889. }
  8890. }
  8891. // record changes in the parallel arrays
  8892. for (i = 0; i < dataLength; i++) {
  8893. if (data[i] === point) {
  8894. series.xData[i] = point.x;
  8895. series.yData[i] = point.y;
  8896. series.options.data[i] = options;
  8897. break;
  8898. }
  8899. }
  8900. // redraw
  8901. series.isDirty = true;
  8902. series.isDirtyData = true;
  8903. if (redraw) {
  8904. chart.redraw(animation);
  8905. }
  8906. });
  8907. },
  8908. /**
  8909. * Remove a point and optionally redraw the series and if necessary the axes
  8910. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  8911. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  8912. * configuration
  8913. */
  8914. remove: function (redraw, animation) {
  8915. var point = this,
  8916. series = point.series,
  8917. chart = series.chart,
  8918. i,
  8919. data = series.data,
  8920. dataLength = data.length;
  8921. setAnimation(animation, chart);
  8922. redraw = pick(redraw, true);
  8923. // fire the event with a default handler of removing the point
  8924. point.firePointEvent('remove', null, function () {
  8925. //erase(series.data, point);
  8926. for (i = 0; i < dataLength; i++) {
  8927. if (data[i] === point) {
  8928. // splice all the parallel arrays
  8929. data.splice(i, 1);
  8930. series.options.data.splice(i, 1);
  8931. series.xData.splice(i, 1);
  8932. series.yData.splice(i, 1);
  8933. break;
  8934. }
  8935. }
  8936. point.destroy();
  8937. // redraw
  8938. series.isDirty = true;
  8939. series.isDirtyData = true;
  8940. if (redraw) {
  8941. chart.redraw();
  8942. }
  8943. });
  8944. },
  8945. /**
  8946. * Fire an event on the Point object. Must not be renamed to fireEvent, as this
  8947. * causes a name clash in MooTools
  8948. * @param {String} eventType
  8949. * @param {Object} eventArgs Additional event arguments
  8950. * @param {Function} defaultFunction Default event handler
  8951. */
  8952. firePointEvent: function (eventType, eventArgs, defaultFunction) {
  8953. var point = this,
  8954. series = this.series,
  8955. seriesOptions = series.options;
  8956. // load event handlers on demand to save time on mouseover/out
  8957. if (seriesOptions.point.events[eventType] || (point.options && point.options.events && point.options.events[eventType])) {
  8958. this.importEvents();
  8959. }
  8960. // add default handler if in selection mode
  8961. if (eventType === 'click' && seriesOptions.allowPointSelect) {
  8962. defaultFunction = function (event) {
  8963. // Control key is for Windows, meta (= Cmd key) for Mac, Shift for Opera
  8964. point.select(null, event.ctrlKey || event.metaKey || event.shiftKey);
  8965. };
  8966. }
  8967. fireEvent(this, eventType, eventArgs, defaultFunction);
  8968. },
  8969. /**
  8970. * Import events from the series' and point's options. Only do it on
  8971. * demand, to save processing time on hovering.
  8972. */
  8973. importEvents: function () {
  8974. if (!this.hasImportedEvents) {
  8975. var point = this,
  8976. options = merge(point.series.options.point, point.options),
  8977. events = options.events,
  8978. eventType;
  8979. point.events = events;
  8980. for (eventType in events) {
  8981. addEvent(point, eventType, events[eventType]);
  8982. }
  8983. this.hasImportedEvents = true;
  8984. }
  8985. },
  8986. /**
  8987. * Set the point's state
  8988. * @param {String} state
  8989. */
  8990. setState: function (state) {
  8991. var point = this,
  8992. plotX = point.plotX,
  8993. plotY = point.plotY,
  8994. series = point.series,
  8995. stateOptions = series.options.states,
  8996. markerOptions = defaultPlotOptions[series.type].marker && series.options.marker,
  8997. normalDisabled = markerOptions && !markerOptions.enabled,
  8998. markerStateOptions = markerOptions && markerOptions.states[state],
  8999. stateDisabled = markerStateOptions && markerStateOptions.enabled === false,
  9000. stateMarkerGraphic = series.stateMarkerGraphic,
  9001. chart = series.chart,
  9002. radius,
  9003. pointAttr = point.pointAttr;
  9004. state = state || NORMAL_STATE; // empty string
  9005. if (
  9006. // already has this state
  9007. state === point.state ||
  9008. // selected points don't respond to hover
  9009. (point.selected && state !== SELECT_STATE) ||
  9010. // series' state options is disabled
  9011. (stateOptions[state] && stateOptions[state].enabled === false) ||
  9012. // point marker's state options is disabled
  9013. (state && (stateDisabled || (normalDisabled && !markerStateOptions.enabled)))
  9014. ) {
  9015. return;
  9016. }
  9017. // apply hover styles to the existing point
  9018. if (point.graphic) {
  9019. radius = markerOptions && point.graphic.symbolName && pointAttr[state].r;
  9020. point.graphic.attr(merge(
  9021. pointAttr[state],
  9022. radius ? { // new symbol attributes (#507, #612)
  9023. x: plotX - radius,
  9024. y: plotY - radius,
  9025. width: 2 * radius,
  9026. height: 2 * radius
  9027. } : {}
  9028. ));
  9029. } else {
  9030. // if a graphic is not applied to each point in the normal state, create a shared
  9031. // graphic for the hover state
  9032. if (state) {
  9033. if (!stateMarkerGraphic) {
  9034. radius = markerOptions.radius;
  9035. series.stateMarkerGraphic = stateMarkerGraphic = chart.renderer.symbol(
  9036. series.symbol,
  9037. -radius,
  9038. -radius,
  9039. 2 * radius,
  9040. 2 * radius
  9041. )
  9042. .attr(pointAttr[state])
  9043. .add(series.group);
  9044. }
  9045. stateMarkerGraphic.translate(
  9046. plotX,
  9047. plotY
  9048. );
  9049. }
  9050. if (stateMarkerGraphic) {
  9051. stateMarkerGraphic[state ? 'show' : 'hide']();
  9052. }
  9053. }
  9054. point.state = state;
  9055. }
  9056. };
  9057. /**
  9058. * @classDescription The base function which all other series types inherit from. The data in the series is stored
  9059. * in various arrays.
  9060. *
  9061. * - First, series.options.data contains all the original config options for
  9062. * each point whether added by options or methods like series.addPoint.
  9063. * - Next, series.data contains those values converted to points, but in case the series data length
  9064. * exceeds the cropThreshold, or if the data is grouped, series.data doesn't contain all the points. It
  9065. * only contains the points that have been created on demand.
  9066. * - Then there's series.points that contains all currently visible point objects. In case of cropping,
  9067. * the cropped-away points are not part of this array. The series.points array starts at series.cropStart
  9068. * compared to series.data and series.options.data. If however the series data is grouped, these can't
  9069. * be correlated one to one.
  9070. * - series.xData and series.processedXData contain clean x values, equivalent to series.data and series.points.
  9071. * - series.yData and series.processedYData contain clean x values, equivalent to series.data and series.points.
  9072. *
  9073. * @param {Object} chart
  9074. * @param {Object} options
  9075. */
  9076. var Series = function () {};
  9077. Series.prototype = {
  9078. isCartesian: true,
  9079. type: 'line',
  9080. pointClass: Point,
  9081. sorted: true, // requires the data to be sorted
  9082. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  9083. stroke: 'lineColor',
  9084. 'stroke-width': 'lineWidth',
  9085. fill: 'fillColor',
  9086. r: 'radius'
  9087. },
  9088. init: function (chart, options) {
  9089. var series = this,
  9090. eventType,
  9091. events,
  9092. //pointEvent,
  9093. index = chart.series.length;
  9094. series.chart = chart;
  9095. series.options = options = series.setOptions(options); // merge with plotOptions
  9096. // bind the axes
  9097. series.bindAxes();
  9098. // set some variables
  9099. extend(series, {
  9100. index: index,
  9101. name: options.name || 'Series ' + (index + 1),
  9102. state: NORMAL_STATE,
  9103. pointAttr: {},
  9104. visible: options.visible !== false, // true by default
  9105. selected: options.selected === true // false by default
  9106. });
  9107. // special
  9108. if (useCanVG) {
  9109. options.animation = false;
  9110. }
  9111. // register event listeners
  9112. events = options.events;
  9113. for (eventType in events) {
  9114. addEvent(series, eventType, events[eventType]);
  9115. }
  9116. if (
  9117. (events && events.click) ||
  9118. (options.point && options.point.events && options.point.events.click) ||
  9119. options.allowPointSelect
  9120. ) {
  9121. chart.runTrackerClick = true;
  9122. }
  9123. series.getColor();
  9124. series.getSymbol();
  9125. // set the data
  9126. series.setData(options.data, false);
  9127. },
  9128. /**
  9129. * Set the xAxis and yAxis properties of cartesian series, and register the series
  9130. * in the axis.series array
  9131. */
  9132. bindAxes: function () {
  9133. var series = this,
  9134. seriesOptions = series.options,
  9135. chart = series.chart,
  9136. axisOptions;
  9137. if (series.isCartesian) {
  9138. each(['xAxis', 'yAxis'], function (AXIS) { // repeat for xAxis and yAxis
  9139. each(chart[AXIS], function (axis) { // loop through the chart's axis objects
  9140. axisOptions = axis.options;
  9141. // apply if the series xAxis or yAxis option mathches the number of the
  9142. // axis, or if undefined, use the first axis
  9143. if ((seriesOptions[AXIS] === axisOptions.index) ||
  9144. (seriesOptions[AXIS] === UNDEFINED && axisOptions.index === 0)) {
  9145. // register this series in the axis.series lookup
  9146. axis.series.push(series);
  9147. // set this series.xAxis or series.yAxis reference
  9148. series[AXIS] = axis;
  9149. // mark dirty for redraw
  9150. axis.isDirty = true;
  9151. }
  9152. });
  9153. });
  9154. }
  9155. },
  9156. /**
  9157. * Return an auto incremented x value based on the pointStart and pointInterval options.
  9158. * This is only used if an x value is not given for the point that calls autoIncrement.
  9159. */
  9160. autoIncrement: function () {
  9161. var series = this,
  9162. options = series.options,
  9163. xIncrement = series.xIncrement;
  9164. xIncrement = pick(xIncrement, options.pointStart, 0);
  9165. series.pointInterval = pick(series.pointInterval, options.pointInterval, 1);
  9166. series.xIncrement = xIncrement + series.pointInterval;
  9167. return xIncrement;
  9168. },
  9169. /**
  9170. * Divide the series data into segments divided by null values.
  9171. */
  9172. getSegments: function () {
  9173. var series = this,
  9174. lastNull = -1,
  9175. segments = [],
  9176. i,
  9177. points = series.points,
  9178. pointsLength = points.length;
  9179. if (pointsLength) { // no action required for []
  9180. // if connect nulls, just remove null points
  9181. if (series.options.connectNulls) {
  9182. i = pointsLength;
  9183. while (i--) {
  9184. if (points[i].y === null) {
  9185. points.splice(i, 1);
  9186. }
  9187. }
  9188. if (points.length) {
  9189. segments = [points];
  9190. }
  9191. // else, split on null points
  9192. } else {
  9193. each(points, function (point, i) {
  9194. if (point.y === null) {
  9195. if (i > lastNull + 1) {
  9196. segments.push(points.slice(lastNull + 1, i));
  9197. }
  9198. lastNull = i;
  9199. } else if (i === pointsLength - 1) { // last value
  9200. segments.push(points.slice(lastNull + 1, i + 1));
  9201. }
  9202. });
  9203. }
  9204. }
  9205. // register it
  9206. series.segments = segments;
  9207. },
  9208. /**
  9209. * Set the series options by merging from the options tree
  9210. * @param {Object} itemOptions
  9211. */
  9212. setOptions: function (itemOptions) {
  9213. var series = this,
  9214. chart = series.chart,
  9215. chartOptions = chart.options,
  9216. plotOptions = chartOptions.plotOptions,
  9217. data = itemOptions.data,
  9218. options;
  9219. itemOptions.data = null; // remove from merge to prevent looping over the data set
  9220. options = merge(
  9221. plotOptions[this.type],
  9222. plotOptions.series,
  9223. itemOptions
  9224. );
  9225. // Re-insert the data array to the options and the original config (#717)
  9226. options.data = itemOptions.data = data;
  9227. // the tooltip options are merged between global and series specific options
  9228. series.tooltipOptions = merge(chartOptions.tooltip, options.tooltip);
  9229. return options;
  9230. },
  9231. /**
  9232. * Get the series' color
  9233. */
  9234. getColor: function () {
  9235. var defaultColors = this.chart.options.colors,
  9236. counters = this.chart.counters;
  9237. this.color = this.options.color || defaultColors[counters.color++] || '#0000ff';
  9238. counters.wrapColor(defaultColors.length);
  9239. },
  9240. /**
  9241. * Get the series' symbol
  9242. */
  9243. getSymbol: function () {
  9244. var series = this,
  9245. seriesMarkerOption = series.options.marker,
  9246. chart = series.chart,
  9247. defaultSymbols = chart.options.symbols,
  9248. counters = chart.counters;
  9249. series.symbol = seriesMarkerOption.symbol || defaultSymbols[counters.symbol++];
  9250. // don't substract radius in image symbols (#604)
  9251. if (/^url/.test(series.symbol)) {
  9252. seriesMarkerOption.radius = 0;
  9253. }
  9254. counters.wrapSymbol(defaultSymbols.length);
  9255. },
  9256. /**
  9257. * Add a point dynamically after chart load time
  9258. * @param {Object} options Point options as given in series.data
  9259. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  9260. * @param {Boolean} shift If shift is true, a point is shifted off the start
  9261. * of the series as one is appended to the end.
  9262. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  9263. * configuration
  9264. */
  9265. addPoint: function (options, redraw, shift, animation) {
  9266. var series = this,
  9267. data = series.data,
  9268. graph = series.graph,
  9269. area = series.area,
  9270. chart = series.chart,
  9271. xData = series.xData,
  9272. yData = series.yData,
  9273. currentShift = (graph && graph.shift) || 0,
  9274. dataOptions = series.options.data,
  9275. point;
  9276. //point = (new series.pointClass()).init(series, options);
  9277. setAnimation(animation, chart);
  9278. // Make graph animate sideways
  9279. if (graph && shift) {
  9280. graph.shift = currentShift + 1;
  9281. }
  9282. if (area) {
  9283. if (shift) { // #780
  9284. area.shift = currentShift + 1;
  9285. }
  9286. area.isArea = true; // needed in animation, both with and without shift
  9287. }
  9288. // Optional redraw, defaults to true
  9289. redraw = pick(redraw, true);
  9290. // Get options and push the point to xData, yData and series.options. In series.generatePoints
  9291. // the Point instance will be created on demand and pushed to the series.data array.
  9292. point = { series: series };
  9293. series.pointClass.prototype.applyOptions.apply(point, [options]);
  9294. xData.push(point.x);
  9295. yData.push(series.valueCount === 4 ? [point.open, point.high, point.low, point.close] : point.y);
  9296. dataOptions.push(options);
  9297. // Shift the first point off the parallel arrays
  9298. // todo: consider series.removePoint(i) method
  9299. if (shift) {
  9300. if (data[0] && data[0].remove) {
  9301. data[0].remove(false);
  9302. } else {
  9303. data.shift();
  9304. xData.shift();
  9305. yData.shift();
  9306. dataOptions.shift();
  9307. }
  9308. }
  9309. series.getAttribs();
  9310. // redraw
  9311. series.isDirty = true;
  9312. series.isDirtyData = true;
  9313. if (redraw) {
  9314. chart.redraw();
  9315. }
  9316. },
  9317. /**
  9318. * Replace the series data with a new set of data
  9319. * @param {Object} data
  9320. * @param {Object} redraw
  9321. */
  9322. setData: function (data, redraw) {
  9323. var series = this,
  9324. oldData = series.points,
  9325. options = series.options,
  9326. initialColor = series.initialColor,
  9327. chart = series.chart,
  9328. firstPoint = null,
  9329. xAxis = series.xAxis,
  9330. i;
  9331. // reset properties
  9332. series.xIncrement = null;
  9333. series.pointRange = (xAxis && xAxis.categories && 1) || options.pointRange;
  9334. if (defined(initialColor)) { // reset colors for pie
  9335. chart.counters.color = initialColor;
  9336. }
  9337. // parallel arrays
  9338. var xData = [],
  9339. yData = [],
  9340. dataLength = data ? data.length : [],
  9341. turboThreshold = options.turboThreshold || 1000,
  9342. pt,
  9343. ohlc = series.valueCount === 4;
  9344. // In turbo mode, only one- or twodimensional arrays of numbers are allowed. The
  9345. // first value is tested, and we assume that all the rest are defined the same
  9346. // way. Although the 'for' loops are similar, they are repeated inside each
  9347. // if-else conditional for max performance.
  9348. if (dataLength > turboThreshold) {
  9349. // find the first non-null point
  9350. i = 0;
  9351. while (firstPoint === null && i < dataLength) {
  9352. firstPoint = data[i];
  9353. i++;
  9354. }
  9355. if (isNumber(firstPoint)) { // assume all points are numbers
  9356. var x = pick(options.pointStart, 0),
  9357. pointInterval = pick(options.pointInterval, 1);
  9358. for (i = 0; i < dataLength; i++) {
  9359. xData[i] = x;
  9360. yData[i] = data[i];
  9361. x += pointInterval;
  9362. }
  9363. series.xIncrement = x;
  9364. } else if (isArray(firstPoint)) { // assume all points are arrays
  9365. if (ohlc) { // [x, o, h, l, c]
  9366. for (i = 0; i < dataLength; i++) {
  9367. pt = data[i];
  9368. xData[i] = pt[0];
  9369. yData[i] = pt.slice(1, 5);
  9370. }
  9371. } else { // [x, y]
  9372. for (i = 0; i < dataLength; i++) {
  9373. pt = data[i];
  9374. xData[i] = pt[0];
  9375. yData[i] = pt[1];
  9376. }
  9377. }
  9378. } /* else {
  9379. error(12); // Highcharts expects configs to be numbers or arrays in turbo mode
  9380. }*/
  9381. } else {
  9382. for (i = 0; i < dataLength; i++) {
  9383. pt = { series: series };
  9384. series.pointClass.prototype.applyOptions.apply(pt, [data[i]]);
  9385. xData[i] = pt.x;
  9386. yData[i] = ohlc ? [pt.open, pt.high, pt.low, pt.close] : pt.y;
  9387. }
  9388. }
  9389. series.data = [];
  9390. series.options.data = data;
  9391. series.xData = xData;
  9392. series.yData = yData;
  9393. // destroy old points
  9394. i = (oldData && oldData.length) || 0;
  9395. while (i--) {
  9396. if (oldData[i] && oldData[i].destroy) {
  9397. oldData[i].destroy();
  9398. }
  9399. }
  9400. // reset minRange (#878)
  9401. // TODO: In protofy, run this code instead:
  9402. // if (xAxis) xAxis.minRange = UNDEFINED;
  9403. if (xAxis && xAxis.setMinRange) {
  9404. xAxis.setMinRange(); // to undefined
  9405. }
  9406. // redraw
  9407. series.isDirty = series.isDirtyData = chart.isDirtyBox = true;
  9408. if (pick(redraw, true)) {
  9409. chart.redraw(false);
  9410. }
  9411. },
  9412. /**
  9413. * Remove a series and optionally redraw the chart
  9414. *
  9415. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  9416. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  9417. * configuration
  9418. */
  9419. remove: function (redraw, animation) {
  9420. var series = this,
  9421. chart = series.chart;
  9422. redraw = pick(redraw, true);
  9423. if (!series.isRemoving) { /* prevent triggering native event in jQuery
  9424. (calling the remove function from the remove event) */
  9425. series.isRemoving = true;
  9426. // fire the event with a default handler of removing the point
  9427. fireEvent(series, 'remove', null, function () {
  9428. // destroy elements
  9429. series.destroy();
  9430. // redraw
  9431. chart.isDirtyLegend = chart.isDirtyBox = true;
  9432. if (redraw) {
  9433. chart.redraw(animation);
  9434. }
  9435. });
  9436. }
  9437. series.isRemoving = false;
  9438. },
  9439. /**
  9440. * Process the data by cropping away unused data points if the series is longer
  9441. * than the crop threshold. This saves computing time for lage series.
  9442. */
  9443. processData: function (force) {
  9444. var series = this,
  9445. processedXData = series.xData, // copied during slice operation below
  9446. processedYData = series.yData,
  9447. dataLength = processedXData.length,
  9448. cropStart = 0,
  9449. cropEnd = dataLength,
  9450. cropped,
  9451. distance,
  9452. closestPointRange,
  9453. xAxis = series.xAxis,
  9454. i, // loop variable
  9455. options = series.options,
  9456. cropThreshold = options.cropThreshold,
  9457. isCartesian = series.isCartesian;
  9458. // If the series data or axes haven't changed, don't go through this. Return false to pass
  9459. // the message on to override methods like in data grouping.
  9460. if (isCartesian && !series.isDirty && !xAxis.isDirty && !series.yAxis.isDirty && !force) {
  9461. return false;
  9462. }
  9463. // optionally filter out points outside the plot area
  9464. if (isCartesian && series.sorted && (!cropThreshold || dataLength > cropThreshold || series.forceCrop)) {
  9465. var extremes = xAxis.getExtremes(),
  9466. min = extremes.min,
  9467. max = extremes.max;
  9468. // it's outside current extremes
  9469. if (processedXData[dataLength - 1] < min || processedXData[0] > max) {
  9470. processedXData = [];
  9471. processedYData = [];
  9472. // only crop if it's actually spilling out
  9473. } else if (processedXData[0] < min || processedXData[dataLength - 1] > max) {
  9474. // iterate up to find slice start
  9475. for (i = 0; i < dataLength; i++) {
  9476. if (processedXData[i] >= min) {
  9477. cropStart = mathMax(0, i - 1);
  9478. break;
  9479. }
  9480. }
  9481. // proceed to find slice end
  9482. for (; i < dataLength; i++) {
  9483. if (processedXData[i] > max) {
  9484. cropEnd = i + 1;
  9485. break;
  9486. }
  9487. }
  9488. processedXData = processedXData.slice(cropStart, cropEnd);
  9489. processedYData = processedYData.slice(cropStart, cropEnd);
  9490. cropped = true;
  9491. }
  9492. }
  9493. // Find the closest distance between processed points
  9494. for (i = processedXData.length - 1; i > 0; i--) {
  9495. distance = processedXData[i] - processedXData[i - 1];
  9496. if (distance > 0 && (closestPointRange === UNDEFINED || distance < closestPointRange)) {
  9497. closestPointRange = distance;
  9498. }
  9499. }
  9500. // Record the properties
  9501. series.cropped = cropped; // undefined or true
  9502. series.cropStart = cropStart;
  9503. series.processedXData = processedXData;
  9504. series.processedYData = processedYData;
  9505. if (options.pointRange === null) { // null means auto, as for columns, candlesticks and OHLC
  9506. series.pointRange = closestPointRange || 1;
  9507. }
  9508. series.closestPointRange = closestPointRange;
  9509. },
  9510. /**
  9511. * Generate the data point after the data has been processed by cropping away
  9512. * unused points and optionally grouped in Highcharts Stock.
  9513. */
  9514. generatePoints: function () {
  9515. var series = this,
  9516. options = series.options,
  9517. dataOptions = options.data,
  9518. data = series.data,
  9519. dataLength,
  9520. processedXData = series.processedXData,
  9521. processedYData = series.processedYData,
  9522. pointClass = series.pointClass,
  9523. processedDataLength = processedXData.length,
  9524. cropStart = series.cropStart || 0,
  9525. cursor,
  9526. hasGroupedData = series.hasGroupedData,
  9527. point,
  9528. points = [],
  9529. i;
  9530. if (!data && !hasGroupedData) {
  9531. var arr = [];
  9532. arr.length = dataOptions.length;
  9533. data = series.data = arr;
  9534. }
  9535. for (i = 0; i < processedDataLength; i++) {
  9536. cursor = cropStart + i;
  9537. if (!hasGroupedData) {
  9538. if (data[cursor]) {
  9539. point = data[cursor];
  9540. } else {
  9541. data[cursor] = point = (new pointClass()).init(series, dataOptions[cursor], processedXData[i]);
  9542. }
  9543. points[i] = point;
  9544. } else {
  9545. // splat the y data in case of ohlc data array
  9546. points[i] = (new pointClass()).init(series, [processedXData[i]].concat(splat(processedYData[i])));
  9547. }
  9548. }
  9549. // Hide cropped-away points - this only runs when the number of points is above cropThreshold, or when
  9550. // swithching view from non-grouped data to grouped data (#637)
  9551. if (data && (processedDataLength !== (dataLength = data.length) || hasGroupedData)) {
  9552. for (i = 0; i < dataLength; i++) {
  9553. if (i === cropStart && !hasGroupedData) { // when has grouped data, clear all points
  9554. i += processedDataLength;
  9555. }
  9556. if (data[i]) {
  9557. data[i].destroyElements();
  9558. }
  9559. }
  9560. }
  9561. series.data = data;
  9562. series.points = points;
  9563. },
  9564. /**
  9565. * Translate data points from raw data values to chart specific positioning data
  9566. * needed later in drawPoints, drawGraph and drawTracker.
  9567. */
  9568. translate: function () {
  9569. if (!this.processedXData) { // hidden series
  9570. this.processData();
  9571. }
  9572. this.generatePoints();
  9573. var series = this,
  9574. chart = series.chart,
  9575. options = series.options,
  9576. stacking = options.stacking,
  9577. xAxis = series.xAxis,
  9578. categories = xAxis.categories,
  9579. yAxis = series.yAxis,
  9580. points = series.points,
  9581. dataLength = points.length,
  9582. hasModifyValue = !!series.modifyValue,
  9583. isLastSeries,
  9584. allStackSeries = yAxis.series,
  9585. i = allStackSeries.length;
  9586. // Is it the last visible series?
  9587. while (i--) {
  9588. if (allStackSeries[i].visible) {
  9589. if (i === series.index) {
  9590. isLastSeries = true;
  9591. }
  9592. break;
  9593. }
  9594. }
  9595. // Translate each point
  9596. for (i = 0; i < dataLength; i++) {
  9597. var point = points[i],
  9598. xValue = point.x,
  9599. yValue = point.y,
  9600. yBottom = point.low,
  9601. stack = yAxis.stacks[(yValue < options.threshold ? '-' : '') + series.stackKey],
  9602. pointStack,
  9603. pointStackTotal;
  9604. // get the plotX translation
  9605. point.plotX = mathRound(xAxis.translate(xValue, 0, 0, 0, 1) * 10) / 10; // Math.round fixes #591
  9606. // calculate the bottom y value for stacked series
  9607. if (stacking && series.visible && stack && stack[xValue]) {
  9608. pointStack = stack[xValue];
  9609. pointStackTotal = pointStack.total;
  9610. pointStack.cum = yBottom = pointStack.cum - yValue; // start from top
  9611. yValue = yBottom + yValue;
  9612. if (isLastSeries) {
  9613. yBottom = options.threshold;
  9614. }
  9615. if (stacking === 'percent') {
  9616. yBottom = pointStackTotal ? yBottom * 100 / pointStackTotal : 0;
  9617. yValue = pointStackTotal ? yValue * 100 / pointStackTotal : 0;
  9618. }
  9619. point.percentage = pointStackTotal ? point.y * 100 / pointStackTotal : 0;
  9620. point.stackTotal = pointStackTotal;
  9621. point.stackY = yValue;
  9622. }
  9623. // Set translated yBottom or remove it
  9624. point.yBottom = defined(yBottom) ?
  9625. yAxis.translate(yBottom, 0, 1, 0, 1) :
  9626. null;
  9627. // general hook, used for Highstock compare mode
  9628. if (hasModifyValue) {
  9629. yValue = series.modifyValue(yValue, point);
  9630. }
  9631. // Set the the plotY value, reset it for redraws
  9632. point.plotY = (typeof yValue === 'number') ?
  9633. mathRound(yAxis.translate(yValue, 0, 1, 0, 1) * 10) / 10 : // Math.round fixes #591
  9634. UNDEFINED;
  9635. // set client related positions for mouse tracking
  9636. point.clientX = chart.inverted ?
  9637. chart.plotHeight - point.plotX :
  9638. point.plotX; // for mouse tracking
  9639. // some API data
  9640. point.category = categories && categories[point.x] !== UNDEFINED ?
  9641. categories[point.x] : point.x;
  9642. }
  9643. // now that we have the cropped data, build the segments
  9644. series.getSegments();
  9645. },
  9646. /**
  9647. * Memoize tooltip texts and positions
  9648. */
  9649. setTooltipPoints: function (renew) {
  9650. var series = this,
  9651. chart = series.chart,
  9652. points = [],
  9653. pointsLength,
  9654. plotSize = chart.plotSizeX,
  9655. low,
  9656. high,
  9657. xAxis = series.xAxis,
  9658. point,
  9659. i,
  9660. tooltipPoints = []; // a lookup array for each pixel in the x dimension
  9661. // don't waste resources if tracker is disabled
  9662. if (series.options.enableMouseTracking === false) {
  9663. return;
  9664. }
  9665. // renew
  9666. if (renew) {
  9667. series.tooltipPoints = null;
  9668. }
  9669. // concat segments to overcome null values
  9670. each(series.segments || series.points, function (segment) {
  9671. points = points.concat(segment);
  9672. });
  9673. // loop the concatenated points and apply each point to all the closest
  9674. // pixel positions
  9675. if (xAxis && xAxis.reversed) {
  9676. points = points.reverse();
  9677. }
  9678. // Assign each pixel position to the nearest point
  9679. pointsLength = points.length;
  9680. for (i = 0; i < pointsLength; i++) {
  9681. point = points[i];
  9682. low = points[i - 1] ? points[i - 1]._high + 1 : 0;
  9683. high = point._high = points[i + 1] ?
  9684. (mathFloor((point.plotX + (points[i + 1] ? points[i + 1].plotX : plotSize)) / 2)) :
  9685. plotSize;
  9686. while (low <= high) {
  9687. tooltipPoints[low++] = point;
  9688. }
  9689. }
  9690. series.tooltipPoints = tooltipPoints;
  9691. },
  9692. /**
  9693. * Format the header of the tooltip
  9694. */
  9695. tooltipHeaderFormatter: function (key) {
  9696. var series = this,
  9697. tooltipOptions = series.tooltipOptions,
  9698. xDateFormat = tooltipOptions.xDateFormat || '%A, %b %e, %Y',
  9699. xAxis = series.xAxis,
  9700. isDateTime = xAxis && xAxis.options.type === 'datetime';
  9701. return tooltipOptions.headerFormat
  9702. .replace('{point.key}', isDateTime ? dateFormat(xDateFormat, key) : key)
  9703. .replace('{series.name}', series.name)
  9704. .replace('{series.color}', series.color);
  9705. },
  9706. /**
  9707. * Series mouse over handler
  9708. */
  9709. onMouseOver: function () {
  9710. var series = this,
  9711. chart = series.chart,
  9712. hoverSeries = chart.hoverSeries;
  9713. if (!hasTouch && chart.mouseIsDown) {
  9714. return;
  9715. }
  9716. // set normal state to previous series
  9717. if (hoverSeries && hoverSeries !== series) {
  9718. hoverSeries.onMouseOut();
  9719. }
  9720. // trigger the event, but to save processing time,
  9721. // only if defined
  9722. if (series.options.events.mouseOver) {
  9723. fireEvent(series, 'mouseOver');
  9724. }
  9725. // hover this
  9726. series.setState(HOVER_STATE);
  9727. chart.hoverSeries = series;
  9728. },
  9729. /**
  9730. * Series mouse out handler
  9731. */
  9732. onMouseOut: function () {
  9733. // trigger the event only if listeners exist
  9734. var series = this,
  9735. options = series.options,
  9736. chart = series.chart,
  9737. tooltip = chart.tooltip,
  9738. hoverPoint = chart.hoverPoint;
  9739. // trigger mouse out on the point, which must be in this series
  9740. if (hoverPoint) {
  9741. hoverPoint.onMouseOut();
  9742. }
  9743. // fire the mouse out event
  9744. if (series && options.events.mouseOut) {
  9745. fireEvent(series, 'mouseOut');
  9746. }
  9747. // hide the tooltip
  9748. if (tooltip && !options.stickyTracking && !tooltip.shared) {
  9749. tooltip.hide();
  9750. }
  9751. // set normal state
  9752. series.setState();
  9753. chart.hoverSeries = null;
  9754. },
  9755. /**
  9756. * Animate in the series
  9757. */
  9758. animate: function (init) {
  9759. var series = this,
  9760. chart = series.chart,
  9761. clipRect = series.clipRect,
  9762. animation = series.options.animation;
  9763. if (animation && !isObject(animation)) {
  9764. animation = {};
  9765. }
  9766. if (init) { // initialize the animation
  9767. if (!clipRect.isAnimating) { // apply it only for one of the series
  9768. clipRect.attr('width', 0);
  9769. clipRect.isAnimating = true;
  9770. }
  9771. } else { // run the animation
  9772. clipRect.animate({
  9773. width: chart.plotSizeX
  9774. }, animation);
  9775. // delete this function to allow it only once
  9776. this.animate = null;
  9777. }
  9778. },
  9779. /**
  9780. * Draw the markers
  9781. */
  9782. drawPoints: function () {
  9783. var series = this,
  9784. pointAttr,
  9785. points = series.points,
  9786. chart = series.chart,
  9787. plotX,
  9788. plotY,
  9789. i,
  9790. point,
  9791. radius,
  9792. symbol,
  9793. isImage,
  9794. graphic;
  9795. if (series.options.marker.enabled) {
  9796. i = points.length;
  9797. while (i--) {
  9798. point = points[i];
  9799. plotX = point.plotX;
  9800. plotY = point.plotY;
  9801. graphic = point.graphic;
  9802. // only draw the point if y is defined
  9803. if (plotY !== UNDEFINED && !isNaN(plotY)) {
  9804. // shortcuts
  9805. pointAttr = point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE];
  9806. radius = pointAttr.r;
  9807. symbol = pick(point.marker && point.marker.symbol, series.symbol);
  9808. isImage = symbol.indexOf('url') === 0;
  9809. if (graphic) { // update
  9810. graphic.animate(extend({
  9811. x: plotX - radius,
  9812. y: plotY - radius
  9813. }, graphic.symbolName ? { // don't apply to image symbols #507
  9814. width: 2 * radius,
  9815. height: 2 * radius
  9816. } : {}));
  9817. } else if (radius > 0 || isImage) {
  9818. point.graphic = chart.renderer.symbol(
  9819. symbol,
  9820. plotX - radius,
  9821. plotY - radius,
  9822. 2 * radius,
  9823. 2 * radius
  9824. )
  9825. .attr(pointAttr)
  9826. .add(series.group);
  9827. }
  9828. }
  9829. }
  9830. }
  9831. },
  9832. /**
  9833. * Convert state properties from API naming conventions to SVG attributes
  9834. *
  9835. * @param {Object} options API options object
  9836. * @param {Object} base1 SVG attribute object to inherit from
  9837. * @param {Object} base2 Second level SVG attribute object to inherit from
  9838. */
  9839. convertAttribs: function (options, base1, base2, base3) {
  9840. var conversion = this.pointAttrToOptions,
  9841. attr,
  9842. option,
  9843. obj = {};
  9844. options = options || {};
  9845. base1 = base1 || {};
  9846. base2 = base2 || {};
  9847. base3 = base3 || {};
  9848. for (attr in conversion) {
  9849. option = conversion[attr];
  9850. obj[attr] = pick(options[option], base1[attr], base2[attr], base3[attr]);
  9851. }
  9852. return obj;
  9853. },
  9854. /**
  9855. * Get the state attributes. Each series type has its own set of attributes
  9856. * that are allowed to change on a point's state change. Series wide attributes are stored for
  9857. * all series, and additionally point specific attributes are stored for all
  9858. * points with individual marker options. If such options are not defined for the point,
  9859. * a reference to the series wide attributes is stored in point.pointAttr.
  9860. */
  9861. getAttribs: function () {
  9862. var series = this,
  9863. normalOptions = defaultPlotOptions[series.type].marker ? series.options.marker : series.options,
  9864. stateOptions = normalOptions.states,
  9865. stateOptionsHover = stateOptions[HOVER_STATE],
  9866. pointStateOptionsHover,
  9867. seriesColor = series.color,
  9868. normalDefaults = {
  9869. stroke: seriesColor,
  9870. fill: seriesColor
  9871. },
  9872. points = series.points,
  9873. i,
  9874. point,
  9875. seriesPointAttr = [],
  9876. pointAttr,
  9877. pointAttrToOptions = series.pointAttrToOptions,
  9878. hasPointSpecificOptions,
  9879. key;
  9880. // series type specific modifications
  9881. if (series.options.marker) { // line, spline, area, areaspline, scatter
  9882. // if no hover radius is given, default to normal radius + 2
  9883. stateOptionsHover.radius = stateOptionsHover.radius || normalOptions.radius + 2;
  9884. stateOptionsHover.lineWidth = stateOptionsHover.lineWidth || normalOptions.lineWidth + 1;
  9885. } else { // column, bar, pie
  9886. // if no hover color is given, brighten the normal color
  9887. stateOptionsHover.color = stateOptionsHover.color ||
  9888. Color(stateOptionsHover.color || seriesColor)
  9889. .brighten(stateOptionsHover.brightness).get();
  9890. }
  9891. // general point attributes for the series normal state
  9892. seriesPointAttr[NORMAL_STATE] = series.convertAttribs(normalOptions, normalDefaults);
  9893. // HOVER_STATE and SELECT_STATE states inherit from normal state except the default radius
  9894. each([HOVER_STATE, SELECT_STATE], function (state) {
  9895. seriesPointAttr[state] =
  9896. series.convertAttribs(stateOptions[state], seriesPointAttr[NORMAL_STATE]);
  9897. });
  9898. // set it
  9899. series.pointAttr = seriesPointAttr;
  9900. // Generate the point-specific attribute collections if specific point
  9901. // options are given. If not, create a referance to the series wide point
  9902. // attributes
  9903. i = points.length;
  9904. while (i--) {
  9905. point = points[i];
  9906. normalOptions = (point.options && point.options.marker) || point.options;
  9907. if (normalOptions && normalOptions.enabled === false) {
  9908. normalOptions.radius = 0;
  9909. }
  9910. hasPointSpecificOptions = false;
  9911. // check if the point has specific visual options
  9912. if (point.options) {
  9913. for (key in pointAttrToOptions) {
  9914. if (defined(normalOptions[pointAttrToOptions[key]])) {
  9915. hasPointSpecificOptions = true;
  9916. }
  9917. }
  9918. }
  9919. // a specific marker config object is defined for the individual point:
  9920. // create it's own attribute collection
  9921. if (hasPointSpecificOptions) {
  9922. pointAttr = [];
  9923. stateOptions = normalOptions.states || {}; // reassign for individual point
  9924. pointStateOptionsHover = stateOptions[HOVER_STATE] = stateOptions[HOVER_STATE] || {};
  9925. // if no hover color is given, brighten the normal color
  9926. if (!series.options.marker) { // column, bar, point
  9927. pointStateOptionsHover.color =
  9928. Color(pointStateOptionsHover.color || point.options.color)
  9929. .brighten(pointStateOptionsHover.brightness ||
  9930. stateOptionsHover.brightness).get();
  9931. }
  9932. // normal point state inherits series wide normal state
  9933. pointAttr[NORMAL_STATE] = series.convertAttribs(normalOptions, seriesPointAttr[NORMAL_STATE]);
  9934. // inherit from point normal and series hover
  9935. pointAttr[HOVER_STATE] = series.convertAttribs(
  9936. stateOptions[HOVER_STATE],
  9937. seriesPointAttr[HOVER_STATE],
  9938. pointAttr[NORMAL_STATE]
  9939. );
  9940. // inherit from point normal and series hover
  9941. pointAttr[SELECT_STATE] = series.convertAttribs(
  9942. stateOptions[SELECT_STATE],
  9943. seriesPointAttr[SELECT_STATE],
  9944. pointAttr[NORMAL_STATE]
  9945. );
  9946. // no marker config object is created: copy a reference to the series-wide
  9947. // attribute collection
  9948. } else {
  9949. pointAttr = seriesPointAttr;
  9950. }
  9951. point.pointAttr = pointAttr;
  9952. }
  9953. },
  9954. /**
  9955. * Clear DOM objects and free up memory
  9956. */
  9957. destroy: function () {
  9958. var series = this,
  9959. chart = series.chart,
  9960. seriesClipRect = series.clipRect,
  9961. issue134 = /AppleWebKit\/533/.test(userAgent),
  9962. destroy,
  9963. i,
  9964. data = series.data || [],
  9965. point,
  9966. prop,
  9967. axis;
  9968. // add event hook
  9969. fireEvent(series, 'destroy');
  9970. // remove all events
  9971. removeEvent(series);
  9972. // erase from axes
  9973. each(['xAxis', 'yAxis'], function (AXIS) {
  9974. axis = series[AXIS];
  9975. if (axis) {
  9976. erase(axis.series, series);
  9977. axis.isDirty = true;
  9978. }
  9979. });
  9980. // remove legend items
  9981. if (series.legendItem) {
  9982. series.chart.legend.destroyItem(series);
  9983. }
  9984. // destroy all points with their elements
  9985. i = data.length;
  9986. while (i--) {
  9987. point = data[i];
  9988. if (point && point.destroy) {
  9989. point.destroy();
  9990. }
  9991. }
  9992. series.points = null;
  9993. // If this series clipRect is not the global one (which is removed on chart.destroy) we
  9994. // destroy it here.
  9995. if (seriesClipRect && seriesClipRect !== chart.clipRect) {
  9996. series.clipRect = seriesClipRect.destroy();
  9997. }
  9998. // destroy all SVGElements associated to the series
  9999. each(['area', 'graph', 'dataLabelsGroup', 'group', 'tracker'], function (prop) {
  10000. if (series[prop]) {
  10001. // issue 134 workaround
  10002. destroy = issue134 && prop === 'group' ?
  10003. 'hide' :
  10004. 'destroy';
  10005. series[prop][destroy]();
  10006. }
  10007. });
  10008. // remove from hoverSeries
  10009. if (chart.hoverSeries === series) {
  10010. chart.hoverSeries = null;
  10011. }
  10012. erase(chart.series, series);
  10013. // clear all members
  10014. for (prop in series) {
  10015. delete series[prop];
  10016. }
  10017. },
  10018. /**
  10019. * Draw the data labels
  10020. */
  10021. drawDataLabels: function () {
  10022. var series = this,
  10023. seriesOptions = series.options,
  10024. options = seriesOptions.dataLabels;
  10025. if (options.enabled || series._hasPointLabels) {
  10026. var x,
  10027. y,
  10028. points = series.points,
  10029. pointOptions,
  10030. generalOptions,
  10031. str,
  10032. dataLabelsGroup = series.dataLabelsGroup,
  10033. chart = series.chart,
  10034. xAxis = series.xAxis,
  10035. groupLeft = xAxis ? xAxis.left : chart.plotLeft,
  10036. yAxis = series.yAxis,
  10037. groupTop = yAxis ? yAxis.top : chart.plotTop,
  10038. renderer = chart.renderer,
  10039. inverted = chart.inverted,
  10040. seriesType = series.type,
  10041. stacking = seriesOptions.stacking,
  10042. isBarLike = seriesType === 'column' || seriesType === 'bar',
  10043. vAlignIsNull = options.verticalAlign === null,
  10044. yIsNull = options.y === null,
  10045. fontMetrics = renderer.fontMetrics(options.style.fontSize), // height and baseline
  10046. fontLineHeight = fontMetrics.h,
  10047. fontBaseline = fontMetrics.b,
  10048. dataLabel,
  10049. enabled;
  10050. if (isBarLike) {
  10051. var defaultYs = {
  10052. top: fontBaseline,
  10053. middle: fontBaseline - fontLineHeight / 2,
  10054. bottom: -fontLineHeight + fontBaseline
  10055. };
  10056. if (stacking) {
  10057. // In stacked series the default label placement is inside the bars
  10058. if (vAlignIsNull) {
  10059. options = merge(options, {verticalAlign: 'middle'});
  10060. }
  10061. // If no y delta is specified, try to create a good default
  10062. if (yIsNull) {
  10063. options = merge(options, { y: defaultYs[options.verticalAlign]});
  10064. }
  10065. } else {
  10066. // In non stacked series the default label placement is on top of the bars
  10067. if (vAlignIsNull) {
  10068. options = merge(options, {verticalAlign: 'top'});
  10069. // If no y delta is specified, try to create a good default (like default bar)
  10070. } else if (yIsNull) {
  10071. options = merge(options, { y: defaultYs[options.verticalAlign]});
  10072. }
  10073. }
  10074. }
  10075. // create a separate group for the data labels to avoid rotation
  10076. if (!dataLabelsGroup) {
  10077. dataLabelsGroup = series.dataLabelsGroup =
  10078. renderer.g('data-labels')
  10079. .attr({
  10080. visibility: series.visible ? VISIBLE : HIDDEN,
  10081. zIndex: 6
  10082. })
  10083. .translate(groupLeft, groupTop)
  10084. .add();
  10085. } else {
  10086. dataLabelsGroup.translate(groupLeft, groupTop);
  10087. }
  10088. // make the labels for each point
  10089. generalOptions = options;
  10090. each(points, function (point) {
  10091. dataLabel = point.dataLabel;
  10092. // Merge in individual options from point
  10093. options = generalOptions; // reset changes from previous points
  10094. pointOptions = point.options;
  10095. if (pointOptions && pointOptions.dataLabels) {
  10096. options = merge(options, pointOptions.dataLabels);
  10097. }
  10098. enabled = options.enabled;
  10099. // Get the positions
  10100. if (enabled) {
  10101. var plotX = (point.barX && point.barX + point.barW / 2) || pick(point.plotX, -999),
  10102. plotY = pick(point.plotY, -999),
  10103. // if options.y is null, which happens by default on column charts, set the position
  10104. // above or below the column depending on the threshold
  10105. individualYDelta = options.y === null ?
  10106. (point.y >= seriesOptions.threshold ?
  10107. -fontLineHeight + fontBaseline : // below the threshold
  10108. fontBaseline) : // above the threshold
  10109. options.y;
  10110. x = (inverted ? chart.plotWidth - plotY : plotX) + options.x;
  10111. y = mathRound((inverted ? chart.plotHeight - plotX : plotY) + individualYDelta);
  10112. }
  10113. // If the point is outside the plot area, destroy it. #678, #820
  10114. if (dataLabel && series.isCartesian && (!chart.isInsidePlot(x, y) || !enabled)) {
  10115. point.dataLabel = dataLabel.destroy();
  10116. // Individual labels are disabled if the are explicitly disabled
  10117. // in the point options, or if they fall outside the plot area.
  10118. } else if (enabled) {
  10119. var align = options.align;
  10120. // Get the string
  10121. str = options.formatter.call(point.getLabelConfig(), options);
  10122. // in columns, align the string to the column
  10123. if (seriesType === 'column') {
  10124. x += { left: -1, right: 1 }[align] * point.barW / 2 || 0;
  10125. }
  10126. if (!stacking && inverted && point.y < 0) {
  10127. align = 'right';
  10128. x -= 10;
  10129. }
  10130. // Determine the color
  10131. options.style.color = pick(options.color, options.style.color, series.color, 'black');
  10132. // update existing label
  10133. if (dataLabel) {
  10134. // vertically centered
  10135. dataLabel
  10136. .attr({
  10137. text: str
  10138. }).animate({
  10139. x: x,
  10140. y: y
  10141. });
  10142. // create new label
  10143. } else if (defined(str)) {
  10144. dataLabel = point.dataLabel = renderer[options.rotation ? 'text' : 'label']( // labels don't support rotation
  10145. str,
  10146. x,
  10147. y,
  10148. null,
  10149. null,
  10150. null,
  10151. options.useHTML,
  10152. true // baseline for backwards compat
  10153. )
  10154. .attr({
  10155. align: align,
  10156. fill: options.backgroundColor,
  10157. stroke: options.borderColor,
  10158. 'stroke-width': options.borderWidth,
  10159. r: options.borderRadius || 0,
  10160. rotation: options.rotation,
  10161. padding: options.padding,
  10162. zIndex: 1
  10163. })
  10164. .css(options.style)
  10165. .add(dataLabelsGroup)
  10166. .shadow(options.shadow);
  10167. }
  10168. if (isBarLike && seriesOptions.stacking && dataLabel) {
  10169. var barX = point.barX,
  10170. barY = point.barY,
  10171. barW = point.barW,
  10172. barH = point.barH;
  10173. dataLabel.align(options, null,
  10174. {
  10175. x: inverted ? chart.plotWidth - barY - barH : barX,
  10176. y: inverted ? chart.plotHeight - barX - barW : barY,
  10177. width: inverted ? barH : barW,
  10178. height: inverted ? barW : barH
  10179. });
  10180. }
  10181. }
  10182. });
  10183. }
  10184. },
  10185. /**
  10186. * Draw the actual graph
  10187. */
  10188. drawGraph: function () {
  10189. var series = this,
  10190. options = series.options,
  10191. chart = series.chart,
  10192. graph = series.graph,
  10193. graphPath = [],
  10194. fillColor,
  10195. area = series.area,
  10196. group = series.group,
  10197. color = options.lineColor || series.color,
  10198. lineWidth = options.lineWidth,
  10199. dashStyle = options.dashStyle,
  10200. segmentPath,
  10201. renderer = chart.renderer,
  10202. translatedThreshold = series.yAxis.getThreshold(options.threshold),
  10203. useArea = /^area/.test(series.type),
  10204. singlePoints = [], // used in drawTracker
  10205. areaPath = [],
  10206. attribs;
  10207. // divide into segments and build graph and area paths
  10208. each(series.segments, function (segment) {
  10209. segmentPath = [];
  10210. // build the segment line
  10211. each(segment, function (point, i) {
  10212. if (series.getPointSpline) { // generate the spline as defined in the SplineSeries object
  10213. segmentPath.push.apply(segmentPath, series.getPointSpline(segment, point, i));
  10214. } else {
  10215. // moveTo or lineTo
  10216. segmentPath.push(i ? L : M);
  10217. // step line?
  10218. if (i && options.step) {
  10219. var lastPoint = segment[i - 1];
  10220. segmentPath.push(
  10221. point.plotX,
  10222. lastPoint.plotY
  10223. );
  10224. }
  10225. // normal line to next point
  10226. segmentPath.push(
  10227. point.plotX,
  10228. point.plotY
  10229. );
  10230. }
  10231. });
  10232. // add the segment to the graph, or a single point for tracking
  10233. if (segment.length > 1) {
  10234. graphPath = graphPath.concat(segmentPath);
  10235. } else {
  10236. singlePoints.push(segment[0]);
  10237. }
  10238. // build the area
  10239. if (useArea) {
  10240. var areaSegmentPath = [],
  10241. i,
  10242. segLength = segmentPath.length;
  10243. for (i = 0; i < segLength; i++) {
  10244. areaSegmentPath.push(segmentPath[i]);
  10245. }
  10246. if (segLength === 3) { // for animation from 1 to two points
  10247. areaSegmentPath.push(L, segmentPath[1], segmentPath[2]);
  10248. }
  10249. if (options.stacking && series.type !== 'areaspline') {
  10250. // Follow stack back. Todo: implement areaspline. A general solution could be to
  10251. // reverse the entire graphPath of the previous series, though may be hard with
  10252. // splines and with series with different extremes
  10253. for (i = segment.length - 1; i >= 0; i--) {
  10254. // step line?
  10255. if (i < segment.length - 1 && options.step) {
  10256. areaSegmentPath.push(segment[i + 1].plotX, segment[i].yBottom);
  10257. }
  10258. areaSegmentPath.push(segment[i].plotX, segment[i].yBottom);
  10259. }
  10260. } else { // follow zero line back
  10261. areaSegmentPath.push(
  10262. L,
  10263. segment[segment.length - 1].plotX,
  10264. translatedThreshold,
  10265. L,
  10266. segment[0].plotX,
  10267. translatedThreshold
  10268. );
  10269. }
  10270. areaPath = areaPath.concat(areaSegmentPath);
  10271. }
  10272. });
  10273. // used in drawTracker:
  10274. series.graphPath = graphPath;
  10275. series.singlePoints = singlePoints;
  10276. // draw the area if area series or areaspline
  10277. if (useArea) {
  10278. fillColor = pick(
  10279. options.fillColor,
  10280. Color(series.color).setOpacity(options.fillOpacity || 0.75).get()
  10281. );
  10282. if (area) {
  10283. area.animate({ d: areaPath });
  10284. } else {
  10285. // draw the area
  10286. series.area = series.chart.renderer.path(areaPath)
  10287. .attr({
  10288. fill: fillColor
  10289. }).add(group);
  10290. }
  10291. }
  10292. // draw the graph
  10293. if (graph) {
  10294. stop(graph); // cancel running animations, #459
  10295. graph.animate({ d: graphPath });
  10296. } else {
  10297. if (lineWidth) {
  10298. attribs = {
  10299. 'stroke': color,
  10300. 'stroke-width': lineWidth
  10301. };
  10302. if (dashStyle) {
  10303. attribs.dashstyle = dashStyle;
  10304. }
  10305. series.graph = renderer.path(graphPath)
  10306. .attr(attribs).add(group).shadow(options.shadow);
  10307. }
  10308. }
  10309. },
  10310. /**
  10311. * Initialize and perform group inversion on series.group and series.trackerGroup
  10312. */
  10313. invertGroups: function () {
  10314. var series = this,
  10315. group = series.group,
  10316. trackerGroup = series.trackerGroup,
  10317. chart = series.chart;
  10318. // A fixed size is needed for inversion to work
  10319. function setInvert() {
  10320. var size = {
  10321. width: series.yAxis.len,
  10322. height: series.xAxis.len
  10323. };
  10324. // Set the series.group size
  10325. group.attr(size).invert();
  10326. // Set the tracker group size
  10327. if (trackerGroup) {
  10328. trackerGroup.attr(size).invert();
  10329. }
  10330. }
  10331. addEvent(chart, 'resize', setInvert); // do it on resize
  10332. addEvent(series, 'destroy', function () {
  10333. removeEvent(chart, 'resize', setInvert);
  10334. });
  10335. // Do it now
  10336. setInvert(); // do it now
  10337. // On subsequent render and redraw, just do setInvert without setting up events again
  10338. series.invertGroups = setInvert;
  10339. },
  10340. /**
  10341. * Render the graph and markers
  10342. */
  10343. render: function () {
  10344. var series = this,
  10345. chart = series.chart,
  10346. group,
  10347. options = series.options,
  10348. doClip = options.clip !== false,
  10349. animation = options.animation,
  10350. doAnimation = animation && series.animate,
  10351. duration = doAnimation ? (animation && animation.duration) || 500 : 0,
  10352. clipRect = series.clipRect,
  10353. renderer = chart.renderer;
  10354. // Add plot area clipping rectangle. If this is before chart.hasRendered,
  10355. // create one shared clipRect.
  10356. // Todo: since creating the clip property, the clipRect is created but
  10357. // never used when clip is false. A better way would be that the animation
  10358. // would run, then the clipRect destroyed.
  10359. if (!clipRect) {
  10360. clipRect = series.clipRect = !chart.hasRendered && chart.clipRect ?
  10361. chart.clipRect :
  10362. renderer.clipRect(0, 0, chart.plotSizeX, chart.plotSizeY + 1);
  10363. if (!chart.clipRect) {
  10364. chart.clipRect = clipRect;
  10365. }
  10366. }
  10367. // the group
  10368. if (!series.group) {
  10369. group = series.group = renderer.g('series');
  10370. group.attr({
  10371. visibility: series.visible ? VISIBLE : HIDDEN,
  10372. zIndex: options.zIndex
  10373. })
  10374. .translate(series.xAxis.left, series.yAxis.top)
  10375. .add(chart.seriesGroup);
  10376. }
  10377. series.drawDataLabels();
  10378. // initiate the animation
  10379. if (doAnimation) {
  10380. series.animate(true);
  10381. }
  10382. // cache attributes for shapes
  10383. series.getAttribs();
  10384. // draw the graph if any
  10385. if (series.drawGraph) {
  10386. series.drawGraph();
  10387. }
  10388. // draw the points
  10389. series.drawPoints();
  10390. // draw the mouse tracking area
  10391. if (series.options.enableMouseTracking !== false) {
  10392. series.drawTracker();
  10393. }
  10394. // Handle inverted series and tracker groups
  10395. if (chart.inverted) {
  10396. series.invertGroups();
  10397. }
  10398. // Do the initial clipping. This must be done after inverting for VML.
  10399. if (doClip && !series.hasRendered) {
  10400. group.clip(clipRect);
  10401. if (series.trackerGroup) {
  10402. series.trackerGroup.clip(chart.clipRect);
  10403. }
  10404. }
  10405. // run the animation
  10406. if (doAnimation) {
  10407. series.animate();
  10408. }
  10409. // finish the individual clipRect
  10410. setTimeout(function () {
  10411. clipRect.isAnimating = false;
  10412. group = series.group; // can be destroyed during the timeout
  10413. if (group && clipRect !== chart.clipRect && clipRect.renderer) {
  10414. if (doClip) {
  10415. group.clip((series.clipRect = chart.clipRect));
  10416. }
  10417. clipRect.destroy();
  10418. }
  10419. }, duration);
  10420. series.isDirty = series.isDirtyData = false; // means data is in accordance with what you see
  10421. // (See #322) series.isDirty = series.isDirtyData = false; // means data is in accordance with what you see
  10422. series.hasRendered = true;
  10423. },
  10424. /**
  10425. * Redraw the series after an update in the axes.
  10426. */
  10427. redraw: function () {
  10428. var series = this,
  10429. chart = series.chart,
  10430. wasDirtyData = series.isDirtyData, // cache it here as it is set to false in render, but used after
  10431. group = series.group;
  10432. // reposition on resize
  10433. if (group) {
  10434. if (chart.inverted) {
  10435. group.attr({
  10436. width: chart.plotWidth,
  10437. height: chart.plotHeight
  10438. });
  10439. }
  10440. group.animate({
  10441. translateX: series.xAxis.left,
  10442. translateY: series.yAxis.top
  10443. });
  10444. }
  10445. series.translate();
  10446. series.setTooltipPoints(true);
  10447. series.render();
  10448. if (wasDirtyData) {
  10449. fireEvent(series, 'updatedData');
  10450. }
  10451. },
  10452. /**
  10453. * Set the state of the graph
  10454. */
  10455. setState: function (state) {
  10456. var series = this,
  10457. options = series.options,
  10458. graph = series.graph,
  10459. stateOptions = options.states,
  10460. lineWidth = options.lineWidth;
  10461. state = state || NORMAL_STATE;
  10462. if (series.state !== state) {
  10463. series.state = state;
  10464. if (stateOptions[state] && stateOptions[state].enabled === false) {
  10465. return;
  10466. }
  10467. if (state) {
  10468. lineWidth = stateOptions[state].lineWidth || lineWidth + 1;
  10469. }
  10470. if (graph && !graph.dashstyle) { // hover is turned off for dashed lines in VML
  10471. graph.attr({ // use attr because animate will cause any other animation on the graph to stop
  10472. 'stroke-width': lineWidth
  10473. }, state ? 0 : 500);
  10474. }
  10475. }
  10476. },
  10477. /**
  10478. * Set the visibility of the graph
  10479. *
  10480. * @param vis {Boolean} True to show the series, false to hide. If UNDEFINED,
  10481. * the visibility is toggled.
  10482. */
  10483. setVisible: function (vis, redraw) {
  10484. var series = this,
  10485. chart = series.chart,
  10486. legendItem = series.legendItem,
  10487. seriesGroup = series.group,
  10488. seriesTracker = series.tracker,
  10489. dataLabelsGroup = series.dataLabelsGroup,
  10490. showOrHide,
  10491. i,
  10492. points = series.points,
  10493. point,
  10494. ignoreHiddenSeries = chart.options.chart.ignoreHiddenSeries,
  10495. oldVisibility = series.visible;
  10496. // if called without an argument, toggle visibility
  10497. series.visible = vis = vis === UNDEFINED ? !oldVisibility : vis;
  10498. showOrHide = vis ? 'show' : 'hide';
  10499. // show or hide series
  10500. if (seriesGroup) { // pies don't have one
  10501. seriesGroup[showOrHide]();
  10502. }
  10503. // show or hide trackers
  10504. if (seriesTracker) {
  10505. seriesTracker[showOrHide]();
  10506. } else if (points) {
  10507. i = points.length;
  10508. while (i--) {
  10509. point = points[i];
  10510. if (point.tracker) {
  10511. point.tracker[showOrHide]();
  10512. }
  10513. }
  10514. }
  10515. if (dataLabelsGroup) {
  10516. dataLabelsGroup[showOrHide]();
  10517. }
  10518. if (legendItem) {
  10519. chart.legend.colorizeItem(series, vis);
  10520. }
  10521. // rescale or adapt to resized chart
  10522. series.isDirty = true;
  10523. // in a stack, all other series are affected
  10524. if (series.options.stacking) {
  10525. each(chart.series, function (otherSeries) {
  10526. if (otherSeries.options.stacking && otherSeries.visible) {
  10527. otherSeries.isDirty = true;
  10528. }
  10529. });
  10530. }
  10531. if (ignoreHiddenSeries) {
  10532. chart.isDirtyBox = true;
  10533. }
  10534. if (redraw !== false) {
  10535. chart.redraw();
  10536. }
  10537. fireEvent(series, showOrHide);
  10538. },
  10539. /**
  10540. * Show the graph
  10541. */
  10542. show: function () {
  10543. this.setVisible(true);
  10544. },
  10545. /**
  10546. * Hide the graph
  10547. */
  10548. hide: function () {
  10549. this.setVisible(false);
  10550. },
  10551. /**
  10552. * Set the selected state of the graph
  10553. *
  10554. * @param selected {Boolean} True to select the series, false to unselect. If
  10555. * UNDEFINED, the selection state is toggled.
  10556. */
  10557. select: function (selected) {
  10558. var series = this;
  10559. // if called without an argument, toggle
  10560. series.selected = selected = (selected === UNDEFINED) ? !series.selected : selected;
  10561. if (series.checkbox) {
  10562. series.checkbox.checked = selected;
  10563. }
  10564. fireEvent(series, selected ? 'select' : 'unselect');
  10565. },
  10566. /**
  10567. * Create a group that holds the tracking object or objects. This allows for
  10568. * individual clipping and placement of each series tracker.
  10569. */
  10570. drawTrackerGroup: function () {
  10571. var trackerGroup = this.trackerGroup,
  10572. chart = this.chart;
  10573. if (this.isCartesian) {
  10574. // Generate it on first call
  10575. if (!trackerGroup) {
  10576. this.trackerGroup = trackerGroup = chart.renderer.g()
  10577. .attr({
  10578. zIndex: this.options.zIndex || 1
  10579. })
  10580. .add(chart.trackerGroup);
  10581. }
  10582. // Place it on first and subsequent (redraw) calls
  10583. trackerGroup.translate(this.xAxis.left, this.yAxis.top);
  10584. }
  10585. return trackerGroup;
  10586. },
  10587. /**
  10588. * Draw the tracker object that sits above all data labels and markers to
  10589. * track mouse events on the graph or points. For the line type charts
  10590. * the tracker uses the same graphPath, but with a greater stroke width
  10591. * for better control.
  10592. */
  10593. drawTracker: function () {
  10594. var series = this,
  10595. options = series.options,
  10596. trackerPath = [].concat(series.graphPath),
  10597. trackerPathLength = trackerPath.length,
  10598. chart = series.chart,
  10599. renderer = chart.renderer,
  10600. snap = chart.options.tooltip.snap,
  10601. tracker = series.tracker,
  10602. cursor = options.cursor,
  10603. css = cursor && { cursor: cursor },
  10604. singlePoints = series.singlePoints,
  10605. trackerGroup = series.drawTrackerGroup(),
  10606. singlePoint,
  10607. i;
  10608. // Extend end points. A better way would be to use round linecaps,
  10609. // but those are not clickable in VML.
  10610. if (trackerPathLength) {
  10611. i = trackerPathLength + 1;
  10612. while (i--) {
  10613. if (trackerPath[i] === M) { // extend left side
  10614. trackerPath.splice(i + 1, 0, trackerPath[i + 1] - snap, trackerPath[i + 2], L);
  10615. }
  10616. if ((i && trackerPath[i] === M) || i === trackerPathLength) { // extend right side
  10617. trackerPath.splice(i, 0, L, trackerPath[i - 2] + snap, trackerPath[i - 1]);
  10618. }
  10619. }
  10620. }
  10621. // handle single points
  10622. for (i = 0; i < singlePoints.length; i++) {
  10623. singlePoint = singlePoints[i];
  10624. trackerPath.push(M, singlePoint.plotX - snap, singlePoint.plotY,
  10625. L, singlePoint.plotX + snap, singlePoint.plotY);
  10626. }
  10627. // draw the tracker
  10628. if (tracker) {
  10629. tracker.attr({ d: trackerPath });
  10630. } else { // create
  10631. series.tracker = renderer.path(trackerPath)
  10632. .attr({
  10633. isTracker: true,
  10634. stroke: TRACKER_FILL,
  10635. fill: NONE,
  10636. 'stroke-linejoin': 'bevel',
  10637. 'stroke-width' : options.lineWidth + 2 * snap,
  10638. visibility: series.visible ? VISIBLE : HIDDEN
  10639. })
  10640. .on(hasTouch ? 'touchstart' : 'mouseover', function () {
  10641. if (chart.hoverSeries !== series) {
  10642. series.onMouseOver();
  10643. }
  10644. })
  10645. .on('mouseout', function () {
  10646. if (!options.stickyTracking) {
  10647. series.onMouseOut();
  10648. }
  10649. })
  10650. .css(css)
  10651. .add(trackerGroup);
  10652. }
  10653. }
  10654. }; // end Series prototype
  10655. /**
  10656. * LineSeries object
  10657. */
  10658. var LineSeries = extendClass(Series);
  10659. seriesTypes.line = LineSeries;
  10660. /**
  10661. * AreaSeries object
  10662. */
  10663. var AreaSeries = extendClass(Series, {
  10664. type: 'area'
  10665. });
  10666. seriesTypes.area = AreaSeries;
  10667. /**
  10668. * SplineSeries object
  10669. */
  10670. var SplineSeries = extendClass(Series, {
  10671. type: 'spline',
  10672. /**
  10673. * Draw the actual graph
  10674. */
  10675. getPointSpline: function (segment, point, i) {
  10676. var smoothing = 1.5, // 1 means control points midway between points, 2 means 1/3 from the point, 3 is 1/4 etc
  10677. denom = smoothing + 1,
  10678. plotX = point.plotX,
  10679. plotY = point.plotY,
  10680. lastPoint = segment[i - 1],
  10681. nextPoint = segment[i + 1],
  10682. leftContX,
  10683. leftContY,
  10684. rightContX,
  10685. rightContY,
  10686. ret;
  10687. // find control points
  10688. if (i && i < segment.length - 1) {
  10689. var lastX = lastPoint.plotX,
  10690. lastY = lastPoint.plotY,
  10691. nextX = nextPoint.plotX,
  10692. nextY = nextPoint.plotY,
  10693. correction;
  10694. leftContX = (smoothing * plotX + lastX) / denom;
  10695. leftContY = (smoothing * plotY + lastY) / denom;
  10696. rightContX = (smoothing * plotX + nextX) / denom;
  10697. rightContY = (smoothing * plotY + nextY) / denom;
  10698. // have the two control points make a straight line through main point
  10699. correction = ((rightContY - leftContY) * (rightContX - plotX)) /
  10700. (rightContX - leftContX) + plotY - rightContY;
  10701. leftContY += correction;
  10702. rightContY += correction;
  10703. // to prevent false extremes, check that control points are between
  10704. // neighbouring points' y values
  10705. if (leftContY > lastY && leftContY > plotY) {
  10706. leftContY = mathMax(lastY, plotY);
  10707. rightContY = 2 * plotY - leftContY; // mirror of left control point
  10708. } else if (leftContY < lastY && leftContY < plotY) {
  10709. leftContY = mathMin(lastY, plotY);
  10710. rightContY = 2 * plotY - leftContY;
  10711. }
  10712. if (rightContY > nextY && rightContY > plotY) {
  10713. rightContY = mathMax(nextY, plotY);
  10714. leftContY = 2 * plotY - rightContY;
  10715. } else if (rightContY < nextY && rightContY < plotY) {
  10716. rightContY = mathMin(nextY, plotY);
  10717. leftContY = 2 * plotY - rightContY;
  10718. }
  10719. // record for drawing in next point
  10720. point.rightContX = rightContX;
  10721. point.rightContY = rightContY;
  10722. }
  10723. // moveTo or lineTo
  10724. if (!i) {
  10725. ret = [M, plotX, plotY];
  10726. } else { // curve from last point to this
  10727. ret = [
  10728. 'C',
  10729. lastPoint.rightContX || lastPoint.plotX,
  10730. lastPoint.rightContY || lastPoint.plotY,
  10731. leftContX || plotX,
  10732. leftContY || plotY,
  10733. plotX,
  10734. plotY
  10735. ];
  10736. lastPoint.rightContX = lastPoint.rightContY = null; // reset for updating series later
  10737. }
  10738. return ret;
  10739. }
  10740. });
  10741. seriesTypes.spline = SplineSeries;
  10742. /**
  10743. * AreaSplineSeries object
  10744. */
  10745. var AreaSplineSeries = extendClass(SplineSeries, {
  10746. type: 'areaspline'
  10747. });
  10748. seriesTypes.areaspline = AreaSplineSeries;
  10749. /**
  10750. * ColumnSeries object
  10751. */
  10752. var ColumnSeries = extendClass(Series, {
  10753. type: 'column',
  10754. tooltipOutsidePlot: true,
  10755. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  10756. stroke: 'borderColor',
  10757. 'stroke-width': 'borderWidth',
  10758. fill: 'color',
  10759. r: 'borderRadius'
  10760. },
  10761. init: function () {
  10762. Series.prototype.init.apply(this, arguments);
  10763. var series = this,
  10764. chart = series.chart;
  10765. // if the series is added dynamically, force redraw of other
  10766. // series affected by a new column
  10767. if (chart.hasRendered) {
  10768. each(chart.series, function (otherSeries) {
  10769. if (otherSeries.type === series.type) {
  10770. otherSeries.isDirty = true;
  10771. }
  10772. });
  10773. }
  10774. },
  10775. /**
  10776. * Translate each point to the plot area coordinate system and find shape positions
  10777. */
  10778. translate: function () {
  10779. var series = this,
  10780. chart = series.chart,
  10781. options = series.options,
  10782. stacking = options.stacking,
  10783. borderWidth = options.borderWidth,
  10784. columnCount = 0,
  10785. xAxis = series.xAxis,
  10786. reversedXAxis = xAxis.reversed,
  10787. stackGroups = {},
  10788. stackKey,
  10789. columnIndex;
  10790. Series.prototype.translate.apply(series);
  10791. // Get the total number of column type series.
  10792. // This is called on every series. Consider moving this logic to a
  10793. // chart.orderStacks() function and call it on init, addSeries and removeSeries
  10794. each(chart.series, function (otherSeries) {
  10795. if (otherSeries.type === series.type && otherSeries.visible &&
  10796. series.options.group === otherSeries.options.group) { // used in Stock charts navigator series
  10797. if (otherSeries.options.stacking) {
  10798. stackKey = otherSeries.stackKey;
  10799. if (stackGroups[stackKey] === UNDEFINED) {
  10800. stackGroups[stackKey] = columnCount++;
  10801. }
  10802. columnIndex = stackGroups[stackKey];
  10803. } else {
  10804. columnIndex = columnCount++;
  10805. }
  10806. otherSeries.columnIndex = columnIndex;
  10807. }
  10808. });
  10809. // calculate the width and position of each column based on
  10810. // the number of column series in the plot, the groupPadding
  10811. // and the pointPadding options
  10812. var points = series.points,
  10813. categoryWidth = mathAbs(xAxis.translationSlope) * (xAxis.ordinalSlope || xAxis.closestPointRange || 1),
  10814. groupPadding = categoryWidth * options.groupPadding,
  10815. groupWidth = categoryWidth - 2 * groupPadding,
  10816. pointOffsetWidth = groupWidth / columnCount,
  10817. optionPointWidth = options.pointWidth,
  10818. pointPadding = defined(optionPointWidth) ? (pointOffsetWidth - optionPointWidth) / 2 :
  10819. pointOffsetWidth * options.pointPadding,
  10820. pointWidth = mathCeil(mathMax(pick(optionPointWidth, pointOffsetWidth - 2 * pointPadding), 1 + 2 * borderWidth)),
  10821. colIndex = (reversedXAxis ? columnCount -
  10822. series.columnIndex : series.columnIndex) || 0,
  10823. pointXOffset = pointPadding + (groupPadding + colIndex *
  10824. pointOffsetWidth - (categoryWidth / 2)) *
  10825. (reversedXAxis ? -1 : 1),
  10826. threshold = options.threshold,
  10827. translatedThreshold = series.yAxis.getThreshold(threshold),
  10828. minPointLength = pick(options.minPointLength, 5);
  10829. // record the new values
  10830. each(points, function (point) {
  10831. var plotY = point.plotY,
  10832. yBottom = pick(point.yBottom, translatedThreshold),
  10833. barX = point.plotX + pointXOffset,
  10834. barY = mathCeil(mathMin(plotY, yBottom)),
  10835. barH = mathCeil(mathMax(plotY, yBottom) - barY),
  10836. stack = series.yAxis.stacks[(point.y < 0 ? '-' : '') + series.stackKey],
  10837. shapeArgs;
  10838. // Record the offset'ed position and width of the bar to be able to align the stacking total correctly
  10839. if (stacking && series.visible && stack && stack[point.x]) {
  10840. stack[point.x].setOffset(pointXOffset, pointWidth);
  10841. }
  10842. // handle options.minPointLength
  10843. if (mathAbs(barH) < minPointLength) {
  10844. if (minPointLength) {
  10845. barH = minPointLength;
  10846. barY =
  10847. mathAbs(barY - translatedThreshold) > minPointLength ? // stacked
  10848. yBottom - minPointLength : // keep position
  10849. translatedThreshold - (plotY <= translatedThreshold ? minPointLength : 0);
  10850. }
  10851. }
  10852. extend(point, {
  10853. barX: barX,
  10854. barY: barY,
  10855. barW: pointWidth,
  10856. barH: barH
  10857. });
  10858. // create shape type and shape args that are reused in drawPoints and drawTracker
  10859. point.shapeType = 'rect';
  10860. shapeArgs = {
  10861. x: barX,
  10862. y: barY,
  10863. width: pointWidth,
  10864. height: barH,
  10865. r: options.borderRadius,
  10866. strokeWidth: borderWidth
  10867. };
  10868. if (borderWidth % 2) { // correct for shorting in crisp method, visible in stacked columns with 1px border
  10869. shapeArgs.y -= 1;
  10870. shapeArgs.height += 1;
  10871. }
  10872. point.shapeArgs = shapeArgs;
  10873. // make small columns responsive to mouse
  10874. point.trackerArgs = mathAbs(barH) < 3 && merge(point.shapeArgs, {
  10875. height: 6,
  10876. y: barY - 3
  10877. });
  10878. });
  10879. },
  10880. getSymbol: function () {
  10881. },
  10882. /**
  10883. * Columns have no graph
  10884. */
  10885. drawGraph: function () {},
  10886. /**
  10887. * Draw the columns. For bars, the series.group is rotated, so the same coordinates
  10888. * apply for columns and bars. This method is inherited by scatter series.
  10889. *
  10890. */
  10891. drawPoints: function () {
  10892. var series = this,
  10893. options = series.options,
  10894. renderer = series.chart.renderer,
  10895. graphic,
  10896. shapeArgs;
  10897. // draw the columns
  10898. each(series.points, function (point) {
  10899. var plotY = point.plotY;
  10900. if (plotY !== UNDEFINED && !isNaN(plotY) && point.y !== null) {
  10901. graphic = point.graphic;
  10902. shapeArgs = point.shapeArgs;
  10903. if (graphic) { // update
  10904. stop(graphic);
  10905. graphic.animate(renderer.Element.prototype.crisp.apply({}, [
  10906. shapeArgs.strokeWidth,
  10907. shapeArgs.x,
  10908. shapeArgs.y,
  10909. shapeArgs.width,
  10910. shapeArgs.height
  10911. ]));
  10912. } else {
  10913. point.graphic = graphic = renderer[point.shapeType](shapeArgs)
  10914. .attr(point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE])
  10915. .add(series.group)
  10916. .shadow(options.shadow);
  10917. }
  10918. }
  10919. });
  10920. },
  10921. /**
  10922. * Draw the individual tracker elements.
  10923. * This method is inherited by scatter and pie charts too.
  10924. */
  10925. drawTracker: function () {
  10926. var series = this,
  10927. chart = series.chart,
  10928. renderer = chart.renderer,
  10929. shapeArgs,
  10930. tracker,
  10931. trackerLabel = +new Date(),
  10932. options = series.options,
  10933. cursor = options.cursor,
  10934. css = cursor && { cursor: cursor },
  10935. trackerGroup = series.drawTrackerGroup(),
  10936. rel,
  10937. plotY,
  10938. validPlotY;
  10939. each(series.points, function (point) {
  10940. tracker = point.tracker;
  10941. shapeArgs = point.trackerArgs || point.shapeArgs;
  10942. plotY = point.plotY;
  10943. validPlotY = !series.isCartesian || (plotY !== UNDEFINED && !isNaN(plotY));
  10944. delete shapeArgs.strokeWidth;
  10945. if (point.y !== null && validPlotY) {
  10946. if (tracker) {// update
  10947. tracker.attr(shapeArgs);
  10948. } else {
  10949. point.tracker =
  10950. renderer[point.shapeType](shapeArgs)
  10951. .attr({
  10952. isTracker: trackerLabel,
  10953. fill: TRACKER_FILL,
  10954. visibility: series.visible ? VISIBLE : HIDDEN
  10955. })
  10956. .on(hasTouch ? 'touchstart' : 'mouseover', function (event) {
  10957. rel = event.relatedTarget || event.fromElement;
  10958. if (chart.hoverSeries !== series && attr(rel, 'isTracker') !== trackerLabel) {
  10959. series.onMouseOver();
  10960. }
  10961. point.onMouseOver();
  10962. })
  10963. .on('mouseout', function (event) {
  10964. if (!options.stickyTracking) {
  10965. rel = event.relatedTarget || event.toElement;
  10966. if (attr(rel, 'isTracker') !== trackerLabel) {
  10967. series.onMouseOut();
  10968. }
  10969. }
  10970. })
  10971. .css(css)
  10972. .add(point.group || trackerGroup); // pies have point group - see issue #118
  10973. }
  10974. }
  10975. });
  10976. },
  10977. /**
  10978. * Animate the column heights one by one from zero
  10979. * @param {Boolean} init Whether to initialize the animation or run it
  10980. */
  10981. animate: function (init) {
  10982. var series = this,
  10983. points = series.points,
  10984. options = series.options;
  10985. if (!init) { // run the animation
  10986. /*
  10987. * Note: Ideally the animation should be initialized by calling
  10988. * series.group.hide(), and then calling series.group.show()
  10989. * after the animation was started. But this rendered the shadows
  10990. * invisible in IE8 standards mode. If the columns flicker on large
  10991. * datasets, this is the cause.
  10992. */
  10993. each(points, function (point) {
  10994. var graphic = point.graphic,
  10995. shapeArgs = point.shapeArgs,
  10996. yAxis = series.yAxis,
  10997. threshold = options.threshold;
  10998. if (graphic) {
  10999. // start values
  11000. graphic.attr({
  11001. height: 0,
  11002. y: defined(threshold) ?
  11003. yAxis.getThreshold(threshold) :
  11004. yAxis.translate(yAxis.getExtremes().min, 0, 1, 0, 1)
  11005. });
  11006. // animate
  11007. graphic.animate({
  11008. height: shapeArgs.height,
  11009. y: shapeArgs.y
  11010. }, options.animation);
  11011. }
  11012. });
  11013. // delete this function to allow it only once
  11014. series.animate = null;
  11015. }
  11016. },
  11017. /**
  11018. * Remove this series from the chart
  11019. */
  11020. remove: function () {
  11021. var series = this,
  11022. chart = series.chart;
  11023. // column and bar series affects other series of the same type
  11024. // as they are either stacked or grouped
  11025. if (chart.hasRendered) {
  11026. each(chart.series, function (otherSeries) {
  11027. if (otherSeries.type === series.type) {
  11028. otherSeries.isDirty = true;
  11029. }
  11030. });
  11031. }
  11032. Series.prototype.remove.apply(series, arguments);
  11033. }
  11034. });
  11035. seriesTypes.column = ColumnSeries;
  11036. var BarSeries = extendClass(ColumnSeries, {
  11037. type: 'bar',
  11038. init: function () {
  11039. this.inverted = true;
  11040. ColumnSeries.prototype.init.apply(this, arguments);
  11041. }
  11042. });
  11043. seriesTypes.bar = BarSeries;
  11044. /**
  11045. * The scatter series class
  11046. */
  11047. var ScatterSeries = extendClass(Series, {
  11048. type: 'scatter',
  11049. sorted: false,
  11050. /**
  11051. * Extend the base Series' translate method by adding shape type and
  11052. * arguments for the point trackers
  11053. */
  11054. translate: function () {
  11055. var series = this;
  11056. Series.prototype.translate.apply(series);
  11057. each(series.points, function (point) {
  11058. point.shapeType = 'circle';
  11059. point.shapeArgs = {
  11060. x: point.plotX,
  11061. y: point.plotY,
  11062. r: series.chart.options.tooltip.snap
  11063. };
  11064. });
  11065. },
  11066. /**
  11067. * Add tracking event listener to the series group, so the point graphics
  11068. * themselves act as trackers
  11069. */
  11070. drawTracker: function () {
  11071. var series = this,
  11072. cursor = series.options.cursor,
  11073. css = cursor && { cursor: cursor },
  11074. points = series.points,
  11075. i = points.length,
  11076. graphic;
  11077. // Set an expando property for the point index, used below
  11078. while (i--) {
  11079. graphic = points[i].graphic;
  11080. if (graphic) { // doesn't exist for null points
  11081. graphic.element._i = i;
  11082. }
  11083. }
  11084. // Add the event listeners, we need to do this only once
  11085. if (!series._hasTracking) {
  11086. series.group
  11087. .attr({
  11088. isTracker: true
  11089. })
  11090. .on(hasTouch ? 'touchstart' : 'mouseover', function (e) {
  11091. series.onMouseOver();
  11092. if (e.target._i !== UNDEFINED) { // undefined on graph in scatterchart
  11093. points[e.target._i].onMouseOver();
  11094. }
  11095. })
  11096. .on('mouseout', function () {
  11097. if (!series.options.stickyTracking) {
  11098. series.onMouseOut();
  11099. }
  11100. })
  11101. .css(css);
  11102. } else {
  11103. series._hasTracking = true;
  11104. }
  11105. }
  11106. });
  11107. seriesTypes.scatter = ScatterSeries;
  11108. /**
  11109. * Extended point object for pies
  11110. */
  11111. var PiePoint = extendClass(Point, {
  11112. /**
  11113. * Initiate the pie slice
  11114. */
  11115. init: function () {
  11116. Point.prototype.init.apply(this, arguments);
  11117. var point = this,
  11118. toggleSlice;
  11119. //visible: options.visible !== false,
  11120. extend(point, {
  11121. visible: point.visible !== false,
  11122. name: pick(point.name, 'Slice')
  11123. });
  11124. // add event listener for select
  11125. toggleSlice = function () {
  11126. point.slice();
  11127. };
  11128. addEvent(point, 'select', toggleSlice);
  11129. addEvent(point, 'unselect', toggleSlice);
  11130. return point;
  11131. },
  11132. /**
  11133. * Toggle the visibility of the pie slice
  11134. * @param {Boolean} vis Whether to show the slice or not. If undefined, the
  11135. * visibility is toggled
  11136. */
  11137. setVisible: function (vis) {
  11138. var point = this,
  11139. chart = point.series.chart,
  11140. tracker = point.tracker,
  11141. dataLabel = point.dataLabel,
  11142. connector = point.connector,
  11143. shadowGroup = point.shadowGroup,
  11144. method;
  11145. // if called without an argument, toggle visibility
  11146. point.visible = vis = vis === UNDEFINED ? !point.visible : vis;
  11147. method = vis ? 'show' : 'hide';
  11148. point.group[method]();
  11149. if (tracker) {
  11150. tracker[method]();
  11151. }
  11152. if (dataLabel) {
  11153. dataLabel[method]();
  11154. }
  11155. if (connector) {
  11156. connector[method]();
  11157. }
  11158. if (shadowGroup) {
  11159. shadowGroup[method]();
  11160. }
  11161. if (point.legendItem) {
  11162. chart.legend.colorizeItem(point, vis);
  11163. }
  11164. },
  11165. /**
  11166. * Set or toggle whether the slice is cut out from the pie
  11167. * @param {Boolean} sliced When undefined, the slice state is toggled
  11168. * @param {Boolean} redraw Whether to redraw the chart. True by default.
  11169. */
  11170. slice: function (sliced, redraw, animation) {
  11171. var point = this,
  11172. series = point.series,
  11173. chart = series.chart,
  11174. slicedTranslation = point.slicedTranslation,
  11175. translation;
  11176. setAnimation(animation, chart);
  11177. // redraw is true by default
  11178. redraw = pick(redraw, true);
  11179. // if called without an argument, toggle
  11180. sliced = point.sliced = defined(sliced) ? sliced : !point.sliced;
  11181. translation = {
  11182. translateX: (sliced ? slicedTranslation[0] : chart.plotLeft),
  11183. translateY: (sliced ? slicedTranslation[1] : chart.plotTop)
  11184. };
  11185. point.group.animate(translation);
  11186. if (point.shadowGroup) {
  11187. point.shadowGroup.animate(translation);
  11188. }
  11189. }
  11190. });
  11191. /**
  11192. * The Pie series class
  11193. */
  11194. var PieSeries = extendClass(Series, {
  11195. type: 'pie',
  11196. isCartesian: false,
  11197. pointClass: PiePoint,
  11198. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  11199. stroke: 'borderColor',
  11200. 'stroke-width': 'borderWidth',
  11201. fill: 'color'
  11202. },
  11203. /**
  11204. * Pies have one color each point
  11205. */
  11206. getColor: function () {
  11207. // record first color for use in setData
  11208. this.initialColor = this.chart.counters.color;
  11209. },
  11210. /**
  11211. * Animate the column heights one by one from zero
  11212. */
  11213. animate: function () {
  11214. var series = this,
  11215. points = series.points;
  11216. each(points, function (point) {
  11217. var graphic = point.graphic,
  11218. args = point.shapeArgs,
  11219. up = -mathPI / 2;
  11220. if (graphic) {
  11221. // start values
  11222. graphic.attr({
  11223. r: 0,
  11224. start: up,
  11225. end: up
  11226. });
  11227. // animate
  11228. graphic.animate({
  11229. r: args.r,
  11230. start: args.start,
  11231. end: args.end
  11232. }, series.options.animation);
  11233. }
  11234. });
  11235. // delete this function to allow it only once
  11236. series.animate = null;
  11237. },
  11238. /**
  11239. * Extend the basic setData method by running processData and generatePoints immediately,
  11240. * in order to access the points from the legend.
  11241. */
  11242. setData: function (data, redraw) {
  11243. Series.prototype.setData.call(this, data, false);
  11244. this.processData();
  11245. this.generatePoints();
  11246. if (pick(redraw, true)) {
  11247. this.chart.redraw();
  11248. }
  11249. },
  11250. /**
  11251. * Do translation for pie slices
  11252. */
  11253. translate: function () {
  11254. this.generatePoints();
  11255. var total = 0,
  11256. series = this,
  11257. cumulative = -0.25, // start at top
  11258. precision = 1000, // issue #172
  11259. options = series.options,
  11260. slicedOffset = options.slicedOffset,
  11261. connectorOffset = slicedOffset + options.borderWidth,
  11262. positions = options.center.concat([options.size, options.innerSize || 0]),
  11263. chart = series.chart,
  11264. plotWidth = chart.plotWidth,
  11265. plotHeight = chart.plotHeight,
  11266. start,
  11267. end,
  11268. angle,
  11269. points = series.points,
  11270. circ = 2 * mathPI,
  11271. fraction,
  11272. smallestSize = mathMin(plotWidth, plotHeight),
  11273. isPercent,
  11274. radiusX, // the x component of the radius vector for a given point
  11275. radiusY,
  11276. labelDistance = options.dataLabels.distance;
  11277. // get positions - either an integer or a percentage string must be given
  11278. positions = map(positions, function (length, i) {
  11279. isPercent = /%$/.test(length);
  11280. return isPercent ?
  11281. // i == 0: centerX, relative to width
  11282. // i == 1: centerY, relative to height
  11283. // i == 2: size, relative to smallestSize
  11284. // i == 4: innerSize, relative to smallestSize
  11285. [plotWidth, plotHeight, smallestSize, smallestSize][i] *
  11286. pInt(length) / 100 :
  11287. length;
  11288. });
  11289. // utility for getting the x value from a given y, used for anticollision logic in data labels
  11290. series.getX = function (y, left) {
  11291. angle = math.asin((y - positions[1]) / (positions[2] / 2 + labelDistance));
  11292. return positions[0] +
  11293. (left ? -1 : 1) *
  11294. (mathCos(angle) * (positions[2] / 2 + labelDistance));
  11295. };
  11296. // set center for later use
  11297. series.center = positions;
  11298. // get the total sum
  11299. each(points, function (point) {
  11300. total += point.y;
  11301. });
  11302. each(points, function (point) {
  11303. // set start and end angle
  11304. fraction = total ? point.y / total : 0;
  11305. start = mathRound(cumulative * circ * precision) / precision;
  11306. cumulative += fraction;
  11307. end = mathRound(cumulative * circ * precision) / precision;
  11308. // set the shape
  11309. point.shapeType = 'arc';
  11310. point.shapeArgs = {
  11311. x: positions[0],
  11312. y: positions[1],
  11313. r: positions[2] / 2,
  11314. innerR: positions[3] / 2,
  11315. start: start,
  11316. end: end
  11317. };
  11318. // center for the sliced out slice
  11319. angle = (end + start) / 2;
  11320. point.slicedTranslation = map([
  11321. mathCos(angle) * slicedOffset + chart.plotLeft,
  11322. mathSin(angle) * slicedOffset + chart.plotTop
  11323. ], mathRound);
  11324. // set the anchor point for tooltips
  11325. radiusX = mathCos(angle) * positions[2] / 2;
  11326. radiusY = mathSin(angle) * positions[2] / 2;
  11327. point.tooltipPos = [
  11328. positions[0] + radiusX * 0.7,
  11329. positions[1] + radiusY * 0.7
  11330. ];
  11331. // set the anchor point for data labels
  11332. point.labelPos = [
  11333. positions[0] + radiusX + mathCos(angle) * labelDistance, // first break of connector
  11334. positions[1] + radiusY + mathSin(angle) * labelDistance, // a/a
  11335. positions[0] + radiusX + mathCos(angle) * connectorOffset, // second break, right outside pie
  11336. positions[1] + radiusY + mathSin(angle) * connectorOffset, // a/a
  11337. positions[0] + radiusX, // landing point for connector
  11338. positions[1] + radiusY, // a/a
  11339. labelDistance < 0 ? // alignment
  11340. 'center' :
  11341. angle < circ / 4 ? 'left' : 'right', // alignment
  11342. angle // center angle
  11343. ];
  11344. // API properties
  11345. point.percentage = fraction * 100;
  11346. point.total = total;
  11347. });
  11348. this.setTooltipPoints();
  11349. },
  11350. /**
  11351. * Render the slices
  11352. */
  11353. render: function () {
  11354. var series = this;
  11355. // cache attributes for shapes
  11356. series.getAttribs();
  11357. this.drawPoints();
  11358. // draw the mouse tracking area
  11359. if (series.options.enableMouseTracking !== false) {
  11360. series.drawTracker();
  11361. }
  11362. this.drawDataLabels();
  11363. if (series.options.animation && series.animate) {
  11364. series.animate();
  11365. }
  11366. // (See #322) series.isDirty = series.isDirtyData = false; // means data is in accordance with what you see
  11367. series.isDirty = false; // means data is in accordance with what you see
  11368. },
  11369. /**
  11370. * Draw the data points
  11371. */
  11372. drawPoints: function () {
  11373. var series = this,
  11374. chart = series.chart,
  11375. renderer = chart.renderer,
  11376. groupTranslation,
  11377. //center,
  11378. graphic,
  11379. group,
  11380. shadow = series.options.shadow,
  11381. shadowGroup,
  11382. shapeArgs;
  11383. // draw the slices
  11384. each(series.points, function (point) {
  11385. graphic = point.graphic;
  11386. shapeArgs = point.shapeArgs;
  11387. group = point.group;
  11388. shadowGroup = point.shadowGroup;
  11389. // put the shadow behind all points
  11390. if (shadow && !shadowGroup) {
  11391. shadowGroup = point.shadowGroup = renderer.g('shadow')
  11392. .attr({ zIndex: 4 })
  11393. .add();
  11394. }
  11395. // create the group the first time
  11396. if (!group) {
  11397. group = point.group = renderer.g('point')
  11398. .attr({ zIndex: 5 })
  11399. .add();
  11400. }
  11401. // if the point is sliced, use special translation, else use plot area traslation
  11402. groupTranslation = point.sliced ? point.slicedTranslation : [chart.plotLeft, chart.plotTop];
  11403. group.translate(groupTranslation[0], groupTranslation[1]);
  11404. if (shadowGroup) {
  11405. shadowGroup.translate(groupTranslation[0], groupTranslation[1]);
  11406. }
  11407. // draw the slice
  11408. if (graphic) {
  11409. graphic.animate(shapeArgs);
  11410. } else {
  11411. point.graphic =
  11412. renderer.arc(shapeArgs)
  11413. .attr(extend(
  11414. point.pointAttr[NORMAL_STATE],
  11415. { 'stroke-linejoin': 'round' }
  11416. ))
  11417. .add(point.group)
  11418. .shadow(shadow, shadowGroup);
  11419. }
  11420. // detect point specific visibility
  11421. if (point.visible === false) {
  11422. point.setVisible(false);
  11423. }
  11424. });
  11425. },
  11426. /**
  11427. * Override the base drawDataLabels method by pie specific functionality
  11428. */
  11429. drawDataLabels: function () {
  11430. var series = this,
  11431. data = series.data,
  11432. point,
  11433. chart = series.chart,
  11434. options = series.options.dataLabels,
  11435. connectorPadding = pick(options.connectorPadding, 10),
  11436. connectorWidth = pick(options.connectorWidth, 1),
  11437. connector,
  11438. connectorPath,
  11439. softConnector = pick(options.softConnector, true),
  11440. distanceOption = options.distance,
  11441. seriesCenter = series.center,
  11442. radius = seriesCenter[2] / 2,
  11443. centerY = seriesCenter[1],
  11444. outside = distanceOption > 0,
  11445. dataLabel,
  11446. labelPos,
  11447. labelHeight,
  11448. halves = [// divide the points into right and left halves for anti collision
  11449. [], // right
  11450. [] // left
  11451. ],
  11452. x,
  11453. y,
  11454. visibility,
  11455. rankArr,
  11456. sort,
  11457. i = 2,
  11458. j;
  11459. // get out if not enabled
  11460. if (!options.enabled) {
  11461. return;
  11462. }
  11463. // run parent method
  11464. Series.prototype.drawDataLabels.apply(series);
  11465. // arrange points for detection collision
  11466. each(data, function (point) {
  11467. if (point.dataLabel) { // it may have been cancelled in the base method (#407)
  11468. halves[
  11469. point.labelPos[7] < mathPI / 2 ? 0 : 1
  11470. ].push(point);
  11471. }
  11472. });
  11473. halves[1].reverse();
  11474. // define the sorting algorithm
  11475. sort = function (a, b) {
  11476. return b.y - a.y;
  11477. };
  11478. // assume equal label heights
  11479. labelHeight = halves[0][0] && halves[0][0].dataLabel && halves[0][0].dataLabel.getBBox().height;
  11480. /* Loop over the points in each half, starting from the top and bottom
  11481. * of the pie to detect overlapping labels.
  11482. */
  11483. while (i--) {
  11484. var slots = [],
  11485. slotsLength,
  11486. usedSlots = [],
  11487. points = halves[i],
  11488. pos,
  11489. length = points.length,
  11490. slotIndex;
  11491. // Only do anti-collision when we are outside the pie and have connectors (#856)
  11492. if (distanceOption > 0) {
  11493. // build the slots
  11494. for (pos = centerY - radius - distanceOption; pos <= centerY + radius + distanceOption; pos += labelHeight) {
  11495. slots.push(pos);
  11496. // visualize the slot
  11497. /*
  11498. var slotX = series.getX(pos, i) + chart.plotLeft - (i ? 100 : 0),
  11499. slotY = pos + chart.plotTop;
  11500. if (!isNaN(slotX)) {
  11501. chart.renderer.rect(slotX, slotY - 7, 100, labelHeight)
  11502. .attr({
  11503. 'stroke-width': 1,
  11504. stroke: 'silver'
  11505. })
  11506. .add();
  11507. chart.renderer.text('Slot '+ (slots.length - 1), slotX, slotY + 4)
  11508. .attr({
  11509. fill: 'silver'
  11510. }).add();
  11511. }
  11512. // */
  11513. }
  11514. slotsLength = slots.length;
  11515. // if there are more values than available slots, remove lowest values
  11516. if (length > slotsLength) {
  11517. // create an array for sorting and ranking the points within each quarter
  11518. rankArr = [].concat(points);
  11519. rankArr.sort(sort);
  11520. j = length;
  11521. while (j--) {
  11522. rankArr[j].rank = j;
  11523. }
  11524. j = length;
  11525. while (j--) {
  11526. if (points[j].rank >= slotsLength) {
  11527. points.splice(j, 1);
  11528. }
  11529. }
  11530. length = points.length;
  11531. }
  11532. // The label goes to the nearest open slot, but not closer to the edge than
  11533. // the label's index.
  11534. for (j = 0; j < length; j++) {
  11535. point = points[j];
  11536. labelPos = point.labelPos;
  11537. var closest = 9999,
  11538. distance,
  11539. slotI;
  11540. // find the closest slot index
  11541. for (slotI = 0; slotI < slotsLength; slotI++) {
  11542. distance = mathAbs(slots[slotI] - labelPos[1]);
  11543. if (distance < closest) {
  11544. closest = distance;
  11545. slotIndex = slotI;
  11546. }
  11547. }
  11548. // if that slot index is closer to the edges of the slots, move it
  11549. // to the closest appropriate slot
  11550. if (slotIndex < j && slots[j] !== null) { // cluster at the top
  11551. slotIndex = j;
  11552. } else if (slotsLength < length - j + slotIndex && slots[j] !== null) { // cluster at the bottom
  11553. slotIndex = slotsLength - length + j;
  11554. while (slots[slotIndex] === null) { // make sure it is not taken
  11555. slotIndex++;
  11556. }
  11557. } else {
  11558. // Slot is taken, find next free slot below. In the next run, the next slice will find the
  11559. // slot above these, because it is the closest one
  11560. while (slots[slotIndex] === null) { // make sure it is not taken
  11561. slotIndex++;
  11562. }
  11563. }
  11564. usedSlots.push({ i: slotIndex, y: slots[slotIndex] });
  11565. slots[slotIndex] = null; // mark as taken
  11566. }
  11567. // sort them in order to fill in from the top
  11568. usedSlots.sort(sort);
  11569. }
  11570. // now the used slots are sorted, fill them up sequentially
  11571. for (j = 0; j < length; j++) {
  11572. var slot, naturalY;
  11573. point = points[j];
  11574. labelPos = point.labelPos;
  11575. dataLabel = point.dataLabel;
  11576. visibility = point.visible === false ? HIDDEN : VISIBLE;
  11577. naturalY = labelPos[1];
  11578. if (distanceOption > 0) {
  11579. slot = usedSlots.pop();
  11580. slotIndex = slot.i;
  11581. // if the slot next to currrent slot is free, the y value is allowed
  11582. // to fall back to the natural position
  11583. y = slot.y;
  11584. if ((naturalY > y && slots[slotIndex + 1] !== null) ||
  11585. (naturalY < y && slots[slotIndex - 1] !== null)) {
  11586. y = naturalY;
  11587. }
  11588. } else {
  11589. y = naturalY;
  11590. }
  11591. // get the x - use the natural x position for first and last slot, to prevent the top
  11592. // and botton slice connectors from touching each other on either side
  11593. x = options.justify ?
  11594. seriesCenter[0] + (i ? -1 : 1) * (radius + distanceOption) :
  11595. series.getX(slotIndex === 0 || slotIndex === slots.length - 1 ? naturalY : y, i);
  11596. // move or place the data label
  11597. dataLabel
  11598. .attr({
  11599. visibility: visibility,
  11600. align: labelPos[6]
  11601. })[dataLabel.moved ? 'animate' : 'attr']({
  11602. x: x + options.x +
  11603. ({ left: connectorPadding, right: -connectorPadding }[labelPos[6]] || 0),
  11604. y: y + options.y
  11605. });
  11606. dataLabel.moved = true;
  11607. // draw the connector
  11608. if (outside && connectorWidth) {
  11609. connector = point.connector;
  11610. connectorPath = softConnector ? [
  11611. M,
  11612. x + (labelPos[6] === 'left' ? 5 : -5), y, // end of the string at the label
  11613. 'C',
  11614. x, y, // first break, next to the label
  11615. 2 * labelPos[2] - labelPos[4], 2 * labelPos[3] - labelPos[5],
  11616. labelPos[2], labelPos[3], // second break
  11617. L,
  11618. labelPos[4], labelPos[5] // base
  11619. ] : [
  11620. M,
  11621. x + (labelPos[6] === 'left' ? 5 : -5), y, // end of the string at the label
  11622. L,
  11623. labelPos[2], labelPos[3], // second break
  11624. L,
  11625. labelPos[4], labelPos[5] // base
  11626. ];
  11627. if (connector) {
  11628. connector.animate({ d: connectorPath });
  11629. connector.attr('visibility', visibility);
  11630. } else {
  11631. point.connector = connector = series.chart.renderer.path(connectorPath).attr({
  11632. 'stroke-width': connectorWidth,
  11633. stroke: options.connectorColor || point.color || '#606060',
  11634. visibility: visibility,
  11635. zIndex: 3
  11636. })
  11637. .translate(chart.plotLeft, chart.plotTop)
  11638. .add();
  11639. }
  11640. }
  11641. }
  11642. }
  11643. },
  11644. /**
  11645. * Draw point specific tracker objects. Inherit directly from column series.
  11646. */
  11647. drawTracker: ColumnSeries.prototype.drawTracker,
  11648. /**
  11649. * Pies don't have point marker symbols
  11650. */
  11651. getSymbol: function () {}
  11652. });
  11653. seriesTypes.pie = PieSeries;
  11654. // global variables
  11655. extend(Highcharts, {
  11656. Chart: Chart,
  11657. dateFormat: dateFormat,
  11658. pathAnim: pathAnim,
  11659. getOptions: getOptions,
  11660. hasBidiBug: hasBidiBug,
  11661. numberFormat: numberFormat,
  11662. Point: Point,
  11663. Color: Color,
  11664. Renderer: Renderer,
  11665. SVGRenderer: SVGRenderer,
  11666. VMLRenderer: VMLRenderer,
  11667. CanVGRenderer: CanVGRenderer,
  11668. seriesTypes: seriesTypes,
  11669. setOptions: setOptions,
  11670. Series: Series,
  11671. // Expose utility funcitons for modules
  11672. addEvent: addEvent,
  11673. removeEvent: removeEvent,
  11674. createElement: createElement,
  11675. discardElement: discardElement,
  11676. css: css,
  11677. each: each,
  11678. extend: extend,
  11679. map: map,
  11680. merge: merge,
  11681. pick: pick,
  11682. splat: splat,
  11683. extendClass: extendClass,
  11684. placeBox: placeBox,
  11685. product: 'Highcharts',
  11686. version: '2.2.3'
  11687. });
  11688. }());