highcharts.src.js 459 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696
  1. // ==ClosureCompiler==
  2. // @compilation_level SIMPLE_OPTIMIZATIONS
  3. /**
  4. * @license Highcharts JS v4.0.3 (2014-07-03)
  5. *
  6. * (c) 2009-2014 Torstein Honsi
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. // JSLint options:
  11. /*global Highcharts, document, window, navigator, setInterval, clearInterval, clearTimeout, setTimeout, location, jQuery, $, console, each, grep */
  12. /*jslint ass: true, sloppy: true, forin: true, plusplus: true, nomen: true, vars: true, regexp: true, newcap: true, browser: true, continue: true, white: true */
  13. (function () {
  14. // encapsulated variables
  15. var UNDEFINED,
  16. doc = document,
  17. win = window,
  18. math = Math,
  19. mathRound = math.round,
  20. mathFloor = math.floor,
  21. mathCeil = math.ceil,
  22. mathMax = math.max,
  23. mathMin = math.min,
  24. mathAbs = math.abs,
  25. mathCos = math.cos,
  26. mathSin = math.sin,
  27. mathPI = math.PI,
  28. deg2rad = mathPI * 2 / 360,
  29. // some variables
  30. userAgent = navigator.userAgent,
  31. isOpera = win.opera,
  32. isIE = /msie/i.test(userAgent) && !isOpera,
  33. docMode8 = doc.documentMode === 8,
  34. isWebKit = /AppleWebKit/.test(userAgent),
  35. isFirefox = /Firefox/.test(userAgent),
  36. isTouchDevice = /(Mobile|Android|Windows Phone)/.test(userAgent),
  37. SVG_NS = 'http://www.w3.org/2000/svg',
  38. hasSVG = !!doc.createElementNS && !!doc.createElementNS(SVG_NS, 'svg').createSVGRect,
  39. hasBidiBug = isFirefox && parseInt(userAgent.split('Firefox/')[1], 10) < 4, // issue #38
  40. useCanVG = !hasSVG && !isIE && !!doc.createElement('canvas').getContext,
  41. Renderer,
  42. hasTouch,
  43. symbolSizes = {},
  44. idCounter = 0,
  45. garbageBin,
  46. defaultOptions,
  47. dateFormat, // function
  48. globalAnimation,
  49. pathAnim,
  50. timeUnits,
  51. error,
  52. noop = function () { return UNDEFINED; },
  53. charts = [],
  54. chartCount = 0,
  55. PRODUCT = 'Highcharts',
  56. VERSION = '4.0.3',
  57. // some constants for frequently used strings
  58. DIV = 'div',
  59. ABSOLUTE = 'absolute',
  60. RELATIVE = 'relative',
  61. HIDDEN = 'hidden',
  62. PREFIX = 'highcharts-',
  63. VISIBLE = 'visible',
  64. PX = 'px',
  65. NONE = 'none',
  66. M = 'M',
  67. L = 'L',
  68. numRegex = /^[0-9]+$/,
  69. NORMAL_STATE = '',
  70. HOVER_STATE = 'hover',
  71. SELECT_STATE = 'select',
  72. // Object for extending Axis
  73. AxisPlotLineOrBandExtension,
  74. // constants for attributes
  75. STROKE_WIDTH = 'stroke-width',
  76. // time methods, changed based on whether or not UTC is used
  77. makeTime,
  78. timezoneOffset,
  79. getMinutes,
  80. getHours,
  81. getDay,
  82. getDate,
  83. getMonth,
  84. getFullYear,
  85. setMinutes,
  86. setHours,
  87. setDate,
  88. setMonth,
  89. setFullYear,
  90. // lookup over the types and the associated classes
  91. seriesTypes = {},
  92. Highcharts;
  93. // The Highcharts namespace
  94. if (win.Highcharts) {
  95. error(16, true);
  96. } else {
  97. Highcharts = win.Highcharts = {};
  98. }
  99. /**
  100. * Extend an object with the members of another
  101. * @param {Object} a The object to be extended
  102. * @param {Object} b The object to add to the first one
  103. */
  104. function extend(a, b) {
  105. var n;
  106. if (!a) {
  107. a = {};
  108. }
  109. for (n in b) {
  110. a[n] = b[n];
  111. }
  112. return a;
  113. }
  114. /**
  115. * Deep merge two or more objects and return a third object. If the first argument is
  116. * true, the contents of the second object is copied into the first object.
  117. * Previously this function redirected to jQuery.extend(true), but this had two limitations.
  118. * First, it deep merged arrays, which lead to workarounds in Highcharts. Second,
  119. * it copied properties from extended prototypes.
  120. */
  121. function merge() {
  122. var i,
  123. args = arguments,
  124. len,
  125. ret = {},
  126. doCopy = function (copy, original) {
  127. var value, key;
  128. // An object is replacing a primitive
  129. if (typeof copy !== 'object') {
  130. copy = {};
  131. }
  132. for (key in original) {
  133. if (original.hasOwnProperty(key)) {
  134. value = original[key];
  135. // Copy the contents of objects, but not arrays or DOM nodes
  136. if (value && typeof value === 'object' && Object.prototype.toString.call(value) !== '[object Array]'
  137. && key !== 'renderTo' && typeof value.nodeType !== 'number') {
  138. copy[key] = doCopy(copy[key] || {}, value);
  139. // Primitives and arrays are copied over directly
  140. } else {
  141. copy[key] = original[key];
  142. }
  143. }
  144. }
  145. return copy;
  146. };
  147. // If first argument is true, copy into the existing object. Used in setOptions.
  148. if (args[0] === true) {
  149. ret = args[1];
  150. args = Array.prototype.slice.call(args, 2);
  151. }
  152. // For each argument, extend the return
  153. len = args.length;
  154. for (i = 0; i < len; i++) {
  155. ret = doCopy(ret, args[i]);
  156. }
  157. return ret;
  158. }
  159. /**
  160. * Shortcut for parseInt
  161. * @param {Object} s
  162. * @param {Number} mag Magnitude
  163. */
  164. function pInt(s, mag) {
  165. return parseInt(s, mag || 10);
  166. }
  167. /**
  168. * Check for string
  169. * @param {Object} s
  170. */
  171. function isString(s) {
  172. return typeof s === 'string';
  173. }
  174. /**
  175. * Check for object
  176. * @param {Object} obj
  177. */
  178. function isObject(obj) {
  179. return obj && typeof obj === 'object';
  180. }
  181. /**
  182. * Check for array
  183. * @param {Object} obj
  184. */
  185. function isArray(obj) {
  186. return Object.prototype.toString.call(obj) === '[object Array]';
  187. }
  188. /**
  189. * Check for number
  190. * @param {Object} n
  191. */
  192. function isNumber(n) {
  193. return typeof n === 'number';
  194. }
  195. function log2lin(num) {
  196. return math.log(num) / math.LN10;
  197. }
  198. function lin2log(num) {
  199. return math.pow(10, num);
  200. }
  201. /**
  202. * Remove last occurence of an item from an array
  203. * @param {Array} arr
  204. * @param {Mixed} item
  205. */
  206. function erase(arr, item) {
  207. var i = arr.length;
  208. while (i--) {
  209. if (arr[i] === item) {
  210. arr.splice(i, 1);
  211. break;
  212. }
  213. }
  214. //return arr;
  215. }
  216. /**
  217. * Returns true if the object is not null or undefined. Like MooTools' $.defined.
  218. * @param {Object} obj
  219. */
  220. function defined(obj) {
  221. return obj !== UNDEFINED && obj !== null;
  222. }
  223. /**
  224. * Set or get an attribute or an object of attributes. Can't use jQuery attr because
  225. * it attempts to set expando properties on the SVG element, which is not allowed.
  226. *
  227. * @param {Object} elem The DOM element to receive the attribute(s)
  228. * @param {String|Object} prop The property or an abject of key-value pairs
  229. * @param {String} value The value if a single property is set
  230. */
  231. function attr(elem, prop, value) {
  232. var key,
  233. ret;
  234. // if the prop is a string
  235. if (isString(prop)) {
  236. // set the value
  237. if (defined(value)) {
  238. elem.setAttribute(prop, value);
  239. // get the value
  240. } else if (elem && elem.getAttribute) { // elem not defined when printing pie demo...
  241. ret = elem.getAttribute(prop);
  242. }
  243. // else if prop is defined, it is a hash of key/value pairs
  244. } else if (defined(prop) && isObject(prop)) {
  245. for (key in prop) {
  246. elem.setAttribute(key, prop[key]);
  247. }
  248. }
  249. return ret;
  250. }
  251. /**
  252. * Check if an element is an array, and if not, make it into an array. Like
  253. * MooTools' $.splat.
  254. */
  255. function splat(obj) {
  256. return isArray(obj) ? obj : [obj];
  257. }
  258. /**
  259. * Return the first value that is defined. Like MooTools' $.pick.
  260. */
  261. function pick() {
  262. var args = arguments,
  263. i,
  264. arg,
  265. length = args.length;
  266. for (i = 0; i < length; i++) {
  267. arg = args[i];
  268. if (arg !== UNDEFINED && arg !== null) {
  269. return arg;
  270. }
  271. }
  272. }
  273. /**
  274. * Set CSS on a given element
  275. * @param {Object} el
  276. * @param {Object} styles Style object with camel case property names
  277. */
  278. function css(el, styles) {
  279. if (isIE && !hasSVG) { // #2686
  280. if (styles && styles.opacity !== UNDEFINED) {
  281. styles.filter = 'alpha(opacity=' + (styles.opacity * 100) + ')';
  282. }
  283. }
  284. extend(el.style, styles);
  285. }
  286. /**
  287. * Utility function to create element with attributes and styles
  288. * @param {Object} tag
  289. * @param {Object} attribs
  290. * @param {Object} styles
  291. * @param {Object} parent
  292. * @param {Object} nopad
  293. */
  294. function createElement(tag, attribs, styles, parent, nopad) {
  295. var el = doc.createElement(tag);
  296. if (attribs) {
  297. extend(el, attribs);
  298. }
  299. if (nopad) {
  300. css(el, {padding: 0, border: NONE, margin: 0});
  301. }
  302. if (styles) {
  303. css(el, styles);
  304. }
  305. if (parent) {
  306. parent.appendChild(el);
  307. }
  308. return el;
  309. }
  310. /**
  311. * Extend a prototyped class by new members
  312. * @param {Object} parent
  313. * @param {Object} members
  314. */
  315. function extendClass(parent, members) {
  316. var object = function () { return UNDEFINED; };
  317. object.prototype = new parent();
  318. extend(object.prototype, members);
  319. return object;
  320. }
  321. /**
  322. * Format a number and return a string based on input settings
  323. * @param {Number} number The input number to format
  324. * @param {Number} decimals The amount of decimals
  325. * @param {String} decPoint The decimal point, defaults to the one given in the lang options
  326. * @param {String} thousandsSep The thousands separator, defaults to the one given in the lang options
  327. */
  328. function numberFormat(number, decimals, decPoint, thousandsSep) {
  329. var lang = defaultOptions.lang,
  330. // http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_number_format/
  331. n = +number || 0,
  332. c = decimals === -1 ?
  333. (n.toString().split('.')[1] || '').length : // preserve decimals
  334. (isNaN(decimals = mathAbs(decimals)) ? 2 : decimals),
  335. d = decPoint === undefined ? lang.decimalPoint : decPoint,
  336. t = thousandsSep === undefined ? lang.thousandsSep : thousandsSep,
  337. s = n < 0 ? "-" : "",
  338. i = String(pInt(n = mathAbs(n).toFixed(c))),
  339. j = i.length > 3 ? i.length % 3 : 0;
  340. return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) +
  341. (c ? d + mathAbs(n - i).toFixed(c).slice(2) : "");
  342. }
  343. /**
  344. * Pad a string to a given length by adding 0 to the beginning
  345. * @param {Number} number
  346. * @param {Number} length
  347. */
  348. function pad(number, length) {
  349. // Create an array of the remaining length +1 and join it with 0's
  350. return new Array((length || 2) + 1 - String(number).length).join(0) + number;
  351. }
  352. /**
  353. * Wrap a method with extended functionality, preserving the original function
  354. * @param {Object} obj The context object that the method belongs to
  355. * @param {String} method The name of the method to extend
  356. * @param {Function} func A wrapper function callback. This function is called with the same arguments
  357. * as the original function, except that the original function is unshifted and passed as the first
  358. * argument.
  359. */
  360. function wrap(obj, method, func) {
  361. var proceed = obj[method];
  362. obj[method] = function () {
  363. var args = Array.prototype.slice.call(arguments);
  364. args.unshift(proceed);
  365. return func.apply(this, args);
  366. };
  367. }
  368. /**
  369. * Based on http://www.php.net/manual/en/function.strftime.php
  370. * @param {String} format
  371. * @param {Number} timestamp
  372. * @param {Boolean} capitalize
  373. */
  374. dateFormat = function (format, timestamp, capitalize) {
  375. if (!defined(timestamp) || isNaN(timestamp)) {
  376. return 'Invalid date';
  377. }
  378. format = pick(format, '%Y-%m-%d %H:%M:%S');
  379. var date = new Date(timestamp - timezoneOffset),
  380. key, // used in for constuct below
  381. // get the basic time values
  382. hours = date[getHours](),
  383. day = date[getDay](),
  384. dayOfMonth = date[getDate](),
  385. month = date[getMonth](),
  386. fullYear = date[getFullYear](),
  387. lang = defaultOptions.lang,
  388. langWeekdays = lang.weekdays,
  389. // List all format keys. Custom formats can be added from the outside.
  390. replacements = extend({
  391. // Day
  392. 'a': langWeekdays[day].substr(0, 3), // Short weekday, like 'Mon'
  393. 'A': langWeekdays[day], // Long weekday, like 'Monday'
  394. 'd': pad(dayOfMonth), // Two digit day of the month, 01 to 31
  395. 'e': dayOfMonth, // Day of the month, 1 through 31
  396. // Week (none implemented)
  397. //'W': weekNumber(),
  398. // Month
  399. 'b': lang.shortMonths[month], // Short month, like 'Jan'
  400. 'B': lang.months[month], // Long month, like 'January'
  401. 'm': pad(month + 1), // Two digit month number, 01 through 12
  402. // Year
  403. 'y': fullYear.toString().substr(2, 2), // Two digits year, like 09 for 2009
  404. 'Y': fullYear, // Four digits year, like 2009
  405. // Time
  406. 'H': pad(hours), // Two digits hours in 24h format, 00 through 23
  407. 'I': pad((hours % 12) || 12), // Two digits hours in 12h format, 00 through 11
  408. 'l': (hours % 12) || 12, // Hours in 12h format, 1 through 12
  409. 'M': pad(date[getMinutes]()), // Two digits minutes, 00 through 59
  410. 'p': hours < 12 ? 'AM' : 'PM', // Upper case AM or PM
  411. 'P': hours < 12 ? 'am' : 'pm', // Lower case AM or PM
  412. 'S': pad(date.getSeconds()), // Two digits seconds, 00 through 59
  413. 'L': pad(mathRound(timestamp % 1000), 3) // Milliseconds (naming from Ruby)
  414. }, Highcharts.dateFormats);
  415. // do the replaces
  416. for (key in replacements) {
  417. while (format.indexOf('%' + key) !== -1) { // regex would do it in one line, but this is faster
  418. format = format.replace('%' + key, typeof replacements[key] === 'function' ? replacements[key](timestamp) : replacements[key]);
  419. }
  420. }
  421. // Optionally capitalize the string and return
  422. return capitalize ? format.substr(0, 1).toUpperCase() + format.substr(1) : format;
  423. };
  424. /**
  425. * Format a single variable. Similar to sprintf, without the % prefix.
  426. */
  427. function formatSingle(format, val) {
  428. var floatRegex = /f$/,
  429. decRegex = /\.([0-9])/,
  430. lang = defaultOptions.lang,
  431. decimals;
  432. if (floatRegex.test(format)) { // float
  433. decimals = format.match(decRegex);
  434. decimals = decimals ? decimals[1] : -1;
  435. if (val !== null) {
  436. val = numberFormat(
  437. val,
  438. decimals,
  439. lang.decimalPoint,
  440. format.indexOf(',') > -1 ? lang.thousandsSep : ''
  441. );
  442. }
  443. } else {
  444. val = dateFormat(format, val);
  445. }
  446. return val;
  447. }
  448. /**
  449. * Format a string according to a subset of the rules of Python's String.format method.
  450. */
  451. function format(str, ctx) {
  452. var splitter = '{',
  453. isInside = false,
  454. segment,
  455. valueAndFormat,
  456. path,
  457. i,
  458. len,
  459. ret = [],
  460. val,
  461. index;
  462. while ((index = str.indexOf(splitter)) !== -1) {
  463. segment = str.slice(0, index);
  464. if (isInside) { // we're on the closing bracket looking back
  465. valueAndFormat = segment.split(':');
  466. path = valueAndFormat.shift().split('.'); // get first and leave format
  467. len = path.length;
  468. val = ctx;
  469. // Assign deeper paths
  470. for (i = 0; i < len; i++) {
  471. val = val[path[i]];
  472. }
  473. // Format the replacement
  474. if (valueAndFormat.length) {
  475. val = formatSingle(valueAndFormat.join(':'), val);
  476. }
  477. // Push the result and advance the cursor
  478. ret.push(val);
  479. } else {
  480. ret.push(segment);
  481. }
  482. str = str.slice(index + 1); // the rest
  483. isInside = !isInside; // toggle
  484. splitter = isInside ? '}' : '{'; // now look for next matching bracket
  485. }
  486. ret.push(str);
  487. return ret.join('');
  488. }
  489. /**
  490. * Get the magnitude of a number
  491. */
  492. function getMagnitude(num) {
  493. return math.pow(10, mathFloor(math.log(num) / math.LN10));
  494. }
  495. /**
  496. * Take an interval and normalize it to multiples of 1, 2, 2.5 and 5
  497. * @param {Number} interval
  498. * @param {Array} multiples
  499. * @param {Number} magnitude
  500. * @param {Object} options
  501. */
  502. function normalizeTickInterval(interval, multiples, magnitude, options) {
  503. var normalized, i;
  504. // round to a tenfold of 1, 2, 2.5 or 5
  505. magnitude = pick(magnitude, 1);
  506. normalized = interval / magnitude;
  507. // multiples for a linear scale
  508. if (!multiples) {
  509. multiples = [1, 2, 2.5, 5, 10];
  510. // the allowDecimals option
  511. if (options && options.allowDecimals === false) {
  512. if (magnitude === 1) {
  513. multiples = [1, 2, 5, 10];
  514. } else if (magnitude <= 0.1) {
  515. multiples = [1 / magnitude];
  516. }
  517. }
  518. }
  519. // normalize the interval to the nearest multiple
  520. for (i = 0; i < multiples.length; i++) {
  521. interval = multiples[i];
  522. if (normalized <= (multiples[i] + (multiples[i + 1] || multiples[i])) / 2) {
  523. break;
  524. }
  525. }
  526. // multiply back to the correct magnitude
  527. interval *= magnitude;
  528. return interval;
  529. }
  530. /**
  531. * Utility method that sorts an object array and keeping the order of equal items.
  532. * ECMA script standard does not specify the behaviour when items are equal.
  533. */
  534. function stableSort(arr, sortFunction) {
  535. var length = arr.length,
  536. sortValue,
  537. i;
  538. // Add index to each item
  539. for (i = 0; i < length; i++) {
  540. arr[i].ss_i = i; // stable sort index
  541. }
  542. arr.sort(function (a, b) {
  543. sortValue = sortFunction(a, b);
  544. return sortValue === 0 ? a.ss_i - b.ss_i : sortValue;
  545. });
  546. // Remove index from items
  547. for (i = 0; i < length; i++) {
  548. delete arr[i].ss_i; // stable sort index
  549. }
  550. }
  551. /**
  552. * Non-recursive method to find the lowest member of an array. Math.min raises a maximum
  553. * call stack size exceeded error in Chrome when trying to apply more than 150.000 points. This
  554. * method is slightly slower, but safe.
  555. */
  556. function arrayMin(data) {
  557. var i = data.length,
  558. min = data[0];
  559. while (i--) {
  560. if (data[i] < min) {
  561. min = data[i];
  562. }
  563. }
  564. return min;
  565. }
  566. /**
  567. * Non-recursive method to find the lowest member of an array. Math.min raises a maximum
  568. * call stack size exceeded error in Chrome when trying to apply more than 150.000 points. This
  569. * method is slightly slower, but safe.
  570. */
  571. function arrayMax(data) {
  572. var i = data.length,
  573. max = data[0];
  574. while (i--) {
  575. if (data[i] > max) {
  576. max = data[i];
  577. }
  578. }
  579. return max;
  580. }
  581. /**
  582. * Utility method that destroys any SVGElement or VMLElement that are properties on the given object.
  583. * It loops all properties and invokes destroy if there is a destroy method. The property is
  584. * then delete'ed.
  585. * @param {Object} The object to destroy properties on
  586. * @param {Object} Exception, do not destroy this property, only delete it.
  587. */
  588. function destroyObjectProperties(obj, except) {
  589. var n;
  590. for (n in obj) {
  591. // If the object is non-null and destroy is defined
  592. if (obj[n] && obj[n] !== except && obj[n].destroy) {
  593. // Invoke the destroy
  594. obj[n].destroy();
  595. }
  596. // Delete the property from the object.
  597. delete obj[n];
  598. }
  599. }
  600. /**
  601. * Discard an element by moving it to the bin and delete
  602. * @param {Object} The HTML node to discard
  603. */
  604. function discardElement(element) {
  605. // create a garbage bin element, not part of the DOM
  606. if (!garbageBin) {
  607. garbageBin = createElement(DIV);
  608. }
  609. // move the node and empty bin
  610. if (element) {
  611. garbageBin.appendChild(element);
  612. }
  613. garbageBin.innerHTML = '';
  614. }
  615. /**
  616. * Provide error messages for debugging, with links to online explanation
  617. */
  618. error = function (code, stop) {
  619. var msg = 'Highcharts error #' + code + ': www.highcharts.com/errors/' + code;
  620. if (stop) {
  621. throw msg;
  622. }
  623. // else ...
  624. if (win.console) {
  625. console.log(msg);
  626. }
  627. };
  628. /**
  629. * Fix JS round off float errors
  630. * @param {Number} num
  631. */
  632. function correctFloat(num) {
  633. return parseFloat(
  634. num.toPrecision(14)
  635. );
  636. }
  637. /**
  638. * Set the global animation to either a given value, or fall back to the
  639. * given chart's animation option
  640. * @param {Object} animation
  641. * @param {Object} chart
  642. */
  643. function setAnimation(animation, chart) {
  644. globalAnimation = pick(animation, chart.animation);
  645. }
  646. /**
  647. * The time unit lookup
  648. */
  649. timeUnits = {
  650. millisecond: 1,
  651. second: 1000,
  652. minute: 60000,
  653. hour: 3600000,
  654. day: 24 * 3600000,
  655. week: 7 * 24 * 3600000,
  656. month: 31 * 24 * 3600000,
  657. year: 31556952000
  658. };
  659. /**
  660. * Path interpolation algorithm used across adapters
  661. */
  662. pathAnim = {
  663. /**
  664. * Prepare start and end values so that the path can be animated one to one
  665. */
  666. init: function (elem, fromD, toD) {
  667. fromD = fromD || '';
  668. var shift = elem.shift,
  669. bezier = fromD.indexOf('C') > -1,
  670. numParams = bezier ? 7 : 3,
  671. endLength,
  672. slice,
  673. i,
  674. start = fromD.split(' '),
  675. end = [].concat(toD), // copy
  676. startBaseLine,
  677. endBaseLine,
  678. sixify = function (arr) { // in splines make move points have six parameters like bezier curves
  679. i = arr.length;
  680. while (i--) {
  681. if (arr[i] === M) {
  682. arr.splice(i + 1, 0, arr[i + 1], arr[i + 2], arr[i + 1], arr[i + 2]);
  683. }
  684. }
  685. };
  686. if (bezier) {
  687. sixify(start);
  688. sixify(end);
  689. }
  690. // pull out the base lines before padding
  691. if (elem.isArea) {
  692. startBaseLine = start.splice(start.length - 6, 6);
  693. endBaseLine = end.splice(end.length - 6, 6);
  694. }
  695. // if shifting points, prepend a dummy point to the end path
  696. if (shift <= end.length / numParams && start.length === end.length) {
  697. while (shift--) {
  698. end = [].concat(end).splice(0, numParams).concat(end);
  699. }
  700. }
  701. elem.shift = 0; // reset for following animations
  702. // copy and append last point until the length matches the end length
  703. if (start.length) {
  704. endLength = end.length;
  705. while (start.length < endLength) {
  706. //bezier && sixify(start);
  707. slice = [].concat(start).splice(start.length - numParams, numParams);
  708. if (bezier) { // disable first control point
  709. slice[numParams - 6] = slice[numParams - 2];
  710. slice[numParams - 5] = slice[numParams - 1];
  711. }
  712. start = start.concat(slice);
  713. }
  714. }
  715. if (startBaseLine) { // append the base lines for areas
  716. start = start.concat(startBaseLine);
  717. end = end.concat(endBaseLine);
  718. }
  719. return [start, end];
  720. },
  721. /**
  722. * Interpolate each value of the path and return the array
  723. */
  724. step: function (start, end, pos, complete) {
  725. var ret = [],
  726. i = start.length,
  727. startVal;
  728. if (pos === 1) { // land on the final path without adjustment points appended in the ends
  729. ret = complete;
  730. } else if (i === end.length && pos < 1) {
  731. while (i--) {
  732. startVal = parseFloat(start[i]);
  733. ret[i] =
  734. isNaN(startVal) ? // a letter instruction like M or L
  735. start[i] :
  736. pos * (parseFloat(end[i] - startVal)) + startVal;
  737. }
  738. } else { // if animation is finished or length not matching, land on right value
  739. ret = end;
  740. }
  741. return ret;
  742. }
  743. };
  744. (function ($) {
  745. /**
  746. * The default HighchartsAdapter for jQuery
  747. */
  748. win.HighchartsAdapter = win.HighchartsAdapter || ($ && {
  749. /**
  750. * Initialize the adapter by applying some extensions to jQuery
  751. */
  752. init: function (pathAnim) {
  753. // extend the animate function to allow SVG animations
  754. var Fx = $.fx,
  755. Step = Fx.step,
  756. dSetter,
  757. Tween = $.Tween,
  758. propHooks = Tween && Tween.propHooks,
  759. opacityHook = $.cssHooks.opacity;
  760. /*jslint unparam: true*//* allow unused param x in this function */
  761. $.extend($.easing, {
  762. easeOutQuad: function (x, t, b, c, d) {
  763. return -c * (t /= d) * (t - 2) + b;
  764. }
  765. });
  766. /*jslint unparam: false*/
  767. // extend some methods to check for elem.attr, which means it is a Highcharts SVG object
  768. $.each(['cur', '_default', 'width', 'height', 'opacity'], function (i, fn) {
  769. var obj = Step,
  770. base;
  771. // Handle different parent objects
  772. if (fn === 'cur') {
  773. obj = Fx.prototype; // 'cur', the getter, relates to Fx.prototype
  774. } else if (fn === '_default' && Tween) { // jQuery 1.8 model
  775. obj = propHooks[fn];
  776. fn = 'set';
  777. }
  778. // Overwrite the method
  779. base = obj[fn];
  780. if (base) { // step.width and step.height don't exist in jQuery < 1.7
  781. // create the extended function replacement
  782. obj[fn] = function (fx) {
  783. var elem;
  784. // Fx.prototype.cur does not use fx argument
  785. fx = i ? fx : this;
  786. // Don't run animations on textual properties like align (#1821)
  787. if (fx.prop === 'align') {
  788. return;
  789. }
  790. // shortcut
  791. elem = fx.elem;
  792. // Fx.prototype.cur returns the current value. The other ones are setters
  793. // and returning a value has no effect.
  794. return elem.attr ? // is SVG element wrapper
  795. elem.attr(fx.prop, fn === 'cur' ? UNDEFINED : fx.now) : // apply the SVG wrapper's method
  796. base.apply(this, arguments); // use jQuery's built-in method
  797. };
  798. }
  799. });
  800. // Extend the opacity getter, needed for fading opacity with IE9 and jQuery 1.10+
  801. wrap(opacityHook, 'get', function (proceed, elem, computed) {
  802. return elem.attr ? (elem.opacity || 0) : proceed.call(this, elem, computed);
  803. });
  804. // Define the setter function for d (path definitions)
  805. dSetter = function (fx) {
  806. var elem = fx.elem,
  807. ends;
  808. // Normally start and end should be set in state == 0, but sometimes,
  809. // for reasons unknown, this doesn't happen. Perhaps state == 0 is skipped
  810. // in these cases
  811. if (!fx.started) {
  812. ends = pathAnim.init(elem, elem.d, elem.toD);
  813. fx.start = ends[0];
  814. fx.end = ends[1];
  815. fx.started = true;
  816. }
  817. // interpolate each value of the path
  818. elem.attr('d', pathAnim.step(fx.start, fx.end, fx.pos, elem.toD));
  819. };
  820. // jQuery 1.8 style
  821. if (Tween) {
  822. propHooks.d = {
  823. set: dSetter
  824. };
  825. // pre 1.8
  826. } else {
  827. // animate paths
  828. Step.d = dSetter;
  829. }
  830. /**
  831. * Utility for iterating over an array. Parameters are reversed compared to jQuery.
  832. * @param {Array} arr
  833. * @param {Function} fn
  834. */
  835. this.each = Array.prototype.forEach ?
  836. function (arr, fn) { // modern browsers
  837. return Array.prototype.forEach.call(arr, fn);
  838. } :
  839. function (arr, fn) { // legacy
  840. var i,
  841. len = arr.length;
  842. for (i = 0; i < len; i++) {
  843. if (fn.call(arr[i], arr[i], i, arr) === false) {
  844. return i;
  845. }
  846. }
  847. };
  848. /**
  849. * Register Highcharts as a plugin in the respective framework
  850. */
  851. $.fn.highcharts = function () {
  852. var constr = 'Chart', // default constructor
  853. args = arguments,
  854. options,
  855. ret,
  856. chart;
  857. if (this[0]) {
  858. if (isString(args[0])) {
  859. constr = args[0];
  860. args = Array.prototype.slice.call(args, 1);
  861. }
  862. options = args[0];
  863. // Create the chart
  864. if (options !== UNDEFINED) {
  865. /*jslint unused:false*/
  866. options.chart = options.chart || {};
  867. options.chart.renderTo = this[0];
  868. chart = new Highcharts[constr](options, args[1]);
  869. ret = this;
  870. /*jslint unused:true*/
  871. }
  872. // When called without parameters or with the return argument, get a predefined chart
  873. if (options === UNDEFINED) {
  874. ret = charts[attr(this[0], 'data-highcharts-chart')];
  875. }
  876. }
  877. return ret;
  878. };
  879. },
  880. /**
  881. * Downloads a script and executes a callback when done.
  882. * @param {String} scriptLocation
  883. * @param {Function} callback
  884. */
  885. getScript: $.getScript,
  886. /**
  887. * Return the index of an item in an array, or -1 if not found
  888. */
  889. inArray: $.inArray,
  890. /**
  891. * A direct link to jQuery methods. MooTools and Prototype adapters must be implemented for each case of method.
  892. * @param {Object} elem The HTML element
  893. * @param {String} method Which method to run on the wrapped element
  894. */
  895. adapterRun: function (elem, method) {
  896. return $(elem)[method]();
  897. },
  898. /**
  899. * Filter an array
  900. */
  901. grep: $.grep,
  902. /**
  903. * Map an array
  904. * @param {Array} arr
  905. * @param {Function} fn
  906. */
  907. map: function (arr, fn) {
  908. //return jQuery.map(arr, fn);
  909. var results = [],
  910. i = 0,
  911. len = arr.length;
  912. for (; i < len; i++) {
  913. results[i] = fn.call(arr[i], arr[i], i, arr);
  914. }
  915. return results;
  916. },
  917. /**
  918. * Get the position of an element relative to the top left of the page
  919. */
  920. offset: function (el) {
  921. return $(el).offset();
  922. },
  923. /**
  924. * Add an event listener
  925. * @param {Object} el A HTML element or custom object
  926. * @param {String} event The event type
  927. * @param {Function} fn The event handler
  928. */
  929. addEvent: function (el, event, fn) {
  930. $(el).bind(event, fn);
  931. },
  932. /**
  933. * Remove event added with addEvent
  934. * @param {Object} el The object
  935. * @param {String} eventType The event type. Leave blank to remove all events.
  936. * @param {Function} handler The function to remove
  937. */
  938. removeEvent: function (el, eventType, handler) {
  939. // workaround for jQuery issue with unbinding custom events:
  940. // http://forum.jQuery.com/topic/javascript-error-when-unbinding-a-custom-event-using-jQuery-1-4-2
  941. var func = doc.removeEventListener ? 'removeEventListener' : 'detachEvent';
  942. if (doc[func] && el && !el[func]) {
  943. el[func] = function () {};
  944. }
  945. $(el).unbind(eventType, handler);
  946. },
  947. /**
  948. * Fire an event on a custom object
  949. * @param {Object} el
  950. * @param {String} type
  951. * @param {Object} eventArguments
  952. * @param {Function} defaultFunction
  953. */
  954. fireEvent: function (el, type, eventArguments, defaultFunction) {
  955. var event = $.Event(type),
  956. detachedType = 'detached' + type,
  957. defaultPrevented;
  958. // Remove warnings in Chrome when accessing returnValue (#2790), layerX and layerY. Although Highcharts
  959. // never uses these properties, Chrome includes them in the default click event and
  960. // raises the warning when they are copied over in the extend statement below.
  961. //
  962. // To avoid problems in IE (see #1010) where we cannot delete the properties and avoid
  963. // testing if they are there (warning in chrome) the only option is to test if running IE.
  964. if (!isIE && eventArguments) {
  965. delete eventArguments.layerX;
  966. delete eventArguments.layerY;
  967. delete eventArguments.returnValue;
  968. }
  969. extend(event, eventArguments);
  970. // Prevent jQuery from triggering the object method that is named the
  971. // same as the event. For example, if the event is 'select', jQuery
  972. // attempts calling el.select and it goes into a loop.
  973. if (el[type]) {
  974. el[detachedType] = el[type];
  975. el[type] = null;
  976. }
  977. // Wrap preventDefault and stopPropagation in try/catch blocks in
  978. // order to prevent JS errors when cancelling events on non-DOM
  979. // objects. #615.
  980. /*jslint unparam: true*/
  981. $.each(['preventDefault', 'stopPropagation'], function (i, fn) {
  982. var base = event[fn];
  983. event[fn] = function () {
  984. try {
  985. base.call(event);
  986. } catch (e) {
  987. if (fn === 'preventDefault') {
  988. defaultPrevented = true;
  989. }
  990. }
  991. };
  992. });
  993. /*jslint unparam: false*/
  994. // trigger it
  995. $(el).trigger(event);
  996. // attach the method
  997. if (el[detachedType]) {
  998. el[type] = el[detachedType];
  999. el[detachedType] = null;
  1000. }
  1001. if (defaultFunction && !event.isDefaultPrevented() && !defaultPrevented) {
  1002. defaultFunction(event);
  1003. }
  1004. },
  1005. /**
  1006. * Extension method needed for MooTools
  1007. */
  1008. washMouseEvent: function (e) {
  1009. var ret = e.originalEvent || e;
  1010. // computed by jQuery, needed by IE8
  1011. if (ret.pageX === UNDEFINED) { // #1236
  1012. ret.pageX = e.pageX;
  1013. ret.pageY = e.pageY;
  1014. }
  1015. return ret;
  1016. },
  1017. /**
  1018. * Animate a HTML element or SVG element wrapper
  1019. * @param {Object} el
  1020. * @param {Object} params
  1021. * @param {Object} options jQuery-like animation options: duration, easing, callback
  1022. */
  1023. animate: function (el, params, options) {
  1024. var $el = $(el);
  1025. if (!el.style) {
  1026. el.style = {}; // #1881
  1027. }
  1028. if (params.d) {
  1029. el.toD = params.d; // keep the array form for paths, used in $.fx.step.d
  1030. params.d = 1; // because in jQuery, animating to an array has a different meaning
  1031. }
  1032. $el.stop();
  1033. if (params.opacity !== UNDEFINED && el.attr) {
  1034. params.opacity += 'px'; // force jQuery to use same logic as width and height (#2161)
  1035. }
  1036. $el.animate(params, options);
  1037. },
  1038. /**
  1039. * Stop running animation
  1040. */
  1041. stop: function (el) {
  1042. $(el).stop();
  1043. }
  1044. });
  1045. }(win.jQuery));
  1046. // check for a custom HighchartsAdapter defined prior to this file
  1047. var globalAdapter = win.HighchartsAdapter,
  1048. adapter = globalAdapter || {};
  1049. // Initialize the adapter
  1050. if (globalAdapter) {
  1051. globalAdapter.init.call(globalAdapter, pathAnim);
  1052. }
  1053. // Utility functions. If the HighchartsAdapter is not defined, adapter is an empty object
  1054. // and all the utility functions will be null. In that case they are populated by the
  1055. // default adapters below.
  1056. var adapterRun = adapter.adapterRun,
  1057. getScript = adapter.getScript,
  1058. inArray = adapter.inArray,
  1059. each = adapter.each,
  1060. grep = adapter.grep,
  1061. offset = adapter.offset,
  1062. map = adapter.map,
  1063. addEvent = adapter.addEvent,
  1064. removeEvent = adapter.removeEvent,
  1065. fireEvent = adapter.fireEvent,
  1066. washMouseEvent = adapter.washMouseEvent,
  1067. animate = adapter.animate,
  1068. stop = adapter.stop;
  1069. /* ****************************************************************************
  1070. * Handle the options *
  1071. *****************************************************************************/
  1072. var
  1073. defaultLabelOptions = {
  1074. enabled: true,
  1075. // rotation: 0,
  1076. // align: 'center',
  1077. x: 0,
  1078. y: 15,
  1079. /*formatter: function () {
  1080. return this.value;
  1081. },*/
  1082. style: {
  1083. color: '#606060',
  1084. cursor: 'default',
  1085. fontSize: '11px'
  1086. }
  1087. };
  1088. defaultOptions = {
  1089. colors: ['#7cb5ec', '#434348', '#90ed7d', '#f7a35c',
  1090. '#8085e9', '#f15c80', '#e4d354', '#8085e8', '#8d4653', '#91e8e1'],
  1091. symbols: ['circle', 'diamond', 'square', 'triangle', 'triangle-down'],
  1092. lang: {
  1093. loading: 'Loading...',
  1094. months: ['January', 'February', 'March', 'April', 'May', 'June', 'July',
  1095. 'August', 'September', 'October', 'November', 'December'],
  1096. shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  1097. weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
  1098. decimalPoint: '.',
  1099. numericSymbols: ['k', 'M', 'G', 'T', 'P', 'E'], // SI prefixes used in axis labels
  1100. resetZoom: 'Reset zoom',
  1101. resetZoomTitle: 'Reset zoom level 1:1',
  1102. thousandsSep: ','
  1103. },
  1104. global: {
  1105. useUTC: true,
  1106. //timezoneOffset: 0,
  1107. canvasToolsURL: 'http://code.highcharts.com/4.0.3/modules/canvas-tools.js',
  1108. VMLRadialGradientURL: 'http://code.highcharts.com/4.0.3/gfx/vml-radial-gradient.png'
  1109. },
  1110. chart: {
  1111. //animation: true,
  1112. //alignTicks: false,
  1113. //reflow: true,
  1114. //className: null,
  1115. //events: { load, selection },
  1116. //margin: [null],
  1117. //marginTop: null,
  1118. //marginRight: null,
  1119. //marginBottom: null,
  1120. //marginLeft: null,
  1121. borderColor: '#4572A7',
  1122. //borderWidth: 0,
  1123. borderRadius: 0,
  1124. defaultSeriesType: 'line',
  1125. ignoreHiddenSeries: true,
  1126. //inverted: false,
  1127. //shadow: false,
  1128. spacing: [10, 10, 15, 10],
  1129. //spacingTop: 10,
  1130. //spacingRight: 10,
  1131. //spacingBottom: 15,
  1132. //spacingLeft: 10,
  1133. //style: {
  1134. // fontFamily: '"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif', // default font
  1135. // fontSize: '12px'
  1136. //},
  1137. backgroundColor: '#FFFFFF',
  1138. //plotBackgroundColor: null,
  1139. plotBorderColor: '#C0C0C0',
  1140. //plotBorderWidth: 0,
  1141. //plotShadow: false,
  1142. //zoomType: ''
  1143. resetZoomButton: {
  1144. theme: {
  1145. zIndex: 20
  1146. },
  1147. position: {
  1148. align: 'right',
  1149. x: -10,
  1150. //verticalAlign: 'top',
  1151. y: 10
  1152. }
  1153. // relativeTo: 'plot'
  1154. }
  1155. },
  1156. title: {
  1157. text: 'Chart title',
  1158. align: 'center',
  1159. // floating: false,
  1160. margin: 15,
  1161. // x: 0,
  1162. // verticalAlign: 'top',
  1163. // y: null,
  1164. style: {
  1165. color: '#333333',
  1166. fontSize: '18px'
  1167. }
  1168. },
  1169. subtitle: {
  1170. text: '',
  1171. align: 'center',
  1172. // floating: false
  1173. // x: 0,
  1174. // verticalAlign: 'top',
  1175. // y: null,
  1176. style: {
  1177. color: '#555555'
  1178. }
  1179. },
  1180. plotOptions: {
  1181. line: { // base series options
  1182. allowPointSelect: false,
  1183. showCheckbox: false,
  1184. animation: {
  1185. duration: 1000
  1186. },
  1187. //connectNulls: false,
  1188. //cursor: 'default',
  1189. //clip: true,
  1190. //dashStyle: null,
  1191. //enableMouseTracking: true,
  1192. events: {},
  1193. //legendIndex: 0,
  1194. //linecap: 'round',
  1195. lineWidth: 2,
  1196. //shadow: false,
  1197. // stacking: null,
  1198. marker: {
  1199. //enabled: true,
  1200. //symbol: null,
  1201. lineWidth: 0,
  1202. radius: 4,
  1203. lineColor: '#FFFFFF',
  1204. //fillColor: null,
  1205. states: { // states for a single point
  1206. hover: {
  1207. enabled: true,
  1208. lineWidthPlus: 1,
  1209. radiusPlus: 2
  1210. },
  1211. select: {
  1212. fillColor: '#FFFFFF',
  1213. lineColor: '#000000',
  1214. lineWidth: 2
  1215. }
  1216. }
  1217. },
  1218. point: {
  1219. events: {}
  1220. },
  1221. dataLabels: merge(defaultLabelOptions, {
  1222. align: 'center',
  1223. //defer: true,
  1224. enabled: false,
  1225. formatter: function () {
  1226. return this.y === null ? '' : numberFormat(this.y, -1);
  1227. },
  1228. verticalAlign: 'bottom', // above singular point
  1229. y: 0
  1230. // backgroundColor: undefined,
  1231. // borderColor: undefined,
  1232. // borderRadius: undefined,
  1233. // borderWidth: undefined,
  1234. // padding: 3,
  1235. // shadow: false
  1236. }),
  1237. cropThreshold: 300, // draw points outside the plot area when the number of points is less than this
  1238. pointRange: 0,
  1239. //pointStart: 0,
  1240. //pointInterval: 1,
  1241. //showInLegend: null, // auto: true for standalone series, false for linked series
  1242. states: { // states for the entire series
  1243. hover: {
  1244. //enabled: false,
  1245. lineWidthPlus: 1,
  1246. marker: {
  1247. // lineWidth: base + 1,
  1248. // radius: base + 1
  1249. },
  1250. halo: {
  1251. size: 10,
  1252. opacity: 0.25
  1253. }
  1254. },
  1255. select: {
  1256. marker: {}
  1257. }
  1258. },
  1259. stickyTracking: true,
  1260. //tooltip: {
  1261. //pointFormat: '<span style="color:{series.color}">\u25CF</span> {series.name}: <b>{point.y}</b>'
  1262. //valueDecimals: null,
  1263. //xDateFormat: '%A, %b %e, %Y',
  1264. //valuePrefix: '',
  1265. //ySuffix: ''
  1266. //}
  1267. turboThreshold: 1000
  1268. // zIndex: null
  1269. }
  1270. },
  1271. labels: {
  1272. //items: [],
  1273. style: {
  1274. //font: defaultFont,
  1275. position: ABSOLUTE,
  1276. color: '#3E576F'
  1277. }
  1278. },
  1279. legend: {
  1280. enabled: true,
  1281. align: 'center',
  1282. //floating: false,
  1283. layout: 'horizontal',
  1284. labelFormatter: function () {
  1285. return this.name;
  1286. },
  1287. //borderWidth: 0,
  1288. borderColor: '#909090',
  1289. borderRadius: 0,
  1290. navigation: {
  1291. // animation: true,
  1292. activeColor: '#274b6d',
  1293. // arrowSize: 12
  1294. inactiveColor: '#CCC'
  1295. // style: {} // text styles
  1296. },
  1297. // margin: 20,
  1298. // reversed: false,
  1299. shadow: false,
  1300. // backgroundColor: null,
  1301. /*style: {
  1302. padding: '5px'
  1303. },*/
  1304. itemStyle: {
  1305. color: '#333333',
  1306. fontSize: '12px',
  1307. fontWeight: 'bold'
  1308. },
  1309. itemHoverStyle: {
  1310. //cursor: 'pointer', removed as of #601
  1311. color: '#000'
  1312. },
  1313. itemHiddenStyle: {
  1314. color: '#CCC'
  1315. },
  1316. itemCheckboxStyle: {
  1317. position: ABSOLUTE,
  1318. width: '13px', // for IE precision
  1319. height: '13px'
  1320. },
  1321. // itemWidth: undefined,
  1322. // symbolRadius: 0,
  1323. // symbolWidth: 16,
  1324. symbolPadding: 5,
  1325. verticalAlign: 'bottom',
  1326. // width: undefined,
  1327. x: 0,
  1328. y: 0,
  1329. title: {
  1330. //text: null,
  1331. style: {
  1332. fontWeight: 'bold'
  1333. }
  1334. }
  1335. },
  1336. loading: {
  1337. // hideDuration: 100,
  1338. labelStyle: {
  1339. fontWeight: 'bold',
  1340. position: RELATIVE,
  1341. top: '45%'
  1342. },
  1343. // showDuration: 0,
  1344. style: {
  1345. position: ABSOLUTE,
  1346. backgroundColor: 'white',
  1347. opacity: 0.5,
  1348. textAlign: 'center'
  1349. }
  1350. },
  1351. tooltip: {
  1352. enabled: true,
  1353. animation: hasSVG,
  1354. //crosshairs: null,
  1355. backgroundColor: 'rgba(249, 249, 249, .85)',
  1356. borderWidth: 1,
  1357. borderRadius: 3,
  1358. dateTimeLabelFormats: {
  1359. millisecond: '%A, %b %e, %H:%M:%S.%L',
  1360. second: '%A, %b %e, %H:%M:%S',
  1361. minute: '%A, %b %e, %H:%M',
  1362. hour: '%A, %b %e, %H:%M',
  1363. day: '%A, %b %e, %Y',
  1364. week: 'Week from %A, %b %e, %Y',
  1365. month: '%B %Y',
  1366. year: '%Y'
  1367. },
  1368. //formatter: defaultFormatter,
  1369. headerFormat: '<span style="font-size: 10px">{point.key}</span><br/>',
  1370. pointFormat: '<span style="color:{series.color}">\u25CF</span> {series.name}: <b>{point.y}</b><br/>',
  1371. shadow: true,
  1372. //shape: 'callout',
  1373. //shared: false,
  1374. snap: isTouchDevice ? 25 : 10,
  1375. style: {
  1376. color: '#333333',
  1377. cursor: 'default',
  1378. fontSize: '12px',
  1379. padding: '8px',
  1380. whiteSpace: 'nowrap'
  1381. }
  1382. //xDateFormat: '%A, %b %e, %Y',
  1383. //valueDecimals: null,
  1384. //valuePrefix: '',
  1385. //valueSuffix: ''
  1386. },
  1387. credits: {
  1388. enabled: true,
  1389. text: '',
  1390. href: 'http://www.highcharts.com',
  1391. position: {
  1392. align: 'right',
  1393. x: -10,
  1394. verticalAlign: 'bottom',
  1395. y: -5
  1396. },
  1397. style: {
  1398. cursor: 'pointer',
  1399. color: '#909090',
  1400. fontSize: '9px'
  1401. }
  1402. }
  1403. };
  1404. // Series defaults
  1405. var defaultPlotOptions = defaultOptions.plotOptions,
  1406. defaultSeriesOptions = defaultPlotOptions.line;
  1407. // set the default time methods
  1408. setTimeMethods();
  1409. /**
  1410. * Set the time methods globally based on the useUTC option. Time method can be either
  1411. * local time or UTC (default).
  1412. */
  1413. function setTimeMethods() {
  1414. var useUTC = defaultOptions.global.useUTC,
  1415. GET = useUTC ? 'getUTC' : 'get',
  1416. SET = useUTC ? 'setUTC' : 'set';
  1417. timezoneOffset = ((useUTC && defaultOptions.global.timezoneOffset) || 0) * 60000;
  1418. makeTime = useUTC ? Date.UTC : function (year, month, date, hours, minutes, seconds) {
  1419. return new Date(
  1420. year,
  1421. month,
  1422. pick(date, 1),
  1423. pick(hours, 0),
  1424. pick(minutes, 0),
  1425. pick(seconds, 0)
  1426. ).getTime();
  1427. };
  1428. getMinutes = GET + 'Minutes';
  1429. getHours = GET + 'Hours';
  1430. getDay = GET + 'Day';
  1431. getDate = GET + 'Date';
  1432. getMonth = GET + 'Month';
  1433. getFullYear = GET + 'FullYear';
  1434. setMinutes = SET + 'Minutes';
  1435. setHours = SET + 'Hours';
  1436. setDate = SET + 'Date';
  1437. setMonth = SET + 'Month';
  1438. setFullYear = SET + 'FullYear';
  1439. }
  1440. /**
  1441. * Merge the default options with custom options and return the new options structure
  1442. * @param {Object} options The new custom options
  1443. */
  1444. function setOptions(options) {
  1445. // Copy in the default options
  1446. defaultOptions = merge(true, defaultOptions, options);
  1447. // Apply UTC
  1448. setTimeMethods();
  1449. return defaultOptions;
  1450. }
  1451. /**
  1452. * Get the updated default options. Until 3.0.7, merely exposing defaultOptions for outside modules
  1453. * wasn't enough because the setOptions method created a new object.
  1454. */
  1455. function getOptions() {
  1456. return defaultOptions;
  1457. }
  1458. /**
  1459. * Handle color operations. The object methods are chainable.
  1460. * @param {String} input The input color in either rbga or hex format
  1461. */
  1462. var rgbaRegEx = /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*\)/,
  1463. hexRegEx = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/,
  1464. rgbRegEx = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/;
  1465. var Color = function (input) {
  1466. // declare variables
  1467. var rgba = [], result, stops;
  1468. /**
  1469. * Parse the input color to rgba array
  1470. * @param {String} input
  1471. */
  1472. function init(input) {
  1473. // Gradients
  1474. if (input && input.stops) {
  1475. stops = map(input.stops, function (stop) {
  1476. return Color(stop[1]);
  1477. });
  1478. // Solid colors
  1479. } else {
  1480. // rgba
  1481. result = rgbaRegEx.exec(input);
  1482. if (result) {
  1483. rgba = [pInt(result[1]), pInt(result[2]), pInt(result[3]), parseFloat(result[4], 10)];
  1484. } else {
  1485. // hex
  1486. result = hexRegEx.exec(input);
  1487. if (result) {
  1488. rgba = [pInt(result[1], 16), pInt(result[2], 16), pInt(result[3], 16), 1];
  1489. } else {
  1490. // rgb
  1491. result = rgbRegEx.exec(input);
  1492. if (result) {
  1493. rgba = [pInt(result[1]), pInt(result[2]), pInt(result[3]), 1];
  1494. }
  1495. }
  1496. }
  1497. }
  1498. }
  1499. /**
  1500. * Return the color a specified format
  1501. * @param {String} format
  1502. */
  1503. function get(format) {
  1504. var ret;
  1505. if (stops) {
  1506. ret = merge(input);
  1507. ret.stops = [].concat(ret.stops);
  1508. each(stops, function (stop, i) {
  1509. ret.stops[i] = [ret.stops[i][0], stop.get(format)];
  1510. });
  1511. // it's NaN if gradient colors on a column chart
  1512. } else if (rgba && !isNaN(rgba[0])) {
  1513. if (format === 'rgb') {
  1514. ret = 'rgb(' + rgba[0] + ',' + rgba[1] + ',' + rgba[2] + ')';
  1515. } else if (format === 'a') {
  1516. ret = rgba[3];
  1517. } else {
  1518. ret = 'rgba(' + rgba.join(',') + ')';
  1519. }
  1520. } else {
  1521. ret = input;
  1522. }
  1523. return ret;
  1524. }
  1525. /**
  1526. * Brighten the color
  1527. * @param {Number} alpha
  1528. */
  1529. function brighten(alpha) {
  1530. if (stops) {
  1531. each(stops, function (stop) {
  1532. stop.brighten(alpha);
  1533. });
  1534. } else if (isNumber(alpha) && alpha !== 0) {
  1535. var i;
  1536. for (i = 0; i < 3; i++) {
  1537. rgba[i] += pInt(alpha * 255);
  1538. if (rgba[i] < 0) {
  1539. rgba[i] = 0;
  1540. }
  1541. if (rgba[i] > 255) {
  1542. rgba[i] = 255;
  1543. }
  1544. }
  1545. }
  1546. return this;
  1547. }
  1548. /**
  1549. * Set the color's opacity to a given alpha value
  1550. * @param {Number} alpha
  1551. */
  1552. function setOpacity(alpha) {
  1553. rgba[3] = alpha;
  1554. return this;
  1555. }
  1556. // initialize: parse the input
  1557. init(input);
  1558. // public methods
  1559. return {
  1560. get: get,
  1561. brighten: brighten,
  1562. rgba: rgba,
  1563. setOpacity: setOpacity
  1564. };
  1565. };
  1566. /**
  1567. * A wrapper object for SVG elements
  1568. */
  1569. function SVGElement() {}
  1570. SVGElement.prototype = {
  1571. // Default base for animation
  1572. opacity: 1,
  1573. // For labels, these CSS properties are applied to the <text> node directly
  1574. textProps: ['fontSize', 'fontWeight', 'fontFamily', 'color',
  1575. 'lineHeight', 'width', 'textDecoration', 'textShadow', 'HcTextStroke'],
  1576. /**
  1577. * Initialize the SVG renderer
  1578. * @param {Object} renderer
  1579. * @param {String} nodeName
  1580. */
  1581. init: function (renderer, nodeName) {
  1582. var wrapper = this;
  1583. wrapper.element = nodeName === 'span' ?
  1584. createElement(nodeName) :
  1585. doc.createElementNS(SVG_NS, nodeName);
  1586. wrapper.renderer = renderer;
  1587. },
  1588. /**
  1589. * Animate a given attribute
  1590. * @param {Object} params
  1591. * @param {Number} options The same options as in jQuery animation
  1592. * @param {Function} complete Function to perform at the end of animation
  1593. */
  1594. animate: function (params, options, complete) {
  1595. var animOptions = pick(options, globalAnimation, true);
  1596. stop(this); // stop regardless of animation actually running, or reverting to .attr (#607)
  1597. if (animOptions) {
  1598. animOptions = merge(animOptions, {}); //#2625
  1599. if (complete) { // allows using a callback with the global animation without overwriting it
  1600. animOptions.complete = complete;
  1601. }
  1602. animate(this, params, animOptions);
  1603. } else {
  1604. this.attr(params);
  1605. if (complete) {
  1606. complete();
  1607. }
  1608. }
  1609. return this;
  1610. },
  1611. /**
  1612. * Build an SVG gradient out of a common JavaScript configuration object
  1613. */
  1614. colorGradient: function (color, prop, elem) {
  1615. var renderer = this.renderer,
  1616. colorObject,
  1617. gradName,
  1618. gradAttr,
  1619. gradients,
  1620. gradientObject,
  1621. stops,
  1622. stopColor,
  1623. stopOpacity,
  1624. radialReference,
  1625. n,
  1626. id,
  1627. key = [];
  1628. // Apply linear or radial gradients
  1629. if (color.linearGradient) {
  1630. gradName = 'linearGradient';
  1631. } else if (color.radialGradient) {
  1632. gradName = 'radialGradient';
  1633. }
  1634. if (gradName) {
  1635. gradAttr = color[gradName];
  1636. gradients = renderer.gradients;
  1637. stops = color.stops;
  1638. radialReference = elem.radialReference;
  1639. // Keep < 2.2 kompatibility
  1640. if (isArray(gradAttr)) {
  1641. color[gradName] = gradAttr = {
  1642. x1: gradAttr[0],
  1643. y1: gradAttr[1],
  1644. x2: gradAttr[2],
  1645. y2: gradAttr[3],
  1646. gradientUnits: 'userSpaceOnUse'
  1647. };
  1648. }
  1649. // Correct the radial gradient for the radial reference system
  1650. if (gradName === 'radialGradient' && radialReference && !defined(gradAttr.gradientUnits)) {
  1651. gradAttr = merge(gradAttr, {
  1652. cx: (radialReference[0] - radialReference[2] / 2) + gradAttr.cx * radialReference[2],
  1653. cy: (radialReference[1] - radialReference[2] / 2) + gradAttr.cy * radialReference[2],
  1654. r: gradAttr.r * radialReference[2],
  1655. gradientUnits: 'userSpaceOnUse'
  1656. });
  1657. }
  1658. // Build the unique key to detect whether we need to create a new element (#1282)
  1659. for (n in gradAttr) {
  1660. if (n !== 'id') {
  1661. key.push(n, gradAttr[n]);
  1662. }
  1663. }
  1664. for (n in stops) {
  1665. key.push(stops[n]);
  1666. }
  1667. key = key.join(',');
  1668. // Check if a gradient object with the same config object is created within this renderer
  1669. if (gradients[key]) {
  1670. id = gradients[key].attr('id');
  1671. } else {
  1672. // Set the id and create the element
  1673. gradAttr.id = id = PREFIX + idCounter++;
  1674. gradients[key] = gradientObject = renderer.createElement(gradName)
  1675. .attr(gradAttr)
  1676. .add(renderer.defs);
  1677. // The gradient needs to keep a list of stops to be able to destroy them
  1678. gradientObject.stops = [];
  1679. each(stops, function (stop) {
  1680. var stopObject;
  1681. if (stop[1].indexOf('rgba') === 0) {
  1682. colorObject = Color(stop[1]);
  1683. stopColor = colorObject.get('rgb');
  1684. stopOpacity = colorObject.get('a');
  1685. } else {
  1686. stopColor = stop[1];
  1687. stopOpacity = 1;
  1688. }
  1689. stopObject = renderer.createElement('stop').attr({
  1690. offset: stop[0],
  1691. 'stop-color': stopColor,
  1692. 'stop-opacity': stopOpacity
  1693. }).add(gradientObject);
  1694. // Add the stop element to the gradient
  1695. gradientObject.stops.push(stopObject);
  1696. });
  1697. }
  1698. // Set the reference to the gradient object
  1699. elem.setAttribute(prop, 'url(' + renderer.url + '#' + id + ')');
  1700. }
  1701. },
  1702. /**
  1703. * Set or get a given attribute
  1704. * @param {Object|String} hash
  1705. * @param {Mixed|Undefined} val
  1706. */
  1707. attr: function (hash, val) {
  1708. var key,
  1709. value,
  1710. element = this.element,
  1711. hasSetSymbolSize,
  1712. ret = this,
  1713. skipAttr;
  1714. // single key-value pair
  1715. if (typeof hash === 'string' && val !== UNDEFINED) {
  1716. key = hash;
  1717. hash = {};
  1718. hash[key] = val;
  1719. }
  1720. // used as a getter: first argument is a string, second is undefined
  1721. if (typeof hash === 'string') {
  1722. ret = (this[hash + 'Getter'] || this._defaultGetter).call(this, hash, element);
  1723. // setter
  1724. } else {
  1725. for (key in hash) {
  1726. value = hash[key];
  1727. skipAttr = false;
  1728. if (this.symbolName && /^(x|y|width|height|r|start|end|innerR|anchorX|anchorY)/.test(key)) {
  1729. if (!hasSetSymbolSize) {
  1730. this.symbolAttr(hash);
  1731. hasSetSymbolSize = true;
  1732. }
  1733. skipAttr = true;
  1734. }
  1735. if (this.rotation && (key === 'x' || key === 'y')) {
  1736. this.doTransform = true;
  1737. }
  1738. if (!skipAttr) {
  1739. (this[key + 'Setter'] || this._defaultSetter).call(this, value, key, element);
  1740. }
  1741. // Let the shadow follow the main element
  1742. if (this.shadows && /^(width|height|visibility|x|y|d|transform|cx|cy|r)$/.test(key)) {
  1743. this.updateShadows(key, value);
  1744. }
  1745. }
  1746. // Update transform. Do this outside the loop to prevent redundant updating for batch setting
  1747. // of attributes.
  1748. if (this.doTransform) {
  1749. this.updateTransform();
  1750. this.doTransform = false;
  1751. }
  1752. }
  1753. return ret;
  1754. },
  1755. updateShadows: function (key, value) {
  1756. var shadows = this.shadows,
  1757. i = shadows.length;
  1758. while (i--) {
  1759. shadows[i].setAttribute(
  1760. key,
  1761. key === 'height' ?
  1762. mathMax(value - (shadows[i].cutHeight || 0), 0) :
  1763. key === 'd' ? this.d : value
  1764. );
  1765. }
  1766. },
  1767. /**
  1768. * Add a class name to an element
  1769. */
  1770. addClass: function (className) {
  1771. var element = this.element,
  1772. currentClassName = attr(element, 'class') || '';
  1773. if (currentClassName.indexOf(className) === -1) {
  1774. attr(element, 'class', currentClassName + ' ' + className);
  1775. }
  1776. return this;
  1777. },
  1778. /* hasClass and removeClass are not (yet) needed
  1779. hasClass: function (className) {
  1780. return attr(this.element, 'class').indexOf(className) !== -1;
  1781. },
  1782. removeClass: function (className) {
  1783. attr(this.element, 'class', attr(this.element, 'class').replace(className, ''));
  1784. return this;
  1785. },
  1786. */
  1787. /**
  1788. * If one of the symbol size affecting parameters are changed,
  1789. * check all the others only once for each call to an element's
  1790. * .attr() method
  1791. * @param {Object} hash
  1792. */
  1793. symbolAttr: function (hash) {
  1794. var wrapper = this;
  1795. each(['x', 'y', 'r', 'start', 'end', 'width', 'height', 'innerR', 'anchorX', 'anchorY'], function (key) {
  1796. wrapper[key] = pick(hash[key], wrapper[key]);
  1797. });
  1798. wrapper.attr({
  1799. d: wrapper.renderer.symbols[wrapper.symbolName](
  1800. wrapper.x,
  1801. wrapper.y,
  1802. wrapper.width,
  1803. wrapper.height,
  1804. wrapper
  1805. )
  1806. });
  1807. },
  1808. /**
  1809. * Apply a clipping path to this object
  1810. * @param {String} id
  1811. */
  1812. clip: function (clipRect) {
  1813. return this.attr('clip-path', clipRect ? 'url(' + this.renderer.url + '#' + clipRect.id + ')' : NONE);
  1814. },
  1815. /**
  1816. * Calculate the coordinates needed for drawing a rectangle crisply and return the
  1817. * calculated attributes
  1818. * @param {Number} strokeWidth
  1819. * @param {Number} x
  1820. * @param {Number} y
  1821. * @param {Number} width
  1822. * @param {Number} height
  1823. */
  1824. crisp: function (rect) {
  1825. var wrapper = this,
  1826. key,
  1827. attribs = {},
  1828. normalizer,
  1829. strokeWidth = rect.strokeWidth || wrapper.strokeWidth || 0;
  1830. normalizer = mathRound(strokeWidth) % 2 / 2; // mathRound because strokeWidth can sometimes have roundoff errors
  1831. // normalize for crisp edges
  1832. rect.x = mathFloor(rect.x || wrapper.x || 0) + normalizer;
  1833. rect.y = mathFloor(rect.y || wrapper.y || 0) + normalizer;
  1834. rect.width = mathFloor((rect.width || wrapper.width || 0) - 2 * normalizer);
  1835. rect.height = mathFloor((rect.height || wrapper.height || 0) - 2 * normalizer);
  1836. rect.strokeWidth = strokeWidth;
  1837. for (key in rect) {
  1838. if (wrapper[key] !== rect[key]) { // only set attribute if changed
  1839. wrapper[key] = attribs[key] = rect[key];
  1840. }
  1841. }
  1842. return attribs;
  1843. },
  1844. /**
  1845. * Set styles for the element
  1846. * @param {Object} styles
  1847. */
  1848. css: function (styles) {
  1849. var elemWrapper = this,
  1850. oldStyles = elemWrapper.styles,
  1851. newStyles = {},
  1852. elem = elemWrapper.element,
  1853. textWidth,
  1854. n,
  1855. serializedCss = '',
  1856. hyphenate,
  1857. hasNew = !oldStyles;
  1858. // convert legacy
  1859. if (styles && styles.color) {
  1860. styles.fill = styles.color;
  1861. }
  1862. // Filter out existing styles to increase performance (#2640)
  1863. if (oldStyles) {
  1864. for (n in styles) {
  1865. if (styles[n] !== oldStyles[n]) {
  1866. newStyles[n] = styles[n];
  1867. hasNew = true;
  1868. }
  1869. }
  1870. }
  1871. if (hasNew) {
  1872. textWidth = elemWrapper.textWidth = styles && styles.width && elem.nodeName.toLowerCase() === 'text' && pInt(styles.width);
  1873. // Merge the new styles with the old ones
  1874. if (oldStyles) {
  1875. styles = extend(
  1876. oldStyles,
  1877. newStyles
  1878. );
  1879. }
  1880. // store object
  1881. elemWrapper.styles = styles;
  1882. if (textWidth && (useCanVG || (!hasSVG && elemWrapper.renderer.forExport))) {
  1883. delete styles.width;
  1884. }
  1885. // serialize and set style attribute
  1886. if (isIE && !hasSVG) {
  1887. css(elemWrapper.element, styles);
  1888. } else {
  1889. /*jslint unparam: true*/
  1890. hyphenate = function (a, b) { return '-' + b.toLowerCase(); };
  1891. /*jslint unparam: false*/
  1892. for (n in styles) {
  1893. serializedCss += n.replace(/([A-Z])/g, hyphenate) + ':' + styles[n] + ';';
  1894. }
  1895. attr(elem, 'style', serializedCss); // #1881
  1896. }
  1897. // re-build text
  1898. if (textWidth && elemWrapper.added) {
  1899. elemWrapper.renderer.buildText(elemWrapper);
  1900. }
  1901. }
  1902. return elemWrapper;
  1903. },
  1904. /**
  1905. * Add an event listener
  1906. * @param {String} eventType
  1907. * @param {Function} handler
  1908. */
  1909. on: function (eventType, handler) {
  1910. var svgElement = this,
  1911. element = svgElement.element;
  1912. // touch
  1913. if (hasTouch && eventType === 'click') {
  1914. element.ontouchstart = function (e) {
  1915. svgElement.touchEventFired = Date.now();
  1916. e.preventDefault();
  1917. handler.call(element, e);
  1918. };
  1919. element.onclick = function (e) {
  1920. if (userAgent.indexOf('Android') === -1 || Date.now() - (svgElement.touchEventFired || 0) > 1100) { // #2269
  1921. handler.call(element, e);
  1922. }
  1923. };
  1924. } else {
  1925. // simplest possible event model for internal use
  1926. element['on' + eventType] = handler;
  1927. }
  1928. return this;
  1929. },
  1930. /**
  1931. * Set the coordinates needed to draw a consistent radial gradient across
  1932. * pie slices regardless of positioning inside the chart. The format is
  1933. * [centerX, centerY, diameter] in pixels.
  1934. */
  1935. setRadialReference: function (coordinates) {
  1936. this.element.radialReference = coordinates;
  1937. return this;
  1938. },
  1939. /**
  1940. * Move an object and its children by x and y values
  1941. * @param {Number} x
  1942. * @param {Number} y
  1943. */
  1944. translate: function (x, y) {
  1945. return this.attr({
  1946. translateX: x,
  1947. translateY: y
  1948. });
  1949. },
  1950. /**
  1951. * Invert a group, rotate and flip
  1952. */
  1953. invert: function () {
  1954. var wrapper = this;
  1955. wrapper.inverted = true;
  1956. wrapper.updateTransform();
  1957. return wrapper;
  1958. },
  1959. /**
  1960. * Private method to update the transform attribute based on internal
  1961. * properties
  1962. */
  1963. updateTransform: function () {
  1964. var wrapper = this,
  1965. translateX = wrapper.translateX || 0,
  1966. translateY = wrapper.translateY || 0,
  1967. scaleX = wrapper.scaleX,
  1968. scaleY = wrapper.scaleY,
  1969. inverted = wrapper.inverted,
  1970. rotation = wrapper.rotation,
  1971. element = wrapper.element,
  1972. transform;
  1973. // flipping affects translate as adjustment for flipping around the group's axis
  1974. if (inverted) {
  1975. translateX += wrapper.attr('width');
  1976. translateY += wrapper.attr('height');
  1977. }
  1978. // Apply translate. Nearly all transformed elements have translation, so instead
  1979. // of checking for translate = 0, do it always (#1767, #1846).
  1980. transform = ['translate(' + translateX + ',' + translateY + ')'];
  1981. // apply rotation
  1982. if (inverted) {
  1983. transform.push('rotate(90) scale(-1,1)');
  1984. } else if (rotation) { // text rotation
  1985. transform.push('rotate(' + rotation + ' ' + (element.getAttribute('x') || 0) + ' ' + (element.getAttribute('y') || 0) + ')');
  1986. }
  1987. // apply scale
  1988. if (defined(scaleX) || defined(scaleY)) {
  1989. transform.push('scale(' + pick(scaleX, 1) + ' ' + pick(scaleY, 1) + ')');
  1990. }
  1991. if (transform.length) {
  1992. element.setAttribute('transform', transform.join(' '));
  1993. }
  1994. },
  1995. /**
  1996. * Bring the element to the front
  1997. */
  1998. toFront: function () {
  1999. var element = this.element;
  2000. element.parentNode.appendChild(element);
  2001. return this;
  2002. },
  2003. /**
  2004. * Break down alignment options like align, verticalAlign, x and y
  2005. * to x and y relative to the chart.
  2006. *
  2007. * @param {Object} alignOptions
  2008. * @param {Boolean} alignByTranslate
  2009. * @param {String[Object} box The box to align to, needs a width and height. When the
  2010. * box is a string, it refers to an object in the Renderer. For example, when
  2011. * box is 'spacingBox', it refers to Renderer.spacingBox which holds width, height
  2012. * x and y properties.
  2013. *
  2014. */
  2015. align: function (alignOptions, alignByTranslate, box) {
  2016. var align,
  2017. vAlign,
  2018. x,
  2019. y,
  2020. attribs = {},
  2021. alignTo,
  2022. renderer = this.renderer,
  2023. alignedObjects = renderer.alignedObjects;
  2024. // First call on instanciate
  2025. if (alignOptions) {
  2026. this.alignOptions = alignOptions;
  2027. this.alignByTranslate = alignByTranslate;
  2028. if (!box || isString(box)) { // boxes other than renderer handle this internally
  2029. this.alignTo = alignTo = box || 'renderer';
  2030. erase(alignedObjects, this); // prevent duplicates, like legendGroup after resize
  2031. alignedObjects.push(this);
  2032. box = null; // reassign it below
  2033. }
  2034. // When called on resize, no arguments are supplied
  2035. } else {
  2036. alignOptions = this.alignOptions;
  2037. alignByTranslate = this.alignByTranslate;
  2038. alignTo = this.alignTo;
  2039. }
  2040. box = pick(box, renderer[alignTo], renderer);
  2041. // Assign variables
  2042. align = alignOptions.align;
  2043. vAlign = alignOptions.verticalAlign;
  2044. x = (box.x || 0) + (alignOptions.x || 0); // default: left align
  2045. y = (box.y || 0) + (alignOptions.y || 0); // default: top align
  2046. // Align
  2047. if (align === 'right' || align === 'center') {
  2048. x += (box.width - (alignOptions.width || 0)) /
  2049. { right: 1, center: 2 }[align];
  2050. }
  2051. attribs[alignByTranslate ? 'translateX' : 'x'] = mathRound(x);
  2052. // Vertical align
  2053. if (vAlign === 'bottom' || vAlign === 'middle') {
  2054. y += (box.height - (alignOptions.height || 0)) /
  2055. ({ bottom: 1, middle: 2 }[vAlign] || 1);
  2056. }
  2057. attribs[alignByTranslate ? 'translateY' : 'y'] = mathRound(y);
  2058. // Animate only if already placed
  2059. this[this.placed ? 'animate' : 'attr'](attribs);
  2060. this.placed = true;
  2061. this.alignAttr = attribs;
  2062. return this;
  2063. },
  2064. /**
  2065. * Get the bounding box (width, height, x and y) for the element
  2066. */
  2067. getBBox: function () {
  2068. var wrapper = this,
  2069. bBox = wrapper.bBox,
  2070. renderer = wrapper.renderer,
  2071. width,
  2072. height,
  2073. rotation = wrapper.rotation,
  2074. element = wrapper.element,
  2075. styles = wrapper.styles,
  2076. rad = rotation * deg2rad,
  2077. textStr = wrapper.textStr,
  2078. cacheKey;
  2079. // Since numbers are monospaced, and numerical labels appear a lot in a chart,
  2080. // we assume that a label of n characters has the same bounding box as others
  2081. // of the same length.
  2082. if (textStr === '' || numRegex.test(textStr)) {
  2083. cacheKey = 'num.' + textStr.toString().length + (styles ? ('|' + styles.fontSize + '|' + styles.fontFamily) : '');
  2084. } //else { // This code block made demo/waterfall fail, related to buildText
  2085. // Caching all strings reduces rendering time by 4-5%.
  2086. // TODO: Check how this affects places where bBox is found on the element
  2087. //cacheKey = textStr + (styles ? ('|' + styles.fontSize + '|' + styles.fontFamily) : '');
  2088. //}
  2089. if (cacheKey) {
  2090. bBox = renderer.cache[cacheKey];
  2091. }
  2092. // No cache found
  2093. if (!bBox) {
  2094. // SVG elements
  2095. if (element.namespaceURI === SVG_NS || renderer.forExport) {
  2096. try { // Fails in Firefox if the container has display: none.
  2097. bBox = element.getBBox ?
  2098. // SVG: use extend because IE9 is not allowed to change width and height in case
  2099. // of rotation (below)
  2100. extend({}, element.getBBox()) :
  2101. // Canvas renderer and legacy IE in export mode
  2102. {
  2103. width: element.offsetWidth,
  2104. height: element.offsetHeight
  2105. };
  2106. } catch (e) {}
  2107. // If the bBox is not set, the try-catch block above failed. The other condition
  2108. // is for Opera that returns a width of -Infinity on hidden elements.
  2109. if (!bBox || bBox.width < 0) {
  2110. bBox = { width: 0, height: 0 };
  2111. }
  2112. // VML Renderer or useHTML within SVG
  2113. } else {
  2114. bBox = wrapper.htmlGetBBox();
  2115. }
  2116. // True SVG elements as well as HTML elements in modern browsers using the .useHTML option
  2117. // need to compensated for rotation
  2118. if (renderer.isSVG) {
  2119. width = bBox.width;
  2120. height = bBox.height;
  2121. // Workaround for wrong bounding box in IE9 and IE10 (#1101, #1505, #1669, #2568)
  2122. if (isIE && styles && styles.fontSize === '11px' && height.toPrecision(3) === '16.9') {
  2123. bBox.height = height = 14;
  2124. }
  2125. // Adjust for rotated text
  2126. if (rotation) {
  2127. bBox.width = mathAbs(height * mathSin(rad)) + mathAbs(width * mathCos(rad));
  2128. bBox.height = mathAbs(height * mathCos(rad)) + mathAbs(width * mathSin(rad));
  2129. }
  2130. }
  2131. // Cache it
  2132. wrapper.bBox = bBox;
  2133. if (cacheKey) {
  2134. renderer.cache[cacheKey] = bBox;
  2135. }
  2136. }
  2137. return bBox;
  2138. },
  2139. /**
  2140. * Show the element
  2141. */
  2142. show: function (inherit) {
  2143. // IE9-11 doesn't handle visibilty:inherit well, so we remove the attribute instead (#2881)
  2144. if (inherit && this.element.namespaceURI === SVG_NS) {
  2145. this.element.removeAttribute('visibility');
  2146. return this;
  2147. } else {
  2148. return this.attr({ visibility: inherit ? 'inherit' : VISIBLE });
  2149. }
  2150. },
  2151. /**
  2152. * Hide the element
  2153. */
  2154. hide: function () {
  2155. return this.attr({ visibility: HIDDEN });
  2156. },
  2157. fadeOut: function (duration) {
  2158. var elemWrapper = this;
  2159. elemWrapper.animate({
  2160. opacity: 0
  2161. }, {
  2162. duration: duration || 150,
  2163. complete: function () {
  2164. elemWrapper.hide();
  2165. }
  2166. });
  2167. },
  2168. /**
  2169. * Add the element
  2170. * @param {Object|Undefined} parent Can be an element, an element wrapper or undefined
  2171. * to append the element to the renderer.box.
  2172. */
  2173. add: function (parent) {
  2174. var renderer = this.renderer,
  2175. parentWrapper = parent || renderer,
  2176. parentNode = parentWrapper.element || renderer.box,
  2177. childNodes,
  2178. element = this.element,
  2179. zIndex = this.zIndex,
  2180. otherElement,
  2181. otherZIndex,
  2182. i,
  2183. inserted;
  2184. if (parent) {
  2185. this.parentGroup = parent;
  2186. }
  2187. // mark as inverted
  2188. this.parentInverted = parent && parent.inverted;
  2189. // build formatted text
  2190. if (this.textStr !== undefined) {
  2191. renderer.buildText(this);
  2192. }
  2193. // mark the container as having z indexed children
  2194. if (zIndex) {
  2195. parentWrapper.handleZ = true;
  2196. zIndex = pInt(zIndex);
  2197. }
  2198. // insert according to this and other elements' zIndex
  2199. if (parentWrapper.handleZ) { // this element or any of its siblings has a z index
  2200. childNodes = parentNode.childNodes;
  2201. for (i = 0; i < childNodes.length; i++) {
  2202. otherElement = childNodes[i];
  2203. otherZIndex = attr(otherElement, 'zIndex');
  2204. if (otherElement !== element && (
  2205. // insert before the first element with a higher zIndex
  2206. pInt(otherZIndex) > zIndex ||
  2207. // if no zIndex given, insert before the first element with a zIndex
  2208. (!defined(zIndex) && defined(otherZIndex))
  2209. )) {
  2210. parentNode.insertBefore(element, otherElement);
  2211. inserted = true;
  2212. break;
  2213. }
  2214. }
  2215. }
  2216. // default: append at the end
  2217. if (!inserted) {
  2218. parentNode.appendChild(element);
  2219. }
  2220. // mark as added
  2221. this.added = true;
  2222. // fire an event for internal hooks
  2223. if (this.onAdd) {
  2224. this.onAdd();
  2225. }
  2226. return this;
  2227. },
  2228. /**
  2229. * Removes a child either by removeChild or move to garbageBin.
  2230. * Issue 490; in VML removeChild results in Orphaned nodes according to sIEve, discardElement does not.
  2231. */
  2232. safeRemoveChild: function (element) {
  2233. var parentNode = element.parentNode;
  2234. if (parentNode) {
  2235. parentNode.removeChild(element);
  2236. }
  2237. },
  2238. /**
  2239. * Destroy the element and element wrapper
  2240. */
  2241. destroy: function () {
  2242. var wrapper = this,
  2243. element = wrapper.element || {},
  2244. shadows = wrapper.shadows,
  2245. parentToClean = wrapper.renderer.isSVG && element.nodeName === 'SPAN' && wrapper.parentGroup,
  2246. grandParent,
  2247. key,
  2248. i;
  2249. // remove events
  2250. element.onclick = element.onmouseout = element.onmouseover = element.onmousemove = element.point = null;
  2251. stop(wrapper); // stop running animations
  2252. if (wrapper.clipPath) {
  2253. wrapper.clipPath = wrapper.clipPath.destroy();
  2254. }
  2255. // Destroy stops in case this is a gradient object
  2256. if (wrapper.stops) {
  2257. for (i = 0; i < wrapper.stops.length; i++) {
  2258. wrapper.stops[i] = wrapper.stops[i].destroy();
  2259. }
  2260. wrapper.stops = null;
  2261. }
  2262. // remove element
  2263. wrapper.safeRemoveChild(element);
  2264. // destroy shadows
  2265. if (shadows) {
  2266. each(shadows, function (shadow) {
  2267. wrapper.safeRemoveChild(shadow);
  2268. });
  2269. }
  2270. // In case of useHTML, clean up empty containers emulating SVG groups (#1960, #2393, #2697).
  2271. while (parentToClean && parentToClean.div && parentToClean.div.childNodes.length === 0) {
  2272. grandParent = parentToClean.parentGroup;
  2273. wrapper.safeRemoveChild(parentToClean.div);
  2274. delete parentToClean.div;
  2275. parentToClean = grandParent;
  2276. }
  2277. // remove from alignObjects
  2278. if (wrapper.alignTo) {
  2279. erase(wrapper.renderer.alignedObjects, wrapper);
  2280. }
  2281. for (key in wrapper) {
  2282. delete wrapper[key];
  2283. }
  2284. return null;
  2285. },
  2286. /**
  2287. * Add a shadow to the element. Must be done after the element is added to the DOM
  2288. * @param {Boolean|Object} shadowOptions
  2289. */
  2290. shadow: function (shadowOptions, group, cutOff) {
  2291. var shadows = [],
  2292. i,
  2293. shadow,
  2294. element = this.element,
  2295. strokeWidth,
  2296. shadowWidth,
  2297. shadowElementOpacity,
  2298. // compensate for inverted plot area
  2299. transform;
  2300. if (shadowOptions) {
  2301. shadowWidth = pick(shadowOptions.width, 3);
  2302. shadowElementOpacity = (shadowOptions.opacity || 0.15) / shadowWidth;
  2303. transform = this.parentInverted ?
  2304. '(-1,-1)' :
  2305. '(' + pick(shadowOptions.offsetX, 1) + ', ' + pick(shadowOptions.offsetY, 1) + ')';
  2306. for (i = 1; i <= shadowWidth; i++) {
  2307. shadow = element.cloneNode(0);
  2308. strokeWidth = (shadowWidth * 2) + 1 - (2 * i);
  2309. attr(shadow, {
  2310. 'isShadow': 'true',
  2311. 'stroke': shadowOptions.color || 'black',
  2312. 'stroke-opacity': shadowElementOpacity * i,
  2313. 'stroke-width': strokeWidth,
  2314. 'transform': 'translate' + transform,
  2315. 'fill': NONE
  2316. });
  2317. if (cutOff) {
  2318. attr(shadow, 'height', mathMax(attr(shadow, 'height') - strokeWidth, 0));
  2319. shadow.cutHeight = strokeWidth;
  2320. }
  2321. if (group) {
  2322. group.element.appendChild(shadow);
  2323. } else {
  2324. element.parentNode.insertBefore(shadow, element);
  2325. }
  2326. shadows.push(shadow);
  2327. }
  2328. this.shadows = shadows;
  2329. }
  2330. return this;
  2331. },
  2332. xGetter: function (key) {
  2333. if (this.element.nodeName === 'circle') {
  2334. key = { x: 'cx', y: 'cy' }[key] || key;
  2335. }
  2336. return this._defaultGetter(key);
  2337. },
  2338. /**
  2339. * Get the current value of an attribute or pseudo attribute, used mainly
  2340. * for animation.
  2341. */
  2342. _defaultGetter: function (key) {
  2343. var ret = pick(this[key], this.element ? this.element.getAttribute(key) : null, 0);
  2344. if (/^[\-0-9\.]+$/.test(ret)) { // is numerical
  2345. ret = parseFloat(ret);
  2346. }
  2347. return ret;
  2348. },
  2349. dSetter: function (value, key, element) {
  2350. if (value && value.join) { // join path
  2351. value = value.join(' ');
  2352. }
  2353. if (/(NaN| {2}|^$)/.test(value)) {
  2354. value = 'M 0 0';
  2355. }
  2356. element.setAttribute(key, value);
  2357. this[key] = value;
  2358. },
  2359. dashstyleSetter: function (value) {
  2360. var i;
  2361. value = value && value.toLowerCase();
  2362. if (value) {
  2363. value = value
  2364. .replace('shortdashdotdot', '3,1,1,1,1,1,')
  2365. .replace('shortdashdot', '3,1,1,1')
  2366. .replace('shortdot', '1,1,')
  2367. .replace('shortdash', '3,1,')
  2368. .replace('longdash', '8,3,')
  2369. .replace(/dot/g, '1,3,')
  2370. .replace('dash', '4,3,')
  2371. .replace(/,$/, '')
  2372. .replace('solid', 1)
  2373. .split(','); // ending comma
  2374. i = value.length;
  2375. while (i--) {
  2376. value[i] = pInt(value[i]) * this['stroke-width'];
  2377. }
  2378. value = value.join(',');
  2379. this.element.setAttribute('stroke-dasharray', value);
  2380. }
  2381. },
  2382. alignSetter: function (value) {
  2383. this.element.setAttribute('text-anchor', { left: 'start', center: 'middle', right: 'end' }[value]);
  2384. },
  2385. opacitySetter: function (value, key, element) {
  2386. this[key] = value;
  2387. element.setAttribute(key, value);
  2388. },
  2389. titleSetter: function (value) {
  2390. var titleNode = this.element.getElementsByTagName('title')[0];
  2391. if (!titleNode) {
  2392. titleNode = doc.createElementNS(SVG_NS, 'title');
  2393. this.element.appendChild(titleNode);
  2394. }
  2395. titleNode.textContent = value;
  2396. },
  2397. textSetter: function (value) {
  2398. if (value !== this.textStr) {
  2399. // Delete bBox memo when the text changes
  2400. delete this.bBox;
  2401. this.textStr = value;
  2402. if (this.added) {
  2403. this.renderer.buildText(this);
  2404. }
  2405. }
  2406. },
  2407. fillSetter: function (value, key, element) {
  2408. if (typeof value === 'string') {
  2409. element.setAttribute(key, value);
  2410. } else if (value) {
  2411. this.colorGradient(value, key, element);
  2412. }
  2413. },
  2414. zIndexSetter: function (value, key, element) {
  2415. element.setAttribute(key, value);
  2416. this[key] = value;
  2417. },
  2418. _defaultSetter: function (value, key, element) {
  2419. element.setAttribute(key, value);
  2420. }
  2421. };
  2422. // Some shared setters and getters
  2423. SVGElement.prototype.yGetter = SVGElement.prototype.xGetter;
  2424. SVGElement.prototype.translateXSetter = SVGElement.prototype.translateYSetter =
  2425. SVGElement.prototype.rotationSetter = SVGElement.prototype.verticalAlignSetter =
  2426. SVGElement.prototype.scaleXSetter = SVGElement.prototype.scaleYSetter = function (value, key) {
  2427. this[key] = value;
  2428. this.doTransform = true;
  2429. };
  2430. // WebKit and Batik have problems with a stroke-width of zero, so in this case we remove the
  2431. // stroke attribute altogether. #1270, #1369, #3065, #3072.
  2432. SVGElement.prototype['stroke-widthSetter'] = SVGElement.prototype.strokeSetter = function (value, key, element) {
  2433. this[key] = value;
  2434. // Only apply the stroke attribute if the stroke width is defined and larger than 0
  2435. if (this.stroke && this['stroke-width']) {
  2436. this.strokeWidth = this['stroke-width'];
  2437. SVGElement.prototype.fillSetter.call(this, this.stroke, 'stroke', element); // use prototype as instance may be overridden
  2438. element.setAttribute('stroke-width', this['stroke-width']);
  2439. this.hasStroke = true;
  2440. } else if (key === 'stroke-width' && value === 0 && this.hasStroke) {
  2441. element.removeAttribute('stroke');
  2442. this.hasStroke = false;
  2443. }
  2444. };
  2445. /**
  2446. * The default SVG renderer
  2447. */
  2448. var SVGRenderer = function () {
  2449. this.init.apply(this, arguments);
  2450. };
  2451. SVGRenderer.prototype = {
  2452. Element: SVGElement,
  2453. /**
  2454. * Initialize the SVGRenderer
  2455. * @param {Object} container
  2456. * @param {Number} width
  2457. * @param {Number} height
  2458. * @param {Boolean} forExport
  2459. */
  2460. init: function (container, width, height, style, forExport) {
  2461. var renderer = this,
  2462. loc = location,
  2463. boxWrapper,
  2464. element,
  2465. desc;
  2466. boxWrapper = renderer.createElement('svg')
  2467. .attr({
  2468. version: '1.1'
  2469. })
  2470. .css(this.getStyle(style));
  2471. element = boxWrapper.element;
  2472. container.appendChild(element);
  2473. // For browsers other than IE, add the namespace attribute (#1978)
  2474. if (container.innerHTML.indexOf('xmlns') === -1) {
  2475. attr(element, 'xmlns', SVG_NS);
  2476. }
  2477. // object properties
  2478. renderer.isSVG = true;
  2479. renderer.box = element;
  2480. renderer.boxWrapper = boxWrapper;
  2481. renderer.alignedObjects = [];
  2482. // Page url used for internal references. #24, #672, #1070
  2483. renderer.url = (isFirefox || isWebKit) && doc.getElementsByTagName('base').length ?
  2484. loc.href
  2485. .replace(/#.*?$/, '') // remove the hash
  2486. .replace(/([\('\)])/g, '\\$1') // escape parantheses and quotes
  2487. .replace(/ /g, '%20') : // replace spaces (needed for Safari only)
  2488. '';
  2489. // Add description
  2490. desc = this.createElement('desc').add();
  2491. desc.element.appendChild(doc.createTextNode('Created with ' + PRODUCT + ' ' + VERSION));
  2492. renderer.defs = this.createElement('defs').add();
  2493. renderer.forExport = forExport;
  2494. renderer.gradients = {}; // Object where gradient SvgElements are stored
  2495. renderer.cache = {}; // Cache for numerical bounding boxes
  2496. renderer.setSize(width, height, false);
  2497. // Issue 110 workaround:
  2498. // In Firefox, if a div is positioned by percentage, its pixel position may land
  2499. // between pixels. The container itself doesn't display this, but an SVG element
  2500. // inside this container will be drawn at subpixel precision. In order to draw
  2501. // sharp lines, this must be compensated for. This doesn't seem to work inside
  2502. // iframes though (like in jsFiddle).
  2503. var subPixelFix, rect;
  2504. if (isFirefox && container.getBoundingClientRect) {
  2505. renderer.subPixelFix = subPixelFix = function () {
  2506. css(container, { left: 0, top: 0 });
  2507. rect = container.getBoundingClientRect();
  2508. css(container, {
  2509. left: (mathCeil(rect.left) - rect.left) + PX,
  2510. top: (mathCeil(rect.top) - rect.top) + PX
  2511. });
  2512. };
  2513. // run the fix now
  2514. subPixelFix();
  2515. // run it on resize
  2516. addEvent(win, 'resize', subPixelFix);
  2517. }
  2518. },
  2519. getStyle: function (style) {
  2520. return (this.style = extend({
  2521. fontFamily: '"Lucida Grande", "Lucida Sans Unicode", Arial, Helvetica, sans-serif', // default font
  2522. fontSize: '12px'
  2523. }, style));
  2524. },
  2525. /**
  2526. * Detect whether the renderer is hidden. This happens when one of the parent elements
  2527. * has display: none. #608.
  2528. */
  2529. isHidden: function () {
  2530. return !this.boxWrapper.getBBox().width;
  2531. },
  2532. /**
  2533. * Destroys the renderer and its allocated members.
  2534. */
  2535. destroy: function () {
  2536. var renderer = this,
  2537. rendererDefs = renderer.defs;
  2538. renderer.box = null;
  2539. renderer.boxWrapper = renderer.boxWrapper.destroy();
  2540. // Call destroy on all gradient elements
  2541. destroyObjectProperties(renderer.gradients || {});
  2542. renderer.gradients = null;
  2543. // Defs are null in VMLRenderer
  2544. // Otherwise, destroy them here.
  2545. if (rendererDefs) {
  2546. renderer.defs = rendererDefs.destroy();
  2547. }
  2548. // Remove sub pixel fix handler
  2549. // We need to check that there is a handler, otherwise all functions that are registered for event 'resize' are removed
  2550. // See issue #982
  2551. if (renderer.subPixelFix) {
  2552. removeEvent(win, 'resize', renderer.subPixelFix);
  2553. }
  2554. renderer.alignedObjects = null;
  2555. return null;
  2556. },
  2557. /**
  2558. * Create a wrapper for an SVG element
  2559. * @param {Object} nodeName
  2560. */
  2561. createElement: function (nodeName) {
  2562. var wrapper = new this.Element();
  2563. wrapper.init(this, nodeName);
  2564. return wrapper;
  2565. },
  2566. /**
  2567. * Dummy function for use in canvas renderer
  2568. */
  2569. draw: function () {},
  2570. /**
  2571. * Parse a simple HTML string into SVG tspans
  2572. *
  2573. * @param {Object} textNode The parent text SVG node
  2574. */
  2575. buildText: function (wrapper) {
  2576. var textNode = wrapper.element,
  2577. renderer = this,
  2578. forExport = renderer.forExport,
  2579. textStr = pick(wrapper.textStr, '').toString(),
  2580. hasMarkup = textStr.indexOf('<') !== -1,
  2581. lines,
  2582. childNodes = textNode.childNodes,
  2583. styleRegex,
  2584. hrefRegex,
  2585. parentX = attr(textNode, 'x'),
  2586. textStyles = wrapper.styles,
  2587. width = wrapper.textWidth,
  2588. textLineHeight = textStyles && textStyles.lineHeight,
  2589. textStroke = textStyles && textStyles.HcTextStroke,
  2590. i = childNodes.length,
  2591. getLineHeight = function (tspan) {
  2592. return textLineHeight ?
  2593. pInt(textLineHeight) :
  2594. renderer.fontMetrics(
  2595. /(px|em)$/.test(tspan && tspan.style.fontSize) ?
  2596. tspan.style.fontSize :
  2597. ((textStyles && textStyles.fontSize) || renderer.style.fontSize || 12),
  2598. tspan
  2599. ).h;
  2600. };
  2601. /// remove old text
  2602. while (i--) {
  2603. textNode.removeChild(childNodes[i]);
  2604. }
  2605. // Skip tspans, add text directly to text node. The forceTSpan is a hook
  2606. // used in text outline hack.
  2607. if (!hasMarkup && !textStroke && textStr.indexOf(' ') === -1) {
  2608. textNode.appendChild(doc.createTextNode(textStr));
  2609. return;
  2610. // Complex strings, add more logic
  2611. } else {
  2612. styleRegex = /<.*style="([^"]+)".*>/;
  2613. hrefRegex = /<.*href="(http[^"]+)".*>/;
  2614. if (width && !wrapper.added) {
  2615. this.box.appendChild(textNode); // attach it to the DOM to read offset width
  2616. }
  2617. if (hasMarkup) {
  2618. lines = textStr
  2619. .replace(/<(b|strong)>/g, '<span style="font-weight:bold">')
  2620. .replace(/<(i|em)>/g, '<span style="font-style:italic">')
  2621. .replace(/<a/g, '<span')
  2622. .replace(/<\/(b|strong|i|em|a)>/g, '</span>')
  2623. .split(/<br.*?>/g);
  2624. } else {
  2625. lines = [textStr];
  2626. }
  2627. // remove empty line at end
  2628. if (lines[lines.length - 1] === '') {
  2629. lines.pop();
  2630. }
  2631. // build the lines
  2632. each(lines, function (line, lineNo) {
  2633. var spans, spanNo = 0;
  2634. line = line.replace(/<span/g, '|||<span').replace(/<\/span>/g, '</span>|||');
  2635. spans = line.split('|||');
  2636. each(spans, function (span) {
  2637. if (span !== '' || spans.length === 1) {
  2638. var attributes = {},
  2639. tspan = doc.createElementNS(SVG_NS, 'tspan'),
  2640. spanStyle; // #390
  2641. if (styleRegex.test(span)) {
  2642. spanStyle = span.match(styleRegex)[1].replace(/(;| |^)color([ :])/, '$1fill$2');
  2643. attr(tspan, 'style', spanStyle);
  2644. }
  2645. if (hrefRegex.test(span) && !forExport) { // Not for export - #1529
  2646. attr(tspan, 'onclick', 'location.href=\"' + span.match(hrefRegex)[1] + '\"');
  2647. css(tspan, { cursor: 'pointer' });
  2648. }
  2649. span = (span.replace(/<(.|\n)*?>/g, '') || ' ')
  2650. .replace(/&lt;/g, '<')
  2651. .replace(/&gt;/g, '>');
  2652. // Nested tags aren't supported, and cause crash in Safari (#1596)
  2653. if (span !== ' ') {
  2654. // add the text node
  2655. tspan.appendChild(doc.createTextNode(span));
  2656. if (!spanNo) { // first span in a line, align it to the left
  2657. if (lineNo && parentX !== null) {
  2658. attributes.x = parentX;
  2659. }
  2660. } else {
  2661. attributes.dx = 0; // #16
  2662. }
  2663. // add attributes
  2664. attr(tspan, attributes);
  2665. // Append it
  2666. textNode.appendChild(tspan);
  2667. // first span on subsequent line, add the line height
  2668. if (!spanNo && lineNo) {
  2669. // allow getting the right offset height in exporting in IE
  2670. if (!hasSVG && forExport) {
  2671. css(tspan, { display: 'block' });
  2672. }
  2673. // Set the line height based on the font size of either
  2674. // the text element or the tspan element
  2675. attr(
  2676. tspan,
  2677. 'dy',
  2678. getLineHeight(tspan)
  2679. );
  2680. }
  2681. // check width and apply soft breaks
  2682. if (width) {
  2683. var words = span.replace(/([^\^])-/g, '$1- ').split(' '), // #1273
  2684. hasWhiteSpace = spans.length > 1 || (words.length > 1 && textStyles.whiteSpace !== 'nowrap'),
  2685. tooLong,
  2686. actualWidth,
  2687. hcHeight = textStyles.HcHeight,
  2688. rest = [],
  2689. dy = getLineHeight(tspan),
  2690. softLineNo = 1,
  2691. bBox;
  2692. while (hasWhiteSpace && (words.length || rest.length)) {
  2693. delete wrapper.bBox; // delete cache
  2694. bBox = wrapper.getBBox();
  2695. actualWidth = bBox.width;
  2696. // Old IE cannot measure the actualWidth for SVG elements (#2314)
  2697. if (!hasSVG && renderer.forExport) {
  2698. actualWidth = renderer.measureSpanWidth(tspan.firstChild.data, wrapper.styles);
  2699. }
  2700. tooLong = actualWidth > width;
  2701. if (!tooLong || words.length === 1) { // new line needed
  2702. words = rest;
  2703. rest = [];
  2704. if (words.length) {
  2705. softLineNo++;
  2706. if (hcHeight && softLineNo * dy > hcHeight) {
  2707. words = ['...'];
  2708. wrapper.attr('title', wrapper.textStr);
  2709. } else {
  2710. tspan = doc.createElementNS(SVG_NS, 'tspan');
  2711. attr(tspan, {
  2712. dy: dy,
  2713. x: parentX
  2714. });
  2715. if (spanStyle) { // #390
  2716. attr(tspan, 'style', spanStyle);
  2717. }
  2718. textNode.appendChild(tspan);
  2719. }
  2720. }
  2721. if (actualWidth > width) { // a single word is pressing it out
  2722. width = actualWidth;
  2723. }
  2724. } else { // append to existing line tspan
  2725. tspan.removeChild(tspan.firstChild);
  2726. rest.unshift(words.pop());
  2727. }
  2728. if (words.length) {
  2729. tspan.appendChild(doc.createTextNode(words.join(' ').replace(/- /g, '-')));
  2730. }
  2731. }
  2732. }
  2733. spanNo++;
  2734. }
  2735. }
  2736. });
  2737. });
  2738. }
  2739. },
  2740. /**
  2741. * Create a button with preset states
  2742. * @param {String} text
  2743. * @param {Number} x
  2744. * @param {Number} y
  2745. * @param {Function} callback
  2746. * @param {Object} normalState
  2747. * @param {Object} hoverState
  2748. * @param {Object} pressedState
  2749. */
  2750. button: function (text, x, y, callback, normalState, hoverState, pressedState, disabledState, shape) {
  2751. var label = this.label(text, x, y, shape, null, null, null, null, 'button'),
  2752. curState = 0,
  2753. stateOptions,
  2754. stateStyle,
  2755. normalStyle,
  2756. hoverStyle,
  2757. pressedStyle,
  2758. disabledStyle,
  2759. verticalGradient = { x1: 0, y1: 0, x2: 0, y2: 1 };
  2760. // Normal state - prepare the attributes
  2761. normalState = merge({
  2762. 'stroke-width': 1,
  2763. stroke: '#CCCCCC',
  2764. fill: {
  2765. linearGradient: verticalGradient,
  2766. stops: [
  2767. [0, '#FEFEFE'],
  2768. [1, '#F6F6F6']
  2769. ]
  2770. },
  2771. r: 2,
  2772. padding: 5,
  2773. style: {
  2774. color: 'black'
  2775. }
  2776. }, normalState);
  2777. normalStyle = normalState.style;
  2778. delete normalState.style;
  2779. // Hover state
  2780. hoverState = merge(normalState, {
  2781. stroke: '#68A',
  2782. fill: {
  2783. linearGradient: verticalGradient,
  2784. stops: [
  2785. [0, '#FFF'],
  2786. [1, '#ACF']
  2787. ]
  2788. }
  2789. }, hoverState);
  2790. hoverStyle = hoverState.style;
  2791. delete hoverState.style;
  2792. // Pressed state
  2793. pressedState = merge(normalState, {
  2794. stroke: '#68A',
  2795. fill: {
  2796. linearGradient: verticalGradient,
  2797. stops: [
  2798. [0, '#9BD'],
  2799. [1, '#CDF']
  2800. ]
  2801. }
  2802. }, pressedState);
  2803. pressedStyle = pressedState.style;
  2804. delete pressedState.style;
  2805. // Disabled state
  2806. disabledState = merge(normalState, {
  2807. style: {
  2808. color: '#CCC'
  2809. }
  2810. }, disabledState);
  2811. disabledStyle = disabledState.style;
  2812. delete disabledState.style;
  2813. // Add the events. IE9 and IE10 need mouseover and mouseout to funciton (#667).
  2814. addEvent(label.element, isIE ? 'mouseover' : 'mouseenter', function () {
  2815. if (curState !== 3) {
  2816. label.attr(hoverState)
  2817. .css(hoverStyle);
  2818. }
  2819. });
  2820. addEvent(label.element, isIE ? 'mouseout' : 'mouseleave', function () {
  2821. if (curState !== 3) {
  2822. stateOptions = [normalState, hoverState, pressedState][curState];
  2823. stateStyle = [normalStyle, hoverStyle, pressedStyle][curState];
  2824. label.attr(stateOptions)
  2825. .css(stateStyle);
  2826. }
  2827. });
  2828. label.setState = function (state) {
  2829. label.state = curState = state;
  2830. if (!state) {
  2831. label.attr(normalState)
  2832. .css(normalStyle);
  2833. } else if (state === 2) {
  2834. label.attr(pressedState)
  2835. .css(pressedStyle);
  2836. } else if (state === 3) {
  2837. label.attr(disabledState)
  2838. .css(disabledStyle);
  2839. }
  2840. };
  2841. return label
  2842. .on('click', function () {
  2843. if (curState !== 3) {
  2844. callback.call(label);
  2845. }
  2846. })
  2847. .attr(normalState)
  2848. .css(extend({ cursor: 'default' }, normalStyle));
  2849. },
  2850. /**
  2851. * Make a straight line crisper by not spilling out to neighbour pixels
  2852. * @param {Array} points
  2853. * @param {Number} width
  2854. */
  2855. crispLine: function (points, width) {
  2856. // points format: [M, 0, 0, L, 100, 0]
  2857. // normalize to a crisp line
  2858. if (points[1] === points[4]) {
  2859. // Substract due to #1129. Now bottom and left axis gridlines behave the same.
  2860. points[1] = points[4] = mathRound(points[1]) - (width % 2 / 2);
  2861. }
  2862. if (points[2] === points[5]) {
  2863. points[2] = points[5] = mathRound(points[2]) + (width % 2 / 2);
  2864. }
  2865. return points;
  2866. },
  2867. /**
  2868. * Draw a path
  2869. * @param {Array} path An SVG path in array form
  2870. */
  2871. path: function (path) {
  2872. var attr = {
  2873. fill: NONE
  2874. };
  2875. if (isArray(path)) {
  2876. attr.d = path;
  2877. } else if (isObject(path)) { // attributes
  2878. extend(attr, path);
  2879. }
  2880. return this.createElement('path').attr(attr);
  2881. },
  2882. /**
  2883. * Draw and return an SVG circle
  2884. * @param {Number} x The x position
  2885. * @param {Number} y The y position
  2886. * @param {Number} r The radius
  2887. */
  2888. circle: function (x, y, r) {
  2889. var attr = isObject(x) ?
  2890. x :
  2891. {
  2892. x: x,
  2893. y: y,
  2894. r: r
  2895. },
  2896. wrapper = this.createElement('circle');
  2897. wrapper.xSetter = function (value) {
  2898. this.element.setAttribute('cx', value);
  2899. };
  2900. wrapper.ySetter = function (value) {
  2901. this.element.setAttribute('cy', value);
  2902. };
  2903. return wrapper.attr(attr);
  2904. },
  2905. /**
  2906. * Draw and return an arc
  2907. * @param {Number} x X position
  2908. * @param {Number} y Y position
  2909. * @param {Number} r Radius
  2910. * @param {Number} innerR Inner radius like used in donut charts
  2911. * @param {Number} start Starting angle
  2912. * @param {Number} end Ending angle
  2913. */
  2914. arc: function (x, y, r, innerR, start, end) {
  2915. var arc;
  2916. if (isObject(x)) {
  2917. y = x.y;
  2918. r = x.r;
  2919. innerR = x.innerR;
  2920. start = x.start;
  2921. end = x.end;
  2922. x = x.x;
  2923. }
  2924. // Arcs are defined as symbols for the ability to set
  2925. // attributes in attr and animate
  2926. arc = this.symbol('arc', x || 0, y || 0, r || 0, r || 0, {
  2927. innerR: innerR || 0,
  2928. start: start || 0,
  2929. end: end || 0
  2930. });
  2931. arc.r = r; // #959
  2932. return arc;
  2933. },
  2934. /**
  2935. * Draw and return a rectangle
  2936. * @param {Number} x Left position
  2937. * @param {Number} y Top position
  2938. * @param {Number} width
  2939. * @param {Number} height
  2940. * @param {Number} r Border corner radius
  2941. * @param {Number} strokeWidth A stroke width can be supplied to allow crisp drawing
  2942. */
  2943. rect: function (x, y, width, height, r, strokeWidth) {
  2944. r = isObject(x) ? x.r : r;
  2945. var wrapper = this.createElement('rect'),
  2946. attribs = isObject(x) ? x : x === UNDEFINED ? {} : {
  2947. x: x,
  2948. y: y,
  2949. width: mathMax(width, 0),
  2950. height: mathMax(height, 0)
  2951. };
  2952. if (strokeWidth !== UNDEFINED) {
  2953. attribs.strokeWidth = strokeWidth;
  2954. attribs = wrapper.crisp(attribs);
  2955. }
  2956. if (r) {
  2957. attribs.r = r;
  2958. }
  2959. wrapper.rSetter = function (value) {
  2960. attr(this.element, {
  2961. rx: value,
  2962. ry: value
  2963. });
  2964. };
  2965. return wrapper.attr(attribs);
  2966. },
  2967. /**
  2968. * Resize the box and re-align all aligned elements
  2969. * @param {Object} width
  2970. * @param {Object} height
  2971. * @param {Boolean} animate
  2972. *
  2973. */
  2974. setSize: function (width, height, animate) {
  2975. var renderer = this,
  2976. alignedObjects = renderer.alignedObjects,
  2977. i = alignedObjects.length;
  2978. renderer.width = width;
  2979. renderer.height = height;
  2980. renderer.boxWrapper[pick(animate, true) ? 'animate' : 'attr']({
  2981. width: width,
  2982. height: height
  2983. });
  2984. while (i--) {
  2985. alignedObjects[i].align();
  2986. }
  2987. },
  2988. /**
  2989. * Create a group
  2990. * @param {String} name The group will be given a class name of 'highcharts-{name}'.
  2991. * This can be used for styling and scripting.
  2992. */
  2993. g: function (name) {
  2994. var elem = this.createElement('g');
  2995. return defined(name) ? elem.attr({ 'class': PREFIX + name }) : elem;
  2996. },
  2997. /**
  2998. * Display an image
  2999. * @param {String} src
  3000. * @param {Number} x
  3001. * @param {Number} y
  3002. * @param {Number} width
  3003. * @param {Number} height
  3004. */
  3005. image: function (src, x, y, width, height) {
  3006. var attribs = {
  3007. preserveAspectRatio: NONE
  3008. },
  3009. elemWrapper;
  3010. // optional properties
  3011. if (arguments.length > 1) {
  3012. extend(attribs, {
  3013. x: x,
  3014. y: y,
  3015. width: width,
  3016. height: height
  3017. });
  3018. }
  3019. elemWrapper = this.createElement('image').attr(attribs);
  3020. // set the href in the xlink namespace
  3021. if (elemWrapper.element.setAttributeNS) {
  3022. elemWrapper.element.setAttributeNS('http://www.w3.org/1999/xlink',
  3023. 'href', src);
  3024. } else {
  3025. // could be exporting in IE
  3026. // using href throws "not supported" in ie7 and under, requries regex shim to fix later
  3027. elemWrapper.element.setAttribute('hc-svg-href', src);
  3028. }
  3029. return elemWrapper;
  3030. },
  3031. /**
  3032. * Draw a symbol out of pre-defined shape paths from the namespace 'symbol' object.
  3033. *
  3034. * @param {Object} symbol
  3035. * @param {Object} x
  3036. * @param {Object} y
  3037. * @param {Object} radius
  3038. * @param {Object} options
  3039. */
  3040. symbol: function (symbol, x, y, width, height, options) {
  3041. var obj,
  3042. // get the symbol definition function
  3043. symbolFn = this.symbols[symbol],
  3044. // check if there's a path defined for this symbol
  3045. path = symbolFn && symbolFn(
  3046. mathRound(x),
  3047. mathRound(y),
  3048. width,
  3049. height,
  3050. options
  3051. ),
  3052. imageElement,
  3053. imageRegex = /^url\((.*?)\)$/,
  3054. imageSrc,
  3055. imageSize,
  3056. centerImage;
  3057. if (path) {
  3058. obj = this.path(path);
  3059. // expando properties for use in animate and attr
  3060. extend(obj, {
  3061. symbolName: symbol,
  3062. x: x,
  3063. y: y,
  3064. width: width,
  3065. height: height
  3066. });
  3067. if (options) {
  3068. extend(obj, options);
  3069. }
  3070. // image symbols
  3071. } else if (imageRegex.test(symbol)) {
  3072. // On image load, set the size and position
  3073. centerImage = function (img, size) {
  3074. if (img.element) { // it may be destroyed in the meantime (#1390)
  3075. img.attr({
  3076. width: size[0],
  3077. height: size[1]
  3078. });
  3079. if (!img.alignByTranslate) { // #185
  3080. img.translate(
  3081. mathRound((width - size[0]) / 2), // #1378
  3082. mathRound((height - size[1]) / 2)
  3083. );
  3084. }
  3085. }
  3086. };
  3087. imageSrc = symbol.match(imageRegex)[1];
  3088. imageSize = symbolSizes[imageSrc];
  3089. // Ireate the image synchronously, add attribs async
  3090. obj = this.image(imageSrc)
  3091. .attr({
  3092. x: x,
  3093. y: y
  3094. });
  3095. obj.isImg = true;
  3096. if (imageSize) {
  3097. centerImage(obj, imageSize);
  3098. } else {
  3099. // Initialize image to be 0 size so export will still function if there's no cached sizes.
  3100. //
  3101. obj.attr({ width: 0, height: 0 });
  3102. // Create a dummy JavaScript image to get the width and height. Due to a bug in IE < 8,
  3103. // the created element must be assigned to a variable in order to load (#292).
  3104. imageElement = createElement('img', {
  3105. onload: function () {
  3106. centerImage(obj, symbolSizes[imageSrc] = [this.width, this.height]);
  3107. },
  3108. src: imageSrc
  3109. });
  3110. }
  3111. }
  3112. return obj;
  3113. },
  3114. /**
  3115. * An extendable collection of functions for defining symbol paths.
  3116. */
  3117. symbols: {
  3118. 'circle': function (x, y, w, h) {
  3119. var cpw = 0.166 * w;
  3120. return [
  3121. M, x + w / 2, y,
  3122. 'C', x + w + cpw, y, x + w + cpw, y + h, x + w / 2, y + h,
  3123. 'C', x - cpw, y + h, x - cpw, y, x + w / 2, y,
  3124. 'Z'
  3125. ];
  3126. },
  3127. 'square': function (x, y, w, h) {
  3128. return [
  3129. M, x, y,
  3130. L, x + w, y,
  3131. x + w, y + h,
  3132. x, y + h,
  3133. 'Z'
  3134. ];
  3135. },
  3136. 'triangle': function (x, y, w, h) {
  3137. return [
  3138. M, x + w / 2, y,
  3139. L, x + w, y + h,
  3140. x, y + h,
  3141. 'Z'
  3142. ];
  3143. },
  3144. 'triangle-down': function (x, y, w, h) {
  3145. return [
  3146. M, x, y,
  3147. L, x + w, y,
  3148. x + w / 2, y + h,
  3149. 'Z'
  3150. ];
  3151. },
  3152. 'diamond': function (x, y, w, h) {
  3153. return [
  3154. M, x + w / 2, y,
  3155. L, x + w, y + h / 2,
  3156. x + w / 2, y + h,
  3157. x, y + h / 2,
  3158. 'Z'
  3159. ];
  3160. },
  3161. 'arc': function (x, y, w, h, options) {
  3162. var start = options.start,
  3163. radius = options.r || w || h,
  3164. end = options.end - 0.001, // to prevent cos and sin of start and end from becoming equal on 360 arcs (related: #1561)
  3165. innerRadius = options.innerR,
  3166. open = options.open,
  3167. cosStart = mathCos(start),
  3168. sinStart = mathSin(start),
  3169. cosEnd = mathCos(end),
  3170. sinEnd = mathSin(end),
  3171. longArc = options.end - start < mathPI ? 0 : 1;
  3172. return [
  3173. M,
  3174. x + radius * cosStart,
  3175. y + radius * sinStart,
  3176. 'A', // arcTo
  3177. radius, // x radius
  3178. radius, // y radius
  3179. 0, // slanting
  3180. longArc, // long or short arc
  3181. 1, // clockwise
  3182. x + radius * cosEnd,
  3183. y + radius * sinEnd,
  3184. open ? M : L,
  3185. x + innerRadius * cosEnd,
  3186. y + innerRadius * sinEnd,
  3187. 'A', // arcTo
  3188. innerRadius, // x radius
  3189. innerRadius, // y radius
  3190. 0, // slanting
  3191. longArc, // long or short arc
  3192. 0, // clockwise
  3193. x + innerRadius * cosStart,
  3194. y + innerRadius * sinStart,
  3195. open ? '' : 'Z' // close
  3196. ];
  3197. },
  3198. /**
  3199. * Callout shape used for default tooltips, also used for rounded rectangles in VML
  3200. */
  3201. callout: function (x, y, w, h, options) {
  3202. var arrowLength = 6,
  3203. halfDistance = 6,
  3204. r = mathMin((options && options.r) || 0, w, h),
  3205. safeDistance = r + halfDistance,
  3206. anchorX = options && options.anchorX,
  3207. anchorY = options && options.anchorY,
  3208. path,
  3209. normalizer = mathRound(options.strokeWidth || 0) % 2 / 2; // mathRound because strokeWidth can sometimes have roundoff errors;
  3210. x += normalizer;
  3211. y += normalizer;
  3212. path = [
  3213. 'M', x + r, y,
  3214. 'L', x + w - r, y, // top side
  3215. 'C', x + w, y, x + w, y, x + w, y + r, // top-right corner
  3216. 'L', x + w, y + h - r, // right side
  3217. 'C', x + w, y + h, x + w, y + h, x + w - r, y + h, // bottom-right corner
  3218. 'L', x + r, y + h, // bottom side
  3219. 'C', x, y + h, x, y + h, x, y + h - r, // bottom-left corner
  3220. 'L', x, y + r, // left side
  3221. 'C', x, y, x, y, x + r, y // top-right corner
  3222. ];
  3223. if (anchorX && anchorX > w && anchorY > y + safeDistance && anchorY < y + h - safeDistance) { // replace right side
  3224. path.splice(13, 3,
  3225. 'L', x + w, anchorY - halfDistance,
  3226. x + w + arrowLength, anchorY,
  3227. x + w, anchorY + halfDistance,
  3228. x + w, y + h - r
  3229. );
  3230. } else if (anchorX && anchorX < 0 && anchorY > y + safeDistance && anchorY < y + h - safeDistance) { // replace left side
  3231. path.splice(33, 3,
  3232. 'L', x, anchorY + halfDistance,
  3233. x - arrowLength, anchorY,
  3234. x, anchorY - halfDistance,
  3235. x, y + r
  3236. );
  3237. } else if (anchorY && anchorY > h && anchorX > x + safeDistance && anchorX < x + w - safeDistance) { // replace bottom
  3238. path.splice(23, 3,
  3239. 'L', anchorX + halfDistance, y + h,
  3240. anchorX, y + h + arrowLength,
  3241. anchorX - halfDistance, y + h,
  3242. x + r, y + h
  3243. );
  3244. } else if (anchorY && anchorY < 0 && anchorX > x + safeDistance && anchorX < x + w - safeDistance) { // replace top
  3245. path.splice(3, 3,
  3246. 'L', anchorX - halfDistance, y,
  3247. anchorX, y - arrowLength,
  3248. anchorX + halfDistance, y,
  3249. w - r, y
  3250. );
  3251. }
  3252. return path;
  3253. }
  3254. },
  3255. /**
  3256. * Define a clipping rectangle
  3257. * @param {String} id
  3258. * @param {Number} x
  3259. * @param {Number} y
  3260. * @param {Number} width
  3261. * @param {Number} height
  3262. */
  3263. clipRect: function (x, y, width, height) {
  3264. var wrapper,
  3265. id = PREFIX + idCounter++,
  3266. clipPath = this.createElement('clipPath').attr({
  3267. id: id
  3268. }).add(this.defs);
  3269. wrapper = this.rect(x, y, width, height, 0).add(clipPath);
  3270. wrapper.id = id;
  3271. wrapper.clipPath = clipPath;
  3272. return wrapper;
  3273. },
  3274. /**
  3275. * Add text to the SVG object
  3276. * @param {String} str
  3277. * @param {Number} x Left position
  3278. * @param {Number} y Top position
  3279. * @param {Boolean} useHTML Use HTML to render the text
  3280. */
  3281. text: function (str, x, y, useHTML) {
  3282. // declare variables
  3283. var renderer = this,
  3284. fakeSVG = useCanVG || (!hasSVG && renderer.forExport),
  3285. wrapper,
  3286. attr = {};
  3287. if (useHTML && !renderer.forExport) {
  3288. return renderer.html(str, x, y);
  3289. }
  3290. attr.x = Math.round(x || 0); // X is always needed for line-wrap logic
  3291. if (y) {
  3292. attr.y = Math.round(y);
  3293. }
  3294. if (str || str === 0) {
  3295. attr.text = str;
  3296. }
  3297. wrapper = renderer.createElement('text')
  3298. .attr(attr);
  3299. // Prevent wrapping from creating false offsetWidths in export in legacy IE (#1079, #1063)
  3300. if (fakeSVG) {
  3301. wrapper.css({
  3302. position: ABSOLUTE
  3303. });
  3304. }
  3305. if (!useHTML) {
  3306. wrapper.xSetter = function (value, key, element) {
  3307. var tspans = element.getElementsByTagName('tspan'),
  3308. tspan,
  3309. parentVal = element.getAttribute(key),
  3310. i;
  3311. for (i = 0; i < tspans.length; i++) {
  3312. tspan = tspans[i];
  3313. // If the x values are equal, the tspan represents a linebreak
  3314. if (tspan.getAttribute(key) === parentVal) {
  3315. tspan.setAttribute(key, value);
  3316. }
  3317. }
  3318. element.setAttribute(key, value);
  3319. };
  3320. }
  3321. return wrapper;
  3322. },
  3323. /**
  3324. * Utility to return the baseline offset and total line height from the font size
  3325. */
  3326. fontMetrics: function (fontSize, elem) {
  3327. fontSize = fontSize || this.style.fontSize;
  3328. if (elem && win.getComputedStyle) {
  3329. elem = elem.element || elem; // SVGElement
  3330. fontSize = win.getComputedStyle(elem, "").fontSize;
  3331. }
  3332. fontSize = /px/.test(fontSize) ? pInt(fontSize) : /em/.test(fontSize) ? parseFloat(fontSize) * 12 : 12;
  3333. // Empirical values found by comparing font size and bounding box height.
  3334. // Applies to the default font family. http://jsfiddle.net/highcharts/7xvn7/
  3335. var lineHeight = fontSize < 24 ? fontSize + 4 : mathRound(fontSize * 1.2),
  3336. baseline = mathRound(lineHeight * 0.8);
  3337. return {
  3338. h: lineHeight,
  3339. b: baseline,
  3340. f: fontSize
  3341. };
  3342. },
  3343. /**
  3344. * Add a label, a text item that can hold a colored or gradient background
  3345. * as well as a border and shadow.
  3346. * @param {string} str
  3347. * @param {Number} x
  3348. * @param {Number} y
  3349. * @param {String} shape
  3350. * @param {Number} anchorX In case the shape has a pointer, like a flag, this is the
  3351. * coordinates it should be pinned to
  3352. * @param {Number} anchorY
  3353. * @param {Boolean} baseline Whether to position the label relative to the text baseline,
  3354. * like renderer.text, or to the upper border of the rectangle.
  3355. * @param {String} className Class name for the group
  3356. */
  3357. label: function (str, x, y, shape, anchorX, anchorY, useHTML, baseline, className) {
  3358. var renderer = this,
  3359. wrapper = renderer.g(className),
  3360. text = renderer.text('', 0, 0, useHTML)
  3361. .attr({
  3362. zIndex: 1
  3363. }),
  3364. //.add(wrapper),
  3365. box,
  3366. bBox,
  3367. alignFactor = 0,
  3368. padding = 3,
  3369. paddingLeft = 0,
  3370. width,
  3371. height,
  3372. wrapperX,
  3373. wrapperY,
  3374. crispAdjust = 0,
  3375. deferredAttr = {},
  3376. baselineOffset,
  3377. needsBox;
  3378. /**
  3379. * This function runs after the label is added to the DOM (when the bounding box is
  3380. * available), and after the text of the label is updated to detect the new bounding
  3381. * box and reflect it in the border box.
  3382. */
  3383. function updateBoxSize() {
  3384. var boxX,
  3385. boxY,
  3386. style = text.element.style;
  3387. bBox = (width === undefined || height === undefined || wrapper.styles.textAlign) && text.textStr &&
  3388. text.getBBox();
  3389. wrapper.width = (width || bBox.width || 0) + 2 * padding + paddingLeft;
  3390. wrapper.height = (height || bBox.height || 0) + 2 * padding;
  3391. // update the label-scoped y offset
  3392. baselineOffset = padding + renderer.fontMetrics(style && style.fontSize, text).b;
  3393. if (needsBox) {
  3394. // create the border box if it is not already present
  3395. if (!box) {
  3396. boxX = mathRound(-alignFactor * padding);
  3397. boxY = baseline ? -baselineOffset : 0;
  3398. wrapper.box = box = shape ?
  3399. renderer.symbol(shape, boxX, boxY, wrapper.width, wrapper.height, deferredAttr) :
  3400. renderer.rect(boxX, boxY, wrapper.width, wrapper.height, 0, deferredAttr[STROKE_WIDTH]);
  3401. box.attr('fill', NONE).add(wrapper);
  3402. }
  3403. // apply the box attributes
  3404. if (!box.isImg) { // #1630
  3405. box.attr(extend({
  3406. width: mathRound(wrapper.width),
  3407. height: mathRound(wrapper.height)
  3408. }, deferredAttr));
  3409. }
  3410. deferredAttr = null;
  3411. }
  3412. }
  3413. /**
  3414. * This function runs after setting text or padding, but only if padding is changed
  3415. */
  3416. function updateTextPadding() {
  3417. var styles = wrapper.styles,
  3418. textAlign = styles && styles.textAlign,
  3419. x = paddingLeft + padding * (1 - alignFactor),
  3420. y;
  3421. // determin y based on the baseline
  3422. y = baseline ? 0 : baselineOffset;
  3423. // compensate for alignment
  3424. if (defined(width) && bBox && (textAlign === 'center' || textAlign === 'right')) {
  3425. x += { center: 0.5, right: 1 }[textAlign] * (width - bBox.width);
  3426. }
  3427. // update if anything changed
  3428. if (x !== text.x || y !== text.y) {
  3429. text.attr('x', x);
  3430. if (y !== UNDEFINED) {
  3431. text.attr('y', y);
  3432. }
  3433. }
  3434. // record current values
  3435. text.x = x;
  3436. text.y = y;
  3437. }
  3438. /**
  3439. * Set a box attribute, or defer it if the box is not yet created
  3440. * @param {Object} key
  3441. * @param {Object} value
  3442. */
  3443. function boxAttr(key, value) {
  3444. if (box) {
  3445. box.attr(key, value);
  3446. } else {
  3447. deferredAttr[key] = value;
  3448. }
  3449. }
  3450. /**
  3451. * After the text element is added, get the desired size of the border box
  3452. * and add it before the text in the DOM.
  3453. */
  3454. wrapper.onAdd = function () {
  3455. text.add(wrapper);
  3456. wrapper.attr({
  3457. text: str || '', // alignment is available now
  3458. x: x,
  3459. y: y
  3460. });
  3461. if (box && defined(anchorX)) {
  3462. wrapper.attr({
  3463. anchorX: anchorX,
  3464. anchorY: anchorY
  3465. });
  3466. }
  3467. };
  3468. /*
  3469. * Add specific attribute setters.
  3470. */
  3471. // only change local variables
  3472. wrapper.widthSetter = function (value) {
  3473. width = value;
  3474. };
  3475. wrapper.heightSetter = function (value) {
  3476. height = value;
  3477. };
  3478. wrapper.paddingSetter = function (value) {
  3479. if (defined(value) && value !== padding) {
  3480. padding = value;
  3481. updateTextPadding();
  3482. }
  3483. };
  3484. wrapper.paddingLeftSetter = function (value) {
  3485. if (defined(value) && value !== paddingLeft) {
  3486. paddingLeft = value;
  3487. updateTextPadding();
  3488. }
  3489. };
  3490. // change local variable and prevent setting attribute on the group
  3491. wrapper.alignSetter = function (value) {
  3492. alignFactor = { left: 0, center: 0.5, right: 1 }[value];
  3493. };
  3494. // apply these to the box and the text alike
  3495. wrapper.textSetter = function (value) {
  3496. if (value !== UNDEFINED) {
  3497. text.textSetter(value);
  3498. }
  3499. updateBoxSize();
  3500. updateTextPadding();
  3501. };
  3502. // apply these to the box but not to the text
  3503. wrapper['stroke-widthSetter'] = function (value, key) {
  3504. if (value) {
  3505. needsBox = true;
  3506. }
  3507. crispAdjust = value % 2 / 2;
  3508. boxAttr(key, value);
  3509. };
  3510. wrapper.strokeSetter = wrapper.fillSetter = wrapper.rSetter = function (value, key) {
  3511. if (key === 'fill' && value) {
  3512. needsBox = true;
  3513. }
  3514. boxAttr(key, value);
  3515. };
  3516. wrapper.anchorXSetter = function (value, key) {
  3517. anchorX = value;
  3518. boxAttr(key, value + crispAdjust - wrapperX);
  3519. };
  3520. wrapper.anchorYSetter = function (value, key) {
  3521. anchorY = value;
  3522. boxAttr(key, value - wrapperY);
  3523. };
  3524. // rename attributes
  3525. wrapper.xSetter = function (value) {
  3526. wrapper.x = value; // for animation getter
  3527. if (alignFactor) {
  3528. value -= alignFactor * ((width || bBox.width) + padding);
  3529. }
  3530. wrapperX = mathRound(value);
  3531. wrapper.attr('translateX', wrapperX);
  3532. };
  3533. wrapper.ySetter = function (value) {
  3534. wrapperY = wrapper.y = mathRound(value);
  3535. wrapper.attr('translateY', wrapperY);
  3536. };
  3537. // Redirect certain methods to either the box or the text
  3538. var baseCss = wrapper.css;
  3539. return extend(wrapper, {
  3540. /**
  3541. * Pick up some properties and apply them to the text instead of the wrapper
  3542. */
  3543. css: function (styles) {
  3544. if (styles) {
  3545. var textStyles = {};
  3546. styles = merge(styles); // create a copy to avoid altering the original object (#537)
  3547. each(wrapper.textProps, function (prop) {
  3548. if (styles[prop] !== UNDEFINED) {
  3549. textStyles[prop] = styles[prop];
  3550. delete styles[prop];
  3551. }
  3552. });
  3553. text.css(textStyles);
  3554. }
  3555. return baseCss.call(wrapper, styles);
  3556. },
  3557. /**
  3558. * Return the bounding box of the box, not the group
  3559. */
  3560. getBBox: function () {
  3561. return {
  3562. width: bBox.width + 2 * padding,
  3563. height: bBox.height + 2 * padding,
  3564. x: bBox.x - padding,
  3565. y: bBox.y - padding
  3566. };
  3567. },
  3568. /**
  3569. * Apply the shadow to the box
  3570. */
  3571. shadow: function (b) {
  3572. if (box) {
  3573. box.shadow(b);
  3574. }
  3575. return wrapper;
  3576. },
  3577. /**
  3578. * Destroy and release memory.
  3579. */
  3580. destroy: function () {
  3581. // Added by button implementation
  3582. removeEvent(wrapper.element, 'mouseenter');
  3583. removeEvent(wrapper.element, 'mouseleave');
  3584. if (text) {
  3585. text = text.destroy();
  3586. }
  3587. if (box) {
  3588. box = box.destroy();
  3589. }
  3590. // Call base implementation to destroy the rest
  3591. SVGElement.prototype.destroy.call(wrapper);
  3592. // Release local pointers (#1298)
  3593. wrapper = renderer = updateBoxSize = updateTextPadding = boxAttr = null;
  3594. }
  3595. });
  3596. }
  3597. }; // end SVGRenderer
  3598. // general renderer
  3599. Renderer = SVGRenderer;
  3600. // extend SvgElement for useHTML option
  3601. extend(SVGElement.prototype, {
  3602. /**
  3603. * Apply CSS to HTML elements. This is used in text within SVG rendering and
  3604. * by the VML renderer
  3605. */
  3606. htmlCss: function (styles) {
  3607. var wrapper = this,
  3608. element = wrapper.element,
  3609. textWidth = styles && element.tagName === 'SPAN' && styles.width;
  3610. if (textWidth) {
  3611. delete styles.width;
  3612. wrapper.textWidth = textWidth;
  3613. wrapper.updateTransform();
  3614. }
  3615. wrapper.styles = extend(wrapper.styles, styles);
  3616. css(wrapper.element, styles);
  3617. return wrapper;
  3618. },
  3619. /**
  3620. * VML and useHTML method for calculating the bounding box based on offsets
  3621. * @param {Boolean} refresh Whether to force a fresh value from the DOM or to
  3622. * use the cached value
  3623. *
  3624. * @return {Object} A hash containing values for x, y, width and height
  3625. */
  3626. htmlGetBBox: function () {
  3627. var wrapper = this,
  3628. element = wrapper.element,
  3629. bBox = wrapper.bBox;
  3630. // faking getBBox in exported SVG in legacy IE
  3631. if (!bBox) {
  3632. // faking getBBox in exported SVG in legacy IE (is this a duplicate of the fix for #1079?)
  3633. if (element.nodeName === 'text') {
  3634. element.style.position = ABSOLUTE;
  3635. }
  3636. bBox = wrapper.bBox = {
  3637. x: element.offsetLeft,
  3638. y: element.offsetTop,
  3639. width: element.offsetWidth,
  3640. height: element.offsetHeight
  3641. };
  3642. }
  3643. return bBox;
  3644. },
  3645. /**
  3646. * VML override private method to update elements based on internal
  3647. * properties based on SVG transform
  3648. */
  3649. htmlUpdateTransform: function () {
  3650. // aligning non added elements is expensive
  3651. if (!this.added) {
  3652. this.alignOnAdd = true;
  3653. return;
  3654. }
  3655. var wrapper = this,
  3656. renderer = wrapper.renderer,
  3657. elem = wrapper.element,
  3658. translateX = wrapper.translateX || 0,
  3659. translateY = wrapper.translateY || 0,
  3660. x = wrapper.x || 0,
  3661. y = wrapper.y || 0,
  3662. align = wrapper.textAlign || 'left',
  3663. alignCorrection = { left: 0, center: 0.5, right: 1 }[align],
  3664. shadows = wrapper.shadows;
  3665. // apply translate
  3666. css(elem, {
  3667. marginLeft: translateX,
  3668. marginTop: translateY
  3669. });
  3670. if (shadows) { // used in labels/tooltip
  3671. each(shadows, function (shadow) {
  3672. css(shadow, {
  3673. marginLeft: translateX + 1,
  3674. marginTop: translateY + 1
  3675. });
  3676. });
  3677. }
  3678. // apply inversion
  3679. if (wrapper.inverted) { // wrapper is a group
  3680. each(elem.childNodes, function (child) {
  3681. renderer.invertChild(child, elem);
  3682. });
  3683. }
  3684. if (elem.tagName === 'SPAN') {
  3685. var width,
  3686. rotation = wrapper.rotation,
  3687. baseline,
  3688. textWidth = pInt(wrapper.textWidth),
  3689. currentTextTransform = [rotation, align, elem.innerHTML, wrapper.textWidth].join(',');
  3690. if (currentTextTransform !== wrapper.cTT) { // do the calculations and DOM access only if properties changed
  3691. baseline = renderer.fontMetrics(elem.style.fontSize).b;
  3692. // Renderer specific handling of span rotation
  3693. if (defined(rotation)) {
  3694. wrapper.setSpanRotation(rotation, alignCorrection, baseline);
  3695. }
  3696. width = pick(wrapper.elemWidth, elem.offsetWidth);
  3697. // Update textWidth
  3698. if (width > textWidth && /[ \-]/.test(elem.textContent || elem.innerText)) { // #983, #1254
  3699. css(elem, {
  3700. width: textWidth + PX,
  3701. display: 'block',
  3702. whiteSpace: 'normal'
  3703. });
  3704. width = textWidth;
  3705. }
  3706. wrapper.getSpanCorrection(width, baseline, alignCorrection, rotation, align);
  3707. }
  3708. // apply position with correction
  3709. css(elem, {
  3710. left: (x + (wrapper.xCorr || 0)) + PX,
  3711. top: (y + (wrapper.yCorr || 0)) + PX
  3712. });
  3713. // force reflow in webkit to apply the left and top on useHTML element (#1249)
  3714. if (isWebKit) {
  3715. baseline = elem.offsetHeight; // assigned to baseline for JSLint purpose
  3716. }
  3717. // record current text transform
  3718. wrapper.cTT = currentTextTransform;
  3719. }
  3720. },
  3721. /**
  3722. * Set the rotation of an individual HTML span
  3723. */
  3724. setSpanRotation: function (rotation, alignCorrection, baseline) {
  3725. var rotationStyle = {},
  3726. cssTransformKey = isIE ? '-ms-transform' : isWebKit ? '-webkit-transform' : isFirefox ? 'MozTransform' : isOpera ? '-o-transform' : '';
  3727. rotationStyle[cssTransformKey] = rotationStyle.transform = 'rotate(' + rotation + 'deg)';
  3728. rotationStyle[cssTransformKey + (isFirefox ? 'Origin' : '-origin')] = rotationStyle.transformOrigin = (alignCorrection * 100) + '% ' + baseline + 'px';
  3729. css(this.element, rotationStyle);
  3730. },
  3731. /**
  3732. * Get the correction in X and Y positioning as the element is rotated.
  3733. */
  3734. getSpanCorrection: function (width, baseline, alignCorrection) {
  3735. this.xCorr = -width * alignCorrection;
  3736. this.yCorr = -baseline;
  3737. }
  3738. });
  3739. // Extend SvgRenderer for useHTML option.
  3740. extend(SVGRenderer.prototype, {
  3741. /**
  3742. * Create HTML text node. This is used by the VML renderer as well as the SVG
  3743. * renderer through the useHTML option.
  3744. *
  3745. * @param {String} str
  3746. * @param {Number} x
  3747. * @param {Number} y
  3748. */
  3749. html: function (str, x, y) {
  3750. var wrapper = this.createElement('span'),
  3751. element = wrapper.element,
  3752. renderer = wrapper.renderer;
  3753. // Text setter
  3754. wrapper.textSetter = function (value) {
  3755. if (value !== element.innerHTML) {
  3756. delete this.bBox;
  3757. }
  3758. element.innerHTML = this.textStr = value;
  3759. };
  3760. // Various setters which rely on update transform
  3761. wrapper.xSetter = wrapper.ySetter = wrapper.alignSetter = wrapper.rotationSetter = function (value, key) {
  3762. if (key === 'align') {
  3763. key = 'textAlign'; // Do not overwrite the SVGElement.align method. Same as VML.
  3764. }
  3765. wrapper[key] = value;
  3766. wrapper.htmlUpdateTransform();
  3767. };
  3768. // Set the default attributes
  3769. wrapper.attr({
  3770. text: str,
  3771. x: mathRound(x),
  3772. y: mathRound(y)
  3773. })
  3774. .css({
  3775. position: ABSOLUTE,
  3776. whiteSpace: 'nowrap',
  3777. fontFamily: this.style.fontFamily,
  3778. fontSize: this.style.fontSize
  3779. });
  3780. // Use the HTML specific .css method
  3781. wrapper.css = wrapper.htmlCss;
  3782. // This is specific for HTML within SVG
  3783. if (renderer.isSVG) {
  3784. wrapper.add = function (svgGroupWrapper) {
  3785. var htmlGroup,
  3786. container = renderer.box.parentNode,
  3787. parentGroup,
  3788. parents = [];
  3789. this.parentGroup = svgGroupWrapper;
  3790. // Create a mock group to hold the HTML elements
  3791. if (svgGroupWrapper) {
  3792. htmlGroup = svgGroupWrapper.div;
  3793. if (!htmlGroup) {
  3794. // Read the parent chain into an array and read from top down
  3795. parentGroup = svgGroupWrapper;
  3796. while (parentGroup) {
  3797. parents.push(parentGroup);
  3798. // Move up to the next parent group
  3799. parentGroup = parentGroup.parentGroup;
  3800. }
  3801. // Ensure dynamically updating position when any parent is translated
  3802. each(parents.reverse(), function (parentGroup) {
  3803. var htmlGroupStyle;
  3804. // Create a HTML div and append it to the parent div to emulate
  3805. // the SVG group structure
  3806. htmlGroup = parentGroup.div = parentGroup.div || createElement(DIV, {
  3807. className: attr(parentGroup.element, 'class')
  3808. }, {
  3809. position: ABSOLUTE,
  3810. left: (parentGroup.translateX || 0) + PX,
  3811. top: (parentGroup.translateY || 0) + PX
  3812. }, htmlGroup || container); // the top group is appended to container
  3813. // Shortcut
  3814. htmlGroupStyle = htmlGroup.style;
  3815. // Set listeners to update the HTML div's position whenever the SVG group
  3816. // position is changed
  3817. extend(parentGroup, {
  3818. translateXSetter: function (value, key) {
  3819. htmlGroupStyle.left = value + PX;
  3820. parentGroup[key] = value;
  3821. parentGroup.doTransform = true;
  3822. },
  3823. translateYSetter: function (value, key) {
  3824. htmlGroupStyle.top = value + PX;
  3825. parentGroup[key] = value;
  3826. parentGroup.doTransform = true;
  3827. },
  3828. visibilitySetter: function (value, key) {
  3829. htmlGroupStyle[key] = value;
  3830. }
  3831. });
  3832. });
  3833. }
  3834. } else {
  3835. htmlGroup = container;
  3836. }
  3837. htmlGroup.appendChild(element);
  3838. // Shared with VML:
  3839. wrapper.added = true;
  3840. if (wrapper.alignOnAdd) {
  3841. wrapper.htmlUpdateTransform();
  3842. }
  3843. return wrapper;
  3844. };
  3845. }
  3846. return wrapper;
  3847. }
  3848. });
  3849. /* ****************************************************************************
  3850. * *
  3851. * START OF INTERNET EXPLORER <= 8 SPECIFIC CODE *
  3852. * *
  3853. * For applications and websites that don't need IE support, like platform *
  3854. * targeted mobile apps and web apps, this code can be removed. *
  3855. * *
  3856. *****************************************************************************/
  3857. /**
  3858. * @constructor
  3859. */
  3860. var VMLRenderer, VMLElement;
  3861. if (!hasSVG && !useCanVG) {
  3862. /**
  3863. * The VML element wrapper.
  3864. */
  3865. VMLElement = {
  3866. /**
  3867. * Initialize a new VML element wrapper. It builds the markup as a string
  3868. * to minimize DOM traffic.
  3869. * @param {Object} renderer
  3870. * @param {Object} nodeName
  3871. */
  3872. init: function (renderer, nodeName) {
  3873. var wrapper = this,
  3874. markup = ['<', nodeName, ' filled="f" stroked="f"'],
  3875. style = ['position: ', ABSOLUTE, ';'],
  3876. isDiv = nodeName === DIV;
  3877. // divs and shapes need size
  3878. if (nodeName === 'shape' || isDiv) {
  3879. style.push('left:0;top:0;width:1px;height:1px;');
  3880. }
  3881. style.push('visibility: ', isDiv ? HIDDEN : VISIBLE);
  3882. markup.push(' style="', style.join(''), '"/>');
  3883. // create element with default attributes and style
  3884. if (nodeName) {
  3885. markup = isDiv || nodeName === 'span' || nodeName === 'img' ?
  3886. markup.join('')
  3887. : renderer.prepVML(markup);
  3888. wrapper.element = createElement(markup);
  3889. }
  3890. wrapper.renderer = renderer;
  3891. },
  3892. /**
  3893. * Add the node to the given parent
  3894. * @param {Object} parent
  3895. */
  3896. add: function (parent) {
  3897. var wrapper = this,
  3898. renderer = wrapper.renderer,
  3899. element = wrapper.element,
  3900. box = renderer.box,
  3901. inverted = parent && parent.inverted,
  3902. // get the parent node
  3903. parentNode = parent ?
  3904. parent.element || parent :
  3905. box;
  3906. // if the parent group is inverted, apply inversion on all children
  3907. if (inverted) { // only on groups
  3908. renderer.invertChild(element, parentNode);
  3909. }
  3910. // append it
  3911. parentNode.appendChild(element);
  3912. // align text after adding to be able to read offset
  3913. wrapper.added = true;
  3914. if (wrapper.alignOnAdd && !wrapper.deferUpdateTransform) {
  3915. wrapper.updateTransform();
  3916. }
  3917. // fire an event for internal hooks
  3918. if (wrapper.onAdd) {
  3919. wrapper.onAdd();
  3920. }
  3921. return wrapper;
  3922. },
  3923. /**
  3924. * VML always uses htmlUpdateTransform
  3925. */
  3926. updateTransform: SVGElement.prototype.htmlUpdateTransform,
  3927. /**
  3928. * Set the rotation of a span with oldIE's filter
  3929. */
  3930. setSpanRotation: function () {
  3931. // Adjust for alignment and rotation. Rotation of useHTML content is not yet implemented
  3932. // but it can probably be implemented for Firefox 3.5+ on user request. FF3.5+
  3933. // has support for CSS3 transform. The getBBox method also needs to be updated
  3934. // to compensate for the rotation, like it currently does for SVG.
  3935. // Test case: http://jsfiddle.net/highcharts/Ybt44/
  3936. var rotation = this.rotation,
  3937. costheta = mathCos(rotation * deg2rad),
  3938. sintheta = mathSin(rotation * deg2rad);
  3939. css(this.element, {
  3940. filter: rotation ? ['progid:DXImageTransform.Microsoft.Matrix(M11=', costheta,
  3941. ', M12=', -sintheta, ', M21=', sintheta, ', M22=', costheta,
  3942. ', sizingMethod=\'auto expand\')'].join('') : NONE
  3943. });
  3944. },
  3945. /**
  3946. * Get the positioning correction for the span after rotating.
  3947. */
  3948. getSpanCorrection: function (width, baseline, alignCorrection, rotation, align) {
  3949. var costheta = rotation ? mathCos(rotation * deg2rad) : 1,
  3950. sintheta = rotation ? mathSin(rotation * deg2rad) : 0,
  3951. height = pick(this.elemHeight, this.element.offsetHeight),
  3952. quad,
  3953. nonLeft = align && align !== 'left';
  3954. // correct x and y
  3955. this.xCorr = costheta < 0 && -width;
  3956. this.yCorr = sintheta < 0 && -height;
  3957. // correct for baseline and corners spilling out after rotation
  3958. quad = costheta * sintheta < 0;
  3959. this.xCorr += sintheta * baseline * (quad ? 1 - alignCorrection : alignCorrection);
  3960. this.yCorr -= costheta * baseline * (rotation ? (quad ? alignCorrection : 1 - alignCorrection) : 1);
  3961. // correct for the length/height of the text
  3962. if (nonLeft) {
  3963. this.xCorr -= width * alignCorrection * (costheta < 0 ? -1 : 1);
  3964. if (rotation) {
  3965. this.yCorr -= height * alignCorrection * (sintheta < 0 ? -1 : 1);
  3966. }
  3967. css(this.element, {
  3968. textAlign: align
  3969. });
  3970. }
  3971. },
  3972. /**
  3973. * Converts a subset of an SVG path definition to its VML counterpart. Takes an array
  3974. * as the parameter and returns a string.
  3975. */
  3976. pathToVML: function (value) {
  3977. // convert paths
  3978. var i = value.length,
  3979. path = [];
  3980. while (i--) {
  3981. // Multiply by 10 to allow subpixel precision.
  3982. // Substracting half a pixel seems to make the coordinates
  3983. // align with SVG, but this hasn't been tested thoroughly
  3984. if (isNumber(value[i])) {
  3985. path[i] = mathRound(value[i] * 10) - 5;
  3986. } else if (value[i] === 'Z') { // close the path
  3987. path[i] = 'x';
  3988. } else {
  3989. path[i] = value[i];
  3990. // When the start X and end X coordinates of an arc are too close,
  3991. // they are rounded to the same value above. In this case, substract or
  3992. // add 1 from the end X and Y positions. #186, #760, #1371, #1410.
  3993. if (value.isArc && (value[i] === 'wa' || value[i] === 'at')) {
  3994. // Start and end X
  3995. if (path[i + 5] === path[i + 7]) {
  3996. path[i + 7] += value[i + 7] > value[i + 5] ? 1 : -1;
  3997. }
  3998. // Start and end Y
  3999. if (path[i + 6] === path[i + 8]) {
  4000. path[i + 8] += value[i + 8] > value[i + 6] ? 1 : -1;
  4001. }
  4002. }
  4003. }
  4004. }
  4005. // Loop up again to handle path shortcuts (#2132)
  4006. /*while (i++ < path.length) {
  4007. if (path[i] === 'H') { // horizontal line to
  4008. path[i] = 'L';
  4009. path.splice(i + 2, 0, path[i - 1]);
  4010. } else if (path[i] === 'V') { // vertical line to
  4011. path[i] = 'L';
  4012. path.splice(i + 1, 0, path[i - 2]);
  4013. }
  4014. }*/
  4015. return path.join(' ') || 'x';
  4016. },
  4017. /**
  4018. * Set the element's clipping to a predefined rectangle
  4019. *
  4020. * @param {String} id The id of the clip rectangle
  4021. */
  4022. clip: function (clipRect) {
  4023. var wrapper = this,
  4024. clipMembers,
  4025. cssRet;
  4026. if (clipRect) {
  4027. clipMembers = clipRect.members;
  4028. erase(clipMembers, wrapper); // Ensure unique list of elements (#1258)
  4029. clipMembers.push(wrapper);
  4030. wrapper.destroyClip = function () {
  4031. erase(clipMembers, wrapper);
  4032. };
  4033. cssRet = clipRect.getCSS(wrapper);
  4034. } else {
  4035. if (wrapper.destroyClip) {
  4036. wrapper.destroyClip();
  4037. }
  4038. cssRet = { clip: docMode8 ? 'inherit' : 'rect(auto)' }; // #1214
  4039. }
  4040. return wrapper.css(cssRet);
  4041. },
  4042. /**
  4043. * Set styles for the element
  4044. * @param {Object} styles
  4045. */
  4046. css: SVGElement.prototype.htmlCss,
  4047. /**
  4048. * Removes a child either by removeChild or move to garbageBin.
  4049. * Issue 490; in VML removeChild results in Orphaned nodes according to sIEve, discardElement does not.
  4050. */
  4051. safeRemoveChild: function (element) {
  4052. // discardElement will detach the node from its parent before attaching it
  4053. // to the garbage bin. Therefore it is important that the node is attached and have parent.
  4054. if (element.parentNode) {
  4055. discardElement(element);
  4056. }
  4057. },
  4058. /**
  4059. * Extend element.destroy by removing it from the clip members array
  4060. */
  4061. destroy: function () {
  4062. if (this.destroyClip) {
  4063. this.destroyClip();
  4064. }
  4065. return SVGElement.prototype.destroy.apply(this);
  4066. },
  4067. /**
  4068. * Add an event listener. VML override for normalizing event parameters.
  4069. * @param {String} eventType
  4070. * @param {Function} handler
  4071. */
  4072. on: function (eventType, handler) {
  4073. // simplest possible event model for internal use
  4074. this.element['on' + eventType] = function () {
  4075. var evt = win.event;
  4076. evt.target = evt.srcElement;
  4077. handler(evt);
  4078. };
  4079. return this;
  4080. },
  4081. /**
  4082. * In stacked columns, cut off the shadows so that they don't overlap
  4083. */
  4084. cutOffPath: function (path, length) {
  4085. var len;
  4086. path = path.split(/[ ,]/);
  4087. len = path.length;
  4088. if (len === 9 || len === 11) {
  4089. path[len - 4] = path[len - 2] = pInt(path[len - 2]) - 10 * length;
  4090. }
  4091. return path.join(' ');
  4092. },
  4093. /**
  4094. * Apply a drop shadow by copying elements and giving them different strokes
  4095. * @param {Boolean|Object} shadowOptions
  4096. */
  4097. shadow: function (shadowOptions, group, cutOff) {
  4098. var shadows = [],
  4099. i,
  4100. element = this.element,
  4101. renderer = this.renderer,
  4102. shadow,
  4103. elemStyle = element.style,
  4104. markup,
  4105. path = element.path,
  4106. strokeWidth,
  4107. modifiedPath,
  4108. shadowWidth,
  4109. shadowElementOpacity;
  4110. // some times empty paths are not strings
  4111. if (path && typeof path.value !== 'string') {
  4112. path = 'x';
  4113. }
  4114. modifiedPath = path;
  4115. if (shadowOptions) {
  4116. shadowWidth = pick(shadowOptions.width, 3);
  4117. shadowElementOpacity = (shadowOptions.opacity || 0.15) / shadowWidth;
  4118. for (i = 1; i <= 3; i++) {
  4119. strokeWidth = (shadowWidth * 2) + 1 - (2 * i);
  4120. // Cut off shadows for stacked column items
  4121. if (cutOff) {
  4122. modifiedPath = this.cutOffPath(path.value, strokeWidth + 0.5);
  4123. }
  4124. markup = ['<shape isShadow="true" strokeweight="', strokeWidth,
  4125. '" filled="false" path="', modifiedPath,
  4126. '" coordsize="10 10" style="', element.style.cssText, '" />'];
  4127. shadow = createElement(renderer.prepVML(markup),
  4128. null, {
  4129. left: pInt(elemStyle.left) + pick(shadowOptions.offsetX, 1),
  4130. top: pInt(elemStyle.top) + pick(shadowOptions.offsetY, 1)
  4131. }
  4132. );
  4133. if (cutOff) {
  4134. shadow.cutOff = strokeWidth + 1;
  4135. }
  4136. // apply the opacity
  4137. markup = ['<stroke color="', shadowOptions.color || 'black', '" opacity="', shadowElementOpacity * i, '"/>'];
  4138. createElement(renderer.prepVML(markup), null, null, shadow);
  4139. // insert it
  4140. if (group) {
  4141. group.element.appendChild(shadow);
  4142. } else {
  4143. element.parentNode.insertBefore(shadow, element);
  4144. }
  4145. // record it
  4146. shadows.push(shadow);
  4147. }
  4148. this.shadows = shadows;
  4149. }
  4150. return this;
  4151. },
  4152. updateShadows: noop, // Used in SVG only
  4153. setAttr: function (key, value) {
  4154. if (docMode8) { // IE8 setAttribute bug
  4155. this.element[key] = value;
  4156. } else {
  4157. this.element.setAttribute(key, value);
  4158. }
  4159. },
  4160. classSetter: function (value) {
  4161. // IE8 Standards mode has problems retrieving the className unless set like this
  4162. this.element.className = value;
  4163. },
  4164. dashstyleSetter: function (value, key, element) {
  4165. var strokeElem = element.getElementsByTagName('stroke')[0] ||
  4166. createElement(this.renderer.prepVML(['<stroke/>']), null, null, element);
  4167. strokeElem[key] = value || 'solid';
  4168. this[key] = value; /* because changing stroke-width will change the dash length
  4169. and cause an epileptic effect */
  4170. },
  4171. dSetter: function (value, key, element) {
  4172. var i,
  4173. shadows = this.shadows;
  4174. value = value || [];
  4175. this.d = value.join && value.join(' '); // used in getter for animation
  4176. element.path = value = this.pathToVML(value);
  4177. // update shadows
  4178. if (shadows) {
  4179. i = shadows.length;
  4180. while (i--) {
  4181. shadows[i].path = shadows[i].cutOff ? this.cutOffPath(value, shadows[i].cutOff) : value;
  4182. }
  4183. }
  4184. this.setAttr(key, value);
  4185. },
  4186. fillSetter: function (value, key, element) {
  4187. var nodeName = element.nodeName;
  4188. if (nodeName === 'SPAN') { // text color
  4189. element.style.color = value;
  4190. } else if (nodeName !== 'IMG') { // #1336
  4191. element.filled = value !== NONE;
  4192. this.setAttr('fillcolor', this.renderer.color(value, element, key, this));
  4193. }
  4194. },
  4195. opacitySetter: noop, // Don't bother - animation is too slow and filters introduce artifacts
  4196. rotationSetter: function (value, key, element) {
  4197. var style = element.style;
  4198. this[key] = style[key] = value; // style is for #1873
  4199. // Correction for the 1x1 size of the shape container. Used in gauge needles.
  4200. style.left = -mathRound(mathSin(value * deg2rad) + 1) + PX;
  4201. style.top = mathRound(mathCos(value * deg2rad)) + PX;
  4202. },
  4203. strokeSetter: function (value, key, element) {
  4204. this.setAttr('strokecolor', this.renderer.color(value, element, key));
  4205. },
  4206. 'stroke-widthSetter': function (value, key, element) {
  4207. element.stroked = !!value; // VML "stroked" attribute
  4208. this[key] = value; // used in getter, issue #113
  4209. if (isNumber(value)) {
  4210. value += PX;
  4211. }
  4212. this.setAttr('strokeweight', value);
  4213. },
  4214. titleSetter: function (value, key) {
  4215. this.setAttr(key, value);
  4216. },
  4217. visibilitySetter: function (value, key, element) {
  4218. // Handle inherited visibility
  4219. if (value === 'inherit') {
  4220. value = VISIBLE;
  4221. }
  4222. // Let the shadow follow the main element
  4223. if (this.shadows) {
  4224. each(this.shadows, function (shadow) {
  4225. shadow.style[key] = value;
  4226. });
  4227. }
  4228. // Instead of toggling the visibility CSS property, move the div out of the viewport.
  4229. // This works around #61 and #586
  4230. if (element.nodeName === 'DIV') {
  4231. value = value === HIDDEN ? '-999em' : 0;
  4232. // In order to redraw, IE7 needs the div to be visible when tucked away
  4233. // outside the viewport. So the visibility is actually opposite of
  4234. // the expected value. This applies to the tooltip only.
  4235. if (!docMode8) {
  4236. element.style[key] = value ? VISIBLE : HIDDEN;
  4237. }
  4238. key = 'top';
  4239. }
  4240. element.style[key] = value;
  4241. },
  4242. xSetter: function (value, key, element) {
  4243. this[key] = value; // used in getter
  4244. if (key === 'x') {
  4245. key = 'left';
  4246. } else if (key === 'y') {
  4247. key = 'top';
  4248. }/* else {
  4249. value = mathMax(0, value); // don't set width or height below zero (#311)
  4250. }*/
  4251. // clipping rectangle special
  4252. if (this.updateClipping) {
  4253. this[key] = value; // the key is now 'left' or 'top' for 'x' and 'y'
  4254. this.updateClipping();
  4255. } else {
  4256. // normal
  4257. element.style[key] = value;
  4258. }
  4259. },
  4260. zIndexSetter: function (value, key, element) {
  4261. element.style[key] = value;
  4262. }
  4263. };
  4264. Highcharts.VMLElement = VMLElement = extendClass(SVGElement, VMLElement);
  4265. // Some shared setters
  4266. VMLElement.prototype.ySetter =
  4267. VMLElement.prototype.widthSetter =
  4268. VMLElement.prototype.heightSetter =
  4269. VMLElement.prototype.xSetter;
  4270. /**
  4271. * The VML renderer
  4272. */
  4273. var VMLRendererExtension = { // inherit SVGRenderer
  4274. Element: VMLElement,
  4275. isIE8: userAgent.indexOf('MSIE 8.0') > -1,
  4276. /**
  4277. * Initialize the VMLRenderer
  4278. * @param {Object} container
  4279. * @param {Number} width
  4280. * @param {Number} height
  4281. */
  4282. init: function (container, width, height, style) {
  4283. var renderer = this,
  4284. boxWrapper,
  4285. box,
  4286. css;
  4287. renderer.alignedObjects = [];
  4288. boxWrapper = renderer.createElement(DIV)
  4289. .css(extend(this.getStyle(style), { position: RELATIVE}));
  4290. box = boxWrapper.element;
  4291. container.appendChild(boxWrapper.element);
  4292. // generate the containing box
  4293. renderer.isVML = true;
  4294. renderer.box = box;
  4295. renderer.boxWrapper = boxWrapper;
  4296. renderer.cache = {};
  4297. renderer.setSize(width, height, false);
  4298. // The only way to make IE6 and IE7 print is to use a global namespace. However,
  4299. // with IE8 the only way to make the dynamic shapes visible in screen and print mode
  4300. // seems to be to add the xmlns attribute and the behaviour style inline.
  4301. if (!doc.namespaces.hcv) {
  4302. doc.namespaces.add('hcv', 'urn:schemas-microsoft-com:vml');
  4303. // Setup default CSS (#2153, #2368, #2384)
  4304. css = 'hcv\\:fill, hcv\\:path, hcv\\:shape, hcv\\:stroke' +
  4305. '{ behavior:url(#default#VML); display: inline-block; } ';
  4306. try {
  4307. doc.createStyleSheet().cssText = css;
  4308. } catch (e) {
  4309. doc.styleSheets[0].cssText += css;
  4310. }
  4311. }
  4312. },
  4313. /**
  4314. * Detect whether the renderer is hidden. This happens when one of the parent elements
  4315. * has display: none
  4316. */
  4317. isHidden: function () {
  4318. return !this.box.offsetWidth;
  4319. },
  4320. /**
  4321. * Define a clipping rectangle. In VML it is accomplished by storing the values
  4322. * for setting the CSS style to all associated members.
  4323. *
  4324. * @param {Number} x
  4325. * @param {Number} y
  4326. * @param {Number} width
  4327. * @param {Number} height
  4328. */
  4329. clipRect: function (x, y, width, height) {
  4330. // create a dummy element
  4331. var clipRect = this.createElement(),
  4332. isObj = isObject(x);
  4333. // mimic a rectangle with its style object for automatic updating in attr
  4334. return extend(clipRect, {
  4335. members: [],
  4336. left: (isObj ? x.x : x) + 1,
  4337. top: (isObj ? x.y : y) + 1,
  4338. width: (isObj ? x.width : width) - 1,
  4339. height: (isObj ? x.height : height) - 1,
  4340. getCSS: function (wrapper) {
  4341. var element = wrapper.element,
  4342. nodeName = element.nodeName,
  4343. isShape = nodeName === 'shape',
  4344. inverted = wrapper.inverted,
  4345. rect = this,
  4346. top = rect.top - (isShape ? element.offsetTop : 0),
  4347. left = rect.left,
  4348. right = left + rect.width,
  4349. bottom = top + rect.height,
  4350. ret = {
  4351. clip: 'rect(' +
  4352. mathRound(inverted ? left : top) + 'px,' +
  4353. mathRound(inverted ? bottom : right) + 'px,' +
  4354. mathRound(inverted ? right : bottom) + 'px,' +
  4355. mathRound(inverted ? top : left) + 'px)'
  4356. };
  4357. // issue 74 workaround
  4358. if (!inverted && docMode8 && nodeName === 'DIV') {
  4359. extend(ret, {
  4360. width: right + PX,
  4361. height: bottom + PX
  4362. });
  4363. }
  4364. return ret;
  4365. },
  4366. // used in attr and animation to update the clipping of all members
  4367. updateClipping: function () {
  4368. each(clipRect.members, function (member) {
  4369. if (member.element) { // Deleted series, like in stock/members/series-remove demo. Should be removed from members, but this will do.
  4370. member.css(clipRect.getCSS(member));
  4371. }
  4372. });
  4373. }
  4374. });
  4375. },
  4376. /**
  4377. * Take a color and return it if it's a string, make it a gradient if it's a
  4378. * gradient configuration object, and apply opacity.
  4379. *
  4380. * @param {Object} color The color or config object
  4381. */
  4382. color: function (color, elem, prop, wrapper) {
  4383. var renderer = this,
  4384. colorObject,
  4385. regexRgba = /^rgba/,
  4386. markup,
  4387. fillType,
  4388. ret = NONE;
  4389. // Check for linear or radial gradient
  4390. if (color && color.linearGradient) {
  4391. fillType = 'gradient';
  4392. } else if (color && color.radialGradient) {
  4393. fillType = 'pattern';
  4394. }
  4395. if (fillType) {
  4396. var stopColor,
  4397. stopOpacity,
  4398. gradient = color.linearGradient || color.radialGradient,
  4399. x1,
  4400. y1,
  4401. x2,
  4402. y2,
  4403. opacity1,
  4404. opacity2,
  4405. color1,
  4406. color2,
  4407. fillAttr = '',
  4408. stops = color.stops,
  4409. firstStop,
  4410. lastStop,
  4411. colors = [],
  4412. addFillNode = function () {
  4413. // Add the fill subnode. When colors attribute is used, the meanings of opacity and o:opacity2
  4414. // are reversed.
  4415. markup = ['<fill colors="' + colors.join(',') + '" opacity="', opacity2, '" o:opacity2="', opacity1,
  4416. '" type="', fillType, '" ', fillAttr, 'focus="100%" method="any" />'];
  4417. createElement(renderer.prepVML(markup), null, null, elem);
  4418. };
  4419. // Extend from 0 to 1
  4420. firstStop = stops[0];
  4421. lastStop = stops[stops.length - 1];
  4422. if (firstStop[0] > 0) {
  4423. stops.unshift([
  4424. 0,
  4425. firstStop[1]
  4426. ]);
  4427. }
  4428. if (lastStop[0] < 1) {
  4429. stops.push([
  4430. 1,
  4431. lastStop[1]
  4432. ]);
  4433. }
  4434. // Compute the stops
  4435. each(stops, function (stop, i) {
  4436. if (regexRgba.test(stop[1])) {
  4437. colorObject = Color(stop[1]);
  4438. stopColor = colorObject.get('rgb');
  4439. stopOpacity = colorObject.get('a');
  4440. } else {
  4441. stopColor = stop[1];
  4442. stopOpacity = 1;
  4443. }
  4444. // Build the color attribute
  4445. colors.push((stop[0] * 100) + '% ' + stopColor);
  4446. // Only start and end opacities are allowed, so we use the first and the last
  4447. if (!i) {
  4448. opacity1 = stopOpacity;
  4449. color2 = stopColor;
  4450. } else {
  4451. opacity2 = stopOpacity;
  4452. color1 = stopColor;
  4453. }
  4454. });
  4455. // Apply the gradient to fills only.
  4456. if (prop === 'fill') {
  4457. // Handle linear gradient angle
  4458. if (fillType === 'gradient') {
  4459. x1 = gradient.x1 || gradient[0] || 0;
  4460. y1 = gradient.y1 || gradient[1] || 0;
  4461. x2 = gradient.x2 || gradient[2] || 0;
  4462. y2 = gradient.y2 || gradient[3] || 0;
  4463. fillAttr = 'angle="' + (90 - math.atan(
  4464. (y2 - y1) / // y vector
  4465. (x2 - x1) // x vector
  4466. ) * 180 / mathPI) + '"';
  4467. addFillNode();
  4468. // Radial (circular) gradient
  4469. } else {
  4470. var r = gradient.r,
  4471. sizex = r * 2,
  4472. sizey = r * 2,
  4473. cx = gradient.cx,
  4474. cy = gradient.cy,
  4475. radialReference = elem.radialReference,
  4476. bBox,
  4477. applyRadialGradient = function () {
  4478. if (radialReference) {
  4479. bBox = wrapper.getBBox();
  4480. cx += (radialReference[0] - bBox.x) / bBox.width - 0.5;
  4481. cy += (radialReference[1] - bBox.y) / bBox.height - 0.5;
  4482. sizex *= radialReference[2] / bBox.width;
  4483. sizey *= radialReference[2] / bBox.height;
  4484. }
  4485. fillAttr = 'src="' + defaultOptions.global.VMLRadialGradientURL + '" ' +
  4486. 'size="' + sizex + ',' + sizey + '" ' +
  4487. 'origin="0.5,0.5" ' +
  4488. 'position="' + cx + ',' + cy + '" ' +
  4489. 'color2="' + color2 + '" ';
  4490. addFillNode();
  4491. };
  4492. // Apply radial gradient
  4493. if (wrapper.added) {
  4494. applyRadialGradient();
  4495. } else {
  4496. // We need to know the bounding box to get the size and position right
  4497. wrapper.onAdd = applyRadialGradient;
  4498. }
  4499. // The fill element's color attribute is broken in IE8 standards mode, so we
  4500. // need to set the parent shape's fillcolor attribute instead.
  4501. ret = color1;
  4502. }
  4503. // Gradients are not supported for VML stroke, return the first color. #722.
  4504. } else {
  4505. ret = stopColor;
  4506. }
  4507. // if the color is an rgba color, split it and add a fill node
  4508. // to hold the opacity component
  4509. } else if (regexRgba.test(color) && elem.tagName !== 'IMG') {
  4510. colorObject = Color(color);
  4511. markup = ['<', prop, ' opacity="', colorObject.get('a'), '"/>'];
  4512. createElement(this.prepVML(markup), null, null, elem);
  4513. ret = colorObject.get('rgb');
  4514. } else {
  4515. var propNodes = elem.getElementsByTagName(prop); // 'stroke' or 'fill' node
  4516. if (propNodes.length) {
  4517. propNodes[0].opacity = 1;
  4518. propNodes[0].type = 'solid';
  4519. }
  4520. ret = color;
  4521. }
  4522. return ret;
  4523. },
  4524. /**
  4525. * Take a VML string and prepare it for either IE8 or IE6/IE7.
  4526. * @param {Array} markup A string array of the VML markup to prepare
  4527. */
  4528. prepVML: function (markup) {
  4529. var vmlStyle = 'display:inline-block;behavior:url(#default#VML);',
  4530. isIE8 = this.isIE8;
  4531. markup = markup.join('');
  4532. if (isIE8) { // add xmlns and style inline
  4533. markup = markup.replace('/>', ' xmlns="urn:schemas-microsoft-com:vml" />');
  4534. if (markup.indexOf('style="') === -1) {
  4535. markup = markup.replace('/>', ' style="' + vmlStyle + '" />');
  4536. } else {
  4537. markup = markup.replace('style="', 'style="' + vmlStyle);
  4538. }
  4539. } else { // add namespace
  4540. markup = markup.replace('<', '<hcv:');
  4541. }
  4542. return markup;
  4543. },
  4544. /**
  4545. * Create rotated and aligned text
  4546. * @param {String} str
  4547. * @param {Number} x
  4548. * @param {Number} y
  4549. */
  4550. text: SVGRenderer.prototype.html,
  4551. /**
  4552. * Create and return a path element
  4553. * @param {Array} path
  4554. */
  4555. path: function (path) {
  4556. var attr = {
  4557. // subpixel precision down to 0.1 (width and height = 1px)
  4558. coordsize: '10 10'
  4559. };
  4560. if (isArray(path)) {
  4561. attr.d = path;
  4562. } else if (isObject(path)) { // attributes
  4563. extend(attr, path);
  4564. }
  4565. // create the shape
  4566. return this.createElement('shape').attr(attr);
  4567. },
  4568. /**
  4569. * Create and return a circle element. In VML circles are implemented as
  4570. * shapes, which is faster than v:oval
  4571. * @param {Number} x
  4572. * @param {Number} y
  4573. * @param {Number} r
  4574. */
  4575. circle: function (x, y, r) {
  4576. var circle = this.symbol('circle');
  4577. if (isObject(x)) {
  4578. r = x.r;
  4579. y = x.y;
  4580. x = x.x;
  4581. }
  4582. circle.isCircle = true; // Causes x and y to mean center (#1682)
  4583. circle.r = r;
  4584. return circle.attr({ x: x, y: y });
  4585. },
  4586. /**
  4587. * Create a group using an outer div and an inner v:group to allow rotating
  4588. * and flipping. A simple v:group would have problems with positioning
  4589. * child HTML elements and CSS clip.
  4590. *
  4591. * @param {String} name The name of the group
  4592. */
  4593. g: function (name) {
  4594. var wrapper,
  4595. attribs;
  4596. // set the class name
  4597. if (name) {
  4598. attribs = { 'className': PREFIX + name, 'class': PREFIX + name };
  4599. }
  4600. // the div to hold HTML and clipping
  4601. wrapper = this.createElement(DIV).attr(attribs);
  4602. return wrapper;
  4603. },
  4604. /**
  4605. * VML override to create a regular HTML image
  4606. * @param {String} src
  4607. * @param {Number} x
  4608. * @param {Number} y
  4609. * @param {Number} width
  4610. * @param {Number} height
  4611. */
  4612. image: function (src, x, y, width, height) {
  4613. var obj = this.createElement('img')
  4614. .attr({ src: src });
  4615. if (arguments.length > 1) {
  4616. obj.attr({
  4617. x: x,
  4618. y: y,
  4619. width: width,
  4620. height: height
  4621. });
  4622. }
  4623. return obj;
  4624. },
  4625. /**
  4626. * For rectangles, VML uses a shape for rect to overcome bugs and rotation problems
  4627. */
  4628. createElement: function (nodeName) {
  4629. return nodeName === 'rect' ? this.symbol(nodeName) : SVGRenderer.prototype.createElement.call(this, nodeName);
  4630. },
  4631. /**
  4632. * In the VML renderer, each child of an inverted div (group) is inverted
  4633. * @param {Object} element
  4634. * @param {Object} parentNode
  4635. */
  4636. invertChild: function (element, parentNode) {
  4637. var ren = this,
  4638. parentStyle = parentNode.style,
  4639. imgStyle = element.tagName === 'IMG' && element.style; // #1111
  4640. css(element, {
  4641. flip: 'x',
  4642. left: pInt(parentStyle.width) - (imgStyle ? pInt(imgStyle.top) : 1),
  4643. top: pInt(parentStyle.height) - (imgStyle ? pInt(imgStyle.left) : 1),
  4644. rotation: -90
  4645. });
  4646. // Recursively invert child elements, needed for nested composite shapes like box plots and error bars. #1680, #1806.
  4647. each(element.childNodes, function (child) {
  4648. ren.invertChild(child, element);
  4649. });
  4650. },
  4651. /**
  4652. * Symbol definitions that override the parent SVG renderer's symbols
  4653. *
  4654. */
  4655. symbols: {
  4656. // VML specific arc function
  4657. arc: function (x, y, w, h, options) {
  4658. var start = options.start,
  4659. end = options.end,
  4660. radius = options.r || w || h,
  4661. innerRadius = options.innerR,
  4662. cosStart = mathCos(start),
  4663. sinStart = mathSin(start),
  4664. cosEnd = mathCos(end),
  4665. sinEnd = mathSin(end),
  4666. ret;
  4667. if (end - start === 0) { // no angle, don't show it.
  4668. return ['x'];
  4669. }
  4670. ret = [
  4671. 'wa', // clockwise arc to
  4672. x - radius, // left
  4673. y - radius, // top
  4674. x + radius, // right
  4675. y + radius, // bottom
  4676. x + radius * cosStart, // start x
  4677. y + radius * sinStart, // start y
  4678. x + radius * cosEnd, // end x
  4679. y + radius * sinEnd // end y
  4680. ];
  4681. if (options.open && !innerRadius) {
  4682. ret.push(
  4683. 'e',
  4684. M,
  4685. x,// - innerRadius,
  4686. y// - innerRadius
  4687. );
  4688. }
  4689. ret.push(
  4690. 'at', // anti clockwise arc to
  4691. x - innerRadius, // left
  4692. y - innerRadius, // top
  4693. x + innerRadius, // right
  4694. y + innerRadius, // bottom
  4695. x + innerRadius * cosEnd, // start x
  4696. y + innerRadius * sinEnd, // start y
  4697. x + innerRadius * cosStart, // end x
  4698. y + innerRadius * sinStart, // end y
  4699. 'x', // finish path
  4700. 'e' // close
  4701. );
  4702. ret.isArc = true;
  4703. return ret;
  4704. },
  4705. // Add circle symbol path. This performs significantly faster than v:oval.
  4706. circle: function (x, y, w, h, wrapper) {
  4707. if (wrapper) {
  4708. w = h = 2 * wrapper.r;
  4709. }
  4710. // Center correction, #1682
  4711. if (wrapper && wrapper.isCircle) {
  4712. x -= w / 2;
  4713. y -= h / 2;
  4714. }
  4715. // Return the path
  4716. return [
  4717. 'wa', // clockwisearcto
  4718. x, // left
  4719. y, // top
  4720. x + w, // right
  4721. y + h, // bottom
  4722. x + w, // start x
  4723. y + h / 2, // start y
  4724. x + w, // end x
  4725. y + h / 2, // end y
  4726. //'x', // finish path
  4727. 'e' // close
  4728. ];
  4729. },
  4730. /**
  4731. * Add rectangle symbol path which eases rotation and omits arcsize problems
  4732. * compared to the built-in VML roundrect shape. When borders are not rounded,
  4733. * use the simpler square path, else use the callout path without the arrow.
  4734. */
  4735. rect: function (x, y, w, h, options) {
  4736. return SVGRenderer.prototype.symbols[
  4737. !defined(options) || !options.r ? 'square' : 'callout'
  4738. ].call(0, x, y, w, h, options);
  4739. }
  4740. }
  4741. };
  4742. Highcharts.VMLRenderer = VMLRenderer = function () {
  4743. this.init.apply(this, arguments);
  4744. };
  4745. VMLRenderer.prototype = merge(SVGRenderer.prototype, VMLRendererExtension);
  4746. // general renderer
  4747. Renderer = VMLRenderer;
  4748. }
  4749. // This method is used with exporting in old IE, when emulating SVG (see #2314)
  4750. SVGRenderer.prototype.measureSpanWidth = function (text, styles) {
  4751. var measuringSpan = doc.createElement('span'),
  4752. offsetWidth,
  4753. textNode = doc.createTextNode(text);
  4754. measuringSpan.appendChild(textNode);
  4755. css(measuringSpan, styles);
  4756. this.box.appendChild(measuringSpan);
  4757. offsetWidth = measuringSpan.offsetWidth;
  4758. discardElement(measuringSpan); // #2463
  4759. return offsetWidth;
  4760. };
  4761. /* ****************************************************************************
  4762. * *
  4763. * END OF INTERNET EXPLORER <= 8 SPECIFIC CODE *
  4764. * *
  4765. *****************************************************************************/
  4766. /* ****************************************************************************
  4767. * *
  4768. * START OF ANDROID < 3 SPECIFIC CODE. THIS CAN BE REMOVED IF YOU'RE NOT *
  4769. * TARGETING THAT SYSTEM. *
  4770. * *
  4771. *****************************************************************************/
  4772. var CanVGRenderer,
  4773. CanVGController;
  4774. if (useCanVG) {
  4775. /**
  4776. * The CanVGRenderer is empty from start to keep the source footprint small.
  4777. * When requested, the CanVGController downloads the rest of the source packaged
  4778. * together with the canvg library.
  4779. */
  4780. Highcharts.CanVGRenderer = CanVGRenderer = function () {
  4781. // Override the global SVG namespace to fake SVG/HTML that accepts CSS
  4782. SVG_NS = 'http://www.w3.org/1999/xhtml';
  4783. };
  4784. /**
  4785. * Start with an empty symbols object. This is needed when exporting is used (exporting.src.js will add a few symbols), but
  4786. * the implementation from SvgRenderer will not be merged in until first render.
  4787. */
  4788. CanVGRenderer.prototype.symbols = {};
  4789. /**
  4790. * Handles on demand download of canvg rendering support.
  4791. */
  4792. CanVGController = (function () {
  4793. // List of renderering calls
  4794. var deferredRenderCalls = [];
  4795. /**
  4796. * When downloaded, we are ready to draw deferred charts.
  4797. */
  4798. function drawDeferred() {
  4799. var callLength = deferredRenderCalls.length,
  4800. callIndex;
  4801. // Draw all pending render calls
  4802. for (callIndex = 0; callIndex < callLength; callIndex++) {
  4803. deferredRenderCalls[callIndex]();
  4804. }
  4805. // Clear the list
  4806. deferredRenderCalls = [];
  4807. }
  4808. return {
  4809. push: function (func, scriptLocation) {
  4810. // Only get the script once
  4811. if (deferredRenderCalls.length === 0) {
  4812. getScript(scriptLocation, drawDeferred);
  4813. }
  4814. // Register render call
  4815. deferredRenderCalls.push(func);
  4816. }
  4817. };
  4818. }());
  4819. Renderer = CanVGRenderer;
  4820. } // end CanVGRenderer
  4821. /* ****************************************************************************
  4822. * *
  4823. * END OF ANDROID < 3 SPECIFIC CODE *
  4824. * *
  4825. *****************************************************************************/
  4826. /**
  4827. * The Tick class
  4828. */
  4829. function Tick(axis, pos, type, noLabel) {
  4830. this.axis = axis;
  4831. this.pos = pos;
  4832. this.type = type || '';
  4833. this.isNew = true;
  4834. if (!type && !noLabel) {
  4835. this.addLabel();
  4836. }
  4837. }
  4838. Tick.prototype = {
  4839. /**
  4840. * Write the tick label
  4841. */
  4842. addLabel: function () {
  4843. var tick = this,
  4844. axis = tick.axis,
  4845. options = axis.options,
  4846. chart = axis.chart,
  4847. horiz = axis.horiz,
  4848. categories = axis.categories,
  4849. names = axis.names,
  4850. pos = tick.pos,
  4851. labelOptions = options.labels,
  4852. rotation = labelOptions.rotation,
  4853. str,
  4854. tickPositions = axis.tickPositions,
  4855. width = (horiz && categories &&
  4856. !labelOptions.step && !labelOptions.staggerLines &&
  4857. !labelOptions.rotation &&
  4858. chart.plotWidth / tickPositions.length) ||
  4859. (!horiz && (chart.margin[3] || chart.chartWidth * 0.33)), // #1580, #1931
  4860. isFirst = pos === tickPositions[0],
  4861. isLast = pos === tickPositions[tickPositions.length - 1],
  4862. css,
  4863. attr,
  4864. value = categories ?
  4865. pick(categories[pos], names[pos], pos) :
  4866. pos,
  4867. label = tick.label,
  4868. tickPositionInfo = tickPositions.info,
  4869. dateTimeLabelFormat;
  4870. // Set the datetime label format. If a higher rank is set for this position, use that. If not,
  4871. // use the general format.
  4872. if (axis.isDatetimeAxis && tickPositionInfo) {
  4873. dateTimeLabelFormat = options.dateTimeLabelFormats[tickPositionInfo.higherRanks[pos] || tickPositionInfo.unitName];
  4874. }
  4875. // set properties for access in render method
  4876. tick.isFirst = isFirst;
  4877. tick.isLast = isLast;
  4878. // get the string
  4879. str = axis.labelFormatter.call({
  4880. axis: axis,
  4881. chart: chart,
  4882. isFirst: isFirst,
  4883. isLast: isLast,
  4884. dateTimeLabelFormat: dateTimeLabelFormat,
  4885. value: axis.isLog ? correctFloat(lin2log(value)) : value
  4886. });
  4887. // prepare CSS
  4888. css = width && { width: mathMax(1, mathRound(width - 2 * (labelOptions.padding || 10))) + PX };
  4889. css = extend(css, labelOptions.style);
  4890. // first call
  4891. if (!defined(label)) {
  4892. attr = {
  4893. align: axis.labelAlign
  4894. };
  4895. if (isNumber(rotation)) {
  4896. attr.rotation = rotation;
  4897. }
  4898. if (width && labelOptions.ellipsis) {
  4899. css.HcHeight = axis.len / tickPositions.length;
  4900. }
  4901. tick.label = label =
  4902. defined(str) && labelOptions.enabled ?
  4903. chart.renderer.text(
  4904. str,
  4905. 0,
  4906. 0,
  4907. labelOptions.useHTML
  4908. )
  4909. .attr(attr)
  4910. // without position absolute, IE export sometimes is wrong
  4911. .css(css)
  4912. .add(axis.labelGroup) :
  4913. null;
  4914. // Set the tick baseline and correct for rotation (#1764)
  4915. axis.tickBaseline = chart.renderer.fontMetrics(labelOptions.style.fontSize, label).b;
  4916. if (rotation && axis.side === 2) {
  4917. axis.tickBaseline *= mathCos(rotation * deg2rad);
  4918. }
  4919. // update
  4920. } else if (label) {
  4921. label.attr({
  4922. text: str
  4923. })
  4924. .css(css);
  4925. }
  4926. tick.yOffset = label ? pick(labelOptions.y, axis.tickBaseline + (axis.side === 2 ? 8 : -(label.getBBox().height / 2))) : 0;
  4927. },
  4928. /**
  4929. * Get the offset height or width of the label
  4930. */
  4931. getLabelSize: function () {
  4932. var label = this.label,
  4933. axis = this.axis;
  4934. return label ?
  4935. label.getBBox()[axis.horiz ? 'height' : 'width'] :
  4936. 0;
  4937. },
  4938. /**
  4939. * Find how far the labels extend to the right and left of the tick's x position. Used for anti-collision
  4940. * detection with overflow logic.
  4941. */
  4942. getLabelSides: function () {
  4943. var bBox = this.label.getBBox(),
  4944. axis = this.axis,
  4945. horiz = axis.horiz,
  4946. options = axis.options,
  4947. labelOptions = options.labels,
  4948. size = horiz ? bBox.width : bBox.height,
  4949. leftSide = horiz ?
  4950. labelOptions.x - size * { left: 0, center: 0.5, right: 1 }[axis.labelAlign] :
  4951. 0,
  4952. rightSide = horiz ?
  4953. size + leftSide :
  4954. size;
  4955. return [leftSide, rightSide];
  4956. },
  4957. /**
  4958. * Handle the label overflow by adjusting the labels to the left and right edge, or
  4959. * hide them if they collide into the neighbour label.
  4960. */
  4961. handleOverflow: function (index, xy) {
  4962. var show = true,
  4963. axis = this.axis,
  4964. isFirst = this.isFirst,
  4965. isLast = this.isLast,
  4966. horiz = axis.horiz,
  4967. pxPos = horiz ? xy.x : xy.y,
  4968. reversed = axis.reversed,
  4969. tickPositions = axis.tickPositions,
  4970. sides = this.getLabelSides(),
  4971. leftSide = sides[0],
  4972. rightSide = sides[1],
  4973. axisLeft,
  4974. axisRight,
  4975. neighbour,
  4976. neighbourEdge,
  4977. line = this.label.line || 0,
  4978. labelEdge = axis.labelEdge,
  4979. justifyLabel = axis.justifyLabels && (isFirst || isLast),
  4980. justifyToPlot;
  4981. // Hide it if it now overlaps the neighbour label
  4982. if (labelEdge[line] === UNDEFINED || pxPos + leftSide > labelEdge[line]) {
  4983. labelEdge[line] = pxPos + rightSide;
  4984. } else if (!justifyLabel) {
  4985. show = false;
  4986. }
  4987. if (justifyLabel) {
  4988. justifyToPlot = axis.justifyToPlot;
  4989. axisLeft = justifyToPlot ? axis.pos : 0;
  4990. axisRight = justifyToPlot ? axisLeft + axis.len : axis.chart.chartWidth;
  4991. // Find the firsth neighbour on the same line
  4992. do {
  4993. index += (isFirst ? 1 : -1);
  4994. neighbour = axis.ticks[tickPositions[index]];
  4995. } while (tickPositions[index] && (!neighbour || !neighbour.label || neighbour.label.line !== line)); // #3044
  4996. neighbourEdge = neighbour && neighbour.label.xy && neighbour.label.xy.x + neighbour.getLabelSides()[isFirst ? 0 : 1];
  4997. if ((isFirst && !reversed) || (isLast && reversed)) {
  4998. // Is the label spilling out to the left of the plot area?
  4999. if (pxPos + leftSide < axisLeft) {
  5000. // Align it to plot left
  5001. pxPos = axisLeft - leftSide;
  5002. // Hide it if it now overlaps the neighbour label
  5003. if (neighbour && pxPos + rightSide > neighbourEdge) {
  5004. show = false;
  5005. }
  5006. }
  5007. } else {
  5008. // Is the label spilling out to the right of the plot area?
  5009. if (pxPos + rightSide > axisRight) {
  5010. // Align it to plot right
  5011. pxPos = axisRight - rightSide;
  5012. // Hide it if it now overlaps the neighbour label
  5013. if (neighbour && pxPos + leftSide < neighbourEdge) {
  5014. show = false;
  5015. }
  5016. }
  5017. }
  5018. // Set the modified x position of the label
  5019. xy.x = pxPos;
  5020. }
  5021. return show;
  5022. },
  5023. /**
  5024. * Get the x and y position for ticks and labels
  5025. */
  5026. getPosition: function (horiz, pos, tickmarkOffset, old) {
  5027. var axis = this.axis,
  5028. chart = axis.chart,
  5029. cHeight = (old && chart.oldChartHeight) || chart.chartHeight;
  5030. return {
  5031. x: horiz ?
  5032. axis.translate(pos + tickmarkOffset, null, null, old) + axis.transB :
  5033. axis.left + axis.offset + (axis.opposite ? ((old && chart.oldChartWidth) || chart.chartWidth) - axis.right - axis.left : 0),
  5034. y: horiz ?
  5035. cHeight - axis.bottom + axis.offset - (axis.opposite ? axis.height : 0) :
  5036. cHeight - axis.translate(pos + tickmarkOffset, null, null, old) - axis.transB
  5037. };
  5038. },
  5039. /**
  5040. * Get the x, y position of the tick label
  5041. */
  5042. getLabelPosition: function (x, y, label, horiz, labelOptions, tickmarkOffset, index, step) {
  5043. var axis = this.axis,
  5044. transA = axis.transA,
  5045. reversed = axis.reversed,
  5046. staggerLines = axis.staggerLines;
  5047. x = x + labelOptions.x - (tickmarkOffset && horiz ?
  5048. tickmarkOffset * transA * (reversed ? -1 : 1) : 0);
  5049. y = y + this.yOffset - (tickmarkOffset && !horiz ?
  5050. tickmarkOffset * transA * (reversed ? 1 : -1) : 0);
  5051. // Correct for staggered labels
  5052. if (staggerLines) {
  5053. label.line = (index / (step || 1) % staggerLines);
  5054. y += label.line * (axis.labelOffset / staggerLines);
  5055. }
  5056. return {
  5057. x: x,
  5058. y: y
  5059. };
  5060. },
  5061. /**
  5062. * Extendible method to return the path of the marker
  5063. */
  5064. getMarkPath: function (x, y, tickLength, tickWidth, horiz, renderer) {
  5065. return renderer.crispLine([
  5066. M,
  5067. x,
  5068. y,
  5069. L,
  5070. x + (horiz ? 0 : -tickLength),
  5071. y + (horiz ? tickLength : 0)
  5072. ], tickWidth);
  5073. },
  5074. /**
  5075. * Put everything in place
  5076. *
  5077. * @param index {Number}
  5078. * @param old {Boolean} Use old coordinates to prepare an animation into new position
  5079. */
  5080. render: function (index, old, opacity) {
  5081. var tick = this,
  5082. axis = tick.axis,
  5083. options = axis.options,
  5084. chart = axis.chart,
  5085. renderer = chart.renderer,
  5086. horiz = axis.horiz,
  5087. type = tick.type,
  5088. label = tick.label,
  5089. pos = tick.pos,
  5090. labelOptions = options.labels,
  5091. gridLine = tick.gridLine,
  5092. gridPrefix = type ? type + 'Grid' : 'grid',
  5093. tickPrefix = type ? type + 'Tick' : 'tick',
  5094. gridLineWidth = options[gridPrefix + 'LineWidth'],
  5095. gridLineColor = options[gridPrefix + 'LineColor'],
  5096. dashStyle = options[gridPrefix + 'LineDashStyle'],
  5097. tickLength = options[tickPrefix + 'Length'],
  5098. tickWidth = options[tickPrefix + 'Width'] || 0,
  5099. tickColor = options[tickPrefix + 'Color'],
  5100. tickPosition = options[tickPrefix + 'Position'],
  5101. gridLinePath,
  5102. mark = tick.mark,
  5103. markPath,
  5104. step = labelOptions.step,
  5105. attribs,
  5106. show = true,
  5107. tickmarkOffset = axis.tickmarkOffset,
  5108. xy = tick.getPosition(horiz, pos, tickmarkOffset, old),
  5109. x = xy.x,
  5110. y = xy.y,
  5111. reverseCrisp = ((horiz && x === axis.pos + axis.len) || (!horiz && y === axis.pos)) ? -1 : 1; // #1480, #1687
  5112. opacity = pick(opacity, 1);
  5113. this.isActive = true;
  5114. // create the grid line
  5115. if (gridLineWidth) {
  5116. gridLinePath = axis.getPlotLinePath(pos + tickmarkOffset, gridLineWidth * reverseCrisp, old, true);
  5117. if (gridLine === UNDEFINED) {
  5118. attribs = {
  5119. stroke: gridLineColor,
  5120. 'stroke-width': gridLineWidth
  5121. };
  5122. if (dashStyle) {
  5123. attribs.dashstyle = dashStyle;
  5124. }
  5125. if (!type) {
  5126. attribs.zIndex = 1;
  5127. }
  5128. if (old) {
  5129. attribs.opacity = 0;
  5130. }
  5131. tick.gridLine = gridLine =
  5132. gridLineWidth ?
  5133. renderer.path(gridLinePath)
  5134. .attr(attribs).add(axis.gridGroup) :
  5135. null;
  5136. }
  5137. // If the parameter 'old' is set, the current call will be followed
  5138. // by another call, therefore do not do any animations this time
  5139. if (!old && gridLine && gridLinePath) {
  5140. gridLine[tick.isNew ? 'attr' : 'animate']({
  5141. d: gridLinePath,
  5142. opacity: opacity
  5143. });
  5144. }
  5145. }
  5146. // create the tick mark
  5147. if (tickWidth && tickLength) {
  5148. // negate the length
  5149. if (tickPosition === 'inside') {
  5150. tickLength = -tickLength;
  5151. }
  5152. if (axis.opposite) {
  5153. tickLength = -tickLength;
  5154. }
  5155. markPath = tick.getMarkPath(x, y, tickLength, tickWidth * reverseCrisp, horiz, renderer);
  5156. if (mark) { // updating
  5157. mark.animate({
  5158. d: markPath,
  5159. opacity: opacity
  5160. });
  5161. } else { // first time
  5162. tick.mark = renderer.path(
  5163. markPath
  5164. ).attr({
  5165. stroke: tickColor,
  5166. 'stroke-width': tickWidth,
  5167. opacity: opacity
  5168. }).add(axis.axisGroup);
  5169. }
  5170. }
  5171. // the label is created on init - now move it into place
  5172. if (label && !isNaN(x)) {
  5173. label.xy = xy = tick.getLabelPosition(x, y, label, horiz, labelOptions, tickmarkOffset, index, step);
  5174. // Apply show first and show last. If the tick is both first and last, it is
  5175. // a single centered tick, in which case we show the label anyway (#2100).
  5176. if ((tick.isFirst && !tick.isLast && !pick(options.showFirstLabel, 1)) ||
  5177. (tick.isLast && !tick.isFirst && !pick(options.showLastLabel, 1))) {
  5178. show = false;
  5179. // Handle label overflow and show or hide accordingly
  5180. } else if (!axis.isRadial && !labelOptions.step && !labelOptions.rotation && !old && opacity !== 0) {
  5181. show = tick.handleOverflow(index, xy);
  5182. }
  5183. // apply step
  5184. if (step && index % step) {
  5185. // show those indices dividable by step
  5186. show = false;
  5187. }
  5188. // Set the new position, and show or hide
  5189. if (show && !isNaN(xy.y)) {
  5190. xy.opacity = opacity;
  5191. label[tick.isNew ? 'attr' : 'animate'](xy);
  5192. tick.isNew = false;
  5193. } else {
  5194. label.attr('y', -9999); // #1338
  5195. }
  5196. }
  5197. },
  5198. /**
  5199. * Destructor for the tick prototype
  5200. */
  5201. destroy: function () {
  5202. destroyObjectProperties(this, this.axis);
  5203. }
  5204. };
  5205. /**
  5206. * The object wrapper for plot lines and plot bands
  5207. * @param {Object} options
  5208. */
  5209. Highcharts.PlotLineOrBand = function (axis, options) {
  5210. this.axis = axis;
  5211. if (options) {
  5212. this.options = options;
  5213. this.id = options.id;
  5214. }
  5215. };
  5216. Highcharts.PlotLineOrBand.prototype = {
  5217. /**
  5218. * Render the plot line or plot band. If it is already existing,
  5219. * move it.
  5220. */
  5221. render: function () {
  5222. var plotLine = this,
  5223. axis = plotLine.axis,
  5224. horiz = axis.horiz,
  5225. halfPointRange = (axis.pointRange || 0) / 2,
  5226. options = plotLine.options,
  5227. optionsLabel = options.label,
  5228. label = plotLine.label,
  5229. width = options.width,
  5230. to = options.to,
  5231. from = options.from,
  5232. isBand = defined(from) && defined(to),
  5233. value = options.value,
  5234. dashStyle = options.dashStyle,
  5235. svgElem = plotLine.svgElem,
  5236. path = [],
  5237. addEvent,
  5238. eventType,
  5239. xs,
  5240. ys,
  5241. x,
  5242. y,
  5243. color = options.color,
  5244. zIndex = options.zIndex,
  5245. events = options.events,
  5246. attribs = {},
  5247. renderer = axis.chart.renderer;
  5248. // logarithmic conversion
  5249. if (axis.isLog) {
  5250. from = log2lin(from);
  5251. to = log2lin(to);
  5252. value = log2lin(value);
  5253. }
  5254. // plot line
  5255. if (width) {
  5256. path = axis.getPlotLinePath(value, width);
  5257. attribs = {
  5258. stroke: color,
  5259. 'stroke-width': width
  5260. };
  5261. if (dashStyle) {
  5262. attribs.dashstyle = dashStyle;
  5263. }
  5264. } else if (isBand) { // plot band
  5265. // keep within plot area
  5266. from = mathMax(from, axis.min - halfPointRange);
  5267. to = mathMin(to, axis.max + halfPointRange);
  5268. path = axis.getPlotBandPath(from, to, options);
  5269. if (color) {
  5270. attribs.fill = color;
  5271. }
  5272. if (options.borderWidth) {
  5273. attribs.stroke = options.borderColor;
  5274. attribs['stroke-width'] = options.borderWidth;
  5275. }
  5276. } else {
  5277. return;
  5278. }
  5279. // zIndex
  5280. if (defined(zIndex)) {
  5281. attribs.zIndex = zIndex;
  5282. }
  5283. // common for lines and bands
  5284. if (svgElem) {
  5285. if (path) {
  5286. svgElem.animate({
  5287. d: path
  5288. }, null, svgElem.onGetPath);
  5289. } else {
  5290. svgElem.hide();
  5291. svgElem.onGetPath = function () {
  5292. svgElem.show();
  5293. };
  5294. if (label) {
  5295. plotLine.label = label = label.destroy();
  5296. }
  5297. }
  5298. } else if (path && path.length) {
  5299. plotLine.svgElem = svgElem = renderer.path(path)
  5300. .attr(attribs).add();
  5301. // events
  5302. if (events) {
  5303. addEvent = function (eventType) {
  5304. svgElem.on(eventType, function (e) {
  5305. events[eventType].apply(plotLine, [e]);
  5306. });
  5307. };
  5308. for (eventType in events) {
  5309. addEvent(eventType);
  5310. }
  5311. }
  5312. }
  5313. // the plot band/line label
  5314. if (optionsLabel && defined(optionsLabel.text) && path && path.length && axis.width > 0 && axis.height > 0) {
  5315. // apply defaults
  5316. optionsLabel = merge({
  5317. align: horiz && isBand && 'center',
  5318. x: horiz ? !isBand && 4 : 10,
  5319. verticalAlign : !horiz && isBand && 'middle',
  5320. y: horiz ? isBand ? 16 : 10 : isBand ? 6 : -4,
  5321. rotation: horiz && !isBand && 90
  5322. }, optionsLabel);
  5323. // add the SVG element
  5324. if (!label) {
  5325. attribs = {
  5326. align: optionsLabel.textAlign || optionsLabel.align,
  5327. rotation: optionsLabel.rotation
  5328. };
  5329. if (defined(zIndex)) {
  5330. attribs.zIndex = zIndex;
  5331. }
  5332. plotLine.label = label = renderer.text(
  5333. optionsLabel.text,
  5334. 0,
  5335. 0,
  5336. optionsLabel.useHTML
  5337. )
  5338. .attr(attribs)
  5339. .css(optionsLabel.style)
  5340. .add();
  5341. }
  5342. // get the bounding box and align the label
  5343. // #3000 changed to better handle choice between plotband or plotline
  5344. xs = [path[1], path[4], (isBand ? path[6] : path[1])];
  5345. ys = [path[2], path[5], (isBand ? path[7] : path[2])];
  5346. x = arrayMin(xs);
  5347. y = arrayMin(ys);
  5348. label.align(optionsLabel, false, {
  5349. x: x,
  5350. y: y,
  5351. width: arrayMax(xs) - x,
  5352. height: arrayMax(ys) - y
  5353. });
  5354. label.show();
  5355. } else if (label) { // move out of sight
  5356. label.hide();
  5357. }
  5358. // chainable
  5359. return plotLine;
  5360. },
  5361. /**
  5362. * Remove the plot line or band
  5363. */
  5364. destroy: function () {
  5365. // remove it from the lookup
  5366. erase(this.axis.plotLinesAndBands, this);
  5367. delete this.axis;
  5368. destroyObjectProperties(this);
  5369. }
  5370. };
  5371. /**
  5372. * Object with members for extending the Axis prototype
  5373. */
  5374. AxisPlotLineOrBandExtension = {
  5375. /**
  5376. * Create the path for a plot band
  5377. */
  5378. getPlotBandPath: function (from, to) {
  5379. var toPath = this.getPlotLinePath(to),
  5380. path = this.getPlotLinePath(from);
  5381. if (path && toPath) {
  5382. path.push(
  5383. toPath[4],
  5384. toPath[5],
  5385. toPath[1],
  5386. toPath[2]
  5387. );
  5388. } else { // outside the axis area
  5389. path = null;
  5390. }
  5391. return path;
  5392. },
  5393. addPlotBand: function (options) {
  5394. return this.addPlotBandOrLine(options, 'plotBands');
  5395. },
  5396. addPlotLine: function (options) {
  5397. return this.addPlotBandOrLine(options, 'plotLines');
  5398. },
  5399. /**
  5400. * Add a plot band or plot line after render time
  5401. *
  5402. * @param options {Object} The plotBand or plotLine configuration object
  5403. */
  5404. addPlotBandOrLine: function (options, coll) {
  5405. var obj = new Highcharts.PlotLineOrBand(this, options).render(),
  5406. userOptions = this.userOptions;
  5407. if (obj) { // #2189
  5408. // Add it to the user options for exporting and Axis.update
  5409. if (coll) {
  5410. userOptions[coll] = userOptions[coll] || [];
  5411. userOptions[coll].push(options);
  5412. }
  5413. this.plotLinesAndBands.push(obj);
  5414. }
  5415. return obj;
  5416. },
  5417. /**
  5418. * Remove a plot band or plot line from the chart by id
  5419. * @param {Object} id
  5420. */
  5421. removePlotBandOrLine: function (id) {
  5422. var plotLinesAndBands = this.plotLinesAndBands,
  5423. options = this.options,
  5424. userOptions = this.userOptions,
  5425. i = plotLinesAndBands.length;
  5426. while (i--) {
  5427. if (plotLinesAndBands[i].id === id) {
  5428. plotLinesAndBands[i].destroy();
  5429. }
  5430. }
  5431. each([options.plotLines || [], userOptions.plotLines || [], options.plotBands || [], userOptions.plotBands || []], function (arr) {
  5432. i = arr.length;
  5433. while (i--) {
  5434. if (arr[i].id === id) {
  5435. erase(arr, arr[i]);
  5436. }
  5437. }
  5438. });
  5439. }
  5440. };
  5441. /**
  5442. * Create a new axis object
  5443. * @param {Object} chart
  5444. * @param {Object} options
  5445. */
  5446. function Axis() {
  5447. this.init.apply(this, arguments);
  5448. }
  5449. Axis.prototype = {
  5450. /**
  5451. * Default options for the X axis - the Y axis has extended defaults
  5452. */
  5453. defaultOptions: {
  5454. // allowDecimals: null,
  5455. // alternateGridColor: null,
  5456. // categories: [],
  5457. dateTimeLabelFormats: {
  5458. millisecond: '%H:%M:%S.%L',
  5459. second: '%H:%M:%S',
  5460. minute: '%H:%M',
  5461. hour: '%H:%M',
  5462. day: '%e. %b',
  5463. week: '%e. %b',
  5464. month: '%b \'%y',
  5465. year: '%Y'
  5466. },
  5467. endOnTick: false,
  5468. gridLineColor: '#C0C0C0',
  5469. // gridLineDashStyle: 'solid',
  5470. // gridLineWidth: 0,
  5471. // reversed: false,
  5472. labels: defaultLabelOptions,
  5473. // { step: null },
  5474. lineColor: '#C0D0E0',
  5475. lineWidth: 1,
  5476. //linkedTo: null,
  5477. //max: undefined,
  5478. //min: undefined,
  5479. minPadding: 0.01,
  5480. maxPadding: 0.01,
  5481. //minRange: null,
  5482. minorGridLineColor: '#E0E0E0',
  5483. // minorGridLineDashStyle: null,
  5484. minorGridLineWidth: 1,
  5485. minorTickColor: '#A0A0A0',
  5486. //minorTickInterval: null,
  5487. minorTickLength: 2,
  5488. minorTickPosition: 'outside', // inside or outside
  5489. //minorTickWidth: 0,
  5490. //opposite: false,
  5491. //offset: 0,
  5492. //plotBands: [{
  5493. // events: {},
  5494. // zIndex: 1,
  5495. // labels: { align, x, verticalAlign, y, style, rotation, textAlign }
  5496. //}],
  5497. //plotLines: [{
  5498. // events: {}
  5499. // dashStyle: {}
  5500. // zIndex:
  5501. // labels: { align, x, verticalAlign, y, style, rotation, textAlign }
  5502. //}],
  5503. //reversed: false,
  5504. // showFirstLabel: true,
  5505. // showLastLabel: true,
  5506. startOfWeek: 1,
  5507. startOnTick: false,
  5508. tickColor: '#C0D0E0',
  5509. //tickInterval: null,
  5510. tickLength: 10,
  5511. tickmarkPlacement: 'between', // on or between
  5512. tickPixelInterval: 100,
  5513. tickPosition: 'outside',
  5514. tickWidth: 1,
  5515. title: {
  5516. //text: null,
  5517. align: 'middle', // low, middle or high
  5518. //margin: 0 for horizontal, 10 for vertical axes,
  5519. //rotation: 0,
  5520. //side: 'outside',
  5521. style: {
  5522. color: '#707070'
  5523. }
  5524. //x: 0,
  5525. //y: 0
  5526. },
  5527. type: 'linear' // linear, logarithmic or datetime
  5528. },
  5529. /**
  5530. * This options set extends the defaultOptions for Y axes
  5531. */
  5532. defaultYAxisOptions: {
  5533. endOnTick: true,
  5534. gridLineWidth: 1,
  5535. tickPixelInterval: 72,
  5536. showLastLabel: true,
  5537. labels: {
  5538. x: -8,
  5539. y: 3
  5540. },
  5541. lineWidth: 0,
  5542. maxPadding: 0.05,
  5543. minPadding: 0.05,
  5544. startOnTick: true,
  5545. tickWidth: 0,
  5546. title: {
  5547. rotation: 270,
  5548. text: 'Values'
  5549. },
  5550. stackLabels: {
  5551. enabled: false,
  5552. //align: dynamic,
  5553. //y: dynamic,
  5554. //x: dynamic,
  5555. //verticalAlign: dynamic,
  5556. //textAlign: dynamic,
  5557. //rotation: 0,
  5558. formatter: function () {
  5559. return numberFormat(this.total, -1);
  5560. },
  5561. style: defaultLabelOptions.style
  5562. }
  5563. },
  5564. /**
  5565. * These options extend the defaultOptions for left axes
  5566. */
  5567. defaultLeftAxisOptions: {
  5568. labels: {
  5569. x: -15,
  5570. y: null
  5571. },
  5572. title: {
  5573. rotation: 270
  5574. }
  5575. },
  5576. /**
  5577. * These options extend the defaultOptions for right axes
  5578. */
  5579. defaultRightAxisOptions: {
  5580. labels: {
  5581. x: 15,
  5582. y: null
  5583. },
  5584. title: {
  5585. rotation: 90
  5586. }
  5587. },
  5588. /**
  5589. * These options extend the defaultOptions for bottom axes
  5590. */
  5591. defaultBottomAxisOptions: {
  5592. labels: {
  5593. x: 0,
  5594. y: null // based on font size
  5595. // overflow: undefined,
  5596. // staggerLines: null
  5597. },
  5598. title: {
  5599. rotation: 0
  5600. }
  5601. },
  5602. /**
  5603. * These options extend the defaultOptions for left axes
  5604. */
  5605. defaultTopAxisOptions: {
  5606. labels: {
  5607. x: 0,
  5608. y: -15
  5609. // overflow: undefined
  5610. // staggerLines: null
  5611. },
  5612. title: {
  5613. rotation: 0
  5614. }
  5615. },
  5616. /**
  5617. * Initialize the axis
  5618. */
  5619. init: function (chart, userOptions) {
  5620. var isXAxis = userOptions.isX,
  5621. axis = this;
  5622. // Flag, is the axis horizontal
  5623. axis.horiz = chart.inverted ? !isXAxis : isXAxis;
  5624. // Flag, isXAxis
  5625. axis.isXAxis = isXAxis;
  5626. axis.coll = isXAxis ? 'xAxis' : 'yAxis';
  5627. axis.opposite = userOptions.opposite; // needed in setOptions
  5628. axis.side = userOptions.side || (axis.horiz ?
  5629. (axis.opposite ? 0 : 2) : // top : bottom
  5630. (axis.opposite ? 1 : 3)); // right : left
  5631. axis.setOptions(userOptions);
  5632. var options = this.options,
  5633. type = options.type,
  5634. isDatetimeAxis = type === 'datetime';
  5635. axis.labelFormatter = options.labels.formatter || axis.defaultLabelFormatter; // can be overwritten by dynamic format
  5636. // Flag, stagger lines or not
  5637. axis.userOptions = userOptions;
  5638. //axis.axisTitleMargin = UNDEFINED,// = options.title.margin,
  5639. axis.minPixelPadding = 0;
  5640. //axis.ignoreMinPadding = UNDEFINED; // can be set to true by a column or bar series
  5641. //axis.ignoreMaxPadding = UNDEFINED;
  5642. axis.chart = chart;
  5643. axis.reversed = options.reversed;
  5644. axis.zoomEnabled = options.zoomEnabled !== false;
  5645. // Initial categories
  5646. axis.categories = options.categories || type === 'category';
  5647. axis.names = [];
  5648. // Elements
  5649. //axis.axisGroup = UNDEFINED;
  5650. //axis.gridGroup = UNDEFINED;
  5651. //axis.axisTitle = UNDEFINED;
  5652. //axis.axisLine = UNDEFINED;
  5653. // Shorthand types
  5654. axis.isLog = type === 'logarithmic';
  5655. axis.isDatetimeAxis = isDatetimeAxis;
  5656. // Flag, if axis is linked to another axis
  5657. axis.isLinked = defined(options.linkedTo);
  5658. // Linked axis.
  5659. //axis.linkedParent = UNDEFINED;
  5660. // Tick positions
  5661. //axis.tickPositions = UNDEFINED; // array containing predefined positions
  5662. // Tick intervals
  5663. //axis.tickInterval = UNDEFINED;
  5664. //axis.minorTickInterval = UNDEFINED;
  5665. axis.tickmarkOffset = (axis.categories && options.tickmarkPlacement === 'between') ? 0.5 : 0;
  5666. // Major ticks
  5667. axis.ticks = {};
  5668. axis.labelEdge = [];
  5669. // Minor ticks
  5670. axis.minorTicks = {};
  5671. //axis.tickAmount = UNDEFINED;
  5672. // List of plotLines/Bands
  5673. axis.plotLinesAndBands = [];
  5674. // Alternate bands
  5675. axis.alternateBands = {};
  5676. // Axis metrics
  5677. //axis.left = UNDEFINED;
  5678. //axis.top = UNDEFINED;
  5679. //axis.width = UNDEFINED;
  5680. //axis.height = UNDEFINED;
  5681. //axis.bottom = UNDEFINED;
  5682. //axis.right = UNDEFINED;
  5683. //axis.transA = UNDEFINED;
  5684. //axis.transB = UNDEFINED;
  5685. //axis.oldTransA = UNDEFINED;
  5686. axis.len = 0;
  5687. //axis.oldMin = UNDEFINED;
  5688. //axis.oldMax = UNDEFINED;
  5689. //axis.oldUserMin = UNDEFINED;
  5690. //axis.oldUserMax = UNDEFINED;
  5691. //axis.oldAxisLength = UNDEFINED;
  5692. axis.minRange = axis.userMinRange = options.minRange || options.maxZoom;
  5693. axis.range = options.range;
  5694. axis.offset = options.offset || 0;
  5695. // Dictionary for stacks
  5696. axis.stacks = {};
  5697. axis.oldStacks = {};
  5698. // Min and max in the data
  5699. //axis.dataMin = UNDEFINED,
  5700. //axis.dataMax = UNDEFINED,
  5701. // The axis range
  5702. axis.max = null;
  5703. axis.min = null;
  5704. // User set min and max
  5705. //axis.userMin = UNDEFINED,
  5706. //axis.userMax = UNDEFINED,
  5707. // Crosshair options
  5708. axis.crosshair = pick(options.crosshair, splat(chart.options.tooltip.crosshairs)[isXAxis ? 0 : 1], false);
  5709. // Run Axis
  5710. var eventType,
  5711. events = axis.options.events;
  5712. // Register
  5713. if (inArray(axis, chart.axes) === -1) { // don't add it again on Axis.update()
  5714. if (isXAxis && !this.isColorAxis) { // #2713
  5715. chart.axes.splice(chart.xAxis.length, 0, axis);
  5716. } else {
  5717. chart.axes.push(axis);
  5718. }
  5719. chart[axis.coll].push(axis);
  5720. }
  5721. axis.series = axis.series || []; // populated by Series
  5722. // inverted charts have reversed xAxes as default
  5723. if (chart.inverted && isXAxis && axis.reversed === UNDEFINED) {
  5724. axis.reversed = true;
  5725. }
  5726. axis.removePlotBand = axis.removePlotBandOrLine;
  5727. axis.removePlotLine = axis.removePlotBandOrLine;
  5728. // register event listeners
  5729. for (eventType in events) {
  5730. addEvent(axis, eventType, events[eventType]);
  5731. }
  5732. // extend logarithmic axis
  5733. if (axis.isLog) {
  5734. axis.val2lin = log2lin;
  5735. axis.lin2val = lin2log;
  5736. }
  5737. },
  5738. /**
  5739. * Merge and set options
  5740. */
  5741. setOptions: function (userOptions) {
  5742. this.options = merge(
  5743. this.defaultOptions,
  5744. this.isXAxis ? {} : this.defaultYAxisOptions,
  5745. [this.defaultTopAxisOptions, this.defaultRightAxisOptions,
  5746. this.defaultBottomAxisOptions, this.defaultLeftAxisOptions][this.side],
  5747. merge(
  5748. defaultOptions[this.coll], // if set in setOptions (#1053)
  5749. userOptions
  5750. )
  5751. );
  5752. },
  5753. /**
  5754. * The default label formatter. The context is a special config object for the label.
  5755. */
  5756. defaultLabelFormatter: function () {
  5757. var axis = this.axis,
  5758. value = this.value,
  5759. categories = axis.categories,
  5760. dateTimeLabelFormat = this.dateTimeLabelFormat,
  5761. numericSymbols = defaultOptions.lang.numericSymbols,
  5762. i = numericSymbols && numericSymbols.length,
  5763. multi,
  5764. ret,
  5765. formatOption = axis.options.labels.format,
  5766. // make sure the same symbol is added for all labels on a linear axis
  5767. numericSymbolDetector = axis.isLog ? value : axis.tickInterval;
  5768. if (formatOption) {
  5769. ret = format(formatOption, this);
  5770. } else if (categories) {
  5771. ret = value;
  5772. } else if (dateTimeLabelFormat) { // datetime axis
  5773. ret = dateFormat(dateTimeLabelFormat, value);
  5774. } else if (i && numericSymbolDetector >= 1000) {
  5775. // Decide whether we should add a numeric symbol like k (thousands) or M (millions).
  5776. // If we are to enable this in tooltip or other places as well, we can move this
  5777. // logic to the numberFormatter and enable it by a parameter.
  5778. while (i-- && ret === UNDEFINED) {
  5779. multi = Math.pow(1000, i + 1);
  5780. if (numericSymbolDetector >= multi && numericSymbols[i] !== null) {
  5781. ret = numberFormat(value / multi, -1) + numericSymbols[i];
  5782. }
  5783. }
  5784. }
  5785. if (ret === UNDEFINED) {
  5786. if (mathAbs(value) >= 10000) { // add thousands separators
  5787. ret = numberFormat(value, 0);
  5788. } else { // small numbers
  5789. ret = numberFormat(value, -1, UNDEFINED, ''); // #2466
  5790. }
  5791. }
  5792. return ret;
  5793. },
  5794. /**
  5795. * Get the minimum and maximum for the series of each axis
  5796. */
  5797. getSeriesExtremes: function () {
  5798. var axis = this,
  5799. chart = axis.chart;
  5800. axis.hasVisibleSeries = false;
  5801. // reset dataMin and dataMax in case we're redrawing
  5802. axis.dataMin = axis.dataMax = null;
  5803. if (axis.buildStacks) {
  5804. axis.buildStacks();
  5805. }
  5806. // loop through this axis' series
  5807. each(axis.series, function (series) {
  5808. if (series.visible || !chart.options.chart.ignoreHiddenSeries) {
  5809. var seriesOptions = series.options,
  5810. xData,
  5811. threshold = seriesOptions.threshold,
  5812. seriesDataMin,
  5813. seriesDataMax;
  5814. axis.hasVisibleSeries = true;
  5815. // Validate threshold in logarithmic axes
  5816. if (axis.isLog && threshold <= 0) {
  5817. threshold = null;
  5818. }
  5819. // Get dataMin and dataMax for X axes
  5820. if (axis.isXAxis) {
  5821. xData = series.xData;
  5822. if (xData.length) {
  5823. axis.dataMin = mathMin(pick(axis.dataMin, xData[0]), arrayMin(xData));
  5824. axis.dataMax = mathMax(pick(axis.dataMax, xData[0]), arrayMax(xData));
  5825. }
  5826. // Get dataMin and dataMax for Y axes, as well as handle stacking and processed data
  5827. } else {
  5828. // Get this particular series extremes
  5829. series.getExtremes();
  5830. seriesDataMax = series.dataMax;
  5831. seriesDataMin = series.dataMin;
  5832. // Get the dataMin and dataMax so far. If percentage is used, the min and max are
  5833. // always 0 and 100. If seriesDataMin and seriesDataMax is null, then series
  5834. // doesn't have active y data, we continue with nulls
  5835. if (defined(seriesDataMin) && defined(seriesDataMax)) {
  5836. axis.dataMin = mathMin(pick(axis.dataMin, seriesDataMin), seriesDataMin);
  5837. axis.dataMax = mathMax(pick(axis.dataMax, seriesDataMax), seriesDataMax);
  5838. }
  5839. // Adjust to threshold
  5840. if (defined(threshold)) {
  5841. if (axis.dataMin >= threshold) {
  5842. axis.dataMin = threshold;
  5843. axis.ignoreMinPadding = true;
  5844. } else if (axis.dataMax < threshold) {
  5845. axis.dataMax = threshold;
  5846. axis.ignoreMaxPadding = true;
  5847. }
  5848. }
  5849. }
  5850. }
  5851. });
  5852. },
  5853. /**
  5854. * Translate from axis value to pixel position on the chart, or back
  5855. *
  5856. */
  5857. translate: function (val, backwards, cvsCoord, old, handleLog, pointPlacement) {
  5858. var axis = this,
  5859. sign = 1,
  5860. cvsOffset = 0,
  5861. localA = old ? axis.oldTransA : axis.transA,
  5862. localMin = old ? axis.oldMin : axis.min,
  5863. returnValue,
  5864. minPixelPadding = axis.minPixelPadding,
  5865. postTranslate = (axis.options.ordinal || (axis.isLog && handleLog)) && axis.lin2val;
  5866. if (!localA) {
  5867. localA = axis.transA;
  5868. }
  5869. // In vertical axes, the canvas coordinates start from 0 at the top like in
  5870. // SVG.
  5871. if (cvsCoord) {
  5872. sign *= -1; // canvas coordinates inverts the value
  5873. cvsOffset = axis.len;
  5874. }
  5875. // Handle reversed axis
  5876. if (axis.reversed) {
  5877. sign *= -1;
  5878. cvsOffset -= sign * (axis.sector || axis.len);
  5879. }
  5880. // From pixels to value
  5881. if (backwards) { // reverse translation
  5882. val = val * sign + cvsOffset;
  5883. val -= minPixelPadding;
  5884. returnValue = val / localA + localMin; // from chart pixel to value
  5885. if (postTranslate) { // log and ordinal axes
  5886. returnValue = axis.lin2val(returnValue);
  5887. }
  5888. // From value to pixels
  5889. } else {
  5890. if (postTranslate) { // log and ordinal axes
  5891. val = axis.val2lin(val);
  5892. }
  5893. if (pointPlacement === 'between') {
  5894. pointPlacement = 0.5;
  5895. }
  5896. returnValue = sign * (val - localMin) * localA + cvsOffset + (sign * minPixelPadding) +
  5897. (isNumber(pointPlacement) ? localA * pointPlacement * axis.pointRange : 0);
  5898. }
  5899. return returnValue;
  5900. },
  5901. /**
  5902. * Utility method to translate an axis value to pixel position.
  5903. * @param {Number} value A value in terms of axis units
  5904. * @param {Boolean} paneCoordinates Whether to return the pixel coordinate relative to the chart
  5905. * or just the axis/pane itself.
  5906. */
  5907. toPixels: function (value, paneCoordinates) {
  5908. return this.translate(value, false, !this.horiz, null, true) + (paneCoordinates ? 0 : this.pos);
  5909. },
  5910. /*
  5911. * Utility method to translate a pixel position in to an axis value
  5912. * @param {Number} pixel The pixel value coordinate
  5913. * @param {Boolean} paneCoordiantes Whether the input pixel is relative to the chart or just the
  5914. * axis/pane itself.
  5915. */
  5916. toValue: function (pixel, paneCoordinates) {
  5917. return this.translate(pixel - (paneCoordinates ? 0 : this.pos), true, !this.horiz, null, true);
  5918. },
  5919. /**
  5920. * Create the path for a plot line that goes from the given value on
  5921. * this axis, across the plot to the opposite side
  5922. * @param {Number} value
  5923. * @param {Number} lineWidth Used for calculation crisp line
  5924. * @param {Number] old Use old coordinates (for resizing and rescaling)
  5925. */
  5926. getPlotLinePath: function (value, lineWidth, old, force, translatedValue) {
  5927. var axis = this,
  5928. chart = axis.chart,
  5929. axisLeft = axis.left,
  5930. axisTop = axis.top,
  5931. x1,
  5932. y1,
  5933. x2,
  5934. y2,
  5935. cHeight = (old && chart.oldChartHeight) || chart.chartHeight,
  5936. cWidth = (old && chart.oldChartWidth) || chart.chartWidth,
  5937. skip,
  5938. transB = axis.transB;
  5939. translatedValue = pick(translatedValue, axis.translate(value, null, null, old));
  5940. x1 = x2 = mathRound(translatedValue + transB);
  5941. y1 = y2 = mathRound(cHeight - translatedValue - transB);
  5942. if (isNaN(translatedValue)) { // no min or max
  5943. skip = true;
  5944. } else if (axis.horiz) {
  5945. y1 = axisTop;
  5946. y2 = cHeight - axis.bottom;
  5947. if (x1 < axisLeft || x1 > axisLeft + axis.width) {
  5948. skip = true;
  5949. }
  5950. } else {
  5951. x1 = axisLeft;
  5952. x2 = cWidth - axis.right;
  5953. if (y1 < axisTop || y1 > axisTop + axis.height) {
  5954. skip = true;
  5955. }
  5956. }
  5957. return skip && !force ?
  5958. null :
  5959. chart.renderer.crispLine([M, x1, y1, L, x2, y2], lineWidth || 1);
  5960. },
  5961. /**
  5962. * Set the tick positions of a linear axis to round values like whole tens or every five.
  5963. */
  5964. getLinearTickPositions: function (tickInterval, min, max) {
  5965. var pos,
  5966. lastPos,
  5967. roundedMin = correctFloat(mathFloor(min / tickInterval) * tickInterval),
  5968. roundedMax = correctFloat(mathCeil(max / tickInterval) * tickInterval),
  5969. tickPositions = [];
  5970. // For single points, add a tick regardless of the relative position (#2662)
  5971. if (min === max && isNumber(min)) {
  5972. return [min];
  5973. }
  5974. // Populate the intermediate values
  5975. pos = roundedMin;
  5976. while (pos <= roundedMax) {
  5977. // Place the tick on the rounded value
  5978. tickPositions.push(pos);
  5979. // Always add the raw tickInterval, not the corrected one.
  5980. pos = correctFloat(pos + tickInterval);
  5981. // If the interval is not big enough in the current min - max range to actually increase
  5982. // the loop variable, we need to break out to prevent endless loop. Issue #619
  5983. if (pos === lastPos) {
  5984. break;
  5985. }
  5986. // Record the last value
  5987. lastPos = pos;
  5988. }
  5989. return tickPositions;
  5990. },
  5991. /**
  5992. * Return the minor tick positions. For logarithmic axes, reuse the same logic
  5993. * as for major ticks.
  5994. */
  5995. getMinorTickPositions: function () {
  5996. var axis = this,
  5997. options = axis.options,
  5998. tickPositions = axis.tickPositions,
  5999. minorTickInterval = axis.minorTickInterval,
  6000. minorTickPositions = [],
  6001. pos,
  6002. i,
  6003. len;
  6004. if (axis.isLog) {
  6005. len = tickPositions.length;
  6006. for (i = 1; i < len; i++) {
  6007. minorTickPositions = minorTickPositions.concat(
  6008. axis.getLogTickPositions(minorTickInterval, tickPositions[i - 1], tickPositions[i], true)
  6009. );
  6010. }
  6011. } else if (axis.isDatetimeAxis && options.minorTickInterval === 'auto') { // #1314
  6012. minorTickPositions = minorTickPositions.concat(
  6013. axis.getTimeTicks(
  6014. axis.normalizeTimeTickInterval(minorTickInterval),
  6015. axis.min,
  6016. axis.max,
  6017. options.startOfWeek
  6018. )
  6019. );
  6020. if (minorTickPositions[0] < axis.min) {
  6021. minorTickPositions.shift();
  6022. }
  6023. } else {
  6024. for (pos = axis.min + (tickPositions[0] - axis.min) % minorTickInterval; pos <= axis.max; pos += minorTickInterval) {
  6025. minorTickPositions.push(pos);
  6026. }
  6027. }
  6028. return minorTickPositions;
  6029. },
  6030. /**
  6031. * Adjust the min and max for the minimum range. Keep in mind that the series data is
  6032. * not yet processed, so we don't have information on data cropping and grouping, or
  6033. * updated axis.pointRange or series.pointRange. The data can't be processed until
  6034. * we have finally established min and max.
  6035. */
  6036. adjustForMinRange: function () {
  6037. var axis = this,
  6038. options = axis.options,
  6039. min = axis.min,
  6040. max = axis.max,
  6041. zoomOffset,
  6042. spaceAvailable = axis.dataMax - axis.dataMin >= axis.minRange,
  6043. closestDataRange,
  6044. i,
  6045. distance,
  6046. xData,
  6047. loopLength,
  6048. minArgs,
  6049. maxArgs;
  6050. // Set the automatic minimum range based on the closest point distance
  6051. if (axis.isXAxis && axis.minRange === UNDEFINED && !axis.isLog) {
  6052. if (defined(options.min) || defined(options.max)) {
  6053. axis.minRange = null; // don't do this again
  6054. } else {
  6055. // Find the closest distance between raw data points, as opposed to
  6056. // closestPointRange that applies to processed points (cropped and grouped)
  6057. each(axis.series, function (series) {
  6058. xData = series.xData;
  6059. loopLength = series.xIncrement ? 1 : xData.length - 1;
  6060. for (i = loopLength; i > 0; i--) {
  6061. distance = xData[i] - xData[i - 1];
  6062. if (closestDataRange === UNDEFINED || distance < closestDataRange) {
  6063. closestDataRange = distance;
  6064. }
  6065. }
  6066. });
  6067. axis.minRange = mathMin(closestDataRange * 5, axis.dataMax - axis.dataMin);
  6068. }
  6069. }
  6070. // if minRange is exceeded, adjust
  6071. if (max - min < axis.minRange) {
  6072. var minRange = axis.minRange;
  6073. zoomOffset = (minRange - max + min) / 2;
  6074. // if min and max options have been set, don't go beyond it
  6075. minArgs = [min - zoomOffset, pick(options.min, min - zoomOffset)];
  6076. if (spaceAvailable) { // if space is available, stay within the data range
  6077. minArgs[2] = axis.dataMin;
  6078. }
  6079. min = arrayMax(minArgs);
  6080. maxArgs = [min + minRange, pick(options.max, min + minRange)];
  6081. if (spaceAvailable) { // if space is availabe, stay within the data range
  6082. maxArgs[2] = axis.dataMax;
  6083. }
  6084. max = arrayMin(maxArgs);
  6085. // now if the max is adjusted, adjust the min back
  6086. if (max - min < minRange) {
  6087. minArgs[0] = max - minRange;
  6088. minArgs[1] = pick(options.min, max - minRange);
  6089. min = arrayMax(minArgs);
  6090. }
  6091. }
  6092. // Record modified extremes
  6093. axis.min = min;
  6094. axis.max = max;
  6095. },
  6096. /**
  6097. * Update translation information
  6098. */
  6099. setAxisTranslation: function (saveOld) {
  6100. var axis = this,
  6101. range = axis.max - axis.min,
  6102. pointRange = axis.axisPointRange || 0,
  6103. closestPointRange,
  6104. minPointOffset = 0,
  6105. pointRangePadding = 0,
  6106. linkedParent = axis.linkedParent,
  6107. ordinalCorrection,
  6108. hasCategories = !!axis.categories,
  6109. transA = axis.transA;
  6110. // Adjust translation for padding. Y axis with categories need to go through the same (#1784).
  6111. if (axis.isXAxis || hasCategories || pointRange) {
  6112. if (linkedParent) {
  6113. minPointOffset = linkedParent.minPointOffset;
  6114. pointRangePadding = linkedParent.pointRangePadding;
  6115. } else {
  6116. each(axis.series, function (series) {
  6117. var seriesPointRange = hasCategories ? 1 : (axis.isXAxis ? series.pointRange : (axis.axisPointRange || 0)), // #2806
  6118. pointPlacement = series.options.pointPlacement,
  6119. seriesClosestPointRange = series.closestPointRange;
  6120. if (seriesPointRange > range) { // #1446
  6121. seriesPointRange = 0;
  6122. }
  6123. pointRange = mathMax(pointRange, seriesPointRange);
  6124. // minPointOffset is the value padding to the left of the axis in order to make
  6125. // room for points with a pointRange, typically columns. When the pointPlacement option
  6126. // is 'between' or 'on', this padding does not apply.
  6127. minPointOffset = mathMax(
  6128. minPointOffset,
  6129. isString(pointPlacement) ? 0 : seriesPointRange / 2
  6130. );
  6131. // Determine the total padding needed to the length of the axis to make room for the
  6132. // pointRange. If the series' pointPlacement is 'on', no padding is added.
  6133. pointRangePadding = mathMax(
  6134. pointRangePadding,
  6135. pointPlacement === 'on' ? 0 : seriesPointRange
  6136. );
  6137. // Set the closestPointRange
  6138. if (!series.noSharedTooltip && defined(seriesClosestPointRange)) {
  6139. closestPointRange = defined(closestPointRange) ?
  6140. mathMin(closestPointRange, seriesClosestPointRange) :
  6141. seriesClosestPointRange;
  6142. }
  6143. });
  6144. }
  6145. // Record minPointOffset and pointRangePadding
  6146. ordinalCorrection = axis.ordinalSlope && closestPointRange ? axis.ordinalSlope / closestPointRange : 1; // #988, #1853
  6147. axis.minPointOffset = minPointOffset = minPointOffset * ordinalCorrection;
  6148. axis.pointRangePadding = pointRangePadding = pointRangePadding * ordinalCorrection;
  6149. // pointRange means the width reserved for each point, like in a column chart
  6150. axis.pointRange = mathMin(pointRange, range);
  6151. // closestPointRange means the closest distance between points. In columns
  6152. // it is mostly equal to pointRange, but in lines pointRange is 0 while closestPointRange
  6153. // is some other value
  6154. axis.closestPointRange = closestPointRange;
  6155. }
  6156. // Secondary values
  6157. if (saveOld) {
  6158. axis.oldTransA = transA;
  6159. }
  6160. axis.translationSlope = axis.transA = transA = axis.len / ((range + pointRangePadding) || 1);
  6161. axis.transB = axis.horiz ? axis.left : axis.bottom; // translation addend
  6162. axis.minPixelPadding = transA * minPointOffset;
  6163. },
  6164. /**
  6165. * Set the tick positions to round values and optionally extend the extremes
  6166. * to the nearest tick
  6167. */
  6168. setTickPositions: function (secondPass) {
  6169. var axis = this,
  6170. chart = axis.chart,
  6171. options = axis.options,
  6172. startOnTick = options.startOnTick,
  6173. endOnTick = options.endOnTick,
  6174. isLog = axis.isLog,
  6175. isDatetimeAxis = axis.isDatetimeAxis,
  6176. isXAxis = axis.isXAxis,
  6177. isLinked = axis.isLinked,
  6178. tickPositioner = axis.options.tickPositioner,
  6179. maxPadding = options.maxPadding,
  6180. minPadding = options.minPadding,
  6181. length,
  6182. linkedParentExtremes,
  6183. tickIntervalOption = options.tickInterval,
  6184. minTickIntervalOption = options.minTickInterval,
  6185. tickPixelIntervalOption = options.tickPixelInterval,
  6186. tickPositions,
  6187. keepTwoTicksOnly,
  6188. categories = axis.categories;
  6189. // linked axis gets the extremes from the parent axis
  6190. if (isLinked) {
  6191. axis.linkedParent = chart[axis.coll][options.linkedTo];
  6192. linkedParentExtremes = axis.linkedParent.getExtremes();
  6193. axis.min = pick(linkedParentExtremes.min, linkedParentExtremes.dataMin);
  6194. axis.max = pick(linkedParentExtremes.max, linkedParentExtremes.dataMax);
  6195. if (options.type !== axis.linkedParent.options.type) {
  6196. error(11, 1); // Can't link axes of different type
  6197. }
  6198. } else { // initial min and max from the extreme data values
  6199. axis.min = pick(axis.userMin, options.min, axis.dataMin);
  6200. axis.max = pick(axis.userMax, options.max, axis.dataMax);
  6201. }
  6202. if (isLog) {
  6203. if (!secondPass && mathMin(axis.min, pick(axis.dataMin, axis.min)) <= 0) { // #978
  6204. error(10, 1); // Can't plot negative values on log axis
  6205. }
  6206. axis.min = correctFloat(log2lin(axis.min)); // correctFloat cures #934
  6207. axis.max = correctFloat(log2lin(axis.max));
  6208. }
  6209. // handle zoomed range
  6210. if (axis.range && defined(axis.max)) {
  6211. axis.userMin = axis.min = mathMax(axis.min, axis.max - axis.range); // #618
  6212. axis.userMax = axis.max;
  6213. axis.range = null; // don't use it when running setExtremes
  6214. }
  6215. // Hook for adjusting this.min and this.max. Used by bubble series.
  6216. if (axis.beforePadding) {
  6217. axis.beforePadding();
  6218. }
  6219. // adjust min and max for the minimum range
  6220. axis.adjustForMinRange();
  6221. // Pad the values to get clear of the chart's edges. To avoid tickInterval taking the padding
  6222. // into account, we do this after computing tick interval (#1337).
  6223. if (!categories && !axis.axisPointRange && !axis.usePercentage && !isLinked && defined(axis.min) && defined(axis.max)) {
  6224. length = axis.max - axis.min;
  6225. if (length) {
  6226. if (!defined(options.min) && !defined(axis.userMin) && minPadding && (axis.dataMin < 0 || !axis.ignoreMinPadding)) {
  6227. axis.min -= length * minPadding;
  6228. }
  6229. if (!defined(options.max) && !defined(axis.userMax) && maxPadding && (axis.dataMax > 0 || !axis.ignoreMaxPadding)) {
  6230. axis.max += length * maxPadding;
  6231. }
  6232. }
  6233. }
  6234. // Stay within floor and ceiling
  6235. if (isNumber(options.floor)) {
  6236. axis.min = mathMax(axis.min, options.floor);
  6237. }
  6238. if (isNumber(options.ceiling)) {
  6239. axis.max = mathMin(axis.max, options.ceiling);
  6240. }
  6241. // get tickInterval
  6242. if (axis.min === axis.max || axis.min === undefined || axis.max === undefined) {
  6243. axis.tickInterval = 1;
  6244. } else if (isLinked && !tickIntervalOption &&
  6245. tickPixelIntervalOption === axis.linkedParent.options.tickPixelInterval) {
  6246. axis.tickInterval = axis.linkedParent.tickInterval;
  6247. } else {
  6248. axis.tickInterval = pick(
  6249. tickIntervalOption,
  6250. categories ? // for categoried axis, 1 is default, for linear axis use tickPix
  6251. 1 :
  6252. // don't let it be more than the data range
  6253. (axis.max - axis.min) * tickPixelIntervalOption / mathMax(axis.len, tickPixelIntervalOption)
  6254. );
  6255. // For squished axes, set only two ticks
  6256. if (!defined(tickIntervalOption) && axis.len < tickPixelIntervalOption && !this.isRadial &&
  6257. !this.isLog && !categories && startOnTick && endOnTick) {
  6258. keepTwoTicksOnly = true;
  6259. axis.tickInterval /= 4; // tick extremes closer to the real values
  6260. }
  6261. }
  6262. // Now we're finished detecting min and max, crop and group series data. This
  6263. // is in turn needed in order to find tick positions in ordinal axes.
  6264. if (isXAxis && !secondPass) {
  6265. each(axis.series, function (series) {
  6266. series.processData(axis.min !== axis.oldMin || axis.max !== axis.oldMax);
  6267. });
  6268. }
  6269. // set the translation factor used in translate function
  6270. axis.setAxisTranslation(true);
  6271. // hook for ordinal axes and radial axes
  6272. if (axis.beforeSetTickPositions) {
  6273. axis.beforeSetTickPositions();
  6274. }
  6275. // hook for extensions, used in Highstock ordinal axes
  6276. if (axis.postProcessTickInterval) {
  6277. axis.tickInterval = axis.postProcessTickInterval(axis.tickInterval);
  6278. }
  6279. // In column-like charts, don't cramp in more ticks than there are points (#1943)
  6280. if (axis.pointRange) {
  6281. axis.tickInterval = mathMax(axis.pointRange, axis.tickInterval);
  6282. }
  6283. // Before normalizing the tick interval, handle minimum tick interval. This applies only if tickInterval is not defined.
  6284. if (!tickIntervalOption && axis.tickInterval < minTickIntervalOption) {
  6285. axis.tickInterval = minTickIntervalOption;
  6286. }
  6287. // for linear axes, get magnitude and normalize the interval
  6288. if (!isDatetimeAxis && !isLog) { // linear
  6289. if (!tickIntervalOption) {
  6290. axis.tickInterval = normalizeTickInterval(axis.tickInterval, null, getMagnitude(axis.tickInterval), options);
  6291. }
  6292. }
  6293. // get minorTickInterval
  6294. axis.minorTickInterval = options.minorTickInterval === 'auto' && axis.tickInterval ?
  6295. axis.tickInterval / 5 : options.minorTickInterval;
  6296. // find the tick positions
  6297. axis.tickPositions = tickPositions = options.tickPositions ?
  6298. [].concat(options.tickPositions) : // Work on a copy (#1565)
  6299. (tickPositioner && tickPositioner.apply(axis, [axis.min, axis.max]));
  6300. if (!tickPositions) {
  6301. // Too many ticks
  6302. if (!axis.ordinalPositions && (axis.max - axis.min) / axis.tickInterval > mathMax(2 * axis.len, 200)) {
  6303. error(19, true);
  6304. }
  6305. if (isDatetimeAxis) {
  6306. tickPositions = axis.getTimeTicks(
  6307. axis.normalizeTimeTickInterval(axis.tickInterval, options.units),
  6308. axis.min,
  6309. axis.max,
  6310. options.startOfWeek,
  6311. axis.ordinalPositions,
  6312. axis.closestPointRange,
  6313. true
  6314. );
  6315. } else if (isLog) {
  6316. tickPositions = axis.getLogTickPositions(axis.tickInterval, axis.min, axis.max);
  6317. } else {
  6318. tickPositions = axis.getLinearTickPositions(axis.tickInterval, axis.min, axis.max);
  6319. }
  6320. if (keepTwoTicksOnly) {
  6321. tickPositions.splice(1, tickPositions.length - 2);
  6322. }
  6323. axis.tickPositions = tickPositions;
  6324. }
  6325. if (!isLinked) {
  6326. // reset min/max or remove extremes based on start/end on tick
  6327. var roundedMin = tickPositions[0],
  6328. roundedMax = tickPositions[tickPositions.length - 1],
  6329. minPointOffset = axis.minPointOffset || 0,
  6330. singlePad;
  6331. // Prevent all ticks from being removed (#3195)
  6332. if (!startOnTick && !endOnTick && !categories && tickPositions.length === 2) {
  6333. tickPositions.splice(1, 0, (roundedMax + roundedMin) / 2);
  6334. }
  6335. if (startOnTick) {
  6336. axis.min = roundedMin;
  6337. } else if (axis.min - minPointOffset > roundedMin) {
  6338. tickPositions.shift();
  6339. }
  6340. if (endOnTick) {
  6341. axis.max = roundedMax;
  6342. } else if (axis.max + minPointOffset < roundedMax) {
  6343. tickPositions.pop();
  6344. }
  6345. // When there is only one point, or all points have the same value on this axis, then min
  6346. // and max are equal and tickPositions.length is 0 or 1. In this case, add some padding
  6347. // in order to center the point, but leave it with one tick. #1337.
  6348. if (tickPositions.length === 1) {
  6349. singlePad = mathAbs(axis.max) > 10e12 ? 1 : 0.001; // The lowest possible number to avoid extra padding on columns (#2619, #2846)
  6350. axis.min -= singlePad;
  6351. axis.max += singlePad;
  6352. }
  6353. }
  6354. },
  6355. /**
  6356. * Set the max ticks of either the x and y axis collection
  6357. */
  6358. setMaxTicks: function () {
  6359. var chart = this.chart,
  6360. maxTicks = chart.maxTicks || {},
  6361. tickPositions = this.tickPositions,
  6362. key = this._maxTicksKey = [this.coll, this.pos, this.len].join('-');
  6363. if (!this.isLinked && !this.isDatetimeAxis && tickPositions && tickPositions.length > (maxTicks[key] || 0) && this.options.alignTicks !== false) {
  6364. maxTicks[key] = tickPositions.length;
  6365. }
  6366. chart.maxTicks = maxTicks;
  6367. },
  6368. /**
  6369. * When using multiple axes, adjust the number of ticks to match the highest
  6370. * number of ticks in that group
  6371. */
  6372. adjustTickAmount: function () {
  6373. var axis = this,
  6374. chart = axis.chart,
  6375. key = axis._maxTicksKey,
  6376. tickPositions = axis.tickPositions,
  6377. maxTicks = chart.maxTicks;
  6378. if (maxTicks && maxTicks[key] && !axis.isDatetimeAxis && !axis.categories && !axis.isLinked &&
  6379. axis.options.alignTicks !== false && this.min !== UNDEFINED) {
  6380. var oldTickAmount = axis.tickAmount,
  6381. calculatedTickAmount = tickPositions.length,
  6382. tickAmount;
  6383. // set the axis-level tickAmount to use below
  6384. axis.tickAmount = tickAmount = maxTicks[key];
  6385. if (calculatedTickAmount < tickAmount) {
  6386. while (tickPositions.length < tickAmount) {
  6387. tickPositions.push(correctFloat(
  6388. tickPositions[tickPositions.length - 1] + axis.tickInterval
  6389. ));
  6390. }
  6391. axis.transA *= (calculatedTickAmount - 1) / (tickAmount - 1);
  6392. axis.max = tickPositions[tickPositions.length - 1];
  6393. }
  6394. if (defined(oldTickAmount) && tickAmount !== oldTickAmount) {
  6395. axis.isDirty = true;
  6396. }
  6397. }
  6398. },
  6399. /**
  6400. * Set the scale based on data min and max, user set min and max or options
  6401. *
  6402. */
  6403. setScale: function () {
  6404. var axis = this,
  6405. stacks = axis.stacks,
  6406. type,
  6407. i,
  6408. isDirtyData,
  6409. isDirtyAxisLength;
  6410. axis.oldMin = axis.min;
  6411. axis.oldMax = axis.max;
  6412. axis.oldAxisLength = axis.len;
  6413. // set the new axisLength
  6414. axis.setAxisSize();
  6415. //axisLength = horiz ? axisWidth : axisHeight;
  6416. isDirtyAxisLength = axis.len !== axis.oldAxisLength;
  6417. // is there new data?
  6418. each(axis.series, function (series) {
  6419. if (series.isDirtyData || series.isDirty ||
  6420. series.xAxis.isDirty) { // when x axis is dirty, we need new data extremes for y as well
  6421. isDirtyData = true;
  6422. }
  6423. });
  6424. // do we really need to go through all this?
  6425. if (isDirtyAxisLength || isDirtyData || axis.isLinked || axis.forceRedraw ||
  6426. axis.userMin !== axis.oldUserMin || axis.userMax !== axis.oldUserMax) {
  6427. // reset stacks
  6428. if (!axis.isXAxis) {
  6429. for (type in stacks) {
  6430. for (i in stacks[type]) {
  6431. stacks[type][i].total = null;
  6432. stacks[type][i].cum = 0;
  6433. }
  6434. }
  6435. }
  6436. axis.forceRedraw = false;
  6437. // get data extremes if needed
  6438. axis.getSeriesExtremes();
  6439. // get fixed positions based on tickInterval
  6440. axis.setTickPositions();
  6441. // record old values to decide whether a rescale is necessary later on (#540)
  6442. axis.oldUserMin = axis.userMin;
  6443. axis.oldUserMax = axis.userMax;
  6444. // Mark as dirty if it is not already set to dirty and extremes have changed. #595.
  6445. if (!axis.isDirty) {
  6446. axis.isDirty = isDirtyAxisLength || axis.min !== axis.oldMin || axis.max !== axis.oldMax;
  6447. }
  6448. } else if (!axis.isXAxis) {
  6449. if (axis.oldStacks) {
  6450. stacks = axis.stacks = axis.oldStacks;
  6451. }
  6452. // reset stacks
  6453. for (type in stacks) {
  6454. for (i in stacks[type]) {
  6455. stacks[type][i].cum = stacks[type][i].total;
  6456. }
  6457. }
  6458. }
  6459. // Set the maximum tick amount
  6460. axis.setMaxTicks();
  6461. },
  6462. /**
  6463. * Set the extremes and optionally redraw
  6464. * @param {Number} newMin
  6465. * @param {Number} newMax
  6466. * @param {Boolean} redraw
  6467. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  6468. * configuration
  6469. * @param {Object} eventArguments
  6470. *
  6471. */
  6472. setExtremes: function (newMin, newMax, redraw, animation, eventArguments) {
  6473. var axis = this,
  6474. chart = axis.chart;
  6475. redraw = pick(redraw, true); // defaults to true
  6476. // Extend the arguments with min and max
  6477. eventArguments = extend(eventArguments, {
  6478. min: newMin,
  6479. max: newMax
  6480. });
  6481. // Fire the event
  6482. fireEvent(axis, 'setExtremes', eventArguments, function () { // the default event handler
  6483. axis.userMin = newMin;
  6484. axis.userMax = newMax;
  6485. axis.eventArgs = eventArguments;
  6486. // Mark for running afterSetExtremes
  6487. axis.isDirtyExtremes = true;
  6488. // redraw
  6489. if (redraw) {
  6490. chart.redraw(animation);
  6491. }
  6492. });
  6493. },
  6494. /**
  6495. * Overridable method for zooming chart. Pulled out in a separate method to allow overriding
  6496. * in stock charts.
  6497. */
  6498. zoom: function (newMin, newMax) {
  6499. var dataMin = this.dataMin,
  6500. dataMax = this.dataMax,
  6501. options = this.options;
  6502. // Prevent pinch zooming out of range. Check for defined is for #1946. #1734.
  6503. if (!this.allowZoomOutside) {
  6504. if (defined(dataMin) && newMin <= mathMin(dataMin, pick(options.min, dataMin))) {
  6505. newMin = UNDEFINED;
  6506. }
  6507. if (defined(dataMax) && newMax >= mathMax(dataMax, pick(options.max, dataMax))) {
  6508. newMax = UNDEFINED;
  6509. }
  6510. }
  6511. // In full view, displaying the reset zoom button is not required
  6512. this.displayBtn = newMin !== UNDEFINED || newMax !== UNDEFINED;
  6513. // Do it
  6514. this.setExtremes(
  6515. newMin,
  6516. newMax,
  6517. false,
  6518. UNDEFINED,
  6519. { trigger: 'zoom' }
  6520. );
  6521. return true;
  6522. },
  6523. /**
  6524. * Update the axis metrics
  6525. */
  6526. setAxisSize: function () {
  6527. var chart = this.chart,
  6528. options = this.options,
  6529. offsetLeft = options.offsetLeft || 0,
  6530. offsetRight = options.offsetRight || 0,
  6531. horiz = this.horiz,
  6532. width = pick(options.width, chart.plotWidth - offsetLeft + offsetRight),
  6533. height = pick(options.height, chart.plotHeight),
  6534. top = pick(options.top, chart.plotTop),
  6535. left = pick(options.left, chart.plotLeft + offsetLeft),
  6536. percentRegex = /%$/;
  6537. // Check for percentage based input values
  6538. if (percentRegex.test(height)) {
  6539. height = parseInt(height, 10) / 100 * chart.plotHeight;
  6540. }
  6541. if (percentRegex.test(top)) {
  6542. top = parseInt(top, 10) / 100 * chart.plotHeight + chart.plotTop;
  6543. }
  6544. // Expose basic values to use in Series object and navigator
  6545. this.left = left;
  6546. this.top = top;
  6547. this.width = width;
  6548. this.height = height;
  6549. this.bottom = chart.chartHeight - height - top;
  6550. this.right = chart.chartWidth - width - left;
  6551. // Direction agnostic properties
  6552. this.len = mathMax(horiz ? width : height, 0); // mathMax fixes #905
  6553. this.pos = horiz ? left : top; // distance from SVG origin
  6554. },
  6555. /**
  6556. * Get the actual axis extremes
  6557. */
  6558. getExtremes: function () {
  6559. var axis = this,
  6560. isLog = axis.isLog;
  6561. return {
  6562. min: isLog ? correctFloat(lin2log(axis.min)) : axis.min,
  6563. max: isLog ? correctFloat(lin2log(axis.max)) : axis.max,
  6564. dataMin: axis.dataMin,
  6565. dataMax: axis.dataMax,
  6566. userMin: axis.userMin,
  6567. userMax: axis.userMax
  6568. };
  6569. },
  6570. /**
  6571. * Get the zero plane either based on zero or on the min or max value.
  6572. * Used in bar and area plots
  6573. */
  6574. getThreshold: function (threshold) {
  6575. var axis = this,
  6576. isLog = axis.isLog;
  6577. var realMin = isLog ? lin2log(axis.min) : axis.min,
  6578. realMax = isLog ? lin2log(axis.max) : axis.max;
  6579. if (realMin > threshold || threshold === null) {
  6580. threshold = realMin;
  6581. } else if (realMax < threshold) {
  6582. threshold = realMax;
  6583. }
  6584. return axis.translate(threshold, 0, 1, 0, 1);
  6585. },
  6586. /**
  6587. * Compute auto alignment for the axis label based on which side the axis is on
  6588. * and the given rotation for the label
  6589. */
  6590. autoLabelAlign: function (rotation) {
  6591. var ret,
  6592. angle = (pick(rotation, 0) - (this.side * 90) + 720) % 360;
  6593. if (angle > 15 && angle < 165) {
  6594. ret = 'right';
  6595. } else if (angle > 195 && angle < 345) {
  6596. ret = 'left';
  6597. } else {
  6598. ret = 'center';
  6599. }
  6600. return ret;
  6601. },
  6602. /**
  6603. * Render the tick labels to a preliminary position to get their sizes
  6604. */
  6605. getOffset: function () {
  6606. var axis = this,
  6607. chart = axis.chart,
  6608. renderer = chart.renderer,
  6609. options = axis.options,
  6610. tickPositions = axis.tickPositions,
  6611. ticks = axis.ticks,
  6612. horiz = axis.horiz,
  6613. side = axis.side,
  6614. invertedSide = chart.inverted ? [1, 0, 3, 2][side] : side,
  6615. hasData,
  6616. showAxis,
  6617. titleOffset = 0,
  6618. titleOffsetOption,
  6619. titleMargin = 0,
  6620. axisTitleOptions = options.title,
  6621. labelOptions = options.labels,
  6622. labelOffset = 0, // reset
  6623. labelOffsetPadded,
  6624. axisOffset = chart.axisOffset,
  6625. clipOffset = chart.clipOffset,
  6626. directionFactor = [-1, 1, 1, -1][side],
  6627. n,
  6628. i,
  6629. autoStaggerLines = 1,
  6630. maxStaggerLines = pick(labelOptions.maxStaggerLines, 5),
  6631. sortedPositions,
  6632. lastRight,
  6633. overlap,
  6634. pos,
  6635. bBox,
  6636. x,
  6637. w,
  6638. lineNo,
  6639. lineHeightCorrection;
  6640. // For reuse in Axis.render
  6641. axis.hasData = hasData = (axis.hasVisibleSeries || (defined(axis.min) && defined(axis.max) && !!tickPositions));
  6642. axis.showAxis = showAxis = hasData || pick(options.showEmpty, true);
  6643. // Set/reset staggerLines
  6644. axis.staggerLines = axis.horiz && labelOptions.staggerLines;
  6645. // Create the axisGroup and gridGroup elements on first iteration
  6646. if (!axis.axisGroup) {
  6647. axis.gridGroup = renderer.g('grid')
  6648. .attr({ zIndex: options.gridZIndex || 1 })
  6649. .add();
  6650. axis.axisGroup = renderer.g('axis')
  6651. .attr({ zIndex: options.zIndex || 2 })
  6652. .add();
  6653. axis.labelGroup = renderer.g('axis-labels')
  6654. .attr({ zIndex: labelOptions.zIndex || 7 })
  6655. .addClass(PREFIX + axis.coll.toLowerCase() + '-labels')
  6656. .add();
  6657. }
  6658. if (hasData || axis.isLinked) {
  6659. // Set the explicit or automatic label alignment
  6660. axis.labelAlign = pick(labelOptions.align || axis.autoLabelAlign(labelOptions.rotation));
  6661. // Generate ticks
  6662. each(tickPositions, function (pos) {
  6663. if (!ticks[pos]) {
  6664. ticks[pos] = new Tick(axis, pos);
  6665. } else {
  6666. ticks[pos].addLabel(); // update labels depending on tick interval
  6667. }
  6668. });
  6669. // Handle automatic stagger lines
  6670. if (axis.horiz && !axis.staggerLines && maxStaggerLines && !labelOptions.rotation) {
  6671. sortedPositions = axis.reversed ? [].concat(tickPositions).reverse() : tickPositions;
  6672. while (autoStaggerLines < maxStaggerLines) {
  6673. lastRight = [];
  6674. overlap = false;
  6675. for (i = 0; i < sortedPositions.length; i++) {
  6676. pos = sortedPositions[i];
  6677. bBox = ticks[pos].label && ticks[pos].label.getBBox();
  6678. w = bBox ? bBox.width : 0;
  6679. lineNo = i % autoStaggerLines;
  6680. if (w) {
  6681. x = axis.translate(pos); // don't handle log
  6682. if (lastRight[lineNo] !== UNDEFINED && x < lastRight[lineNo]) {
  6683. overlap = true;
  6684. }
  6685. lastRight[lineNo] = x + w;
  6686. }
  6687. }
  6688. if (overlap) {
  6689. autoStaggerLines++;
  6690. } else {
  6691. break;
  6692. }
  6693. }
  6694. if (autoStaggerLines > 1) {
  6695. axis.staggerLines = autoStaggerLines;
  6696. }
  6697. }
  6698. each(tickPositions, function (pos) {
  6699. // left side must be align: right and right side must have align: left for labels
  6700. if (side === 0 || side === 2 || { 1: 'left', 3: 'right' }[side] === axis.labelAlign) {
  6701. // get the highest offset
  6702. labelOffset = mathMax(
  6703. ticks[pos].getLabelSize(),
  6704. labelOffset
  6705. );
  6706. }
  6707. });
  6708. if (axis.staggerLines) {
  6709. labelOffset *= axis.staggerLines;
  6710. axis.labelOffset = labelOffset;
  6711. }
  6712. } else { // doesn't have data
  6713. for (n in ticks) {
  6714. ticks[n].destroy();
  6715. delete ticks[n];
  6716. }
  6717. }
  6718. if (axisTitleOptions && axisTitleOptions.text && axisTitleOptions.enabled !== false) {
  6719. if (!axis.axisTitle) {
  6720. axis.axisTitle = renderer.text(
  6721. axisTitleOptions.text,
  6722. 0,
  6723. 0,
  6724. axisTitleOptions.useHTML
  6725. )
  6726. .attr({
  6727. zIndex: 7,
  6728. rotation: axisTitleOptions.rotation || 0,
  6729. align:
  6730. axisTitleOptions.textAlign ||
  6731. { low: 'left', middle: 'center', high: 'right' }[axisTitleOptions.align]
  6732. })
  6733. .addClass(PREFIX + this.coll.toLowerCase() + '-title')
  6734. .css(axisTitleOptions.style)
  6735. .add(axis.axisGroup);
  6736. axis.axisTitle.isNew = true;
  6737. }
  6738. if (showAxis) {
  6739. titleOffset = axis.axisTitle.getBBox()[horiz ? 'height' : 'width'];
  6740. titleOffsetOption = axisTitleOptions.offset;
  6741. titleMargin = defined(titleOffsetOption) ? 0 : pick(axisTitleOptions.margin, horiz ? 5 : 10);
  6742. }
  6743. // hide or show the title depending on whether showEmpty is set
  6744. axis.axisTitle[showAxis ? 'show' : 'hide']();
  6745. }
  6746. // handle automatic or user set offset
  6747. axis.offset = directionFactor * pick(options.offset, axisOffset[side]);
  6748. lineHeightCorrection = side === 2 ? axis.tickBaseline : 0;
  6749. labelOffsetPadded = labelOffset + titleMargin +
  6750. (labelOffset && (directionFactor * (horiz ? pick(labelOptions.y, axis.tickBaseline + 8) : labelOptions.x) - lineHeightCorrection));
  6751. axis.axisTitleMargin = pick(titleOffsetOption, labelOffsetPadded);
  6752. axisOffset[side] = mathMax(
  6753. axisOffset[side],
  6754. axis.axisTitleMargin + titleOffset + directionFactor * axis.offset,
  6755. labelOffsetPadded // #3027
  6756. );
  6757. clipOffset[invertedSide] = mathMax(clipOffset[invertedSide], mathFloor(options.lineWidth / 2) * 2);
  6758. },
  6759. /**
  6760. * Get the path for the axis line
  6761. */
  6762. getLinePath: function (lineWidth) {
  6763. var chart = this.chart,
  6764. opposite = this.opposite,
  6765. offset = this.offset,
  6766. horiz = this.horiz,
  6767. lineLeft = this.left + (opposite ? this.width : 0) + offset,
  6768. lineTop = chart.chartHeight - this.bottom - (opposite ? this.height : 0) + offset;
  6769. if (opposite) {
  6770. lineWidth *= -1; // crispify the other way - #1480, #1687
  6771. }
  6772. return chart.renderer.crispLine([
  6773. M,
  6774. horiz ?
  6775. this.left :
  6776. lineLeft,
  6777. horiz ?
  6778. lineTop :
  6779. this.top,
  6780. L,
  6781. horiz ?
  6782. chart.chartWidth - this.right :
  6783. lineLeft,
  6784. horiz ?
  6785. lineTop :
  6786. chart.chartHeight - this.bottom
  6787. ], lineWidth);
  6788. },
  6789. /**
  6790. * Position the title
  6791. */
  6792. getTitlePosition: function () {
  6793. // compute anchor points for each of the title align options
  6794. var horiz = this.horiz,
  6795. axisLeft = this.left,
  6796. axisTop = this.top,
  6797. axisLength = this.len,
  6798. axisTitleOptions = this.options.title,
  6799. margin = horiz ? axisLeft : axisTop,
  6800. opposite = this.opposite,
  6801. offset = this.offset,
  6802. fontSize = pInt(axisTitleOptions.style.fontSize || 12),
  6803. // the position in the length direction of the axis
  6804. alongAxis = {
  6805. low: margin + (horiz ? 0 : axisLength),
  6806. middle: margin + axisLength / 2,
  6807. high: margin + (horiz ? axisLength : 0)
  6808. }[axisTitleOptions.align],
  6809. // the position in the perpendicular direction of the axis
  6810. offAxis = (horiz ? axisTop + this.height : axisLeft) +
  6811. (horiz ? 1 : -1) * // horizontal axis reverses the margin
  6812. (opposite ? -1 : 1) * // so does opposite axes
  6813. this.axisTitleMargin +
  6814. (this.side === 2 ? fontSize : 0);
  6815. return {
  6816. x: horiz ?
  6817. alongAxis :
  6818. offAxis + (opposite ? this.width : 0) + offset +
  6819. (axisTitleOptions.x || 0), // x
  6820. y: horiz ?
  6821. offAxis - (opposite ? this.height : 0) + offset :
  6822. alongAxis + (axisTitleOptions.y || 0) // y
  6823. };
  6824. },
  6825. /**
  6826. * Render the axis
  6827. */
  6828. render: function () {
  6829. var axis = this,
  6830. horiz = axis.horiz,
  6831. reversed = axis.reversed,
  6832. chart = axis.chart,
  6833. renderer = chart.renderer,
  6834. options = axis.options,
  6835. isLog = axis.isLog,
  6836. isLinked = axis.isLinked,
  6837. tickPositions = axis.tickPositions,
  6838. sortedPositions,
  6839. axisTitle = axis.axisTitle,
  6840. ticks = axis.ticks,
  6841. minorTicks = axis.minorTicks,
  6842. alternateBands = axis.alternateBands,
  6843. stackLabelOptions = options.stackLabels,
  6844. alternateGridColor = options.alternateGridColor,
  6845. tickmarkOffset = axis.tickmarkOffset,
  6846. lineWidth = options.lineWidth,
  6847. linePath,
  6848. hasRendered = chart.hasRendered,
  6849. slideInTicks = hasRendered && defined(axis.oldMin) && !isNaN(axis.oldMin),
  6850. hasData = axis.hasData,
  6851. showAxis = axis.showAxis,
  6852. from,
  6853. overflow = options.labels.overflow,
  6854. justifyLabels = axis.justifyLabels = horiz && overflow !== false,
  6855. to;
  6856. // Reset
  6857. axis.labelEdge.length = 0;
  6858. axis.justifyToPlot = overflow === 'justify';
  6859. // Mark all elements inActive before we go over and mark the active ones
  6860. each([ticks, minorTicks, alternateBands], function (coll) {
  6861. var pos;
  6862. for (pos in coll) {
  6863. coll[pos].isActive = false;
  6864. }
  6865. });
  6866. // If the series has data draw the ticks. Else only the line and title
  6867. if (hasData || isLinked) {
  6868. // minor ticks
  6869. if (axis.minorTickInterval && !axis.categories) {
  6870. each(axis.getMinorTickPositions(), function (pos) {
  6871. if (!minorTicks[pos]) {
  6872. minorTicks[pos] = new Tick(axis, pos, 'minor');
  6873. }
  6874. // render new ticks in old position
  6875. if (slideInTicks && minorTicks[pos].isNew) {
  6876. minorTicks[pos].render(null, true);
  6877. }
  6878. minorTicks[pos].render(null, false, 1);
  6879. });
  6880. }
  6881. // Major ticks. Pull out the first item and render it last so that
  6882. // we can get the position of the neighbour label. #808.
  6883. if (tickPositions.length) { // #1300
  6884. sortedPositions = tickPositions.slice();
  6885. if ((horiz && reversed) || (!horiz && !reversed)) {
  6886. sortedPositions.reverse();
  6887. }
  6888. if (justifyLabels) {
  6889. sortedPositions = sortedPositions.slice(1).concat([sortedPositions[0]]);
  6890. }
  6891. each(sortedPositions, function (pos, i) {
  6892. // Reorganize the indices
  6893. if (justifyLabels) {
  6894. i = (i === sortedPositions.length - 1) ? 0 : i + 1;
  6895. }
  6896. // linked axes need an extra check to find out if
  6897. if (!isLinked || (pos >= axis.min && pos <= axis.max)) {
  6898. if (!ticks[pos]) {
  6899. ticks[pos] = new Tick(axis, pos);
  6900. }
  6901. // render new ticks in old position
  6902. if (slideInTicks && ticks[pos].isNew) {
  6903. ticks[pos].render(i, true, 0.1);
  6904. }
  6905. ticks[pos].render(i);
  6906. }
  6907. });
  6908. // In a categorized axis, the tick marks are displayed between labels. So
  6909. // we need to add a tick mark and grid line at the left edge of the X axis.
  6910. if (tickmarkOffset && axis.min === 0) {
  6911. if (!ticks[-1]) {
  6912. ticks[-1] = new Tick(axis, -1, null, true);
  6913. }
  6914. ticks[-1].render(-1);
  6915. }
  6916. }
  6917. // alternate grid color
  6918. if (alternateGridColor) {
  6919. each(tickPositions, function (pos, i) {
  6920. if (i % 2 === 0 && pos < axis.max) {
  6921. if (!alternateBands[pos]) {
  6922. alternateBands[pos] = new Highcharts.PlotLineOrBand(axis);
  6923. }
  6924. from = pos + tickmarkOffset; // #949
  6925. to = tickPositions[i + 1] !== UNDEFINED ? tickPositions[i + 1] + tickmarkOffset : axis.max;
  6926. alternateBands[pos].options = {
  6927. from: isLog ? lin2log(from) : from,
  6928. to: isLog ? lin2log(to) : to,
  6929. color: alternateGridColor
  6930. };
  6931. alternateBands[pos].render();
  6932. alternateBands[pos].isActive = true;
  6933. }
  6934. });
  6935. }
  6936. // custom plot lines and bands
  6937. if (!axis._addedPlotLB) { // only first time
  6938. each((options.plotLines || []).concat(options.plotBands || []), function (plotLineOptions) {
  6939. axis.addPlotBandOrLine(plotLineOptions);
  6940. });
  6941. axis._addedPlotLB = true;
  6942. }
  6943. } // end if hasData
  6944. // Remove inactive ticks
  6945. each([ticks, minorTicks, alternateBands], function (coll) {
  6946. var pos,
  6947. i,
  6948. forDestruction = [],
  6949. delay = globalAnimation ? globalAnimation.duration || 500 : 0,
  6950. destroyInactiveItems = function () {
  6951. i = forDestruction.length;
  6952. while (i--) {
  6953. // When resizing rapidly, the same items may be destroyed in different timeouts,
  6954. // or the may be reactivated
  6955. if (coll[forDestruction[i]] && !coll[forDestruction[i]].isActive) {
  6956. coll[forDestruction[i]].destroy();
  6957. delete coll[forDestruction[i]];
  6958. }
  6959. }
  6960. };
  6961. for (pos in coll) {
  6962. if (!coll[pos].isActive) {
  6963. // Render to zero opacity
  6964. coll[pos].render(pos, false, 0);
  6965. coll[pos].isActive = false;
  6966. forDestruction.push(pos);
  6967. }
  6968. }
  6969. // When the objects are finished fading out, destroy them
  6970. if (coll === alternateBands || !chart.hasRendered || !delay) {
  6971. destroyInactiveItems();
  6972. } else if (delay) {
  6973. setTimeout(destroyInactiveItems, delay);
  6974. }
  6975. });
  6976. // Static items. As the axis group is cleared on subsequent calls
  6977. // to render, these items are added outside the group.
  6978. // axis line
  6979. if (lineWidth) {
  6980. linePath = axis.getLinePath(lineWidth);
  6981. if (!axis.axisLine) {
  6982. axis.axisLine = renderer.path(linePath)
  6983. .attr({
  6984. stroke: options.lineColor,
  6985. 'stroke-width': lineWidth,
  6986. zIndex: 7
  6987. })
  6988. .add(axis.axisGroup);
  6989. } else {
  6990. axis.axisLine.animate({ d: linePath });
  6991. }
  6992. // show or hide the line depending on options.showEmpty
  6993. axis.axisLine[showAxis ? 'show' : 'hide']();
  6994. }
  6995. if (axisTitle && showAxis) {
  6996. axisTitle[axisTitle.isNew ? 'attr' : 'animate'](
  6997. axis.getTitlePosition()
  6998. );
  6999. axisTitle.isNew = false;
  7000. }
  7001. // Stacked totals:
  7002. if (stackLabelOptions && stackLabelOptions.enabled) {
  7003. axis.renderStackTotals();
  7004. }
  7005. // End stacked totals
  7006. axis.isDirty = false;
  7007. },
  7008. /**
  7009. * Redraw the axis to reflect changes in the data or axis extremes
  7010. */
  7011. redraw: function () {
  7012. // render the axis
  7013. this.render();
  7014. // move plot lines and bands
  7015. each(this.plotLinesAndBands, function (plotLine) {
  7016. plotLine.render();
  7017. });
  7018. // mark associated series as dirty and ready for redraw
  7019. each(this.series, function (series) {
  7020. series.isDirty = true;
  7021. });
  7022. },
  7023. /**
  7024. * Destroys an Axis instance.
  7025. */
  7026. destroy: function (keepEvents) {
  7027. var axis = this,
  7028. stacks = axis.stacks,
  7029. stackKey,
  7030. plotLinesAndBands = axis.plotLinesAndBands,
  7031. i;
  7032. // Remove the events
  7033. if (!keepEvents) {
  7034. removeEvent(axis);
  7035. }
  7036. // Destroy each stack total
  7037. for (stackKey in stacks) {
  7038. destroyObjectProperties(stacks[stackKey]);
  7039. stacks[stackKey] = null;
  7040. }
  7041. // Destroy collections
  7042. each([axis.ticks, axis.minorTicks, axis.alternateBands], function (coll) {
  7043. destroyObjectProperties(coll);
  7044. });
  7045. i = plotLinesAndBands.length;
  7046. while (i--) { // #1975
  7047. plotLinesAndBands[i].destroy();
  7048. }
  7049. // Destroy local variables
  7050. each(['stackTotalGroup', 'axisLine', 'axisTitle', 'axisGroup', 'cross', 'gridGroup', 'labelGroup'], function (prop) {
  7051. if (axis[prop]) {
  7052. axis[prop] = axis[prop].destroy();
  7053. }
  7054. });
  7055. // Destroy crosshair
  7056. if (this.cross) {
  7057. this.cross.destroy();
  7058. }
  7059. },
  7060. /**
  7061. * Draw the crosshair
  7062. */
  7063. drawCrosshair: function (e, point) {
  7064. if (!this.crosshair) { return; }// Do not draw crosshairs if you don't have too.
  7065. if ((defined(point) || !pick(this.crosshair.snap, true)) === false) {
  7066. this.hideCrosshair();
  7067. return;
  7068. }
  7069. var path,
  7070. options = this.crosshair,
  7071. animation = options.animation,
  7072. pos;
  7073. // Get the path
  7074. if (!pick(options.snap, true)) {
  7075. pos = (this.horiz ? e.chartX - this.pos : this.len - e.chartY + this.pos);
  7076. } else if (defined(point)) {
  7077. /*jslint eqeq: true*/
  7078. pos = (this.chart.inverted != this.horiz) ? point.plotX : this.len - point.plotY;
  7079. /*jslint eqeq: false*/
  7080. }
  7081. if (this.isRadial) {
  7082. path = this.getPlotLinePath(this.isXAxis ? point.x : pick(point.stackY, point.y));
  7083. } else {
  7084. path = this.getPlotLinePath(null, null, null, null, pos);
  7085. }
  7086. if (path === null) {
  7087. this.hideCrosshair();
  7088. return;
  7089. }
  7090. // Draw the cross
  7091. if (this.cross) {
  7092. this.cross
  7093. .attr({ visibility: VISIBLE })[animation ? 'animate' : 'attr']({ d: path }, animation);
  7094. } else {
  7095. var attribs = {
  7096. 'stroke-width': options.width || 1,
  7097. stroke: options.color || '#C0C0C0',
  7098. zIndex: options.zIndex || 2
  7099. };
  7100. if (options.dashStyle) {
  7101. attribs.dashstyle = options.dashStyle;
  7102. }
  7103. this.cross = this.chart.renderer.path(path).attr(attribs).add();
  7104. }
  7105. },
  7106. /**
  7107. * Hide the crosshair.
  7108. */
  7109. hideCrosshair: function () {
  7110. if (this.cross) {
  7111. this.cross.hide();
  7112. }
  7113. }
  7114. }; // end Axis
  7115. extend(Axis.prototype, AxisPlotLineOrBandExtension);
  7116. /**
  7117. * Set the tick positions to a time unit that makes sense, for example
  7118. * on the first of each month or on every Monday. Return an array
  7119. * with the time positions. Used in datetime axes as well as for grouping
  7120. * data on a datetime axis.
  7121. *
  7122. * @param {Object} normalizedInterval The interval in axis values (ms) and the count
  7123. * @param {Number} min The minimum in axis values
  7124. * @param {Number} max The maximum in axis values
  7125. * @param {Number} startOfWeek
  7126. */
  7127. Axis.prototype.getTimeTicks = function (normalizedInterval, min, max, startOfWeek) {
  7128. var tickPositions = [],
  7129. i,
  7130. higherRanks = {},
  7131. useUTC = defaultOptions.global.useUTC,
  7132. minYear, // used in months and years as a basis for Date.UTC()
  7133. minDate = new Date(min - timezoneOffset),
  7134. interval = normalizedInterval.unitRange,
  7135. count = normalizedInterval.count;
  7136. if (defined(min)) { // #1300
  7137. if (interval >= timeUnits.second) { // second
  7138. minDate.setMilliseconds(0);
  7139. minDate.setSeconds(interval >= timeUnits.minute ? 0 :
  7140. count * mathFloor(minDate.getSeconds() / count));
  7141. }
  7142. if (interval >= timeUnits.minute) { // minute
  7143. minDate[setMinutes](interval >= timeUnits.hour ? 0 :
  7144. count * mathFloor(minDate[getMinutes]() / count));
  7145. }
  7146. if (interval >= timeUnits.hour) { // hour
  7147. minDate[setHours](interval >= timeUnits.day ? 0 :
  7148. count * mathFloor(minDate[getHours]() / count));
  7149. }
  7150. if (interval >= timeUnits.day) { // day
  7151. minDate[setDate](interval >= timeUnits.month ? 1 :
  7152. count * mathFloor(minDate[getDate]() / count));
  7153. }
  7154. if (interval >= timeUnits.month) { // month
  7155. minDate[setMonth](interval >= timeUnits.year ? 0 :
  7156. count * mathFloor(minDate[getMonth]() / count));
  7157. minYear = minDate[getFullYear]();
  7158. }
  7159. if (interval >= timeUnits.year) { // year
  7160. minYear -= minYear % count;
  7161. minDate[setFullYear](minYear);
  7162. }
  7163. // week is a special case that runs outside the hierarchy
  7164. if (interval === timeUnits.week) {
  7165. // get start of current week, independent of count
  7166. minDate[setDate](minDate[getDate]() - minDate[getDay]() +
  7167. pick(startOfWeek, 1));
  7168. }
  7169. // get tick positions
  7170. i = 1;
  7171. if (timezoneOffset) {
  7172. minDate = new Date(minDate.getTime() + timezoneOffset);
  7173. }
  7174. minYear = minDate[getFullYear]();
  7175. var time = minDate.getTime(),
  7176. minMonth = minDate[getMonth](),
  7177. minDateDate = minDate[getDate](),
  7178. localTimezoneOffset = useUTC ?
  7179. timezoneOffset :
  7180. (24 * 3600 * 1000 + minDate.getTimezoneOffset() * 60 * 1000) % (24 * 3600 * 1000); // #950
  7181. // iterate and add tick positions at appropriate values
  7182. while (time < max) {
  7183. tickPositions.push(time);
  7184. // if the interval is years, use Date.UTC to increase years
  7185. if (interval === timeUnits.year) {
  7186. time = makeTime(minYear + i * count, 0);
  7187. // if the interval is months, use Date.UTC to increase months
  7188. } else if (interval === timeUnits.month) {
  7189. time = makeTime(minYear, minMonth + i * count);
  7190. // if we're using global time, the interval is not fixed as it jumps
  7191. // one hour at the DST crossover
  7192. } else if (!useUTC && (interval === timeUnits.day || interval === timeUnits.week)) {
  7193. time = makeTime(minYear, minMonth, minDateDate +
  7194. i * count * (interval === timeUnits.day ? 1 : 7));
  7195. // else, the interval is fixed and we use simple addition
  7196. } else {
  7197. time += interval * count;
  7198. }
  7199. i++;
  7200. }
  7201. // push the last time
  7202. tickPositions.push(time);
  7203. // mark new days if the time is dividible by day (#1649, #1760)
  7204. each(grep(tickPositions, function (time) {
  7205. return interval <= timeUnits.hour && time % timeUnits.day === localTimezoneOffset;
  7206. }), function (time) {
  7207. higherRanks[time] = 'day';
  7208. });
  7209. }
  7210. // record information on the chosen unit - for dynamic label formatter
  7211. tickPositions.info = extend(normalizedInterval, {
  7212. higherRanks: higherRanks,
  7213. totalRange: interval * count
  7214. });
  7215. return tickPositions;
  7216. };
  7217. /**
  7218. * Get a normalized tick interval for dates. Returns a configuration object with
  7219. * unit range (interval), count and name. Used to prepare data for getTimeTicks.
  7220. * Previously this logic was part of getTimeTicks, but as getTimeTicks now runs
  7221. * of segments in stock charts, the normalizing logic was extracted in order to
  7222. * prevent it for running over again for each segment having the same interval.
  7223. * #662, #697.
  7224. */
  7225. Axis.prototype.normalizeTimeTickInterval = function (tickInterval, unitsOption) {
  7226. var units = unitsOption || [[
  7227. 'millisecond', // unit name
  7228. [1, 2, 5, 10, 20, 25, 50, 100, 200, 500] // allowed multiples
  7229. ], [
  7230. 'second',
  7231. [1, 2, 5, 10, 15, 30]
  7232. ], [
  7233. 'minute',
  7234. [1, 2, 5, 10, 15, 30]
  7235. ], [
  7236. 'hour',
  7237. [1, 2, 3, 4, 6, 8, 12]
  7238. ], [
  7239. 'day',
  7240. [1, 2]
  7241. ], [
  7242. 'week',
  7243. [1, 2]
  7244. ], [
  7245. 'month',
  7246. [1, 2, 3, 4, 6]
  7247. ], [
  7248. 'year',
  7249. null
  7250. ]],
  7251. unit = units[units.length - 1], // default unit is years
  7252. interval = timeUnits[unit[0]],
  7253. multiples = unit[1],
  7254. count,
  7255. i;
  7256. // loop through the units to find the one that best fits the tickInterval
  7257. for (i = 0; i < units.length; i++) {
  7258. unit = units[i];
  7259. interval = timeUnits[unit[0]];
  7260. multiples = unit[1];
  7261. if (units[i + 1]) {
  7262. // lessThan is in the middle between the highest multiple and the next unit.
  7263. var lessThan = (interval * multiples[multiples.length - 1] +
  7264. timeUnits[units[i + 1][0]]) / 2;
  7265. // break and keep the current unit
  7266. if (tickInterval <= lessThan) {
  7267. break;
  7268. }
  7269. }
  7270. }
  7271. // prevent 2.5 years intervals, though 25, 250 etc. are allowed
  7272. if (interval === timeUnits.year && tickInterval < 5 * interval) {
  7273. multiples = [1, 2, 5];
  7274. }
  7275. // get the count
  7276. count = normalizeTickInterval(
  7277. tickInterval / interval,
  7278. multiples,
  7279. unit[0] === 'year' ? mathMax(getMagnitude(tickInterval / interval), 1) : 1 // #1913, #2360
  7280. );
  7281. return {
  7282. unitRange: interval,
  7283. count: count,
  7284. unitName: unit[0]
  7285. };
  7286. };/**
  7287. * Methods defined on the Axis prototype
  7288. */
  7289. /**
  7290. * Set the tick positions of a logarithmic axis
  7291. */
  7292. Axis.prototype.getLogTickPositions = function (interval, min, max, minor) {
  7293. var axis = this,
  7294. options = axis.options,
  7295. axisLength = axis.len,
  7296. // Since we use this method for both major and minor ticks,
  7297. // use a local variable and return the result
  7298. positions = [];
  7299. // Reset
  7300. if (!minor) {
  7301. axis._minorAutoInterval = null;
  7302. }
  7303. // First case: All ticks fall on whole logarithms: 1, 10, 100 etc.
  7304. if (interval >= 0.5) {
  7305. interval = mathRound(interval);
  7306. positions = axis.getLinearTickPositions(interval, min, max);
  7307. // Second case: We need intermediary ticks. For example
  7308. // 1, 2, 4, 6, 8, 10, 20, 40 etc.
  7309. } else if (interval >= 0.08) {
  7310. var roundedMin = mathFloor(min),
  7311. intermediate,
  7312. i,
  7313. j,
  7314. len,
  7315. pos,
  7316. lastPos,
  7317. break2;
  7318. if (interval > 0.3) {
  7319. intermediate = [1, 2, 4];
  7320. } else if (interval > 0.15) { // 0.2 equals five minor ticks per 1, 10, 100 etc
  7321. intermediate = [1, 2, 4, 6, 8];
  7322. } else { // 0.1 equals ten minor ticks per 1, 10, 100 etc
  7323. intermediate = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  7324. }
  7325. for (i = roundedMin; i < max + 1 && !break2; i++) {
  7326. len = intermediate.length;
  7327. for (j = 0; j < len && !break2; j++) {
  7328. pos = log2lin(lin2log(i) * intermediate[j]);
  7329. if (pos > min && (!minor || lastPos <= max) && lastPos !== UNDEFINED) { // #1670, lastPos is #3113
  7330. positions.push(lastPos);
  7331. }
  7332. if (lastPos > max) {
  7333. break2 = true;
  7334. }
  7335. lastPos = pos;
  7336. }
  7337. }
  7338. // Third case: We are so deep in between whole logarithmic values that
  7339. // we might as well handle the tick positions like a linear axis. For
  7340. // example 1.01, 1.02, 1.03, 1.04.
  7341. } else {
  7342. var realMin = lin2log(min),
  7343. realMax = lin2log(max),
  7344. tickIntervalOption = options[minor ? 'minorTickInterval' : 'tickInterval'],
  7345. filteredTickIntervalOption = tickIntervalOption === 'auto' ? null : tickIntervalOption,
  7346. tickPixelIntervalOption = options.tickPixelInterval / (minor ? 5 : 1),
  7347. totalPixelLength = minor ? axisLength / axis.tickPositions.length : axisLength;
  7348. interval = pick(
  7349. filteredTickIntervalOption,
  7350. axis._minorAutoInterval,
  7351. (realMax - realMin) * tickPixelIntervalOption / (totalPixelLength || 1)
  7352. );
  7353. interval = normalizeTickInterval(
  7354. interval,
  7355. null,
  7356. getMagnitude(interval)
  7357. );
  7358. positions = map(axis.getLinearTickPositions(
  7359. interval,
  7360. realMin,
  7361. realMax
  7362. ), log2lin);
  7363. if (!minor) {
  7364. axis._minorAutoInterval = interval / 5;
  7365. }
  7366. }
  7367. // Set the axis-level tickInterval variable
  7368. if (!minor) {
  7369. axis.tickInterval = interval;
  7370. }
  7371. return positions;
  7372. };/**
  7373. * The tooltip object
  7374. * @param {Object} chart The chart instance
  7375. * @param {Object} options Tooltip options
  7376. */
  7377. var Tooltip = Highcharts.Tooltip = function () {
  7378. this.init.apply(this, arguments);
  7379. };
  7380. Tooltip.prototype = {
  7381. init: function (chart, options) {
  7382. var borderWidth = options.borderWidth,
  7383. style = options.style,
  7384. padding = pInt(style.padding);
  7385. // Save the chart and options
  7386. this.chart = chart;
  7387. this.options = options;
  7388. // Keep track of the current series
  7389. //this.currentSeries = UNDEFINED;
  7390. // List of crosshairs
  7391. this.crosshairs = [];
  7392. // Current values of x and y when animating
  7393. this.now = { x: 0, y: 0 };
  7394. // The tooltip is initially hidden
  7395. this.isHidden = true;
  7396. // create the label
  7397. this.label = chart.renderer.label('', 0, 0, options.shape || 'callout', null, null, options.useHTML, null, 'tooltip')
  7398. .attr({
  7399. padding: padding,
  7400. fill: options.backgroundColor,
  7401. 'stroke-width': borderWidth,
  7402. r: options.borderRadius,
  7403. zIndex: 8
  7404. })
  7405. .css(style)
  7406. .css({ padding: 0 }) // Remove it from VML, the padding is applied as an attribute instead (#1117)
  7407. .add()
  7408. .attr({ y: -9999 }); // #2301, #2657
  7409. // When using canVG the shadow shows up as a gray circle
  7410. // even if the tooltip is hidden.
  7411. if (!useCanVG) {
  7412. this.label.shadow(options.shadow);
  7413. }
  7414. // Public property for getting the shared state.
  7415. this.shared = options.shared;
  7416. },
  7417. /**
  7418. * Destroy the tooltip and its elements.
  7419. */
  7420. destroy: function () {
  7421. // Destroy and clear local variables
  7422. if (this.label) {
  7423. this.label = this.label.destroy();
  7424. }
  7425. clearTimeout(this.hideTimer);
  7426. clearTimeout(this.tooltipTimeout);
  7427. },
  7428. /**
  7429. * Provide a soft movement for the tooltip
  7430. *
  7431. * @param {Number} x
  7432. * @param {Number} y
  7433. * @private
  7434. */
  7435. move: function (x, y, anchorX, anchorY) {
  7436. var tooltip = this,
  7437. now = tooltip.now,
  7438. animate = tooltip.options.animation !== false && !tooltip.isHidden &&
  7439. // When we get close to the target position, abort animation and land on the right place (#3056)
  7440. (mathAbs(x - now.x) > 1 || mathAbs(y - now.y) > 1),
  7441. skipAnchor = tooltip.followPointer || tooltip.len > 1;
  7442. // Get intermediate values for animation
  7443. extend(now, {
  7444. x: animate ? (2 * now.x + x) / 3 : x,
  7445. y: animate ? (now.y + y) / 2 : y,
  7446. anchorX: skipAnchor ? UNDEFINED : animate ? (2 * now.anchorX + anchorX) / 3 : anchorX,
  7447. anchorY: skipAnchor ? UNDEFINED : animate ? (now.anchorY + anchorY) / 2 : anchorY
  7448. });
  7449. // Move to the intermediate value
  7450. tooltip.label.attr(now);
  7451. // Run on next tick of the mouse tracker
  7452. if (animate) {
  7453. // Never allow two timeouts
  7454. clearTimeout(this.tooltipTimeout);
  7455. // Set the fixed interval ticking for the smooth tooltip
  7456. this.tooltipTimeout = setTimeout(function () {
  7457. // The interval function may still be running during destroy, so check that the chart is really there before calling.
  7458. if (tooltip) {
  7459. tooltip.move(x, y, anchorX, anchorY);
  7460. }
  7461. }, 32);
  7462. }
  7463. },
  7464. /**
  7465. * Hide the tooltip
  7466. */
  7467. hide: function () {
  7468. var tooltip = this,
  7469. hoverPoints;
  7470. clearTimeout(this.hideTimer); // disallow duplicate timers (#1728, #1766)
  7471. if (!this.isHidden) {
  7472. hoverPoints = this.chart.hoverPoints;
  7473. this.hideTimer = setTimeout(function () {
  7474. tooltip.label.fadeOut();
  7475. tooltip.isHidden = true;
  7476. }, pick(this.options.hideDelay, 500));
  7477. // hide previous hoverPoints and set new
  7478. if (hoverPoints) {
  7479. each(hoverPoints, function (point) {
  7480. point.setState();
  7481. });
  7482. }
  7483. this.chart.hoverPoints = null;
  7484. }
  7485. },
  7486. /**
  7487. * Extendable method to get the anchor position of the tooltip
  7488. * from a point or set of points
  7489. */
  7490. getAnchor: function (points, mouseEvent) {
  7491. var ret,
  7492. chart = this.chart,
  7493. inverted = chart.inverted,
  7494. plotTop = chart.plotTop,
  7495. plotX = 0,
  7496. plotY = 0,
  7497. yAxis;
  7498. points = splat(points);
  7499. // Pie uses a special tooltipPos
  7500. ret = points[0].tooltipPos;
  7501. // When tooltip follows mouse, relate the position to the mouse
  7502. if (this.followPointer && mouseEvent) {
  7503. if (mouseEvent.chartX === UNDEFINED) {
  7504. mouseEvent = chart.pointer.normalize(mouseEvent);
  7505. }
  7506. ret = [
  7507. mouseEvent.chartX - chart.plotLeft,
  7508. mouseEvent.chartY - plotTop
  7509. ];
  7510. }
  7511. // When shared, use the average position
  7512. if (!ret) {
  7513. each(points, function (point) {
  7514. yAxis = point.series.yAxis;
  7515. plotX += point.plotX;
  7516. plotY += (point.plotLow ? (point.plotLow + point.plotHigh) / 2 : point.plotY) +
  7517. (!inverted && yAxis ? yAxis.top - plotTop : 0); // #1151
  7518. });
  7519. plotX /= points.length;
  7520. plotY /= points.length;
  7521. ret = [
  7522. inverted ? chart.plotWidth - plotY : plotX,
  7523. this.shared && !inverted && points.length > 1 && mouseEvent ?
  7524. mouseEvent.chartY - plotTop : // place shared tooltip next to the mouse (#424)
  7525. inverted ? chart.plotHeight - plotX : plotY
  7526. ];
  7527. }
  7528. return map(ret, mathRound);
  7529. },
  7530. /**
  7531. * Place the tooltip in a chart without spilling over
  7532. * and not covering the point it self.
  7533. */
  7534. getPosition: function (boxWidth, boxHeight, point) {
  7535. var chart = this.chart,
  7536. distance = this.distance,
  7537. ret = {},
  7538. swapped,
  7539. first = ['y', chart.chartHeight, boxHeight, point.plotY + chart.plotTop],
  7540. second = ['x', chart.chartWidth, boxWidth, point.plotX + chart.plotLeft],
  7541. // The far side is right or bottom
  7542. preferFarSide = point.ttBelow || (chart.inverted && !point.negative) || (!chart.inverted && point.negative),
  7543. /**
  7544. * Handle the preferred dimension. When the preferred dimension is tooltip
  7545. * on top or bottom of the point, it will look for space there.
  7546. */
  7547. firstDimension = function (dim, outerSize, innerSize, point) {
  7548. var roomLeft = innerSize < point - distance,
  7549. roomRight = point + distance + innerSize < outerSize,
  7550. alignedLeft = point - distance - innerSize,
  7551. alignedRight = point + distance;
  7552. if (preferFarSide && roomRight) {
  7553. ret[dim] = alignedRight;
  7554. } else if (!preferFarSide && roomLeft) {
  7555. ret[dim] = alignedLeft;
  7556. } else if (roomLeft) {
  7557. ret[dim] = alignedLeft;
  7558. } else if (roomRight) {
  7559. ret[dim] = alignedRight;
  7560. } else {
  7561. return false;
  7562. }
  7563. },
  7564. /**
  7565. * Handle the secondary dimension. If the preferred dimension is tooltip
  7566. * on top or bottom of the point, the second dimension is to align the tooltip
  7567. * above the point, trying to align center but allowing left or right
  7568. * align within the chart box.
  7569. */
  7570. secondDimension = function (dim, outerSize, innerSize, point) {
  7571. // Too close to the edge, return false and swap dimensions
  7572. if (point < distance || point > outerSize - distance) {
  7573. return false;
  7574. // Align left/top
  7575. } else if (point < innerSize / 2) {
  7576. ret[dim] = 1;
  7577. // Align right/bottom
  7578. } else if (point > outerSize - innerSize / 2) {
  7579. ret[dim] = outerSize - innerSize - 2;
  7580. // Align center
  7581. } else {
  7582. ret[dim] = point - innerSize / 2;
  7583. }
  7584. },
  7585. /**
  7586. * Swap the dimensions
  7587. */
  7588. swap = function (count) {
  7589. var temp = first;
  7590. first = second;
  7591. second = temp;
  7592. swapped = count;
  7593. },
  7594. run = function () {
  7595. if (firstDimension.apply(0, first) !== false) {
  7596. if (secondDimension.apply(0, second) === false && !swapped) {
  7597. swap(true);
  7598. run();
  7599. }
  7600. } else if (!swapped) {
  7601. swap(true);
  7602. run();
  7603. } else {
  7604. ret.x = ret.y = 0;
  7605. }
  7606. };
  7607. // Under these conditions, prefer the tooltip on the side of the point
  7608. if (chart.inverted || this.len > 1) {
  7609. swap();
  7610. }
  7611. run();
  7612. return ret;
  7613. },
  7614. /**
  7615. * In case no user defined formatter is given, this will be used. Note that the context
  7616. * here is an object holding point, series, x, y etc.
  7617. */
  7618. defaultFormatter: function (tooltip) {
  7619. var items = this.points || splat(this),
  7620. series = items[0].series,
  7621. s;
  7622. // build the header
  7623. s = [tooltip.tooltipHeaderFormatter(items[0])];
  7624. // build the values
  7625. each(items, function (item) {
  7626. series = item.series;
  7627. s.push((series.tooltipFormatter && series.tooltipFormatter(item)) ||
  7628. item.point.tooltipFormatter(series.tooltipOptions.pointFormat));
  7629. });
  7630. // footer
  7631. s.push(tooltip.options.footerFormat || '');
  7632. return s.join('');
  7633. },
  7634. /**
  7635. * Refresh the tooltip's text and position.
  7636. * @param {Object} point
  7637. */
  7638. refresh: function (point, mouseEvent) {
  7639. var tooltip = this,
  7640. chart = tooltip.chart,
  7641. label = tooltip.label,
  7642. options = tooltip.options,
  7643. x,
  7644. y,
  7645. anchor,
  7646. textConfig = {},
  7647. text,
  7648. pointConfig = [],
  7649. formatter = options.formatter || tooltip.defaultFormatter,
  7650. hoverPoints = chart.hoverPoints,
  7651. borderColor,
  7652. shared = tooltip.shared,
  7653. currentSeries;
  7654. clearTimeout(this.hideTimer);
  7655. // get the reference point coordinates (pie charts use tooltipPos)
  7656. tooltip.followPointer = splat(point)[0].series.tooltipOptions.followPointer;
  7657. anchor = tooltip.getAnchor(point, mouseEvent);
  7658. x = anchor[0];
  7659. y = anchor[1];
  7660. // shared tooltip, array is sent over
  7661. if (shared && !(point.series && point.series.noSharedTooltip)) {
  7662. // hide previous hoverPoints and set new
  7663. chart.hoverPoints = point;
  7664. if (hoverPoints) {
  7665. each(hoverPoints, function (point) {
  7666. point.setState();
  7667. });
  7668. }
  7669. each(point, function (item) {
  7670. item.setState(HOVER_STATE);
  7671. pointConfig.push(item.getLabelConfig());
  7672. });
  7673. textConfig = {
  7674. x: point[0].category,
  7675. y: point[0].y
  7676. };
  7677. textConfig.points = pointConfig;
  7678. this.len = pointConfig.length;
  7679. point = point[0];
  7680. // single point tooltip
  7681. } else {
  7682. textConfig = point.getLabelConfig();
  7683. }
  7684. text = formatter.call(textConfig, tooltip);
  7685. // register the current series
  7686. currentSeries = point.series;
  7687. this.distance = pick(currentSeries.tooltipOptions.distance, 16);
  7688. // update the inner HTML
  7689. if (text === false) {
  7690. this.hide();
  7691. } else {
  7692. // show it
  7693. if (tooltip.isHidden) {
  7694. stop(label);
  7695. label.attr('opacity', 1).show();
  7696. }
  7697. // update text
  7698. label.attr({
  7699. text: text
  7700. });
  7701. // set the stroke color of the box
  7702. borderColor = options.borderColor || point.color || currentSeries.color || '#606060';
  7703. label.attr({
  7704. stroke: borderColor
  7705. });
  7706. tooltip.updatePosition({ plotX: x, plotY: y, negative: point.negative, ttBelow: point.ttBelow });
  7707. this.isHidden = false;
  7708. }
  7709. fireEvent(chart, 'tooltipRefresh', {
  7710. text: text,
  7711. x: x + chart.plotLeft,
  7712. y: y + chart.plotTop,
  7713. borderColor: borderColor
  7714. });
  7715. },
  7716. /**
  7717. * Find the new position and perform the move
  7718. */
  7719. updatePosition: function (point) {
  7720. var chart = this.chart,
  7721. label = this.label,
  7722. pos = (this.options.positioner || this.getPosition).call(
  7723. this,
  7724. label.width,
  7725. label.height,
  7726. point
  7727. );
  7728. // do the move
  7729. this.move(
  7730. mathRound(pos.x),
  7731. mathRound(pos.y),
  7732. point.plotX + chart.plotLeft,
  7733. point.plotY + chart.plotTop
  7734. );
  7735. },
  7736. /**
  7737. * Format the header of the tooltip
  7738. */
  7739. tooltipHeaderFormatter: function (point) {
  7740. var series = point.series,
  7741. tooltipOptions = series.tooltipOptions,
  7742. dateTimeLabelFormats = tooltipOptions.dateTimeLabelFormats,
  7743. xDateFormat = tooltipOptions.xDateFormat,
  7744. xAxis = series.xAxis,
  7745. isDateTime = xAxis && xAxis.options.type === 'datetime' && isNumber(point.key),
  7746. headerFormat = tooltipOptions.headerFormat,
  7747. closestPointRange = xAxis && xAxis.closestPointRange,
  7748. n;
  7749. // Guess the best date format based on the closest point distance (#568)
  7750. if (isDateTime && !xDateFormat) {
  7751. if (closestPointRange) {
  7752. for (n in timeUnits) {
  7753. if (timeUnits[n] >= closestPointRange ||
  7754. // If the point is placed every day at 23:59, we need to show
  7755. // the minutes as well. This logic only works for time units less than
  7756. // a day, since all higher time units are dividable by those. #2637.
  7757. (timeUnits[n] <= timeUnits.day && point.key % timeUnits[n] > 0)) {
  7758. xDateFormat = dateTimeLabelFormats[n];
  7759. break;
  7760. }
  7761. }
  7762. } else {
  7763. xDateFormat = dateTimeLabelFormats.day;
  7764. }
  7765. xDateFormat = xDateFormat || dateTimeLabelFormats.year; // #2546, 2581
  7766. }
  7767. // Insert the header date format if any
  7768. if (isDateTime && xDateFormat) {
  7769. headerFormat = headerFormat.replace('{point.key}', '{point.key:' + xDateFormat + '}');
  7770. }
  7771. return format(headerFormat, {
  7772. point: point,
  7773. series: series
  7774. });
  7775. }
  7776. };
  7777. var hoverChartIndex;
  7778. // Global flag for touch support
  7779. hasTouch = doc.documentElement.ontouchstart !== UNDEFINED;
  7780. /**
  7781. * The mouse tracker object. All methods starting with "on" are primary DOM event handlers.
  7782. * Subsequent methods should be named differently from what they are doing.
  7783. * @param {Object} chart The Chart instance
  7784. * @param {Object} options The root options object
  7785. */
  7786. var Pointer = Highcharts.Pointer = function (chart, options) {
  7787. this.init(chart, options);
  7788. };
  7789. Pointer.prototype = {
  7790. /**
  7791. * Initialize Pointer
  7792. */
  7793. init: function (chart, options) {
  7794. var chartOptions = options.chart,
  7795. chartEvents = chartOptions.events,
  7796. zoomType = useCanVG ? '' : chartOptions.zoomType,
  7797. inverted = chart.inverted,
  7798. zoomX,
  7799. zoomY;
  7800. // Store references
  7801. this.options = options;
  7802. this.chart = chart;
  7803. // Zoom status
  7804. this.zoomX = zoomX = /x/.test(zoomType);
  7805. this.zoomY = zoomY = /y/.test(zoomType);
  7806. this.zoomHor = (zoomX && !inverted) || (zoomY && inverted);
  7807. this.zoomVert = (zoomY && !inverted) || (zoomX && inverted);
  7808. this.hasZoom = zoomX || zoomY;
  7809. // Do we need to handle click on a touch device?
  7810. this.runChartClick = chartEvents && !!chartEvents.click;
  7811. this.pinchDown = [];
  7812. this.lastValidTouch = {};
  7813. if (Highcharts.Tooltip && options.tooltip.enabled) {
  7814. chart.tooltip = new Tooltip(chart, options.tooltip);
  7815. this.followTouchMove = options.tooltip.followTouchMove;
  7816. }
  7817. this.setDOMEvents();
  7818. },
  7819. /**
  7820. * Add crossbrowser support for chartX and chartY
  7821. * @param {Object} e The event object in standard browsers
  7822. */
  7823. normalize: function (e, chartPosition) {
  7824. var chartX,
  7825. chartY,
  7826. ePos;
  7827. // common IE normalizing
  7828. e = e || window.event;
  7829. // Framework specific normalizing (#1165)
  7830. e = washMouseEvent(e);
  7831. // More IE normalizing, needs to go after washMouseEvent
  7832. if (!e.target) {
  7833. e.target = e.srcElement;
  7834. }
  7835. // iOS (#2757)
  7836. ePos = e.touches ? (e.touches.length ? e.touches.item(0) : e.changedTouches[0]) : e;
  7837. // Get mouse position
  7838. if (!chartPosition) {
  7839. this.chartPosition = chartPosition = offset(this.chart.container);
  7840. }
  7841. // chartX and chartY
  7842. if (ePos.pageX === UNDEFINED) { // IE < 9. #886.
  7843. chartX = mathMax(e.x, e.clientX - chartPosition.left); // #2005, #2129: the second case is
  7844. // for IE10 quirks mode within framesets
  7845. chartY = e.y;
  7846. } else {
  7847. chartX = ePos.pageX - chartPosition.left;
  7848. chartY = ePos.pageY - chartPosition.top;
  7849. }
  7850. return extend(e, {
  7851. chartX: mathRound(chartX),
  7852. chartY: mathRound(chartY)
  7853. });
  7854. },
  7855. /**
  7856. * Get the click position in terms of axis values.
  7857. *
  7858. * @param {Object} e A pointer event
  7859. */
  7860. getCoordinates: function (e) {
  7861. var coordinates = {
  7862. xAxis: [],
  7863. yAxis: []
  7864. };
  7865. each(this.chart.axes, function (axis) {
  7866. coordinates[axis.isXAxis ? 'xAxis' : 'yAxis'].push({
  7867. axis: axis,
  7868. value: axis.toValue(e[axis.horiz ? 'chartX' : 'chartY'])
  7869. });
  7870. });
  7871. return coordinates;
  7872. },
  7873. /**
  7874. * Return the index in the tooltipPoints array, corresponding to pixel position in
  7875. * the plot area.
  7876. */
  7877. getIndex: function (e) {
  7878. var chart = this.chart;
  7879. return chart.inverted ?
  7880. chart.plotHeight + chart.plotTop - e.chartY :
  7881. e.chartX - chart.plotLeft;
  7882. },
  7883. /**
  7884. * With line type charts with a single tracker, get the point closest to the mouse.
  7885. * Run Point.onMouseOver and display tooltip for the point or points.
  7886. */
  7887. runPointActions: function (e) {
  7888. var pointer = this,
  7889. chart = pointer.chart,
  7890. series = chart.series,
  7891. tooltip = chart.tooltip,
  7892. followPointer,
  7893. point,
  7894. points,
  7895. hoverPoint = chart.hoverPoint,
  7896. hoverSeries = chart.hoverSeries,
  7897. i,
  7898. j,
  7899. distance = chart.chartWidth,
  7900. index = pointer.getIndex(e),
  7901. anchor;
  7902. // shared tooltip
  7903. if (tooltip && pointer.options.tooltip.shared && !(hoverSeries && hoverSeries.noSharedTooltip)) {
  7904. points = [];
  7905. // loop over all series and find the ones with points closest to the mouse
  7906. i = series.length;
  7907. for (j = 0; j < i; j++) {
  7908. if (series[j].visible &&
  7909. series[j].options.enableMouseTracking !== false &&
  7910. !series[j].noSharedTooltip && series[j].singularTooltips !== true && series[j].tooltipPoints.length) {
  7911. point = series[j].tooltipPoints[index];
  7912. if (point && point.series) { // not a dummy point, #1544
  7913. point._dist = mathAbs(index - point.clientX);
  7914. distance = mathMin(distance, point._dist);
  7915. points.push(point);
  7916. }
  7917. }
  7918. }
  7919. // remove furthest points
  7920. i = points.length;
  7921. while (i--) {
  7922. if (points[i]._dist > distance) {
  7923. points.splice(i, 1);
  7924. }
  7925. }
  7926. // refresh the tooltip if necessary
  7927. if (points.length && (points[0].clientX !== pointer.hoverX)) {
  7928. tooltip.refresh(points, e);
  7929. pointer.hoverX = points[0].clientX;
  7930. }
  7931. }
  7932. // Separate tooltip and general mouse events
  7933. followPointer = hoverSeries && hoverSeries.tooltipOptions.followPointer;
  7934. if (hoverSeries && hoverSeries.tracker && !followPointer) { // #2584, #2830
  7935. // get the point
  7936. point = hoverSeries.tooltipPoints[index];
  7937. // a new point is hovered, refresh the tooltip
  7938. if (point && point !== hoverPoint) {
  7939. // trigger the events
  7940. point.onMouseOver(e);
  7941. }
  7942. } else if (tooltip && followPointer && !tooltip.isHidden) {
  7943. anchor = tooltip.getAnchor([{}], e);
  7944. tooltip.updatePosition({ plotX: anchor[0], plotY: anchor[1] });
  7945. }
  7946. // Start the event listener to pick up the tooltip
  7947. if (tooltip && !pointer._onDocumentMouseMove) {
  7948. pointer._onDocumentMouseMove = function (e) {
  7949. if (charts[hoverChartIndex]) {
  7950. charts[hoverChartIndex].pointer.onDocumentMouseMove(e);
  7951. }
  7952. };
  7953. addEvent(doc, 'mousemove', pointer._onDocumentMouseMove);
  7954. }
  7955. // Draw independent crosshairs
  7956. each(chart.axes, function (axis) {
  7957. axis.drawCrosshair(e, pick(point, hoverPoint));
  7958. });
  7959. },
  7960. /**
  7961. * Reset the tracking by hiding the tooltip, the hover series state and the hover point
  7962. *
  7963. * @param allowMove {Boolean} Instead of destroying the tooltip altogether, allow moving it if possible
  7964. */
  7965. reset: function (allowMove) {
  7966. var pointer = this,
  7967. chart = pointer.chart,
  7968. hoverSeries = chart.hoverSeries,
  7969. hoverPoint = chart.hoverPoint,
  7970. tooltip = chart.tooltip,
  7971. tooltipPoints = tooltip && tooltip.shared ? chart.hoverPoints : hoverPoint;
  7972. // Narrow in allowMove
  7973. allowMove = allowMove && tooltip && tooltipPoints;
  7974. // Check if the points have moved outside the plot area, #1003
  7975. if (allowMove && splat(tooltipPoints)[0].plotX === UNDEFINED) {
  7976. allowMove = false;
  7977. }
  7978. // Just move the tooltip, #349
  7979. if (allowMove) {
  7980. tooltip.refresh(tooltipPoints);
  7981. if (hoverPoint) { // #2500
  7982. hoverPoint.setState(hoverPoint.state, true);
  7983. }
  7984. // Full reset
  7985. } else {
  7986. if (hoverPoint) {
  7987. hoverPoint.onMouseOut();
  7988. }
  7989. if (hoverSeries) {
  7990. hoverSeries.onMouseOut();
  7991. }
  7992. if (tooltip) {
  7993. tooltip.hide();
  7994. }
  7995. if (pointer._onDocumentMouseMove) {
  7996. removeEvent(doc, 'mousemove', pointer._onDocumentMouseMove);
  7997. pointer._onDocumentMouseMove = null;
  7998. }
  7999. // Remove crosshairs
  8000. each(chart.axes, function (axis) {
  8001. axis.hideCrosshair();
  8002. });
  8003. pointer.hoverX = null;
  8004. }
  8005. },
  8006. /**
  8007. * Scale series groups to a certain scale and translation
  8008. */
  8009. scaleGroups: function (attribs, clip) {
  8010. var chart = this.chart,
  8011. seriesAttribs;
  8012. // Scale each series
  8013. each(chart.series, function (series) {
  8014. seriesAttribs = attribs || series.getPlotBox(); // #1701
  8015. if (series.xAxis && series.xAxis.zoomEnabled) {
  8016. series.group.attr(seriesAttribs);
  8017. if (series.markerGroup) {
  8018. series.markerGroup.attr(seriesAttribs);
  8019. series.markerGroup.clip(clip ? chart.clipRect : null);
  8020. }
  8021. if (series.dataLabelsGroup) {
  8022. series.dataLabelsGroup.attr(seriesAttribs);
  8023. }
  8024. }
  8025. });
  8026. // Clip
  8027. chart.clipRect.attr(clip || chart.clipBox);
  8028. },
  8029. /**
  8030. * Start a drag operation
  8031. */
  8032. dragStart: function (e) {
  8033. var chart = this.chart;
  8034. // Record the start position
  8035. chart.mouseIsDown = e.type;
  8036. chart.cancelClick = false;
  8037. chart.mouseDownX = this.mouseDownX = e.chartX;
  8038. chart.mouseDownY = this.mouseDownY = e.chartY;
  8039. },
  8040. /**
  8041. * Perform a drag operation in response to a mousemove event while the mouse is down
  8042. */
  8043. drag: function (e) {
  8044. var chart = this.chart,
  8045. chartOptions = chart.options.chart,
  8046. chartX = e.chartX,
  8047. chartY = e.chartY,
  8048. zoomHor = this.zoomHor,
  8049. zoomVert = this.zoomVert,
  8050. plotLeft = chart.plotLeft,
  8051. plotTop = chart.plotTop,
  8052. plotWidth = chart.plotWidth,
  8053. plotHeight = chart.plotHeight,
  8054. clickedInside,
  8055. size,
  8056. mouseDownX = this.mouseDownX,
  8057. mouseDownY = this.mouseDownY,
  8058. panKey = chartOptions.panKey && e[chartOptions.panKey + 'Key'];
  8059. // If the mouse is outside the plot area, adjust to cooordinates
  8060. // inside to prevent the selection marker from going outside
  8061. if (chartX < plotLeft) {
  8062. chartX = plotLeft;
  8063. } else if (chartX > plotLeft + plotWidth) {
  8064. chartX = plotLeft + plotWidth;
  8065. }
  8066. if (chartY < plotTop) {
  8067. chartY = plotTop;
  8068. } else if (chartY > plotTop + plotHeight) {
  8069. chartY = plotTop + plotHeight;
  8070. }
  8071. // determine if the mouse has moved more than 10px
  8072. this.hasDragged = Math.sqrt(
  8073. Math.pow(mouseDownX - chartX, 2) +
  8074. Math.pow(mouseDownY - chartY, 2)
  8075. );
  8076. if (this.hasDragged > 10) {
  8077. clickedInside = chart.isInsidePlot(mouseDownX - plotLeft, mouseDownY - plotTop);
  8078. // make a selection
  8079. if (chart.hasCartesianSeries && (this.zoomX || this.zoomY) && clickedInside && !panKey) {
  8080. if (!this.selectionMarker) {
  8081. this.selectionMarker = chart.renderer.rect(
  8082. plotLeft,
  8083. plotTop,
  8084. zoomHor ? 1 : plotWidth,
  8085. zoomVert ? 1 : plotHeight,
  8086. 0
  8087. )
  8088. .attr({
  8089. fill: chartOptions.selectionMarkerFill || 'rgba(69,114,167,0.25)',
  8090. zIndex: 7
  8091. })
  8092. .add();
  8093. }
  8094. }
  8095. // adjust the width of the selection marker
  8096. if (this.selectionMarker && zoomHor) {
  8097. size = chartX - mouseDownX;
  8098. this.selectionMarker.attr({
  8099. width: mathAbs(size),
  8100. x: (size > 0 ? 0 : size) + mouseDownX
  8101. });
  8102. }
  8103. // adjust the height of the selection marker
  8104. if (this.selectionMarker && zoomVert) {
  8105. size = chartY - mouseDownY;
  8106. this.selectionMarker.attr({
  8107. height: mathAbs(size),
  8108. y: (size > 0 ? 0 : size) + mouseDownY
  8109. });
  8110. }
  8111. // panning
  8112. if (clickedInside && !this.selectionMarker && chartOptions.panning) {
  8113. chart.pan(e, chartOptions.panning);
  8114. }
  8115. }
  8116. },
  8117. /**
  8118. * On mouse up or touch end across the entire document, drop the selection.
  8119. */
  8120. drop: function (e) {
  8121. var chart = this.chart,
  8122. hasPinched = this.hasPinched;
  8123. if (this.selectionMarker) {
  8124. var selectionData = {
  8125. xAxis: [],
  8126. yAxis: [],
  8127. originalEvent: e.originalEvent || e
  8128. },
  8129. selectionBox = this.selectionMarker,
  8130. selectionLeft = selectionBox.attr ? selectionBox.attr('x') : selectionBox.x,
  8131. selectionTop = selectionBox.attr ? selectionBox.attr('y') : selectionBox.y,
  8132. selectionWidth = selectionBox.attr ? selectionBox.attr('width') : selectionBox.width,
  8133. selectionHeight = selectionBox.attr ? selectionBox.attr('height') : selectionBox.height,
  8134. runZoom;
  8135. // a selection has been made
  8136. if (this.hasDragged || hasPinched) {
  8137. // record each axis' min and max
  8138. each(chart.axes, function (axis) {
  8139. if (axis.zoomEnabled) {
  8140. var horiz = axis.horiz,
  8141. minPixelPadding = e.type === 'touchend' ? axis.minPixelPadding: 0, // #1207, #3075
  8142. selectionMin = axis.toValue((horiz ? selectionLeft : selectionTop) + minPixelPadding),
  8143. selectionMax = axis.toValue((horiz ? selectionLeft + selectionWidth : selectionTop + selectionHeight) - minPixelPadding);
  8144. if (!isNaN(selectionMin) && !isNaN(selectionMax)) { // #859
  8145. selectionData[axis.coll].push({
  8146. axis: axis,
  8147. min: mathMin(selectionMin, selectionMax), // for reversed axes,
  8148. max: mathMax(selectionMin, selectionMax)
  8149. });
  8150. runZoom = true;
  8151. }
  8152. }
  8153. });
  8154. if (runZoom) {
  8155. fireEvent(chart, 'selection', selectionData, function (args) {
  8156. chart.zoom(extend(args, hasPinched ? { animation: false } : null));
  8157. });
  8158. }
  8159. }
  8160. this.selectionMarker = this.selectionMarker.destroy();
  8161. // Reset scaling preview
  8162. if (hasPinched) {
  8163. this.scaleGroups();
  8164. }
  8165. }
  8166. // Reset all
  8167. if (chart) { // it may be destroyed on mouse up - #877
  8168. css(chart.container, { cursor: chart._cursor });
  8169. chart.cancelClick = this.hasDragged > 10; // #370
  8170. chart.mouseIsDown = this.hasDragged = this.hasPinched = false;
  8171. this.pinchDown = [];
  8172. }
  8173. },
  8174. onContainerMouseDown: function (e) {
  8175. e = this.normalize(e);
  8176. // issue #295, dragging not always working in Firefox
  8177. if (e.preventDefault) {
  8178. e.preventDefault();
  8179. }
  8180. this.dragStart(e);
  8181. },
  8182. onDocumentMouseUp: function (e) {
  8183. if (charts[hoverChartIndex]) {
  8184. charts[hoverChartIndex].pointer.drop(e);
  8185. }
  8186. },
  8187. /**
  8188. * Special handler for mouse move that will hide the tooltip when the mouse leaves the plotarea.
  8189. * Issue #149 workaround. The mouseleave event does not always fire.
  8190. */
  8191. onDocumentMouseMove: function (e) {
  8192. var chart = this.chart,
  8193. chartPosition = this.chartPosition,
  8194. hoverSeries = chart.hoverSeries;
  8195. e = this.normalize(e, chartPosition);
  8196. // If we're outside, hide the tooltip
  8197. if (chartPosition && hoverSeries && !this.inClass(e.target, 'highcharts-tracker') &&
  8198. !chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop)) {
  8199. this.reset();
  8200. }
  8201. },
  8202. /**
  8203. * When mouse leaves the container, hide the tooltip.
  8204. */
  8205. onContainerMouseLeave: function () {
  8206. var chart = charts[hoverChartIndex];
  8207. if (chart) {
  8208. chart.pointer.reset();
  8209. chart.pointer.chartPosition = null; // also reset the chart position, used in #149 fix
  8210. }
  8211. },
  8212. // The mousemove, touchmove and touchstart event handler
  8213. onContainerMouseMove: function (e) {
  8214. var chart = this.chart;
  8215. hoverChartIndex = chart.index;
  8216. e = this.normalize(e);
  8217. e.returnValue = false; // #2251, #3224
  8218. if (chart.mouseIsDown === 'mousedown') {
  8219. this.drag(e);
  8220. }
  8221. // Show the tooltip and run mouse over events (#977)
  8222. if ((this.inClass(e.target, 'highcharts-tracker') ||
  8223. chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop)) && !chart.openMenu) {
  8224. this.runPointActions(e);
  8225. }
  8226. },
  8227. /**
  8228. * Utility to detect whether an element has, or has a parent with, a specific
  8229. * class name. Used on detection of tracker objects and on deciding whether
  8230. * hovering the tooltip should cause the active series to mouse out.
  8231. */
  8232. inClass: function (element, className) {
  8233. var elemClassName;
  8234. while (element) {
  8235. elemClassName = attr(element, 'class');
  8236. if (elemClassName) {
  8237. if (elemClassName.indexOf(className) !== -1) {
  8238. return true;
  8239. } else if (elemClassName.indexOf(PREFIX + 'container') !== -1) {
  8240. return false;
  8241. }
  8242. }
  8243. element = element.parentNode;
  8244. }
  8245. },
  8246. onTrackerMouseOut: function (e) {
  8247. var series = this.chart.hoverSeries,
  8248. relatedTarget = e.relatedTarget || e.toElement,
  8249. relatedSeries = relatedTarget && relatedTarget.point && relatedTarget.point.series; // #2499
  8250. if (series && !series.options.stickyTracking && !this.inClass(relatedTarget, PREFIX + 'tooltip') &&
  8251. relatedSeries !== series) {
  8252. series.onMouseOut();
  8253. }
  8254. },
  8255. onContainerClick: function (e) {
  8256. var chart = this.chart,
  8257. hoverPoint = chart.hoverPoint,
  8258. plotLeft = chart.plotLeft,
  8259. plotTop = chart.plotTop;
  8260. e = this.normalize(e);
  8261. e.cancelBubble = true; // IE specific
  8262. if (!chart.cancelClick) {
  8263. // On tracker click, fire the series and point events. #783, #1583
  8264. if (hoverPoint && this.inClass(e.target, PREFIX + 'tracker')) {
  8265. // the series click event
  8266. fireEvent(hoverPoint.series, 'click', extend(e, {
  8267. point: hoverPoint
  8268. }));
  8269. // the point click event
  8270. if (chart.hoverPoint) { // it may be destroyed (#1844)
  8271. hoverPoint.firePointEvent('click', e);
  8272. }
  8273. // When clicking outside a tracker, fire a chart event
  8274. } else {
  8275. extend(e, this.getCoordinates(e));
  8276. // fire a click event in the chart
  8277. if (chart.isInsidePlot(e.chartX - plotLeft, e.chartY - plotTop)) {
  8278. fireEvent(chart, 'click', e);
  8279. }
  8280. }
  8281. }
  8282. },
  8283. /**
  8284. * Set the JS DOM events on the container and document. This method should contain
  8285. * a one-to-one assignment between methods and their handlers. Any advanced logic should
  8286. * be moved to the handler reflecting the event's name.
  8287. */
  8288. setDOMEvents: function () {
  8289. var pointer = this,
  8290. container = pointer.chart.container;
  8291. container.onmousedown = function (e) {
  8292. pointer.onContainerMouseDown(e);
  8293. };
  8294. container.onmousemove = function (e) {
  8295. pointer.onContainerMouseMove(e);
  8296. };
  8297. container.onclick = function (e) {
  8298. pointer.onContainerClick(e);
  8299. };
  8300. addEvent(container, 'mouseleave', pointer.onContainerMouseLeave);
  8301. if (chartCount === 1) {
  8302. addEvent(doc, 'mouseup', pointer.onDocumentMouseUp);
  8303. }
  8304. if (hasTouch) {
  8305. container.ontouchstart = function (e) {
  8306. pointer.onContainerTouchStart(e);
  8307. };
  8308. container.ontouchmove = function (e) {
  8309. pointer.onContainerTouchMove(e);
  8310. };
  8311. if (chartCount === 1) {
  8312. addEvent(doc, 'touchend', pointer.onDocumentTouchEnd);
  8313. }
  8314. }
  8315. },
  8316. /**
  8317. * Destroys the Pointer object and disconnects DOM events.
  8318. */
  8319. destroy: function () {
  8320. var prop;
  8321. removeEvent(this.chart.container, 'mouseleave', this.onContainerMouseLeave);
  8322. if (!chartCount) {
  8323. removeEvent(doc, 'mouseup', this.onDocumentMouseUp);
  8324. removeEvent(doc, 'touchend', this.onDocumentTouchEnd);
  8325. }
  8326. // memory and CPU leak
  8327. clearInterval(this.tooltipTimeout);
  8328. for (prop in this) {
  8329. this[prop] = null;
  8330. }
  8331. }
  8332. };
  8333. /* Support for touch devices */
  8334. extend(Highcharts.Pointer.prototype, {
  8335. /**
  8336. * Run translation operations
  8337. */
  8338. pinchTranslate: function (pinchDown, touches, transform, selectionMarker, clip, lastValidTouch) {
  8339. if (this.zoomHor || this.pinchHor) {
  8340. this.pinchTranslateDirection(true, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch);
  8341. }
  8342. if (this.zoomVert || this.pinchVert) {
  8343. this.pinchTranslateDirection(false, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch);
  8344. }
  8345. },
  8346. /**
  8347. * Run translation operations for each direction (horizontal and vertical) independently
  8348. */
  8349. pinchTranslateDirection: function (horiz, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch, forcedScale) {
  8350. var chart = this.chart,
  8351. xy = horiz ? 'x' : 'y',
  8352. XY = horiz ? 'X' : 'Y',
  8353. sChartXY = 'chart' + XY,
  8354. wh = horiz ? 'width' : 'height',
  8355. plotLeftTop = chart['plot' + (horiz ? 'Left' : 'Top')],
  8356. selectionWH,
  8357. selectionXY,
  8358. clipXY,
  8359. scale = forcedScale || 1,
  8360. inverted = chart.inverted,
  8361. bounds = chart.bounds[horiz ? 'h' : 'v'],
  8362. singleTouch = pinchDown.length === 1,
  8363. touch0Start = pinchDown[0][sChartXY],
  8364. touch0Now = touches[0][sChartXY],
  8365. touch1Start = !singleTouch && pinchDown[1][sChartXY],
  8366. touch1Now = !singleTouch && touches[1][sChartXY],
  8367. outOfBounds,
  8368. transformScale,
  8369. scaleKey,
  8370. setScale = function () {
  8371. if (!singleTouch && mathAbs(touch0Start - touch1Start) > 20) { // Don't zoom if fingers are too close on this axis
  8372. scale = forcedScale || mathAbs(touch0Now - touch1Now) / mathAbs(touch0Start - touch1Start);
  8373. }
  8374. clipXY = ((plotLeftTop - touch0Now) / scale) + touch0Start;
  8375. selectionWH = chart['plot' + (horiz ? 'Width' : 'Height')] / scale;
  8376. };
  8377. // Set the scale, first pass
  8378. setScale();
  8379. selectionXY = clipXY; // the clip position (x or y) is altered if out of bounds, the selection position is not
  8380. // Out of bounds
  8381. if (selectionXY < bounds.min) {
  8382. selectionXY = bounds.min;
  8383. outOfBounds = true;
  8384. } else if (selectionXY + selectionWH > bounds.max) {
  8385. selectionXY = bounds.max - selectionWH;
  8386. outOfBounds = true;
  8387. }
  8388. // Is the chart dragged off its bounds, determined by dataMin and dataMax?
  8389. if (outOfBounds) {
  8390. // Modify the touchNow position in order to create an elastic drag movement. This indicates
  8391. // to the user that the chart is responsive but can't be dragged further.
  8392. touch0Now -= 0.8 * (touch0Now - lastValidTouch[xy][0]);
  8393. if (!singleTouch) {
  8394. touch1Now -= 0.8 * (touch1Now - lastValidTouch[xy][1]);
  8395. }
  8396. // Set the scale, second pass to adapt to the modified touchNow positions
  8397. setScale();
  8398. } else {
  8399. lastValidTouch[xy] = [touch0Now, touch1Now];
  8400. }
  8401. // Set geometry for clipping, selection and transformation
  8402. if (!inverted) { // TODO: implement clipping for inverted charts
  8403. clip[xy] = clipXY - plotLeftTop;
  8404. clip[wh] = selectionWH;
  8405. }
  8406. scaleKey = inverted ? (horiz ? 'scaleY' : 'scaleX') : 'scale' + XY;
  8407. transformScale = inverted ? 1 / scale : scale;
  8408. selectionMarker[wh] = selectionWH;
  8409. selectionMarker[xy] = selectionXY;
  8410. transform[scaleKey] = scale;
  8411. transform['translate' + XY] = (transformScale * plotLeftTop) + (touch0Now - (transformScale * touch0Start));
  8412. },
  8413. /**
  8414. * Handle touch events with two touches
  8415. */
  8416. pinch: function (e) {
  8417. var self = this,
  8418. chart = self.chart,
  8419. pinchDown = self.pinchDown,
  8420. followTouchMove = self.followTouchMove,
  8421. touches = e.touches,
  8422. touchesLength = touches.length,
  8423. lastValidTouch = self.lastValidTouch,
  8424. hasZoom = self.hasZoom,
  8425. selectionMarker = self.selectionMarker,
  8426. transform = {},
  8427. fireClickEvent = touchesLength === 1 && ((self.inClass(e.target, PREFIX + 'tracker') &&
  8428. chart.runTrackerClick) || chart.runChartClick),
  8429. clip = {};
  8430. // On touch devices, only proceed to trigger click if a handler is defined
  8431. if ((hasZoom || followTouchMove) && !fireClickEvent) {
  8432. e.preventDefault();
  8433. }
  8434. // Normalize each touch
  8435. map(touches, function (e) {
  8436. return self.normalize(e);
  8437. });
  8438. // Register the touch start position
  8439. if (e.type === 'touchstart') {
  8440. each(touches, function (e, i) {
  8441. pinchDown[i] = { chartX: e.chartX, chartY: e.chartY };
  8442. });
  8443. lastValidTouch.x = [pinchDown[0].chartX, pinchDown[1] && pinchDown[1].chartX];
  8444. lastValidTouch.y = [pinchDown[0].chartY, pinchDown[1] && pinchDown[1].chartY];
  8445. // Identify the data bounds in pixels
  8446. each(chart.axes, function (axis) {
  8447. if (axis.zoomEnabled) {
  8448. var bounds = chart.bounds[axis.horiz ? 'h' : 'v'],
  8449. minPixelPadding = axis.minPixelPadding,
  8450. min = axis.toPixels(pick(axis.options.min, axis.dataMin)),
  8451. max = axis.toPixels(pick(axis.options.max, axis.dataMax)),
  8452. absMin = mathMin(min, max),
  8453. absMax = mathMax(min, max);
  8454. // Store the bounds for use in the touchmove handler
  8455. bounds.min = mathMin(axis.pos, absMin - minPixelPadding);
  8456. bounds.max = mathMax(axis.pos + axis.len, absMax + minPixelPadding);
  8457. }
  8458. });
  8459. // Event type is touchmove, handle panning and pinching
  8460. } else if (pinchDown.length) { // can be 0 when releasing, if touchend fires first
  8461. // Set the marker
  8462. if (!selectionMarker) {
  8463. self.selectionMarker = selectionMarker = extend({
  8464. destroy: noop
  8465. }, chart.plotBox);
  8466. }
  8467. self.pinchTranslate(pinchDown, touches, transform, selectionMarker, clip, lastValidTouch);
  8468. self.hasPinched = hasZoom;
  8469. // Scale and translate the groups to provide visual feedback during pinching
  8470. self.scaleGroups(transform, clip);
  8471. // Optionally move the tooltip on touchmove
  8472. if (!hasZoom && followTouchMove && touchesLength === 1) {
  8473. this.runPointActions(self.normalize(e));
  8474. }
  8475. }
  8476. },
  8477. onContainerTouchStart: function (e) {
  8478. var chart = this.chart;
  8479. hoverChartIndex = chart.index;
  8480. if (e.touches.length === 1) {
  8481. e = this.normalize(e);
  8482. if (chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop)) {
  8483. // Run mouse events and display tooltip etc
  8484. this.runPointActions(e);
  8485. this.pinch(e);
  8486. } else {
  8487. // Hide the tooltip on touching outside the plot area (#1203)
  8488. this.reset();
  8489. }
  8490. } else if (e.touches.length === 2) {
  8491. this.pinch(e);
  8492. }
  8493. },
  8494. onContainerTouchMove: function (e) {
  8495. if (e.touches.length === 1 || e.touches.length === 2) {
  8496. this.pinch(e);
  8497. }
  8498. },
  8499. onDocumentTouchEnd: function (e) {
  8500. if (charts[hoverChartIndex]) {
  8501. charts[hoverChartIndex].pointer.drop(e);
  8502. }
  8503. }
  8504. });
  8505. if (win.PointerEvent || win.MSPointerEvent) {
  8506. // The touches object keeps track of the points being touched at all times
  8507. var touches = {},
  8508. hasPointerEvent = !!win.PointerEvent,
  8509. getWebkitTouches = function () {
  8510. var key, fake = [];
  8511. fake.item = function (i) { return this[i]; };
  8512. for (key in touches) {
  8513. if (touches.hasOwnProperty(key)) {
  8514. fake.push({
  8515. pageX: touches[key].pageX,
  8516. pageY: touches[key].pageY,
  8517. target: touches[key].target
  8518. });
  8519. }
  8520. }
  8521. return fake;
  8522. },
  8523. translateMSPointer = function (e, method, wktype, callback) {
  8524. var p;
  8525. e = e.originalEvent || e;
  8526. if ((e.pointerType === 'touch' || e.pointerType === e.MSPOINTER_TYPE_TOUCH) && charts[hoverChartIndex]) {
  8527. callback(e);
  8528. p = charts[hoverChartIndex].pointer;
  8529. p[method]({
  8530. type: wktype,
  8531. target: e.currentTarget,
  8532. preventDefault: noop,
  8533. touches: getWebkitTouches()
  8534. });
  8535. }
  8536. };
  8537. /**
  8538. * Extend the Pointer prototype with methods for each event handler and more
  8539. */
  8540. extend(Pointer.prototype, {
  8541. onContainerPointerDown: function (e) {
  8542. translateMSPointer(e, 'onContainerTouchStart', 'touchstart', function (e) {
  8543. touches[e.pointerId] = { pageX: e.pageX, pageY: e.pageY, target: e.currentTarget };
  8544. });
  8545. },
  8546. onContainerPointerMove: function (e) {
  8547. translateMSPointer(e, 'onContainerTouchMove', 'touchmove', function (e) {
  8548. touches[e.pointerId] = { pageX: e.pageX, pageY: e.pageY };
  8549. if (!touches[e.pointerId].target) {
  8550. touches[e.pointerId].target = e.currentTarget;
  8551. }
  8552. });
  8553. },
  8554. onDocumentPointerUp: function (e) {
  8555. translateMSPointer(e, 'onContainerTouchEnd', 'touchend', function (e) {
  8556. delete touches[e.pointerId];
  8557. });
  8558. },
  8559. /**
  8560. * Add or remove the MS Pointer specific events
  8561. */
  8562. batchMSEvents: function (fn) {
  8563. fn(this.chart.container, hasPointerEvent ? 'pointerdown' : 'MSPointerDown', this.onContainerPointerDown);
  8564. fn(this.chart.container, hasPointerEvent ? 'pointermove' : 'MSPointerMove', this.onContainerPointerMove);
  8565. fn(doc, hasPointerEvent ? 'pointerup' : 'MSPointerUp', this.onDocumentPointerUp);
  8566. }
  8567. });
  8568. // Disable default IE actions for pinch and such on chart element
  8569. wrap(Pointer.prototype, 'init', function (proceed, chart, options) {
  8570. proceed.call(this, chart, options);
  8571. if (this.hasZoom || this.followTouchMove) {
  8572. css(chart.container, {
  8573. '-ms-touch-action': NONE,
  8574. 'touch-action': NONE
  8575. });
  8576. }
  8577. });
  8578. // Add IE specific touch events to chart
  8579. wrap(Pointer.prototype, 'setDOMEvents', function (proceed) {
  8580. proceed.apply(this);
  8581. if (this.hasZoom || this.followTouchMove) {
  8582. this.batchMSEvents(addEvent);
  8583. }
  8584. });
  8585. // Destroy MS events also
  8586. wrap(Pointer.prototype, 'destroy', function (proceed) {
  8587. this.batchMSEvents(removeEvent);
  8588. proceed.call(this);
  8589. });
  8590. }
  8591. /**
  8592. * The overview of the chart's series
  8593. */
  8594. var Legend = Highcharts.Legend = function (chart, options) {
  8595. this.init(chart, options);
  8596. };
  8597. Legend.prototype = {
  8598. /**
  8599. * Initialize the legend
  8600. */
  8601. init: function (chart, options) {
  8602. var legend = this,
  8603. itemStyle = options.itemStyle,
  8604. padding = pick(options.padding, 8),
  8605. itemMarginTop = options.itemMarginTop || 0;
  8606. this.options = options;
  8607. if (!options.enabled) {
  8608. return;
  8609. }
  8610. legend.itemStyle = itemStyle;
  8611. legend.itemHiddenStyle = merge(itemStyle, options.itemHiddenStyle);
  8612. legend.itemMarginTop = itemMarginTop;
  8613. legend.padding = padding;
  8614. legend.initialItemX = padding;
  8615. legend.initialItemY = padding - 5; // 5 is the number of pixels above the text
  8616. legend.maxItemWidth = 0;
  8617. legend.chart = chart;
  8618. legend.itemHeight = 0;
  8619. legend.lastLineHeight = 0;
  8620. legend.symbolWidth = pick(options.symbolWidth, 16);
  8621. legend.pages = [];
  8622. // Render it
  8623. legend.render();
  8624. // move checkboxes
  8625. addEvent(legend.chart, 'endResize', function () {
  8626. legend.positionCheckboxes();
  8627. });
  8628. },
  8629. /**
  8630. * Set the colors for the legend item
  8631. * @param {Object} item A Series or Point instance
  8632. * @param {Object} visible Dimmed or colored
  8633. */
  8634. colorizeItem: function (item, visible) {
  8635. var legend = this,
  8636. options = legend.options,
  8637. legendItem = item.legendItem,
  8638. legendLine = item.legendLine,
  8639. legendSymbol = item.legendSymbol,
  8640. hiddenColor = legend.itemHiddenStyle.color,
  8641. textColor = visible ? options.itemStyle.color : hiddenColor,
  8642. symbolColor = visible ? (item.legendColor || item.color || '#CCC') : hiddenColor,
  8643. markerOptions = item.options && item.options.marker,
  8644. symbolAttr = { fill: symbolColor },
  8645. key,
  8646. val;
  8647. if (legendItem) {
  8648. legendItem.css({ fill: textColor, color: textColor }); // color for #1553, oldIE
  8649. }
  8650. if (legendLine) {
  8651. legendLine.attr({ stroke: symbolColor });
  8652. }
  8653. if (legendSymbol) {
  8654. // Apply marker options
  8655. if (markerOptions && legendSymbol.isMarker) { // #585
  8656. symbolAttr.stroke = symbolColor;
  8657. markerOptions = item.convertAttribs(markerOptions);
  8658. for (key in markerOptions) {
  8659. val = markerOptions[key];
  8660. if (val !== UNDEFINED) {
  8661. symbolAttr[key] = val;
  8662. }
  8663. }
  8664. }
  8665. legendSymbol.attr(symbolAttr);
  8666. }
  8667. },
  8668. /**
  8669. * Position the legend item
  8670. * @param {Object} item A Series or Point instance
  8671. */
  8672. positionItem: function (item) {
  8673. var legend = this,
  8674. options = legend.options,
  8675. symbolPadding = options.symbolPadding,
  8676. ltr = !options.rtl,
  8677. legendItemPos = item._legendItemPos,
  8678. itemX = legendItemPos[0],
  8679. itemY = legendItemPos[1],
  8680. checkbox = item.checkbox;
  8681. if (item.legendGroup) {
  8682. item.legendGroup.translate(
  8683. ltr ? itemX : legend.legendWidth - itemX - 2 * symbolPadding - 4,
  8684. itemY
  8685. );
  8686. }
  8687. if (checkbox) {
  8688. checkbox.x = itemX;
  8689. checkbox.y = itemY;
  8690. }
  8691. },
  8692. /**
  8693. * Destroy a single legend item
  8694. * @param {Object} item The series or point
  8695. */
  8696. destroyItem: function (item) {
  8697. var checkbox = item.checkbox;
  8698. // destroy SVG elements
  8699. each(['legendItem', 'legendLine', 'legendSymbol', 'legendGroup'], function (key) {
  8700. if (item[key]) {
  8701. item[key] = item[key].destroy();
  8702. }
  8703. });
  8704. if (checkbox) {
  8705. discardElement(item.checkbox);
  8706. }
  8707. },
  8708. /**
  8709. * Destroys the legend.
  8710. */
  8711. destroy: function () {
  8712. var legend = this,
  8713. legendGroup = legend.group,
  8714. box = legend.box;
  8715. if (box) {
  8716. legend.box = box.destroy();
  8717. }
  8718. if (legendGroup) {
  8719. legend.group = legendGroup.destroy();
  8720. }
  8721. },
  8722. /**
  8723. * Position the checkboxes after the width is determined
  8724. */
  8725. positionCheckboxes: function (scrollOffset) {
  8726. var alignAttr = this.group.alignAttr,
  8727. translateY,
  8728. clipHeight = this.clipHeight || this.legendHeight;
  8729. if (alignAttr) {
  8730. translateY = alignAttr.translateY;
  8731. each(this.allItems, function (item) {
  8732. var checkbox = item.checkbox,
  8733. top;
  8734. if (checkbox) {
  8735. top = (translateY + checkbox.y + (scrollOffset || 0) + 3);
  8736. css(checkbox, {
  8737. left: (alignAttr.translateX + item.checkboxOffset + checkbox.x - 20) + PX,
  8738. top: top + PX,
  8739. display: top > translateY - 6 && top < translateY + clipHeight - 6 ? '' : NONE
  8740. });
  8741. }
  8742. });
  8743. }
  8744. },
  8745. /**
  8746. * Render the legend title on top of the legend
  8747. */
  8748. renderTitle: function () {
  8749. var options = this.options,
  8750. padding = this.padding,
  8751. titleOptions = options.title,
  8752. titleHeight = 0,
  8753. bBox;
  8754. if (titleOptions.text) {
  8755. if (!this.title) {
  8756. this.title = this.chart.renderer.label(titleOptions.text, padding - 3, padding - 4, null, null, null, null, null, 'legend-title')
  8757. .attr({ zIndex: 1 })
  8758. .css(titleOptions.style)
  8759. .add(this.group);
  8760. }
  8761. bBox = this.title.getBBox();
  8762. titleHeight = bBox.height;
  8763. this.offsetWidth = bBox.width; // #1717
  8764. this.contentGroup.attr({ translateY: titleHeight });
  8765. }
  8766. this.titleHeight = titleHeight;
  8767. },
  8768. /**
  8769. * Render a single specific legend item
  8770. * @param {Object} item A series or point
  8771. */
  8772. renderItem: function (item) {
  8773. var legend = this,
  8774. chart = legend.chart,
  8775. renderer = chart.renderer,
  8776. options = legend.options,
  8777. horizontal = options.layout === 'horizontal',
  8778. symbolWidth = legend.symbolWidth,
  8779. symbolPadding = options.symbolPadding,
  8780. itemStyle = legend.itemStyle,
  8781. itemHiddenStyle = legend.itemHiddenStyle,
  8782. padding = legend.padding,
  8783. itemDistance = horizontal ? pick(options.itemDistance, 20) : 0,
  8784. ltr = !options.rtl,
  8785. itemHeight,
  8786. widthOption = options.width,
  8787. itemMarginBottom = options.itemMarginBottom || 0,
  8788. itemMarginTop = legend.itemMarginTop,
  8789. initialItemX = legend.initialItemX,
  8790. bBox,
  8791. itemWidth,
  8792. li = item.legendItem,
  8793. series = item.series && item.series.drawLegendSymbol ? item.series : item,
  8794. seriesOptions = series.options,
  8795. showCheckbox = legend.createCheckboxForItem && seriesOptions && seriesOptions.showCheckbox,
  8796. useHTML = options.useHTML;
  8797. if (!li) { // generate it once, later move it
  8798. // Generate the group box
  8799. // A group to hold the symbol and text. Text is to be appended in Legend class.
  8800. item.legendGroup = renderer.g('legend-item')
  8801. .attr({ zIndex: 1 })
  8802. .add(legend.scrollGroup);
  8803. // Generate the list item text and add it to the group
  8804. item.legendItem = li = renderer.text(
  8805. options.labelFormat ? format(options.labelFormat, item) : options.labelFormatter.call(item),
  8806. ltr ? symbolWidth + symbolPadding : -symbolPadding,
  8807. legend.baseline || 0,
  8808. useHTML
  8809. )
  8810. .css(merge(item.visible ? itemStyle : itemHiddenStyle)) // merge to prevent modifying original (#1021)
  8811. .attr({
  8812. align: ltr ? 'left' : 'right',
  8813. zIndex: 2
  8814. })
  8815. .add(item.legendGroup);
  8816. // Get the baseline for the first item - the font size is equal for all
  8817. if (!legend.baseline) {
  8818. legend.baseline = renderer.fontMetrics(itemStyle.fontSize, li).f + 3 + itemMarginTop;
  8819. li.attr('y', legend.baseline);
  8820. }
  8821. // Draw the legend symbol inside the group box
  8822. series.drawLegendSymbol(legend, item);
  8823. if (legend.setItemEvents) {
  8824. legend.setItemEvents(item, li, useHTML, itemStyle, itemHiddenStyle);
  8825. }
  8826. // Colorize the items
  8827. legend.colorizeItem(item, item.visible);
  8828. // add the HTML checkbox on top
  8829. if (showCheckbox) {
  8830. legend.createCheckboxForItem(item);
  8831. }
  8832. }
  8833. // calculate the positions for the next line
  8834. bBox = li.getBBox();
  8835. itemWidth = item.checkboxOffset =
  8836. options.itemWidth ||
  8837. item.legendItemWidth ||
  8838. symbolWidth + symbolPadding + bBox.width + itemDistance + (showCheckbox ? 20 : 0);
  8839. legend.itemHeight = itemHeight = mathRound(item.legendItemHeight || bBox.height);
  8840. // if the item exceeds the width, start a new line
  8841. if (horizontal && legend.itemX - initialItemX + itemWidth >
  8842. (widthOption || (chart.chartWidth - 2 * padding - initialItemX - options.x))) {
  8843. legend.itemX = initialItemX;
  8844. legend.itemY += itemMarginTop + legend.lastLineHeight + itemMarginBottom;
  8845. legend.lastLineHeight = 0; // reset for next line
  8846. }
  8847. // If the item exceeds the height, start a new column
  8848. /*if (!horizontal && legend.itemY + options.y + itemHeight > chart.chartHeight - spacingTop - spacingBottom) {
  8849. legend.itemY = legend.initialItemY;
  8850. legend.itemX += legend.maxItemWidth;
  8851. legend.maxItemWidth = 0;
  8852. }*/
  8853. // Set the edge positions
  8854. legend.maxItemWidth = mathMax(legend.maxItemWidth, itemWidth);
  8855. legend.lastItemY = itemMarginTop + legend.itemY + itemMarginBottom;
  8856. legend.lastLineHeight = mathMax(itemHeight, legend.lastLineHeight); // #915
  8857. // cache the position of the newly generated or reordered items
  8858. item._legendItemPos = [legend.itemX, legend.itemY];
  8859. // advance
  8860. if (horizontal) {
  8861. legend.itemX += itemWidth;
  8862. } else {
  8863. legend.itemY += itemMarginTop + itemHeight + itemMarginBottom;
  8864. legend.lastLineHeight = itemHeight;
  8865. }
  8866. // the width of the widest item
  8867. legend.offsetWidth = widthOption || mathMax(
  8868. (horizontal ? legend.itemX - initialItemX - itemDistance : itemWidth) + padding,
  8869. legend.offsetWidth
  8870. );
  8871. },
  8872. /**
  8873. * Get all items, which is one item per series for normal series and one item per point
  8874. * for pie series.
  8875. */
  8876. getAllItems: function () {
  8877. var allItems = [];
  8878. each(this.chart.series, function (series) {
  8879. var seriesOptions = series.options;
  8880. // Handle showInLegend. If the series is linked to another series, defaults to false.
  8881. if (!pick(seriesOptions.showInLegend, !defined(seriesOptions.linkedTo) ? UNDEFINED : false, true)) {
  8882. return;
  8883. }
  8884. // use points or series for the legend item depending on legendType
  8885. allItems = allItems.concat(
  8886. series.legendItems ||
  8887. (seriesOptions.legendType === 'point' ?
  8888. series.data :
  8889. series)
  8890. );
  8891. });
  8892. return allItems;
  8893. },
  8894. /**
  8895. * Render the legend. This method can be called both before and after
  8896. * chart.render. If called after, it will only rearrange items instead
  8897. * of creating new ones.
  8898. */
  8899. render: function () {
  8900. var legend = this,
  8901. chart = legend.chart,
  8902. renderer = chart.renderer,
  8903. legendGroup = legend.group,
  8904. allItems,
  8905. display,
  8906. legendWidth,
  8907. legendHeight,
  8908. box = legend.box,
  8909. options = legend.options,
  8910. padding = legend.padding,
  8911. legendBorderWidth = options.borderWidth,
  8912. legendBackgroundColor = options.backgroundColor;
  8913. legend.itemX = legend.initialItemX;
  8914. legend.itemY = legend.initialItemY;
  8915. legend.offsetWidth = 0;
  8916. legend.lastItemY = 0;
  8917. if (!legendGroup) {
  8918. legend.group = legendGroup = renderer.g('legend')
  8919. .attr({ zIndex: 7 })
  8920. .add();
  8921. legend.contentGroup = renderer.g()
  8922. .attr({ zIndex: 1 }) // above background
  8923. .add(legendGroup);
  8924. legend.scrollGroup = renderer.g()
  8925. .add(legend.contentGroup);
  8926. }
  8927. legend.renderTitle();
  8928. // add each series or point
  8929. allItems = legend.getAllItems();
  8930. // sort by legendIndex
  8931. stableSort(allItems, function (a, b) {
  8932. return ((a.options && a.options.legendIndex) || 0) - ((b.options && b.options.legendIndex) || 0);
  8933. });
  8934. // reversed legend
  8935. if (options.reversed) {
  8936. allItems.reverse();
  8937. }
  8938. legend.allItems = allItems;
  8939. legend.display = display = !!allItems.length;
  8940. // render the items
  8941. each(allItems, function (item) {
  8942. legend.renderItem(item);
  8943. });
  8944. // Draw the border
  8945. legendWidth = options.width || legend.offsetWidth;
  8946. legendHeight = legend.lastItemY + legend.lastLineHeight + legend.titleHeight;
  8947. legendHeight = legend.handleOverflow(legendHeight);
  8948. if (legendBorderWidth || legendBackgroundColor) {
  8949. legendWidth += padding;
  8950. legendHeight += padding;
  8951. if (!box) {
  8952. legend.box = box = renderer.rect(
  8953. 0,
  8954. 0,
  8955. legendWidth,
  8956. legendHeight,
  8957. options.borderRadius,
  8958. legendBorderWidth || 0
  8959. ).attr({
  8960. stroke: options.borderColor,
  8961. 'stroke-width': legendBorderWidth || 0,
  8962. fill: legendBackgroundColor || NONE
  8963. })
  8964. .add(legendGroup)
  8965. .shadow(options.shadow);
  8966. box.isNew = true;
  8967. } else if (legendWidth > 0 && legendHeight > 0) {
  8968. box[box.isNew ? 'attr' : 'animate'](
  8969. box.crisp({ width: legendWidth, height: legendHeight })
  8970. );
  8971. box.isNew = false;
  8972. }
  8973. // hide the border if no items
  8974. box[display ? 'show' : 'hide']();
  8975. }
  8976. legend.legendWidth = legendWidth;
  8977. legend.legendHeight = legendHeight;
  8978. // Now that the legend width and height are established, put the items in the
  8979. // final position
  8980. each(allItems, function (item) {
  8981. legend.positionItem(item);
  8982. });
  8983. // 1.x compatibility: positioning based on style
  8984. /*var props = ['left', 'right', 'top', 'bottom'],
  8985. prop,
  8986. i = 4;
  8987. while (i--) {
  8988. prop = props[i];
  8989. if (options.style[prop] && options.style[prop] !== 'auto') {
  8990. options[i < 2 ? 'align' : 'verticalAlign'] = prop;
  8991. options[i < 2 ? 'x' : 'y'] = pInt(options.style[prop]) * (i % 2 ? -1 : 1);
  8992. }
  8993. }*/
  8994. if (display) {
  8995. legendGroup.align(extend({
  8996. width: legendWidth,
  8997. height: legendHeight
  8998. }, options), true, 'spacingBox');
  8999. }
  9000. if (!chart.isResizing) {
  9001. this.positionCheckboxes();
  9002. }
  9003. },
  9004. /**
  9005. * Set up the overflow handling by adding navigation with up and down arrows below the
  9006. * legend.
  9007. */
  9008. handleOverflow: function (legendHeight) {
  9009. var legend = this,
  9010. chart = this.chart,
  9011. renderer = chart.renderer,
  9012. options = this.options,
  9013. optionsY = options.y,
  9014. alignTop = options.verticalAlign === 'top',
  9015. spaceHeight = chart.spacingBox.height + (alignTop ? -optionsY : optionsY) - this.padding,
  9016. maxHeight = options.maxHeight,
  9017. clipHeight,
  9018. clipRect = this.clipRect,
  9019. navOptions = options.navigation,
  9020. animation = pick(navOptions.animation, true),
  9021. arrowSize = navOptions.arrowSize || 12,
  9022. nav = this.nav,
  9023. pages = this.pages,
  9024. lastY,
  9025. allItems = this.allItems;
  9026. // Adjust the height
  9027. if (options.layout === 'horizontal') {
  9028. spaceHeight /= 2;
  9029. }
  9030. if (maxHeight) {
  9031. spaceHeight = mathMin(spaceHeight, maxHeight);
  9032. }
  9033. // Reset the legend height and adjust the clipping rectangle
  9034. pages.length = 0;
  9035. if (legendHeight > spaceHeight && !options.useHTML) {
  9036. this.clipHeight = clipHeight = mathMax(spaceHeight - 20 - this.titleHeight - this.padding, 0);
  9037. this.currentPage = pick(this.currentPage, 1);
  9038. this.fullHeight = legendHeight;
  9039. // Fill pages with Y positions so that the top of each a legend item defines
  9040. // the scroll top for each page (#2098)
  9041. each(allItems, function (item, i) {
  9042. var y = item._legendItemPos[1],
  9043. h = mathRound(item.legendItem.getBBox().height),
  9044. len = pages.length;
  9045. if (!len || (y - pages[len - 1] > clipHeight && (lastY || y) !== pages[len - 1])) {
  9046. pages.push(lastY || y);
  9047. len++;
  9048. }
  9049. if (i === allItems.length - 1 && y + h - pages[len - 1] > clipHeight) {
  9050. pages.push(y);
  9051. }
  9052. if (y !== lastY) {
  9053. lastY = y;
  9054. }
  9055. });
  9056. // Only apply clipping if needed. Clipping causes blurred legend in PDF export (#1787)
  9057. if (!clipRect) {
  9058. clipRect = legend.clipRect = renderer.clipRect(0, this.padding, 9999, 0);
  9059. legend.contentGroup.clip(clipRect);
  9060. }
  9061. clipRect.attr({
  9062. height: clipHeight
  9063. });
  9064. // Add navigation elements
  9065. if (!nav) {
  9066. this.nav = nav = renderer.g().attr({ zIndex: 1 }).add(this.group);
  9067. this.up = renderer.symbol('triangle', 0, 0, arrowSize, arrowSize)
  9068. .on('click', function () {
  9069. legend.scroll(-1, animation);
  9070. })
  9071. .add(nav);
  9072. this.pager = renderer.text('', 15, 10)
  9073. .css(navOptions.style)
  9074. .add(nav);
  9075. this.down = renderer.symbol('triangle-down', 0, 0, arrowSize, arrowSize)
  9076. .on('click', function () {
  9077. legend.scroll(1, animation);
  9078. })
  9079. .add(nav);
  9080. }
  9081. // Set initial position
  9082. legend.scroll(0);
  9083. legendHeight = spaceHeight;
  9084. } else if (nav) {
  9085. clipRect.attr({
  9086. height: chart.chartHeight
  9087. });
  9088. nav.hide();
  9089. this.scrollGroup.attr({
  9090. translateY: 1
  9091. });
  9092. this.clipHeight = 0; // #1379
  9093. }
  9094. return legendHeight;
  9095. },
  9096. /**
  9097. * Scroll the legend by a number of pages
  9098. * @param {Object} scrollBy
  9099. * @param {Object} animation
  9100. */
  9101. scroll: function (scrollBy, animation) {
  9102. var pages = this.pages,
  9103. pageCount = pages.length,
  9104. currentPage = this.currentPage + scrollBy,
  9105. clipHeight = this.clipHeight,
  9106. navOptions = this.options.navigation,
  9107. activeColor = navOptions.activeColor,
  9108. inactiveColor = navOptions.inactiveColor,
  9109. pager = this.pager,
  9110. padding = this.padding,
  9111. scrollOffset;
  9112. // When resizing while looking at the last page
  9113. if (currentPage > pageCount) {
  9114. currentPage = pageCount;
  9115. }
  9116. if (currentPage > 0) {
  9117. if (animation !== UNDEFINED) {
  9118. setAnimation(animation, this.chart);
  9119. }
  9120. this.nav.attr({
  9121. translateX: padding,
  9122. translateY: clipHeight + this.padding + 7 + this.titleHeight,
  9123. visibility: VISIBLE
  9124. });
  9125. this.up.attr({
  9126. fill: currentPage === 1 ? inactiveColor : activeColor
  9127. })
  9128. .css({
  9129. cursor: currentPage === 1 ? 'default' : 'pointer'
  9130. });
  9131. pager.attr({
  9132. text: currentPage + '/' + pageCount
  9133. });
  9134. this.down.attr({
  9135. x: 18 + this.pager.getBBox().width, // adjust to text width
  9136. fill: currentPage === pageCount ? inactiveColor : activeColor
  9137. })
  9138. .css({
  9139. cursor: currentPage === pageCount ? 'default' : 'pointer'
  9140. });
  9141. scrollOffset = -pages[currentPage - 1] + this.initialItemY;
  9142. this.scrollGroup.animate({
  9143. translateY: scrollOffset
  9144. });
  9145. this.currentPage = currentPage;
  9146. this.positionCheckboxes(scrollOffset);
  9147. }
  9148. }
  9149. };
  9150. /*
  9151. * LegendSymbolMixin
  9152. */
  9153. var LegendSymbolMixin = Highcharts.LegendSymbolMixin = {
  9154. /**
  9155. * Get the series' symbol in the legend
  9156. *
  9157. * @param {Object} legend The legend object
  9158. * @param {Object} item The series (this) or point
  9159. */
  9160. drawRectangle: function (legend, item) {
  9161. var symbolHeight = legend.options.symbolHeight || 12;
  9162. item.legendSymbol = this.chart.renderer.rect(
  9163. 0,
  9164. legend.baseline - 5 - (symbolHeight / 2),
  9165. legend.symbolWidth,
  9166. symbolHeight,
  9167. legend.options.symbolRadius || 0
  9168. ).attr({
  9169. zIndex: 3
  9170. }).add(item.legendGroup);
  9171. },
  9172. /**
  9173. * Get the series' symbol in the legend. This method should be overridable to create custom
  9174. * symbols through Highcharts.seriesTypes[type].prototype.drawLegendSymbols.
  9175. *
  9176. * @param {Object} legend The legend object
  9177. */
  9178. drawLineMarker: function (legend) {
  9179. var options = this.options,
  9180. markerOptions = options.marker,
  9181. radius,
  9182. legendOptions = legend.options,
  9183. legendSymbol,
  9184. symbolWidth = legend.symbolWidth,
  9185. renderer = this.chart.renderer,
  9186. legendItemGroup = this.legendGroup,
  9187. verticalCenter = legend.baseline - mathRound(renderer.fontMetrics(legendOptions.itemStyle.fontSize, this.legendItem).b * 0.3),
  9188. attr;
  9189. // Draw the line
  9190. if (options.lineWidth) {
  9191. attr = {
  9192. 'stroke-width': options.lineWidth
  9193. };
  9194. if (options.dashStyle) {
  9195. attr.dashstyle = options.dashStyle;
  9196. }
  9197. this.legendLine = renderer.path([
  9198. M,
  9199. 0,
  9200. verticalCenter,
  9201. L,
  9202. symbolWidth,
  9203. verticalCenter
  9204. ])
  9205. .attr(attr)
  9206. .add(legendItemGroup);
  9207. }
  9208. // Draw the marker
  9209. if (markerOptions && markerOptions.enabled !== false) {
  9210. radius = markerOptions.radius;
  9211. this.legendSymbol = legendSymbol = renderer.symbol(
  9212. this.symbol,
  9213. (symbolWidth / 2) - radius,
  9214. verticalCenter - radius,
  9215. 2 * radius,
  9216. 2 * radius
  9217. )
  9218. .add(legendItemGroup);
  9219. legendSymbol.isMarker = true;
  9220. }
  9221. }
  9222. };
  9223. // Workaround for #2030, horizontal legend items not displaying in IE11 Preview,
  9224. // and for #2580, a similar drawing flaw in Firefox 26.
  9225. // TODO: Explore if there's a general cause for this. The problem may be related
  9226. // to nested group elements, as the legend item texts are within 4 group elements.
  9227. if (/Trident\/7\.0/.test(userAgent) || isFirefox) {
  9228. wrap(Legend.prototype, 'positionItem', function (proceed, item) {
  9229. var legend = this,
  9230. runPositionItem = function () { // If chart destroyed in sync, this is undefined (#2030)
  9231. if (item._legendItemPos) {
  9232. proceed.call(legend, item);
  9233. }
  9234. };
  9235. // Do it now, for export and to get checkbox placement
  9236. runPositionItem();
  9237. // Do it after to work around the core issue
  9238. setTimeout(runPositionItem);
  9239. });
  9240. }
  9241. /**
  9242. * The chart class
  9243. * @param {Object} options
  9244. * @param {Function} callback Function to run when the chart has loaded
  9245. */
  9246. function Chart() {
  9247. this.init.apply(this, arguments);
  9248. }
  9249. Chart.prototype = {
  9250. /**
  9251. * Initialize the chart
  9252. */
  9253. init: function (userOptions, callback) {
  9254. // Handle regular options
  9255. var options,
  9256. seriesOptions = userOptions.series; // skip merging data points to increase performance
  9257. userOptions.series = null;
  9258. options = merge(defaultOptions, userOptions); // do the merge
  9259. options.series = userOptions.series = seriesOptions; // set back the series data
  9260. this.userOptions = userOptions;
  9261. var optionsChart = options.chart;
  9262. // Create margin & spacing array
  9263. this.margin = this.splashArray('margin', optionsChart);
  9264. this.spacing = this.splashArray('spacing', optionsChart);
  9265. var chartEvents = optionsChart.events;
  9266. //this.runChartClick = chartEvents && !!chartEvents.click;
  9267. this.bounds = { h: {}, v: {} }; // Pixel data bounds for touch zoom
  9268. this.callback = callback;
  9269. this.isResizing = 0;
  9270. this.options = options;
  9271. //chartTitleOptions = UNDEFINED;
  9272. //chartSubtitleOptions = UNDEFINED;
  9273. this.axes = [];
  9274. this.series = [];
  9275. this.hasCartesianSeries = optionsChart.showAxes;
  9276. //this.axisOffset = UNDEFINED;
  9277. //this.maxTicks = UNDEFINED; // handle the greatest amount of ticks on grouped axes
  9278. //this.inverted = UNDEFINED;
  9279. //this.loadingShown = UNDEFINED;
  9280. //this.container = UNDEFINED;
  9281. //this.chartWidth = UNDEFINED;
  9282. //this.chartHeight = UNDEFINED;
  9283. //this.marginRight = UNDEFINED;
  9284. //this.marginBottom = UNDEFINED;
  9285. //this.containerWidth = UNDEFINED;
  9286. //this.containerHeight = UNDEFINED;
  9287. //this.oldChartWidth = UNDEFINED;
  9288. //this.oldChartHeight = UNDEFINED;
  9289. //this.renderTo = UNDEFINED;
  9290. //this.renderToClone = UNDEFINED;
  9291. //this.spacingBox = UNDEFINED
  9292. //this.legend = UNDEFINED;
  9293. // Elements
  9294. //this.chartBackground = UNDEFINED;
  9295. //this.plotBackground = UNDEFINED;
  9296. //this.plotBGImage = UNDEFINED;
  9297. //this.plotBorder = UNDEFINED;
  9298. //this.loadingDiv = UNDEFINED;
  9299. //this.loadingSpan = UNDEFINED;
  9300. var chart = this,
  9301. eventType;
  9302. // Add the chart to the global lookup
  9303. chart.index = charts.length;
  9304. charts.push(chart);
  9305. chartCount++;
  9306. // Set up auto resize
  9307. if (optionsChart.reflow !== false) {
  9308. addEvent(chart, 'load', function () {
  9309. chart.initReflow();
  9310. });
  9311. }
  9312. // Chart event handlers
  9313. if (chartEvents) {
  9314. for (eventType in chartEvents) {
  9315. addEvent(chart, eventType, chartEvents[eventType]);
  9316. }
  9317. }
  9318. chart.xAxis = [];
  9319. chart.yAxis = [];
  9320. // Expose methods and variables
  9321. chart.animation = useCanVG ? false : pick(optionsChart.animation, true);
  9322. chart.pointCount = chart.colorCounter = chart.symbolCounter = 0;
  9323. chart.firstRender();
  9324. },
  9325. /**
  9326. * Initialize an individual series, called internally before render time
  9327. */
  9328. initSeries: function (options) {
  9329. var chart = this,
  9330. optionsChart = chart.options.chart,
  9331. type = options.type || optionsChart.type || optionsChart.defaultSeriesType,
  9332. series,
  9333. constr = seriesTypes[type];
  9334. // No such series type
  9335. if (!constr) {
  9336. error(17, true);
  9337. }
  9338. series = new constr();
  9339. series.init(this, options);
  9340. return series;
  9341. },
  9342. /**
  9343. * Check whether a given point is within the plot area
  9344. *
  9345. * @param {Number} plotX Pixel x relative to the plot area
  9346. * @param {Number} plotY Pixel y relative to the plot area
  9347. * @param {Boolean} inverted Whether the chart is inverted
  9348. */
  9349. isInsidePlot: function (plotX, plotY, inverted) {
  9350. var x = inverted ? plotY : plotX,
  9351. y = inverted ? plotX : plotY;
  9352. return x >= 0 &&
  9353. x <= this.plotWidth &&
  9354. y >= 0 &&
  9355. y <= this.plotHeight;
  9356. },
  9357. /**
  9358. * Adjust all axes tick amounts
  9359. */
  9360. adjustTickAmounts: function () {
  9361. if (this.options.chart.alignTicks !== false) {
  9362. each(this.axes, function (axis) {
  9363. axis.adjustTickAmount();
  9364. });
  9365. }
  9366. this.maxTicks = null;
  9367. },
  9368. /**
  9369. * Redraw legend, axes or series based on updated data
  9370. *
  9371. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  9372. * configuration
  9373. */
  9374. redraw: function (animation) {
  9375. var chart = this,
  9376. axes = chart.axes,
  9377. series = chart.series,
  9378. pointer = chart.pointer,
  9379. legend = chart.legend,
  9380. redrawLegend = chart.isDirtyLegend,
  9381. hasStackedSeries,
  9382. hasDirtyStacks,
  9383. hasCartesianSeries = chart.hasCartesianSeries,
  9384. isDirtyBox = chart.isDirtyBox, // todo: check if it has actually changed?
  9385. seriesLength = series.length,
  9386. i = seriesLength,
  9387. serie,
  9388. renderer = chart.renderer,
  9389. isHiddenChart = renderer.isHidden(),
  9390. afterRedraw = [];
  9391. setAnimation(animation, chart);
  9392. if (isHiddenChart) {
  9393. chart.cloneRenderTo();
  9394. }
  9395. // Adjust title layout (reflow multiline text)
  9396. chart.layOutTitles();
  9397. // link stacked series
  9398. while (i--) {
  9399. serie = series[i];
  9400. if (serie.options.stacking) {
  9401. hasStackedSeries = true;
  9402. if (serie.isDirty) {
  9403. hasDirtyStacks = true;
  9404. break;
  9405. }
  9406. }
  9407. }
  9408. if (hasDirtyStacks) { // mark others as dirty
  9409. i = seriesLength;
  9410. while (i--) {
  9411. serie = series[i];
  9412. if (serie.options.stacking) {
  9413. serie.isDirty = true;
  9414. }
  9415. }
  9416. }
  9417. // handle updated data in the series
  9418. each(series, function (serie) {
  9419. if (serie.isDirty) { // prepare the data so axis can read it
  9420. if (serie.options.legendType === 'point') {
  9421. redrawLegend = true;
  9422. }
  9423. }
  9424. });
  9425. // handle added or removed series
  9426. if (redrawLegend && legend.options.enabled) { // series or pie points are added or removed
  9427. // draw legend graphics
  9428. legend.render();
  9429. chart.isDirtyLegend = false;
  9430. }
  9431. // reset stacks
  9432. if (hasStackedSeries) {
  9433. chart.getStacks();
  9434. }
  9435. if (hasCartesianSeries) {
  9436. if (!chart.isResizing) {
  9437. // reset maxTicks
  9438. chart.maxTicks = null;
  9439. // set axes scales
  9440. each(axes, function (axis) {
  9441. axis.setScale();
  9442. });
  9443. }
  9444. chart.adjustTickAmounts();
  9445. }
  9446. chart.getMargins(); // #3098
  9447. if (hasCartesianSeries) {
  9448. // If one axis is dirty, all axes must be redrawn (#792, #2169)
  9449. each(axes, function (axis) {
  9450. if (axis.isDirty) {
  9451. isDirtyBox = true;
  9452. }
  9453. });
  9454. // redraw axes
  9455. each(axes, function (axis) {
  9456. // Fire 'afterSetExtremes' only if extremes are set
  9457. if (axis.isDirtyExtremes) { // #821
  9458. axis.isDirtyExtremes = false;
  9459. afterRedraw.push(function () { // prevent a recursive call to chart.redraw() (#1119)
  9460. fireEvent(axis, 'afterSetExtremes', extend(axis.eventArgs, axis.getExtremes())); // #747, #751
  9461. delete axis.eventArgs;
  9462. });
  9463. }
  9464. if (isDirtyBox || hasStackedSeries) {
  9465. axis.redraw();
  9466. }
  9467. });
  9468. }
  9469. // the plot areas size has changed
  9470. if (isDirtyBox) {
  9471. chart.drawChartBox();
  9472. }
  9473. // redraw affected series
  9474. each(series, function (serie) {
  9475. if (serie.isDirty && serie.visible &&
  9476. (!serie.isCartesian || serie.xAxis)) { // issue #153
  9477. serie.redraw();
  9478. }
  9479. });
  9480. // move tooltip or reset
  9481. if (pointer) {
  9482. pointer.reset(true);
  9483. }
  9484. // redraw if canvas
  9485. renderer.draw();
  9486. // fire the event
  9487. fireEvent(chart, 'redraw'); // jQuery breaks this when calling it from addEvent. Overwrites chart.redraw
  9488. if (isHiddenChart) {
  9489. chart.cloneRenderTo(true);
  9490. }
  9491. // Fire callbacks that are put on hold until after the redraw
  9492. each(afterRedraw, function (callback) {
  9493. callback.call();
  9494. });
  9495. },
  9496. /**
  9497. * Get an axis, series or point object by id.
  9498. * @param id {String} The id as given in the configuration options
  9499. */
  9500. get: function (id) {
  9501. var chart = this,
  9502. axes = chart.axes,
  9503. series = chart.series;
  9504. var i,
  9505. j,
  9506. points;
  9507. // search axes
  9508. for (i = 0; i < axes.length; i++) {
  9509. if (axes[i].options.id === id) {
  9510. return axes[i];
  9511. }
  9512. }
  9513. // search series
  9514. for (i = 0; i < series.length; i++) {
  9515. if (series[i].options.id === id) {
  9516. return series[i];
  9517. }
  9518. }
  9519. // search points
  9520. for (i = 0; i < series.length; i++) {
  9521. points = series[i].points || [];
  9522. for (j = 0; j < points.length; j++) {
  9523. if (points[j].id === id) {
  9524. return points[j];
  9525. }
  9526. }
  9527. }
  9528. return null;
  9529. },
  9530. /**
  9531. * Create the Axis instances based on the config options
  9532. */
  9533. getAxes: function () {
  9534. var chart = this,
  9535. options = this.options,
  9536. xAxisOptions = options.xAxis = splat(options.xAxis || {}),
  9537. yAxisOptions = options.yAxis = splat(options.yAxis || {}),
  9538. optionsArray,
  9539. axis;
  9540. // make sure the options are arrays and add some members
  9541. each(xAxisOptions, function (axis, i) {
  9542. axis.index = i;
  9543. axis.isX = true;
  9544. });
  9545. each(yAxisOptions, function (axis, i) {
  9546. axis.index = i;
  9547. });
  9548. // concatenate all axis options into one array
  9549. optionsArray = xAxisOptions.concat(yAxisOptions);
  9550. each(optionsArray, function (axisOptions) {
  9551. axis = new Axis(chart, axisOptions);
  9552. });
  9553. chart.adjustTickAmounts();
  9554. },
  9555. /**
  9556. * Get the currently selected points from all series
  9557. */
  9558. getSelectedPoints: function () {
  9559. var points = [];
  9560. each(this.series, function (serie) {
  9561. points = points.concat(grep(serie.points || [], function (point) {
  9562. return point.selected;
  9563. }));
  9564. });
  9565. return points;
  9566. },
  9567. /**
  9568. * Get the currently selected series
  9569. */
  9570. getSelectedSeries: function () {
  9571. return grep(this.series, function (serie) {
  9572. return serie.selected;
  9573. });
  9574. },
  9575. /**
  9576. * Generate stacks for each series and calculate stacks total values
  9577. */
  9578. getStacks: function () {
  9579. var chart = this;
  9580. // reset stacks for each yAxis
  9581. each(chart.yAxis, function (axis) {
  9582. if (axis.stacks && axis.hasVisibleSeries) {
  9583. axis.oldStacks = axis.stacks;
  9584. }
  9585. });
  9586. each(chart.series, function (series) {
  9587. if (series.options.stacking && (series.visible === true || chart.options.chart.ignoreHiddenSeries === false)) {
  9588. series.stackKey = series.type + pick(series.options.stack, '');
  9589. }
  9590. });
  9591. },
  9592. /**
  9593. * Show the title and subtitle of the chart
  9594. *
  9595. * @param titleOptions {Object} New title options
  9596. * @param subtitleOptions {Object} New subtitle options
  9597. *
  9598. */
  9599. setTitle: function (titleOptions, subtitleOptions, redraw) {
  9600. var chart = this,
  9601. options = chart.options,
  9602. chartTitleOptions,
  9603. chartSubtitleOptions;
  9604. chartTitleOptions = options.title = merge(options.title, titleOptions);
  9605. chartSubtitleOptions = options.subtitle = merge(options.subtitle, subtitleOptions);
  9606. // add title and subtitle
  9607. each([
  9608. ['title', titleOptions, chartTitleOptions],
  9609. ['subtitle', subtitleOptions, chartSubtitleOptions]
  9610. ], function (arr) {
  9611. var name = arr[0],
  9612. title = chart[name],
  9613. titleOptions = arr[1],
  9614. chartTitleOptions = arr[2];
  9615. if (title && titleOptions) {
  9616. chart[name] = title = title.destroy(); // remove old
  9617. }
  9618. if (chartTitleOptions && chartTitleOptions.text && !title) {
  9619. chart[name] = chart.renderer.text(
  9620. chartTitleOptions.text,
  9621. 0,
  9622. 0,
  9623. chartTitleOptions.useHTML
  9624. )
  9625. .attr({
  9626. align: chartTitleOptions.align,
  9627. 'class': PREFIX + name,
  9628. zIndex: chartTitleOptions.zIndex || 4
  9629. })
  9630. .css(chartTitleOptions.style)
  9631. .add();
  9632. }
  9633. });
  9634. chart.layOutTitles(redraw);
  9635. },
  9636. /**
  9637. * Lay out the chart titles and cache the full offset height for use in getMargins
  9638. */
  9639. layOutTitles: function (redraw) {
  9640. var titleOffset = 0,
  9641. title = this.title,
  9642. subtitle = this.subtitle,
  9643. options = this.options,
  9644. titleOptions = options.title,
  9645. subtitleOptions = options.subtitle,
  9646. requiresDirtyBox,
  9647. renderer = this.renderer,
  9648. autoWidth = this.spacingBox.width - 44; // 44 makes room for default context button
  9649. if (title) {
  9650. title
  9651. .css({ width: (titleOptions.width || autoWidth) + PX })
  9652. .align(extend({
  9653. y: renderer.fontMetrics(titleOptions.style.fontSize, title).b - 3
  9654. }, titleOptions), false, 'spacingBox');
  9655. if (!titleOptions.floating && !titleOptions.verticalAlign) {
  9656. titleOffset = title.getBBox().height;
  9657. }
  9658. }
  9659. if (subtitle) {
  9660. subtitle
  9661. .css({ width: (subtitleOptions.width || autoWidth) + PX })
  9662. .align(extend({
  9663. y: titleOffset + (titleOptions.margin - 13) + renderer.fontMetrics(titleOptions.style.fontSize, subtitle).b
  9664. }, subtitleOptions), false, 'spacingBox');
  9665. if (!subtitleOptions.floating && !subtitleOptions.verticalAlign) {
  9666. titleOffset = mathCeil(titleOffset + subtitle.getBBox().height);
  9667. }
  9668. }
  9669. requiresDirtyBox = this.titleOffset !== titleOffset;
  9670. this.titleOffset = titleOffset; // used in getMargins
  9671. if (!this.isDirtyBox && requiresDirtyBox) {
  9672. this.isDirtyBox = requiresDirtyBox;
  9673. // Redraw if necessary (#2719, #2744)
  9674. if (this.hasRendered && pick(redraw, true) && this.isDirtyBox) {
  9675. this.redraw();
  9676. }
  9677. }
  9678. },
  9679. /**
  9680. * Get chart width and height according to options and container size
  9681. */
  9682. getChartSize: function () {
  9683. var chart = this,
  9684. optionsChart = chart.options.chart,
  9685. widthOption = optionsChart.width,
  9686. heightOption = optionsChart.height,
  9687. renderTo = chart.renderToClone || chart.renderTo;
  9688. // get inner width and height from jQuery (#824)
  9689. if (!defined(widthOption)) {
  9690. chart.containerWidth = adapterRun(renderTo, 'width');
  9691. }
  9692. if (!defined(heightOption)) {
  9693. chart.containerHeight = adapterRun(renderTo, 'height');
  9694. }
  9695. chart.chartWidth = mathMax(0, widthOption || chart.containerWidth || 600); // #1393, 1460
  9696. chart.chartHeight = mathMax(0, pick(heightOption,
  9697. // the offsetHeight of an empty container is 0 in standard browsers, but 19 in IE7:
  9698. chart.containerHeight > 19 ? chart.containerHeight : 400));
  9699. },
  9700. /**
  9701. * Create a clone of the chart's renderTo div and place it outside the viewport to allow
  9702. * size computation on chart.render and chart.redraw
  9703. */
  9704. cloneRenderTo: function (revert) {
  9705. var clone = this.renderToClone,
  9706. container = this.container;
  9707. // Destroy the clone and bring the container back to the real renderTo div
  9708. if (revert) {
  9709. if (clone) {
  9710. this.renderTo.appendChild(container);
  9711. discardElement(clone);
  9712. delete this.renderToClone;
  9713. }
  9714. // Set up the clone
  9715. } else {
  9716. if (container && container.parentNode === this.renderTo) {
  9717. this.renderTo.removeChild(container); // do not clone this
  9718. }
  9719. this.renderToClone = clone = this.renderTo.cloneNode(0);
  9720. css(clone, {
  9721. position: ABSOLUTE,
  9722. top: '-9999px',
  9723. display: 'block' // #833
  9724. });
  9725. if (clone.style.setProperty) { // #2631
  9726. clone.style.setProperty('display', 'block', 'important');
  9727. }
  9728. doc.body.appendChild(clone);
  9729. if (container) {
  9730. clone.appendChild(container);
  9731. }
  9732. }
  9733. },
  9734. /**
  9735. * Get the containing element, determine the size and create the inner container
  9736. * div to hold the chart
  9737. */
  9738. getContainer: function () {
  9739. var chart = this,
  9740. container,
  9741. optionsChart = chart.options.chart,
  9742. chartWidth,
  9743. chartHeight,
  9744. renderTo,
  9745. indexAttrName = 'data-highcharts-chart',
  9746. oldChartIndex,
  9747. containerId;
  9748. chart.renderTo = renderTo = optionsChart.renderTo;
  9749. containerId = PREFIX + idCounter++;
  9750. if (isString(renderTo)) {
  9751. chart.renderTo = renderTo = doc.getElementById(renderTo);
  9752. }
  9753. // Display an error if the renderTo is wrong
  9754. if (!renderTo) {
  9755. error(13, true);
  9756. }
  9757. // If the container already holds a chart, destroy it. The check for hasRendered is there
  9758. // because web pages that are saved to disk from the browser, will preserve the data-highcharts-chart
  9759. // attribute and the SVG contents, but not an interactive chart. So in this case,
  9760. // charts[oldChartIndex] will point to the wrong chart if any (#2609).
  9761. oldChartIndex = pInt(attr(renderTo, indexAttrName));
  9762. if (!isNaN(oldChartIndex) && charts[oldChartIndex] && charts[oldChartIndex].hasRendered) {
  9763. charts[oldChartIndex].destroy();
  9764. }
  9765. // Make a reference to the chart from the div
  9766. attr(renderTo, indexAttrName, chart.index);
  9767. // remove previous chart
  9768. renderTo.innerHTML = '';
  9769. // If the container doesn't have an offsetWidth, it has or is a child of a node
  9770. // that has display:none. We need to temporarily move it out to a visible
  9771. // state to determine the size, else the legend and tooltips won't render
  9772. // properly. The allowClone option is used in sparklines as a micro optimization,
  9773. // saving about 1-2 ms each chart.
  9774. if (!optionsChart.skipClone && !renderTo.offsetWidth) {
  9775. chart.cloneRenderTo();
  9776. }
  9777. // get the width and height
  9778. chart.getChartSize();
  9779. chartWidth = chart.chartWidth;
  9780. chartHeight = chart.chartHeight;
  9781. // create the inner container
  9782. chart.container = container = createElement(DIV, {
  9783. className: PREFIX + 'container' +
  9784. (optionsChart.className ? ' ' + optionsChart.className : ''),
  9785. id: containerId
  9786. }, extend({
  9787. position: RELATIVE,
  9788. overflow: HIDDEN, // needed for context menu (avoid scrollbars) and
  9789. // content overflow in IE
  9790. width: chartWidth + PX,
  9791. height: chartHeight + PX,
  9792. textAlign: 'left',
  9793. lineHeight: 'normal', // #427
  9794. zIndex: 0, // #1072
  9795. '-webkit-tap-highlight-color': 'rgba(0,0,0,0)'
  9796. }, optionsChart.style),
  9797. chart.renderToClone || renderTo
  9798. );
  9799. // cache the cursor (#1650)
  9800. chart._cursor = container.style.cursor;
  9801. // Initialize the renderer
  9802. chart.renderer =
  9803. optionsChart.forExport ? // force SVG, used for SVG export
  9804. new SVGRenderer(container, chartWidth, chartHeight, optionsChart.style, true) :
  9805. new Renderer(container, chartWidth, chartHeight, optionsChart.style);
  9806. if (useCanVG) {
  9807. // If we need canvg library, extend and configure the renderer
  9808. // to get the tracker for translating mouse events
  9809. chart.renderer.create(chart, container, chartWidth, chartHeight);
  9810. }
  9811. },
  9812. /**
  9813. * Calculate margins by rendering axis labels in a preliminary position. Title,
  9814. * subtitle and legend have already been rendered at this stage, but will be
  9815. * moved into their final positions
  9816. */
  9817. getMargins: function () {
  9818. var chart = this,
  9819. spacing = chart.spacing,
  9820. axisOffset,
  9821. legend = chart.legend,
  9822. margin = chart.margin,
  9823. legendOptions = chart.options.legend,
  9824. legendMargin = pick(legendOptions.margin, 20),
  9825. legendX = legendOptions.x,
  9826. legendY = legendOptions.y,
  9827. align = legendOptions.align,
  9828. verticalAlign = legendOptions.verticalAlign,
  9829. titleOffset = chart.titleOffset;
  9830. chart.resetMargins();
  9831. axisOffset = chart.axisOffset;
  9832. // Adjust for title and subtitle
  9833. if (titleOffset && !defined(margin[0])) {
  9834. chart.plotTop = mathMax(chart.plotTop, titleOffset + chart.options.title.margin + spacing[0]);
  9835. }
  9836. // Adjust for legend
  9837. if (legend.display && !legendOptions.floating) {
  9838. if (align === 'right') { // horizontal alignment handled first
  9839. if (!defined(margin[1])) {
  9840. chart.marginRight = mathMax(
  9841. chart.marginRight,
  9842. legend.legendWidth - legendX + legendMargin + spacing[1]
  9843. );
  9844. }
  9845. } else if (align === 'left') {
  9846. if (!defined(margin[3])) {
  9847. chart.plotLeft = mathMax(
  9848. chart.plotLeft,
  9849. legend.legendWidth + legendX + legendMargin + spacing[3]
  9850. );
  9851. }
  9852. } else if (verticalAlign === 'top') {
  9853. if (!defined(margin[0])) {
  9854. chart.plotTop = mathMax(
  9855. chart.plotTop,
  9856. legend.legendHeight + legendY + legendMargin + spacing[0]
  9857. );
  9858. }
  9859. } else if (verticalAlign === 'bottom') {
  9860. if (!defined(margin[2])) {
  9861. chart.marginBottom = mathMax(
  9862. chart.marginBottom,
  9863. legend.legendHeight - legendY + legendMargin + spacing[2]
  9864. );
  9865. }
  9866. }
  9867. }
  9868. // adjust for scroller
  9869. if (chart.extraBottomMargin) {
  9870. chart.marginBottom += chart.extraBottomMargin;
  9871. }
  9872. if (chart.extraTopMargin) {
  9873. chart.plotTop += chart.extraTopMargin;
  9874. }
  9875. // pre-render axes to get labels offset width
  9876. if (chart.hasCartesianSeries) {
  9877. each(chart.axes, function (axis) {
  9878. axis.getOffset();
  9879. });
  9880. }
  9881. if (!defined(margin[3])) {
  9882. chart.plotLeft += axisOffset[3];
  9883. }
  9884. if (!defined(margin[0])) {
  9885. chart.plotTop += axisOffset[0];
  9886. }
  9887. if (!defined(margin[2])) {
  9888. chart.marginBottom += axisOffset[2];
  9889. }
  9890. if (!defined(margin[1])) {
  9891. chart.marginRight += axisOffset[1];
  9892. }
  9893. chart.setChartSize();
  9894. },
  9895. /**
  9896. * Resize the chart to its container if size is not explicitly set
  9897. */
  9898. reflow: function (e) {
  9899. var chart = this,
  9900. optionsChart = chart.options.chart,
  9901. renderTo = chart.renderTo,
  9902. width = optionsChart.width || adapterRun(renderTo, 'width'),
  9903. height = optionsChart.height || adapterRun(renderTo, 'height'),
  9904. target = e ? e.target : win, // #805 - MooTools doesn't supply e
  9905. doReflow = function () {
  9906. if (chart.container) { // It may have been destroyed in the meantime (#1257)
  9907. chart.setSize(width, height, false);
  9908. chart.hasUserSize = null;
  9909. }
  9910. };
  9911. // Width and height checks for display:none. Target is doc in IE8 and Opera,
  9912. // win in Firefox, Chrome and IE9.
  9913. if (!chart.hasUserSize && width && height && (target === win || target === doc)) {
  9914. if (width !== chart.containerWidth || height !== chart.containerHeight) {
  9915. clearTimeout(chart.reflowTimeout);
  9916. if (e) { // Called from window.resize
  9917. chart.reflowTimeout = setTimeout(doReflow, 100);
  9918. } else { // Called directly (#2224)
  9919. doReflow();
  9920. }
  9921. }
  9922. chart.containerWidth = width;
  9923. chart.containerHeight = height;
  9924. }
  9925. },
  9926. /**
  9927. * Add the event handlers necessary for auto resizing
  9928. */
  9929. initReflow: function () {
  9930. var chart = this,
  9931. reflow = function (e) {
  9932. chart.reflow(e);
  9933. };
  9934. addEvent(win, 'resize', reflow);
  9935. addEvent(chart, 'destroy', function () {
  9936. removeEvent(win, 'resize', reflow);
  9937. });
  9938. },
  9939. /**
  9940. * Resize the chart to a given width and height
  9941. * @param {Number} width
  9942. * @param {Number} height
  9943. * @param {Object|Boolean} animation
  9944. */
  9945. setSize: function (width, height, animation) {
  9946. var chart = this,
  9947. chartWidth,
  9948. chartHeight,
  9949. fireEndResize;
  9950. // Handle the isResizing counter
  9951. chart.isResizing += 1;
  9952. fireEndResize = function () {
  9953. if (chart) {
  9954. fireEvent(chart, 'endResize', null, function () {
  9955. chart.isResizing -= 1;
  9956. });
  9957. }
  9958. };
  9959. // set the animation for the current process
  9960. setAnimation(animation, chart);
  9961. chart.oldChartHeight = chart.chartHeight;
  9962. chart.oldChartWidth = chart.chartWidth;
  9963. if (defined(width)) {
  9964. chart.chartWidth = chartWidth = mathMax(0, mathRound(width));
  9965. chart.hasUserSize = !!chartWidth;
  9966. }
  9967. if (defined(height)) {
  9968. chart.chartHeight = chartHeight = mathMax(0, mathRound(height));
  9969. }
  9970. // Resize the container with the global animation applied if enabled (#2503)
  9971. (globalAnimation ? animate : css)(chart.container, {
  9972. width: chartWidth + PX,
  9973. height: chartHeight + PX
  9974. }, globalAnimation);
  9975. chart.setChartSize(true);
  9976. chart.renderer.setSize(chartWidth, chartHeight, animation);
  9977. // handle axes
  9978. chart.maxTicks = null;
  9979. each(chart.axes, function (axis) {
  9980. axis.isDirty = true;
  9981. axis.setScale();
  9982. });
  9983. // make sure non-cartesian series are also handled
  9984. each(chart.series, function (serie) {
  9985. serie.isDirty = true;
  9986. });
  9987. chart.isDirtyLegend = true; // force legend redraw
  9988. chart.isDirtyBox = true; // force redraw of plot and chart border
  9989. chart.layOutTitles(); // #2857
  9990. chart.getMargins();
  9991. chart.redraw(animation);
  9992. chart.oldChartHeight = null;
  9993. fireEvent(chart, 'resize');
  9994. // fire endResize and set isResizing back
  9995. // If animation is disabled, fire without delay
  9996. if (globalAnimation === false) {
  9997. fireEndResize();
  9998. } else { // else set a timeout with the animation duration
  9999. setTimeout(fireEndResize, (globalAnimation && globalAnimation.duration) || 500);
  10000. }
  10001. },
  10002. /**
  10003. * Set the public chart properties. This is done before and after the pre-render
  10004. * to determine margin sizes
  10005. */
  10006. setChartSize: function (skipAxes) {
  10007. var chart = this,
  10008. inverted = chart.inverted,
  10009. renderer = chart.renderer,
  10010. chartWidth = chart.chartWidth,
  10011. chartHeight = chart.chartHeight,
  10012. optionsChart = chart.options.chart,
  10013. spacing = chart.spacing,
  10014. clipOffset = chart.clipOffset,
  10015. clipX,
  10016. clipY,
  10017. plotLeft,
  10018. plotTop,
  10019. plotWidth,
  10020. plotHeight,
  10021. plotBorderWidth;
  10022. chart.plotLeft = plotLeft = mathRound(chart.plotLeft);
  10023. chart.plotTop = plotTop = mathRound(chart.plotTop);
  10024. chart.plotWidth = plotWidth = mathMax(0, mathRound(chartWidth - plotLeft - chart.marginRight));
  10025. chart.plotHeight = plotHeight = mathMax(0, mathRound(chartHeight - plotTop - chart.marginBottom));
  10026. chart.plotSizeX = inverted ? plotHeight : plotWidth;
  10027. chart.plotSizeY = inverted ? plotWidth : plotHeight;
  10028. chart.plotBorderWidth = optionsChart.plotBorderWidth || 0;
  10029. // Set boxes used for alignment
  10030. chart.spacingBox = renderer.spacingBox = {
  10031. x: spacing[3],
  10032. y: spacing[0],
  10033. width: chartWidth - spacing[3] - spacing[1],
  10034. height: chartHeight - spacing[0] - spacing[2]
  10035. };
  10036. chart.plotBox = renderer.plotBox = {
  10037. x: plotLeft,
  10038. y: plotTop,
  10039. width: plotWidth,
  10040. height: plotHeight
  10041. };
  10042. plotBorderWidth = 2 * mathFloor(chart.plotBorderWidth / 2);
  10043. clipX = mathCeil(mathMax(plotBorderWidth, clipOffset[3]) / 2);
  10044. clipY = mathCeil(mathMax(plotBorderWidth, clipOffset[0]) / 2);
  10045. chart.clipBox = {
  10046. x: clipX,
  10047. y: clipY,
  10048. width: mathFloor(chart.plotSizeX - mathMax(plotBorderWidth, clipOffset[1]) / 2 - clipX),
  10049. height: mathMax(0, mathFloor(chart.plotSizeY - mathMax(plotBorderWidth, clipOffset[2]) / 2 - clipY))
  10050. };
  10051. if (!skipAxes) {
  10052. each(chart.axes, function (axis) {
  10053. axis.setAxisSize();
  10054. axis.setAxisTranslation();
  10055. });
  10056. }
  10057. },
  10058. /**
  10059. * Initial margins before auto size margins are applied
  10060. */
  10061. resetMargins: function () {
  10062. var chart = this,
  10063. spacing = chart.spacing,
  10064. margin = chart.margin;
  10065. chart.plotTop = pick(margin[0], spacing[0]);
  10066. chart.marginRight = pick(margin[1], spacing[1]);
  10067. chart.marginBottom = pick(margin[2], spacing[2]);
  10068. chart.plotLeft = pick(margin[3], spacing[3]);
  10069. chart.axisOffset = [0, 0, 0, 0]; // top, right, bottom, left
  10070. chart.clipOffset = [0, 0, 0, 0];
  10071. },
  10072. /**
  10073. * Draw the borders and backgrounds for chart and plot area
  10074. */
  10075. drawChartBox: function () {
  10076. var chart = this,
  10077. optionsChart = chart.options.chart,
  10078. renderer = chart.renderer,
  10079. chartWidth = chart.chartWidth,
  10080. chartHeight = chart.chartHeight,
  10081. chartBackground = chart.chartBackground,
  10082. plotBackground = chart.plotBackground,
  10083. plotBorder = chart.plotBorder,
  10084. plotBGImage = chart.plotBGImage,
  10085. chartBorderWidth = optionsChart.borderWidth || 0,
  10086. chartBackgroundColor = optionsChart.backgroundColor,
  10087. plotBackgroundColor = optionsChart.plotBackgroundColor,
  10088. plotBackgroundImage = optionsChart.plotBackgroundImage,
  10089. plotBorderWidth = optionsChart.plotBorderWidth || 0,
  10090. mgn,
  10091. bgAttr,
  10092. plotLeft = chart.plotLeft,
  10093. plotTop = chart.plotTop,
  10094. plotWidth = chart.plotWidth,
  10095. plotHeight = chart.plotHeight,
  10096. plotBox = chart.plotBox,
  10097. clipRect = chart.clipRect,
  10098. clipBox = chart.clipBox;
  10099. // Chart area
  10100. mgn = chartBorderWidth + (optionsChart.shadow ? 8 : 0);
  10101. if (chartBorderWidth || chartBackgroundColor) {
  10102. if (!chartBackground) {
  10103. bgAttr = {
  10104. fill: chartBackgroundColor || NONE
  10105. };
  10106. if (chartBorderWidth) { // #980
  10107. bgAttr.stroke = optionsChart.borderColor;
  10108. bgAttr['stroke-width'] = chartBorderWidth;
  10109. }
  10110. chart.chartBackground = renderer.rect(mgn / 2, mgn / 2, chartWidth - mgn, chartHeight - mgn,
  10111. optionsChart.borderRadius, chartBorderWidth)
  10112. .attr(bgAttr)
  10113. .addClass(PREFIX + 'background')
  10114. .add()
  10115. .shadow(optionsChart.shadow);
  10116. } else { // resize
  10117. chartBackground.animate(
  10118. chartBackground.crisp({ width: chartWidth - mgn, height: chartHeight - mgn })
  10119. );
  10120. }
  10121. }
  10122. // Plot background
  10123. if (plotBackgroundColor) {
  10124. if (!plotBackground) {
  10125. chart.plotBackground = renderer.rect(plotLeft, plotTop, plotWidth, plotHeight, 0)
  10126. .attr({
  10127. fill: plotBackgroundColor
  10128. })
  10129. .add()
  10130. .shadow(optionsChart.plotShadow);
  10131. } else {
  10132. plotBackground.animate(plotBox);
  10133. }
  10134. }
  10135. if (plotBackgroundImage) {
  10136. if (!plotBGImage) {
  10137. chart.plotBGImage = renderer.image(plotBackgroundImage, plotLeft, plotTop, plotWidth, plotHeight)
  10138. .add();
  10139. } else {
  10140. plotBGImage.animate(plotBox);
  10141. }
  10142. }
  10143. // Plot clip
  10144. if (!clipRect) {
  10145. chart.clipRect = renderer.clipRect(clipBox);
  10146. } else {
  10147. clipRect.animate({
  10148. width: clipBox.width,
  10149. height: clipBox.height
  10150. });
  10151. }
  10152. // Plot area border
  10153. if (plotBorderWidth) {
  10154. if (!plotBorder) {
  10155. chart.plotBorder = renderer.rect(plotLeft, plotTop, plotWidth, plotHeight, 0, -plotBorderWidth)
  10156. .attr({
  10157. stroke: optionsChart.plotBorderColor,
  10158. 'stroke-width': plotBorderWidth,
  10159. fill: NONE,
  10160. zIndex: 1
  10161. })
  10162. .add();
  10163. } else {
  10164. plotBorder.animate(
  10165. plotBorder.crisp({ x: plotLeft, y: plotTop, width: plotWidth, height: plotHeight })
  10166. );
  10167. }
  10168. }
  10169. // reset
  10170. chart.isDirtyBox = false;
  10171. },
  10172. /**
  10173. * Detect whether a certain chart property is needed based on inspecting its options
  10174. * and series. This mainly applies to the chart.invert property, and in extensions to
  10175. * the chart.angular and chart.polar properties.
  10176. */
  10177. propFromSeries: function () {
  10178. var chart = this,
  10179. optionsChart = chart.options.chart,
  10180. klass,
  10181. seriesOptions = chart.options.series,
  10182. i,
  10183. value;
  10184. each(['inverted', 'angular', 'polar'], function (key) {
  10185. // The default series type's class
  10186. klass = seriesTypes[optionsChart.type || optionsChart.defaultSeriesType];
  10187. // Get the value from available chart-wide properties
  10188. value = (
  10189. chart[key] || // 1. it is set before
  10190. optionsChart[key] || // 2. it is set in the options
  10191. (klass && klass.prototype[key]) // 3. it's default series class requires it
  10192. );
  10193. // 4. Check if any the chart's series require it
  10194. i = seriesOptions && seriesOptions.length;
  10195. while (!value && i--) {
  10196. klass = seriesTypes[seriesOptions[i].type];
  10197. if (klass && klass.prototype[key]) {
  10198. value = true;
  10199. }
  10200. }
  10201. // Set the chart property
  10202. chart[key] = value;
  10203. });
  10204. },
  10205. /**
  10206. * Link two or more series together. This is done initially from Chart.render,
  10207. * and after Chart.addSeries and Series.remove.
  10208. */
  10209. linkSeries: function () {
  10210. var chart = this,
  10211. chartSeries = chart.series;
  10212. // Reset links
  10213. each(chartSeries, function (series) {
  10214. series.linkedSeries.length = 0;
  10215. });
  10216. // Apply new links
  10217. each(chartSeries, function (series) {
  10218. var linkedTo = series.options.linkedTo;
  10219. if (isString(linkedTo)) {
  10220. if (linkedTo === ':previous') {
  10221. linkedTo = chart.series[series.index - 1];
  10222. } else {
  10223. linkedTo = chart.get(linkedTo);
  10224. }
  10225. if (linkedTo) {
  10226. linkedTo.linkedSeries.push(series);
  10227. series.linkedParent = linkedTo;
  10228. }
  10229. }
  10230. });
  10231. },
  10232. /**
  10233. * Render series for the chart
  10234. */
  10235. renderSeries: function () {
  10236. each(this.series, function (serie) {
  10237. serie.translate();
  10238. if (serie.setTooltipPoints) {
  10239. serie.setTooltipPoints();
  10240. }
  10241. serie.render();
  10242. });
  10243. },
  10244. /**
  10245. * Render labels for the chart
  10246. */
  10247. renderLabels: function () {
  10248. var chart = this,
  10249. labels = chart.options.labels;
  10250. if (labels.items) {
  10251. each(labels.items, function (label) {
  10252. var style = extend(labels.style, label.style),
  10253. x = pInt(style.left) + chart.plotLeft,
  10254. y = pInt(style.top) + chart.plotTop + 12;
  10255. // delete to prevent rewriting in IE
  10256. delete style.left;
  10257. delete style.top;
  10258. chart.renderer.text(
  10259. label.html,
  10260. x,
  10261. y
  10262. )
  10263. .attr({ zIndex: 2 })
  10264. .css(style)
  10265. .add();
  10266. });
  10267. }
  10268. },
  10269. /**
  10270. * Render all graphics for the chart
  10271. */
  10272. render: function () {
  10273. var chart = this,
  10274. axes = chart.axes,
  10275. renderer = chart.renderer,
  10276. options = chart.options;
  10277. // Title
  10278. chart.setTitle();
  10279. // Legend
  10280. chart.legend = new Legend(chart, options.legend);
  10281. chart.getStacks(); // render stacks
  10282. // Get margins by pre-rendering axes
  10283. // set axes scales
  10284. each(axes, function (axis) {
  10285. axis.setScale();
  10286. });
  10287. chart.getMargins();
  10288. chart.maxTicks = null; // reset for second pass
  10289. each(axes, function (axis) {
  10290. axis.setTickPositions(true); // update to reflect the new margins
  10291. axis.setMaxTicks();
  10292. });
  10293. chart.adjustTickAmounts();
  10294. chart.getMargins(); // second pass to check for new labels
  10295. // Draw the borders and backgrounds
  10296. chart.drawChartBox();
  10297. // Axes
  10298. if (chart.hasCartesianSeries) {
  10299. each(axes, function (axis) {
  10300. axis.render();
  10301. });
  10302. }
  10303. // The series
  10304. if (!chart.seriesGroup) {
  10305. chart.seriesGroup = renderer.g('series-group')
  10306. .attr({ zIndex: 3 })
  10307. .add();
  10308. }
  10309. chart.renderSeries();
  10310. // Labels
  10311. chart.renderLabels();
  10312. // Credits
  10313. chart.showCredits(options.credits);
  10314. // Set flag
  10315. chart.hasRendered = true;
  10316. },
  10317. /**
  10318. * Show chart credits based on config options
  10319. */
  10320. showCredits: function (credits) {
  10321. if (credits.enabled && !this.credits) {
  10322. this.credits = this.renderer.text(
  10323. credits.text,
  10324. 0,
  10325. 0
  10326. )
  10327. .on('click', function () {
  10328. if (credits.href) {
  10329. location.href = credits.href;
  10330. }
  10331. })
  10332. .attr({
  10333. align: credits.position.align,
  10334. zIndex: 8
  10335. })
  10336. .css(credits.style)
  10337. .add()
  10338. .align(credits.position);
  10339. }
  10340. },
  10341. /**
  10342. * Clean up memory usage
  10343. */
  10344. destroy: function () {
  10345. var chart = this,
  10346. axes = chart.axes,
  10347. series = chart.series,
  10348. container = chart.container,
  10349. i,
  10350. parentNode = container && container.parentNode;
  10351. // fire the chart.destoy event
  10352. fireEvent(chart, 'destroy');
  10353. // Delete the chart from charts lookup array
  10354. charts[chart.index] = UNDEFINED;
  10355. chartCount--;
  10356. chart.renderTo.removeAttribute('data-highcharts-chart');
  10357. // remove events
  10358. removeEvent(chart);
  10359. // ==== Destroy collections:
  10360. // Destroy axes
  10361. i = axes.length;
  10362. while (i--) {
  10363. axes[i] = axes[i].destroy();
  10364. }
  10365. // Destroy each series
  10366. i = series.length;
  10367. while (i--) {
  10368. series[i] = series[i].destroy();
  10369. }
  10370. // ==== Destroy chart properties:
  10371. each(['title', 'subtitle', 'chartBackground', 'plotBackground', 'plotBGImage',
  10372. 'plotBorder', 'seriesGroup', 'clipRect', 'credits', 'pointer', 'scroller',
  10373. 'rangeSelector', 'legend', 'resetZoomButton', 'tooltip', 'renderer'], function (name) {
  10374. var prop = chart[name];
  10375. if (prop && prop.destroy) {
  10376. chart[name] = prop.destroy();
  10377. }
  10378. });
  10379. // remove container and all SVG
  10380. if (container) { // can break in IE when destroyed before finished loading
  10381. container.innerHTML = '';
  10382. removeEvent(container);
  10383. if (parentNode) {
  10384. discardElement(container);
  10385. }
  10386. }
  10387. // clean it all up
  10388. for (i in chart) {
  10389. delete chart[i];
  10390. }
  10391. },
  10392. /**
  10393. * VML namespaces can't be added until after complete. Listening
  10394. * for Perini's doScroll hack is not enough.
  10395. */
  10396. isReadyToRender: function () {
  10397. var chart = this;
  10398. // Note: in spite of JSLint's complaints, win == win.top is required
  10399. /*jslint eqeq: true*/
  10400. if ((!hasSVG && (win == win.top && doc.readyState !== 'complete')) || (useCanVG && !win.canvg)) {
  10401. /*jslint eqeq: false*/
  10402. if (useCanVG) {
  10403. // Delay rendering until canvg library is downloaded and ready
  10404. CanVGController.push(function () { chart.firstRender(); }, chart.options.global.canvasToolsURL);
  10405. } else {
  10406. doc.attachEvent('onreadystatechange', function () {
  10407. doc.detachEvent('onreadystatechange', chart.firstRender);
  10408. if (doc.readyState === 'complete') {
  10409. chart.firstRender();
  10410. }
  10411. });
  10412. }
  10413. return false;
  10414. }
  10415. return true;
  10416. },
  10417. /**
  10418. * Prepare for first rendering after all data are loaded
  10419. */
  10420. firstRender: function () {
  10421. var chart = this,
  10422. options = chart.options,
  10423. callback = chart.callback;
  10424. // Check whether the chart is ready to render
  10425. if (!chart.isReadyToRender()) {
  10426. return;
  10427. }
  10428. // Create the container
  10429. chart.getContainer();
  10430. // Run an early event after the container and renderer are established
  10431. fireEvent(chart, 'init');
  10432. chart.resetMargins();
  10433. chart.setChartSize();
  10434. // Set the common chart properties (mainly invert) from the given series
  10435. chart.propFromSeries();
  10436. // get axes
  10437. chart.getAxes();
  10438. // Initialize the series
  10439. each(options.series || [], function (serieOptions) {
  10440. chart.initSeries(serieOptions);
  10441. });
  10442. chart.linkSeries();
  10443. // Run an event after axes and series are initialized, but before render. At this stage,
  10444. // the series data is indexed and cached in the xData and yData arrays, so we can access
  10445. // those before rendering. Used in Highstock.
  10446. fireEvent(chart, 'beforeRender');
  10447. // depends on inverted and on margins being set
  10448. if (Highcharts.Pointer) {
  10449. chart.pointer = new Pointer(chart, options);
  10450. }
  10451. chart.render();
  10452. // add canvas
  10453. chart.renderer.draw();
  10454. // run callbacks
  10455. if (callback) {
  10456. callback.apply(chart, [chart]);
  10457. }
  10458. each(chart.callbacks, function (fn) {
  10459. fn.apply(chart, [chart]);
  10460. });
  10461. // If the chart was rendered outside the top container, put it back in
  10462. chart.cloneRenderTo(true);
  10463. fireEvent(chart, 'load');
  10464. },
  10465. /**
  10466. * Creates arrays for spacing and margin from given options.
  10467. */
  10468. splashArray: function (target, options) {
  10469. var oVar = options[target],
  10470. tArray = isObject(oVar) ? oVar : [oVar, oVar, oVar, oVar];
  10471. return [pick(options[target + 'Top'], tArray[0]),
  10472. pick(options[target + 'Right'], tArray[1]),
  10473. pick(options[target + 'Bottom'], tArray[2]),
  10474. pick(options[target + 'Left'], tArray[3])];
  10475. }
  10476. }; // end Chart
  10477. // Hook for exporting module
  10478. Chart.prototype.callbacks = [];
  10479. var CenteredSeriesMixin = Highcharts.CenteredSeriesMixin = {
  10480. /**
  10481. * Get the center of the pie based on the size and center options relative to the
  10482. * plot area. Borrowed by the polar and gauge series types.
  10483. */
  10484. getCenter: function () {
  10485. var options = this.options,
  10486. chart = this.chart,
  10487. slicingRoom = 2 * (options.slicedOffset || 0),
  10488. handleSlicingRoom,
  10489. plotWidth = chart.plotWidth - 2 * slicingRoom,
  10490. plotHeight = chart.plotHeight - 2 * slicingRoom,
  10491. centerOption = options.center,
  10492. positions = [pick(centerOption[0], '50%'), pick(centerOption[1], '50%'), options.size || '100%', options.innerSize || 0],
  10493. smallestSize = mathMin(plotWidth, plotHeight),
  10494. isPercent;
  10495. return map(positions, function (length, i) {
  10496. isPercent = /%$/.test(length);
  10497. handleSlicingRoom = i < 2 || (i === 2 && isPercent);
  10498. return (isPercent ?
  10499. // i == 0: centerX, relative to width
  10500. // i == 1: centerY, relative to height
  10501. // i == 2: size, relative to smallestSize
  10502. // i == 4: innerSize, relative to smallestSize
  10503. [plotWidth, plotHeight, smallestSize, smallestSize][i] *
  10504. pInt(length) / 100 :
  10505. length) + (handleSlicingRoom ? slicingRoom : 0);
  10506. });
  10507. }
  10508. };
  10509. /**
  10510. * The Point object and prototype. Inheritable and used as base for PiePoint
  10511. */
  10512. var Point = function () {};
  10513. Point.prototype = {
  10514. /**
  10515. * Initialize the point
  10516. * @param {Object} series The series object containing this point
  10517. * @param {Object} options The data in either number, array or object format
  10518. */
  10519. init: function (series, options, x) {
  10520. var point = this,
  10521. colors;
  10522. point.series = series;
  10523. point.applyOptions(options, x);
  10524. point.pointAttr = {};
  10525. if (series.options.colorByPoint) {
  10526. colors = series.options.colors || series.chart.options.colors;
  10527. point.color = point.color || colors[series.colorCounter++];
  10528. // loop back to zero
  10529. if (series.colorCounter === colors.length) {
  10530. series.colorCounter = 0;
  10531. }
  10532. }
  10533. series.chart.pointCount++;
  10534. return point;
  10535. },
  10536. /**
  10537. * Apply the options containing the x and y data and possible some extra properties.
  10538. * This is called on point init or from point.update.
  10539. *
  10540. * @param {Object} options
  10541. */
  10542. applyOptions: function (options, x) {
  10543. var point = this,
  10544. series = point.series,
  10545. pointValKey = series.options.pointValKey || series.pointValKey;
  10546. options = Point.prototype.optionsToObject.call(this, options);
  10547. // copy options directly to point
  10548. extend(point, options);
  10549. point.options = point.options ? extend(point.options, options) : options;
  10550. // For higher dimension series types. For instance, for ranges, point.y is mapped to point.low.
  10551. if (pointValKey) {
  10552. point.y = point[pointValKey];
  10553. }
  10554. // If no x is set by now, get auto incremented value. All points must have an
  10555. // x value, however the y value can be null to create a gap in the series
  10556. if (point.x === UNDEFINED && series) {
  10557. point.x = x === UNDEFINED ? series.autoIncrement() : x;
  10558. }
  10559. return point;
  10560. },
  10561. /**
  10562. * Transform number or array configs into objects
  10563. */
  10564. optionsToObject: function (options) {
  10565. var ret = {},
  10566. series = this.series,
  10567. pointArrayMap = series.pointArrayMap || ['y'],
  10568. valueCount = pointArrayMap.length,
  10569. firstItemType,
  10570. i = 0,
  10571. j = 0;
  10572. if (typeof options === 'number' || options === null) {
  10573. ret[pointArrayMap[0]] = options;
  10574. } else if (isArray(options)) {
  10575. // with leading x value
  10576. if (options.length > valueCount) {
  10577. firstItemType = typeof options[0];
  10578. if (firstItemType === 'string') {
  10579. ret.name = options[0];
  10580. } else if (firstItemType === 'number') {
  10581. ret.x = options[0];
  10582. }
  10583. i++;
  10584. }
  10585. while (j < valueCount) {
  10586. ret[pointArrayMap[j++]] = options[i++];
  10587. }
  10588. } else if (typeof options === 'object') {
  10589. ret = options;
  10590. // This is the fastest way to detect if there are individual point dataLabels that need
  10591. // to be considered in drawDataLabels. These can only occur in object configs.
  10592. if (options.dataLabels) {
  10593. series._hasPointLabels = true;
  10594. }
  10595. // Same approach as above for markers
  10596. if (options.marker) {
  10597. series._hasPointMarkers = true;
  10598. }
  10599. }
  10600. return ret;
  10601. },
  10602. /**
  10603. * Destroy a point to clear memory. Its reference still stays in series.data.
  10604. */
  10605. destroy: function () {
  10606. var point = this,
  10607. series = point.series,
  10608. chart = series.chart,
  10609. hoverPoints = chart.hoverPoints,
  10610. prop;
  10611. chart.pointCount--;
  10612. if (hoverPoints) {
  10613. point.setState();
  10614. erase(hoverPoints, point);
  10615. if (!hoverPoints.length) {
  10616. chart.hoverPoints = null;
  10617. }
  10618. }
  10619. if (point === chart.hoverPoint) {
  10620. point.onMouseOut();
  10621. }
  10622. // remove all events
  10623. if (point.graphic || point.dataLabel) { // removeEvent and destroyElements are performance expensive
  10624. removeEvent(point);
  10625. point.destroyElements();
  10626. }
  10627. if (point.legendItem) { // pies have legend items
  10628. chart.legend.destroyItem(point);
  10629. }
  10630. for (prop in point) {
  10631. point[prop] = null;
  10632. }
  10633. },
  10634. /**
  10635. * Destroy SVG elements associated with the point
  10636. */
  10637. destroyElements: function () {
  10638. var point = this,
  10639. props = ['graphic', 'dataLabel', 'dataLabelUpper', 'group', 'connector', 'shadowGroup'],
  10640. prop,
  10641. i = 6;
  10642. while (i--) {
  10643. prop = props[i];
  10644. if (point[prop]) {
  10645. point[prop] = point[prop].destroy();
  10646. }
  10647. }
  10648. },
  10649. /**
  10650. * Return the configuration hash needed for the data label and tooltip formatters
  10651. */
  10652. getLabelConfig: function () {
  10653. var point = this;
  10654. return {
  10655. x: point.category,
  10656. y: point.y,
  10657. key: point.name || point.category,
  10658. series: point.series,
  10659. point: point,
  10660. percentage: point.percentage,
  10661. total: point.total || point.stackTotal
  10662. };
  10663. },
  10664. /**
  10665. * Extendable method for formatting each point's tooltip line
  10666. *
  10667. * @return {String} A string to be concatenated in to the common tooltip text
  10668. */
  10669. tooltipFormatter: function (pointFormat) {
  10670. // Insert options for valueDecimals, valuePrefix, and valueSuffix
  10671. var series = this.series,
  10672. seriesTooltipOptions = series.tooltipOptions,
  10673. valueDecimals = pick(seriesTooltipOptions.valueDecimals, ''),
  10674. valuePrefix = seriesTooltipOptions.valuePrefix || '',
  10675. valueSuffix = seriesTooltipOptions.valueSuffix || '';
  10676. // Loop over the point array map and replace unformatted values with sprintf formatting markup
  10677. each(series.pointArrayMap || ['y'], function (key) {
  10678. key = '{point.' + key; // without the closing bracket
  10679. if (valuePrefix || valueSuffix) {
  10680. pointFormat = pointFormat.replace(key + '}', valuePrefix + key + '}' + valueSuffix);
  10681. }
  10682. pointFormat = pointFormat.replace(key + '}', key + ':,.' + valueDecimals + 'f}');
  10683. });
  10684. return format(pointFormat, {
  10685. point: this,
  10686. series: this.series
  10687. });
  10688. },
  10689. /**
  10690. * Fire an event on the Point object. Must not be renamed to fireEvent, as this
  10691. * causes a name clash in MooTools
  10692. * @param {String} eventType
  10693. * @param {Object} eventArgs Additional event arguments
  10694. * @param {Function} defaultFunction Default event handler
  10695. */
  10696. firePointEvent: function (eventType, eventArgs, defaultFunction) {
  10697. var point = this,
  10698. series = this.series,
  10699. seriesOptions = series.options;
  10700. // load event handlers on demand to save time on mouseover/out
  10701. if (seriesOptions.point.events[eventType] || (point.options && point.options.events && point.options.events[eventType])) {
  10702. this.importEvents();
  10703. }
  10704. // add default handler if in selection mode
  10705. if (eventType === 'click' && seriesOptions.allowPointSelect) {
  10706. defaultFunction = function (event) {
  10707. // Control key is for Windows, meta (= Cmd key) for Mac, Shift for Opera
  10708. point.select(null, event.ctrlKey || event.metaKey || event.shiftKey);
  10709. };
  10710. }
  10711. fireEvent(this, eventType, eventArgs, defaultFunction);
  10712. }
  10713. };/**
  10714. * @classDescription The base function which all other series types inherit from. The data in the series is stored
  10715. * in various arrays.
  10716. *
  10717. * - First, series.options.data contains all the original config options for
  10718. * each point whether added by options or methods like series.addPoint.
  10719. * - Next, series.data contains those values converted to points, but in case the series data length
  10720. * exceeds the cropThreshold, or if the data is grouped, series.data doesn't contain all the points. It
  10721. * only contains the points that have been created on demand.
  10722. * - Then there's series.points that contains all currently visible point objects. In case of cropping,
  10723. * the cropped-away points are not part of this array. The series.points array starts at series.cropStart
  10724. * compared to series.data and series.options.data. If however the series data is grouped, these can't
  10725. * be correlated one to one.
  10726. * - series.xData and series.processedXData contain clean x values, equivalent to series.data and series.points.
  10727. * - series.yData and series.processedYData contain clean x values, equivalent to series.data and series.points.
  10728. *
  10729. * @param {Object} chart
  10730. * @param {Object} options
  10731. */
  10732. var Series = function () {};
  10733. Series.prototype = {
  10734. isCartesian: true,
  10735. type: 'line',
  10736. pointClass: Point,
  10737. sorted: true, // requires the data to be sorted
  10738. requireSorting: true,
  10739. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  10740. stroke: 'lineColor',
  10741. 'stroke-width': 'lineWidth',
  10742. fill: 'fillColor',
  10743. r: 'radius'
  10744. },
  10745. axisTypes: ['xAxis', 'yAxis'],
  10746. colorCounter: 0,
  10747. parallelArrays: ['x', 'y'], // each point's x and y values are stored in this.xData and this.yData
  10748. init: function (chart, options) {
  10749. var series = this,
  10750. eventType,
  10751. events,
  10752. chartSeries = chart.series,
  10753. sortByIndex = function (a, b) {
  10754. return pick(a.options.index, a._i) - pick(b.options.index, b._i);
  10755. };
  10756. series.chart = chart;
  10757. series.options = options = series.setOptions(options); // merge with plotOptions
  10758. series.linkedSeries = [];
  10759. // bind the axes
  10760. series.bindAxes();
  10761. // set some variables
  10762. extend(series, {
  10763. name: options.name,
  10764. state: NORMAL_STATE,
  10765. pointAttr: {},
  10766. visible: options.visible !== false, // true by default
  10767. selected: options.selected === true // false by default
  10768. });
  10769. // special
  10770. if (useCanVG) {
  10771. options.animation = false;
  10772. }
  10773. // register event listeners
  10774. events = options.events;
  10775. for (eventType in events) {
  10776. addEvent(series, eventType, events[eventType]);
  10777. }
  10778. if (
  10779. (events && events.click) ||
  10780. (options.point && options.point.events && options.point.events.click) ||
  10781. options.allowPointSelect
  10782. ) {
  10783. chart.runTrackerClick = true;
  10784. }
  10785. series.getColor();
  10786. series.getSymbol();
  10787. // Set the data
  10788. each(series.parallelArrays, function (key) {
  10789. series[key + 'Data'] = [];
  10790. });
  10791. series.setData(options.data, false);
  10792. // Mark cartesian
  10793. if (series.isCartesian) {
  10794. chart.hasCartesianSeries = true;
  10795. }
  10796. // Register it in the chart
  10797. chartSeries.push(series);
  10798. series._i = chartSeries.length - 1;
  10799. // Sort series according to index option (#248, #1123, #2456)
  10800. stableSort(chartSeries, sortByIndex);
  10801. if (this.yAxis) {
  10802. stableSort(this.yAxis.series, sortByIndex);
  10803. }
  10804. each(chartSeries, function (series, i) {
  10805. series.index = i;
  10806. series.name = series.name || 'Series ' + (i + 1);
  10807. });
  10808. },
  10809. /**
  10810. * Set the xAxis and yAxis properties of cartesian series, and register the series
  10811. * in the axis.series array
  10812. */
  10813. bindAxes: function () {
  10814. var series = this,
  10815. seriesOptions = series.options,
  10816. chart = series.chart,
  10817. axisOptions;
  10818. each(series.axisTypes || [], function (AXIS) { // repeat for xAxis and yAxis
  10819. each(chart[AXIS], function (axis) { // loop through the chart's axis objects
  10820. axisOptions = axis.options;
  10821. // apply if the series xAxis or yAxis option mathches the number of the
  10822. // axis, or if undefined, use the first axis
  10823. if ((seriesOptions[AXIS] === axisOptions.index) ||
  10824. (seriesOptions[AXIS] !== UNDEFINED && seriesOptions[AXIS] === axisOptions.id) ||
  10825. (seriesOptions[AXIS] === UNDEFINED && axisOptions.index === 0)) {
  10826. // register this series in the axis.series lookup
  10827. axis.series.push(series);
  10828. // set this series.xAxis or series.yAxis reference
  10829. series[AXIS] = axis;
  10830. // mark dirty for redraw
  10831. axis.isDirty = true;
  10832. }
  10833. });
  10834. // The series needs an X and an Y axis
  10835. if (!series[AXIS] && series.optionalAxis !== AXIS) {
  10836. error(18, true);
  10837. }
  10838. });
  10839. },
  10840. /**
  10841. * For simple series types like line and column, the data values are held in arrays like
  10842. * xData and yData for quick lookup to find extremes and more. For multidimensional series
  10843. * like bubble and map, this can be extended with arrays like zData and valueData by
  10844. * adding to the series.parallelArrays array.
  10845. */
  10846. updateParallelArrays: function (point, i) {
  10847. var series = point.series,
  10848. args = arguments,
  10849. fn = typeof i === 'number' ?
  10850. // Insert the value in the given position
  10851. function (key) {
  10852. var val = key === 'y' && series.toYData ? series.toYData(point) : point[key];
  10853. series[key + 'Data'][i] = val;
  10854. } :
  10855. // Apply the method specified in i with the following arguments as arguments
  10856. function (key) {
  10857. Array.prototype[i].apply(series[key + 'Data'], Array.prototype.slice.call(args, 2));
  10858. };
  10859. each(series.parallelArrays, fn);
  10860. },
  10861. /**
  10862. * Return an auto incremented x value based on the pointStart and pointInterval options.
  10863. * This is only used if an x value is not given for the point that calls autoIncrement.
  10864. */
  10865. autoIncrement: function () {
  10866. var series = this,
  10867. options = series.options,
  10868. xIncrement = series.xIncrement;
  10869. xIncrement = pick(xIncrement, options.pointStart, 0);
  10870. series.pointInterval = pick(series.pointInterval, options.pointInterval, 1);
  10871. series.xIncrement = xIncrement + series.pointInterval;
  10872. return xIncrement;
  10873. },
  10874. /**
  10875. * Divide the series data into segments divided by null values.
  10876. */
  10877. getSegments: function () {
  10878. var series = this,
  10879. lastNull = -1,
  10880. segments = [],
  10881. i,
  10882. points = series.points,
  10883. pointsLength = points.length;
  10884. if (pointsLength) { // no action required for []
  10885. // if connect nulls, just remove null points
  10886. if (series.options.connectNulls) {
  10887. i = pointsLength;
  10888. while (i--) {
  10889. if (points[i].y === null) {
  10890. points.splice(i, 1);
  10891. }
  10892. }
  10893. if (points.length) {
  10894. segments = [points];
  10895. }
  10896. // else, split on null points
  10897. } else {
  10898. each(points, function (point, i) {
  10899. if (point.y === null) {
  10900. if (i > lastNull + 1) {
  10901. segments.push(points.slice(lastNull + 1, i));
  10902. }
  10903. lastNull = i;
  10904. } else if (i === pointsLength - 1) { // last value
  10905. segments.push(points.slice(lastNull + 1, i + 1));
  10906. }
  10907. });
  10908. }
  10909. }
  10910. // register it
  10911. series.segments = segments;
  10912. },
  10913. /**
  10914. * Set the series options by merging from the options tree
  10915. * @param {Object} itemOptions
  10916. */
  10917. setOptions: function (itemOptions) {
  10918. var chart = this.chart,
  10919. chartOptions = chart.options,
  10920. plotOptions = chartOptions.plotOptions,
  10921. userOptions = chart.userOptions || {},
  10922. userPlotOptions = userOptions.plotOptions || {},
  10923. typeOptions = plotOptions[this.type],
  10924. options;
  10925. this.userOptions = itemOptions;
  10926. options = merge(
  10927. typeOptions,
  10928. plotOptions.series,
  10929. itemOptions
  10930. );
  10931. // The tooltip options are merged between global and series specific options
  10932. this.tooltipOptions = merge(
  10933. defaultOptions.tooltip,
  10934. defaultOptions.plotOptions[this.type].tooltip,
  10935. userOptions.tooltip,
  10936. userPlotOptions.series && userPlotOptions.series.tooltip,
  10937. userPlotOptions[this.type] && userPlotOptions[this.type].tooltip,
  10938. itemOptions.tooltip
  10939. );
  10940. // Delete marker object if not allowed (#1125)
  10941. if (typeOptions.marker === null) {
  10942. delete options.marker;
  10943. }
  10944. return options;
  10945. },
  10946. getCyclic: function (prop, value, defaults) {
  10947. var i,
  10948. userOptions = this.userOptions,
  10949. indexName = '_' + prop + 'Index',
  10950. counterName = prop + 'Counter';
  10951. if (!value) {
  10952. if (defined(userOptions[indexName])) { // after Series.update()
  10953. i = userOptions[indexName];
  10954. } else {
  10955. userOptions[indexName] = i = this.chart[counterName] % defaults.length;
  10956. this.chart[counterName] += 1;
  10957. }
  10958. value = defaults[i];
  10959. }
  10960. this[prop] = value;
  10961. },
  10962. /**
  10963. * Get the series' color
  10964. */
  10965. getColor: function () {
  10966. if (!this.options.colorByPoint) {
  10967. this.getCyclic('color', this.options.color || defaultPlotOptions[this.type].color, this.chart.options.colors);
  10968. }
  10969. },
  10970. /**
  10971. * Get the series' symbol
  10972. */
  10973. getSymbol: function () {
  10974. var seriesMarkerOption = this.options.marker;
  10975. this.getCyclic('symbol', seriesMarkerOption.symbol, this.chart.options.symbols);
  10976. // don't substract radius in image symbols (#604)
  10977. if (/^url/.test(this.symbol)) {
  10978. seriesMarkerOption.radius = 0;
  10979. }
  10980. },
  10981. drawLegendSymbol: LegendSymbolMixin.drawLineMarker,
  10982. /**
  10983. * Replace the series data with a new set of data
  10984. * @param {Object} data
  10985. * @param {Object} redraw
  10986. */
  10987. setData: function (data, redraw, animation, updatePoints) {
  10988. var series = this,
  10989. oldData = series.points,
  10990. oldDataLength = (oldData && oldData.length) || 0,
  10991. dataLength,
  10992. options = series.options,
  10993. chart = series.chart,
  10994. firstPoint = null,
  10995. xAxis = series.xAxis,
  10996. hasCategories = xAxis && !!xAxis.categories,
  10997. tooltipPoints = series.tooltipPoints,
  10998. i,
  10999. turboThreshold = options.turboThreshold,
  11000. pt,
  11001. xData = this.xData,
  11002. yData = this.yData,
  11003. pointArrayMap = series.pointArrayMap,
  11004. valueCount = pointArrayMap && pointArrayMap.length;
  11005. data = data || [];
  11006. dataLength = data.length;
  11007. redraw = pick(redraw, true);
  11008. // If the point count is the same as is was, just run Point.update which is
  11009. // cheaper, allows animation, and keeps references to points.
  11010. if (updatePoints !== false && dataLength && oldDataLength === dataLength && !series.cropped && !series.hasGroupedData) {
  11011. each(data, function (point, i) {
  11012. oldData[i].update(point, false);
  11013. });
  11014. } else {
  11015. // Reset properties
  11016. series.xIncrement = null;
  11017. series.pointRange = hasCategories ? 1 : options.pointRange;
  11018. series.colorCounter = 0; // for series with colorByPoint (#1547)
  11019. // Update parallel arrays
  11020. each(this.parallelArrays, function (key) {
  11021. series[key + 'Data'].length = 0;
  11022. });
  11023. // In turbo mode, only one- or twodimensional arrays of numbers are allowed. The
  11024. // first value is tested, and we assume that all the rest are defined the same
  11025. // way. Although the 'for' loops are similar, they are repeated inside each
  11026. // if-else conditional for max performance.
  11027. if (turboThreshold && dataLength > turboThreshold) {
  11028. // find the first non-null point
  11029. i = 0;
  11030. while (firstPoint === null && i < dataLength) {
  11031. firstPoint = data[i];
  11032. i++;
  11033. }
  11034. if (isNumber(firstPoint)) { // assume all points are numbers
  11035. var x = pick(options.pointStart, 0),
  11036. pointInterval = pick(options.pointInterval, 1);
  11037. for (i = 0; i < dataLength; i++) {
  11038. xData[i] = x;
  11039. yData[i] = data[i];
  11040. x += pointInterval;
  11041. }
  11042. series.xIncrement = x;
  11043. } else if (isArray(firstPoint)) { // assume all points are arrays
  11044. if (valueCount) { // [x, low, high] or [x, o, h, l, c]
  11045. for (i = 0; i < dataLength; i++) {
  11046. pt = data[i];
  11047. xData[i] = pt[0];
  11048. yData[i] = pt.slice(1, valueCount + 1);
  11049. }
  11050. } else { // [x, y]
  11051. for (i = 0; i < dataLength; i++) {
  11052. pt = data[i];
  11053. xData[i] = pt[0];
  11054. yData[i] = pt[1];
  11055. }
  11056. }
  11057. } else {
  11058. error(12); // Highcharts expects configs to be numbers or arrays in turbo mode
  11059. }
  11060. } else {
  11061. for (i = 0; i < dataLength; i++) {
  11062. if (data[i] !== UNDEFINED) { // stray commas in oldIE
  11063. pt = { series: series };
  11064. series.pointClass.prototype.applyOptions.apply(pt, [data[i]]);
  11065. series.updateParallelArrays(pt, i);
  11066. if (hasCategories && pt.name) {
  11067. xAxis.names[pt.x] = pt.name; // #2046
  11068. }
  11069. }
  11070. }
  11071. }
  11072. // Forgetting to cast strings to numbers is a common caveat when handling CSV or JSON
  11073. if (isString(yData[0])) {
  11074. error(14, true);
  11075. }
  11076. series.data = [];
  11077. series.options.data = data;
  11078. //series.zData = zData;
  11079. // destroy old points
  11080. i = oldDataLength;
  11081. while (i--) {
  11082. if (oldData[i] && oldData[i].destroy) {
  11083. oldData[i].destroy();
  11084. }
  11085. }
  11086. if (tooltipPoints) { // #2594
  11087. tooltipPoints.length = 0;
  11088. }
  11089. // reset minRange (#878)
  11090. if (xAxis) {
  11091. xAxis.minRange = xAxis.userMinRange;
  11092. }
  11093. // redraw
  11094. series.isDirty = series.isDirtyData = chart.isDirtyBox = true;
  11095. animation = false;
  11096. }
  11097. if (redraw) {
  11098. chart.redraw(animation);
  11099. }
  11100. },
  11101. /**
  11102. * Process the data by cropping away unused data points if the series is longer
  11103. * than the crop threshold. This saves computing time for lage series.
  11104. */
  11105. processData: function (force) {
  11106. var series = this,
  11107. processedXData = series.xData, // copied during slice operation below
  11108. processedYData = series.yData,
  11109. dataLength = processedXData.length,
  11110. croppedData,
  11111. cropStart = 0,
  11112. cropped,
  11113. distance,
  11114. closestPointRange,
  11115. xAxis = series.xAxis,
  11116. i, // loop variable
  11117. options = series.options,
  11118. cropThreshold = options.cropThreshold,
  11119. activePointCount = 0,
  11120. isCartesian = series.isCartesian,
  11121. xExtremes,
  11122. min,
  11123. max;
  11124. // If the series data or axes haven't changed, don't go through this. Return false to pass
  11125. // the message on to override methods like in data grouping.
  11126. if (isCartesian && !series.isDirty && !xAxis.isDirty && !series.yAxis.isDirty && !force) {
  11127. return false;
  11128. }
  11129. // optionally filter out points outside the plot area
  11130. if (isCartesian && series.sorted && (!cropThreshold || dataLength > cropThreshold || series.forceCrop)) {
  11131. xExtremes = xAxis.getExtremes(); // corrected for log axis (#3053)
  11132. min = xExtremes.min;
  11133. max = xExtremes.max;
  11134. // it's outside current extremes
  11135. if (processedXData[dataLength - 1] < min || processedXData[0] > max) {
  11136. processedXData = [];
  11137. processedYData = [];
  11138. // only crop if it's actually spilling out
  11139. } else if (processedXData[0] < min || processedXData[dataLength - 1] > max) {
  11140. croppedData = this.cropData(series.xData, series.yData, min, max);
  11141. processedXData = croppedData.xData;
  11142. processedYData = croppedData.yData;
  11143. cropStart = croppedData.start;
  11144. cropped = true;
  11145. activePointCount = processedXData.length;
  11146. }
  11147. }
  11148. // Find the closest distance between processed points
  11149. for (i = processedXData.length - 1; i >= 0; i--) {
  11150. distance = processedXData[i] - processedXData[i - 1];
  11151. if (!cropped && processedXData[i] > min && processedXData[i] < max) {
  11152. activePointCount++;
  11153. }
  11154. if (distance > 0 && (closestPointRange === UNDEFINED || distance < closestPointRange)) {
  11155. closestPointRange = distance;
  11156. // Unsorted data is not supported by the line tooltip, as well as data grouping and
  11157. // navigation in Stock charts (#725) and width calculation of columns (#1900)
  11158. } else if (distance < 0 && series.requireSorting) {
  11159. error(15);
  11160. }
  11161. }
  11162. // Record the properties
  11163. series.cropped = cropped; // undefined or true
  11164. series.cropStart = cropStart;
  11165. series.processedXData = processedXData;
  11166. series.processedYData = processedYData;
  11167. series.activePointCount = activePointCount;
  11168. if (options.pointRange === null) { // null means auto, as for columns, candlesticks and OHLC
  11169. series.pointRange = closestPointRange || 1;
  11170. }
  11171. series.closestPointRange = closestPointRange;
  11172. },
  11173. /**
  11174. * Iterate over xData and crop values between min and max. Returns object containing crop start/end
  11175. * cropped xData with corresponding part of yData, dataMin and dataMax within the cropped range
  11176. */
  11177. cropData: function (xData, yData, min, max) {
  11178. var dataLength = xData.length,
  11179. cropStart = 0,
  11180. cropEnd = dataLength,
  11181. cropShoulder = pick(this.cropShoulder, 1), // line-type series need one point outside
  11182. i;
  11183. // iterate up to find slice start
  11184. for (i = 0; i < dataLength; i++) {
  11185. if (xData[i] >= min) {
  11186. cropStart = mathMax(0, i - cropShoulder);
  11187. break;
  11188. }
  11189. }
  11190. // proceed to find slice end
  11191. for (; i < dataLength; i++) {
  11192. if (xData[i] > max) {
  11193. cropEnd = i + cropShoulder;
  11194. break;
  11195. }
  11196. }
  11197. return {
  11198. xData: xData.slice(cropStart, cropEnd),
  11199. yData: yData.slice(cropStart, cropEnd),
  11200. start: cropStart,
  11201. end: cropEnd
  11202. };
  11203. },
  11204. /**
  11205. * Generate the data point after the data has been processed by cropping away
  11206. * unused points and optionally grouped in Highcharts Stock.
  11207. */
  11208. generatePoints: function () {
  11209. var series = this,
  11210. options = series.options,
  11211. dataOptions = options.data,
  11212. data = series.data,
  11213. dataLength,
  11214. processedXData = series.processedXData,
  11215. processedYData = series.processedYData,
  11216. pointClass = series.pointClass,
  11217. processedDataLength = processedXData.length,
  11218. cropStart = series.cropStart || 0,
  11219. cursor,
  11220. hasGroupedData = series.hasGroupedData,
  11221. point,
  11222. points = [],
  11223. i;
  11224. if (!data && !hasGroupedData) {
  11225. var arr = [];
  11226. arr.length = dataOptions.length;
  11227. data = series.data = arr;
  11228. }
  11229. for (i = 0; i < processedDataLength; i++) {
  11230. cursor = cropStart + i;
  11231. if (!hasGroupedData) {
  11232. if (data[cursor]) {
  11233. point = data[cursor];
  11234. } else if (dataOptions[cursor] !== UNDEFINED) { // #970
  11235. data[cursor] = point = (new pointClass()).init(series, dataOptions[cursor], processedXData[i]);
  11236. }
  11237. points[i] = point;
  11238. } else {
  11239. // splat the y data in case of ohlc data array
  11240. points[i] = (new pointClass()).init(series, [processedXData[i]].concat(splat(processedYData[i])));
  11241. }
  11242. }
  11243. // Hide cropped-away points - this only runs when the number of points is above cropThreshold, or when
  11244. // swithching view from non-grouped data to grouped data (#637)
  11245. if (data && (processedDataLength !== (dataLength = data.length) || hasGroupedData)) {
  11246. for (i = 0; i < dataLength; i++) {
  11247. if (i === cropStart && !hasGroupedData) { // when has grouped data, clear all points
  11248. i += processedDataLength;
  11249. }
  11250. if (data[i]) {
  11251. data[i].destroyElements();
  11252. data[i].plotX = UNDEFINED; // #1003
  11253. }
  11254. }
  11255. }
  11256. series.data = data;
  11257. series.points = points;
  11258. },
  11259. /**
  11260. * Calculate Y extremes for visible data
  11261. */
  11262. getExtremes: function (yData) {
  11263. var xAxis = this.xAxis,
  11264. yAxis = this.yAxis,
  11265. xData = this.processedXData,
  11266. yDataLength,
  11267. activeYData = [],
  11268. activeCounter = 0,
  11269. xExtremes = xAxis.getExtremes(), // #2117, need to compensate for log X axis
  11270. xMin = xExtremes.min,
  11271. xMax = xExtremes.max,
  11272. validValue,
  11273. withinRange,
  11274. dataMin,
  11275. dataMax,
  11276. x,
  11277. y,
  11278. i,
  11279. j;
  11280. yData = yData || this.stackedYData || this.processedYData;
  11281. yDataLength = yData.length;
  11282. for (i = 0; i < yDataLength; i++) {
  11283. x = xData[i];
  11284. y = yData[i];
  11285. // For points within the visible range, including the first point outside the
  11286. // visible range, consider y extremes
  11287. validValue = y !== null && y !== UNDEFINED && (!yAxis.isLog || (y.length || y > 0));
  11288. withinRange = this.getExtremesFromAll || this.cropped || ((xData[i + 1] || x) >= xMin &&
  11289. (xData[i - 1] || x) <= xMax);
  11290. if (validValue && withinRange) {
  11291. j = y.length;
  11292. if (j) { // array, like ohlc or range data
  11293. while (j--) {
  11294. if (y[j] !== null) {
  11295. activeYData[activeCounter++] = y[j];
  11296. }
  11297. }
  11298. } else {
  11299. activeYData[activeCounter++] = y;
  11300. }
  11301. }
  11302. }
  11303. this.dataMin = pick(dataMin, arrayMin(activeYData));
  11304. this.dataMax = pick(dataMax, arrayMax(activeYData));
  11305. },
  11306. /**
  11307. * Translate data points from raw data values to chart specific positioning data
  11308. * needed later in drawPoints, drawGraph and drawTracker.
  11309. */
  11310. translate: function () {
  11311. if (!this.processedXData) { // hidden series
  11312. this.processData();
  11313. }
  11314. this.generatePoints();
  11315. var series = this,
  11316. options = series.options,
  11317. stacking = options.stacking,
  11318. xAxis = series.xAxis,
  11319. categories = xAxis.categories,
  11320. yAxis = series.yAxis,
  11321. points = series.points,
  11322. dataLength = points.length,
  11323. hasModifyValue = !!series.modifyValue,
  11324. i,
  11325. pointPlacement = options.pointPlacement,
  11326. dynamicallyPlaced = pointPlacement === 'between' || isNumber(pointPlacement),
  11327. threshold = options.threshold;
  11328. // Translate each point
  11329. for (i = 0; i < dataLength; i++) {
  11330. var point = points[i],
  11331. xValue = point.x,
  11332. yValue = point.y,
  11333. yBottom = point.low,
  11334. stack = stacking && yAxis.stacks[(series.negStacks && yValue < threshold ? '-' : '') + series.stackKey],
  11335. pointStack,
  11336. stackValues;
  11337. // Discard disallowed y values for log axes
  11338. if (yAxis.isLog && yValue <= 0) {
  11339. point.y = yValue = null;
  11340. }
  11341. // Get the plotX translation
  11342. point.plotX = xAxis.translate(xValue, 0, 0, 0, 1, pointPlacement, this.type === 'flags'); // Math.round fixes #591
  11343. // Calculate the bottom y value for stacked series
  11344. if (stacking && series.visible && stack && stack[xValue]) {
  11345. pointStack = stack[xValue];
  11346. stackValues = pointStack.points[series.index + ',' + i];
  11347. yBottom = stackValues[0];
  11348. yValue = stackValues[1];
  11349. if (yBottom === 0) {
  11350. yBottom = pick(threshold, yAxis.min);
  11351. }
  11352. if (yAxis.isLog && yBottom <= 0) { // #1200, #1232
  11353. yBottom = null;
  11354. }
  11355. point.total = point.stackTotal = pointStack.total;
  11356. point.percentage = pointStack.total && (point.y / pointStack.total * 100);
  11357. point.stackY = yValue;
  11358. // Place the stack label
  11359. pointStack.setOffset(series.pointXOffset || 0, series.barW || 0);
  11360. }
  11361. // Set translated yBottom or remove it
  11362. point.yBottom = defined(yBottom) ?
  11363. yAxis.translate(yBottom, 0, 1, 0, 1) :
  11364. null;
  11365. // general hook, used for Highstock compare mode
  11366. if (hasModifyValue) {
  11367. yValue = series.modifyValue(yValue, point);
  11368. }
  11369. // Set the the plotY value, reset it for redraws
  11370. point.plotY = (typeof yValue === 'number' && yValue !== Infinity) ?
  11371. //mathRound(yAxis.translate(yValue, 0, 1, 0, 1) * 10) / 10 : // Math.round fixes #591
  11372. yAxis.translate(yValue, 0, 1, 0, 1) :
  11373. UNDEFINED;
  11374. // Set client related positions for mouse tracking
  11375. point.clientX = dynamicallyPlaced ? xAxis.translate(xValue, 0, 0, 0, 1) : point.plotX; // #1514
  11376. point.negative = point.y < (threshold || 0);
  11377. // some API data
  11378. point.category = categories && categories[point.x] !== UNDEFINED ?
  11379. categories[point.x] : point.x;
  11380. }
  11381. // now that we have the cropped data, build the segments
  11382. series.getSegments();
  11383. },
  11384. /**
  11385. * Animate in the series
  11386. */
  11387. animate: function (init) {
  11388. var series = this,
  11389. chart = series.chart,
  11390. renderer = chart.renderer,
  11391. clipRect,
  11392. markerClipRect,
  11393. animation = series.options.animation,
  11394. clipBox = series.clipBox || chart.clipBox,
  11395. inverted = chart.inverted,
  11396. sharedClipKey;
  11397. // Animation option is set to true
  11398. if (animation && !isObject(animation)) {
  11399. animation = defaultPlotOptions[series.type].animation;
  11400. }
  11401. sharedClipKey = ['_sharedClip', animation.duration, animation.easing, clipBox.height].join(',');
  11402. // Initialize the animation. Set up the clipping rectangle.
  11403. if (init) {
  11404. // If a clipping rectangle with the same properties is currently present in the chart, use that.
  11405. clipRect = chart[sharedClipKey];
  11406. markerClipRect = chart[sharedClipKey + 'm'];
  11407. if (!clipRect) {
  11408. chart[sharedClipKey] = clipRect = renderer.clipRect(
  11409. extend(clipBox, { width: 0 })
  11410. );
  11411. chart[sharedClipKey + 'm'] = markerClipRect = renderer.clipRect(
  11412. -99, // include the width of the first marker
  11413. inverted ? -chart.plotLeft : -chart.plotTop,
  11414. 99,
  11415. inverted ? chart.chartWidth : chart.chartHeight
  11416. );
  11417. }
  11418. series.group.clip(clipRect);
  11419. series.markerGroup.clip(markerClipRect);
  11420. series.sharedClipKey = sharedClipKey;
  11421. // Run the animation
  11422. } else {
  11423. clipRect = chart[sharedClipKey];
  11424. if (clipRect) {
  11425. clipRect.animate({
  11426. width: chart.plotSizeX
  11427. }, animation);
  11428. }
  11429. if (chart[sharedClipKey + 'm']) {
  11430. chart[sharedClipKey + 'm'].animate({
  11431. width: chart.plotSizeX + 99
  11432. }, animation);
  11433. }
  11434. // Delete this function to allow it only once
  11435. series.animate = null;
  11436. }
  11437. },
  11438. /**
  11439. * This runs after animation to land on the final plot clipping
  11440. */
  11441. afterAnimate: function () {
  11442. var chart = this.chart,
  11443. sharedClipKey = this.sharedClipKey,
  11444. group = this.group,
  11445. clipBox = this.clipBox;
  11446. if (group && this.options.clip !== false) {
  11447. if (!sharedClipKey || !clipBox) {
  11448. group.clip(clipBox ? chart.renderer.clipRect(clipBox) : chart.clipRect);
  11449. }
  11450. this.markerGroup.clip(); // no clip
  11451. }
  11452. fireEvent(this, 'afterAnimate');
  11453. // Remove the shared clipping rectancgle when all series are shown
  11454. setTimeout(function () {
  11455. if (sharedClipKey && chart[sharedClipKey]) {
  11456. if (!clipBox) {
  11457. chart[sharedClipKey] = chart[sharedClipKey].destroy();
  11458. }
  11459. if (chart[sharedClipKey + 'm']) {
  11460. chart[sharedClipKey + 'm'] = chart[sharedClipKey + 'm'].destroy();
  11461. }
  11462. }
  11463. }, 100);
  11464. },
  11465. /**
  11466. * Draw the markers
  11467. */
  11468. drawPoints: function () {
  11469. var series = this,
  11470. pointAttr,
  11471. points = series.points,
  11472. chart = series.chart,
  11473. plotX,
  11474. plotY,
  11475. i,
  11476. point,
  11477. radius,
  11478. symbol,
  11479. isImage,
  11480. graphic,
  11481. options = series.options,
  11482. seriesMarkerOptions = options.marker,
  11483. seriesPointAttr = series.pointAttr[''],
  11484. pointMarkerOptions,
  11485. enabled,
  11486. isInside,
  11487. markerGroup = series.markerGroup,
  11488. globallyEnabled = pick(
  11489. seriesMarkerOptions.enabled,
  11490. series.activePointCount < (0.5 * series.xAxis.len / seriesMarkerOptions.radius)
  11491. );
  11492. if (seriesMarkerOptions.enabled !== false || series._hasPointMarkers) {
  11493. i = points.length;
  11494. while (i--) {
  11495. point = points[i];
  11496. plotX = mathFloor(point.plotX); // #1843
  11497. plotY = point.plotY;
  11498. graphic = point.graphic;
  11499. pointMarkerOptions = point.marker || {};
  11500. enabled = (globallyEnabled && pointMarkerOptions.enabled === UNDEFINED) || pointMarkerOptions.enabled;
  11501. isInside = chart.isInsidePlot(mathRound(plotX), plotY, chart.inverted); // #1858
  11502. // only draw the point if y is defined
  11503. if (enabled && plotY !== UNDEFINED && !isNaN(plotY) && point.y !== null) {
  11504. // shortcuts
  11505. pointAttr = point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE] || seriesPointAttr;
  11506. radius = pointAttr.r;
  11507. symbol = pick(pointMarkerOptions.symbol, series.symbol);
  11508. isImage = symbol.indexOf('url') === 0;
  11509. if (graphic) { // update
  11510. graphic[isInside ? 'show' : 'hide'](true) // Since the marker group isn't clipped, each individual marker must be toggled
  11511. .animate(extend({
  11512. x: plotX - radius,
  11513. y: plotY - radius
  11514. }, graphic.symbolName ? { // don't apply to image symbols #507
  11515. width: 2 * radius,
  11516. height: 2 * radius
  11517. } : {}));
  11518. } else if (isInside && (radius > 0 || isImage)) {
  11519. point.graphic = graphic = chart.renderer.symbol(
  11520. symbol,
  11521. plotX - radius,
  11522. plotY - radius,
  11523. 2 * radius,
  11524. 2 * radius
  11525. )
  11526. .attr(pointAttr)
  11527. .add(markerGroup);
  11528. }
  11529. } else if (graphic) {
  11530. point.graphic = graphic.destroy(); // #1269
  11531. }
  11532. }
  11533. }
  11534. },
  11535. /**
  11536. * Convert state properties from API naming conventions to SVG attributes
  11537. *
  11538. * @param {Object} options API options object
  11539. * @param {Object} base1 SVG attribute object to inherit from
  11540. * @param {Object} base2 Second level SVG attribute object to inherit from
  11541. */
  11542. convertAttribs: function (options, base1, base2, base3) {
  11543. var conversion = this.pointAttrToOptions,
  11544. attr,
  11545. option,
  11546. obj = {};
  11547. options = options || {};
  11548. base1 = base1 || {};
  11549. base2 = base2 || {};
  11550. base3 = base3 || {};
  11551. for (attr in conversion) {
  11552. option = conversion[attr];
  11553. obj[attr] = pick(options[option], base1[attr], base2[attr], base3[attr]);
  11554. }
  11555. return obj;
  11556. },
  11557. /**
  11558. * Get the state attributes. Each series type has its own set of attributes
  11559. * that are allowed to change on a point's state change. Series wide attributes are stored for
  11560. * all series, and additionally point specific attributes are stored for all
  11561. * points with individual marker options. If such options are not defined for the point,
  11562. * a reference to the series wide attributes is stored in point.pointAttr.
  11563. */
  11564. getAttribs: function () {
  11565. var series = this,
  11566. seriesOptions = series.options,
  11567. normalOptions = defaultPlotOptions[series.type].marker ? seriesOptions.marker : seriesOptions,
  11568. stateOptions = normalOptions.states,
  11569. stateOptionsHover = stateOptions[HOVER_STATE],
  11570. pointStateOptionsHover,
  11571. seriesColor = series.color,
  11572. normalDefaults = {
  11573. stroke: seriesColor,
  11574. fill: seriesColor
  11575. },
  11576. points = series.points || [], // #927
  11577. i,
  11578. point,
  11579. seriesPointAttr = [],
  11580. pointAttr,
  11581. pointAttrToOptions = series.pointAttrToOptions,
  11582. hasPointSpecificOptions = series.hasPointSpecificOptions,
  11583. negativeColor = seriesOptions.negativeColor,
  11584. defaultLineColor = normalOptions.lineColor,
  11585. defaultFillColor = normalOptions.fillColor,
  11586. turboThreshold = seriesOptions.turboThreshold,
  11587. attr,
  11588. key;
  11589. // series type specific modifications
  11590. if (seriesOptions.marker) { // line, spline, area, areaspline, scatter
  11591. // if no hover radius is given, default to normal radius + 2
  11592. stateOptionsHover.radius = stateOptionsHover.radius || normalOptions.radius + stateOptionsHover.radiusPlus;
  11593. stateOptionsHover.lineWidth = stateOptionsHover.lineWidth || normalOptions.lineWidth + stateOptionsHover.lineWidthPlus;
  11594. } else { // column, bar, pie
  11595. // if no hover color is given, brighten the normal color
  11596. stateOptionsHover.color = stateOptionsHover.color ||
  11597. Color(stateOptionsHover.color || seriesColor)
  11598. .brighten(stateOptionsHover.brightness).get();
  11599. }
  11600. // general point attributes for the series normal state
  11601. seriesPointAttr[NORMAL_STATE] = series.convertAttribs(normalOptions, normalDefaults);
  11602. // HOVER_STATE and SELECT_STATE states inherit from normal state except the default radius
  11603. each([HOVER_STATE, SELECT_STATE], function (state) {
  11604. seriesPointAttr[state] =
  11605. series.convertAttribs(stateOptions[state], seriesPointAttr[NORMAL_STATE]);
  11606. });
  11607. // set it
  11608. series.pointAttr = seriesPointAttr;
  11609. // Generate the point-specific attribute collections if specific point
  11610. // options are given. If not, create a referance to the series wide point
  11611. // attributes
  11612. i = points.length;
  11613. if (!turboThreshold || i < turboThreshold || hasPointSpecificOptions) {
  11614. while (i--) {
  11615. point = points[i];
  11616. normalOptions = (point.options && point.options.marker) || point.options;
  11617. if (normalOptions && normalOptions.enabled === false) {
  11618. normalOptions.radius = 0;
  11619. }
  11620. if (point.negative && negativeColor) {
  11621. point.color = point.fillColor = negativeColor;
  11622. }
  11623. hasPointSpecificOptions = seriesOptions.colorByPoint || point.color; // #868
  11624. // check if the point has specific visual options
  11625. if (point.options) {
  11626. for (key in pointAttrToOptions) {
  11627. if (defined(normalOptions[pointAttrToOptions[key]])) {
  11628. hasPointSpecificOptions = true;
  11629. }
  11630. }
  11631. }
  11632. // a specific marker config object is defined for the individual point:
  11633. // create it's own attribute collection
  11634. if (hasPointSpecificOptions) {
  11635. normalOptions = normalOptions || {};
  11636. pointAttr = [];
  11637. stateOptions = normalOptions.states || {}; // reassign for individual point
  11638. pointStateOptionsHover = stateOptions[HOVER_STATE] = stateOptions[HOVER_STATE] || {};
  11639. // Handle colors for column and pies
  11640. if (!seriesOptions.marker) { // column, bar, point
  11641. // If no hover color is given, brighten the normal color. #1619, #2579
  11642. pointStateOptionsHover.color = pointStateOptionsHover.color || (!point.options.color && stateOptionsHover.color) ||
  11643. Color(point.color)
  11644. .brighten(pointStateOptionsHover.brightness || stateOptionsHover.brightness)
  11645. .get();
  11646. }
  11647. // normal point state inherits series wide normal state
  11648. attr = { color: point.color }; // #868
  11649. if (!defaultFillColor) { // Individual point color or negative color markers (#2219)
  11650. attr.fillColor = point.color;
  11651. }
  11652. if (!defaultLineColor) {
  11653. attr.lineColor = point.color; // Bubbles take point color, line markers use white
  11654. }
  11655. pointAttr[NORMAL_STATE] = series.convertAttribs(extend(attr, normalOptions), seriesPointAttr[NORMAL_STATE]);
  11656. // inherit from point normal and series hover
  11657. pointAttr[HOVER_STATE] = series.convertAttribs(
  11658. stateOptions[HOVER_STATE],
  11659. seriesPointAttr[HOVER_STATE],
  11660. pointAttr[NORMAL_STATE]
  11661. );
  11662. // inherit from point normal and series hover
  11663. pointAttr[SELECT_STATE] = series.convertAttribs(
  11664. stateOptions[SELECT_STATE],
  11665. seriesPointAttr[SELECT_STATE],
  11666. pointAttr[NORMAL_STATE]
  11667. );
  11668. // no marker config object is created: copy a reference to the series-wide
  11669. // attribute collection
  11670. } else {
  11671. pointAttr = seriesPointAttr;
  11672. }
  11673. point.pointAttr = pointAttr;
  11674. }
  11675. }
  11676. },
  11677. /**
  11678. * Clear DOM objects and free up memory
  11679. */
  11680. destroy: function () {
  11681. var series = this,
  11682. chart = series.chart,
  11683. issue134 = /AppleWebKit\/533/.test(userAgent),
  11684. destroy,
  11685. i,
  11686. data = series.data || [],
  11687. point,
  11688. prop,
  11689. axis;
  11690. // add event hook
  11691. fireEvent(series, 'destroy');
  11692. // remove all events
  11693. removeEvent(series);
  11694. // erase from axes
  11695. each(series.axisTypes || [], function (AXIS) {
  11696. axis = series[AXIS];
  11697. if (axis) {
  11698. erase(axis.series, series);
  11699. axis.isDirty = axis.forceRedraw = true;
  11700. }
  11701. });
  11702. // remove legend items
  11703. if (series.legendItem) {
  11704. series.chart.legend.destroyItem(series);
  11705. }
  11706. // destroy all points with their elements
  11707. i = data.length;
  11708. while (i--) {
  11709. point = data[i];
  11710. if (point && point.destroy) {
  11711. point.destroy();
  11712. }
  11713. }
  11714. series.points = null;
  11715. // Clear the animation timeout if we are destroying the series during initial animation
  11716. clearTimeout(series.animationTimeout);
  11717. // destroy all SVGElements associated to the series
  11718. each(['area', 'graph', 'dataLabelsGroup', 'group', 'markerGroup', 'tracker',
  11719. 'graphNeg', 'areaNeg', 'posClip', 'negClip'], function (prop) {
  11720. if (series[prop]) {
  11721. // issue 134 workaround
  11722. destroy = issue134 && prop === 'group' ?
  11723. 'hide' :
  11724. 'destroy';
  11725. series[prop][destroy]();
  11726. }
  11727. });
  11728. // remove from hoverSeries
  11729. if (chart.hoverSeries === series) {
  11730. chart.hoverSeries = null;
  11731. }
  11732. erase(chart.series, series);
  11733. // clear all members
  11734. for (prop in series) {
  11735. delete series[prop];
  11736. }
  11737. },
  11738. /**
  11739. * Return the graph path of a segment
  11740. */
  11741. getSegmentPath: function (segment) {
  11742. var series = this,
  11743. segmentPath = [],
  11744. step = series.options.step;
  11745. // build the segment line
  11746. each(segment, function (point, i) {
  11747. var plotX = point.plotX,
  11748. plotY = point.plotY,
  11749. lastPoint;
  11750. if (series.getPointSpline) { // generate the spline as defined in the SplineSeries object
  11751. segmentPath.push.apply(segmentPath, series.getPointSpline(segment, point, i));
  11752. } else {
  11753. // moveTo or lineTo
  11754. segmentPath.push(i ? L : M);
  11755. // step line?
  11756. if (step && i) {
  11757. lastPoint = segment[i - 1];
  11758. if (step === 'right') {
  11759. segmentPath.push(
  11760. lastPoint.plotX,
  11761. plotY
  11762. );
  11763. } else if (step === 'center') {
  11764. segmentPath.push(
  11765. (lastPoint.plotX + plotX) / 2,
  11766. lastPoint.plotY,
  11767. (lastPoint.plotX + plotX) / 2,
  11768. plotY
  11769. );
  11770. } else {
  11771. segmentPath.push(
  11772. plotX,
  11773. lastPoint.plotY
  11774. );
  11775. }
  11776. }
  11777. // normal line to next point
  11778. segmentPath.push(
  11779. point.plotX,
  11780. point.plotY
  11781. );
  11782. }
  11783. });
  11784. return segmentPath;
  11785. },
  11786. /**
  11787. * Get the graph path
  11788. */
  11789. getGraphPath: function () {
  11790. var series = this,
  11791. graphPath = [],
  11792. segmentPath,
  11793. singlePoints = []; // used in drawTracker
  11794. // Divide into segments and build graph and area paths
  11795. each(series.segments, function (segment) {
  11796. segmentPath = series.getSegmentPath(segment);
  11797. // add the segment to the graph, or a single point for tracking
  11798. if (segment.length > 1) {
  11799. graphPath = graphPath.concat(segmentPath);
  11800. } else {
  11801. singlePoints.push(segment[0]);
  11802. }
  11803. });
  11804. // Record it for use in drawGraph and drawTracker, and return graphPath
  11805. series.singlePoints = singlePoints;
  11806. series.graphPath = graphPath;
  11807. return graphPath;
  11808. },
  11809. /**
  11810. * Draw the actual graph
  11811. */
  11812. drawGraph: function () {
  11813. var series = this,
  11814. options = this.options,
  11815. props = [['graph', options.lineColor || this.color]],
  11816. lineWidth = options.lineWidth,
  11817. dashStyle = options.dashStyle,
  11818. roundCap = options.linecap !== 'square',
  11819. graphPath = this.getGraphPath(),
  11820. negativeColor = options.negativeColor;
  11821. if (negativeColor) {
  11822. props.push(['graphNeg', negativeColor]);
  11823. }
  11824. // draw the graph
  11825. each(props, function (prop, i) {
  11826. var graphKey = prop[0],
  11827. graph = series[graphKey],
  11828. attribs;
  11829. if (graph) {
  11830. stop(graph); // cancel running animations, #459
  11831. graph.animate({ d: graphPath });
  11832. } else if (lineWidth && graphPath.length) { // #1487
  11833. attribs = {
  11834. stroke: prop[1],
  11835. 'stroke-width': lineWidth,
  11836. fill: NONE,
  11837. zIndex: 1 // #1069
  11838. };
  11839. if (dashStyle) {
  11840. attribs.dashstyle = dashStyle;
  11841. } else if (roundCap) {
  11842. attribs['stroke-linecap'] = attribs['stroke-linejoin'] = 'round';
  11843. }
  11844. series[graphKey] = series.chart.renderer.path(graphPath)
  11845. .attr(attribs)
  11846. .add(series.group)
  11847. .shadow(!i && options.shadow);
  11848. }
  11849. });
  11850. },
  11851. /**
  11852. * Clip the graphs into the positive and negative coloured graphs
  11853. */
  11854. clipNeg: function () {
  11855. var options = this.options,
  11856. chart = this.chart,
  11857. renderer = chart.renderer,
  11858. negativeColor = options.negativeColor || options.negativeFillColor,
  11859. translatedThreshold,
  11860. posAttr,
  11861. negAttr,
  11862. graph = this.graph,
  11863. area = this.area,
  11864. posClip = this.posClip,
  11865. negClip = this.negClip,
  11866. chartWidth = chart.chartWidth,
  11867. chartHeight = chart.chartHeight,
  11868. chartSizeMax = mathMax(chartWidth, chartHeight),
  11869. yAxis = this.yAxis,
  11870. above,
  11871. below;
  11872. if (negativeColor && (graph || area)) {
  11873. translatedThreshold = mathRound(yAxis.toPixels(options.threshold || 0, true));
  11874. if (translatedThreshold < 0) {
  11875. chartSizeMax -= translatedThreshold; // #2534
  11876. }
  11877. above = {
  11878. x: 0,
  11879. y: 0,
  11880. width: chartSizeMax,
  11881. height: translatedThreshold
  11882. };
  11883. below = {
  11884. x: 0,
  11885. y: translatedThreshold,
  11886. width: chartSizeMax,
  11887. height: chartSizeMax
  11888. };
  11889. if (chart.inverted) {
  11890. above.height = below.y = chart.plotWidth - translatedThreshold;
  11891. if (renderer.isVML) {
  11892. above = {
  11893. x: chart.plotWidth - translatedThreshold - chart.plotLeft,
  11894. y: 0,
  11895. width: chartWidth,
  11896. height: chartHeight
  11897. };
  11898. below = {
  11899. x: translatedThreshold + chart.plotLeft - chartWidth,
  11900. y: 0,
  11901. width: chart.plotLeft + translatedThreshold,
  11902. height: chartWidth
  11903. };
  11904. }
  11905. }
  11906. if (yAxis.reversed) {
  11907. posAttr = below;
  11908. negAttr = above;
  11909. } else {
  11910. posAttr = above;
  11911. negAttr = below;
  11912. }
  11913. if (posClip) { // update
  11914. posClip.animate(posAttr);
  11915. negClip.animate(negAttr);
  11916. } else {
  11917. this.posClip = posClip = renderer.clipRect(posAttr);
  11918. this.negClip = negClip = renderer.clipRect(negAttr);
  11919. if (graph && this.graphNeg) {
  11920. graph.clip(posClip);
  11921. this.graphNeg.clip(negClip);
  11922. }
  11923. if (area) {
  11924. area.clip(posClip);
  11925. this.areaNeg.clip(negClip);
  11926. }
  11927. }
  11928. }
  11929. },
  11930. /**
  11931. * Initialize and perform group inversion on series.group and series.markerGroup
  11932. */
  11933. invertGroups: function () {
  11934. var series = this,
  11935. chart = series.chart;
  11936. // Pie, go away (#1736)
  11937. if (!series.xAxis) {
  11938. return;
  11939. }
  11940. // A fixed size is needed for inversion to work
  11941. function setInvert() {
  11942. var size = {
  11943. width: series.yAxis.len,
  11944. height: series.xAxis.len
  11945. };
  11946. each(['group', 'markerGroup'], function (groupName) {
  11947. if (series[groupName]) {
  11948. series[groupName].attr(size).invert();
  11949. }
  11950. });
  11951. }
  11952. addEvent(chart, 'resize', setInvert); // do it on resize
  11953. addEvent(series, 'destroy', function () {
  11954. removeEvent(chart, 'resize', setInvert);
  11955. });
  11956. // Do it now
  11957. setInvert(); // do it now
  11958. // On subsequent render and redraw, just do setInvert without setting up events again
  11959. series.invertGroups = setInvert;
  11960. },
  11961. /**
  11962. * General abstraction for creating plot groups like series.group, series.dataLabelsGroup and
  11963. * series.markerGroup. On subsequent calls, the group will only be adjusted to the updated plot size.
  11964. */
  11965. plotGroup: function (prop, name, visibility, zIndex, parent) {
  11966. var group = this[prop],
  11967. isNew = !group;
  11968. // Generate it on first call
  11969. if (isNew) {
  11970. this[prop] = group = this.chart.renderer.g(name)
  11971. .attr({
  11972. visibility: visibility,
  11973. zIndex: zIndex || 0.1 // IE8 needs this
  11974. })
  11975. .add(parent);
  11976. }
  11977. // Place it on first and subsequent (redraw) calls
  11978. group[isNew ? 'attr' : 'animate'](this.getPlotBox());
  11979. return group;
  11980. },
  11981. /**
  11982. * Get the translation and scale for the plot area of this series
  11983. */
  11984. getPlotBox: function () {
  11985. var chart = this.chart,
  11986. xAxis = this.xAxis,
  11987. yAxis = this.yAxis;
  11988. // Swap axes for inverted (#2339)
  11989. if (chart.inverted) {
  11990. xAxis = yAxis;
  11991. yAxis = this.xAxis;
  11992. }
  11993. return {
  11994. translateX: xAxis ? xAxis.left : chart.plotLeft,
  11995. translateY: yAxis ? yAxis.top : chart.plotTop,
  11996. scaleX: 1, // #1623
  11997. scaleY: 1
  11998. };
  11999. },
  12000. /**
  12001. * Render the graph and markers
  12002. */
  12003. render: function () {
  12004. var series = this,
  12005. chart = series.chart,
  12006. group,
  12007. options = series.options,
  12008. animation = options.animation,
  12009. // Animation doesn't work in IE8 quirks when the group div is hidden,
  12010. // and looks bad in other oldIE
  12011. animDuration = (animation && !!series.animate && chart.renderer.isSVG && pick(animation.duration, 500)) || 0,
  12012. visibility = series.visible ? VISIBLE : HIDDEN,
  12013. zIndex = options.zIndex,
  12014. hasRendered = series.hasRendered,
  12015. chartSeriesGroup = chart.seriesGroup;
  12016. // the group
  12017. group = series.plotGroup(
  12018. 'group',
  12019. 'series',
  12020. visibility,
  12021. zIndex,
  12022. chartSeriesGroup
  12023. );
  12024. series.markerGroup = series.plotGroup(
  12025. 'markerGroup',
  12026. 'markers',
  12027. visibility,
  12028. zIndex,
  12029. chartSeriesGroup
  12030. );
  12031. // initiate the animation
  12032. if (animDuration) {
  12033. series.animate(true);
  12034. }
  12035. // cache attributes for shapes
  12036. series.getAttribs();
  12037. // SVGRenderer needs to know this before drawing elements (#1089, #1795)
  12038. group.inverted = series.isCartesian ? chart.inverted : false;
  12039. // draw the graph if any
  12040. if (series.drawGraph) {
  12041. series.drawGraph();
  12042. series.clipNeg();
  12043. }
  12044. // draw the data labels (inn pies they go before the points)
  12045. if (series.drawDataLabels) {
  12046. series.drawDataLabels();
  12047. }
  12048. // draw the points
  12049. if (series.visible) {
  12050. series.drawPoints();
  12051. }
  12052. // draw the mouse tracking area
  12053. if (series.drawTracker && series.options.enableMouseTracking !== false) {
  12054. series.drawTracker();
  12055. }
  12056. // Handle inverted series and tracker groups
  12057. if (chart.inverted) {
  12058. series.invertGroups();
  12059. }
  12060. // Initial clipping, must be defined after inverting groups for VML
  12061. if (options.clip !== false && !series.sharedClipKey && !hasRendered) {
  12062. group.clip(chart.clipRect);
  12063. }
  12064. // Run the animation
  12065. if (animDuration) {
  12066. series.animate();
  12067. }
  12068. // Call the afterAnimate function on animation complete (but don't overwrite the animation.complete option
  12069. // which should be available to the user).
  12070. if (!hasRendered) {
  12071. if (animDuration) {
  12072. series.animationTimeout = setTimeout(function () {
  12073. series.afterAnimate();
  12074. }, animDuration);
  12075. } else {
  12076. series.afterAnimate();
  12077. }
  12078. }
  12079. series.isDirty = series.isDirtyData = false; // means data is in accordance with what you see
  12080. // (See #322) series.isDirty = series.isDirtyData = false; // means data is in accordance with what you see
  12081. series.hasRendered = true;
  12082. },
  12083. /**
  12084. * Redraw the series after an update in the axes.
  12085. */
  12086. redraw: function () {
  12087. var series = this,
  12088. chart = series.chart,
  12089. wasDirtyData = series.isDirtyData, // cache it here as it is set to false in render, but used after
  12090. group = series.group,
  12091. xAxis = series.xAxis,
  12092. yAxis = series.yAxis;
  12093. // reposition on resize
  12094. if (group) {
  12095. if (chart.inverted) {
  12096. group.attr({
  12097. width: chart.plotWidth,
  12098. height: chart.plotHeight
  12099. });
  12100. }
  12101. group.animate({
  12102. translateX: pick(xAxis && xAxis.left, chart.plotLeft),
  12103. translateY: pick(yAxis && yAxis.top, chart.plotTop)
  12104. });
  12105. }
  12106. series.translate();
  12107. if (series.setTooltipPoints) {
  12108. series.setTooltipPoints(true);
  12109. }
  12110. series.render();
  12111. if (wasDirtyData) {
  12112. fireEvent(series, 'updatedData');
  12113. }
  12114. }
  12115. }; // end Series prototype
  12116. /**
  12117. * The class for stack items
  12118. */
  12119. function StackItem(axis, options, isNegative, x, stackOption) {
  12120. var inverted = axis.chart.inverted;
  12121. this.axis = axis;
  12122. // Tells if the stack is negative
  12123. this.isNegative = isNegative;
  12124. // Save the options to be able to style the label
  12125. this.options = options;
  12126. // Save the x value to be able to position the label later
  12127. this.x = x;
  12128. // Initialize total value
  12129. this.total = null;
  12130. // This will keep each points' extremes stored by series.index and point index
  12131. this.points = {};
  12132. // Save the stack option on the series configuration object, and whether to treat it as percent
  12133. this.stack = stackOption;
  12134. // The align options and text align varies on whether the stack is negative and
  12135. // if the chart is inverted or not.
  12136. // First test the user supplied value, then use the dynamic.
  12137. this.alignOptions = {
  12138. align: options.align || (inverted ? (isNegative ? 'left' : 'right') : 'center'),
  12139. verticalAlign: options.verticalAlign || (inverted ? 'middle' : (isNegative ? 'bottom' : 'top')),
  12140. y: pick(options.y, inverted ? 4 : (isNegative ? 14 : -6)),
  12141. x: pick(options.x, inverted ? (isNegative ? -6 : 6) : 0)
  12142. };
  12143. this.textAlign = options.textAlign || (inverted ? (isNegative ? 'right' : 'left') : 'center');
  12144. }
  12145. StackItem.prototype = {
  12146. destroy: function () {
  12147. destroyObjectProperties(this, this.axis);
  12148. },
  12149. /**
  12150. * Renders the stack total label and adds it to the stack label group.
  12151. */
  12152. render: function (group) {
  12153. var options = this.options,
  12154. formatOption = options.format,
  12155. str = formatOption ?
  12156. format(formatOption, this) :
  12157. options.formatter.call(this); // format the text in the label
  12158. // Change the text to reflect the new total and set visibility to hidden in case the serie is hidden
  12159. if (this.label) {
  12160. this.label.attr({text: str, visibility: HIDDEN});
  12161. // Create new label
  12162. } else {
  12163. this.label =
  12164. this.axis.chart.renderer.text(str, null, null, options.useHTML) // dummy positions, actual position updated with setOffset method in columnseries
  12165. .css(options.style) // apply style
  12166. .attr({
  12167. align: this.textAlign, // fix the text-anchor
  12168. rotation: options.rotation, // rotation
  12169. visibility: HIDDEN // hidden until setOffset is called
  12170. })
  12171. .add(group); // add to the labels-group
  12172. }
  12173. },
  12174. /**
  12175. * Sets the offset that the stack has from the x value and repositions the label.
  12176. */
  12177. setOffset: function (xOffset, xWidth) {
  12178. var stackItem = this,
  12179. axis = stackItem.axis,
  12180. chart = axis.chart,
  12181. inverted = chart.inverted,
  12182. neg = this.isNegative, // special treatment is needed for negative stacks
  12183. y = axis.translate(axis.usePercentage ? 100 : this.total, 0, 0, 0, 1), // stack value translated mapped to chart coordinates
  12184. yZero = axis.translate(0), // stack origin
  12185. h = mathAbs(y - yZero), // stack height
  12186. x = chart.xAxis[0].translate(this.x) + xOffset, // stack x position
  12187. plotHeight = chart.plotHeight,
  12188. stackBox = { // this is the box for the complete stack
  12189. x: inverted ? (neg ? y : y - h) : x,
  12190. y: inverted ? plotHeight - x - xWidth : (neg ? (plotHeight - y - h) : plotHeight - y),
  12191. width: inverted ? h : xWidth,
  12192. height: inverted ? xWidth : h
  12193. },
  12194. label = this.label,
  12195. alignAttr;
  12196. if (label) {
  12197. label.align(this.alignOptions, null, stackBox); // align the label to the box
  12198. // Set visibility (#678)
  12199. alignAttr = label.alignAttr;
  12200. label[this.options.crop === false || chart.isInsidePlot(alignAttr.x, alignAttr.y) ? 'show' : 'hide'](true);
  12201. }
  12202. }
  12203. };
  12204. // Stacking methods defined on the Axis prototype
  12205. /**
  12206. * Build the stacks from top down
  12207. */
  12208. Axis.prototype.buildStacks = function () {
  12209. var series = this.series,
  12210. reversedStacks = pick(this.options.reversedStacks, true),
  12211. i = series.length;
  12212. if (!this.isXAxis) {
  12213. this.usePercentage = false;
  12214. while (i--) {
  12215. series[reversedStacks ? i : series.length - i - 1].setStackedPoints();
  12216. }
  12217. // Loop up again to compute percent stack
  12218. if (this.usePercentage) {
  12219. for (i = 0; i < series.length; i++) {
  12220. series[i].setPercentStacks();
  12221. }
  12222. }
  12223. }
  12224. };
  12225. Axis.prototype.renderStackTotals = function () {
  12226. var axis = this,
  12227. chart = axis.chart,
  12228. renderer = chart.renderer,
  12229. stacks = axis.stacks,
  12230. stackKey,
  12231. oneStack,
  12232. stackCategory,
  12233. stackTotalGroup = axis.stackTotalGroup;
  12234. // Create a separate group for the stack total labels
  12235. if (!stackTotalGroup) {
  12236. axis.stackTotalGroup = stackTotalGroup =
  12237. renderer.g('stack-labels')
  12238. .attr({
  12239. visibility: VISIBLE,
  12240. zIndex: 6
  12241. })
  12242. .add();
  12243. }
  12244. // plotLeft/Top will change when y axis gets wider so we need to translate the
  12245. // stackTotalGroup at every render call. See bug #506 and #516
  12246. stackTotalGroup.translate(chart.plotLeft, chart.plotTop);
  12247. // Render each stack total
  12248. for (stackKey in stacks) {
  12249. oneStack = stacks[stackKey];
  12250. for (stackCategory in oneStack) {
  12251. oneStack[stackCategory].render(stackTotalGroup);
  12252. }
  12253. }
  12254. };
  12255. // Stacking methods defnied for Series prototype
  12256. /**
  12257. * Adds series' points value to corresponding stack
  12258. */
  12259. Series.prototype.setStackedPoints = function () {
  12260. if (!this.options.stacking || (this.visible !== true && this.chart.options.chart.ignoreHiddenSeries !== false)) {
  12261. return;
  12262. }
  12263. var series = this,
  12264. xData = series.processedXData,
  12265. yData = series.processedYData,
  12266. stackedYData = [],
  12267. yDataLength = yData.length,
  12268. seriesOptions = series.options,
  12269. threshold = seriesOptions.threshold,
  12270. stackOption = seriesOptions.stack,
  12271. stacking = seriesOptions.stacking,
  12272. stackKey = series.stackKey,
  12273. negKey = '-' + stackKey,
  12274. negStacks = series.negStacks,
  12275. yAxis = series.yAxis,
  12276. stacks = yAxis.stacks,
  12277. oldStacks = yAxis.oldStacks,
  12278. isNegative,
  12279. stack,
  12280. other,
  12281. key,
  12282. pointKey,
  12283. i,
  12284. x,
  12285. y;
  12286. // loop over the non-null y values and read them into a local array
  12287. for (i = 0; i < yDataLength; i++) {
  12288. x = xData[i];
  12289. y = yData[i];
  12290. pointKey = series.index + ',' + i;
  12291. // Read stacked values into a stack based on the x value,
  12292. // the sign of y and the stack key. Stacking is also handled for null values (#739)
  12293. isNegative = negStacks && y < threshold;
  12294. key = isNegative ? negKey : stackKey;
  12295. // Create empty object for this stack if it doesn't exist yet
  12296. if (!stacks[key]) {
  12297. stacks[key] = {};
  12298. }
  12299. // Initialize StackItem for this x
  12300. if (!stacks[key][x]) {
  12301. if (oldStacks[key] && oldStacks[key][x]) {
  12302. stacks[key][x] = oldStacks[key][x];
  12303. stacks[key][x].total = null;
  12304. } else {
  12305. stacks[key][x] = new StackItem(yAxis, yAxis.options.stackLabels, isNegative, x, stackOption);
  12306. }
  12307. }
  12308. // If the StackItem doesn't exist, create it first
  12309. stack = stacks[key][x];
  12310. stack.points[pointKey] = [stack.cum || 0];
  12311. // Add value to the stack total
  12312. if (stacking === 'percent') {
  12313. // Percent stacked column, totals are the same for the positive and negative stacks
  12314. other = isNegative ? stackKey : negKey;
  12315. if (negStacks && stacks[other] && stacks[other][x]) {
  12316. other = stacks[other][x];
  12317. stack.total = other.total = mathMax(other.total, stack.total) + mathAbs(y) || 0;
  12318. // Percent stacked areas
  12319. } else {
  12320. stack.total = correctFloat(stack.total + (mathAbs(y) || 0));
  12321. }
  12322. } else {
  12323. stack.total = correctFloat(stack.total + (y || 0));
  12324. }
  12325. stack.cum = (stack.cum || 0) + (y || 0);
  12326. stack.points[pointKey].push(stack.cum);
  12327. stackedYData[i] = stack.cum;
  12328. }
  12329. if (stacking === 'percent') {
  12330. yAxis.usePercentage = true;
  12331. }
  12332. this.stackedYData = stackedYData; // To be used in getExtremes
  12333. // Reset old stacks
  12334. yAxis.oldStacks = {};
  12335. };
  12336. /**
  12337. * Iterate over all stacks and compute the absolute values to percent
  12338. */
  12339. Series.prototype.setPercentStacks = function () {
  12340. var series = this,
  12341. stackKey = series.stackKey,
  12342. stacks = series.yAxis.stacks,
  12343. processedXData = series.processedXData;
  12344. each([stackKey, '-' + stackKey], function (key) {
  12345. var i = processedXData.length,
  12346. x,
  12347. stack,
  12348. pointExtremes,
  12349. totalFactor;
  12350. while (i--) {
  12351. x = processedXData[i];
  12352. stack = stacks[key] && stacks[key][x];
  12353. pointExtremes = stack && stack.points[series.index + ',' + i];
  12354. if (pointExtremes) {
  12355. totalFactor = stack.total ? 100 / stack.total : 0;
  12356. pointExtremes[0] = correctFloat(pointExtremes[0] * totalFactor); // Y bottom value
  12357. pointExtremes[1] = correctFloat(pointExtremes[1] * totalFactor); // Y value
  12358. series.stackedYData[i] = pointExtremes[1];
  12359. }
  12360. }
  12361. });
  12362. };
  12363. // Extend the Chart prototype for dynamic methods
  12364. extend(Chart.prototype, {
  12365. /**
  12366. * Add a series dynamically after time
  12367. *
  12368. * @param {Object} options The config options
  12369. * @param {Boolean} redraw Whether to redraw the chart after adding. Defaults to true.
  12370. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  12371. * configuration
  12372. *
  12373. * @return {Object} series The newly created series object
  12374. */
  12375. addSeries: function (options, redraw, animation) {
  12376. var series,
  12377. chart = this;
  12378. if (options) {
  12379. redraw = pick(redraw, true); // defaults to true
  12380. fireEvent(chart, 'addSeries', { options: options }, function () {
  12381. series = chart.initSeries(options);
  12382. chart.isDirtyLegend = true; // the series array is out of sync with the display
  12383. chart.linkSeries();
  12384. if (redraw) {
  12385. chart.redraw(animation);
  12386. }
  12387. });
  12388. }
  12389. return series;
  12390. },
  12391. /**
  12392. * Add an axis to the chart
  12393. * @param {Object} options The axis option
  12394. * @param {Boolean} isX Whether it is an X axis or a value axis
  12395. */
  12396. addAxis: function (options, isX, redraw, animation) {
  12397. var key = isX ? 'xAxis' : 'yAxis',
  12398. chartOptions = this.options,
  12399. axis;
  12400. /*jslint unused: false*/
  12401. axis = new Axis(this, merge(options, {
  12402. index: this[key].length,
  12403. isX: isX
  12404. }));
  12405. /*jslint unused: true*/
  12406. // Push the new axis options to the chart options
  12407. chartOptions[key] = splat(chartOptions[key] || {});
  12408. chartOptions[key].push(options);
  12409. if (pick(redraw, true)) {
  12410. this.redraw(animation);
  12411. }
  12412. },
  12413. /**
  12414. * Dim the chart and show a loading text or symbol
  12415. * @param {String} str An optional text to show in the loading label instead of the default one
  12416. */
  12417. showLoading: function (str) {
  12418. var chart = this,
  12419. options = chart.options,
  12420. loadingDiv = chart.loadingDiv,
  12421. loadingOptions = options.loading,
  12422. setLoadingSize = function () {
  12423. if (loadingDiv) {
  12424. css(loadingDiv, {
  12425. left: chart.plotLeft + PX,
  12426. top: chart.plotTop + PX,
  12427. width: chart.plotWidth + PX,
  12428. height: chart.plotHeight + PX
  12429. });
  12430. }
  12431. };
  12432. // create the layer at the first call
  12433. if (!loadingDiv) {
  12434. chart.loadingDiv = loadingDiv = createElement(DIV, {
  12435. className: PREFIX + 'loading'
  12436. }, extend(loadingOptions.style, {
  12437. zIndex: 10,
  12438. display: NONE
  12439. }), chart.container);
  12440. chart.loadingSpan = createElement(
  12441. 'span',
  12442. null,
  12443. loadingOptions.labelStyle,
  12444. loadingDiv
  12445. );
  12446. addEvent(chart, 'redraw', setLoadingSize); // #1080
  12447. }
  12448. // update text
  12449. chart.loadingSpan.innerHTML = str || options.lang.loading;
  12450. // show it
  12451. if (!chart.loadingShown) {
  12452. css(loadingDiv, {
  12453. opacity: 0,
  12454. display: ''
  12455. });
  12456. animate(loadingDiv, {
  12457. opacity: loadingOptions.style.opacity
  12458. }, {
  12459. duration: loadingOptions.showDuration || 0
  12460. });
  12461. chart.loadingShown = true;
  12462. }
  12463. setLoadingSize();
  12464. },
  12465. /**
  12466. * Hide the loading layer
  12467. */
  12468. hideLoading: function () {
  12469. var options = this.options,
  12470. loadingDiv = this.loadingDiv;
  12471. if (loadingDiv) {
  12472. animate(loadingDiv, {
  12473. opacity: 0
  12474. }, {
  12475. duration: options.loading.hideDuration || 100,
  12476. complete: function () {
  12477. css(loadingDiv, { display: NONE });
  12478. }
  12479. });
  12480. }
  12481. this.loadingShown = false;
  12482. }
  12483. });
  12484. // extend the Point prototype for dynamic methods
  12485. extend(Point.prototype, {
  12486. /**
  12487. * Update the point with new options (typically x/y data) and optionally redraw the series.
  12488. *
  12489. * @param {Object} options Point options as defined in the series.data array
  12490. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  12491. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  12492. * configuration
  12493. *
  12494. */
  12495. update: function (options, redraw, animation) {
  12496. var point = this,
  12497. series = point.series,
  12498. graphic = point.graphic,
  12499. i,
  12500. data = series.data,
  12501. chart = series.chart,
  12502. seriesOptions = series.options;
  12503. redraw = pick(redraw, true);
  12504. // fire the event with a default handler of doing the update
  12505. point.firePointEvent('update', { options: options }, function () {
  12506. point.applyOptions(options);
  12507. // update visuals
  12508. if (isObject(options)) {
  12509. series.getAttribs();
  12510. if (graphic) {
  12511. if (options && options.marker && options.marker.symbol) {
  12512. point.graphic = graphic.destroy();
  12513. } else {
  12514. graphic.attr(point.pointAttr[point.state || '']);
  12515. }
  12516. }
  12517. if (options && options.dataLabels && point.dataLabel) { // #2468
  12518. point.dataLabel = point.dataLabel.destroy();
  12519. }
  12520. }
  12521. // record changes in the parallel arrays
  12522. i = inArray(point, data);
  12523. series.updateParallelArrays(point, i);
  12524. seriesOptions.data[i] = point.options;
  12525. // redraw
  12526. series.isDirty = series.isDirtyData = true;
  12527. if (!series.fixedBox && series.hasCartesianSeries) { // #1906, #2320
  12528. chart.isDirtyBox = true;
  12529. }
  12530. if (seriesOptions.legendType === 'point') { // #1831, #1885
  12531. chart.legend.destroyItem(point);
  12532. }
  12533. if (redraw) {
  12534. chart.redraw(animation);
  12535. }
  12536. });
  12537. },
  12538. /**
  12539. * Remove a point and optionally redraw the series and if necessary the axes
  12540. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  12541. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  12542. * configuration
  12543. */
  12544. remove: function (redraw, animation) {
  12545. var point = this,
  12546. series = point.series,
  12547. points = series.points,
  12548. chart = series.chart,
  12549. i,
  12550. data = series.data;
  12551. setAnimation(animation, chart);
  12552. redraw = pick(redraw, true);
  12553. // fire the event with a default handler of removing the point
  12554. point.firePointEvent('remove', null, function () {
  12555. // splice all the parallel arrays
  12556. i = inArray(point, data);
  12557. if (data.length === points.length) {
  12558. points.splice(i, 1);
  12559. }
  12560. data.splice(i, 1);
  12561. series.options.data.splice(i, 1);
  12562. series.updateParallelArrays(point, 'splice', i, 1);
  12563. point.destroy();
  12564. // redraw
  12565. series.isDirty = true;
  12566. series.isDirtyData = true;
  12567. if (redraw) {
  12568. chart.redraw();
  12569. }
  12570. });
  12571. }
  12572. });
  12573. // Extend the series prototype for dynamic methods
  12574. extend(Series.prototype, {
  12575. /**
  12576. * Add a point dynamically after chart load time
  12577. * @param {Object} options Point options as given in series.data
  12578. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  12579. * @param {Boolean} shift If shift is true, a point is shifted off the start
  12580. * of the series as one is appended to the end.
  12581. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  12582. * configuration
  12583. */
  12584. addPoint: function (options, redraw, shift, animation) {
  12585. var series = this,
  12586. seriesOptions = series.options,
  12587. data = series.data,
  12588. graph = series.graph,
  12589. area = series.area,
  12590. chart = series.chart,
  12591. names = series.xAxis && series.xAxis.names,
  12592. currentShift = (graph && graph.shift) || 0,
  12593. dataOptions = seriesOptions.data,
  12594. point,
  12595. isInTheMiddle,
  12596. xData = series.xData,
  12597. x,
  12598. i;
  12599. setAnimation(animation, chart);
  12600. // Make graph animate sideways
  12601. if (shift) {
  12602. each([graph, area, series.graphNeg, series.areaNeg], function (shape) {
  12603. if (shape) {
  12604. shape.shift = currentShift + 1;
  12605. }
  12606. });
  12607. }
  12608. if (area) {
  12609. area.isArea = true; // needed in animation, both with and without shift
  12610. }
  12611. // Optional redraw, defaults to true
  12612. redraw = pick(redraw, true);
  12613. // Get options and push the point to xData, yData and series.options. In series.generatePoints
  12614. // the Point instance will be created on demand and pushed to the series.data array.
  12615. point = { series: series };
  12616. series.pointClass.prototype.applyOptions.apply(point, [options]);
  12617. x = point.x;
  12618. // Get the insertion point
  12619. i = xData.length;
  12620. if (series.requireSorting && x < xData[i - 1]) {
  12621. isInTheMiddle = true;
  12622. while (i && xData[i - 1] > x) {
  12623. i--;
  12624. }
  12625. }
  12626. series.updateParallelArrays(point, 'splice', i, 0, 0); // insert undefined item
  12627. series.updateParallelArrays(point, i); // update it
  12628. if (names) {
  12629. names[x] = point.name;
  12630. }
  12631. dataOptions.splice(i, 0, options);
  12632. if (isInTheMiddle) {
  12633. series.data.splice(i, 0, null);
  12634. series.processData();
  12635. }
  12636. // Generate points to be added to the legend (#1329)
  12637. if (seriesOptions.legendType === 'point') {
  12638. series.generatePoints();
  12639. }
  12640. // Shift the first point off the parallel arrays
  12641. // todo: consider series.removePoint(i) method
  12642. if (shift) {
  12643. if (data[0] && data[0].remove) {
  12644. data[0].remove(false);
  12645. } else {
  12646. data.shift();
  12647. series.updateParallelArrays(point, 'shift');
  12648. dataOptions.shift();
  12649. }
  12650. }
  12651. // redraw
  12652. series.isDirty = true;
  12653. series.isDirtyData = true;
  12654. if (redraw) {
  12655. series.getAttribs(); // #1937
  12656. chart.redraw();
  12657. }
  12658. },
  12659. /**
  12660. * Remove a series and optionally redraw the chart
  12661. *
  12662. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  12663. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  12664. * configuration
  12665. */
  12666. remove: function (redraw, animation) {
  12667. var series = this,
  12668. chart = series.chart;
  12669. redraw = pick(redraw, true);
  12670. if (!series.isRemoving) { /* prevent triggering native event in jQuery
  12671. (calling the remove function from the remove event) */
  12672. series.isRemoving = true;
  12673. // fire the event with a default handler of removing the point
  12674. fireEvent(series, 'remove', null, function () {
  12675. // destroy elements
  12676. series.destroy();
  12677. // redraw
  12678. chart.isDirtyLegend = chart.isDirtyBox = true;
  12679. chart.linkSeries();
  12680. if (redraw) {
  12681. chart.redraw(animation);
  12682. }
  12683. });
  12684. }
  12685. series.isRemoving = false;
  12686. },
  12687. /**
  12688. * Update the series with a new set of options
  12689. */
  12690. update: function (newOptions, redraw) {
  12691. var series = this,
  12692. chart = this.chart,
  12693. // must use user options when changing type because this.options is merged
  12694. // in with type specific plotOptions
  12695. oldOptions = this.userOptions,
  12696. oldType = this.type,
  12697. proto = seriesTypes[oldType].prototype,
  12698. preserve = ['group', 'markerGroup', 'dataLabelsGroup'],
  12699. n;
  12700. // Make sure groups are not destroyed (#3094)
  12701. each(preserve, function (prop) {
  12702. preserve[prop] = series[prop];
  12703. delete series[prop];
  12704. });
  12705. // Do the merge, with some forced options
  12706. newOptions = merge(oldOptions, {
  12707. animation: false,
  12708. index: this.index,
  12709. pointStart: this.xData[0] // when updating after addPoint
  12710. }, { data: this.options.data }, newOptions);
  12711. // Destroy the series and reinsert methods from the type prototype
  12712. this.remove(false);
  12713. for (n in proto) { // Overwrite series-type specific methods (#2270)
  12714. if (proto.hasOwnProperty(n)) {
  12715. this[n] = UNDEFINED;
  12716. }
  12717. }
  12718. extend(this, seriesTypes[newOptions.type || oldType].prototype);
  12719. // Re-register groups (#3094)
  12720. each(preserve, function (prop) {
  12721. series[prop] = preserve[prop];
  12722. });
  12723. this.init(chart, newOptions);
  12724. chart.linkSeries(); // Links are lost in this.remove (#3028)
  12725. if (pick(redraw, true)) {
  12726. chart.redraw(false);
  12727. }
  12728. }
  12729. });
  12730. // Extend the Axis.prototype for dynamic methods
  12731. extend(Axis.prototype, {
  12732. /**
  12733. * Update the axis with a new options structure
  12734. */
  12735. update: function (newOptions, redraw) {
  12736. var chart = this.chart;
  12737. newOptions = chart.options[this.coll][this.options.index] = merge(this.userOptions, newOptions);
  12738. this.destroy(true);
  12739. this._addedPlotLB = UNDEFINED; // #1611, #2887
  12740. this.init(chart, extend(newOptions, { events: UNDEFINED }));
  12741. chart.isDirtyBox = true;
  12742. if (pick(redraw, true)) {
  12743. chart.redraw();
  12744. }
  12745. },
  12746. /**
  12747. * Remove the axis from the chart
  12748. */
  12749. remove: function (redraw) {
  12750. var chart = this.chart,
  12751. key = this.coll, // xAxis or yAxis
  12752. axisSeries = this.series,
  12753. i = axisSeries.length;
  12754. // Remove associated series (#2687)
  12755. while (i--) {
  12756. if (axisSeries[i]) {
  12757. axisSeries[i].remove(false);
  12758. }
  12759. }
  12760. // Remove the axis
  12761. erase(chart.axes, this);
  12762. erase(chart[key], this);
  12763. chart.options[key].splice(this.options.index, 1);
  12764. each(chart[key], function (axis, i) { // Re-index, #1706
  12765. axis.options.index = i;
  12766. });
  12767. this.destroy();
  12768. chart.isDirtyBox = true;
  12769. if (pick(redraw, true)) {
  12770. chart.redraw();
  12771. }
  12772. },
  12773. /**
  12774. * Update the axis title by options
  12775. */
  12776. setTitle: function (newTitleOptions, redraw) {
  12777. this.update({ title: newTitleOptions }, redraw);
  12778. },
  12779. /**
  12780. * Set new axis categories and optionally redraw
  12781. * @param {Array} categories
  12782. * @param {Boolean} redraw
  12783. */
  12784. setCategories: function (categories, redraw) {
  12785. this.update({ categories: categories }, redraw);
  12786. }
  12787. });
  12788. /**
  12789. * LineSeries object
  12790. */
  12791. var LineSeries = extendClass(Series);
  12792. seriesTypes.line = LineSeries;
  12793. /**
  12794. * Set the default options for area
  12795. */
  12796. defaultPlotOptions.area = merge(defaultSeriesOptions, {
  12797. threshold: 0
  12798. // trackByArea: false,
  12799. // lineColor: null, // overrides color, but lets fillColor be unaltered
  12800. // fillOpacity: 0.75,
  12801. // fillColor: null
  12802. });
  12803. /**
  12804. * AreaSeries object
  12805. */
  12806. var AreaSeries = extendClass(Series, {
  12807. type: 'area',
  12808. /**
  12809. * For stacks, don't split segments on null values. Instead, draw null values with
  12810. * no marker. Also insert dummy points for any X position that exists in other series
  12811. * in the stack.
  12812. */
  12813. getSegments: function () {
  12814. var series = this,
  12815. segments = [],
  12816. segment = [],
  12817. keys = [],
  12818. xAxis = this.xAxis,
  12819. yAxis = this.yAxis,
  12820. stack = yAxis.stacks[this.stackKey],
  12821. pointMap = {},
  12822. plotX,
  12823. plotY,
  12824. points = this.points,
  12825. connectNulls = this.options.connectNulls,
  12826. i,
  12827. x;
  12828. if (this.options.stacking && !this.cropped) { // cropped causes artefacts in Stock, and perf issue
  12829. // Create a map where we can quickly look up the points by their X value.
  12830. for (i = 0; i < points.length; i++) {
  12831. pointMap[points[i].x] = points[i];
  12832. }
  12833. // Sort the keys (#1651)
  12834. for (x in stack) {
  12835. if (stack[x].total !== null) { // nulled after switching between grouping and not (#1651, #2336)
  12836. keys.push(+x);
  12837. }
  12838. }
  12839. keys.sort(function (a, b) {
  12840. return a - b;
  12841. });
  12842. each(keys, function (x) {
  12843. var y = 0,
  12844. stackPoint;
  12845. if (connectNulls && (!pointMap[x] || pointMap[x].y === null)) { // #1836
  12846. return;
  12847. // The point exists, push it to the segment
  12848. } else if (pointMap[x]) {
  12849. segment.push(pointMap[x]);
  12850. // There is no point for this X value in this series, so we
  12851. // insert a dummy point in order for the areas to be drawn
  12852. // correctly.
  12853. } else {
  12854. // Loop down the stack to find the series below this one that has
  12855. // a value (#1991)
  12856. for (i = series.index; i <= yAxis.series.length; i++) {
  12857. stackPoint = stack[x].points[i + ',' + x];
  12858. if (stackPoint) {
  12859. y = stackPoint[1];
  12860. break;
  12861. }
  12862. }
  12863. plotX = xAxis.translate(x);
  12864. plotY = yAxis.toPixels(y, true);
  12865. segment.push({
  12866. y: null,
  12867. plotX: plotX,
  12868. clientX: plotX,
  12869. plotY: plotY,
  12870. yBottom: plotY,
  12871. onMouseOver: noop
  12872. });
  12873. }
  12874. });
  12875. if (segment.length) {
  12876. segments.push(segment);
  12877. }
  12878. } else {
  12879. Series.prototype.getSegments.call(this);
  12880. segments = this.segments;
  12881. }
  12882. this.segments = segments;
  12883. },
  12884. /**
  12885. * Extend the base Series getSegmentPath method by adding the path for the area.
  12886. * This path is pushed to the series.areaPath property.
  12887. */
  12888. getSegmentPath: function (segment) {
  12889. var segmentPath = Series.prototype.getSegmentPath.call(this, segment), // call base method
  12890. areaSegmentPath = [].concat(segmentPath), // work on a copy for the area path
  12891. i,
  12892. options = this.options,
  12893. segLength = segmentPath.length,
  12894. translatedThreshold = this.yAxis.getThreshold(options.threshold), // #2181
  12895. yBottom;
  12896. if (segLength === 3) { // for animation from 1 to two points
  12897. areaSegmentPath.push(L, segmentPath[1], segmentPath[2]);
  12898. }
  12899. if (options.stacking && !this.closedStacks) {
  12900. // Follow stack back. Todo: implement areaspline. A general solution could be to
  12901. // reverse the entire graphPath of the previous series, though may be hard with
  12902. // splines and with series with different extremes
  12903. for (i = segment.length - 1; i >= 0; i--) {
  12904. yBottom = pick(segment[i].yBottom, translatedThreshold);
  12905. // step line?
  12906. if (i < segment.length - 1 && options.step) {
  12907. areaSegmentPath.push(segment[i + 1].plotX, yBottom);
  12908. }
  12909. areaSegmentPath.push(segment[i].plotX, yBottom);
  12910. }
  12911. } else { // follow zero line back
  12912. this.closeSegment(areaSegmentPath, segment, translatedThreshold);
  12913. }
  12914. this.areaPath = this.areaPath.concat(areaSegmentPath);
  12915. return segmentPath;
  12916. },
  12917. /**
  12918. * Extendable method to close the segment path of an area. This is overridden in polar
  12919. * charts.
  12920. */
  12921. closeSegment: function (path, segment, translatedThreshold) {
  12922. path.push(
  12923. L,
  12924. segment[segment.length - 1].plotX,
  12925. translatedThreshold,
  12926. L,
  12927. segment[0].plotX,
  12928. translatedThreshold
  12929. );
  12930. },
  12931. /**
  12932. * Draw the graph and the underlying area. This method calls the Series base
  12933. * function and adds the area. The areaPath is calculated in the getSegmentPath
  12934. * method called from Series.prototype.drawGraph.
  12935. */
  12936. drawGraph: function () {
  12937. // Define or reset areaPath
  12938. this.areaPath = [];
  12939. // Call the base method
  12940. Series.prototype.drawGraph.apply(this);
  12941. // Define local variables
  12942. var series = this,
  12943. areaPath = this.areaPath,
  12944. options = this.options,
  12945. negativeColor = options.negativeColor,
  12946. negativeFillColor = options.negativeFillColor,
  12947. props = [['area', this.color, options.fillColor]]; // area name, main color, fill color
  12948. if (negativeColor || negativeFillColor) {
  12949. props.push(['areaNeg', negativeColor, negativeFillColor]);
  12950. }
  12951. each(props, function (prop) {
  12952. var areaKey = prop[0],
  12953. area = series[areaKey];
  12954. // Create or update the area
  12955. if (area) { // update
  12956. area.animate({ d: areaPath });
  12957. } else { // create
  12958. series[areaKey] = series.chart.renderer.path(areaPath)
  12959. .attr({
  12960. fill: pick(
  12961. prop[2],
  12962. Color(prop[1]).setOpacity(pick(options.fillOpacity, 0.75)).get()
  12963. ),
  12964. zIndex: 0 // #1069
  12965. }).add(series.group);
  12966. }
  12967. });
  12968. },
  12969. drawLegendSymbol: LegendSymbolMixin.drawRectangle
  12970. });
  12971. seriesTypes.area = AreaSeries;
  12972. /**
  12973. * Set the default options for spline
  12974. */
  12975. defaultPlotOptions.spline = merge(defaultSeriesOptions);
  12976. /**
  12977. * SplineSeries object
  12978. */
  12979. var SplineSeries = extendClass(Series, {
  12980. type: 'spline',
  12981. /**
  12982. * Get the spline segment from a given point's previous neighbour to the given point
  12983. */
  12984. getPointSpline: function (segment, point, i) {
  12985. var smoothing = 1.5, // 1 means control points midway between points, 2 means 1/3 from the point, 3 is 1/4 etc
  12986. denom = smoothing + 1,
  12987. plotX = point.plotX,
  12988. plotY = point.plotY,
  12989. lastPoint = segment[i - 1],
  12990. nextPoint = segment[i + 1],
  12991. leftContX,
  12992. leftContY,
  12993. rightContX,
  12994. rightContY,
  12995. ret;
  12996. // find control points
  12997. if (lastPoint && nextPoint) {
  12998. var lastX = lastPoint.plotX,
  12999. lastY = lastPoint.plotY,
  13000. nextX = nextPoint.plotX,
  13001. nextY = nextPoint.plotY,
  13002. correction;
  13003. leftContX = (smoothing * plotX + lastX) / denom;
  13004. leftContY = (smoothing * plotY + lastY) / denom;
  13005. rightContX = (smoothing * plotX + nextX) / denom;
  13006. rightContY = (smoothing * plotY + nextY) / denom;
  13007. // have the two control points make a straight line through main point
  13008. correction = ((rightContY - leftContY) * (rightContX - plotX)) /
  13009. (rightContX - leftContX) + plotY - rightContY;
  13010. leftContY += correction;
  13011. rightContY += correction;
  13012. // to prevent false extremes, check that control points are between
  13013. // neighbouring points' y values
  13014. if (leftContY > lastY && leftContY > plotY) {
  13015. leftContY = mathMax(lastY, plotY);
  13016. rightContY = 2 * plotY - leftContY; // mirror of left control point
  13017. } else if (leftContY < lastY && leftContY < plotY) {
  13018. leftContY = mathMin(lastY, plotY);
  13019. rightContY = 2 * plotY - leftContY;
  13020. }
  13021. if (rightContY > nextY && rightContY > plotY) {
  13022. rightContY = mathMax(nextY, plotY);
  13023. leftContY = 2 * plotY - rightContY;
  13024. } else if (rightContY < nextY && rightContY < plotY) {
  13025. rightContY = mathMin(nextY, plotY);
  13026. leftContY = 2 * plotY - rightContY;
  13027. }
  13028. // record for drawing in next point
  13029. point.rightContX = rightContX;
  13030. point.rightContY = rightContY;
  13031. }
  13032. // Visualize control points for debugging
  13033. /*
  13034. if (leftContX) {
  13035. this.chart.renderer.circle(leftContX + this.chart.plotLeft, leftContY + this.chart.plotTop, 2)
  13036. .attr({
  13037. stroke: 'red',
  13038. 'stroke-width': 1,
  13039. fill: 'none'
  13040. })
  13041. .add();
  13042. this.chart.renderer.path(['M', leftContX + this.chart.plotLeft, leftContY + this.chart.plotTop,
  13043. 'L', plotX + this.chart.plotLeft, plotY + this.chart.plotTop])
  13044. .attr({
  13045. stroke: 'red',
  13046. 'stroke-width': 1
  13047. })
  13048. .add();
  13049. this.chart.renderer.circle(rightContX + this.chart.plotLeft, rightContY + this.chart.plotTop, 2)
  13050. .attr({
  13051. stroke: 'green',
  13052. 'stroke-width': 1,
  13053. fill: 'none'
  13054. })
  13055. .add();
  13056. this.chart.renderer.path(['M', rightContX + this.chart.plotLeft, rightContY + this.chart.plotTop,
  13057. 'L', plotX + this.chart.plotLeft, plotY + this.chart.plotTop])
  13058. .attr({
  13059. stroke: 'green',
  13060. 'stroke-width': 1
  13061. })
  13062. .add();
  13063. }
  13064. */
  13065. // moveTo or lineTo
  13066. if (!i) {
  13067. ret = [M, plotX, plotY];
  13068. } else { // curve from last point to this
  13069. ret = [
  13070. 'C',
  13071. lastPoint.rightContX || lastPoint.plotX,
  13072. lastPoint.rightContY || lastPoint.plotY,
  13073. leftContX || plotX,
  13074. leftContY || plotY,
  13075. plotX,
  13076. plotY
  13077. ];
  13078. lastPoint.rightContX = lastPoint.rightContY = null; // reset for updating series later
  13079. }
  13080. return ret;
  13081. }
  13082. });
  13083. seriesTypes.spline = SplineSeries;
  13084. /**
  13085. * Set the default options for areaspline
  13086. */
  13087. defaultPlotOptions.areaspline = merge(defaultPlotOptions.area);
  13088. /**
  13089. * AreaSplineSeries object
  13090. */
  13091. var areaProto = AreaSeries.prototype,
  13092. AreaSplineSeries = extendClass(SplineSeries, {
  13093. type: 'areaspline',
  13094. closedStacks: true, // instead of following the previous graph back, follow the threshold back
  13095. // Mix in methods from the area series
  13096. getSegmentPath: areaProto.getSegmentPath,
  13097. closeSegment: areaProto.closeSegment,
  13098. drawGraph: areaProto.drawGraph,
  13099. drawLegendSymbol: LegendSymbolMixin.drawRectangle
  13100. });
  13101. seriesTypes.areaspline = AreaSplineSeries;
  13102. /**
  13103. * Set the default options for column
  13104. */
  13105. defaultPlotOptions.column = merge(defaultSeriesOptions, {
  13106. borderColor: '#FFFFFF',
  13107. //borderWidth: 1,
  13108. borderRadius: 0,
  13109. //colorByPoint: undefined,
  13110. groupPadding: 0.2,
  13111. //grouping: true,
  13112. marker: null, // point options are specified in the base options
  13113. pointPadding: 0.1,
  13114. //pointWidth: null,
  13115. minPointLength: 0,
  13116. cropThreshold: 50, // when there are more points, they will not animate out of the chart on xAxis.setExtremes
  13117. pointRange: null, // null means auto, meaning 1 in a categorized axis and least distance between points if not categories
  13118. states: {
  13119. hover: {
  13120. brightness: 0.1,
  13121. shadow: false,
  13122. halo: false
  13123. },
  13124. select: {
  13125. color: '#C0C0C0',
  13126. borderColor: '#000000',
  13127. shadow: false
  13128. }
  13129. },
  13130. dataLabels: {
  13131. align: null, // auto
  13132. verticalAlign: null, // auto
  13133. y: null
  13134. },
  13135. stickyTracking: false,
  13136. tooltip: {
  13137. distance: 6
  13138. },
  13139. threshold: 0
  13140. });
  13141. /**
  13142. * ColumnSeries object
  13143. */
  13144. var ColumnSeries = extendClass(Series, {
  13145. type: 'column',
  13146. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  13147. stroke: 'borderColor',
  13148. fill: 'color',
  13149. r: 'borderRadius'
  13150. },
  13151. cropShoulder: 0,
  13152. trackerGroups: ['group', 'dataLabelsGroup'],
  13153. negStacks: true, // use separate negative stacks, unlike area stacks where a negative
  13154. // point is substracted from previous (#1910)
  13155. /**
  13156. * Initialize the series
  13157. */
  13158. init: function () {
  13159. Series.prototype.init.apply(this, arguments);
  13160. var series = this,
  13161. chart = series.chart;
  13162. // if the series is added dynamically, force redraw of other
  13163. // series affected by a new column
  13164. if (chart.hasRendered) {
  13165. each(chart.series, function (otherSeries) {
  13166. if (otherSeries.type === series.type) {
  13167. otherSeries.isDirty = true;
  13168. }
  13169. });
  13170. }
  13171. },
  13172. /**
  13173. * Return the width and x offset of the columns adjusted for grouping, groupPadding, pointPadding,
  13174. * pointWidth etc.
  13175. */
  13176. getColumnMetrics: function () {
  13177. var series = this,
  13178. options = series.options,
  13179. xAxis = series.xAxis,
  13180. yAxis = series.yAxis,
  13181. reversedXAxis = xAxis.reversed,
  13182. stackKey,
  13183. stackGroups = {},
  13184. columnIndex,
  13185. columnCount = 0;
  13186. // Get the total number of column type series.
  13187. // This is called on every series. Consider moving this logic to a
  13188. // chart.orderStacks() function and call it on init, addSeries and removeSeries
  13189. if (options.grouping === false) {
  13190. columnCount = 1;
  13191. } else {
  13192. each(series.chart.series, function (otherSeries) {
  13193. var otherOptions = otherSeries.options,
  13194. otherYAxis = otherSeries.yAxis;
  13195. if (otherSeries.type === series.type && otherSeries.visible &&
  13196. yAxis.len === otherYAxis.len && yAxis.pos === otherYAxis.pos) { // #642, #2086
  13197. if (otherOptions.stacking) {
  13198. stackKey = otherSeries.stackKey;
  13199. if (stackGroups[stackKey] === UNDEFINED) {
  13200. stackGroups[stackKey] = columnCount++;
  13201. }
  13202. columnIndex = stackGroups[stackKey];
  13203. } else if (otherOptions.grouping !== false) { // #1162
  13204. columnIndex = columnCount++;
  13205. }
  13206. otherSeries.columnIndex = columnIndex;
  13207. }
  13208. });
  13209. }
  13210. var categoryWidth = mathMin(
  13211. mathAbs(xAxis.transA) * (xAxis.ordinalSlope || options.pointRange || xAxis.closestPointRange || xAxis.tickInterval || 1), // #2610
  13212. xAxis.len // #1535
  13213. ),
  13214. groupPadding = categoryWidth * options.groupPadding,
  13215. groupWidth = categoryWidth - 2 * groupPadding,
  13216. pointOffsetWidth = groupWidth / columnCount,
  13217. optionPointWidth = options.pointWidth,
  13218. pointPadding = defined(optionPointWidth) ? (pointOffsetWidth - optionPointWidth) / 2 :
  13219. pointOffsetWidth * options.pointPadding,
  13220. pointWidth = pick(optionPointWidth, pointOffsetWidth - 2 * pointPadding), // exact point width, used in polar charts
  13221. colIndex = (reversedXAxis ?
  13222. columnCount - (series.columnIndex || 0) : // #1251
  13223. series.columnIndex) || 0,
  13224. pointXOffset = pointPadding + (groupPadding + colIndex *
  13225. pointOffsetWidth - (categoryWidth / 2)) *
  13226. (reversedXAxis ? -1 : 1);
  13227. // Save it for reading in linked series (Error bars particularly)
  13228. return (series.columnMetrics = {
  13229. width: pointWidth,
  13230. offset: pointXOffset
  13231. });
  13232. },
  13233. /**
  13234. * Translate each point to the plot area coordinate system and find shape positions
  13235. */
  13236. translate: function () {
  13237. var series = this,
  13238. chart = series.chart,
  13239. options = series.options,
  13240. borderWidth = series.borderWidth = pick(
  13241. options.borderWidth,
  13242. series.activePointCount > 0.5 * series.xAxis.len ? 0 : 1
  13243. ),
  13244. yAxis = series.yAxis,
  13245. threshold = options.threshold,
  13246. translatedThreshold = series.translatedThreshold = yAxis.getThreshold(threshold),
  13247. minPointLength = pick(options.minPointLength, 5),
  13248. metrics = series.getColumnMetrics(),
  13249. pointWidth = metrics.width,
  13250. seriesBarW = series.barW = mathMax(pointWidth, 1 + 2 * borderWidth), // postprocessed for border width
  13251. pointXOffset = series.pointXOffset = metrics.offset,
  13252. xCrisp = -(borderWidth % 2 ? 0.5 : 0),
  13253. yCrisp = borderWidth % 2 ? 0.5 : 1;
  13254. if (chart.renderer.isVML && chart.inverted) {
  13255. yCrisp += 1;
  13256. }
  13257. // When the pointPadding is 0, we want the columns to be packed tightly, so we allow individual
  13258. // columns to have individual sizes. When pointPadding is greater, we strive for equal-width
  13259. // columns (#2694).
  13260. if (options.pointPadding) {
  13261. seriesBarW = mathCeil(seriesBarW);
  13262. }
  13263. Series.prototype.translate.apply(series);
  13264. // Record the new values
  13265. each(series.points, function (point) {
  13266. var yBottom = pick(point.yBottom, translatedThreshold),
  13267. plotY = mathMin(mathMax(-999 - yBottom, point.plotY), yAxis.len + 999 + yBottom), // Don't draw too far outside plot area (#1303, #2241)
  13268. barX = point.plotX + pointXOffset,
  13269. barW = seriesBarW,
  13270. barY = mathMin(plotY, yBottom),
  13271. right,
  13272. bottom,
  13273. fromTop,
  13274. barH = mathMax(plotY, yBottom) - barY;
  13275. // Handle options.minPointLength
  13276. if (mathAbs(barH) < minPointLength) {
  13277. if (minPointLength) {
  13278. barH = minPointLength;
  13279. barY =
  13280. mathRound(mathAbs(barY - translatedThreshold) > minPointLength ? // stacked
  13281. yBottom - minPointLength : // keep position
  13282. translatedThreshold - (yAxis.translate(point.y, 0, 1, 0, 1) <= translatedThreshold ? minPointLength : 0)); // use exact yAxis.translation (#1485)
  13283. }
  13284. }
  13285. // Cache for access in polar
  13286. point.barX = barX;
  13287. point.pointWidth = pointWidth;
  13288. // Fix the tooltip on center of grouped columns (#1216)
  13289. point.tooltipPos = chart.inverted ? [yAxis.len - plotY, series.xAxis.len - barX - barW / 2] : [barX + barW / 2, plotY];
  13290. // Round off to obtain crisp edges and avoid overlapping with neighbours (#2694)
  13291. right = mathRound(barX + barW) + xCrisp;
  13292. barX = mathRound(barX) + xCrisp;
  13293. barW = right - barX;
  13294. fromTop = mathAbs(barY) < 0.5;
  13295. bottom = mathRound(barY + barH) + yCrisp;
  13296. barY = mathRound(barY) + yCrisp;
  13297. barH = bottom - barY;
  13298. // Top edges are exceptions
  13299. if (fromTop) {
  13300. barY -= 1;
  13301. barH += 1;
  13302. }
  13303. // Register shape type and arguments to be used in drawPoints
  13304. point.shapeType = 'rect';
  13305. point.shapeArgs = {
  13306. x: barX,
  13307. y: barY,
  13308. width: barW,
  13309. height: barH
  13310. };
  13311. });
  13312. },
  13313. getSymbol: noop,
  13314. /**
  13315. * Use a solid rectangle like the area series types
  13316. */
  13317. drawLegendSymbol: LegendSymbolMixin.drawRectangle,
  13318. /**
  13319. * Columns have no graph
  13320. */
  13321. drawGraph: noop,
  13322. /**
  13323. * Draw the columns. For bars, the series.group is rotated, so the same coordinates
  13324. * apply for columns and bars. This method is inherited by scatter series.
  13325. *
  13326. */
  13327. drawPoints: function () {
  13328. var series = this,
  13329. chart = this.chart,
  13330. options = series.options,
  13331. renderer = chart.renderer,
  13332. animationLimit = options.animationLimit || 250,
  13333. shapeArgs,
  13334. pointAttr;
  13335. // draw the columns
  13336. each(series.points, function (point) {
  13337. var plotY = point.plotY,
  13338. graphic = point.graphic,
  13339. borderAttr;
  13340. if (plotY !== UNDEFINED && !isNaN(plotY) && point.y !== null) {
  13341. shapeArgs = point.shapeArgs;
  13342. borderAttr = defined(series.borderWidth) ? {
  13343. 'stroke-width': series.borderWidth
  13344. } : {};
  13345. pointAttr = point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE] || series.pointAttr[NORMAL_STATE];
  13346. if (graphic) { // update
  13347. stop(graphic);
  13348. graphic.attr(borderAttr)[chart.pointCount < animationLimit ? 'animate' : 'attr'](merge(shapeArgs));
  13349. } else {
  13350. point.graphic = graphic = renderer[point.shapeType](shapeArgs)
  13351. .attr(pointAttr)
  13352. .attr(borderAttr)
  13353. .add(series.group)
  13354. .shadow(options.shadow, null, options.stacking && !options.borderRadius);
  13355. }
  13356. } else if (graphic) {
  13357. point.graphic = graphic.destroy(); // #1269
  13358. }
  13359. });
  13360. },
  13361. /**
  13362. * Animate the column heights one by one from zero
  13363. * @param {Boolean} init Whether to initialize the animation or run it
  13364. */
  13365. animate: function (init) {
  13366. var series = this,
  13367. yAxis = this.yAxis,
  13368. options = series.options,
  13369. inverted = this.chart.inverted,
  13370. attr = {},
  13371. translatedThreshold;
  13372. if (hasSVG) { // VML is too slow anyway
  13373. if (init) {
  13374. attr.scaleY = 0.001;
  13375. translatedThreshold = mathMin(yAxis.pos + yAxis.len, mathMax(yAxis.pos, yAxis.toPixels(options.threshold)));
  13376. if (inverted) {
  13377. attr.translateX = translatedThreshold - yAxis.len;
  13378. } else {
  13379. attr.translateY = translatedThreshold;
  13380. }
  13381. series.group.attr(attr);
  13382. } else { // run the animation
  13383. attr.scaleY = 1;
  13384. attr[inverted ? 'translateX' : 'translateY'] = yAxis.pos;
  13385. series.group.animate(attr, series.options.animation);
  13386. // delete this function to allow it only once
  13387. series.animate = null;
  13388. }
  13389. }
  13390. },
  13391. /**
  13392. * Remove this series from the chart
  13393. */
  13394. remove: function () {
  13395. var series = this,
  13396. chart = series.chart;
  13397. // column and bar series affects other series of the same type
  13398. // as they are either stacked or grouped
  13399. if (chart.hasRendered) {
  13400. each(chart.series, function (otherSeries) {
  13401. if (otherSeries.type === series.type) {
  13402. otherSeries.isDirty = true;
  13403. }
  13404. });
  13405. }
  13406. Series.prototype.remove.apply(series, arguments);
  13407. }
  13408. });
  13409. seriesTypes.column = ColumnSeries;
  13410. /**
  13411. * Set the default options for bar
  13412. */
  13413. defaultPlotOptions.bar = merge(defaultPlotOptions.column);
  13414. /**
  13415. * The Bar series class
  13416. */
  13417. var BarSeries = extendClass(ColumnSeries, {
  13418. type: 'bar',
  13419. inverted: true
  13420. });
  13421. seriesTypes.bar = BarSeries;
  13422. /**
  13423. * Set the default options for scatter
  13424. */
  13425. defaultPlotOptions.scatter = merge(defaultSeriesOptions, {
  13426. lineWidth: 0,
  13427. tooltip: {
  13428. headerFormat: '<span style="color:{series.color}">\u25CF</span> <span style="font-size: 10px;"> {series.name}</span><br/>',
  13429. pointFormat: 'x: <b>{point.x}</b><br/>y: <b>{point.y}</b><br/>'
  13430. },
  13431. stickyTracking: false
  13432. });
  13433. /**
  13434. * The scatter series class
  13435. */
  13436. var ScatterSeries = extendClass(Series, {
  13437. type: 'scatter',
  13438. sorted: false,
  13439. requireSorting: false,
  13440. noSharedTooltip: true,
  13441. trackerGroups: ['markerGroup', 'dataLabelsGroup'],
  13442. takeOrdinalPosition: false, // #2342
  13443. singularTooltips: true,
  13444. drawGraph: function () {
  13445. if (this.options.lineWidth) {
  13446. Series.prototype.drawGraph.call(this);
  13447. }
  13448. }
  13449. });
  13450. seriesTypes.scatter = ScatterSeries;
  13451. /**
  13452. * Set the default options for pie
  13453. */
  13454. defaultPlotOptions.pie = merge(defaultSeriesOptions, {
  13455. borderColor: '#FFFFFF',
  13456. borderWidth: 1,
  13457. center: [null, null],
  13458. clip: false,
  13459. colorByPoint: true, // always true for pies
  13460. dataLabels: {
  13461. // align: null,
  13462. // connectorWidth: 1,
  13463. // connectorColor: point.color,
  13464. // connectorPadding: 5,
  13465. distance: 30,
  13466. enabled: true,
  13467. formatter: function () { // #2945
  13468. return this.point.name;
  13469. }
  13470. // softConnector: true,
  13471. //y: 0
  13472. },
  13473. ignoreHiddenPoint: true,
  13474. //innerSize: 0,
  13475. legendType: 'point',
  13476. marker: null, // point options are specified in the base options
  13477. size: null,
  13478. showInLegend: false,
  13479. slicedOffset: 10,
  13480. states: {
  13481. hover: {
  13482. brightness: 0.1,
  13483. shadow: false
  13484. }
  13485. },
  13486. stickyTracking: false,
  13487. tooltip: {
  13488. followPointer: true
  13489. }
  13490. });
  13491. /**
  13492. * Extended point object for pies
  13493. */
  13494. var PiePoint = extendClass(Point, {
  13495. /**
  13496. * Initiate the pie slice
  13497. */
  13498. init: function () {
  13499. Point.prototype.init.apply(this, arguments);
  13500. var point = this,
  13501. toggleSlice;
  13502. // Disallow negative values (#1530)
  13503. if (point.y < 0) {
  13504. point.y = null;
  13505. }
  13506. //visible: options.visible !== false,
  13507. extend(point, {
  13508. visible: point.visible !== false,
  13509. name: pick(point.name, 'Slice')
  13510. });
  13511. // add event listener for select
  13512. toggleSlice = function (e) {
  13513. point.slice(e.type === 'select');
  13514. };
  13515. addEvent(point, 'select', toggleSlice);
  13516. addEvent(point, 'unselect', toggleSlice);
  13517. return point;
  13518. },
  13519. /**
  13520. * Toggle the visibility of the pie slice
  13521. * @param {Boolean} vis Whether to show the slice or not. If undefined, the
  13522. * visibility is toggled
  13523. */
  13524. setVisible: function (vis) {
  13525. var point = this,
  13526. series = point.series,
  13527. chart = series.chart;
  13528. // if called without an argument, toggle visibility
  13529. point.visible = point.options.visible = vis = vis === UNDEFINED ? !point.visible : vis;
  13530. series.options.data[inArray(point, series.data)] = point.options; // update userOptions.data
  13531. // Show and hide associated elements
  13532. each(['graphic', 'dataLabel', 'connector', 'shadowGroup'], function (key) {
  13533. if (point[key]) {
  13534. point[key][vis ? 'show' : 'hide'](true);
  13535. }
  13536. });
  13537. if (point.legendItem) {
  13538. chart.legend.colorizeItem(point, vis);
  13539. }
  13540. // Handle ignore hidden slices
  13541. if (!series.isDirty && series.options.ignoreHiddenPoint) {
  13542. series.isDirty = true;
  13543. chart.redraw();
  13544. }
  13545. },
  13546. /**
  13547. * Set or toggle whether the slice is cut out from the pie
  13548. * @param {Boolean} sliced When undefined, the slice state is toggled
  13549. * @param {Boolean} redraw Whether to redraw the chart. True by default.
  13550. */
  13551. slice: function (sliced, redraw, animation) {
  13552. var point = this,
  13553. series = point.series,
  13554. chart = series.chart,
  13555. translation;
  13556. setAnimation(animation, chart);
  13557. // redraw is true by default
  13558. redraw = pick(redraw, true);
  13559. // if called without an argument, toggle
  13560. point.sliced = point.options.sliced = sliced = defined(sliced) ? sliced : !point.sliced;
  13561. series.options.data[inArray(point, series.data)] = point.options; // update userOptions.data
  13562. translation = sliced ? point.slicedTranslation : {
  13563. translateX: 0,
  13564. translateY: 0
  13565. };
  13566. point.graphic.animate(translation);
  13567. if (point.shadowGroup) {
  13568. point.shadowGroup.animate(translation);
  13569. }
  13570. },
  13571. haloPath: function (size) {
  13572. var shapeArgs = this.shapeArgs,
  13573. chart = this.series.chart;
  13574. 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, {
  13575. innerR: this.shapeArgs.r,
  13576. start: shapeArgs.start,
  13577. end: shapeArgs.end
  13578. });
  13579. }
  13580. });
  13581. /**
  13582. * The Pie series class
  13583. */
  13584. var PieSeries = {
  13585. type: 'pie',
  13586. isCartesian: false,
  13587. pointClass: PiePoint,
  13588. requireSorting: false,
  13589. noSharedTooltip: true,
  13590. trackerGroups: ['group', 'dataLabelsGroup'],
  13591. axisTypes: [],
  13592. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  13593. stroke: 'borderColor',
  13594. 'stroke-width': 'borderWidth',
  13595. fill: 'color'
  13596. },
  13597. singularTooltips: true,
  13598. /**
  13599. * Pies have one color each point
  13600. */
  13601. getColor: noop,
  13602. /**
  13603. * Animate the pies in
  13604. */
  13605. animate: function (init) {
  13606. var series = this,
  13607. points = series.points,
  13608. startAngleRad = series.startAngleRad;
  13609. if (!init) {
  13610. each(points, function (point) {
  13611. var graphic = point.graphic,
  13612. args = point.shapeArgs;
  13613. if (graphic) {
  13614. // start values
  13615. graphic.attr({
  13616. r: series.center[3] / 2, // animate from inner radius (#779)
  13617. start: startAngleRad,
  13618. end: startAngleRad
  13619. });
  13620. // animate
  13621. graphic.animate({
  13622. r: args.r,
  13623. start: args.start,
  13624. end: args.end
  13625. }, series.options.animation);
  13626. }
  13627. });
  13628. // delete this function to allow it only once
  13629. series.animate = null;
  13630. }
  13631. },
  13632. /**
  13633. * Extend the basic setData method by running processData and generatePoints immediately,
  13634. * in order to access the points from the legend.
  13635. */
  13636. setData: function (data, redraw, animation, updatePoints) {
  13637. Series.prototype.setData.call(this, data, false, animation, updatePoints);
  13638. this.processData();
  13639. this.generatePoints();
  13640. if (pick(redraw, true)) {
  13641. this.chart.redraw(animation);
  13642. }
  13643. },
  13644. /**
  13645. * Extend the generatePoints method by adding total and percentage properties to each point
  13646. */
  13647. generatePoints: function () {
  13648. var i,
  13649. total = 0,
  13650. points,
  13651. len,
  13652. point,
  13653. ignoreHiddenPoint = this.options.ignoreHiddenPoint;
  13654. Series.prototype.generatePoints.call(this);
  13655. // Populate local vars
  13656. points = this.points;
  13657. len = points.length;
  13658. // Get the total sum
  13659. for (i = 0; i < len; i++) {
  13660. point = points[i];
  13661. total += (ignoreHiddenPoint && !point.visible) ? 0 : point.y;
  13662. }
  13663. this.total = total;
  13664. // Set each point's properties
  13665. for (i = 0; i < len; i++) {
  13666. point = points[i];
  13667. point.percentage = total > 0 ? (point.y / total) * 100 : 0;
  13668. point.total = total;
  13669. }
  13670. },
  13671. /**
  13672. * Do translation for pie slices
  13673. */
  13674. translate: function (positions) {
  13675. this.generatePoints();
  13676. var series = this,
  13677. cumulative = 0,
  13678. precision = 1000, // issue #172
  13679. options = series.options,
  13680. slicedOffset = options.slicedOffset,
  13681. connectorOffset = slicedOffset + options.borderWidth,
  13682. start,
  13683. end,
  13684. angle,
  13685. startAngle = options.startAngle || 0,
  13686. startAngleRad = series.startAngleRad = mathPI / 180 * (startAngle - 90),
  13687. endAngleRad = series.endAngleRad = mathPI / 180 * ((pick(options.endAngle, startAngle + 360)) - 90),
  13688. circ = endAngleRad - startAngleRad, //2 * mathPI,
  13689. points = series.points,
  13690. radiusX, // the x component of the radius vector for a given point
  13691. radiusY,
  13692. labelDistance = options.dataLabels.distance,
  13693. ignoreHiddenPoint = options.ignoreHiddenPoint,
  13694. i,
  13695. len = points.length,
  13696. point;
  13697. // Get positions - either an integer or a percentage string must be given.
  13698. // If positions are passed as a parameter, we're in a recursive loop for adjusting
  13699. // space for data labels.
  13700. if (!positions) {
  13701. series.center = positions = series.getCenter();
  13702. }
  13703. // utility for getting the x value from a given y, used for anticollision logic in data labels
  13704. series.getX = function (y, left) {
  13705. angle = math.asin(mathMin((y - positions[1]) / (positions[2] / 2 + labelDistance), 1));
  13706. return positions[0] +
  13707. (left ? -1 : 1) *
  13708. (mathCos(angle) * (positions[2] / 2 + labelDistance));
  13709. };
  13710. // Calculate the geometry for each point
  13711. for (i = 0; i < len; i++) {
  13712. point = points[i];
  13713. // set start and end angle
  13714. start = startAngleRad + (cumulative * circ);
  13715. if (!ignoreHiddenPoint || point.visible) {
  13716. cumulative += point.percentage / 100;
  13717. }
  13718. end = startAngleRad + (cumulative * circ);
  13719. // set the shape
  13720. point.shapeType = 'arc';
  13721. point.shapeArgs = {
  13722. x: positions[0],
  13723. y: positions[1],
  13724. r: positions[2] / 2,
  13725. innerR: positions[3] / 2,
  13726. start: mathRound(start * precision) / precision,
  13727. end: mathRound(end * precision) / precision
  13728. };
  13729. // The angle must stay within -90 and 270 (#2645)
  13730. angle = (end + start) / 2;
  13731. if (angle > 1.5 * mathPI) {
  13732. angle -= 2 * mathPI;
  13733. } else if (angle < -mathPI / 2) {
  13734. angle += 2 * mathPI;
  13735. }
  13736. // Center for the sliced out slice
  13737. point.slicedTranslation = {
  13738. translateX: mathRound(mathCos(angle) * slicedOffset),
  13739. translateY: mathRound(mathSin(angle) * slicedOffset)
  13740. };
  13741. // set the anchor point for tooltips
  13742. radiusX = mathCos(angle) * positions[2] / 2;
  13743. radiusY = mathSin(angle) * positions[2] / 2;
  13744. point.tooltipPos = [
  13745. positions[0] + radiusX * 0.7,
  13746. positions[1] + radiusY * 0.7
  13747. ];
  13748. point.half = angle < -mathPI / 2 || angle > mathPI / 2 ? 1 : 0;
  13749. point.angle = angle;
  13750. // set the anchor point for data labels
  13751. connectorOffset = mathMin(connectorOffset, labelDistance / 2); // #1678
  13752. point.labelPos = [
  13753. positions[0] + radiusX + mathCos(angle) * labelDistance, // first break of connector
  13754. positions[1] + radiusY + mathSin(angle) * labelDistance, // a/a
  13755. positions[0] + radiusX + mathCos(angle) * connectorOffset, // second break, right outside pie
  13756. positions[1] + radiusY + mathSin(angle) * connectorOffset, // a/a
  13757. positions[0] + radiusX, // landing point for connector
  13758. positions[1] + radiusY, // a/a
  13759. labelDistance < 0 ? // alignment
  13760. 'center' :
  13761. point.half ? 'right' : 'left', // alignment
  13762. angle // center angle
  13763. ];
  13764. }
  13765. },
  13766. drawGraph: null,
  13767. /**
  13768. * Draw the data points
  13769. */
  13770. drawPoints: function () {
  13771. var series = this,
  13772. chart = series.chart,
  13773. renderer = chart.renderer,
  13774. groupTranslation,
  13775. //center,
  13776. graphic,
  13777. //group,
  13778. shadow = series.options.shadow,
  13779. shadowGroup,
  13780. shapeArgs;
  13781. if (shadow && !series.shadowGroup) {
  13782. series.shadowGroup = renderer.g('shadow')
  13783. .add(series.group);
  13784. }
  13785. // draw the slices
  13786. each(series.points, function (point) {
  13787. graphic = point.graphic;
  13788. shapeArgs = point.shapeArgs;
  13789. shadowGroup = point.shadowGroup;
  13790. // put the shadow behind all points
  13791. if (shadow && !shadowGroup) {
  13792. shadowGroup = point.shadowGroup = renderer.g('shadow')
  13793. .add(series.shadowGroup);
  13794. }
  13795. // if the point is sliced, use special translation, else use plot area traslation
  13796. groupTranslation = point.sliced ? point.slicedTranslation : {
  13797. translateX: 0,
  13798. translateY: 0
  13799. };
  13800. //group.translate(groupTranslation[0], groupTranslation[1]);
  13801. if (shadowGroup) {
  13802. shadowGroup.attr(groupTranslation);
  13803. }
  13804. // draw the slice
  13805. if (graphic) {
  13806. graphic.animate(extend(shapeArgs, groupTranslation));
  13807. } else {
  13808. point.graphic = graphic = renderer[point.shapeType](shapeArgs)
  13809. .setRadialReference(series.center)
  13810. .attr(
  13811. point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE]
  13812. )
  13813. .attr({
  13814. 'stroke-linejoin': 'round'
  13815. //zIndex: 1 // #2722 (reversed)
  13816. })
  13817. .attr(groupTranslation)
  13818. .add(series.group)
  13819. .shadow(shadow, shadowGroup);
  13820. }
  13821. // detect point specific visibility (#2430)
  13822. if (point.visible !== undefined) {
  13823. point.setVisible(point.visible);
  13824. }
  13825. });
  13826. },
  13827. /**
  13828. * Utility for sorting data labels
  13829. */
  13830. sortByAngle: function (points, sign) {
  13831. points.sort(function (a, b) {
  13832. return a.angle !== undefined && (b.angle - a.angle) * sign;
  13833. });
  13834. },
  13835. /**
  13836. * Use a simple symbol from LegendSymbolMixin
  13837. */
  13838. drawLegendSymbol: LegendSymbolMixin.drawRectangle,
  13839. /**
  13840. * Use the getCenter method from drawLegendSymbol
  13841. */
  13842. getCenter: CenteredSeriesMixin.getCenter,
  13843. /**
  13844. * Pies don't have point marker symbols
  13845. */
  13846. getSymbol: noop
  13847. };
  13848. PieSeries = extendClass(Series, PieSeries);
  13849. seriesTypes.pie = PieSeries;
  13850. /**
  13851. * Draw the data labels
  13852. */
  13853. Series.prototype.drawDataLabels = function () {
  13854. var series = this,
  13855. seriesOptions = series.options,
  13856. cursor = seriesOptions.cursor,
  13857. options = seriesOptions.dataLabels,
  13858. points = series.points,
  13859. pointOptions,
  13860. generalOptions,
  13861. str,
  13862. dataLabelsGroup;
  13863. if (options.enabled || series._hasPointLabels) {
  13864. // Process default alignment of data labels for columns
  13865. if (series.dlProcessOptions) {
  13866. series.dlProcessOptions(options);
  13867. }
  13868. // Create a separate group for the data labels to avoid rotation
  13869. dataLabelsGroup = series.plotGroup(
  13870. 'dataLabelsGroup',
  13871. 'data-labels',
  13872. options.defer ? HIDDEN : VISIBLE,
  13873. options.zIndex || 6
  13874. );
  13875. if (!series.hasRendered && pick(options.defer, true)) {
  13876. dataLabelsGroup.attr({ opacity: 0 });
  13877. addEvent(series, 'afterAnimate', function () {
  13878. if (series.visible) { // #3023, #3024
  13879. dataLabelsGroup.show();
  13880. }
  13881. dataLabelsGroup[seriesOptions.animation ? 'animate' : 'attr']({ opacity: 1 }, { duration: 200 });
  13882. });
  13883. }
  13884. // Make the labels for each point
  13885. generalOptions = options;
  13886. each(points, function (point) {
  13887. var enabled,
  13888. dataLabel = point.dataLabel,
  13889. labelConfig,
  13890. attr,
  13891. name,
  13892. rotation,
  13893. connector = point.connector,
  13894. isNew = true;
  13895. // Determine if each data label is enabled
  13896. pointOptions = point.options && point.options.dataLabels;
  13897. enabled = pick(pointOptions && pointOptions.enabled, generalOptions.enabled); // #2282
  13898. // If the point is outside the plot area, destroy it. #678, #820
  13899. if (dataLabel && !enabled) {
  13900. point.dataLabel = dataLabel.destroy();
  13901. // Individual labels are disabled if the are explicitly disabled
  13902. // in the point options, or if they fall outside the plot area.
  13903. } else if (enabled) {
  13904. // Create individual options structure that can be extended without
  13905. // affecting others
  13906. options = merge(generalOptions, pointOptions);
  13907. rotation = options.rotation;
  13908. // Get the string
  13909. labelConfig = point.getLabelConfig();
  13910. str = options.format ?
  13911. format(options.format, labelConfig) :
  13912. options.formatter.call(labelConfig, options);
  13913. // Determine the color
  13914. options.style.color = pick(options.color, options.style.color, series.color, 'black');
  13915. // update existing label
  13916. if (dataLabel) {
  13917. if (defined(str)) {
  13918. dataLabel
  13919. .attr({
  13920. text: str
  13921. });
  13922. isNew = false;
  13923. } else { // #1437 - the label is shown conditionally
  13924. point.dataLabel = dataLabel = dataLabel.destroy();
  13925. if (connector) {
  13926. point.connector = connector.destroy();
  13927. }
  13928. }
  13929. // create new label
  13930. } else if (defined(str)) {
  13931. attr = {
  13932. //align: align,
  13933. fill: options.backgroundColor,
  13934. stroke: options.borderColor,
  13935. 'stroke-width': options.borderWidth,
  13936. r: options.borderRadius || 0,
  13937. rotation: rotation,
  13938. padding: options.padding,
  13939. zIndex: 1
  13940. };
  13941. // Remove unused attributes (#947)
  13942. for (name in attr) {
  13943. if (attr[name] === UNDEFINED) {
  13944. delete attr[name];
  13945. }
  13946. }
  13947. dataLabel = point.dataLabel = series.chart.renderer[rotation ? 'text' : 'label']( // labels don't support rotation
  13948. str,
  13949. 0,
  13950. -999,
  13951. null,
  13952. null,
  13953. null,
  13954. options.useHTML
  13955. )
  13956. .attr(attr)
  13957. .css(extend(options.style, cursor && { cursor: cursor }))
  13958. .add(dataLabelsGroup)
  13959. .shadow(options.shadow);
  13960. }
  13961. if (dataLabel) {
  13962. // Now the data label is created and placed at 0,0, so we need to align it
  13963. series.alignDataLabel(point, dataLabel, options, null, isNew);
  13964. }
  13965. }
  13966. });
  13967. }
  13968. };
  13969. /**
  13970. * Align each individual data label
  13971. */
  13972. Series.prototype.alignDataLabel = function (point, dataLabel, options, alignTo, isNew) {
  13973. var chart = this.chart,
  13974. inverted = chart.inverted,
  13975. plotX = pick(point.plotX, -999),
  13976. plotY = pick(point.plotY, -999),
  13977. bBox = dataLabel.getBBox(),
  13978. // Math.round for rounding errors (#2683), alignTo to allow column labels (#2700)
  13979. visible = this.visible && (point.series.forceDL || chart.isInsidePlot(plotX, mathRound(plotY), inverted) ||
  13980. (alignTo && chart.isInsidePlot(plotX, inverted ? alignTo.x + 1 : alignTo.y + alignTo.height - 1, inverted))),
  13981. alignAttr; // the final position;
  13982. if (visible) {
  13983. // The alignment box is a singular point
  13984. alignTo = extend({
  13985. x: inverted ? chart.plotWidth - plotY : plotX,
  13986. y: mathRound(inverted ? chart.plotHeight - plotX : plotY),
  13987. width: 0,
  13988. height: 0
  13989. }, alignTo);
  13990. // Add the text size for alignment calculation
  13991. extend(options, {
  13992. width: bBox.width,
  13993. height: bBox.height
  13994. });
  13995. // Allow a hook for changing alignment in the last moment, then do the alignment
  13996. if (options.rotation) { // Fancy box alignment isn't supported for rotated text
  13997. dataLabel[isNew ? 'attr' : 'animate']({
  13998. x: alignTo.x + options.x + alignTo.width / 2,
  13999. y: alignTo.y + options.y + alignTo.height / 2
  14000. })
  14001. .attr({ // #3003
  14002. align: options.align
  14003. });
  14004. } else {
  14005. dataLabel.align(options, null, alignTo);
  14006. alignAttr = dataLabel.alignAttr;
  14007. // Handle justify or crop
  14008. if (pick(options.overflow, 'justify') === 'justify') {
  14009. this.justifyDataLabel(dataLabel, options, alignAttr, bBox, alignTo, isNew);
  14010. } else if (pick(options.crop, true)) {
  14011. // Now check that the data label is within the plot area
  14012. visible = chart.isInsidePlot(alignAttr.x, alignAttr.y) && chart.isInsidePlot(alignAttr.x + bBox.width, alignAttr.y + bBox.height);
  14013. }
  14014. }
  14015. }
  14016. // Show or hide based on the final aligned position
  14017. if (!visible) {
  14018. dataLabel.attr({ y: -999 });
  14019. dataLabel.placed = false; // don't animate back in
  14020. }
  14021. };
  14022. /**
  14023. * If data labels fall partly outside the plot area, align them back in, in a way that
  14024. * doesn't hide the point.
  14025. */
  14026. Series.prototype.justifyDataLabel = function (dataLabel, options, alignAttr, bBox, alignTo, isNew) {
  14027. var chart = this.chart,
  14028. align = options.align,
  14029. verticalAlign = options.verticalAlign,
  14030. off,
  14031. justified;
  14032. // Off left
  14033. off = alignAttr.x;
  14034. if (off < 0) {
  14035. if (align === 'right') {
  14036. options.align = 'left';
  14037. } else {
  14038. options.x = -off;
  14039. }
  14040. justified = true;
  14041. }
  14042. // Off right
  14043. off = alignAttr.x + bBox.width;
  14044. if (off > chart.plotWidth) {
  14045. if (align === 'left') {
  14046. options.align = 'right';
  14047. } else {
  14048. options.x = chart.plotWidth - off;
  14049. }
  14050. justified = true;
  14051. }
  14052. // Off top
  14053. off = alignAttr.y;
  14054. if (off < 0) {
  14055. if (verticalAlign === 'bottom') {
  14056. options.verticalAlign = 'top';
  14057. } else {
  14058. options.y = -off;
  14059. }
  14060. justified = true;
  14061. }
  14062. // Off bottom
  14063. off = alignAttr.y + bBox.height;
  14064. if (off > chart.plotHeight) {
  14065. if (verticalAlign === 'top') {
  14066. options.verticalAlign = 'bottom';
  14067. } else {
  14068. options.y = chart.plotHeight - off;
  14069. }
  14070. justified = true;
  14071. }
  14072. if (justified) {
  14073. dataLabel.placed = !isNew;
  14074. dataLabel.align(options, null, alignTo);
  14075. }
  14076. };
  14077. /**
  14078. * Override the base drawDataLabels method by pie specific functionality
  14079. */
  14080. if (seriesTypes.pie) {
  14081. seriesTypes.pie.prototype.drawDataLabels = function () {
  14082. var series = this,
  14083. data = series.data,
  14084. point,
  14085. chart = series.chart,
  14086. options = series.options.dataLabels,
  14087. connectorPadding = pick(options.connectorPadding, 10),
  14088. connectorWidth = pick(options.connectorWidth, 1),
  14089. plotWidth = chart.plotWidth,
  14090. plotHeight = chart.plotHeight,
  14091. connector,
  14092. connectorPath,
  14093. softConnector = pick(options.softConnector, true),
  14094. distanceOption = options.distance,
  14095. seriesCenter = series.center,
  14096. radius = seriesCenter[2] / 2,
  14097. centerY = seriesCenter[1],
  14098. outside = distanceOption > 0,
  14099. dataLabel,
  14100. dataLabelWidth,
  14101. labelPos,
  14102. labelHeight,
  14103. halves = [// divide the points into right and left halves for anti collision
  14104. [], // right
  14105. [] // left
  14106. ],
  14107. x,
  14108. y,
  14109. visibility,
  14110. rankArr,
  14111. i,
  14112. j,
  14113. overflow = [0, 0, 0, 0], // top, right, bottom, left
  14114. sort = function (a, b) {
  14115. return b.y - a.y;
  14116. };
  14117. // get out if not enabled
  14118. if (!series.visible || (!options.enabled && !series._hasPointLabels)) {
  14119. return;
  14120. }
  14121. // run parent method
  14122. Series.prototype.drawDataLabels.apply(series);
  14123. // arrange points for detection collision
  14124. each(data, function (point) {
  14125. if (point.dataLabel && point.visible) { // #407, #2510
  14126. halves[point.half].push(point);
  14127. }
  14128. });
  14129. /* Loop over the points in each half, starting from the top and bottom
  14130. * of the pie to detect overlapping labels.
  14131. */
  14132. i = 2;
  14133. while (i--) {
  14134. var slots = [],
  14135. slotsLength,
  14136. usedSlots = [],
  14137. points = halves[i],
  14138. pos,
  14139. bottom,
  14140. length = points.length,
  14141. slotIndex;
  14142. if (!length) {
  14143. continue;
  14144. }
  14145. // Sort by angle
  14146. series.sortByAngle(points, i - 0.5);
  14147. // Assume equal label heights on either hemisphere (#2630)
  14148. j = labelHeight = 0;
  14149. while (!labelHeight && points[j]) { // #1569
  14150. labelHeight = points[j] && points[j].dataLabel && (points[j].dataLabel.getBBox().height || 21); // 21 is for #968
  14151. j++;
  14152. }
  14153. // Only do anti-collision when we are outside the pie and have connectors (#856)
  14154. if (distanceOption > 0) {
  14155. // Build the slots
  14156. bottom = mathMin(centerY + radius + distanceOption, chart.plotHeight);
  14157. for (pos = mathMax(0, centerY - radius - distanceOption); pos <= bottom; pos += labelHeight) {
  14158. slots.push(pos);
  14159. }
  14160. slotsLength = slots.length;
  14161. /* Visualize the slots
  14162. if (!series.slotElements) {
  14163. series.slotElements = [];
  14164. }
  14165. if (i === 1) {
  14166. series.slotElements.forEach(function (elem) {
  14167. elem.destroy();
  14168. });
  14169. series.slotElements.length = 0;
  14170. }
  14171. slots.forEach(function (pos, no) {
  14172. var slotX = series.getX(pos, i) + chart.plotLeft - (i ? 100 : 0),
  14173. slotY = pos + chart.plotTop;
  14174. if (!isNaN(slotX)) {
  14175. series.slotElements.push(chart.renderer.rect(slotX, slotY - 7, 100, labelHeight, 1)
  14176. .attr({
  14177. 'stroke-width': 1,
  14178. stroke: 'silver',
  14179. fill: 'rgba(0,0,255,0.1)'
  14180. })
  14181. .add());
  14182. series.slotElements.push(chart.renderer.text('Slot '+ no, slotX, slotY + 4)
  14183. .attr({
  14184. fill: 'silver'
  14185. }).add());
  14186. }
  14187. });
  14188. // */
  14189. // if there are more values than available slots, remove lowest values
  14190. if (length > slotsLength) {
  14191. // create an array for sorting and ranking the points within each quarter
  14192. rankArr = [].concat(points);
  14193. rankArr.sort(sort);
  14194. j = length;
  14195. while (j--) {
  14196. rankArr[j].rank = j;
  14197. }
  14198. j = length;
  14199. while (j--) {
  14200. if (points[j].rank >= slotsLength) {
  14201. points.splice(j, 1);
  14202. }
  14203. }
  14204. length = points.length;
  14205. }
  14206. // The label goes to the nearest open slot, but not closer to the edge than
  14207. // the label's index.
  14208. for (j = 0; j < length; j++) {
  14209. point = points[j];
  14210. labelPos = point.labelPos;
  14211. var closest = 9999,
  14212. distance,
  14213. slotI;
  14214. // find the closest slot index
  14215. for (slotI = 0; slotI < slotsLength; slotI++) {
  14216. distance = mathAbs(slots[slotI] - labelPos[1]);
  14217. if (distance < closest) {
  14218. closest = distance;
  14219. slotIndex = slotI;
  14220. }
  14221. }
  14222. // if that slot index is closer to the edges of the slots, move it
  14223. // to the closest appropriate slot
  14224. if (slotIndex < j && slots[j] !== null) { // cluster at the top
  14225. slotIndex = j;
  14226. } else if (slotsLength < length - j + slotIndex && slots[j] !== null) { // cluster at the bottom
  14227. slotIndex = slotsLength - length + j;
  14228. while (slots[slotIndex] === null) { // make sure it is not taken
  14229. slotIndex++;
  14230. }
  14231. } else {
  14232. // Slot is taken, find next free slot below. In the next run, the next slice will find the
  14233. // slot above these, because it is the closest one
  14234. while (slots[slotIndex] === null) { // make sure it is not taken
  14235. slotIndex++;
  14236. }
  14237. }
  14238. usedSlots.push({ i: slotIndex, y: slots[slotIndex] });
  14239. slots[slotIndex] = null; // mark as taken
  14240. }
  14241. // sort them in order to fill in from the top
  14242. usedSlots.sort(sort);
  14243. }
  14244. // now the used slots are sorted, fill them up sequentially
  14245. for (j = 0; j < length; j++) {
  14246. var slot, naturalY;
  14247. point = points[j];
  14248. labelPos = point.labelPos;
  14249. dataLabel = point.dataLabel;
  14250. visibility = point.visible === false ? HIDDEN : VISIBLE;
  14251. naturalY = labelPos[1];
  14252. if (distanceOption > 0) {
  14253. slot = usedSlots.pop();
  14254. slotIndex = slot.i;
  14255. // if the slot next to currrent slot is free, the y value is allowed
  14256. // to fall back to the natural position
  14257. y = slot.y;
  14258. if ((naturalY > y && slots[slotIndex + 1] !== null) ||
  14259. (naturalY < y && slots[slotIndex - 1] !== null)) {
  14260. y = mathMin(mathMax(0, naturalY), chart.plotHeight);
  14261. }
  14262. } else {
  14263. y = naturalY;
  14264. }
  14265. // get the x - use the natural x position for first and last slot, to prevent the top
  14266. // and botton slice connectors from touching each other on either side
  14267. x = options.justify ?
  14268. seriesCenter[0] + (i ? -1 : 1) * (radius + distanceOption) :
  14269. series.getX(y === centerY - radius - distanceOption || y === centerY + radius + distanceOption ? naturalY : y, i);
  14270. // Record the placement and visibility
  14271. dataLabel._attr = {
  14272. visibility: visibility,
  14273. align: labelPos[6]
  14274. };
  14275. dataLabel._pos = {
  14276. x: x + options.x +
  14277. ({ left: connectorPadding, right: -connectorPadding }[labelPos[6]] || 0),
  14278. y: y + options.y - 10 // 10 is for the baseline (label vs text)
  14279. };
  14280. dataLabel.connX = x;
  14281. dataLabel.connY = y;
  14282. // Detect overflowing data labels
  14283. if (this.options.size === null) {
  14284. dataLabelWidth = dataLabel.width;
  14285. // Overflow left
  14286. if (x - dataLabelWidth < connectorPadding) {
  14287. overflow[3] = mathMax(mathRound(dataLabelWidth - x + connectorPadding), overflow[3]);
  14288. // Overflow right
  14289. } else if (x + dataLabelWidth > plotWidth - connectorPadding) {
  14290. overflow[1] = mathMax(mathRound(x + dataLabelWidth - plotWidth + connectorPadding), overflow[1]);
  14291. }
  14292. // Overflow top
  14293. if (y - labelHeight / 2 < 0) {
  14294. overflow[0] = mathMax(mathRound(-y + labelHeight / 2), overflow[0]);
  14295. // Overflow left
  14296. } else if (y + labelHeight / 2 > plotHeight) {
  14297. overflow[2] = mathMax(mathRound(y + labelHeight / 2 - plotHeight), overflow[2]);
  14298. }
  14299. }
  14300. } // for each point
  14301. } // for each half
  14302. // Do not apply the final placement and draw the connectors until we have verified
  14303. // that labels are not spilling over.
  14304. if (arrayMax(overflow) === 0 || this.verifyDataLabelOverflow(overflow)) {
  14305. // Place the labels in the final position
  14306. this.placeDataLabels();
  14307. // Draw the connectors
  14308. if (outside && connectorWidth) {
  14309. each(this.points, function (point) {
  14310. connector = point.connector;
  14311. labelPos = point.labelPos;
  14312. dataLabel = point.dataLabel;
  14313. if (dataLabel && dataLabel._pos) {
  14314. visibility = dataLabel._attr.visibility;
  14315. x = dataLabel.connX;
  14316. y = dataLabel.connY;
  14317. connectorPath = softConnector ? [
  14318. M,
  14319. x + (labelPos[6] === 'left' ? 5 : -5), y, // end of the string at the label
  14320. 'C',
  14321. x, y, // first break, next to the label
  14322. 2 * labelPos[2] - labelPos[4], 2 * labelPos[3] - labelPos[5],
  14323. labelPos[2], labelPos[3], // second break
  14324. L,
  14325. labelPos[4], labelPos[5] // base
  14326. ] : [
  14327. M,
  14328. x + (labelPos[6] === 'left' ? 5 : -5), y, // end of the string at the label
  14329. L,
  14330. labelPos[2], labelPos[3], // second break
  14331. L,
  14332. labelPos[4], labelPos[5] // base
  14333. ];
  14334. if (connector) {
  14335. connector.animate({ d: connectorPath });
  14336. connector.attr('visibility', visibility);
  14337. } else {
  14338. point.connector = connector = series.chart.renderer.path(connectorPath).attr({
  14339. 'stroke-width': connectorWidth,
  14340. stroke: options.connectorColor || point.color || '#606060',
  14341. visibility: visibility
  14342. //zIndex: 0 // #2722 (reversed)
  14343. })
  14344. .add(series.dataLabelsGroup);
  14345. }
  14346. } else if (connector) {
  14347. point.connector = connector.destroy();
  14348. }
  14349. });
  14350. }
  14351. }
  14352. };
  14353. /**
  14354. * Perform the final placement of the data labels after we have verified that they
  14355. * fall within the plot area.
  14356. */
  14357. seriesTypes.pie.prototype.placeDataLabels = function () {
  14358. each(this.points, function (point) {
  14359. var dataLabel = point.dataLabel,
  14360. _pos;
  14361. if (dataLabel) {
  14362. _pos = dataLabel._pos;
  14363. if (_pos) {
  14364. dataLabel.attr(dataLabel._attr);
  14365. dataLabel[dataLabel.moved ? 'animate' : 'attr'](_pos);
  14366. dataLabel.moved = true;
  14367. } else if (dataLabel) {
  14368. dataLabel.attr({ y: -999 });
  14369. }
  14370. }
  14371. });
  14372. };
  14373. seriesTypes.pie.prototype.alignDataLabel = noop;
  14374. /**
  14375. * Verify whether the data labels are allowed to draw, or we should run more translation and data
  14376. * label positioning to keep them inside the plot area. Returns true when data labels are ready
  14377. * to draw.
  14378. */
  14379. seriesTypes.pie.prototype.verifyDataLabelOverflow = function (overflow) {
  14380. var center = this.center,
  14381. options = this.options,
  14382. centerOption = options.center,
  14383. minSize = options.minSize || 80,
  14384. newSize = minSize,
  14385. ret;
  14386. // Handle horizontal size and center
  14387. if (centerOption[0] !== null) { // Fixed center
  14388. newSize = mathMax(center[2] - mathMax(overflow[1], overflow[3]), minSize);
  14389. } else { // Auto center
  14390. newSize = mathMax(
  14391. center[2] - overflow[1] - overflow[3], // horizontal overflow
  14392. minSize
  14393. );
  14394. center[0] += (overflow[3] - overflow[1]) / 2; // horizontal center
  14395. }
  14396. // Handle vertical size and center
  14397. if (centerOption[1] !== null) { // Fixed center
  14398. newSize = mathMax(mathMin(newSize, center[2] - mathMax(overflow[0], overflow[2])), minSize);
  14399. } else { // Auto center
  14400. newSize = mathMax(
  14401. mathMin(
  14402. newSize,
  14403. center[2] - overflow[0] - overflow[2] // vertical overflow
  14404. ),
  14405. minSize
  14406. );
  14407. center[1] += (overflow[0] - overflow[2]) / 2; // vertical center
  14408. }
  14409. // If the size must be decreased, we need to run translate and drawDataLabels again
  14410. if (newSize < center[2]) {
  14411. center[2] = newSize;
  14412. this.translate(center);
  14413. each(this.points, function (point) {
  14414. if (point.dataLabel) {
  14415. point.dataLabel._pos = null; // reset
  14416. }
  14417. });
  14418. if (this.drawDataLabels) {
  14419. this.drawDataLabels();
  14420. }
  14421. // Else, return true to indicate that the pie and its labels is within the plot area
  14422. } else {
  14423. ret = true;
  14424. }
  14425. return ret;
  14426. };
  14427. }
  14428. if (seriesTypes.column) {
  14429. /**
  14430. * Override the basic data label alignment by adjusting for the position of the column
  14431. */
  14432. seriesTypes.column.prototype.alignDataLabel = function (point, dataLabel, options, alignTo, isNew) {
  14433. var chart = this.chart,
  14434. inverted = chart.inverted,
  14435. dlBox = point.dlBox || point.shapeArgs, // data label box for alignment
  14436. below = point.below || (point.plotY > pick(this.translatedThreshold, chart.plotSizeY)),
  14437. inside = pick(options.inside, !!this.options.stacking); // draw it inside the box?
  14438. // Align to the column itself, or the top of it
  14439. if (dlBox) { // Area range uses this method but not alignTo
  14440. alignTo = merge(dlBox);
  14441. if (inverted) {
  14442. alignTo = {
  14443. x: chart.plotWidth - alignTo.y - alignTo.height,
  14444. y: chart.plotHeight - alignTo.x - alignTo.width,
  14445. width: alignTo.height,
  14446. height: alignTo.width
  14447. };
  14448. }
  14449. // Compute the alignment box
  14450. if (!inside) {
  14451. if (inverted) {
  14452. alignTo.x += below ? 0 : alignTo.width;
  14453. alignTo.width = 0;
  14454. } else {
  14455. alignTo.y += below ? alignTo.height : 0;
  14456. alignTo.height = 0;
  14457. }
  14458. }
  14459. }
  14460. // When alignment is undefined (typically columns and bars), display the individual
  14461. // point below or above the point depending on the threshold
  14462. options.align = pick(
  14463. options.align,
  14464. !inverted || inside ? 'center' : below ? 'right' : 'left'
  14465. );
  14466. options.verticalAlign = pick(
  14467. options.verticalAlign,
  14468. inverted || inside ? 'middle' : below ? 'top' : 'bottom'
  14469. );
  14470. // Call the parent method
  14471. Series.prototype.alignDataLabel.call(this, point, dataLabel, options, alignTo, isNew);
  14472. };
  14473. }
  14474. /**
  14475. * TrackerMixin for points and graphs
  14476. */
  14477. var TrackerMixin = Highcharts.TrackerMixin = {
  14478. drawTrackerPoint: function () {
  14479. var series = this,
  14480. chart = series.chart,
  14481. pointer = chart.pointer,
  14482. cursor = series.options.cursor,
  14483. css = cursor && { cursor: cursor },
  14484. onMouseOver = function (e) {
  14485. var target = e.target,
  14486. point;
  14487. if (chart.hoverSeries !== series) {
  14488. series.onMouseOver();
  14489. }
  14490. while (target && !point) {
  14491. point = target.point;
  14492. target = target.parentNode;
  14493. }
  14494. if (point !== UNDEFINED && point !== chart.hoverPoint) { // undefined on graph in scatterchart
  14495. point.onMouseOver(e);
  14496. }
  14497. };
  14498. // Add reference to the point
  14499. each(series.points, function (point) {
  14500. if (point.graphic) {
  14501. point.graphic.element.point = point;
  14502. }
  14503. if (point.dataLabel) {
  14504. point.dataLabel.element.point = point;
  14505. }
  14506. });
  14507. // Add the event listeners, we need to do this only once
  14508. if (!series._hasTracking) {
  14509. each(series.trackerGroups, function (key) {
  14510. if (series[key]) { // we don't always have dataLabelsGroup
  14511. series[key]
  14512. .addClass(PREFIX + 'tracker')
  14513. .on('mouseover', onMouseOver)
  14514. .on('mouseout', function (e) { pointer.onTrackerMouseOut(e); })
  14515. .css(css);
  14516. if (hasTouch) {
  14517. series[key].on('touchstart', onMouseOver);
  14518. }
  14519. }
  14520. });
  14521. series._hasTracking = true;
  14522. }
  14523. },
  14524. /**
  14525. * Draw the tracker object that sits above all data labels and markers to
  14526. * track mouse events on the graph or points. For the line type charts
  14527. * the tracker uses the same graphPath, but with a greater stroke width
  14528. * for better control.
  14529. */
  14530. drawTrackerGraph: function () {
  14531. var series = this,
  14532. options = series.options,
  14533. trackByArea = options.trackByArea,
  14534. trackerPath = [].concat(trackByArea ? series.areaPath : series.graphPath),
  14535. trackerPathLength = trackerPath.length,
  14536. chart = series.chart,
  14537. pointer = chart.pointer,
  14538. renderer = chart.renderer,
  14539. snap = chart.options.tooltip.snap,
  14540. tracker = series.tracker,
  14541. cursor = options.cursor,
  14542. css = cursor && { cursor: cursor },
  14543. singlePoints = series.singlePoints,
  14544. singlePoint,
  14545. i,
  14546. onMouseOver = function () {
  14547. if (chart.hoverSeries !== series) {
  14548. series.onMouseOver();
  14549. }
  14550. },
  14551. /*
  14552. * Empirical lowest possible opacities for TRACKER_FILL for an element to stay invisible but clickable
  14553. * IE6: 0.002
  14554. * IE7: 0.002
  14555. * IE8: 0.002
  14556. * IE9: 0.00000000001 (unlimited)
  14557. * IE10: 0.0001 (exporting only)
  14558. * FF: 0.00000000001 (unlimited)
  14559. * Chrome: 0.000001
  14560. * Safari: 0.000001
  14561. * Opera: 0.00000000001 (unlimited)
  14562. */
  14563. TRACKER_FILL = 'rgba(192,192,192,' + (hasSVG ? 0.0001 : 0.002) + ')';
  14564. // Extend end points. A better way would be to use round linecaps,
  14565. // but those are not clickable in VML.
  14566. if (trackerPathLength && !trackByArea) {
  14567. i = trackerPathLength + 1;
  14568. while (i--) {
  14569. if (trackerPath[i] === M) { // extend left side
  14570. trackerPath.splice(i + 1, 0, trackerPath[i + 1] - snap, trackerPath[i + 2], L);
  14571. }
  14572. if ((i && trackerPath[i] === M) || i === trackerPathLength) { // extend right side
  14573. trackerPath.splice(i, 0, L, trackerPath[i - 2] + snap, trackerPath[i - 1]);
  14574. }
  14575. }
  14576. }
  14577. // handle single points
  14578. for (i = 0; i < singlePoints.length; i++) {
  14579. singlePoint = singlePoints[i];
  14580. trackerPath.push(M, singlePoint.plotX - snap, singlePoint.plotY,
  14581. L, singlePoint.plotX + snap, singlePoint.plotY);
  14582. }
  14583. // draw the tracker
  14584. if (tracker) {
  14585. tracker.attr({ d: trackerPath });
  14586. } else { // create
  14587. series.tracker = renderer.path(trackerPath)
  14588. .attr({
  14589. 'stroke-linejoin': 'round', // #1225
  14590. visibility: series.visible ? VISIBLE : HIDDEN,
  14591. stroke: TRACKER_FILL,
  14592. fill: trackByArea ? TRACKER_FILL : NONE,
  14593. 'stroke-width' : options.lineWidth + (trackByArea ? 0 : 2 * snap),
  14594. zIndex: 2
  14595. })
  14596. .add(series.group);
  14597. // The tracker is added to the series group, which is clipped, but is covered
  14598. // by the marker group. So the marker group also needs to capture events.
  14599. each([series.tracker, series.markerGroup], function (tracker) {
  14600. tracker.addClass(PREFIX + 'tracker')
  14601. .on('mouseover', onMouseOver)
  14602. .on('mouseout', function (e) { pointer.onTrackerMouseOut(e); })
  14603. .css(css);
  14604. if (hasTouch) {
  14605. tracker.on('touchstart', onMouseOver);
  14606. }
  14607. });
  14608. }
  14609. }
  14610. };
  14611. /* End TrackerMixin */
  14612. /**
  14613. * Add tracking event listener to the series group, so the point graphics
  14614. * themselves act as trackers
  14615. */
  14616. if (seriesTypes.column) {
  14617. ColumnSeries.prototype.drawTracker = TrackerMixin.drawTrackerPoint;
  14618. }
  14619. if (seriesTypes.pie) {
  14620. seriesTypes.pie.prototype.drawTracker = TrackerMixin.drawTrackerPoint;
  14621. }
  14622. if (seriesTypes.scatter) {
  14623. ScatterSeries.prototype.drawTracker = TrackerMixin.drawTrackerPoint;
  14624. }
  14625. /*
  14626. * Extend Legend for item events
  14627. */
  14628. extend(Legend.prototype, {
  14629. setItemEvents: function (item, legendItem, useHTML, itemStyle, itemHiddenStyle) {
  14630. var legend = this;
  14631. // Set the events on the item group, or in case of useHTML, the item itself (#1249)
  14632. (useHTML ? legendItem : item.legendGroup).on('mouseover', function () {
  14633. item.setState(HOVER_STATE);
  14634. legendItem.css(legend.options.itemHoverStyle);
  14635. })
  14636. .on('mouseout', function () {
  14637. legendItem.css(item.visible ? itemStyle : itemHiddenStyle);
  14638. item.setState();
  14639. })
  14640. .on('click', function (event) {
  14641. var strLegendItemClick = 'legendItemClick',
  14642. fnLegendItemClick = function () {
  14643. item.setVisible();
  14644. };
  14645. // Pass over the click/touch event. #4.
  14646. event = {
  14647. browserEvent: event
  14648. };
  14649. // click the name or symbol
  14650. if (item.firePointEvent) { // point
  14651. item.firePointEvent(strLegendItemClick, event, fnLegendItemClick);
  14652. } else {
  14653. fireEvent(item, strLegendItemClick, event, fnLegendItemClick);
  14654. }
  14655. });
  14656. },
  14657. createCheckboxForItem: function (item) {
  14658. var legend = this;
  14659. item.checkbox = createElement('input', {
  14660. type: 'checkbox',
  14661. checked: item.selected,
  14662. defaultChecked: item.selected // required by IE7
  14663. }, legend.options.itemCheckboxStyle, legend.chart.container);
  14664. addEvent(item.checkbox, 'click', function (event) {
  14665. var target = event.target;
  14666. fireEvent(item, 'checkboxClick', {
  14667. checked: target.checked
  14668. },
  14669. function () {
  14670. item.select();
  14671. }
  14672. );
  14673. });
  14674. }
  14675. });
  14676. /*
  14677. * Add pointer cursor to legend itemstyle in defaultOptions
  14678. */
  14679. defaultOptions.legend.itemStyle.cursor = 'pointer';
  14680. /*
  14681. * Extend the Chart object with interaction
  14682. */
  14683. extend(Chart.prototype, {
  14684. /**
  14685. * Display the zoom button
  14686. */
  14687. showResetZoom: function () {
  14688. var chart = this,
  14689. lang = defaultOptions.lang,
  14690. btnOptions = chart.options.chart.resetZoomButton,
  14691. theme = btnOptions.theme,
  14692. states = theme.states,
  14693. alignTo = btnOptions.relativeTo === 'chart' ? null : 'plotBox';
  14694. this.resetZoomButton = chart.renderer.button(lang.resetZoom, null, null, function () { chart.zoomOut(); }, theme, states && states.hover)
  14695. .attr({
  14696. align: btnOptions.position.align,
  14697. title: lang.resetZoomTitle
  14698. })
  14699. .add()
  14700. .align(btnOptions.position, false, alignTo);
  14701. },
  14702. /**
  14703. * Zoom out to 1:1
  14704. */
  14705. zoomOut: function () {
  14706. var chart = this;
  14707. fireEvent(chart, 'selection', { resetSelection: true }, function () {
  14708. chart.zoom();
  14709. });
  14710. },
  14711. /**
  14712. * Zoom into a given portion of the chart given by axis coordinates
  14713. * @param {Object} event
  14714. */
  14715. zoom: function (event) {
  14716. var chart = this,
  14717. hasZoomed,
  14718. pointer = chart.pointer,
  14719. displayButton = false,
  14720. resetZoomButton;
  14721. // If zoom is called with no arguments, reset the axes
  14722. if (!event || event.resetSelection) {
  14723. each(chart.axes, function (axis) {
  14724. hasZoomed = axis.zoom();
  14725. });
  14726. } else { // else, zoom in on all axes
  14727. each(event.xAxis.concat(event.yAxis), function (axisData) {
  14728. var axis = axisData.axis,
  14729. isXAxis = axis.isXAxis;
  14730. // don't zoom more than minRange
  14731. if (pointer[isXAxis ? 'zoomX' : 'zoomY'] || pointer[isXAxis ? 'pinchX' : 'pinchY']) {
  14732. hasZoomed = axis.zoom(axisData.min, axisData.max);
  14733. if (axis.displayBtn) {
  14734. displayButton = true;
  14735. }
  14736. }
  14737. });
  14738. }
  14739. // Show or hide the Reset zoom button
  14740. resetZoomButton = chart.resetZoomButton;
  14741. if (displayButton && !resetZoomButton) {
  14742. chart.showResetZoom();
  14743. } else if (!displayButton && isObject(resetZoomButton)) {
  14744. chart.resetZoomButton = resetZoomButton.destroy();
  14745. }
  14746. // Redraw
  14747. if (hasZoomed) {
  14748. chart.redraw(
  14749. pick(chart.options.chart.animation, event && event.animation, chart.pointCount < 100) // animation
  14750. );
  14751. }
  14752. },
  14753. /**
  14754. * Pan the chart by dragging the mouse across the pane. This function is called
  14755. * on mouse move, and the distance to pan is computed from chartX compared to
  14756. * the first chartX position in the dragging operation.
  14757. */
  14758. pan: function (e, panning) {
  14759. var chart = this,
  14760. hoverPoints = chart.hoverPoints,
  14761. doRedraw;
  14762. // remove active points for shared tooltip
  14763. if (hoverPoints) {
  14764. each(hoverPoints, function (point) {
  14765. point.setState();
  14766. });
  14767. }
  14768. each(panning === 'xy' ? [1, 0] : [1], function (isX) { // xy is used in maps
  14769. var mousePos = e[isX ? 'chartX' : 'chartY'],
  14770. axis = chart[isX ? 'xAxis' : 'yAxis'][0],
  14771. startPos = chart[isX ? 'mouseDownX' : 'mouseDownY'],
  14772. halfPointRange = (axis.pointRange || 0) / 2,
  14773. extremes = axis.getExtremes(),
  14774. newMin = axis.toValue(startPos - mousePos, true) + halfPointRange,
  14775. newMax = axis.toValue(startPos + chart[isX ? 'plotWidth' : 'plotHeight'] - mousePos, true) - halfPointRange;
  14776. if (axis.series.length && newMin > mathMin(extremes.dataMin, extremes.min) && newMax < mathMax(extremes.dataMax, extremes.max)) {
  14777. axis.setExtremes(newMin, newMax, false, false, { trigger: 'pan' });
  14778. doRedraw = true;
  14779. }
  14780. chart[isX ? 'mouseDownX' : 'mouseDownY'] = mousePos; // set new reference for next run
  14781. });
  14782. if (doRedraw) {
  14783. chart.redraw(false);
  14784. }
  14785. css(chart.container, { cursor: 'move' });
  14786. }
  14787. });
  14788. /*
  14789. * Extend the Point object with interaction
  14790. */
  14791. extend(Point.prototype, {
  14792. /**
  14793. * Toggle the selection status of a point
  14794. * @param {Boolean} selected Whether to select or unselect the point.
  14795. * @param {Boolean} accumulate Whether to add to the previous selection. By default,
  14796. * this happens if the control key (Cmd on Mac) was pressed during clicking.
  14797. */
  14798. select: function (selected, accumulate) {
  14799. var point = this,
  14800. series = point.series,
  14801. chart = series.chart;
  14802. selected = pick(selected, !point.selected);
  14803. // fire the event with the defalut handler
  14804. point.firePointEvent(selected ? 'select' : 'unselect', { accumulate: accumulate }, function () {
  14805. point.selected = point.options.selected = selected;
  14806. series.options.data[inArray(point, series.data)] = point.options;
  14807. point.setState(selected && SELECT_STATE);
  14808. // unselect all other points unless Ctrl or Cmd + click
  14809. if (!accumulate) {
  14810. each(chart.getSelectedPoints(), function (loopPoint) {
  14811. if (loopPoint.selected && loopPoint !== point) {
  14812. loopPoint.selected = loopPoint.options.selected = false;
  14813. series.options.data[inArray(loopPoint, series.data)] = loopPoint.options;
  14814. loopPoint.setState(NORMAL_STATE);
  14815. loopPoint.firePointEvent('unselect');
  14816. }
  14817. });
  14818. }
  14819. });
  14820. },
  14821. /**
  14822. * Runs on mouse over the point
  14823. */
  14824. onMouseOver: function (e) {
  14825. var point = this,
  14826. series = point.series,
  14827. chart = series.chart,
  14828. tooltip = chart.tooltip,
  14829. hoverPoint = chart.hoverPoint;
  14830. // set normal state to previous series
  14831. if (hoverPoint && hoverPoint !== point) {
  14832. hoverPoint.onMouseOut();
  14833. }
  14834. // trigger the event
  14835. point.firePointEvent('mouseOver');
  14836. // update the tooltip
  14837. if (tooltip && (!tooltip.shared || series.noSharedTooltip)) {
  14838. tooltip.refresh(point, e);
  14839. }
  14840. // hover this
  14841. point.setState(HOVER_STATE);
  14842. chart.hoverPoint = point;
  14843. },
  14844. /**
  14845. * Runs on mouse out from the point
  14846. */
  14847. onMouseOut: function () {
  14848. var chart = this.series.chart,
  14849. hoverPoints = chart.hoverPoints;
  14850. this.firePointEvent('mouseOut');
  14851. if (!hoverPoints || inArray(this, hoverPoints) === -1) { // #887, #2240
  14852. this.setState();
  14853. chart.hoverPoint = null;
  14854. }
  14855. },
  14856. /**
  14857. * Import events from the series' and point's options. Only do it on
  14858. * demand, to save processing time on hovering.
  14859. */
  14860. importEvents: function () {
  14861. if (!this.hasImportedEvents) {
  14862. var point = this,
  14863. options = merge(point.series.options.point, point.options),
  14864. events = options.events,
  14865. eventType;
  14866. point.events = events;
  14867. for (eventType in events) {
  14868. addEvent(point, eventType, events[eventType]);
  14869. }
  14870. this.hasImportedEvents = true;
  14871. }
  14872. },
  14873. /**
  14874. * Set the point's state
  14875. * @param {String} state
  14876. */
  14877. setState: function (state, move) {
  14878. var point = this,
  14879. plotX = point.plotX,
  14880. plotY = point.plotY,
  14881. series = point.series,
  14882. stateOptions = series.options.states,
  14883. markerOptions = defaultPlotOptions[series.type].marker && series.options.marker,
  14884. normalDisabled = markerOptions && !markerOptions.enabled,
  14885. markerStateOptions = markerOptions && markerOptions.states[state],
  14886. stateDisabled = markerStateOptions && markerStateOptions.enabled === false,
  14887. stateMarkerGraphic = series.stateMarkerGraphic,
  14888. pointMarker = point.marker || {},
  14889. chart = series.chart,
  14890. radius,
  14891. halo = series.halo,
  14892. haloOptions,
  14893. newSymbol,
  14894. pointAttr;
  14895. state = state || NORMAL_STATE; // empty string
  14896. pointAttr = point.pointAttr[state] || series.pointAttr[state];
  14897. if (
  14898. // already has this state
  14899. (state === point.state && !move) ||
  14900. // selected points don't respond to hover
  14901. (point.selected && state !== SELECT_STATE) ||
  14902. // series' state options is disabled
  14903. (stateOptions[state] && stateOptions[state].enabled === false) ||
  14904. // general point marker's state options is disabled
  14905. (state && (stateDisabled || (normalDisabled && markerStateOptions.enabled === false))) ||
  14906. // individual point marker's state options is disabled
  14907. (state && pointMarker.states && pointMarker.states[state] && pointMarker.states[state].enabled === false) // #1610
  14908. ) {
  14909. return;
  14910. }
  14911. // apply hover styles to the existing point
  14912. if (point.graphic) {
  14913. radius = markerOptions && point.graphic.symbolName && pointAttr.r;
  14914. point.graphic.attr(merge(
  14915. pointAttr,
  14916. radius ? { // new symbol attributes (#507, #612)
  14917. x: plotX - radius,
  14918. y: plotY - radius,
  14919. width: 2 * radius,
  14920. height: 2 * radius
  14921. } : {}
  14922. ));
  14923. // Zooming in from a range with no markers to a range with markers
  14924. if (stateMarkerGraphic) {
  14925. stateMarkerGraphic.hide();
  14926. }
  14927. } else {
  14928. // if a graphic is not applied to each point in the normal state, create a shared
  14929. // graphic for the hover state
  14930. if (state && markerStateOptions) {
  14931. radius = markerStateOptions.radius;
  14932. newSymbol = pointMarker.symbol || series.symbol;
  14933. // If the point has another symbol than the previous one, throw away the
  14934. // state marker graphic and force a new one (#1459)
  14935. if (stateMarkerGraphic && stateMarkerGraphic.currentSymbol !== newSymbol) {
  14936. stateMarkerGraphic = stateMarkerGraphic.destroy();
  14937. }
  14938. // Add a new state marker graphic
  14939. if (!stateMarkerGraphic) {
  14940. if (newSymbol) {
  14941. series.stateMarkerGraphic = stateMarkerGraphic = chart.renderer.symbol(
  14942. newSymbol,
  14943. plotX - radius,
  14944. plotY - radius,
  14945. 2 * radius,
  14946. 2 * radius
  14947. )
  14948. .attr(pointAttr)
  14949. .add(series.markerGroup);
  14950. stateMarkerGraphic.currentSymbol = newSymbol;
  14951. }
  14952. // Move the existing graphic
  14953. } else {
  14954. stateMarkerGraphic[move ? 'animate' : 'attr']({ // #1054
  14955. x: plotX - radius,
  14956. y: plotY - radius
  14957. });
  14958. }
  14959. }
  14960. if (stateMarkerGraphic) {
  14961. stateMarkerGraphic[state && chart.isInsidePlot(plotX, plotY, chart.inverted) ? 'show' : 'hide'](); // #2450
  14962. }
  14963. }
  14964. // Show me your halo
  14965. haloOptions = stateOptions[state] && stateOptions[state].halo;
  14966. if (haloOptions && haloOptions.size) {
  14967. if (!halo) {
  14968. series.halo = halo = chart.renderer.path()
  14969. .add(series.seriesGroup);
  14970. }
  14971. halo.attr(extend({
  14972. fill: Color(point.color || series.color).setOpacity(haloOptions.opacity).get()
  14973. }, haloOptions.attributes))[move ? 'animate' : 'attr']({
  14974. d: point.haloPath(haloOptions.size)
  14975. });
  14976. } else if (halo) {
  14977. halo.attr({ d: [] });
  14978. }
  14979. point.state = state;
  14980. },
  14981. haloPath: function (size) {
  14982. var series = this.series,
  14983. chart = series.chart,
  14984. plotBox = series.getPlotBox(),
  14985. inverted = chart.inverted;
  14986. return chart.renderer.symbols.circle(
  14987. plotBox.translateX + (inverted ? series.yAxis.len - this.plotY : this.plotX) - size,
  14988. plotBox.translateY + (inverted ? series.xAxis.len - this.plotX : this.plotY) - size,
  14989. size * 2,
  14990. size * 2
  14991. );
  14992. }
  14993. });
  14994. /*
  14995. * Extend the Series object with interaction
  14996. */
  14997. extend(Series.prototype, {
  14998. /**
  14999. * Series mouse over handler
  15000. */
  15001. onMouseOver: function () {
  15002. var series = this,
  15003. chart = series.chart,
  15004. hoverSeries = chart.hoverSeries;
  15005. // set normal state to previous series
  15006. if (hoverSeries && hoverSeries !== series) {
  15007. hoverSeries.onMouseOut();
  15008. }
  15009. // trigger the event, but to save processing time,
  15010. // only if defined
  15011. if (series.options.events.mouseOver) {
  15012. fireEvent(series, 'mouseOver');
  15013. }
  15014. // hover this
  15015. series.setState(HOVER_STATE);
  15016. chart.hoverSeries = series;
  15017. },
  15018. /**
  15019. * Series mouse out handler
  15020. */
  15021. onMouseOut: function () {
  15022. // trigger the event only if listeners exist
  15023. var series = this,
  15024. options = series.options,
  15025. chart = series.chart,
  15026. tooltip = chart.tooltip,
  15027. hoverPoint = chart.hoverPoint;
  15028. // trigger mouse out on the point, which must be in this series
  15029. if (hoverPoint) {
  15030. hoverPoint.onMouseOut();
  15031. }
  15032. // fire the mouse out event
  15033. if (series && options.events.mouseOut) {
  15034. fireEvent(series, 'mouseOut');
  15035. }
  15036. // hide the tooltip
  15037. if (tooltip && !options.stickyTracking && (!tooltip.shared || series.noSharedTooltip)) {
  15038. tooltip.hide();
  15039. }
  15040. // set normal state
  15041. series.setState();
  15042. chart.hoverSeries = null;
  15043. },
  15044. /**
  15045. * Set the state of the graph
  15046. */
  15047. setState: function (state) {
  15048. var series = this,
  15049. options = series.options,
  15050. graph = series.graph,
  15051. graphNeg = series.graphNeg,
  15052. stateOptions = options.states,
  15053. lineWidth = options.lineWidth,
  15054. attribs;
  15055. state = state || NORMAL_STATE;
  15056. if (series.state !== state) {
  15057. series.state = state;
  15058. if (stateOptions[state] && stateOptions[state].enabled === false) {
  15059. return;
  15060. }
  15061. if (state) {
  15062. lineWidth = stateOptions[state].lineWidth || lineWidth + (stateOptions[state].lineWidthPlus || 0);
  15063. }
  15064. if (graph && !graph.dashstyle) { // hover is turned off for dashed lines in VML
  15065. attribs = {
  15066. 'stroke-width': lineWidth
  15067. };
  15068. // use attr because animate will cause any other animation on the graph to stop
  15069. graph.attr(attribs);
  15070. if (graphNeg) {
  15071. graphNeg.attr(attribs);
  15072. }
  15073. }
  15074. }
  15075. },
  15076. /**
  15077. * Set the visibility of the graph
  15078. *
  15079. * @param vis {Boolean} True to show the series, false to hide. If UNDEFINED,
  15080. * the visibility is toggled.
  15081. */
  15082. setVisible: function (vis, redraw) {
  15083. var series = this,
  15084. chart = series.chart,
  15085. legendItem = series.legendItem,
  15086. showOrHide,
  15087. ignoreHiddenSeries = chart.options.chart.ignoreHiddenSeries,
  15088. oldVisibility = series.visible;
  15089. // if called without an argument, toggle visibility
  15090. series.visible = vis = series.userOptions.visible = vis === UNDEFINED ? !oldVisibility : vis;
  15091. showOrHide = vis ? 'show' : 'hide';
  15092. // show or hide elements
  15093. each(['group', 'dataLabelsGroup', 'markerGroup', 'tracker'], function (key) {
  15094. if (series[key]) {
  15095. series[key][showOrHide]();
  15096. }
  15097. });
  15098. // hide tooltip (#1361)
  15099. if (chart.hoverSeries === series) {
  15100. series.onMouseOut();
  15101. }
  15102. if (legendItem) {
  15103. chart.legend.colorizeItem(series, vis);
  15104. }
  15105. // rescale or adapt to resized chart
  15106. series.isDirty = true;
  15107. // in a stack, all other series are affected
  15108. if (series.options.stacking) {
  15109. each(chart.series, function (otherSeries) {
  15110. if (otherSeries.options.stacking && otherSeries.visible) {
  15111. otherSeries.isDirty = true;
  15112. }
  15113. });
  15114. }
  15115. // show or hide linked series
  15116. each(series.linkedSeries, function (otherSeries) {
  15117. otherSeries.setVisible(vis, false);
  15118. });
  15119. if (ignoreHiddenSeries) {
  15120. chart.isDirtyBox = true;
  15121. }
  15122. if (redraw !== false) {
  15123. chart.redraw();
  15124. }
  15125. fireEvent(series, showOrHide);
  15126. },
  15127. /**
  15128. * Memorize tooltip texts and positions
  15129. */
  15130. setTooltipPoints: function (renew) {
  15131. var series = this,
  15132. points = [],
  15133. pointsLength,
  15134. low,
  15135. high,
  15136. xAxis = series.xAxis,
  15137. xExtremes = xAxis && xAxis.getExtremes(),
  15138. axisLength = xAxis ? (xAxis.tooltipLen || xAxis.len) : series.chart.plotSizeX, // tooltipLen and tooltipPosName used in polar
  15139. point,
  15140. pointX,
  15141. nextPoint,
  15142. i,
  15143. tooltipPoints = []; // a lookup array for each pixel in the x dimension
  15144. // don't waste resources if tracker is disabled
  15145. if (series.options.enableMouseTracking === false || series.singularTooltips) {
  15146. return;
  15147. }
  15148. // renew
  15149. if (renew) {
  15150. series.tooltipPoints = null;
  15151. }
  15152. // concat segments to overcome null values
  15153. each(series.segments || series.points, function (segment) {
  15154. points = points.concat(segment);
  15155. });
  15156. // Reverse the points in case the X axis is reversed
  15157. if (xAxis && xAxis.reversed) {
  15158. points = points.reverse();
  15159. }
  15160. // Polar needs additional shaping
  15161. if (series.orderTooltipPoints) {
  15162. series.orderTooltipPoints(points);
  15163. }
  15164. // Assign each pixel position to the nearest point
  15165. pointsLength = points.length;
  15166. for (i = 0; i < pointsLength; i++) {
  15167. point = points[i];
  15168. pointX = point.x;
  15169. if (pointX >= xExtremes.min && pointX <= xExtremes.max) { // #1149
  15170. nextPoint = points[i + 1];
  15171. // Set this range's low to the last range's high plus one
  15172. low = high === UNDEFINED ? 0 : high + 1;
  15173. // Now find the new high
  15174. high = points[i + 1] ?
  15175. mathMin(mathMax(0, mathFloor( // #2070
  15176. (point.clientX + (nextPoint ? (nextPoint.wrappedClientX || nextPoint.clientX) : axisLength)) / 2
  15177. )), axisLength) :
  15178. axisLength;
  15179. while (low >= 0 && low <= high) {
  15180. tooltipPoints[low++] = point;
  15181. }
  15182. }
  15183. }
  15184. series.tooltipPoints = tooltipPoints;
  15185. },
  15186. /**
  15187. * Show the graph
  15188. */
  15189. show: function () {
  15190. this.setVisible(true);
  15191. },
  15192. /**
  15193. * Hide the graph
  15194. */
  15195. hide: function () {
  15196. this.setVisible(false);
  15197. },
  15198. /**
  15199. * Set the selected state of the graph
  15200. *
  15201. * @param selected {Boolean} True to select the series, false to unselect. If
  15202. * UNDEFINED, the selection state is toggled.
  15203. */
  15204. select: function (selected) {
  15205. var series = this;
  15206. // if called without an argument, toggle
  15207. series.selected = selected = (selected === UNDEFINED) ? !series.selected : selected;
  15208. if (series.checkbox) {
  15209. series.checkbox.checked = selected;
  15210. }
  15211. fireEvent(series, selected ? 'select' : 'unselect');
  15212. },
  15213. drawTracker: TrackerMixin.drawTrackerGraph
  15214. });
  15215. // global variables
  15216. extend(Highcharts, {
  15217. // Constructors
  15218. Axis: Axis,
  15219. Chart: Chart,
  15220. Color: Color,
  15221. Point: Point,
  15222. Tick: Tick,
  15223. Renderer: Renderer,
  15224. Series: Series,
  15225. SVGElement: SVGElement,
  15226. SVGRenderer: SVGRenderer,
  15227. // Various
  15228. arrayMin: arrayMin,
  15229. arrayMax: arrayMax,
  15230. charts: charts,
  15231. dateFormat: dateFormat,
  15232. format: format,
  15233. pathAnim: pathAnim,
  15234. getOptions: getOptions,
  15235. hasBidiBug: hasBidiBug,
  15236. isTouchDevice: isTouchDevice,
  15237. numberFormat: numberFormat,
  15238. seriesTypes: seriesTypes,
  15239. setOptions: setOptions,
  15240. addEvent: addEvent,
  15241. removeEvent: removeEvent,
  15242. createElement: createElement,
  15243. discardElement: discardElement,
  15244. css: css,
  15245. each: each,
  15246. extend: extend,
  15247. map: map,
  15248. merge: merge,
  15249. pick: pick,
  15250. splat: splat,
  15251. extendClass: extendClass,
  15252. pInt: pInt,
  15253. wrap: wrap,
  15254. svg: hasSVG,
  15255. canvas: useCanVG,
  15256. vml: !hasSVG && !useCanVG,
  15257. product: PRODUCT,
  15258. version: VERSION
  15259. });
  15260. }());