highcharts.src.js 716 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664
  1. // ==ClosureCompiler==
  2. // @compilation_level SIMPLE_OPTIMIZATIONS
  3. /**
  4. * @license Highcharts JS v4.2.5 (2016-05-06)
  5. *
  6. * (c) 2009-2016 Torstein Honsi
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. (function (root, factory) {
  11. if (typeof module === 'object' && module.exports) {
  12. module.exports = root.document ?
  13. factory(root) :
  14. factory;
  15. } else {
  16. root.Highcharts = factory(root);
  17. }
  18. }(typeof window !== 'undefined' ? window : this, function (win) { // eslint-disable-line no-undef
  19. // encapsulated variables
  20. var UNDEFINED,
  21. doc = win.document,
  22. math = Math,
  23. mathRound = math.round,
  24. mathFloor = math.floor,
  25. mathCeil = math.ceil,
  26. mathMax = math.max,
  27. mathMin = math.min,
  28. mathAbs = math.abs,
  29. mathCos = math.cos,
  30. mathSin = math.sin,
  31. mathPI = math.PI,
  32. deg2rad = mathPI * 2 / 360,
  33. // some variables
  34. userAgent = (win.navigator && win.navigator.userAgent) || '',
  35. isOpera = win.opera,
  36. isMS = /(msie|trident|edge)/i.test(userAgent) && !isOpera,
  37. docMode8 = doc && doc.documentMode === 8,
  38. isWebKit = !isMS && /AppleWebKit/.test(userAgent),
  39. isFirefox = /Firefox/.test(userAgent),
  40. isTouchDevice = /(Mobile|Android|Windows Phone)/.test(userAgent),
  41. SVG_NS = 'http://www.w3.org/2000/svg',
  42. hasSVG = doc && doc.createElementNS && !!doc.createElementNS(SVG_NS, 'svg').createSVGRect,
  43. hasBidiBug = isFirefox && parseInt(userAgent.split('Firefox/')[1], 10) < 4, // issue #38
  44. useCanVG = doc && !hasSVG && !isMS && !!doc.createElement('canvas').getContext,
  45. Renderer,
  46. hasTouch,
  47. symbolSizes = {},
  48. idCounter = 0,
  49. garbageBin,
  50. defaultOptions,
  51. dateFormat, // function
  52. pathAnim,
  53. timeUnits,
  54. noop = function () {},
  55. charts = [],
  56. chartCount = 0,
  57. PRODUCT = 'Highcharts',
  58. VERSION = '4.2.5',
  59. // some constants for frequently used strings
  60. DIV = 'div',
  61. ABSOLUTE = 'absolute',
  62. RELATIVE = 'relative',
  63. HIDDEN = 'hidden',
  64. PREFIX = 'highcharts-',
  65. VISIBLE = 'visible',
  66. PX = 'px',
  67. NONE = 'none',
  68. M = 'M',
  69. L = 'L',
  70. numRegex = /^[0-9]+$/,
  71. NORMAL_STATE = '',
  72. HOVER_STATE = 'hover',
  73. SELECT_STATE = 'select',
  74. marginNames = ['plotTop', 'marginRight', 'marginBottom', 'plotLeft'],
  75. // Object for extending Axis
  76. AxisPlotLineOrBandExtension,
  77. // constants for attributes
  78. STROKE_WIDTH = 'stroke-width',
  79. // time methods, changed based on whether or not UTC is used
  80. Date, // Allow using a different Date class
  81. makeTime,
  82. timezoneOffset,
  83. getTimezoneOffset,
  84. getMinutes,
  85. getHours,
  86. getDay,
  87. getDate,
  88. getMonth,
  89. getFullYear,
  90. setMilliseconds,
  91. setSeconds,
  92. setMinutes,
  93. setHours,
  94. setDate,
  95. setMonth,
  96. setFullYear,
  97. // lookup over the types and the associated classes
  98. seriesTypes = {},
  99. Highcharts;
  100. /**
  101. * Provide error messages for debugging, with links to online explanation
  102. */
  103. function error(code, stop) {
  104. var msg = 'Highcharts error #' + code + ': www.highcharts.com/errors/' + code;
  105. if (stop) {
  106. throw new Error(msg);
  107. }
  108. // else ...
  109. if (win.console) {
  110. console.log(msg); // eslint-disable-line no-console
  111. }
  112. }
  113. // The Highcharts namespace
  114. Highcharts = win.Highcharts ? error(16, true) : { win: win };
  115. Highcharts.seriesTypes = seriesTypes;
  116. var timers = [],
  117. getStyle,
  118. // Previous adapter functions
  119. inArray,
  120. each,
  121. grep,
  122. offset,
  123. map,
  124. addEvent,
  125. removeEvent,
  126. fireEvent,
  127. animate,
  128. stop;
  129. /**
  130. * An animator object. One instance applies to one property (attribute or style prop)
  131. * on one element.
  132. *
  133. * @param {object} elem The element to animate. May be a DOM element or a Highcharts SVGElement wrapper.
  134. * @param {object} options Animation options, including duration, easing, step and complete.
  135. * @param {object} prop The property to animate.
  136. */
  137. function Fx(elem, options, prop) {
  138. this.options = options;
  139. this.elem = elem;
  140. this.prop = prop;
  141. }
  142. Fx.prototype = {
  143. /**
  144. * Animating a path definition on SVGElement
  145. * @returns {undefined}
  146. */
  147. dSetter: function () {
  148. var start = this.paths[0],
  149. end = this.paths[1],
  150. ret = [],
  151. now = this.now,
  152. i = start.length,
  153. startVal;
  154. if (now === 1) { // land on the final path without adjustment points appended in the ends
  155. ret = this.toD;
  156. } else if (i === end.length && now < 1) {
  157. while (i--) {
  158. startVal = parseFloat(start[i]);
  159. ret[i] =
  160. isNaN(startVal) ? // a letter instruction like M or L
  161. start[i] :
  162. now * (parseFloat(end[i] - startVal)) + startVal;
  163. }
  164. } else { // if animation is finished or length not matching, land on right value
  165. ret = end;
  166. }
  167. this.elem.attr('d', ret);
  168. },
  169. /**
  170. * Update the element with the current animation step
  171. * @returns {undefined}
  172. */
  173. update: function () {
  174. var elem = this.elem,
  175. prop = this.prop, // if destroyed, it is null
  176. now = this.now,
  177. step = this.options.step;
  178. // Animation setter defined from outside
  179. if (this[prop + 'Setter']) {
  180. this[prop + 'Setter']();
  181. // Other animations on SVGElement
  182. } else if (elem.attr) {
  183. if (elem.element) {
  184. elem.attr(prop, now);
  185. }
  186. // HTML styles, raw HTML content like container size
  187. } else {
  188. elem.style[prop] = now + this.unit;
  189. }
  190. if (step) {
  191. step.call(elem, now, this);
  192. }
  193. },
  194. /**
  195. * Run an animation
  196. */
  197. run: function (from, to, unit) {
  198. var self = this,
  199. timer = function (gotoEnd) {
  200. return timer.stopped ? false : self.step(gotoEnd);
  201. },
  202. i;
  203. this.startTime = +new Date();
  204. this.start = from;
  205. this.end = to;
  206. this.unit = unit;
  207. this.now = this.start;
  208. this.pos = 0;
  209. timer.elem = this.elem;
  210. if (timer() && timers.push(timer) === 1) {
  211. timer.timerId = setInterval(function () {
  212. for (i = 0; i < timers.length; i++) {
  213. if (!timers[i]()) {
  214. timers.splice(i--, 1);
  215. }
  216. }
  217. if (!timers.length) {
  218. clearInterval(timer.timerId);
  219. }
  220. }, 13);
  221. }
  222. },
  223. /**
  224. * Run a single step in the animation
  225. * @param {Boolean} gotoEnd Whether to go to then endpoint of the animation after abort
  226. * @returns {Boolean} True if animation continues
  227. */
  228. step: function (gotoEnd) {
  229. var t = +new Date(),
  230. ret,
  231. done,
  232. options = this.options,
  233. elem = this.elem,
  234. complete = options.complete,
  235. duration = options.duration,
  236. curAnim = options.curAnim,
  237. i;
  238. if (elem.attr && !elem.element) { // #2616, element including flag is destroyed
  239. ret = false;
  240. } else if (gotoEnd || t >= duration + this.startTime) {
  241. this.now = this.end;
  242. this.pos = 1;
  243. this.update();
  244. curAnim[this.prop] = true;
  245. done = true;
  246. for (i in curAnim) {
  247. if (curAnim[i] !== true) {
  248. done = false;
  249. }
  250. }
  251. if (done && complete) {
  252. complete.call(elem);
  253. }
  254. ret = false;
  255. } else {
  256. this.pos = options.easing((t - this.startTime) / duration);
  257. this.now = this.start + ((this.end - this.start) * this.pos);
  258. this.update();
  259. ret = true;
  260. }
  261. return ret;
  262. },
  263. /**
  264. * Prepare start and end values so that the path can be animated one to one
  265. */
  266. initPath: function (elem, fromD, toD) {
  267. fromD = fromD || '';
  268. var shift = elem.shift,
  269. bezier = fromD.indexOf('C') > -1,
  270. numParams = bezier ? 7 : 3,
  271. endLength,
  272. slice,
  273. i,
  274. start = fromD.split(' '),
  275. end = [].concat(toD), // copy
  276. isArea = elem.isArea,
  277. positionFactor = isArea ? 2 : 1,
  278. sixify = function (arr) { // in splines make move points have six parameters like bezier curves
  279. i = arr.length;
  280. while (i--) {
  281. if (arr[i] === M || arr[i] === L) {
  282. arr.splice(i + 1, 0, arr[i + 1], arr[i + 2], arr[i + 1], arr[i + 2]);
  283. }
  284. }
  285. };
  286. if (bezier) {
  287. sixify(start);
  288. sixify(end);
  289. }
  290. // If shifting points, prepend a dummy point to the end path. For areas,
  291. // prepend both at the beginning and end of the path.
  292. if (shift <= end.length / numParams && start.length === end.length) {
  293. while (shift--) {
  294. end = end.slice(0, numParams).concat(end);
  295. if (isArea) {
  296. end = end.concat(end.slice(end.length - numParams));
  297. }
  298. }
  299. }
  300. elem.shift = 0; // reset for following animations
  301. // Copy and append last point until the length matches the end length
  302. if (start.length) {
  303. endLength = end.length;
  304. while (start.length < endLength) {
  305. // Pull out the slice that is going to be appended or inserted. In a line graph,
  306. // the positionFactor is 1, and the last point is sliced out. In an area graph,
  307. // the positionFactor is 2, causing the middle two points to be sliced out, since
  308. // an area path starts at left, follows the upper path then turns and follows the
  309. // bottom back.
  310. slice = start.slice().splice(
  311. (start.length / positionFactor) - numParams,
  312. numParams * positionFactor
  313. );
  314. // Disable first control point
  315. if (bezier) {
  316. slice[numParams - 6] = slice[numParams - 2];
  317. slice[numParams - 5] = slice[numParams - 1];
  318. }
  319. // Now insert the slice, either in the middle (for areas) or at the end (for lines)
  320. [].splice.apply(
  321. start,
  322. [(start.length / positionFactor), 0].concat(slice)
  323. );
  324. }
  325. }
  326. return [start, end];
  327. }
  328. }; // End of Fx prototype
  329. /**
  330. * Extend an object with the members of another
  331. * @param {Object} a The object to be extended
  332. * @param {Object} b The object to add to the first one
  333. */
  334. var extend = Highcharts.extend = function (a, b) {
  335. var n;
  336. if (!a) {
  337. a = {};
  338. }
  339. for (n in b) {
  340. a[n] = b[n];
  341. }
  342. return a;
  343. };
  344. /**
  345. * Deep merge two or more objects and return a third object. If the first argument is
  346. * true, the contents of the second object is copied into the first object.
  347. * Previously this function redirected to jQuery.extend(true), but this had two limitations.
  348. * First, it deep merged arrays, which lead to workarounds in Highcharts. Second,
  349. * it copied properties from extended prototypes.
  350. */
  351. function merge() {
  352. var i,
  353. args = arguments,
  354. len,
  355. ret = {},
  356. doCopy = function (copy, original) {
  357. var value, key;
  358. // An object is replacing a primitive
  359. if (typeof copy !== 'object') {
  360. copy = {};
  361. }
  362. for (key in original) {
  363. if (original.hasOwnProperty(key)) {
  364. value = original[key];
  365. // Copy the contents of objects, but not arrays or DOM nodes
  366. if (value && typeof value === 'object' && Object.prototype.toString.call(value) !== '[object Array]' &&
  367. key !== 'renderTo' && typeof value.nodeType !== 'number') {
  368. copy[key] = doCopy(copy[key] || {}, value);
  369. // Primitives and arrays are copied over directly
  370. } else {
  371. copy[key] = original[key];
  372. }
  373. }
  374. }
  375. return copy;
  376. };
  377. // If first argument is true, copy into the existing object. Used in setOptions.
  378. if (args[0] === true) {
  379. ret = args[1];
  380. args = Array.prototype.slice.call(args, 2);
  381. }
  382. // For each argument, extend the return
  383. len = args.length;
  384. for (i = 0; i < len; i++) {
  385. ret = doCopy(ret, args[i]);
  386. }
  387. return ret;
  388. }
  389. /**
  390. * Shortcut for parseInt
  391. * @param {Object} s
  392. * @param {Number} mag Magnitude
  393. */
  394. function pInt(s, mag) {
  395. return parseInt(s, mag || 10);
  396. }
  397. /**
  398. * Check for string
  399. * @param {Object} s
  400. */
  401. function isString(s) {
  402. return typeof s === 'string';
  403. }
  404. /**
  405. * Check for object
  406. * @param {Object} obj
  407. */
  408. function isObject(obj) {
  409. return obj && typeof obj === 'object';
  410. }
  411. /**
  412. * Check for array
  413. * @param {Object} obj
  414. */
  415. function isArray(obj) {
  416. return Object.prototype.toString.call(obj) === '[object Array]';
  417. }
  418. /**
  419. * Check for number
  420. * @param {Object} n
  421. */
  422. var isNumber = Highcharts.isNumber = function isNumber(n) {
  423. return typeof n === 'number' && !isNaN(n);
  424. };
  425. /**
  426. * Remove last occurence of an item from an array
  427. * @param {Array} arr
  428. * @param {Mixed} item
  429. */
  430. function erase(arr, item) {
  431. var i = arr.length;
  432. while (i--) {
  433. if (arr[i] === item) {
  434. arr.splice(i, 1);
  435. break;
  436. }
  437. }
  438. //return arr;
  439. }
  440. /**
  441. * Returns true if the object is not null or undefined.
  442. * @param {Object} obj
  443. */
  444. function defined(obj) {
  445. return obj !== UNDEFINED && obj !== null;
  446. }
  447. /**
  448. * Set or get an attribute or an object of attributes. Can't use jQuery attr because
  449. * it attempts to set expando properties on the SVG element, which is not allowed.
  450. *
  451. * @param {Object} elem The DOM element to receive the attribute(s)
  452. * @param {String|Object} prop The property or an abject of key-value pairs
  453. * @param {String} value The value if a single property is set
  454. */
  455. function attr(elem, prop, value) {
  456. var key,
  457. ret;
  458. // if the prop is a string
  459. if (isString(prop)) {
  460. // set the value
  461. if (defined(value)) {
  462. elem.setAttribute(prop, value);
  463. // get the value
  464. } else if (elem && elem.getAttribute) { // elem not defined when printing pie demo...
  465. ret = elem.getAttribute(prop);
  466. }
  467. // else if prop is defined, it is a hash of key/value pairs
  468. } else if (defined(prop) && isObject(prop)) {
  469. for (key in prop) {
  470. elem.setAttribute(key, prop[key]);
  471. }
  472. }
  473. return ret;
  474. }
  475. /**
  476. * Check if an element is an array, and if not, make it into an array.
  477. */
  478. function splat(obj) {
  479. return isArray(obj) ? obj : [obj];
  480. }
  481. /**
  482. * Set a timeout if the delay is given, otherwise perform the function synchronously
  483. * @param {Function} fn The function to perform
  484. * @param {Number} delay Delay in milliseconds
  485. * @param {Ojbect} context The context
  486. * @returns {Nubmer} An identifier for the timeout
  487. */
  488. function syncTimeout(fn, delay, context) {
  489. if (delay) {
  490. return setTimeout(fn, delay, context);
  491. }
  492. fn.call(0, context);
  493. }
  494. /**
  495. * Return the first value that is defined.
  496. */
  497. var pick = Highcharts.pick = function () {
  498. var args = arguments,
  499. i,
  500. arg,
  501. length = args.length;
  502. for (i = 0; i < length; i++) {
  503. arg = args[i];
  504. if (arg !== UNDEFINED && arg !== null) {
  505. return arg;
  506. }
  507. }
  508. };
  509. /**
  510. * Set CSS on a given element
  511. * @param {Object} el
  512. * @param {Object} styles Style object with camel case property names
  513. */
  514. function css(el, styles) {
  515. if (isMS && !hasSVG) { // #2686
  516. if (styles && styles.opacity !== UNDEFINED) {
  517. styles.filter = 'alpha(opacity=' + (styles.opacity * 100) + ')';
  518. }
  519. }
  520. extend(el.style, styles);
  521. }
  522. /**
  523. * Utility function to create element with attributes and styles
  524. * @param {Object} tag
  525. * @param {Object} attribs
  526. * @param {Object} styles
  527. * @param {Object} parent
  528. * @param {Object} nopad
  529. */
  530. function createElement(tag, attribs, styles, parent, nopad) {
  531. var el = doc.createElement(tag);
  532. if (attribs) {
  533. extend(el, attribs);
  534. }
  535. if (nopad) {
  536. css(el, { padding: 0, border: 'none', margin: 0 });
  537. }
  538. if (styles) {
  539. css(el, styles);
  540. }
  541. if (parent) {
  542. parent.appendChild(el);
  543. }
  544. return el;
  545. }
  546. /**
  547. * Extend a prototyped class by new members
  548. * @param {Object} parent
  549. * @param {Object} members
  550. */
  551. function extendClass(Parent, members) {
  552. var object = function () {
  553. };
  554. object.prototype = new Parent();
  555. extend(object.prototype, members);
  556. return object;
  557. }
  558. /**
  559. * Pad a string to a given length by adding 0 to the beginning
  560. * @param {Number} number
  561. * @param {Number} length
  562. */
  563. function pad(number, length, padder) {
  564. return new Array((length || 2) + 1 - String(number).length).join(padder || 0) + number;
  565. }
  566. /**
  567. * Return a length based on either the integer value, or a percentage of a base.
  568. */
  569. function relativeLength(value, base) {
  570. return (/%$/).test(value) ? base * parseFloat(value) / 100 : parseFloat(value);
  571. }
  572. /**
  573. * Wrap a method with extended functionality, preserving the original function
  574. * @param {Object} obj The context object that the method belongs to
  575. * @param {String} method The name of the method to extend
  576. * @param {Function} func A wrapper function callback. This function is called with the same arguments
  577. * as the original function, except that the original function is unshifted and passed as the first
  578. * argument.
  579. */
  580. var wrap = Highcharts.wrap = function (obj, method, func) {
  581. var proceed = obj[method];
  582. obj[method] = function () {
  583. var args = Array.prototype.slice.call(arguments);
  584. args.unshift(proceed);
  585. return func.apply(this, args);
  586. };
  587. };
  588. function getTZOffset(timestamp) {
  589. return ((getTimezoneOffset && getTimezoneOffset(timestamp)) || timezoneOffset || 0) * 60000;
  590. }
  591. /**
  592. * Based on http://www.php.net/manual/en/function.strftime.php
  593. * @param {String} format
  594. * @param {Number} timestamp
  595. * @param {Boolean} capitalize
  596. */
  597. dateFormat = function (format, timestamp, capitalize) {
  598. if (!isNumber(timestamp)) {
  599. return defaultOptions.lang.invalidDate || '';
  600. }
  601. format = pick(format, '%Y-%m-%d %H:%M:%S');
  602. var date = new Date(timestamp - getTZOffset(timestamp)),
  603. key, // used in for constuct below
  604. // get the basic time values
  605. hours = date[getHours](),
  606. day = date[getDay](),
  607. dayOfMonth = date[getDate](),
  608. month = date[getMonth](),
  609. fullYear = date[getFullYear](),
  610. lang = defaultOptions.lang,
  611. langWeekdays = lang.weekdays,
  612. shortWeekdays = lang.shortWeekdays,
  613. // List all format keys. Custom formats can be added from the outside.
  614. replacements = extend({
  615. // Day
  616. 'a': shortWeekdays ? shortWeekdays[day] : langWeekdays[day].substr(0, 3), // Short weekday, like 'Mon'
  617. 'A': langWeekdays[day], // Long weekday, like 'Monday'
  618. 'd': pad(dayOfMonth), // Two digit day of the month, 01 to 31
  619. 'e': pad(dayOfMonth, 2, ' '), // Day of the month, 1 through 31
  620. 'w': day,
  621. // Week (none implemented)
  622. //'W': weekNumber(),
  623. // Month
  624. 'b': lang.shortMonths[month], // Short month, like 'Jan'
  625. 'B': lang.months[month], // Long month, like 'January'
  626. 'm': pad(month + 1), // Two digit month number, 01 through 12
  627. // Year
  628. 'y': fullYear.toString().substr(2, 2), // Two digits year, like 09 for 2009
  629. 'Y': fullYear, // Four digits year, like 2009
  630. // Time
  631. 'H': pad(hours), // Two digits hours in 24h format, 00 through 23
  632. 'k': hours, // Hours in 24h format, 0 through 23
  633. 'I': pad((hours % 12) || 12), // Two digits hours in 12h format, 00 through 11
  634. 'l': (hours % 12) || 12, // Hours in 12h format, 1 through 12
  635. 'M': pad(date[getMinutes]()), // Two digits minutes, 00 through 59
  636. 'p': hours < 12 ? 'AM' : 'PM', // Upper case AM or PM
  637. 'P': hours < 12 ? 'am' : 'pm', // Lower case AM or PM
  638. 'S': pad(date.getSeconds()), // Two digits seconds, 00 through 59
  639. 'L': pad(mathRound(timestamp % 1000), 3) // Milliseconds (naming from Ruby)
  640. }, Highcharts.dateFormats);
  641. // do the replaces
  642. for (key in replacements) {
  643. while (format.indexOf('%' + key) !== -1) { // regex would do it in one line, but this is faster
  644. format = format.replace('%' + key, typeof replacements[key] === 'function' ? replacements[key](timestamp) : replacements[key]);
  645. }
  646. }
  647. // Optionally capitalize the string and return
  648. return capitalize ? format.substr(0, 1).toUpperCase() + format.substr(1) : format;
  649. };
  650. /**
  651. * Format a single variable. Similar to sprintf, without the % prefix.
  652. */
  653. function formatSingle(format, val) {
  654. var floatRegex = /f$/,
  655. decRegex = /\.([0-9])/,
  656. lang = defaultOptions.lang,
  657. decimals;
  658. if (floatRegex.test(format)) { // float
  659. decimals = format.match(decRegex);
  660. decimals = decimals ? decimals[1] : -1;
  661. if (val !== null) {
  662. val = Highcharts.numberFormat(
  663. val,
  664. decimals,
  665. lang.decimalPoint,
  666. format.indexOf(',') > -1 ? lang.thousandsSep : ''
  667. );
  668. }
  669. } else {
  670. val = dateFormat(format, val);
  671. }
  672. return val;
  673. }
  674. /**
  675. * Format a string according to a subset of the rules of Python's String.format method.
  676. */
  677. function format(str, ctx) {
  678. var splitter = '{',
  679. isInside = false,
  680. segment,
  681. valueAndFormat,
  682. path,
  683. i,
  684. len,
  685. ret = [],
  686. val,
  687. index;
  688. while ((index = str.indexOf(splitter)) !== -1) {
  689. segment = str.slice(0, index);
  690. if (isInside) { // we're on the closing bracket looking back
  691. valueAndFormat = segment.split(':');
  692. path = valueAndFormat.shift().split('.'); // get first and leave format
  693. len = path.length;
  694. val = ctx;
  695. // Assign deeper paths
  696. for (i = 0; i < len; i++) {
  697. val = val[path[i]];
  698. }
  699. // Format the replacement
  700. if (valueAndFormat.length) {
  701. val = formatSingle(valueAndFormat.join(':'), val);
  702. }
  703. // Push the result and advance the cursor
  704. ret.push(val);
  705. } else {
  706. ret.push(segment);
  707. }
  708. str = str.slice(index + 1); // the rest
  709. isInside = !isInside; // toggle
  710. splitter = isInside ? '}' : '{'; // now look for next matching bracket
  711. }
  712. ret.push(str);
  713. return ret.join('');
  714. }
  715. /**
  716. * Get the magnitude of a number
  717. */
  718. function getMagnitude(num) {
  719. return math.pow(10, mathFloor(math.log(num) / math.LN10));
  720. }
  721. /**
  722. * Take an interval and normalize it to multiples of 1, 2, 2.5 and 5
  723. * @param {Number} interval
  724. * @param {Array} multiples
  725. * @param {Number} magnitude
  726. * @param {Object} options
  727. */
  728. function normalizeTickInterval(interval, multiples, magnitude, allowDecimals, preventExceed) {
  729. var normalized,
  730. i,
  731. retInterval = interval;
  732. // round to a tenfold of 1, 2, 2.5 or 5
  733. magnitude = pick(magnitude, 1);
  734. normalized = interval / magnitude;
  735. // multiples for a linear scale
  736. if (!multiples) {
  737. multiples = [1, 2, 2.5, 5, 10];
  738. // the allowDecimals option
  739. if (allowDecimals === false) {
  740. if (magnitude === 1) {
  741. multiples = [1, 2, 5, 10];
  742. } else if (magnitude <= 0.1) {
  743. multiples = [1 / magnitude];
  744. }
  745. }
  746. }
  747. // normalize the interval to the nearest multiple
  748. for (i = 0; i < multiples.length; i++) {
  749. retInterval = multiples[i];
  750. if ((preventExceed && retInterval * magnitude >= interval) || // only allow tick amounts smaller than natural
  751. (!preventExceed && (normalized <= (multiples[i] + (multiples[i + 1] || multiples[i])) / 2))) {
  752. break;
  753. }
  754. }
  755. // multiply back to the correct magnitude
  756. retInterval *= magnitude;
  757. return retInterval;
  758. }
  759. /**
  760. * Utility method that sorts an object array and keeping the order of equal items.
  761. * ECMA script standard does not specify the behaviour when items are equal.
  762. */
  763. function stableSort(arr, sortFunction) {
  764. var length = arr.length,
  765. sortValue,
  766. i;
  767. // Add index to each item
  768. for (i = 0; i < length; i++) {
  769. arr[i].safeI = i; // stable sort index
  770. }
  771. arr.sort(function (a, b) {
  772. sortValue = sortFunction(a, b);
  773. return sortValue === 0 ? a.safeI - b.safeI : sortValue;
  774. });
  775. // Remove index from items
  776. for (i = 0; i < length; i++) {
  777. delete arr[i].safeI; // stable sort index
  778. }
  779. }
  780. /**
  781. * Non-recursive method to find the lowest member of an array. Math.min raises a maximum
  782. * call stack size exceeded error in Chrome when trying to apply more than 150.000 points. This
  783. * method is slightly slower, but safe.
  784. */
  785. function arrayMin(data) {
  786. var i = data.length,
  787. min = data[0];
  788. while (i--) {
  789. if (data[i] < min) {
  790. min = data[i];
  791. }
  792. }
  793. return min;
  794. }
  795. /**
  796. * Non-recursive method to find the lowest member of an array. Math.min raises a maximum
  797. * call stack size exceeded error in Chrome when trying to apply more than 150.000 points. This
  798. * method is slightly slower, but safe.
  799. */
  800. function arrayMax(data) {
  801. var i = data.length,
  802. max = data[0];
  803. while (i--) {
  804. if (data[i] > max) {
  805. max = data[i];
  806. }
  807. }
  808. return max;
  809. }
  810. /**
  811. * Utility method that destroys any SVGElement or VMLElement that are properties on the given object.
  812. * It loops all properties and invokes destroy if there is a destroy method. The property is
  813. * then delete'ed.
  814. * @param {Object} The object to destroy properties on
  815. * @param {Object} Exception, do not destroy this property, only delete it.
  816. */
  817. function destroyObjectProperties(obj, except) {
  818. var n;
  819. for (n in obj) {
  820. // If the object is non-null and destroy is defined
  821. if (obj[n] && obj[n] !== except && obj[n].destroy) {
  822. // Invoke the destroy
  823. obj[n].destroy();
  824. }
  825. // Delete the property from the object.
  826. delete obj[n];
  827. }
  828. }
  829. /**
  830. * Discard an element by moving it to the bin and delete
  831. * @param {Object} The HTML node to discard
  832. */
  833. function discardElement(element) {
  834. // create a garbage bin element, not part of the DOM
  835. if (!garbageBin) {
  836. garbageBin = createElement(DIV);
  837. }
  838. // move the node and empty bin
  839. if (element) {
  840. garbageBin.appendChild(element);
  841. }
  842. garbageBin.innerHTML = '';
  843. }
  844. /**
  845. * Fix JS round off float errors
  846. * @param {Number} num
  847. */
  848. function correctFloat(num, prec) {
  849. return parseFloat(
  850. num.toPrecision(prec || 14)
  851. );
  852. }
  853. /**
  854. * Set the global animation to either a given value, or fall back to the
  855. * given chart's animation option
  856. * @param {Object} animation
  857. * @param {Object} chart
  858. */
  859. function setAnimation(animation, chart) {
  860. chart.renderer.globalAnimation = pick(animation, chart.animation);
  861. }
  862. /**
  863. * Get the animation in object form, where a disabled animation is always
  864. * returned with duration: 0
  865. */
  866. function animObject(animation) {
  867. return isObject(animation) ? merge(animation) : { duration: animation ? 500 : 0 };
  868. }
  869. /**
  870. * The time unit lookup
  871. */
  872. timeUnits = {
  873. millisecond: 1,
  874. second: 1000,
  875. minute: 60000,
  876. hour: 3600000,
  877. day: 24 * 3600000,
  878. week: 7 * 24 * 3600000,
  879. month: 28 * 24 * 3600000,
  880. year: 364 * 24 * 3600000
  881. };
  882. /**
  883. * Format a number and return a string based on input settings
  884. * @param {Number} number The input number to format
  885. * @param {Number} decimals The amount of decimals
  886. * @param {String} decimalPoint The decimal point, defaults to the one given in the lang options
  887. * @param {String} thousandsSep The thousands separator, defaults to the one given in the lang options
  888. */
  889. Highcharts.numberFormat = function (number, decimals, decimalPoint, thousandsSep) {
  890. number = +number || 0;
  891. decimals = +decimals;
  892. var lang = defaultOptions.lang,
  893. origDec = (number.toString().split('.')[1] || '').length,
  894. decimalComponent,
  895. strinteger,
  896. thousands,
  897. absNumber = Math.abs(number),
  898. ret;
  899. if (decimals === -1) {
  900. decimals = Math.min(origDec, 20); // Preserve decimals. Not huge numbers (#3793).
  901. } else if (!isNumber(decimals)) {
  902. decimals = 2;
  903. }
  904. // A string containing the positive integer component of the number
  905. strinteger = String(pInt(absNumber.toFixed(decimals)));
  906. // Leftover after grouping into thousands. Can be 0, 1 or 3.
  907. thousands = strinteger.length > 3 ? strinteger.length % 3 : 0;
  908. // Language
  909. decimalPoint = pick(decimalPoint, lang.decimalPoint);
  910. thousandsSep = pick(thousandsSep, lang.thousandsSep);
  911. // Start building the return
  912. ret = number < 0 ? '-' : '';
  913. // Add the leftover after grouping into thousands. For example, in the number 42 000 000,
  914. // this line adds 42.
  915. ret += thousands ? strinteger.substr(0, thousands) + thousandsSep : '';
  916. // Add the remaining thousands groups, joined by the thousands separator
  917. ret += strinteger.substr(thousands).replace(/(\d{3})(?=\d)/g, '$1' + thousandsSep);
  918. // Add the decimal point and the decimal component
  919. if (decimals) {
  920. // Get the decimal component, and add power to avoid rounding errors with float numbers (#4573)
  921. decimalComponent = Math.abs(absNumber - strinteger + Math.pow(10, -Math.max(decimals, origDec) - 1));
  922. ret += decimalPoint + decimalComponent.toFixed(decimals).slice(2);
  923. }
  924. return ret;
  925. };
  926. /**
  927. * Easing definition
  928. * @param {Number} pos Current position, ranging from 0 to 1
  929. */
  930. Math.easeInOutSine = function (pos) {
  931. return -0.5 * (Math.cos(Math.PI * pos) - 1);
  932. };
  933. /**
  934. * Internal method to return CSS value for given element and property
  935. */
  936. getStyle = function (el, prop) {
  937. var style;
  938. // For width and height, return the actual inner pixel size (#4913)
  939. if (prop === 'width') {
  940. return Math.min(el.offsetWidth, el.scrollWidth) - getStyle(el, 'padding-left') - getStyle(el, 'padding-right');
  941. } else if (prop === 'height') {
  942. return Math.min(el.offsetHeight, el.scrollHeight) - getStyle(el, 'padding-top') - getStyle(el, 'padding-bottom');
  943. }
  944. // Otherwise, get the computed style
  945. style = win.getComputedStyle(el, undefined);
  946. return style && pInt(style.getPropertyValue(prop));
  947. };
  948. /**
  949. * Return the index of an item in an array, or -1 if not found
  950. */
  951. inArray = function (item, arr) {
  952. return arr.indexOf ? arr.indexOf(item) : [].indexOf.call(arr, item);
  953. };
  954. /**
  955. * Filter an array
  956. */
  957. grep = function (elements, callback) {
  958. return [].filter.call(elements, callback);
  959. };
  960. /**
  961. * Map an array
  962. */
  963. map = function (arr, fn) {
  964. var results = [],
  965. i = 0,
  966. len = arr.length;
  967. for (; i < len; i++) {
  968. results[i] = fn.call(arr[i], arr[i], i, arr);
  969. }
  970. return results;
  971. };
  972. /**
  973. * Get the element's offset position, corrected by overflow:auto.
  974. */
  975. offset = function (el) {
  976. var docElem = doc.documentElement,
  977. box = el.getBoundingClientRect();
  978. return {
  979. top: box.top + (win.pageYOffset || docElem.scrollTop) - (docElem.clientTop || 0),
  980. left: box.left + (win.pageXOffset || docElem.scrollLeft) - (docElem.clientLeft || 0)
  981. };
  982. };
  983. /**
  984. * Stop running animation.
  985. * A possible extension to this would be to stop a single property, when
  986. * we want to continue animating others. Then assign the prop to the timer
  987. * in the Fx.run method, and check for the prop here. This would be an improvement
  988. * in all cases where we stop the animation from .attr. Instead of stopping
  989. * everything, we can just stop the actual attributes we're setting.
  990. */
  991. stop = function (el) {
  992. var i = timers.length;
  993. // Remove timers related to this element (#4519)
  994. while (i--) {
  995. if (timers[i].elem === el) {
  996. timers[i].stopped = true; // #4667
  997. }
  998. }
  999. };
  1000. /**
  1001. * Utility for iterating over an array.
  1002. * @param {Array} arr
  1003. * @param {Function} fn
  1004. */
  1005. each = function (arr, fn) { // modern browsers
  1006. return Array.prototype.forEach.call(arr, fn);
  1007. };
  1008. /**
  1009. * Add an event listener
  1010. */
  1011. addEvent = function (el, type, fn) {
  1012. var events = el.hcEvents = el.hcEvents || {};
  1013. function wrappedFn(e) {
  1014. e.target = e.srcElement || win; // #2820
  1015. fn.call(el, e);
  1016. }
  1017. // Handle DOM events in modern browsers
  1018. if (el.addEventListener) {
  1019. el.addEventListener(type, fn, false);
  1020. // Handle old IE implementation
  1021. } else if (el.attachEvent) {
  1022. if (!el.hcEventsIE) {
  1023. el.hcEventsIE = {};
  1024. }
  1025. // Link wrapped fn with original fn, so we can get this in removeEvent
  1026. el.hcEventsIE[fn.toString()] = wrappedFn;
  1027. el.attachEvent('on' + type, wrappedFn);
  1028. }
  1029. if (!events[type]) {
  1030. events[type] = [];
  1031. }
  1032. events[type].push(fn);
  1033. };
  1034. /**
  1035. * Remove event added with addEvent
  1036. */
  1037. removeEvent = function (el, type, fn) {
  1038. var events,
  1039. hcEvents = el.hcEvents,
  1040. index;
  1041. function removeOneEvent(type, fn) {
  1042. if (el.removeEventListener) {
  1043. el.removeEventListener(type, fn, false);
  1044. } else if (el.attachEvent) {
  1045. fn = el.hcEventsIE[fn.toString()];
  1046. el.detachEvent('on' + type, fn);
  1047. }
  1048. }
  1049. function removeAllEvents() {
  1050. var types,
  1051. len,
  1052. n;
  1053. if (!el.nodeName) {
  1054. return; // break on non-DOM events
  1055. }
  1056. if (type) {
  1057. types = {};
  1058. types[type] = true;
  1059. } else {
  1060. types = hcEvents;
  1061. }
  1062. for (n in types) {
  1063. if (hcEvents[n]) {
  1064. len = hcEvents[n].length;
  1065. while (len--) {
  1066. removeOneEvent(n, hcEvents[n][len]);
  1067. }
  1068. }
  1069. }
  1070. }
  1071. if (hcEvents) {
  1072. if (type) {
  1073. events = hcEvents[type] || [];
  1074. if (fn) {
  1075. index = inArray(fn, events);
  1076. if (index > -1) {
  1077. events.splice(index, 1);
  1078. hcEvents[type] = events;
  1079. }
  1080. removeOneEvent(type, fn);
  1081. } else {
  1082. removeAllEvents();
  1083. hcEvents[type] = [];
  1084. }
  1085. } else {
  1086. removeAllEvents();
  1087. el.hcEvents = {};
  1088. }
  1089. }
  1090. };
  1091. /**
  1092. * Fire an event on a custom object
  1093. */
  1094. fireEvent = function (el, type, eventArguments, defaultFunction) {
  1095. var e,
  1096. hcEvents = el.hcEvents,
  1097. events,
  1098. len,
  1099. i,
  1100. fn;
  1101. eventArguments = eventArguments || {};
  1102. if (doc.createEvent && (el.dispatchEvent || el.fireEvent)) {
  1103. e = doc.createEvent('Events');
  1104. e.initEvent(type, true, true);
  1105. e.target = el;
  1106. extend(e, eventArguments);
  1107. if (el.dispatchEvent) {
  1108. el.dispatchEvent(e);
  1109. } else {
  1110. el.fireEvent(type, e);
  1111. }
  1112. } else if (hcEvents) {
  1113. events = hcEvents[type] || [];
  1114. len = events.length;
  1115. // Attach a simple preventDefault function to skip default handler if called.
  1116. // The built-in defaultPrevented property is not overwritable (#5112)
  1117. if (!eventArguments.preventDefault) {
  1118. eventArguments.preventDefault = function () {
  1119. eventArguments.defaultPrevented = true;
  1120. };
  1121. }
  1122. eventArguments.target = el;
  1123. // If the type is not set, we're running a custom event (#2297). If it is set,
  1124. // we're running a browser event, and setting it will cause en error in
  1125. // IE8 (#2465).
  1126. if (!eventArguments.type) {
  1127. eventArguments.type = type;
  1128. }
  1129. for (i = 0; i < len; i++) {
  1130. fn = events[i];
  1131. // If the event handler return false, prevent the default handler from executing
  1132. if (fn.call(el, eventArguments) === false) {
  1133. eventArguments.preventDefault();
  1134. }
  1135. }
  1136. }
  1137. // Run the default if not prevented
  1138. if (defaultFunction && !eventArguments.defaultPrevented) {
  1139. defaultFunction(eventArguments);
  1140. }
  1141. };
  1142. /**
  1143. * The global animate method, which uses Fx to create individual animators.
  1144. */
  1145. animate = function (el, params, opt) {
  1146. var start,
  1147. unit = '',
  1148. end,
  1149. fx,
  1150. args,
  1151. prop;
  1152. if (!isObject(opt)) { // Number or undefined/null
  1153. args = arguments;
  1154. opt = {
  1155. duration: args[2],
  1156. easing: args[3],
  1157. complete: args[4]
  1158. };
  1159. }
  1160. if (!isNumber(opt.duration)) {
  1161. opt.duration = 400;
  1162. }
  1163. opt.easing = typeof opt.easing === 'function' ? opt.easing : (Math[opt.easing] || Math.easeInOutSine);
  1164. opt.curAnim = merge(params);
  1165. for (prop in params) {
  1166. fx = new Fx(el, opt, prop);
  1167. end = null;
  1168. if (prop === 'd') {
  1169. fx.paths = fx.initPath(
  1170. el,
  1171. el.d,
  1172. params.d
  1173. );
  1174. fx.toD = params.d;
  1175. start = 0;
  1176. end = 1;
  1177. } else if (el.attr) {
  1178. start = el.attr(prop);
  1179. } else {
  1180. start = parseFloat(getStyle(el, prop)) || 0;
  1181. if (prop !== 'opacity') {
  1182. unit = 'px';
  1183. }
  1184. }
  1185. if (!end) {
  1186. end = params[prop];
  1187. }
  1188. if (end.match && end.match('px')) {
  1189. end = end.replace(/px/g, ''); // #4351
  1190. }
  1191. fx.run(start, end, unit);
  1192. }
  1193. };
  1194. /**
  1195. * Register Highcharts as a plugin in jQuery
  1196. */
  1197. if (win.jQuery) {
  1198. win.jQuery.fn.highcharts = function () {
  1199. var args = [].slice.call(arguments);
  1200. if (this[0]) { // this[0] is the renderTo div
  1201. // Create the chart
  1202. if (args[0]) {
  1203. new Highcharts[ // eslint-disable-line no-new
  1204. isString(args[0]) ? args.shift() : 'Chart' // Constructor defaults to Chart
  1205. ](this[0], args[0], args[1]);
  1206. return this;
  1207. }
  1208. // When called without parameters or with the return argument, return an existing chart
  1209. return charts[attr(this[0], 'data-highcharts-chart')];
  1210. }
  1211. };
  1212. }
  1213. /**
  1214. * Compatibility section to add support for legacy IE. This can be removed if old IE
  1215. * support is not needed.
  1216. */
  1217. if (doc && !doc.defaultView) {
  1218. getStyle = function (el, prop) {
  1219. var val,
  1220. alias = { width: 'clientWidth', height: 'clientHeight' }[prop];
  1221. if (el.style[prop]) {
  1222. return pInt(el.style[prop]);
  1223. }
  1224. if (prop === 'opacity') {
  1225. prop = 'filter';
  1226. }
  1227. // Getting the rendered width and height
  1228. if (alias) {
  1229. el.style.zoom = 1;
  1230. return Math.max(el[alias] - 2 * getStyle(el, 'padding'), 0);
  1231. }
  1232. val = el.currentStyle[prop.replace(/\-(\w)/g, function (a, b) {
  1233. return b.toUpperCase();
  1234. })];
  1235. if (prop === 'filter') {
  1236. val = val.replace(
  1237. /alpha\(opacity=([0-9]+)\)/,
  1238. function (a, b) {
  1239. return b / 100;
  1240. }
  1241. );
  1242. }
  1243. return val === '' ? 1 : pInt(val);
  1244. };
  1245. }
  1246. if (!Array.prototype.forEach) {
  1247. each = function (arr, fn) { // legacy
  1248. var i = 0,
  1249. len = arr.length;
  1250. for (; i < len; i++) {
  1251. if (fn.call(arr[i], arr[i], i, arr) === false) {
  1252. return i;
  1253. }
  1254. }
  1255. };
  1256. }
  1257. if (!Array.prototype.indexOf) {
  1258. inArray = function (item, arr) {
  1259. var len,
  1260. i = 0;
  1261. if (arr) {
  1262. len = arr.length;
  1263. for (; i < len; i++) {
  1264. if (arr[i] === item) {
  1265. return i;
  1266. }
  1267. }
  1268. }
  1269. return -1;
  1270. };
  1271. }
  1272. if (!Array.prototype.filter) {
  1273. grep = function (elements, fn) {
  1274. var ret = [],
  1275. i = 0,
  1276. length = elements.length;
  1277. for (; i < length; i++) {
  1278. if (fn(elements[i], i)) {
  1279. ret.push(elements[i]);
  1280. }
  1281. }
  1282. return ret;
  1283. };
  1284. }
  1285. //--- End compatibility section ---
  1286. // Expose utilities
  1287. Highcharts.Fx = Fx;
  1288. Highcharts.inArray = inArray;
  1289. Highcharts.each = each;
  1290. Highcharts.grep = grep;
  1291. Highcharts.offset = offset;
  1292. Highcharts.map = map;
  1293. Highcharts.addEvent = addEvent;
  1294. Highcharts.removeEvent = removeEvent;
  1295. Highcharts.fireEvent = fireEvent;
  1296. Highcharts.animate = animate;
  1297. Highcharts.animObject = animObject;
  1298. Highcharts.stop = stop;
  1299. /* ****************************************************************************
  1300. * Handle the options *
  1301. *****************************************************************************/
  1302. defaultOptions = {
  1303. colors: ['#7cb5ec', '#434348', '#90ed7d', '#f7a35c',
  1304. '#8085e9', '#f15c80', '#e4d354', '#2b908f', '#f45b5b', '#91e8e1'],
  1305. symbols: ['circle', 'diamond', 'square', 'triangle', 'triangle-down'],
  1306. lang: {
  1307. loading: 'Loading...',
  1308. months: ['January', 'February', 'March', 'April', 'May', 'June', 'July',
  1309. 'August', 'September', 'October', 'November', 'December'],
  1310. shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  1311. weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
  1312. // invalidDate: '',
  1313. decimalPoint: '.',
  1314. numericSymbols: ['k', 'M', 'G', 'T', 'P', 'E'], // SI prefixes used in axis labels
  1315. resetZoom: 'Reset zoom',
  1316. resetZoomTitle: 'Reset zoom level 1:1',
  1317. thousandsSep: ' '
  1318. },
  1319. global: {
  1320. useUTC: true,
  1321. //timezoneOffset: 0,
  1322. canvasToolsURL: 'http://code.highcharts.com/modules/canvas-tools.js',
  1323. VMLRadialGradientURL: 'http://code.highcharts.com/4.2.5/gfx/vml-radial-gradient.png'
  1324. },
  1325. chart: {
  1326. //animation: true,
  1327. //alignTicks: false,
  1328. //reflow: true,
  1329. //className: null,
  1330. //events: { load, selection },
  1331. //margin: [null],
  1332. //marginTop: null,
  1333. //marginRight: null,
  1334. //marginBottom: null,
  1335. //marginLeft: null,
  1336. borderColor: '#4572A7',
  1337. //borderWidth: 0,
  1338. borderRadius: 0,
  1339. defaultSeriesType: 'line',
  1340. ignoreHiddenSeries: true,
  1341. //inverted: false,
  1342. //shadow: false,
  1343. spacing: [10, 10, 15, 10],
  1344. //spacingTop: 10,
  1345. //spacingRight: 10,
  1346. //spacingBottom: 15,
  1347. //spacingLeft: 10,
  1348. //style: {
  1349. // fontFamily: '"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif', // default font
  1350. // fontSize: '12px'
  1351. //},
  1352. backgroundColor: '#FFFFFF',
  1353. //plotBackgroundColor: null,
  1354. plotBorderColor: '#C0C0C0',
  1355. //plotBorderWidth: 0,
  1356. //plotShadow: false,
  1357. //zoomType: ''
  1358. resetZoomButton: {
  1359. theme: {
  1360. zIndex: 20
  1361. },
  1362. position: {
  1363. align: 'right',
  1364. x: -10,
  1365. //verticalAlign: 'top',
  1366. y: 10
  1367. }
  1368. // relativeTo: 'plot'
  1369. }
  1370. },
  1371. title: {
  1372. text: 'Chart title',
  1373. align: 'center',
  1374. // floating: false,
  1375. margin: 15,
  1376. // x: 0,
  1377. // verticalAlign: 'top',
  1378. // y: null,
  1379. style: {
  1380. color: '#333333',
  1381. fontSize: '18px'
  1382. },
  1383. widthAdjust: -44
  1384. },
  1385. subtitle: {
  1386. text: '',
  1387. align: 'center',
  1388. // floating: false
  1389. // x: 0,
  1390. // verticalAlign: 'top',
  1391. // y: null,
  1392. style: {
  1393. color: '#555555'
  1394. },
  1395. widthAdjust: -44
  1396. },
  1397. plotOptions: {
  1398. line: { // base series options
  1399. allowPointSelect: false,
  1400. showCheckbox: false,
  1401. animation: {
  1402. duration: 1000
  1403. },
  1404. //connectNulls: false,
  1405. //cursor: 'default',
  1406. //clip: true,
  1407. //dashStyle: null,
  1408. //enableMouseTracking: true,
  1409. events: {},
  1410. //legendIndex: 0,
  1411. //linecap: 'round',
  1412. lineWidth: 2,
  1413. //shadow: false,
  1414. // stacking: null,
  1415. marker: {
  1416. //enabled: true,
  1417. //symbol: null,
  1418. lineWidth: 0,
  1419. radius: 4,
  1420. lineColor: '#FFFFFF',
  1421. //fillColor: null,
  1422. states: { // states for a single point
  1423. hover: {
  1424. enabled: true,
  1425. lineWidthPlus: 1,
  1426. radiusPlus: 2
  1427. },
  1428. select: {
  1429. fillColor: '#FFFFFF',
  1430. lineColor: '#000000',
  1431. lineWidth: 2
  1432. }
  1433. }
  1434. },
  1435. point: {
  1436. events: {}
  1437. },
  1438. dataLabels: {
  1439. align: 'center',
  1440. // defer: true,
  1441. // enabled: false,
  1442. formatter: function () {
  1443. return this.y === null ? '' : Highcharts.numberFormat(this.y, -1);
  1444. },
  1445. style: {
  1446. color: 'contrast',
  1447. fontSize: '11px',
  1448. fontWeight: 'bold',
  1449. textShadow: '0 0 6px contrast, 0 0 3px contrast'
  1450. },
  1451. verticalAlign: 'bottom', // above singular point
  1452. x: 0,
  1453. y: 0,
  1454. // backgroundColor: undefined,
  1455. // borderColor: undefined,
  1456. // borderRadius: undefined,
  1457. // borderWidth: undefined,
  1458. padding: 5
  1459. // shadow: false
  1460. },
  1461. cropThreshold: 300, // draw points outside the plot area when the number of points is less than this
  1462. pointRange: 0,
  1463. //pointStart: 0,
  1464. //pointInterval: 1,
  1465. //showInLegend: null, // auto: true for standalone series, false for linked series
  1466. softThreshold: true,
  1467. states: { // states for the entire series
  1468. hover: {
  1469. //enabled: false,
  1470. lineWidthPlus: 1,
  1471. marker: {
  1472. // lineWidth: base + 1,
  1473. // radius: base + 1
  1474. },
  1475. halo: {
  1476. size: 10,
  1477. opacity: 0.25
  1478. }
  1479. },
  1480. select: {
  1481. marker: {}
  1482. }
  1483. },
  1484. stickyTracking: true,
  1485. //tooltip: {
  1486. //pointFormat: '<span style="color:{point.color}">\u25CF</span> {series.name}: <b>{point.y}</b>'
  1487. //valueDecimals: null,
  1488. //xDateFormat: '%A, %b %e, %Y',
  1489. //valuePrefix: '',
  1490. //ySuffix: ''
  1491. //}
  1492. turboThreshold: 1000
  1493. // zIndex: null
  1494. }
  1495. },
  1496. labels: {
  1497. //items: [],
  1498. style: {
  1499. //font: defaultFont,
  1500. position: ABSOLUTE,
  1501. color: '#3E576F'
  1502. }
  1503. },
  1504. legend: {
  1505. enabled: true,
  1506. align: 'center',
  1507. //floating: false,
  1508. layout: 'horizontal',
  1509. labelFormatter: function () {
  1510. return this.name;
  1511. },
  1512. //borderWidth: 0,
  1513. borderColor: '#909090',
  1514. borderRadius: 0,
  1515. navigation: {
  1516. // animation: true,
  1517. activeColor: '#274b6d',
  1518. // arrowSize: 12
  1519. inactiveColor: '#CCC'
  1520. // style: {} // text styles
  1521. },
  1522. // margin: 20,
  1523. // reversed: false,
  1524. shadow: false,
  1525. // backgroundColor: null,
  1526. /*style: {
  1527. padding: '5px'
  1528. },*/
  1529. itemStyle: {
  1530. color: '#333333',
  1531. fontSize: '12px',
  1532. fontWeight: 'bold'
  1533. },
  1534. itemHoverStyle: {
  1535. //cursor: 'pointer', removed as of #601
  1536. color: '#000'
  1537. },
  1538. itemHiddenStyle: {
  1539. color: '#CCC'
  1540. },
  1541. itemCheckboxStyle: {
  1542. position: ABSOLUTE,
  1543. width: '13px', // for IE precision
  1544. height: '13px'
  1545. },
  1546. // itemWidth: undefined,
  1547. // symbolRadius: 0,
  1548. // symbolWidth: 16,
  1549. symbolPadding: 5,
  1550. verticalAlign: 'bottom',
  1551. // width: undefined,
  1552. x: 0,
  1553. y: 0,
  1554. title: {
  1555. //text: null,
  1556. style: {
  1557. fontWeight: 'bold'
  1558. }
  1559. }
  1560. },
  1561. loading: {
  1562. // hideDuration: 100,
  1563. labelStyle: {
  1564. fontWeight: 'bold',
  1565. position: RELATIVE,
  1566. top: '45%'
  1567. },
  1568. // showDuration: 0,
  1569. style: {
  1570. position: ABSOLUTE,
  1571. backgroundColor: 'white',
  1572. opacity: 0.5,
  1573. textAlign: 'center'
  1574. }
  1575. },
  1576. tooltip: {
  1577. enabled: true,
  1578. animation: hasSVG,
  1579. //crosshairs: null,
  1580. backgroundColor: 'rgba(249, 249, 249, .85)',
  1581. borderWidth: 1,
  1582. borderRadius: 3,
  1583. dateTimeLabelFormats: {
  1584. millisecond: '%A, %b %e, %H:%M:%S.%L',
  1585. second: '%A, %b %e, %H:%M:%S',
  1586. minute: '%A, %b %e, %H:%M',
  1587. hour: '%A, %b %e, %H:%M',
  1588. day: '%A, %b %e, %Y',
  1589. week: 'Week from %A, %b %e, %Y',
  1590. month: '%B %Y',
  1591. year: '%Y'
  1592. },
  1593. footerFormat: '',
  1594. //formatter: defaultFormatter,
  1595. headerFormat: '<span style="font-size: 10px">{point.key}</span><br/>',
  1596. pointFormat: '<span style="color:{point.color}">\u25CF</span> {series.name}: <b>{point.y}</b><br/>',
  1597. shadow: true,
  1598. //shape: 'callout',
  1599. //shared: false,
  1600. snap: isTouchDevice ? 25 : 10,
  1601. style: {
  1602. color: '#333333',
  1603. cursor: 'default',
  1604. fontSize: '12px',
  1605. padding: '8px',
  1606. pointerEvents: 'none', // #1686 http://caniuse.com/#feat=pointer-events
  1607. whiteSpace: 'nowrap'
  1608. }
  1609. //xDateFormat: '%A, %b %e, %Y',
  1610. //valueDecimals: null,
  1611. //valuePrefix: '',
  1612. //valueSuffix: ''
  1613. },
  1614. credits: {
  1615. enabled: true,
  1616. text: 'Highcharts.com',
  1617. href: 'http://www.highcharts.com',
  1618. position: {
  1619. align: 'right',
  1620. x: -10,
  1621. verticalAlign: 'bottom',
  1622. y: -5
  1623. },
  1624. style: {
  1625. cursor: 'pointer',
  1626. color: '#909090',
  1627. fontSize: '9px'
  1628. }
  1629. }
  1630. };
  1631. /**
  1632. * Set the time methods globally based on the useUTC option. Time method can be either
  1633. * local time or UTC (default).
  1634. */
  1635. function setTimeMethods() {
  1636. var globalOptions = defaultOptions.global,
  1637. useUTC = globalOptions.useUTC,
  1638. GET = useUTC ? 'getUTC' : 'get',
  1639. SET = useUTC ? 'setUTC' : 'set';
  1640. Date = globalOptions.Date || win.Date;
  1641. timezoneOffset = useUTC && globalOptions.timezoneOffset;
  1642. getTimezoneOffset = useUTC && globalOptions.getTimezoneOffset;
  1643. makeTime = function (year, month, date, hours, minutes, seconds) {
  1644. var d;
  1645. if (useUTC) {
  1646. d = Date.UTC.apply(0, arguments);
  1647. d += getTZOffset(d);
  1648. } else {
  1649. d = new Date(
  1650. year,
  1651. month,
  1652. pick(date, 1),
  1653. pick(hours, 0),
  1654. pick(minutes, 0),
  1655. pick(seconds, 0)
  1656. ).getTime();
  1657. }
  1658. return d;
  1659. };
  1660. getMinutes = GET + 'Minutes';
  1661. getHours = GET + 'Hours';
  1662. getDay = GET + 'Day';
  1663. getDate = GET + 'Date';
  1664. getMonth = GET + 'Month';
  1665. getFullYear = GET + 'FullYear';
  1666. setMilliseconds = SET + 'Milliseconds';
  1667. setSeconds = SET + 'Seconds';
  1668. setMinutes = SET + 'Minutes';
  1669. setHours = SET + 'Hours';
  1670. setDate = SET + 'Date';
  1671. setMonth = SET + 'Month';
  1672. setFullYear = SET + 'FullYear';
  1673. }
  1674. /**
  1675. * Merge the default options with custom options and return the new options structure
  1676. * @param {Object} options The new custom options
  1677. */
  1678. function setOptions(options) {
  1679. // Copy in the default options
  1680. defaultOptions = merge(true, defaultOptions, options);
  1681. // Apply UTC
  1682. setTimeMethods();
  1683. return defaultOptions;
  1684. }
  1685. /**
  1686. * Get the updated default options. Until 3.0.7, merely exposing defaultOptions for outside modules
  1687. * wasn't enough because the setOptions method created a new object.
  1688. */
  1689. function getOptions() {
  1690. return defaultOptions;
  1691. }
  1692. // Series defaults
  1693. var defaultPlotOptions = defaultOptions.plotOptions,
  1694. defaultSeriesOptions = defaultPlotOptions.line;
  1695. // set the default time methods
  1696. setTimeMethods();
  1697. /**
  1698. * Handle color operations. The object methods are chainable.
  1699. * @param {String} input The input color in either rbga or hex format
  1700. */
  1701. function Color(input) {
  1702. // Backwards compatibility, allow instanciation without new
  1703. if (!(this instanceof Color)) {
  1704. return new Color(input);
  1705. }
  1706. // Initialize
  1707. this.init(input);
  1708. }
  1709. Color.prototype = {
  1710. // Collection of parsers. This can be extended from the outside by pushing parsers
  1711. // to Highcharts.Colors.prototype.parsers.
  1712. parsers: [{
  1713. // RGBA color
  1714. regex: /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*\)/,
  1715. parse: function (result) {
  1716. return [pInt(result[1]), pInt(result[2]), pInt(result[3]), parseFloat(result[4], 10)];
  1717. }
  1718. }, {
  1719. // HEX color
  1720. regex: /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/,
  1721. parse: function (result) {
  1722. return [pInt(result[1], 16), pInt(result[2], 16), pInt(result[3], 16), 1];
  1723. }
  1724. }, {
  1725. // RGB color
  1726. regex: /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/,
  1727. parse: function (result) {
  1728. return [pInt(result[1]), pInt(result[2]), pInt(result[3]), 1];
  1729. }
  1730. }],
  1731. /**
  1732. * Parse the input color to rgba array
  1733. * @param {String} input
  1734. */
  1735. init: function (input) {
  1736. var result,
  1737. rgba,
  1738. i,
  1739. parser;
  1740. this.input = input;
  1741. // Gradients
  1742. if (input && input.stops) {
  1743. this.stops = map(input.stops, function (stop) {
  1744. return new Color(stop[1]);
  1745. });
  1746. // Solid colors
  1747. } else {
  1748. i = this.parsers.length;
  1749. while (i-- && !rgba) {
  1750. parser = this.parsers[i];
  1751. result = parser.regex.exec(input);
  1752. if (result) {
  1753. rgba = parser.parse(result);
  1754. }
  1755. }
  1756. }
  1757. this.rgba = rgba || [];
  1758. },
  1759. /**
  1760. * Return the color a specified format
  1761. * @param {String} format
  1762. */
  1763. get: function (format) {
  1764. var input = this.input,
  1765. rgba = this.rgba,
  1766. ret;
  1767. if (this.stops) {
  1768. ret = merge(input);
  1769. ret.stops = [].concat(ret.stops);
  1770. each(this.stops, function (stop, i) {
  1771. ret.stops[i] = [ret.stops[i][0], stop.get(format)];
  1772. });
  1773. // it's NaN if gradient colors on a column chart
  1774. } else if (rgba && isNumber(rgba[0])) {
  1775. if (format === 'rgb' || (!format && rgba[3] === 1)) {
  1776. ret = 'rgb(' + rgba[0] + ',' + rgba[1] + ',' + rgba[2] + ')';
  1777. } else if (format === 'a') {
  1778. ret = rgba[3];
  1779. } else {
  1780. ret = 'rgba(' + rgba.join(',') + ')';
  1781. }
  1782. } else {
  1783. ret = input;
  1784. }
  1785. return ret;
  1786. },
  1787. /**
  1788. * Brighten the color
  1789. * @param {Number} alpha
  1790. */
  1791. brighten: function (alpha) {
  1792. var i,
  1793. rgba = this.rgba;
  1794. if (this.stops) {
  1795. each(this.stops, function (stop) {
  1796. stop.brighten(alpha);
  1797. });
  1798. } else if (isNumber(alpha) && alpha !== 0) {
  1799. for (i = 0; i < 3; i++) {
  1800. rgba[i] += pInt(alpha * 255);
  1801. if (rgba[i] < 0) {
  1802. rgba[i] = 0;
  1803. }
  1804. if (rgba[i] > 255) {
  1805. rgba[i] = 255;
  1806. }
  1807. }
  1808. }
  1809. return this;
  1810. },
  1811. /**
  1812. * Set the color's opacity to a given alpha value
  1813. * @param {Number} alpha
  1814. */
  1815. setOpacity: function (alpha) {
  1816. this.rgba[3] = alpha;
  1817. return this;
  1818. }
  1819. };
  1820. /**
  1821. * A wrapper object for SVG elements
  1822. */
  1823. function SVGElement() {}
  1824. SVGElement.prototype = {
  1825. // Default base for animation
  1826. opacity: 1,
  1827. // For labels, these CSS properties are applied to the <text> node directly
  1828. textProps: ['direction', 'fontSize', 'fontWeight', 'fontFamily', 'fontStyle', 'color',
  1829. 'lineHeight', 'width', 'textDecoration', 'textOverflow', 'textShadow'],
  1830. /**
  1831. * Initialize the SVG renderer
  1832. * @param {Object} renderer
  1833. * @param {String} nodeName
  1834. */
  1835. init: function (renderer, nodeName) {
  1836. var wrapper = this;
  1837. wrapper.element = nodeName === 'span' ?
  1838. createElement(nodeName) :
  1839. doc.createElementNS(SVG_NS, nodeName);
  1840. wrapper.renderer = renderer;
  1841. },
  1842. /**
  1843. * Animate a given attribute
  1844. * @param {Object} params
  1845. * @param {Number} options Options include duration, easing, step and complete
  1846. * @param {Function} complete Function to perform at the end of animation
  1847. */
  1848. animate: function (params, options, complete) {
  1849. var animOptions = pick(options, this.renderer.globalAnimation, true);
  1850. stop(this); // stop regardless of animation actually running, or reverting to .attr (#607)
  1851. if (animOptions) {
  1852. if (complete) { // allows using a callback with the global animation without overwriting it
  1853. animOptions.complete = complete;
  1854. }
  1855. animate(this, params, animOptions);
  1856. } else {
  1857. this.attr(params, null, complete);
  1858. }
  1859. return this;
  1860. },
  1861. /**
  1862. * Build an SVG gradient out of a common JavaScript configuration object
  1863. */
  1864. colorGradient: function (color, prop, elem) {
  1865. var renderer = this.renderer,
  1866. colorObject,
  1867. gradName,
  1868. gradAttr,
  1869. radAttr,
  1870. gradients,
  1871. gradientObject,
  1872. stops,
  1873. stopColor,
  1874. stopOpacity,
  1875. radialReference,
  1876. n,
  1877. id,
  1878. key = [],
  1879. value;
  1880. // Apply linear or radial gradients
  1881. if (color.linearGradient) {
  1882. gradName = 'linearGradient';
  1883. } else if (color.radialGradient) {
  1884. gradName = 'radialGradient';
  1885. }
  1886. if (gradName) {
  1887. gradAttr = color[gradName];
  1888. gradients = renderer.gradients;
  1889. stops = color.stops;
  1890. radialReference = elem.radialReference;
  1891. // Keep < 2.2 kompatibility
  1892. if (isArray(gradAttr)) {
  1893. color[gradName] = gradAttr = {
  1894. x1: gradAttr[0],
  1895. y1: gradAttr[1],
  1896. x2: gradAttr[2],
  1897. y2: gradAttr[3],
  1898. gradientUnits: 'userSpaceOnUse'
  1899. };
  1900. }
  1901. // Correct the radial gradient for the radial reference system
  1902. if (gradName === 'radialGradient' && radialReference && !defined(gradAttr.gradientUnits)) {
  1903. radAttr = gradAttr; // Save the radial attributes for updating
  1904. gradAttr = merge(gradAttr,
  1905. renderer.getRadialAttr(radialReference, radAttr),
  1906. { gradientUnits: 'userSpaceOnUse' }
  1907. );
  1908. }
  1909. // Build the unique key to detect whether we need to create a new element (#1282)
  1910. for (n in gradAttr) {
  1911. if (n !== 'id') {
  1912. key.push(n, gradAttr[n]);
  1913. }
  1914. }
  1915. for (n in stops) {
  1916. key.push(stops[n]);
  1917. }
  1918. key = key.join(',');
  1919. // Check if a gradient object with the same config object is created within this renderer
  1920. if (gradients[key]) {
  1921. id = gradients[key].attr('id');
  1922. } else {
  1923. // Set the id and create the element
  1924. gradAttr.id = id = PREFIX + idCounter++;
  1925. gradients[key] = gradientObject = renderer.createElement(gradName)
  1926. .attr(gradAttr)
  1927. .add(renderer.defs);
  1928. gradientObject.radAttr = radAttr;
  1929. // The gradient needs to keep a list of stops to be able to destroy them
  1930. gradientObject.stops = [];
  1931. each(stops, function (stop) {
  1932. var stopObject;
  1933. if (stop[1].indexOf('rgba') === 0) {
  1934. colorObject = Color(stop[1]);
  1935. stopColor = colorObject.get('rgb');
  1936. stopOpacity = colorObject.get('a');
  1937. } else {
  1938. stopColor = stop[1];
  1939. stopOpacity = 1;
  1940. }
  1941. stopObject = renderer.createElement('stop').attr({
  1942. offset: stop[0],
  1943. 'stop-color': stopColor,
  1944. 'stop-opacity': stopOpacity
  1945. }).add(gradientObject);
  1946. // Add the stop element to the gradient
  1947. gradientObject.stops.push(stopObject);
  1948. });
  1949. }
  1950. // Set the reference to the gradient object
  1951. value = 'url(' + renderer.url + '#' + id + ')';
  1952. elem.setAttribute(prop, value);
  1953. elem.gradient = key;
  1954. // Allow the color to be concatenated into tooltips formatters etc. (#2995)
  1955. color.toString = function () {
  1956. return value;
  1957. };
  1958. }
  1959. },
  1960. /**
  1961. * Apply a polyfill to the text-stroke CSS property, by copying the text element
  1962. * and apply strokes to the copy.
  1963. *
  1964. * Contrast checks at http://jsfiddle.net/highcharts/43soe9m1/2/
  1965. */
  1966. applyTextShadow: function (textShadow) {
  1967. var elem = this.element,
  1968. tspans,
  1969. hasContrast = textShadow.indexOf('contrast') !== -1,
  1970. styles = {},
  1971. forExport = this.renderer.forExport,
  1972. // IE10 and IE11 report textShadow in elem.style even though it doesn't work. Check
  1973. // this again with new IE release. In exports, the rendering is passed to PhantomJS.
  1974. supports = forExport || (elem.style.textShadow !== UNDEFINED && !isMS);
  1975. // When the text shadow is set to contrast, use dark stroke for light text and vice versa
  1976. if (hasContrast) {
  1977. styles.textShadow = textShadow = textShadow.replace(/contrast/g, this.renderer.getContrast(elem.style.fill));
  1978. }
  1979. // Safari with retina displays as well as PhantomJS bug (#3974). Firefox does not tolerate this,
  1980. // it removes the text shadows.
  1981. if (isWebKit || forExport) {
  1982. styles.textRendering = 'geometricPrecision';
  1983. }
  1984. /* Selective side-by-side testing in supported browser (http://jsfiddle.net/highcharts/73L1ptrh/)
  1985. if (elem.textContent.indexOf('2.') === 0) {
  1986. elem.style['text-shadow'] = 'none';
  1987. supports = false;
  1988. }
  1989. // */
  1990. // No reason to polyfill, we've got native support
  1991. if (supports) {
  1992. this.css(styles); // Apply altered textShadow or textRendering workaround
  1993. } else {
  1994. this.fakeTS = true; // Fake text shadow
  1995. // In order to get the right y position of the clones,
  1996. // copy over the y setter
  1997. this.ySetter = this.xSetter;
  1998. tspans = [].slice.call(elem.getElementsByTagName('tspan'));
  1999. each(textShadow.split(/\s?,\s?/g), function (textShadow) {
  2000. var firstChild = elem.firstChild,
  2001. color,
  2002. strokeWidth;
  2003. textShadow = textShadow.split(' ');
  2004. color = textShadow[textShadow.length - 1];
  2005. // Approximately tune the settings to the text-shadow behaviour
  2006. strokeWidth = textShadow[textShadow.length - 2];
  2007. if (strokeWidth) {
  2008. each(tspans, function (tspan, y) {
  2009. var clone;
  2010. // Let the first line start at the correct X position
  2011. if (y === 0) {
  2012. tspan.setAttribute('x', elem.getAttribute('x'));
  2013. y = elem.getAttribute('y');
  2014. tspan.setAttribute('y', y || 0);
  2015. if (y === null) {
  2016. elem.setAttribute('y', 0);
  2017. }
  2018. }
  2019. // Create the clone and apply shadow properties
  2020. clone = tspan.cloneNode(1);
  2021. attr(clone, {
  2022. 'class': PREFIX + 'text-shadow',
  2023. 'fill': color,
  2024. 'stroke': color,
  2025. 'stroke-opacity': 1 / mathMax(pInt(strokeWidth), 3),
  2026. 'stroke-width': strokeWidth,
  2027. 'stroke-linejoin': 'round'
  2028. });
  2029. elem.insertBefore(clone, firstChild);
  2030. });
  2031. }
  2032. });
  2033. }
  2034. },
  2035. /**
  2036. * Set or get a given attribute
  2037. * @param {Object|String} hash
  2038. * @param {Mixed|Undefined} val
  2039. */
  2040. attr: function (hash, val, complete) {
  2041. var key,
  2042. value,
  2043. element = this.element,
  2044. hasSetSymbolSize,
  2045. ret = this,
  2046. skipAttr,
  2047. setter;
  2048. // single key-value pair
  2049. if (typeof hash === 'string' && val !== UNDEFINED) {
  2050. key = hash;
  2051. hash = {};
  2052. hash[key] = val;
  2053. }
  2054. // used as a getter: first argument is a string, second is undefined
  2055. if (typeof hash === 'string') {
  2056. ret = (this[hash + 'Getter'] || this._defaultGetter).call(this, hash, element);
  2057. // setter
  2058. } else {
  2059. for (key in hash) {
  2060. value = hash[key];
  2061. skipAttr = false;
  2062. if (this.symbolName && /^(x|y|width|height|r|start|end|innerR|anchorX|anchorY)/.test(key)) {
  2063. if (!hasSetSymbolSize) {
  2064. this.symbolAttr(hash);
  2065. hasSetSymbolSize = true;
  2066. }
  2067. skipAttr = true;
  2068. }
  2069. if (this.rotation && (key === 'x' || key === 'y')) {
  2070. this.doTransform = true;
  2071. }
  2072. if (!skipAttr) {
  2073. setter = this[key + 'Setter'] || this._defaultSetter;
  2074. setter.call(this, value, key, element);
  2075. // Let the shadow follow the main element
  2076. if (this.shadows && /^(width|height|visibility|x|y|d|transform|cx|cy|r)$/.test(key)) {
  2077. this.updateShadows(key, value, setter);
  2078. }
  2079. }
  2080. }
  2081. // Update transform. Do this outside the loop to prevent redundant updating for batch setting
  2082. // of attributes.
  2083. if (this.doTransform) {
  2084. this.updateTransform();
  2085. this.doTransform = false;
  2086. }
  2087. }
  2088. // In accordance with animate, run a complete callback
  2089. if (complete) {
  2090. complete();
  2091. }
  2092. return ret;
  2093. },
  2094. /**
  2095. * Update the shadow elements with new attributes
  2096. * @param {String} key The attribute name
  2097. * @param {String|Number} value The value of the attribute
  2098. * @param {Function} setter The setter function, inherited from the parent wrapper
  2099. * @returns {undefined}
  2100. */
  2101. updateShadows: function (key, value, setter) {
  2102. var shadows = this.shadows,
  2103. i = shadows.length;
  2104. while (i--) {
  2105. setter.call(
  2106. shadows[i],
  2107. key === 'height' ?
  2108. Math.max(value - (shadows[i].cutHeight || 0), 0) :
  2109. key === 'd' ? this.d : value,
  2110. key,
  2111. shadows[i]
  2112. );
  2113. }
  2114. },
  2115. /**
  2116. * Add a class name to an element
  2117. */
  2118. addClass: function (className) {
  2119. var element = this.element,
  2120. currentClassName = attr(element, 'class') || '';
  2121. if (currentClassName.indexOf(className) === -1) {
  2122. attr(element, 'class', currentClassName + ' ' + className);
  2123. }
  2124. return this;
  2125. },
  2126. /* hasClass and removeClass are not (yet) needed
  2127. hasClass: function (className) {
  2128. return attr(this.element, 'class').indexOf(className) !== -1;
  2129. },
  2130. removeClass: function (className) {
  2131. attr(this.element, 'class', attr(this.element, 'class').replace(className, ''));
  2132. return this;
  2133. },
  2134. */
  2135. /**
  2136. * If one of the symbol size affecting parameters are changed,
  2137. * check all the others only once for each call to an element's
  2138. * .attr() method
  2139. * @param {Object} hash
  2140. */
  2141. symbolAttr: function (hash) {
  2142. var wrapper = this;
  2143. each(['x', 'y', 'r', 'start', 'end', 'width', 'height', 'innerR', 'anchorX', 'anchorY'], function (key) {
  2144. wrapper[key] = pick(hash[key], wrapper[key]);
  2145. });
  2146. wrapper.attr({
  2147. d: wrapper.renderer.symbols[wrapper.symbolName](
  2148. wrapper.x,
  2149. wrapper.y,
  2150. wrapper.width,
  2151. wrapper.height,
  2152. wrapper
  2153. )
  2154. });
  2155. },
  2156. /**
  2157. * Apply a clipping path to this object
  2158. * @param {String} id
  2159. */
  2160. clip: function (clipRect) {
  2161. return this.attr('clip-path', clipRect ? 'url(' + this.renderer.url + '#' + clipRect.id + ')' : NONE);
  2162. },
  2163. /**
  2164. * Calculate the coordinates needed for drawing a rectangle crisply and return the
  2165. * calculated attributes
  2166. * @param {Number} strokeWidth
  2167. * @param {Number} x
  2168. * @param {Number} y
  2169. * @param {Number} width
  2170. * @param {Number} height
  2171. */
  2172. crisp: function (rect) {
  2173. var wrapper = this,
  2174. key,
  2175. attribs = {},
  2176. normalizer,
  2177. strokeWidth = wrapper.strokeWidth || 0;
  2178. normalizer = mathRound(strokeWidth) % 2 / 2; // mathRound because strokeWidth can sometimes have roundoff errors
  2179. // normalize for crisp edges
  2180. rect.x = mathFloor(rect.x || wrapper.x || 0) + normalizer;
  2181. rect.y = mathFloor(rect.y || wrapper.y || 0) + normalizer;
  2182. rect.width = mathFloor((rect.width || wrapper.width || 0) - 2 * normalizer);
  2183. rect.height = mathFloor((rect.height || wrapper.height || 0) - 2 * normalizer);
  2184. rect.strokeWidth = strokeWidth;
  2185. for (key in rect) {
  2186. if (wrapper[key] !== rect[key]) { // only set attribute if changed
  2187. wrapper[key] = attribs[key] = rect[key];
  2188. }
  2189. }
  2190. return attribs;
  2191. },
  2192. /**
  2193. * Set styles for the element
  2194. * @param {Object} styles
  2195. */
  2196. css: function (styles) {
  2197. var elemWrapper = this,
  2198. oldStyles = elemWrapper.styles,
  2199. newStyles = {},
  2200. elem = elemWrapper.element,
  2201. textWidth,
  2202. n,
  2203. serializedCss = '',
  2204. hyphenate,
  2205. hasNew = !oldStyles;
  2206. // convert legacy
  2207. if (styles && styles.color) {
  2208. styles.fill = styles.color;
  2209. }
  2210. // Filter out existing styles to increase performance (#2640)
  2211. if (oldStyles) {
  2212. for (n in styles) {
  2213. if (styles[n] !== oldStyles[n]) {
  2214. newStyles[n] = styles[n];
  2215. hasNew = true;
  2216. }
  2217. }
  2218. }
  2219. if (hasNew) {
  2220. textWidth = elemWrapper.textWidth =
  2221. (styles && styles.width && elem.nodeName.toLowerCase() === 'text' && pInt(styles.width)) ||
  2222. elemWrapper.textWidth; // #3501
  2223. // Merge the new styles with the old ones
  2224. if (oldStyles) {
  2225. styles = extend(
  2226. oldStyles,
  2227. newStyles
  2228. );
  2229. }
  2230. // store object
  2231. elemWrapper.styles = styles;
  2232. if (textWidth && (useCanVG || (!hasSVG && elemWrapper.renderer.forExport))) {
  2233. delete styles.width;
  2234. }
  2235. // serialize and set style attribute
  2236. if (isMS && !hasSVG) {
  2237. css(elemWrapper.element, styles);
  2238. } else {
  2239. hyphenate = function (a, b) {
  2240. return '-' + b.toLowerCase();
  2241. };
  2242. for (n in styles) {
  2243. serializedCss += n.replace(/([A-Z])/g, hyphenate) + ':' + styles[n] + ';';
  2244. }
  2245. attr(elem, 'style', serializedCss); // #1881
  2246. }
  2247. // re-build text
  2248. if (textWidth && elemWrapper.added) {
  2249. elemWrapper.renderer.buildText(elemWrapper);
  2250. }
  2251. }
  2252. return elemWrapper;
  2253. },
  2254. /**
  2255. * Add an event listener
  2256. * @param {String} eventType
  2257. * @param {Function} handler
  2258. */
  2259. on: function (eventType, handler) {
  2260. var svgElement = this,
  2261. element = svgElement.element;
  2262. // touch
  2263. if (hasTouch && eventType === 'click') {
  2264. element.ontouchstart = function (e) {
  2265. svgElement.touchEventFired = Date.now();
  2266. e.preventDefault();
  2267. handler.call(element, e);
  2268. };
  2269. element.onclick = function (e) {
  2270. if (userAgent.indexOf('Android') === -1 || Date.now() - (svgElement.touchEventFired || 0) > 1100) { // #2269
  2271. handler.call(element, e);
  2272. }
  2273. };
  2274. } else {
  2275. // simplest possible event model for internal use
  2276. element['on' + eventType] = handler;
  2277. }
  2278. return this;
  2279. },
  2280. /**
  2281. * Set the coordinates needed to draw a consistent radial gradient across
  2282. * pie slices regardless of positioning inside the chart. The format is
  2283. * [centerX, centerY, diameter] in pixels.
  2284. */
  2285. setRadialReference: function (coordinates) {
  2286. var existingGradient = this.renderer.gradients[this.element.gradient];
  2287. this.element.radialReference = coordinates;
  2288. // On redrawing objects with an existing gradient, the gradient needs
  2289. // to be repositioned (#3801)
  2290. if (existingGradient && existingGradient.radAttr) {
  2291. existingGradient.animate(
  2292. this.renderer.getRadialAttr(
  2293. coordinates,
  2294. existingGradient.radAttr
  2295. )
  2296. );
  2297. }
  2298. return this;
  2299. },
  2300. /**
  2301. * Move an object and its children by x and y values
  2302. * @param {Number} x
  2303. * @param {Number} y
  2304. */
  2305. translate: function (x, y) {
  2306. return this.attr({
  2307. translateX: x,
  2308. translateY: y
  2309. });
  2310. },
  2311. /**
  2312. * Invert a group, rotate and flip
  2313. */
  2314. invert: function () {
  2315. var wrapper = this;
  2316. wrapper.inverted = true;
  2317. wrapper.updateTransform();
  2318. return wrapper;
  2319. },
  2320. /**
  2321. * Private method to update the transform attribute based on internal
  2322. * properties
  2323. */
  2324. updateTransform: function () {
  2325. var wrapper = this,
  2326. translateX = wrapper.translateX || 0,
  2327. translateY = wrapper.translateY || 0,
  2328. scaleX = wrapper.scaleX,
  2329. scaleY = wrapper.scaleY,
  2330. inverted = wrapper.inverted,
  2331. rotation = wrapper.rotation,
  2332. element = wrapper.element,
  2333. transform;
  2334. // flipping affects translate as adjustment for flipping around the group's axis
  2335. if (inverted) {
  2336. translateX += wrapper.attr('width');
  2337. translateY += wrapper.attr('height');
  2338. }
  2339. // Apply translate. Nearly all transformed elements have translation, so instead
  2340. // of checking for translate = 0, do it always (#1767, #1846).
  2341. transform = ['translate(' + translateX + ',' + translateY + ')'];
  2342. // apply rotation
  2343. if (inverted) {
  2344. transform.push('rotate(90) scale(-1,1)');
  2345. } else if (rotation) { // text rotation
  2346. transform.push('rotate(' + rotation + ' ' + (element.getAttribute('x') || 0) + ' ' + (element.getAttribute('y') || 0) + ')');
  2347. // Delete bBox memo when the rotation changes
  2348. //delete wrapper.bBox;
  2349. }
  2350. // apply scale
  2351. if (defined(scaleX) || defined(scaleY)) {
  2352. transform.push('scale(' + pick(scaleX, 1) + ' ' + pick(scaleY, 1) + ')');
  2353. }
  2354. if (transform.length) {
  2355. element.setAttribute('transform', transform.join(' '));
  2356. }
  2357. },
  2358. /**
  2359. * Bring the element to the front
  2360. */
  2361. toFront: function () {
  2362. var element = this.element;
  2363. element.parentNode.appendChild(element);
  2364. return this;
  2365. },
  2366. /**
  2367. * Break down alignment options like align, verticalAlign, x and y
  2368. * to x and y relative to the chart.
  2369. *
  2370. * @param {Object} alignOptions
  2371. * @param {Boolean} alignByTranslate
  2372. * @param {String[Object} box The box to align to, needs a width and height. When the
  2373. * box is a string, it refers to an object in the Renderer. For example, when
  2374. * box is 'spacingBox', it refers to Renderer.spacingBox which holds width, height
  2375. * x and y properties.
  2376. *
  2377. */
  2378. align: function (alignOptions, alignByTranslate, box) {
  2379. var align,
  2380. vAlign,
  2381. x,
  2382. y,
  2383. attribs = {},
  2384. alignTo,
  2385. renderer = this.renderer,
  2386. alignedObjects = renderer.alignedObjects;
  2387. // First call on instanciate
  2388. if (alignOptions) {
  2389. this.alignOptions = alignOptions;
  2390. this.alignByTranslate = alignByTranslate;
  2391. if (!box || isString(box)) { // boxes other than renderer handle this internally
  2392. this.alignTo = alignTo = box || 'renderer';
  2393. erase(alignedObjects, this); // prevent duplicates, like legendGroup after resize
  2394. alignedObjects.push(this);
  2395. box = null; // reassign it below
  2396. }
  2397. // When called on resize, no arguments are supplied
  2398. } else {
  2399. alignOptions = this.alignOptions;
  2400. alignByTranslate = this.alignByTranslate;
  2401. alignTo = this.alignTo;
  2402. }
  2403. box = pick(box, renderer[alignTo], renderer);
  2404. // Assign variables
  2405. align = alignOptions.align;
  2406. vAlign = alignOptions.verticalAlign;
  2407. x = (box.x || 0) + (alignOptions.x || 0); // default: left align
  2408. y = (box.y || 0) + (alignOptions.y || 0); // default: top align
  2409. // Align
  2410. if (align === 'right' || align === 'center') {
  2411. x += (box.width - (alignOptions.width || 0)) /
  2412. { right: 1, center: 2 }[align];
  2413. }
  2414. attribs[alignByTranslate ? 'translateX' : 'x'] = mathRound(x);
  2415. // Vertical align
  2416. if (vAlign === 'bottom' || vAlign === 'middle') {
  2417. y += (box.height - (alignOptions.height || 0)) /
  2418. ({ bottom: 1, middle: 2 }[vAlign] || 1);
  2419. }
  2420. attribs[alignByTranslate ? 'translateY' : 'y'] = mathRound(y);
  2421. // Animate only if already placed
  2422. this[this.placed ? 'animate' : 'attr'](attribs);
  2423. this.placed = true;
  2424. this.alignAttr = attribs;
  2425. return this;
  2426. },
  2427. /**
  2428. * Get the bounding box (width, height, x and y) for the element
  2429. */
  2430. getBBox: function (reload, rot) {
  2431. var wrapper = this,
  2432. bBox, // = wrapper.bBox,
  2433. renderer = wrapper.renderer,
  2434. width,
  2435. height,
  2436. rotation,
  2437. rad,
  2438. element = wrapper.element,
  2439. styles = wrapper.styles,
  2440. textStr = wrapper.textStr,
  2441. textShadow,
  2442. elemStyle = element.style,
  2443. toggleTextShadowShim,
  2444. cache = renderer.cache,
  2445. cacheKeys = renderer.cacheKeys,
  2446. cacheKey;
  2447. rotation = pick(rot, wrapper.rotation);
  2448. rad = rotation * deg2rad;
  2449. if (textStr !== UNDEFINED) {
  2450. // Properties that affect bounding box
  2451. cacheKey = ['', rotation || 0, styles && styles.fontSize, element.style.width].join(',');
  2452. // Since numbers are monospaced, and numerical labels appear a lot in a chart,
  2453. // we assume that a label of n characters has the same bounding box as others
  2454. // of the same length.
  2455. if (textStr === '' || numRegex.test(textStr)) {
  2456. cacheKey = 'num:' + textStr.toString().length + cacheKey;
  2457. // Caching all strings reduces rendering time by 4-5%.
  2458. } else {
  2459. cacheKey = textStr + cacheKey;
  2460. }
  2461. }
  2462. if (cacheKey && !reload) {
  2463. bBox = cache[cacheKey];
  2464. }
  2465. // No cache found
  2466. if (!bBox) {
  2467. // SVG elements
  2468. if (element.namespaceURI === SVG_NS || renderer.forExport) {
  2469. try { // Fails in Firefox if the container has display: none.
  2470. // When the text shadow shim is used, we need to hide the fake shadows
  2471. // to get the correct bounding box (#3872)
  2472. toggleTextShadowShim = this.fakeTS && function (display) {
  2473. each(element.querySelectorAll('.' + PREFIX + 'text-shadow'), function (tspan) {
  2474. tspan.style.display = display;
  2475. });
  2476. };
  2477. // Workaround for #3842, Firefox reporting wrong bounding box for shadows
  2478. if (isFirefox && elemStyle.textShadow) {
  2479. textShadow = elemStyle.textShadow;
  2480. elemStyle.textShadow = '';
  2481. } else if (toggleTextShadowShim) {
  2482. toggleTextShadowShim(NONE);
  2483. }
  2484. bBox = element.getBBox ?
  2485. // SVG: use extend because IE9 is not allowed to change width and height in case
  2486. // of rotation (below)
  2487. extend({}, element.getBBox()) :
  2488. // Canvas renderer and legacy IE in export mode
  2489. {
  2490. width: element.offsetWidth,
  2491. height: element.offsetHeight
  2492. };
  2493. // #3842
  2494. if (textShadow) {
  2495. elemStyle.textShadow = textShadow;
  2496. } else if (toggleTextShadowShim) {
  2497. toggleTextShadowShim('');
  2498. }
  2499. } catch (e) {}
  2500. // If the bBox is not set, the try-catch block above failed. The other condition
  2501. // is for Opera that returns a width of -Infinity on hidden elements.
  2502. if (!bBox || bBox.width < 0) {
  2503. bBox = { width: 0, height: 0 };
  2504. }
  2505. // VML Renderer or useHTML within SVG
  2506. } else {
  2507. bBox = wrapper.htmlGetBBox();
  2508. }
  2509. // True SVG elements as well as HTML elements in modern browsers using the .useHTML option
  2510. // need to compensated for rotation
  2511. if (renderer.isSVG) {
  2512. width = bBox.width;
  2513. height = bBox.height;
  2514. // Workaround for wrong bounding box in IE9 and IE10 (#1101, #1505, #1669, #2568)
  2515. if (isMS && styles && styles.fontSize === '11px' && height.toPrecision(3) === '16.9') {
  2516. bBox.height = height = 14;
  2517. }
  2518. // Adjust for rotated text
  2519. if (rotation) {
  2520. bBox.width = mathAbs(height * mathSin(rad)) + mathAbs(width * mathCos(rad));
  2521. bBox.height = mathAbs(height * mathCos(rad)) + mathAbs(width * mathSin(rad));
  2522. }
  2523. }
  2524. // Cache it
  2525. if (cacheKey) {
  2526. // Rotate (#4681)
  2527. while (cacheKeys.length > 250) {
  2528. delete cache[cacheKeys.shift()];
  2529. }
  2530. if (!cache[cacheKey]) {
  2531. cacheKeys.push(cacheKey);
  2532. }
  2533. cache[cacheKey] = bBox;
  2534. }
  2535. }
  2536. return bBox;
  2537. },
  2538. /**
  2539. * Show the element
  2540. */
  2541. show: function (inherit) {
  2542. return this.attr({ visibility: inherit ? 'inherit' : VISIBLE });
  2543. },
  2544. /**
  2545. * Hide the element
  2546. */
  2547. hide: function () {
  2548. return this.attr({ visibility: HIDDEN });
  2549. },
  2550. fadeOut: function (duration) {
  2551. var elemWrapper = this;
  2552. elemWrapper.animate({
  2553. opacity: 0
  2554. }, {
  2555. duration: duration || 150,
  2556. complete: function () {
  2557. elemWrapper.attr({ y: -9999 }); // #3088, assuming we're only using this for tooltips
  2558. }
  2559. });
  2560. },
  2561. /**
  2562. * Add the element
  2563. * @param {Object|Undefined} parent Can be an element, an element wrapper or undefined
  2564. * to append the element to the renderer.box.
  2565. */
  2566. add: function (parent) {
  2567. var renderer = this.renderer,
  2568. element = this.element,
  2569. inserted;
  2570. if (parent) {
  2571. this.parentGroup = parent;
  2572. }
  2573. // mark as inverted
  2574. this.parentInverted = parent && parent.inverted;
  2575. // build formatted text
  2576. if (this.textStr !== undefined) {
  2577. renderer.buildText(this);
  2578. }
  2579. // Mark as added
  2580. this.added = true;
  2581. // If we're adding to renderer root, or other elements in the group
  2582. // have a z index, we need to handle it
  2583. if (!parent || parent.handleZ || this.zIndex) {
  2584. inserted = this.zIndexSetter();
  2585. }
  2586. // If zIndex is not handled, append at the end
  2587. if (!inserted) {
  2588. (parent ? parent.element : renderer.box).appendChild(element);
  2589. }
  2590. // fire an event for internal hooks
  2591. if (this.onAdd) {
  2592. this.onAdd();
  2593. }
  2594. return this;
  2595. },
  2596. /**
  2597. * Removes a child either by removeChild or move to garbageBin.
  2598. * Issue 490; in VML removeChild results in Orphaned nodes according to sIEve, discardElement does not.
  2599. */
  2600. safeRemoveChild: function (element) {
  2601. var parentNode = element.parentNode;
  2602. if (parentNode) {
  2603. parentNode.removeChild(element);
  2604. }
  2605. },
  2606. /**
  2607. * Destroy the element and element wrapper
  2608. */
  2609. destroy: function () {
  2610. var wrapper = this,
  2611. element = wrapper.element || {},
  2612. shadows = wrapper.shadows,
  2613. parentToClean = wrapper.renderer.isSVG && element.nodeName === 'SPAN' && wrapper.parentGroup,
  2614. grandParent,
  2615. key,
  2616. i;
  2617. // remove events
  2618. element.onclick = element.onmouseout = element.onmouseover = element.onmousemove = element.point = null;
  2619. stop(wrapper); // stop running animations
  2620. if (wrapper.clipPath) {
  2621. wrapper.clipPath = wrapper.clipPath.destroy();
  2622. }
  2623. // Destroy stops in case this is a gradient object
  2624. if (wrapper.stops) {
  2625. for (i = 0; i < wrapper.stops.length; i++) {
  2626. wrapper.stops[i] = wrapper.stops[i].destroy();
  2627. }
  2628. wrapper.stops = null;
  2629. }
  2630. // remove element
  2631. wrapper.safeRemoveChild(element);
  2632. // destroy shadows
  2633. if (shadows) {
  2634. each(shadows, function (shadow) {
  2635. wrapper.safeRemoveChild(shadow);
  2636. });
  2637. }
  2638. // In case of useHTML, clean up empty containers emulating SVG groups (#1960, #2393, #2697).
  2639. while (parentToClean && parentToClean.div && parentToClean.div.childNodes.length === 0) {
  2640. grandParent = parentToClean.parentGroup;
  2641. wrapper.safeRemoveChild(parentToClean.div);
  2642. delete parentToClean.div;
  2643. parentToClean = grandParent;
  2644. }
  2645. // remove from alignObjects
  2646. if (wrapper.alignTo) {
  2647. erase(wrapper.renderer.alignedObjects, wrapper);
  2648. }
  2649. for (key in wrapper) {
  2650. delete wrapper[key];
  2651. }
  2652. return null;
  2653. },
  2654. /**
  2655. * Add a shadow to the element. Must be done after the element is added to the DOM
  2656. * @param {Boolean|Object} shadowOptions
  2657. */
  2658. shadow: function (shadowOptions, group, cutOff) {
  2659. var shadows = [],
  2660. i,
  2661. shadow,
  2662. element = this.element,
  2663. strokeWidth,
  2664. shadowWidth,
  2665. shadowElementOpacity,
  2666. // compensate for inverted plot area
  2667. transform;
  2668. if (shadowOptions) {
  2669. shadowWidth = pick(shadowOptions.width, 3);
  2670. shadowElementOpacity = (shadowOptions.opacity || 0.15) / shadowWidth;
  2671. transform = this.parentInverted ?
  2672. '(-1,-1)' :
  2673. '(' + pick(shadowOptions.offsetX, 1) + ', ' + pick(shadowOptions.offsetY, 1) + ')';
  2674. for (i = 1; i <= shadowWidth; i++) {
  2675. shadow = element.cloneNode(0);
  2676. strokeWidth = (shadowWidth * 2) + 1 - (2 * i);
  2677. attr(shadow, {
  2678. 'isShadow': 'true',
  2679. 'stroke': shadowOptions.color || 'black',
  2680. 'stroke-opacity': shadowElementOpacity * i,
  2681. 'stroke-width': strokeWidth,
  2682. 'transform': 'translate' + transform,
  2683. 'fill': NONE
  2684. });
  2685. if (cutOff) {
  2686. attr(shadow, 'height', mathMax(attr(shadow, 'height') - strokeWidth, 0));
  2687. shadow.cutHeight = strokeWidth;
  2688. }
  2689. if (group) {
  2690. group.element.appendChild(shadow);
  2691. } else {
  2692. element.parentNode.insertBefore(shadow, element);
  2693. }
  2694. shadows.push(shadow);
  2695. }
  2696. this.shadows = shadows;
  2697. }
  2698. return this;
  2699. },
  2700. xGetter: function (key) {
  2701. if (this.element.nodeName === 'circle') {
  2702. key = { x: 'cx', y: 'cy' }[key] || key;
  2703. }
  2704. return this._defaultGetter(key);
  2705. },
  2706. /**
  2707. * Get the current value of an attribute or pseudo attribute, used mainly
  2708. * for animation.
  2709. */
  2710. _defaultGetter: function (key) {
  2711. var ret = pick(this[key], this.element ? this.element.getAttribute(key) : null, 0);
  2712. if (/^[\-0-9\.]+$/.test(ret)) { // is numerical
  2713. ret = parseFloat(ret);
  2714. }
  2715. return ret;
  2716. },
  2717. dSetter: function (value, key, element) {
  2718. if (value && value.join) { // join path
  2719. value = value.join(' ');
  2720. }
  2721. if (/(NaN| {2}|^$)/.test(value)) {
  2722. value = 'M 0 0';
  2723. }
  2724. element.setAttribute(key, value);
  2725. this[key] = value;
  2726. },
  2727. dashstyleSetter: function (value) {
  2728. var i,
  2729. strokeWidth = this['stroke-width'];
  2730. // If "inherit", like maps in IE, assume 1 (#4981). With HC5 and the new strokeWidth
  2731. // function, we should be able to use that instead.
  2732. if (strokeWidth === 'inherit') {
  2733. strokeWidth = 1;
  2734. }
  2735. value = value && value.toLowerCase();
  2736. if (value) {
  2737. value = value
  2738. .replace('shortdashdotdot', '3,1,1,1,1,1,')
  2739. .replace('shortdashdot', '3,1,1,1')
  2740. .replace('shortdot', '1,1,')
  2741. .replace('shortdash', '3,1,')
  2742. .replace('longdash', '8,3,')
  2743. .replace(/dot/g, '1,3,')
  2744. .replace('dash', '4,3,')
  2745. .replace(/,$/, '')
  2746. .split(','); // ending comma
  2747. i = value.length;
  2748. while (i--) {
  2749. value[i] = pInt(value[i]) * strokeWidth;
  2750. }
  2751. value = value.join(',')
  2752. .replace(/NaN/g, 'none'); // #3226
  2753. this.element.setAttribute('stroke-dasharray', value);
  2754. }
  2755. },
  2756. alignSetter: function (value) {
  2757. this.element.setAttribute('text-anchor', { left: 'start', center: 'middle', right: 'end' }[value]);
  2758. },
  2759. opacitySetter: function (value, key, element) {
  2760. this[key] = value;
  2761. element.setAttribute(key, value);
  2762. },
  2763. titleSetter: function (value) {
  2764. var titleNode = this.element.getElementsByTagName('title')[0];
  2765. if (!titleNode) {
  2766. titleNode = doc.createElementNS(SVG_NS, 'title');
  2767. this.element.appendChild(titleNode);
  2768. }
  2769. // Remove text content if it exists
  2770. if (titleNode.firstChild) {
  2771. titleNode.removeChild(titleNode.firstChild);
  2772. }
  2773. titleNode.appendChild(
  2774. doc.createTextNode(
  2775. (String(pick(value), '')).replace(/<[^>]*>/g, '') // #3276, #3895
  2776. )
  2777. );
  2778. },
  2779. textSetter: function (value) {
  2780. if (value !== this.textStr) {
  2781. // Delete bBox memo when the text changes
  2782. delete this.bBox;
  2783. this.textStr = value;
  2784. if (this.added) {
  2785. this.renderer.buildText(this);
  2786. }
  2787. }
  2788. },
  2789. fillSetter: function (value, key, element) {
  2790. if (typeof value === 'string') {
  2791. element.setAttribute(key, value);
  2792. } else if (value) {
  2793. this.colorGradient(value, key, element);
  2794. }
  2795. },
  2796. visibilitySetter: function (value, key, element) {
  2797. // IE9-11 doesn't handle visibilty:inherit well, so we remove the attribute instead (#2881, #3909)
  2798. if (value === 'inherit') {
  2799. element.removeAttribute(key);
  2800. } else {
  2801. element.setAttribute(key, value);
  2802. }
  2803. },
  2804. zIndexSetter: function (value, key) {
  2805. var renderer = this.renderer,
  2806. parentGroup = this.parentGroup,
  2807. parentWrapper = parentGroup || renderer,
  2808. parentNode = parentWrapper.element || renderer.box,
  2809. childNodes,
  2810. otherElement,
  2811. otherZIndex,
  2812. element = this.element,
  2813. inserted,
  2814. run = this.added,
  2815. i;
  2816. if (defined(value)) {
  2817. element.zIndex = value; // So we can read it for other elements in the group
  2818. value = +value;
  2819. if (this[key] === value) { // Only update when needed (#3865)
  2820. run = false;
  2821. }
  2822. this[key] = value;
  2823. }
  2824. // Insert according to this and other elements' zIndex. Before .add() is called,
  2825. // nothing is done. Then on add, or by later calls to zIndexSetter, the node
  2826. // is placed on the right place in the DOM.
  2827. if (run) {
  2828. value = this.zIndex;
  2829. if (value && parentGroup) {
  2830. parentGroup.handleZ = true;
  2831. }
  2832. childNodes = parentNode.childNodes;
  2833. for (i = 0; i < childNodes.length && !inserted; i++) {
  2834. otherElement = childNodes[i];
  2835. otherZIndex = otherElement.zIndex;
  2836. if (otherElement !== element && (
  2837. // Insert before the first element with a higher zIndex
  2838. pInt(otherZIndex) > value ||
  2839. // If no zIndex given, insert before the first element with a zIndex
  2840. (!defined(value) && defined(otherZIndex))
  2841. )) {
  2842. parentNode.insertBefore(element, otherElement);
  2843. inserted = true;
  2844. }
  2845. }
  2846. if (!inserted) {
  2847. parentNode.appendChild(element);
  2848. }
  2849. }
  2850. return inserted;
  2851. },
  2852. _defaultSetter: function (value, key, element) {
  2853. element.setAttribute(key, value);
  2854. }
  2855. };
  2856. // Some shared setters and getters
  2857. SVGElement.prototype.yGetter = SVGElement.prototype.xGetter;
  2858. SVGElement.prototype.translateXSetter = SVGElement.prototype.translateYSetter =
  2859. SVGElement.prototype.rotationSetter = SVGElement.prototype.verticalAlignSetter =
  2860. SVGElement.prototype.scaleXSetter = SVGElement.prototype.scaleYSetter = function (value, key) {
  2861. this[key] = value;
  2862. this.doTransform = true;
  2863. };
  2864. // WebKit and Batik have problems with a stroke-width of zero, so in this case we remove the
  2865. // stroke attribute altogether. #1270, #1369, #3065, #3072.
  2866. SVGElement.prototype['stroke-widthSetter'] = SVGElement.prototype.strokeSetter = function (value, key, element) {
  2867. this[key] = value;
  2868. // Only apply the stroke attribute if the stroke width is defined and larger than 0
  2869. if (this.stroke && this['stroke-width']) {
  2870. this.strokeWidth = this['stroke-width'];
  2871. SVGElement.prototype.fillSetter.call(this, this.stroke, 'stroke', element); // use prototype as instance may be overridden
  2872. element.setAttribute('stroke-width', this['stroke-width']);
  2873. this.hasStroke = true;
  2874. } else if (key === 'stroke-width' && value === 0 && this.hasStroke) {
  2875. element.removeAttribute('stroke');
  2876. this.hasStroke = false;
  2877. }
  2878. };
  2879. /**
  2880. * The default SVG renderer
  2881. */
  2882. var SVGRenderer = function () {
  2883. this.init.apply(this, arguments);
  2884. };
  2885. SVGRenderer.prototype = {
  2886. Element: SVGElement,
  2887. /**
  2888. * Initialize the SVGRenderer
  2889. * @param {Object} container
  2890. * @param {Number} width
  2891. * @param {Number} height
  2892. * @param {Boolean} forExport
  2893. */
  2894. init: function (container, width, height, style, forExport, allowHTML) {
  2895. var renderer = this,
  2896. boxWrapper,
  2897. element,
  2898. desc;
  2899. boxWrapper = renderer.createElement('svg')
  2900. .attr({
  2901. version: '1.1'
  2902. })
  2903. .css(this.getStyle(style));
  2904. element = boxWrapper.element;
  2905. container.appendChild(element);
  2906. // For browsers other than IE, add the namespace attribute (#1978)
  2907. if (container.innerHTML.indexOf('xmlns') === -1) {
  2908. attr(element, 'xmlns', SVG_NS);
  2909. }
  2910. // object properties
  2911. renderer.isSVG = true;
  2912. renderer.box = element;
  2913. renderer.boxWrapper = boxWrapper;
  2914. renderer.alignedObjects = [];
  2915. // Page url used for internal references. #24, #672, #1070
  2916. renderer.url = (isFirefox || isWebKit) && doc.getElementsByTagName('base').length ?
  2917. win.location.href
  2918. .replace(/#.*?$/, '') // remove the hash
  2919. .replace(/([\('\)])/g, '\\$1') // escape parantheses and quotes
  2920. .replace(/ /g, '%20') : // replace spaces (needed for Safari only)
  2921. '';
  2922. // Add description
  2923. desc = this.createElement('desc').add();
  2924. desc.element.appendChild(doc.createTextNode('Created with ' + PRODUCT + ' ' + VERSION));
  2925. renderer.defs = this.createElement('defs').add();
  2926. renderer.allowHTML = allowHTML;
  2927. renderer.forExport = forExport;
  2928. renderer.gradients = {}; // Object where gradient SvgElements are stored
  2929. renderer.cache = {}; // Cache for numerical bounding boxes
  2930. renderer.cacheKeys = [];
  2931. renderer.imgCount = 0;
  2932. renderer.setSize(width, height, false);
  2933. // Issue 110 workaround:
  2934. // In Firefox, if a div is positioned by percentage, its pixel position may land
  2935. // between pixels. The container itself doesn't display this, but an SVG element
  2936. // inside this container will be drawn at subpixel precision. In order to draw
  2937. // sharp lines, this must be compensated for. This doesn't seem to work inside
  2938. // iframes though (like in jsFiddle).
  2939. var subPixelFix, rect;
  2940. if (isFirefox && container.getBoundingClientRect) {
  2941. renderer.subPixelFix = subPixelFix = function () {
  2942. css(container, { left: 0, top: 0 });
  2943. rect = container.getBoundingClientRect();
  2944. css(container, {
  2945. left: (mathCeil(rect.left) - rect.left) + PX,
  2946. top: (mathCeil(rect.top) - rect.top) + PX
  2947. });
  2948. };
  2949. // run the fix now
  2950. subPixelFix();
  2951. // run it on resize
  2952. addEvent(win, 'resize', subPixelFix);
  2953. }
  2954. },
  2955. getStyle: function (style) {
  2956. this.style = extend({
  2957. fontFamily: '"Lucida Grande", "Lucida Sans Unicode", Arial, Helvetica, sans-serif', // default font
  2958. fontSize: '12px'
  2959. }, style);
  2960. return this.style;
  2961. },
  2962. /**
  2963. * Detect whether the renderer is hidden. This happens when one of the parent elements
  2964. * has display: none. #608.
  2965. */
  2966. isHidden: function () {
  2967. return !this.boxWrapper.getBBox().width;
  2968. },
  2969. /**
  2970. * Destroys the renderer and its allocated members.
  2971. */
  2972. destroy: function () {
  2973. var renderer = this,
  2974. rendererDefs = renderer.defs;
  2975. renderer.box = null;
  2976. renderer.boxWrapper = renderer.boxWrapper.destroy();
  2977. // Call destroy on all gradient elements
  2978. destroyObjectProperties(renderer.gradients || {});
  2979. renderer.gradients = null;
  2980. // Defs are null in VMLRenderer
  2981. // Otherwise, destroy them here.
  2982. if (rendererDefs) {
  2983. renderer.defs = rendererDefs.destroy();
  2984. }
  2985. // Remove sub pixel fix handler
  2986. // We need to check that there is a handler, otherwise all functions that are registered for event 'resize' are removed
  2987. // See issue #982
  2988. if (renderer.subPixelFix) {
  2989. removeEvent(win, 'resize', renderer.subPixelFix);
  2990. }
  2991. renderer.alignedObjects = null;
  2992. return null;
  2993. },
  2994. /**
  2995. * Create a wrapper for an SVG element
  2996. * @param {Object} nodeName
  2997. */
  2998. createElement: function (nodeName) {
  2999. var wrapper = new this.Element();
  3000. wrapper.init(this, nodeName);
  3001. return wrapper;
  3002. },
  3003. /**
  3004. * Dummy function for use in canvas renderer
  3005. */
  3006. draw: function () {},
  3007. /**
  3008. * Get converted radial gradient attributes
  3009. */
  3010. getRadialAttr: function (radialReference, gradAttr) {
  3011. return {
  3012. cx: (radialReference[0] - radialReference[2] / 2) + gradAttr.cx * radialReference[2],
  3013. cy: (radialReference[1] - radialReference[2] / 2) + gradAttr.cy * radialReference[2],
  3014. r: gradAttr.r * radialReference[2]
  3015. };
  3016. },
  3017. /**
  3018. * Parse a simple HTML string into SVG tspans
  3019. *
  3020. * @param {Object} textNode The parent text SVG node
  3021. */
  3022. buildText: function (wrapper) {
  3023. var textNode = wrapper.element,
  3024. renderer = this,
  3025. forExport = renderer.forExport,
  3026. textStr = pick(wrapper.textStr, '').toString(),
  3027. hasMarkup = textStr.indexOf('<') !== -1,
  3028. lines,
  3029. childNodes = textNode.childNodes,
  3030. styleRegex,
  3031. hrefRegex,
  3032. wasTooLong,
  3033. parentX = attr(textNode, 'x'),
  3034. textStyles = wrapper.styles,
  3035. width = wrapper.textWidth,
  3036. textLineHeight = textStyles && textStyles.lineHeight,
  3037. textShadow = textStyles && textStyles.textShadow,
  3038. ellipsis = textStyles && textStyles.textOverflow === 'ellipsis',
  3039. i = childNodes.length,
  3040. tempParent = width && !wrapper.added && this.box,
  3041. getLineHeight = function (tspan) {
  3042. return textLineHeight ?
  3043. pInt(textLineHeight) :
  3044. renderer.fontMetrics(
  3045. /(px|em)$/.test(tspan && tspan.style.fontSize) ?
  3046. tspan.style.fontSize :
  3047. ((textStyles && textStyles.fontSize) || renderer.style.fontSize || 12),
  3048. tspan
  3049. ).h;
  3050. },
  3051. unescapeAngleBrackets = function (inputStr) {
  3052. return inputStr.replace(/&lt;/g, '<').replace(/&gt;/g, '>');
  3053. };
  3054. /// remove old text
  3055. while (i--) {
  3056. textNode.removeChild(childNodes[i]);
  3057. }
  3058. // Skip tspans, add text directly to text node. The forceTSpan is a hook
  3059. // used in text outline hack.
  3060. if (!hasMarkup && !textShadow && !ellipsis && textStr.indexOf(' ') === -1) {
  3061. textNode.appendChild(doc.createTextNode(unescapeAngleBrackets(textStr)));
  3062. // Complex strings, add more logic
  3063. } else {
  3064. styleRegex = /<.*style="([^"]+)".*>/;
  3065. hrefRegex = /<.*href="(http[^"]+)".*>/;
  3066. if (tempParent) {
  3067. tempParent.appendChild(textNode); // attach it to the DOM to read offset width
  3068. }
  3069. if (hasMarkup) {
  3070. lines = textStr
  3071. .replace(/<(b|strong)>/g, '<span style="font-weight:bold">')
  3072. .replace(/<(i|em)>/g, '<span style="font-style:italic">')
  3073. .replace(/<a/g, '<span')
  3074. .replace(/<\/(b|strong|i|em|a)>/g, '</span>')
  3075. .split(/<br.*?>/g);
  3076. } else {
  3077. lines = [textStr];
  3078. }
  3079. // Trim empty lines (#5261)
  3080. lines = grep(lines, function (line) {
  3081. return line !== '';
  3082. });
  3083. // build the lines
  3084. each(lines, function buildTextLines(line, lineNo) {
  3085. var spans,
  3086. spanNo = 0;
  3087. line = line
  3088. .replace(/^\s+|\s+$/g, '') // Trim to prevent useless/costly process on the spaces (#5258)
  3089. .replace(/<span/g, '|||<span')
  3090. .replace(/<\/span>/g, '</span>|||');
  3091. spans = line.split('|||');
  3092. each(spans, function buildTextSpans(span) {
  3093. if (span !== '' || spans.length === 1) {
  3094. var attributes = {},
  3095. tspan = doc.createElementNS(SVG_NS, 'tspan'),
  3096. spanStyle; // #390
  3097. if (styleRegex.test(span)) {
  3098. spanStyle = span.match(styleRegex)[1].replace(/(;| |^)color([ :])/, '$1fill$2');
  3099. attr(tspan, 'style', spanStyle);
  3100. }
  3101. if (hrefRegex.test(span) && !forExport) { // Not for export - #1529
  3102. attr(tspan, 'onclick', 'location.href=\"' + span.match(hrefRegex)[1] + '\"');
  3103. css(tspan, { cursor: 'pointer' });
  3104. }
  3105. span = unescapeAngleBrackets(span.replace(/<(.|\n)*?>/g, '') || ' ');
  3106. // Nested tags aren't supported, and cause crash in Safari (#1596)
  3107. if (span !== ' ') {
  3108. // add the text node
  3109. tspan.appendChild(doc.createTextNode(span));
  3110. if (!spanNo) { // first span in a line, align it to the left
  3111. if (lineNo && parentX !== null) {
  3112. attributes.x = parentX;
  3113. }
  3114. } else {
  3115. attributes.dx = 0; // #16
  3116. }
  3117. // add attributes
  3118. attr(tspan, attributes);
  3119. // Append it
  3120. textNode.appendChild(tspan);
  3121. // first span on subsequent line, add the line height
  3122. if (!spanNo && lineNo) {
  3123. // allow getting the right offset height in exporting in IE
  3124. if (!hasSVG && forExport) {
  3125. css(tspan, { display: 'block' });
  3126. }
  3127. // Set the line height based on the font size of either
  3128. // the text element or the tspan element
  3129. attr(
  3130. tspan,
  3131. 'dy',
  3132. getLineHeight(tspan)
  3133. );
  3134. }
  3135. /*if (width) {
  3136. renderer.breakText(wrapper, width);
  3137. }*/
  3138. // Check width and apply soft breaks or ellipsis
  3139. if (width) {
  3140. var words = span.replace(/([^\^])-/g, '$1- ').split(' '), // #1273
  3141. hasWhiteSpace = spans.length > 1 || lineNo || (words.length > 1 && textStyles.whiteSpace !== 'nowrap'),
  3142. tooLong,
  3143. actualWidth,
  3144. rest = [],
  3145. dy = getLineHeight(tspan),
  3146. softLineNo = 1,
  3147. rotation = wrapper.rotation,
  3148. wordStr = span, // for ellipsis
  3149. cursor = wordStr.length, // binary search cursor
  3150. bBox;
  3151. while ((hasWhiteSpace || ellipsis) && (words.length || rest.length)) {
  3152. wrapper.rotation = 0; // discard rotation when computing box
  3153. bBox = wrapper.getBBox(true);
  3154. actualWidth = bBox.width;
  3155. // Old IE cannot measure the actualWidth for SVG elements (#2314)
  3156. if (!hasSVG && renderer.forExport) {
  3157. actualWidth = renderer.measureSpanWidth(tspan.firstChild.data, wrapper.styles);
  3158. }
  3159. tooLong = actualWidth > width;
  3160. // For ellipsis, do a binary search for the correct string length
  3161. if (wasTooLong === undefined) {
  3162. wasTooLong = tooLong; // First time
  3163. }
  3164. if (ellipsis && wasTooLong) {
  3165. cursor /= 2;
  3166. if (wordStr === '' || (!tooLong && cursor < 0.5)) {
  3167. words = []; // All ok, break out
  3168. } else {
  3169. wordStr = span.substring(0, wordStr.length + (tooLong ? -1 : 1) * mathCeil(cursor));
  3170. words = [wordStr + (width > 3 ? '\u2026' : '')];
  3171. tspan.removeChild(tspan.firstChild);
  3172. }
  3173. // Looping down, this is the first word sequence that is not too long,
  3174. // so we can move on to build the next line.
  3175. } else if (!tooLong || words.length === 1) {
  3176. words = rest;
  3177. rest = [];
  3178. if (words.length) {
  3179. softLineNo++;
  3180. tspan = doc.createElementNS(SVG_NS, 'tspan');
  3181. attr(tspan, {
  3182. dy: dy,
  3183. x: parentX
  3184. });
  3185. if (spanStyle) { // #390
  3186. attr(tspan, 'style', spanStyle);
  3187. }
  3188. textNode.appendChild(tspan);
  3189. }
  3190. if (actualWidth > width) { // a single word is pressing it out
  3191. width = actualWidth;
  3192. }
  3193. } else { // append to existing line tspan
  3194. tspan.removeChild(tspan.firstChild);
  3195. rest.unshift(words.pop());
  3196. }
  3197. if (words.length) {
  3198. tspan.appendChild(doc.createTextNode(words.join(' ').replace(/- /g, '-')));
  3199. }
  3200. }
  3201. wrapper.rotation = rotation;
  3202. }
  3203. spanNo++;
  3204. }
  3205. }
  3206. });
  3207. });
  3208. if (wasTooLong) {
  3209. wrapper.attr('title', wrapper.textStr);
  3210. }
  3211. if (tempParent) {
  3212. tempParent.removeChild(textNode); // attach it to the DOM to read offset width
  3213. }
  3214. // Apply the text shadow
  3215. if (textShadow && wrapper.applyTextShadow) {
  3216. wrapper.applyTextShadow(textShadow);
  3217. }
  3218. }
  3219. },
  3220. /*
  3221. breakText: function (wrapper, width) {
  3222. var bBox = wrapper.getBBox(),
  3223. node = wrapper.element,
  3224. textLength = node.textContent.length,
  3225. pos = mathRound(width * textLength / bBox.width), // try this position first, based on average character width
  3226. increment = 0,
  3227. finalPos;
  3228. if (bBox.width > width) {
  3229. while (finalPos === undefined) {
  3230. textLength = node.getSubStringLength(0, pos);
  3231. if (textLength <= width) {
  3232. if (increment === -1) {
  3233. finalPos = pos;
  3234. } else {
  3235. increment = 1;
  3236. }
  3237. } else {
  3238. if (increment === 1) {
  3239. finalPos = pos - 1;
  3240. } else {
  3241. increment = -1;
  3242. }
  3243. }
  3244. pos += increment;
  3245. }
  3246. }
  3247. console.log('width', width, 'stringWidth', node.getSubStringLength(0, finalPos))
  3248. },
  3249. */
  3250. /**
  3251. * Returns white for dark colors and black for bright colors
  3252. */
  3253. getContrast: function (color) {
  3254. color = Color(color).rgba;
  3255. return color[0] + color[1] + color[2] > 384 ? '#000000' : '#FFFFFF';
  3256. },
  3257. /**
  3258. * Create a button with preset states
  3259. * @param {String} text
  3260. * @param {Number} x
  3261. * @param {Number} y
  3262. * @param {Function} callback
  3263. * @param {Object} normalState
  3264. * @param {Object} hoverState
  3265. * @param {Object} pressedState
  3266. */
  3267. button: function (text, x, y, callback, normalState, hoverState, pressedState, disabledState, shape) {
  3268. var label = this.label(text, x, y, shape, null, null, null, null, 'button'),
  3269. curState = 0,
  3270. stateOptions,
  3271. stateStyle,
  3272. normalStyle,
  3273. hoverStyle,
  3274. pressedStyle,
  3275. disabledStyle,
  3276. verticalGradient = { x1: 0, y1: 0, x2: 0, y2: 1 };
  3277. // Normal state - prepare the attributes
  3278. normalState = merge({
  3279. 'stroke-width': 1,
  3280. stroke: '#CCCCCC',
  3281. fill: {
  3282. linearGradient: verticalGradient,
  3283. stops: [
  3284. [0, '#FEFEFE'],
  3285. [1, '#F6F6F6']
  3286. ]
  3287. },
  3288. r: 2,
  3289. padding: 5,
  3290. style: {
  3291. color: 'black'
  3292. }
  3293. }, normalState);
  3294. normalStyle = normalState.style;
  3295. delete normalState.style;
  3296. // Hover state
  3297. hoverState = merge(normalState, {
  3298. stroke: '#68A',
  3299. fill: {
  3300. linearGradient: verticalGradient,
  3301. stops: [
  3302. [0, '#FFF'],
  3303. [1, '#ACF']
  3304. ]
  3305. }
  3306. }, hoverState);
  3307. hoverStyle = hoverState.style;
  3308. delete hoverState.style;
  3309. // Pressed state
  3310. pressedState = merge(normalState, {
  3311. stroke: '#68A',
  3312. fill: {
  3313. linearGradient: verticalGradient,
  3314. stops: [
  3315. [0, '#9BD'],
  3316. [1, '#CDF']
  3317. ]
  3318. }
  3319. }, pressedState);
  3320. pressedStyle = pressedState.style;
  3321. delete pressedState.style;
  3322. // Disabled state
  3323. disabledState = merge(normalState, {
  3324. style: {
  3325. color: '#CCC'
  3326. }
  3327. }, disabledState);
  3328. disabledStyle = disabledState.style;
  3329. delete disabledState.style;
  3330. // Add the events. IE9 and IE10 need mouseover and mouseout to funciton (#667).
  3331. addEvent(label.element, isMS ? 'mouseover' : 'mouseenter', function () {
  3332. if (curState !== 3) {
  3333. label.attr(hoverState)
  3334. .css(hoverStyle);
  3335. }
  3336. });
  3337. addEvent(label.element, isMS ? 'mouseout' : 'mouseleave', function () {
  3338. if (curState !== 3) {
  3339. stateOptions = [normalState, hoverState, pressedState][curState];
  3340. stateStyle = [normalStyle, hoverStyle, pressedStyle][curState];
  3341. label.attr(stateOptions)
  3342. .css(stateStyle);
  3343. }
  3344. });
  3345. label.setState = function (state) {
  3346. label.state = curState = state;
  3347. if (!state) {
  3348. label.attr(normalState)
  3349. .css(normalStyle);
  3350. } else if (state === 2) {
  3351. label.attr(pressedState)
  3352. .css(pressedStyle);
  3353. } else if (state === 3) {
  3354. label.attr(disabledState)
  3355. .css(disabledStyle);
  3356. }
  3357. };
  3358. return label
  3359. .on('click', function (e) {
  3360. if (curState !== 3) {
  3361. callback.call(label, e);
  3362. }
  3363. })
  3364. .attr(normalState)
  3365. .css(extend({ cursor: 'default' }, normalStyle));
  3366. },
  3367. /**
  3368. * Make a straight line crisper by not spilling out to neighbour pixels
  3369. * @param {Array} points
  3370. * @param {Number} width
  3371. */
  3372. crispLine: function (points, width) {
  3373. // points format: [M, 0, 0, L, 100, 0]
  3374. // normalize to a crisp line
  3375. if (points[1] === points[4]) {
  3376. // Substract due to #1129. Now bottom and left axis gridlines behave the same.
  3377. points[1] = points[4] = mathRound(points[1]) - (width % 2 / 2);
  3378. }
  3379. if (points[2] === points[5]) {
  3380. points[2] = points[5] = mathRound(points[2]) + (width % 2 / 2);
  3381. }
  3382. return points;
  3383. },
  3384. /**
  3385. * Draw a path
  3386. * @param {Array} path An SVG path in array form
  3387. */
  3388. path: function (path) {
  3389. var attr = {
  3390. fill: NONE
  3391. };
  3392. if (isArray(path)) {
  3393. attr.d = path;
  3394. } else if (isObject(path)) { // attributes
  3395. extend(attr, path);
  3396. }
  3397. return this.createElement('path').attr(attr);
  3398. },
  3399. /**
  3400. * Draw and return an SVG circle
  3401. * @param {Number} x The x position
  3402. * @param {Number} y The y position
  3403. * @param {Number} r The radius
  3404. */
  3405. circle: function (x, y, r) {
  3406. var attr = isObject(x) ? x : { x: x, y: y, r: r },
  3407. wrapper = this.createElement('circle');
  3408. // Setting x or y translates to cx and cy
  3409. wrapper.xSetter = wrapper.ySetter = function (value, key, element) {
  3410. element.setAttribute('c' + key, value);
  3411. };
  3412. return wrapper.attr(attr);
  3413. },
  3414. /**
  3415. * Draw and return an arc
  3416. * @param {Number} x X position
  3417. * @param {Number} y Y position
  3418. * @param {Number} r Radius
  3419. * @param {Number} innerR Inner radius like used in donut charts
  3420. * @param {Number} start Starting angle
  3421. * @param {Number} end Ending angle
  3422. */
  3423. arc: function (x, y, r, innerR, start, end) {
  3424. var arc;
  3425. if (isObject(x)) {
  3426. y = x.y;
  3427. r = x.r;
  3428. innerR = x.innerR;
  3429. start = x.start;
  3430. end = x.end;
  3431. x = x.x;
  3432. }
  3433. // Arcs are defined as symbols for the ability to set
  3434. // attributes in attr and animate
  3435. arc = this.symbol('arc', x || 0, y || 0, r || 0, r || 0, {
  3436. innerR: innerR || 0,
  3437. start: start || 0,
  3438. end: end || 0
  3439. });
  3440. arc.r = r; // #959
  3441. return arc;
  3442. },
  3443. /**
  3444. * Draw and return a rectangle
  3445. * @param {Number} x Left position
  3446. * @param {Number} y Top position
  3447. * @param {Number} width
  3448. * @param {Number} height
  3449. * @param {Number} r Border corner radius
  3450. * @param {Number} strokeWidth A stroke width can be supplied to allow crisp drawing
  3451. */
  3452. rect: function (x, y, width, height, r, strokeWidth) {
  3453. r = isObject(x) ? x.r : r;
  3454. var wrapper = this.createElement('rect'),
  3455. attribs = isObject(x) ? x : x === UNDEFINED ? {} : {
  3456. x: x,
  3457. y: y,
  3458. width: mathMax(width, 0),
  3459. height: mathMax(height, 0)
  3460. };
  3461. if (strokeWidth !== UNDEFINED) {
  3462. wrapper.strokeWidth = strokeWidth;
  3463. attribs = wrapper.crisp(attribs);
  3464. }
  3465. if (r) {
  3466. attribs.r = r;
  3467. }
  3468. wrapper.rSetter = function (value, key, element) {
  3469. attr(element, {
  3470. rx: value,
  3471. ry: value
  3472. });
  3473. };
  3474. return wrapper.attr(attribs);
  3475. },
  3476. /**
  3477. * Resize the box and re-align all aligned elements
  3478. * @param {Object} width
  3479. * @param {Object} height
  3480. * @param {Boolean} animate
  3481. *
  3482. */
  3483. setSize: function (width, height, animate) {
  3484. var renderer = this,
  3485. alignedObjects = renderer.alignedObjects,
  3486. i = alignedObjects.length;
  3487. renderer.width = width;
  3488. renderer.height = height;
  3489. renderer.boxWrapper[pick(animate, true) ? 'animate' : 'attr']({
  3490. width: width,
  3491. height: height
  3492. });
  3493. while (i--) {
  3494. alignedObjects[i].align();
  3495. }
  3496. },
  3497. /**
  3498. * Create a group
  3499. * @param {String} name The group will be given a class name of 'highcharts-{name}'.
  3500. * This can be used for styling and scripting.
  3501. */
  3502. g: function (name) {
  3503. var elem = this.createElement('g');
  3504. return defined(name) ? elem.attr({ 'class': PREFIX + name }) : elem;
  3505. },
  3506. /**
  3507. * Display an image
  3508. * @param {String} src
  3509. * @param {Number} x
  3510. * @param {Number} y
  3511. * @param {Number} width
  3512. * @param {Number} height
  3513. */
  3514. image: function (src, x, y, width, height) {
  3515. var attribs = {
  3516. preserveAspectRatio: NONE
  3517. },
  3518. elemWrapper;
  3519. // optional properties
  3520. if (arguments.length > 1) {
  3521. extend(attribs, {
  3522. x: x,
  3523. y: y,
  3524. width: width,
  3525. height: height
  3526. });
  3527. }
  3528. elemWrapper = this.createElement('image').attr(attribs);
  3529. // set the href in the xlink namespace
  3530. if (elemWrapper.element.setAttributeNS) {
  3531. elemWrapper.element.setAttributeNS('http://www.w3.org/1999/xlink',
  3532. 'href', src);
  3533. } else {
  3534. // could be exporting in IE
  3535. // using href throws "not supported" in ie7 and under, requries regex shim to fix later
  3536. elemWrapper.element.setAttribute('hc-svg-href', src);
  3537. }
  3538. return elemWrapper;
  3539. },
  3540. /**
  3541. * Draw a symbol out of pre-defined shape paths from the namespace 'symbol' object.
  3542. *
  3543. * @param {Object} symbol
  3544. * @param {Object} x
  3545. * @param {Object} y
  3546. * @param {Object} radius
  3547. * @param {Object} options
  3548. */
  3549. symbol: function (symbol, x, y, width, height, options) {
  3550. var ren = this,
  3551. obj,
  3552. // get the symbol definition function
  3553. symbolFn = this.symbols[symbol],
  3554. // check if there's a path defined for this symbol
  3555. path = symbolFn && symbolFn(
  3556. mathRound(x),
  3557. mathRound(y),
  3558. width,
  3559. height,
  3560. options
  3561. ),
  3562. imageRegex = /^url\((.*?)\)$/,
  3563. imageSrc,
  3564. imageSize,
  3565. centerImage;
  3566. if (path) {
  3567. obj = this.path(path);
  3568. // expando properties for use in animate and attr
  3569. extend(obj, {
  3570. symbolName: symbol,
  3571. x: x,
  3572. y: y,
  3573. width: width,
  3574. height: height
  3575. });
  3576. if (options) {
  3577. extend(obj, options);
  3578. }
  3579. // image symbols
  3580. } else if (imageRegex.test(symbol)) {
  3581. // On image load, set the size and position
  3582. centerImage = function (img, size) {
  3583. if (img.element) { // it may be destroyed in the meantime (#1390)
  3584. img.attr({
  3585. width: size[0],
  3586. height: size[1]
  3587. });
  3588. if (!img.alignByTranslate) { // #185
  3589. img.translate(
  3590. mathRound((width - size[0]) / 2), // #1378
  3591. mathRound((height - size[1]) / 2)
  3592. );
  3593. }
  3594. }
  3595. };
  3596. imageSrc = symbol.match(imageRegex)[1];
  3597. imageSize = symbolSizes[imageSrc] || (options && options.width && options.height && [options.width, options.height]);
  3598. // Ireate the image synchronously, add attribs async
  3599. obj = this.image(imageSrc)
  3600. .attr({
  3601. x: x,
  3602. y: y
  3603. });
  3604. obj.isImg = true;
  3605. if (imageSize) {
  3606. centerImage(obj, imageSize);
  3607. } else {
  3608. // Initialize image to be 0 size so export will still function if there's no cached sizes.
  3609. obj.attr({ width: 0, height: 0 });
  3610. // Create a dummy JavaScript image to get the width and height. Due to a bug in IE < 8,
  3611. // the created element must be assigned to a variable in order to load (#292).
  3612. createElement('img', {
  3613. onload: function () {
  3614. // Special case for SVGs on IE11, the width is not accessible until the image is
  3615. // part of the DOM (#2854).
  3616. if (this.width === 0) {
  3617. css(this, {
  3618. position: ABSOLUTE,
  3619. top: '-999em'
  3620. });
  3621. doc.body.appendChild(this);
  3622. }
  3623. // Center the image
  3624. centerImage(obj, symbolSizes[imageSrc] = [this.width, this.height]);
  3625. // Clean up after #2854 workaround.
  3626. if (this.parentNode) {
  3627. this.parentNode.removeChild(this);
  3628. }
  3629. // Fire the load event when all external images are loaded
  3630. ren.imgCount--;
  3631. if (!ren.imgCount && charts[ren.chartIndex].onload) {
  3632. charts[ren.chartIndex].onload();
  3633. }
  3634. },
  3635. src: imageSrc
  3636. });
  3637. this.imgCount++;
  3638. }
  3639. }
  3640. return obj;
  3641. },
  3642. /**
  3643. * An extendable collection of functions for defining symbol paths.
  3644. */
  3645. symbols: {
  3646. 'circle': function (x, y, w, h) {
  3647. var cpw = 0.166 * w;
  3648. return [
  3649. M, x + w / 2, y,
  3650. 'C', x + w + cpw, y, x + w + cpw, y + h, x + w / 2, y + h,
  3651. 'C', x - cpw, y + h, x - cpw, y, x + w / 2, y,
  3652. 'Z'
  3653. ];
  3654. },
  3655. 'square': function (x, y, w, h) {
  3656. return [
  3657. M, x, y,
  3658. L, x + w, y,
  3659. x + w, y + h,
  3660. x, y + h,
  3661. 'Z'
  3662. ];
  3663. },
  3664. 'triangle': function (x, y, w, h) {
  3665. return [
  3666. M, x + w / 2, y,
  3667. L, x + w, y + h,
  3668. x, y + h,
  3669. 'Z'
  3670. ];
  3671. },
  3672. 'triangle-down': function (x, y, w, h) {
  3673. return [
  3674. M, x, y,
  3675. L, x + w, y,
  3676. x + w / 2, y + h,
  3677. 'Z'
  3678. ];
  3679. },
  3680. 'diamond': function (x, y, w, h) {
  3681. return [
  3682. M, x + w / 2, y,
  3683. L, x + w, y + h / 2,
  3684. x + w / 2, y + h,
  3685. x, y + h / 2,
  3686. 'Z'
  3687. ];
  3688. },
  3689. 'arc': function (x, y, w, h, options) {
  3690. var start = options.start,
  3691. radius = options.r || w || h,
  3692. end = options.end - 0.001, // to prevent cos and sin of start and end from becoming equal on 360 arcs (related: #1561)
  3693. innerRadius = options.innerR,
  3694. open = options.open,
  3695. cosStart = mathCos(start),
  3696. sinStart = mathSin(start),
  3697. cosEnd = mathCos(end),
  3698. sinEnd = mathSin(end),
  3699. longArc = options.end - start < mathPI ? 0 : 1;
  3700. return [
  3701. M,
  3702. x + radius * cosStart,
  3703. y + radius * sinStart,
  3704. 'A', // arcTo
  3705. radius, // x radius
  3706. radius, // y radius
  3707. 0, // slanting
  3708. longArc, // long or short arc
  3709. 1, // clockwise
  3710. x + radius * cosEnd,
  3711. y + radius * sinEnd,
  3712. open ? M : L,
  3713. x + innerRadius * cosEnd,
  3714. y + innerRadius * sinEnd,
  3715. 'A', // arcTo
  3716. innerRadius, // x radius
  3717. innerRadius, // y radius
  3718. 0, // slanting
  3719. longArc, // long or short arc
  3720. 0, // clockwise
  3721. x + innerRadius * cosStart,
  3722. y + innerRadius * sinStart,
  3723. open ? '' : 'Z' // close
  3724. ];
  3725. },
  3726. /**
  3727. * Callout shape used for default tooltips, also used for rounded rectangles in VML
  3728. */
  3729. callout: function (x, y, w, h, options) {
  3730. var arrowLength = 6,
  3731. halfDistance = 6,
  3732. r = mathMin((options && options.r) || 0, w, h),
  3733. safeDistance = r + halfDistance,
  3734. anchorX = options && options.anchorX,
  3735. anchorY = options && options.anchorY,
  3736. path;
  3737. path = [
  3738. 'M', x + r, y,
  3739. 'L', x + w - r, y, // top side
  3740. 'C', x + w, y, x + w, y, x + w, y + r, // top-right corner
  3741. 'L', x + w, y + h - r, // right side
  3742. 'C', x + w, y + h, x + w, y + h, x + w - r, y + h, // bottom-right corner
  3743. 'L', x + r, y + h, // bottom side
  3744. 'C', x, y + h, x, y + h, x, y + h - r, // bottom-left corner
  3745. 'L', x, y + r, // left side
  3746. 'C', x, y, x, y, x + r, y // top-right corner
  3747. ];
  3748. if (anchorX && anchorX > w && anchorY > y + safeDistance && anchorY < y + h - safeDistance) { // replace right side
  3749. path.splice(13, 3,
  3750. 'L', x + w, anchorY - halfDistance,
  3751. x + w + arrowLength, anchorY,
  3752. x + w, anchorY + halfDistance,
  3753. x + w, y + h - r
  3754. );
  3755. } else if (anchorX && anchorX < 0 && anchorY > y + safeDistance && anchorY < y + h - safeDistance) { // replace left side
  3756. path.splice(33, 3,
  3757. 'L', x, anchorY + halfDistance,
  3758. x - arrowLength, anchorY,
  3759. x, anchorY - halfDistance,
  3760. x, y + r
  3761. );
  3762. } else if (anchorY && anchorY > h && anchorX > x + safeDistance && anchorX < x + w - safeDistance) { // replace bottom
  3763. path.splice(23, 3,
  3764. 'L', anchorX + halfDistance, y + h,
  3765. anchorX, y + h + arrowLength,
  3766. anchorX - halfDistance, y + h,
  3767. x + r, y + h
  3768. );
  3769. } else if (anchorY && anchorY < 0 && anchorX > x + safeDistance && anchorX < x + w - safeDistance) { // replace top
  3770. path.splice(3, 3,
  3771. 'L', anchorX - halfDistance, y,
  3772. anchorX, y - arrowLength,
  3773. anchorX + halfDistance, y,
  3774. w - r, y
  3775. );
  3776. }
  3777. return path;
  3778. }
  3779. },
  3780. /**
  3781. * Define a clipping rectangle
  3782. * @param {String} id
  3783. * @param {Number} x
  3784. * @param {Number} y
  3785. * @param {Number} width
  3786. * @param {Number} height
  3787. */
  3788. clipRect: function (x, y, width, height) {
  3789. var wrapper,
  3790. id = PREFIX + idCounter++,
  3791. clipPath = this.createElement('clipPath').attr({
  3792. id: id
  3793. }).add(this.defs);
  3794. wrapper = this.rect(x, y, width, height, 0).add(clipPath);
  3795. wrapper.id = id;
  3796. wrapper.clipPath = clipPath;
  3797. wrapper.count = 0;
  3798. return wrapper;
  3799. },
  3800. /**
  3801. * Add text to the SVG object
  3802. * @param {String} str
  3803. * @param {Number} x Left position
  3804. * @param {Number} y Top position
  3805. * @param {Boolean} useHTML Use HTML to render the text
  3806. */
  3807. text: function (str, x, y, useHTML) {
  3808. // declare variables
  3809. var renderer = this,
  3810. fakeSVG = useCanVG || (!hasSVG && renderer.forExport),
  3811. wrapper,
  3812. attr = {};
  3813. if (useHTML && (renderer.allowHTML || !renderer.forExport)) {
  3814. return renderer.html(str, x, y);
  3815. }
  3816. attr.x = Math.round(x || 0); // X is always needed for line-wrap logic
  3817. if (y) {
  3818. attr.y = Math.round(y);
  3819. }
  3820. if (str || str === 0) {
  3821. attr.text = str;
  3822. }
  3823. wrapper = renderer.createElement('text')
  3824. .attr(attr);
  3825. // Prevent wrapping from creating false offsetWidths in export in legacy IE (#1079, #1063)
  3826. if (fakeSVG) {
  3827. wrapper.css({
  3828. position: ABSOLUTE
  3829. });
  3830. }
  3831. if (!useHTML) {
  3832. wrapper.xSetter = function (value, key, element) {
  3833. var tspans = element.getElementsByTagName('tspan'),
  3834. tspan,
  3835. parentVal = element.getAttribute(key),
  3836. i;
  3837. for (i = 0; i < tspans.length; i++) {
  3838. tspan = tspans[i];
  3839. // If the x values are equal, the tspan represents a linebreak
  3840. if (tspan.getAttribute(key) === parentVal) {
  3841. tspan.setAttribute(key, value);
  3842. }
  3843. }
  3844. element.setAttribute(key, value);
  3845. };
  3846. }
  3847. return wrapper;
  3848. },
  3849. /**
  3850. * Utility to return the baseline offset and total line height from the font size
  3851. */
  3852. fontMetrics: function (fontSize, elem) {
  3853. var lineHeight,
  3854. baseline,
  3855. style;
  3856. fontSize = fontSize || this.style.fontSize;
  3857. if (!fontSize && elem && win.getComputedStyle) {
  3858. elem = elem.element || elem; // SVGElement
  3859. style = win.getComputedStyle(elem, '');
  3860. fontSize = style && style.fontSize; // #4309, the style doesn't exist inside a hidden iframe in Firefox
  3861. }
  3862. fontSize = /px/.test(fontSize) ? pInt(fontSize) : /em/.test(fontSize) ? parseFloat(fontSize) * 12 : 12;
  3863. // Empirical values found by comparing font size and bounding box height.
  3864. // Applies to the default font family. http://jsfiddle.net/highcharts/7xvn7/
  3865. lineHeight = fontSize < 24 ? fontSize + 3 : mathRound(fontSize * 1.2);
  3866. baseline = mathRound(lineHeight * 0.8);
  3867. return {
  3868. h: lineHeight,
  3869. b: baseline,
  3870. f: fontSize
  3871. };
  3872. },
  3873. /**
  3874. * Correct X and Y positioning of a label for rotation (#1764)
  3875. */
  3876. rotCorr: function (baseline, rotation, alterY) {
  3877. var y = baseline;
  3878. if (rotation && alterY) {
  3879. y = mathMax(y * mathCos(rotation * deg2rad), 4);
  3880. }
  3881. return {
  3882. x: (-baseline / 3) * mathSin(rotation * deg2rad),
  3883. y: y
  3884. };
  3885. },
  3886. /**
  3887. * Add a label, a text item that can hold a colored or gradient background
  3888. * as well as a border and shadow.
  3889. * @param {string} str
  3890. * @param {Number} x
  3891. * @param {Number} y
  3892. * @param {String} shape
  3893. * @param {Number} anchorX In case the shape has a pointer, like a flag, this is the
  3894. * coordinates it should be pinned to
  3895. * @param {Number} anchorY
  3896. * @param {Boolean} baseline Whether to position the label relative to the text baseline,
  3897. * like renderer.text, or to the upper border of the rectangle.
  3898. * @param {String} className Class name for the group
  3899. */
  3900. label: function (str, x, y, shape, anchorX, anchorY, useHTML, baseline, className) {
  3901. var renderer = this,
  3902. wrapper = renderer.g(className),
  3903. text = renderer.text('', 0, 0, useHTML)
  3904. .attr({
  3905. zIndex: 1
  3906. }),
  3907. //.add(wrapper),
  3908. box,
  3909. bBox,
  3910. alignFactor = 0,
  3911. padding = 3,
  3912. paddingLeft = 0,
  3913. width,
  3914. height,
  3915. wrapperX,
  3916. wrapperY,
  3917. crispAdjust = 0,
  3918. deferredAttr = {},
  3919. baselineOffset,
  3920. needsBox,
  3921. updateBoxSize,
  3922. updateTextPadding,
  3923. boxAttr;
  3924. /**
  3925. * This function runs after the label is added to the DOM (when the bounding box is
  3926. * available), and after the text of the label is updated to detect the new bounding
  3927. * box and reflect it in the border box.
  3928. */
  3929. updateBoxSize = function () {
  3930. var boxX,
  3931. boxY,
  3932. style = text.element.style;
  3933. bBox = (width === undefined || height === undefined || wrapper.styles.textAlign) && defined(text.textStr) &&
  3934. text.getBBox(); //#3295 && 3514 box failure when string equals 0
  3935. wrapper.width = (width || bBox.width || 0) + 2 * padding + paddingLeft;
  3936. wrapper.height = (height || bBox.height || 0) + 2 * padding;
  3937. // update the label-scoped y offset
  3938. baselineOffset = padding + renderer.fontMetrics(style && style.fontSize, text).b;
  3939. if (needsBox) {
  3940. if (!box) {
  3941. // create the border box if it is not already present
  3942. boxX = crispAdjust;
  3943. boxY = (baseline ? -baselineOffset : 0) + crispAdjust;
  3944. wrapper.box = box = shape ?
  3945. renderer.symbol(shape, boxX, boxY, wrapper.width, wrapper.height, deferredAttr) :
  3946. renderer.rect(boxX, boxY, wrapper.width, wrapper.height, 0, deferredAttr[STROKE_WIDTH]);
  3947. if (!box.isImg) { // #4324, fill "none" causes it to be ignored by mouse events in IE
  3948. box.attr('fill', NONE);
  3949. }
  3950. box.add(wrapper);
  3951. }
  3952. // apply the box attributes
  3953. if (!box.isImg) { // #1630
  3954. box.attr(extend({
  3955. width: mathRound(wrapper.width),
  3956. height: mathRound(wrapper.height)
  3957. }, deferredAttr));
  3958. }
  3959. deferredAttr = null;
  3960. }
  3961. };
  3962. /**
  3963. * This function runs after setting text or padding, but only if padding is changed
  3964. */
  3965. updateTextPadding = function () {
  3966. var styles = wrapper.styles,
  3967. textAlign = styles && styles.textAlign,
  3968. x = paddingLeft + padding,
  3969. y;
  3970. // determin y based on the baseline
  3971. y = baseline ? 0 : baselineOffset;
  3972. // compensate for alignment
  3973. if (defined(width) && bBox && (textAlign === 'center' || textAlign === 'right')) {
  3974. x += { center: 0.5, right: 1 }[textAlign] * (width - bBox.width);
  3975. }
  3976. // update if anything changed
  3977. if (x !== text.x || y !== text.y) {
  3978. text.attr('x', x);
  3979. if (y !== UNDEFINED) {
  3980. text.attr('y', y);
  3981. }
  3982. }
  3983. // record current values
  3984. text.x = x;
  3985. text.y = y;
  3986. };
  3987. /**
  3988. * Set a box attribute, or defer it if the box is not yet created
  3989. * @param {Object} key
  3990. * @param {Object} value
  3991. */
  3992. boxAttr = function (key, value) {
  3993. if (box) {
  3994. box.attr(key, value);
  3995. } else {
  3996. deferredAttr[key] = value;
  3997. }
  3998. };
  3999. /**
  4000. * After the text element is added, get the desired size of the border box
  4001. * and add it before the text in the DOM.
  4002. */
  4003. wrapper.onAdd = function () {
  4004. text.add(wrapper);
  4005. wrapper.attr({
  4006. text: (str || str === 0) ? str : '', // alignment is available now // #3295: 0 not rendered if given as a value
  4007. x: x,
  4008. y: y
  4009. });
  4010. if (box && defined(anchorX)) {
  4011. wrapper.attr({
  4012. anchorX: anchorX,
  4013. anchorY: anchorY
  4014. });
  4015. }
  4016. };
  4017. /*
  4018. * Add specific attribute setters.
  4019. */
  4020. // only change local variables
  4021. wrapper.widthSetter = function (value) {
  4022. width = value;
  4023. };
  4024. wrapper.heightSetter = function (value) {
  4025. height = value;
  4026. };
  4027. wrapper.paddingSetter = function (value) {
  4028. if (defined(value) && value !== padding) {
  4029. padding = wrapper.padding = value;
  4030. updateTextPadding();
  4031. }
  4032. };
  4033. wrapper.paddingLeftSetter = function (value) {
  4034. if (defined(value) && value !== paddingLeft) {
  4035. paddingLeft = value;
  4036. updateTextPadding();
  4037. }
  4038. };
  4039. // change local variable and prevent setting attribute on the group
  4040. wrapper.alignSetter = function (value) {
  4041. value = { left: 0, center: 0.5, right: 1 }[value];
  4042. if (value !== alignFactor) {
  4043. alignFactor = value;
  4044. if (bBox) { // Bounding box exists, means we're dynamically changing
  4045. wrapper.attr({ x: wrapperX }); // #5134
  4046. }
  4047. }
  4048. };
  4049. // apply these to the box and the text alike
  4050. wrapper.textSetter = function (value) {
  4051. if (value !== UNDEFINED) {
  4052. text.textSetter(value);
  4053. }
  4054. updateBoxSize();
  4055. updateTextPadding();
  4056. };
  4057. // apply these to the box but not to the text
  4058. wrapper['stroke-widthSetter'] = function (value, key) {
  4059. if (value) {
  4060. needsBox = true;
  4061. }
  4062. crispAdjust = value % 2 / 2;
  4063. boxAttr(key, value);
  4064. };
  4065. wrapper.strokeSetter = wrapper.fillSetter = wrapper.rSetter = function (value, key) {
  4066. if (key === 'fill' && value) {
  4067. needsBox = true;
  4068. }
  4069. boxAttr(key, value);
  4070. };
  4071. wrapper.anchorXSetter = function (value, key) {
  4072. anchorX = value;
  4073. boxAttr(key, mathRound(value) - crispAdjust - wrapperX);
  4074. };
  4075. wrapper.anchorYSetter = function (value, key) {
  4076. anchorY = value;
  4077. boxAttr(key, value - wrapperY);
  4078. };
  4079. // rename attributes
  4080. wrapper.xSetter = function (value) {
  4081. wrapper.x = value; // for animation getter
  4082. if (alignFactor) {
  4083. value -= alignFactor * ((width || bBox.width) + 2 * padding);
  4084. }
  4085. wrapperX = mathRound(value);
  4086. wrapper.attr('translateX', wrapperX);
  4087. };
  4088. wrapper.ySetter = function (value) {
  4089. wrapperY = wrapper.y = mathRound(value);
  4090. wrapper.attr('translateY', wrapperY);
  4091. };
  4092. // Redirect certain methods to either the box or the text
  4093. var baseCss = wrapper.css;
  4094. return extend(wrapper, {
  4095. /**
  4096. * Pick up some properties and apply them to the text instead of the wrapper
  4097. */
  4098. css: function (styles) {
  4099. if (styles) {
  4100. var textStyles = {};
  4101. styles = merge(styles); // create a copy to avoid altering the original object (#537)
  4102. each(wrapper.textProps, function (prop) {
  4103. if (styles[prop] !== UNDEFINED) {
  4104. textStyles[prop] = styles[prop];
  4105. delete styles[prop];
  4106. }
  4107. });
  4108. text.css(textStyles);
  4109. }
  4110. return baseCss.call(wrapper, styles);
  4111. },
  4112. /**
  4113. * Return the bounding box of the box, not the group
  4114. */
  4115. getBBox: function () {
  4116. return {
  4117. width: bBox.width + 2 * padding,
  4118. height: bBox.height + 2 * padding,
  4119. x: bBox.x - padding,
  4120. y: bBox.y - padding
  4121. };
  4122. },
  4123. /**
  4124. * Apply the shadow to the box
  4125. */
  4126. shadow: function (b) {
  4127. if (box) {
  4128. box.shadow(b);
  4129. }
  4130. return wrapper;
  4131. },
  4132. /**
  4133. * Destroy and release memory.
  4134. */
  4135. destroy: function () {
  4136. // Added by button implementation
  4137. removeEvent(wrapper.element, 'mouseenter');
  4138. removeEvent(wrapper.element, 'mouseleave');
  4139. if (text) {
  4140. text = text.destroy();
  4141. }
  4142. if (box) {
  4143. box = box.destroy();
  4144. }
  4145. // Call base implementation to destroy the rest
  4146. SVGElement.prototype.destroy.call(wrapper);
  4147. // Release local pointers (#1298)
  4148. wrapper = renderer = updateBoxSize = updateTextPadding = boxAttr = null;
  4149. }
  4150. });
  4151. }
  4152. }; // end SVGRenderer
  4153. // general renderer
  4154. Renderer = SVGRenderer;
  4155. // extend SvgElement for useHTML option
  4156. extend(SVGElement.prototype, {
  4157. /**
  4158. * Apply CSS to HTML elements. This is used in text within SVG rendering and
  4159. * by the VML renderer
  4160. */
  4161. htmlCss: function (styles) {
  4162. var wrapper = this,
  4163. element = wrapper.element,
  4164. textWidth = styles && element.tagName === 'SPAN' && styles.width;
  4165. if (textWidth) {
  4166. delete styles.width;
  4167. wrapper.textWidth = textWidth;
  4168. wrapper.updateTransform();
  4169. }
  4170. if (styles && styles.textOverflow === 'ellipsis') {
  4171. styles.whiteSpace = 'nowrap';
  4172. styles.overflow = 'hidden';
  4173. }
  4174. wrapper.styles = extend(wrapper.styles, styles);
  4175. css(wrapper.element, styles);
  4176. return wrapper;
  4177. },
  4178. /**
  4179. * VML and useHTML method for calculating the bounding box based on offsets
  4180. * @param {Boolean} refresh Whether to force a fresh value from the DOM or to
  4181. * use the cached value
  4182. *
  4183. * @return {Object} A hash containing values for x, y, width and height
  4184. */
  4185. htmlGetBBox: function () {
  4186. var wrapper = this,
  4187. element = wrapper.element;
  4188. // faking getBBox in exported SVG in legacy IE
  4189. // faking getBBox in exported SVG in legacy IE (is this a duplicate of the fix for #1079?)
  4190. if (element.nodeName === 'text') {
  4191. element.style.position = ABSOLUTE;
  4192. }
  4193. return {
  4194. x: element.offsetLeft,
  4195. y: element.offsetTop,
  4196. width: element.offsetWidth,
  4197. height: element.offsetHeight
  4198. };
  4199. },
  4200. /**
  4201. * VML override private method to update elements based on internal
  4202. * properties based on SVG transform
  4203. */
  4204. htmlUpdateTransform: function () {
  4205. // aligning non added elements is expensive
  4206. if (!this.added) {
  4207. this.alignOnAdd = true;
  4208. return;
  4209. }
  4210. var wrapper = this,
  4211. renderer = wrapper.renderer,
  4212. elem = wrapper.element,
  4213. translateX = wrapper.translateX || 0,
  4214. translateY = wrapper.translateY || 0,
  4215. x = wrapper.x || 0,
  4216. y = wrapper.y || 0,
  4217. align = wrapper.textAlign || 'left',
  4218. alignCorrection = { left: 0, center: 0.5, right: 1 }[align],
  4219. shadows = wrapper.shadows,
  4220. styles = wrapper.styles;
  4221. // apply translate
  4222. css(elem, {
  4223. marginLeft: translateX,
  4224. marginTop: translateY
  4225. });
  4226. if (shadows) { // used in labels/tooltip
  4227. each(shadows, function (shadow) {
  4228. css(shadow, {
  4229. marginLeft: translateX + 1,
  4230. marginTop: translateY + 1
  4231. });
  4232. });
  4233. }
  4234. // apply inversion
  4235. if (wrapper.inverted) { // wrapper is a group
  4236. each(elem.childNodes, function (child) {
  4237. renderer.invertChild(child, elem);
  4238. });
  4239. }
  4240. if (elem.tagName === 'SPAN') {
  4241. var rotation = wrapper.rotation,
  4242. baseline,
  4243. textWidth = pInt(wrapper.textWidth),
  4244. whiteSpace = styles && styles.whiteSpace,
  4245. currentTextTransform = [rotation, align, elem.innerHTML, wrapper.textWidth, wrapper.textAlign].join(',');
  4246. if (currentTextTransform !== wrapper.cTT) { // do the calculations and DOM access only if properties changed
  4247. baseline = renderer.fontMetrics(elem.style.fontSize).b;
  4248. // Renderer specific handling of span rotation
  4249. if (defined(rotation)) {
  4250. wrapper.setSpanRotation(rotation, alignCorrection, baseline);
  4251. }
  4252. // Update textWidth
  4253. if (elem.offsetWidth > textWidth && /[ \-]/.test(elem.textContent || elem.innerText)) { // #983, #1254
  4254. css(elem, {
  4255. width: textWidth + PX,
  4256. display: 'block',
  4257. whiteSpace: whiteSpace || 'normal' // #3331
  4258. });
  4259. wrapper.hasTextWidth = true;
  4260. } else if (wrapper.hasTextWidth) { // #4928
  4261. css(elem, {
  4262. width: '',
  4263. display: '',
  4264. whiteSpace: whiteSpace || 'nowrap'
  4265. });
  4266. wrapper.hasTextWidth = false;
  4267. }
  4268. wrapper.getSpanCorrection(wrapper.hasTextWidth ? textWidth : elem.offsetWidth, baseline, alignCorrection, rotation, align);
  4269. }
  4270. // apply position with correction
  4271. css(elem, {
  4272. left: (x + (wrapper.xCorr || 0)) + PX,
  4273. top: (y + (wrapper.yCorr || 0)) + PX
  4274. });
  4275. // force reflow in webkit to apply the left and top on useHTML element (#1249)
  4276. if (isWebKit) {
  4277. baseline = elem.offsetHeight; // assigned to baseline for lint purpose
  4278. }
  4279. // record current text transform
  4280. wrapper.cTT = currentTextTransform;
  4281. }
  4282. },
  4283. /**
  4284. * Set the rotation of an individual HTML span
  4285. */
  4286. setSpanRotation: function (rotation, alignCorrection, baseline) {
  4287. var rotationStyle = {},
  4288. cssTransformKey = isMS ? '-ms-transform' : isWebKit ? '-webkit-transform' : isFirefox ? 'MozTransform' : isOpera ? '-o-transform' : '';
  4289. rotationStyle[cssTransformKey] = rotationStyle.transform = 'rotate(' + rotation + 'deg)';
  4290. rotationStyle[cssTransformKey + (isFirefox ? 'Origin' : '-origin')] = rotationStyle.transformOrigin = (alignCorrection * 100) + '% ' + baseline + 'px';
  4291. css(this.element, rotationStyle);
  4292. },
  4293. /**
  4294. * Get the correction in X and Y positioning as the element is rotated.
  4295. */
  4296. getSpanCorrection: function (width, baseline, alignCorrection) {
  4297. this.xCorr = -width * alignCorrection;
  4298. this.yCorr = -baseline;
  4299. }
  4300. });
  4301. // Extend SvgRenderer for useHTML option.
  4302. extend(SVGRenderer.prototype, {
  4303. /**
  4304. * Create HTML text node. This is used by the VML renderer as well as the SVG
  4305. * renderer through the useHTML option.
  4306. *
  4307. * @param {String} str
  4308. * @param {Number} x
  4309. * @param {Number} y
  4310. */
  4311. html: function (str, x, y) {
  4312. var wrapper = this.createElement('span'),
  4313. element = wrapper.element,
  4314. renderer = wrapper.renderer,
  4315. isSVG = renderer.isSVG,
  4316. addSetters = function (element, style) {
  4317. // These properties are set as attributes on the SVG group, and as
  4318. // identical CSS properties on the div. (#3542)
  4319. each(['opacity', 'visibility'], function (prop) {
  4320. wrap(element, prop + 'Setter', function (proceed, value, key, elem) {
  4321. proceed.call(this, value, key, elem);
  4322. style[key] = value;
  4323. });
  4324. });
  4325. };
  4326. // Text setter
  4327. wrapper.textSetter = function (value) {
  4328. if (value !== element.innerHTML) {
  4329. delete this.bBox;
  4330. }
  4331. element.innerHTML = this.textStr = value;
  4332. wrapper.htmlUpdateTransform();
  4333. };
  4334. // Add setters for the element itself (#4938)
  4335. if (isSVG) { // #4938, only for HTML within SVG
  4336. addSetters(wrapper, wrapper.element.style);
  4337. }
  4338. // Various setters which rely on update transform
  4339. wrapper.xSetter = wrapper.ySetter = wrapper.alignSetter = wrapper.rotationSetter = function (value, key) {
  4340. if (key === 'align') {
  4341. key = 'textAlign'; // Do not overwrite the SVGElement.align method. Same as VML.
  4342. }
  4343. wrapper[key] = value;
  4344. wrapper.htmlUpdateTransform();
  4345. };
  4346. // Set the default attributes
  4347. wrapper
  4348. .attr({
  4349. text: str,
  4350. x: mathRound(x),
  4351. y: mathRound(y)
  4352. })
  4353. .css({
  4354. position: ABSOLUTE,
  4355. fontFamily: this.style.fontFamily,
  4356. fontSize: this.style.fontSize
  4357. });
  4358. // Keep the whiteSpace style outside the wrapper.styles collection
  4359. element.style.whiteSpace = 'nowrap';
  4360. // Use the HTML specific .css method
  4361. wrapper.css = wrapper.htmlCss;
  4362. // This is specific for HTML within SVG
  4363. if (isSVG) {
  4364. wrapper.add = function (svgGroupWrapper) {
  4365. var htmlGroup,
  4366. container = renderer.box.parentNode,
  4367. parentGroup,
  4368. parents = [];
  4369. this.parentGroup = svgGroupWrapper;
  4370. // Create a mock group to hold the HTML elements
  4371. if (svgGroupWrapper) {
  4372. htmlGroup = svgGroupWrapper.div;
  4373. if (!htmlGroup) {
  4374. // Read the parent chain into an array and read from top down
  4375. parentGroup = svgGroupWrapper;
  4376. while (parentGroup) {
  4377. parents.push(parentGroup);
  4378. // Move up to the next parent group
  4379. parentGroup = parentGroup.parentGroup;
  4380. }
  4381. // Ensure dynamically updating position when any parent is translated
  4382. each(parents.reverse(), function (parentGroup) {
  4383. var htmlGroupStyle,
  4384. cls = attr(parentGroup.element, 'class');
  4385. if (cls) {
  4386. cls = { className: cls };
  4387. } // else null
  4388. // Create a HTML div and append it to the parent div to emulate
  4389. // the SVG group structure
  4390. htmlGroup = parentGroup.div = parentGroup.div || createElement(DIV, cls, {
  4391. position: ABSOLUTE,
  4392. left: (parentGroup.translateX || 0) + PX,
  4393. top: (parentGroup.translateY || 0) + PX,
  4394. opacity: parentGroup.opacity // #5075
  4395. }, htmlGroup || container); // the top group is appended to container
  4396. // Shortcut
  4397. htmlGroupStyle = htmlGroup.style;
  4398. // Set listeners to update the HTML div's position whenever the SVG group
  4399. // position is changed
  4400. extend(parentGroup, {
  4401. translateXSetter: function (value, key) {
  4402. htmlGroupStyle.left = value + PX;
  4403. parentGroup[key] = value;
  4404. parentGroup.doTransform = true;
  4405. },
  4406. translateYSetter: function (value, key) {
  4407. htmlGroupStyle.top = value + PX;
  4408. parentGroup[key] = value;
  4409. parentGroup.doTransform = true;
  4410. }
  4411. });
  4412. addSetters(parentGroup, htmlGroupStyle);
  4413. });
  4414. }
  4415. } else {
  4416. htmlGroup = container;
  4417. }
  4418. htmlGroup.appendChild(element);
  4419. // Shared with VML:
  4420. wrapper.added = true;
  4421. if (wrapper.alignOnAdd) {
  4422. wrapper.htmlUpdateTransform();
  4423. }
  4424. return wrapper;
  4425. };
  4426. }
  4427. return wrapper;
  4428. }
  4429. });
  4430. /* ****************************************************************************
  4431. * *
  4432. * START OF INTERNET EXPLORER <= 8 SPECIFIC CODE *
  4433. * *
  4434. * For applications and websites that don't need IE support, like platform *
  4435. * targeted mobile apps and web apps, this code can be removed. *
  4436. * *
  4437. *****************************************************************************/
  4438. /**
  4439. * @constructor
  4440. */
  4441. var VMLRenderer, VMLElement;
  4442. if (!hasSVG && !useCanVG) {
  4443. /**
  4444. * The VML element wrapper.
  4445. */
  4446. VMLElement = {
  4447. /**
  4448. * Initialize a new VML element wrapper. It builds the markup as a string
  4449. * to minimize DOM traffic.
  4450. * @param {Object} renderer
  4451. * @param {Object} nodeName
  4452. */
  4453. init: function (renderer, nodeName) {
  4454. var wrapper = this,
  4455. markup = ['<', nodeName, ' filled="f" stroked="f"'],
  4456. style = ['position: ', ABSOLUTE, ';'],
  4457. isDiv = nodeName === DIV;
  4458. // divs and shapes need size
  4459. if (nodeName === 'shape' || isDiv) {
  4460. style.push('left:0;top:0;width:1px;height:1px;');
  4461. }
  4462. style.push('visibility: ', isDiv ? HIDDEN : VISIBLE);
  4463. markup.push(' style="', style.join(''), '"/>');
  4464. // create element with default attributes and style
  4465. if (nodeName) {
  4466. markup = isDiv || nodeName === 'span' || nodeName === 'img' ?
  4467. markup.join('') :
  4468. renderer.prepVML(markup);
  4469. wrapper.element = createElement(markup);
  4470. }
  4471. wrapper.renderer = renderer;
  4472. },
  4473. /**
  4474. * Add the node to the given parent
  4475. * @param {Object} parent
  4476. */
  4477. add: function (parent) {
  4478. var wrapper = this,
  4479. renderer = wrapper.renderer,
  4480. element = wrapper.element,
  4481. box = renderer.box,
  4482. inverted = parent && parent.inverted,
  4483. // get the parent node
  4484. parentNode = parent ?
  4485. parent.element || parent :
  4486. box;
  4487. if (parent) {
  4488. this.parentGroup = parent;
  4489. }
  4490. // if the parent group is inverted, apply inversion on all children
  4491. if (inverted) { // only on groups
  4492. renderer.invertChild(element, parentNode);
  4493. }
  4494. // append it
  4495. parentNode.appendChild(element);
  4496. // align text after adding to be able to read offset
  4497. wrapper.added = true;
  4498. if (wrapper.alignOnAdd && !wrapper.deferUpdateTransform) {
  4499. wrapper.updateTransform();
  4500. }
  4501. // fire an event for internal hooks
  4502. if (wrapper.onAdd) {
  4503. wrapper.onAdd();
  4504. }
  4505. return wrapper;
  4506. },
  4507. /**
  4508. * VML always uses htmlUpdateTransform
  4509. */
  4510. updateTransform: SVGElement.prototype.htmlUpdateTransform,
  4511. /**
  4512. * Set the rotation of a span with oldIE's filter
  4513. */
  4514. setSpanRotation: function () {
  4515. // Adjust for alignment and rotation. Rotation of useHTML content is not yet implemented
  4516. // but it can probably be implemented for Firefox 3.5+ on user request. FF3.5+
  4517. // has support for CSS3 transform. The getBBox method also needs to be updated
  4518. // to compensate for the rotation, like it currently does for SVG.
  4519. // Test case: http://jsfiddle.net/highcharts/Ybt44/
  4520. var rotation = this.rotation,
  4521. costheta = mathCos(rotation * deg2rad),
  4522. sintheta = mathSin(rotation * deg2rad);
  4523. css(this.element, {
  4524. filter: rotation ? ['progid:DXImageTransform.Microsoft.Matrix(M11=', costheta,
  4525. ', M12=', -sintheta, ', M21=', sintheta, ', M22=', costheta,
  4526. ', sizingMethod=\'auto expand\')'].join('') : NONE
  4527. });
  4528. },
  4529. /**
  4530. * Get the positioning correction for the span after rotating.
  4531. */
  4532. getSpanCorrection: function (width, baseline, alignCorrection, rotation, align) {
  4533. var costheta = rotation ? mathCos(rotation * deg2rad) : 1,
  4534. sintheta = rotation ? mathSin(rotation * deg2rad) : 0,
  4535. height = pick(this.elemHeight, this.element.offsetHeight),
  4536. quad,
  4537. nonLeft = align && align !== 'left';
  4538. // correct x and y
  4539. this.xCorr = costheta < 0 && -width;
  4540. this.yCorr = sintheta < 0 && -height;
  4541. // correct for baseline and corners spilling out after rotation
  4542. quad = costheta * sintheta < 0;
  4543. this.xCorr += sintheta * baseline * (quad ? 1 - alignCorrection : alignCorrection);
  4544. this.yCorr -= costheta * baseline * (rotation ? (quad ? alignCorrection : 1 - alignCorrection) : 1);
  4545. // correct for the length/height of the text
  4546. if (nonLeft) {
  4547. this.xCorr -= width * alignCorrection * (costheta < 0 ? -1 : 1);
  4548. if (rotation) {
  4549. this.yCorr -= height * alignCorrection * (sintheta < 0 ? -1 : 1);
  4550. }
  4551. css(this.element, {
  4552. textAlign: align
  4553. });
  4554. }
  4555. },
  4556. /**
  4557. * Converts a subset of an SVG path definition to its VML counterpart. Takes an array
  4558. * as the parameter and returns a string.
  4559. */
  4560. pathToVML: function (value) {
  4561. // convert paths
  4562. var i = value.length,
  4563. path = [];
  4564. while (i--) {
  4565. // Multiply by 10 to allow subpixel precision.
  4566. // Substracting half a pixel seems to make the coordinates
  4567. // align with SVG, but this hasn't been tested thoroughly
  4568. if (isNumber(value[i])) {
  4569. path[i] = mathRound(value[i] * 10) - 5;
  4570. } else if (value[i] === 'Z') { // close the path
  4571. path[i] = 'x';
  4572. } else {
  4573. path[i] = value[i];
  4574. // When the start X and end X coordinates of an arc are too close,
  4575. // they are rounded to the same value above. In this case, substract or
  4576. // add 1 from the end X and Y positions. #186, #760, #1371, #1410.
  4577. if (value.isArc && (value[i] === 'wa' || value[i] === 'at')) {
  4578. // Start and end X
  4579. if (path[i + 5] === path[i + 7]) {
  4580. path[i + 7] += value[i + 7] > value[i + 5] ? 1 : -1;
  4581. }
  4582. // Start and end Y
  4583. if (path[i + 6] === path[i + 8]) {
  4584. path[i + 8] += value[i + 8] > value[i + 6] ? 1 : -1;
  4585. }
  4586. }
  4587. }
  4588. }
  4589. // Loop up again to handle path shortcuts (#2132)
  4590. /*while (i++ < path.length) {
  4591. if (path[i] === 'H') { // horizontal line to
  4592. path[i] = 'L';
  4593. path.splice(i + 2, 0, path[i - 1]);
  4594. } else if (path[i] === 'V') { // vertical line to
  4595. path[i] = 'L';
  4596. path.splice(i + 1, 0, path[i - 2]);
  4597. }
  4598. }*/
  4599. return path.join(' ') || 'x';
  4600. },
  4601. /**
  4602. * Set the element's clipping to a predefined rectangle
  4603. *
  4604. * @param {String} id The id of the clip rectangle
  4605. */
  4606. clip: function (clipRect) {
  4607. var wrapper = this,
  4608. clipMembers,
  4609. cssRet;
  4610. if (clipRect) {
  4611. clipMembers = clipRect.members;
  4612. erase(clipMembers, wrapper); // Ensure unique list of elements (#1258)
  4613. clipMembers.push(wrapper);
  4614. wrapper.destroyClip = function () {
  4615. erase(clipMembers, wrapper);
  4616. };
  4617. cssRet = clipRect.getCSS(wrapper);
  4618. } else {
  4619. if (wrapper.destroyClip) {
  4620. wrapper.destroyClip();
  4621. }
  4622. cssRet = { clip: docMode8 ? 'inherit' : 'rect(auto)' }; // #1214
  4623. }
  4624. return wrapper.css(cssRet);
  4625. },
  4626. /**
  4627. * Set styles for the element
  4628. * @param {Object} styles
  4629. */
  4630. css: SVGElement.prototype.htmlCss,
  4631. /**
  4632. * Removes a child either by removeChild or move to garbageBin.
  4633. * Issue 490; in VML removeChild results in Orphaned nodes according to sIEve, discardElement does not.
  4634. */
  4635. safeRemoveChild: function (element) {
  4636. // discardElement will detach the node from its parent before attaching it
  4637. // to the garbage bin. Therefore it is important that the node is attached and have parent.
  4638. if (element.parentNode) {
  4639. discardElement(element);
  4640. }
  4641. },
  4642. /**
  4643. * Extend element.destroy by removing it from the clip members array
  4644. */
  4645. destroy: function () {
  4646. if (this.destroyClip) {
  4647. this.destroyClip();
  4648. }
  4649. return SVGElement.prototype.destroy.apply(this);
  4650. },
  4651. /**
  4652. * Add an event listener. VML override for normalizing event parameters.
  4653. * @param {String} eventType
  4654. * @param {Function} handler
  4655. */
  4656. on: function (eventType, handler) {
  4657. // simplest possible event model for internal use
  4658. this.element['on' + eventType] = function () {
  4659. var evt = win.event;
  4660. evt.target = evt.srcElement;
  4661. handler(evt);
  4662. };
  4663. return this;
  4664. },
  4665. /**
  4666. * In stacked columns, cut off the shadows so that they don't overlap
  4667. */
  4668. cutOffPath: function (path, length) {
  4669. var len;
  4670. path = path.split(/[ ,]/);
  4671. len = path.length;
  4672. if (len === 9 || len === 11) {
  4673. path[len - 4] = path[len - 2] = pInt(path[len - 2]) - 10 * length;
  4674. }
  4675. return path.join(' ');
  4676. },
  4677. /**
  4678. * Apply a drop shadow by copying elements and giving them different strokes
  4679. * @param {Boolean|Object} shadowOptions
  4680. */
  4681. shadow: function (shadowOptions, group, cutOff) {
  4682. var shadows = [],
  4683. i,
  4684. element = this.element,
  4685. renderer = this.renderer,
  4686. shadow,
  4687. elemStyle = element.style,
  4688. markup,
  4689. path = element.path,
  4690. strokeWidth,
  4691. modifiedPath,
  4692. shadowWidth,
  4693. shadowElementOpacity;
  4694. // some times empty paths are not strings
  4695. if (path && typeof path.value !== 'string') {
  4696. path = 'x';
  4697. }
  4698. modifiedPath = path;
  4699. if (shadowOptions) {
  4700. shadowWidth = pick(shadowOptions.width, 3);
  4701. shadowElementOpacity = (shadowOptions.opacity || 0.15) / shadowWidth;
  4702. for (i = 1; i <= 3; i++) {
  4703. strokeWidth = (shadowWidth * 2) + 1 - (2 * i);
  4704. // Cut off shadows for stacked column items
  4705. if (cutOff) {
  4706. modifiedPath = this.cutOffPath(path.value, strokeWidth + 0.5);
  4707. }
  4708. markup = ['<shape isShadow="true" strokeweight="', strokeWidth,
  4709. '" filled="false" path="', modifiedPath,
  4710. '" coordsize="10 10" style="', element.style.cssText, '" />'];
  4711. shadow = createElement(renderer.prepVML(markup),
  4712. null, {
  4713. left: pInt(elemStyle.left) + pick(shadowOptions.offsetX, 1),
  4714. top: pInt(elemStyle.top) + pick(shadowOptions.offsetY, 1)
  4715. }
  4716. );
  4717. if (cutOff) {
  4718. shadow.cutOff = strokeWidth + 1;
  4719. }
  4720. // apply the opacity
  4721. markup = ['<stroke color="', shadowOptions.color || 'black', '" opacity="', shadowElementOpacity * i, '"/>'];
  4722. createElement(renderer.prepVML(markup), null, null, shadow);
  4723. // insert it
  4724. if (group) {
  4725. group.element.appendChild(shadow);
  4726. } else {
  4727. element.parentNode.insertBefore(shadow, element);
  4728. }
  4729. // record it
  4730. shadows.push(shadow);
  4731. }
  4732. this.shadows = shadows;
  4733. }
  4734. return this;
  4735. },
  4736. updateShadows: noop, // Used in SVG only
  4737. setAttr: function (key, value) {
  4738. if (docMode8) { // IE8 setAttribute bug
  4739. this.element[key] = value;
  4740. } else {
  4741. this.element.setAttribute(key, value);
  4742. }
  4743. },
  4744. classSetter: function (value) {
  4745. // IE8 Standards mode has problems retrieving the className unless set like this
  4746. this.element.className = value;
  4747. },
  4748. dashstyleSetter: function (value, key, element) {
  4749. var strokeElem = element.getElementsByTagName('stroke')[0] ||
  4750. createElement(this.renderer.prepVML(['<stroke/>']), null, null, element);
  4751. strokeElem[key] = value || 'solid';
  4752. this[key] = value; /* because changing stroke-width will change the dash length
  4753. and cause an epileptic effect */
  4754. },
  4755. dSetter: function (value, key, element) {
  4756. var i,
  4757. shadows = this.shadows;
  4758. value = value || [];
  4759. this.d = value.join && value.join(' '); // used in getter for animation
  4760. element.path = value = this.pathToVML(value);
  4761. // update shadows
  4762. if (shadows) {
  4763. i = shadows.length;
  4764. while (i--) {
  4765. shadows[i].path = shadows[i].cutOff ? this.cutOffPath(value, shadows[i].cutOff) : value;
  4766. }
  4767. }
  4768. this.setAttr(key, value);
  4769. },
  4770. fillSetter: function (value, key, element) {
  4771. var nodeName = element.nodeName;
  4772. if (nodeName === 'SPAN') { // text color
  4773. element.style.color = value;
  4774. } else if (nodeName !== 'IMG') { // #1336
  4775. element.filled = value !== NONE;
  4776. this.setAttr('fillcolor', this.renderer.color(value, element, key, this));
  4777. }
  4778. },
  4779. 'fill-opacitySetter': function (value, key, element) {
  4780. createElement(
  4781. this.renderer.prepVML(['<', key.split('-')[0], ' opacity="', value, '"/>']),
  4782. null,
  4783. null,
  4784. element
  4785. );
  4786. },
  4787. opacitySetter: noop, // Don't bother - animation is too slow and filters introduce artifacts
  4788. rotationSetter: function (value, key, element) {
  4789. var style = element.style;
  4790. this[key] = style[key] = value; // style is for #1873
  4791. // Correction for the 1x1 size of the shape container. Used in gauge needles.
  4792. style.left = -mathRound(mathSin(value * deg2rad) + 1) + PX;
  4793. style.top = mathRound(mathCos(value * deg2rad)) + PX;
  4794. },
  4795. strokeSetter: function (value, key, element) {
  4796. this.setAttr('strokecolor', this.renderer.color(value, element, key, this));
  4797. },
  4798. 'stroke-widthSetter': function (value, key, element) {
  4799. element.stroked = !!value; // VML "stroked" attribute
  4800. this[key] = value; // used in getter, issue #113
  4801. if (isNumber(value)) {
  4802. value += PX;
  4803. }
  4804. this.setAttr('strokeweight', value);
  4805. },
  4806. titleSetter: function (value, key) {
  4807. this.setAttr(key, value);
  4808. },
  4809. visibilitySetter: function (value, key, element) {
  4810. // Handle inherited visibility
  4811. if (value === 'inherit') {
  4812. value = VISIBLE;
  4813. }
  4814. // Let the shadow follow the main element
  4815. if (this.shadows) {
  4816. each(this.shadows, function (shadow) {
  4817. shadow.style[key] = value;
  4818. });
  4819. }
  4820. // Instead of toggling the visibility CSS property, move the div out of the viewport.
  4821. // This works around #61 and #586
  4822. if (element.nodeName === 'DIV') {
  4823. value = value === HIDDEN ? '-999em' : 0;
  4824. // In order to redraw, IE7 needs the div to be visible when tucked away
  4825. // outside the viewport. So the visibility is actually opposite of
  4826. // the expected value. This applies to the tooltip only.
  4827. if (!docMode8) {
  4828. element.style[key] = value ? VISIBLE : HIDDEN;
  4829. }
  4830. key = 'top';
  4831. }
  4832. element.style[key] = value;
  4833. },
  4834. xSetter: function (value, key, element) {
  4835. this[key] = value; // used in getter
  4836. if (key === 'x') {
  4837. key = 'left';
  4838. } else if (key === 'y') {
  4839. key = 'top';
  4840. }/* else {
  4841. value = mathMax(0, value); // don't set width or height below zero (#311)
  4842. }*/
  4843. // clipping rectangle special
  4844. if (this.updateClipping) {
  4845. this[key] = value; // the key is now 'left' or 'top' for 'x' and 'y'
  4846. this.updateClipping();
  4847. } else {
  4848. // normal
  4849. element.style[key] = value;
  4850. }
  4851. },
  4852. zIndexSetter: function (value, key, element) {
  4853. element.style[key] = value;
  4854. }
  4855. };
  4856. VMLElement['stroke-opacitySetter'] = VMLElement['fill-opacitySetter'];
  4857. Highcharts.VMLElement = VMLElement = extendClass(SVGElement, VMLElement);
  4858. // Some shared setters
  4859. VMLElement.prototype.ySetter =
  4860. VMLElement.prototype.widthSetter =
  4861. VMLElement.prototype.heightSetter =
  4862. VMLElement.prototype.xSetter;
  4863. /**
  4864. * The VML renderer
  4865. */
  4866. var VMLRendererExtension = { // inherit SVGRenderer
  4867. Element: VMLElement,
  4868. isIE8: userAgent.indexOf('MSIE 8.0') > -1,
  4869. /**
  4870. * Initialize the VMLRenderer
  4871. * @param {Object} container
  4872. * @param {Number} width
  4873. * @param {Number} height
  4874. */
  4875. init: function (container, width, height, style) {
  4876. var renderer = this,
  4877. boxWrapper,
  4878. box,
  4879. css;
  4880. renderer.alignedObjects = [];
  4881. boxWrapper = renderer.createElement(DIV)
  4882. .css(extend(this.getStyle(style), { position: 'relative' }));
  4883. box = boxWrapper.element;
  4884. container.appendChild(boxWrapper.element);
  4885. // generate the containing box
  4886. renderer.isVML = true;
  4887. renderer.box = box;
  4888. renderer.boxWrapper = boxWrapper;
  4889. renderer.gradients = {};
  4890. renderer.cache = {}; // Cache for numerical bounding boxes
  4891. renderer.cacheKeys = [];
  4892. renderer.imgCount = 0;
  4893. renderer.setSize(width, height, false);
  4894. // The only way to make IE6 and IE7 print is to use a global namespace. However,
  4895. // with IE8 the only way to make the dynamic shapes visible in screen and print mode
  4896. // seems to be to add the xmlns attribute and the behaviour style inline.
  4897. if (!doc.namespaces.hcv) {
  4898. doc.namespaces.add('hcv', 'urn:schemas-microsoft-com:vml');
  4899. // Setup default CSS (#2153, #2368, #2384)
  4900. css = 'hcv\\:fill, hcv\\:path, hcv\\:shape, hcv\\:stroke' +
  4901. '{ behavior:url(#default#VML); display: inline-block; } ';
  4902. try {
  4903. doc.createStyleSheet().cssText = css;
  4904. } catch (e) {
  4905. doc.styleSheets[0].cssText += css;
  4906. }
  4907. }
  4908. },
  4909. /**
  4910. * Detect whether the renderer is hidden. This happens when one of the parent elements
  4911. * has display: none
  4912. */
  4913. isHidden: function () {
  4914. return !this.box.offsetWidth;
  4915. },
  4916. /**
  4917. * Define a clipping rectangle. In VML it is accomplished by storing the values
  4918. * for setting the CSS style to all associated members.
  4919. *
  4920. * @param {Number} x
  4921. * @param {Number} y
  4922. * @param {Number} width
  4923. * @param {Number} height
  4924. */
  4925. clipRect: function (x, y, width, height) {
  4926. // create a dummy element
  4927. var clipRect = this.createElement(),
  4928. isObj = isObject(x);
  4929. // mimic a rectangle with its style object for automatic updating in attr
  4930. return extend(clipRect, {
  4931. members: [],
  4932. count: 0,
  4933. left: (isObj ? x.x : x) + 1,
  4934. top: (isObj ? x.y : y) + 1,
  4935. width: (isObj ? x.width : width) - 1,
  4936. height: (isObj ? x.height : height) - 1,
  4937. getCSS: function (wrapper) {
  4938. var element = wrapper.element,
  4939. nodeName = element.nodeName,
  4940. isShape = nodeName === 'shape',
  4941. inverted = wrapper.inverted,
  4942. rect = this,
  4943. top = rect.top - (isShape ? element.offsetTop : 0),
  4944. left = rect.left,
  4945. right = left + rect.width,
  4946. bottom = top + rect.height,
  4947. ret = {
  4948. clip: 'rect(' +
  4949. mathRound(inverted ? left : top) + 'px,' +
  4950. mathRound(inverted ? bottom : right) + 'px,' +
  4951. mathRound(inverted ? right : bottom) + 'px,' +
  4952. mathRound(inverted ? top : left) + 'px)'
  4953. };
  4954. // issue 74 workaround
  4955. if (!inverted && docMode8 && nodeName === 'DIV') {
  4956. extend(ret, {
  4957. width: right + PX,
  4958. height: bottom + PX
  4959. });
  4960. }
  4961. return ret;
  4962. },
  4963. // used in attr and animation to update the clipping of all members
  4964. updateClipping: function () {
  4965. each(clipRect.members, function (member) {
  4966. if (member.element) { // Deleted series, like in stock/members/series-remove demo. Should be removed from members, but this will do.
  4967. member.css(clipRect.getCSS(member));
  4968. }
  4969. });
  4970. }
  4971. });
  4972. },
  4973. /**
  4974. * Take a color and return it if it's a string, make it a gradient if it's a
  4975. * gradient configuration object, and apply opacity.
  4976. *
  4977. * @param {Object} color The color or config object
  4978. */
  4979. color: function (color, elem, prop, wrapper) {
  4980. var renderer = this,
  4981. colorObject,
  4982. regexRgba = /^rgba/,
  4983. markup,
  4984. fillType,
  4985. ret = NONE;
  4986. // Check for linear or radial gradient
  4987. if (color && color.linearGradient) {
  4988. fillType = 'gradient';
  4989. } else if (color && color.radialGradient) {
  4990. fillType = 'pattern';
  4991. }
  4992. if (fillType) {
  4993. var stopColor,
  4994. stopOpacity,
  4995. gradient = color.linearGradient || color.radialGradient,
  4996. x1,
  4997. y1,
  4998. x2,
  4999. y2,
  5000. opacity1,
  5001. opacity2,
  5002. color1,
  5003. color2,
  5004. fillAttr = '',
  5005. stops = color.stops,
  5006. firstStop,
  5007. lastStop,
  5008. colors = [],
  5009. addFillNode = function () {
  5010. // Add the fill subnode. When colors attribute is used, the meanings of opacity and o:opacity2
  5011. // are reversed.
  5012. markup = ['<fill colors="' + colors.join(',') + '" opacity="', opacity2, '" o:opacity2="', opacity1,
  5013. '" type="', fillType, '" ', fillAttr, 'focus="100%" method="any" />'];
  5014. createElement(renderer.prepVML(markup), null, null, elem);
  5015. };
  5016. // Extend from 0 to 1
  5017. firstStop = stops[0];
  5018. lastStop = stops[stops.length - 1];
  5019. if (firstStop[0] > 0) {
  5020. stops.unshift([
  5021. 0,
  5022. firstStop[1]
  5023. ]);
  5024. }
  5025. if (lastStop[0] < 1) {
  5026. stops.push([
  5027. 1,
  5028. lastStop[1]
  5029. ]);
  5030. }
  5031. // Compute the stops
  5032. each(stops, function (stop, i) {
  5033. if (regexRgba.test(stop[1])) {
  5034. colorObject = Color(stop[1]);
  5035. stopColor = colorObject.get('rgb');
  5036. stopOpacity = colorObject.get('a');
  5037. } else {
  5038. stopColor = stop[1];
  5039. stopOpacity = 1;
  5040. }
  5041. // Build the color attribute
  5042. colors.push((stop[0] * 100) + '% ' + stopColor);
  5043. // Only start and end opacities are allowed, so we use the first and the last
  5044. if (!i) {
  5045. opacity1 = stopOpacity;
  5046. color2 = stopColor;
  5047. } else {
  5048. opacity2 = stopOpacity;
  5049. color1 = stopColor;
  5050. }
  5051. });
  5052. // Apply the gradient to fills only.
  5053. if (prop === 'fill') {
  5054. // Handle linear gradient angle
  5055. if (fillType === 'gradient') {
  5056. x1 = gradient.x1 || gradient[0] || 0;
  5057. y1 = gradient.y1 || gradient[1] || 0;
  5058. x2 = gradient.x2 || gradient[2] || 0;
  5059. y2 = gradient.y2 || gradient[3] || 0;
  5060. fillAttr = 'angle="' + (90 - math.atan(
  5061. (y2 - y1) / // y vector
  5062. (x2 - x1) // x vector
  5063. ) * 180 / mathPI) + '"';
  5064. addFillNode();
  5065. // Radial (circular) gradient
  5066. } else {
  5067. var r = gradient.r,
  5068. sizex = r * 2,
  5069. sizey = r * 2,
  5070. cx = gradient.cx,
  5071. cy = gradient.cy,
  5072. radialReference = elem.radialReference,
  5073. bBox,
  5074. applyRadialGradient = function () {
  5075. if (radialReference) {
  5076. bBox = wrapper.getBBox();
  5077. cx += (radialReference[0] - bBox.x) / bBox.width - 0.5;
  5078. cy += (radialReference[1] - bBox.y) / bBox.height - 0.5;
  5079. sizex *= radialReference[2] / bBox.width;
  5080. sizey *= radialReference[2] / bBox.height;
  5081. }
  5082. fillAttr = 'src="' + defaultOptions.global.VMLRadialGradientURL + '" ' +
  5083. 'size="' + sizex + ',' + sizey + '" ' +
  5084. 'origin="0.5,0.5" ' +
  5085. 'position="' + cx + ',' + cy + '" ' +
  5086. 'color2="' + color2 + '" ';
  5087. addFillNode();
  5088. };
  5089. // Apply radial gradient
  5090. if (wrapper.added) {
  5091. applyRadialGradient();
  5092. } else {
  5093. // We need to know the bounding box to get the size and position right
  5094. wrapper.onAdd = applyRadialGradient;
  5095. }
  5096. // The fill element's color attribute is broken in IE8 standards mode, so we
  5097. // need to set the parent shape's fillcolor attribute instead.
  5098. ret = color1;
  5099. }
  5100. // Gradients are not supported for VML stroke, return the first color. #722.
  5101. } else {
  5102. ret = stopColor;
  5103. }
  5104. // If the color is an rgba color, split it and add a fill node
  5105. // to hold the opacity component
  5106. } else if (regexRgba.test(color) && elem.tagName !== 'IMG') {
  5107. colorObject = Color(color);
  5108. wrapper[prop + '-opacitySetter'](colorObject.get('a'), prop, elem);
  5109. ret = colorObject.get('rgb');
  5110. } else {
  5111. var propNodes = elem.getElementsByTagName(prop); // 'stroke' or 'fill' node
  5112. if (propNodes.length) {
  5113. propNodes[0].opacity = 1;
  5114. propNodes[0].type = 'solid';
  5115. }
  5116. ret = color;
  5117. }
  5118. return ret;
  5119. },
  5120. /**
  5121. * Take a VML string and prepare it for either IE8 or IE6/IE7.
  5122. * @param {Array} markup A string array of the VML markup to prepare
  5123. */
  5124. prepVML: function (markup) {
  5125. var vmlStyle = 'display:inline-block;behavior:url(#default#VML);',
  5126. isIE8 = this.isIE8;
  5127. markup = markup.join('');
  5128. if (isIE8) { // add xmlns and style inline
  5129. markup = markup.replace('/>', ' xmlns="urn:schemas-microsoft-com:vml" />');
  5130. if (markup.indexOf('style="') === -1) {
  5131. markup = markup.replace('/>', ' style="' + vmlStyle + '" />');
  5132. } else {
  5133. markup = markup.replace('style="', 'style="' + vmlStyle);
  5134. }
  5135. } else { // add namespace
  5136. markup = markup.replace('<', '<hcv:');
  5137. }
  5138. return markup;
  5139. },
  5140. /**
  5141. * Create rotated and aligned text
  5142. * @param {String} str
  5143. * @param {Number} x
  5144. * @param {Number} y
  5145. */
  5146. text: SVGRenderer.prototype.html,
  5147. /**
  5148. * Create and return a path element
  5149. * @param {Array} path
  5150. */
  5151. path: function (path) {
  5152. var attr = {
  5153. // subpixel precision down to 0.1 (width and height = 1px)
  5154. coordsize: '10 10'
  5155. };
  5156. if (isArray(path)) {
  5157. attr.d = path;
  5158. } else if (isObject(path)) { // attributes
  5159. extend(attr, path);
  5160. }
  5161. // create the shape
  5162. return this.createElement('shape').attr(attr);
  5163. },
  5164. /**
  5165. * Create and return a circle element. In VML circles are implemented as
  5166. * shapes, which is faster than v:oval
  5167. * @param {Number} x
  5168. * @param {Number} y
  5169. * @param {Number} r
  5170. */
  5171. circle: function (x, y, r) {
  5172. var circle = this.symbol('circle');
  5173. if (isObject(x)) {
  5174. r = x.r;
  5175. y = x.y;
  5176. x = x.x;
  5177. }
  5178. circle.isCircle = true; // Causes x and y to mean center (#1682)
  5179. circle.r = r;
  5180. return circle.attr({ x: x, y: y });
  5181. },
  5182. /**
  5183. * Create a group using an outer div and an inner v:group to allow rotating
  5184. * and flipping. A simple v:group would have problems with positioning
  5185. * child HTML elements and CSS clip.
  5186. *
  5187. * @param {String} name The name of the group
  5188. */
  5189. g: function (name) {
  5190. var wrapper,
  5191. attribs;
  5192. // set the class name
  5193. if (name) {
  5194. attribs = { 'className': PREFIX + name, 'class': PREFIX + name };
  5195. }
  5196. // the div to hold HTML and clipping
  5197. wrapper = this.createElement(DIV).attr(attribs);
  5198. return wrapper;
  5199. },
  5200. /**
  5201. * VML override to create a regular HTML image
  5202. * @param {String} src
  5203. * @param {Number} x
  5204. * @param {Number} y
  5205. * @param {Number} width
  5206. * @param {Number} height
  5207. */
  5208. image: function (src, x, y, width, height) {
  5209. var obj = this.createElement('img')
  5210. .attr({ src: src });
  5211. if (arguments.length > 1) {
  5212. obj.attr({
  5213. x: x,
  5214. y: y,
  5215. width: width,
  5216. height: height
  5217. });
  5218. }
  5219. return obj;
  5220. },
  5221. /**
  5222. * For rectangles, VML uses a shape for rect to overcome bugs and rotation problems
  5223. */
  5224. createElement: function (nodeName) {
  5225. return nodeName === 'rect' ? this.symbol(nodeName) : SVGRenderer.prototype.createElement.call(this, nodeName);
  5226. },
  5227. /**
  5228. * In the VML renderer, each child of an inverted div (group) is inverted
  5229. * @param {Object} element
  5230. * @param {Object} parentNode
  5231. */
  5232. invertChild: function (element, parentNode) {
  5233. var ren = this,
  5234. parentStyle = parentNode.style,
  5235. imgStyle = element.tagName === 'IMG' && element.style; // #1111
  5236. css(element, {
  5237. flip: 'x',
  5238. left: pInt(parentStyle.width) - (imgStyle ? pInt(imgStyle.top) : 1),
  5239. top: pInt(parentStyle.height) - (imgStyle ? pInt(imgStyle.left) : 1),
  5240. rotation: -90
  5241. });
  5242. // Recursively invert child elements, needed for nested composite shapes like box plots and error bars. #1680, #1806.
  5243. each(element.childNodes, function (child) {
  5244. ren.invertChild(child, element);
  5245. });
  5246. },
  5247. /**
  5248. * Symbol definitions that override the parent SVG renderer's symbols
  5249. *
  5250. */
  5251. symbols: {
  5252. // VML specific arc function
  5253. arc: function (x, y, w, h, options) {
  5254. var start = options.start,
  5255. end = options.end,
  5256. radius = options.r || w || h,
  5257. innerRadius = options.innerR,
  5258. cosStart = mathCos(start),
  5259. sinStart = mathSin(start),
  5260. cosEnd = mathCos(end),
  5261. sinEnd = mathSin(end),
  5262. ret;
  5263. if (end - start === 0) { // no angle, don't show it.
  5264. return ['x'];
  5265. }
  5266. ret = [
  5267. 'wa', // clockwise arc to
  5268. x - radius, // left
  5269. y - radius, // top
  5270. x + radius, // right
  5271. y + radius, // bottom
  5272. x + radius * cosStart, // start x
  5273. y + radius * sinStart, // start y
  5274. x + radius * cosEnd, // end x
  5275. y + radius * sinEnd // end y
  5276. ];
  5277. if (options.open && !innerRadius) {
  5278. ret.push(
  5279. 'e',
  5280. M,
  5281. x, // - innerRadius,
  5282. y// - innerRadius
  5283. );
  5284. }
  5285. ret.push(
  5286. 'at', // anti clockwise arc to
  5287. x - innerRadius, // left
  5288. y - innerRadius, // top
  5289. x + innerRadius, // right
  5290. y + innerRadius, // bottom
  5291. x + innerRadius * cosEnd, // start x
  5292. y + innerRadius * sinEnd, // start y
  5293. x + innerRadius * cosStart, // end x
  5294. y + innerRadius * sinStart, // end y
  5295. 'x', // finish path
  5296. 'e' // close
  5297. );
  5298. ret.isArc = true;
  5299. return ret;
  5300. },
  5301. // Add circle symbol path. This performs significantly faster than v:oval.
  5302. circle: function (x, y, w, h, wrapper) {
  5303. if (wrapper) {
  5304. w = h = 2 * wrapper.r;
  5305. }
  5306. // Center correction, #1682
  5307. if (wrapper && wrapper.isCircle) {
  5308. x -= w / 2;
  5309. y -= h / 2;
  5310. }
  5311. // Return the path
  5312. return [
  5313. 'wa', // clockwisearcto
  5314. x, // left
  5315. y, // top
  5316. x + w, // right
  5317. y + h, // bottom
  5318. x + w, // start x
  5319. y + h / 2, // start y
  5320. x + w, // end x
  5321. y + h / 2, // end y
  5322. //'x', // finish path
  5323. 'e' // close
  5324. ];
  5325. },
  5326. /**
  5327. * Add rectangle symbol path which eases rotation and omits arcsize problems
  5328. * compared to the built-in VML roundrect shape. When borders are not rounded,
  5329. * use the simpler square path, else use the callout path without the arrow.
  5330. */
  5331. rect: function (x, y, w, h, options) {
  5332. return SVGRenderer.prototype.symbols[
  5333. !defined(options) || !options.r ? 'square' : 'callout'
  5334. ].call(0, x, y, w, h, options);
  5335. }
  5336. }
  5337. };
  5338. Highcharts.VMLRenderer = VMLRenderer = function () {
  5339. this.init.apply(this, arguments);
  5340. };
  5341. VMLRenderer.prototype = merge(SVGRenderer.prototype, VMLRendererExtension);
  5342. // general renderer
  5343. Renderer = VMLRenderer;
  5344. }
  5345. // This method is used with exporting in old IE, when emulating SVG (see #2314)
  5346. SVGRenderer.prototype.measureSpanWidth = function (text, styles) {
  5347. var measuringSpan = doc.createElement('span'),
  5348. offsetWidth,
  5349. textNode = doc.createTextNode(text);
  5350. measuringSpan.appendChild(textNode);
  5351. css(measuringSpan, styles);
  5352. this.box.appendChild(measuringSpan);
  5353. offsetWidth = measuringSpan.offsetWidth;
  5354. discardElement(measuringSpan); // #2463
  5355. return offsetWidth;
  5356. };
  5357. /* ****************************************************************************
  5358. * *
  5359. * END OF INTERNET EXPLORER <= 8 SPECIFIC CODE *
  5360. * *
  5361. *****************************************************************************/
  5362. /* ****************************************************************************
  5363. * *
  5364. * START OF ANDROID < 3 SPECIFIC CODE. THIS CAN BE REMOVED IF YOU'RE NOT *
  5365. * TARGETING THAT SYSTEM. *
  5366. * *
  5367. *****************************************************************************/
  5368. var CanVGRenderer,
  5369. CanVGController;
  5370. /**
  5371. * Downloads a script and executes a callback when done.
  5372. * @param {String} scriptLocation
  5373. * @param {Function} callback
  5374. */
  5375. function getScript(scriptLocation, callback) {
  5376. var head = doc.getElementsByTagName('head')[0],
  5377. script = doc.createElement('script');
  5378. script.type = 'text/javascript';
  5379. script.src = scriptLocation;
  5380. script.onload = callback;
  5381. head.appendChild(script);
  5382. }
  5383. if (useCanVG) {
  5384. /**
  5385. * The CanVGRenderer is empty from start to keep the source footprint small.
  5386. * When requested, the CanVGController downloads the rest of the source packaged
  5387. * together with the canvg library.
  5388. */
  5389. Highcharts.CanVGRenderer = CanVGRenderer = function () {
  5390. // Override the global SVG namespace to fake SVG/HTML that accepts CSS
  5391. SVG_NS = 'http://www.w3.org/1999/xhtml';
  5392. };
  5393. /**
  5394. * Start with an empty symbols object. This is needed when exporting is used (exporting.src.js will add a few symbols), but
  5395. * the implementation from SvgRenderer will not be merged in until first render.
  5396. */
  5397. CanVGRenderer.prototype.symbols = {};
  5398. /**
  5399. * Handles on demand download of canvg rendering support.
  5400. */
  5401. CanVGController = (function () {
  5402. // List of renderering calls
  5403. var deferredRenderCalls = [];
  5404. /**
  5405. * When downloaded, we are ready to draw deferred charts.
  5406. */
  5407. function drawDeferred() {
  5408. var callLength = deferredRenderCalls.length,
  5409. callIndex;
  5410. // Draw all pending render calls
  5411. for (callIndex = 0; callIndex < callLength; callIndex++) {
  5412. deferredRenderCalls[callIndex]();
  5413. }
  5414. // Clear the list
  5415. deferredRenderCalls = [];
  5416. }
  5417. return {
  5418. push: function (func, scriptLocation) {
  5419. // Only get the script once
  5420. if (deferredRenderCalls.length === 0) {
  5421. getScript(scriptLocation, drawDeferred);
  5422. }
  5423. // Register render call
  5424. deferredRenderCalls.push(func);
  5425. }
  5426. };
  5427. }());
  5428. Renderer = CanVGRenderer;
  5429. } // end CanVGRenderer
  5430. /* ****************************************************************************
  5431. * *
  5432. * END OF ANDROID < 3 SPECIFIC CODE *
  5433. * *
  5434. *****************************************************************************/
  5435. /**
  5436. * The Tick class
  5437. */
  5438. function Tick(axis, pos, type, noLabel) {
  5439. this.axis = axis;
  5440. this.pos = pos;
  5441. this.type = type || '';
  5442. this.isNew = true;
  5443. if (!type && !noLabel) {
  5444. this.addLabel();
  5445. }
  5446. }
  5447. Tick.prototype = {
  5448. /**
  5449. * Write the tick label
  5450. */
  5451. addLabel: function () {
  5452. var tick = this,
  5453. axis = tick.axis,
  5454. options = axis.options,
  5455. chart = axis.chart,
  5456. categories = axis.categories,
  5457. names = axis.names,
  5458. pos = tick.pos,
  5459. labelOptions = options.labels,
  5460. str,
  5461. tickPositions = axis.tickPositions,
  5462. isFirst = pos === tickPositions[0],
  5463. isLast = pos === tickPositions[tickPositions.length - 1],
  5464. value = categories ?
  5465. pick(categories[pos], names[pos], pos) :
  5466. pos,
  5467. label = tick.label,
  5468. tickPositionInfo = tickPositions.info,
  5469. dateTimeLabelFormat;
  5470. // Set the datetime label format. If a higher rank is set for this position, use that. If not,
  5471. // use the general format.
  5472. if (axis.isDatetimeAxis && tickPositionInfo) {
  5473. dateTimeLabelFormat = options.dateTimeLabelFormats[tickPositionInfo.higherRanks[pos] || tickPositionInfo.unitName];
  5474. }
  5475. // set properties for access in render method
  5476. tick.isFirst = isFirst;
  5477. tick.isLast = isLast;
  5478. // get the string
  5479. str = axis.labelFormatter.call({
  5480. axis: axis,
  5481. chart: chart,
  5482. isFirst: isFirst,
  5483. isLast: isLast,
  5484. dateTimeLabelFormat: dateTimeLabelFormat,
  5485. value: axis.isLog ? correctFloat(axis.lin2log(value)) : value
  5486. });
  5487. // prepare CSS
  5488. //css = width && { width: mathMax(1, mathRound(width - 2 * (labelOptions.padding || 10))) + PX };
  5489. // first call
  5490. if (!defined(label)) {
  5491. tick.label = label =
  5492. defined(str) && labelOptions.enabled ?
  5493. chart.renderer.text(
  5494. str,
  5495. 0,
  5496. 0,
  5497. labelOptions.useHTML
  5498. )
  5499. //.attr(attr)
  5500. // without position absolute, IE export sometimes is wrong
  5501. .css(merge(labelOptions.style))
  5502. .add(axis.labelGroup) :
  5503. null;
  5504. tick.labelLength = label && label.getBBox().width; // Un-rotated length
  5505. tick.rotation = 0; // Base value to detect change for new calls to getBBox
  5506. // update
  5507. } else if (label) {
  5508. label.attr({ text: str });
  5509. }
  5510. },
  5511. /**
  5512. * Get the offset height or width of the label
  5513. */
  5514. getLabelSize: function () {
  5515. return this.label ?
  5516. this.label.getBBox()[this.axis.horiz ? 'height' : 'width'] :
  5517. 0;
  5518. },
  5519. /**
  5520. * Handle the label overflow by adjusting the labels to the left and right edge, or
  5521. * hide them if they collide into the neighbour label.
  5522. */
  5523. handleOverflow: function (xy) {
  5524. var axis = this.axis,
  5525. pxPos = xy.x,
  5526. chartWidth = axis.chart.chartWidth,
  5527. spacing = axis.chart.spacing,
  5528. leftBound = pick(axis.labelLeft, mathMin(axis.pos, spacing[3])),
  5529. rightBound = pick(axis.labelRight, mathMax(axis.pos + axis.len, chartWidth - spacing[1])),
  5530. label = this.label,
  5531. rotation = this.rotation,
  5532. factor = { left: 0, center: 0.5, right: 1 }[axis.labelAlign],
  5533. labelWidth = label.getBBox().width,
  5534. slotWidth = axis.getSlotWidth(),
  5535. modifiedSlotWidth = slotWidth,
  5536. xCorrection = factor,
  5537. goRight = 1,
  5538. leftPos,
  5539. rightPos,
  5540. textWidth,
  5541. css = {};
  5542. // Check if the label overshoots the chart spacing box. If it does, move it.
  5543. // If it now overshoots the slotWidth, add ellipsis.
  5544. if (!rotation) {
  5545. leftPos = pxPos - factor * labelWidth;
  5546. rightPos = pxPos + (1 - factor) * labelWidth;
  5547. if (leftPos < leftBound) {
  5548. modifiedSlotWidth = xy.x + modifiedSlotWidth * (1 - factor) - leftBound;
  5549. } else if (rightPos > rightBound) {
  5550. modifiedSlotWidth = rightBound - xy.x + modifiedSlotWidth * factor;
  5551. goRight = -1;
  5552. }
  5553. modifiedSlotWidth = mathMin(slotWidth, modifiedSlotWidth); // #4177
  5554. if (modifiedSlotWidth < slotWidth && axis.labelAlign === 'center') {
  5555. xy.x += goRight * (slotWidth - modifiedSlotWidth - xCorrection * (slotWidth - mathMin(labelWidth, modifiedSlotWidth)));
  5556. }
  5557. // If the label width exceeds the available space, set a text width to be
  5558. // picked up below. Also, if a width has been set before, we need to set a new
  5559. // one because the reported labelWidth will be limited by the box (#3938).
  5560. if (labelWidth > modifiedSlotWidth || (axis.autoRotation && label.styles.width)) {
  5561. textWidth = modifiedSlotWidth;
  5562. }
  5563. // Add ellipsis to prevent rotated labels to be clipped against the edge of the chart
  5564. } else if (rotation < 0 && pxPos - factor * labelWidth < leftBound) {
  5565. textWidth = mathRound(pxPos / mathCos(rotation * deg2rad) - leftBound);
  5566. } else if (rotation > 0 && pxPos + factor * labelWidth > rightBound) {
  5567. textWidth = mathRound((chartWidth - pxPos) / mathCos(rotation * deg2rad));
  5568. }
  5569. if (textWidth) {
  5570. css.width = textWidth;
  5571. if (!axis.options.labels.style.textOverflow) {
  5572. css.textOverflow = 'ellipsis';
  5573. }
  5574. label.css(css);
  5575. }
  5576. },
  5577. /**
  5578. * Get the x and y position for ticks and labels
  5579. */
  5580. getPosition: function (horiz, pos, tickmarkOffset, old) {
  5581. var axis = this.axis,
  5582. chart = axis.chart,
  5583. cHeight = (old && chart.oldChartHeight) || chart.chartHeight;
  5584. return {
  5585. x: horiz ?
  5586. axis.translate(pos + tickmarkOffset, null, null, old) + axis.transB :
  5587. axis.left + axis.offset + (axis.opposite ? ((old && chart.oldChartWidth) || chart.chartWidth) - axis.right - axis.left : 0),
  5588. y: horiz ?
  5589. cHeight - axis.bottom + axis.offset - (axis.opposite ? axis.height : 0) :
  5590. cHeight - axis.translate(pos + tickmarkOffset, null, null, old) - axis.transB
  5591. };
  5592. },
  5593. /**
  5594. * Get the x, y position of the tick label
  5595. */
  5596. getLabelPosition: function (x, y, label, horiz, labelOptions, tickmarkOffset, index, step) {
  5597. var axis = this.axis,
  5598. transA = axis.transA,
  5599. reversed = axis.reversed,
  5600. staggerLines = axis.staggerLines,
  5601. rotCorr = axis.tickRotCorr || { x: 0, y: 0 },
  5602. yOffset = labelOptions.y,
  5603. line;
  5604. if (!defined(yOffset)) {
  5605. if (axis.side === 0) {
  5606. yOffset = label.rotation ? -8 : -label.getBBox().height;
  5607. } else if (axis.side === 2) {
  5608. yOffset = rotCorr.y + 8;
  5609. } else {
  5610. // #3140, #3140
  5611. yOffset = mathCos(label.rotation * deg2rad) * (rotCorr.y - label.getBBox(false, 0).height / 2);
  5612. }
  5613. }
  5614. x = x + labelOptions.x + rotCorr.x - (tickmarkOffset && horiz ?
  5615. tickmarkOffset * transA * (reversed ? -1 : 1) : 0);
  5616. y = y + yOffset - (tickmarkOffset && !horiz ?
  5617. tickmarkOffset * transA * (reversed ? 1 : -1) : 0);
  5618. // Correct for staggered labels
  5619. if (staggerLines) {
  5620. line = (index / (step || 1) % staggerLines);
  5621. if (axis.opposite) {
  5622. line = staggerLines - line - 1;
  5623. }
  5624. y += line * (axis.labelOffset / staggerLines);
  5625. }
  5626. return {
  5627. x: x,
  5628. y: mathRound(y)
  5629. };
  5630. },
  5631. /**
  5632. * Extendible method to return the path of the marker
  5633. */
  5634. getMarkPath: function (x, y, tickLength, tickWidth, horiz, renderer) {
  5635. return renderer.crispLine([
  5636. M,
  5637. x,
  5638. y,
  5639. L,
  5640. x + (horiz ? 0 : -tickLength),
  5641. y + (horiz ? tickLength : 0)
  5642. ], tickWidth);
  5643. },
  5644. /**
  5645. * Put everything in place
  5646. *
  5647. * @param index {Number}
  5648. * @param old {Boolean} Use old coordinates to prepare an animation into new position
  5649. */
  5650. render: function (index, old, opacity) {
  5651. var tick = this,
  5652. axis = tick.axis,
  5653. options = axis.options,
  5654. chart = axis.chart,
  5655. renderer = chart.renderer,
  5656. horiz = axis.horiz,
  5657. type = tick.type,
  5658. label = tick.label,
  5659. pos = tick.pos,
  5660. labelOptions = options.labels,
  5661. gridLine = tick.gridLine,
  5662. gridPrefix = type ? type + 'Grid' : 'grid',
  5663. tickPrefix = type ? type + 'Tick' : 'tick',
  5664. gridLineWidth = options[gridPrefix + 'LineWidth'],
  5665. gridLineColor = options[gridPrefix + 'LineColor'],
  5666. dashStyle = options[gridPrefix + 'LineDashStyle'],
  5667. tickSize = axis.tickSize(tickPrefix),
  5668. tickColor = options[tickPrefix + 'Color'],
  5669. gridLinePath,
  5670. mark = tick.mark,
  5671. markPath,
  5672. step = /*axis.labelStep || */labelOptions.step,
  5673. attribs,
  5674. show = true,
  5675. tickmarkOffset = axis.tickmarkOffset,
  5676. xy = tick.getPosition(horiz, pos, tickmarkOffset, old),
  5677. x = xy.x,
  5678. y = xy.y,
  5679. reverseCrisp = ((horiz && x === axis.pos + axis.len) || (!horiz && y === axis.pos)) ? -1 : 1; // #1480, #1687
  5680. opacity = pick(opacity, 1);
  5681. this.isActive = true;
  5682. // create the grid line
  5683. if (gridLineWidth) {
  5684. gridLinePath = axis.getPlotLinePath(pos + tickmarkOffset, gridLineWidth * reverseCrisp, old, true);
  5685. if (gridLine === UNDEFINED) {
  5686. attribs = {
  5687. stroke: gridLineColor,
  5688. 'stroke-width': gridLineWidth
  5689. };
  5690. if (dashStyle) {
  5691. attribs.dashstyle = dashStyle;
  5692. }
  5693. if (!type) {
  5694. attribs.zIndex = 1;
  5695. }
  5696. if (old) {
  5697. attribs.opacity = 0;
  5698. }
  5699. tick.gridLine = gridLine =
  5700. gridLineWidth ?
  5701. renderer.path(gridLinePath)
  5702. .attr(attribs).add(axis.gridGroup) :
  5703. null;
  5704. }
  5705. // If the parameter 'old' is set, the current call will be followed
  5706. // by another call, therefore do not do any animations this time
  5707. if (!old && gridLine && gridLinePath) {
  5708. gridLine[tick.isNew ? 'attr' : 'animate']({
  5709. d: gridLinePath,
  5710. opacity: opacity
  5711. });
  5712. }
  5713. }
  5714. // create the tick mark
  5715. if (tickSize) {
  5716. if (axis.opposite) {
  5717. tickSize[0] = -tickSize[0];
  5718. }
  5719. markPath = tick.getMarkPath(x, y, tickSize[0], tickSize[1] * reverseCrisp, horiz, renderer);
  5720. if (mark) { // updating
  5721. mark.animate({
  5722. d: markPath,
  5723. opacity: opacity
  5724. });
  5725. } else { // first time
  5726. tick.mark = renderer.path(
  5727. markPath
  5728. ).attr({
  5729. stroke: tickColor,
  5730. 'stroke-width': tickSize[1],
  5731. opacity: opacity
  5732. }).add(axis.axisGroup);
  5733. }
  5734. }
  5735. // the label is created on init - now move it into place
  5736. if (label && isNumber(x)) {
  5737. label.xy = xy = tick.getLabelPosition(x, y, label, horiz, labelOptions, tickmarkOffset, index, step);
  5738. // Apply show first and show last. If the tick is both first and last, it is
  5739. // a single centered tick, in which case we show the label anyway (#2100).
  5740. if ((tick.isFirst && !tick.isLast && !pick(options.showFirstLabel, 1)) ||
  5741. (tick.isLast && !tick.isFirst && !pick(options.showLastLabel, 1))) {
  5742. show = false;
  5743. // Handle label overflow and show or hide accordingly
  5744. } else if (horiz && !axis.isRadial && !labelOptions.step && !labelOptions.rotation && !old && opacity !== 0) {
  5745. tick.handleOverflow(xy);
  5746. }
  5747. // apply step
  5748. if (step && index % step) {
  5749. // show those indices dividable by step
  5750. show = false;
  5751. }
  5752. // Set the new position, and show or hide
  5753. if (show && isNumber(xy.y)) {
  5754. xy.opacity = opacity;
  5755. label[tick.isNew ? 'attr' : 'animate'](xy);
  5756. tick.isNew = false;
  5757. } else {
  5758. label.attr('y', -9999); // #1338
  5759. }
  5760. }
  5761. },
  5762. /**
  5763. * Destructor for the tick prototype
  5764. */
  5765. destroy: function () {
  5766. destroyObjectProperties(this, this.axis);
  5767. }
  5768. };
  5769. /**
  5770. * The object wrapper for plot lines and plot bands
  5771. * @param {Object} options
  5772. */
  5773. Highcharts.PlotLineOrBand = function (axis, options) {
  5774. this.axis = axis;
  5775. if (options) {
  5776. this.options = options;
  5777. this.id = options.id;
  5778. }
  5779. };
  5780. Highcharts.PlotLineOrBand.prototype = {
  5781. /**
  5782. * Render the plot line or plot band. If it is already existing,
  5783. * move it.
  5784. */
  5785. render: function () {
  5786. var plotLine = this,
  5787. axis = plotLine.axis,
  5788. horiz = axis.horiz,
  5789. options = plotLine.options,
  5790. optionsLabel = options.label,
  5791. label = plotLine.label,
  5792. width = options.width,
  5793. to = options.to,
  5794. from = options.from,
  5795. isBand = defined(from) && defined(to),
  5796. value = options.value,
  5797. dashStyle = options.dashStyle,
  5798. svgElem = plotLine.svgElem,
  5799. path = [],
  5800. addEvent,
  5801. eventType,
  5802. color = options.color,
  5803. zIndex = pick(options.zIndex, 0),
  5804. events = options.events,
  5805. attribs = {},
  5806. renderer = axis.chart.renderer,
  5807. log2lin = axis.log2lin;
  5808. // logarithmic conversion
  5809. if (axis.isLog) {
  5810. from = log2lin(from);
  5811. to = log2lin(to);
  5812. value = log2lin(value);
  5813. }
  5814. // plot line
  5815. if (width) {
  5816. path = axis.getPlotLinePath(value, width);
  5817. attribs = {
  5818. stroke: color,
  5819. 'stroke-width': width
  5820. };
  5821. if (dashStyle) {
  5822. attribs.dashstyle = dashStyle;
  5823. }
  5824. } else if (isBand) { // plot band
  5825. path = axis.getPlotBandPath(from, to, options);
  5826. if (color) {
  5827. attribs.fill = color;
  5828. }
  5829. if (options.borderWidth) {
  5830. attribs.stroke = options.borderColor;
  5831. attribs['stroke-width'] = options.borderWidth;
  5832. }
  5833. } else {
  5834. return;
  5835. }
  5836. // zIndex
  5837. attribs.zIndex = zIndex;
  5838. // common for lines and bands
  5839. if (svgElem) {
  5840. if (path) {
  5841. svgElem.show();
  5842. svgElem.animate({ d: path });
  5843. } else {
  5844. svgElem.hide();
  5845. if (label) {
  5846. plotLine.label = label = label.destroy();
  5847. }
  5848. }
  5849. } else if (path && path.length) {
  5850. plotLine.svgElem = svgElem = renderer.path(path)
  5851. .attr(attribs).add();
  5852. // events
  5853. if (events) {
  5854. addEvent = function (eventType) {
  5855. svgElem.on(eventType, function (e) {
  5856. events[eventType].apply(plotLine, [e]);
  5857. });
  5858. };
  5859. for (eventType in events) {
  5860. addEvent(eventType);
  5861. }
  5862. }
  5863. }
  5864. // the plot band/line label
  5865. if (optionsLabel && defined(optionsLabel.text) && path && path.length &&
  5866. axis.width > 0 && axis.height > 0 && !path.flat) {
  5867. // apply defaults
  5868. optionsLabel = merge({
  5869. align: horiz && isBand && 'center',
  5870. x: horiz ? !isBand && 4 : 10,
  5871. verticalAlign: !horiz && isBand && 'middle',
  5872. y: horiz ? isBand ? 16 : 10 : isBand ? 6 : -4,
  5873. rotation: horiz && !isBand && 90
  5874. }, optionsLabel);
  5875. this.renderLabel(optionsLabel, path, isBand, zIndex);
  5876. } else if (label) { // move out of sight
  5877. label.hide();
  5878. }
  5879. // chainable
  5880. return plotLine;
  5881. },
  5882. /**
  5883. * Render and align label for plot line or band.
  5884. */
  5885. renderLabel: function (optionsLabel, path, isBand, zIndex) {
  5886. var plotLine = this,
  5887. label = plotLine.label,
  5888. renderer = plotLine.axis.chart.renderer,
  5889. attribs,
  5890. xs,
  5891. ys,
  5892. x,
  5893. y;
  5894. // add the SVG element
  5895. if (!label) {
  5896. attribs = {
  5897. align: optionsLabel.textAlign || optionsLabel.align,
  5898. rotation: optionsLabel.rotation
  5899. };
  5900. attribs.zIndex = zIndex;
  5901. plotLine.label = label = renderer.text(
  5902. optionsLabel.text,
  5903. 0,
  5904. 0,
  5905. optionsLabel.useHTML
  5906. )
  5907. .attr(attribs)
  5908. .css(optionsLabel.style)
  5909. .add();
  5910. }
  5911. // get the bounding box and align the label
  5912. // #3000 changed to better handle choice between plotband or plotline
  5913. xs = [path[1], path[4], (isBand ? path[6] : path[1])];
  5914. ys = [path[2], path[5], (isBand ? path[7] : path[2])];
  5915. x = arrayMin(xs);
  5916. y = arrayMin(ys);
  5917. label.align(optionsLabel, false, {
  5918. x: x,
  5919. y: y,
  5920. width: arrayMax(xs) - x,
  5921. height: arrayMax(ys) - y
  5922. });
  5923. label.show();
  5924. },
  5925. /**
  5926. * Remove the plot line or band
  5927. */
  5928. destroy: function () {
  5929. // remove it from the lookup
  5930. erase(this.axis.plotLinesAndBands, this);
  5931. delete this.axis;
  5932. destroyObjectProperties(this);
  5933. }
  5934. };
  5935. /**
  5936. * Object with members for extending the Axis prototype
  5937. */
  5938. AxisPlotLineOrBandExtension = {
  5939. /**
  5940. * Create the path for a plot band
  5941. */
  5942. getPlotBandPath: function (from, to) {
  5943. var toPath = this.getPlotLinePath(to, null, null, true),
  5944. path = this.getPlotLinePath(from, null, null, true);
  5945. if (path && toPath) {
  5946. // Flat paths don't need labels (#3836)
  5947. path.flat = path.toString() === toPath.toString();
  5948. path.push(
  5949. toPath[4],
  5950. toPath[5],
  5951. toPath[1],
  5952. toPath[2]
  5953. );
  5954. } else { // outside the axis area
  5955. path = null;
  5956. }
  5957. return path;
  5958. },
  5959. addPlotBand: function (options) {
  5960. return this.addPlotBandOrLine(options, 'plotBands');
  5961. },
  5962. addPlotLine: function (options) {
  5963. return this.addPlotBandOrLine(options, 'plotLines');
  5964. },
  5965. /**
  5966. * Add a plot band or plot line after render time
  5967. *
  5968. * @param options {Object} The plotBand or plotLine configuration object
  5969. */
  5970. addPlotBandOrLine: function (options, coll) {
  5971. var obj = new Highcharts.PlotLineOrBand(this, options).render(),
  5972. userOptions = this.userOptions;
  5973. if (obj) { // #2189
  5974. // Add it to the user options for exporting and Axis.update
  5975. if (coll) {
  5976. userOptions[coll] = userOptions[coll] || [];
  5977. userOptions[coll].push(options);
  5978. }
  5979. this.plotLinesAndBands.push(obj);
  5980. }
  5981. return obj;
  5982. },
  5983. /**
  5984. * Remove a plot band or plot line from the chart by id
  5985. * @param {Object} id
  5986. */
  5987. removePlotBandOrLine: function (id) {
  5988. var plotLinesAndBands = this.plotLinesAndBands,
  5989. options = this.options,
  5990. userOptions = this.userOptions,
  5991. i = plotLinesAndBands.length;
  5992. while (i--) {
  5993. if (plotLinesAndBands[i].id === id) {
  5994. plotLinesAndBands[i].destroy();
  5995. }
  5996. }
  5997. each([options.plotLines || [], userOptions.plotLines || [], options.plotBands || [], userOptions.plotBands || []], function (arr) {
  5998. i = arr.length;
  5999. while (i--) {
  6000. if (arr[i].id === id) {
  6001. erase(arr, arr[i]);
  6002. }
  6003. }
  6004. });
  6005. }
  6006. };
  6007. /**
  6008. * Create a new axis object
  6009. * @param {Object} chart
  6010. * @param {Object} options
  6011. */
  6012. var Axis = Highcharts.Axis = function () {
  6013. this.init.apply(this, arguments);
  6014. };
  6015. Axis.prototype = {
  6016. /**
  6017. * Default options for the X axis - the Y axis has extended defaults
  6018. */
  6019. defaultOptions: {
  6020. // allowDecimals: null,
  6021. // alternateGridColor: null,
  6022. // categories: [],
  6023. dateTimeLabelFormats: {
  6024. millisecond: '%H:%M:%S.%L',
  6025. second: '%H:%M:%S',
  6026. minute: '%H:%M',
  6027. hour: '%H:%M',
  6028. day: '%e. %b',
  6029. week: '%e. %b',
  6030. month: '%b \'%y',
  6031. year: '%Y'
  6032. },
  6033. endOnTick: false,
  6034. gridLineColor: '#D8D8D8',
  6035. // gridLineDashStyle: 'solid',
  6036. // gridLineWidth: 0,
  6037. // reversed: false,
  6038. labels: {
  6039. enabled: true,
  6040. // rotation: 0,
  6041. // align: 'center',
  6042. // step: null,
  6043. style: {
  6044. color: '#606060',
  6045. cursor: 'default',
  6046. fontSize: '11px'
  6047. },
  6048. x: 0
  6049. //y: undefined
  6050. /*formatter: function () {
  6051. return this.value;
  6052. },*/
  6053. },
  6054. lineColor: '#C0D0E0',
  6055. lineWidth: 1,
  6056. //linkedTo: null,
  6057. //max: undefined,
  6058. //min: undefined,
  6059. minPadding: 0.01,
  6060. maxPadding: 0.01,
  6061. //minRange: null,
  6062. minorGridLineColor: '#E0E0E0',
  6063. // minorGridLineDashStyle: null,
  6064. minorGridLineWidth: 1,
  6065. minorTickColor: '#A0A0A0',
  6066. //minorTickInterval: null,
  6067. minorTickLength: 2,
  6068. minorTickPosition: 'outside', // inside or outside
  6069. //minorTickWidth: 0,
  6070. //opposite: false,
  6071. //offset: 0,
  6072. //plotBands: [{
  6073. // events: {},
  6074. // zIndex: 1,
  6075. // labels: { align, x, verticalAlign, y, style, rotation, textAlign }
  6076. //}],
  6077. //plotLines: [{
  6078. // events: {}
  6079. // dashStyle: {}
  6080. // zIndex:
  6081. // labels: { align, x, verticalAlign, y, style, rotation, textAlign }
  6082. //}],
  6083. //reversed: false,
  6084. // showFirstLabel: true,
  6085. // showLastLabel: true,
  6086. startOfWeek: 1,
  6087. startOnTick: false,
  6088. tickColor: '#C0D0E0',
  6089. //tickInterval: null,
  6090. tickLength: 10,
  6091. tickmarkPlacement: 'between', // on or between
  6092. tickPixelInterval: 100,
  6093. tickPosition: 'outside',
  6094. //tickWidth: 1,
  6095. title: {
  6096. //text: null,
  6097. align: 'middle', // low, middle or high
  6098. //margin: 0 for horizontal, 10 for vertical axes,
  6099. //rotation: 0,
  6100. //side: 'outside',
  6101. style: {
  6102. color: '#707070'
  6103. }
  6104. //x: 0,
  6105. //y: 0
  6106. },
  6107. type: 'linear' // linear, logarithmic or datetime
  6108. //visible: true
  6109. },
  6110. /**
  6111. * This options set extends the defaultOptions for Y axes
  6112. */
  6113. defaultYAxisOptions: {
  6114. endOnTick: true,
  6115. gridLineWidth: 1,
  6116. tickPixelInterval: 72,
  6117. showLastLabel: true,
  6118. labels: {
  6119. x: -8
  6120. },
  6121. lineWidth: 0,
  6122. maxPadding: 0.05,
  6123. minPadding: 0.05,
  6124. startOnTick: true,
  6125. //tickWidth: 0,
  6126. title: {
  6127. rotation: 270,
  6128. text: 'Values'
  6129. },
  6130. stackLabels: {
  6131. enabled: false,
  6132. //align: dynamic,
  6133. //y: dynamic,
  6134. //x: dynamic,
  6135. //verticalAlign: dynamic,
  6136. //textAlign: dynamic,
  6137. //rotation: 0,
  6138. formatter: function () {
  6139. return Highcharts.numberFormat(this.total, -1);
  6140. },
  6141. style: merge(defaultPlotOptions.line.dataLabels.style, { color: '#000000' })
  6142. }
  6143. },
  6144. /**
  6145. * These options extend the defaultOptions for left axes
  6146. */
  6147. defaultLeftAxisOptions: {
  6148. labels: {
  6149. x: -15
  6150. },
  6151. title: {
  6152. rotation: 270
  6153. }
  6154. },
  6155. /**
  6156. * These options extend the defaultOptions for right axes
  6157. */
  6158. defaultRightAxisOptions: {
  6159. labels: {
  6160. x: 15
  6161. },
  6162. title: {
  6163. rotation: 90
  6164. }
  6165. },
  6166. /**
  6167. * These options extend the defaultOptions for bottom axes
  6168. */
  6169. defaultBottomAxisOptions: {
  6170. labels: {
  6171. autoRotation: [-45],
  6172. x: 0
  6173. // overflow: undefined,
  6174. // staggerLines: null
  6175. },
  6176. title: {
  6177. rotation: 0
  6178. }
  6179. },
  6180. /**
  6181. * These options extend the defaultOptions for top axes
  6182. */
  6183. defaultTopAxisOptions: {
  6184. labels: {
  6185. autoRotation: [-45],
  6186. x: 0
  6187. // overflow: undefined
  6188. // staggerLines: null
  6189. },
  6190. title: {
  6191. rotation: 0
  6192. }
  6193. },
  6194. /**
  6195. * Initialize the axis
  6196. */
  6197. init: function (chart, userOptions) {
  6198. var isXAxis = userOptions.isX,
  6199. axis = this;
  6200. axis.chart = chart;
  6201. // Flag, is the axis horizontal
  6202. axis.horiz = chart.inverted ? !isXAxis : isXAxis;
  6203. // Flag, isXAxis
  6204. axis.isXAxis = isXAxis;
  6205. axis.coll = isXAxis ? 'xAxis' : 'yAxis';
  6206. axis.opposite = userOptions.opposite; // needed in setOptions
  6207. axis.side = userOptions.side || (axis.horiz ?
  6208. (axis.opposite ? 0 : 2) : // top : bottom
  6209. (axis.opposite ? 1 : 3)); // right : left
  6210. axis.setOptions(userOptions);
  6211. var options = this.options,
  6212. type = options.type,
  6213. isDatetimeAxis = type === 'datetime';
  6214. axis.labelFormatter = options.labels.formatter || axis.defaultLabelFormatter; // can be overwritten by dynamic format
  6215. // Flag, stagger lines or not
  6216. axis.userOptions = userOptions;
  6217. //axis.axisTitleMargin = UNDEFINED,// = options.title.margin,
  6218. axis.minPixelPadding = 0;
  6219. axis.reversed = options.reversed;
  6220. axis.visible = options.visible !== false;
  6221. axis.zoomEnabled = options.zoomEnabled !== false;
  6222. // Initial categories
  6223. axis.categories = options.categories || type === 'category';
  6224. axis.names = axis.names || []; // Preserve on update (#3830)
  6225. // Elements
  6226. //axis.axisGroup = UNDEFINED;
  6227. //axis.gridGroup = UNDEFINED;
  6228. //axis.axisTitle = UNDEFINED;
  6229. //axis.axisLine = UNDEFINED;
  6230. // Shorthand types
  6231. axis.isLog = type === 'logarithmic';
  6232. axis.isDatetimeAxis = isDatetimeAxis;
  6233. // Flag, if axis is linked to another axis
  6234. axis.isLinked = defined(options.linkedTo);
  6235. // Linked axis.
  6236. //axis.linkedParent = UNDEFINED;
  6237. // Tick positions
  6238. //axis.tickPositions = UNDEFINED; // array containing predefined positions
  6239. // Tick intervals
  6240. //axis.tickInterval = UNDEFINED;
  6241. //axis.minorTickInterval = UNDEFINED;
  6242. // Major ticks
  6243. axis.ticks = {};
  6244. axis.labelEdge = [];
  6245. // Minor ticks
  6246. axis.minorTicks = {};
  6247. // List of plotLines/Bands
  6248. axis.plotLinesAndBands = [];
  6249. // Alternate bands
  6250. axis.alternateBands = {};
  6251. // Axis metrics
  6252. //axis.left = UNDEFINED;
  6253. //axis.top = UNDEFINED;
  6254. //axis.width = UNDEFINED;
  6255. //axis.height = UNDEFINED;
  6256. //axis.bottom = UNDEFINED;
  6257. //axis.right = UNDEFINED;
  6258. //axis.transA = UNDEFINED;
  6259. //axis.transB = UNDEFINED;
  6260. //axis.oldTransA = UNDEFINED;
  6261. axis.len = 0;
  6262. //axis.oldMin = UNDEFINED;
  6263. //axis.oldMax = UNDEFINED;
  6264. //axis.oldUserMin = UNDEFINED;
  6265. //axis.oldUserMax = UNDEFINED;
  6266. //axis.oldAxisLength = UNDEFINED;
  6267. axis.minRange = axis.userMinRange = options.minRange || options.maxZoom;
  6268. axis.range = options.range;
  6269. axis.offset = options.offset || 0;
  6270. // Dictionary for stacks
  6271. axis.stacks = {};
  6272. axis.oldStacks = {};
  6273. axis.stacksTouched = 0;
  6274. // Min and max in the data
  6275. //axis.dataMin = UNDEFINED,
  6276. //axis.dataMax = UNDEFINED,
  6277. // The axis range
  6278. axis.max = null;
  6279. axis.min = null;
  6280. // User set min and max
  6281. //axis.userMin = UNDEFINED,
  6282. //axis.userMax = UNDEFINED,
  6283. // Crosshair options
  6284. axis.crosshair = pick(options.crosshair, splat(chart.options.tooltip.crosshairs)[isXAxis ? 0 : 1], false);
  6285. // Run Axis
  6286. var eventType,
  6287. events = axis.options.events;
  6288. // Register
  6289. if (inArray(axis, chart.axes) === -1) { // don't add it again on Axis.update()
  6290. if (isXAxis && !this.isColorAxis) { // #2713
  6291. chart.axes.splice(chart.xAxis.length, 0, axis);
  6292. } else {
  6293. chart.axes.push(axis);
  6294. }
  6295. chart[axis.coll].push(axis);
  6296. }
  6297. axis.series = axis.series || []; // populated by Series
  6298. // inverted charts have reversed xAxes as default
  6299. if (chart.inverted && isXAxis && axis.reversed === UNDEFINED) {
  6300. axis.reversed = true;
  6301. }
  6302. axis.removePlotBand = axis.removePlotBandOrLine;
  6303. axis.removePlotLine = axis.removePlotBandOrLine;
  6304. // register event listeners
  6305. for (eventType in events) {
  6306. addEvent(axis, eventType, events[eventType]);
  6307. }
  6308. // extend logarithmic axis
  6309. if (axis.isLog) {
  6310. axis.val2lin = axis.log2lin;
  6311. axis.lin2val = axis.lin2log;
  6312. }
  6313. },
  6314. /**
  6315. * Merge and set options
  6316. */
  6317. setOptions: function (userOptions) {
  6318. this.options = merge(
  6319. this.defaultOptions,
  6320. this.isXAxis ? {} : this.defaultYAxisOptions,
  6321. [this.defaultTopAxisOptions, this.defaultRightAxisOptions,
  6322. this.defaultBottomAxisOptions, this.defaultLeftAxisOptions][this.side],
  6323. merge(
  6324. defaultOptions[this.coll], // if set in setOptions (#1053)
  6325. userOptions
  6326. )
  6327. );
  6328. },
  6329. /**
  6330. * The default label formatter. The context is a special config object for the label.
  6331. */
  6332. defaultLabelFormatter: function () {
  6333. var axis = this.axis,
  6334. value = this.value,
  6335. categories = axis.categories,
  6336. dateTimeLabelFormat = this.dateTimeLabelFormat,
  6337. numericSymbols = defaultOptions.lang.numericSymbols,
  6338. i = numericSymbols && numericSymbols.length,
  6339. multi,
  6340. ret,
  6341. formatOption = axis.options.labels.format,
  6342. // make sure the same symbol is added for all labels on a linear axis
  6343. numericSymbolDetector = axis.isLog ? value : axis.tickInterval;
  6344. if (formatOption) {
  6345. ret = format(formatOption, this);
  6346. } else if (categories) {
  6347. ret = value;
  6348. } else if (dateTimeLabelFormat) { // datetime axis
  6349. ret = dateFormat(dateTimeLabelFormat, value);
  6350. } else if (i && numericSymbolDetector >= 1000) {
  6351. // Decide whether we should add a numeric symbol like k (thousands) or M (millions).
  6352. // If we are to enable this in tooltip or other places as well, we can move this
  6353. // logic to the numberFormatter and enable it by a parameter.
  6354. while (i-- && ret === UNDEFINED) {
  6355. multi = Math.pow(1000, i + 1);
  6356. if (numericSymbolDetector >= multi && (value * 10) % multi === 0 && numericSymbols[i] !== null) {
  6357. ret = Highcharts.numberFormat(value / multi, -1) + numericSymbols[i];
  6358. }
  6359. }
  6360. }
  6361. if (ret === UNDEFINED) {
  6362. if (mathAbs(value) >= 10000) { // add thousands separators
  6363. ret = Highcharts.numberFormat(value, -1);
  6364. } else { // small numbers
  6365. ret = Highcharts.numberFormat(value, -1, UNDEFINED, ''); // #2466
  6366. }
  6367. }
  6368. return ret;
  6369. },
  6370. /**
  6371. * Get the minimum and maximum for the series of each axis
  6372. */
  6373. getSeriesExtremes: function () {
  6374. var axis = this,
  6375. chart = axis.chart;
  6376. axis.hasVisibleSeries = false;
  6377. // Reset properties in case we're redrawing (#3353)
  6378. axis.dataMin = axis.dataMax = axis.threshold = null;
  6379. axis.softThreshold = !axis.isXAxis;
  6380. if (axis.buildStacks) {
  6381. axis.buildStacks();
  6382. }
  6383. // loop through this axis' series
  6384. each(axis.series, function (series) {
  6385. if (series.visible || !chart.options.chart.ignoreHiddenSeries) {
  6386. var seriesOptions = series.options,
  6387. xData,
  6388. threshold = seriesOptions.threshold,
  6389. seriesDataMin,
  6390. seriesDataMax;
  6391. axis.hasVisibleSeries = true;
  6392. // Validate threshold in logarithmic axes
  6393. if (axis.isLog && threshold <= 0) {
  6394. threshold = null;
  6395. }
  6396. // Get dataMin and dataMax for X axes
  6397. if (axis.isXAxis) {
  6398. xData = series.xData;
  6399. if (xData.length) {
  6400. // If xData contains values which is not numbers, then filter them out.
  6401. // To prevent performance hit, we only do this after we have already
  6402. // found seriesDataMin because in most cases all data is valid. #5234.
  6403. seriesDataMin = arrayMin(xData);
  6404. if (!isNumber(seriesDataMin) && !(seriesDataMin instanceof Date)) { // Date for #5010
  6405. xData = grep(xData, function (x) {
  6406. return isNumber(x);
  6407. });
  6408. seriesDataMin = arrayMin(xData); // Do it again with valid data
  6409. }
  6410. axis.dataMin = mathMin(pick(axis.dataMin, xData[0]), seriesDataMin);
  6411. axis.dataMax = mathMax(pick(axis.dataMax, xData[0]), arrayMax(xData));
  6412. }
  6413. // Get dataMin and dataMax for Y axes, as well as handle stacking and processed data
  6414. } else {
  6415. // Get this particular series extremes
  6416. series.getExtremes();
  6417. seriesDataMax = series.dataMax;
  6418. seriesDataMin = series.dataMin;
  6419. // Get the dataMin and dataMax so far. If percentage is used, the min and max are
  6420. // always 0 and 100. If seriesDataMin and seriesDataMax is null, then series
  6421. // doesn't have active y data, we continue with nulls
  6422. if (defined(seriesDataMin) && defined(seriesDataMax)) {
  6423. axis.dataMin = mathMin(pick(axis.dataMin, seriesDataMin), seriesDataMin);
  6424. axis.dataMax = mathMax(pick(axis.dataMax, seriesDataMax), seriesDataMax);
  6425. }
  6426. // Adjust to threshold
  6427. if (defined(threshold)) {
  6428. axis.threshold = threshold;
  6429. }
  6430. // If any series has a hard threshold, it takes precedence
  6431. if (!seriesOptions.softThreshold || axis.isLog) {
  6432. axis.softThreshold = false;
  6433. }
  6434. }
  6435. }
  6436. });
  6437. },
  6438. /**
  6439. * Translate from axis value to pixel position on the chart, or back
  6440. *
  6441. */
  6442. translate: function (val, backwards, cvsCoord, old, handleLog, pointPlacement) {
  6443. var axis = this.linkedParent || this, // #1417
  6444. sign = 1,
  6445. cvsOffset = 0,
  6446. localA = old ? axis.oldTransA : axis.transA,
  6447. localMin = old ? axis.oldMin : axis.min,
  6448. returnValue,
  6449. minPixelPadding = axis.minPixelPadding,
  6450. doPostTranslate = (axis.isOrdinal || axis.isBroken || (axis.isLog && handleLog)) && axis.lin2val;
  6451. if (!localA) {
  6452. localA = axis.transA;
  6453. }
  6454. // In vertical axes, the canvas coordinates start from 0 at the top like in
  6455. // SVG.
  6456. if (cvsCoord) {
  6457. sign *= -1; // canvas coordinates inverts the value
  6458. cvsOffset = axis.len;
  6459. }
  6460. // Handle reversed axis
  6461. if (axis.reversed) {
  6462. sign *= -1;
  6463. cvsOffset -= sign * (axis.sector || axis.len);
  6464. }
  6465. // From pixels to value
  6466. if (backwards) { // reverse translation
  6467. val = val * sign + cvsOffset;
  6468. val -= minPixelPadding;
  6469. returnValue = val / localA + localMin; // from chart pixel to value
  6470. if (doPostTranslate) { // log and ordinal axes
  6471. returnValue = axis.lin2val(returnValue);
  6472. }
  6473. // From value to pixels
  6474. } else {
  6475. if (doPostTranslate) { // log and ordinal axes
  6476. val = axis.val2lin(val);
  6477. }
  6478. if (pointPlacement === 'between') {
  6479. pointPlacement = 0.5;
  6480. }
  6481. returnValue = sign * (val - localMin) * localA + cvsOffset + (sign * minPixelPadding) +
  6482. (isNumber(pointPlacement) ? localA * pointPlacement * axis.pointRange : 0);
  6483. }
  6484. return returnValue;
  6485. },
  6486. /**
  6487. * Utility method to translate an axis value to pixel position.
  6488. * @param {Number} value A value in terms of axis units
  6489. * @param {Boolean} paneCoordinates Whether to return the pixel coordinate relative to the chart
  6490. * or just the axis/pane itself.
  6491. */
  6492. toPixels: function (value, paneCoordinates) {
  6493. return this.translate(value, false, !this.horiz, null, true) + (paneCoordinates ? 0 : this.pos);
  6494. },
  6495. /*
  6496. * Utility method to translate a pixel position in to an axis value
  6497. * @param {Number} pixel The pixel value coordinate
  6498. * @param {Boolean} paneCoordiantes Whether the input pixel is relative to the chart or just the
  6499. * axis/pane itself.
  6500. */
  6501. toValue: function (pixel, paneCoordinates) {
  6502. return this.translate(pixel - (paneCoordinates ? 0 : this.pos), true, !this.horiz, null, true);
  6503. },
  6504. /**
  6505. * Create the path for a plot line that goes from the given value on
  6506. * this axis, across the plot to the opposite side
  6507. * @param {Number} value
  6508. * @param {Number} lineWidth Used for calculation crisp line
  6509. * @param {Number] old Use old coordinates (for resizing and rescaling)
  6510. */
  6511. getPlotLinePath: function (value, lineWidth, old, force, translatedValue) {
  6512. var axis = this,
  6513. chart = axis.chart,
  6514. axisLeft = axis.left,
  6515. axisTop = axis.top,
  6516. x1,
  6517. y1,
  6518. x2,
  6519. y2,
  6520. cHeight = (old && chart.oldChartHeight) || chart.chartHeight,
  6521. cWidth = (old && chart.oldChartWidth) || chart.chartWidth,
  6522. skip,
  6523. transB = axis.transB,
  6524. /**
  6525. * Check if x is between a and b. If not, either move to a/b or skip,
  6526. * depending on the force parameter.
  6527. */
  6528. between = function (x, a, b) {
  6529. if (x < a || x > b) {
  6530. if (force) {
  6531. x = mathMin(mathMax(a, x), b);
  6532. } else {
  6533. skip = true;
  6534. }
  6535. }
  6536. return x;
  6537. };
  6538. translatedValue = pick(translatedValue, axis.translate(value, null, null, old));
  6539. x1 = x2 = mathRound(translatedValue + transB);
  6540. y1 = y2 = mathRound(cHeight - translatedValue - transB);
  6541. if (!isNumber(translatedValue)) { // no min or max
  6542. skip = true;
  6543. } else if (axis.horiz) {
  6544. y1 = axisTop;
  6545. y2 = cHeight - axis.bottom;
  6546. x1 = x2 = between(x1, axisLeft, axisLeft + axis.width);
  6547. } else {
  6548. x1 = axisLeft;
  6549. x2 = cWidth - axis.right;
  6550. y1 = y2 = between(y1, axisTop, axisTop + axis.height);
  6551. }
  6552. return skip && !force ?
  6553. null :
  6554. chart.renderer.crispLine([M, x1, y1, L, x2, y2], lineWidth || 1);
  6555. },
  6556. /**
  6557. * Set the tick positions of a linear axis to round values like whole tens or every five.
  6558. */
  6559. getLinearTickPositions: function (tickInterval, min, max) {
  6560. var pos,
  6561. lastPos,
  6562. roundedMin = correctFloat(mathFloor(min / tickInterval) * tickInterval),
  6563. roundedMax = correctFloat(mathCeil(max / tickInterval) * tickInterval),
  6564. tickPositions = [];
  6565. // For single points, add a tick regardless of the relative position (#2662)
  6566. if (min === max && isNumber(min)) {
  6567. return [min];
  6568. }
  6569. // Populate the intermediate values
  6570. pos = roundedMin;
  6571. while (pos <= roundedMax) {
  6572. // Place the tick on the rounded value
  6573. tickPositions.push(pos);
  6574. // Always add the raw tickInterval, not the corrected one.
  6575. pos = correctFloat(pos + tickInterval);
  6576. // If the interval is not big enough in the current min - max range to actually increase
  6577. // the loop variable, we need to break out to prevent endless loop. Issue #619
  6578. if (pos === lastPos) {
  6579. break;
  6580. }
  6581. // Record the last value
  6582. lastPos = pos;
  6583. }
  6584. return tickPositions;
  6585. },
  6586. /**
  6587. * Return the minor tick positions. For logarithmic axes, reuse the same logic
  6588. * as for major ticks.
  6589. */
  6590. getMinorTickPositions: function () {
  6591. var axis = this,
  6592. options = axis.options,
  6593. tickPositions = axis.tickPositions,
  6594. minorTickInterval = axis.minorTickInterval,
  6595. minorTickPositions = [],
  6596. pos,
  6597. i,
  6598. pointRangePadding = axis.pointRangePadding || 0,
  6599. min = axis.min - pointRangePadding, // #1498
  6600. max = axis.max + pointRangePadding, // #1498
  6601. range = max - min,
  6602. len;
  6603. // If minor ticks get too dense, they are hard to read, and may cause long running script. So we don't draw them.
  6604. if (range && range / minorTickInterval < axis.len / 3) { // #3875
  6605. if (axis.isLog) {
  6606. len = tickPositions.length;
  6607. for (i = 1; i < len; i++) {
  6608. minorTickPositions = minorTickPositions.concat(
  6609. axis.getLogTickPositions(minorTickInterval, tickPositions[i - 1], tickPositions[i], true)
  6610. );
  6611. }
  6612. } else if (axis.isDatetimeAxis && options.minorTickInterval === 'auto') { // #1314
  6613. minorTickPositions = minorTickPositions.concat(
  6614. axis.getTimeTicks(
  6615. axis.normalizeTimeTickInterval(minorTickInterval),
  6616. min,
  6617. max,
  6618. options.startOfWeek
  6619. )
  6620. );
  6621. } else {
  6622. for (pos = min + (tickPositions[0] - min) % minorTickInterval; pos <= max; pos += minorTickInterval) {
  6623. minorTickPositions.push(pos);
  6624. }
  6625. }
  6626. }
  6627. if (minorTickPositions.length !== 0) { // don't change the extremes, when there is no minor ticks
  6628. axis.trimTicks(minorTickPositions, options.startOnTick, options.endOnTick); // #3652 #3743 #1498
  6629. }
  6630. return minorTickPositions;
  6631. },
  6632. /**
  6633. * Adjust the min and max for the minimum range. Keep in mind that the series data is
  6634. * not yet processed, so we don't have information on data cropping and grouping, or
  6635. * updated axis.pointRange or series.pointRange. The data can't be processed until
  6636. * we have finally established min and max.
  6637. */
  6638. adjustForMinRange: function () {
  6639. var axis = this,
  6640. options = axis.options,
  6641. min = axis.min,
  6642. max = axis.max,
  6643. zoomOffset,
  6644. spaceAvailable = axis.dataMax - axis.dataMin >= axis.minRange,
  6645. closestDataRange,
  6646. i,
  6647. distance,
  6648. xData,
  6649. loopLength,
  6650. minArgs,
  6651. maxArgs,
  6652. minRange;
  6653. // Set the automatic minimum range based on the closest point distance
  6654. if (axis.isXAxis && axis.minRange === UNDEFINED && !axis.isLog) {
  6655. if (defined(options.min) || defined(options.max)) {
  6656. axis.minRange = null; // don't do this again
  6657. } else {
  6658. // Find the closest distance between raw data points, as opposed to
  6659. // closestPointRange that applies to processed points (cropped and grouped)
  6660. each(axis.series, function (series) {
  6661. xData = series.xData;
  6662. loopLength = series.xIncrement ? 1 : xData.length - 1;
  6663. for (i = loopLength; i > 0; i--) {
  6664. distance = xData[i] - xData[i - 1];
  6665. if (closestDataRange === UNDEFINED || distance < closestDataRange) {
  6666. closestDataRange = distance;
  6667. }
  6668. }
  6669. });
  6670. axis.minRange = mathMin(closestDataRange * 5, axis.dataMax - axis.dataMin);
  6671. }
  6672. }
  6673. // if minRange is exceeded, adjust
  6674. if (max - min < axis.minRange) {
  6675. minRange = axis.minRange;
  6676. zoomOffset = (minRange - max + min) / 2;
  6677. // if min and max options have been set, don't go beyond it
  6678. minArgs = [min - zoomOffset, pick(options.min, min - zoomOffset)];
  6679. if (spaceAvailable) { // if space is available, stay within the data range
  6680. minArgs[2] = axis.dataMin;
  6681. }
  6682. min = arrayMax(minArgs);
  6683. maxArgs = [min + minRange, pick(options.max, min + minRange)];
  6684. if (spaceAvailable) { // if space is availabe, stay within the data range
  6685. maxArgs[2] = axis.dataMax;
  6686. }
  6687. max = arrayMin(maxArgs);
  6688. // now if the max is adjusted, adjust the min back
  6689. if (max - min < minRange) {
  6690. minArgs[0] = max - minRange;
  6691. minArgs[1] = pick(options.min, max - minRange);
  6692. min = arrayMax(minArgs);
  6693. }
  6694. }
  6695. // Record modified extremes
  6696. axis.min = min;
  6697. axis.max = max;
  6698. },
  6699. /**
  6700. * Find the closestPointRange across all series
  6701. */
  6702. getClosest: function () {
  6703. var ret;
  6704. each(this.series, function (series) {
  6705. var seriesClosest = series.closestPointRange;
  6706. if (!series.noSharedTooltip && defined(seriesClosest)) {
  6707. ret = defined(ret) ?
  6708. mathMin(ret, seriesClosest) :
  6709. seriesClosest;
  6710. }
  6711. });
  6712. return ret;
  6713. },
  6714. /**
  6715. * Update translation information
  6716. */
  6717. setAxisTranslation: function (saveOld) {
  6718. var axis = this,
  6719. range = axis.max - axis.min,
  6720. pointRange = axis.axisPointRange || 0,
  6721. closestPointRange,
  6722. minPointOffset = 0,
  6723. pointRangePadding = 0,
  6724. linkedParent = axis.linkedParent,
  6725. ordinalCorrection,
  6726. hasCategories = !!axis.categories,
  6727. transA = axis.transA,
  6728. isXAxis = axis.isXAxis;
  6729. // Adjust translation for padding. Y axis with categories need to go through the same (#1784).
  6730. if (isXAxis || hasCategories || pointRange) {
  6731. if (linkedParent) {
  6732. minPointOffset = linkedParent.minPointOffset;
  6733. pointRangePadding = linkedParent.pointRangePadding;
  6734. } else {
  6735. // Get the closest points
  6736. closestPointRange = axis.getClosest();
  6737. each(axis.series, function (series) {
  6738. var seriesPointRange = hasCategories ?
  6739. 1 :
  6740. (isXAxis ?
  6741. pick(series.options.pointRange, closestPointRange, 0) :
  6742. (axis.axisPointRange || 0)), // #2806
  6743. pointPlacement = series.options.pointPlacement;
  6744. pointRange = mathMax(pointRange, seriesPointRange);
  6745. if (!axis.single) {
  6746. // minPointOffset is the value padding to the left of the axis in order to make
  6747. // room for points with a pointRange, typically columns. When the pointPlacement option
  6748. // is 'between' or 'on', this padding does not apply.
  6749. minPointOffset = mathMax(
  6750. minPointOffset,
  6751. isString(pointPlacement) ? 0 : seriesPointRange / 2
  6752. );
  6753. // Determine the total padding needed to the length of the axis to make room for the
  6754. // pointRange. If the series' pointPlacement is 'on', no padding is added.
  6755. pointRangePadding = mathMax(
  6756. pointRangePadding,
  6757. pointPlacement === 'on' ? 0 : seriesPointRange
  6758. );
  6759. }
  6760. });
  6761. }
  6762. // Record minPointOffset and pointRangePadding
  6763. ordinalCorrection = axis.ordinalSlope && closestPointRange ? axis.ordinalSlope / closestPointRange : 1; // #988, #1853
  6764. axis.minPointOffset = minPointOffset = minPointOffset * ordinalCorrection;
  6765. axis.pointRangePadding = pointRangePadding = pointRangePadding * ordinalCorrection;
  6766. // pointRange means the width reserved for each point, like in a column chart
  6767. axis.pointRange = mathMin(pointRange, range);
  6768. // closestPointRange means the closest distance between points. In columns
  6769. // it is mostly equal to pointRange, but in lines pointRange is 0 while closestPointRange
  6770. // is some other value
  6771. if (isXAxis) {
  6772. axis.closestPointRange = closestPointRange;
  6773. }
  6774. }
  6775. // Secondary values
  6776. if (saveOld) {
  6777. axis.oldTransA = transA;
  6778. }
  6779. axis.translationSlope = axis.transA = transA = axis.len / ((range + pointRangePadding) || 1);
  6780. axis.transB = axis.horiz ? axis.left : axis.bottom; // translation addend
  6781. axis.minPixelPadding = transA * minPointOffset;
  6782. },
  6783. minFromRange: function () {
  6784. return this.max - this.range;
  6785. },
  6786. /**
  6787. * Set the tick positions to round values and optionally extend the extremes
  6788. * to the nearest tick
  6789. */
  6790. setTickInterval: function (secondPass) {
  6791. var axis = this,
  6792. chart = axis.chart,
  6793. options = axis.options,
  6794. isLog = axis.isLog,
  6795. log2lin = axis.log2lin,
  6796. isDatetimeAxis = axis.isDatetimeAxis,
  6797. isXAxis = axis.isXAxis,
  6798. isLinked = axis.isLinked,
  6799. maxPadding = options.maxPadding,
  6800. minPadding = options.minPadding,
  6801. length,
  6802. linkedParentExtremes,
  6803. tickIntervalOption = options.tickInterval,
  6804. minTickInterval,
  6805. tickPixelIntervalOption = options.tickPixelInterval,
  6806. categories = axis.categories,
  6807. threshold = axis.threshold,
  6808. softThreshold = axis.softThreshold,
  6809. thresholdMin,
  6810. thresholdMax,
  6811. hardMin,
  6812. hardMax;
  6813. if (!isDatetimeAxis && !categories && !isLinked) {
  6814. this.getTickAmount();
  6815. }
  6816. // Min or max set either by zooming/setExtremes or initial options
  6817. hardMin = pick(axis.userMin, options.min);
  6818. hardMax = pick(axis.userMax, options.max);
  6819. // Linked axis gets the extremes from the parent axis
  6820. if (isLinked) {
  6821. axis.linkedParent = chart[axis.coll][options.linkedTo];
  6822. linkedParentExtremes = axis.linkedParent.getExtremes();
  6823. axis.min = pick(linkedParentExtremes.min, linkedParentExtremes.dataMin);
  6824. axis.max = pick(linkedParentExtremes.max, linkedParentExtremes.dataMax);
  6825. if (options.type !== axis.linkedParent.options.type) {
  6826. error(11, 1); // Can't link axes of different type
  6827. }
  6828. // Initial min and max from the extreme data values
  6829. } else {
  6830. // Adjust to hard threshold
  6831. if (!softThreshold && defined(threshold)) {
  6832. if (axis.dataMin >= threshold) {
  6833. thresholdMin = threshold;
  6834. minPadding = 0;
  6835. } else if (axis.dataMax <= threshold) {
  6836. thresholdMax = threshold;
  6837. maxPadding = 0;
  6838. }
  6839. }
  6840. axis.min = pick(hardMin, thresholdMin, axis.dataMin);
  6841. axis.max = pick(hardMax, thresholdMax, axis.dataMax);
  6842. }
  6843. if (isLog) {
  6844. if (!secondPass && mathMin(axis.min, pick(axis.dataMin, axis.min)) <= 0) { // #978
  6845. error(10, 1); // Can't plot negative values on log axis
  6846. }
  6847. // The correctFloat cures #934, float errors on full tens. But it
  6848. // was too aggressive for #4360 because of conversion back to lin,
  6849. // therefore use precision 15.
  6850. axis.min = correctFloat(log2lin(axis.min), 15);
  6851. axis.max = correctFloat(log2lin(axis.max), 15);
  6852. }
  6853. // handle zoomed range
  6854. if (axis.range && defined(axis.max)) {
  6855. axis.userMin = axis.min = hardMin = mathMax(axis.min, axis.minFromRange()); // #618
  6856. axis.userMax = hardMax = axis.max;
  6857. axis.range = null; // don't use it when running setExtremes
  6858. }
  6859. // Hook for Highstock Scroller. Consider combining with beforePadding.
  6860. fireEvent(axis, 'foundExtremes');
  6861. // Hook for adjusting this.min and this.max. Used by bubble series.
  6862. if (axis.beforePadding) {
  6863. axis.beforePadding();
  6864. }
  6865. // adjust min and max for the minimum range
  6866. axis.adjustForMinRange();
  6867. // Pad the values to get clear of the chart's edges. To avoid tickInterval taking the padding
  6868. // into account, we do this after computing tick interval (#1337).
  6869. if (!categories && !axis.axisPointRange && !axis.usePercentage && !isLinked && defined(axis.min) && defined(axis.max)) {
  6870. length = axis.max - axis.min;
  6871. if (length) {
  6872. if (!defined(hardMin) && minPadding) {
  6873. axis.min -= length * minPadding;
  6874. }
  6875. if (!defined(hardMax) && maxPadding) {
  6876. axis.max += length * maxPadding;
  6877. }
  6878. }
  6879. }
  6880. // Stay within floor and ceiling
  6881. if (isNumber(options.floor)) {
  6882. axis.min = mathMax(axis.min, options.floor);
  6883. }
  6884. if (isNumber(options.ceiling)) {
  6885. axis.max = mathMin(axis.max, options.ceiling);
  6886. }
  6887. // When the threshold is soft, adjust the extreme value only if
  6888. // the data extreme and the padded extreme land on either side of the threshold. For example,
  6889. // a series of [0, 1, 2, 3] would make the yAxis add a tick for -1 because of the
  6890. // default minPadding and startOnTick options. This is prevented by the softThreshold
  6891. // option.
  6892. if (softThreshold && defined(axis.dataMin)) {
  6893. threshold = threshold || 0;
  6894. if (!defined(hardMin) && axis.min < threshold && axis.dataMin >= threshold) {
  6895. axis.min = threshold;
  6896. } else if (!defined(hardMax) && axis.max > threshold && axis.dataMax <= threshold) {
  6897. axis.max = threshold;
  6898. }
  6899. }
  6900. // get tickInterval
  6901. if (axis.min === axis.max || axis.min === undefined || axis.max === undefined) {
  6902. axis.tickInterval = 1;
  6903. } else if (isLinked && !tickIntervalOption &&
  6904. tickPixelIntervalOption === axis.linkedParent.options.tickPixelInterval) {
  6905. axis.tickInterval = tickIntervalOption = axis.linkedParent.tickInterval;
  6906. } else {
  6907. axis.tickInterval = pick(
  6908. tickIntervalOption,
  6909. this.tickAmount ? ((axis.max - axis.min) / mathMax(this.tickAmount - 1, 1)) : undefined,
  6910. categories ? // for categoried axis, 1 is default, for linear axis use tickPix
  6911. 1 :
  6912. // don't let it be more than the data range
  6913. (axis.max - axis.min) * tickPixelIntervalOption / mathMax(axis.len, tickPixelIntervalOption)
  6914. );
  6915. }
  6916. // Now we're finished detecting min and max, crop and group series data. This
  6917. // is in turn needed in order to find tick positions in ordinal axes.
  6918. if (isXAxis && !secondPass) {
  6919. each(axis.series, function (series) {
  6920. series.processData(axis.min !== axis.oldMin || axis.max !== axis.oldMax);
  6921. });
  6922. }
  6923. // set the translation factor used in translate function
  6924. axis.setAxisTranslation(true);
  6925. // hook for ordinal axes and radial axes
  6926. if (axis.beforeSetTickPositions) {
  6927. axis.beforeSetTickPositions();
  6928. }
  6929. // hook for extensions, used in Highstock ordinal axes
  6930. if (axis.postProcessTickInterval) {
  6931. axis.tickInterval = axis.postProcessTickInterval(axis.tickInterval);
  6932. }
  6933. // In column-like charts, don't cramp in more ticks than there are points (#1943, #4184)
  6934. if (axis.pointRange && !tickIntervalOption) {
  6935. axis.tickInterval = mathMax(axis.pointRange, axis.tickInterval);
  6936. }
  6937. // Before normalizing the tick interval, handle minimum tick interval. This applies only if tickInterval is not defined.
  6938. minTickInterval = pick(options.minTickInterval, axis.isDatetimeAxis && axis.closestPointRange);
  6939. if (!tickIntervalOption && axis.tickInterval < minTickInterval) {
  6940. axis.tickInterval = minTickInterval;
  6941. }
  6942. // for linear axes, get magnitude and normalize the interval
  6943. if (!isDatetimeAxis && !isLog && !tickIntervalOption) {
  6944. axis.tickInterval = normalizeTickInterval(
  6945. axis.tickInterval,
  6946. null,
  6947. getMagnitude(axis.tickInterval),
  6948. // If the tick interval is between 0.5 and 5 and the axis max is in the order of
  6949. // thousands, chances are we are dealing with years. Don't allow decimals. #3363.
  6950. pick(options.allowDecimals, !(axis.tickInterval > 0.5 && axis.tickInterval < 5 && axis.max > 1000 && axis.max < 9999)),
  6951. !!this.tickAmount
  6952. );
  6953. }
  6954. // Prevent ticks from getting so close that we can't draw the labels
  6955. if (!this.tickAmount && this.len) { // Color axis with disabled legend has no length
  6956. axis.tickInterval = axis.unsquish();
  6957. }
  6958. this.setTickPositions();
  6959. },
  6960. /**
  6961. * Now we have computed the normalized tickInterval, get the tick positions
  6962. */
  6963. setTickPositions: function () {
  6964. var options = this.options,
  6965. tickPositions,
  6966. tickPositionsOption = options.tickPositions,
  6967. tickPositioner = options.tickPositioner,
  6968. startOnTick = options.startOnTick,
  6969. endOnTick = options.endOnTick,
  6970. single;
  6971. // Set the tickmarkOffset
  6972. this.tickmarkOffset = (this.categories && options.tickmarkPlacement === 'between' &&
  6973. this.tickInterval === 1) ? 0.5 : 0; // #3202
  6974. // get minorTickInterval
  6975. this.minorTickInterval = options.minorTickInterval === 'auto' && this.tickInterval ?
  6976. this.tickInterval / 5 : options.minorTickInterval;
  6977. // Find the tick positions
  6978. this.tickPositions = tickPositions = tickPositionsOption && tickPositionsOption.slice(); // Work on a copy (#1565)
  6979. if (!tickPositions) {
  6980. if (this.isDatetimeAxis) {
  6981. tickPositions = this.getTimeTicks(
  6982. this.normalizeTimeTickInterval(this.tickInterval, options.units),
  6983. this.min,
  6984. this.max,
  6985. options.startOfWeek,
  6986. this.ordinalPositions,
  6987. this.closestPointRange,
  6988. true
  6989. );
  6990. } else if (this.isLog) {
  6991. tickPositions = this.getLogTickPositions(this.tickInterval, this.min, this.max);
  6992. } else {
  6993. tickPositions = this.getLinearTickPositions(this.tickInterval, this.min, this.max);
  6994. }
  6995. // Too dense ticks, keep only the first and last (#4477)
  6996. if (tickPositions.length > this.len) {
  6997. tickPositions = [tickPositions[0], tickPositions.pop()];
  6998. }
  6999. this.tickPositions = tickPositions;
  7000. // Run the tick positioner callback, that allows modifying auto tick positions.
  7001. if (tickPositioner) {
  7002. tickPositioner = tickPositioner.apply(this, [this.min, this.max]);
  7003. if (tickPositioner) {
  7004. this.tickPositions = tickPositions = tickPositioner;
  7005. }
  7006. }
  7007. }
  7008. if (!this.isLinked) {
  7009. // reset min/max or remove extremes based on start/end on tick
  7010. this.trimTicks(tickPositions, startOnTick, endOnTick);
  7011. // When there is only one point, or all points have the same value on this axis, then min
  7012. // and max are equal and tickPositions.length is 0 or 1. In this case, add some padding
  7013. // in order to center the point, but leave it with one tick. #1337.
  7014. if (this.min === this.max && defined(this.min) && !this.tickAmount) {
  7015. // Substract half a unit (#2619, #2846, #2515, #3390)
  7016. single = true;
  7017. this.min -= 0.5;
  7018. this.max += 0.5;
  7019. }
  7020. this.single = single;
  7021. if (!tickPositionsOption && !tickPositioner) {
  7022. this.adjustTickAmount();
  7023. }
  7024. }
  7025. },
  7026. /**
  7027. * Handle startOnTick and endOnTick by either adapting to padding min/max or rounded min/max
  7028. */
  7029. trimTicks: function (tickPositions, startOnTick, endOnTick) {
  7030. var roundedMin = tickPositions[0],
  7031. roundedMax = tickPositions[tickPositions.length - 1],
  7032. minPointOffset = this.minPointOffset || 0;
  7033. if (startOnTick) {
  7034. this.min = roundedMin;
  7035. } else {
  7036. while (this.min - minPointOffset > tickPositions[0]) {
  7037. tickPositions.shift();
  7038. }
  7039. }
  7040. if (endOnTick) {
  7041. this.max = roundedMax;
  7042. } else {
  7043. while (this.max + minPointOffset < tickPositions[tickPositions.length - 1]) {
  7044. tickPositions.pop();
  7045. }
  7046. }
  7047. // If no tick are left, set one tick in the middle (#3195)
  7048. if (tickPositions.length === 0 && defined(roundedMin)) {
  7049. tickPositions.push((roundedMax + roundedMin) / 2);
  7050. }
  7051. },
  7052. /**
  7053. * Check if there are multiple axes in the same pane
  7054. * @returns {Boolean} There are other axes
  7055. */
  7056. alignToOthers: function () {
  7057. var others = {}, // Whether there is another axis to pair with this one
  7058. hasOther,
  7059. options = this.options;
  7060. if (this.chart.options.chart.alignTicks !== false && options.alignTicks !== false) {
  7061. each(this.chart[this.coll], function (axis) {
  7062. var otherOptions = axis.options,
  7063. horiz = axis.horiz,
  7064. key = [
  7065. horiz ? otherOptions.left : otherOptions.top,
  7066. otherOptions.width,
  7067. otherOptions.height,
  7068. otherOptions.pane
  7069. ].join(',');
  7070. if (axis.series.length) { // #4442
  7071. if (others[key]) {
  7072. hasOther = true; // #4201
  7073. } else {
  7074. others[key] = 1;
  7075. }
  7076. }
  7077. });
  7078. }
  7079. return hasOther;
  7080. },
  7081. /**
  7082. * Set the max ticks of either the x and y axis collection
  7083. */
  7084. getTickAmount: function () {
  7085. var options = this.options,
  7086. tickAmount = options.tickAmount,
  7087. tickPixelInterval = options.tickPixelInterval;
  7088. if (!defined(options.tickInterval) && this.len < tickPixelInterval && !this.isRadial &&
  7089. !this.isLog && options.startOnTick && options.endOnTick) {
  7090. tickAmount = 2;
  7091. }
  7092. if (!tickAmount && this.alignToOthers()) {
  7093. // Add 1 because 4 tick intervals require 5 ticks (including first and last)
  7094. tickAmount = mathCeil(this.len / tickPixelInterval) + 1;
  7095. }
  7096. // For tick amounts of 2 and 3, compute five ticks and remove the intermediate ones. This
  7097. // prevents the axis from adding ticks that are too far away from the data extremes.
  7098. if (tickAmount < 4) {
  7099. this.finalTickAmt = tickAmount;
  7100. tickAmount = 5;
  7101. }
  7102. this.tickAmount = tickAmount;
  7103. },
  7104. /**
  7105. * When using multiple axes, adjust the number of ticks to match the highest
  7106. * number of ticks in that group
  7107. */
  7108. adjustTickAmount: function () {
  7109. var tickInterval = this.tickInterval,
  7110. tickPositions = this.tickPositions,
  7111. tickAmount = this.tickAmount,
  7112. finalTickAmt = this.finalTickAmt,
  7113. currentTickAmount = tickPositions && tickPositions.length,
  7114. i,
  7115. len;
  7116. if (currentTickAmount < tickAmount) {
  7117. while (tickPositions.length < tickAmount) {
  7118. tickPositions.push(correctFloat(
  7119. tickPositions[tickPositions.length - 1] + tickInterval
  7120. ));
  7121. }
  7122. this.transA *= (currentTickAmount - 1) / (tickAmount - 1);
  7123. this.max = tickPositions[tickPositions.length - 1];
  7124. // We have too many ticks, run second pass to try to reduce ticks
  7125. } else if (currentTickAmount > tickAmount) {
  7126. this.tickInterval *= 2;
  7127. this.setTickPositions();
  7128. }
  7129. // The finalTickAmt property is set in getTickAmount
  7130. if (defined(finalTickAmt)) {
  7131. i = len = tickPositions.length;
  7132. while (i--) {
  7133. if (
  7134. (finalTickAmt === 3 && i % 2 === 1) || // Remove every other tick
  7135. (finalTickAmt <= 2 && i > 0 && i < len - 1) // Remove all but first and last
  7136. ) {
  7137. tickPositions.splice(i, 1);
  7138. }
  7139. }
  7140. this.finalTickAmt = UNDEFINED;
  7141. }
  7142. },
  7143. /**
  7144. * Set the scale based on data min and max, user set min and max or options
  7145. *
  7146. */
  7147. setScale: function () {
  7148. var axis = this,
  7149. isDirtyData,
  7150. isDirtyAxisLength;
  7151. axis.oldMin = axis.min;
  7152. axis.oldMax = axis.max;
  7153. axis.oldAxisLength = axis.len;
  7154. // set the new axisLength
  7155. axis.setAxisSize();
  7156. //axisLength = horiz ? axisWidth : axisHeight;
  7157. isDirtyAxisLength = axis.len !== axis.oldAxisLength;
  7158. // is there new data?
  7159. each(axis.series, function (series) {
  7160. if (series.isDirtyData || series.isDirty ||
  7161. series.xAxis.isDirty) { // when x axis is dirty, we need new data extremes for y as well
  7162. isDirtyData = true;
  7163. }
  7164. });
  7165. // do we really need to go through all this?
  7166. if (isDirtyAxisLength || isDirtyData || axis.isLinked || axis.forceRedraw ||
  7167. axis.userMin !== axis.oldUserMin || axis.userMax !== axis.oldUserMax || axis.alignToOthers()) {
  7168. if (axis.resetStacks) {
  7169. axis.resetStacks();
  7170. }
  7171. axis.forceRedraw = false;
  7172. // get data extremes if needed
  7173. axis.getSeriesExtremes();
  7174. // get fixed positions based on tickInterval
  7175. axis.setTickInterval();
  7176. // record old values to decide whether a rescale is necessary later on (#540)
  7177. axis.oldUserMin = axis.userMin;
  7178. axis.oldUserMax = axis.userMax;
  7179. // Mark as dirty if it is not already set to dirty and extremes have changed. #595.
  7180. if (!axis.isDirty) {
  7181. axis.isDirty = isDirtyAxisLength || axis.min !== axis.oldMin || axis.max !== axis.oldMax;
  7182. }
  7183. } else if (axis.cleanStacks) {
  7184. axis.cleanStacks();
  7185. }
  7186. },
  7187. /**
  7188. * Set the extremes and optionally redraw
  7189. * @param {Number} newMin
  7190. * @param {Number} newMax
  7191. * @param {Boolean} redraw
  7192. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  7193. * configuration
  7194. * @param {Object} eventArguments
  7195. *
  7196. */
  7197. setExtremes: function (newMin, newMax, redraw, animation, eventArguments) {
  7198. var axis = this,
  7199. chart = axis.chart;
  7200. redraw = pick(redraw, true); // defaults to true
  7201. each(axis.series, function (serie) {
  7202. delete serie.kdTree;
  7203. });
  7204. // Extend the arguments with min and max
  7205. eventArguments = extend(eventArguments, {
  7206. min: newMin,
  7207. max: newMax
  7208. });
  7209. // Fire the event
  7210. fireEvent(axis, 'setExtremes', eventArguments, function () { // the default event handler
  7211. axis.userMin = newMin;
  7212. axis.userMax = newMax;
  7213. axis.eventArgs = eventArguments;
  7214. if (redraw) {
  7215. chart.redraw(animation);
  7216. }
  7217. });
  7218. },
  7219. /**
  7220. * Overridable method for zooming chart. Pulled out in a separate method to allow overriding
  7221. * in stock charts.
  7222. */
  7223. zoom: function (newMin, newMax) {
  7224. var dataMin = this.dataMin,
  7225. dataMax = this.dataMax,
  7226. options = this.options,
  7227. min = mathMin(dataMin, pick(options.min, dataMin)),
  7228. max = mathMax(dataMax, pick(options.max, dataMax));
  7229. // Prevent pinch zooming out of range. Check for defined is for #1946. #1734.
  7230. if (!this.allowZoomOutside) {
  7231. if (defined(dataMin) && newMin <= min) {
  7232. newMin = min;
  7233. }
  7234. if (defined(dataMax) && newMax >= max) {
  7235. newMax = max;
  7236. }
  7237. }
  7238. // In full view, displaying the reset zoom button is not required
  7239. this.displayBtn = newMin !== UNDEFINED || newMax !== UNDEFINED;
  7240. // Do it
  7241. this.setExtremes(
  7242. newMin,
  7243. newMax,
  7244. false,
  7245. UNDEFINED,
  7246. { trigger: 'zoom' }
  7247. );
  7248. return true;
  7249. },
  7250. /**
  7251. * Update the axis metrics
  7252. */
  7253. setAxisSize: function () {
  7254. var chart = this.chart,
  7255. options = this.options,
  7256. offsetLeft = options.offsetLeft || 0,
  7257. offsetRight = options.offsetRight || 0,
  7258. horiz = this.horiz,
  7259. width = pick(options.width, chart.plotWidth - offsetLeft + offsetRight),
  7260. height = pick(options.height, chart.plotHeight),
  7261. top = pick(options.top, chart.plotTop),
  7262. left = pick(options.left, chart.plotLeft + offsetLeft),
  7263. percentRegex = /%$/;
  7264. // Check for percentage based input values. Rounding fixes problems with
  7265. // column overflow and plot line filtering (#4898, #4899)
  7266. if (percentRegex.test(height)) {
  7267. height = Math.round(parseFloat(height) / 100 * chart.plotHeight);
  7268. }
  7269. if (percentRegex.test(top)) {
  7270. top = Math.round(parseFloat(top) / 100 * chart.plotHeight + chart.plotTop);
  7271. }
  7272. // Expose basic values to use in Series object and navigator
  7273. this.left = left;
  7274. this.top = top;
  7275. this.width = width;
  7276. this.height = height;
  7277. this.bottom = chart.chartHeight - height - top;
  7278. this.right = chart.chartWidth - width - left;
  7279. // Direction agnostic properties
  7280. this.len = mathMax(horiz ? width : height, 0); // mathMax fixes #905
  7281. this.pos = horiz ? left : top; // distance from SVG origin
  7282. },
  7283. /**
  7284. * Get the actual axis extremes
  7285. */
  7286. getExtremes: function () {
  7287. var axis = this,
  7288. isLog = axis.isLog,
  7289. lin2log = axis.lin2log;
  7290. return {
  7291. min: isLog ? correctFloat(lin2log(axis.min)) : axis.min,
  7292. max: isLog ? correctFloat(lin2log(axis.max)) : axis.max,
  7293. dataMin: axis.dataMin,
  7294. dataMax: axis.dataMax,
  7295. userMin: axis.userMin,
  7296. userMax: axis.userMax
  7297. };
  7298. },
  7299. /**
  7300. * Get the zero plane either based on zero or on the min or max value.
  7301. * Used in bar and area plots
  7302. */
  7303. getThreshold: function (threshold) {
  7304. var axis = this,
  7305. isLog = axis.isLog,
  7306. lin2log = axis.lin2log,
  7307. realMin = isLog ? lin2log(axis.min) : axis.min,
  7308. realMax = isLog ? lin2log(axis.max) : axis.max;
  7309. // With a threshold of null, make the columns/areas rise from the top or bottom
  7310. // depending on the value, assuming an actual threshold of 0 (#4233).
  7311. if (threshold === null) {
  7312. threshold = realMax < 0 ? realMax : realMin;
  7313. } else if (realMin > threshold) {
  7314. threshold = realMin;
  7315. } else if (realMax < threshold) {
  7316. threshold = realMax;
  7317. }
  7318. return axis.translate(threshold, 0, 1, 0, 1);
  7319. },
  7320. /**
  7321. * Compute auto alignment for the axis label based on which side the axis is on
  7322. * and the given rotation for the label
  7323. */
  7324. autoLabelAlign: function (rotation) {
  7325. var ret,
  7326. angle = (pick(rotation, 0) - (this.side * 90) + 720) % 360;
  7327. if (angle > 15 && angle < 165) {
  7328. ret = 'right';
  7329. } else if (angle > 195 && angle < 345) {
  7330. ret = 'left';
  7331. } else {
  7332. ret = 'center';
  7333. }
  7334. return ret;
  7335. },
  7336. /**
  7337. * Get the tick length and width for the axis.
  7338. * @param {String} prefix 'tick' or 'minorTick'
  7339. * @returns {Array} An array of tickLength and tickWidth
  7340. */
  7341. tickSize: function (prefix) {
  7342. var options = this.options,
  7343. tickLength = options[prefix + 'Length'],
  7344. tickWidth = pick(options[prefix + 'Width'], prefix === 'tick' && this.isXAxis ? 1 : 0); // X axis defaults to 1
  7345. if (tickWidth && tickLength) {
  7346. // Negate the length
  7347. if (options[prefix + 'Position'] === 'inside') {
  7348. tickLength = -tickLength;
  7349. }
  7350. return [tickLength, tickWidth];
  7351. }
  7352. },
  7353. /**
  7354. * Return the size of the labels
  7355. */
  7356. labelMetrics: function () {
  7357. return this.chart.renderer.fontMetrics(
  7358. this.options.labels.style.fontSize,
  7359. this.ticks[0] && this.ticks[0].label
  7360. );
  7361. },
  7362. /**
  7363. * Prevent the ticks from getting so close we can't draw the labels. On a horizontal
  7364. * axis, this is handled by rotating the labels, removing ticks and adding ellipsis.
  7365. * On a vertical axis remove ticks and add ellipsis.
  7366. */
  7367. unsquish: function () {
  7368. var labelOptions = this.options.labels,
  7369. horiz = this.horiz,
  7370. tickInterval = this.tickInterval,
  7371. newTickInterval = tickInterval,
  7372. slotSize = this.len / (((this.categories ? 1 : 0) + this.max - this.min) / tickInterval),
  7373. rotation,
  7374. rotationOption = labelOptions.rotation,
  7375. labelMetrics = this.labelMetrics(),
  7376. step,
  7377. bestScore = Number.MAX_VALUE,
  7378. autoRotation,
  7379. // Return the multiple of tickInterval that is needed to avoid collision
  7380. getStep = function (spaceNeeded) {
  7381. var step = spaceNeeded / (slotSize || 1);
  7382. step = step > 1 ? mathCeil(step) : 1;
  7383. return step * tickInterval;
  7384. };
  7385. if (horiz) {
  7386. autoRotation = !labelOptions.staggerLines && !labelOptions.step && ( // #3971
  7387. defined(rotationOption) ?
  7388. [rotationOption] :
  7389. slotSize < pick(labelOptions.autoRotationLimit, 80) && labelOptions.autoRotation
  7390. );
  7391. if (autoRotation) {
  7392. // Loop over the given autoRotation options, and determine which gives the best score. The
  7393. // best score is that with the lowest number of steps and a rotation closest to horizontal.
  7394. each(autoRotation, function (rot) {
  7395. var score;
  7396. if (rot === rotationOption || (rot && rot >= -90 && rot <= 90)) { // #3891
  7397. step = getStep(mathAbs(labelMetrics.h / mathSin(deg2rad * rot)));
  7398. score = step + mathAbs(rot / 360);
  7399. if (score < bestScore) {
  7400. bestScore = score;
  7401. rotation = rot;
  7402. newTickInterval = step;
  7403. }
  7404. }
  7405. });
  7406. }
  7407. } else if (!labelOptions.step) { // #4411
  7408. newTickInterval = getStep(labelMetrics.h);
  7409. }
  7410. this.autoRotation = autoRotation;
  7411. this.labelRotation = pick(rotation, rotationOption);
  7412. return newTickInterval;
  7413. },
  7414. /**
  7415. * Get the general slot width for this axis. This may change between the pre-render (from Axis.getOffset)
  7416. * and the final tick rendering and placement (#5086).
  7417. */
  7418. getSlotWidth: function () {
  7419. var chart = this.chart,
  7420. horiz = this.horiz,
  7421. labelOptions = this.options.labels,
  7422. slotCount = Math.max(this.tickPositions.length - (this.categories ? 0 : 1), 1),
  7423. marginLeft = chart.margin[3];
  7424. return (horiz && (labelOptions.step || 0) < 2 && !labelOptions.rotation && // #4415
  7425. ((this.staggerLines || 1) * chart.plotWidth) / slotCount) ||
  7426. (!horiz && ((marginLeft && (marginLeft - chart.spacing[3])) || chart.chartWidth * 0.33)); // #1580, #1931
  7427. },
  7428. /**
  7429. * Render the axis labels and determine whether ellipsis or rotation need to be applied
  7430. */
  7431. renderUnsquish: function () {
  7432. var chart = this.chart,
  7433. renderer = chart.renderer,
  7434. tickPositions = this.tickPositions,
  7435. ticks = this.ticks,
  7436. labelOptions = this.options.labels,
  7437. horiz = this.horiz,
  7438. slotWidth = this.getSlotWidth(),
  7439. innerWidth = mathMax(1, mathRound(slotWidth - 2 * (labelOptions.padding || 5))),
  7440. attr = {},
  7441. labelMetrics = this.labelMetrics(),
  7442. textOverflowOption = labelOptions.style.textOverflow,
  7443. css,
  7444. labelLength = 0,
  7445. label,
  7446. i,
  7447. pos;
  7448. // Set rotation option unless it is "auto", like in gauges
  7449. if (!isString(labelOptions.rotation)) {
  7450. attr.rotation = labelOptions.rotation || 0; // #4443
  7451. }
  7452. // Handle auto rotation on horizontal axis
  7453. if (this.autoRotation) {
  7454. // Get the longest label length
  7455. each(tickPositions, function (tick) {
  7456. tick = ticks[tick];
  7457. if (tick && tick.labelLength > labelLength) {
  7458. labelLength = tick.labelLength;
  7459. }
  7460. });
  7461. // Apply rotation only if the label is too wide for the slot, and
  7462. // the label is wider than its height.
  7463. if (labelLength > innerWidth && labelLength > labelMetrics.h) {
  7464. attr.rotation = this.labelRotation;
  7465. } else {
  7466. this.labelRotation = 0;
  7467. }
  7468. // Handle word-wrap or ellipsis on vertical axis
  7469. } else if (slotWidth) {
  7470. // For word-wrap or ellipsis
  7471. css = { width: innerWidth + PX };
  7472. if (!textOverflowOption) {
  7473. css.textOverflow = 'clip';
  7474. // On vertical axis, only allow word wrap if there is room for more lines.
  7475. i = tickPositions.length;
  7476. while (!horiz && i--) {
  7477. pos = tickPositions[i];
  7478. label = ticks[pos].label;
  7479. if (label) {
  7480. // Reset ellipsis in order to get the correct bounding box (#4070)
  7481. if (label.styles.textOverflow === 'ellipsis') {
  7482. label.css({ textOverflow: 'clip' });
  7483. // Set the correct width in order to read the bounding box height (#4678, #5034)
  7484. } else if (ticks[pos].labelLength > slotWidth) {
  7485. label.css({ width: slotWidth + 'px' });
  7486. }
  7487. if (label.getBBox().height > this.len / tickPositions.length - (labelMetrics.h - labelMetrics.f)) {
  7488. label.specCss = { textOverflow: 'ellipsis' };
  7489. }
  7490. }
  7491. }
  7492. }
  7493. }
  7494. // Add ellipsis if the label length is significantly longer than ideal
  7495. if (attr.rotation) {
  7496. css = {
  7497. width: (labelLength > chart.chartHeight * 0.5 ? chart.chartHeight * 0.33 : chart.chartHeight) + PX
  7498. };
  7499. if (!textOverflowOption) {
  7500. css.textOverflow = 'ellipsis';
  7501. }
  7502. }
  7503. // Set the explicit or automatic label alignment
  7504. this.labelAlign = labelOptions.align || this.autoLabelAlign(this.labelRotation);
  7505. if (this.labelAlign) {
  7506. attr.align = this.labelAlign;
  7507. }
  7508. // Apply general and specific CSS
  7509. each(tickPositions, function (pos) {
  7510. var tick = ticks[pos],
  7511. label = tick && tick.label;
  7512. if (label) {
  7513. label.attr(attr); // This needs to go before the CSS in old IE (#4502)
  7514. if (css) {
  7515. label.css(merge(css, label.specCss));
  7516. }
  7517. delete label.specCss;
  7518. tick.rotation = attr.rotation;
  7519. }
  7520. });
  7521. // Note: Why is this not part of getLabelPosition?
  7522. this.tickRotCorr = renderer.rotCorr(labelMetrics.b, this.labelRotation || 0, this.side !== 0);
  7523. },
  7524. /**
  7525. * Return true if the axis has associated data
  7526. */
  7527. hasData: function () {
  7528. return this.hasVisibleSeries || (defined(this.min) && defined(this.max) && !!this.tickPositions);
  7529. },
  7530. /**
  7531. * Render the tick labels to a preliminary position to get their sizes
  7532. */
  7533. getOffset: function () {
  7534. var axis = this,
  7535. chart = axis.chart,
  7536. renderer = chart.renderer,
  7537. options = axis.options,
  7538. tickPositions = axis.tickPositions,
  7539. ticks = axis.ticks,
  7540. horiz = axis.horiz,
  7541. side = axis.side,
  7542. invertedSide = chart.inverted ? [1, 0, 3, 2][side] : side,
  7543. hasData,
  7544. showAxis,
  7545. titleOffset = 0,
  7546. titleOffsetOption,
  7547. titleMargin = 0,
  7548. axisTitleOptions = options.title,
  7549. labelOptions = options.labels,
  7550. labelOffset = 0, // reset
  7551. labelOffsetPadded,
  7552. opposite = axis.opposite,
  7553. axisOffset = chart.axisOffset,
  7554. clipOffset = chart.clipOffset,
  7555. clip,
  7556. directionFactor = [-1, 1, 1, -1][side],
  7557. n,
  7558. textAlign,
  7559. axisParent = axis.axisParent, // Used in color axis
  7560. lineHeightCorrection,
  7561. tickSize = this.tickSize('tick');
  7562. // For reuse in Axis.render
  7563. hasData = axis.hasData();
  7564. axis.showAxis = showAxis = hasData || pick(options.showEmpty, true);
  7565. // Set/reset staggerLines
  7566. axis.staggerLines = axis.horiz && labelOptions.staggerLines;
  7567. // Create the axisGroup and gridGroup elements on first iteration
  7568. if (!axis.axisGroup) {
  7569. axis.gridGroup = renderer.g('grid')
  7570. .attr({ zIndex: options.gridZIndex || 1 })
  7571. .add(axisParent);
  7572. axis.axisGroup = renderer.g('axis')
  7573. .attr({ zIndex: options.zIndex || 2 })
  7574. .add(axisParent);
  7575. axis.labelGroup = renderer.g('axis-labels')
  7576. .attr({ zIndex: labelOptions.zIndex || 7 })
  7577. .addClass(PREFIX + axis.coll.toLowerCase() + '-labels')
  7578. .add(axisParent);
  7579. }
  7580. if (hasData || axis.isLinked) {
  7581. // Generate ticks
  7582. each(tickPositions, function (pos) {
  7583. if (!ticks[pos]) {
  7584. ticks[pos] = new Tick(axis, pos);
  7585. } else {
  7586. ticks[pos].addLabel(); // update labels depending on tick interval
  7587. }
  7588. });
  7589. axis.renderUnsquish();
  7590. // Left side must be align: right and right side must have align: left for labels
  7591. if (labelOptions.reserveSpace !== false && (side === 0 || side === 2 ||
  7592. { 1: 'left', 3: 'right' }[side] === axis.labelAlign || axis.labelAlign === 'center')) {
  7593. each(tickPositions, function (pos) {
  7594. // get the highest offset
  7595. labelOffset = mathMax(
  7596. ticks[pos].getLabelSize(),
  7597. labelOffset
  7598. );
  7599. });
  7600. }
  7601. if (axis.staggerLines) {
  7602. labelOffset *= axis.staggerLines;
  7603. axis.labelOffset = labelOffset * (axis.opposite ? -1 : 1);
  7604. }
  7605. } else { // doesn't have data
  7606. for (n in ticks) {
  7607. ticks[n].destroy();
  7608. delete ticks[n];
  7609. }
  7610. }
  7611. if (axisTitleOptions && axisTitleOptions.text && axisTitleOptions.enabled !== false) {
  7612. if (!axis.axisTitle) {
  7613. textAlign = axisTitleOptions.textAlign;
  7614. if (!textAlign) {
  7615. textAlign = (horiz ? {
  7616. low: 'left',
  7617. middle: 'center',
  7618. high: 'right'
  7619. } : {
  7620. low: opposite ? 'right' : 'left',
  7621. middle: 'center',
  7622. high: opposite ? 'left' : 'right'
  7623. })[axisTitleOptions.align];
  7624. }
  7625. axis.axisTitle = renderer.text(
  7626. axisTitleOptions.text,
  7627. 0,
  7628. 0,
  7629. axisTitleOptions.useHTML
  7630. )
  7631. .attr({
  7632. zIndex: 7,
  7633. rotation: axisTitleOptions.rotation || 0,
  7634. align: textAlign
  7635. })
  7636. .addClass(PREFIX + this.coll.toLowerCase() + '-title')
  7637. .css(axisTitleOptions.style)
  7638. .add(axis.axisGroup);
  7639. axis.axisTitle.isNew = true;
  7640. }
  7641. if (showAxis) {
  7642. titleOffset = axis.axisTitle.getBBox()[horiz ? 'height' : 'width'];
  7643. titleOffsetOption = axisTitleOptions.offset;
  7644. titleMargin = defined(titleOffsetOption) ? 0 : pick(axisTitleOptions.margin, horiz ? 5 : 10);
  7645. }
  7646. // hide or show the title depending on whether showEmpty is set
  7647. axis.axisTitle[showAxis ? 'show' : 'hide'](true);
  7648. }
  7649. // handle automatic or user set offset
  7650. axis.offset = directionFactor * pick(options.offset, axisOffset[side]);
  7651. axis.tickRotCorr = axis.tickRotCorr || { x: 0, y: 0 }; // polar
  7652. if (side === 0) {
  7653. lineHeightCorrection = -axis.labelMetrics().h;
  7654. } else if (side === 2) {
  7655. lineHeightCorrection = axis.tickRotCorr.y;
  7656. } else {
  7657. lineHeightCorrection = 0;
  7658. }
  7659. // Find the padded label offset
  7660. labelOffsetPadded = Math.abs(labelOffset) + titleMargin;
  7661. if (labelOffset) {
  7662. labelOffsetPadded -= lineHeightCorrection;
  7663. labelOffsetPadded += directionFactor * (horiz ? pick(labelOptions.y, axis.tickRotCorr.y + directionFactor * 8) : labelOptions.x);
  7664. }
  7665. axis.axisTitleMargin = pick(titleOffsetOption, labelOffsetPadded);
  7666. axisOffset[side] = mathMax(
  7667. axisOffset[side],
  7668. axis.axisTitleMargin + titleOffset + directionFactor * axis.offset,
  7669. labelOffsetPadded, // #3027
  7670. hasData && tickPositions.length && tickSize ? tickSize[0] : 0 // #4866
  7671. );
  7672. // Decide the clipping needed to keep the graph inside the plot area and axis lines
  7673. clip = options.offset ? 0 : mathFloor(options.lineWidth / 2) * 2; // #4308, #4371
  7674. clipOffset[invertedSide] = mathMax(clipOffset[invertedSide], clip);
  7675. },
  7676. /**
  7677. * Get the path for the axis line
  7678. */
  7679. getLinePath: function (lineWidth) {
  7680. var chart = this.chart,
  7681. opposite = this.opposite,
  7682. offset = this.offset,
  7683. horiz = this.horiz,
  7684. lineLeft = this.left + (opposite ? this.width : 0) + offset,
  7685. lineTop = chart.chartHeight - this.bottom - (opposite ? this.height : 0) + offset;
  7686. if (opposite) {
  7687. lineWidth *= -1; // crispify the other way - #1480, #1687
  7688. }
  7689. return chart.renderer
  7690. .crispLine([
  7691. M,
  7692. horiz ?
  7693. this.left :
  7694. lineLeft,
  7695. horiz ?
  7696. lineTop :
  7697. this.top,
  7698. L,
  7699. horiz ?
  7700. chart.chartWidth - this.right :
  7701. lineLeft,
  7702. horiz ?
  7703. lineTop :
  7704. chart.chartHeight - this.bottom
  7705. ], lineWidth);
  7706. },
  7707. /**
  7708. * Position the title
  7709. */
  7710. getTitlePosition: function () {
  7711. // compute anchor points for each of the title align options
  7712. var horiz = this.horiz,
  7713. axisLeft = this.left,
  7714. axisTop = this.top,
  7715. axisLength = this.len,
  7716. axisTitleOptions = this.options.title,
  7717. margin = horiz ? axisLeft : axisTop,
  7718. opposite = this.opposite,
  7719. offset = this.offset,
  7720. xOption = axisTitleOptions.x || 0,
  7721. yOption = axisTitleOptions.y || 0,
  7722. fontSize = pInt(axisTitleOptions.style.fontSize || 12),
  7723. // the position in the length direction of the axis
  7724. alongAxis = {
  7725. low: margin + (horiz ? 0 : axisLength),
  7726. middle: margin + axisLength / 2,
  7727. high: margin + (horiz ? axisLength : 0)
  7728. }[axisTitleOptions.align],
  7729. // the position in the perpendicular direction of the axis
  7730. offAxis = (horiz ? axisTop + this.height : axisLeft) +
  7731. (horiz ? 1 : -1) * // horizontal axis reverses the margin
  7732. (opposite ? -1 : 1) * // so does opposite axes
  7733. this.axisTitleMargin +
  7734. (this.side === 2 ? fontSize : 0);
  7735. return {
  7736. x: horiz ?
  7737. alongAxis + xOption :
  7738. offAxis + (opposite ? this.width : 0) + offset + xOption,
  7739. y: horiz ?
  7740. offAxis + yOption - (opposite ? this.height : 0) + offset :
  7741. alongAxis + yOption
  7742. };
  7743. },
  7744. /**
  7745. * Render the axis
  7746. */
  7747. render: function () {
  7748. var axis = this,
  7749. chart = axis.chart,
  7750. renderer = chart.renderer,
  7751. options = axis.options,
  7752. isLog = axis.isLog,
  7753. lin2log = axis.lin2log,
  7754. isLinked = axis.isLinked,
  7755. tickPositions = axis.tickPositions,
  7756. axisTitle = axis.axisTitle,
  7757. ticks = axis.ticks,
  7758. minorTicks = axis.minorTicks,
  7759. alternateBands = axis.alternateBands,
  7760. stackLabelOptions = options.stackLabels,
  7761. alternateGridColor = options.alternateGridColor,
  7762. tickmarkOffset = axis.tickmarkOffset,
  7763. lineWidth = options.lineWidth,
  7764. linePath,
  7765. hasRendered = chart.hasRendered,
  7766. slideInTicks = hasRendered && isNumber(axis.oldMin),
  7767. showAxis = axis.showAxis,
  7768. animation = animObject(renderer.globalAnimation),
  7769. from,
  7770. to;
  7771. // Reset
  7772. axis.labelEdge.length = 0;
  7773. //axis.justifyToPlot = overflow === 'justify';
  7774. axis.overlap = false;
  7775. // Mark all elements inActive before we go over and mark the active ones
  7776. each([ticks, minorTicks, alternateBands], function (coll) {
  7777. var pos;
  7778. for (pos in coll) {
  7779. coll[pos].isActive = false;
  7780. }
  7781. });
  7782. // If the series has data draw the ticks. Else only the line and title
  7783. if (axis.hasData() || isLinked) {
  7784. // minor ticks
  7785. if (axis.minorTickInterval && !axis.categories) {
  7786. each(axis.getMinorTickPositions(), function (pos) {
  7787. if (!minorTicks[pos]) {
  7788. minorTicks[pos] = new Tick(axis, pos, 'minor');
  7789. }
  7790. // render new ticks in old position
  7791. if (slideInTicks && minorTicks[pos].isNew) {
  7792. minorTicks[pos].render(null, true);
  7793. }
  7794. minorTicks[pos].render(null, false, 1);
  7795. });
  7796. }
  7797. // Major ticks. Pull out the first item and render it last so that
  7798. // we can get the position of the neighbour label. #808.
  7799. if (tickPositions.length) { // #1300
  7800. each(tickPositions, function (pos, i) {
  7801. // linked axes need an extra check to find out if
  7802. if (!isLinked || (pos >= axis.min && pos <= axis.max)) {
  7803. if (!ticks[pos]) {
  7804. ticks[pos] = new Tick(axis, pos);
  7805. }
  7806. // render new ticks in old position
  7807. if (slideInTicks && ticks[pos].isNew) {
  7808. ticks[pos].render(i, true, 0.1);
  7809. }
  7810. ticks[pos].render(i);
  7811. }
  7812. });
  7813. // In a categorized axis, the tick marks are displayed between labels. So
  7814. // we need to add a tick mark and grid line at the left edge of the X axis.
  7815. if (tickmarkOffset && (axis.min === 0 || axis.single)) {
  7816. if (!ticks[-1]) {
  7817. ticks[-1] = new Tick(axis, -1, null, true);
  7818. }
  7819. ticks[-1].render(-1);
  7820. }
  7821. }
  7822. // alternate grid color
  7823. if (alternateGridColor) {
  7824. each(tickPositions, function (pos, i) {
  7825. to = tickPositions[i + 1] !== UNDEFINED ? tickPositions[i + 1] + tickmarkOffset : axis.max - tickmarkOffset;
  7826. if (i % 2 === 0 && pos < axis.max && to <= axis.max + (chart.polar ? -tickmarkOffset : tickmarkOffset)) { // #2248, #4660
  7827. if (!alternateBands[pos]) {
  7828. alternateBands[pos] = new Highcharts.PlotLineOrBand(axis);
  7829. }
  7830. from = pos + tickmarkOffset; // #949
  7831. alternateBands[pos].options = {
  7832. from: isLog ? lin2log(from) : from,
  7833. to: isLog ? lin2log(to) : to,
  7834. color: alternateGridColor
  7835. };
  7836. alternateBands[pos].render();
  7837. alternateBands[pos].isActive = true;
  7838. }
  7839. });
  7840. }
  7841. // custom plot lines and bands
  7842. if (!axis._addedPlotLB) { // only first time
  7843. each((options.plotLines || []).concat(options.plotBands || []), function (plotLineOptions) {
  7844. axis.addPlotBandOrLine(plotLineOptions);
  7845. });
  7846. axis._addedPlotLB = true;
  7847. }
  7848. } // end if hasData
  7849. // Remove inactive ticks
  7850. each([ticks, minorTicks, alternateBands], function (coll) {
  7851. var pos,
  7852. i,
  7853. forDestruction = [],
  7854. delay = animation.duration,
  7855. destroyInactiveItems = function () {
  7856. i = forDestruction.length;
  7857. while (i--) {
  7858. // When resizing rapidly, the same items may be destroyed in different timeouts,
  7859. // or the may be reactivated
  7860. if (coll[forDestruction[i]] && !coll[forDestruction[i]].isActive) {
  7861. coll[forDestruction[i]].destroy();
  7862. delete coll[forDestruction[i]];
  7863. }
  7864. }
  7865. };
  7866. for (pos in coll) {
  7867. if (!coll[pos].isActive) {
  7868. // Render to zero opacity
  7869. coll[pos].render(pos, false, 0);
  7870. coll[pos].isActive = false;
  7871. forDestruction.push(pos);
  7872. }
  7873. }
  7874. // When the objects are finished fading out, destroy them
  7875. syncTimeout(
  7876. destroyInactiveItems,
  7877. coll === alternateBands || !chart.hasRendered || !delay ? 0 : delay
  7878. );
  7879. });
  7880. // Static items. As the axis group is cleared on subsequent calls
  7881. // to render, these items are added outside the group.
  7882. // axis line
  7883. if (lineWidth) {
  7884. linePath = axis.getLinePath(lineWidth);
  7885. if (!axis.axisLine) {
  7886. axis.axisLine = renderer.path(linePath)
  7887. .attr({
  7888. stroke: options.lineColor,
  7889. 'stroke-width': lineWidth,
  7890. zIndex: 7
  7891. })
  7892. .add(axis.axisGroup);
  7893. } else {
  7894. axis.axisLine.animate({ d: linePath });
  7895. }
  7896. // show or hide the line depending on options.showEmpty
  7897. axis.axisLine[showAxis ? 'show' : 'hide'](true);
  7898. }
  7899. if (axisTitle && showAxis) {
  7900. axisTitle[axisTitle.isNew ? 'attr' : 'animate'](
  7901. axis.getTitlePosition()
  7902. );
  7903. axisTitle.isNew = false;
  7904. }
  7905. // Stacked totals:
  7906. if (stackLabelOptions && stackLabelOptions.enabled) {
  7907. axis.renderStackTotals();
  7908. }
  7909. // End stacked totals
  7910. axis.isDirty = false;
  7911. },
  7912. /**
  7913. * Redraw the axis to reflect changes in the data or axis extremes
  7914. */
  7915. redraw: function () {
  7916. if (this.visible) {
  7917. // render the axis
  7918. this.render();
  7919. // move plot lines and bands
  7920. each(this.plotLinesAndBands, function (plotLine) {
  7921. plotLine.render();
  7922. });
  7923. }
  7924. // mark associated series as dirty and ready for redraw
  7925. each(this.series, function (series) {
  7926. series.isDirty = true;
  7927. });
  7928. },
  7929. /**
  7930. * Destroys an Axis instance.
  7931. */
  7932. destroy: function (keepEvents) {
  7933. var axis = this,
  7934. stacks = axis.stacks,
  7935. stackKey,
  7936. plotLinesAndBands = axis.plotLinesAndBands,
  7937. i;
  7938. // Remove the events
  7939. if (!keepEvents) {
  7940. removeEvent(axis);
  7941. }
  7942. // Destroy each stack total
  7943. for (stackKey in stacks) {
  7944. destroyObjectProperties(stacks[stackKey]);
  7945. stacks[stackKey] = null;
  7946. }
  7947. // Destroy collections
  7948. each([axis.ticks, axis.minorTicks, axis.alternateBands], function (coll) {
  7949. destroyObjectProperties(coll);
  7950. });
  7951. i = plotLinesAndBands.length;
  7952. while (i--) { // #1975
  7953. plotLinesAndBands[i].destroy();
  7954. }
  7955. // Destroy local variables
  7956. each(['stackTotalGroup', 'axisLine', 'axisTitle', 'axisGroup', 'cross', 'gridGroup', 'labelGroup'], function (prop) {
  7957. if (axis[prop]) {
  7958. axis[prop] = axis[prop].destroy();
  7959. }
  7960. });
  7961. // Destroy crosshair
  7962. if (this.cross) {
  7963. this.cross.destroy();
  7964. }
  7965. },
  7966. /**
  7967. * Draw the crosshair
  7968. *
  7969. * @param {Object} e The event arguments from the modified pointer event
  7970. * @param {Object} point The Point object
  7971. */
  7972. drawCrosshair: function (e, point) {
  7973. var path,
  7974. options = this.crosshair,
  7975. pos,
  7976. attribs,
  7977. categorized,
  7978. strokeWidth;
  7979. if (
  7980. // Disabled in options
  7981. !this.crosshair ||
  7982. // Snap
  7983. ((defined(point) || !pick(options.snap, true)) === false)
  7984. ) {
  7985. this.hideCrosshair();
  7986. } else {
  7987. // Get the path
  7988. if (!pick(options.snap, true)) {
  7989. pos = (this.horiz ? e.chartX - this.pos : this.len - e.chartY + this.pos);
  7990. } else if (defined(point)) {
  7991. pos = this.isXAxis ? point.plotX : this.len - point.plotY; // #3834
  7992. }
  7993. if (this.isRadial) {
  7994. path = this.getPlotLinePath(this.isXAxis ? point.x : pick(point.stackY, point.y)) || null; // #3189
  7995. } else {
  7996. path = this.getPlotLinePath(null, null, null, null, pos) || null; // #3189
  7997. }
  7998. if (path === null) {
  7999. this.hideCrosshair();
  8000. return;
  8001. }
  8002. categorized = this.categories && !this.isRadial;
  8003. strokeWidth = pick(options.width, (categorized ? this.transA : 1));
  8004. // Draw the cross
  8005. if (this.cross) {
  8006. this.cross
  8007. .attr({
  8008. d: path,
  8009. visibility: 'visible',
  8010. 'stroke-width': strokeWidth // #4737
  8011. });
  8012. } else {
  8013. attribs = {
  8014. 'pointer-events': 'none', // #5259
  8015. 'stroke-width': strokeWidth,
  8016. stroke: options.color || (categorized ? 'rgba(155,200,255,0.2)' : '#C0C0C0'),
  8017. zIndex: pick(options.zIndex, 2)
  8018. };
  8019. if (options.dashStyle) {
  8020. attribs.dashstyle = options.dashStyle;
  8021. }
  8022. this.cross = this.chart.renderer.path(path).attr(attribs).add();
  8023. }
  8024. }
  8025. },
  8026. /**
  8027. * Hide the crosshair.
  8028. */
  8029. hideCrosshair: function () {
  8030. if (this.cross) {
  8031. this.cross.hide();
  8032. }
  8033. }
  8034. }; // end Axis
  8035. extend(Axis.prototype, AxisPlotLineOrBandExtension);
  8036. /**
  8037. * Set the tick positions to a time unit that makes sense, for example
  8038. * on the first of each month or on every Monday. Return an array
  8039. * with the time positions. Used in datetime axes as well as for grouping
  8040. * data on a datetime axis.
  8041. *
  8042. * @param {Object} normalizedInterval The interval in axis values (ms) and the count
  8043. * @param {Number} min The minimum in axis values
  8044. * @param {Number} max The maximum in axis values
  8045. * @param {Number} startOfWeek
  8046. */
  8047. Axis.prototype.getTimeTicks = function (normalizedInterval, min, max, startOfWeek) {
  8048. var tickPositions = [],
  8049. i,
  8050. higherRanks = {},
  8051. useUTC = defaultOptions.global.useUTC,
  8052. minYear, // used in months and years as a basis for Date.UTC()
  8053. minDate = new Date(min - getTZOffset(min)),
  8054. interval = normalizedInterval.unitRange,
  8055. count = normalizedInterval.count;
  8056. if (defined(min)) { // #1300
  8057. minDate[setMilliseconds](interval >= timeUnits.second ? 0 : // #3935
  8058. count * mathFloor(minDate.getMilliseconds() / count)); // #3652, #3654
  8059. if (interval >= timeUnits.second) { // second
  8060. minDate[setSeconds](interval >= timeUnits.minute ? 0 : // #3935
  8061. count * mathFloor(minDate.getSeconds() / count));
  8062. }
  8063. if (interval >= timeUnits.minute) { // minute
  8064. minDate[setMinutes](interval >= timeUnits.hour ? 0 :
  8065. count * mathFloor(minDate[getMinutes]() / count));
  8066. }
  8067. if (interval >= timeUnits.hour) { // hour
  8068. minDate[setHours](interval >= timeUnits.day ? 0 :
  8069. count * mathFloor(minDate[getHours]() / count));
  8070. }
  8071. if (interval >= timeUnits.day) { // day
  8072. minDate[setDate](interval >= timeUnits.month ? 1 :
  8073. count * mathFloor(minDate[getDate]() / count));
  8074. }
  8075. if (interval >= timeUnits.month) { // month
  8076. minDate[setMonth](interval >= timeUnits.year ? 0 :
  8077. count * mathFloor(minDate[getMonth]() / count));
  8078. minYear = minDate[getFullYear]();
  8079. }
  8080. if (interval >= timeUnits.year) { // year
  8081. minYear -= minYear % count;
  8082. minDate[setFullYear](minYear);
  8083. }
  8084. // week is a special case that runs outside the hierarchy
  8085. if (interval === timeUnits.week) {
  8086. // get start of current week, independent of count
  8087. minDate[setDate](minDate[getDate]() - minDate[getDay]() +
  8088. pick(startOfWeek, 1));
  8089. }
  8090. // get tick positions
  8091. i = 1;
  8092. if (timezoneOffset || getTimezoneOffset) {
  8093. minDate = minDate.getTime();
  8094. minDate = new Date(minDate + getTZOffset(minDate));
  8095. }
  8096. minYear = minDate[getFullYear]();
  8097. var time = minDate.getTime(),
  8098. minMonth = minDate[getMonth](),
  8099. minDateDate = minDate[getDate](),
  8100. variableDayLength = !useUTC || !!getTimezoneOffset, // #4951
  8101. localTimezoneOffset = (timeUnits.day +
  8102. (useUTC ? getTZOffset(minDate) : minDate.getTimezoneOffset() * 60 * 1000)
  8103. ) % timeUnits.day; // #950, #3359
  8104. // iterate and add tick positions at appropriate values
  8105. while (time < max) {
  8106. tickPositions.push(time);
  8107. // if the interval is years, use Date.UTC to increase years
  8108. if (interval === timeUnits.year) {
  8109. time = makeTime(minYear + i * count, 0);
  8110. // if the interval is months, use Date.UTC to increase months
  8111. } else if (interval === timeUnits.month) {
  8112. time = makeTime(minYear, minMonth + i * count);
  8113. // if we're using global time, the interval is not fixed as it jumps
  8114. // one hour at the DST crossover
  8115. } else if (variableDayLength && (interval === timeUnits.day || interval === timeUnits.week)) {
  8116. time = makeTime(minYear, minMonth, minDateDate +
  8117. i * count * (interval === timeUnits.day ? 1 : 7));
  8118. // else, the interval is fixed and we use simple addition
  8119. } else {
  8120. time += interval * count;
  8121. }
  8122. i++;
  8123. }
  8124. // push the last time
  8125. tickPositions.push(time);
  8126. // mark new days if the time is dividible by day (#1649, #1760)
  8127. each(grep(tickPositions, function (time) {
  8128. return interval <= timeUnits.hour && time % timeUnits.day === localTimezoneOffset;
  8129. }), function (time) {
  8130. higherRanks[time] = 'day';
  8131. });
  8132. }
  8133. // record information on the chosen unit - for dynamic label formatter
  8134. tickPositions.info = extend(normalizedInterval, {
  8135. higherRanks: higherRanks,
  8136. totalRange: interval * count
  8137. });
  8138. return tickPositions;
  8139. };
  8140. /**
  8141. * Get a normalized tick interval for dates. Returns a configuration object with
  8142. * unit range (interval), count and name. Used to prepare data for getTimeTicks.
  8143. * Previously this logic was part of getTimeTicks, but as getTimeTicks now runs
  8144. * of segments in stock charts, the normalizing logic was extracted in order to
  8145. * prevent it for running over again for each segment having the same interval.
  8146. * #662, #697.
  8147. */
  8148. Axis.prototype.normalizeTimeTickInterval = function (tickInterval, unitsOption) {
  8149. var units = unitsOption || [[
  8150. 'millisecond', // unit name
  8151. [1, 2, 5, 10, 20, 25, 50, 100, 200, 500] // allowed multiples
  8152. ], [
  8153. 'second',
  8154. [1, 2, 5, 10, 15, 30]
  8155. ], [
  8156. 'minute',
  8157. [1, 2, 5, 10, 15, 30]
  8158. ], [
  8159. 'hour',
  8160. [1, 2, 3, 4, 6, 8, 12]
  8161. ], [
  8162. 'day',
  8163. [1, 2]
  8164. ], [
  8165. 'week',
  8166. [1, 2]
  8167. ], [
  8168. 'month',
  8169. [1, 2, 3, 4, 6]
  8170. ], [
  8171. 'year',
  8172. null
  8173. ]],
  8174. unit = units[units.length - 1], // default unit is years
  8175. interval = timeUnits[unit[0]],
  8176. multiples = unit[1],
  8177. count,
  8178. i;
  8179. // loop through the units to find the one that best fits the tickInterval
  8180. for (i = 0; i < units.length; i++) {
  8181. unit = units[i];
  8182. interval = timeUnits[unit[0]];
  8183. multiples = unit[1];
  8184. if (units[i + 1]) {
  8185. // lessThan is in the middle between the highest multiple and the next unit.
  8186. var lessThan = (interval * multiples[multiples.length - 1] +
  8187. timeUnits[units[i + 1][0]]) / 2;
  8188. // break and keep the current unit
  8189. if (tickInterval <= lessThan) {
  8190. break;
  8191. }
  8192. }
  8193. }
  8194. // prevent 2.5 years intervals, though 25, 250 etc. are allowed
  8195. if (interval === timeUnits.year && tickInterval < 5 * interval) {
  8196. multiples = [1, 2, 5];
  8197. }
  8198. // get the count
  8199. count = normalizeTickInterval(
  8200. tickInterval / interval,
  8201. multiples,
  8202. unit[0] === 'year' ? mathMax(getMagnitude(tickInterval / interval), 1) : 1 // #1913, #2360
  8203. );
  8204. return {
  8205. unitRange: interval,
  8206. count: count,
  8207. unitName: unit[0]
  8208. };
  8209. };
  8210. /**
  8211. * Methods defined on the Axis prototype
  8212. */
  8213. /**
  8214. * Set the tick positions of a logarithmic axis
  8215. */
  8216. Axis.prototype.getLogTickPositions = function (interval, min, max, minor) {
  8217. var axis = this,
  8218. options = axis.options,
  8219. axisLength = axis.len,
  8220. lin2log = axis.lin2log,
  8221. log2lin = axis.log2lin,
  8222. // Since we use this method for both major and minor ticks,
  8223. // use a local variable and return the result
  8224. positions = [];
  8225. // Reset
  8226. if (!minor) {
  8227. axis._minorAutoInterval = null;
  8228. }
  8229. // First case: All ticks fall on whole logarithms: 1, 10, 100 etc.
  8230. if (interval >= 0.5) {
  8231. interval = mathRound(interval);
  8232. positions = axis.getLinearTickPositions(interval, min, max);
  8233. // Second case: We need intermediary ticks. For example
  8234. // 1, 2, 4, 6, 8, 10, 20, 40 etc.
  8235. } else if (interval >= 0.08) {
  8236. var roundedMin = mathFloor(min),
  8237. intermediate,
  8238. i,
  8239. j,
  8240. len,
  8241. pos,
  8242. lastPos,
  8243. break2;
  8244. if (interval > 0.3) {
  8245. intermediate = [1, 2, 4];
  8246. } else if (interval > 0.15) { // 0.2 equals five minor ticks per 1, 10, 100 etc
  8247. intermediate = [1, 2, 4, 6, 8];
  8248. } else { // 0.1 equals ten minor ticks per 1, 10, 100 etc
  8249. intermediate = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  8250. }
  8251. for (i = roundedMin; i < max + 1 && !break2; i++) {
  8252. len = intermediate.length;
  8253. for (j = 0; j < len && !break2; j++) {
  8254. pos = log2lin(lin2log(i) * intermediate[j]);
  8255. if (pos > min && (!minor || lastPos <= max) && lastPos !== UNDEFINED) { // #1670, lastPos is #3113
  8256. positions.push(lastPos);
  8257. }
  8258. if (lastPos > max) {
  8259. break2 = true;
  8260. }
  8261. lastPos = pos;
  8262. }
  8263. }
  8264. // Third case: We are so deep in between whole logarithmic values that
  8265. // we might as well handle the tick positions like a linear axis. For
  8266. // example 1.01, 1.02, 1.03, 1.04.
  8267. } else {
  8268. var realMin = lin2log(min),
  8269. realMax = lin2log(max),
  8270. tickIntervalOption = options[minor ? 'minorTickInterval' : 'tickInterval'],
  8271. filteredTickIntervalOption = tickIntervalOption === 'auto' ? null : tickIntervalOption,
  8272. tickPixelIntervalOption = options.tickPixelInterval / (minor ? 5 : 1),
  8273. totalPixelLength = minor ? axisLength / axis.tickPositions.length : axisLength;
  8274. interval = pick(
  8275. filteredTickIntervalOption,
  8276. axis._minorAutoInterval,
  8277. (realMax - realMin) * tickPixelIntervalOption / (totalPixelLength || 1)
  8278. );
  8279. interval = normalizeTickInterval(
  8280. interval,
  8281. null,
  8282. getMagnitude(interval)
  8283. );
  8284. positions = map(axis.getLinearTickPositions(
  8285. interval,
  8286. realMin,
  8287. realMax
  8288. ), log2lin);
  8289. if (!minor) {
  8290. axis._minorAutoInterval = interval / 5;
  8291. }
  8292. }
  8293. // Set the axis-level tickInterval variable
  8294. if (!minor) {
  8295. axis.tickInterval = interval;
  8296. }
  8297. return positions;
  8298. };
  8299. Axis.prototype.log2lin = function (num) {
  8300. return math.log(num) / math.LN10;
  8301. };
  8302. Axis.prototype.lin2log = function (num) {
  8303. return math.pow(10, num);
  8304. };
  8305. /**
  8306. * The tooltip object
  8307. * @param {Object} chart The chart instance
  8308. * @param {Object} options Tooltip options
  8309. */
  8310. var Tooltip = Highcharts.Tooltip = function () {
  8311. this.init.apply(this, arguments);
  8312. };
  8313. Tooltip.prototype = {
  8314. init: function (chart, options) {
  8315. var borderWidth = options.borderWidth,
  8316. style = options.style,
  8317. padding = pInt(style.padding);
  8318. // Save the chart and options
  8319. this.chart = chart;
  8320. this.options = options;
  8321. // Keep track of the current series
  8322. //this.currentSeries = UNDEFINED;
  8323. // List of crosshairs
  8324. this.crosshairs = [];
  8325. // Current values of x and y when animating
  8326. this.now = { x: 0, y: 0 };
  8327. // The tooltip is initially hidden
  8328. this.isHidden = true;
  8329. // create the label
  8330. this.label = chart.renderer.label('', 0, 0, options.shape || 'callout', null, null, options.useHTML, null, 'tooltip')
  8331. .attr({
  8332. padding: padding,
  8333. fill: options.backgroundColor,
  8334. 'stroke-width': borderWidth,
  8335. r: options.borderRadius,
  8336. zIndex: 8
  8337. })
  8338. .css(style)
  8339. .css({ padding: 0 }) // Remove it from VML, the padding is applied as an attribute instead (#1117)
  8340. .add()
  8341. .attr({ y: -9999 }); // #2301, #2657
  8342. // When using canVG the shadow shows up as a gray circle
  8343. // even if the tooltip is hidden.
  8344. if (!useCanVG) {
  8345. this.label.shadow(options.shadow);
  8346. }
  8347. // Public property for getting the shared state.
  8348. this.shared = options.shared;
  8349. },
  8350. /**
  8351. * Destroy the tooltip and its elements.
  8352. */
  8353. destroy: function () {
  8354. // Destroy and clear local variables
  8355. if (this.label) {
  8356. this.label = this.label.destroy();
  8357. }
  8358. clearTimeout(this.hideTimer);
  8359. clearTimeout(this.tooltipTimeout);
  8360. },
  8361. /**
  8362. * Provide a soft movement for the tooltip
  8363. *
  8364. * @param {Number} x
  8365. * @param {Number} y
  8366. * @private
  8367. */
  8368. move: function (x, y, anchorX, anchorY) {
  8369. var tooltip = this,
  8370. now = tooltip.now,
  8371. animate = tooltip.options.animation !== false && !tooltip.isHidden &&
  8372. // When we get close to the target position, abort animation and land on the right place (#3056)
  8373. (mathAbs(x - now.x) > 1 || mathAbs(y - now.y) > 1),
  8374. skipAnchor = tooltip.followPointer || tooltip.len > 1;
  8375. // Get intermediate values for animation
  8376. extend(now, {
  8377. x: animate ? (2 * now.x + x) / 3 : x,
  8378. y: animate ? (now.y + y) / 2 : y,
  8379. anchorX: skipAnchor ? UNDEFINED : animate ? (2 * now.anchorX + anchorX) / 3 : anchorX,
  8380. anchorY: skipAnchor ? UNDEFINED : animate ? (now.anchorY + anchorY) / 2 : anchorY
  8381. });
  8382. // Move to the intermediate value
  8383. tooltip.label.attr(now);
  8384. // Run on next tick of the mouse tracker
  8385. if (animate) {
  8386. // Never allow two timeouts
  8387. clearTimeout(this.tooltipTimeout);
  8388. // Set the fixed interval ticking for the smooth tooltip
  8389. this.tooltipTimeout = setTimeout(function () {
  8390. // The interval function may still be running during destroy, so check that the chart is really there before calling.
  8391. if (tooltip) {
  8392. tooltip.move(x, y, anchorX, anchorY);
  8393. }
  8394. }, 32);
  8395. }
  8396. },
  8397. /**
  8398. * Hide the tooltip
  8399. */
  8400. hide: function (delay) {
  8401. var tooltip = this;
  8402. clearTimeout(this.hideTimer); // disallow duplicate timers (#1728, #1766)
  8403. delay = pick(delay, this.options.hideDelay, 500);
  8404. if (!this.isHidden) {
  8405. this.hideTimer = syncTimeout(function () {
  8406. tooltip.label[delay ? 'fadeOut' : 'hide']();
  8407. tooltip.isHidden = true;
  8408. }, delay);
  8409. }
  8410. },
  8411. /**
  8412. * Extendable method to get the anchor position of the tooltip
  8413. * from a point or set of points
  8414. */
  8415. getAnchor: function (points, mouseEvent) {
  8416. var ret,
  8417. chart = this.chart,
  8418. inverted = chart.inverted,
  8419. plotTop = chart.plotTop,
  8420. plotLeft = chart.plotLeft,
  8421. plotX = 0,
  8422. plotY = 0,
  8423. yAxis,
  8424. xAxis;
  8425. points = splat(points);
  8426. // Pie uses a special tooltipPos
  8427. ret = points[0].tooltipPos;
  8428. // When tooltip follows mouse, relate the position to the mouse
  8429. if (this.followPointer && mouseEvent) {
  8430. if (mouseEvent.chartX === UNDEFINED) {
  8431. mouseEvent = chart.pointer.normalize(mouseEvent);
  8432. }
  8433. ret = [
  8434. mouseEvent.chartX - chart.plotLeft,
  8435. mouseEvent.chartY - plotTop
  8436. ];
  8437. }
  8438. // When shared, use the average position
  8439. if (!ret) {
  8440. each(points, function (point) {
  8441. yAxis = point.series.yAxis;
  8442. xAxis = point.series.xAxis;
  8443. plotX += point.plotX + (!inverted && xAxis ? xAxis.left - plotLeft : 0);
  8444. plotY += (point.plotLow ? (point.plotLow + point.plotHigh) / 2 : point.plotY) +
  8445. (!inverted && yAxis ? yAxis.top - plotTop : 0); // #1151
  8446. });
  8447. plotX /= points.length;
  8448. plotY /= points.length;
  8449. ret = [
  8450. inverted ? chart.plotWidth - plotY : plotX,
  8451. this.shared && !inverted && points.length > 1 && mouseEvent ?
  8452. mouseEvent.chartY - plotTop : // place shared tooltip next to the mouse (#424)
  8453. inverted ? chart.plotHeight - plotX : plotY
  8454. ];
  8455. }
  8456. return map(ret, mathRound);
  8457. },
  8458. /**
  8459. * Place the tooltip in a chart without spilling over
  8460. * and not covering the point it self.
  8461. */
  8462. getPosition: function (boxWidth, boxHeight, point) {
  8463. var chart = this.chart,
  8464. distance = this.distance,
  8465. ret = {},
  8466. h = point.h || 0, // #4117
  8467. swapped,
  8468. first = ['y', chart.chartHeight, boxHeight, point.plotY + chart.plotTop, chart.plotTop, chart.plotTop + chart.plotHeight],
  8469. second = ['x', chart.chartWidth, boxWidth, point.plotX + chart.plotLeft, chart.plotLeft, chart.plotLeft + chart.plotWidth],
  8470. // The far side is right or bottom
  8471. preferFarSide = !this.followPointer && pick(point.ttBelow, !chart.inverted === !!point.negative), // #4984
  8472. /**
  8473. * Handle the preferred dimension. When the preferred dimension is tooltip
  8474. * on top or bottom of the point, it will look for space there.
  8475. */
  8476. firstDimension = function (dim, outerSize, innerSize, point, min, max) {
  8477. var roomLeft = innerSize < point - distance,
  8478. roomRight = point + distance + innerSize < outerSize,
  8479. alignedLeft = point - distance - innerSize,
  8480. alignedRight = point + distance;
  8481. if (preferFarSide && roomRight) {
  8482. ret[dim] = alignedRight;
  8483. } else if (!preferFarSide && roomLeft) {
  8484. ret[dim] = alignedLeft;
  8485. } else if (roomLeft) {
  8486. ret[dim] = mathMin(max - innerSize, alignedLeft - h < 0 ? alignedLeft : alignedLeft - h);
  8487. } else if (roomRight) {
  8488. ret[dim] = mathMax(min, alignedRight + h + innerSize > outerSize ? alignedRight : alignedRight + h);
  8489. } else {
  8490. return false;
  8491. }
  8492. },
  8493. /**
  8494. * Handle the secondary dimension. If the preferred dimension is tooltip
  8495. * on top or bottom of the point, the second dimension is to align the tooltip
  8496. * above the point, trying to align center but allowing left or right
  8497. * align within the chart box.
  8498. */
  8499. secondDimension = function (dim, outerSize, innerSize, point) {
  8500. var retVal;
  8501. // Too close to the edge, return false and swap dimensions
  8502. if (point < distance || point > outerSize - distance) {
  8503. retVal = false;
  8504. // Align left/top
  8505. } else if (point < innerSize / 2) {
  8506. ret[dim] = 1;
  8507. // Align right/bottom
  8508. } else if (point > outerSize - innerSize / 2) {
  8509. ret[dim] = outerSize - innerSize - 2;
  8510. // Align center
  8511. } else {
  8512. ret[dim] = point - innerSize / 2;
  8513. }
  8514. return retVal;
  8515. },
  8516. /**
  8517. * Swap the dimensions
  8518. */
  8519. swap = function (count) {
  8520. var temp = first;
  8521. first = second;
  8522. second = temp;
  8523. swapped = count;
  8524. },
  8525. run = function () {
  8526. if (firstDimension.apply(0, first) !== false) {
  8527. if (secondDimension.apply(0, second) === false && !swapped) {
  8528. swap(true);
  8529. run();
  8530. }
  8531. } else if (!swapped) {
  8532. swap(true);
  8533. run();
  8534. } else {
  8535. ret.x = ret.y = 0;
  8536. }
  8537. };
  8538. // Under these conditions, prefer the tooltip on the side of the point
  8539. if (chart.inverted || this.len > 1) {
  8540. swap();
  8541. }
  8542. run();
  8543. return ret;
  8544. },
  8545. /**
  8546. * In case no user defined formatter is given, this will be used. Note that the context
  8547. * here is an object holding point, series, x, y etc.
  8548. */
  8549. defaultFormatter: function (tooltip) {
  8550. var items = this.points || splat(this),
  8551. s;
  8552. // build the header
  8553. s = [tooltip.tooltipFooterHeaderFormatter(items[0])]; //#3397: abstraction to enable formatting of footer and header
  8554. // build the values
  8555. s = s.concat(tooltip.bodyFormatter(items));
  8556. // footer
  8557. s.push(tooltip.tooltipFooterHeaderFormatter(items[0], true)); //#3397: abstraction to enable formatting of footer and header
  8558. return s.join('');
  8559. },
  8560. /**
  8561. * Refresh the tooltip's text and position.
  8562. * @param {Object} point
  8563. */
  8564. refresh: function (point, mouseEvent) {
  8565. var tooltip = this,
  8566. chart = tooltip.chart,
  8567. label = tooltip.label,
  8568. options = tooltip.options,
  8569. x,
  8570. y,
  8571. anchor,
  8572. textConfig = {},
  8573. text,
  8574. pointConfig = [],
  8575. formatter = options.formatter || tooltip.defaultFormatter,
  8576. hoverPoints = chart.hoverPoints,
  8577. borderColor,
  8578. shared = tooltip.shared,
  8579. currentSeries;
  8580. clearTimeout(this.hideTimer);
  8581. // get the reference point coordinates (pie charts use tooltipPos)
  8582. tooltip.followPointer = splat(point)[0].series.tooltipOptions.followPointer;
  8583. anchor = tooltip.getAnchor(point, mouseEvent);
  8584. x = anchor[0];
  8585. y = anchor[1];
  8586. // shared tooltip, array is sent over
  8587. if (shared && !(point.series && point.series.noSharedTooltip)) {
  8588. // hide previous hoverPoints and set new
  8589. chart.hoverPoints = point;
  8590. if (hoverPoints) {
  8591. each(hoverPoints, function (point) {
  8592. point.setState();
  8593. });
  8594. }
  8595. each(point, function (item) {
  8596. item.setState(HOVER_STATE);
  8597. pointConfig.push(item.getLabelConfig());
  8598. });
  8599. textConfig = {
  8600. x: point[0].category,
  8601. y: point[0].y
  8602. };
  8603. textConfig.points = pointConfig;
  8604. this.len = pointConfig.length;
  8605. point = point[0];
  8606. // single point tooltip
  8607. } else {
  8608. textConfig = point.getLabelConfig();
  8609. }
  8610. text = formatter.call(textConfig, tooltip);
  8611. // register the current series
  8612. currentSeries = point.series;
  8613. this.distance = pick(currentSeries.tooltipOptions.distance, 16);
  8614. // update the inner HTML
  8615. if (text === false) {
  8616. this.hide();
  8617. } else {
  8618. // show it
  8619. if (tooltip.isHidden) {
  8620. stop(label);
  8621. label.attr('opacity', 1).show();
  8622. }
  8623. // update text
  8624. label.attr({
  8625. text: text
  8626. });
  8627. // set the stroke color of the box
  8628. borderColor = options.borderColor || point.color || currentSeries.color || '#606060';
  8629. label.attr({
  8630. stroke: borderColor
  8631. });
  8632. tooltip.updatePosition({
  8633. plotX: x,
  8634. plotY: y,
  8635. negative: point.negative,
  8636. ttBelow: point.ttBelow,
  8637. h: anchor[2] || 0
  8638. });
  8639. this.isHidden = false;
  8640. }
  8641. fireEvent(chart, 'tooltipRefresh', {
  8642. text: text,
  8643. x: x + chart.plotLeft,
  8644. y: y + chart.plotTop,
  8645. borderColor: borderColor
  8646. });
  8647. },
  8648. /**
  8649. * Find the new position and perform the move
  8650. */
  8651. updatePosition: function (point) {
  8652. var chart = this.chart,
  8653. label = this.label,
  8654. pos = (this.options.positioner || this.getPosition).call(
  8655. this,
  8656. label.width,
  8657. label.height,
  8658. point
  8659. );
  8660. // do the move
  8661. this.move(
  8662. mathRound(pos.x),
  8663. mathRound(pos.y || 0), // can be undefined (#3977)
  8664. point.plotX + chart.plotLeft,
  8665. point.plotY + chart.plotTop
  8666. );
  8667. },
  8668. /**
  8669. * Get the best X date format based on the closest point range on the axis.
  8670. */
  8671. getXDateFormat: function (point, options, xAxis) {
  8672. var xDateFormat,
  8673. dateTimeLabelFormats = options.dateTimeLabelFormats,
  8674. closestPointRange = xAxis && xAxis.closestPointRange,
  8675. n,
  8676. blank = '01-01 00:00:00.000',
  8677. strpos = {
  8678. millisecond: 15,
  8679. second: 12,
  8680. minute: 9,
  8681. hour: 6,
  8682. day: 3
  8683. },
  8684. date,
  8685. lastN = 'millisecond'; // for sub-millisecond data, #4223
  8686. if (closestPointRange) {
  8687. date = dateFormat('%m-%d %H:%M:%S.%L', point.x);
  8688. for (n in timeUnits) {
  8689. // If the range is exactly one week and we're looking at a Sunday/Monday, go for the week format
  8690. if (closestPointRange === timeUnits.week && +dateFormat('%w', point.x) === xAxis.options.startOfWeek &&
  8691. date.substr(6) === blank.substr(6)) {
  8692. n = 'week';
  8693. break;
  8694. }
  8695. // The first format that is too great for the range
  8696. if (timeUnits[n] > closestPointRange) {
  8697. n = lastN;
  8698. break;
  8699. }
  8700. // If the point is placed every day at 23:59, we need to show
  8701. // the minutes as well. #2637.
  8702. if (strpos[n] && date.substr(strpos[n]) !== blank.substr(strpos[n])) {
  8703. break;
  8704. }
  8705. // Weeks are outside the hierarchy, only apply them on Mondays/Sundays like in the first condition
  8706. if (n !== 'week') {
  8707. lastN = n;
  8708. }
  8709. }
  8710. if (n) {
  8711. xDateFormat = dateTimeLabelFormats[n];
  8712. }
  8713. } else {
  8714. xDateFormat = dateTimeLabelFormats.day;
  8715. }
  8716. return xDateFormat || dateTimeLabelFormats.year; // #2546, 2581
  8717. },
  8718. /**
  8719. * Format the footer/header of the tooltip
  8720. * #3397: abstraction to enable formatting of footer and header
  8721. */
  8722. tooltipFooterHeaderFormatter: function (point, isFooter) {
  8723. var footOrHead = isFooter ? 'footer' : 'header',
  8724. series = point.series,
  8725. tooltipOptions = series.tooltipOptions,
  8726. xDateFormat = tooltipOptions.xDateFormat,
  8727. xAxis = series.xAxis,
  8728. isDateTime = xAxis && xAxis.options.type === 'datetime' && isNumber(point.key),
  8729. formatString = tooltipOptions[footOrHead + 'Format'];
  8730. // Guess the best date format based on the closest point distance (#568, #3418)
  8731. if (isDateTime && !xDateFormat) {
  8732. xDateFormat = this.getXDateFormat(point, tooltipOptions, xAxis);
  8733. }
  8734. // Insert the footer date format if any
  8735. if (isDateTime && xDateFormat) {
  8736. formatString = formatString.replace('{point.key}', '{point.key:' + xDateFormat + '}');
  8737. }
  8738. return format(formatString, {
  8739. point: point,
  8740. series: series
  8741. });
  8742. },
  8743. /**
  8744. * Build the body (lines) of the tooltip by iterating over the items and returning one entry for each item,
  8745. * abstracting this functionality allows to easily overwrite and extend it.
  8746. */
  8747. bodyFormatter: function (items) {
  8748. return map(items, function (item) {
  8749. var tooltipOptions = item.series.tooltipOptions;
  8750. return (tooltipOptions.pointFormatter || item.point.tooltipFormatter).call(item.point, tooltipOptions.pointFormat);
  8751. });
  8752. }
  8753. };
  8754. var hoverChartIndex;
  8755. // Global flag for touch support
  8756. hasTouch = doc && doc.documentElement.ontouchstart !== UNDEFINED;
  8757. /**
  8758. * The mouse tracker object. All methods starting with "on" are primary DOM event handlers.
  8759. * Subsequent methods should be named differently from what they are doing.
  8760. * @param {Object} chart The Chart instance
  8761. * @param {Object} options The root options object
  8762. */
  8763. var Pointer = Highcharts.Pointer = function (chart, options) {
  8764. this.init(chart, options);
  8765. };
  8766. Pointer.prototype = {
  8767. /**
  8768. * Initialize Pointer
  8769. */
  8770. init: function (chart, options) {
  8771. var chartOptions = options.chart,
  8772. chartEvents = chartOptions.events,
  8773. zoomType = useCanVG ? '' : chartOptions.zoomType,
  8774. inverted = chart.inverted,
  8775. zoomX,
  8776. zoomY;
  8777. // Store references
  8778. this.options = options;
  8779. this.chart = chart;
  8780. // Zoom status
  8781. this.zoomX = zoomX = /x/.test(zoomType);
  8782. this.zoomY = zoomY = /y/.test(zoomType);
  8783. this.zoomHor = (zoomX && !inverted) || (zoomY && inverted);
  8784. this.zoomVert = (zoomY && !inverted) || (zoomX && inverted);
  8785. this.hasZoom = zoomX || zoomY;
  8786. // Do we need to handle click on a touch device?
  8787. this.runChartClick = chartEvents && !!chartEvents.click;
  8788. this.pinchDown = [];
  8789. this.lastValidTouch = {};
  8790. if (Highcharts.Tooltip && options.tooltip.enabled) {
  8791. chart.tooltip = new Tooltip(chart, options.tooltip);
  8792. this.followTouchMove = pick(options.tooltip.followTouchMove, true);
  8793. }
  8794. this.setDOMEvents();
  8795. },
  8796. /**
  8797. * Add crossbrowser support for chartX and chartY
  8798. * @param {Object} e The event object in standard browsers
  8799. */
  8800. normalize: function (e, chartPosition) {
  8801. var chartX,
  8802. chartY,
  8803. ePos;
  8804. // IE normalizing
  8805. e = e || win.event;
  8806. if (!e.target) {
  8807. e.target = e.srcElement;
  8808. }
  8809. // iOS (#2757)
  8810. ePos = e.touches ? (e.touches.length ? e.touches.item(0) : e.changedTouches[0]) : e;
  8811. // Get mouse position
  8812. if (!chartPosition) {
  8813. this.chartPosition = chartPosition = offset(this.chart.container);
  8814. }
  8815. // chartX and chartY
  8816. if (ePos.pageX === UNDEFINED) { // IE < 9. #886.
  8817. chartX = mathMax(e.x, e.clientX - chartPosition.left); // #2005, #2129: the second case is
  8818. // for IE10 quirks mode within framesets
  8819. chartY = e.y;
  8820. } else {
  8821. chartX = ePos.pageX - chartPosition.left;
  8822. chartY = ePos.pageY - chartPosition.top;
  8823. }
  8824. return extend(e, {
  8825. chartX: mathRound(chartX),
  8826. chartY: mathRound(chartY)
  8827. });
  8828. },
  8829. /**
  8830. * Get the click position in terms of axis values.
  8831. *
  8832. * @param {Object} e A pointer event
  8833. */
  8834. getCoordinates: function (e) {
  8835. var coordinates = {
  8836. xAxis: [],
  8837. yAxis: []
  8838. };
  8839. each(this.chart.axes, function (axis) {
  8840. coordinates[axis.isXAxis ? 'xAxis' : 'yAxis'].push({
  8841. axis: axis,
  8842. value: axis.toValue(e[axis.horiz ? 'chartX' : 'chartY'])
  8843. });
  8844. });
  8845. return coordinates;
  8846. },
  8847. /**
  8848. * With line type charts with a single tracker, get the point closest to the mouse.
  8849. * Run Point.onMouseOver and display tooltip for the point or points.
  8850. */
  8851. runPointActions: function (e) {
  8852. var pointer = this,
  8853. chart = pointer.chart,
  8854. series = chart.series,
  8855. tooltip = chart.tooltip,
  8856. shared = tooltip ? tooltip.shared : false,
  8857. followPointer,
  8858. hoverPoint = chart.hoverPoint,
  8859. hoverSeries = chart.hoverSeries,
  8860. i,
  8861. distance = [Number.MAX_VALUE, Number.MAX_VALUE], // #4511
  8862. anchor,
  8863. noSharedTooltip,
  8864. stickToHoverSeries,
  8865. directTouch,
  8866. kdpoints = [],
  8867. kdpoint = [],
  8868. kdpointT;
  8869. // For hovering over the empty parts of the plot area (hoverSeries is undefined).
  8870. // If there is one series with point tracking (combo chart), don't go to nearest neighbour.
  8871. if (!shared && !hoverSeries) {
  8872. for (i = 0; i < series.length; i++) {
  8873. if (series[i].directTouch || !series[i].options.stickyTracking) {
  8874. series = [];
  8875. }
  8876. }
  8877. }
  8878. // If it has a hoverPoint and that series requires direct touch (like columns, #3899), or we're on
  8879. // a noSharedTooltip series among shared tooltip series (#4546), use the hoverPoint . Otherwise,
  8880. // search the k-d tree.
  8881. stickToHoverSeries = hoverSeries && (shared ? hoverSeries.noSharedTooltip : hoverSeries.directTouch);
  8882. if (stickToHoverSeries && hoverPoint) {
  8883. kdpoint = [hoverPoint];
  8884. // Handle shared tooltip or cases where a series is not yet hovered
  8885. } else {
  8886. // Find nearest points on all series
  8887. each(series, function (s) {
  8888. // Skip hidden series
  8889. noSharedTooltip = s.noSharedTooltip && shared;
  8890. directTouch = !shared && s.directTouch;
  8891. if (s.visible && !noSharedTooltip && !directTouch && pick(s.options.enableMouseTracking, true)) { // #3821
  8892. kdpointT = s.searchPoint(e, !noSharedTooltip && s.kdDimensions === 1); // #3828
  8893. if (kdpointT) {
  8894. kdpoints.push(kdpointT);
  8895. }
  8896. }
  8897. });
  8898. // Find absolute nearest point
  8899. each(kdpoints, function (p) {
  8900. if (p) {
  8901. // Store both closest points, using point.dist and point.distX comparisons (#4645):
  8902. each(['dist', 'distX'], function (dist, k) {
  8903. if (isNumber(p[dist])) {
  8904. var
  8905. // It is closer than the reference point
  8906. isCloser = p[dist] < distance[k],
  8907. // It is equally close, but above the reference point (#4679)
  8908. isAbove = p[dist] === distance[k] && p.series.group.zIndex >= kdpoint[k].series.group.zIndex;
  8909. if (isCloser || isAbove) {
  8910. distance[k] = p[dist];
  8911. kdpoint[k] = p;
  8912. }
  8913. }
  8914. });
  8915. }
  8916. });
  8917. }
  8918. // Remove points with different x-positions, required for shared tooltip and crosshairs (#4645):
  8919. if (shared) {
  8920. i = kdpoints.length;
  8921. while (i--) {
  8922. if (kdpoints[i].clientX !== kdpoint[1].clientX || kdpoints[i].series.noSharedTooltip) {
  8923. kdpoints.splice(i, 1);
  8924. }
  8925. }
  8926. }
  8927. // Refresh tooltip for kdpoint if new hover point or tooltip was hidden // #3926, #4200
  8928. if (kdpoint[0] && (kdpoint[0] !== this.prevKDPoint || (tooltip && tooltip.isHidden))) {
  8929. // Draw tooltip if necessary
  8930. if (shared && !kdpoint[0].series.noSharedTooltip) {
  8931. if (kdpoints.length && tooltip) {
  8932. tooltip.refresh(kdpoints, e);
  8933. }
  8934. // Do mouseover on all points (#3919, #3985, #4410)
  8935. each(kdpoints, function (point) {
  8936. point.onMouseOver(e, point !== ((hoverSeries && hoverSeries.directTouch && hoverPoint) || kdpoint[0]));
  8937. });
  8938. this.prevKDPoint = kdpoint[1];
  8939. } else {
  8940. if (tooltip) {
  8941. tooltip.refresh(kdpoint[0], e);
  8942. }
  8943. if (!hoverSeries || !hoverSeries.directTouch) { // #4448
  8944. kdpoint[0].onMouseOver(e);
  8945. }
  8946. this.prevKDPoint = kdpoint[0];
  8947. }
  8948. // Update positions (regardless of kdpoint or hoverPoint)
  8949. } else {
  8950. followPointer = hoverSeries && hoverSeries.tooltipOptions.followPointer;
  8951. if (tooltip && followPointer && !tooltip.isHidden) {
  8952. anchor = tooltip.getAnchor([{}], e);
  8953. tooltip.updatePosition({ plotX: anchor[0], plotY: anchor[1] });
  8954. }
  8955. }
  8956. // Start the event listener to pick up the tooltip and crosshairs
  8957. if (!pointer._onDocumentMouseMove) {
  8958. pointer._onDocumentMouseMove = function (e) {
  8959. if (charts[hoverChartIndex]) {
  8960. charts[hoverChartIndex].pointer.onDocumentMouseMove(e);
  8961. }
  8962. };
  8963. addEvent(doc, 'mousemove', pointer._onDocumentMouseMove);
  8964. }
  8965. // Crosshair. For each hover point, loop over axes and draw cross if that point
  8966. // belongs to the axis (#4927).
  8967. each(shared ? kdpoints : [pick(hoverPoint, kdpoint[1])], function (point) { // #5269
  8968. each(chart.axes, function (axis) {
  8969. // In case of snap = false, point is undefined, and we draw the crosshair anyway (#5066)
  8970. if (!point || point.series[axis.coll] === axis) {
  8971. axis.drawCrosshair(e, point);
  8972. }
  8973. });
  8974. });
  8975. },
  8976. /**
  8977. * Reset the tracking by hiding the tooltip, the hover series state and the hover point
  8978. *
  8979. * @param allowMove {Boolean} Instead of destroying the tooltip altogether, allow moving it if possible
  8980. */
  8981. reset: function (allowMove, delay) {
  8982. var pointer = this,
  8983. chart = pointer.chart,
  8984. hoverSeries = chart.hoverSeries,
  8985. hoverPoint = chart.hoverPoint,
  8986. hoverPoints = chart.hoverPoints,
  8987. tooltip = chart.tooltip,
  8988. tooltipPoints = tooltip && tooltip.shared ? hoverPoints : hoverPoint;
  8989. // Check if the points have moved outside the plot area (#1003, #4736, #5101)
  8990. if (allowMove && tooltipPoints) {
  8991. each(splat(tooltipPoints), function (point) {
  8992. if (point.series.isCartesian && point.plotX === undefined) {
  8993. allowMove = false;
  8994. }
  8995. });
  8996. }
  8997. // Just move the tooltip, #349
  8998. if (allowMove) {
  8999. if (tooltip && tooltipPoints) {
  9000. tooltip.refresh(tooltipPoints);
  9001. if (hoverPoint) { // #2500
  9002. hoverPoint.setState(hoverPoint.state, true);
  9003. each(chart.axes, function (axis) {
  9004. if (pick(axis.crosshair && axis.crosshair.snap, true)) {
  9005. axis.drawCrosshair(null, hoverPoint);
  9006. } else {
  9007. axis.hideCrosshair();
  9008. }
  9009. });
  9010. }
  9011. }
  9012. // Full reset
  9013. } else {
  9014. if (hoverPoint) {
  9015. hoverPoint.onMouseOut();
  9016. }
  9017. if (hoverPoints) {
  9018. each(hoverPoints, function (point) {
  9019. point.setState();
  9020. });
  9021. }
  9022. if (hoverSeries) {
  9023. hoverSeries.onMouseOut();
  9024. }
  9025. if (tooltip) {
  9026. tooltip.hide(delay);
  9027. }
  9028. if (pointer._onDocumentMouseMove) {
  9029. removeEvent(doc, 'mousemove', pointer._onDocumentMouseMove);
  9030. pointer._onDocumentMouseMove = null;
  9031. }
  9032. // Remove crosshairs
  9033. each(chart.axes, function (axis) {
  9034. axis.hideCrosshair();
  9035. });
  9036. pointer.hoverX = chart.hoverPoints = chart.hoverPoint = null;
  9037. }
  9038. },
  9039. /**
  9040. * Scale series groups to a certain scale and translation
  9041. */
  9042. scaleGroups: function (attribs, clip) {
  9043. var chart = this.chart,
  9044. seriesAttribs;
  9045. // Scale each series
  9046. each(chart.series, function (series) {
  9047. seriesAttribs = attribs || series.getPlotBox(); // #1701
  9048. if (series.xAxis && series.xAxis.zoomEnabled) {
  9049. series.group.attr(seriesAttribs);
  9050. if (series.markerGroup) {
  9051. series.markerGroup.attr(seriesAttribs);
  9052. series.markerGroup.clip(clip ? chart.clipRect : null);
  9053. }
  9054. if (series.dataLabelsGroup) {
  9055. series.dataLabelsGroup.attr(seriesAttribs);
  9056. }
  9057. }
  9058. });
  9059. // Clip
  9060. chart.clipRect.attr(clip || chart.clipBox);
  9061. },
  9062. /**
  9063. * Start a drag operation
  9064. */
  9065. dragStart: function (e) {
  9066. var chart = this.chart;
  9067. // Record the start position
  9068. chart.mouseIsDown = e.type;
  9069. chart.cancelClick = false;
  9070. chart.mouseDownX = this.mouseDownX = e.chartX;
  9071. chart.mouseDownY = this.mouseDownY = e.chartY;
  9072. },
  9073. /**
  9074. * Perform a drag operation in response to a mousemove event while the mouse is down
  9075. */
  9076. drag: function (e) {
  9077. var chart = this.chart,
  9078. chartOptions = chart.options.chart,
  9079. chartX = e.chartX,
  9080. chartY = e.chartY,
  9081. zoomHor = this.zoomHor,
  9082. zoomVert = this.zoomVert,
  9083. plotLeft = chart.plotLeft,
  9084. plotTop = chart.plotTop,
  9085. plotWidth = chart.plotWidth,
  9086. plotHeight = chart.plotHeight,
  9087. clickedInside,
  9088. size,
  9089. selectionMarker = this.selectionMarker,
  9090. mouseDownX = this.mouseDownX,
  9091. mouseDownY = this.mouseDownY,
  9092. panKey = chartOptions.panKey && e[chartOptions.panKey + 'Key'];
  9093. // If the device supports both touch and mouse (like IE11), and we are touch-dragging
  9094. // inside the plot area, don't handle the mouse event. #4339.
  9095. if (selectionMarker && selectionMarker.touch) {
  9096. return;
  9097. }
  9098. // If the mouse is outside the plot area, adjust to cooordinates
  9099. // inside to prevent the selection marker from going outside
  9100. if (chartX < plotLeft) {
  9101. chartX = plotLeft;
  9102. } else if (chartX > plotLeft + plotWidth) {
  9103. chartX = plotLeft + plotWidth;
  9104. }
  9105. if (chartY < plotTop) {
  9106. chartY = plotTop;
  9107. } else if (chartY > plotTop + plotHeight) {
  9108. chartY = plotTop + plotHeight;
  9109. }
  9110. // determine if the mouse has moved more than 10px
  9111. this.hasDragged = Math.sqrt(
  9112. Math.pow(mouseDownX - chartX, 2) +
  9113. Math.pow(mouseDownY - chartY, 2)
  9114. );
  9115. if (this.hasDragged > 10) {
  9116. clickedInside = chart.isInsidePlot(mouseDownX - plotLeft, mouseDownY - plotTop);
  9117. // make a selection
  9118. if (chart.hasCartesianSeries && (this.zoomX || this.zoomY) && clickedInside && !panKey) {
  9119. if (!selectionMarker) {
  9120. this.selectionMarker = selectionMarker = chart.renderer.rect(
  9121. plotLeft,
  9122. plotTop,
  9123. zoomHor ? 1 : plotWidth,
  9124. zoomVert ? 1 : plotHeight,
  9125. 0
  9126. )
  9127. .attr({
  9128. fill: chartOptions.selectionMarkerFill || 'rgba(69,114,167,0.25)',
  9129. zIndex: 7
  9130. })
  9131. .add();
  9132. }
  9133. }
  9134. // adjust the width of the selection marker
  9135. if (selectionMarker && zoomHor) {
  9136. size = chartX - mouseDownX;
  9137. selectionMarker.attr({
  9138. width: mathAbs(size),
  9139. x: (size > 0 ? 0 : size) + mouseDownX
  9140. });
  9141. }
  9142. // adjust the height of the selection marker
  9143. if (selectionMarker && zoomVert) {
  9144. size = chartY - mouseDownY;
  9145. selectionMarker.attr({
  9146. height: mathAbs(size),
  9147. y: (size > 0 ? 0 : size) + mouseDownY
  9148. });
  9149. }
  9150. // panning
  9151. if (clickedInside && !selectionMarker && chartOptions.panning) {
  9152. chart.pan(e, chartOptions.panning);
  9153. }
  9154. }
  9155. },
  9156. /**
  9157. * On mouse up or touch end across the entire document, drop the selection.
  9158. */
  9159. drop: function (e) {
  9160. var pointer = this,
  9161. chart = this.chart,
  9162. hasPinched = this.hasPinched;
  9163. if (this.selectionMarker) {
  9164. var selectionData = {
  9165. originalEvent: e, // #4890
  9166. xAxis: [],
  9167. yAxis: []
  9168. },
  9169. selectionBox = this.selectionMarker,
  9170. selectionLeft = selectionBox.attr ? selectionBox.attr('x') : selectionBox.x,
  9171. selectionTop = selectionBox.attr ? selectionBox.attr('y') : selectionBox.y,
  9172. selectionWidth = selectionBox.attr ? selectionBox.attr('width') : selectionBox.width,
  9173. selectionHeight = selectionBox.attr ? selectionBox.attr('height') : selectionBox.height,
  9174. runZoom;
  9175. // a selection has been made
  9176. if (this.hasDragged || hasPinched) {
  9177. // record each axis' min and max
  9178. each(chart.axes, function (axis) {
  9179. if (axis.zoomEnabled && defined(axis.min) && (hasPinched || pointer[{ xAxis: 'zoomX', yAxis: 'zoomY' }[axis.coll]])) { // #859, #3569
  9180. var horiz = axis.horiz,
  9181. minPixelPadding = e.type === 'touchend' ? axis.minPixelPadding : 0, // #1207, #3075
  9182. selectionMin = axis.toValue((horiz ? selectionLeft : selectionTop) + minPixelPadding),
  9183. selectionMax = axis.toValue((horiz ? selectionLeft + selectionWidth : selectionTop + selectionHeight) - minPixelPadding);
  9184. selectionData[axis.coll].push({
  9185. axis: axis,
  9186. min: mathMin(selectionMin, selectionMax), // for reversed axes
  9187. max: mathMax(selectionMin, selectionMax)
  9188. });
  9189. runZoom = true;
  9190. }
  9191. });
  9192. if (runZoom) {
  9193. fireEvent(chart, 'selection', selectionData, function (args) {
  9194. chart.zoom(extend(args, hasPinched ? { animation: false } : null));
  9195. });
  9196. }
  9197. }
  9198. this.selectionMarker = this.selectionMarker.destroy();
  9199. // Reset scaling preview
  9200. if (hasPinched) {
  9201. this.scaleGroups();
  9202. }
  9203. }
  9204. // Reset all
  9205. if (chart) { // it may be destroyed on mouse up - #877
  9206. css(chart.container, { cursor: chart._cursor });
  9207. chart.cancelClick = this.hasDragged > 10; // #370
  9208. chart.mouseIsDown = this.hasDragged = this.hasPinched = false;
  9209. this.pinchDown = [];
  9210. }
  9211. },
  9212. onContainerMouseDown: function (e) {
  9213. e = this.normalize(e);
  9214. // issue #295, dragging not always working in Firefox
  9215. if (e.preventDefault) {
  9216. e.preventDefault();
  9217. }
  9218. this.dragStart(e);
  9219. },
  9220. onDocumentMouseUp: function (e) {
  9221. if (charts[hoverChartIndex]) {
  9222. charts[hoverChartIndex].pointer.drop(e);
  9223. }
  9224. },
  9225. /**
  9226. * Special handler for mouse move that will hide the tooltip when the mouse leaves the plotarea.
  9227. * Issue #149 workaround. The mouseleave event does not always fire.
  9228. */
  9229. onDocumentMouseMove: function (e) {
  9230. var chart = this.chart,
  9231. chartPosition = this.chartPosition;
  9232. e = this.normalize(e, chartPosition);
  9233. // If we're outside, hide the tooltip
  9234. if (chartPosition && !this.inClass(e.target, 'highcharts-tracker') &&
  9235. !chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop)) {
  9236. this.reset();
  9237. }
  9238. },
  9239. /**
  9240. * When mouse leaves the container, hide the tooltip.
  9241. */
  9242. onContainerMouseLeave: function (e) {
  9243. var chart = charts[hoverChartIndex];
  9244. if (chart && (e.relatedTarget || e.toElement)) { // #4886, MS Touch end fires mouseleave but with no related target
  9245. chart.pointer.reset();
  9246. chart.pointer.chartPosition = null; // also reset the chart position, used in #149 fix
  9247. }
  9248. },
  9249. // The mousemove, touchmove and touchstart event handler
  9250. onContainerMouseMove: function (e) {
  9251. var chart = this.chart;
  9252. if (!defined(hoverChartIndex) || !charts[hoverChartIndex] || !charts[hoverChartIndex].mouseIsDown) {
  9253. hoverChartIndex = chart.index;
  9254. }
  9255. e = this.normalize(e);
  9256. e.returnValue = false; // #2251, #3224
  9257. if (chart.mouseIsDown === 'mousedown') {
  9258. this.drag(e);
  9259. }
  9260. // Show the tooltip and run mouse over events (#977)
  9261. if ((this.inClass(e.target, 'highcharts-tracker') ||
  9262. chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop)) && !chart.openMenu) {
  9263. this.runPointActions(e);
  9264. }
  9265. },
  9266. /**
  9267. * Utility to detect whether an element has, or has a parent with, a specific
  9268. * class name. Used on detection of tracker objects and on deciding whether
  9269. * hovering the tooltip should cause the active series to mouse out.
  9270. */
  9271. inClass: function (element, className) {
  9272. var elemClassName;
  9273. while (element) {
  9274. elemClassName = attr(element, 'class');
  9275. if (elemClassName) {
  9276. if (elemClassName.indexOf(className) !== -1) {
  9277. return true;
  9278. }
  9279. if (elemClassName.indexOf(PREFIX + 'container') !== -1) {
  9280. return false;
  9281. }
  9282. }
  9283. element = element.parentNode;
  9284. }
  9285. },
  9286. onTrackerMouseOut: function (e) {
  9287. var series = this.chart.hoverSeries,
  9288. relatedTarget = e.relatedTarget || e.toElement;
  9289. if (series && relatedTarget && !series.options.stickyTracking && // #4886
  9290. !this.inClass(relatedTarget, PREFIX + 'tooltip') &&
  9291. !this.inClass(relatedTarget, PREFIX + 'series-' + series.index)) { // #2499, #4465
  9292. series.onMouseOut();
  9293. }
  9294. },
  9295. onContainerClick: function (e) {
  9296. var chart = this.chart,
  9297. hoverPoint = chart.hoverPoint,
  9298. plotLeft = chart.plotLeft,
  9299. plotTop = chart.plotTop;
  9300. e = this.normalize(e);
  9301. if (!chart.cancelClick) {
  9302. // On tracker click, fire the series and point events. #783, #1583
  9303. if (hoverPoint && this.inClass(e.target, PREFIX + 'tracker')) {
  9304. // the series click event
  9305. fireEvent(hoverPoint.series, 'click', extend(e, {
  9306. point: hoverPoint
  9307. }));
  9308. // the point click event
  9309. if (chart.hoverPoint) { // it may be destroyed (#1844)
  9310. hoverPoint.firePointEvent('click', e);
  9311. }
  9312. // When clicking outside a tracker, fire a chart event
  9313. } else {
  9314. extend(e, this.getCoordinates(e));
  9315. // fire a click event in the chart
  9316. if (chart.isInsidePlot(e.chartX - plotLeft, e.chartY - plotTop)) {
  9317. fireEvent(chart, 'click', e);
  9318. }
  9319. }
  9320. }
  9321. },
  9322. /**
  9323. * Set the JS DOM events on the container and document. This method should contain
  9324. * a one-to-one assignment between methods and their handlers. Any advanced logic should
  9325. * be moved to the handler reflecting the event's name.
  9326. */
  9327. setDOMEvents: function () {
  9328. var pointer = this,
  9329. container = pointer.chart.container;
  9330. container.onmousedown = function (e) {
  9331. pointer.onContainerMouseDown(e);
  9332. };
  9333. container.onmousemove = function (e) {
  9334. pointer.onContainerMouseMove(e);
  9335. };
  9336. container.onclick = function (e) {
  9337. pointer.onContainerClick(e);
  9338. };
  9339. addEvent(container, 'mouseleave', pointer.onContainerMouseLeave);
  9340. if (chartCount === 1) {
  9341. addEvent(doc, 'mouseup', pointer.onDocumentMouseUp);
  9342. }
  9343. if (hasTouch) {
  9344. container.ontouchstart = function (e) {
  9345. pointer.onContainerTouchStart(e);
  9346. };
  9347. container.ontouchmove = function (e) {
  9348. pointer.onContainerTouchMove(e);
  9349. };
  9350. if (chartCount === 1) {
  9351. addEvent(doc, 'touchend', pointer.onDocumentTouchEnd);
  9352. }
  9353. }
  9354. },
  9355. /**
  9356. * Destroys the Pointer object and disconnects DOM events.
  9357. */
  9358. destroy: function () {
  9359. var prop;
  9360. removeEvent(this.chart.container, 'mouseleave', this.onContainerMouseLeave);
  9361. if (!chartCount) {
  9362. removeEvent(doc, 'mouseup', this.onDocumentMouseUp);
  9363. removeEvent(doc, 'touchend', this.onDocumentTouchEnd);
  9364. }
  9365. // memory and CPU leak
  9366. clearInterval(this.tooltipTimeout);
  9367. for (prop in this) {
  9368. this[prop] = null;
  9369. }
  9370. }
  9371. };
  9372. /* Support for touch devices */
  9373. extend(Highcharts.Pointer.prototype, {
  9374. /**
  9375. * Run translation operations
  9376. */
  9377. pinchTranslate: function (pinchDown, touches, transform, selectionMarker, clip, lastValidTouch) {
  9378. if (this.zoomHor || this.pinchHor) {
  9379. this.pinchTranslateDirection(true, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch);
  9380. }
  9381. if (this.zoomVert || this.pinchVert) {
  9382. this.pinchTranslateDirection(false, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch);
  9383. }
  9384. },
  9385. /**
  9386. * Run translation operations for each direction (horizontal and vertical) independently
  9387. */
  9388. pinchTranslateDirection: function (horiz, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch, forcedScale) {
  9389. var chart = this.chart,
  9390. xy = horiz ? 'x' : 'y',
  9391. XY = horiz ? 'X' : 'Y',
  9392. sChartXY = 'chart' + XY,
  9393. wh = horiz ? 'width' : 'height',
  9394. plotLeftTop = chart['plot' + (horiz ? 'Left' : 'Top')],
  9395. selectionWH,
  9396. selectionXY,
  9397. clipXY,
  9398. scale = forcedScale || 1,
  9399. inverted = chart.inverted,
  9400. bounds = chart.bounds[horiz ? 'h' : 'v'],
  9401. singleTouch = pinchDown.length === 1,
  9402. touch0Start = pinchDown[0][sChartXY],
  9403. touch0Now = touches[0][sChartXY],
  9404. touch1Start = !singleTouch && pinchDown[1][sChartXY],
  9405. touch1Now = !singleTouch && touches[1][sChartXY],
  9406. outOfBounds,
  9407. transformScale,
  9408. scaleKey,
  9409. setScale = function () {
  9410. if (!singleTouch && mathAbs(touch0Start - touch1Start) > 20) { // Don't zoom if fingers are too close on this axis
  9411. scale = forcedScale || mathAbs(touch0Now - touch1Now) / mathAbs(touch0Start - touch1Start);
  9412. }
  9413. clipXY = ((plotLeftTop - touch0Now) / scale) + touch0Start;
  9414. selectionWH = chart['plot' + (horiz ? 'Width' : 'Height')] / scale;
  9415. };
  9416. // Set the scale, first pass
  9417. setScale();
  9418. selectionXY = clipXY; // the clip position (x or y) is altered if out of bounds, the selection position is not
  9419. // Out of bounds
  9420. if (selectionXY < bounds.min) {
  9421. selectionXY = bounds.min;
  9422. outOfBounds = true;
  9423. } else if (selectionXY + selectionWH > bounds.max) {
  9424. selectionXY = bounds.max - selectionWH;
  9425. outOfBounds = true;
  9426. }
  9427. // Is the chart dragged off its bounds, determined by dataMin and dataMax?
  9428. if (outOfBounds) {
  9429. // Modify the touchNow position in order to create an elastic drag movement. This indicates
  9430. // to the user that the chart is responsive but can't be dragged further.
  9431. touch0Now -= 0.8 * (touch0Now - lastValidTouch[xy][0]);
  9432. if (!singleTouch) {
  9433. touch1Now -= 0.8 * (touch1Now - lastValidTouch[xy][1]);
  9434. }
  9435. // Set the scale, second pass to adapt to the modified touchNow positions
  9436. setScale();
  9437. } else {
  9438. lastValidTouch[xy] = [touch0Now, touch1Now];
  9439. }
  9440. // Set geometry for clipping, selection and transformation
  9441. if (!inverted) {
  9442. clip[xy] = clipXY - plotLeftTop;
  9443. clip[wh] = selectionWH;
  9444. }
  9445. scaleKey = inverted ? (horiz ? 'scaleY' : 'scaleX') : 'scale' + XY;
  9446. transformScale = inverted ? 1 / scale : scale;
  9447. selectionMarker[wh] = selectionWH;
  9448. selectionMarker[xy] = selectionXY;
  9449. transform[scaleKey] = scale;
  9450. transform['translate' + XY] = (transformScale * plotLeftTop) + (touch0Now - (transformScale * touch0Start));
  9451. },
  9452. /**
  9453. * Handle touch events with two touches
  9454. */
  9455. pinch: function (e) {
  9456. var self = this,
  9457. chart = self.chart,
  9458. pinchDown = self.pinchDown,
  9459. touches = e.touches,
  9460. touchesLength = touches.length,
  9461. lastValidTouch = self.lastValidTouch,
  9462. hasZoom = self.hasZoom,
  9463. selectionMarker = self.selectionMarker,
  9464. transform = {},
  9465. fireClickEvent = touchesLength === 1 && ((self.inClass(e.target, PREFIX + 'tracker') &&
  9466. chart.runTrackerClick) || self.runChartClick),
  9467. clip = {};
  9468. // Don't initiate panning until the user has pinched. This prevents us from
  9469. // blocking page scrolling as users scroll down a long page (#4210).
  9470. if (touchesLength > 1) {
  9471. self.initiated = true;
  9472. }
  9473. // On touch devices, only proceed to trigger click if a handler is defined
  9474. if (hasZoom && self.initiated && !fireClickEvent) {
  9475. e.preventDefault();
  9476. }
  9477. // Normalize each touch
  9478. map(touches, function (e) {
  9479. return self.normalize(e);
  9480. });
  9481. // Register the touch start position
  9482. if (e.type === 'touchstart') {
  9483. each(touches, function (e, i) {
  9484. pinchDown[i] = { chartX: e.chartX, chartY: e.chartY };
  9485. });
  9486. lastValidTouch.x = [pinchDown[0].chartX, pinchDown[1] && pinchDown[1].chartX];
  9487. lastValidTouch.y = [pinchDown[0].chartY, pinchDown[1] && pinchDown[1].chartY];
  9488. // Identify the data bounds in pixels
  9489. each(chart.axes, function (axis) {
  9490. if (axis.zoomEnabled) {
  9491. var bounds = chart.bounds[axis.horiz ? 'h' : 'v'],
  9492. minPixelPadding = axis.minPixelPadding,
  9493. min = axis.toPixels(pick(axis.options.min, axis.dataMin)),
  9494. max = axis.toPixels(pick(axis.options.max, axis.dataMax)),
  9495. absMin = mathMin(min, max),
  9496. absMax = mathMax(min, max);
  9497. // Store the bounds for use in the touchmove handler
  9498. bounds.min = mathMin(axis.pos, absMin - minPixelPadding);
  9499. bounds.max = mathMax(axis.pos + axis.len, absMax + minPixelPadding);
  9500. }
  9501. });
  9502. self.res = true; // reset on next move
  9503. // Event type is touchmove, handle panning and pinching
  9504. } else if (pinchDown.length) { // can be 0 when releasing, if touchend fires first
  9505. // Set the marker
  9506. if (!selectionMarker) {
  9507. self.selectionMarker = selectionMarker = extend({
  9508. destroy: noop,
  9509. touch: true
  9510. }, chart.plotBox);
  9511. }
  9512. self.pinchTranslate(pinchDown, touches, transform, selectionMarker, clip, lastValidTouch);
  9513. self.hasPinched = hasZoom;
  9514. // Scale and translate the groups to provide visual feedback during pinching
  9515. self.scaleGroups(transform, clip);
  9516. // Optionally move the tooltip on touchmove
  9517. if (!hasZoom && self.followTouchMove && touchesLength === 1) {
  9518. this.runPointActions(self.normalize(e));
  9519. } else if (self.res) {
  9520. self.res = false;
  9521. this.reset(false, 0);
  9522. }
  9523. }
  9524. },
  9525. /**
  9526. * General touch handler shared by touchstart and touchmove.
  9527. */
  9528. touch: function (e, start) {
  9529. var chart = this.chart,
  9530. hasMoved,
  9531. pinchDown;
  9532. hoverChartIndex = chart.index;
  9533. if (e.touches.length === 1) {
  9534. e = this.normalize(e);
  9535. if (chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop) && !chart.openMenu) {
  9536. // Run mouse events and display tooltip etc
  9537. if (start) {
  9538. this.runPointActions(e);
  9539. }
  9540. // Android fires touchmove events after the touchstart even if the
  9541. // finger hasn't moved, or moved only a pixel or two. In iOS however,
  9542. // the touchmove doesn't fire unless the finger moves more than ~4px.
  9543. // So we emulate this behaviour in Android by checking how much it
  9544. // moved, and cancelling on small distances. #3450.
  9545. if (e.type === 'touchmove') {
  9546. pinchDown = this.pinchDown;
  9547. hasMoved = pinchDown[0] ? Math.sqrt( // #5266
  9548. Math.pow(pinchDown[0].chartX - e.chartX, 2) +
  9549. Math.pow(pinchDown[0].chartY - e.chartY, 2)
  9550. ) >= 4 : false;
  9551. }
  9552. if (pick(hasMoved, true)) {
  9553. this.pinch(e);
  9554. }
  9555. } else if (start) {
  9556. // Hide the tooltip on touching outside the plot area (#1203)
  9557. this.reset();
  9558. }
  9559. } else if (e.touches.length === 2) {
  9560. this.pinch(e);
  9561. }
  9562. },
  9563. onContainerTouchStart: function (e) {
  9564. this.touch(e, true);
  9565. },
  9566. onContainerTouchMove: function (e) {
  9567. this.touch(e);
  9568. },
  9569. onDocumentTouchEnd: function (e) {
  9570. if (charts[hoverChartIndex]) {
  9571. charts[hoverChartIndex].pointer.drop(e);
  9572. }
  9573. }
  9574. });
  9575. if (win.PointerEvent || win.MSPointerEvent) {
  9576. // The touches object keeps track of the points being touched at all times
  9577. var touches = {},
  9578. hasPointerEvent = !!win.PointerEvent,
  9579. getWebkitTouches = function () {
  9580. var key,
  9581. fake = [];
  9582. fake.item = function (i) {
  9583. return this[i];
  9584. };
  9585. for (key in touches) {
  9586. if (touches.hasOwnProperty(key)) {
  9587. fake.push({
  9588. pageX: touches[key].pageX,
  9589. pageY: touches[key].pageY,
  9590. target: touches[key].target
  9591. });
  9592. }
  9593. }
  9594. return fake;
  9595. },
  9596. translateMSPointer = function (e, method, wktype, func) {
  9597. var p;
  9598. if ((e.pointerType === 'touch' || e.pointerType === e.MSPOINTER_TYPE_TOUCH) && charts[hoverChartIndex]) {
  9599. func(e);
  9600. p = charts[hoverChartIndex].pointer;
  9601. p[method]({
  9602. type: wktype,
  9603. target: e.currentTarget,
  9604. preventDefault: noop,
  9605. touches: getWebkitTouches()
  9606. });
  9607. }
  9608. };
  9609. /**
  9610. * Extend the Pointer prototype with methods for each event handler and more
  9611. */
  9612. extend(Pointer.prototype, {
  9613. onContainerPointerDown: function (e) {
  9614. translateMSPointer(e, 'onContainerTouchStart', 'touchstart', function (e) {
  9615. touches[e.pointerId] = { pageX: e.pageX, pageY: e.pageY, target: e.currentTarget };
  9616. });
  9617. },
  9618. onContainerPointerMove: function (e) {
  9619. translateMSPointer(e, 'onContainerTouchMove', 'touchmove', function (e) {
  9620. touches[e.pointerId] = { pageX: e.pageX, pageY: e.pageY };
  9621. if (!touches[e.pointerId].target) {
  9622. touches[e.pointerId].target = e.currentTarget;
  9623. }
  9624. });
  9625. },
  9626. onDocumentPointerUp: function (e) {
  9627. translateMSPointer(e, 'onDocumentTouchEnd', 'touchend', function (e) {
  9628. delete touches[e.pointerId];
  9629. });
  9630. },
  9631. /**
  9632. * Add or remove the MS Pointer specific events
  9633. */
  9634. batchMSEvents: function (fn) {
  9635. fn(this.chart.container, hasPointerEvent ? 'pointerdown' : 'MSPointerDown', this.onContainerPointerDown);
  9636. fn(this.chart.container, hasPointerEvent ? 'pointermove' : 'MSPointerMove', this.onContainerPointerMove);
  9637. fn(doc, hasPointerEvent ? 'pointerup' : 'MSPointerUp', this.onDocumentPointerUp);
  9638. }
  9639. });
  9640. // Disable default IE actions for pinch and such on chart element
  9641. wrap(Pointer.prototype, 'init', function (proceed, chart, options) {
  9642. proceed.call(this, chart, options);
  9643. if (this.hasZoom) { // #4014
  9644. css(chart.container, {
  9645. '-ms-touch-action': NONE,
  9646. 'touch-action': NONE
  9647. });
  9648. }
  9649. });
  9650. // Add IE specific touch events to chart
  9651. wrap(Pointer.prototype, 'setDOMEvents', function (proceed) {
  9652. proceed.apply(this);
  9653. if (this.hasZoom || this.followTouchMove) {
  9654. this.batchMSEvents(addEvent);
  9655. }
  9656. });
  9657. // Destroy MS events also
  9658. wrap(Pointer.prototype, 'destroy', function (proceed) {
  9659. this.batchMSEvents(removeEvent);
  9660. proceed.call(this);
  9661. });
  9662. }
  9663. /**
  9664. * The overview of the chart's series
  9665. */
  9666. var Legend = Highcharts.Legend = function (chart, options) {
  9667. this.init(chart, options);
  9668. };
  9669. Legend.prototype = {
  9670. /**
  9671. * Initialize the legend
  9672. */
  9673. init: function (chart, options) {
  9674. var legend = this,
  9675. itemStyle = options.itemStyle,
  9676. padding,
  9677. itemMarginTop = options.itemMarginTop || 0;
  9678. this.options = options;
  9679. if (!options.enabled) {
  9680. return;
  9681. }
  9682. legend.itemStyle = itemStyle;
  9683. legend.itemHiddenStyle = merge(itemStyle, options.itemHiddenStyle);
  9684. legend.itemMarginTop = itemMarginTop;
  9685. legend.padding = padding = pick(options.padding, 8);
  9686. legend.initialItemX = padding;
  9687. legend.initialItemY = padding - 5; // 5 is the number of pixels above the text
  9688. legend.maxItemWidth = 0;
  9689. legend.chart = chart;
  9690. legend.itemHeight = 0;
  9691. legend.symbolWidth = pick(options.symbolWidth, 16);
  9692. legend.pages = [];
  9693. // Render it
  9694. legend.render();
  9695. // move checkboxes
  9696. addEvent(legend.chart, 'endResize', function () {
  9697. legend.positionCheckboxes();
  9698. });
  9699. },
  9700. /**
  9701. * Set the colors for the legend item
  9702. * @param {Object} item A Series or Point instance
  9703. * @param {Object} visible Dimmed or colored
  9704. */
  9705. colorizeItem: function (item, visible) {
  9706. var legend = this,
  9707. options = legend.options,
  9708. legendItem = item.legendItem,
  9709. legendLine = item.legendLine,
  9710. legendSymbol = item.legendSymbol,
  9711. hiddenColor = legend.itemHiddenStyle.color,
  9712. textColor = visible ? options.itemStyle.color : hiddenColor,
  9713. symbolColor = visible ? (item.legendColor || item.color || '#CCC') : hiddenColor,
  9714. markerOptions = item.options && item.options.marker,
  9715. symbolAttr = { fill: symbolColor },
  9716. key,
  9717. val;
  9718. if (legendItem) {
  9719. legendItem.css({ fill: textColor, color: textColor }); // color for #1553, oldIE
  9720. }
  9721. if (legendLine) {
  9722. legendLine.attr({ stroke: symbolColor });
  9723. }
  9724. if (legendSymbol) {
  9725. // Apply marker options
  9726. if (markerOptions && legendSymbol.isMarker) { // #585
  9727. symbolAttr.stroke = symbolColor;
  9728. markerOptions = item.convertAttribs(markerOptions);
  9729. for (key in markerOptions) {
  9730. val = markerOptions[key];
  9731. if (val !== UNDEFINED) {
  9732. symbolAttr[key] = val;
  9733. }
  9734. }
  9735. }
  9736. legendSymbol.attr(symbolAttr);
  9737. }
  9738. },
  9739. /**
  9740. * Position the legend item
  9741. * @param {Object} item A Series or Point instance
  9742. */
  9743. positionItem: function (item) {
  9744. var legend = this,
  9745. options = legend.options,
  9746. symbolPadding = options.symbolPadding,
  9747. ltr = !options.rtl,
  9748. legendItemPos = item._legendItemPos,
  9749. itemX = legendItemPos[0],
  9750. itemY = legendItemPos[1],
  9751. checkbox = item.checkbox,
  9752. legendGroup = item.legendGroup;
  9753. if (legendGroup && legendGroup.element) {
  9754. legendGroup.translate(
  9755. ltr ? itemX : legend.legendWidth - itemX - 2 * symbolPadding - 4,
  9756. itemY
  9757. );
  9758. }
  9759. if (checkbox) {
  9760. checkbox.x = itemX;
  9761. checkbox.y = itemY;
  9762. }
  9763. },
  9764. /**
  9765. * Destroy a single legend item
  9766. * @param {Object} item The series or point
  9767. */
  9768. destroyItem: function (item) {
  9769. var checkbox = item.checkbox;
  9770. // destroy SVG elements
  9771. each(['legendItem', 'legendLine', 'legendSymbol', 'legendGroup'], function (key) {
  9772. if (item[key]) {
  9773. item[key] = item[key].destroy();
  9774. }
  9775. });
  9776. if (checkbox) {
  9777. discardElement(item.checkbox);
  9778. }
  9779. },
  9780. /**
  9781. * Destroys the legend.
  9782. */
  9783. destroy: function () {
  9784. var legend = this,
  9785. legendGroup = legend.group,
  9786. box = legend.box;
  9787. if (box) {
  9788. legend.box = box.destroy();
  9789. }
  9790. if (legendGroup) {
  9791. legend.group = legendGroup.destroy();
  9792. }
  9793. },
  9794. /**
  9795. * Position the checkboxes after the width is determined
  9796. */
  9797. positionCheckboxes: function (scrollOffset) {
  9798. var alignAttr = this.group.alignAttr,
  9799. translateY,
  9800. clipHeight = this.clipHeight || this.legendHeight,
  9801. titleHeight = this.titleHeight;
  9802. if (alignAttr) {
  9803. translateY = alignAttr.translateY;
  9804. each(this.allItems, function (item) {
  9805. var checkbox = item.checkbox,
  9806. top;
  9807. if (checkbox) {
  9808. top = translateY + titleHeight + checkbox.y + (scrollOffset || 0) + 3;
  9809. css(checkbox, {
  9810. left: (alignAttr.translateX + item.checkboxOffset + checkbox.x - 20) + PX,
  9811. top: top + PX,
  9812. display: top > translateY - 6 && top < translateY + clipHeight - 6 ? '' : NONE
  9813. });
  9814. }
  9815. });
  9816. }
  9817. },
  9818. /**
  9819. * Render the legend title on top of the legend
  9820. */
  9821. renderTitle: function () {
  9822. var options = this.options,
  9823. padding = this.padding,
  9824. titleOptions = options.title,
  9825. titleHeight = 0,
  9826. bBox;
  9827. if (titleOptions.text) {
  9828. if (!this.title) {
  9829. this.title = this.chart.renderer.label(titleOptions.text, padding - 3, padding - 4, null, null, null, null, null, 'legend-title')
  9830. .attr({ zIndex: 1 })
  9831. .css(titleOptions.style)
  9832. .add(this.group);
  9833. }
  9834. bBox = this.title.getBBox();
  9835. titleHeight = bBox.height;
  9836. this.offsetWidth = bBox.width; // #1717
  9837. this.contentGroup.attr({ translateY: titleHeight });
  9838. }
  9839. this.titleHeight = titleHeight;
  9840. },
  9841. /**
  9842. * Set the legend item text
  9843. */
  9844. setText: function (item) {
  9845. var options = this.options;
  9846. item.legendItem.attr({
  9847. text: options.labelFormat ? format(options.labelFormat, item) : options.labelFormatter.call(item)
  9848. });
  9849. },
  9850. /**
  9851. * Render a single specific legend item
  9852. * @param {Object} item A series or point
  9853. */
  9854. renderItem: function (item) {
  9855. var legend = this,
  9856. chart = legend.chart,
  9857. renderer = chart.renderer,
  9858. options = legend.options,
  9859. horizontal = options.layout === 'horizontal',
  9860. symbolWidth = legend.symbolWidth,
  9861. symbolPadding = options.symbolPadding,
  9862. itemStyle = legend.itemStyle,
  9863. itemHiddenStyle = legend.itemHiddenStyle,
  9864. padding = legend.padding,
  9865. itemDistance = horizontal ? pick(options.itemDistance, 20) : 0,
  9866. ltr = !options.rtl,
  9867. itemHeight,
  9868. widthOption = options.width,
  9869. itemMarginBottom = options.itemMarginBottom || 0,
  9870. itemMarginTop = legend.itemMarginTop,
  9871. initialItemX = legend.initialItemX,
  9872. bBox,
  9873. itemWidth,
  9874. li = item.legendItem,
  9875. series = item.series && item.series.drawLegendSymbol ? item.series : item,
  9876. seriesOptions = series.options,
  9877. showCheckbox = legend.createCheckboxForItem && seriesOptions && seriesOptions.showCheckbox,
  9878. useHTML = options.useHTML;
  9879. if (!li) { // generate it once, later move it
  9880. // Generate the group box
  9881. // A group to hold the symbol and text. Text is to be appended in Legend class.
  9882. item.legendGroup = renderer.g('legend-item')
  9883. .attr({ zIndex: 1 })
  9884. .add(legend.scrollGroup);
  9885. // Generate the list item text and add it to the group
  9886. item.legendItem = li = renderer.text(
  9887. '',
  9888. ltr ? symbolWidth + symbolPadding : -symbolPadding,
  9889. legend.baseline || 0,
  9890. useHTML
  9891. )
  9892. .css(merge(item.visible ? itemStyle : itemHiddenStyle)) // merge to prevent modifying original (#1021)
  9893. .attr({
  9894. align: ltr ? 'left' : 'right',
  9895. zIndex: 2
  9896. })
  9897. .add(item.legendGroup);
  9898. // Get the baseline for the first item - the font size is equal for all
  9899. if (!legend.baseline) {
  9900. legend.fontMetrics = renderer.fontMetrics(itemStyle.fontSize, li);
  9901. legend.baseline = legend.fontMetrics.f + 3 + itemMarginTop;
  9902. li.attr('y', legend.baseline);
  9903. }
  9904. // Draw the legend symbol inside the group box
  9905. series.drawLegendSymbol(legend, item);
  9906. if (legend.setItemEvents) {
  9907. legend.setItemEvents(item, li, useHTML, itemStyle, itemHiddenStyle);
  9908. }
  9909. // add the HTML checkbox on top
  9910. if (showCheckbox) {
  9911. legend.createCheckboxForItem(item);
  9912. }
  9913. }
  9914. // Colorize the items
  9915. legend.colorizeItem(item, item.visible);
  9916. // Always update the text
  9917. legend.setText(item);
  9918. // calculate the positions for the next line
  9919. bBox = li.getBBox();
  9920. itemWidth = item.checkboxOffset =
  9921. options.itemWidth ||
  9922. item.legendItemWidth ||
  9923. symbolWidth + symbolPadding + bBox.width + itemDistance + (showCheckbox ? 20 : 0);
  9924. legend.itemHeight = itemHeight = mathRound(item.legendItemHeight || bBox.height);
  9925. // if the item exceeds the width, start a new line
  9926. if (horizontal && legend.itemX - initialItemX + itemWidth >
  9927. (widthOption || (chart.chartWidth - 2 * padding - initialItemX - options.x))) {
  9928. legend.itemX = initialItemX;
  9929. legend.itemY += itemMarginTop + legend.lastLineHeight + itemMarginBottom;
  9930. legend.lastLineHeight = 0; // reset for next line (#915, #3976)
  9931. }
  9932. // If the item exceeds the height, start a new column
  9933. /*if (!horizontal && legend.itemY + options.y + itemHeight > chart.chartHeight - spacingTop - spacingBottom) {
  9934. legend.itemY = legend.initialItemY;
  9935. legend.itemX += legend.maxItemWidth;
  9936. legend.maxItemWidth = 0;
  9937. }*/
  9938. // Set the edge positions
  9939. legend.maxItemWidth = mathMax(legend.maxItemWidth, itemWidth);
  9940. legend.lastItemY = itemMarginTop + legend.itemY + itemMarginBottom;
  9941. legend.lastLineHeight = mathMax(itemHeight, legend.lastLineHeight); // #915
  9942. // cache the position of the newly generated or reordered items
  9943. item._legendItemPos = [legend.itemX, legend.itemY];
  9944. // advance
  9945. if (horizontal) {
  9946. legend.itemX += itemWidth;
  9947. } else {
  9948. legend.itemY += itemMarginTop + itemHeight + itemMarginBottom;
  9949. legend.lastLineHeight = itemHeight;
  9950. }
  9951. // the width of the widest item
  9952. legend.offsetWidth = widthOption || mathMax(
  9953. (horizontal ? legend.itemX - initialItemX - itemDistance : itemWidth) + padding,
  9954. legend.offsetWidth
  9955. );
  9956. },
  9957. /**
  9958. * Get all items, which is one item per series for normal series and one item per point
  9959. * for pie series.
  9960. */
  9961. getAllItems: function () {
  9962. var allItems = [];
  9963. each(this.chart.series, function (series) {
  9964. var seriesOptions = series.options;
  9965. // Handle showInLegend. If the series is linked to another series, defaults to false.
  9966. if (!pick(seriesOptions.showInLegend, !defined(seriesOptions.linkedTo) ? UNDEFINED : false, true)) {
  9967. return;
  9968. }
  9969. // use points or series for the legend item depending on legendType
  9970. allItems = allItems.concat(
  9971. series.legendItems ||
  9972. (seriesOptions.legendType === 'point' ?
  9973. series.data :
  9974. series)
  9975. );
  9976. });
  9977. return allItems;
  9978. },
  9979. /**
  9980. * Adjust the chart margins by reserving space for the legend on only one side
  9981. * of the chart. If the position is set to a corner, top or bottom is reserved
  9982. * for horizontal legends and left or right for vertical ones.
  9983. */
  9984. adjustMargins: function (margin, spacing) {
  9985. var chart = this.chart,
  9986. options = this.options,
  9987. // Use the first letter of each alignment option in order to detect the side
  9988. alignment = options.align.charAt(0) + options.verticalAlign.charAt(0) + options.layout.charAt(0); // #4189 - use charAt(x) notation instead of [x] for IE7
  9989. if (this.display && !options.floating) {
  9990. each([
  9991. /(lth|ct|rth)/,
  9992. /(rtv|rm|rbv)/,
  9993. /(rbh|cb|lbh)/,
  9994. /(lbv|lm|ltv)/
  9995. ], function (alignments, side) {
  9996. if (alignments.test(alignment) && !defined(margin[side])) {
  9997. // Now we have detected on which side of the chart we should reserve space for the legend
  9998. chart[marginNames[side]] = mathMax(
  9999. chart[marginNames[side]],
  10000. chart.legend[(side + 1) % 2 ? 'legendHeight' : 'legendWidth'] +
  10001. [1, -1, -1, 1][side] * options[(side % 2) ? 'x' : 'y'] +
  10002. pick(options.margin, 12) +
  10003. spacing[side]
  10004. );
  10005. }
  10006. });
  10007. }
  10008. },
  10009. /**
  10010. * Render the legend. This method can be called both before and after
  10011. * chart.render. If called after, it will only rearrange items instead
  10012. * of creating new ones.
  10013. */
  10014. render: function () {
  10015. var legend = this,
  10016. chart = legend.chart,
  10017. renderer = chart.renderer,
  10018. legendGroup = legend.group,
  10019. allItems,
  10020. display,
  10021. legendWidth,
  10022. legendHeight,
  10023. box = legend.box,
  10024. options = legend.options,
  10025. padding = legend.padding,
  10026. legendBorderWidth = options.borderWidth,
  10027. legendBackgroundColor = options.backgroundColor;
  10028. legend.itemX = legend.initialItemX;
  10029. legend.itemY = legend.initialItemY;
  10030. legend.offsetWidth = 0;
  10031. legend.lastItemY = 0;
  10032. if (!legendGroup) {
  10033. legend.group = legendGroup = renderer.g('legend')
  10034. .attr({ zIndex: 7 })
  10035. .add();
  10036. legend.contentGroup = renderer.g()
  10037. .attr({ zIndex: 1 }) // above background
  10038. .add(legendGroup);
  10039. legend.scrollGroup = renderer.g()
  10040. .add(legend.contentGroup);
  10041. }
  10042. legend.renderTitle();
  10043. // add each series or point
  10044. allItems = legend.getAllItems();
  10045. // sort by legendIndex
  10046. stableSort(allItems, function (a, b) {
  10047. return ((a.options && a.options.legendIndex) || 0) - ((b.options && b.options.legendIndex) || 0);
  10048. });
  10049. // reversed legend
  10050. if (options.reversed) {
  10051. allItems.reverse();
  10052. }
  10053. legend.allItems = allItems;
  10054. legend.display = display = !!allItems.length;
  10055. // render the items
  10056. legend.lastLineHeight = 0;
  10057. each(allItems, function (item) {
  10058. legend.renderItem(item);
  10059. });
  10060. // Get the box
  10061. legendWidth = (options.width || legend.offsetWidth) + padding;
  10062. legendHeight = legend.lastItemY + legend.lastLineHeight + legend.titleHeight;
  10063. legendHeight = legend.handleOverflow(legendHeight);
  10064. legendHeight += padding;
  10065. // Draw the border and/or background
  10066. if (legendBorderWidth || legendBackgroundColor) {
  10067. if (!box) {
  10068. legend.box = box = renderer.rect(
  10069. 0,
  10070. 0,
  10071. legendWidth,
  10072. legendHeight,
  10073. options.borderRadius,
  10074. legendBorderWidth || 0
  10075. ).attr({
  10076. stroke: options.borderColor,
  10077. 'stroke-width': legendBorderWidth || 0,
  10078. fill: legendBackgroundColor || NONE
  10079. })
  10080. .add(legendGroup)
  10081. .shadow(options.shadow);
  10082. box.isNew = true;
  10083. } else if (legendWidth > 0 && legendHeight > 0) {
  10084. box[box.isNew ? 'attr' : 'animate'](
  10085. box.crisp({ width: legendWidth, height: legendHeight })
  10086. );
  10087. box.isNew = false;
  10088. }
  10089. // hide the border if no items
  10090. box[display ? 'show' : 'hide']();
  10091. }
  10092. legend.legendWidth = legendWidth;
  10093. legend.legendHeight = legendHeight;
  10094. // Now that the legend width and height are established, put the items in the
  10095. // final position
  10096. each(allItems, function (item) {
  10097. legend.positionItem(item);
  10098. });
  10099. // 1.x compatibility: positioning based on style
  10100. /*var props = ['left', 'right', 'top', 'bottom'],
  10101. prop,
  10102. i = 4;
  10103. while (i--) {
  10104. prop = props[i];
  10105. if (options.style[prop] && options.style[prop] !== 'auto') {
  10106. options[i < 2 ? 'align' : 'verticalAlign'] = prop;
  10107. options[i < 2 ? 'x' : 'y'] = pInt(options.style[prop]) * (i % 2 ? -1 : 1);
  10108. }
  10109. }*/
  10110. if (display) {
  10111. legendGroup.align(extend({
  10112. width: legendWidth,
  10113. height: legendHeight
  10114. }, options), true, 'spacingBox');
  10115. }
  10116. if (!chart.isResizing) {
  10117. this.positionCheckboxes();
  10118. }
  10119. },
  10120. /**
  10121. * Set up the overflow handling by adding navigation with up and down arrows below the
  10122. * legend.
  10123. */
  10124. handleOverflow: function (legendHeight) {
  10125. var legend = this,
  10126. chart = this.chart,
  10127. renderer = chart.renderer,
  10128. options = this.options,
  10129. optionsY = options.y,
  10130. alignTop = options.verticalAlign === 'top',
  10131. spaceHeight = chart.spacingBox.height + (alignTop ? -optionsY : optionsY) - this.padding,
  10132. maxHeight = options.maxHeight,
  10133. clipHeight,
  10134. clipRect = this.clipRect,
  10135. navOptions = options.navigation,
  10136. animation = pick(navOptions.animation, true),
  10137. arrowSize = navOptions.arrowSize || 12,
  10138. nav = this.nav,
  10139. pages = this.pages,
  10140. padding = this.padding,
  10141. lastY,
  10142. allItems = this.allItems,
  10143. clipToHeight = function (height) {
  10144. clipRect.attr({
  10145. height: height
  10146. });
  10147. // useHTML
  10148. if (legend.contentGroup.div) {
  10149. legend.contentGroup.div.style.clip = 'rect(' + padding + 'px,9999px,' + (padding + height) + 'px,0)';
  10150. }
  10151. };
  10152. // Adjust the height
  10153. if (options.layout === 'horizontal') {
  10154. spaceHeight /= 2;
  10155. }
  10156. if (maxHeight) {
  10157. spaceHeight = mathMin(spaceHeight, maxHeight);
  10158. }
  10159. // Reset the legend height and adjust the clipping rectangle
  10160. pages.length = 0;
  10161. if (legendHeight > spaceHeight && navOptions.enabled !== false) {
  10162. this.clipHeight = clipHeight = mathMax(spaceHeight - 20 - this.titleHeight - padding, 0);
  10163. this.currentPage = pick(this.currentPage, 1);
  10164. this.fullHeight = legendHeight;
  10165. // Fill pages with Y positions so that the top of each a legend item defines
  10166. // the scroll top for each page (#2098)
  10167. each(allItems, function (item, i) {
  10168. var y = item._legendItemPos[1],
  10169. h = mathRound(item.legendItem.getBBox().height),
  10170. len = pages.length;
  10171. if (!len || (y - pages[len - 1] > clipHeight && (lastY || y) !== pages[len - 1])) {
  10172. pages.push(lastY || y);
  10173. len++;
  10174. }
  10175. if (i === allItems.length - 1 && y + h - pages[len - 1] > clipHeight) {
  10176. pages.push(y);
  10177. }
  10178. if (y !== lastY) {
  10179. lastY = y;
  10180. }
  10181. });
  10182. // Only apply clipping if needed. Clipping causes blurred legend in PDF export (#1787)
  10183. if (!clipRect) {
  10184. clipRect = legend.clipRect = renderer.clipRect(0, padding, 9999, 0);
  10185. legend.contentGroup.clip(clipRect);
  10186. }
  10187. clipToHeight(clipHeight);
  10188. // Add navigation elements
  10189. if (!nav) {
  10190. this.nav = nav = renderer.g().attr({ zIndex: 1 }).add(this.group);
  10191. this.up = renderer.symbol('triangle', 0, 0, arrowSize, arrowSize)
  10192. .on('click', function () {
  10193. legend.scroll(-1, animation);
  10194. })
  10195. .add(nav);
  10196. this.pager = renderer.text('', 15, 10)
  10197. .css(navOptions.style)
  10198. .add(nav);
  10199. this.down = renderer.symbol('triangle-down', 0, 0, arrowSize, arrowSize)
  10200. .on('click', function () {
  10201. legend.scroll(1, animation);
  10202. })
  10203. .add(nav);
  10204. }
  10205. // Set initial position
  10206. legend.scroll(0);
  10207. legendHeight = spaceHeight;
  10208. } else if (nav) {
  10209. clipToHeight(chart.chartHeight);
  10210. nav.hide();
  10211. this.scrollGroup.attr({
  10212. translateY: 1
  10213. });
  10214. this.clipHeight = 0; // #1379
  10215. }
  10216. return legendHeight;
  10217. },
  10218. /**
  10219. * Scroll the legend by a number of pages
  10220. * @param {Object} scrollBy
  10221. * @param {Object} animation
  10222. */
  10223. scroll: function (scrollBy, animation) {
  10224. var pages = this.pages,
  10225. pageCount = pages.length,
  10226. currentPage = this.currentPage + scrollBy,
  10227. clipHeight = this.clipHeight,
  10228. navOptions = this.options.navigation,
  10229. activeColor = navOptions.activeColor,
  10230. inactiveColor = navOptions.inactiveColor,
  10231. pager = this.pager,
  10232. padding = this.padding,
  10233. scrollOffset;
  10234. // When resizing while looking at the last page
  10235. if (currentPage > pageCount) {
  10236. currentPage = pageCount;
  10237. }
  10238. if (currentPage > 0) {
  10239. if (animation !== UNDEFINED) {
  10240. setAnimation(animation, this.chart);
  10241. }
  10242. this.nav.attr({
  10243. translateX: padding,
  10244. translateY: clipHeight + this.padding + 7 + this.titleHeight,
  10245. visibility: VISIBLE
  10246. });
  10247. this.up.attr({
  10248. fill: currentPage === 1 ? inactiveColor : activeColor
  10249. })
  10250. .css({
  10251. cursor: currentPage === 1 ? 'default' : 'pointer'
  10252. });
  10253. pager.attr({
  10254. text: currentPage + '/' + pageCount
  10255. });
  10256. this.down.attr({
  10257. x: 18 + this.pager.getBBox().width, // adjust to text width
  10258. fill: currentPage === pageCount ? inactiveColor : activeColor
  10259. })
  10260. .css({
  10261. cursor: currentPage === pageCount ? 'default' : 'pointer'
  10262. });
  10263. scrollOffset = -pages[currentPage - 1] + this.initialItemY;
  10264. this.scrollGroup.animate({
  10265. translateY: scrollOffset
  10266. });
  10267. this.currentPage = currentPage;
  10268. this.positionCheckboxes(scrollOffset);
  10269. }
  10270. }
  10271. };
  10272. /*
  10273. * LegendSymbolMixin
  10274. */
  10275. var LegendSymbolMixin = Highcharts.LegendSymbolMixin = {
  10276. /**
  10277. * Get the series' symbol in the legend
  10278. *
  10279. * @param {Object} legend The legend object
  10280. * @param {Object} item The series (this) or point
  10281. */
  10282. drawRectangle: function (legend, item) {
  10283. var symbolHeight = legend.options.symbolHeight || legend.fontMetrics.f;
  10284. item.legendSymbol = this.chart.renderer.rect(
  10285. 0,
  10286. legend.baseline - symbolHeight + 1, // #3988
  10287. legend.symbolWidth,
  10288. symbolHeight,
  10289. legend.options.symbolRadius || 0
  10290. ).attr({
  10291. zIndex: 3
  10292. }).add(item.legendGroup);
  10293. },
  10294. /**
  10295. * Get the series' symbol in the legend. This method should be overridable to create custom
  10296. * symbols through Highcharts.seriesTypes[type].prototype.drawLegendSymbols.
  10297. *
  10298. * @param {Object} legend The legend object
  10299. */
  10300. drawLineMarker: function (legend) {
  10301. var options = this.options,
  10302. markerOptions = options.marker,
  10303. radius,
  10304. legendSymbol,
  10305. symbolWidth = legend.symbolWidth,
  10306. renderer = this.chart.renderer,
  10307. legendItemGroup = this.legendGroup,
  10308. verticalCenter = legend.baseline - mathRound(legend.fontMetrics.b * 0.3),
  10309. attr;
  10310. // Draw the line
  10311. if (options.lineWidth) {
  10312. attr = {
  10313. 'stroke-width': options.lineWidth
  10314. };
  10315. if (options.dashStyle) {
  10316. attr.dashstyle = options.dashStyle;
  10317. }
  10318. this.legendLine = renderer.path([
  10319. M,
  10320. 0,
  10321. verticalCenter,
  10322. L,
  10323. symbolWidth,
  10324. verticalCenter
  10325. ])
  10326. .attr(attr)
  10327. .add(legendItemGroup);
  10328. }
  10329. // Draw the marker
  10330. if (markerOptions && markerOptions.enabled !== false) {
  10331. radius = markerOptions.radius;
  10332. this.legendSymbol = legendSymbol = renderer.symbol(
  10333. this.symbol,
  10334. (symbolWidth / 2) - radius,
  10335. verticalCenter - radius,
  10336. 2 * radius,
  10337. 2 * radius,
  10338. markerOptions
  10339. )
  10340. .add(legendItemGroup);
  10341. legendSymbol.isMarker = true;
  10342. }
  10343. }
  10344. };
  10345. // Workaround for #2030, horizontal legend items not displaying in IE11 Preview,
  10346. // and for #2580, a similar drawing flaw in Firefox 26.
  10347. // Explore if there's a general cause for this. The problem may be related
  10348. // to nested group elements, as the legend item texts are within 4 group elements.
  10349. if (/Trident\/7\.0/.test(userAgent) || isFirefox) {
  10350. wrap(Legend.prototype, 'positionItem', function (proceed, item) {
  10351. var legend = this,
  10352. runPositionItem = function () { // If chart destroyed in sync, this is undefined (#2030)
  10353. if (item._legendItemPos) {
  10354. proceed.call(legend, item);
  10355. }
  10356. };
  10357. // Do it now, for export and to get checkbox placement
  10358. runPositionItem();
  10359. // Do it after to work around the core issue
  10360. setTimeout(runPositionItem);
  10361. });
  10362. }
  10363. /**
  10364. * The Chart class
  10365. * @param {String|Object} renderTo The DOM element to render to, or its id
  10366. * @param {Object} options
  10367. * @param {Function} callback Function to run when the chart has loaded
  10368. */
  10369. var Chart = Highcharts.Chart = function () {
  10370. this.getArgs.apply(this, arguments);
  10371. };
  10372. Highcharts.chart = function (a, b, c) {
  10373. return new Chart(a, b, c);
  10374. };
  10375. Chart.prototype = {
  10376. /**
  10377. * Hook for modules
  10378. */
  10379. callbacks: [],
  10380. /**
  10381. * Handle the arguments passed to the constructor
  10382. * @returns {Array} Arguments without renderTo
  10383. */
  10384. getArgs: function () {
  10385. var args = [].slice.call(arguments);
  10386. // Remove the optional first argument, renderTo, and
  10387. // set it on this.
  10388. if (isString(args[0]) || args[0].nodeName) {
  10389. this.renderTo = args.shift();
  10390. }
  10391. this.init(args[0], args[1]);
  10392. },
  10393. /**
  10394. * Initialize the chart
  10395. */
  10396. init: function (userOptions, callback) {
  10397. // Handle regular options
  10398. var options,
  10399. seriesOptions = userOptions.series; // skip merging data points to increase performance
  10400. userOptions.series = null;
  10401. options = merge(defaultOptions, userOptions); // do the merge
  10402. options.series = userOptions.series = seriesOptions; // set back the series data
  10403. this.userOptions = userOptions;
  10404. var optionsChart = options.chart;
  10405. // Create margin & spacing array
  10406. this.margin = this.splashArray('margin', optionsChart);
  10407. this.spacing = this.splashArray('spacing', optionsChart);
  10408. var chartEvents = optionsChart.events;
  10409. //this.runChartClick = chartEvents && !!chartEvents.click;
  10410. this.bounds = { h: {}, v: {} }; // Pixel data bounds for touch zoom
  10411. this.callback = callback;
  10412. this.isResizing = 0;
  10413. this.options = options;
  10414. //chartTitleOptions = UNDEFINED;
  10415. //chartSubtitleOptions = UNDEFINED;
  10416. this.axes = [];
  10417. this.series = [];
  10418. this.hasCartesianSeries = optionsChart.showAxes;
  10419. //this.axisOffset = UNDEFINED;
  10420. //this.maxTicks = UNDEFINED; // handle the greatest amount of ticks on grouped axes
  10421. //this.inverted = UNDEFINED;
  10422. //this.loadingShown = UNDEFINED;
  10423. //this.container = UNDEFINED;
  10424. //this.chartWidth = UNDEFINED;
  10425. //this.chartHeight = UNDEFINED;
  10426. //this.marginRight = UNDEFINED;
  10427. //this.marginBottom = UNDEFINED;
  10428. //this.containerWidth = UNDEFINED;
  10429. //this.containerHeight = UNDEFINED;
  10430. //this.oldChartWidth = UNDEFINED;
  10431. //this.oldChartHeight = UNDEFINED;
  10432. //this.renderTo = UNDEFINED;
  10433. //this.renderToClone = UNDEFINED;
  10434. //this.spacingBox = UNDEFINED
  10435. //this.legend = UNDEFINED;
  10436. // Elements
  10437. //this.chartBackground = UNDEFINED;
  10438. //this.plotBackground = UNDEFINED;
  10439. //this.plotBGImage = UNDEFINED;
  10440. //this.plotBorder = UNDEFINED;
  10441. //this.loadingDiv = UNDEFINED;
  10442. //this.loadingSpan = UNDEFINED;
  10443. var chart = this,
  10444. eventType;
  10445. // Add the chart to the global lookup
  10446. chart.index = charts.length;
  10447. charts.push(chart);
  10448. chartCount++;
  10449. // Set up auto resize
  10450. if (optionsChart.reflow !== false) {
  10451. addEvent(chart, 'load', function () {
  10452. chart.initReflow();
  10453. });
  10454. }
  10455. // Chart event handlers
  10456. if (chartEvents) {
  10457. for (eventType in chartEvents) {
  10458. addEvent(chart, eventType, chartEvents[eventType]);
  10459. }
  10460. }
  10461. chart.xAxis = [];
  10462. chart.yAxis = [];
  10463. // Expose methods and variables
  10464. chart.animation = useCanVG ? false : pick(optionsChart.animation, true);
  10465. chart.pointCount = chart.colorCounter = chart.symbolCounter = 0;
  10466. chart.firstRender();
  10467. },
  10468. /**
  10469. * Initialize an individual series, called internally before render time
  10470. */
  10471. initSeries: function (options) {
  10472. var chart = this,
  10473. optionsChart = chart.options.chart,
  10474. type = options.type || optionsChart.type || optionsChart.defaultSeriesType,
  10475. series,
  10476. constr = seriesTypes[type];
  10477. // No such series type
  10478. if (!constr) {
  10479. error(17, true);
  10480. }
  10481. series = new constr();
  10482. series.init(this, options);
  10483. return series;
  10484. },
  10485. /**
  10486. * Check whether a given point is within the plot area
  10487. *
  10488. * @param {Number} plotX Pixel x relative to the plot area
  10489. * @param {Number} plotY Pixel y relative to the plot area
  10490. * @param {Boolean} inverted Whether the chart is inverted
  10491. */
  10492. isInsidePlot: function (plotX, plotY, inverted) {
  10493. var x = inverted ? plotY : plotX,
  10494. y = inverted ? plotX : plotY;
  10495. return x >= 0 &&
  10496. x <= this.plotWidth &&
  10497. y >= 0 &&
  10498. y <= this.plotHeight;
  10499. },
  10500. /**
  10501. * Redraw legend, axes or series based on updated data
  10502. *
  10503. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  10504. * configuration
  10505. */
  10506. redraw: function (animation) {
  10507. var chart = this,
  10508. axes = chart.axes,
  10509. series = chart.series,
  10510. pointer = chart.pointer,
  10511. legend = chart.legend,
  10512. redrawLegend = chart.isDirtyLegend,
  10513. hasStackedSeries,
  10514. hasDirtyStacks,
  10515. hasCartesianSeries = chart.hasCartesianSeries,
  10516. isDirtyBox = chart.isDirtyBox,
  10517. seriesLength = series.length,
  10518. i = seriesLength,
  10519. serie,
  10520. renderer = chart.renderer,
  10521. isHiddenChart = renderer.isHidden(),
  10522. afterRedraw = [];
  10523. setAnimation(animation, chart);
  10524. if (isHiddenChart) {
  10525. chart.cloneRenderTo();
  10526. }
  10527. // Adjust title layout (reflow multiline text)
  10528. chart.layOutTitles();
  10529. // link stacked series
  10530. while (i--) {
  10531. serie = series[i];
  10532. if (serie.options.stacking) {
  10533. hasStackedSeries = true;
  10534. if (serie.isDirty) {
  10535. hasDirtyStacks = true;
  10536. break;
  10537. }
  10538. }
  10539. }
  10540. if (hasDirtyStacks) { // mark others as dirty
  10541. i = seriesLength;
  10542. while (i--) {
  10543. serie = series[i];
  10544. if (serie.options.stacking) {
  10545. serie.isDirty = true;
  10546. }
  10547. }
  10548. }
  10549. // Handle updated data in the series
  10550. each(series, function (serie) {
  10551. if (serie.isDirty) {
  10552. if (serie.options.legendType === 'point') {
  10553. if (serie.updateTotals) {
  10554. serie.updateTotals();
  10555. }
  10556. redrawLegend = true;
  10557. }
  10558. }
  10559. if (serie.isDirtyData) {
  10560. fireEvent(serie, 'updatedData');
  10561. }
  10562. });
  10563. // handle added or removed series
  10564. if (redrawLegend && legend.options.enabled) { // series or pie points are added or removed
  10565. // draw legend graphics
  10566. legend.render();
  10567. chart.isDirtyLegend = false;
  10568. }
  10569. // reset stacks
  10570. if (hasStackedSeries) {
  10571. chart.getStacks();
  10572. }
  10573. if (hasCartesianSeries) {
  10574. if (!chart.isResizing) {
  10575. // reset maxTicks
  10576. chart.maxTicks = null;
  10577. // set axes scales
  10578. each(axes, function (axis) {
  10579. axis.setScale();
  10580. });
  10581. }
  10582. }
  10583. chart.getMargins(); // #3098
  10584. if (hasCartesianSeries) {
  10585. // If one axis is dirty, all axes must be redrawn (#792, #2169)
  10586. each(axes, function (axis) {
  10587. if (axis.isDirty) {
  10588. isDirtyBox = true;
  10589. }
  10590. });
  10591. // redraw axes
  10592. each(axes, function (axis) {
  10593. // Fire 'afterSetExtremes' only if extremes are set
  10594. var key = axis.min + ',' + axis.max;
  10595. if (axis.extKey !== key) { // #821, #4452
  10596. axis.extKey = key;
  10597. afterRedraw.push(function () { // prevent a recursive call to chart.redraw() (#1119)
  10598. fireEvent(axis, 'afterSetExtremes', extend(axis.eventArgs, axis.getExtremes())); // #747, #751
  10599. delete axis.eventArgs;
  10600. });
  10601. }
  10602. if (isDirtyBox || hasStackedSeries) {
  10603. axis.redraw();
  10604. }
  10605. });
  10606. }
  10607. // the plot areas size has changed
  10608. if (isDirtyBox) {
  10609. chart.drawChartBox();
  10610. }
  10611. // redraw affected series
  10612. each(series, function (serie) {
  10613. if (serie.isDirty && serie.visible &&
  10614. (!serie.isCartesian || serie.xAxis)) { // issue #153
  10615. serie.redraw();
  10616. }
  10617. });
  10618. // move tooltip or reset
  10619. if (pointer) {
  10620. pointer.reset(true);
  10621. }
  10622. // redraw if canvas
  10623. renderer.draw();
  10624. // fire the event
  10625. fireEvent(chart, 'redraw');
  10626. if (isHiddenChart) {
  10627. chart.cloneRenderTo(true);
  10628. }
  10629. // Fire callbacks that are put on hold until after the redraw
  10630. each(afterRedraw, function (callback) {
  10631. callback.call();
  10632. });
  10633. },
  10634. /**
  10635. * Get an axis, series or point object by id.
  10636. * @param id {String} The id as given in the configuration options
  10637. */
  10638. get: function (id) {
  10639. var chart = this,
  10640. axes = chart.axes,
  10641. series = chart.series;
  10642. var i,
  10643. j,
  10644. points;
  10645. // search axes
  10646. for (i = 0; i < axes.length; i++) {
  10647. if (axes[i].options.id === id) {
  10648. return axes[i];
  10649. }
  10650. }
  10651. // search series
  10652. for (i = 0; i < series.length; i++) {
  10653. if (series[i].options.id === id) {
  10654. return series[i];
  10655. }
  10656. }
  10657. // search points
  10658. for (i = 0; i < series.length; i++) {
  10659. points = series[i].points || [];
  10660. for (j = 0; j < points.length; j++) {
  10661. if (points[j].id === id) {
  10662. return points[j];
  10663. }
  10664. }
  10665. }
  10666. return null;
  10667. },
  10668. /**
  10669. * Create the Axis instances based on the config options
  10670. */
  10671. getAxes: function () {
  10672. var chart = this,
  10673. options = this.options,
  10674. xAxisOptions = options.xAxis = splat(options.xAxis || {}),
  10675. yAxisOptions = options.yAxis = splat(options.yAxis || {}),
  10676. optionsArray;
  10677. // make sure the options are arrays and add some members
  10678. each(xAxisOptions, function (axis, i) {
  10679. axis.index = i;
  10680. axis.isX = true;
  10681. });
  10682. each(yAxisOptions, function (axis, i) {
  10683. axis.index = i;
  10684. });
  10685. // concatenate all axis options into one array
  10686. optionsArray = xAxisOptions.concat(yAxisOptions);
  10687. each(optionsArray, function (axisOptions) {
  10688. new Axis(chart, axisOptions); // eslint-disable-line no-new
  10689. });
  10690. },
  10691. /**
  10692. * Get the currently selected points from all series
  10693. */
  10694. getSelectedPoints: function () {
  10695. var points = [];
  10696. each(this.series, function (serie) {
  10697. points = points.concat(grep(serie.points || [], function (point) {
  10698. return point.selected;
  10699. }));
  10700. });
  10701. return points;
  10702. },
  10703. /**
  10704. * Get the currently selected series
  10705. */
  10706. getSelectedSeries: function () {
  10707. return grep(this.series, function (serie) {
  10708. return serie.selected;
  10709. });
  10710. },
  10711. /**
  10712. * Show the title and subtitle of the chart
  10713. *
  10714. * @param titleOptions {Object} New title options
  10715. * @param subtitleOptions {Object} New subtitle options
  10716. *
  10717. */
  10718. setTitle: function (titleOptions, subtitleOptions, redraw) {
  10719. var chart = this,
  10720. options = chart.options,
  10721. chartTitleOptions,
  10722. chartSubtitleOptions;
  10723. chartTitleOptions = options.title = merge(options.title, titleOptions);
  10724. chartSubtitleOptions = options.subtitle = merge(options.subtitle, subtitleOptions);
  10725. // add title and subtitle
  10726. each([
  10727. ['title', titleOptions, chartTitleOptions],
  10728. ['subtitle', subtitleOptions, chartSubtitleOptions]
  10729. ], function (arr) {
  10730. var name = arr[0],
  10731. title = chart[name],
  10732. titleOptions = arr[1],
  10733. chartTitleOptions = arr[2];
  10734. if (title && titleOptions) {
  10735. chart[name] = title = title.destroy(); // remove old
  10736. }
  10737. if (chartTitleOptions && chartTitleOptions.text && !title) {
  10738. chart[name] = chart.renderer.text(
  10739. chartTitleOptions.text,
  10740. 0,
  10741. 0,
  10742. chartTitleOptions.useHTML
  10743. )
  10744. .attr({
  10745. align: chartTitleOptions.align,
  10746. 'class': PREFIX + name,
  10747. zIndex: chartTitleOptions.zIndex || 4
  10748. })
  10749. .css(chartTitleOptions.style)
  10750. .add();
  10751. }
  10752. });
  10753. chart.layOutTitles(redraw);
  10754. },
  10755. /**
  10756. * Lay out the chart titles and cache the full offset height for use in getMargins
  10757. */
  10758. layOutTitles: function (redraw) {
  10759. var titleOffset = 0,
  10760. title = this.title,
  10761. subtitle = this.subtitle,
  10762. options = this.options,
  10763. titleOptions = options.title,
  10764. subtitleOptions = options.subtitle,
  10765. requiresDirtyBox,
  10766. renderer = this.renderer,
  10767. spacingBox = this.spacingBox;
  10768. if (title) {
  10769. title
  10770. .css({ width: (titleOptions.width || spacingBox.width + titleOptions.widthAdjust) + PX })
  10771. .align(extend({
  10772. y: renderer.fontMetrics(titleOptions.style.fontSize, title).b - 3
  10773. }, titleOptions), false, spacingBox);
  10774. if (!titleOptions.floating && !titleOptions.verticalAlign) {
  10775. titleOffset = title.getBBox().height;
  10776. }
  10777. }
  10778. if (subtitle) {
  10779. subtitle
  10780. .css({ width: (subtitleOptions.width || spacingBox.width + subtitleOptions.widthAdjust) + PX })
  10781. .align(extend({
  10782. y: titleOffset + (titleOptions.margin - 13) + renderer.fontMetrics(subtitleOptions.style.fontSize, title).b
  10783. }, subtitleOptions), false, spacingBox);
  10784. if (!subtitleOptions.floating && !subtitleOptions.verticalAlign) {
  10785. titleOffset = mathCeil(titleOffset + subtitle.getBBox().height);
  10786. }
  10787. }
  10788. requiresDirtyBox = this.titleOffset !== titleOffset;
  10789. this.titleOffset = titleOffset; // used in getMargins
  10790. if (!this.isDirtyBox && requiresDirtyBox) {
  10791. this.isDirtyBox = requiresDirtyBox;
  10792. // Redraw if necessary (#2719, #2744)
  10793. if (this.hasRendered && pick(redraw, true) && this.isDirtyBox) {
  10794. this.redraw();
  10795. }
  10796. }
  10797. },
  10798. /**
  10799. * Get chart width and height according to options and container size
  10800. */
  10801. getChartSize: function () {
  10802. var chart = this,
  10803. optionsChart = chart.options.chart,
  10804. widthOption = optionsChart.width,
  10805. heightOption = optionsChart.height,
  10806. renderTo = chart.renderToClone || chart.renderTo;
  10807. // Get inner width and height
  10808. if (!defined(widthOption)) {
  10809. chart.containerWidth = getStyle(renderTo, 'width');
  10810. }
  10811. if (!defined(heightOption)) {
  10812. chart.containerHeight = getStyle(renderTo, 'height');
  10813. }
  10814. chart.chartWidth = mathMax(0, widthOption || chart.containerWidth || 600); // #1393, 1460
  10815. chart.chartHeight = mathMax(0, pick(heightOption,
  10816. // the offsetHeight of an empty container is 0 in standard browsers, but 19 in IE7:
  10817. chart.containerHeight > 19 ? chart.containerHeight : 400));
  10818. },
  10819. /**
  10820. * Create a clone of the chart's renderTo div and place it outside the viewport to allow
  10821. * size computation on chart.render and chart.redraw
  10822. */
  10823. cloneRenderTo: function (revert) {
  10824. var clone = this.renderToClone,
  10825. container = this.container;
  10826. // Destroy the clone and bring the container back to the real renderTo div
  10827. if (revert) {
  10828. if (clone) {
  10829. this.renderTo.appendChild(container);
  10830. discardElement(clone);
  10831. delete this.renderToClone;
  10832. }
  10833. // Set up the clone
  10834. } else {
  10835. if (container && container.parentNode === this.renderTo) {
  10836. this.renderTo.removeChild(container); // do not clone this
  10837. }
  10838. this.renderToClone = clone = this.renderTo.cloneNode(0);
  10839. css(clone, {
  10840. position: ABSOLUTE,
  10841. top: '-9999px',
  10842. display: 'block' // #833
  10843. });
  10844. if (clone.style.setProperty) { // #2631
  10845. clone.style.setProperty('display', 'block', 'important');
  10846. }
  10847. doc.body.appendChild(clone);
  10848. if (container) {
  10849. clone.appendChild(container);
  10850. }
  10851. }
  10852. },
  10853. /**
  10854. * Get the containing element, determine the size and create the inner container
  10855. * div to hold the chart
  10856. */
  10857. getContainer: function () {
  10858. var chart = this,
  10859. container,
  10860. options = chart.options,
  10861. optionsChart = options.chart,
  10862. chartWidth,
  10863. chartHeight,
  10864. renderTo = chart.renderTo,
  10865. indexAttrName = 'data-highcharts-chart',
  10866. oldChartIndex,
  10867. Ren,
  10868. containerId = 'highcharts-' + idCounter++;
  10869. if (!renderTo) {
  10870. chart.renderTo = renderTo = optionsChart.renderTo;
  10871. }
  10872. if (isString(renderTo)) {
  10873. chart.renderTo = renderTo = doc.getElementById(renderTo);
  10874. }
  10875. // Display an error if the renderTo is wrong
  10876. if (!renderTo) {
  10877. error(13, true);
  10878. }
  10879. // If the container already holds a chart, destroy it. The check for hasRendered is there
  10880. // because web pages that are saved to disk from the browser, will preserve the data-highcharts-chart
  10881. // attribute and the SVG contents, but not an interactive chart. So in this case,
  10882. // charts[oldChartIndex] will point to the wrong chart if any (#2609).
  10883. oldChartIndex = pInt(attr(renderTo, indexAttrName));
  10884. if (isNumber(oldChartIndex) && charts[oldChartIndex] && charts[oldChartIndex].hasRendered) {
  10885. charts[oldChartIndex].destroy();
  10886. }
  10887. // Make a reference to the chart from the div
  10888. attr(renderTo, indexAttrName, chart.index);
  10889. // remove previous chart
  10890. renderTo.innerHTML = '';
  10891. // If the container doesn't have an offsetWidth, it has or is a child of a node
  10892. // that has display:none. We need to temporarily move it out to a visible
  10893. // state to determine the size, else the legend and tooltips won't render
  10894. // properly. The allowClone option is used in sparklines as a micro optimization,
  10895. // saving about 1-2 ms each chart.
  10896. if (!optionsChart.skipClone && !renderTo.offsetWidth) {
  10897. chart.cloneRenderTo();
  10898. }
  10899. // get the width and height
  10900. chart.getChartSize();
  10901. chartWidth = chart.chartWidth;
  10902. chartHeight = chart.chartHeight;
  10903. // create the inner container
  10904. chart.container = container = createElement(DIV, {
  10905. className: PREFIX + 'container' +
  10906. (optionsChart.className ? ' ' + optionsChart.className : ''),
  10907. id: containerId
  10908. }, extend({
  10909. position: RELATIVE,
  10910. overflow: HIDDEN, // needed for context menu (avoid scrollbars) and
  10911. // content overflow in IE
  10912. width: chartWidth + PX,
  10913. height: chartHeight + PX,
  10914. textAlign: 'left',
  10915. lineHeight: 'normal', // #427
  10916. zIndex: 0, // #1072
  10917. '-webkit-tap-highlight-color': 'rgba(0,0,0,0)'
  10918. }, optionsChart.style),
  10919. chart.renderToClone || renderTo
  10920. );
  10921. // cache the cursor (#1650)
  10922. chart._cursor = container.style.cursor;
  10923. // Initialize the renderer
  10924. Ren = Highcharts[optionsChart.renderer] || Renderer;
  10925. chart.renderer = new Ren(
  10926. container,
  10927. chartWidth,
  10928. chartHeight,
  10929. optionsChart.style,
  10930. optionsChart.forExport,
  10931. options.exporting && options.exporting.allowHTML
  10932. );
  10933. if (useCanVG) {
  10934. // If we need canvg library, extend and configure the renderer
  10935. // to get the tracker for translating mouse events
  10936. chart.renderer.create(chart, container, chartWidth, chartHeight);
  10937. }
  10938. // Add a reference to the charts index
  10939. chart.renderer.chartIndex = chart.index;
  10940. },
  10941. /**
  10942. * Calculate margins by rendering axis labels in a preliminary position. Title,
  10943. * subtitle and legend have already been rendered at this stage, but will be
  10944. * moved into their final positions
  10945. */
  10946. getMargins: function (skipAxes) {
  10947. var chart = this,
  10948. spacing = chart.spacing,
  10949. margin = chart.margin,
  10950. titleOffset = chart.titleOffset;
  10951. chart.resetMargins();
  10952. // Adjust for title and subtitle
  10953. if (titleOffset && !defined(margin[0])) {
  10954. chart.plotTop = mathMax(chart.plotTop, titleOffset + chart.options.title.margin + spacing[0]);
  10955. }
  10956. // Adjust for legend
  10957. chart.legend.adjustMargins(margin, spacing);
  10958. // adjust for scroller
  10959. if (chart.extraBottomMargin) {
  10960. chart.marginBottom += chart.extraBottomMargin;
  10961. }
  10962. if (chart.extraTopMargin) {
  10963. chart.plotTop += chart.extraTopMargin;
  10964. }
  10965. if (!skipAxes) {
  10966. this.getAxisMargins();
  10967. }
  10968. },
  10969. getAxisMargins: function () {
  10970. var chart = this,
  10971. axisOffset = chart.axisOffset = [0, 0, 0, 0], // top, right, bottom, left
  10972. margin = chart.margin;
  10973. // pre-render axes to get labels offset width
  10974. if (chart.hasCartesianSeries) {
  10975. each(chart.axes, function (axis) {
  10976. if (axis.visible) {
  10977. axis.getOffset();
  10978. }
  10979. });
  10980. }
  10981. // Add the axis offsets
  10982. each(marginNames, function (m, side) {
  10983. if (!defined(margin[side])) {
  10984. chart[m] += axisOffset[side];
  10985. }
  10986. });
  10987. chart.setChartSize();
  10988. },
  10989. /**
  10990. * Resize the chart to its container if size is not explicitly set
  10991. */
  10992. reflow: function (e) {
  10993. var chart = this,
  10994. optionsChart = chart.options.chart,
  10995. renderTo = chart.renderTo,
  10996. width = optionsChart.width || getStyle(renderTo, 'width'),
  10997. height = optionsChart.height || getStyle(renderTo, 'height'),
  10998. target = e ? e.target : win;
  10999. // Width and height checks for display:none. Target is doc in IE8 and Opera,
  11000. // win in Firefox, Chrome and IE9.
  11001. if (!chart.hasUserSize && !chart.isPrinting && width && height && (target === win || target === doc)) { // #1093
  11002. if (width !== chart.containerWidth || height !== chart.containerHeight) {
  11003. clearTimeout(chart.reflowTimeout);
  11004. // When called from window.resize, e is set, else it's called directly (#2224)
  11005. chart.reflowTimeout = syncTimeout(function () {
  11006. if (chart.container) { // It may have been destroyed in the meantime (#1257)
  11007. chart.setSize(width, height, false);
  11008. chart.hasUserSize = null;
  11009. }
  11010. }, e ? 100 : 0);
  11011. }
  11012. chart.containerWidth = width;
  11013. chart.containerHeight = height;
  11014. }
  11015. },
  11016. /**
  11017. * Add the event handlers necessary for auto resizing
  11018. */
  11019. initReflow: function () {
  11020. var chart = this,
  11021. reflow = function (e) {
  11022. chart.reflow(e);
  11023. };
  11024. addEvent(win, 'resize', reflow);
  11025. addEvent(chart, 'destroy', function () {
  11026. removeEvent(win, 'resize', reflow);
  11027. });
  11028. },
  11029. /**
  11030. * Resize the chart to a given width and height
  11031. * @param {Number} width
  11032. * @param {Number} height
  11033. * @param {Object|Boolean} animation
  11034. */
  11035. setSize: function (width, height, animation) {
  11036. var chart = this,
  11037. chartWidth,
  11038. chartHeight,
  11039. renderer = chart.renderer,
  11040. globalAnimation;
  11041. // Handle the isResizing counter
  11042. chart.isResizing += 1;
  11043. // set the animation for the current process
  11044. setAnimation(animation, chart);
  11045. chart.oldChartHeight = chart.chartHeight;
  11046. chart.oldChartWidth = chart.chartWidth;
  11047. if (defined(width)) {
  11048. chart.chartWidth = chartWidth = mathMax(0, mathRound(width));
  11049. chart.hasUserSize = !!chartWidth;
  11050. }
  11051. if (defined(height)) {
  11052. chart.chartHeight = chartHeight = mathMax(0, mathRound(height));
  11053. }
  11054. // Resize the container with the global animation applied if enabled (#2503)
  11055. globalAnimation = renderer.globalAnimation;
  11056. (globalAnimation ? animate : css)(chart.container, {
  11057. width: chartWidth + PX,
  11058. height: chartHeight + PX
  11059. }, globalAnimation);
  11060. chart.setChartSize(true);
  11061. renderer.setSize(chartWidth, chartHeight, animation);
  11062. // handle axes
  11063. chart.maxTicks = null;
  11064. each(chart.axes, function (axis) {
  11065. axis.isDirty = true;
  11066. axis.setScale();
  11067. });
  11068. // make sure non-cartesian series are also handled
  11069. each(chart.series, function (serie) {
  11070. serie.isDirty = true;
  11071. });
  11072. chart.isDirtyLegend = true; // force legend redraw
  11073. chart.isDirtyBox = true; // force redraw of plot and chart border
  11074. chart.layOutTitles(); // #2857
  11075. chart.getMargins();
  11076. chart.redraw(animation);
  11077. chart.oldChartHeight = null;
  11078. fireEvent(chart, 'resize');
  11079. // Fire endResize and set isResizing back. If animation is disabled, fire without delay
  11080. syncTimeout(function () {
  11081. if (chart) {
  11082. fireEvent(chart, 'endResize', null, function () {
  11083. chart.isResizing -= 1;
  11084. });
  11085. }
  11086. }, animObject(globalAnimation).duration);
  11087. },
  11088. /**
  11089. * Set the public chart properties. This is done before and after the pre-render
  11090. * to determine margin sizes
  11091. */
  11092. setChartSize: function (skipAxes) {
  11093. var chart = this,
  11094. inverted = chart.inverted,
  11095. renderer = chart.renderer,
  11096. chartWidth = chart.chartWidth,
  11097. chartHeight = chart.chartHeight,
  11098. optionsChart = chart.options.chart,
  11099. spacing = chart.spacing,
  11100. clipOffset = chart.clipOffset,
  11101. clipX,
  11102. clipY,
  11103. plotLeft,
  11104. plotTop,
  11105. plotWidth,
  11106. plotHeight,
  11107. plotBorderWidth;
  11108. chart.plotLeft = plotLeft = mathRound(chart.plotLeft);
  11109. chart.plotTop = plotTop = mathRound(chart.plotTop);
  11110. chart.plotWidth = plotWidth = mathMax(0, mathRound(chartWidth - plotLeft - chart.marginRight));
  11111. chart.plotHeight = plotHeight = mathMax(0, mathRound(chartHeight - plotTop - chart.marginBottom));
  11112. chart.plotSizeX = inverted ? plotHeight : plotWidth;
  11113. chart.plotSizeY = inverted ? plotWidth : plotHeight;
  11114. chart.plotBorderWidth = optionsChart.plotBorderWidth || 0;
  11115. // Set boxes used for alignment
  11116. chart.spacingBox = renderer.spacingBox = {
  11117. x: spacing[3],
  11118. y: spacing[0],
  11119. width: chartWidth - spacing[3] - spacing[1],
  11120. height: chartHeight - spacing[0] - spacing[2]
  11121. };
  11122. chart.plotBox = renderer.plotBox = {
  11123. x: plotLeft,
  11124. y: plotTop,
  11125. width: plotWidth,
  11126. height: plotHeight
  11127. };
  11128. plotBorderWidth = 2 * mathFloor(chart.plotBorderWidth / 2);
  11129. clipX = mathCeil(mathMax(plotBorderWidth, clipOffset[3]) / 2);
  11130. clipY = mathCeil(mathMax(plotBorderWidth, clipOffset[0]) / 2);
  11131. chart.clipBox = {
  11132. x: clipX,
  11133. y: clipY,
  11134. width: mathFloor(chart.plotSizeX - mathMax(plotBorderWidth, clipOffset[1]) / 2 - clipX),
  11135. height: mathMax(0, mathFloor(chart.plotSizeY - mathMax(plotBorderWidth, clipOffset[2]) / 2 - clipY))
  11136. };
  11137. if (!skipAxes) {
  11138. each(chart.axes, function (axis) {
  11139. axis.setAxisSize();
  11140. axis.setAxisTranslation();
  11141. });
  11142. }
  11143. },
  11144. /**
  11145. * Initial margins before auto size margins are applied
  11146. */
  11147. resetMargins: function () {
  11148. var chart = this;
  11149. each(marginNames, function (m, side) {
  11150. chart[m] = pick(chart.margin[side], chart.spacing[side]);
  11151. });
  11152. chart.axisOffset = [0, 0, 0, 0]; // top, right, bottom, left
  11153. chart.clipOffset = [0, 0, 0, 0];
  11154. },
  11155. /**
  11156. * Draw the borders and backgrounds for chart and plot area
  11157. */
  11158. drawChartBox: function () {
  11159. var chart = this,
  11160. optionsChart = chart.options.chart,
  11161. renderer = chart.renderer,
  11162. chartWidth = chart.chartWidth,
  11163. chartHeight = chart.chartHeight,
  11164. chartBackground = chart.chartBackground,
  11165. plotBackground = chart.plotBackground,
  11166. plotBorder = chart.plotBorder,
  11167. plotBGImage = chart.plotBGImage,
  11168. chartBorderWidth = optionsChart.borderWidth || 0,
  11169. chartBackgroundColor = optionsChart.backgroundColor,
  11170. plotBackgroundColor = optionsChart.plotBackgroundColor,
  11171. plotBackgroundImage = optionsChart.plotBackgroundImage,
  11172. plotBorderWidth = optionsChart.plotBorderWidth || 0,
  11173. mgn,
  11174. bgAttr,
  11175. plotLeft = chart.plotLeft,
  11176. plotTop = chart.plotTop,
  11177. plotWidth = chart.plotWidth,
  11178. plotHeight = chart.plotHeight,
  11179. plotBox = chart.plotBox,
  11180. clipRect = chart.clipRect,
  11181. clipBox = chart.clipBox;
  11182. // Chart area
  11183. mgn = chartBorderWidth + (optionsChart.shadow ? 8 : 0);
  11184. if (chartBorderWidth || chartBackgroundColor) {
  11185. if (!chartBackground) {
  11186. bgAttr = {
  11187. fill: chartBackgroundColor || NONE
  11188. };
  11189. if (chartBorderWidth) { // #980
  11190. bgAttr.stroke = optionsChart.borderColor;
  11191. bgAttr['stroke-width'] = chartBorderWidth;
  11192. }
  11193. chart.chartBackground = renderer.rect(mgn / 2, mgn / 2, chartWidth - mgn, chartHeight - mgn,
  11194. optionsChart.borderRadius, chartBorderWidth)
  11195. .attr(bgAttr)
  11196. .addClass(PREFIX + 'background')
  11197. .add()
  11198. .shadow(optionsChart.shadow);
  11199. } else { // resize
  11200. chartBackground.animate(
  11201. chartBackground.crisp({ width: chartWidth - mgn, height: chartHeight - mgn })
  11202. );
  11203. }
  11204. }
  11205. // Plot background
  11206. if (plotBackgroundColor) {
  11207. if (!plotBackground) {
  11208. chart.plotBackground = renderer.rect(plotLeft, plotTop, plotWidth, plotHeight, 0)
  11209. .attr({
  11210. fill: plotBackgroundColor
  11211. })
  11212. .add()
  11213. .shadow(optionsChart.plotShadow);
  11214. } else {
  11215. plotBackground.animate(plotBox);
  11216. }
  11217. }
  11218. if (plotBackgroundImage) {
  11219. if (!plotBGImage) {
  11220. chart.plotBGImage = renderer.image(plotBackgroundImage, plotLeft, plotTop, plotWidth, plotHeight)
  11221. .add();
  11222. } else {
  11223. plotBGImage.animate(plotBox);
  11224. }
  11225. }
  11226. // Plot clip
  11227. if (!clipRect) {
  11228. chart.clipRect = renderer.clipRect(clipBox);
  11229. } else {
  11230. clipRect.animate({
  11231. width: clipBox.width,
  11232. height: clipBox.height
  11233. });
  11234. }
  11235. // Plot area border
  11236. if (plotBorderWidth) {
  11237. if (!plotBorder) {
  11238. chart.plotBorder = renderer.rect(plotLeft, plotTop, plotWidth, plotHeight, 0, -plotBorderWidth)
  11239. .attr({
  11240. stroke: optionsChart.plotBorderColor,
  11241. 'stroke-width': plotBorderWidth,
  11242. fill: NONE,
  11243. zIndex: 1
  11244. })
  11245. .add();
  11246. } else {
  11247. plotBorder.strokeWidth = -plotBorderWidth;
  11248. plotBorder.animate(
  11249. plotBorder.crisp({ x: plotLeft, y: plotTop, width: plotWidth, height: plotHeight }) //#3282 plotBorder should be negative
  11250. );
  11251. }
  11252. }
  11253. // reset
  11254. chart.isDirtyBox = false;
  11255. },
  11256. /**
  11257. * Detect whether a certain chart property is needed based on inspecting its options
  11258. * and series. This mainly applies to the chart.invert property, and in extensions to
  11259. * the chart.angular and chart.polar properties.
  11260. */
  11261. propFromSeries: function () {
  11262. var chart = this,
  11263. optionsChart = chart.options.chart,
  11264. klass,
  11265. seriesOptions = chart.options.series,
  11266. i,
  11267. value;
  11268. each(['inverted', 'angular', 'polar'], function (key) {
  11269. // The default series type's class
  11270. klass = seriesTypes[optionsChart.type || optionsChart.defaultSeriesType];
  11271. // Get the value from available chart-wide properties
  11272. value = (
  11273. chart[key] || // 1. it is set before
  11274. optionsChart[key] || // 2. it is set in the options
  11275. (klass && klass.prototype[key]) // 3. it's default series class requires it
  11276. );
  11277. // 4. Check if any the chart's series require it
  11278. i = seriesOptions && seriesOptions.length;
  11279. while (!value && i--) {
  11280. klass = seriesTypes[seriesOptions[i].type];
  11281. if (klass && klass.prototype[key]) {
  11282. value = true;
  11283. }
  11284. }
  11285. // Set the chart property
  11286. chart[key] = value;
  11287. });
  11288. },
  11289. /**
  11290. * Link two or more series together. This is done initially from Chart.render,
  11291. * and after Chart.addSeries and Series.remove.
  11292. */
  11293. linkSeries: function () {
  11294. var chart = this,
  11295. chartSeries = chart.series;
  11296. // Reset links
  11297. each(chartSeries, function (series) {
  11298. series.linkedSeries.length = 0;
  11299. });
  11300. // Apply new links
  11301. each(chartSeries, function (series) {
  11302. var linkedTo = series.options.linkedTo;
  11303. if (isString(linkedTo)) {
  11304. if (linkedTo === ':previous') {
  11305. linkedTo = chart.series[series.index - 1];
  11306. } else {
  11307. linkedTo = chart.get(linkedTo);
  11308. }
  11309. if (linkedTo) {
  11310. linkedTo.linkedSeries.push(series);
  11311. series.linkedParent = linkedTo;
  11312. series.visible = pick(series.options.visible, linkedTo.options.visible, series.visible); // #3879
  11313. }
  11314. }
  11315. });
  11316. },
  11317. /**
  11318. * Render series for the chart
  11319. */
  11320. renderSeries: function () {
  11321. each(this.series, function (serie) {
  11322. serie.translate();
  11323. serie.render();
  11324. });
  11325. },
  11326. /**
  11327. * Render labels for the chart
  11328. */
  11329. renderLabels: function () {
  11330. var chart = this,
  11331. labels = chart.options.labels;
  11332. if (labels.items) {
  11333. each(labels.items, function (label) {
  11334. var style = extend(labels.style, label.style),
  11335. x = pInt(style.left) + chart.plotLeft,
  11336. y = pInt(style.top) + chart.plotTop + 12;
  11337. // delete to prevent rewriting in IE
  11338. delete style.left;
  11339. delete style.top;
  11340. chart.renderer.text(
  11341. label.html,
  11342. x,
  11343. y
  11344. )
  11345. .attr({ zIndex: 2 })
  11346. .css(style)
  11347. .add();
  11348. });
  11349. }
  11350. },
  11351. /**
  11352. * Render all graphics for the chart
  11353. */
  11354. render: function () {
  11355. var chart = this,
  11356. axes = chart.axes,
  11357. renderer = chart.renderer,
  11358. options = chart.options,
  11359. tempWidth,
  11360. tempHeight,
  11361. redoHorizontal,
  11362. redoVertical;
  11363. // Title
  11364. chart.setTitle();
  11365. // Legend
  11366. chart.legend = new Legend(chart, options.legend);
  11367. // Get stacks
  11368. if (chart.getStacks) {
  11369. chart.getStacks();
  11370. }
  11371. // Get chart margins
  11372. chart.getMargins(true);
  11373. chart.setChartSize();
  11374. // Record preliminary dimensions for later comparison
  11375. tempWidth = chart.plotWidth;
  11376. tempHeight = chart.plotHeight = chart.plotHeight - 21; // 21 is the most common correction for X axis labels
  11377. // Get margins by pre-rendering axes
  11378. each(axes, function (axis) {
  11379. axis.setScale();
  11380. });
  11381. chart.getAxisMargins();
  11382. // If the plot area size has changed significantly, calculate tick positions again
  11383. redoHorizontal = tempWidth / chart.plotWidth > 1.1;
  11384. redoVertical = tempHeight / chart.plotHeight > 1.05; // Height is more sensitive
  11385. if (redoHorizontal || redoVertical) {
  11386. chart.maxTicks = null; // reset for second pass
  11387. each(axes, function (axis) {
  11388. if ((axis.horiz && redoHorizontal) || (!axis.horiz && redoVertical)) {
  11389. axis.setTickInterval(true); // update to reflect the new margins
  11390. }
  11391. });
  11392. chart.getMargins(); // second pass to check for new labels
  11393. }
  11394. // Draw the borders and backgrounds
  11395. chart.drawChartBox();
  11396. // Axes
  11397. if (chart.hasCartesianSeries) {
  11398. each(axes, function (axis) {
  11399. if (axis.visible) {
  11400. axis.render();
  11401. }
  11402. });
  11403. }
  11404. // The series
  11405. if (!chart.seriesGroup) {
  11406. chart.seriesGroup = renderer.g('series-group')
  11407. .attr({ zIndex: 3 })
  11408. .add();
  11409. }
  11410. chart.renderSeries();
  11411. // Labels
  11412. chart.renderLabels();
  11413. // Credits
  11414. chart.showCredits(options.credits);
  11415. // Set flag
  11416. chart.hasRendered = true;
  11417. },
  11418. /**
  11419. * Show chart credits based on config options
  11420. */
  11421. showCredits: function (credits) {
  11422. if (credits.enabled && !this.credits) {
  11423. this.credits = this.renderer.text(
  11424. credits.text,
  11425. 0,
  11426. 0
  11427. )
  11428. .on('click', function () {
  11429. if (credits.href) {
  11430. win.location.href = credits.href;
  11431. }
  11432. })
  11433. .attr({
  11434. align: credits.position.align,
  11435. zIndex: 8
  11436. })
  11437. .css(credits.style)
  11438. .add()
  11439. .align(credits.position);
  11440. }
  11441. },
  11442. /**
  11443. * Clean up memory usage
  11444. */
  11445. destroy: function () {
  11446. var chart = this,
  11447. axes = chart.axes,
  11448. series = chart.series,
  11449. container = chart.container,
  11450. i,
  11451. parentNode = container && container.parentNode;
  11452. // fire the chart.destoy event
  11453. fireEvent(chart, 'destroy');
  11454. // Delete the chart from charts lookup array
  11455. charts[chart.index] = UNDEFINED;
  11456. chartCount--;
  11457. chart.renderTo.removeAttribute('data-highcharts-chart');
  11458. // remove events
  11459. removeEvent(chart);
  11460. // ==== Destroy collections:
  11461. // Destroy axes
  11462. i = axes.length;
  11463. while (i--) {
  11464. axes[i] = axes[i].destroy();
  11465. }
  11466. // Destroy each series
  11467. i = series.length;
  11468. while (i--) {
  11469. series[i] = series[i].destroy();
  11470. }
  11471. // ==== Destroy chart properties:
  11472. each(['title', 'subtitle', 'chartBackground', 'plotBackground', 'plotBGImage',
  11473. 'plotBorder', 'seriesGroup', 'clipRect', 'credits', 'pointer', 'scroller',
  11474. 'rangeSelector', 'legend', 'resetZoomButton', 'tooltip', 'renderer'], function (name) {
  11475. var prop = chart[name];
  11476. if (prop && prop.destroy) {
  11477. chart[name] = prop.destroy();
  11478. }
  11479. });
  11480. // remove container and all SVG
  11481. if (container) { // can break in IE when destroyed before finished loading
  11482. container.innerHTML = '';
  11483. removeEvent(container);
  11484. if (parentNode) {
  11485. discardElement(container);
  11486. }
  11487. }
  11488. // clean it all up
  11489. for (i in chart) {
  11490. delete chart[i];
  11491. }
  11492. },
  11493. /**
  11494. * VML namespaces can't be added until after complete. Listening
  11495. * for Perini's doScroll hack is not enough.
  11496. */
  11497. isReadyToRender: function () {
  11498. var chart = this;
  11499. // Note: win == win.top is required
  11500. if ((!hasSVG && (win == win.top && doc.readyState !== 'complete')) || (useCanVG && !win.canvg)) { // eslint-disable-line eqeqeq
  11501. if (useCanVG) {
  11502. // Delay rendering until canvg library is downloaded and ready
  11503. CanVGController.push(function () {
  11504. chart.firstRender();
  11505. }, chart.options.global.canvasToolsURL);
  11506. } else {
  11507. doc.attachEvent('onreadystatechange', function () {
  11508. doc.detachEvent('onreadystatechange', chart.firstRender);
  11509. if (doc.readyState === 'complete') {
  11510. chart.firstRender();
  11511. }
  11512. });
  11513. }
  11514. return false;
  11515. }
  11516. return true;
  11517. },
  11518. /**
  11519. * Prepare for first rendering after all data are loaded
  11520. */
  11521. firstRender: function () {
  11522. var chart = this,
  11523. options = chart.options;
  11524. // Check whether the chart is ready to render
  11525. if (!chart.isReadyToRender()) {
  11526. return;
  11527. }
  11528. // Create the container
  11529. chart.getContainer();
  11530. // Run an early event after the container and renderer are established
  11531. fireEvent(chart, 'init');
  11532. chart.resetMargins();
  11533. chart.setChartSize();
  11534. // Set the common chart properties (mainly invert) from the given series
  11535. chart.propFromSeries();
  11536. // get axes
  11537. chart.getAxes();
  11538. // Initialize the series
  11539. each(options.series || [], function (serieOptions) {
  11540. chart.initSeries(serieOptions);
  11541. });
  11542. chart.linkSeries();
  11543. // Run an event after axes and series are initialized, but before render. At this stage,
  11544. // the series data is indexed and cached in the xData and yData arrays, so we can access
  11545. // those before rendering. Used in Highstock.
  11546. fireEvent(chart, 'beforeRender');
  11547. // depends on inverted and on margins being set
  11548. if (Highcharts.Pointer) {
  11549. chart.pointer = new Pointer(chart, options);
  11550. }
  11551. chart.render();
  11552. // add canvas
  11553. chart.renderer.draw();
  11554. // Fire the load event if there are no external images
  11555. if (!chart.renderer.imgCount && chart.onload) {
  11556. chart.onload();
  11557. }
  11558. // If the chart was rendered outside the top container, put it back in (#3679)
  11559. chart.cloneRenderTo(true);
  11560. },
  11561. /**
  11562. * On chart load
  11563. */
  11564. onload: function () {
  11565. var chart = this;
  11566. // Run callbacks
  11567. each([this.callback].concat(this.callbacks), function (fn) {
  11568. if (fn && chart.index !== undefined) { // Chart destroyed in its own callback (#3600)
  11569. fn.apply(chart, [chart]);
  11570. }
  11571. });
  11572. fireEvent(chart, 'load');
  11573. // Don't run again
  11574. this.onload = null;
  11575. },
  11576. /**
  11577. * Creates arrays for spacing and margin from given options.
  11578. */
  11579. splashArray: function (target, options) {
  11580. var oVar = options[target],
  11581. tArray = isObject(oVar) ? oVar : [oVar, oVar, oVar, oVar];
  11582. return [pick(options[target + 'Top'], tArray[0]),
  11583. pick(options[target + 'Right'], tArray[1]),
  11584. pick(options[target + 'Bottom'], tArray[2]),
  11585. pick(options[target + 'Left'], tArray[3])];
  11586. }
  11587. }; // end Chart
  11588. var CenteredSeriesMixin = Highcharts.CenteredSeriesMixin = {
  11589. /**
  11590. * Get the center of the pie based on the size and center options relative to the
  11591. * plot area. Borrowed by the polar and gauge series types.
  11592. */
  11593. getCenter: function () {
  11594. var options = this.options,
  11595. chart = this.chart,
  11596. slicingRoom = 2 * (options.slicedOffset || 0),
  11597. handleSlicingRoom,
  11598. plotWidth = chart.plotWidth - 2 * slicingRoom,
  11599. plotHeight = chart.plotHeight - 2 * slicingRoom,
  11600. centerOption = options.center,
  11601. positions = [pick(centerOption[0], '50%'), pick(centerOption[1], '50%'), options.size || '100%', options.innerSize || 0],
  11602. smallestSize = mathMin(plotWidth, plotHeight),
  11603. i,
  11604. value;
  11605. for (i = 0; i < 4; ++i) {
  11606. value = positions[i];
  11607. handleSlicingRoom = i < 2 || (i === 2 && /%$/.test(value));
  11608. // i == 0: centerX, relative to width
  11609. // i == 1: centerY, relative to height
  11610. // i == 2: size, relative to smallestSize
  11611. // i == 3: innerSize, relative to size
  11612. positions[i] = relativeLength(value, [plotWidth, plotHeight, smallestSize, positions[2]][i]) +
  11613. (handleSlicingRoom ? slicingRoom : 0);
  11614. }
  11615. // innerSize cannot be larger than size (#3632)
  11616. if (positions[3] > positions[2]) {
  11617. positions[3] = positions[2];
  11618. }
  11619. return positions;
  11620. }
  11621. };
  11622. /**
  11623. * The Point object and prototype. Inheritable and used as base for PiePoint
  11624. */
  11625. var Point = function () {};
  11626. Point.prototype = {
  11627. /**
  11628. * Initialize the point
  11629. * @param {Object} series The series object containing this point
  11630. * @param {Object} options The data in either number, array or object format
  11631. */
  11632. init: function (series, options, x) {
  11633. var point = this,
  11634. colors;
  11635. point.series = series;
  11636. point.color = series.color; // #3445
  11637. point.applyOptions(options, x);
  11638. point.pointAttr = {};
  11639. if (series.options.colorByPoint) {
  11640. colors = series.options.colors || series.chart.options.colors;
  11641. point.color = point.color || colors[series.colorCounter++];
  11642. // loop back to zero
  11643. if (series.colorCounter === colors.length) {
  11644. series.colorCounter = 0;
  11645. }
  11646. }
  11647. series.chart.pointCount++;
  11648. return point;
  11649. },
  11650. /**
  11651. * Apply the options containing the x and y data and possible some extra properties.
  11652. * This is called on point init or from point.update.
  11653. *
  11654. * @param {Object} options
  11655. */
  11656. applyOptions: function (options, x) {
  11657. var point = this,
  11658. series = point.series,
  11659. pointValKey = series.options.pointValKey || series.pointValKey;
  11660. options = Point.prototype.optionsToObject.call(this, options);
  11661. // copy options directly to point
  11662. extend(point, options);
  11663. point.options = point.options ? extend(point.options, options) : options;
  11664. // For higher dimension series types. For instance, for ranges, point.y is mapped to point.low.
  11665. if (pointValKey) {
  11666. point.y = point[pointValKey];
  11667. }
  11668. point.isNull = point.x === null || point.y === null;
  11669. // If no x is set by now, get auto incremented value. All points must have an
  11670. // x value, however the y value can be null to create a gap in the series
  11671. if (point.x === undefined && series) {
  11672. point.x = x === undefined ? series.autoIncrement() : x;
  11673. }
  11674. return point;
  11675. },
  11676. /**
  11677. * Transform number or array configs into objects
  11678. */
  11679. optionsToObject: function (options) {
  11680. var ret = {},
  11681. series = this.series,
  11682. keys = series.options.keys,
  11683. pointArrayMap = keys || series.pointArrayMap || ['y'],
  11684. valueCount = pointArrayMap.length,
  11685. firstItemType,
  11686. i = 0,
  11687. j = 0;
  11688. if (isNumber(options) || options === null) {
  11689. ret[pointArrayMap[0]] = options;
  11690. } else if (isArray(options)) {
  11691. // with leading x value
  11692. if (!keys && options.length > valueCount) {
  11693. firstItemType = typeof options[0];
  11694. if (firstItemType === 'string') {
  11695. ret.name = options[0];
  11696. } else if (firstItemType === 'number') {
  11697. ret.x = options[0];
  11698. }
  11699. i++;
  11700. }
  11701. while (j < valueCount) {
  11702. if (!keys || options[i] !== undefined) { // Skip undefined positions for keys
  11703. ret[pointArrayMap[j]] = options[i];
  11704. }
  11705. i++;
  11706. j++;
  11707. }
  11708. } else if (typeof options === 'object') {
  11709. ret = options;
  11710. // This is the fastest way to detect if there are individual point dataLabels that need
  11711. // to be considered in drawDataLabels. These can only occur in object configs.
  11712. if (options.dataLabels) {
  11713. series._hasPointLabels = true;
  11714. }
  11715. // Same approach as above for markers
  11716. if (options.marker) {
  11717. series._hasPointMarkers = true;
  11718. }
  11719. }
  11720. return ret;
  11721. },
  11722. /**
  11723. * Destroy a point to clear memory. Its reference still stays in series.data.
  11724. */
  11725. destroy: function () {
  11726. var point = this,
  11727. series = point.series,
  11728. chart = series.chart,
  11729. hoverPoints = chart.hoverPoints,
  11730. prop;
  11731. chart.pointCount--;
  11732. if (hoverPoints) {
  11733. point.setState();
  11734. erase(hoverPoints, point);
  11735. if (!hoverPoints.length) {
  11736. chart.hoverPoints = null;
  11737. }
  11738. }
  11739. if (point === chart.hoverPoint) {
  11740. point.onMouseOut();
  11741. }
  11742. // remove all events
  11743. if (point.graphic || point.dataLabel) { // removeEvent and destroyElements are performance expensive
  11744. removeEvent(point);
  11745. point.destroyElements();
  11746. }
  11747. if (point.legendItem) { // pies have legend items
  11748. chart.legend.destroyItem(point);
  11749. }
  11750. for (prop in point) {
  11751. point[prop] = null;
  11752. }
  11753. },
  11754. /**
  11755. * Destroy SVG elements associated with the point
  11756. */
  11757. destroyElements: function () {
  11758. var point = this,
  11759. props = ['graphic', 'dataLabel', 'dataLabelUpper', 'connector', 'shadowGroup'],
  11760. prop,
  11761. i = 6;
  11762. while (i--) {
  11763. prop = props[i];
  11764. if (point[prop]) {
  11765. point[prop] = point[prop].destroy();
  11766. }
  11767. }
  11768. },
  11769. /**
  11770. * Return the configuration hash needed for the data label and tooltip formatters
  11771. */
  11772. getLabelConfig: function () {
  11773. return {
  11774. x: this.category,
  11775. y: this.y,
  11776. color: this.color,
  11777. key: this.name || this.category,
  11778. series: this.series,
  11779. point: this,
  11780. percentage: this.percentage,
  11781. total: this.total || this.stackTotal
  11782. };
  11783. },
  11784. /**
  11785. * Extendable method for formatting each point's tooltip line
  11786. *
  11787. * @return {String} A string to be concatenated in to the common tooltip text
  11788. */
  11789. tooltipFormatter: function (pointFormat) {
  11790. // Insert options for valueDecimals, valuePrefix, and valueSuffix
  11791. var series = this.series,
  11792. seriesTooltipOptions = series.tooltipOptions,
  11793. valueDecimals = pick(seriesTooltipOptions.valueDecimals, ''),
  11794. valuePrefix = seriesTooltipOptions.valuePrefix || '',
  11795. valueSuffix = seriesTooltipOptions.valueSuffix || '';
  11796. // Loop over the point array map and replace unformatted values with sprintf formatting markup
  11797. each(series.pointArrayMap || ['y'], function (key) {
  11798. key = '{point.' + key; // without the closing bracket
  11799. if (valuePrefix || valueSuffix) {
  11800. pointFormat = pointFormat.replace(key + '}', valuePrefix + key + '}' + valueSuffix);
  11801. }
  11802. pointFormat = pointFormat.replace(key + '}', key + ':,.' + valueDecimals + 'f}');
  11803. });
  11804. return format(pointFormat, {
  11805. point: this,
  11806. series: this.series
  11807. });
  11808. },
  11809. /**
  11810. * Fire an event on the Point object.
  11811. * @param {String} eventType
  11812. * @param {Object} eventArgs Additional event arguments
  11813. * @param {Function} defaultFunction Default event handler
  11814. */
  11815. firePointEvent: function (eventType, eventArgs, defaultFunction) {
  11816. var point = this,
  11817. series = this.series,
  11818. seriesOptions = series.options;
  11819. // load event handlers on demand to save time on mouseover/out
  11820. if (seriesOptions.point.events[eventType] || (point.options && point.options.events && point.options.events[eventType])) {
  11821. this.importEvents();
  11822. }
  11823. // add default handler if in selection mode
  11824. if (eventType === 'click' && seriesOptions.allowPointSelect) {
  11825. defaultFunction = function (event) {
  11826. // Control key is for Windows, meta (= Cmd key) for Mac, Shift for Opera
  11827. if (point.select) { // Could be destroyed by prior event handlers (#2911)
  11828. point.select(null, event.ctrlKey || event.metaKey || event.shiftKey);
  11829. }
  11830. };
  11831. }
  11832. fireEvent(this, eventType, eventArgs, defaultFunction);
  11833. },
  11834. visible: true
  11835. };/**
  11836. * @classDescription The base function which all other series types inherit from. The data in the series is stored
  11837. * in various arrays.
  11838. *
  11839. * - First, series.options.data contains all the original config options for
  11840. * each point whether added by options or methods like series.addPoint.
  11841. * - Next, series.data contains those values converted to points, but in case the series data length
  11842. * exceeds the cropThreshold, or if the data is grouped, series.data doesn't contain all the points. It
  11843. * only contains the points that have been created on demand.
  11844. * - Then there's series.points that contains all currently visible point objects. In case of cropping,
  11845. * the cropped-away points are not part of this array. The series.points array starts at series.cropStart
  11846. * compared to series.data and series.options.data. If however the series data is grouped, these can't
  11847. * be correlated one to one.
  11848. * - series.xData and series.processedXData contain clean x values, equivalent to series.data and series.points.
  11849. * - series.yData and series.processedYData contain clean x values, equivalent to series.data and series.points.
  11850. *
  11851. * @param {Object} chart
  11852. * @param {Object} options
  11853. */
  11854. var Series = Highcharts.Series = function () {};
  11855. Series.prototype = {
  11856. isCartesian: true,
  11857. type: 'line',
  11858. pointClass: Point,
  11859. sorted: true, // requires the data to be sorted
  11860. requireSorting: true,
  11861. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  11862. stroke: 'lineColor',
  11863. 'stroke-width': 'lineWidth',
  11864. fill: 'fillColor',
  11865. r: 'radius'
  11866. },
  11867. directTouch: false,
  11868. axisTypes: ['xAxis', 'yAxis'],
  11869. colorCounter: 0,
  11870. parallelArrays: ['x', 'y'], // each point's x and y values are stored in this.xData and this.yData
  11871. init: function (chart, options) {
  11872. var series = this,
  11873. eventType,
  11874. events,
  11875. chartSeries = chart.series,
  11876. sortByIndex = function (a, b) {
  11877. return pick(a.options.index, a._i) - pick(b.options.index, b._i);
  11878. };
  11879. series.chart = chart;
  11880. series.options = options = series.setOptions(options); // merge with plotOptions
  11881. series.linkedSeries = [];
  11882. // bind the axes
  11883. series.bindAxes();
  11884. // set some variables
  11885. extend(series, {
  11886. name: options.name,
  11887. state: NORMAL_STATE,
  11888. pointAttr: {},
  11889. visible: options.visible !== false, // true by default
  11890. selected: options.selected === true // false by default
  11891. });
  11892. // special
  11893. if (useCanVG) {
  11894. options.animation = false;
  11895. }
  11896. // register event listeners
  11897. events = options.events;
  11898. for (eventType in events) {
  11899. addEvent(series, eventType, events[eventType]);
  11900. }
  11901. if (
  11902. (events && events.click) ||
  11903. (options.point && options.point.events && options.point.events.click) ||
  11904. options.allowPointSelect
  11905. ) {
  11906. chart.runTrackerClick = true;
  11907. }
  11908. series.getColor();
  11909. series.getSymbol();
  11910. // Set the data
  11911. each(series.parallelArrays, function (key) {
  11912. series[key + 'Data'] = [];
  11913. });
  11914. series.setData(options.data, false);
  11915. // Mark cartesian
  11916. if (series.isCartesian) {
  11917. chart.hasCartesianSeries = true;
  11918. }
  11919. // Register it in the chart
  11920. chartSeries.push(series);
  11921. series._i = chartSeries.length - 1;
  11922. // Sort series according to index option (#248, #1123, #2456)
  11923. stableSort(chartSeries, sortByIndex);
  11924. if (this.yAxis) {
  11925. stableSort(this.yAxis.series, sortByIndex);
  11926. }
  11927. each(chartSeries, function (series, i) {
  11928. series.index = i;
  11929. series.name = series.name || 'Series ' + (i + 1);
  11930. });
  11931. },
  11932. /**
  11933. * Set the xAxis and yAxis properties of cartesian series, and register the series
  11934. * in the axis.series array
  11935. */
  11936. bindAxes: function () {
  11937. var series = this,
  11938. seriesOptions = series.options,
  11939. chart = series.chart,
  11940. axisOptions;
  11941. each(series.axisTypes || [], function (AXIS) { // repeat for xAxis and yAxis
  11942. each(chart[AXIS], function (axis) { // loop through the chart's axis objects
  11943. axisOptions = axis.options;
  11944. // apply if the series xAxis or yAxis option mathches the number of the
  11945. // axis, or if undefined, use the first axis
  11946. if ((seriesOptions[AXIS] === axisOptions.index) ||
  11947. (seriesOptions[AXIS] !== UNDEFINED && seriesOptions[AXIS] === axisOptions.id) ||
  11948. (seriesOptions[AXIS] === UNDEFINED && axisOptions.index === 0)) {
  11949. // register this series in the axis.series lookup
  11950. axis.series.push(series);
  11951. // set this series.xAxis or series.yAxis reference
  11952. series[AXIS] = axis;
  11953. // mark dirty for redraw
  11954. axis.isDirty = true;
  11955. }
  11956. });
  11957. // The series needs an X and an Y axis
  11958. if (!series[AXIS] && series.optionalAxis !== AXIS) {
  11959. error(18, true);
  11960. }
  11961. });
  11962. },
  11963. /**
  11964. * For simple series types like line and column, the data values are held in arrays like
  11965. * xData and yData for quick lookup to find extremes and more. For multidimensional series
  11966. * like bubble and map, this can be extended with arrays like zData and valueData by
  11967. * adding to the series.parallelArrays array.
  11968. */
  11969. updateParallelArrays: function (point, i) {
  11970. var series = point.series,
  11971. args = arguments,
  11972. fn = isNumber(i) ?
  11973. // Insert the value in the given position
  11974. function (key) {
  11975. var val = key === 'y' && series.toYData ? series.toYData(point) : point[key];
  11976. series[key + 'Data'][i] = val;
  11977. } :
  11978. // Apply the method specified in i with the following arguments as arguments
  11979. function (key) {
  11980. Array.prototype[i].apply(series[key + 'Data'], Array.prototype.slice.call(args, 2));
  11981. };
  11982. each(series.parallelArrays, fn);
  11983. },
  11984. /**
  11985. * Return an auto incremented x value based on the pointStart and pointInterval options.
  11986. * This is only used if an x value is not given for the point that calls autoIncrement.
  11987. */
  11988. autoIncrement: function () {
  11989. var options = this.options,
  11990. xIncrement = this.xIncrement,
  11991. date,
  11992. pointInterval,
  11993. pointIntervalUnit = options.pointIntervalUnit;
  11994. xIncrement = pick(xIncrement, options.pointStart, 0);
  11995. this.pointInterval = pointInterval = pick(this.pointInterval, options.pointInterval, 1);
  11996. // Added code for pointInterval strings
  11997. if (pointIntervalUnit) {
  11998. date = new Date(xIncrement);
  11999. if (pointIntervalUnit === 'day') {
  12000. date = +date[setDate](date[getDate]() + pointInterval);
  12001. } else if (pointIntervalUnit === 'month') {
  12002. date = +date[setMonth](date[getMonth]() + pointInterval);
  12003. } else if (pointIntervalUnit === 'year') {
  12004. date = +date[setFullYear](date[getFullYear]() + pointInterval);
  12005. }
  12006. pointInterval = date - xIncrement;
  12007. }
  12008. this.xIncrement = xIncrement + pointInterval;
  12009. return xIncrement;
  12010. },
  12011. /**
  12012. * Set the series options by merging from the options tree
  12013. * @param {Object} itemOptions
  12014. */
  12015. setOptions: function (itemOptions) {
  12016. var chart = this.chart,
  12017. chartOptions = chart.options,
  12018. plotOptions = chartOptions.plotOptions,
  12019. userOptions = chart.userOptions || {},
  12020. userPlotOptions = userOptions.plotOptions || {},
  12021. typeOptions = plotOptions[this.type],
  12022. options,
  12023. zones;
  12024. this.userOptions = itemOptions;
  12025. // General series options take precedence over type options because otherwise, default
  12026. // type options like column.animation would be overwritten by the general option.
  12027. // But issues have been raised here (#3881), and the solution may be to distinguish
  12028. // between default option and userOptions like in the tooltip below.
  12029. options = merge(
  12030. typeOptions,
  12031. plotOptions.series,
  12032. itemOptions
  12033. );
  12034. // The tooltip options are merged between global and series specific options
  12035. this.tooltipOptions = merge(
  12036. defaultOptions.tooltip,
  12037. defaultOptions.plotOptions[this.type].tooltip,
  12038. userOptions.tooltip,
  12039. userPlotOptions.series && userPlotOptions.series.tooltip,
  12040. userPlotOptions[this.type] && userPlotOptions[this.type].tooltip,
  12041. itemOptions.tooltip
  12042. );
  12043. // Delete marker object if not allowed (#1125)
  12044. if (typeOptions.marker === null) {
  12045. delete options.marker;
  12046. }
  12047. // Handle color zones
  12048. this.zoneAxis = options.zoneAxis;
  12049. zones = this.zones = (options.zones || []).slice();
  12050. if ((options.negativeColor || options.negativeFillColor) && !options.zones) {
  12051. zones.push({
  12052. value: options[this.zoneAxis + 'Threshold'] || options.threshold || 0,
  12053. color: options.negativeColor,
  12054. fillColor: options.negativeFillColor
  12055. });
  12056. }
  12057. if (zones.length) { // Push one extra zone for the rest
  12058. if (defined(zones[zones.length - 1].value)) {
  12059. zones.push({
  12060. color: this.color,
  12061. fillColor: this.fillColor
  12062. });
  12063. }
  12064. }
  12065. return options;
  12066. },
  12067. getCyclic: function (prop, value, defaults) {
  12068. var i,
  12069. userOptions = this.userOptions,
  12070. indexName = '_' + prop + 'Index',
  12071. counterName = prop + 'Counter';
  12072. if (!value) {
  12073. if (defined(userOptions[indexName])) { // after Series.update()
  12074. i = userOptions[indexName];
  12075. } else {
  12076. userOptions[indexName] = i = this.chart[counterName] % defaults.length;
  12077. this.chart[counterName] += 1;
  12078. }
  12079. value = defaults[i];
  12080. }
  12081. this[prop] = value;
  12082. },
  12083. /**
  12084. * Get the series' color
  12085. */
  12086. getColor: function () {
  12087. if (this.options.colorByPoint) {
  12088. this.options.color = null; // #4359, selected slice got series.color even when colorByPoint was set.
  12089. } else {
  12090. this.getCyclic('color', this.options.color || defaultPlotOptions[this.type].color, this.chart.options.colors);
  12091. }
  12092. },
  12093. /**
  12094. * Get the series' symbol
  12095. */
  12096. getSymbol: function () {
  12097. var seriesMarkerOption = this.options.marker;
  12098. this.getCyclic('symbol', seriesMarkerOption.symbol, this.chart.options.symbols);
  12099. // don't substract radius in image symbols (#604)
  12100. if (/^url/.test(this.symbol)) {
  12101. seriesMarkerOption.radius = 0;
  12102. }
  12103. },
  12104. drawLegendSymbol: LegendSymbolMixin.drawLineMarker,
  12105. /**
  12106. * Replace the series data with a new set of data
  12107. * @param {Object} data
  12108. * @param {Object} redraw
  12109. */
  12110. setData: function (data, redraw, animation, updatePoints) {
  12111. var series = this,
  12112. oldData = series.points,
  12113. oldDataLength = (oldData && oldData.length) || 0,
  12114. dataLength,
  12115. options = series.options,
  12116. chart = series.chart,
  12117. firstPoint = null,
  12118. xAxis = series.xAxis,
  12119. hasCategories = xAxis && !!xAxis.categories,
  12120. i,
  12121. turboThreshold = options.turboThreshold,
  12122. pt,
  12123. xData = this.xData,
  12124. yData = this.yData,
  12125. pointArrayMap = series.pointArrayMap,
  12126. valueCount = pointArrayMap && pointArrayMap.length;
  12127. data = data || [];
  12128. dataLength = data.length;
  12129. redraw = pick(redraw, true);
  12130. // If the point count is the same as is was, just run Point.update which is
  12131. // cheaper, allows animation, and keeps references to points.
  12132. if (updatePoints !== false && dataLength && oldDataLength === dataLength && !series.cropped && !series.hasGroupedData && series.visible) {
  12133. each(data, function (point, i) {
  12134. // .update doesn't exist on a linked, hidden series (#3709)
  12135. if (oldData[i].update && point !== options.data[i]) {
  12136. oldData[i].update(point, false, null, false);
  12137. }
  12138. });
  12139. } else {
  12140. // Reset properties
  12141. series.xIncrement = null;
  12142. series.colorCounter = 0; // for series with colorByPoint (#1547)
  12143. // Update parallel arrays
  12144. each(this.parallelArrays, function (key) {
  12145. series[key + 'Data'].length = 0;
  12146. });
  12147. // In turbo mode, only one- or twodimensional arrays of numbers are allowed. The
  12148. // first value is tested, and we assume that all the rest are defined the same
  12149. // way. Although the 'for' loops are similar, they are repeated inside each
  12150. // if-else conditional for max performance.
  12151. if (turboThreshold && dataLength > turboThreshold) {
  12152. // find the first non-null point
  12153. i = 0;
  12154. while (firstPoint === null && i < dataLength) {
  12155. firstPoint = data[i];
  12156. i++;
  12157. }
  12158. if (isNumber(firstPoint)) { // assume all points are numbers
  12159. var x = pick(options.pointStart, 0),
  12160. pointInterval = pick(options.pointInterval, 1);
  12161. for (i = 0; i < dataLength; i++) {
  12162. xData[i] = x;
  12163. yData[i] = data[i];
  12164. x += pointInterval;
  12165. }
  12166. series.xIncrement = x;
  12167. } else if (isArray(firstPoint)) { // assume all points are arrays
  12168. if (valueCount) { // [x, low, high] or [x, o, h, l, c]
  12169. for (i = 0; i < dataLength; i++) {
  12170. pt = data[i];
  12171. xData[i] = pt[0];
  12172. yData[i] = pt.slice(1, valueCount + 1);
  12173. }
  12174. } else { // [x, y]
  12175. for (i = 0; i < dataLength; i++) {
  12176. pt = data[i];
  12177. xData[i] = pt[0];
  12178. yData[i] = pt[1];
  12179. }
  12180. }
  12181. } else {
  12182. error(12); // Highcharts expects configs to be numbers or arrays in turbo mode
  12183. }
  12184. } else {
  12185. for (i = 0; i < dataLength; i++) {
  12186. if (data[i] !== UNDEFINED) { // stray commas in oldIE
  12187. pt = { series: series };
  12188. series.pointClass.prototype.applyOptions.apply(pt, [data[i]]);
  12189. series.updateParallelArrays(pt, i);
  12190. if (hasCategories && defined(pt.name)) { // #4401
  12191. xAxis.names[pt.x] = pt.name; // #2046
  12192. }
  12193. }
  12194. }
  12195. }
  12196. // Forgetting to cast strings to numbers is a common caveat when handling CSV or JSON
  12197. if (isString(yData[0])) {
  12198. error(14, true);
  12199. }
  12200. series.data = [];
  12201. series.options.data = series.userOptions.data = data;
  12202. // destroy old points
  12203. i = oldDataLength;
  12204. while (i--) {
  12205. if (oldData[i] && oldData[i].destroy) {
  12206. oldData[i].destroy();
  12207. }
  12208. }
  12209. // reset minRange (#878)
  12210. if (xAxis) {
  12211. xAxis.minRange = xAxis.userMinRange;
  12212. }
  12213. // redraw
  12214. series.isDirty = series.isDirtyData = chart.isDirtyBox = true;
  12215. animation = false;
  12216. }
  12217. // Typically for pie series, points need to be processed and generated
  12218. // prior to rendering the legend
  12219. if (options.legendType === 'point') {
  12220. this.processData();
  12221. this.generatePoints();
  12222. }
  12223. if (redraw) {
  12224. chart.redraw(animation);
  12225. }
  12226. },
  12227. /**
  12228. * Process the data by cropping away unused data points if the series is longer
  12229. * than the crop threshold. This saves computing time for lage series.
  12230. */
  12231. processData: function (force) {
  12232. var series = this,
  12233. processedXData = series.xData, // copied during slice operation below
  12234. processedYData = series.yData,
  12235. dataLength = processedXData.length,
  12236. croppedData,
  12237. cropStart = 0,
  12238. cropped,
  12239. distance,
  12240. closestPointRange,
  12241. xAxis = series.xAxis,
  12242. i, // loop variable
  12243. options = series.options,
  12244. cropThreshold = options.cropThreshold,
  12245. getExtremesFromAll = series.getExtremesFromAll || options.getExtremesFromAll, // #4599
  12246. isCartesian = series.isCartesian,
  12247. xExtremes,
  12248. val2lin = xAxis && xAxis.val2lin,
  12249. isLog = xAxis && xAxis.isLog,
  12250. min,
  12251. max;
  12252. // If the series data or axes haven't changed, don't go through this. Return false to pass
  12253. // the message on to override methods like in data grouping.
  12254. if (isCartesian && !series.isDirty && !xAxis.isDirty && !series.yAxis.isDirty && !force) {
  12255. return false;
  12256. }
  12257. if (xAxis) {
  12258. xExtremes = xAxis.getExtremes(); // corrected for log axis (#3053)
  12259. min = xExtremes.min;
  12260. max = xExtremes.max;
  12261. }
  12262. // optionally filter out points outside the plot area
  12263. if (isCartesian && series.sorted && !getExtremesFromAll && (!cropThreshold || dataLength > cropThreshold || series.forceCrop)) {
  12264. // it's outside current extremes
  12265. if (processedXData[dataLength - 1] < min || processedXData[0] > max) {
  12266. processedXData = [];
  12267. processedYData = [];
  12268. // only crop if it's actually spilling out
  12269. } else if (processedXData[0] < min || processedXData[dataLength - 1] > max) {
  12270. croppedData = this.cropData(series.xData, series.yData, min, max);
  12271. processedXData = croppedData.xData;
  12272. processedYData = croppedData.yData;
  12273. cropStart = croppedData.start;
  12274. cropped = true;
  12275. }
  12276. }
  12277. // Find the closest distance between processed points
  12278. i = processedXData.length || 1;
  12279. while (--i) {
  12280. distance = isLog ?
  12281. val2lin(processedXData[i]) - val2lin(processedXData[i - 1]) :
  12282. processedXData[i] - processedXData[i - 1];
  12283. if (distance > 0 && (closestPointRange === UNDEFINED || distance < closestPointRange)) {
  12284. closestPointRange = distance;
  12285. // Unsorted data is not supported by the line tooltip, as well as data grouping and
  12286. // navigation in Stock charts (#725) and width calculation of columns (#1900)
  12287. } else if (distance < 0 && series.requireSorting) {
  12288. error(15);
  12289. }
  12290. }
  12291. // Record the properties
  12292. series.cropped = cropped; // undefined or true
  12293. series.cropStart = cropStart;
  12294. series.processedXData = processedXData;
  12295. series.processedYData = processedYData;
  12296. series.closestPointRange = closestPointRange;
  12297. },
  12298. /**
  12299. * Iterate over xData and crop values between min and max. Returns object containing crop start/end
  12300. * cropped xData with corresponding part of yData, dataMin and dataMax within the cropped range
  12301. */
  12302. cropData: function (xData, yData, min, max) {
  12303. var dataLength = xData.length,
  12304. cropStart = 0,
  12305. cropEnd = dataLength,
  12306. cropShoulder = pick(this.cropShoulder, 1), // line-type series need one point outside
  12307. i,
  12308. j;
  12309. // iterate up to find slice start
  12310. for (i = 0; i < dataLength; i++) {
  12311. if (xData[i] >= min) {
  12312. cropStart = mathMax(0, i - cropShoulder);
  12313. break;
  12314. }
  12315. }
  12316. // proceed to find slice end
  12317. for (j = i; j < dataLength; j++) {
  12318. if (xData[j] > max) {
  12319. cropEnd = j + cropShoulder;
  12320. break;
  12321. }
  12322. }
  12323. return {
  12324. xData: xData.slice(cropStart, cropEnd),
  12325. yData: yData.slice(cropStart, cropEnd),
  12326. start: cropStart,
  12327. end: cropEnd
  12328. };
  12329. },
  12330. /**
  12331. * Generate the data point after the data has been processed by cropping away
  12332. * unused points and optionally grouped in Highcharts Stock.
  12333. */
  12334. generatePoints: function () {
  12335. var series = this,
  12336. options = series.options,
  12337. dataOptions = options.data,
  12338. data = series.data,
  12339. dataLength,
  12340. processedXData = series.processedXData,
  12341. processedYData = series.processedYData,
  12342. pointClass = series.pointClass,
  12343. processedDataLength = processedXData.length,
  12344. cropStart = series.cropStart || 0,
  12345. cursor,
  12346. hasGroupedData = series.hasGroupedData,
  12347. point,
  12348. points = [],
  12349. i;
  12350. if (!data && !hasGroupedData) {
  12351. var arr = [];
  12352. arr.length = dataOptions.length;
  12353. data = series.data = arr;
  12354. }
  12355. for (i = 0; i < processedDataLength; i++) {
  12356. cursor = cropStart + i;
  12357. if (!hasGroupedData) {
  12358. if (data[cursor]) {
  12359. point = data[cursor];
  12360. } else if (dataOptions[cursor] !== UNDEFINED) { // #970
  12361. data[cursor] = point = (new pointClass()).init(series, dataOptions[cursor], processedXData[i]);
  12362. }
  12363. points[i] = point;
  12364. } else {
  12365. // splat the y data in case of ohlc data array
  12366. points[i] = (new pointClass()).init(series, [processedXData[i]].concat(splat(processedYData[i])));
  12367. points[i].dataGroup = series.groupMap[i];
  12368. }
  12369. points[i].index = cursor; // For faster access in Point.update
  12370. }
  12371. // Hide cropped-away points - this only runs when the number of points is above cropThreshold, or when
  12372. // swithching view from non-grouped data to grouped data (#637)
  12373. if (data && (processedDataLength !== (dataLength = data.length) || hasGroupedData)) {
  12374. for (i = 0; i < dataLength; i++) {
  12375. if (i === cropStart && !hasGroupedData) { // when has grouped data, clear all points
  12376. i += processedDataLength;
  12377. }
  12378. if (data[i]) {
  12379. data[i].destroyElements();
  12380. data[i].plotX = UNDEFINED; // #1003
  12381. }
  12382. }
  12383. }
  12384. series.data = data;
  12385. series.points = points;
  12386. },
  12387. /**
  12388. * Calculate Y extremes for visible data
  12389. */
  12390. getExtremes: function (yData) {
  12391. var xAxis = this.xAxis,
  12392. yAxis = this.yAxis,
  12393. xData = this.processedXData,
  12394. yDataLength,
  12395. activeYData = [],
  12396. activeCounter = 0,
  12397. xExtremes = xAxis.getExtremes(), // #2117, need to compensate for log X axis
  12398. xMin = xExtremes.min,
  12399. xMax = xExtremes.max,
  12400. validValue,
  12401. withinRange,
  12402. x,
  12403. y,
  12404. i,
  12405. j;
  12406. yData = yData || this.stackedYData || this.processedYData || [];
  12407. yDataLength = yData.length;
  12408. for (i = 0; i < yDataLength; i++) {
  12409. x = xData[i];
  12410. y = yData[i];
  12411. // For points within the visible range, including the first point outside the
  12412. // visible range, consider y extremes
  12413. validValue = y !== null && y !== UNDEFINED && (!yAxis.isLog || (y.length || y > 0));
  12414. withinRange = this.getExtremesFromAll || this.options.getExtremesFromAll || this.cropped ||
  12415. ((xData[i + 1] || x) >= xMin && (xData[i - 1] || x) <= xMax);
  12416. if (validValue && withinRange) {
  12417. j = y.length;
  12418. if (j) { // array, like ohlc or range data
  12419. while (j--) {
  12420. if (y[j] !== null) {
  12421. activeYData[activeCounter++] = y[j];
  12422. }
  12423. }
  12424. } else {
  12425. activeYData[activeCounter++] = y;
  12426. }
  12427. }
  12428. }
  12429. this.dataMin = arrayMin(activeYData);
  12430. this.dataMax = arrayMax(activeYData);
  12431. },
  12432. /**
  12433. * Translate data points from raw data values to chart specific positioning data
  12434. * needed later in drawPoints, drawGraph and drawTracker.
  12435. */
  12436. translate: function () {
  12437. if (!this.processedXData) { // hidden series
  12438. this.processData();
  12439. }
  12440. this.generatePoints();
  12441. var series = this,
  12442. options = series.options,
  12443. stacking = options.stacking,
  12444. xAxis = series.xAxis,
  12445. categories = xAxis.categories,
  12446. yAxis = series.yAxis,
  12447. points = series.points,
  12448. dataLength = points.length,
  12449. hasModifyValue = !!series.modifyValue,
  12450. i,
  12451. pointPlacement = options.pointPlacement,
  12452. dynamicallyPlaced = pointPlacement === 'between' || isNumber(pointPlacement),
  12453. threshold = options.threshold,
  12454. stackThreshold = options.startFromThreshold ? threshold : 0,
  12455. plotX,
  12456. plotY,
  12457. lastPlotX,
  12458. stackIndicator,
  12459. closestPointRangePx = Number.MAX_VALUE;
  12460. // Translate each point
  12461. for (i = 0; i < dataLength; i++) {
  12462. var point = points[i],
  12463. xValue = point.x,
  12464. yValue = point.y,
  12465. yBottom = point.low,
  12466. stack = stacking && yAxis.stacks[(series.negStacks && yValue < (stackThreshold ? 0 : threshold) ? '-' : '') + series.stackKey],
  12467. pointStack,
  12468. stackValues;
  12469. // Discard disallowed y values for log axes (#3434)
  12470. if (yAxis.isLog && yValue !== null && yValue <= 0) {
  12471. point.y = yValue = null;
  12472. error(10);
  12473. }
  12474. // Get the plotX translation
  12475. point.plotX = plotX = correctFloat( // #5236
  12476. mathMin(mathMax(-1e5, xAxis.translate(xValue, 0, 0, 0, 1, pointPlacement, this.type === 'flags')), 1e5) // #3923
  12477. );
  12478. // Calculate the bottom y value for stacked series
  12479. if (stacking && series.visible && !point.isNull && stack && stack[xValue]) {
  12480. stackIndicator = series.getStackIndicator(stackIndicator, xValue, series.index);
  12481. pointStack = stack[xValue];
  12482. stackValues = pointStack.points[stackIndicator.key];
  12483. yBottom = stackValues[0];
  12484. yValue = stackValues[1];
  12485. if (yBottom === stackThreshold) {
  12486. yBottom = pick(threshold, yAxis.min);
  12487. }
  12488. if (yAxis.isLog && yBottom <= 0) { // #1200, #1232
  12489. yBottom = null;
  12490. }
  12491. point.total = point.stackTotal = pointStack.total;
  12492. point.percentage = pointStack.total && (point.y / pointStack.total * 100);
  12493. point.stackY = yValue;
  12494. // Place the stack label
  12495. pointStack.setOffset(series.pointXOffset || 0, series.barW || 0);
  12496. }
  12497. // Set translated yBottom or remove it
  12498. point.yBottom = defined(yBottom) ?
  12499. yAxis.translate(yBottom, 0, 1, 0, 1) :
  12500. null;
  12501. // general hook, used for Highstock compare mode
  12502. if (hasModifyValue) {
  12503. yValue = series.modifyValue(yValue, point);
  12504. }
  12505. // Set the the plotY value, reset it for redraws
  12506. point.plotY = plotY = (typeof yValue === 'number' && yValue !== Infinity) ?
  12507. mathMin(mathMax(-1e5, yAxis.translate(yValue, 0, 1, 0, 1)), 1e5) : // #3201
  12508. UNDEFINED;
  12509. point.isInside = plotY !== UNDEFINED && plotY >= 0 && plotY <= yAxis.len && // #3519
  12510. plotX >= 0 && plotX <= xAxis.len;
  12511. // Set client related positions for mouse tracking
  12512. point.clientX = dynamicallyPlaced ? xAxis.translate(xValue, 0, 0, 0, 1) : plotX; // #1514
  12513. point.negative = point.y < (threshold || 0);
  12514. // some API data
  12515. point.category = categories && categories[point.x] !== UNDEFINED ?
  12516. categories[point.x] : point.x;
  12517. // Determine auto enabling of markers (#3635, #5099)
  12518. if (!point.isNull) {
  12519. if (lastPlotX !== undefined) {
  12520. closestPointRangePx = mathMin(closestPointRangePx, mathAbs(plotX - lastPlotX));
  12521. }
  12522. lastPlotX = plotX;
  12523. }
  12524. }
  12525. series.closestPointRangePx = closestPointRangePx;
  12526. },
  12527. /**
  12528. * Return the series points with null points filtered out
  12529. */
  12530. getValidPoints: function (points, insideOnly) {
  12531. var chart = this.chart;
  12532. return grep(points || this.points || [], function isValidPoint(point) { // #3916, #5029
  12533. if (insideOnly && !chart.isInsidePlot(point.plotX, point.plotY, chart.inverted)) { // #5085
  12534. return false;
  12535. }
  12536. return !point.isNull;
  12537. });
  12538. },
  12539. /**
  12540. * Set the clipping for the series. For animated series it is called twice, first to initiate
  12541. * animating the clip then the second time without the animation to set the final clip.
  12542. */
  12543. setClip: function (animation) {
  12544. var chart = this.chart,
  12545. options = this.options,
  12546. renderer = chart.renderer,
  12547. inverted = chart.inverted,
  12548. seriesClipBox = this.clipBox,
  12549. clipBox = seriesClipBox || chart.clipBox,
  12550. sharedClipKey = this.sharedClipKey || ['_sharedClip', animation && animation.duration, animation && animation.easing, clipBox.height, options.xAxis, options.yAxis].join(','), // #4526
  12551. clipRect = chart[sharedClipKey],
  12552. markerClipRect = chart[sharedClipKey + 'm'];
  12553. // If a clipping rectangle with the same properties is currently present in the chart, use that.
  12554. if (!clipRect) {
  12555. // When animation is set, prepare the initial positions
  12556. if (animation) {
  12557. clipBox.width = 0;
  12558. chart[sharedClipKey + 'm'] = markerClipRect = renderer.clipRect(
  12559. -99, // include the width of the first marker
  12560. inverted ? -chart.plotLeft : -chart.plotTop,
  12561. 99,
  12562. inverted ? chart.chartWidth : chart.chartHeight
  12563. );
  12564. }
  12565. chart[sharedClipKey] = clipRect = renderer.clipRect(clipBox);
  12566. }
  12567. if (animation) {
  12568. clipRect.count += 1;
  12569. }
  12570. if (options.clip !== false) {
  12571. this.group.clip(animation || seriesClipBox ? clipRect : chart.clipRect);
  12572. this.markerGroup.clip(markerClipRect);
  12573. this.sharedClipKey = sharedClipKey;
  12574. }
  12575. // Remove the shared clipping rectangle when all series are shown
  12576. if (!animation) {
  12577. clipRect.count -= 1;
  12578. if (clipRect.count <= 0 && sharedClipKey && chart[sharedClipKey]) {
  12579. if (!seriesClipBox) {
  12580. chart[sharedClipKey] = chart[sharedClipKey].destroy();
  12581. }
  12582. if (chart[sharedClipKey + 'm']) {
  12583. chart[sharedClipKey + 'm'] = chart[sharedClipKey + 'm'].destroy();
  12584. }
  12585. }
  12586. }
  12587. },
  12588. /**
  12589. * Animate in the series
  12590. */
  12591. animate: function (init) {
  12592. var series = this,
  12593. chart = series.chart,
  12594. clipRect,
  12595. animation = series.options.animation,
  12596. sharedClipKey;
  12597. // Animation option is set to true
  12598. if (animation && !isObject(animation)) {
  12599. animation = defaultPlotOptions[series.type].animation;
  12600. }
  12601. // Initialize the animation. Set up the clipping rectangle.
  12602. if (init) {
  12603. series.setClip(animation);
  12604. // Run the animation
  12605. } else {
  12606. sharedClipKey = this.sharedClipKey;
  12607. clipRect = chart[sharedClipKey];
  12608. if (clipRect) {
  12609. clipRect.animate({
  12610. width: chart.plotSizeX
  12611. }, animation);
  12612. }
  12613. if (chart[sharedClipKey + 'm']) {
  12614. chart[sharedClipKey + 'm'].animate({
  12615. width: chart.plotSizeX + 99
  12616. }, animation);
  12617. }
  12618. // Delete this function to allow it only once
  12619. series.animate = null;
  12620. }
  12621. },
  12622. /**
  12623. * This runs after animation to land on the final plot clipping
  12624. */
  12625. afterAnimate: function () {
  12626. this.setClip();
  12627. fireEvent(this, 'afterAnimate');
  12628. },
  12629. /**
  12630. * Draw the markers
  12631. */
  12632. drawPoints: function () {
  12633. var series = this,
  12634. pointAttr,
  12635. points = series.points,
  12636. chart = series.chart,
  12637. plotX,
  12638. plotY,
  12639. i,
  12640. point,
  12641. radius,
  12642. symbol,
  12643. isImage,
  12644. graphic,
  12645. options = series.options,
  12646. seriesMarkerOptions = options.marker,
  12647. seriesPointAttr = series.pointAttr[''],
  12648. pointMarkerOptions,
  12649. hasPointMarker,
  12650. enabled,
  12651. isInside,
  12652. markerGroup = series.markerGroup,
  12653. xAxis = series.xAxis,
  12654. globallyEnabled = pick(
  12655. seriesMarkerOptions.enabled,
  12656. xAxis.isRadial,
  12657. series.closestPointRangePx > 2 * seriesMarkerOptions.radius
  12658. );
  12659. if (seriesMarkerOptions.enabled !== false || series._hasPointMarkers) {
  12660. i = points.length;
  12661. while (i--) {
  12662. point = points[i];
  12663. plotX = mathFloor(point.plotX); // #1843
  12664. plotY = point.plotY;
  12665. graphic = point.graphic;
  12666. pointMarkerOptions = point.marker || {};
  12667. hasPointMarker = !!point.marker;
  12668. enabled = (globallyEnabled && pointMarkerOptions.enabled === UNDEFINED) || pointMarkerOptions.enabled;
  12669. isInside = point.isInside;
  12670. // only draw the point if y is defined
  12671. if (enabled && isNumber(plotY) && point.y !== null) {
  12672. // shortcuts
  12673. pointAttr = point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE] || seriesPointAttr;
  12674. radius = pointAttr.r;
  12675. symbol = pick(pointMarkerOptions.symbol, series.symbol);
  12676. isImage = symbol.indexOf('url') === 0;
  12677. if (graphic) { // update
  12678. graphic[isInside ? 'show' : 'hide'](true) // Since the marker group isn't clipped, each individual marker must be toggled
  12679. .attr(pointAttr) // #4759
  12680. .animate(extend({
  12681. x: plotX - radius,
  12682. y: plotY - radius
  12683. }, graphic.symbolName ? { // don't apply to image symbols #507
  12684. width: 2 * radius,
  12685. height: 2 * radius
  12686. } : {}));
  12687. } else if (isInside && (radius > 0 || isImage)) {
  12688. point.graphic = graphic = chart.renderer.symbol(
  12689. symbol,
  12690. plotX - radius,
  12691. plotY - radius,
  12692. 2 * radius,
  12693. 2 * radius,
  12694. hasPointMarker ? pointMarkerOptions : seriesMarkerOptions
  12695. )
  12696. .attr(pointAttr)
  12697. .add(markerGroup);
  12698. }
  12699. } else if (graphic) {
  12700. point.graphic = graphic.destroy(); // #1269
  12701. }
  12702. }
  12703. }
  12704. },
  12705. /**
  12706. * Convert state properties from API naming conventions to SVG attributes
  12707. *
  12708. * @param {Object} options API options object
  12709. * @param {Object} base1 SVG attribute object to inherit from
  12710. * @param {Object} base2 Second level SVG attribute object to inherit from
  12711. */
  12712. convertAttribs: function (options, base1, base2, base3) {
  12713. var conversion = this.pointAttrToOptions,
  12714. attr,
  12715. option,
  12716. obj = {};
  12717. options = options || {};
  12718. base1 = base1 || {};
  12719. base2 = base2 || {};
  12720. base3 = base3 || {};
  12721. for (attr in conversion) {
  12722. option = conversion[attr];
  12723. obj[attr] = pick(options[option], base1[attr], base2[attr], base3[attr]);
  12724. }
  12725. return obj;
  12726. },
  12727. /**
  12728. * Get the state attributes. Each series type has its own set of attributes
  12729. * that are allowed to change on a point's state change. Series wide attributes are stored for
  12730. * all series, and additionally point specific attributes are stored for all
  12731. * points with individual marker options. If such options are not defined for the point,
  12732. * a reference to the series wide attributes is stored in point.pointAttr.
  12733. */
  12734. getAttribs: function () {
  12735. var series = this,
  12736. seriesOptions = series.options,
  12737. normalOptions = defaultPlotOptions[series.type].marker ? seriesOptions.marker : seriesOptions,
  12738. stateOptions = normalOptions.states,
  12739. stateOptionsHover = stateOptions[HOVER_STATE],
  12740. pointStateOptionsHover,
  12741. seriesColor = series.color,
  12742. seriesNegativeColor = series.options.negativeColor,
  12743. normalDefaults = {
  12744. stroke: seriesColor,
  12745. fill: seriesColor
  12746. },
  12747. points = series.points || [], // #927
  12748. i,
  12749. j,
  12750. threshold,
  12751. point,
  12752. seriesPointAttr = [],
  12753. pointAttr,
  12754. pointAttrToOptions = series.pointAttrToOptions,
  12755. hasPointSpecificOptions = series.hasPointSpecificOptions,
  12756. defaultLineColor = normalOptions.lineColor,
  12757. defaultFillColor = normalOptions.fillColor,
  12758. turboThreshold = seriesOptions.turboThreshold,
  12759. zones = series.zones,
  12760. zoneAxis = series.zoneAxis || 'y',
  12761. zoneColor,
  12762. attr,
  12763. key;
  12764. // series type specific modifications
  12765. if (seriesOptions.marker) { // line, spline, area, areaspline, scatter
  12766. // if no hover radius is given, default to normal radius + 2
  12767. stateOptionsHover.radius = stateOptionsHover.radius || normalOptions.radius + stateOptionsHover.radiusPlus;
  12768. stateOptionsHover.lineWidth = stateOptionsHover.lineWidth || normalOptions.lineWidth + stateOptionsHover.lineWidthPlus;
  12769. } else { // column, bar, pie
  12770. // if no hover color is given, brighten the normal color
  12771. stateOptionsHover.color = stateOptionsHover.color ||
  12772. Color(stateOptionsHover.color || seriesColor)
  12773. .brighten(stateOptionsHover.brightness).get();
  12774. // if no hover negativeColor is given, brighten the normal negativeColor
  12775. stateOptionsHover.negativeColor = stateOptionsHover.negativeColor ||
  12776. Color(stateOptionsHover.negativeColor || seriesNegativeColor)
  12777. .brighten(stateOptionsHover.brightness).get();
  12778. }
  12779. // general point attributes for the series normal state
  12780. seriesPointAttr[NORMAL_STATE] = series.convertAttribs(normalOptions, normalDefaults);
  12781. // HOVER_STATE and SELECT_STATE states inherit from normal state except the default radius
  12782. each([HOVER_STATE, SELECT_STATE], function (state) {
  12783. seriesPointAttr[state] =
  12784. series.convertAttribs(stateOptions[state], seriesPointAttr[NORMAL_STATE]);
  12785. });
  12786. // set it
  12787. series.pointAttr = seriesPointAttr;
  12788. // Generate the point-specific attribute collections if specific point
  12789. // options are given. If not, create a referance to the series wide point
  12790. // attributes
  12791. i = points.length;
  12792. if (!turboThreshold || i < turboThreshold || hasPointSpecificOptions) {
  12793. while (i--) {
  12794. point = points[i];
  12795. normalOptions = (point.options && point.options.marker) || point.options;
  12796. if (normalOptions && normalOptions.enabled === false) {
  12797. normalOptions.radius = 0;
  12798. }
  12799. zoneColor = null;
  12800. if (zones.length) {
  12801. j = 0;
  12802. threshold = zones[j];
  12803. while (point[zoneAxis] >= threshold.value) {
  12804. threshold = zones[++j];
  12805. }
  12806. point.color = point.fillColor = zoneColor = pick(threshold.color, series.color); // #3636, #4267, #4430 - inherit color from series, when color is undefined
  12807. }
  12808. hasPointSpecificOptions = seriesOptions.colorByPoint || point.color; // #868
  12809. // check if the point has specific visual options
  12810. if (point.options) {
  12811. for (key in pointAttrToOptions) {
  12812. if (defined(normalOptions[pointAttrToOptions[key]])) {
  12813. hasPointSpecificOptions = true;
  12814. }
  12815. }
  12816. }
  12817. // a specific marker config object is defined for the individual point:
  12818. // create it's own attribute collection
  12819. if (hasPointSpecificOptions) {
  12820. normalOptions = normalOptions || {};
  12821. pointAttr = [];
  12822. stateOptions = normalOptions.states || {}; // reassign for individual point
  12823. pointStateOptionsHover = stateOptions[HOVER_STATE] = stateOptions[HOVER_STATE] || {};
  12824. // Handle colors for column and pies
  12825. if (!seriesOptions.marker || (point.negative && !pointStateOptionsHover.fillColor && !stateOptionsHover.fillColor)) { // column, bar, point or negative threshold for series with markers (#3636)
  12826. // If no hover color is given, brighten the normal color. #1619, #2579
  12827. pointStateOptionsHover[series.pointAttrToOptions.fill] = pointStateOptionsHover.color || (!point.options.color && stateOptionsHover[(point.negative && seriesNegativeColor ? 'negativeColor' : 'color')]) ||
  12828. Color(point.color)
  12829. .brighten(pointStateOptionsHover.brightness || stateOptionsHover.brightness)
  12830. .get();
  12831. }
  12832. // normal point state inherits series wide normal state
  12833. attr = { color: point.color }; // #868
  12834. if (!defaultFillColor) { // Individual point color or negative color markers (#2219)
  12835. attr.fillColor = point.color;
  12836. }
  12837. if (!defaultLineColor) {
  12838. attr.lineColor = point.color; // Bubbles take point color, line markers use white
  12839. }
  12840. // Color is explicitly set to null or undefined (#1288, #4068)
  12841. if (normalOptions.hasOwnProperty('color') && !normalOptions.color) {
  12842. delete normalOptions.color;
  12843. }
  12844. // When zone is set, but series.states.hover.color is not set, apply zone color on hover, #4670:
  12845. if (zoneColor && !stateOptionsHover.fillColor) {
  12846. pointStateOptionsHover.fillColor = zoneColor;
  12847. }
  12848. pointAttr[NORMAL_STATE] = series.convertAttribs(extend(attr, normalOptions), seriesPointAttr[NORMAL_STATE]);
  12849. // inherit from point normal and series hover
  12850. pointAttr[HOVER_STATE] = series.convertAttribs(
  12851. stateOptions[HOVER_STATE],
  12852. seriesPointAttr[HOVER_STATE],
  12853. pointAttr[NORMAL_STATE]
  12854. );
  12855. // inherit from point normal and series hover
  12856. pointAttr[SELECT_STATE] = series.convertAttribs(
  12857. stateOptions[SELECT_STATE],
  12858. seriesPointAttr[SELECT_STATE],
  12859. pointAttr[NORMAL_STATE]
  12860. );
  12861. // no marker config object is created: copy a reference to the series-wide
  12862. // attribute collection
  12863. } else {
  12864. pointAttr = seriesPointAttr;
  12865. }
  12866. point.pointAttr = pointAttr;
  12867. }
  12868. }
  12869. },
  12870. /**
  12871. * Clear DOM objects and free up memory
  12872. */
  12873. destroy: function () {
  12874. var series = this,
  12875. chart = series.chart,
  12876. issue134 = /AppleWebKit\/533/.test(userAgent),
  12877. destroy,
  12878. i,
  12879. data = series.data || [],
  12880. point,
  12881. prop,
  12882. axis;
  12883. // add event hook
  12884. fireEvent(series, 'destroy');
  12885. // remove all events
  12886. removeEvent(series);
  12887. // erase from axes
  12888. each(series.axisTypes || [], function (AXIS) {
  12889. axis = series[AXIS];
  12890. if (axis) {
  12891. erase(axis.series, series);
  12892. axis.isDirty = axis.forceRedraw = true;
  12893. }
  12894. });
  12895. // remove legend items
  12896. if (series.legendItem) {
  12897. series.chart.legend.destroyItem(series);
  12898. }
  12899. // destroy all points with their elements
  12900. i = data.length;
  12901. while (i--) {
  12902. point = data[i];
  12903. if (point && point.destroy) {
  12904. point.destroy();
  12905. }
  12906. }
  12907. series.points = null;
  12908. // Clear the animation timeout if we are destroying the series during initial animation
  12909. clearTimeout(series.animationTimeout);
  12910. // Destroy all SVGElements associated to the series
  12911. for (prop in series) {
  12912. if (series[prop] instanceof SVGElement && !series[prop].survive) { // Survive provides a hook for not destroying
  12913. // issue 134 workaround
  12914. destroy = issue134 && prop === 'group' ?
  12915. 'hide' :
  12916. 'destroy';
  12917. series[prop][destroy]();
  12918. }
  12919. }
  12920. // remove from hoverSeries
  12921. if (chart.hoverSeries === series) {
  12922. chart.hoverSeries = null;
  12923. }
  12924. erase(chart.series, series);
  12925. // clear all members
  12926. for (prop in series) {
  12927. delete series[prop];
  12928. }
  12929. },
  12930. /**
  12931. * Get the graph path
  12932. */
  12933. getGraphPath: function (points, nullsAsZeroes, connectCliffs) {
  12934. var series = this,
  12935. options = series.options,
  12936. step = options.step,
  12937. reversed,
  12938. graphPath = [],
  12939. gap;
  12940. points = points || series.points;
  12941. // Bottom of a stack is reversed
  12942. reversed = points.reversed;
  12943. if (reversed) {
  12944. points.reverse();
  12945. }
  12946. // Reverse the steps (#5004)
  12947. step = { right: 1, center: 2 }[step] || (step && 3);
  12948. if (step && reversed) {
  12949. step = 4 - step;
  12950. }
  12951. // Remove invalid points, especially in spline (#5015)
  12952. if (options.connectNulls && !nullsAsZeroes && !connectCliffs) {
  12953. points = this.getValidPoints(points);
  12954. }
  12955. // Build the line
  12956. each(points, function (point, i) {
  12957. var plotX = point.plotX,
  12958. plotY = point.plotY,
  12959. lastPoint = points[i - 1],
  12960. pathToPoint; // the path to this point from the previous
  12961. if ((point.leftCliff || (lastPoint && lastPoint.rightCliff)) && !connectCliffs) {
  12962. gap = true; // ... and continue
  12963. }
  12964. // Line series, nullsAsZeroes is not handled
  12965. if (point.isNull && !defined(nullsAsZeroes) && i > 0) {
  12966. gap = !options.connectNulls;
  12967. // Area series, nullsAsZeroes is set
  12968. } else if (point.isNull && !nullsAsZeroes) {
  12969. gap = true;
  12970. } else {
  12971. if (i === 0 || gap) {
  12972. pathToPoint = [M, point.plotX, point.plotY];
  12973. } else if (series.getPointSpline) { // generate the spline as defined in the SplineSeries object
  12974. pathToPoint = series.getPointSpline(points, point, i);
  12975. } else if (step) {
  12976. if (step === 1) { // right
  12977. pathToPoint = [
  12978. L,
  12979. lastPoint.plotX,
  12980. plotY
  12981. ];
  12982. } else if (step === 2) { // center
  12983. pathToPoint = [
  12984. L,
  12985. (lastPoint.plotX + plotX) / 2,
  12986. lastPoint.plotY,
  12987. L,
  12988. (lastPoint.plotX + plotX) / 2,
  12989. plotY
  12990. ];
  12991. } else {
  12992. pathToPoint = [
  12993. L,
  12994. plotX,
  12995. lastPoint.plotY
  12996. ];
  12997. }
  12998. pathToPoint.push(L, plotX, plotY);
  12999. } else {
  13000. // normal line to next point
  13001. pathToPoint = [
  13002. L,
  13003. plotX,
  13004. plotY
  13005. ];
  13006. }
  13007. graphPath.push.apply(graphPath, pathToPoint);
  13008. gap = false;
  13009. }
  13010. });
  13011. series.graphPath = graphPath;
  13012. return graphPath;
  13013. },
  13014. /**
  13015. * Draw the actual graph
  13016. */
  13017. drawGraph: function () {
  13018. var series = this,
  13019. options = this.options,
  13020. props = [['graph', options.lineColor || this.color, options.dashStyle]],
  13021. lineWidth = options.lineWidth,
  13022. roundCap = options.linecap !== 'square',
  13023. graphPath = (this.gappedPath || this.getGraphPath).call(this),
  13024. fillColor = (this.fillGraph && this.color) || NONE, // polygon series use filled graph
  13025. zones = this.zones;
  13026. each(zones, function (threshold, i) {
  13027. props.push(['zoneGraph' + i, threshold.color || series.color, threshold.dashStyle || options.dashStyle]);
  13028. });
  13029. // Draw the graph
  13030. each(props, function (prop, i) {
  13031. var graphKey = prop[0],
  13032. graph = series[graphKey],
  13033. attribs;
  13034. if (graph) {
  13035. graph.animate({ d: graphPath });
  13036. } else if ((lineWidth || fillColor) && graphPath.length) { // #1487
  13037. attribs = {
  13038. stroke: prop[1],
  13039. 'stroke-width': lineWidth,
  13040. fill: fillColor,
  13041. zIndex: 1 // #1069
  13042. };
  13043. if (prop[2]) {
  13044. attribs.dashstyle = prop[2];
  13045. } else if (roundCap) {
  13046. attribs['stroke-linecap'] = attribs['stroke-linejoin'] = 'round';
  13047. }
  13048. series[graphKey] = series.chart.renderer.path(graphPath)
  13049. .attr(attribs)
  13050. .add(series.group)
  13051. .shadow((i < 2) && options.shadow); // add shadow to normal series (0) or to first zone (1) #3932
  13052. }
  13053. });
  13054. },
  13055. /**
  13056. * Clip the graphs into the positive and negative coloured graphs
  13057. */
  13058. applyZones: function () {
  13059. var series = this,
  13060. chart = this.chart,
  13061. renderer = chart.renderer,
  13062. zones = this.zones,
  13063. translatedFrom,
  13064. translatedTo,
  13065. clips = this.clips || [],
  13066. clipAttr,
  13067. graph = this.graph,
  13068. area = this.area,
  13069. chartSizeMax = mathMax(chart.chartWidth, chart.chartHeight),
  13070. axis = this[(this.zoneAxis || 'y') + 'Axis'],
  13071. extremes,
  13072. reversed = axis.reversed,
  13073. inverted = chart.inverted,
  13074. horiz = axis.horiz,
  13075. pxRange,
  13076. pxPosMin,
  13077. pxPosMax,
  13078. ignoreZones = false;
  13079. if (zones.length && (graph || area) && axis.min !== UNDEFINED) {
  13080. // The use of the Color Threshold assumes there are no gaps
  13081. // so it is safe to hide the original graph and area
  13082. if (graph) {
  13083. graph.hide();
  13084. }
  13085. if (area) {
  13086. area.hide();
  13087. }
  13088. // Create the clips
  13089. extremes = axis.getExtremes();
  13090. each(zones, function (threshold, i) {
  13091. translatedFrom = reversed ?
  13092. (horiz ? chart.plotWidth : 0) :
  13093. (horiz ? 0 : axis.toPixels(extremes.min));
  13094. translatedFrom = mathMin(mathMax(pick(translatedTo, translatedFrom), 0), chartSizeMax);
  13095. translatedTo = mathMin(mathMax(mathRound(axis.toPixels(pick(threshold.value, extremes.max), true)), 0), chartSizeMax);
  13096. if (ignoreZones) {
  13097. translatedFrom = translatedTo = axis.toPixels(extremes.max);
  13098. }
  13099. pxRange = Math.abs(translatedFrom - translatedTo);
  13100. pxPosMin = mathMin(translatedFrom, translatedTo);
  13101. pxPosMax = mathMax(translatedFrom, translatedTo);
  13102. if (axis.isXAxis) {
  13103. clipAttr = {
  13104. x: inverted ? pxPosMax : pxPosMin,
  13105. y: 0,
  13106. width: pxRange,
  13107. height: chartSizeMax
  13108. };
  13109. if (!horiz) {
  13110. clipAttr.x = chart.plotHeight - clipAttr.x;
  13111. }
  13112. } else {
  13113. clipAttr = {
  13114. x: 0,
  13115. y: inverted ? pxPosMax : pxPosMin,
  13116. width: chartSizeMax,
  13117. height: pxRange
  13118. };
  13119. if (horiz) {
  13120. clipAttr.y = chart.plotWidth - clipAttr.y;
  13121. }
  13122. }
  13123. /// VML SUPPPORT
  13124. if (chart.inverted && renderer.isVML) {
  13125. if (axis.isXAxis) {
  13126. clipAttr = {
  13127. x: 0,
  13128. y: reversed ? pxPosMin : pxPosMax,
  13129. height: clipAttr.width,
  13130. width: chart.chartWidth
  13131. };
  13132. } else {
  13133. clipAttr = {
  13134. x: clipAttr.y - chart.plotLeft - chart.spacingBox.x,
  13135. y: 0,
  13136. width: clipAttr.height,
  13137. height: chart.chartHeight
  13138. };
  13139. }
  13140. }
  13141. /// END OF VML SUPPORT
  13142. if (clips[i]) {
  13143. clips[i].animate(clipAttr);
  13144. } else {
  13145. clips[i] = renderer.clipRect(clipAttr);
  13146. if (graph) {
  13147. series['zoneGraph' + i].clip(clips[i]);
  13148. }
  13149. if (area) {
  13150. series['zoneArea' + i].clip(clips[i]);
  13151. }
  13152. }
  13153. // if this zone extends out of the axis, ignore the others
  13154. ignoreZones = threshold.value > extremes.max;
  13155. });
  13156. this.clips = clips;
  13157. }
  13158. },
  13159. /**
  13160. * Initialize and perform group inversion on series.group and series.markerGroup
  13161. */
  13162. invertGroups: function () {
  13163. var series = this,
  13164. chart = series.chart;
  13165. // Pie, go away (#1736)
  13166. if (!series.xAxis) {
  13167. return;
  13168. }
  13169. // A fixed size is needed for inversion to work
  13170. function setInvert() {
  13171. var size = {
  13172. width: series.yAxis.len,
  13173. height: series.xAxis.len
  13174. };
  13175. each(['group', 'markerGroup'], function (groupName) {
  13176. if (series[groupName]) {
  13177. series[groupName].attr(size).invert();
  13178. }
  13179. });
  13180. }
  13181. addEvent(chart, 'resize', setInvert); // do it on resize
  13182. addEvent(series, 'destroy', function () {
  13183. removeEvent(chart, 'resize', setInvert);
  13184. });
  13185. // Do it now
  13186. setInvert(); // do it now
  13187. // On subsequent render and redraw, just do setInvert without setting up events again
  13188. series.invertGroups = setInvert;
  13189. },
  13190. /**
  13191. * General abstraction for creating plot groups like series.group, series.dataLabelsGroup and
  13192. * series.markerGroup. On subsequent calls, the group will only be adjusted to the updated plot size.
  13193. */
  13194. plotGroup: function (prop, name, visibility, zIndex, parent) {
  13195. var group = this[prop],
  13196. isNew = !group;
  13197. // Generate it on first call
  13198. if (isNew) {
  13199. this[prop] = group = this.chart.renderer.g(name)
  13200. .attr({
  13201. zIndex: zIndex || 0.1 // IE8 and pointer logic use this
  13202. })
  13203. .add(parent);
  13204. group.addClass('highcharts-series-' + this.index);
  13205. }
  13206. // Place it on first and subsequent (redraw) calls
  13207. group.attr({ visibility: visibility })[isNew ? 'attr' : 'animate'](this.getPlotBox());
  13208. return group;
  13209. },
  13210. /**
  13211. * Get the translation and scale for the plot area of this series
  13212. */
  13213. getPlotBox: function () {
  13214. var chart = this.chart,
  13215. xAxis = this.xAxis,
  13216. yAxis = this.yAxis;
  13217. // Swap axes for inverted (#2339)
  13218. if (chart.inverted) {
  13219. xAxis = yAxis;
  13220. yAxis = this.xAxis;
  13221. }
  13222. return {
  13223. translateX: xAxis ? xAxis.left : chart.plotLeft,
  13224. translateY: yAxis ? yAxis.top : chart.plotTop,
  13225. scaleX: 1, // #1623
  13226. scaleY: 1
  13227. };
  13228. },
  13229. /**
  13230. * Render the graph and markers
  13231. */
  13232. render: function () {
  13233. var series = this,
  13234. chart = series.chart,
  13235. group,
  13236. options = series.options,
  13237. // Animation doesn't work in IE8 quirks when the group div is hidden,
  13238. // and looks bad in other oldIE
  13239. animDuration = !!series.animate && chart.renderer.isSVG && animObject(options.animation).duration,
  13240. visibility = series.visible ? 'inherit' : 'hidden', // #2597
  13241. zIndex = options.zIndex,
  13242. hasRendered = series.hasRendered,
  13243. chartSeriesGroup = chart.seriesGroup;
  13244. // the group
  13245. group = series.plotGroup(
  13246. 'group',
  13247. 'series',
  13248. visibility,
  13249. zIndex,
  13250. chartSeriesGroup
  13251. );
  13252. series.markerGroup = series.plotGroup(
  13253. 'markerGroup',
  13254. 'markers',
  13255. visibility,
  13256. zIndex,
  13257. chartSeriesGroup
  13258. );
  13259. // initiate the animation
  13260. if (animDuration) {
  13261. series.animate(true);
  13262. }
  13263. // cache attributes for shapes
  13264. series.getAttribs();
  13265. // SVGRenderer needs to know this before drawing elements (#1089, #1795)
  13266. group.inverted = series.isCartesian ? chart.inverted : false;
  13267. // draw the graph if any
  13268. if (series.drawGraph) {
  13269. series.drawGraph();
  13270. series.applyZones();
  13271. }
  13272. each(series.points, function (point) {
  13273. if (point.redraw) {
  13274. point.redraw();
  13275. }
  13276. });
  13277. // draw the data labels (inn pies they go before the points)
  13278. if (series.drawDataLabels) {
  13279. series.drawDataLabels();
  13280. }
  13281. // draw the points
  13282. if (series.visible) {
  13283. series.drawPoints();
  13284. }
  13285. // draw the mouse tracking area
  13286. if (series.drawTracker && series.options.enableMouseTracking !== false) {
  13287. series.drawTracker();
  13288. }
  13289. // Handle inverted series and tracker groups
  13290. if (chart.inverted) {
  13291. series.invertGroups();
  13292. }
  13293. // Initial clipping, must be defined after inverting groups for VML. Applies to columns etc. (#3839).
  13294. if (options.clip !== false && !series.sharedClipKey && !hasRendered) {
  13295. group.clip(chart.clipRect);
  13296. }
  13297. // Run the animation
  13298. if (animDuration) {
  13299. series.animate();
  13300. }
  13301. // Call the afterAnimate function on animation complete (but don't overwrite the animation.complete option
  13302. // which should be available to the user).
  13303. if (!hasRendered) {
  13304. series.animationTimeout = syncTimeout(function () {
  13305. series.afterAnimate();
  13306. }, animDuration);
  13307. }
  13308. series.isDirty = series.isDirtyData = false; // means data is in accordance with what you see
  13309. // (See #322) series.isDirty = series.isDirtyData = false; // means data is in accordance with what you see
  13310. series.hasRendered = true;
  13311. },
  13312. /**
  13313. * Redraw the series after an update in the axes.
  13314. */
  13315. redraw: function () {
  13316. var series = this,
  13317. chart = series.chart,
  13318. wasDirty = series.isDirty || series.isDirtyData, // cache it here as it is set to false in render, but used after
  13319. group = series.group,
  13320. xAxis = series.xAxis,
  13321. yAxis = series.yAxis;
  13322. // reposition on resize
  13323. if (group) {
  13324. if (chart.inverted) {
  13325. group.attr({
  13326. width: chart.plotWidth,
  13327. height: chart.plotHeight
  13328. });
  13329. }
  13330. group.animate({
  13331. translateX: pick(xAxis && xAxis.left, chart.plotLeft),
  13332. translateY: pick(yAxis && yAxis.top, chart.plotTop)
  13333. });
  13334. }
  13335. series.translate();
  13336. series.render();
  13337. if (wasDirty) { // #3868, #3945
  13338. delete this.kdTree;
  13339. }
  13340. },
  13341. /**
  13342. * KD Tree && PointSearching Implementation
  13343. */
  13344. kdDimensions: 1,
  13345. kdAxisArray: ['clientX', 'plotY'],
  13346. searchPoint: function (e, compareX) {
  13347. var series = this,
  13348. xAxis = series.xAxis,
  13349. yAxis = series.yAxis,
  13350. inverted = series.chart.inverted;
  13351. return this.searchKDTree({
  13352. clientX: inverted ? xAxis.len - e.chartY + xAxis.pos : e.chartX - xAxis.pos,
  13353. plotY: inverted ? yAxis.len - e.chartX + yAxis.pos : e.chartY - yAxis.pos
  13354. }, compareX);
  13355. },
  13356. buildKDTree: function () {
  13357. var series = this,
  13358. dimensions = series.kdDimensions;
  13359. // Internal function
  13360. function _kdtree(points, depth, dimensions) {
  13361. var axis,
  13362. median,
  13363. length = points && points.length;
  13364. if (length) {
  13365. // alternate between the axis
  13366. axis = series.kdAxisArray[depth % dimensions];
  13367. // sort point array
  13368. points.sort(function (a, b) {
  13369. return a[axis] - b[axis];
  13370. });
  13371. median = Math.floor(length / 2);
  13372. // build and return nod
  13373. return {
  13374. point: points[median],
  13375. left: _kdtree(points.slice(0, median), depth + 1, dimensions),
  13376. right: _kdtree(points.slice(median + 1), depth + 1, dimensions)
  13377. };
  13378. }
  13379. }
  13380. // Start the recursive build process with a clone of the points array and null points filtered out (#3873)
  13381. function startRecursive() {
  13382. series.kdTree = _kdtree(
  13383. series.getValidPoints(
  13384. null,
  13385. !series.directTouch // For line-type series restrict to plot area, but column-type series not (#3916, #4511)
  13386. ),
  13387. dimensions,
  13388. dimensions
  13389. );
  13390. }
  13391. delete series.kdTree;
  13392. // For testing tooltips, don't build async
  13393. syncTimeout(startRecursive, series.options.kdNow ? 0 : 1);
  13394. },
  13395. searchKDTree: function (point, compareX) {
  13396. var series = this,
  13397. kdX = this.kdAxisArray[0],
  13398. kdY = this.kdAxisArray[1],
  13399. kdComparer = compareX ? 'distX' : 'dist';
  13400. // Set the one and two dimensional distance on the point object
  13401. function setDistance(p1, p2) {
  13402. var x = (defined(p1[kdX]) && defined(p2[kdX])) ? Math.pow(p1[kdX] - p2[kdX], 2) : null,
  13403. y = (defined(p1[kdY]) && defined(p2[kdY])) ? Math.pow(p1[kdY] - p2[kdY], 2) : null,
  13404. r = (x || 0) + (y || 0);
  13405. p2.dist = defined(r) ? Math.sqrt(r) : Number.MAX_VALUE;
  13406. p2.distX = defined(x) ? Math.sqrt(x) : Number.MAX_VALUE;
  13407. }
  13408. function _search(search, tree, depth, dimensions) {
  13409. var point = tree.point,
  13410. axis = series.kdAxisArray[depth % dimensions],
  13411. tdist,
  13412. sideA,
  13413. sideB,
  13414. ret = point,
  13415. nPoint1,
  13416. nPoint2;
  13417. setDistance(search, point);
  13418. // Pick side based on distance to splitting point
  13419. tdist = search[axis] - point[axis];
  13420. sideA = tdist < 0 ? 'left' : 'right';
  13421. sideB = tdist < 0 ? 'right' : 'left';
  13422. // End of tree
  13423. if (tree[sideA]) {
  13424. nPoint1 = _search(search, tree[sideA], depth + 1, dimensions);
  13425. ret = (nPoint1[kdComparer] < ret[kdComparer] ? nPoint1 : point);
  13426. }
  13427. if (tree[sideB]) {
  13428. // compare distance to current best to splitting point to decide wether to check side B or not
  13429. if (Math.sqrt(tdist * tdist) < ret[kdComparer]) {
  13430. nPoint2 = _search(search, tree[sideB], depth + 1, dimensions);
  13431. ret = (nPoint2[kdComparer] < ret[kdComparer] ? nPoint2 : ret);
  13432. }
  13433. }
  13434. return ret;
  13435. }
  13436. if (!this.kdTree) {
  13437. this.buildKDTree();
  13438. }
  13439. if (this.kdTree) {
  13440. return _search(point,
  13441. this.kdTree, this.kdDimensions, this.kdDimensions);
  13442. }
  13443. }
  13444. }; // end Series prototype
  13445. /**
  13446. * The class for stack items
  13447. */
  13448. function StackItem(axis, options, isNegative, x, stackOption) {
  13449. var inverted = axis.chart.inverted;
  13450. this.axis = axis;
  13451. // Tells if the stack is negative
  13452. this.isNegative = isNegative;
  13453. // Save the options to be able to style the label
  13454. this.options = options;
  13455. // Save the x value to be able to position the label later
  13456. this.x = x;
  13457. // Initialize total value
  13458. this.total = null;
  13459. // This will keep each points' extremes stored by series.index and point index
  13460. this.points = {};
  13461. // Save the stack option on the series configuration object, and whether to treat it as percent
  13462. this.stack = stackOption;
  13463. this.leftCliff = 0;
  13464. this.rightCliff = 0;
  13465. // The align options and text align varies on whether the stack is negative and
  13466. // if the chart is inverted or not.
  13467. // First test the user supplied value, then use the dynamic.
  13468. this.alignOptions = {
  13469. align: options.align || (inverted ? (isNegative ? 'left' : 'right') : 'center'),
  13470. verticalAlign: options.verticalAlign || (inverted ? 'middle' : (isNegative ? 'bottom' : 'top')),
  13471. y: pick(options.y, inverted ? 4 : (isNegative ? 14 : -6)),
  13472. x: pick(options.x, inverted ? (isNegative ? -6 : 6) : 0)
  13473. };
  13474. this.textAlign = options.textAlign || (inverted ? (isNegative ? 'right' : 'left') : 'center');
  13475. }
  13476. StackItem.prototype = {
  13477. destroy: function () {
  13478. destroyObjectProperties(this, this.axis);
  13479. },
  13480. /**
  13481. * Renders the stack total label and adds it to the stack label group.
  13482. */
  13483. render: function (group) {
  13484. var options = this.options,
  13485. formatOption = options.format,
  13486. str = formatOption ?
  13487. format(formatOption, this) :
  13488. options.formatter.call(this); // format the text in the label
  13489. // Change the text to reflect the new total and set visibility to hidden in case the serie is hidden
  13490. if (this.label) {
  13491. this.label.attr({ text: str, visibility: 'hidden' });
  13492. // Create new label
  13493. } else {
  13494. this.label =
  13495. this.axis.chart.renderer.text(str, null, null, options.useHTML) // dummy positions, actual position updated with setOffset method in columnseries
  13496. .css(options.style) // apply style
  13497. .attr({
  13498. align: this.textAlign, // fix the text-anchor
  13499. rotation: options.rotation, // rotation
  13500. visibility: HIDDEN // hidden until setOffset is called
  13501. })
  13502. .add(group); // add to the labels-group
  13503. }
  13504. },
  13505. /**
  13506. * Sets the offset that the stack has from the x value and repositions the label.
  13507. */
  13508. setOffset: function (xOffset, xWidth) {
  13509. var stackItem = this,
  13510. axis = stackItem.axis,
  13511. chart = axis.chart,
  13512. inverted = chart.inverted,
  13513. reversed = axis.reversed,
  13514. neg = (this.isNegative && !reversed) || (!this.isNegative && reversed), // #4056
  13515. y = axis.translate(axis.usePercentage ? 100 : this.total, 0, 0, 0, 1), // stack value translated mapped to chart coordinates
  13516. yZero = axis.translate(0), // stack origin
  13517. h = mathAbs(y - yZero), // stack height
  13518. x = chart.xAxis[0].translate(this.x) + xOffset, // stack x position
  13519. plotHeight = chart.plotHeight,
  13520. stackBox = { // this is the box for the complete stack
  13521. x: inverted ? (neg ? y : y - h) : x,
  13522. y: inverted ? plotHeight - x - xWidth : (neg ? (plotHeight - y - h) : plotHeight - y),
  13523. width: inverted ? h : xWidth,
  13524. height: inverted ? xWidth : h
  13525. },
  13526. label = this.label,
  13527. alignAttr;
  13528. if (label) {
  13529. label.align(this.alignOptions, null, stackBox); // align the label to the box
  13530. // Set visibility (#678)
  13531. alignAttr = label.alignAttr;
  13532. label[this.options.crop === false || chart.isInsidePlot(alignAttr.x, alignAttr.y) ? 'show' : 'hide'](true);
  13533. }
  13534. }
  13535. };
  13536. /**
  13537. * Generate stacks for each series and calculate stacks total values
  13538. */
  13539. Chart.prototype.getStacks = function () {
  13540. var chart = this;
  13541. // reset stacks for each yAxis
  13542. each(chart.yAxis, function (axis) {
  13543. if (axis.stacks && axis.hasVisibleSeries) {
  13544. axis.oldStacks = axis.stacks;
  13545. }
  13546. });
  13547. each(chart.series, function (series) {
  13548. if (series.options.stacking && (series.visible === true || chart.options.chart.ignoreHiddenSeries === false)) {
  13549. series.stackKey = series.type + pick(series.options.stack, '');
  13550. }
  13551. });
  13552. };
  13553. // Stacking methods defined on the Axis prototype
  13554. /**
  13555. * Build the stacks from top down
  13556. */
  13557. Axis.prototype.buildStacks = function () {
  13558. var axisSeries = this.series,
  13559. series,
  13560. reversedStacks = pick(this.options.reversedStacks, true),
  13561. len = axisSeries.length,
  13562. i;
  13563. if (!this.isXAxis) {
  13564. this.usePercentage = false;
  13565. i = len;
  13566. while (i--) {
  13567. axisSeries[reversedStacks ? i : len - i - 1].setStackedPoints();
  13568. }
  13569. i = len;
  13570. while (i--) {
  13571. series = axisSeries[reversedStacks ? i : len - i - 1];
  13572. if (series.setStackCliffs) {
  13573. series.setStackCliffs();
  13574. }
  13575. }
  13576. // Loop up again to compute percent stack
  13577. if (this.usePercentage) {
  13578. for (i = 0; i < len; i++) {
  13579. axisSeries[i].setPercentStacks();
  13580. }
  13581. }
  13582. }
  13583. };
  13584. Axis.prototype.renderStackTotals = function () {
  13585. var axis = this,
  13586. chart = axis.chart,
  13587. renderer = chart.renderer,
  13588. stacks = axis.stacks,
  13589. stackKey,
  13590. oneStack,
  13591. stackCategory,
  13592. stackTotalGroup = axis.stackTotalGroup;
  13593. // Create a separate group for the stack total labels
  13594. if (!stackTotalGroup) {
  13595. axis.stackTotalGroup = stackTotalGroup =
  13596. renderer.g('stack-labels')
  13597. .attr({
  13598. visibility: VISIBLE,
  13599. zIndex: 6
  13600. })
  13601. .add();
  13602. }
  13603. // plotLeft/Top will change when y axis gets wider so we need to translate the
  13604. // stackTotalGroup at every render call. See bug #506 and #516
  13605. stackTotalGroup.translate(chart.plotLeft, chart.plotTop);
  13606. // Render each stack total
  13607. for (stackKey in stacks) {
  13608. oneStack = stacks[stackKey];
  13609. for (stackCategory in oneStack) {
  13610. oneStack[stackCategory].render(stackTotalGroup);
  13611. }
  13612. }
  13613. };
  13614. /**
  13615. * Set all the stacks to initial states and destroy unused ones.
  13616. */
  13617. Axis.prototype.resetStacks = function () {
  13618. var stacks = this.stacks,
  13619. type,
  13620. i;
  13621. if (!this.isXAxis) {
  13622. for (type in stacks) {
  13623. for (i in stacks[type]) {
  13624. // Clean up memory after point deletion (#1044, #4320)
  13625. if (stacks[type][i].touched < this.stacksTouched) {
  13626. stacks[type][i].destroy();
  13627. delete stacks[type][i];
  13628. // Reset stacks
  13629. } else {
  13630. stacks[type][i].total = null;
  13631. stacks[type][i].cum = 0;
  13632. }
  13633. }
  13634. }
  13635. }
  13636. };
  13637. Axis.prototype.cleanStacks = function () {
  13638. var stacks, type, i;
  13639. if (!this.isXAxis) {
  13640. if (this.oldStacks) {
  13641. stacks = this.stacks = this.oldStacks;
  13642. }
  13643. // reset stacks
  13644. for (type in stacks) {
  13645. for (i in stacks[type]) {
  13646. stacks[type][i].cum = stacks[type][i].total;
  13647. }
  13648. }
  13649. }
  13650. };
  13651. // Stacking methods defnied for Series prototype
  13652. /**
  13653. * Adds series' points value to corresponding stack
  13654. */
  13655. Series.prototype.setStackedPoints = function () {
  13656. if (!this.options.stacking || (this.visible !== true && this.chart.options.chart.ignoreHiddenSeries !== false)) {
  13657. return;
  13658. }
  13659. var series = this,
  13660. xData = series.processedXData,
  13661. yData = series.processedYData,
  13662. stackedYData = [],
  13663. yDataLength = yData.length,
  13664. seriesOptions = series.options,
  13665. threshold = seriesOptions.threshold,
  13666. stackThreshold = seriesOptions.startFromThreshold ? threshold : 0,
  13667. stackOption = seriesOptions.stack,
  13668. stacking = seriesOptions.stacking,
  13669. stackKey = series.stackKey,
  13670. negKey = '-' + stackKey,
  13671. negStacks = series.negStacks,
  13672. yAxis = series.yAxis,
  13673. stacks = yAxis.stacks,
  13674. oldStacks = yAxis.oldStacks,
  13675. stackIndicator,
  13676. isNegative,
  13677. stack,
  13678. other,
  13679. key,
  13680. pointKey,
  13681. i,
  13682. x,
  13683. y;
  13684. yAxis.stacksTouched += 1;
  13685. // loop over the non-null y values and read them into a local array
  13686. for (i = 0; i < yDataLength; i++) {
  13687. x = xData[i];
  13688. y = yData[i];
  13689. stackIndicator = series.getStackIndicator(stackIndicator, x, series.index);
  13690. pointKey = stackIndicator.key;
  13691. // Read stacked values into a stack based on the x value,
  13692. // the sign of y and the stack key. Stacking is also handled for null values (#739)
  13693. isNegative = negStacks && y < (stackThreshold ? 0 : threshold);
  13694. key = isNegative ? negKey : stackKey;
  13695. // Create empty object for this stack if it doesn't exist yet
  13696. if (!stacks[key]) {
  13697. stacks[key] = {};
  13698. }
  13699. // Initialize StackItem for this x
  13700. if (!stacks[key][x]) {
  13701. if (oldStacks[key] && oldStacks[key][x]) {
  13702. stacks[key][x] = oldStacks[key][x];
  13703. stacks[key][x].total = null;
  13704. } else {
  13705. stacks[key][x] = new StackItem(yAxis, yAxis.options.stackLabels, isNegative, x, stackOption);
  13706. }
  13707. }
  13708. // If the StackItem doesn't exist, create it first
  13709. stack = stacks[key][x];
  13710. if (y !== null) {
  13711. stack.points[pointKey] = stack.points[series.index] = [pick(stack.cum, stackThreshold)];
  13712. stack.touched = yAxis.stacksTouched;
  13713. // In area charts, if there are multiple points on the same X value, let the
  13714. // area fill the full span of those points
  13715. if (stackIndicator.index > 0 && series.singleStacks === false) {
  13716. stack.points[pointKey][0] = stack.points[series.index + ',' + x + ',0'][0];
  13717. }
  13718. }
  13719. // Add value to the stack total
  13720. if (stacking === 'percent') {
  13721. // Percent stacked column, totals are the same for the positive and negative stacks
  13722. other = isNegative ? stackKey : negKey;
  13723. if (negStacks && stacks[other] && stacks[other][x]) {
  13724. other = stacks[other][x];
  13725. stack.total = other.total = mathMax(other.total, stack.total) + mathAbs(y) || 0;
  13726. // Percent stacked areas
  13727. } else {
  13728. stack.total = correctFloat(stack.total + (mathAbs(y) || 0));
  13729. }
  13730. } else {
  13731. stack.total = correctFloat(stack.total + (y || 0));
  13732. }
  13733. stack.cum = pick(stack.cum, stackThreshold) + (y || 0);
  13734. if (y !== null) {
  13735. stack.points[pointKey].push(stack.cum);
  13736. stackedYData[i] = stack.cum;
  13737. }
  13738. }
  13739. if (stacking === 'percent') {
  13740. yAxis.usePercentage = true;
  13741. }
  13742. this.stackedYData = stackedYData; // To be used in getExtremes
  13743. // Reset old stacks
  13744. yAxis.oldStacks = {};
  13745. };
  13746. /**
  13747. * Iterate over all stacks and compute the absolute values to percent
  13748. */
  13749. Series.prototype.setPercentStacks = function () {
  13750. var series = this,
  13751. stackKey = series.stackKey,
  13752. stacks = series.yAxis.stacks,
  13753. processedXData = series.processedXData,
  13754. stackIndicator;
  13755. each([stackKey, '-' + stackKey], function (key) {
  13756. var i = processedXData.length,
  13757. x,
  13758. stack,
  13759. pointExtremes,
  13760. totalFactor;
  13761. while (i--) {
  13762. x = processedXData[i];
  13763. stackIndicator = series.getStackIndicator(stackIndicator, x, series.index);
  13764. stack = stacks[key] && stacks[key][x];
  13765. pointExtremes = stack && stack.points[stackIndicator.key];
  13766. if (pointExtremes) {
  13767. totalFactor = stack.total ? 100 / stack.total : 0;
  13768. pointExtremes[0] = correctFloat(pointExtremes[0] * totalFactor); // Y bottom value
  13769. pointExtremes[1] = correctFloat(pointExtremes[1] * totalFactor); // Y value
  13770. series.stackedYData[i] = pointExtremes[1];
  13771. }
  13772. }
  13773. });
  13774. };
  13775. /**
  13776. * Get stack indicator, according to it's x-value, to determine points with the same x-value
  13777. */
  13778. Series.prototype.getStackIndicator = function (stackIndicator, x, index) {
  13779. if (!defined(stackIndicator) || stackIndicator.x !== x) {
  13780. stackIndicator = {
  13781. x: x,
  13782. index: 0
  13783. };
  13784. } else {
  13785. stackIndicator.index++;
  13786. }
  13787. stackIndicator.key = [index, x, stackIndicator.index].join(',');
  13788. return stackIndicator;
  13789. };
  13790. // Extend the Chart prototype for dynamic methods
  13791. extend(Chart.prototype, {
  13792. /**
  13793. * Add a series dynamically after time
  13794. *
  13795. * @param {Object} options The config options
  13796. * @param {Boolean} redraw Whether to redraw the chart after adding. Defaults to true.
  13797. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  13798. * configuration
  13799. *
  13800. * @return {Object} series The newly created series object
  13801. */
  13802. addSeries: function (options, redraw, animation) {
  13803. var series,
  13804. chart = this;
  13805. if (options) {
  13806. redraw = pick(redraw, true); // defaults to true
  13807. fireEvent(chart, 'addSeries', { options: options }, function () {
  13808. series = chart.initSeries(options);
  13809. chart.isDirtyLegend = true; // the series array is out of sync with the display
  13810. chart.linkSeries();
  13811. if (redraw) {
  13812. chart.redraw(animation);
  13813. }
  13814. });
  13815. }
  13816. return series;
  13817. },
  13818. /**
  13819. * Add an axis to the chart
  13820. * @param {Object} options The axis option
  13821. * @param {Boolean} isX Whether it is an X axis or a value axis
  13822. */
  13823. addAxis: function (options, isX, redraw, animation) {
  13824. var key = isX ? 'xAxis' : 'yAxis',
  13825. chartOptions = this.options,
  13826. userOptions = merge(options, {
  13827. index: this[key].length,
  13828. isX: isX
  13829. });
  13830. new Axis(this, userOptions); // eslint-disable-line no-new
  13831. // Push the new axis options to the chart options
  13832. chartOptions[key] = splat(chartOptions[key] || {});
  13833. chartOptions[key].push(userOptions);
  13834. if (pick(redraw, true)) {
  13835. this.redraw(animation);
  13836. }
  13837. },
  13838. /**
  13839. * Dim the chart and show a loading text or symbol
  13840. * @param {String} str An optional text to show in the loading label instead of the default one
  13841. */
  13842. showLoading: function (str) {
  13843. var chart = this,
  13844. options = chart.options,
  13845. loadingDiv = chart.loadingDiv,
  13846. loadingOptions = options.loading,
  13847. setLoadingSize = function () {
  13848. if (loadingDiv) {
  13849. css(loadingDiv, {
  13850. left: chart.plotLeft + PX,
  13851. top: chart.plotTop + PX,
  13852. width: chart.plotWidth + PX,
  13853. height: chart.plotHeight + PX
  13854. });
  13855. }
  13856. };
  13857. // create the layer at the first call
  13858. if (!loadingDiv) {
  13859. chart.loadingDiv = loadingDiv = createElement(DIV, {
  13860. className: PREFIX + 'loading'
  13861. }, extend(loadingOptions.style, {
  13862. zIndex: 10,
  13863. display: NONE
  13864. }), chart.container);
  13865. chart.loadingSpan = createElement(
  13866. 'span',
  13867. null,
  13868. loadingOptions.labelStyle,
  13869. loadingDiv
  13870. );
  13871. addEvent(chart, 'redraw', setLoadingSize); // #1080
  13872. }
  13873. // update text
  13874. chart.loadingSpan.innerHTML = str || options.lang.loading;
  13875. // show it
  13876. if (!chart.loadingShown) {
  13877. css(loadingDiv, {
  13878. opacity: 0,
  13879. display: ''
  13880. });
  13881. animate(loadingDiv, {
  13882. opacity: loadingOptions.style.opacity
  13883. }, {
  13884. duration: loadingOptions.showDuration || 0
  13885. });
  13886. chart.loadingShown = true;
  13887. }
  13888. setLoadingSize();
  13889. },
  13890. /**
  13891. * Hide the loading layer
  13892. */
  13893. hideLoading: function () {
  13894. var options = this.options,
  13895. loadingDiv = this.loadingDiv;
  13896. if (loadingDiv) {
  13897. animate(loadingDiv, {
  13898. opacity: 0
  13899. }, {
  13900. duration: options.loading.hideDuration || 100,
  13901. complete: function () {
  13902. css(loadingDiv, { display: NONE });
  13903. }
  13904. });
  13905. }
  13906. this.loadingShown = false;
  13907. }
  13908. });
  13909. // extend the Point prototype for dynamic methods
  13910. extend(Point.prototype, {
  13911. /**
  13912. * Update the point with new options (typically x/y data) and optionally redraw the series.
  13913. *
  13914. * @param {Object} options Point options as defined in the series.data array
  13915. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  13916. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  13917. * configuration
  13918. *
  13919. */
  13920. update: function (options, redraw, animation, runEvent) {
  13921. var point = this,
  13922. series = point.series,
  13923. graphic = point.graphic,
  13924. i,
  13925. chart = series.chart,
  13926. seriesOptions = series.options,
  13927. names = series.xAxis && series.xAxis.names;
  13928. redraw = pick(redraw, true);
  13929. function update() {
  13930. point.applyOptions(options);
  13931. // Update visuals
  13932. if (point.y === null && graphic) { // #4146
  13933. point.graphic = graphic.destroy();
  13934. }
  13935. if (isObject(options) && !isArray(options)) {
  13936. // Defer the actual redraw until getAttribs has been called (#3260)
  13937. point.redraw = function () {
  13938. if (graphic && graphic.element) {
  13939. if (options && options.marker && options.marker.symbol) {
  13940. point.graphic = graphic.destroy();
  13941. }
  13942. }
  13943. if (options && options.dataLabels && point.dataLabel) { // #2468
  13944. point.dataLabel = point.dataLabel.destroy();
  13945. }
  13946. point.redraw = null;
  13947. };
  13948. }
  13949. // record changes in the parallel arrays
  13950. i = point.index;
  13951. series.updateParallelArrays(point, i);
  13952. if (names && point.name) {
  13953. names[point.x] = point.name;
  13954. }
  13955. // Record the options to options.data. If there is an object from before,
  13956. // use point options, otherwise use raw options. (#4701)
  13957. seriesOptions.data[i] = (isObject(seriesOptions.data[i]) && !isArray(seriesOptions.data[i])) ? point.options : options;
  13958. // redraw
  13959. series.isDirty = series.isDirtyData = true;
  13960. if (!series.fixedBox && series.hasCartesianSeries) { // #1906, #2320
  13961. chart.isDirtyBox = true;
  13962. }
  13963. if (seriesOptions.legendType === 'point') { // #1831, #1885
  13964. chart.isDirtyLegend = true;
  13965. }
  13966. if (redraw) {
  13967. chart.redraw(animation);
  13968. }
  13969. }
  13970. // Fire the event with a default handler of doing the update
  13971. if (runEvent === false) { // When called from setData
  13972. update();
  13973. } else {
  13974. point.firePointEvent('update', { options: options }, update);
  13975. }
  13976. },
  13977. /**
  13978. * Remove a point and optionally redraw the series and if necessary the axes
  13979. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  13980. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  13981. * configuration
  13982. */
  13983. remove: function (redraw, animation) {
  13984. this.series.removePoint(inArray(this, this.series.data), redraw, animation);
  13985. }
  13986. });
  13987. // Extend the series prototype for dynamic methods
  13988. extend(Series.prototype, {
  13989. /**
  13990. * Add a point dynamically after chart load time
  13991. * @param {Object} options Point options as given in series.data
  13992. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  13993. * @param {Boolean} shift If shift is true, a point is shifted off the start
  13994. * of the series as one is appended to the end.
  13995. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  13996. * configuration
  13997. */
  13998. addPoint: function (options, redraw, shift, animation) {
  13999. var series = this,
  14000. seriesOptions = series.options,
  14001. data = series.data,
  14002. graph = series.graph,
  14003. area = series.area,
  14004. chart = series.chart,
  14005. names = series.xAxis && series.xAxis.names,
  14006. currentShift = (graph && graph.shift) || 0,
  14007. shiftShapes = ['graph', 'area'],
  14008. dataOptions = seriesOptions.data,
  14009. point,
  14010. isInTheMiddle,
  14011. xData = series.xData,
  14012. i,
  14013. x;
  14014. setAnimation(animation, chart);
  14015. // Make graph animate sideways
  14016. if (shift) {
  14017. i = series.zones.length;
  14018. while (i--) {
  14019. shiftShapes.push('zoneGraph' + i, 'zoneArea' + i);
  14020. }
  14021. each(shiftShapes, function (shape) {
  14022. if (series[shape]) {
  14023. series[shape].shift = currentShift + (seriesOptions.step ? 2 : 1);
  14024. }
  14025. });
  14026. }
  14027. if (area) {
  14028. area.isArea = true; // needed in animation, both with and without shift
  14029. }
  14030. // Optional redraw, defaults to true
  14031. redraw = pick(redraw, true);
  14032. // Get options and push the point to xData, yData and series.options. In series.generatePoints
  14033. // the Point instance will be created on demand and pushed to the series.data array.
  14034. point = { series: series };
  14035. series.pointClass.prototype.applyOptions.apply(point, [options]);
  14036. x = point.x;
  14037. // Get the insertion point
  14038. i = xData.length;
  14039. if (series.requireSorting && x < xData[i - 1]) {
  14040. isInTheMiddle = true;
  14041. while (i && xData[i - 1] > x) {
  14042. i--;
  14043. }
  14044. }
  14045. series.updateParallelArrays(point, 'splice', i, 0, 0); // insert undefined item
  14046. series.updateParallelArrays(point, i); // update it
  14047. if (names && point.name) {
  14048. names[x] = point.name;
  14049. }
  14050. dataOptions.splice(i, 0, options);
  14051. if (isInTheMiddle) {
  14052. series.data.splice(i, 0, null);
  14053. series.processData();
  14054. }
  14055. // Generate points to be added to the legend (#1329)
  14056. if (seriesOptions.legendType === 'point') {
  14057. series.generatePoints();
  14058. }
  14059. // Shift the first point off the parallel arrays
  14060. if (shift) {
  14061. if (data[0] && data[0].remove) {
  14062. data[0].remove(false);
  14063. } else {
  14064. data.shift();
  14065. series.updateParallelArrays(point, 'shift');
  14066. dataOptions.shift();
  14067. }
  14068. }
  14069. // redraw
  14070. series.isDirty = true;
  14071. series.isDirtyData = true;
  14072. if (redraw) {
  14073. series.getAttribs(); // #1937
  14074. chart.redraw();
  14075. }
  14076. },
  14077. /**
  14078. * Remove a point (rendered or not), by index
  14079. */
  14080. removePoint: function (i, redraw, animation) {
  14081. var series = this,
  14082. data = series.data,
  14083. point = data[i],
  14084. points = series.points,
  14085. chart = series.chart,
  14086. remove = function () {
  14087. if (points && points.length === data.length) { // #4935
  14088. points.splice(i, 1);
  14089. }
  14090. data.splice(i, 1);
  14091. series.options.data.splice(i, 1);
  14092. series.updateParallelArrays(point || { series: series }, 'splice', i, 1);
  14093. if (point) {
  14094. point.destroy();
  14095. }
  14096. // redraw
  14097. series.isDirty = true;
  14098. series.isDirtyData = true;
  14099. if (redraw) {
  14100. chart.redraw();
  14101. }
  14102. };
  14103. setAnimation(animation, chart);
  14104. redraw = pick(redraw, true);
  14105. // Fire the event with a default handler of removing the point
  14106. if (point) {
  14107. point.firePointEvent('remove', null, remove);
  14108. } else {
  14109. remove();
  14110. }
  14111. },
  14112. /**
  14113. * Remove a series and optionally redraw the chart
  14114. *
  14115. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  14116. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  14117. * configuration
  14118. */
  14119. remove: function (redraw, animation) {
  14120. var series = this,
  14121. chart = series.chart;
  14122. // Fire the event with a default handler of removing the point
  14123. fireEvent(series, 'remove', null, function () {
  14124. // Destroy elements
  14125. series.destroy();
  14126. // Redraw
  14127. chart.isDirtyLegend = chart.isDirtyBox = true;
  14128. chart.linkSeries();
  14129. if (pick(redraw, true)) {
  14130. chart.redraw(animation);
  14131. }
  14132. });
  14133. },
  14134. /**
  14135. * Update the series with a new set of options
  14136. */
  14137. update: function (newOptions, redraw) {
  14138. var series = this,
  14139. chart = this.chart,
  14140. // must use user options when changing type because this.options is merged
  14141. // in with type specific plotOptions
  14142. oldOptions = this.userOptions,
  14143. oldType = this.type,
  14144. proto = seriesTypes[oldType].prototype,
  14145. preserve = ['group', 'markerGroup', 'dataLabelsGroup'],
  14146. n;
  14147. // If we're changing type or zIndex, create new groups (#3380, #3404)
  14148. if ((newOptions.type && newOptions.type !== oldType) || newOptions.zIndex !== undefined) {
  14149. preserve.length = 0;
  14150. }
  14151. // Make sure groups are not destroyed (#3094)
  14152. each(preserve, function (prop) {
  14153. preserve[prop] = series[prop];
  14154. delete series[prop];
  14155. });
  14156. // Do the merge, with some forced options
  14157. newOptions = merge(oldOptions, {
  14158. animation: false,
  14159. index: this.index,
  14160. pointStart: this.xData[0] // when updating after addPoint
  14161. }, { data: this.options.data }, newOptions);
  14162. // Destroy the series and delete all properties. Reinsert all methods
  14163. // and properties from the new type prototype (#2270, #3719)
  14164. this.remove(false);
  14165. for (n in proto) {
  14166. this[n] = UNDEFINED;
  14167. }
  14168. extend(this, seriesTypes[newOptions.type || oldType].prototype);
  14169. // Re-register groups (#3094)
  14170. each(preserve, function (prop) {
  14171. series[prop] = preserve[prop];
  14172. });
  14173. this.init(chart, newOptions);
  14174. chart.linkSeries(); // Links are lost in this.remove (#3028)
  14175. if (pick(redraw, true)) {
  14176. chart.redraw(false);
  14177. }
  14178. }
  14179. });
  14180. // Extend the Axis.prototype for dynamic methods
  14181. extend(Axis.prototype, {
  14182. /**
  14183. * Update the axis with a new options structure
  14184. */
  14185. update: function (newOptions, redraw) {
  14186. var chart = this.chart;
  14187. newOptions = chart.options[this.coll][this.options.index] = merge(this.userOptions, newOptions);
  14188. this.destroy(true);
  14189. this._addedPlotLB = this.chart._labelPanes = UNDEFINED; // #1611, #2887, #4314
  14190. this.init(chart, extend(newOptions, { events: UNDEFINED }));
  14191. chart.isDirtyBox = true;
  14192. if (pick(redraw, true)) {
  14193. chart.redraw();
  14194. }
  14195. },
  14196. /**
  14197. * Remove the axis from the chart
  14198. */
  14199. remove: function (redraw) {
  14200. var chart = this.chart,
  14201. key = this.coll, // xAxis or yAxis
  14202. axisSeries = this.series,
  14203. i = axisSeries.length;
  14204. // Remove associated series (#2687)
  14205. while (i--) {
  14206. if (axisSeries[i]) {
  14207. axisSeries[i].remove(false);
  14208. }
  14209. }
  14210. // Remove the axis
  14211. erase(chart.axes, this);
  14212. erase(chart[key], this);
  14213. chart.options[key].splice(this.options.index, 1);
  14214. each(chart[key], function (axis, i) { // Re-index, #1706
  14215. axis.options.index = i;
  14216. });
  14217. this.destroy();
  14218. chart.isDirtyBox = true;
  14219. if (pick(redraw, true)) {
  14220. chart.redraw();
  14221. }
  14222. },
  14223. /**
  14224. * Update the axis title by options
  14225. */
  14226. setTitle: function (newTitleOptions, redraw) {
  14227. this.update({ title: newTitleOptions }, redraw);
  14228. },
  14229. /**
  14230. * Set new axis categories and optionally redraw
  14231. * @param {Array} categories
  14232. * @param {Boolean} redraw
  14233. */
  14234. setCategories: function (categories, redraw) {
  14235. this.update({ categories: categories }, redraw);
  14236. }
  14237. });
  14238. /**
  14239. * LineSeries object
  14240. */
  14241. var LineSeries = extendClass(Series);
  14242. seriesTypes.line = LineSeries;
  14243. /**
  14244. * Set the default options for area
  14245. */
  14246. defaultPlotOptions.area = merge(defaultSeriesOptions, {
  14247. softThreshold: false,
  14248. threshold: 0
  14249. // trackByArea: false,
  14250. // lineColor: null, // overrides color, but lets fillColor be unaltered
  14251. // fillOpacity: 0.75,
  14252. // fillColor: null
  14253. });
  14254. /**
  14255. * AreaSeries object
  14256. */
  14257. var AreaSeries = extendClass(Series, {
  14258. type: 'area',
  14259. singleStacks: false,
  14260. /**
  14261. * Return an array of stacked points, where null and missing points are replaced by
  14262. * dummy points in order for gaps to be drawn correctly in stacks.
  14263. */
  14264. getStackPoints: function () {
  14265. var series = this,
  14266. segment = [],
  14267. keys = [],
  14268. xAxis = this.xAxis,
  14269. yAxis = this.yAxis,
  14270. stack = yAxis.stacks[this.stackKey],
  14271. pointMap = {},
  14272. points = this.points,
  14273. seriesIndex = series.index,
  14274. yAxisSeries = yAxis.series,
  14275. seriesLength = yAxisSeries.length,
  14276. visibleSeries,
  14277. upOrDown = pick(yAxis.options.reversedStacks, true) ? 1 : -1,
  14278. i,
  14279. x;
  14280. if (this.options.stacking) {
  14281. // Create a map where we can quickly look up the points by their X value.
  14282. for (i = 0; i < points.length; i++) {
  14283. pointMap[points[i].x] = points[i];
  14284. }
  14285. // Sort the keys (#1651)
  14286. for (x in stack) {
  14287. if (stack[x].total !== null) { // nulled after switching between grouping and not (#1651, #2336)
  14288. keys.push(x);
  14289. }
  14290. }
  14291. keys.sort(function (a, b) {
  14292. return a - b;
  14293. });
  14294. visibleSeries = map(yAxisSeries, function () {
  14295. return this.visible;
  14296. });
  14297. each(keys, function (x, idx) {
  14298. var y = 0,
  14299. stackPoint,
  14300. stackedValues;
  14301. if (pointMap[x] && !pointMap[x].isNull) {
  14302. segment.push(pointMap[x]);
  14303. // Find left and right cliff. -1 goes left, 1 goes right.
  14304. each([-1, 1], function (direction) {
  14305. var nullName = direction === 1 ? 'rightNull' : 'leftNull',
  14306. cliffName = direction === 1 ? 'rightCliff' : 'leftCliff',
  14307. cliff = 0,
  14308. otherStack = stack[keys[idx + direction]];
  14309. // If there is a stack next to this one, to the left or to the right...
  14310. if (otherStack) {
  14311. i = seriesIndex;
  14312. while (i >= 0 && i < seriesLength) { // Can go either up or down, depending on reversedStacks
  14313. stackPoint = otherStack.points[i];
  14314. if (!stackPoint) {
  14315. // If the next point in this series is missing, mark the point
  14316. // with point.leftNull or point.rightNull = true.
  14317. if (i === seriesIndex) {
  14318. pointMap[x][nullName] = true;
  14319. // If there are missing points in the next stack in any of the
  14320. // series below this one, we need to substract the missing values
  14321. // and add a hiatus to the left or right.
  14322. } else if (visibleSeries[i]) {
  14323. stackedValues = stack[x].points[i];
  14324. if (stackedValues) {
  14325. cliff -= stackedValues[1] - stackedValues[0];
  14326. }
  14327. }
  14328. }
  14329. // When reversedStacks is true, loop up, else loop down
  14330. i += upOrDown;
  14331. }
  14332. }
  14333. pointMap[x][cliffName] = cliff;
  14334. });
  14335. // There is no point for this X value in this series, so we
  14336. // insert a dummy point in order for the areas to be drawn
  14337. // correctly.
  14338. } else {
  14339. // Loop down the stack to find the series below this one that has
  14340. // a value (#1991)
  14341. i = seriesIndex;
  14342. while (i >= 0 && i < seriesLength) {
  14343. stackPoint = stack[x].points[i];
  14344. if (stackPoint) {
  14345. y = stackPoint[1];
  14346. break;
  14347. }
  14348. // When reversedStacks is true, loop up, else loop down
  14349. i += upOrDown;
  14350. }
  14351. y = yAxis.toPixels(y, true);
  14352. segment.push({
  14353. isNull: true,
  14354. plotX: xAxis.toPixels(x, true),
  14355. plotY: y,
  14356. yBottom: y
  14357. });
  14358. }
  14359. });
  14360. }
  14361. return segment;
  14362. },
  14363. getGraphPath: function (points) {
  14364. var getGraphPath = Series.prototype.getGraphPath,
  14365. graphPath,
  14366. options = this.options,
  14367. stacking = options.stacking,
  14368. yAxis = this.yAxis,
  14369. topPath,
  14370. //topPoints = [],
  14371. bottomPath,
  14372. bottomPoints = [],
  14373. graphPoints = [],
  14374. seriesIndex = this.index,
  14375. i,
  14376. areaPath,
  14377. plotX,
  14378. stacks = yAxis.stacks[this.stackKey],
  14379. threshold = options.threshold,
  14380. translatedThreshold = yAxis.getThreshold(options.threshold),
  14381. isNull,
  14382. yBottom,
  14383. connectNulls = options.connectNulls || stacking === 'percent',
  14384. /**
  14385. * To display null points in underlying stacked series, this series graph must be
  14386. * broken, and the area also fall down to fill the gap left by the null point. #2069
  14387. */
  14388. addDummyPoints = function (i, otherI, side) {
  14389. var point = points[i],
  14390. stackedValues = stacking && stacks[point.x].points[seriesIndex],
  14391. nullVal = point[side + 'Null'] || 0,
  14392. cliffVal = point[side + 'Cliff'] || 0,
  14393. top,
  14394. bottom,
  14395. isNull = true;
  14396. if (cliffVal || nullVal) {
  14397. top = (nullVal ? stackedValues[0] : stackedValues[1]) + cliffVal;
  14398. bottom = stackedValues[0] + cliffVal;
  14399. isNull = !!nullVal;
  14400. } else if (!stacking && points[otherI] && points[otherI].isNull) {
  14401. top = bottom = threshold;
  14402. }
  14403. // Add to the top and bottom line of the area
  14404. if (top !== undefined) {
  14405. graphPoints.push({
  14406. plotX: plotX,
  14407. plotY: top === null ? translatedThreshold : yAxis.getThreshold(top),
  14408. isNull: isNull
  14409. });
  14410. bottomPoints.push({
  14411. plotX: plotX,
  14412. plotY: bottom === null ? translatedThreshold : yAxis.getThreshold(bottom)
  14413. });
  14414. }
  14415. };
  14416. // Find what points to use
  14417. points = points || this.points;
  14418. // Fill in missing points
  14419. if (stacking) {
  14420. points = this.getStackPoints();
  14421. }
  14422. for (i = 0; i < points.length; i++) {
  14423. isNull = points[i].isNull;
  14424. plotX = pick(points[i].rectPlotX, points[i].plotX);
  14425. yBottom = pick(points[i].yBottom, translatedThreshold);
  14426. if (!isNull || connectNulls) {
  14427. if (!connectNulls) {
  14428. addDummyPoints(i, i - 1, 'left');
  14429. }
  14430. if (!(isNull && !stacking && connectNulls)) { // Skip null point when stacking is false and connectNulls true
  14431. graphPoints.push(points[i]);
  14432. bottomPoints.push({
  14433. x: i,
  14434. plotX: plotX,
  14435. plotY: yBottom
  14436. });
  14437. }
  14438. if (!connectNulls) {
  14439. addDummyPoints(i, i + 1, 'right');
  14440. }
  14441. }
  14442. }
  14443. topPath = getGraphPath.call(this, graphPoints, true, true);
  14444. bottomPoints.reversed = true;
  14445. bottomPath = getGraphPath.call(this, bottomPoints, true, true);
  14446. if (bottomPath.length) {
  14447. bottomPath[0] = L;
  14448. }
  14449. areaPath = topPath.concat(bottomPath);
  14450. graphPath = getGraphPath.call(this, graphPoints, false, connectNulls); // TODO: don't set leftCliff and rightCliff when connectNulls?
  14451. this.areaPath = areaPath;
  14452. return graphPath;
  14453. },
  14454. /**
  14455. * Draw the graph and the underlying area. This method calls the Series base
  14456. * function and adds the area. The areaPath is calculated in the getSegmentPath
  14457. * method called from Series.prototype.drawGraph.
  14458. */
  14459. drawGraph: function () {
  14460. // Define or reset areaPath
  14461. this.areaPath = [];
  14462. // Call the base method
  14463. Series.prototype.drawGraph.apply(this);
  14464. // Define local variables
  14465. var series = this,
  14466. areaPath = this.areaPath,
  14467. options = this.options,
  14468. zones = this.zones,
  14469. props = [['area', this.color, options.fillColor]]; // area name, main color, fill color
  14470. each(zones, function (threshold, i) {
  14471. props.push(['zoneArea' + i, threshold.color || series.color, threshold.fillColor || options.fillColor]);
  14472. });
  14473. each(props, function (prop) {
  14474. var areaKey = prop[0],
  14475. area = series[areaKey],
  14476. attr;
  14477. // Create or update the area
  14478. if (area) { // update
  14479. area.animate({ d: areaPath });
  14480. } else { // create
  14481. attr = {
  14482. fill: prop[2] || prop[1],
  14483. zIndex: 0 // #1069
  14484. };
  14485. if (!prop[2]) {
  14486. attr['fill-opacity'] = pick(options.fillOpacity, 0.75);
  14487. }
  14488. series[areaKey] = series.chart.renderer.path(areaPath)
  14489. .attr(attr)
  14490. .add(series.group);
  14491. }
  14492. });
  14493. },
  14494. drawLegendSymbol: LegendSymbolMixin.drawRectangle
  14495. });
  14496. seriesTypes.area = AreaSeries;
  14497. /**
  14498. * Set the default options for spline
  14499. */
  14500. defaultPlotOptions.spline = merge(defaultSeriesOptions);
  14501. /**
  14502. * SplineSeries object
  14503. */
  14504. var SplineSeries = extendClass(Series, {
  14505. type: 'spline',
  14506. /**
  14507. * Get the spline segment from a given point's previous neighbour to the given point
  14508. */
  14509. getPointSpline: function (points, point, i) {
  14510. var smoothing = 1.5, // 1 means control points midway between points, 2 means 1/3 from the point, 3 is 1/4 etc
  14511. denom = smoothing + 1,
  14512. plotX = point.plotX,
  14513. plotY = point.plotY,
  14514. lastPoint = points[i - 1],
  14515. nextPoint = points[i + 1],
  14516. leftContX,
  14517. leftContY,
  14518. rightContX,
  14519. rightContY,
  14520. ret;
  14521. // Find control points
  14522. if (lastPoint && !lastPoint.isNull && nextPoint && !nextPoint.isNull) {
  14523. var lastX = lastPoint.plotX,
  14524. lastY = lastPoint.plotY,
  14525. nextX = nextPoint.plotX,
  14526. nextY = nextPoint.plotY,
  14527. correction = 0;
  14528. leftContX = (smoothing * plotX + lastX) / denom;
  14529. leftContY = (smoothing * plotY + lastY) / denom;
  14530. rightContX = (smoothing * plotX + nextX) / denom;
  14531. rightContY = (smoothing * plotY + nextY) / denom;
  14532. // Have the two control points make a straight line through main point
  14533. if (rightContX !== leftContX) { // #5016, division by zero
  14534. correction = ((rightContY - leftContY) * (rightContX - plotX)) /
  14535. (rightContX - leftContX) + plotY - rightContY;
  14536. }
  14537. leftContY += correction;
  14538. rightContY += correction;
  14539. // to prevent false extremes, check that control points are between
  14540. // neighbouring points' y values
  14541. if (leftContY > lastY && leftContY > plotY) {
  14542. leftContY = mathMax(lastY, plotY);
  14543. rightContY = 2 * plotY - leftContY; // mirror of left control point
  14544. } else if (leftContY < lastY && leftContY < plotY) {
  14545. leftContY = mathMin(lastY, plotY);
  14546. rightContY = 2 * plotY - leftContY;
  14547. }
  14548. if (rightContY > nextY && rightContY > plotY) {
  14549. rightContY = mathMax(nextY, plotY);
  14550. leftContY = 2 * plotY - rightContY;
  14551. } else if (rightContY < nextY && rightContY < plotY) {
  14552. rightContY = mathMin(nextY, plotY);
  14553. leftContY = 2 * plotY - rightContY;
  14554. }
  14555. // record for drawing in next point
  14556. point.rightContX = rightContX;
  14557. point.rightContY = rightContY;
  14558. }
  14559. // Visualize control points for debugging
  14560. /*
  14561. if (leftContX) {
  14562. this.chart.renderer.circle(leftContX + this.chart.plotLeft, leftContY + this.chart.plotTop, 2)
  14563. .attr({
  14564. stroke: 'red',
  14565. 'stroke-width': 1,
  14566. fill: 'none'
  14567. })
  14568. .add();
  14569. this.chart.renderer.path(['M', leftContX + this.chart.plotLeft, leftContY + this.chart.plotTop,
  14570. 'L', plotX + this.chart.plotLeft, plotY + this.chart.plotTop])
  14571. .attr({
  14572. stroke: 'red',
  14573. 'stroke-width': 1
  14574. })
  14575. .add();
  14576. this.chart.renderer.circle(rightContX + this.chart.plotLeft, rightContY + this.chart.plotTop, 2)
  14577. .attr({
  14578. stroke: 'green',
  14579. 'stroke-width': 1,
  14580. fill: 'none'
  14581. })
  14582. .add();
  14583. this.chart.renderer.path(['M', rightContX + this.chart.plotLeft, rightContY + this.chart.plotTop,
  14584. 'L', plotX + this.chart.plotLeft, plotY + this.chart.plotTop])
  14585. .attr({
  14586. stroke: 'green',
  14587. 'stroke-width': 1
  14588. })
  14589. .add();
  14590. }
  14591. // */
  14592. ret = [
  14593. 'C',
  14594. pick(lastPoint.rightContX, lastPoint.plotX),
  14595. pick(lastPoint.rightContY, lastPoint.plotY),
  14596. pick(leftContX, plotX),
  14597. pick(leftContY, plotY),
  14598. plotX,
  14599. plotY
  14600. ];
  14601. lastPoint.rightContX = lastPoint.rightContY = null; // reset for updating series later
  14602. return ret;
  14603. }
  14604. });
  14605. seriesTypes.spline = SplineSeries;
  14606. /**
  14607. * Set the default options for areaspline
  14608. */
  14609. defaultPlotOptions.areaspline = merge(defaultPlotOptions.area);
  14610. /**
  14611. * AreaSplineSeries object
  14612. */
  14613. var areaProto = AreaSeries.prototype,
  14614. AreaSplineSeries = extendClass(SplineSeries, {
  14615. type: 'areaspline',
  14616. getStackPoints: areaProto.getStackPoints,
  14617. getGraphPath: areaProto.getGraphPath,
  14618. setStackCliffs: areaProto.setStackCliffs,
  14619. drawGraph: areaProto.drawGraph,
  14620. drawLegendSymbol: LegendSymbolMixin.drawRectangle
  14621. });
  14622. seriesTypes.areaspline = AreaSplineSeries;
  14623. /**
  14624. * Set the default options for column
  14625. */
  14626. defaultPlotOptions.column = merge(defaultSeriesOptions, {
  14627. borderColor: '#FFFFFF',
  14628. //borderWidth: 1,
  14629. borderRadius: 0,
  14630. //colorByPoint: undefined,
  14631. groupPadding: 0.2,
  14632. //grouping: true,
  14633. marker: null, // point options are specified in the base options
  14634. pointPadding: 0.1,
  14635. //pointWidth: null,
  14636. minPointLength: 0,
  14637. cropThreshold: 50, // when there are more points, they will not animate out of the chart on xAxis.setExtremes
  14638. pointRange: null, // null means auto, meaning 1 in a categorized axis and least distance between points if not categories
  14639. states: {
  14640. hover: {
  14641. brightness: 0.1,
  14642. shadow: false,
  14643. halo: false
  14644. },
  14645. select: {
  14646. color: '#C0C0C0',
  14647. borderColor: '#000000',
  14648. shadow: false
  14649. }
  14650. },
  14651. dataLabels: {
  14652. align: null, // auto
  14653. verticalAlign: null, // auto
  14654. y: null
  14655. },
  14656. softThreshold: false,
  14657. startFromThreshold: true, // false doesn't work well: http://jsfiddle.net/highcharts/hz8fopan/14/
  14658. stickyTracking: false,
  14659. tooltip: {
  14660. distance: 6
  14661. },
  14662. threshold: 0
  14663. });
  14664. /**
  14665. * ColumnSeries object
  14666. */
  14667. var ColumnSeries = extendClass(Series, {
  14668. type: 'column',
  14669. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  14670. stroke: 'borderColor',
  14671. fill: 'color',
  14672. r: 'borderRadius'
  14673. },
  14674. cropShoulder: 0,
  14675. directTouch: true, // When tooltip is not shared, this series (and derivatives) requires direct touch/hover. KD-tree does not apply.
  14676. trackerGroups: ['group', 'dataLabelsGroup'],
  14677. negStacks: true, // use separate negative stacks, unlike area stacks where a negative
  14678. // point is substracted from previous (#1910)
  14679. /**
  14680. * Initialize the series
  14681. */
  14682. init: function () {
  14683. Series.prototype.init.apply(this, arguments);
  14684. var series = this,
  14685. chart = series.chart;
  14686. // if the series is added dynamically, force redraw of other
  14687. // series affected by a new column
  14688. if (chart.hasRendered) {
  14689. each(chart.series, function (otherSeries) {
  14690. if (otherSeries.type === series.type) {
  14691. otherSeries.isDirty = true;
  14692. }
  14693. });
  14694. }
  14695. },
  14696. /**
  14697. * Return the width and x offset of the columns adjusted for grouping, groupPadding, pointPadding,
  14698. * pointWidth etc.
  14699. */
  14700. getColumnMetrics: function () {
  14701. var series = this,
  14702. options = series.options,
  14703. xAxis = series.xAxis,
  14704. yAxis = series.yAxis,
  14705. reversedXAxis = xAxis.reversed,
  14706. stackKey,
  14707. stackGroups = {},
  14708. columnCount = 0;
  14709. // Get the total number of column type series.
  14710. // This is called on every series. Consider moving this logic to a
  14711. // chart.orderStacks() function and call it on init, addSeries and removeSeries
  14712. if (options.grouping === false) {
  14713. columnCount = 1;
  14714. } else {
  14715. each(series.chart.series, function (otherSeries) {
  14716. var otherOptions = otherSeries.options,
  14717. otherYAxis = otherSeries.yAxis,
  14718. columnIndex;
  14719. if (otherSeries.type === series.type && otherSeries.visible &&
  14720. yAxis.len === otherYAxis.len && yAxis.pos === otherYAxis.pos) { // #642, #2086
  14721. if (otherOptions.stacking) {
  14722. stackKey = otherSeries.stackKey;
  14723. if (stackGroups[stackKey] === UNDEFINED) {
  14724. stackGroups[stackKey] = columnCount++;
  14725. }
  14726. columnIndex = stackGroups[stackKey];
  14727. } else if (otherOptions.grouping !== false) { // #1162
  14728. columnIndex = columnCount++;
  14729. }
  14730. otherSeries.columnIndex = columnIndex;
  14731. }
  14732. });
  14733. }
  14734. var categoryWidth = mathMin(
  14735. mathAbs(xAxis.transA) * (xAxis.ordinalSlope || options.pointRange || xAxis.closestPointRange || xAxis.tickInterval || 1), // #2610
  14736. xAxis.len // #1535
  14737. ),
  14738. groupPadding = categoryWidth * options.groupPadding,
  14739. groupWidth = categoryWidth - 2 * groupPadding,
  14740. pointOffsetWidth = groupWidth / columnCount,
  14741. pointWidth = mathMin(
  14742. options.maxPointWidth || xAxis.len,
  14743. pick(options.pointWidth, pointOffsetWidth * (1 - 2 * options.pointPadding))
  14744. ),
  14745. pointPadding = (pointOffsetWidth - pointWidth) / 2,
  14746. colIndex = (series.columnIndex || 0) + (reversedXAxis ? 1 : 0), // #1251, #3737
  14747. pointXOffset = pointPadding + (groupPadding + colIndex *
  14748. pointOffsetWidth - (categoryWidth / 2)) *
  14749. (reversedXAxis ? -1 : 1);
  14750. // Save it for reading in linked series (Error bars particularly)
  14751. series.columnMetrics = {
  14752. width: pointWidth,
  14753. offset: pointXOffset
  14754. };
  14755. return series.columnMetrics;
  14756. },
  14757. /**
  14758. * Make the columns crisp. The edges are rounded to the nearest full pixel.
  14759. */
  14760. crispCol: function (x, y, w, h) {
  14761. var chart = this.chart,
  14762. borderWidth = this.borderWidth,
  14763. xCrisp = -(borderWidth % 2 ? 0.5 : 0),
  14764. yCrisp = borderWidth % 2 ? 0.5 : 1,
  14765. right,
  14766. bottom,
  14767. fromTop;
  14768. if (chart.inverted && chart.renderer.isVML) {
  14769. yCrisp += 1;
  14770. }
  14771. // Horizontal. We need to first compute the exact right edge, then round it
  14772. // and compute the width from there.
  14773. right = Math.round(x + w) + xCrisp;
  14774. x = Math.round(x) + xCrisp;
  14775. w = right - x;
  14776. // Vertical
  14777. bottom = Math.round(y + h) + yCrisp;
  14778. fromTop = mathAbs(y) <= 0.5 && bottom > 0.5; // #4504, #4656
  14779. y = Math.round(y) + yCrisp;
  14780. h = bottom - y;
  14781. // Top edges are exceptions
  14782. if (fromTop && h) { // #5146
  14783. y -= 1;
  14784. h += 1;
  14785. }
  14786. return {
  14787. x: x,
  14788. y: y,
  14789. width: w,
  14790. height: h
  14791. };
  14792. },
  14793. /**
  14794. * Translate each point to the plot area coordinate system and find shape positions
  14795. */
  14796. translate: function () {
  14797. var series = this,
  14798. chart = series.chart,
  14799. options = series.options,
  14800. borderWidth = series.borderWidth = pick(
  14801. options.borderWidth,
  14802. series.closestPointRange * series.xAxis.transA < 2 ? 0 : 1 // #3635
  14803. ),
  14804. yAxis = series.yAxis,
  14805. threshold = options.threshold,
  14806. translatedThreshold = series.translatedThreshold = yAxis.getThreshold(threshold),
  14807. minPointLength = pick(options.minPointLength, 5),
  14808. metrics = series.getColumnMetrics(),
  14809. pointWidth = metrics.width,
  14810. seriesBarW = series.barW = mathMax(pointWidth, 1 + 2 * borderWidth), // postprocessed for border width
  14811. pointXOffset = series.pointXOffset = metrics.offset;
  14812. if (chart.inverted) {
  14813. translatedThreshold -= 0.5; // #3355
  14814. }
  14815. // When the pointPadding is 0, we want the columns to be packed tightly, so we allow individual
  14816. // columns to have individual sizes. When pointPadding is greater, we strive for equal-width
  14817. // columns (#2694).
  14818. if (options.pointPadding) {
  14819. seriesBarW = mathCeil(seriesBarW);
  14820. }
  14821. Series.prototype.translate.apply(series);
  14822. // Record the new values
  14823. each(series.points, function (point) {
  14824. var yBottom = mathMin(pick(point.yBottom, translatedThreshold), 9e4), // #3575
  14825. safeDistance = 999 + mathAbs(yBottom),
  14826. plotY = mathMin(mathMax(-safeDistance, point.plotY), yAxis.len + safeDistance), // Don't draw too far outside plot area (#1303, #2241, #4264)
  14827. barX = point.plotX + pointXOffset,
  14828. barW = seriesBarW,
  14829. barY = mathMin(plotY, yBottom),
  14830. up,
  14831. barH = mathMax(plotY, yBottom) - barY;
  14832. // Handle options.minPointLength
  14833. if (mathAbs(barH) < minPointLength) {
  14834. if (minPointLength) {
  14835. barH = minPointLength;
  14836. up = (!yAxis.reversed && !point.negative) || (yAxis.reversed && point.negative);
  14837. barY = mathAbs(barY - translatedThreshold) > minPointLength ? // stacked
  14838. yBottom - minPointLength : // keep position
  14839. translatedThreshold - (up ? minPointLength : 0); // #1485, #4051
  14840. }
  14841. }
  14842. // Cache for access in polar
  14843. point.barX = barX;
  14844. point.pointWidth = pointWidth;
  14845. // Fix the tooltip on center of grouped columns (#1216, #424, #3648)
  14846. point.tooltipPos = chart.inverted ?
  14847. [yAxis.len + yAxis.pos - chart.plotLeft - plotY, series.xAxis.len - barX - barW / 2, barH] :
  14848. [barX + barW / 2, plotY + yAxis.pos - chart.plotTop, barH];
  14849. // Register shape type and arguments to be used in drawPoints
  14850. point.shapeType = 'rect';
  14851. point.shapeArgs = series.crispCol(barX, barY, barW, barH);
  14852. });
  14853. },
  14854. getSymbol: noop,
  14855. /**
  14856. * Use a solid rectangle like the area series types
  14857. */
  14858. drawLegendSymbol: LegendSymbolMixin.drawRectangle,
  14859. /**
  14860. * Columns have no graph
  14861. */
  14862. drawGraph: noop,
  14863. /**
  14864. * Draw the columns. For bars, the series.group is rotated, so the same coordinates
  14865. * apply for columns and bars. This method is inherited by scatter series.
  14866. *
  14867. */
  14868. drawPoints: function () {
  14869. var series = this,
  14870. chart = this.chart,
  14871. options = series.options,
  14872. renderer = chart.renderer,
  14873. animationLimit = options.animationLimit || 250,
  14874. shapeArgs,
  14875. pointAttr;
  14876. // draw the columns
  14877. each(series.points, function (point) {
  14878. var plotY = point.plotY,
  14879. graphic = point.graphic,
  14880. borderAttr;
  14881. if (isNumber(plotY) && point.y !== null) {
  14882. shapeArgs = point.shapeArgs;
  14883. borderAttr = defined(series.borderWidth) ? {
  14884. 'stroke-width': series.borderWidth
  14885. } : {};
  14886. pointAttr = point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE] || series.pointAttr[NORMAL_STATE];
  14887. if (graphic) { // update
  14888. stop(graphic);
  14889. graphic.attr(borderAttr).attr(pointAttr)[chart.pointCount < animationLimit ? 'animate' : 'attr'](merge(shapeArgs)); // #4267
  14890. } else {
  14891. point.graphic = graphic = renderer[point.shapeType](shapeArgs)
  14892. .attr(borderAttr)
  14893. .attr(pointAttr)
  14894. .add(point.group || series.group)
  14895. .shadow(options.shadow, null, options.stacking && !options.borderRadius);
  14896. }
  14897. } else if (graphic) {
  14898. point.graphic = graphic.destroy(); // #1269
  14899. }
  14900. });
  14901. },
  14902. /**
  14903. * Animate the column heights one by one from zero
  14904. * @param {Boolean} init Whether to initialize the animation or run it
  14905. */
  14906. animate: function (init) {
  14907. var series = this,
  14908. yAxis = this.yAxis,
  14909. options = series.options,
  14910. inverted = this.chart.inverted,
  14911. attr = {},
  14912. translatedThreshold;
  14913. if (hasSVG) { // VML is too slow anyway
  14914. if (init) {
  14915. attr.scaleY = 0.001;
  14916. translatedThreshold = mathMin(yAxis.pos + yAxis.len, mathMax(yAxis.pos, yAxis.toPixels(options.threshold)));
  14917. if (inverted) {
  14918. attr.translateX = translatedThreshold - yAxis.len;
  14919. } else {
  14920. attr.translateY = translatedThreshold;
  14921. }
  14922. series.group.attr(attr);
  14923. } else { // run the animation
  14924. attr[inverted ? 'translateX' : 'translateY'] = yAxis.pos;
  14925. series.group.animate(attr, extend(animObject(series.options.animation), {
  14926. // Do the scale synchronously to ensure smooth updating (#5030)
  14927. step: function (val, fx) {
  14928. series.group.attr({
  14929. scaleY: mathMax(0.001, fx.pos) // #5250
  14930. });
  14931. }
  14932. }));
  14933. // delete this function to allow it only once
  14934. series.animate = null;
  14935. }
  14936. }
  14937. },
  14938. /**
  14939. * Remove this series from the chart
  14940. */
  14941. remove: function () {
  14942. var series = this,
  14943. chart = series.chart;
  14944. // column and bar series affects other series of the same type
  14945. // as they are either stacked or grouped
  14946. if (chart.hasRendered) {
  14947. each(chart.series, function (otherSeries) {
  14948. if (otherSeries.type === series.type) {
  14949. otherSeries.isDirty = true;
  14950. }
  14951. });
  14952. }
  14953. Series.prototype.remove.apply(series, arguments);
  14954. }
  14955. });
  14956. seriesTypes.column = ColumnSeries;
  14957. /**
  14958. * Set the default options for bar
  14959. */
  14960. defaultPlotOptions.bar = merge(defaultPlotOptions.column);
  14961. /**
  14962. * The Bar series class
  14963. */
  14964. var BarSeries = extendClass(ColumnSeries, {
  14965. type: 'bar',
  14966. inverted: true
  14967. });
  14968. seriesTypes.bar = BarSeries;
  14969. /**
  14970. * Set the default options for scatter
  14971. */
  14972. defaultPlotOptions.scatter = merge(defaultSeriesOptions, {
  14973. lineWidth: 0,
  14974. marker: {
  14975. enabled: true // Overrides auto-enabling in line series (#3647)
  14976. },
  14977. tooltip: {
  14978. headerFormat: '<span style="color:{point.color}">\u25CF</span> <span style="font-size: 10px;"> {series.name}</span><br/>',
  14979. pointFormat: 'x: <b>{point.x}</b><br/>y: <b>{point.y}</b><br/>'
  14980. }
  14981. });
  14982. /**
  14983. * The scatter series class
  14984. */
  14985. var ScatterSeries = extendClass(Series, {
  14986. type: 'scatter',
  14987. sorted: false,
  14988. requireSorting: false,
  14989. noSharedTooltip: true,
  14990. trackerGroups: ['group', 'markerGroup', 'dataLabelsGroup'],
  14991. takeOrdinalPosition: false, // #2342
  14992. kdDimensions: 2,
  14993. drawGraph: function () {
  14994. if (this.options.lineWidth) {
  14995. Series.prototype.drawGraph.call(this);
  14996. }
  14997. }
  14998. });
  14999. seriesTypes.scatter = ScatterSeries;
  15000. /**
  15001. * Set the default options for pie
  15002. */
  15003. defaultPlotOptions.pie = merge(defaultSeriesOptions, {
  15004. borderColor: '#FFFFFF',
  15005. borderWidth: 1,
  15006. center: [null, null],
  15007. clip: false,
  15008. colorByPoint: true, // always true for pies
  15009. dataLabels: {
  15010. // align: null,
  15011. // connectorWidth: 1,
  15012. // connectorColor: point.color,
  15013. // connectorPadding: 5,
  15014. distance: 30,
  15015. enabled: true,
  15016. formatter: function () { // #2945
  15017. return this.y === null ? undefined : this.point.name;
  15018. },
  15019. // softConnector: true,
  15020. x: 0
  15021. // y: 0
  15022. },
  15023. ignoreHiddenPoint: true,
  15024. //innerSize: 0,
  15025. legendType: 'point',
  15026. marker: null, // point options are specified in the base options
  15027. size: null,
  15028. showInLegend: false,
  15029. slicedOffset: 10,
  15030. states: {
  15031. hover: {
  15032. brightness: 0.1,
  15033. shadow: false
  15034. }
  15035. },
  15036. stickyTracking: false,
  15037. tooltip: {
  15038. followPointer: true
  15039. }
  15040. });
  15041. /**
  15042. * Extended point object for pies
  15043. */
  15044. var PiePoint = extendClass(Point, {
  15045. /**
  15046. * Initiate the pie slice
  15047. */
  15048. init: function () {
  15049. Point.prototype.init.apply(this, arguments);
  15050. var point = this,
  15051. toggleSlice;
  15052. point.name = pick(point.name, 'Slice');
  15053. // add event listener for select
  15054. toggleSlice = function (e) {
  15055. point.slice(e.type === 'select');
  15056. };
  15057. addEvent(point, 'select', toggleSlice);
  15058. addEvent(point, 'unselect', toggleSlice);
  15059. return point;
  15060. },
  15061. /**
  15062. * Toggle the visibility of the pie slice
  15063. * @param {Boolean} vis Whether to show the slice or not. If undefined, the
  15064. * visibility is toggled
  15065. */
  15066. setVisible: function (vis, redraw) {
  15067. var point = this,
  15068. series = point.series,
  15069. chart = series.chart,
  15070. ignoreHiddenPoint = series.options.ignoreHiddenPoint;
  15071. redraw = pick(redraw, ignoreHiddenPoint);
  15072. if (vis !== point.visible) {
  15073. // If called without an argument, toggle visibility
  15074. point.visible = point.options.visible = vis = vis === UNDEFINED ? !point.visible : vis;
  15075. series.options.data[inArray(point, series.data)] = point.options; // update userOptions.data
  15076. // Show and hide associated elements. This is performed regardless of redraw or not,
  15077. // because chart.redraw only handles full series.
  15078. each(['graphic', 'dataLabel', 'connector', 'shadowGroup'], function (key) {
  15079. if (point[key]) {
  15080. point[key][vis ? 'show' : 'hide'](true);
  15081. }
  15082. });
  15083. if (point.legendItem) {
  15084. chart.legend.colorizeItem(point, vis);
  15085. }
  15086. // #4170, hide halo after hiding point
  15087. if (!vis && point.state === 'hover') {
  15088. point.setState('');
  15089. }
  15090. // Handle ignore hidden slices
  15091. if (ignoreHiddenPoint) {
  15092. series.isDirty = true;
  15093. }
  15094. if (redraw) {
  15095. chart.redraw();
  15096. }
  15097. }
  15098. },
  15099. /**
  15100. * Set or toggle whether the slice is cut out from the pie
  15101. * @param {Boolean} sliced When undefined, the slice state is toggled
  15102. * @param {Boolean} redraw Whether to redraw the chart. True by default.
  15103. */
  15104. slice: function (sliced, redraw, animation) {
  15105. var point = this,
  15106. series = point.series,
  15107. chart = series.chart,
  15108. translation;
  15109. setAnimation(animation, chart);
  15110. // redraw is true by default
  15111. redraw = pick(redraw, true);
  15112. // if called without an argument, toggle
  15113. point.sliced = point.options.sliced = sliced = defined(sliced) ? sliced : !point.sliced;
  15114. series.options.data[inArray(point, series.data)] = point.options; // update userOptions.data
  15115. translation = sliced ? point.slicedTranslation : {
  15116. translateX: 0,
  15117. translateY: 0
  15118. };
  15119. point.graphic.animate(translation);
  15120. if (point.shadowGroup) {
  15121. point.shadowGroup.animate(translation);
  15122. }
  15123. },
  15124. haloPath: function (size) {
  15125. var shapeArgs = this.shapeArgs,
  15126. chart = this.series.chart;
  15127. return this.sliced || !this.visible ? [] : this.series.chart.renderer.symbols.arc(chart.plotLeft + shapeArgs.x, chart.plotTop + shapeArgs.y, shapeArgs.r + size, shapeArgs.r + size, {
  15128. innerR: this.shapeArgs.r,
  15129. start: shapeArgs.start,
  15130. end: shapeArgs.end
  15131. });
  15132. }
  15133. });
  15134. /**
  15135. * The Pie series class
  15136. */
  15137. var PieSeries = {
  15138. type: 'pie',
  15139. isCartesian: false,
  15140. pointClass: PiePoint,
  15141. requireSorting: false,
  15142. directTouch: true,
  15143. noSharedTooltip: true,
  15144. trackerGroups: ['group', 'dataLabelsGroup'],
  15145. axisTypes: [],
  15146. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  15147. stroke: 'borderColor',
  15148. 'stroke-width': 'borderWidth',
  15149. fill: 'color'
  15150. },
  15151. /**
  15152. * Animate the pies in
  15153. */
  15154. animate: function (init) {
  15155. var series = this,
  15156. points = series.points,
  15157. startAngleRad = series.startAngleRad;
  15158. if (!init) {
  15159. each(points, function (point) {
  15160. var graphic = point.graphic,
  15161. args = point.shapeArgs;
  15162. if (graphic) {
  15163. // start values
  15164. graphic.attr({
  15165. r: point.startR || (series.center[3] / 2), // animate from inner radius (#779)
  15166. start: startAngleRad,
  15167. end: startAngleRad
  15168. });
  15169. // animate
  15170. graphic.animate({
  15171. r: args.r,
  15172. start: args.start,
  15173. end: args.end
  15174. }, series.options.animation);
  15175. }
  15176. });
  15177. // delete this function to allow it only once
  15178. series.animate = null;
  15179. }
  15180. },
  15181. /**
  15182. * Recompute total chart sum and update percentages of points.
  15183. */
  15184. updateTotals: function () {
  15185. var i,
  15186. total = 0,
  15187. points = this.points,
  15188. len = points.length,
  15189. point,
  15190. ignoreHiddenPoint = this.options.ignoreHiddenPoint;
  15191. // Get the total sum
  15192. for (i = 0; i < len; i++) {
  15193. point = points[i];
  15194. total += (ignoreHiddenPoint && !point.visible) ? 0 : point.y;
  15195. }
  15196. this.total = total;
  15197. // Set each point's properties
  15198. for (i = 0; i < len; i++) {
  15199. point = points[i];
  15200. point.percentage = (total > 0 && (point.visible || !ignoreHiddenPoint)) ? point.y / total * 100 : 0;
  15201. point.total = total;
  15202. }
  15203. },
  15204. /**
  15205. * Extend the generatePoints method by adding total and percentage properties to each point
  15206. */
  15207. generatePoints: function () {
  15208. Series.prototype.generatePoints.call(this);
  15209. this.updateTotals();
  15210. },
  15211. /**
  15212. * Do translation for pie slices
  15213. */
  15214. translate: function (positions) {
  15215. this.generatePoints();
  15216. var series = this,
  15217. cumulative = 0,
  15218. precision = 1000, // issue #172
  15219. options = series.options,
  15220. slicedOffset = options.slicedOffset,
  15221. connectorOffset = slicedOffset + options.borderWidth,
  15222. start,
  15223. end,
  15224. angle,
  15225. startAngle = options.startAngle || 0,
  15226. startAngleRad = series.startAngleRad = mathPI / 180 * (startAngle - 90),
  15227. endAngleRad = series.endAngleRad = mathPI / 180 * ((pick(options.endAngle, startAngle + 360)) - 90),
  15228. circ = endAngleRad - startAngleRad, //2 * mathPI,
  15229. points = series.points,
  15230. radiusX, // the x component of the radius vector for a given point
  15231. radiusY,
  15232. labelDistance = options.dataLabels.distance,
  15233. ignoreHiddenPoint = options.ignoreHiddenPoint,
  15234. i,
  15235. len = points.length,
  15236. point;
  15237. // Get positions - either an integer or a percentage string must be given.
  15238. // If positions are passed as a parameter, we're in a recursive loop for adjusting
  15239. // space for data labels.
  15240. if (!positions) {
  15241. series.center = positions = series.getCenter();
  15242. }
  15243. // utility for getting the x value from a given y, used for anticollision logic in data labels
  15244. series.getX = function (y, left) {
  15245. angle = math.asin(mathMin((y - positions[1]) / (positions[2] / 2 + labelDistance), 1));
  15246. return positions[0] +
  15247. (left ? -1 : 1) *
  15248. (mathCos(angle) * (positions[2] / 2 + labelDistance));
  15249. };
  15250. // Calculate the geometry for each point
  15251. for (i = 0; i < len; i++) {
  15252. point = points[i];
  15253. // set start and end angle
  15254. start = startAngleRad + (cumulative * circ);
  15255. if (!ignoreHiddenPoint || point.visible) {
  15256. cumulative += point.percentage / 100;
  15257. }
  15258. end = startAngleRad + (cumulative * circ);
  15259. // set the shape
  15260. point.shapeType = 'arc';
  15261. point.shapeArgs = {
  15262. x: positions[0],
  15263. y: positions[1],
  15264. r: positions[2] / 2,
  15265. innerR: positions[3] / 2,
  15266. start: mathRound(start * precision) / precision,
  15267. end: mathRound(end * precision) / precision
  15268. };
  15269. // The angle must stay within -90 and 270 (#2645)
  15270. angle = (end + start) / 2;
  15271. if (angle > 1.5 * mathPI) {
  15272. angle -= 2 * mathPI;
  15273. } else if (angle < -mathPI / 2) {
  15274. angle += 2 * mathPI;
  15275. }
  15276. // Center for the sliced out slice
  15277. point.slicedTranslation = {
  15278. translateX: mathRound(mathCos(angle) * slicedOffset),
  15279. translateY: mathRound(mathSin(angle) * slicedOffset)
  15280. };
  15281. // set the anchor point for tooltips
  15282. radiusX = mathCos(angle) * positions[2] / 2;
  15283. radiusY = mathSin(angle) * positions[2] / 2;
  15284. point.tooltipPos = [
  15285. positions[0] + radiusX * 0.7,
  15286. positions[1] + radiusY * 0.7
  15287. ];
  15288. point.half = angle < -mathPI / 2 || angle > mathPI / 2 ? 1 : 0;
  15289. point.angle = angle;
  15290. // set the anchor point for data labels
  15291. connectorOffset = mathMin(connectorOffset, labelDistance / 2); // #1678
  15292. point.labelPos = [
  15293. positions[0] + radiusX + mathCos(angle) * labelDistance, // first break of connector
  15294. positions[1] + radiusY + mathSin(angle) * labelDistance, // a/a
  15295. positions[0] + radiusX + mathCos(angle) * connectorOffset, // second break, right outside pie
  15296. positions[1] + radiusY + mathSin(angle) * connectorOffset, // a/a
  15297. positions[0] + radiusX, // landing point for connector
  15298. positions[1] + radiusY, // a/a
  15299. labelDistance < 0 ? // alignment
  15300. 'center' :
  15301. point.half ? 'right' : 'left', // alignment
  15302. angle // center angle
  15303. ];
  15304. }
  15305. },
  15306. drawGraph: null,
  15307. /**
  15308. * Draw the data points
  15309. */
  15310. drawPoints: function () {
  15311. var series = this,
  15312. chart = series.chart,
  15313. renderer = chart.renderer,
  15314. groupTranslation,
  15315. //center,
  15316. graphic,
  15317. //group,
  15318. shadow = series.options.shadow,
  15319. shadowGroup,
  15320. pointAttr,
  15321. shapeArgs,
  15322. attr;
  15323. if (shadow && !series.shadowGroup) {
  15324. series.shadowGroup = renderer.g('shadow')
  15325. .add(series.group);
  15326. }
  15327. // draw the slices
  15328. each(series.points, function (point) {
  15329. if (point.y !== null) {
  15330. graphic = point.graphic;
  15331. shapeArgs = point.shapeArgs;
  15332. shadowGroup = point.shadowGroup;
  15333. pointAttr = point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE];
  15334. if (!pointAttr.stroke) {
  15335. pointAttr.stroke = pointAttr.fill;
  15336. }
  15337. // put the shadow behind all points
  15338. if (shadow && !shadowGroup) {
  15339. shadowGroup = point.shadowGroup = renderer.g('shadow')
  15340. .add(series.shadowGroup);
  15341. }
  15342. // if the point is sliced, use special translation, else use plot area traslation
  15343. groupTranslation = point.sliced ? point.slicedTranslation : {
  15344. translateX: 0,
  15345. translateY: 0
  15346. };
  15347. //group.translate(groupTranslation[0], groupTranslation[1]);
  15348. if (shadowGroup) {
  15349. shadowGroup.attr(groupTranslation);
  15350. }
  15351. // draw the slice
  15352. if (graphic) {
  15353. graphic
  15354. .setRadialReference(series.center)
  15355. .attr(pointAttr)
  15356. .animate(extend(shapeArgs, groupTranslation));
  15357. } else {
  15358. attr = { 'stroke-linejoin': 'round' };
  15359. if (!point.visible) {
  15360. attr.visibility = 'hidden';
  15361. }
  15362. point.graphic = graphic = renderer[point.shapeType](shapeArgs)
  15363. .setRadialReference(series.center)
  15364. .attr(pointAttr)
  15365. .attr(attr)
  15366. .attr(groupTranslation)
  15367. .add(series.group)
  15368. .shadow(shadow, shadowGroup);
  15369. }
  15370. }
  15371. });
  15372. },
  15373. searchPoint: noop,
  15374. /**
  15375. * Utility for sorting data labels
  15376. */
  15377. sortByAngle: function (points, sign) {
  15378. points.sort(function (a, b) {
  15379. return a.angle !== undefined && (b.angle - a.angle) * sign;
  15380. });
  15381. },
  15382. /**
  15383. * Use a simple symbol from LegendSymbolMixin
  15384. */
  15385. drawLegendSymbol: LegendSymbolMixin.drawRectangle,
  15386. /**
  15387. * Use the getCenter method from drawLegendSymbol
  15388. */
  15389. getCenter: CenteredSeriesMixin.getCenter,
  15390. /**
  15391. * Pies don't have point marker symbols
  15392. */
  15393. getSymbol: noop
  15394. };
  15395. PieSeries = extendClass(Series, PieSeries);
  15396. seriesTypes.pie = PieSeries;
  15397. /**
  15398. * Draw the data labels
  15399. */
  15400. Series.prototype.drawDataLabels = function () {
  15401. var series = this,
  15402. seriesOptions = series.options,
  15403. cursor = seriesOptions.cursor,
  15404. options = seriesOptions.dataLabels,
  15405. points = series.points,
  15406. pointOptions,
  15407. generalOptions,
  15408. hasRendered = series.hasRendered || 0,
  15409. str,
  15410. dataLabelsGroup,
  15411. defer = pick(options.defer, true),
  15412. renderer = series.chart.renderer;
  15413. if (options.enabled || series._hasPointLabels) {
  15414. // Process default alignment of data labels for columns
  15415. if (series.dlProcessOptions) {
  15416. series.dlProcessOptions(options);
  15417. }
  15418. // Create a separate group for the data labels to avoid rotation
  15419. dataLabelsGroup = series.plotGroup(
  15420. 'dataLabelsGroup',
  15421. 'data-labels',
  15422. defer && !hasRendered ? 'hidden' : 'visible', // #5133
  15423. options.zIndex || 6
  15424. );
  15425. if (defer) {
  15426. dataLabelsGroup.attr({ opacity: +hasRendered }); // #3300
  15427. if (!hasRendered) {
  15428. addEvent(series, 'afterAnimate', function () {
  15429. if (series.visible) { // #3023, #3024
  15430. dataLabelsGroup.show();
  15431. }
  15432. dataLabelsGroup[seriesOptions.animation ? 'animate' : 'attr']({ opacity: 1 }, { duration: 200 });
  15433. });
  15434. }
  15435. }
  15436. // Make the labels for each point
  15437. generalOptions = options;
  15438. each(points, function (point) {
  15439. var enabled,
  15440. dataLabel = point.dataLabel,
  15441. labelConfig,
  15442. attr,
  15443. name,
  15444. rotation,
  15445. connector = point.connector,
  15446. isNew = true,
  15447. style,
  15448. moreStyle = {};
  15449. // Determine if each data label is enabled
  15450. pointOptions = point.dlOptions || (point.options && point.options.dataLabels); // dlOptions is used in treemaps
  15451. enabled = pick(pointOptions && pointOptions.enabled, generalOptions.enabled) && point.y !== null; // #2282, #4641
  15452. // If the point is outside the plot area, destroy it. #678, #820
  15453. if (dataLabel && !enabled) {
  15454. point.dataLabel = dataLabel.destroy();
  15455. // Individual labels are disabled if the are explicitly disabled
  15456. // in the point options, or if they fall outside the plot area.
  15457. } else if (enabled) {
  15458. // Create individual options structure that can be extended without
  15459. // affecting others
  15460. options = merge(generalOptions, pointOptions);
  15461. style = options.style;
  15462. rotation = options.rotation;
  15463. // Get the string
  15464. labelConfig = point.getLabelConfig();
  15465. str = options.format ?
  15466. format(options.format, labelConfig) :
  15467. options.formatter.call(labelConfig, options);
  15468. // Determine the color
  15469. style.color = pick(options.color, style.color, series.color, 'black');
  15470. // update existing label
  15471. if (dataLabel) {
  15472. if (defined(str)) {
  15473. dataLabel
  15474. .attr({
  15475. text: str
  15476. });
  15477. isNew = false;
  15478. } else { // #1437 - the label is shown conditionally
  15479. point.dataLabel = dataLabel = dataLabel.destroy();
  15480. if (connector) {
  15481. point.connector = connector.destroy();
  15482. }
  15483. }
  15484. // create new label
  15485. } else if (defined(str)) {
  15486. attr = {
  15487. //align: align,
  15488. fill: options.backgroundColor,
  15489. stroke: options.borderColor,
  15490. 'stroke-width': options.borderWidth,
  15491. r: options.borderRadius || 0,
  15492. rotation: rotation,
  15493. padding: options.padding,
  15494. zIndex: 1
  15495. };
  15496. // Get automated contrast color
  15497. if (style.color === 'contrast') {
  15498. moreStyle.color = options.inside || options.distance < 0 || !!seriesOptions.stacking ?
  15499. renderer.getContrast(point.color || series.color) :
  15500. '#000000';
  15501. }
  15502. if (cursor) {
  15503. moreStyle.cursor = cursor;
  15504. }
  15505. // Remove unused attributes (#947)
  15506. for (name in attr) {
  15507. if (attr[name] === UNDEFINED) {
  15508. delete attr[name];
  15509. }
  15510. }
  15511. dataLabel = point.dataLabel = renderer[rotation ? 'text' : 'label']( // labels don't support rotation
  15512. str,
  15513. 0,
  15514. -9999,
  15515. options.shape,
  15516. null,
  15517. null,
  15518. options.useHTML
  15519. )
  15520. .attr(attr)
  15521. .css(extend(style, moreStyle))
  15522. .add(dataLabelsGroup)
  15523. .shadow(options.shadow);
  15524. }
  15525. if (dataLabel) {
  15526. // Now the data label is created and placed at 0,0, so we need to align it
  15527. series.alignDataLabel(point, dataLabel, options, null, isNew);
  15528. }
  15529. }
  15530. });
  15531. }
  15532. };
  15533. /**
  15534. * Align each individual data label
  15535. */
  15536. Series.prototype.alignDataLabel = function (point, dataLabel, options, alignTo, isNew) {
  15537. var chart = this.chart,
  15538. inverted = chart.inverted,
  15539. plotX = pick(point.plotX, -9999),
  15540. plotY = pick(point.plotY, -9999),
  15541. bBox = dataLabel.getBBox(),
  15542. baseline = chart.renderer.fontMetrics(options.style.fontSize).b,
  15543. rotation = options.rotation,
  15544. normRotation,
  15545. negRotation,
  15546. align = options.align,
  15547. rotCorr, // rotation correction
  15548. // Math.round for rounding errors (#2683), alignTo to allow column labels (#2700)
  15549. visible = this.visible && (point.series.forceDL || chart.isInsidePlot(plotX, mathRound(plotY), inverted) ||
  15550. (alignTo && chart.isInsidePlot(plotX, inverted ? alignTo.x + 1 : alignTo.y + alignTo.height - 1, inverted))),
  15551. alignAttr, // the final position;
  15552. justify = pick(options.overflow, 'justify') === 'justify';
  15553. if (visible) {
  15554. // The alignment box is a singular point
  15555. alignTo = extend({
  15556. x: inverted ? chart.plotWidth - plotY : plotX,
  15557. y: mathRound(inverted ? chart.plotHeight - plotX : plotY),
  15558. width: 0,
  15559. height: 0
  15560. }, alignTo);
  15561. // Add the text size for alignment calculation
  15562. extend(options, {
  15563. width: bBox.width,
  15564. height: bBox.height
  15565. });
  15566. // Allow a hook for changing alignment in the last moment, then do the alignment
  15567. if (rotation) {
  15568. justify = false; // Not supported for rotated text
  15569. rotCorr = chart.renderer.rotCorr(baseline, rotation); // #3723
  15570. alignAttr = {
  15571. x: alignTo.x + options.x + alignTo.width / 2 + rotCorr.x,
  15572. y: alignTo.y + options.y + { top: 0, middle: 0.5, bottom: 1 }[options.verticalAlign] * alignTo.height
  15573. };
  15574. dataLabel[isNew ? 'attr' : 'animate'](alignAttr)
  15575. .attr({ // #3003
  15576. align: align
  15577. });
  15578. // Compensate for the rotated label sticking out on the sides
  15579. normRotation = (rotation + 720) % 360;
  15580. negRotation = normRotation > 180 && normRotation < 360;
  15581. if (align === 'left') {
  15582. alignAttr.y -= negRotation ? bBox.height : 0;
  15583. } else if (align === 'center') {
  15584. alignAttr.x -= bBox.width / 2;
  15585. alignAttr.y -= bBox.height / 2;
  15586. } else if (align === 'right') {
  15587. alignAttr.x -= bBox.width;
  15588. alignAttr.y -= negRotation ? 0 : bBox.height;
  15589. }
  15590. } else {
  15591. dataLabel.align(options, null, alignTo);
  15592. alignAttr = dataLabel.alignAttr;
  15593. }
  15594. // Handle justify or crop
  15595. if (justify) {
  15596. this.justifyDataLabel(dataLabel, options, alignAttr, bBox, alignTo, isNew);
  15597. // Now check that the data label is within the plot area
  15598. } else if (pick(options.crop, true)) {
  15599. visible = chart.isInsidePlot(alignAttr.x, alignAttr.y) && chart.isInsidePlot(alignAttr.x + bBox.width, alignAttr.y + bBox.height);
  15600. }
  15601. // When we're using a shape, make it possible with a connector or an arrow pointing to thie point
  15602. if (options.shape && !rotation) {
  15603. dataLabel.attr({
  15604. anchorX: point.plotX,
  15605. anchorY: point.plotY
  15606. });
  15607. }
  15608. }
  15609. // Show or hide based on the final aligned position
  15610. if (!visible) {
  15611. stop(dataLabel);
  15612. dataLabel.attr({ y: -9999 });
  15613. dataLabel.placed = false; // don't animate back in
  15614. }
  15615. };
  15616. /**
  15617. * If data labels fall partly outside the plot area, align them back in, in a way that
  15618. * doesn't hide the point.
  15619. */
  15620. Series.prototype.justifyDataLabel = function (dataLabel, options, alignAttr, bBox, alignTo, isNew) {
  15621. var chart = this.chart,
  15622. align = options.align,
  15623. verticalAlign = options.verticalAlign,
  15624. off,
  15625. justified,
  15626. padding = dataLabel.box ? 0 : (dataLabel.padding || 0);
  15627. // Off left
  15628. off = alignAttr.x + padding;
  15629. if (off < 0) {
  15630. if (align === 'right') {
  15631. options.align = 'left';
  15632. } else {
  15633. options.x = -off;
  15634. }
  15635. justified = true;
  15636. }
  15637. // Off right
  15638. off = alignAttr.x + bBox.width - padding;
  15639. if (off > chart.plotWidth) {
  15640. if (align === 'left') {
  15641. options.align = 'right';
  15642. } else {
  15643. options.x = chart.plotWidth - off;
  15644. }
  15645. justified = true;
  15646. }
  15647. // Off top
  15648. off = alignAttr.y + padding;
  15649. if (off < 0) {
  15650. if (verticalAlign === 'bottom') {
  15651. options.verticalAlign = 'top';
  15652. } else {
  15653. options.y = -off;
  15654. }
  15655. justified = true;
  15656. }
  15657. // Off bottom
  15658. off = alignAttr.y + bBox.height - padding;
  15659. if (off > chart.plotHeight) {
  15660. if (verticalAlign === 'top') {
  15661. options.verticalAlign = 'bottom';
  15662. } else {
  15663. options.y = chart.plotHeight - off;
  15664. }
  15665. justified = true;
  15666. }
  15667. if (justified) {
  15668. dataLabel.placed = !isNew;
  15669. dataLabel.align(options, null, alignTo);
  15670. }
  15671. };
  15672. /**
  15673. * Override the base drawDataLabels method by pie specific functionality
  15674. */
  15675. if (seriesTypes.pie) {
  15676. seriesTypes.pie.prototype.drawDataLabels = function () {
  15677. var series = this,
  15678. data = series.data,
  15679. point,
  15680. chart = series.chart,
  15681. options = series.options.dataLabels,
  15682. connectorPadding = pick(options.connectorPadding, 10),
  15683. connectorWidth = pick(options.connectorWidth, 1),
  15684. plotWidth = chart.plotWidth,
  15685. plotHeight = chart.plotHeight,
  15686. connector,
  15687. connectorPath,
  15688. softConnector = pick(options.softConnector, true),
  15689. distanceOption = options.distance,
  15690. seriesCenter = series.center,
  15691. radius = seriesCenter[2] / 2,
  15692. centerY = seriesCenter[1],
  15693. outside = distanceOption > 0,
  15694. dataLabel,
  15695. dataLabelWidth,
  15696. labelPos,
  15697. labelHeight,
  15698. halves = [// divide the points into right and left halves for anti collision
  15699. [], // right
  15700. [] // left
  15701. ],
  15702. x,
  15703. y,
  15704. visibility,
  15705. rankArr,
  15706. i,
  15707. j,
  15708. overflow = [0, 0, 0, 0], // top, right, bottom, left
  15709. sort = function (a, b) {
  15710. return b.y - a.y;
  15711. };
  15712. // get out if not enabled
  15713. if (!series.visible || (!options.enabled && !series._hasPointLabels)) {
  15714. return;
  15715. }
  15716. // run parent method
  15717. Series.prototype.drawDataLabels.apply(series);
  15718. each(data, function (point) {
  15719. if (point.dataLabel && point.visible) { // #407, #2510
  15720. // Arrange points for detection collision
  15721. halves[point.half].push(point);
  15722. // Reset positions (#4905)
  15723. point.dataLabel._pos = null;
  15724. }
  15725. });
  15726. /* Loop over the points in each half, starting from the top and bottom
  15727. * of the pie to detect overlapping labels.
  15728. */
  15729. i = 2;
  15730. while (i--) {
  15731. var slots = [],
  15732. slotsLength,
  15733. usedSlots = [],
  15734. points = halves[i],
  15735. pos,
  15736. bottom,
  15737. length = points.length,
  15738. slotIndex;
  15739. if (!length) {
  15740. continue;
  15741. }
  15742. // Sort by angle
  15743. series.sortByAngle(points, i - 0.5);
  15744. // Assume equal label heights on either hemisphere (#2630)
  15745. j = labelHeight = 0;
  15746. while (!labelHeight && points[j]) { // #1569
  15747. labelHeight = points[j] && points[j].dataLabel && (points[j].dataLabel.getBBox().height || 21); // 21 is for #968
  15748. j++;
  15749. }
  15750. // Only do anti-collision when we are outside the pie and have connectors (#856)
  15751. if (distanceOption > 0) {
  15752. // Build the slots
  15753. bottom = mathMin(centerY + radius + distanceOption, chart.plotHeight);
  15754. for (pos = mathMax(0, centerY - radius - distanceOption); pos <= bottom; pos += labelHeight) {
  15755. slots.push(pos);
  15756. }
  15757. slotsLength = slots.length;
  15758. /* Visualize the slots
  15759. if (!series.slotElements) {
  15760. series.slotElements = [];
  15761. }
  15762. if (i === 1) {
  15763. series.slotElements.forEach(function (elem) {
  15764. elem.destroy();
  15765. });
  15766. series.slotElements.length = 0;
  15767. }
  15768. slots.forEach(function (pos, no) {
  15769. var slotX = series.getX(pos, i) + chart.plotLeft - (i ? 100 : 0),
  15770. slotY = pos + chart.plotTop;
  15771. if (isNumber(slotX)) {
  15772. series.slotElements.push(chart.renderer.rect(slotX, slotY - 7, 100, labelHeight, 1)
  15773. .attr({
  15774. 'stroke-width': 1,
  15775. stroke: 'silver',
  15776. fill: 'rgba(0,0,255,0.1)'
  15777. })
  15778. .add());
  15779. series.slotElements.push(chart.renderer.text('Slot '+ no, slotX, slotY + 4)
  15780. .attr({
  15781. fill: 'silver'
  15782. }).add());
  15783. }
  15784. });
  15785. // */
  15786. // if there are more values than available slots, remove lowest values
  15787. if (length > slotsLength) {
  15788. // create an array for sorting and ranking the points within each quarter
  15789. rankArr = [].concat(points);
  15790. rankArr.sort(sort);
  15791. j = length;
  15792. while (j--) {
  15793. rankArr[j].rank = j;
  15794. }
  15795. j = length;
  15796. while (j--) {
  15797. if (points[j].rank >= slotsLength) {
  15798. points.splice(j, 1);
  15799. }
  15800. }
  15801. length = points.length;
  15802. }
  15803. // The label goes to the nearest open slot, but not closer to the edge than
  15804. // the label's index.
  15805. for (j = 0; j < length; j++) {
  15806. point = points[j];
  15807. labelPos = point.labelPos;
  15808. var closest = 9999,
  15809. distance,
  15810. slotI;
  15811. // find the closest slot index
  15812. for (slotI = 0; slotI < slotsLength; slotI++) {
  15813. distance = mathAbs(slots[slotI] - labelPos[1]);
  15814. if (distance < closest) {
  15815. closest = distance;
  15816. slotIndex = slotI;
  15817. }
  15818. }
  15819. // if that slot index is closer to the edges of the slots, move it
  15820. // to the closest appropriate slot
  15821. if (slotIndex < j && slots[j] !== null) { // cluster at the top
  15822. slotIndex = j;
  15823. } else if (slotsLength < length - j + slotIndex && slots[j] !== null) { // cluster at the bottom
  15824. slotIndex = slotsLength - length + j;
  15825. while (slots[slotIndex] === null) { // make sure it is not taken
  15826. slotIndex++;
  15827. }
  15828. } else {
  15829. // Slot is taken, find next free slot below. In the next run, the next slice will find the
  15830. // slot above these, because it is the closest one
  15831. while (slots[slotIndex] === null) { // make sure it is not taken
  15832. slotIndex++;
  15833. }
  15834. }
  15835. usedSlots.push({ i: slotIndex, y: slots[slotIndex] });
  15836. slots[slotIndex] = null; // mark as taken
  15837. }
  15838. // sort them in order to fill in from the top
  15839. usedSlots.sort(sort);
  15840. }
  15841. // now the used slots are sorted, fill them up sequentially
  15842. for (j = 0; j < length; j++) {
  15843. var slot, naturalY;
  15844. point = points[j];
  15845. labelPos = point.labelPos;
  15846. dataLabel = point.dataLabel;
  15847. visibility = point.visible === false ? HIDDEN : 'inherit';
  15848. naturalY = labelPos[1];
  15849. if (distanceOption > 0) {
  15850. slot = usedSlots.pop();
  15851. slotIndex = slot.i;
  15852. // if the slot next to currrent slot is free, the y value is allowed
  15853. // to fall back to the natural position
  15854. y = slot.y;
  15855. if ((naturalY > y && slots[slotIndex + 1] !== null) ||
  15856. (naturalY < y && slots[slotIndex - 1] !== null)) {
  15857. y = mathMin(mathMax(0, naturalY), chart.plotHeight);
  15858. }
  15859. } else {
  15860. y = naturalY;
  15861. }
  15862. // get the x - use the natural x position for first and last slot, to prevent the top
  15863. // and botton slice connectors from touching each other on either side
  15864. x = options.justify ?
  15865. seriesCenter[0] + (i ? -1 : 1) * (radius + distanceOption) :
  15866. series.getX(y === centerY - radius - distanceOption || y === centerY + radius + distanceOption ? naturalY : y, i);
  15867. // Record the placement and visibility
  15868. dataLabel._attr = {
  15869. visibility: visibility,
  15870. align: labelPos[6]
  15871. };
  15872. dataLabel._pos = {
  15873. x: x + options.x +
  15874. ({ left: connectorPadding, right: -connectorPadding }[labelPos[6]] || 0),
  15875. y: y + options.y - 10 // 10 is for the baseline (label vs text)
  15876. };
  15877. dataLabel.connX = x;
  15878. dataLabel.connY = y;
  15879. // Detect overflowing data labels
  15880. if (this.options.size === null) {
  15881. dataLabelWidth = dataLabel.width;
  15882. // Overflow left
  15883. if (x - dataLabelWidth < connectorPadding) {
  15884. overflow[3] = mathMax(mathRound(dataLabelWidth - x + connectorPadding), overflow[3]);
  15885. // Overflow right
  15886. } else if (x + dataLabelWidth > plotWidth - connectorPadding) {
  15887. overflow[1] = mathMax(mathRound(x + dataLabelWidth - plotWidth + connectorPadding), overflow[1]);
  15888. }
  15889. // Overflow top
  15890. if (y - labelHeight / 2 < 0) {
  15891. overflow[0] = mathMax(mathRound(-y + labelHeight / 2), overflow[0]);
  15892. // Overflow left
  15893. } else if (y + labelHeight / 2 > plotHeight) {
  15894. overflow[2] = mathMax(mathRound(y + labelHeight / 2 - plotHeight), overflow[2]);
  15895. }
  15896. }
  15897. } // for each point
  15898. } // for each half
  15899. // Do not apply the final placement and draw the connectors until we have verified
  15900. // that labels are not spilling over.
  15901. if (arrayMax(overflow) === 0 || this.verifyDataLabelOverflow(overflow)) {
  15902. // Place the labels in the final position
  15903. this.placeDataLabels();
  15904. // Draw the connectors
  15905. if (outside && connectorWidth) {
  15906. each(this.points, function (point) {
  15907. connector = point.connector;
  15908. labelPos = point.labelPos;
  15909. dataLabel = point.dataLabel;
  15910. if (dataLabel && dataLabel._pos && point.visible) {
  15911. visibility = dataLabel._attr.visibility;
  15912. x = dataLabel.connX;
  15913. y = dataLabel.connY;
  15914. connectorPath = softConnector ? [
  15915. M,
  15916. x + (labelPos[6] === 'left' ? 5 : -5), y, // end of the string at the label
  15917. 'C',
  15918. x, y, // first break, next to the label
  15919. 2 * labelPos[2] - labelPos[4], 2 * labelPos[3] - labelPos[5],
  15920. labelPos[2], labelPos[3], // second break
  15921. L,
  15922. labelPos[4], labelPos[5] // base
  15923. ] : [
  15924. M,
  15925. x + (labelPos[6] === 'left' ? 5 : -5), y, // end of the string at the label
  15926. L,
  15927. labelPos[2], labelPos[3], // second break
  15928. L,
  15929. labelPos[4], labelPos[5] // base
  15930. ];
  15931. if (connector) {
  15932. connector.animate({ d: connectorPath });
  15933. connector.attr('visibility', visibility);
  15934. } else {
  15935. point.connector = connector = series.chart.renderer.path(connectorPath).attr({
  15936. 'stroke-width': connectorWidth,
  15937. stroke: options.connectorColor || point.color || '#606060',
  15938. visibility: visibility
  15939. //zIndex: 0 // #2722 (reversed)
  15940. })
  15941. .add(series.dataLabelsGroup);
  15942. }
  15943. } else if (connector) {
  15944. point.connector = connector.destroy();
  15945. }
  15946. });
  15947. }
  15948. }
  15949. };
  15950. /**
  15951. * Perform the final placement of the data labels after we have verified that they
  15952. * fall within the plot area.
  15953. */
  15954. seriesTypes.pie.prototype.placeDataLabels = function () {
  15955. each(this.points, function (point) {
  15956. var dataLabel = point.dataLabel,
  15957. _pos;
  15958. if (dataLabel && point.visible) {
  15959. _pos = dataLabel._pos;
  15960. if (_pos) {
  15961. dataLabel.attr(dataLabel._attr);
  15962. dataLabel[dataLabel.moved ? 'animate' : 'attr'](_pos);
  15963. dataLabel.moved = true;
  15964. } else if (dataLabel) {
  15965. dataLabel.attr({ y: -9999 });
  15966. }
  15967. }
  15968. });
  15969. };
  15970. seriesTypes.pie.prototype.alignDataLabel = noop;
  15971. /**
  15972. * Verify whether the data labels are allowed to draw, or we should run more translation and data
  15973. * label positioning to keep them inside the plot area. Returns true when data labels are ready
  15974. * to draw.
  15975. */
  15976. seriesTypes.pie.prototype.verifyDataLabelOverflow = function (overflow) {
  15977. var center = this.center,
  15978. options = this.options,
  15979. centerOption = options.center,
  15980. minSize = options.minSize || 80,
  15981. newSize = minSize,
  15982. ret;
  15983. // Handle horizontal size and center
  15984. if (centerOption[0] !== null) { // Fixed center
  15985. newSize = mathMax(center[2] - mathMax(overflow[1], overflow[3]), minSize);
  15986. } else { // Auto center
  15987. newSize = mathMax(
  15988. center[2] - overflow[1] - overflow[3], // horizontal overflow
  15989. minSize
  15990. );
  15991. center[0] += (overflow[3] - overflow[1]) / 2; // horizontal center
  15992. }
  15993. // Handle vertical size and center
  15994. if (centerOption[1] !== null) { // Fixed center
  15995. newSize = mathMax(mathMin(newSize, center[2] - mathMax(overflow[0], overflow[2])), minSize);
  15996. } else { // Auto center
  15997. newSize = mathMax(
  15998. mathMin(
  15999. newSize,
  16000. center[2] - overflow[0] - overflow[2] // vertical overflow
  16001. ),
  16002. minSize
  16003. );
  16004. center[1] += (overflow[0] - overflow[2]) / 2; // vertical center
  16005. }
  16006. // If the size must be decreased, we need to run translate and drawDataLabels again
  16007. if (newSize < center[2]) {
  16008. center[2] = newSize;
  16009. center[3] = Math.min(relativeLength(options.innerSize || 0, newSize), newSize); // #3632
  16010. this.translate(center);
  16011. if (this.drawDataLabels) {
  16012. this.drawDataLabels();
  16013. }
  16014. // Else, return true to indicate that the pie and its labels is within the plot area
  16015. } else {
  16016. ret = true;
  16017. }
  16018. return ret;
  16019. };
  16020. }
  16021. if (seriesTypes.column) {
  16022. /**
  16023. * Override the basic data label alignment by adjusting for the position of the column
  16024. */
  16025. seriesTypes.column.prototype.alignDataLabel = function (point, dataLabel, options, alignTo, isNew) {
  16026. var inverted = this.chart.inverted,
  16027. series = point.series,
  16028. dlBox = point.dlBox || point.shapeArgs, // data label box for alignment
  16029. below = pick(point.below, point.plotY > pick(this.translatedThreshold, series.yAxis.len)), // point.below is used in range series
  16030. inside = pick(options.inside, !!this.options.stacking), // draw it inside the box?
  16031. overshoot;
  16032. // Align to the column itself, or the top of it
  16033. if (dlBox) { // Area range uses this method but not alignTo
  16034. alignTo = merge(dlBox);
  16035. if (alignTo.y < 0) {
  16036. alignTo.height += alignTo.y;
  16037. alignTo.y = 0;
  16038. }
  16039. overshoot = alignTo.y + alignTo.height - series.yAxis.len;
  16040. if (overshoot > 0) {
  16041. alignTo.height -= overshoot;
  16042. }
  16043. if (inverted) {
  16044. alignTo = {
  16045. x: series.yAxis.len - alignTo.y - alignTo.height,
  16046. y: series.xAxis.len - alignTo.x - alignTo.width,
  16047. width: alignTo.height,
  16048. height: alignTo.width
  16049. };
  16050. }
  16051. // Compute the alignment box
  16052. if (!inside) {
  16053. if (inverted) {
  16054. alignTo.x += below ? 0 : alignTo.width;
  16055. alignTo.width = 0;
  16056. } else {
  16057. alignTo.y += below ? alignTo.height : 0;
  16058. alignTo.height = 0;
  16059. }
  16060. }
  16061. }
  16062. // When alignment is undefined (typically columns and bars), display the individual
  16063. // point below or above the point depending on the threshold
  16064. options.align = pick(
  16065. options.align,
  16066. !inverted || inside ? 'center' : below ? 'right' : 'left'
  16067. );
  16068. options.verticalAlign = pick(
  16069. options.verticalAlign,
  16070. inverted || inside ? 'middle' : below ? 'top' : 'bottom'
  16071. );
  16072. // Call the parent method
  16073. Series.prototype.alignDataLabel.call(this, point, dataLabel, options, alignTo, isNew);
  16074. };
  16075. }
  16076. /**
  16077. * Highcharts module to hide overlapping data labels. This module is included in Highcharts.
  16078. */
  16079. (function (H) {
  16080. var Chart = H.Chart,
  16081. each = H.each,
  16082. pick = H.pick,
  16083. addEvent = H.addEvent;
  16084. // Collect potensial overlapping data labels. Stack labels probably don't need to be
  16085. // considered because they are usually accompanied by data labels that lie inside the columns.
  16086. Chart.prototype.callbacks.push(function (chart) {
  16087. function collectAndHide() {
  16088. var labels = [];
  16089. each(chart.series, function (series) {
  16090. var dlOptions = series.options.dataLabels,
  16091. collections = series.dataLabelCollections || ['dataLabel']; // Range series have two collections
  16092. if ((dlOptions.enabled || series._hasPointLabels) && !dlOptions.allowOverlap && series.visible) { // #3866
  16093. each(collections, function (coll) {
  16094. each(series.points, function (point) {
  16095. if (point[coll]) {
  16096. point[coll].labelrank = pick(point.labelrank, point.shapeArgs && point.shapeArgs.height); // #4118
  16097. labels.push(point[coll]);
  16098. }
  16099. });
  16100. });
  16101. }
  16102. });
  16103. chart.hideOverlappingLabels(labels);
  16104. }
  16105. // Do it now ...
  16106. collectAndHide();
  16107. // ... and after each chart redraw
  16108. addEvent(chart, 'redraw', collectAndHide);
  16109. });
  16110. /**
  16111. * Hide overlapping labels. Labels are moved and faded in and out on zoom to provide a smooth
  16112. * visual imression.
  16113. */
  16114. Chart.prototype.hideOverlappingLabels = function (labels) {
  16115. var len = labels.length,
  16116. label,
  16117. i,
  16118. j,
  16119. label1,
  16120. label2,
  16121. isIntersecting,
  16122. pos1,
  16123. pos2,
  16124. parent1,
  16125. parent2,
  16126. padding,
  16127. intersectRect = function (x1, y1, w1, h1, x2, y2, w2, h2) {
  16128. return !(
  16129. x2 > x1 + w1 ||
  16130. x2 + w2 < x1 ||
  16131. y2 > y1 + h1 ||
  16132. y2 + h2 < y1
  16133. );
  16134. };
  16135. // Mark with initial opacity
  16136. for (i = 0; i < len; i++) {
  16137. label = labels[i];
  16138. if (label) {
  16139. label.oldOpacity = label.opacity;
  16140. label.newOpacity = 1;
  16141. }
  16142. }
  16143. // Prevent a situation in a gradually rising slope, that each label
  16144. // will hide the previous one because the previous one always has
  16145. // lower rank.
  16146. labels.sort(function (a, b) {
  16147. return (b.labelrank || 0) - (a.labelrank || 0);
  16148. });
  16149. // Detect overlapping labels
  16150. for (i = 0; i < len; i++) {
  16151. label1 = labels[i];
  16152. for (j = i + 1; j < len; ++j) {
  16153. label2 = labels[j];
  16154. if (label1 && label2 && label1.placed && label2.placed && label1.newOpacity !== 0 && label2.newOpacity !== 0) {
  16155. pos1 = label1.alignAttr;
  16156. pos2 = label2.alignAttr;
  16157. parent1 = label1.parentGroup; // Different panes have different positions
  16158. parent2 = label2.parentGroup;
  16159. padding = 2 * (label1.box ? 0 : label1.padding); // Substract the padding if no background or border (#4333)
  16160. isIntersecting = intersectRect(
  16161. pos1.x + parent1.translateX,
  16162. pos1.y + parent1.translateY,
  16163. label1.width - padding,
  16164. label1.height - padding,
  16165. pos2.x + parent2.translateX,
  16166. pos2.y + parent2.translateY,
  16167. label2.width - padding,
  16168. label2.height - padding
  16169. );
  16170. if (isIntersecting) {
  16171. (label1.labelrank < label2.labelrank ? label1 : label2).newOpacity = 0;
  16172. }
  16173. }
  16174. }
  16175. }
  16176. // Hide or show
  16177. each(labels, function (label) {
  16178. var complete,
  16179. newOpacity;
  16180. if (label) {
  16181. newOpacity = label.newOpacity;
  16182. if (label.oldOpacity !== newOpacity && label.placed) {
  16183. // Make sure the label is completely hidden to avoid catching clicks (#4362)
  16184. if (newOpacity) {
  16185. label.show(true);
  16186. } else {
  16187. complete = function () {
  16188. label.hide();
  16189. };
  16190. }
  16191. // Animate or set the opacity
  16192. label.alignAttr.opacity = newOpacity;
  16193. label[label.isOld ? 'animate' : 'attr'](label.alignAttr, null, complete);
  16194. }
  16195. label.isOld = true;
  16196. }
  16197. });
  16198. };
  16199. }(Highcharts));
  16200. /**
  16201. * TrackerMixin for points and graphs
  16202. */
  16203. var TrackerMixin = Highcharts.TrackerMixin = {
  16204. drawTrackerPoint: function () {
  16205. var series = this,
  16206. chart = series.chart,
  16207. pointer = chart.pointer,
  16208. cursor = series.options.cursor,
  16209. css = cursor && { cursor: cursor },
  16210. onMouseOver = function (e) {
  16211. var target = e.target,
  16212. point;
  16213. while (target && !point) {
  16214. point = target.point;
  16215. target = target.parentNode;
  16216. }
  16217. if (point !== UNDEFINED && point !== chart.hoverPoint) { // undefined on graph in scatterchart
  16218. point.onMouseOver(e);
  16219. }
  16220. };
  16221. // Add reference to the point
  16222. each(series.points, function (point) {
  16223. if (point.graphic) {
  16224. point.graphic.element.point = point;
  16225. }
  16226. if (point.dataLabel) {
  16227. point.dataLabel.element.point = point;
  16228. }
  16229. });
  16230. // Add the event listeners, we need to do this only once
  16231. if (!series._hasTracking) {
  16232. each(series.trackerGroups, function (key) {
  16233. if (series[key]) { // we don't always have dataLabelsGroup
  16234. series[key]
  16235. .addClass(PREFIX + 'tracker')
  16236. .on('mouseover', onMouseOver)
  16237. .on('mouseout', function (e) {
  16238. pointer.onTrackerMouseOut(e);
  16239. })
  16240. .css(css);
  16241. if (hasTouch) {
  16242. series[key].on('touchstart', onMouseOver);
  16243. }
  16244. }
  16245. });
  16246. series._hasTracking = true;
  16247. }
  16248. },
  16249. /**
  16250. * Draw the tracker object that sits above all data labels and markers to
  16251. * track mouse events on the graph or points. For the line type charts
  16252. * the tracker uses the same graphPath, but with a greater stroke width
  16253. * for better control.
  16254. */
  16255. drawTrackerGraph: function () {
  16256. var series = this,
  16257. options = series.options,
  16258. trackByArea = options.trackByArea,
  16259. trackerPath = [].concat(trackByArea ? series.areaPath : series.graphPath),
  16260. trackerPathLength = trackerPath.length,
  16261. chart = series.chart,
  16262. pointer = chart.pointer,
  16263. renderer = chart.renderer,
  16264. snap = chart.options.tooltip.snap,
  16265. tracker = series.tracker,
  16266. cursor = options.cursor,
  16267. css = cursor && { cursor: cursor },
  16268. i,
  16269. onMouseOver = function () {
  16270. if (chart.hoverSeries !== series) {
  16271. series.onMouseOver();
  16272. }
  16273. },
  16274. /*
  16275. * Empirical lowest possible opacities for TRACKER_FILL for an element to stay invisible but clickable
  16276. * IE6: 0.002
  16277. * IE7: 0.002
  16278. * IE8: 0.002
  16279. * IE9: 0.00000000001 (unlimited)
  16280. * IE10: 0.0001 (exporting only)
  16281. * FF: 0.00000000001 (unlimited)
  16282. * Chrome: 0.000001
  16283. * Safari: 0.000001
  16284. * Opera: 0.00000000001 (unlimited)
  16285. */
  16286. TRACKER_FILL = 'rgba(192,192,192,' + (hasSVG ? 0.0001 : 0.002) + ')';
  16287. // Extend end points. A better way would be to use round linecaps,
  16288. // but those are not clickable in VML.
  16289. if (trackerPathLength && !trackByArea) {
  16290. i = trackerPathLength + 1;
  16291. while (i--) {
  16292. if (trackerPath[i] === M) { // extend left side
  16293. trackerPath.splice(i + 1, 0, trackerPath[i + 1] - snap, trackerPath[i + 2], L);
  16294. }
  16295. if ((i && trackerPath[i] === M) || i === trackerPathLength) { // extend right side
  16296. trackerPath.splice(i, 0, L, trackerPath[i - 2] + snap, trackerPath[i - 1]);
  16297. }
  16298. }
  16299. }
  16300. // handle single points
  16301. /*for (i = 0; i < singlePoints.length; i++) {
  16302. singlePoint = singlePoints[i];
  16303. trackerPath.push(M, singlePoint.plotX - snap, singlePoint.plotY,
  16304. L, singlePoint.plotX + snap, singlePoint.plotY);
  16305. }*/
  16306. // draw the tracker
  16307. if (tracker) {
  16308. tracker.attr({ d: trackerPath });
  16309. } else { // create
  16310. series.tracker = renderer.path(trackerPath)
  16311. .attr({
  16312. 'stroke-linejoin': 'round', // #1225
  16313. visibility: series.visible ? VISIBLE : HIDDEN,
  16314. stroke: TRACKER_FILL,
  16315. fill: trackByArea ? TRACKER_FILL : NONE,
  16316. 'stroke-width': options.lineWidth + (trackByArea ? 0 : 2 * snap),
  16317. zIndex: 2
  16318. })
  16319. .add(series.group);
  16320. // The tracker is added to the series group, which is clipped, but is covered
  16321. // by the marker group. So the marker group also needs to capture events.
  16322. each([series.tracker, series.markerGroup], function (tracker) {
  16323. tracker.addClass(PREFIX + 'tracker')
  16324. .on('mouseover', onMouseOver)
  16325. .on('mouseout', function (e) {
  16326. pointer.onTrackerMouseOut(e);
  16327. })
  16328. .css(css);
  16329. if (hasTouch) {
  16330. tracker.on('touchstart', onMouseOver);
  16331. }
  16332. });
  16333. }
  16334. }
  16335. };
  16336. /* End TrackerMixin */
  16337. /**
  16338. * Add tracking event listener to the series group, so the point graphics
  16339. * themselves act as trackers
  16340. */
  16341. if (seriesTypes.column) {
  16342. ColumnSeries.prototype.drawTracker = TrackerMixin.drawTrackerPoint;
  16343. }
  16344. if (seriesTypes.pie) {
  16345. seriesTypes.pie.prototype.drawTracker = TrackerMixin.drawTrackerPoint;
  16346. }
  16347. if (seriesTypes.scatter) {
  16348. ScatterSeries.prototype.drawTracker = TrackerMixin.drawTrackerPoint;
  16349. }
  16350. /*
  16351. * Extend Legend for item events
  16352. */
  16353. extend(Legend.prototype, {
  16354. setItemEvents: function (item, legendItem, useHTML, itemStyle, itemHiddenStyle) {
  16355. var legend = this;
  16356. // Set the events on the item group, or in case of useHTML, the item itself (#1249)
  16357. (useHTML ? legendItem : item.legendGroup).on('mouseover', function () {
  16358. item.setState(HOVER_STATE);
  16359. legendItem.css(legend.options.itemHoverStyle);
  16360. })
  16361. .on('mouseout', function () {
  16362. legendItem.css(item.visible ? itemStyle : itemHiddenStyle);
  16363. item.setState();
  16364. })
  16365. .on('click', function (event) {
  16366. var strLegendItemClick = 'legendItemClick',
  16367. fnLegendItemClick = function () {
  16368. if (item.setVisible) {
  16369. item.setVisible();
  16370. }
  16371. };
  16372. // Pass over the click/touch event. #4.
  16373. event = {
  16374. browserEvent: event
  16375. };
  16376. // click the name or symbol
  16377. if (item.firePointEvent) { // point
  16378. item.firePointEvent(strLegendItemClick, event, fnLegendItemClick);
  16379. } else {
  16380. fireEvent(item, strLegendItemClick, event, fnLegendItemClick);
  16381. }
  16382. });
  16383. },
  16384. createCheckboxForItem: function (item) {
  16385. var legend = this;
  16386. item.checkbox = createElement('input', {
  16387. type: 'checkbox',
  16388. checked: item.selected,
  16389. defaultChecked: item.selected // required by IE7
  16390. }, legend.options.itemCheckboxStyle, legend.chart.container);
  16391. addEvent(item.checkbox, 'click', function (event) {
  16392. var target = event.target;
  16393. fireEvent(
  16394. item.series || item,
  16395. 'checkboxClick',
  16396. { // #3712
  16397. checked: target.checked,
  16398. item: item
  16399. },
  16400. function () {
  16401. item.select();
  16402. }
  16403. );
  16404. });
  16405. }
  16406. });
  16407. /*
  16408. * Add pointer cursor to legend itemstyle in defaultOptions
  16409. */
  16410. defaultOptions.legend.itemStyle.cursor = 'pointer';
  16411. /*
  16412. * Extend the Chart object with interaction
  16413. */
  16414. extend(Chart.prototype, {
  16415. /**
  16416. * Display the zoom button
  16417. */
  16418. showResetZoom: function () {
  16419. var chart = this,
  16420. lang = defaultOptions.lang,
  16421. btnOptions = chart.options.chart.resetZoomButton,
  16422. theme = btnOptions.theme,
  16423. states = theme.states,
  16424. alignTo = btnOptions.relativeTo === 'chart' ? null : 'plotBox';
  16425. function zoomOut() {
  16426. chart.zoomOut();
  16427. }
  16428. this.resetZoomButton = chart.renderer.button(lang.resetZoom, null, null, zoomOut, theme, states && states.hover)
  16429. .attr({
  16430. align: btnOptions.position.align,
  16431. title: lang.resetZoomTitle
  16432. })
  16433. .add()
  16434. .align(btnOptions.position, false, alignTo);
  16435. },
  16436. /**
  16437. * Zoom out to 1:1
  16438. */
  16439. zoomOut: function () {
  16440. var chart = this;
  16441. fireEvent(chart, 'selection', { resetSelection: true }, function () {
  16442. chart.zoom();
  16443. });
  16444. },
  16445. /**
  16446. * Zoom into a given portion of the chart given by axis coordinates
  16447. * @param {Object} event
  16448. */
  16449. zoom: function (event) {
  16450. var chart = this,
  16451. hasZoomed,
  16452. pointer = chart.pointer,
  16453. displayButton = false,
  16454. resetZoomButton;
  16455. // If zoom is called with no arguments, reset the axes
  16456. if (!event || event.resetSelection) {
  16457. each(chart.axes, function (axis) {
  16458. hasZoomed = axis.zoom();
  16459. });
  16460. } else { // else, zoom in on all axes
  16461. each(event.xAxis.concat(event.yAxis), function (axisData) {
  16462. var axis = axisData.axis,
  16463. isXAxis = axis.isXAxis;
  16464. // don't zoom more than minRange
  16465. if (pointer[isXAxis ? 'zoomX' : 'zoomY'] || pointer[isXAxis ? 'pinchX' : 'pinchY']) {
  16466. hasZoomed = axis.zoom(axisData.min, axisData.max);
  16467. if (axis.displayBtn) {
  16468. displayButton = true;
  16469. }
  16470. }
  16471. });
  16472. }
  16473. // Show or hide the Reset zoom button
  16474. resetZoomButton = chart.resetZoomButton;
  16475. if (displayButton && !resetZoomButton) {
  16476. chart.showResetZoom();
  16477. } else if (!displayButton && isObject(resetZoomButton)) {
  16478. chart.resetZoomButton = resetZoomButton.destroy();
  16479. }
  16480. // Redraw
  16481. if (hasZoomed) {
  16482. chart.redraw(
  16483. pick(chart.options.chart.animation, event && event.animation, chart.pointCount < 100) // animation
  16484. );
  16485. }
  16486. },
  16487. /**
  16488. * Pan the chart by dragging the mouse across the pane. This function is called
  16489. * on mouse move, and the distance to pan is computed from chartX compared to
  16490. * the first chartX position in the dragging operation.
  16491. */
  16492. pan: function (e, panning) {
  16493. var chart = this,
  16494. hoverPoints = chart.hoverPoints,
  16495. doRedraw;
  16496. // remove active points for shared tooltip
  16497. if (hoverPoints) {
  16498. each(hoverPoints, function (point) {
  16499. point.setState();
  16500. });
  16501. }
  16502. each(panning === 'xy' ? [1, 0] : [1], function (isX) { // xy is used in maps
  16503. var axis = chart[isX ? 'xAxis' : 'yAxis'][0],
  16504. horiz = axis.horiz,
  16505. mousePos = e[horiz ? 'chartX' : 'chartY'],
  16506. mouseDown = horiz ? 'mouseDownX' : 'mouseDownY',
  16507. startPos = chart[mouseDown],
  16508. halfPointRange = (axis.pointRange || 0) / 2,
  16509. extremes = axis.getExtremes(),
  16510. newMin = axis.toValue(startPos - mousePos, true) + halfPointRange,
  16511. newMax = axis.toValue(startPos + axis.len - mousePos, true) - halfPointRange,
  16512. goingLeft = startPos > mousePos; // #3613
  16513. if (axis.series.length &&
  16514. (goingLeft || newMin > mathMin(extremes.dataMin, extremes.min)) &&
  16515. (!goingLeft || newMax < mathMax(extremes.dataMax, extremes.max))) {
  16516. axis.setExtremes(newMin, newMax, false, false, { trigger: 'pan' });
  16517. doRedraw = true;
  16518. }
  16519. chart[mouseDown] = mousePos; // set new reference for next run
  16520. });
  16521. if (doRedraw) {
  16522. chart.redraw(false);
  16523. }
  16524. css(chart.container, { cursor: 'move' });
  16525. }
  16526. });
  16527. /*
  16528. * Extend the Point object with interaction
  16529. */
  16530. extend(Point.prototype, {
  16531. /**
  16532. * Toggle the selection status of a point
  16533. * @param {Boolean} selected Whether to select or unselect the point.
  16534. * @param {Boolean} accumulate Whether to add to the previous selection. By default,
  16535. * this happens if the control key (Cmd on Mac) was pressed during clicking.
  16536. */
  16537. select: function (selected, accumulate) {
  16538. var point = this,
  16539. series = point.series,
  16540. chart = series.chart;
  16541. selected = pick(selected, !point.selected);
  16542. // fire the event with the default handler
  16543. point.firePointEvent(selected ? 'select' : 'unselect', { accumulate: accumulate }, function () {
  16544. point.selected = point.options.selected = selected;
  16545. series.options.data[inArray(point, series.data)] = point.options;
  16546. point.setState(selected && SELECT_STATE);
  16547. // unselect all other points unless Ctrl or Cmd + click
  16548. if (!accumulate) {
  16549. each(chart.getSelectedPoints(), function (loopPoint) {
  16550. if (loopPoint.selected && loopPoint !== point) {
  16551. loopPoint.selected = loopPoint.options.selected = false;
  16552. series.options.data[inArray(loopPoint, series.data)] = loopPoint.options;
  16553. loopPoint.setState(NORMAL_STATE);
  16554. loopPoint.firePointEvent('unselect');
  16555. }
  16556. });
  16557. }
  16558. });
  16559. },
  16560. /**
  16561. * Runs on mouse over the point
  16562. *
  16563. * @param {Object} e The event arguments
  16564. * @param {Boolean} byProximity Falsy for kd points that are closest to the mouse, or to
  16565. * actually hovered points. True for other points in shared tooltip.
  16566. */
  16567. onMouseOver: function (e, byProximity) {
  16568. var point = this,
  16569. series = point.series,
  16570. chart = series.chart,
  16571. tooltip = chart.tooltip,
  16572. hoverPoint = chart.hoverPoint;
  16573. if (chart.hoverSeries !== series) {
  16574. series.onMouseOver();
  16575. }
  16576. // set normal state to previous series
  16577. if (hoverPoint && hoverPoint !== point) {
  16578. hoverPoint.onMouseOut();
  16579. }
  16580. if (point.series) { // It may have been destroyed, #4130
  16581. // trigger the event
  16582. point.firePointEvent('mouseOver');
  16583. // update the tooltip
  16584. if (tooltip && (!tooltip.shared || series.noSharedTooltip)) {
  16585. tooltip.refresh(point, e);
  16586. }
  16587. // hover this
  16588. point.setState(HOVER_STATE);
  16589. if (!byProximity) {
  16590. chart.hoverPoint = point;
  16591. }
  16592. }
  16593. },
  16594. /**
  16595. * Runs on mouse out from the point
  16596. */
  16597. onMouseOut: function () {
  16598. var chart = this.series.chart,
  16599. hoverPoints = chart.hoverPoints;
  16600. this.firePointEvent('mouseOut');
  16601. if (!hoverPoints || inArray(this, hoverPoints) === -1) { // #887, #2240
  16602. this.setState();
  16603. chart.hoverPoint = null;
  16604. }
  16605. },
  16606. /**
  16607. * Import events from the series' and point's options. Only do it on
  16608. * demand, to save processing time on hovering.
  16609. */
  16610. importEvents: function () {
  16611. if (!this.hasImportedEvents) {
  16612. var point = this,
  16613. options = merge(point.series.options.point, point.options),
  16614. events = options.events,
  16615. eventType;
  16616. point.events = events;
  16617. for (eventType in events) {
  16618. addEvent(point, eventType, events[eventType]);
  16619. }
  16620. this.hasImportedEvents = true;
  16621. }
  16622. },
  16623. /**
  16624. * Set the point's state
  16625. * @param {String} state
  16626. */
  16627. setState: function (state, move) {
  16628. var point = this,
  16629. plotX = mathFloor(point.plotX), // #4586
  16630. plotY = point.plotY,
  16631. series = point.series,
  16632. stateOptions = series.options.states,
  16633. markerOptions = defaultPlotOptions[series.type].marker && series.options.marker,
  16634. normalDisabled = markerOptions && !markerOptions.enabled,
  16635. markerStateOptions = markerOptions && markerOptions.states[state],
  16636. stateDisabled = markerStateOptions && markerStateOptions.enabled === false,
  16637. stateMarkerGraphic = series.stateMarkerGraphic,
  16638. pointMarker = point.marker || {},
  16639. chart = series.chart,
  16640. radius,
  16641. halo = series.halo,
  16642. haloOptions,
  16643. newSymbol,
  16644. pointAttr;
  16645. state = state || NORMAL_STATE; // empty string
  16646. pointAttr = point.pointAttr[state] || series.pointAttr[state];
  16647. if (
  16648. // already has this state
  16649. (state === point.state && !move) ||
  16650. // selected points don't respond to hover
  16651. (point.selected && state !== SELECT_STATE) ||
  16652. // series' state options is disabled
  16653. (stateOptions[state] && stateOptions[state].enabled === false) ||
  16654. // general point marker's state options is disabled
  16655. (state && (stateDisabled || (normalDisabled && markerStateOptions.enabled === false))) ||
  16656. // individual point marker's state options is disabled
  16657. (state && pointMarker.states && pointMarker.states[state] && pointMarker.states[state].enabled === false) // #1610
  16658. ) {
  16659. return;
  16660. }
  16661. // apply hover styles to the existing point
  16662. if (point.graphic) {
  16663. radius = markerOptions && point.graphic.symbolName && pointAttr.r;
  16664. point.graphic.attr(merge(
  16665. pointAttr,
  16666. radius ? { // new symbol attributes (#507, #612)
  16667. x: plotX - radius,
  16668. y: plotY - radius,
  16669. width: 2 * radius,
  16670. height: 2 * radius
  16671. } : {}
  16672. ));
  16673. // Zooming in from a range with no markers to a range with markers
  16674. if (stateMarkerGraphic) {
  16675. stateMarkerGraphic.hide();
  16676. }
  16677. } else {
  16678. // if a graphic is not applied to each point in the normal state, create a shared
  16679. // graphic for the hover state
  16680. if (state && markerStateOptions) {
  16681. radius = markerStateOptions.radius;
  16682. newSymbol = pointMarker.symbol || series.symbol;
  16683. // If the point has another symbol than the previous one, throw away the
  16684. // state marker graphic and force a new one (#1459)
  16685. if (stateMarkerGraphic && stateMarkerGraphic.currentSymbol !== newSymbol) {
  16686. stateMarkerGraphic = stateMarkerGraphic.destroy();
  16687. }
  16688. // Add a new state marker graphic
  16689. if (!stateMarkerGraphic) {
  16690. if (newSymbol) {
  16691. series.stateMarkerGraphic = stateMarkerGraphic = chart.renderer.symbol(
  16692. newSymbol,
  16693. plotX - radius,
  16694. plotY - radius,
  16695. 2 * radius,
  16696. 2 * radius
  16697. )
  16698. .attr(pointAttr)
  16699. .add(series.markerGroup);
  16700. stateMarkerGraphic.currentSymbol = newSymbol;
  16701. }
  16702. // Move the existing graphic
  16703. } else {
  16704. stateMarkerGraphic[move ? 'animate' : 'attr']({ // #1054
  16705. x: plotX - radius,
  16706. y: plotY - radius
  16707. });
  16708. }
  16709. }
  16710. if (stateMarkerGraphic) {
  16711. stateMarkerGraphic[state && chart.isInsidePlot(plotX, plotY, chart.inverted) ? 'show' : 'hide'](); // #2450
  16712. stateMarkerGraphic.element.point = point; // #4310
  16713. }
  16714. }
  16715. // Show me your halo
  16716. haloOptions = stateOptions[state] && stateOptions[state].halo;
  16717. if (haloOptions && haloOptions.size) {
  16718. if (!halo) {
  16719. series.halo = halo = chart.renderer.path()
  16720. .add(chart.seriesGroup);
  16721. }
  16722. halo.attr(extend({
  16723. 'fill': point.color || series.color,
  16724. 'fill-opacity': haloOptions.opacity,
  16725. 'zIndex': -1 // #4929, IE8 added halo above everything
  16726. },
  16727. haloOptions.attributes))[move ? 'animate' : 'attr']({
  16728. d: point.haloPath(haloOptions.size)
  16729. });
  16730. } else if (halo) {
  16731. halo.attr({ d: [] });
  16732. }
  16733. point.state = state;
  16734. },
  16735. /**
  16736. * Get the circular path definition for the halo
  16737. * @param {Number} size The radius of the circular halo
  16738. * @returns {Array} The path definition
  16739. */
  16740. haloPath: function (size) {
  16741. var series = this.series,
  16742. chart = series.chart,
  16743. plotBox = series.getPlotBox(),
  16744. inverted = chart.inverted,
  16745. plotX = Math.floor(this.plotX);
  16746. return chart.renderer.symbols.circle(
  16747. plotBox.translateX + (inverted ? series.yAxis.len - this.plotY : plotX) - size,
  16748. plotBox.translateY + (inverted ? series.xAxis.len - plotX : this.plotY) - size,
  16749. size * 2,
  16750. size * 2
  16751. );
  16752. }
  16753. });
  16754. /*
  16755. * Extend the Series object with interaction
  16756. */
  16757. extend(Series.prototype, {
  16758. /**
  16759. * Series mouse over handler
  16760. */
  16761. onMouseOver: function () {
  16762. var series = this,
  16763. chart = series.chart,
  16764. hoverSeries = chart.hoverSeries;
  16765. // set normal state to previous series
  16766. if (hoverSeries && hoverSeries !== series) {
  16767. hoverSeries.onMouseOut();
  16768. }
  16769. // trigger the event, but to save processing time,
  16770. // only if defined
  16771. if (series.options.events.mouseOver) {
  16772. fireEvent(series, 'mouseOver');
  16773. }
  16774. // hover this
  16775. series.setState(HOVER_STATE);
  16776. chart.hoverSeries = series;
  16777. },
  16778. /**
  16779. * Series mouse out handler
  16780. */
  16781. onMouseOut: function () {
  16782. // trigger the event only if listeners exist
  16783. var series = this,
  16784. options = series.options,
  16785. chart = series.chart,
  16786. tooltip = chart.tooltip,
  16787. hoverPoint = chart.hoverPoint;
  16788. chart.hoverSeries = null; // #182, set to null before the mouseOut event fires
  16789. // trigger mouse out on the point, which must be in this series
  16790. if (hoverPoint) {
  16791. hoverPoint.onMouseOut();
  16792. }
  16793. // fire the mouse out event
  16794. if (series && options.events.mouseOut) {
  16795. fireEvent(series, 'mouseOut');
  16796. }
  16797. // hide the tooltip
  16798. if (tooltip && !options.stickyTracking && (!tooltip.shared || series.noSharedTooltip)) {
  16799. tooltip.hide();
  16800. }
  16801. // set normal state
  16802. series.setState();
  16803. },
  16804. /**
  16805. * Set the state of the graph
  16806. */
  16807. setState: function (state) {
  16808. var series = this,
  16809. options = series.options,
  16810. graph = series.graph,
  16811. stateOptions = options.states,
  16812. lineWidth = options.lineWidth,
  16813. attribs,
  16814. i = 0;
  16815. state = state || NORMAL_STATE;
  16816. if (series.state !== state) {
  16817. series.state = state;
  16818. if (stateOptions[state] && stateOptions[state].enabled === false) {
  16819. return;
  16820. }
  16821. if (state) {
  16822. lineWidth = stateOptions[state].lineWidth || lineWidth + (stateOptions[state].lineWidthPlus || 0); // #4035
  16823. }
  16824. if (graph && !graph.dashstyle) { // hover is turned off for dashed lines in VML
  16825. attribs = {
  16826. 'stroke-width': lineWidth
  16827. };
  16828. // use attr because animate will cause any other animation on the graph to stop
  16829. graph.attr(attribs);
  16830. while (series['zoneGraph' + i]) {
  16831. series['zoneGraph' + i].attr(attribs);
  16832. i = i + 1;
  16833. }
  16834. }
  16835. }
  16836. },
  16837. /**
  16838. * Set the visibility of the graph
  16839. *
  16840. * @param vis {Boolean} True to show the series, false to hide. If UNDEFINED,
  16841. * the visibility is toggled.
  16842. */
  16843. setVisible: function (vis, redraw) {
  16844. var series = this,
  16845. chart = series.chart,
  16846. legendItem = series.legendItem,
  16847. showOrHide,
  16848. ignoreHiddenSeries = chart.options.chart.ignoreHiddenSeries,
  16849. oldVisibility = series.visible;
  16850. // if called without an argument, toggle visibility
  16851. series.visible = vis = series.userOptions.visible = vis === UNDEFINED ? !oldVisibility : vis;
  16852. showOrHide = vis ? 'show' : 'hide';
  16853. // show or hide elements
  16854. each(['group', 'dataLabelsGroup', 'markerGroup', 'tracker'], function (key) {
  16855. if (series[key]) {
  16856. series[key][showOrHide]();
  16857. }
  16858. });
  16859. // hide tooltip (#1361)
  16860. if (chart.hoverSeries === series || (chart.hoverPoint && chart.hoverPoint.series) === series) {
  16861. series.onMouseOut();
  16862. }
  16863. if (legendItem) {
  16864. chart.legend.colorizeItem(series, vis);
  16865. }
  16866. // rescale or adapt to resized chart
  16867. series.isDirty = true;
  16868. // in a stack, all other series are affected
  16869. if (series.options.stacking) {
  16870. each(chart.series, function (otherSeries) {
  16871. if (otherSeries.options.stacking && otherSeries.visible) {
  16872. otherSeries.isDirty = true;
  16873. }
  16874. });
  16875. }
  16876. // show or hide linked series
  16877. each(series.linkedSeries, function (otherSeries) {
  16878. otherSeries.setVisible(vis, false);
  16879. });
  16880. if (ignoreHiddenSeries) {
  16881. chart.isDirtyBox = true;
  16882. }
  16883. if (redraw !== false) {
  16884. chart.redraw();
  16885. }
  16886. fireEvent(series, showOrHide);
  16887. },
  16888. /**
  16889. * Show the graph
  16890. */
  16891. show: function () {
  16892. this.setVisible(true);
  16893. },
  16894. /**
  16895. * Hide the graph
  16896. */
  16897. hide: function () {
  16898. this.setVisible(false);
  16899. },
  16900. /**
  16901. * Set the selected state of the graph
  16902. *
  16903. * @param selected {Boolean} True to select the series, false to unselect. If
  16904. * UNDEFINED, the selection state is toggled.
  16905. */
  16906. select: function (selected) {
  16907. var series = this;
  16908. // if called without an argument, toggle
  16909. series.selected = selected = (selected === UNDEFINED) ? !series.selected : selected;
  16910. if (series.checkbox) {
  16911. series.checkbox.checked = selected;
  16912. }
  16913. fireEvent(series, selected ? 'select' : 'unselect');
  16914. },
  16915. drawTracker: TrackerMixin.drawTrackerGraph
  16916. });
  16917. // global variables
  16918. extend(Highcharts, {
  16919. // Constructors
  16920. Color: Color,
  16921. Point: Point,
  16922. Tick: Tick,
  16923. Renderer: Renderer,
  16924. SVGElement: SVGElement,
  16925. SVGRenderer: SVGRenderer,
  16926. // Various
  16927. arrayMin: arrayMin,
  16928. arrayMax: arrayMax,
  16929. charts: charts,
  16930. correctFloat: correctFloat,
  16931. dateFormat: dateFormat,
  16932. error: error,
  16933. format: format,
  16934. pathAnim: pathAnim,
  16935. getOptions: getOptions,
  16936. hasBidiBug: hasBidiBug,
  16937. isTouchDevice: isTouchDevice,
  16938. setOptions: setOptions,
  16939. addEvent: addEvent,
  16940. removeEvent: removeEvent,
  16941. createElement: createElement,
  16942. discardElement: discardElement,
  16943. css: css,
  16944. each: each,
  16945. map: map,
  16946. merge: merge,
  16947. splat: splat,
  16948. stableSort: stableSort,
  16949. extendClass: extendClass,
  16950. pInt: pInt,
  16951. svg: hasSVG,
  16952. canvas: useCanVG,
  16953. vml: !hasSVG && !useCanVG,
  16954. product: PRODUCT,
  16955. version: VERSION
  16956. });
  16957. return Highcharts;
  16958. }));