highcharts.src.js 611 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991
  1. // ==ClosureCompiler==
  2. // @compilation_level SIMPLE_OPTIMIZATIONS
  3. /**
  4. * @license Highcharts JS v3.0.6 (2013-10-04)
  5. *
  6. * (c) 2009-2013 Torstein Hønsi
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. // JSLint options:
  11. /*global Highcharts, document, window, navigator, setInterval, clearInterval, clearTimeout, setTimeout, location, jQuery, $, console, each, grep */
  12. (function () {
  13. // encapsulated variables
  14. var UNDEFINED,
  15. doc = document,
  16. win = window,
  17. math = Math,
  18. mathRound = math.round,
  19. mathFloor = math.floor,
  20. mathCeil = math.ceil,
  21. mathMax = math.max,
  22. mathMin = math.min,
  23. mathAbs = math.abs,
  24. mathCos = math.cos,
  25. mathSin = math.sin,
  26. mathPI = math.PI,
  27. deg2rad = mathPI * 2 / 360,
  28. // some variables
  29. userAgent = navigator.userAgent,
  30. isOpera = win.opera,
  31. isIE = /msie/i.test(userAgent) && !isOpera,
  32. docMode8 = doc.documentMode === 8,
  33. isWebKit = /AppleWebKit/.test(userAgent),
  34. isFirefox = /Firefox/.test(userAgent),
  35. isTouchDevice = /(Mobile|Android|Windows Phone)/.test(userAgent),
  36. SVG_NS = 'http://www.w3.org/2000/svg',
  37. hasSVG = !!doc.createElementNS && !!doc.createElementNS(SVG_NS, 'svg').createSVGRect,
  38. hasBidiBug = isFirefox && parseInt(userAgent.split('Firefox/')[1], 10) < 4, // issue #38
  39. useCanVG = !hasSVG && !isIE && !!doc.createElement('canvas').getContext,
  40. Renderer,
  41. hasTouch = doc.documentElement.ontouchstart !== UNDEFINED,
  42. symbolSizes = {},
  43. idCounter = 0,
  44. garbageBin,
  45. defaultOptions,
  46. dateFormat, // function
  47. globalAnimation,
  48. pathAnim,
  49. timeUnits,
  50. noop = function () {
  51. },
  52. charts = [],
  53. PRODUCT = 'Highcharts',
  54. VERSION = '3.0.6',
  55. // some constants for frequently used strings
  56. DIV = 'div',
  57. ABSOLUTE = 'absolute',
  58. RELATIVE = 'relative',
  59. HIDDEN = 'hidden',
  60. PREFIX = 'highcharts-',
  61. VISIBLE = 'visible',
  62. PX = 'px',
  63. NONE = 'none',
  64. M = 'M',
  65. L = 'L',
  66. /*
  67. * Empirical lowest possible opacities for TRACKER_FILL
  68. * IE6: 0.002
  69. * IE7: 0.002
  70. * IE8: 0.002
  71. * IE9: 0.00000000001 (unlimited)
  72. * IE10: 0.0001 (exporting only)
  73. * FF: 0.00000000001 (unlimited)
  74. * Chrome: 0.000001
  75. * Safari: 0.000001
  76. * Opera: 0.00000000001 (unlimited)
  77. */
  78. TRACKER_FILL = 'rgba(192,192,192,' + (hasSVG ? 0.0001 : 0.002) + ')', // invisible but clickable
  79. //TRACKER_FILL = 'rgba(192,192,192,0.5)',
  80. NORMAL_STATE = '',
  81. HOVER_STATE = 'hover',
  82. SELECT_STATE = 'select',
  83. MILLISECOND = 'millisecond',
  84. SECOND = 'second',
  85. MINUTE = 'minute',
  86. HOUR = 'hour',
  87. DAY = 'day',
  88. WEEK = 'week',
  89. MONTH = 'month',
  90. YEAR = 'year',
  91. // constants for attributes
  92. LINEAR_GRADIENT = 'linearGradient',
  93. STOPS = 'stops',
  94. STROKE_WIDTH = 'stroke-width',
  95. // time methods, changed based on whether or not UTC is used
  96. makeTime,
  97. getMinutes,
  98. getHours,
  99. getDay,
  100. getDate,
  101. getMonth,
  102. getFullYear,
  103. setMinutes,
  104. setHours,
  105. setDate,
  106. setMonth,
  107. setFullYear,
  108. // lookup over the types and the associated classes
  109. seriesTypes = {};
  110. // The Highcharts namespace
  111. win.Highcharts = win.Highcharts ? error(16, true) : {};
  112. /**
  113. * Extend an object with the members of another
  114. * @param {Object} a The object to be extended
  115. * @param {Object} b The object to add to the first one
  116. */
  117. function extend(a, b) {
  118. var n;
  119. if (!a) {
  120. a = {};
  121. }
  122. for (n in b) {
  123. a[n] = b[n];
  124. }
  125. return a;
  126. }
  127. /**
  128. * Deep merge two or more objects and return a third object.
  129. * Previously this function redirected to jQuery.extend(true), but this had two limitations.
  130. * First, it deep merged arrays, which lead to workarounds in Highcharts. Second,
  131. * it copied properties from extended prototypes.
  132. */
  133. function merge() {
  134. var i,
  135. len = arguments.length,
  136. ret = {},
  137. doCopy = function (copy, original) {
  138. var value, key;
  139. // An object is replacing a primitive
  140. if (typeof copy !== 'object') {
  141. copy = {};
  142. }
  143. for (key in original) {
  144. if (original.hasOwnProperty(key)) {
  145. value = original[key];
  146. // Copy the contents of objects, but not arrays or DOM nodes
  147. if (value && typeof value === 'object' && Object.prototype.toString.call(value) !== '[object Array]'
  148. && typeof value.nodeType !== 'number') {
  149. copy[key] = doCopy(copy[key] || {}, value);
  150. // Primitives and arrays are copied over directly
  151. } else {
  152. copy[key] = original[key];
  153. }
  154. }
  155. }
  156. return copy;
  157. };
  158. // For each argument, extend the return
  159. for (i = 0; i < len; i++) {
  160. ret = doCopy(ret, arguments[i]);
  161. }
  162. return ret;
  163. }
  164. /**
  165. * Take an array and turn into a hash with even number arguments as keys and odd numbers as
  166. * values. Allows creating constants for commonly used style properties, attributes etc.
  167. * Avoid it in performance critical situations like looping
  168. */
  169. function hash() {
  170. var i = 0,
  171. args = arguments,
  172. length = args.length,
  173. obj = {};
  174. for (; i < length; i++) {
  175. obj[args[i++]] = args[i];
  176. }
  177. return obj;
  178. }
  179. /**
  180. * Shortcut for parseInt
  181. * @param {Object} s
  182. * @param {Number} mag Magnitude
  183. */
  184. function pInt(s, mag) {
  185. return parseInt(s, mag || 10);
  186. }
  187. /**
  188. * Check for string
  189. * @param {Object} s
  190. */
  191. function isString(s) {
  192. return typeof s === 'string';
  193. }
  194. /**
  195. * Check for object
  196. * @param {Object} obj
  197. */
  198. function isObject(obj) {
  199. return typeof obj === 'object';
  200. }
  201. /**
  202. * Check for array
  203. * @param {Object} obj
  204. */
  205. function isArray(obj) {
  206. return Object.prototype.toString.call(obj) === '[object Array]';
  207. }
  208. /**
  209. * Check for number
  210. * @param {Object} n
  211. */
  212. function isNumber(n) {
  213. return typeof n === 'number';
  214. }
  215. function log2lin(num) {
  216. return math.log(num) / math.LN10;
  217. }
  218. function lin2log(num) {
  219. return math.pow(10, num);
  220. }
  221. /**
  222. * Remove last occurence of an item from an array
  223. * @param {Array} arr
  224. * @param {Mixed} item
  225. */
  226. function erase(arr, item) {
  227. var i = arr.length;
  228. while (i--) {
  229. if (arr[i] === item) {
  230. arr.splice(i, 1);
  231. break;
  232. }
  233. }
  234. //return arr;
  235. }
  236. /**
  237. * Returns true if the object is not null or undefined. Like MooTools' $.defined.
  238. * @param {Object} obj
  239. */
  240. function defined(obj) {
  241. return obj !== UNDEFINED && obj !== null;
  242. }
  243. /**
  244. * Set or get an attribute or an object of attributes. Can't use jQuery attr because
  245. * it attempts to set expando properties on the SVG element, which is not allowed.
  246. *
  247. * @param {Object} elem The DOM element to receive the attribute(s)
  248. * @param {String|Object} prop The property or an abject of key-value pairs
  249. * @param {String} value The value if a single property is set
  250. */
  251. function attr(elem, prop, value) {
  252. var key,
  253. setAttribute = 'setAttribute',
  254. ret;
  255. // if the prop is a string
  256. if (isString(prop)) {
  257. // set the value
  258. if (defined(value)) {
  259. elem[setAttribute](prop, value);
  260. // get the value
  261. } else if (elem && elem.getAttribute) { // elem not defined when printing pie demo...
  262. ret = elem.getAttribute(prop);
  263. }
  264. // else if prop is defined, it is a hash of key/value pairs
  265. } else if (defined(prop) && isObject(prop)) {
  266. for (key in prop) {
  267. elem[setAttribute](key, prop[key]);
  268. }
  269. }
  270. return ret;
  271. }
  272. /**
  273. * Check if an element is an array, and if not, make it into an array. Like
  274. * MooTools' $.splat.
  275. */
  276. function splat(obj) {
  277. return isArray(obj) ? obj : [obj];
  278. }
  279. /**
  280. * Return the first value that is defined. Like MooTools' $.pick.
  281. */
  282. function pick() {
  283. var args = arguments,
  284. i,
  285. arg,
  286. length = args.length;
  287. for (i = 0; i < length; i++) {
  288. arg = args[i];
  289. if (typeof arg !== 'undefined' && arg !== null) {
  290. return arg;
  291. }
  292. }
  293. }
  294. /**
  295. * Set CSS on a given element
  296. * @param {Object} el
  297. * @param {Object} styles Style object with camel case property names
  298. */
  299. function css(el, styles) {
  300. if (isIE) {
  301. if (styles && styles.opacity !== UNDEFINED) {
  302. styles.filter = 'alpha(opacity=' + (styles.opacity * 100) + ')';
  303. }
  304. }
  305. extend(el.style, styles);
  306. }
  307. /**
  308. * Utility function to create element with attributes and styles
  309. * @param {Object} tag
  310. * @param {Object} attribs
  311. * @param {Object} styles
  312. * @param {Object} parent
  313. * @param {Object} nopad
  314. */
  315. function createElement(tag, attribs, styles, parent, nopad) {
  316. var el = doc.createElement(tag);
  317. if (attribs) {
  318. extend(el, attribs);
  319. }
  320. if (nopad) {
  321. css(el, {padding: 0, border: NONE, margin: 0});
  322. }
  323. if (styles) {
  324. css(el, styles);
  325. }
  326. if (parent) {
  327. parent.appendChild(el);
  328. }
  329. return el;
  330. }
  331. /**
  332. * Extend a prototyped class by new members
  333. * @param {Object} parent
  334. * @param {Object} members
  335. */
  336. function extendClass(parent, members) {
  337. var object = function () {
  338. };
  339. object.prototype = new parent();
  340. extend(object.prototype, members);
  341. return object;
  342. }
  343. /**
  344. * Format a number and return a string based on input settings
  345. * @param {Number} number The input number to format
  346. * @param {Number} decimals The amount of decimals
  347. * @param {String} decPoint The decimal point, defaults to the one given in the lang options
  348. * @param {String} thousandsSep The thousands separator, defaults to the one given in the lang options
  349. */
  350. function numberFormat(number, decimals, decPoint, thousandsSep) {
  351. var lang = defaultOptions.lang,
  352. // http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_number_format/
  353. n = +number || 0,
  354. c = decimals === -1 ?
  355. (n.toString().split('.')[1] || '').length : // preserve decimals
  356. (isNaN(decimals = mathAbs(decimals)) ? 2 : decimals),
  357. d = decPoint === undefined ? lang.decimalPoint : decPoint,
  358. t = thousandsSep === undefined ? lang.thousandsSep : thousandsSep,
  359. s = n < 0 ? "-" : "",
  360. i = String(pInt(n = mathAbs(n).toFixed(c))),
  361. j = i.length > 3 ? i.length % 3 : 0;
  362. return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) +
  363. (c ? d + mathAbs(n - i).toFixed(c).slice(2) : "");
  364. }
  365. /**
  366. * Pad a string to a given length by adding 0 to the beginning
  367. * @param {Number} number
  368. * @param {Number} length
  369. */
  370. function pad(number, length) {
  371. // Create an array of the remaining length +1 and join it with 0's
  372. return new Array((length || 2) + 1 - String(number).length).join(0) + number;
  373. }
  374. /**
  375. * Wrap a method with extended functionality, preserving the original function
  376. * @param {Object} obj The context object that the method belongs to
  377. * @param {String} method The name of the method to extend
  378. * @param {Function} func A wrapper function callback. This function is called with the same arguments
  379. * as the original function, except that the original function is unshifted and passed as the first
  380. * argument.
  381. */
  382. function wrap(obj, method, func) {
  383. var proceed = obj[method];
  384. obj[method] = function () {
  385. var args = Array.prototype.slice.call(arguments);
  386. args.unshift(proceed);
  387. return func.apply(this, args);
  388. };
  389. }
  390. /**
  391. * Based on http://www.php.net/manual/en/function.strftime.php
  392. * @param {String} format
  393. * @param {Number} timestamp
  394. * @param {Boolean} capitalize
  395. */
  396. dateFormat = function (format, timestamp, capitalize) {
  397. if (!defined(timestamp) || isNaN(timestamp)) {
  398. return 'Invalid date';
  399. }
  400. format = pick(format, '%Y-%m-%d %H:%M:%S');
  401. var date = new Date(timestamp),
  402. key, // used in for constuct below
  403. // get the basic time values
  404. hours = date[getHours](),
  405. day = date[getDay](),
  406. dayOfMonth = date[getDate](),
  407. month = date[getMonth](),
  408. fullYear = date[getFullYear](),
  409. lang = defaultOptions.lang,
  410. langWeekdays = lang.weekdays,
  411. // List all format keys. Custom formats can be added from the outside.
  412. replacements = extend({
  413. // Day
  414. 'a': langWeekdays[day].substr(0, 3), // Short weekday, like 'Mon'
  415. 'A': langWeekdays[day], // Long weekday, like 'Monday'
  416. 'd': pad(dayOfMonth), // Two digit day of the month, 01 to 31
  417. 'e': dayOfMonth, // Day of the month, 1 through 31
  418. // Week (none implemented)
  419. //'W': weekNumber(),
  420. // Month
  421. 'b': lang.shortMonths[month], // Short month, like 'Jan'
  422. 'B': lang.months[month], // Long month, like 'January'
  423. 'm': pad(month + 1), // Two digit month number, 01 through 12
  424. // Year
  425. 'y': fullYear.toString().substr(2, 2), // Two digits year, like 09 for 2009
  426. 'Y': fullYear, // Four digits year, like 2009
  427. // Time
  428. 'H': pad(hours), // Two digits hours in 24h format, 00 through 23
  429. 'I': pad((hours % 12) || 12), // Two digits hours in 12h format, 00 through 11
  430. 'l': (hours % 12) || 12, // Hours in 12h format, 1 through 12
  431. 'M': pad(date[getMinutes]()), // Two digits minutes, 00 through 59
  432. 'p': hours < 12 ? 'AM' : 'PM', // Upper case AM or PM
  433. 'P': hours < 12 ? 'am' : 'pm', // Lower case AM or PM
  434. 'S': pad(date.getSeconds()), // Two digits seconds, 00 through 59
  435. 'L': pad(mathRound(timestamp % 1000), 3) // Milliseconds (naming from Ruby)
  436. }, Highcharts.dateFormats);
  437. // do the replaces
  438. for (key in replacements) {
  439. while (format.indexOf('%' + key) !== -1) { // regex would do it in one line, but this is faster
  440. format = format.replace('%' + key, typeof replacements[key] === 'function' ? replacements[key](timestamp) : replacements[key]);
  441. }
  442. }
  443. // Optionally capitalize the string and return
  444. return capitalize ? format.substr(0, 1).toUpperCase() + format.substr(1) : format;
  445. };
  446. /**
  447. * Format a single variable. Similar to sprintf, without the % prefix.
  448. */
  449. function formatSingle(format, val) {
  450. var floatRegex = /f$/,
  451. decRegex = /\.([0-9])/,
  452. lang = defaultOptions.lang,
  453. decimals;
  454. if (floatRegex.test(format)) { // float
  455. decimals = format.match(decRegex);
  456. decimals = decimals ? decimals[1] : -1;
  457. val = numberFormat(
  458. val,
  459. decimals,
  460. lang.decimalPoint,
  461. format.indexOf(',') > -1 ? lang.thousandsSep : ''
  462. );
  463. } else {
  464. val = dateFormat(format, val);
  465. }
  466. return val;
  467. }
  468. /**
  469. * Format a string according to a subset of the rules of Python's String.format method.
  470. */
  471. function format(str, ctx) {
  472. var splitter = '{',
  473. isInside = false,
  474. segment,
  475. valueAndFormat,
  476. path,
  477. i,
  478. len,
  479. ret = [],
  480. val,
  481. index;
  482. while ((index = str.indexOf(splitter)) !== -1) {
  483. segment = str.slice(0, index);
  484. if (isInside) { // we're on the closing bracket looking back
  485. valueAndFormat = segment.split(':');
  486. path = valueAndFormat.shift().split('.'); // get first and leave format
  487. len = path.length;
  488. val = ctx;
  489. // Assign deeper paths
  490. for (i = 0; i < len; i++) {
  491. val = val[path[i]];
  492. }
  493. // Format the replacement
  494. if (valueAndFormat.length) {
  495. val = formatSingle(valueAndFormat.join(':'), val);
  496. }
  497. // Push the result and advance the cursor
  498. ret.push(val);
  499. } else {
  500. ret.push(segment);
  501. }
  502. str = str.slice(index + 1); // the rest
  503. isInside = !isInside; // toggle
  504. splitter = isInside ? '}' : '{'; // now look for next matching bracket
  505. }
  506. ret.push(str);
  507. return ret.join('');
  508. }
  509. /**
  510. * Get the magnitude of a number
  511. */
  512. function getMagnitude(num) {
  513. return math.pow(10, mathFloor(math.log(num) / math.LN10));
  514. }
  515. /**
  516. * Take an interval and normalize it to multiples of 1, 2, 2.5 and 5
  517. * @param {Number} interval
  518. * @param {Array} multiples
  519. * @param {Number} magnitude
  520. * @param {Object} options
  521. */
  522. function normalizeTickInterval(interval, multiples, magnitude, options) {
  523. var normalized, i;
  524. // round to a tenfold of 1, 2, 2.5 or 5
  525. magnitude = pick(magnitude, 1);
  526. normalized = interval / magnitude;
  527. // multiples for a linear scale
  528. if (!multiples) {
  529. multiples = [1, 2, 2.5, 5, 10];
  530. // the allowDecimals option
  531. if (options && options.allowDecimals === false) {
  532. if (magnitude === 1) {
  533. multiples = [1, 2, 5, 10];
  534. } else if (magnitude <= 0.1) {
  535. multiples = [1 / magnitude];
  536. }
  537. }
  538. }
  539. // normalize the interval to the nearest multiple
  540. for (i = 0; i < multiples.length; i++) {
  541. interval = multiples[i];
  542. if (normalized <= (multiples[i] + (multiples[i + 1] || multiples[i])) / 2) {
  543. break;
  544. }
  545. }
  546. // multiply back to the correct magnitude
  547. interval *= magnitude;
  548. return interval;
  549. }
  550. /**
  551. * Get a normalized tick interval for dates. Returns a configuration object with
  552. * unit range (interval), count and name. Used to prepare data for getTimeTicks.
  553. * Previously this logic was part of getTimeTicks, but as getTimeTicks now runs
  554. * of segments in stock charts, the normalizing logic was extracted in order to
  555. * prevent it for running over again for each segment having the same interval.
  556. * #662, #697.
  557. */
  558. function normalizeTimeTickInterval(tickInterval, unitsOption) {
  559. var units = unitsOption || [[
  560. MILLISECOND, // unit name
  561. [1, 2, 5, 10, 20, 25, 50, 100, 200, 500] // allowed multiples
  562. ], [
  563. SECOND,
  564. [1, 2, 5, 10, 15, 30]
  565. ], [
  566. MINUTE,
  567. [1, 2, 5, 10, 15, 30]
  568. ], [
  569. HOUR,
  570. [1, 2, 3, 4, 6, 8, 12]
  571. ], [
  572. DAY,
  573. [1, 2]
  574. ], [
  575. WEEK,
  576. [1, 2]
  577. ], [
  578. MONTH,
  579. [1, 2, 3, 4, 6]
  580. ], [
  581. YEAR,
  582. null
  583. ]],
  584. unit = units[units.length - 1], // default unit is years
  585. interval = timeUnits[unit[0]],
  586. multiples = unit[1],
  587. count,
  588. i;
  589. // loop through the units to find the one that best fits the tickInterval
  590. for (i = 0; i < units.length; i++) {
  591. unit = units[i];
  592. interval = timeUnits[unit[0]];
  593. multiples = unit[1];
  594. if (units[i + 1]) {
  595. // lessThan is in the middle between the highest multiple and the next unit.
  596. var lessThan = (interval * multiples[multiples.length - 1] +
  597. timeUnits[units[i + 1][0]]) / 2;
  598. // break and keep the current unit
  599. if (tickInterval <= lessThan) {
  600. break;
  601. }
  602. }
  603. }
  604. // prevent 2.5 years intervals, though 25, 250 etc. are allowed
  605. if (interval === timeUnits[YEAR] && tickInterval < 5 * interval) {
  606. multiples = [1, 2, 5];
  607. }
  608. // get the count
  609. count = normalizeTickInterval(
  610. tickInterval / interval,
  611. multiples,
  612. unit[0] === YEAR ? getMagnitude(tickInterval / interval) : 1 // #1913
  613. );
  614. return {
  615. unitRange: interval,
  616. count: count,
  617. unitName: unit[0]
  618. };
  619. }
  620. /**
  621. * Set the tick positions to a time unit that makes sense, for example
  622. * on the first of each month or on every Monday. Return an array
  623. * with the time positions. Used in datetime axes as well as for grouping
  624. * data on a datetime axis.
  625. *
  626. * @param {Object} normalizedInterval The interval in axis values (ms) and the count
  627. * @param {Number} min The minimum in axis values
  628. * @param {Number} max The maximum in axis values
  629. * @param {Number} startOfWeek
  630. */
  631. function getTimeTicks(normalizedInterval, min, max, startOfWeek) {
  632. var tickPositions = [],
  633. i,
  634. higherRanks = {},
  635. useUTC = defaultOptions.global.useUTC,
  636. minYear, // used in months and years as a basis for Date.UTC()
  637. minDate = new Date(min),
  638. interval = normalizedInterval.unitRange,
  639. count = normalizedInterval.count;
  640. if (defined(min)) { // #1300
  641. if (interval >= timeUnits[SECOND]) { // second
  642. minDate.setMilliseconds(0);
  643. minDate.setSeconds(interval >= timeUnits[MINUTE] ? 0 :
  644. count * mathFloor(minDate.getSeconds() / count));
  645. }
  646. if (interval >= timeUnits[MINUTE]) { // minute
  647. minDate[setMinutes](interval >= timeUnits[HOUR] ? 0 :
  648. count * mathFloor(minDate[getMinutes]() / count));
  649. }
  650. if (interval >= timeUnits[HOUR]) { // hour
  651. minDate[setHours](interval >= timeUnits[DAY] ? 0 :
  652. count * mathFloor(minDate[getHours]() / count));
  653. }
  654. if (interval >= timeUnits[DAY]) { // day
  655. minDate[setDate](interval >= timeUnits[MONTH] ? 1 :
  656. count * mathFloor(minDate[getDate]() / count));
  657. }
  658. if (interval >= timeUnits[MONTH]) { // month
  659. minDate[setMonth](interval >= timeUnits[YEAR] ? 0 :
  660. count * mathFloor(minDate[getMonth]() / count));
  661. minYear = minDate[getFullYear]();
  662. }
  663. if (interval >= timeUnits[YEAR]) { // year
  664. minYear -= minYear % count;
  665. minDate[setFullYear](minYear);
  666. }
  667. // week is a special case that runs outside the hierarchy
  668. if (interval === timeUnits[WEEK]) {
  669. // get start of current week, independent of count
  670. minDate[setDate](minDate[getDate]() - minDate[getDay]() +
  671. pick(startOfWeek, 1));
  672. }
  673. // get tick positions
  674. i = 1;
  675. minYear = minDate[getFullYear]();
  676. var time = minDate.getTime(),
  677. minMonth = minDate[getMonth](),
  678. minDateDate = minDate[getDate](),
  679. timezoneOffset = useUTC ?
  680. 0 :
  681. (24 * 3600 * 1000 + minDate.getTimezoneOffset() * 60 * 1000) % (24 * 3600 * 1000); // #950
  682. // iterate and add tick positions at appropriate values
  683. while (time < max) {
  684. tickPositions.push(time);
  685. // if the interval is years, use Date.UTC to increase years
  686. if (interval === timeUnits[YEAR]) {
  687. time = makeTime(minYear + i * count, 0);
  688. // if the interval is months, use Date.UTC to increase months
  689. } else if (interval === timeUnits[MONTH]) {
  690. time = makeTime(minYear, minMonth + i * count);
  691. // if we're using global time, the interval is not fixed as it jumps
  692. // one hour at the DST crossover
  693. } else if (!useUTC && (interval === timeUnits[DAY] || interval === timeUnits[WEEK])) {
  694. time = makeTime(minYear, minMonth, minDateDate +
  695. i * count * (interval === timeUnits[DAY] ? 1 : 7));
  696. // else, the interval is fixed and we use simple addition
  697. } else {
  698. time += interval * count;
  699. }
  700. i++;
  701. }
  702. // push the last time
  703. tickPositions.push(time);
  704. // mark new days if the time is dividible by day (#1649, #1760)
  705. each(grep(tickPositions, function (time) {
  706. return interval <= timeUnits[HOUR] && time % timeUnits[DAY] === timezoneOffset;
  707. }), function (time) {
  708. higherRanks[time] = DAY;
  709. });
  710. }
  711. // record information on the chosen unit - for dynamic label formatter
  712. tickPositions.info = extend(normalizedInterval, {
  713. higherRanks: higherRanks,
  714. totalRange: interval * count
  715. });
  716. return tickPositions;
  717. }
  718. /**
  719. * Helper class that contains variuos counters that are local to the chart.
  720. */
  721. function ChartCounters() {
  722. this.color = 0;
  723. this.symbol = 0;
  724. }
  725. ChartCounters.prototype = {
  726. /**
  727. * Wraps the color counter if it reaches the specified length.
  728. */
  729. wrapColor: function (length) {
  730. if (this.color >= length) {
  731. this.color = 0;
  732. }
  733. },
  734. /**
  735. * Wraps the symbol counter if it reaches the specified length.
  736. */
  737. wrapSymbol: function (length) {
  738. if (this.symbol >= length) {
  739. this.symbol = 0;
  740. }
  741. }
  742. };
  743. /**
  744. * Utility method that sorts an object array and keeping the order of equal items.
  745. * ECMA script standard does not specify the behaviour when items are equal.
  746. */
  747. function stableSort(arr, sortFunction) {
  748. var length = arr.length,
  749. sortValue,
  750. i;
  751. // Add index to each item
  752. for (i = 0; i < length; i++) {
  753. arr[i].ss_i = i; // stable sort index
  754. }
  755. arr.sort(function (a, b) {
  756. sortValue = sortFunction(a, b);
  757. return sortValue === 0 ? a.ss_i - b.ss_i : sortValue;
  758. });
  759. // Remove index from items
  760. for (i = 0; i < length; i++) {
  761. delete arr[i].ss_i; // stable sort index
  762. }
  763. }
  764. /**
  765. * Non-recursive method to find the lowest member of an array. Math.min raises a maximum
  766. * call stack size exceeded error in Chrome when trying to apply more than 150.000 points. This
  767. * method is slightly slower, but safe.
  768. */
  769. function arrayMin(data) {
  770. var i = data.length,
  771. min = data[0];
  772. while (i--) {
  773. if (data[i] < min) {
  774. min = data[i];
  775. }
  776. }
  777. return min;
  778. }
  779. /**
  780. * Non-recursive method to find the lowest member of an array. Math.min raises a maximum
  781. * call stack size exceeded error in Chrome when trying to apply more than 150.000 points. This
  782. * method is slightly slower, but safe.
  783. */
  784. function arrayMax(data) {
  785. var i = data.length,
  786. max = data[0];
  787. while (i--) {
  788. if (data[i] > max) {
  789. max = data[i];
  790. }
  791. }
  792. return max;
  793. }
  794. /**
  795. * Utility method that destroys any SVGElement or VMLElement that are properties on the given object.
  796. * It loops all properties and invokes destroy if there is a destroy method. The property is
  797. * then delete'ed.
  798. * @param {Object} The object to destroy properties on
  799. * @param {Object} Exception, do not destroy this property, only delete it.
  800. */
  801. function destroyObjectProperties(obj, except) {
  802. var n;
  803. for (n in obj) {
  804. // If the object is non-null and destroy is defined
  805. if (obj[n] && obj[n] !== except && obj[n].destroy) {
  806. // Invoke the destroy
  807. obj[n].destroy();
  808. }
  809. // Delete the property from the object.
  810. delete obj[n];
  811. }
  812. }
  813. /**
  814. * Discard an element by moving it to the bin and delete
  815. * @param {Object} The HTML node to discard
  816. */
  817. function discardElement(element) {
  818. // create a garbage bin element, not part of the DOM
  819. if (!garbageBin) {
  820. garbageBin = createElement(DIV);
  821. }
  822. // move the node and empty bin
  823. if (element) {
  824. garbageBin.appendChild(element);
  825. }
  826. garbageBin.innerHTML = '';
  827. }
  828. /**
  829. * Provide error messages for debugging, with links to online explanation
  830. */
  831. function error(code, stop) {
  832. var msg = 'Highcharts error #' + code + ': www.highcharts.com/errors/' + code;
  833. if (stop) {
  834. throw msg;
  835. } else if (win.console) {
  836. console.log(msg);
  837. }
  838. }
  839. /**
  840. * Fix JS round off float errors
  841. * @param {Number} num
  842. */
  843. function correctFloat(num) {
  844. return parseFloat(
  845. num.toPrecision(14)
  846. );
  847. }
  848. /**
  849. * Set the global animation to either a given value, or fall back to the
  850. * given chart's animation option
  851. * @param {Object} animation
  852. * @param {Object} chart
  853. */
  854. function setAnimation(animation, chart) {
  855. globalAnimation = pick(animation, chart.animation);
  856. }
  857. /**
  858. * The time unit lookup
  859. */
  860. /*jslint white: true*/
  861. timeUnits = hash(
  862. MILLISECOND, 1,
  863. SECOND, 1000,
  864. MINUTE, 60000,
  865. HOUR, 3600000,
  866. DAY, 24 * 3600000,
  867. WEEK, 7 * 24 * 3600000,
  868. MONTH, 31 * 24 * 3600000,
  869. YEAR, 31556952000
  870. );
  871. /*jslint white: false*/
  872. /**
  873. * Path interpolation algorithm used across adapters
  874. */
  875. pathAnim = {
  876. /**
  877. * Prepare start and end values so that the path can be animated one to one
  878. */
  879. init: function (elem, fromD, toD) {
  880. fromD = fromD || '';
  881. var shift = elem.shift,
  882. bezier = fromD.indexOf('C') > -1,
  883. numParams = bezier ? 7 : 3,
  884. endLength,
  885. slice,
  886. i,
  887. start = fromD.split(' '),
  888. end = [].concat(toD), // copy
  889. startBaseLine,
  890. endBaseLine,
  891. sixify = function (arr) { // in splines make move points have six parameters like bezier curves
  892. i = arr.length;
  893. while (i--) {
  894. if (arr[i] === M) {
  895. arr.splice(i + 1, 0, arr[i + 1], arr[i + 2], arr[i + 1], arr[i + 2]);
  896. }
  897. }
  898. };
  899. if (bezier) {
  900. sixify(start);
  901. sixify(end);
  902. }
  903. // pull out the base lines before padding
  904. if (elem.isArea) {
  905. startBaseLine = start.splice(start.length - 6, 6);
  906. endBaseLine = end.splice(end.length - 6, 6);
  907. }
  908. // if shifting points, prepend a dummy point to the end path
  909. if (shift <= end.length / numParams && start.length === end.length) {
  910. while (shift--) {
  911. end = [].concat(end).splice(0, numParams).concat(end);
  912. }
  913. }
  914. elem.shift = 0; // reset for following animations
  915. // copy and append last point until the length matches the end length
  916. if (start.length) {
  917. endLength = end.length;
  918. while (start.length < endLength) {
  919. //bezier && sixify(start);
  920. slice = [].concat(start).splice(start.length - numParams, numParams);
  921. if (bezier) { // disable first control point
  922. slice[numParams - 6] = slice[numParams - 2];
  923. slice[numParams - 5] = slice[numParams - 1];
  924. }
  925. start = start.concat(slice);
  926. }
  927. }
  928. if (startBaseLine) { // append the base lines for areas
  929. start = start.concat(startBaseLine);
  930. end = end.concat(endBaseLine);
  931. }
  932. return [start, end];
  933. },
  934. /**
  935. * Interpolate each value of the path and return the array
  936. */
  937. step: function (start, end, pos, complete) {
  938. var ret = [],
  939. i = start.length,
  940. startVal;
  941. if (pos === 1) { // land on the final path without adjustment points appended in the ends
  942. ret = complete;
  943. } else if (i === end.length && pos < 1) {
  944. while (i--) {
  945. startVal = parseFloat(start[i]);
  946. ret[i] =
  947. isNaN(startVal) ? // a letter instruction like M or L
  948. start[i] :
  949. pos * (parseFloat(end[i] - startVal)) + startVal;
  950. }
  951. } else { // if animation is finished or length not matching, land on right value
  952. ret = end;
  953. }
  954. return ret;
  955. }
  956. };
  957. (function ($) {
  958. /**
  959. * The default HighchartsAdapter for jQuery
  960. */
  961. win.HighchartsAdapter = win.HighchartsAdapter || ($ && {
  962. /**
  963. * Initialize the adapter by applying some extensions to jQuery
  964. */
  965. init: function (pathAnim) {
  966. // extend the animate function to allow SVG animations
  967. var Fx = $.fx,
  968. Step = Fx.step,
  969. dSetter,
  970. Tween = $.Tween,
  971. propHooks = Tween && Tween.propHooks,
  972. opacityHook = $.cssHooks.opacity;
  973. /*jslint unparam: true*//* allow unused param x in this function */
  974. $.extend($.easing, {
  975. easeOutQuad: function (x, t, b, c, d) {
  976. return -c * (t /= d) * (t - 2) + b;
  977. }
  978. });
  979. /*jslint unparam: false*/
  980. // extend some methods to check for elem.attr, which means it is a Highcharts SVG object
  981. $.each(['cur', '_default', 'width', 'height', 'opacity'], function (i, fn) {
  982. var obj = Step,
  983. base,
  984. elem;
  985. // Handle different parent objects
  986. if (fn === 'cur') {
  987. obj = Fx.prototype; // 'cur', the getter, relates to Fx.prototype
  988. } else if (fn === '_default' && Tween) { // jQuery 1.8 model
  989. obj = propHooks[fn];
  990. fn = 'set';
  991. }
  992. // Overwrite the method
  993. base = obj[fn];
  994. if (base) { // step.width and step.height don't exist in jQuery < 1.7
  995. // create the extended function replacement
  996. obj[fn] = function (fx) {
  997. // Fx.prototype.cur does not use fx argument
  998. fx = i ? fx : this;
  999. // Don't run animations on textual properties like align (#1821)
  1000. if (fx.prop === 'align') {
  1001. return;
  1002. }
  1003. // shortcut
  1004. elem = fx.elem;
  1005. // Fx.prototype.cur returns the current value. The other ones are setters
  1006. // and returning a value has no effect.
  1007. return elem.attr ? // is SVG element wrapper
  1008. elem.attr(fx.prop, fn === 'cur' ? UNDEFINED : fx.now) : // apply the SVG wrapper's method
  1009. base.apply(this, arguments); // use jQuery's built-in method
  1010. };
  1011. }
  1012. });
  1013. // Extend the opacity getter, needed for fading opacity with IE9 and jQuery 1.10+
  1014. wrap(opacityHook, 'get', function (proceed, elem, computed) {
  1015. return elem.attr ? (elem.opacity || 0) : proceed.call(this, elem, computed);
  1016. });
  1017. // Define the setter function for d (path definitions)
  1018. dSetter = function (fx) {
  1019. var elem = fx.elem,
  1020. ends;
  1021. // Normally start and end should be set in state == 0, but sometimes,
  1022. // for reasons unknown, this doesn't happen. Perhaps state == 0 is skipped
  1023. // in these cases
  1024. if (!fx.started) {
  1025. ends = pathAnim.init(elem, elem.d, elem.toD);
  1026. fx.start = ends[0];
  1027. fx.end = ends[1];
  1028. fx.started = true;
  1029. }
  1030. // interpolate each value of the path
  1031. elem.attr('d', pathAnim.step(fx.start, fx.end, fx.pos, elem.toD));
  1032. };
  1033. // jQuery 1.8 style
  1034. if (Tween) {
  1035. propHooks.d = {
  1036. set: dSetter
  1037. };
  1038. // pre 1.8
  1039. } else {
  1040. // animate paths
  1041. Step.d = dSetter;
  1042. }
  1043. /**
  1044. * Utility for iterating over an array. Parameters are reversed compared to jQuery.
  1045. * @param {Array} arr
  1046. * @param {Function} fn
  1047. */
  1048. this.each = Array.prototype.forEach ?
  1049. function (arr, fn) { // modern browsers
  1050. return Array.prototype.forEach.call(arr, fn);
  1051. } :
  1052. function (arr, fn) { // legacy
  1053. var i = 0,
  1054. len = arr.length;
  1055. for (; i < len; i++) {
  1056. if (fn.call(arr[i], arr[i], i, arr) === false) {
  1057. return i;
  1058. }
  1059. }
  1060. };
  1061. /**
  1062. * Register Highcharts as a plugin in the respective framework
  1063. */
  1064. $.fn.highcharts = function () {
  1065. var constr = 'Chart', // default constructor
  1066. args = arguments,
  1067. options,
  1068. ret,
  1069. chart;
  1070. if (isString(args[0])) {
  1071. constr = args[0];
  1072. args = Array.prototype.slice.call(args, 1);
  1073. }
  1074. options = args[0];
  1075. // Create the chart
  1076. if (options !== UNDEFINED) {
  1077. /*jslint unused:false*/
  1078. options.chart = options.chart || {};
  1079. options.chart.renderTo = this[0];
  1080. chart = new Highcharts[constr](options, args[1]);
  1081. ret = this;
  1082. /*jslint unused:true*/
  1083. }
  1084. // When called without parameters or with the return argument, get a predefined chart
  1085. if (options === UNDEFINED) {
  1086. ret = charts[attr(this[0], 'data-highcharts-chart')];
  1087. }
  1088. return ret;
  1089. };
  1090. },
  1091. /**
  1092. * Downloads a script and executes a callback when done.
  1093. * @param {String} scriptLocation
  1094. * @param {Function} callback
  1095. */
  1096. getScript: $.getScript,
  1097. /**
  1098. * Return the index of an item in an array, or -1 if not found
  1099. */
  1100. inArray: $.inArray,
  1101. /**
  1102. * A direct link to jQuery methods. MooTools and Prototype adapters must be implemented for each case of method.
  1103. * @param {Object} elem The HTML element
  1104. * @param {String} method Which method to run on the wrapped element
  1105. */
  1106. adapterRun: function (elem, method) {
  1107. return $(elem)[method]();
  1108. },
  1109. /**
  1110. * Filter an array
  1111. */
  1112. grep: $.grep,
  1113. /**
  1114. * Map an array
  1115. * @param {Array} arr
  1116. * @param {Function} fn
  1117. */
  1118. map: function (arr, fn) {
  1119. //return jQuery.map(arr, fn);
  1120. var results = [],
  1121. i = 0,
  1122. len = arr.length;
  1123. for (; i < len; i++) {
  1124. results[i] = fn.call(arr[i], arr[i], i, arr);
  1125. }
  1126. return results;
  1127. },
  1128. /**
  1129. * Get the position of an element relative to the top left of the page
  1130. */
  1131. offset: function (el) {
  1132. return $(el).offset();
  1133. },
  1134. /**
  1135. * Add an event listener
  1136. * @param {Object} el A HTML element or custom object
  1137. * @param {String} event The event type
  1138. * @param {Function} fn The event handler
  1139. */
  1140. addEvent: function (el, event, fn) {
  1141. $(el).bind(event, fn);
  1142. },
  1143. /**
  1144. * Remove event added with addEvent
  1145. * @param {Object} el The object
  1146. * @param {String} eventType The event type. Leave blank to remove all events.
  1147. * @param {Function} handler The function to remove
  1148. */
  1149. removeEvent: function (el, eventType, handler) {
  1150. // workaround for jQuery issue with unbinding custom events:
  1151. // http://forum.jQuery.com/topic/javascript-error-when-unbinding-a-custom-event-using-jQuery-1-4-2
  1152. var func = doc.removeEventListener ? 'removeEventListener' : 'detachEvent';
  1153. if (doc[func] && el && !el[func]) {
  1154. el[func] = function () {
  1155. };
  1156. }
  1157. $(el).unbind(eventType, handler);
  1158. },
  1159. /**
  1160. * Fire an event on a custom object
  1161. * @param {Object} el
  1162. * @param {String} type
  1163. * @param {Object} eventArguments
  1164. * @param {Function} defaultFunction
  1165. */
  1166. fireEvent: function (el, type, eventArguments, defaultFunction) {
  1167. var event = $.Event(type),
  1168. detachedType = 'detached' + type,
  1169. defaultPrevented;
  1170. // Remove warnings in Chrome when accessing layerX and layerY. Although Highcharts
  1171. // never uses these properties, Chrome includes them in the default click event and
  1172. // raises the warning when they are copied over in the extend statement below.
  1173. //
  1174. // To avoid problems in IE (see #1010) where we cannot delete the properties and avoid
  1175. // testing if they are there (warning in chrome) the only option is to test if running IE.
  1176. if (!isIE && eventArguments) {
  1177. delete eventArguments.layerX;
  1178. delete eventArguments.layerY;
  1179. }
  1180. extend(event, eventArguments);
  1181. // Prevent jQuery from triggering the object method that is named the
  1182. // same as the event. For example, if the event is 'select', jQuery
  1183. // attempts calling el.select and it goes into a loop.
  1184. if (el[type]) {
  1185. el[detachedType] = el[type];
  1186. el[type] = null;
  1187. }
  1188. // Wrap preventDefault and stopPropagation in try/catch blocks in
  1189. // order to prevent JS errors when cancelling events on non-DOM
  1190. // objects. #615.
  1191. /*jslint unparam: true*/
  1192. $.each(['preventDefault', 'stopPropagation'], function (i, fn) {
  1193. var base = event[fn];
  1194. event[fn] = function () {
  1195. try {
  1196. base.call(event);
  1197. } catch (e) {
  1198. if (fn === 'preventDefault') {
  1199. defaultPrevented = true;
  1200. }
  1201. }
  1202. };
  1203. });
  1204. /*jslint unparam: false*/
  1205. // trigger it
  1206. $(el).trigger(event);
  1207. // attach the method
  1208. if (el[detachedType]) {
  1209. el[type] = el[detachedType];
  1210. el[detachedType] = null;
  1211. }
  1212. if (defaultFunction && !event.isDefaultPrevented() && !defaultPrevented) {
  1213. defaultFunction(event);
  1214. }
  1215. },
  1216. /**
  1217. * Extension method needed for MooTools
  1218. */
  1219. washMouseEvent: function (e) {
  1220. var ret = e.originalEvent || e;
  1221. // computed by jQuery, needed by IE8
  1222. if (ret.pageX === UNDEFINED) { // #1236
  1223. ret.pageX = e.pageX;
  1224. ret.pageY = e.pageY;
  1225. }
  1226. return ret;
  1227. },
  1228. /**
  1229. * Animate a HTML element or SVG element wrapper
  1230. * @param {Object} el
  1231. * @param {Object} params
  1232. * @param {Object} options jQuery-like animation options: duration, easing, callback
  1233. */
  1234. animate: function (el, params, options) {
  1235. var $el = $(el);
  1236. if (!el.style) {
  1237. el.style = {}; // #1881
  1238. }
  1239. if (params.d) {
  1240. el.toD = params.d; // keep the array form for paths, used in $.fx.step.d
  1241. params.d = 1; // because in jQuery, animating to an array has a different meaning
  1242. }
  1243. $el.stop();
  1244. if (params.opacity !== UNDEFINED && el.attr) {
  1245. params.opacity += 'px'; // force jQuery to use same logic as width and height (#2161)
  1246. }
  1247. $el.animate(params, options);
  1248. },
  1249. /**
  1250. * Stop running animation
  1251. */
  1252. stop: function (el) {
  1253. $(el).stop();
  1254. }
  1255. });
  1256. }(win.jQuery));
  1257. // check for a custom HighchartsAdapter defined prior to this file
  1258. var globalAdapter = win.HighchartsAdapter,
  1259. adapter = globalAdapter || {};
  1260. // Initialize the adapter
  1261. if (globalAdapter) {
  1262. globalAdapter.init.call(globalAdapter, pathAnim);
  1263. }
  1264. // Utility functions. If the HighchartsAdapter is not defined, adapter is an empty object
  1265. // and all the utility functions will be null. In that case they are populated by the
  1266. // default adapters below.
  1267. var adapterRun = adapter.adapterRun,
  1268. getScript = adapter.getScript,
  1269. inArray = adapter.inArray,
  1270. each = adapter.each,
  1271. grep = adapter.grep,
  1272. offset = adapter.offset,
  1273. map = adapter.map,
  1274. addEvent = adapter.addEvent,
  1275. removeEvent = adapter.removeEvent,
  1276. fireEvent = adapter.fireEvent,
  1277. washMouseEvent = adapter.washMouseEvent,
  1278. animate = adapter.animate,
  1279. stop = adapter.stop;
  1280. /* ****************************************************************************
  1281. * Handle the options *
  1282. *****************************************************************************/
  1283. var
  1284. defaultLabelOptions = {
  1285. enabled: true,
  1286. // rotation: 0,
  1287. // align: 'center',
  1288. x: 0,
  1289. y: 15,
  1290. /*formatter: function () {
  1291. return this.value;
  1292. },*/
  1293. style: {
  1294. color: '#666',
  1295. cursor: 'default',
  1296. fontSize: '11px',
  1297. lineHeight: '14px'
  1298. }
  1299. };
  1300. defaultOptions = {
  1301. colors: ['#2f7ed8', '#0d233a', '#8bbc21', '#910000', '#1aadce', '#492970',
  1302. '#f28f43', '#77a1e5', '#c42525', '#a6c96a'],
  1303. symbols: ['circle', 'diamond', 'square', 'triangle', 'triangle-down'],
  1304. lang: {
  1305. loading: 'Loading...',
  1306. months: ['January', 'February', 'March', 'April', 'May', 'June', 'July',
  1307. 'August', 'September', 'October', 'November', 'December'],
  1308. shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  1309. weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
  1310. decimalPoint: '.',
  1311. numericSymbols: ['k', 'M', 'G', 'T', 'P', 'E'], // SI prefixes used in axis labels
  1312. resetZoom: 'Reset zoom',
  1313. resetZoomTitle: 'Reset zoom level 1:1',
  1314. thousandsSep: ','
  1315. },
  1316. global: {
  1317. useUTC: true,
  1318. canvasToolsURL: 'http://code.highcharts.com/3.0.6/modules/canvas-tools.js',
  1319. VMLRadialGradientURL: 'http://code.highcharts.com/3.0.6/gfx/vml-radial-gradient.png'
  1320. },
  1321. chart: {
  1322. //animation: true,
  1323. //alignTicks: false,
  1324. //reflow: true,
  1325. //className: null,
  1326. //events: { load, selection },
  1327. //margin: [null],
  1328. //marginTop: null,
  1329. //marginRight: null,
  1330. //marginBottom: null,
  1331. //marginLeft: null,
  1332. borderColor: '#4572A7',
  1333. //borderWidth: 0,
  1334. borderRadius: 5,
  1335. defaultSeriesType: 'line',
  1336. ignoreHiddenSeries: true,
  1337. //inverted: false,
  1338. //shadow: false,
  1339. spacing: [10, 10, 15, 10],
  1340. //spacingTop: 10,
  1341. //spacingRight: 10,
  1342. //spacingBottom: 15,
  1343. //spacingLeft: 10,
  1344. style: {
  1345. fontFamily: '"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif', // default font
  1346. fontSize: '12px'
  1347. },
  1348. backgroundColor: '#FFFFFF',
  1349. //plotBackgroundColor: null,
  1350. plotBorderColor: '#C0C0C0',
  1351. //plotBorderWidth: 0,
  1352. //plotShadow: false,
  1353. //zoomType: ''
  1354. resetZoomButton: {
  1355. theme: {
  1356. zIndex: 20
  1357. },
  1358. position: {
  1359. align: 'right',
  1360. x: -10,
  1361. //verticalAlign: 'top',
  1362. y: 10
  1363. }
  1364. // relativeTo: 'plot'
  1365. }
  1366. },
  1367. title: {
  1368. text: 'Chart title',
  1369. align: 'center',
  1370. // floating: false,
  1371. margin: 15,
  1372. // x: 0,
  1373. // verticalAlign: 'top',
  1374. // y: null,
  1375. style: {
  1376. color: '#274b6d',//#3E576F',
  1377. fontSize: '16px'
  1378. }
  1379. },
  1380. subtitle: {
  1381. text: '',
  1382. align: 'center',
  1383. // floating: false
  1384. // x: 0,
  1385. // verticalAlign: 'top',
  1386. // y: null,
  1387. style: {
  1388. color: '#4d759e'
  1389. }
  1390. },
  1391. plotOptions: {
  1392. line: { // base series options
  1393. allowPointSelect: false,
  1394. showCheckbox: false,
  1395. animation: {
  1396. duration: 1000
  1397. },
  1398. //connectNulls: false,
  1399. //cursor: 'default',
  1400. //clip: true,
  1401. //dashStyle: null,
  1402. //enableMouseTracking: true,
  1403. events: {},
  1404. //legendIndex: 0,
  1405. lineWidth: 2,
  1406. //shadow: false,
  1407. // stacking: null,
  1408. marker: {
  1409. enabled: true,
  1410. //symbol: null,
  1411. lineWidth: 0,
  1412. radius: 4,
  1413. lineColor: '#FFFFFF',
  1414. //fillColor: null,
  1415. states: { // states for a single point
  1416. hover: {
  1417. enabled: true
  1418. //radius: base + 2
  1419. },
  1420. select: {
  1421. fillColor: '#FFFFFF',
  1422. lineColor: '#000000',
  1423. lineWidth: 2
  1424. }
  1425. }
  1426. },
  1427. point: {
  1428. events: {}
  1429. },
  1430. dataLabels: merge(defaultLabelOptions, {
  1431. align: 'center',
  1432. enabled: false,
  1433. formatter: function () {
  1434. return this.y === null ? '' : numberFormat(this.y, -1);
  1435. },
  1436. verticalAlign: 'bottom', // above singular point
  1437. y: 0
  1438. // backgroundColor: undefined,
  1439. // borderColor: undefined,
  1440. // borderRadius: undefined,
  1441. // borderWidth: undefined,
  1442. // padding: 3,
  1443. // shadow: false
  1444. }),
  1445. cropThreshold: 300, // draw points outside the plot area when the number of points is less than this
  1446. pointRange: 0,
  1447. //pointStart: 0,
  1448. //pointInterval: 1,
  1449. showInLegend: true,
  1450. states: { // states for the entire series
  1451. hover: {
  1452. //enabled: false,
  1453. //lineWidth: base + 1,
  1454. marker: {
  1455. // lineWidth: base + 1,
  1456. // radius: base + 1
  1457. }
  1458. },
  1459. select: {
  1460. marker: {}
  1461. }
  1462. },
  1463. stickyTracking: true
  1464. //tooltip: {
  1465. //pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b>'
  1466. //valueDecimals: null,
  1467. //xDateFormat: '%A, %b %e, %Y',
  1468. //valuePrefix: '',
  1469. //ySuffix: ''
  1470. //}
  1471. // turboThreshold: 1000
  1472. // zIndex: null
  1473. }
  1474. },
  1475. labels: {
  1476. //items: [],
  1477. style: {
  1478. //font: defaultFont,
  1479. position: ABSOLUTE,
  1480. color: '#3E576F'
  1481. }
  1482. },
  1483. legend: {
  1484. enabled: true,
  1485. align: 'center',
  1486. //floating: false,
  1487. layout: 'horizontal',
  1488. labelFormatter: function () {
  1489. return this.name;
  1490. },
  1491. borderWidth: 1,
  1492. borderColor: '#909090',
  1493. borderRadius: 5,
  1494. navigation: {
  1495. // animation: true,
  1496. activeColor: '#274b6d',
  1497. // arrowSize: 12
  1498. inactiveColor: '#CCC'
  1499. // style: {} // text styles
  1500. },
  1501. // margin: 10,
  1502. // reversed: false,
  1503. shadow: false,
  1504. // backgroundColor: null,
  1505. /*style: {
  1506. padding: '5px'
  1507. },*/
  1508. itemStyle: {
  1509. cursor: 'pointer',
  1510. color: '#274b6d',
  1511. fontSize: '12px'
  1512. },
  1513. itemHoverStyle: {
  1514. //cursor: 'pointer', removed as of #601
  1515. color: '#000'
  1516. },
  1517. itemHiddenStyle: {
  1518. color: '#CCC'
  1519. },
  1520. itemCheckboxStyle: {
  1521. position: ABSOLUTE,
  1522. width: '13px', // for IE precision
  1523. height: '13px'
  1524. },
  1525. // itemWidth: undefined,
  1526. symbolWidth: 16,
  1527. symbolPadding: 5,
  1528. verticalAlign: 'bottom',
  1529. // width: undefined,
  1530. x: 0,
  1531. y: 0,
  1532. title: {
  1533. //text: null,
  1534. style: {
  1535. fontWeight: 'bold'
  1536. }
  1537. }
  1538. },
  1539. loading: {
  1540. // hideDuration: 100,
  1541. labelStyle: {
  1542. fontWeight: 'bold',
  1543. position: RELATIVE,
  1544. top: '1em'
  1545. },
  1546. // showDuration: 0,
  1547. style: {
  1548. position: ABSOLUTE,
  1549. backgroundColor: 'white',
  1550. opacity: 0.5,
  1551. textAlign: 'center'
  1552. }
  1553. },
  1554. tooltip: {
  1555. enabled: true,
  1556. animation: hasSVG,
  1557. //crosshairs: null,
  1558. backgroundColor: 'rgba(255, 255, 255, .85)',
  1559. borderWidth: 1,
  1560. borderRadius: 3,
  1561. dateTimeLabelFormats: {
  1562. millisecond: '%A, %b %e, %H:%M:%S.%L',
  1563. second: '%A, %b %e, %H:%M:%S',
  1564. minute: '%A, %b %e, %H:%M',
  1565. hour: '%A, %b %e, %H:%M',
  1566. day: '%A, %b %e, %Y',
  1567. week: 'Week from %A, %b %e, %Y',
  1568. month: '%B %Y',
  1569. year: '%Y'
  1570. },
  1571. //formatter: defaultFormatter,
  1572. headerFormat: '<span style="font-size: 10px">{point.key}</span><br/>',
  1573. pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b><br/>',
  1574. shadow: true,
  1575. //shared: false,
  1576. snap: isTouchDevice ? 25 : 10,
  1577. style: {
  1578. color: '#333333',
  1579. cursor: 'default',
  1580. fontSize: '12px',
  1581. padding: '8px',
  1582. whiteSpace: 'nowrap'
  1583. }
  1584. //xDateFormat: '%A, %b %e, %Y',
  1585. //valueDecimals: null,
  1586. //valuePrefix: '',
  1587. //valueSuffix: ''
  1588. },
  1589. credits: {
  1590. enabled: true,
  1591. text: 'Highcharts.com',
  1592. href: 'http://www.highcharts.com',
  1593. position: {
  1594. align: 'right',
  1595. x: -10,
  1596. verticalAlign: 'bottom',
  1597. y: -5
  1598. },
  1599. style: {
  1600. cursor: 'pointer',
  1601. color: '#909090',
  1602. fontSize: '9px'
  1603. }
  1604. }
  1605. };
  1606. // Series defaults
  1607. var defaultPlotOptions = defaultOptions.plotOptions,
  1608. defaultSeriesOptions = defaultPlotOptions.line;
  1609. // set the default time methods
  1610. setTimeMethods();
  1611. /**
  1612. * Set the time methods globally based on the useUTC option. Time method can be either
  1613. * local time or UTC (default).
  1614. */
  1615. function setTimeMethods() {
  1616. var useUTC = defaultOptions.global.useUTC,
  1617. GET = useUTC ? 'getUTC' : 'get',
  1618. SET = useUTC ? 'setUTC' : 'set';
  1619. makeTime = useUTC ? Date.UTC : function (year, month, date, hours, minutes, seconds) {
  1620. return new Date(
  1621. year,
  1622. month,
  1623. pick(date, 1),
  1624. pick(hours, 0),
  1625. pick(minutes, 0),
  1626. pick(seconds, 0)
  1627. ).getTime();
  1628. };
  1629. getMinutes = GET + 'Minutes';
  1630. getHours = GET + 'Hours';
  1631. getDay = GET + 'Day';
  1632. getDate = GET + 'Date';
  1633. getMonth = GET + 'Month';
  1634. getFullYear = GET + 'FullYear';
  1635. setMinutes = SET + 'Minutes';
  1636. setHours = SET + 'Hours';
  1637. setDate = SET + 'Date';
  1638. setMonth = SET + 'Month';
  1639. setFullYear = SET + 'FullYear';
  1640. }
  1641. /**
  1642. * Merge the default options with custom options and return the new options structure
  1643. * @param {Object} options The new custom options
  1644. */
  1645. function setOptions(options) {
  1646. // Pull out axis options and apply them to the respective default axis options
  1647. /*defaultXAxisOptions = merge(defaultXAxisOptions, options.xAxis);
  1648. defaultYAxisOptions = merge(defaultYAxisOptions, options.yAxis);
  1649. options.xAxis = options.yAxis = UNDEFINED;*/
  1650. // Merge in the default options
  1651. defaultOptions = merge(defaultOptions, options);
  1652. // Apply UTC
  1653. setTimeMethods();
  1654. return defaultOptions;
  1655. }
  1656. /**
  1657. * Get the updated default options. Merely exposing defaultOptions for outside modules
  1658. * isn't enough because the setOptions method creates a new object.
  1659. */
  1660. function getOptions() {
  1661. return defaultOptions;
  1662. }
  1663. /**
  1664. * Handle color operations. The object methods are chainable.
  1665. * @param {String} input The input color in either rbga or hex format
  1666. */
  1667. var Color = function (input) {
  1668. // declare variables
  1669. var rgba = [], result, stops;
  1670. /**
  1671. * Parse the input color to rgba array
  1672. * @param {String} input
  1673. */
  1674. function init(input) {
  1675. // Gradients
  1676. if (input && input.stops) {
  1677. stops = map(input.stops, function (stop) {
  1678. return Color(stop[1]);
  1679. });
  1680. // Solid colors
  1681. } else {
  1682. // rgba
  1683. result = /rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]?(?:\.[0-9]+)?)\s*\)/.exec(input);
  1684. if (result) {
  1685. rgba = [pInt(result[1]), pInt(result[2]), pInt(result[3]), parseFloat(result[4], 10)];
  1686. } else {
  1687. // hex
  1688. result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(input);
  1689. if (result) {
  1690. rgba = [pInt(result[1], 16), pInt(result[2], 16), pInt(result[3], 16), 1];
  1691. } else {
  1692. // rgb
  1693. result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(input);
  1694. if (result) {
  1695. rgba = [pInt(result[1]), pInt(result[2]), pInt(result[3]), 1];
  1696. }
  1697. }
  1698. }
  1699. }
  1700. }
  1701. /**
  1702. * Return the color a specified format
  1703. * @param {String} format
  1704. */
  1705. function get(format) {
  1706. var ret;
  1707. if (stops) {
  1708. ret = merge(input);
  1709. ret.stops = [].concat(ret.stops);
  1710. each(stops, function (stop, i) {
  1711. ret.stops[i] = [ret.stops[i][0], stop.get(format)];
  1712. });
  1713. // it's NaN if gradient colors on a column chart
  1714. } else if (rgba && !isNaN(rgba[0])) {
  1715. if (format === 'rgb') {
  1716. ret = 'rgb(' + rgba[0] + ',' + rgba[1] + ',' + rgba[2] + ')';
  1717. } else if (format === 'a') {
  1718. ret = rgba[3];
  1719. } else {
  1720. ret = 'rgba(' + rgba.join(',') + ')';
  1721. }
  1722. } else {
  1723. ret = input;
  1724. }
  1725. return ret;
  1726. }
  1727. /**
  1728. * Brighten the color
  1729. * @param {Number} alpha
  1730. */
  1731. function brighten(alpha) {
  1732. if (stops) {
  1733. each(stops, function (stop) {
  1734. stop.brighten(alpha);
  1735. });
  1736. } else if (isNumber(alpha) && alpha !== 0) {
  1737. var i;
  1738. for (i = 0; i < 3; i++) {
  1739. rgba[i] += pInt(alpha * 255);
  1740. if (rgba[i] < 0) {
  1741. rgba[i] = 0;
  1742. }
  1743. if (rgba[i] > 255) {
  1744. rgba[i] = 255;
  1745. }
  1746. }
  1747. }
  1748. return this;
  1749. }
  1750. /**
  1751. * Set the color's opacity to a given alpha value
  1752. * @param {Number} alpha
  1753. */
  1754. function setOpacity(alpha) {
  1755. rgba[3] = alpha;
  1756. return this;
  1757. }
  1758. // initialize: parse the input
  1759. init(input);
  1760. // public methods
  1761. return {
  1762. get: get,
  1763. brighten: brighten,
  1764. rgba: rgba,
  1765. setOpacity: setOpacity
  1766. };
  1767. };
  1768. /**
  1769. * A wrapper object for SVG elements
  1770. */
  1771. function SVGElement() {
  1772. }
  1773. SVGElement.prototype = {
  1774. /**
  1775. * Initialize the SVG renderer
  1776. * @param {Object} renderer
  1777. * @param {String} nodeName
  1778. */
  1779. init: function (renderer, nodeName) {
  1780. var wrapper = this;
  1781. wrapper.element = nodeName === 'span' ?
  1782. createElement(nodeName) :
  1783. doc.createElementNS(SVG_NS, nodeName);
  1784. wrapper.renderer = renderer;
  1785. /**
  1786. * A collection of attribute setters. These methods, if defined, are called right before a certain
  1787. * attribute is set on an element wrapper. Returning false prevents the default attribute
  1788. * setter to run. Returning a value causes the default setter to set that value. Used in
  1789. * Renderer.label.
  1790. */
  1791. wrapper.attrSetters = {};
  1792. },
  1793. /**
  1794. * Default base for animation
  1795. */
  1796. opacity: 1,
  1797. /**
  1798. * Animate a given attribute
  1799. * @param {Object} params
  1800. * @param {Number} options The same options as in jQuery animation
  1801. * @param {Function} complete Function to perform at the end of animation
  1802. */
  1803. animate: function (params, options, complete) {
  1804. var animOptions = pick(options, globalAnimation, true);
  1805. stop(this); // stop regardless of animation actually running, or reverting to .attr (#607)
  1806. if (animOptions) {
  1807. animOptions = merge(animOptions);
  1808. if (complete) { // allows using a callback with the global animation without overwriting it
  1809. animOptions.complete = complete;
  1810. }
  1811. animate(this, params, animOptions);
  1812. } else {
  1813. this.attr(params);
  1814. if (complete) {
  1815. complete();
  1816. }
  1817. }
  1818. },
  1819. /**
  1820. * Set or get a given attribute
  1821. * @param {Object|String} hash
  1822. * @param {Mixed|Undefined} val
  1823. */
  1824. attr: function (hash, val) {
  1825. var wrapper = this,
  1826. key,
  1827. value,
  1828. result,
  1829. i,
  1830. child,
  1831. element = wrapper.element,
  1832. nodeName = element.nodeName.toLowerCase(), // Android2 requires lower for "text"
  1833. renderer = wrapper.renderer,
  1834. skipAttr,
  1835. titleNode,
  1836. attrSetters = wrapper.attrSetters,
  1837. shadows = wrapper.shadows,
  1838. hasSetSymbolSize,
  1839. doTransform,
  1840. ret = wrapper;
  1841. // single key-value pair
  1842. if (isString(hash) && defined(val)) {
  1843. key = hash;
  1844. hash = {};
  1845. hash[key] = val;
  1846. }
  1847. // used as a getter: first argument is a string, second is undefined
  1848. if (isString(hash)) {
  1849. key = hash;
  1850. if (nodeName === 'circle') {
  1851. key = {x: 'cx', y: 'cy'}[key] || key;
  1852. } else if (key === 'strokeWidth') {
  1853. key = 'stroke-width';
  1854. }
  1855. ret = attr(element, key) || wrapper[key] || 0;
  1856. if (key !== 'd' && key !== 'visibility' && key !== 'fill') { // 'd' is string in animation step
  1857. ret = parseFloat(ret);
  1858. }
  1859. // setter
  1860. } else {
  1861. for (key in hash) {
  1862. skipAttr = false; // reset
  1863. value = hash[key];
  1864. // check for a specific attribute setter
  1865. result = attrSetters[key] && attrSetters[key].call(wrapper, value, key);
  1866. if (result !== false) {
  1867. if (result !== UNDEFINED) {
  1868. value = result; // the attribute setter has returned a new value to set
  1869. }
  1870. // paths
  1871. if (key === 'd') {
  1872. if (value && value.join) { // join path
  1873. value = value.join(' ');
  1874. }
  1875. if (/(NaN| {2}|^$)/.test(value)) {
  1876. value = 'M 0 0';
  1877. }
  1878. //wrapper.d = value; // shortcut for animations
  1879. // update child tspans x values
  1880. } else if (key === 'x' && nodeName === 'text') {
  1881. for (i = 0; i < element.childNodes.length; i++) {
  1882. child = element.childNodes[i];
  1883. // if the x values are equal, the tspan represents a linebreak
  1884. if (attr(child, 'x') === attr(element, 'x')) {
  1885. //child.setAttribute('x', value);
  1886. attr(child, 'x', value);
  1887. }
  1888. }
  1889. } else if (wrapper.rotation && (key === 'x' || key === 'y')) {
  1890. doTransform = true;
  1891. // apply gradients
  1892. } else if (key === 'fill') {
  1893. value = renderer.color(value, element, key);
  1894. // circle x and y
  1895. } else if (nodeName === 'circle' && (key === 'x' || key === 'y')) {
  1896. key = {x: 'cx', y: 'cy'}[key] || key;
  1897. // rectangle border radius
  1898. } else if (nodeName === 'rect' && key === 'r') {
  1899. attr(element, {
  1900. rx: value,
  1901. ry: value
  1902. });
  1903. skipAttr = true;
  1904. // translation and text rotation
  1905. } else if (key === 'translateX' || key === 'translateY' || key === 'rotation' ||
  1906. key === 'verticalAlign' || key === 'scaleX' || key === 'scaleY') {
  1907. doTransform = true;
  1908. skipAttr = true;
  1909. // apply opacity as subnode (required by legacy WebKit and Batik)
  1910. } else if (key === 'stroke') {
  1911. value = renderer.color(value, element, key);
  1912. // emulate VML's dashstyle implementation
  1913. } else if (key === 'dashstyle') {
  1914. key = 'stroke-dasharray';
  1915. value = value && value.toLowerCase();
  1916. if (value === 'solid') {
  1917. value = NONE;
  1918. } else if (value) {
  1919. value = value
  1920. .replace('shortdashdotdot', '3,1,1,1,1,1,')
  1921. .replace('shortdashdot', '3,1,1,1')
  1922. .replace('shortdot', '1,1,')
  1923. .replace('shortdash', '3,1,')
  1924. .replace('longdash', '8,3,')
  1925. .replace(/dot/g, '1,3,')
  1926. .replace('dash', '4,3,')
  1927. .replace(/,$/, '')
  1928. .split(','); // ending comma
  1929. i = value.length;
  1930. while (i--) {
  1931. value[i] = pInt(value[i]) * pick(hash['stroke-width'], wrapper['stroke-width']);
  1932. }
  1933. value = value.join(',');
  1934. }
  1935. // IE9/MooTools combo: MooTools returns objects instead of numbers and IE9 Beta 2
  1936. // is unable to cast them. Test again with final IE9.
  1937. } else if (key === 'width') {
  1938. value = pInt(value);
  1939. // Text alignment
  1940. } else if (key === 'align') {
  1941. key = 'text-anchor';
  1942. value = {left: 'start', center: 'middle', right: 'end'}[value];
  1943. // Title requires a subnode, #431
  1944. } else if (key === 'title') {
  1945. titleNode = element.getElementsByTagName('title')[0];
  1946. if (!titleNode) {
  1947. titleNode = doc.createElementNS(SVG_NS, 'title');
  1948. element.appendChild(titleNode);
  1949. }
  1950. titleNode.textContent = value;
  1951. }
  1952. // jQuery animate changes case
  1953. if (key === 'strokeWidth') {
  1954. key = 'stroke-width';
  1955. }
  1956. // In Chrome/Win < 6 as well as Batik, the stroke attribute can't be set when the stroke-
  1957. // width is 0. #1369
  1958. if (key === 'stroke-width' || key === 'stroke') {
  1959. wrapper[key] = value;
  1960. // Only apply the stroke attribute if the stroke width is defined and larger than 0
  1961. if (wrapper.stroke && wrapper['stroke-width']) {
  1962. attr(element, 'stroke', wrapper.stroke);
  1963. attr(element, 'stroke-width', wrapper['stroke-width']);
  1964. wrapper.hasStroke = true;
  1965. } else if (key === 'stroke-width' && value === 0 && wrapper.hasStroke) {
  1966. element.removeAttribute('stroke');
  1967. wrapper.hasStroke = false;
  1968. }
  1969. skipAttr = true;
  1970. }
  1971. // symbols
  1972. if (wrapper.symbolName && /^(x|y|width|height|r|start|end|innerR|anchorX|anchorY)/.test(key)) {
  1973. if (!hasSetSymbolSize) {
  1974. wrapper.symbolAttr(hash);
  1975. hasSetSymbolSize = true;
  1976. }
  1977. skipAttr = true;
  1978. }
  1979. // let the shadow follow the main element
  1980. if (shadows && /^(width|height|visibility|x|y|d|transform|cx|cy|r)$/.test(key)) {
  1981. i = shadows.length;
  1982. while (i--) {
  1983. attr(
  1984. shadows[i],
  1985. key,
  1986. key === 'height' ?
  1987. mathMax(value - (shadows[i].cutHeight || 0), 0) :
  1988. value
  1989. );
  1990. }
  1991. }
  1992. // validate heights
  1993. if ((key === 'width' || key === 'height') && nodeName === 'rect' && value < 0) {
  1994. value = 0;
  1995. }
  1996. // Record for animation and quick access without polling the DOM
  1997. wrapper[key] = value;
  1998. if (key === 'text') {
  1999. // Delete bBox memo when the text changes
  2000. if (value !== wrapper.textStr) {
  2001. delete wrapper.bBox;
  2002. }
  2003. wrapper.textStr = value;
  2004. if (wrapper.added) {
  2005. renderer.buildText(wrapper);
  2006. }
  2007. } else if (!skipAttr) {
  2008. attr(element, key, value);
  2009. }
  2010. }
  2011. }
  2012. // Update transform. Do this outside the loop to prevent redundant updating for batch setting
  2013. // of attributes.
  2014. if (doTransform) {
  2015. wrapper.updateTransform();
  2016. }
  2017. }
  2018. return ret;
  2019. },
  2020. /**
  2021. * Add a class name to an element
  2022. */
  2023. addClass: function (className) {
  2024. var element = this.element,
  2025. currentClassName = attr(element, 'class') || '';
  2026. if (currentClassName.indexOf(className) === -1) {
  2027. attr(element, 'class', currentClassName + ' ' + className);
  2028. }
  2029. return this;
  2030. },
  2031. /* hasClass and removeClass are not (yet) needed
  2032. hasClass: function (className) {
  2033. return attr(this.element, 'class').indexOf(className) !== -1;
  2034. },
  2035. removeClass: function (className) {
  2036. attr(this.element, 'class', attr(this.element, 'class').replace(className, ''));
  2037. return this;
  2038. },
  2039. */
  2040. /**
  2041. * If one of the symbol size affecting parameters are changed,
  2042. * check all the others only once for each call to an element's
  2043. * .attr() method
  2044. * @param {Object} hash
  2045. */
  2046. symbolAttr: function (hash) {
  2047. var wrapper = this;
  2048. each(['x', 'y', 'r', 'start', 'end', 'width', 'height', 'innerR', 'anchorX', 'anchorY'], function (key) {
  2049. wrapper[key] = pick(hash[key], wrapper[key]);
  2050. });
  2051. wrapper.attr({
  2052. d: wrapper.renderer.symbols[wrapper.symbolName](
  2053. wrapper.x,
  2054. wrapper.y,
  2055. wrapper.width,
  2056. wrapper.height,
  2057. wrapper
  2058. )
  2059. });
  2060. },
  2061. /**
  2062. * Apply a clipping path to this object
  2063. * @param {String} id
  2064. */
  2065. clip: function (clipRect) {
  2066. return this.attr('clip-path', clipRect ? 'url(' + this.renderer.url + '#' + clipRect.id + ')' : NONE);
  2067. },
  2068. /**
  2069. * Calculate the coordinates needed for drawing a rectangle crisply and return the
  2070. * calculated attributes
  2071. * @param {Number} strokeWidth
  2072. * @param {Number} x
  2073. * @param {Number} y
  2074. * @param {Number} width
  2075. * @param {Number} height
  2076. */
  2077. crisp: function (strokeWidth, x, y, width, height) {
  2078. var wrapper = this,
  2079. key,
  2080. attribs = {},
  2081. values = {},
  2082. normalizer;
  2083. strokeWidth = strokeWidth || wrapper.strokeWidth || (wrapper.attr && wrapper.attr('stroke-width')) || 0;
  2084. normalizer = mathRound(strokeWidth) % 2 / 2; // mathRound because strokeWidth can sometimes have roundoff errors
  2085. // normalize for crisp edges
  2086. values.x = mathFloor(x || wrapper.x || 0) + normalizer;
  2087. values.y = mathFloor(y || wrapper.y || 0) + normalizer;
  2088. values.width = mathFloor((width || wrapper.width || 0) - 2 * normalizer);
  2089. values.height = mathFloor((height || wrapper.height || 0) - 2 * normalizer);
  2090. values.strokeWidth = strokeWidth;
  2091. for (key in values) {
  2092. if (wrapper[key] !== values[key]) { // only set attribute if changed
  2093. wrapper[key] = attribs[key] = values[key];
  2094. }
  2095. }
  2096. return attribs;
  2097. },
  2098. /**
  2099. * Set styles for the element
  2100. * @param {Object} styles
  2101. */
  2102. css: function (styles) {
  2103. /*jslint unparam: true*//* allow unused param a in the regexp function below */
  2104. var elemWrapper = this,
  2105. elem = elemWrapper.element,
  2106. textWidth = styles && styles.width && elem.nodeName.toLowerCase() === 'text',
  2107. n,
  2108. serializedCss = '',
  2109. hyphenate = function (a, b) {
  2110. return '-' + b.toLowerCase();
  2111. };
  2112. /*jslint unparam: false*/
  2113. // convert legacy
  2114. if (styles && styles.color) {
  2115. styles.fill = styles.color;
  2116. }
  2117. // Merge the new styles with the old ones
  2118. styles = extend(
  2119. elemWrapper.styles,
  2120. styles
  2121. );
  2122. // store object
  2123. elemWrapper.styles = styles;
  2124. // Don't handle line wrap on canvas
  2125. if (useCanVG && textWidth) {
  2126. delete styles.width;
  2127. }
  2128. // serialize and set style attribute
  2129. if (isIE && !hasSVG) { // legacy IE doesn't support setting style attribute
  2130. if (textWidth) {
  2131. delete styles.width;
  2132. }
  2133. css(elemWrapper.element, styles);
  2134. } else {
  2135. for (n in styles) {
  2136. serializedCss += n.replace(/([A-Z])/g, hyphenate) + ':' + styles[n] + ';';
  2137. }
  2138. attr(elem, 'style', serializedCss); // #1881
  2139. }
  2140. // re-build text
  2141. if (textWidth && elemWrapper.added) {
  2142. elemWrapper.renderer.buildText(elemWrapper);
  2143. }
  2144. return elemWrapper;
  2145. },
  2146. /**
  2147. * Add an event listener
  2148. * @param {String} eventType
  2149. * @param {Function} handler
  2150. */
  2151. on: function (eventType, handler) {
  2152. var svgElement = this,
  2153. element = svgElement.element;
  2154. // touch
  2155. if (hasTouch && eventType === 'click') {
  2156. element.ontouchstart = function (e) {
  2157. svgElement.touchEventFired = Date.now();
  2158. e.preventDefault();
  2159. handler.call(element, e);
  2160. };
  2161. element.onclick = function (e) {
  2162. if (userAgent.indexOf('Android') === -1 || Date.now() - (svgElement.touchEventFired || 0) > 1100) { // #2269
  2163. handler.call(element, e);
  2164. }
  2165. };
  2166. } else {
  2167. // simplest possible event model for internal use
  2168. element['on' + eventType] = handler;
  2169. }
  2170. return this;
  2171. },
  2172. /**
  2173. * Set the coordinates needed to draw a consistent radial gradient across
  2174. * pie slices regardless of positioning inside the chart. The format is
  2175. * [centerX, centerY, diameter] in pixels.
  2176. */
  2177. setRadialReference: function (coordinates) {
  2178. this.element.radialReference = coordinates;
  2179. return this;
  2180. },
  2181. /**
  2182. * Move an object and its children by x and y values
  2183. * @param {Number} x
  2184. * @param {Number} y
  2185. */
  2186. translate: function (x, y) {
  2187. return this.attr({
  2188. translateX: x,
  2189. translateY: y
  2190. });
  2191. },
  2192. /**
  2193. * Invert a group, rotate and flip
  2194. */
  2195. invert: function () {
  2196. var wrapper = this;
  2197. wrapper.inverted = true;
  2198. wrapper.updateTransform();
  2199. return wrapper;
  2200. },
  2201. /**
  2202. * Apply CSS to HTML elements. This is used in text within SVG rendering and
  2203. * by the VML renderer
  2204. */
  2205. htmlCss: function (styles) {
  2206. var wrapper = this,
  2207. element = wrapper.element,
  2208. textWidth = styles && element.tagName === 'SPAN' && styles.width;
  2209. if (textWidth) {
  2210. delete styles.width;
  2211. wrapper.textWidth = textWidth;
  2212. wrapper.updateTransform();
  2213. }
  2214. wrapper.styles = extend(wrapper.styles, styles);
  2215. css(wrapper.element, styles);
  2216. return wrapper;
  2217. },
  2218. /**
  2219. * VML and useHTML method for calculating the bounding box based on offsets
  2220. * @param {Boolean} refresh Whether to force a fresh value from the DOM or to
  2221. * use the cached value
  2222. *
  2223. * @return {Object} A hash containing values for x, y, width and height
  2224. */
  2225. htmlGetBBox: function () {
  2226. var wrapper = this,
  2227. element = wrapper.element,
  2228. bBox = wrapper.bBox;
  2229. // faking getBBox in exported SVG in legacy IE
  2230. if (!bBox) {
  2231. // faking getBBox in exported SVG in legacy IE (is this a duplicate of the fix for #1079?)
  2232. if (element.nodeName === 'text') {
  2233. element.style.position = ABSOLUTE;
  2234. }
  2235. bBox = wrapper.bBox = {
  2236. x: element.offsetLeft,
  2237. y: element.offsetTop,
  2238. width: element.offsetWidth,
  2239. height: element.offsetHeight
  2240. };
  2241. }
  2242. return bBox;
  2243. },
  2244. /**
  2245. * VML override private method to update elements based on internal
  2246. * properties based on SVG transform
  2247. */
  2248. htmlUpdateTransform: function () {
  2249. // aligning non added elements is expensive
  2250. if (!this.added) {
  2251. this.alignOnAdd = true;
  2252. return;
  2253. }
  2254. var wrapper = this,
  2255. renderer = wrapper.renderer,
  2256. elem = wrapper.element,
  2257. translateX = wrapper.translateX || 0,
  2258. translateY = wrapper.translateY || 0,
  2259. x = wrapper.x || 0,
  2260. y = wrapper.y || 0,
  2261. align = wrapper.textAlign || 'left',
  2262. alignCorrection = {left: 0, center: 0.5, right: 1}[align],
  2263. nonLeft = align && align !== 'left',
  2264. shadows = wrapper.shadows;
  2265. // apply translate
  2266. css(elem, {
  2267. marginLeft: translateX,
  2268. marginTop: translateY
  2269. });
  2270. if (shadows) { // used in labels/tooltip
  2271. each(shadows, function (shadow) {
  2272. css(shadow, {
  2273. marginLeft: translateX + 1,
  2274. marginTop: translateY + 1
  2275. });
  2276. });
  2277. }
  2278. // apply inversion
  2279. if (wrapper.inverted) { // wrapper is a group
  2280. each(elem.childNodes, function (child) {
  2281. renderer.invertChild(child, elem);
  2282. });
  2283. }
  2284. if (elem.tagName === 'SPAN') {
  2285. var width, height,
  2286. rotation = wrapper.rotation,
  2287. baseline,
  2288. radians = 0,
  2289. costheta = 1,
  2290. sintheta = 0,
  2291. quad,
  2292. textWidth = pInt(wrapper.textWidth),
  2293. xCorr = wrapper.xCorr || 0,
  2294. yCorr = wrapper.yCorr || 0,
  2295. currentTextTransform = [rotation, align, elem.innerHTML, wrapper.textWidth].join(',');
  2296. if (currentTextTransform !== wrapper.cTT) { // do the calculations and DOM access only if properties changed
  2297. if (defined(rotation)) {
  2298. radians = rotation * deg2rad; // deg to rad
  2299. costheta = mathCos(radians);
  2300. sintheta = mathSin(radians);
  2301. wrapper.setSpanRotation(rotation, sintheta, costheta);
  2302. }
  2303. width = pick(wrapper.elemWidth, elem.offsetWidth);
  2304. height = pick(wrapper.elemHeight, elem.offsetHeight);
  2305. // update textWidth
  2306. if (width > textWidth && /[ \-]/.test(elem.textContent || elem.innerText)) { // #983, #1254
  2307. css(elem, {
  2308. width: textWidth + PX,
  2309. display: 'block',
  2310. whiteSpace: 'normal'
  2311. });
  2312. width = textWidth;
  2313. }
  2314. // correct x and y
  2315. baseline = renderer.fontMetrics(elem.style.fontSize).b;
  2316. xCorr = costheta < 0 && -width;
  2317. yCorr = sintheta < 0 && -height;
  2318. // correct for baseline and corners spilling out after rotation
  2319. quad = costheta * sintheta < 0;
  2320. xCorr += sintheta * baseline * (quad ? 1 - alignCorrection : alignCorrection);
  2321. yCorr -= costheta * baseline * (rotation ? (quad ? alignCorrection : 1 - alignCorrection) : 1);
  2322. // correct for the length/height of the text
  2323. if (nonLeft) {
  2324. xCorr -= width * alignCorrection * (costheta < 0 ? -1 : 1);
  2325. if (rotation) {
  2326. yCorr -= height * alignCorrection * (sintheta < 0 ? -1 : 1);
  2327. }
  2328. css(elem, {
  2329. textAlign: align
  2330. });
  2331. }
  2332. // record correction
  2333. wrapper.xCorr = xCorr;
  2334. wrapper.yCorr = yCorr;
  2335. }
  2336. // apply position with correction
  2337. css(elem, {
  2338. left: (x + xCorr) + PX,
  2339. top: (y + yCorr) + PX
  2340. });
  2341. // force reflow in webkit to apply the left and top on useHTML element (#1249)
  2342. if (isWebKit) {
  2343. height = elem.offsetHeight; // assigned to height for JSLint purpose
  2344. }
  2345. // record current text transform
  2346. wrapper.cTT = currentTextTransform;
  2347. }
  2348. },
  2349. /**
  2350. * Set the rotation of an individual HTML span
  2351. */
  2352. setSpanRotation: function (rotation) {
  2353. var rotationStyle = {},
  2354. cssTransformKey = isIE ? '-ms-transform' : isWebKit ? '-webkit-transform' : isFirefox ? 'MozTransform' : isOpera ? '-o-transform' : '';
  2355. rotationStyle[cssTransformKey] = rotationStyle.transform = 'rotate(' + rotation + 'deg)';
  2356. css(this.element, rotationStyle);
  2357. },
  2358. /**
  2359. * Private method to update the transform attribute based on internal
  2360. * properties
  2361. */
  2362. updateTransform: function () {
  2363. var wrapper = this,
  2364. translateX = wrapper.translateX || 0,
  2365. translateY = wrapper.translateY || 0,
  2366. scaleX = wrapper.scaleX,
  2367. scaleY = wrapper.scaleY,
  2368. inverted = wrapper.inverted,
  2369. rotation = wrapper.rotation,
  2370. transform;
  2371. // flipping affects translate as adjustment for flipping around the group's axis
  2372. if (inverted) {
  2373. translateX += wrapper.attr('width');
  2374. translateY += wrapper.attr('height');
  2375. }
  2376. // Apply translate. Nearly all transformed elements have translation, so instead
  2377. // of checking for translate = 0, do it always (#1767, #1846).
  2378. transform = ['translate(' + translateX + ',' + translateY + ')'];
  2379. // apply rotation
  2380. if (inverted) {
  2381. transform.push('rotate(90) scale(-1,1)');
  2382. } else if (rotation) { // text rotation
  2383. transform.push('rotate(' + rotation + ' ' + (wrapper.x || 0) + ' ' + (wrapper.y || 0) + ')');
  2384. }
  2385. // apply scale
  2386. if (defined(scaleX) || defined(scaleY)) {
  2387. transform.push('scale(' + pick(scaleX, 1) + ' ' + pick(scaleY, 1) + ')');
  2388. }
  2389. if (transform.length) {
  2390. attr(wrapper.element, 'transform', transform.join(' '));
  2391. }
  2392. },
  2393. /**
  2394. * Bring the element to the front
  2395. */
  2396. toFront: function () {
  2397. var element = this.element;
  2398. element.parentNode.appendChild(element);
  2399. return this;
  2400. },
  2401. /**
  2402. * Break down alignment options like align, verticalAlign, x and y
  2403. * to x and y relative to the chart.
  2404. *
  2405. * @param {Object} alignOptions
  2406. * @param {Boolean} alignByTranslate
  2407. * @param {String[Object} box The box to align to, needs a width and height. When the
  2408. * box is a string, it refers to an object in the Renderer. For example, when
  2409. * box is 'spacingBox', it refers to Renderer.spacingBox which holds width, height
  2410. * x and y properties.
  2411. *
  2412. */
  2413. align: function (alignOptions, alignByTranslate, box) {
  2414. var align,
  2415. vAlign,
  2416. x,
  2417. y,
  2418. attribs = {},
  2419. alignTo,
  2420. renderer = this.renderer,
  2421. alignedObjects = renderer.alignedObjects;
  2422. // First call on instanciate
  2423. if (alignOptions) {
  2424. this.alignOptions = alignOptions;
  2425. this.alignByTranslate = alignByTranslate;
  2426. if (!box || isString(box)) { // boxes other than renderer handle this internally
  2427. this.alignTo = alignTo = box || 'renderer';
  2428. erase(alignedObjects, this); // prevent duplicates, like legendGroup after resize
  2429. alignedObjects.push(this);
  2430. box = null; // reassign it below
  2431. }
  2432. // When called on resize, no arguments are supplied
  2433. } else {
  2434. alignOptions = this.alignOptions;
  2435. alignByTranslate = this.alignByTranslate;
  2436. alignTo = this.alignTo;
  2437. }
  2438. box = pick(box, renderer[alignTo], renderer);
  2439. // Assign variables
  2440. align = alignOptions.align;
  2441. vAlign = alignOptions.verticalAlign;
  2442. x = (box.x || 0) + (alignOptions.x || 0); // default: left align
  2443. y = (box.y || 0) + (alignOptions.y || 0); // default: top align
  2444. // Align
  2445. if (align === 'right' || align === 'center') {
  2446. x += (box.width - (alignOptions.width || 0)) /
  2447. {right: 1, center: 2}[align];
  2448. }
  2449. attribs[alignByTranslate ? 'translateX' : 'x'] = mathRound(x);
  2450. // Vertical align
  2451. if (vAlign === 'bottom' || vAlign === 'middle') {
  2452. y += (box.height - (alignOptions.height || 0)) /
  2453. ({bottom: 1, middle: 2}[vAlign] || 1);
  2454. }
  2455. attribs[alignByTranslate ? 'translateY' : 'y'] = mathRound(y);
  2456. // Animate only if already placed
  2457. this[this.placed ? 'animate' : 'attr'](attribs);
  2458. this.placed = true;
  2459. this.alignAttr = attribs;
  2460. return this;
  2461. },
  2462. /**
  2463. * Get the bounding box (width, height, x and y) for the element
  2464. */
  2465. getBBox: function () {
  2466. var wrapper = this,
  2467. bBox = wrapper.bBox,
  2468. renderer = wrapper.renderer,
  2469. width,
  2470. height,
  2471. rotation = wrapper.rotation,
  2472. element = wrapper.element,
  2473. styles = wrapper.styles,
  2474. rad = rotation * deg2rad;
  2475. if (!bBox) {
  2476. // SVG elements
  2477. if (element.namespaceURI === SVG_NS || renderer.forExport) {
  2478. try { // Fails in Firefox if the container has display: none.
  2479. bBox = element.getBBox ?
  2480. // SVG: use extend because IE9 is not allowed to change width and height in case
  2481. // of rotation (below)
  2482. extend({}, element.getBBox()) :
  2483. // Canvas renderer and legacy IE in export mode
  2484. {
  2485. width: element.offsetWidth,
  2486. height: element.offsetHeight
  2487. };
  2488. } catch (e) {
  2489. }
  2490. // If the bBox is not set, the try-catch block above failed. The other condition
  2491. // is for Opera that returns a width of -Infinity on hidden elements.
  2492. if (!bBox || bBox.width < 0) {
  2493. bBox = {width: 0, height: 0};
  2494. }
  2495. // VML Renderer or useHTML within SVG
  2496. } else {
  2497. bBox = wrapper.htmlGetBBox();
  2498. }
  2499. // True SVG elements as well as HTML elements in modern browsers using the .useHTML option
  2500. // need to compensated for rotation
  2501. if (renderer.isSVG) {
  2502. width = bBox.width;
  2503. height = bBox.height;
  2504. // Workaround for wrong bounding box in IE9 and IE10 (#1101, #1505, #1669)
  2505. if (isIE && styles && styles.fontSize === '11px' && height.toPrecision(3) === '22.7') {
  2506. bBox.height = height = 14;
  2507. }
  2508. // Adjust for rotated text
  2509. if (rotation) {
  2510. bBox.width = mathAbs(height * mathSin(rad)) + mathAbs(width * mathCos(rad));
  2511. bBox.height = mathAbs(height * mathCos(rad)) + mathAbs(width * mathSin(rad));
  2512. }
  2513. }
  2514. wrapper.bBox = bBox;
  2515. }
  2516. return bBox;
  2517. },
  2518. /**
  2519. * Show the element
  2520. */
  2521. show: function () {
  2522. return this.attr({visibility: VISIBLE});
  2523. },
  2524. /**
  2525. * Hide the element
  2526. */
  2527. hide: function () {
  2528. return this.attr({visibility: HIDDEN});
  2529. },
  2530. fadeOut: function (duration) {
  2531. var elemWrapper = this;
  2532. elemWrapper.animate({
  2533. opacity: 0
  2534. }, {
  2535. duration: duration || 150,
  2536. complete: function () {
  2537. elemWrapper.hide();
  2538. }
  2539. });
  2540. },
  2541. /**
  2542. * Add the element
  2543. * @param {Object|Undefined} parent Can be an element, an element wrapper or undefined
  2544. * to append the element to the renderer.box.
  2545. */
  2546. add: function (parent) {
  2547. var renderer = this.renderer,
  2548. parentWrapper = parent || renderer,
  2549. parentNode = parentWrapper.element || renderer.box,
  2550. childNodes = parentNode.childNodes,
  2551. element = this.element,
  2552. zIndex = attr(element, 'zIndex'),
  2553. otherElement,
  2554. otherZIndex,
  2555. i,
  2556. inserted;
  2557. if (parent) {
  2558. this.parentGroup = parent;
  2559. }
  2560. // mark as inverted
  2561. this.parentInverted = parent && parent.inverted;
  2562. // build formatted text
  2563. if (this.textStr !== undefined) {
  2564. renderer.buildText(this);
  2565. }
  2566. // mark the container as having z indexed children
  2567. if (zIndex) {
  2568. parentWrapper.handleZ = true;
  2569. zIndex = pInt(zIndex);
  2570. }
  2571. // insert according to this and other elements' zIndex
  2572. if (parentWrapper.handleZ) { // this element or any of its siblings has a z index
  2573. for (i = 0; i < childNodes.length; i++) {
  2574. otherElement = childNodes[i];
  2575. otherZIndex = attr(otherElement, 'zIndex');
  2576. if (otherElement !== element && (
  2577. // insert before the first element with a higher zIndex
  2578. pInt(otherZIndex) > zIndex ||
  2579. // if no zIndex given, insert before the first element with a zIndex
  2580. (!defined(zIndex) && defined(otherZIndex))
  2581. )) {
  2582. parentNode.insertBefore(element, otherElement);
  2583. inserted = true;
  2584. break;
  2585. }
  2586. }
  2587. }
  2588. // default: append at the end
  2589. if (!inserted) {
  2590. parentNode.appendChild(element);
  2591. }
  2592. // mark as added
  2593. this.added = true;
  2594. // fire an event for internal hooks
  2595. fireEvent(this, 'add');
  2596. return this;
  2597. },
  2598. /**
  2599. * Removes a child either by removeChild or move to garbageBin.
  2600. * Issue 490; in VML removeChild results in Orphaned nodes according to sIEve, discardElement does not.
  2601. */
  2602. safeRemoveChild: function (element) {
  2603. var parentNode = element.parentNode;
  2604. if (parentNode) {
  2605. parentNode.removeChild(element);
  2606. }
  2607. },
  2608. /**
  2609. * Destroy the element and element wrapper
  2610. */
  2611. destroy: function () {
  2612. var wrapper = this,
  2613. element = wrapper.element || {},
  2614. shadows = wrapper.shadows,
  2615. parentToClean = wrapper.renderer.isSVG && element.nodeName === 'SPAN' && element.parentNode,
  2616. grandParent,
  2617. key,
  2618. i;
  2619. // remove events
  2620. element.onclick = element.onmouseout = element.onmouseover = element.onmousemove = element.point = null;
  2621. stop(wrapper); // stop running animations
  2622. if (wrapper.clipPath) {
  2623. wrapper.clipPath = wrapper.clipPath.destroy();
  2624. }
  2625. // Destroy stops in case this is a gradient object
  2626. if (wrapper.stops) {
  2627. for (i = 0; i < wrapper.stops.length; i++) {
  2628. wrapper.stops[i] = wrapper.stops[i].destroy();
  2629. }
  2630. wrapper.stops = null;
  2631. }
  2632. // remove element
  2633. wrapper.safeRemoveChild(element);
  2634. // destroy shadows
  2635. if (shadows) {
  2636. each(shadows, function (shadow) {
  2637. wrapper.safeRemoveChild(shadow);
  2638. });
  2639. }
  2640. // In case of useHTML, clean up empty containers emulating SVG groups (#1960).
  2641. while (parentToClean && parentToClean.childNodes.length === 0) {
  2642. grandParent = parentToClean.parentNode;
  2643. wrapper.safeRemoveChild(parentToClean);
  2644. parentToClean = grandParent;
  2645. }
  2646. // remove from alignObjects
  2647. if (wrapper.alignTo) {
  2648. erase(wrapper.renderer.alignedObjects, wrapper);
  2649. }
  2650. for (key in wrapper) {
  2651. delete wrapper[key];
  2652. }
  2653. return null;
  2654. },
  2655. /**
  2656. * Add a shadow to the element. Must be done after the element is added to the DOM
  2657. * @param {Boolean|Object} shadowOptions
  2658. */
  2659. shadow: function (shadowOptions, group, cutOff) {
  2660. var shadows = [],
  2661. i,
  2662. shadow,
  2663. element = this.element,
  2664. strokeWidth,
  2665. shadowWidth,
  2666. shadowElementOpacity,
  2667. // compensate for inverted plot area
  2668. transform;
  2669. if (shadowOptions) {
  2670. shadowWidth = pick(shadowOptions.width, 3);
  2671. shadowElementOpacity = (shadowOptions.opacity || 0.15) / shadowWidth;
  2672. transform = this.parentInverted ?
  2673. '(-1,-1)' :
  2674. '(' + pick(shadowOptions.offsetX, 1) + ', ' + pick(shadowOptions.offsetY, 1) + ')';
  2675. for (i = 1; i <= shadowWidth; i++) {
  2676. shadow = element.cloneNode(0);
  2677. strokeWidth = (shadowWidth * 2) + 1 - (2 * i);
  2678. attr(shadow, {
  2679. 'isShadow': 'true',
  2680. 'stroke': shadowOptions.color || 'black',
  2681. 'stroke-opacity': shadowElementOpacity * i,
  2682. 'stroke-width': strokeWidth,
  2683. 'transform': 'translate' + transform,
  2684. 'fill': NONE
  2685. });
  2686. if (cutOff) {
  2687. attr(shadow, 'height', mathMax(attr(shadow, 'height') - strokeWidth, 0));
  2688. shadow.cutHeight = strokeWidth;
  2689. }
  2690. if (group) {
  2691. group.element.appendChild(shadow);
  2692. } else {
  2693. element.parentNode.insertBefore(shadow, element);
  2694. }
  2695. shadows.push(shadow);
  2696. }
  2697. this.shadows = shadows;
  2698. }
  2699. return this;
  2700. }
  2701. };
  2702. /**
  2703. * The default SVG renderer
  2704. */
  2705. var SVGRenderer = function () {
  2706. this.init.apply(this, arguments);
  2707. };
  2708. SVGRenderer.prototype = {
  2709. Element: SVGElement,
  2710. /**
  2711. * Initialize the SVGRenderer
  2712. * @param {Object} container
  2713. * @param {Number} width
  2714. * @param {Number} height
  2715. * @param {Boolean} forExport
  2716. */
  2717. init: function (container, width, height, forExport) {
  2718. var renderer = this,
  2719. loc = location,
  2720. boxWrapper,
  2721. element,
  2722. desc;
  2723. boxWrapper = renderer.createElement('svg')
  2724. .attr({
  2725. version: '1.1'
  2726. });
  2727. element = boxWrapper.element;
  2728. container.appendChild(element);
  2729. // For browsers other than IE, add the namespace attribute (#1978)
  2730. if (container.innerHTML.indexOf('xmlns') === -1) {
  2731. attr(element, 'xmlns', SVG_NS);
  2732. }
  2733. // object properties
  2734. renderer.isSVG = true;
  2735. renderer.box = element;
  2736. renderer.boxWrapper = boxWrapper;
  2737. renderer.alignedObjects = [];
  2738. // Page url used for internal references. #24, #672, #1070
  2739. renderer.url = (isFirefox || isWebKit) && doc.getElementsByTagName('base').length ?
  2740. loc.href
  2741. .replace(/#.*?$/, '') // remove the hash
  2742. .replace(/([\('\)])/g, '\\$1') // escape parantheses and quotes
  2743. .replace(/ /g, '%20') : // replace spaces (needed for Safari only)
  2744. '';
  2745. // Add description
  2746. desc = this.createElement('desc').add();
  2747. desc.element.appendChild(doc.createTextNode('Created with ' + PRODUCT + ' ' + VERSION));
  2748. renderer.defs = this.createElement('defs').add();
  2749. renderer.forExport = forExport;
  2750. renderer.gradients = {}; // Object where gradient SvgElements are stored
  2751. renderer.setSize(width, height, false);
  2752. // Issue 110 workaround:
  2753. // In Firefox, if a div is positioned by percentage, its pixel position may land
  2754. // between pixels. The container itself doesn't display this, but an SVG element
  2755. // inside this container will be drawn at subpixel precision. In order to draw
  2756. // sharp lines, this must be compensated for. This doesn't seem to work inside
  2757. // iframes though (like in jsFiddle).
  2758. var subPixelFix, rect;
  2759. if (isFirefox && container.getBoundingClientRect) {
  2760. renderer.subPixelFix = subPixelFix = function () {
  2761. css(container, {left: 0, top: 0});
  2762. rect = container.getBoundingClientRect();
  2763. css(container, {
  2764. left: (mathCeil(rect.left) - rect.left) + PX,
  2765. top: (mathCeil(rect.top) - rect.top) + PX
  2766. });
  2767. };
  2768. // run the fix now
  2769. subPixelFix();
  2770. // run it on resize
  2771. addEvent(win, 'resize', subPixelFix);
  2772. }
  2773. },
  2774. /**
  2775. * Detect whether the renderer is hidden. This happens when one of the parent elements
  2776. * has display: none. #608.
  2777. */
  2778. isHidden: function () {
  2779. return !this.boxWrapper.getBBox().width;
  2780. },
  2781. /**
  2782. * Destroys the renderer and its allocated members.
  2783. */
  2784. destroy: function () {
  2785. var renderer = this,
  2786. rendererDefs = renderer.defs;
  2787. renderer.box = null;
  2788. renderer.boxWrapper = renderer.boxWrapper.destroy();
  2789. // Call destroy on all gradient elements
  2790. destroyObjectProperties(renderer.gradients || {});
  2791. renderer.gradients = null;
  2792. // Defs are null in VMLRenderer
  2793. // Otherwise, destroy them here.
  2794. if (rendererDefs) {
  2795. renderer.defs = rendererDefs.destroy();
  2796. }
  2797. // Remove sub pixel fix handler
  2798. // We need to check that there is a handler, otherwise all functions that are registered for event 'resize' are removed
  2799. // See issue #982
  2800. if (renderer.subPixelFix) {
  2801. removeEvent(win, 'resize', renderer.subPixelFix);
  2802. }
  2803. renderer.alignedObjects = null;
  2804. return null;
  2805. },
  2806. /**
  2807. * Create a wrapper for an SVG element
  2808. * @param {Object} nodeName
  2809. */
  2810. createElement: function (nodeName) {
  2811. var wrapper = new this.Element();
  2812. wrapper.init(this, nodeName);
  2813. return wrapper;
  2814. },
  2815. /**
  2816. * Dummy function for use in canvas renderer
  2817. */
  2818. draw: function () {
  2819. },
  2820. /**
  2821. * Parse a simple HTML string into SVG tspans
  2822. *
  2823. * @param {Object} textNode The parent text SVG node
  2824. */
  2825. buildText: function (wrapper) {
  2826. var textNode = wrapper.element,
  2827. renderer = this,
  2828. forExport = renderer.forExport,
  2829. lines = pick(wrapper.textStr, '').toString()
  2830. .replace(/<(b|strong)>/g, '<span style="font-weight:bold">')
  2831. .replace(/<(i|em)>/g, '<span style="font-style:italic">')
  2832. .replace(/<a/g, '<span')
  2833. .replace(/<\/(b|strong|i|em|a)>/g, '</span>')
  2834. .split(/<br.*?>/g),
  2835. childNodes = textNode.childNodes,
  2836. styleRegex = /style="([^"]+)"/,
  2837. hrefRegex = /href="(http[^"]+)"/,
  2838. parentX = attr(textNode, 'x'),
  2839. textStyles = wrapper.styles,
  2840. width = textStyles && textStyles.width && pInt(textStyles.width),
  2841. textLineHeight = textStyles && textStyles.lineHeight,
  2842. i = childNodes.length;
  2843. /// remove old text
  2844. while (i--) {
  2845. textNode.removeChild(childNodes[i]);
  2846. }
  2847. if (width && !wrapper.added) {
  2848. this.box.appendChild(textNode); // attach it to the DOM to read offset width
  2849. }
  2850. // remove empty line at end
  2851. if (lines[lines.length - 1] === '') {
  2852. lines.pop();
  2853. }
  2854. // build the lines
  2855. each(lines, function (line, lineNo) {
  2856. var spans, spanNo = 0;
  2857. line = line.replace(/<span/g, '|||<span').replace(/<\/span>/g, '</span>|||');
  2858. spans = line.split('|||');
  2859. each(spans, function (span) {
  2860. if (span !== '' || spans.length === 1) {
  2861. var attributes = {},
  2862. tspan = doc.createElementNS(SVG_NS, 'tspan'),
  2863. spanStyle; // #390
  2864. if (styleRegex.test(span)) {
  2865. spanStyle = span.match(styleRegex)[1].replace(/(;| |^)color([ :])/, '$1fill$2');
  2866. attr(tspan, 'style', spanStyle);
  2867. }
  2868. if (hrefRegex.test(span) && !forExport) { // Not for export - #1529
  2869. attr(tspan, 'onclick', 'location.href=\"' + span.match(hrefRegex)[1] + '\"');
  2870. css(tspan, {cursor: 'pointer'});
  2871. }
  2872. span = (span.replace(/<(.|\n)*?>/g, '') || ' ')
  2873. .replace(/&lt;/g, '<')
  2874. .replace(/&gt;/g, '>');
  2875. // Nested tags aren't supported, and cause crash in Safari (#1596)
  2876. if (span !== ' ') {
  2877. // add the text node
  2878. tspan.appendChild(doc.createTextNode(span));
  2879. if (!spanNo) { // first span in a line, align it to the left
  2880. attributes.x = parentX;
  2881. } else {
  2882. attributes.dx = 0; // #16
  2883. }
  2884. // add attributes
  2885. attr(tspan, attributes);
  2886. // first span on subsequent line, add the line height
  2887. if (!spanNo && lineNo) {
  2888. // allow getting the right offset height in exporting in IE
  2889. if (!hasSVG && forExport) {
  2890. css(tspan, {display: 'block'});
  2891. }
  2892. // Set the line height based on the font size of either
  2893. // the text element or the tspan element
  2894. attr(
  2895. tspan,
  2896. 'dy',
  2897. textLineHeight || renderer.fontMetrics(
  2898. /px$/.test(tspan.style.fontSize) ?
  2899. tspan.style.fontSize :
  2900. textStyles.fontSize
  2901. ).h,
  2902. // Safari 6.0.2 - too optimized for its own good (#1539)
  2903. // TODO: revisit this with future versions of Safari
  2904. isWebKit && tspan.offsetHeight
  2905. );
  2906. }
  2907. // Append it
  2908. textNode.appendChild(tspan);
  2909. spanNo++;
  2910. // check width and apply soft breaks
  2911. if (width) {
  2912. var words = span.replace(/([^\^])-/g, '$1- ').split(' '), // #1273
  2913. tooLong,
  2914. actualWidth,
  2915. clipHeight = wrapper._clipHeight,
  2916. rest = [],
  2917. dy = pInt(textLineHeight || 16),
  2918. softLineNo = 1,
  2919. bBox;
  2920. while (words.length || rest.length) {
  2921. delete wrapper.bBox; // delete cache
  2922. bBox = wrapper.getBBox();
  2923. actualWidth = bBox.width;
  2924. tooLong = actualWidth > width;
  2925. if (!tooLong || words.length === 1) { // new line needed
  2926. words = rest;
  2927. rest = [];
  2928. if (words.length) {
  2929. softLineNo++;
  2930. if (clipHeight && softLineNo * dy > clipHeight) {
  2931. words = ['...'];
  2932. wrapper.attr('title', wrapper.textStr);
  2933. } else {
  2934. tspan = doc.createElementNS(SVG_NS, 'tspan');
  2935. attr(tspan, {
  2936. dy: dy,
  2937. x: parentX
  2938. });
  2939. if (spanStyle) { // #390
  2940. attr(tspan, 'style', spanStyle);
  2941. }
  2942. textNode.appendChild(tspan);
  2943. if (actualWidth > width) { // a single word is pressing it out
  2944. width = actualWidth;
  2945. }
  2946. }
  2947. }
  2948. } else { // append to existing line tspan
  2949. tspan.removeChild(tspan.firstChild);
  2950. rest.unshift(words.pop());
  2951. }
  2952. if (words.length) {
  2953. tspan.appendChild(doc.createTextNode(words.join(' ').replace(/- /g, '-')));
  2954. }
  2955. }
  2956. }
  2957. }
  2958. }
  2959. });
  2960. });
  2961. },
  2962. /**
  2963. * Create a button with preset states
  2964. * @param {String} text
  2965. * @param {Number} x
  2966. * @param {Number} y
  2967. * @param {Function} callback
  2968. * @param {Object} normalState
  2969. * @param {Object} hoverState
  2970. * @param {Object} pressedState
  2971. */
  2972. button: function (text, x, y, callback, normalState, hoverState, pressedState, disabledState) {
  2973. var label = this.label(text, x, y, null, null, null, null, null, 'button'),
  2974. curState = 0,
  2975. stateOptions,
  2976. stateStyle,
  2977. normalStyle,
  2978. hoverStyle,
  2979. pressedStyle,
  2980. disabledStyle,
  2981. STYLE = 'style',
  2982. verticalGradient = {x1: 0, y1: 0, x2: 0, y2: 1};
  2983. // Normal state - prepare the attributes
  2984. normalState = merge({
  2985. 'stroke-width': 1,
  2986. stroke: '#CCCCCC',
  2987. fill: {
  2988. linearGradient: verticalGradient,
  2989. stops: [
  2990. [0, '#FEFEFE'],
  2991. [1, '#F6F6F6']
  2992. ]
  2993. },
  2994. r: 2,
  2995. padding: 5,
  2996. style: {
  2997. color: 'black'
  2998. }
  2999. }, normalState);
  3000. normalStyle = normalState[STYLE];
  3001. delete normalState[STYLE];
  3002. // Hover state
  3003. hoverState = merge(normalState, {
  3004. stroke: '#68A',
  3005. fill: {
  3006. linearGradient: verticalGradient,
  3007. stops: [
  3008. [0, '#FFF'],
  3009. [1, '#ACF']
  3010. ]
  3011. }
  3012. }, hoverState);
  3013. hoverStyle = hoverState[STYLE];
  3014. delete hoverState[STYLE];
  3015. // Pressed state
  3016. pressedState = merge(normalState, {
  3017. stroke: '#68A',
  3018. fill: {
  3019. linearGradient: verticalGradient,
  3020. stops: [
  3021. [0, '#9BD'],
  3022. [1, '#CDF']
  3023. ]
  3024. }
  3025. }, pressedState);
  3026. pressedStyle = pressedState[STYLE];
  3027. delete pressedState[STYLE];
  3028. // Disabled state
  3029. disabledState = merge(normalState, {
  3030. style: {
  3031. color: '#CCC'
  3032. }
  3033. }, disabledState);
  3034. disabledStyle = disabledState[STYLE];
  3035. delete disabledState[STYLE];
  3036. // Add the events. IE9 and IE10 need mouseover and mouseout to funciton (#667).
  3037. addEvent(label.element, isIE ? 'mouseover' : 'mouseenter', function () {
  3038. if (curState !== 3) {
  3039. label.attr(hoverState)
  3040. .css(hoverStyle);
  3041. }
  3042. });
  3043. addEvent(label.element, isIE ? 'mouseout' : 'mouseleave', function () {
  3044. if (curState !== 3) {
  3045. stateOptions = [normalState, hoverState, pressedState][curState];
  3046. stateStyle = [normalStyle, hoverStyle, pressedStyle][curState];
  3047. label.attr(stateOptions)
  3048. .css(stateStyle);
  3049. }
  3050. });
  3051. label.setState = function (state) {
  3052. label.state = curState = state;
  3053. if (!state) {
  3054. label.attr(normalState)
  3055. .css(normalStyle);
  3056. } else if (state === 2) {
  3057. label.attr(pressedState)
  3058. .css(pressedStyle);
  3059. } else if (state === 3) {
  3060. label.attr(disabledState)
  3061. .css(disabledStyle);
  3062. }
  3063. };
  3064. return label
  3065. .on('click', function () {
  3066. if (curState !== 3) {
  3067. callback.call(label);
  3068. }
  3069. })
  3070. .attr(normalState)
  3071. .css(extend({cursor: 'default'}, normalStyle));
  3072. },
  3073. /**
  3074. * Make a straight line crisper by not spilling out to neighbour pixels
  3075. * @param {Array} points
  3076. * @param {Number} width
  3077. */
  3078. crispLine: function (points, width) {
  3079. // points format: [M, 0, 0, L, 100, 0]
  3080. // normalize to a crisp line
  3081. if (points[1] === points[4]) {
  3082. // Substract due to #1129. Now bottom and left axis gridlines behave the same.
  3083. points[1] = points[4] = mathRound(points[1]) - (width % 2 / 2);
  3084. }
  3085. if (points[2] === points[5]) {
  3086. points[2] = points[5] = mathRound(points[2]) + (width % 2 / 2);
  3087. }
  3088. return points;
  3089. },
  3090. /**
  3091. * Draw a path
  3092. * @param {Array} path An SVG path in array form
  3093. */
  3094. path: function (path) {
  3095. var attr = {
  3096. fill: NONE
  3097. };
  3098. if (isArray(path)) {
  3099. attr.d = path;
  3100. } else if (isObject(path)) { // attributes
  3101. extend(attr, path);
  3102. }
  3103. return this.createElement('path').attr(attr);
  3104. },
  3105. /**
  3106. * Draw and return an SVG circle
  3107. * @param {Number} x The x position
  3108. * @param {Number} y The y position
  3109. * @param {Number} r The radius
  3110. */
  3111. circle: function (x, y, r) {
  3112. var attr = isObject(x) ?
  3113. x :
  3114. {
  3115. x: x,
  3116. y: y,
  3117. r: r
  3118. };
  3119. return this.createElement('circle').attr(attr);
  3120. },
  3121. /**
  3122. * Draw and return an arc
  3123. * @param {Number} x X position
  3124. * @param {Number} y Y position
  3125. * @param {Number} r Radius
  3126. * @param {Number} innerR Inner radius like used in donut charts
  3127. * @param {Number} start Starting angle
  3128. * @param {Number} end Ending angle
  3129. */
  3130. arc: function (x, y, r, innerR, start, end) {
  3131. var arc;
  3132. if (isObject(x)) {
  3133. y = x.y;
  3134. r = x.r;
  3135. innerR = x.innerR;
  3136. start = x.start;
  3137. end = x.end;
  3138. x = x.x;
  3139. }
  3140. // Arcs are defined as symbols for the ability to set
  3141. // attributes in attr and animate
  3142. arc = this.symbol('arc', x || 0, y || 0, r || 0, r || 0, {
  3143. innerR: innerR || 0,
  3144. start: start || 0,
  3145. end: end || 0
  3146. });
  3147. arc.r = r; // #959
  3148. return arc;
  3149. },
  3150. /**
  3151. * Draw and return a rectangle
  3152. * @param {Number} x Left position
  3153. * @param {Number} y Top position
  3154. * @param {Number} width
  3155. * @param {Number} height
  3156. * @param {Number} r Border corner radius
  3157. * @param {Number} strokeWidth A stroke width can be supplied to allow crisp drawing
  3158. */
  3159. rect: function (x, y, width, height, r, strokeWidth) {
  3160. r = isObject(x) ? x.r : r;
  3161. var wrapper = this.createElement('rect').attr({
  3162. rx: r,
  3163. ry: r,
  3164. fill: NONE
  3165. });
  3166. return wrapper.attr(
  3167. isObject(x) ?
  3168. x :
  3169. // do not crispify when an object is passed in (as in column charts)
  3170. wrapper.crisp(strokeWidth, x, y, mathMax(width, 0), mathMax(height, 0))
  3171. );
  3172. },
  3173. /**
  3174. * Resize the box and re-align all aligned elements
  3175. * @param {Object} width
  3176. * @param {Object} height
  3177. * @param {Boolean} animate
  3178. *
  3179. */
  3180. setSize: function (width, height, animate) {
  3181. var renderer = this,
  3182. alignedObjects = renderer.alignedObjects,
  3183. i = alignedObjects.length;
  3184. renderer.width = width;
  3185. renderer.height = height;
  3186. renderer.boxWrapper[pick(animate, true) ? 'animate' : 'attr']({
  3187. width: width,
  3188. height: height
  3189. });
  3190. while (i--) {
  3191. alignedObjects[i].align();
  3192. }
  3193. },
  3194. /**
  3195. * Create a group
  3196. * @param {String} name The group will be given a class name of 'highcharts-{name}'.
  3197. * This can be used for styling and scripting.
  3198. */
  3199. g: function (name) {
  3200. var elem = this.createElement('g');
  3201. return defined(name) ? elem.attr({'class': PREFIX + name}) : elem;
  3202. },
  3203. /**
  3204. * Display an image
  3205. * @param {String} src
  3206. * @param {Number} x
  3207. * @param {Number} y
  3208. * @param {Number} width
  3209. * @param {Number} height
  3210. */
  3211. image: function (src, x, y, width, height) {
  3212. var attribs = {
  3213. preserveAspectRatio: NONE
  3214. },
  3215. elemWrapper;
  3216. // optional properties
  3217. if (arguments.length > 1) {
  3218. extend(attribs, {
  3219. x: x,
  3220. y: y,
  3221. width: width,
  3222. height: height
  3223. });
  3224. }
  3225. elemWrapper = this.createElement('image').attr(attribs);
  3226. // set the href in the xlink namespace
  3227. if (elemWrapper.element.setAttributeNS) {
  3228. elemWrapper.element.setAttributeNS('http://www.w3.org/1999/xlink',
  3229. 'href', src);
  3230. } else {
  3231. // could be exporting in IE
  3232. // using href throws "not supported" in ie7 and under, requries regex shim to fix later
  3233. elemWrapper.element.setAttribute('hc-svg-href', src);
  3234. }
  3235. return elemWrapper;
  3236. },
  3237. /**
  3238. * Draw a symbol out of pre-defined shape paths from the namespace 'symbol' object.
  3239. *
  3240. * @param {Object} symbol
  3241. * @param {Object} x
  3242. * @param {Object} y
  3243. * @param {Object} radius
  3244. * @param {Object} options
  3245. */
  3246. symbol: function (symbol, x, y, width, height, options) {
  3247. var obj,
  3248. // get the symbol definition function
  3249. symbolFn = this.symbols[symbol],
  3250. // check if there's a path defined for this symbol
  3251. path = symbolFn && symbolFn(
  3252. mathRound(x),
  3253. mathRound(y),
  3254. width,
  3255. height,
  3256. options
  3257. ),
  3258. imageElement,
  3259. imageRegex = /^url\((.*?)\)$/,
  3260. imageSrc,
  3261. imageSize,
  3262. centerImage;
  3263. if (path) {
  3264. obj = this.path(path);
  3265. // expando properties for use in animate and attr
  3266. extend(obj, {
  3267. symbolName: symbol,
  3268. x: x,
  3269. y: y,
  3270. width: width,
  3271. height: height
  3272. });
  3273. if (options) {
  3274. extend(obj, options);
  3275. }
  3276. // image symbols
  3277. } else if (imageRegex.test(symbol)) {
  3278. // On image load, set the size and position
  3279. centerImage = function (img, size) {
  3280. if (img.element) { // it may be destroyed in the meantime (#1390)
  3281. img.attr({
  3282. width: size[0],
  3283. height: size[1]
  3284. });
  3285. if (!img.alignByTranslate) { // #185
  3286. img.translate(
  3287. mathRound((width - size[0]) / 2), // #1378
  3288. mathRound((height - size[1]) / 2)
  3289. );
  3290. }
  3291. }
  3292. };
  3293. imageSrc = symbol.match(imageRegex)[1];
  3294. imageSize = symbolSizes[imageSrc];
  3295. // Ireate the image synchronously, add attribs async
  3296. obj = this.image(imageSrc)
  3297. .attr({
  3298. x: x,
  3299. y: y
  3300. });
  3301. obj.isImg = true;
  3302. if (imageSize) {
  3303. centerImage(obj, imageSize);
  3304. } else {
  3305. // Initialize image to be 0 size so export will still function if there's no cached sizes.
  3306. //
  3307. obj.attr({width: 0, height: 0});
  3308. // Create a dummy JavaScript image to get the width and height. Due to a bug in IE < 8,
  3309. // the created element must be assigned to a variable in order to load (#292).
  3310. imageElement = createElement('img', {
  3311. onload: function () {
  3312. centerImage(obj, symbolSizes[imageSrc] = [this.width, this.height]);
  3313. },
  3314. src: imageSrc
  3315. });
  3316. }
  3317. }
  3318. return obj;
  3319. },
  3320. /**
  3321. * An extendable collection of functions for defining symbol paths.
  3322. */
  3323. symbols: {
  3324. 'circle': function (x, y, w, h) {
  3325. var cpw = 0.166 * w;
  3326. return [
  3327. M, x + w / 2, y,
  3328. 'C', x + w + cpw, y, x + w + cpw, y + h, x + w / 2, y + h,
  3329. 'C', x - cpw, y + h, x - cpw, y, x + w / 2, y,
  3330. 'Z'
  3331. ];
  3332. },
  3333. 'square': function (x, y, w, h) {
  3334. return [
  3335. M, x, y,
  3336. L, x + w, y,
  3337. x + w, y + h,
  3338. x, y + h,
  3339. 'Z'
  3340. ];
  3341. },
  3342. 'triangle': function (x, y, w, h) {
  3343. return [
  3344. M, x + w / 2, y,
  3345. L, x + w, y + h,
  3346. x, y + h,
  3347. 'Z'
  3348. ];
  3349. },
  3350. 'triangle-down': function (x, y, w, h) {
  3351. return [
  3352. M, x, y,
  3353. L, x + w, y,
  3354. x + w / 2, y + h,
  3355. 'Z'
  3356. ];
  3357. },
  3358. 'diamond': function (x, y, w, h) {
  3359. return [
  3360. M, x + w / 2, y,
  3361. L, x + w, y + h / 2,
  3362. x + w / 2, y + h,
  3363. x, y + h / 2,
  3364. 'Z'
  3365. ];
  3366. },
  3367. 'arc': function (x, y, w, h, options) {
  3368. var start = options.start,
  3369. radius = options.r || w || h,
  3370. end = options.end - 0.001, // to prevent cos and sin of start and end from becoming equal on 360 arcs (related: #1561)
  3371. innerRadius = options.innerR,
  3372. open = options.open,
  3373. cosStart = mathCos(start),
  3374. sinStart = mathSin(start),
  3375. cosEnd = mathCos(end),
  3376. sinEnd = mathSin(end),
  3377. longArc = options.end - start < mathPI ? 0 : 1;
  3378. return [
  3379. M,
  3380. x + radius * cosStart,
  3381. y + radius * sinStart,
  3382. 'A', // arcTo
  3383. radius, // x radius
  3384. radius, // y radius
  3385. 0, // slanting
  3386. longArc, // long or short arc
  3387. 1, // clockwise
  3388. x + radius * cosEnd,
  3389. y + radius * sinEnd,
  3390. open ? M : L,
  3391. x + innerRadius * cosEnd,
  3392. y + innerRadius * sinEnd,
  3393. 'A', // arcTo
  3394. innerRadius, // x radius
  3395. innerRadius, // y radius
  3396. 0, // slanting
  3397. longArc, // long or short arc
  3398. 0, // clockwise
  3399. x + innerRadius * cosStart,
  3400. y + innerRadius * sinStart,
  3401. open ? '' : 'Z' // close
  3402. ];
  3403. }
  3404. },
  3405. /**
  3406. * Define a clipping rectangle
  3407. * @param {String} id
  3408. * @param {Number} x
  3409. * @param {Number} y
  3410. * @param {Number} width
  3411. * @param {Number} height
  3412. */
  3413. clipRect: function (x, y, width, height) {
  3414. var wrapper,
  3415. id = PREFIX + idCounter++,
  3416. clipPath = this.createElement('clipPath').attr({
  3417. id: id
  3418. }).add(this.defs);
  3419. wrapper = this.rect(x, y, width, height, 0).add(clipPath);
  3420. wrapper.id = id;
  3421. wrapper.clipPath = clipPath;
  3422. return wrapper;
  3423. },
  3424. /**
  3425. * Take a color and return it if it's a string, make it a gradient if it's a
  3426. * gradient configuration object. Prior to Highstock, an array was used to define
  3427. * a linear gradient with pixel positions relative to the SVG. In newer versions
  3428. * we change the coordinates to apply relative to the shape, using coordinates
  3429. * 0-1 within the shape. To preserve backwards compatibility, linearGradient
  3430. * in this definition is an object of x1, y1, x2 and y2.
  3431. *
  3432. * @param {Object} color The color or config object
  3433. */
  3434. color: function (color, elem, prop) {
  3435. var renderer = this,
  3436. colorObject,
  3437. regexRgba = /^rgba/,
  3438. gradName,
  3439. gradAttr,
  3440. gradients,
  3441. gradientObject,
  3442. stops,
  3443. stopColor,
  3444. stopOpacity,
  3445. radialReference,
  3446. n,
  3447. id,
  3448. key = [];
  3449. // Apply linear or radial gradients
  3450. if (color && color.linearGradient) {
  3451. gradName = 'linearGradient';
  3452. } else if (color && color.radialGradient) {
  3453. gradName = 'radialGradient';
  3454. }
  3455. if (gradName) {
  3456. gradAttr = color[gradName];
  3457. gradients = renderer.gradients;
  3458. stops = color.stops;
  3459. radialReference = elem.radialReference;
  3460. // Keep < 2.2 kompatibility
  3461. if (isArray(gradAttr)) {
  3462. color[gradName] = gradAttr = {
  3463. x1: gradAttr[0],
  3464. y1: gradAttr[1],
  3465. x2: gradAttr[2],
  3466. y2: gradAttr[3],
  3467. gradientUnits: 'userSpaceOnUse'
  3468. };
  3469. }
  3470. // Correct the radial gradient for the radial reference system
  3471. if (gradName === 'radialGradient' && radialReference && !defined(gradAttr.gradientUnits)) {
  3472. gradAttr = merge(gradAttr, {
  3473. cx: (radialReference[0] - radialReference[2] / 2) + gradAttr.cx * radialReference[2],
  3474. cy: (radialReference[1] - radialReference[2] / 2) + gradAttr.cy * radialReference[2],
  3475. r: gradAttr.r * radialReference[2],
  3476. gradientUnits: 'userSpaceOnUse'
  3477. });
  3478. }
  3479. // Build the unique key to detect whether we need to create a new element (#1282)
  3480. for (n in gradAttr) {
  3481. if (n !== 'id') {
  3482. key.push(n, gradAttr[n]);
  3483. }
  3484. }
  3485. for (n in stops) {
  3486. key.push(stops[n]);
  3487. }
  3488. key = key.join(',');
  3489. // Check if a gradient object with the same config object is created within this renderer
  3490. if (gradients[key]) {
  3491. id = gradients[key].id;
  3492. } else {
  3493. // Set the id and create the element
  3494. gradAttr.id = id = PREFIX + idCounter++;
  3495. gradients[key] = gradientObject = renderer.createElement(gradName)
  3496. .attr(gradAttr)
  3497. .add(renderer.defs);
  3498. // The gradient needs to keep a list of stops to be able to destroy them
  3499. gradientObject.stops = [];
  3500. each(stops, function (stop) {
  3501. var stopObject;
  3502. if (regexRgba.test(stop[1])) {
  3503. colorObject = Color(stop[1]);
  3504. stopColor = colorObject.get('rgb');
  3505. stopOpacity = colorObject.get('a');
  3506. } else {
  3507. stopColor = stop[1];
  3508. stopOpacity = 1;
  3509. }
  3510. stopObject = renderer.createElement('stop').attr({
  3511. offset: stop[0],
  3512. 'stop-color': stopColor,
  3513. 'stop-opacity': stopOpacity
  3514. }).add(gradientObject);
  3515. // Add the stop element to the gradient
  3516. gradientObject.stops.push(stopObject);
  3517. });
  3518. }
  3519. // Return the reference to the gradient object
  3520. return 'url(' + renderer.url + '#' + id + ')';
  3521. // Webkit and Batik can't show rgba.
  3522. } else if (regexRgba.test(color)) {
  3523. colorObject = Color(color);
  3524. attr(elem, prop + '-opacity', colorObject.get('a'));
  3525. return colorObject.get('rgb');
  3526. } else {
  3527. // Remove the opacity attribute added above. Does not throw if the attribute is not there.
  3528. elem.removeAttribute(prop + '-opacity');
  3529. return color;
  3530. }
  3531. },
  3532. /**
  3533. * Add text to the SVG object
  3534. * @param {String} str
  3535. * @param {Number} x Left position
  3536. * @param {Number} y Top position
  3537. * @param {Boolean} useHTML Use HTML to render the text
  3538. */
  3539. text: function (str, x, y, useHTML) {
  3540. // declare variables
  3541. var renderer = this,
  3542. defaultChartStyle = defaultOptions.chart.style,
  3543. fakeSVG = useCanVG || (!hasSVG && renderer.forExport),
  3544. wrapper;
  3545. if (useHTML && !renderer.forExport) {
  3546. return renderer.html(str, x, y);
  3547. }
  3548. x = mathRound(pick(x, 0));
  3549. y = mathRound(pick(y, 0));
  3550. wrapper = renderer.createElement('text')
  3551. .attr({
  3552. x: x,
  3553. y: y,
  3554. text: str
  3555. })
  3556. .css({
  3557. fontFamily: defaultChartStyle.fontFamily,
  3558. fontSize: defaultChartStyle.fontSize
  3559. });
  3560. // Prevent wrapping from creating false offsetWidths in export in legacy IE (#1079, #1063)
  3561. if (fakeSVG) {
  3562. wrapper.css({
  3563. position: ABSOLUTE
  3564. });
  3565. }
  3566. wrapper.x = x;
  3567. wrapper.y = y;
  3568. return wrapper;
  3569. },
  3570. /**
  3571. * Create HTML text node. This is used by the VML renderer as well as the SVG
  3572. * renderer through the useHTML option.
  3573. *
  3574. * @param {String} str
  3575. * @param {Number} x
  3576. * @param {Number} y
  3577. */
  3578. html: function (str, x, y) {
  3579. var defaultChartStyle = defaultOptions.chart.style,
  3580. wrapper = this.createElement('span'),
  3581. attrSetters = wrapper.attrSetters,
  3582. element = wrapper.element,
  3583. renderer = wrapper.renderer;
  3584. // Text setter
  3585. attrSetters.text = function (value) {
  3586. if (value !== element.innerHTML) {
  3587. delete this.bBox;
  3588. }
  3589. element.innerHTML = value;
  3590. return false;
  3591. };
  3592. // Various setters which rely on update transform
  3593. attrSetters.x = attrSetters.y = attrSetters.align = function (value, key) {
  3594. if (key === 'align') {
  3595. key = 'textAlign'; // Do not overwrite the SVGElement.align method. Same as VML.
  3596. }
  3597. wrapper[key] = value;
  3598. wrapper.htmlUpdateTransform();
  3599. return false;
  3600. };
  3601. // Set the default attributes
  3602. wrapper.attr({
  3603. text: str,
  3604. x: mathRound(x),
  3605. y: mathRound(y)
  3606. })
  3607. .css({
  3608. position: ABSOLUTE,
  3609. whiteSpace: 'nowrap',
  3610. fontFamily: defaultChartStyle.fontFamily,
  3611. fontSize: defaultChartStyle.fontSize
  3612. });
  3613. // Use the HTML specific .css method
  3614. wrapper.css = wrapper.htmlCss;
  3615. // This is specific for HTML within SVG
  3616. if (renderer.isSVG) {
  3617. wrapper.add = function (svgGroupWrapper) {
  3618. var htmlGroup,
  3619. container = renderer.box.parentNode,
  3620. parentGroup,
  3621. parents = [];
  3622. // Create a mock group to hold the HTML elements
  3623. if (svgGroupWrapper) {
  3624. htmlGroup = svgGroupWrapper.div;
  3625. if (!htmlGroup) {
  3626. // Read the parent chain into an array and read from top down
  3627. parentGroup = svgGroupWrapper;
  3628. while (parentGroup) {
  3629. parents.push(parentGroup);
  3630. // Move up to the next parent group
  3631. parentGroup = parentGroup.parentGroup;
  3632. }
  3633. // Ensure dynamically updating position when any parent is translated
  3634. each(parents.reverse(), function (parentGroup) {
  3635. var htmlGroupStyle;
  3636. // Create a HTML div and append it to the parent div to emulate
  3637. // the SVG group structure
  3638. htmlGroup = parentGroup.div = parentGroup.div || createElement(DIV, {
  3639. className: attr(parentGroup.element, 'class')
  3640. }, {
  3641. position: ABSOLUTE,
  3642. left: (parentGroup.translateX || 0) + PX,
  3643. top: (parentGroup.translateY || 0) + PX
  3644. }, htmlGroup || container); // the top group is appended to container
  3645. // Shortcut
  3646. htmlGroupStyle = htmlGroup.style;
  3647. // Set listeners to update the HTML div's position whenever the SVG group
  3648. // position is changed
  3649. extend(parentGroup.attrSetters, {
  3650. translateX: function (value) {
  3651. htmlGroupStyle.left = value + PX;
  3652. },
  3653. translateY: function (value) {
  3654. htmlGroupStyle.top = value + PX;
  3655. },
  3656. visibility: function (value, key) {
  3657. htmlGroupStyle[key] = value;
  3658. }
  3659. });
  3660. });
  3661. }
  3662. } else {
  3663. htmlGroup = container;
  3664. }
  3665. htmlGroup.appendChild(element);
  3666. // Shared with VML:
  3667. wrapper.added = true;
  3668. if (wrapper.alignOnAdd) {
  3669. wrapper.htmlUpdateTransform();
  3670. }
  3671. return wrapper;
  3672. };
  3673. }
  3674. return wrapper;
  3675. },
  3676. /**
  3677. * Utility to return the baseline offset and total line height from the font size
  3678. */
  3679. fontMetrics: function (fontSize) {
  3680. fontSize = pInt(fontSize || 11);
  3681. // Empirical values found by comparing font size and bounding box height.
  3682. // Applies to the default font family. http://jsfiddle.net/highcharts/7xvn7/
  3683. var lineHeight = fontSize < 24 ? fontSize + 4 : mathRound(fontSize * 1.2),
  3684. baseline = mathRound(lineHeight * 0.8);
  3685. return {
  3686. h: lineHeight,
  3687. b: baseline
  3688. };
  3689. },
  3690. /**
  3691. * Add a label, a text item that can hold a colored or gradient background
  3692. * as well as a border and shadow.
  3693. * @param {string} str
  3694. * @param {Number} x
  3695. * @param {Number} y
  3696. * @param {String} shape
  3697. * @param {Number} anchorX In case the shape has a pointer, like a flag, this is the
  3698. * coordinates it should be pinned to
  3699. * @param {Number} anchorY
  3700. * @param {Boolean} baseline Whether to position the label relative to the text baseline,
  3701. * like renderer.text, or to the upper border of the rectangle.
  3702. * @param {String} className Class name for the group
  3703. */
  3704. label: function (str, x, y, shape, anchorX, anchorY, useHTML, baseline, className) {
  3705. var renderer = this,
  3706. wrapper = renderer.g(className),
  3707. text = renderer.text('', 0, 0, useHTML)
  3708. .attr({
  3709. zIndex: 1
  3710. }),
  3711. //.add(wrapper),
  3712. box,
  3713. bBox,
  3714. alignFactor = 0,
  3715. padding = 3,
  3716. paddingLeft = 0,
  3717. width,
  3718. height,
  3719. wrapperX,
  3720. wrapperY,
  3721. crispAdjust = 0,
  3722. deferredAttr = {},
  3723. baselineOffset,
  3724. attrSetters = wrapper.attrSetters,
  3725. needsBox;
  3726. /**
  3727. * This function runs after the label is added to the DOM (when the bounding box is
  3728. * available), and after the text of the label is updated to detect the new bounding
  3729. * box and reflect it in the border box.
  3730. */
  3731. function updateBoxSize() {
  3732. var boxX,
  3733. boxY,
  3734. style = text.element.style;
  3735. bBox = (width === undefined || height === undefined || wrapper.styles.textAlign) &&
  3736. text.getBBox();
  3737. wrapper.width = (width || bBox.width || 0) + 2 * padding + paddingLeft;
  3738. wrapper.height = (height || bBox.height || 0) + 2 * padding;
  3739. // update the label-scoped y offset
  3740. baselineOffset = padding + renderer.fontMetrics(style && style.fontSize).b;
  3741. if (needsBox) {
  3742. // create the border box if it is not already present
  3743. if (!box) {
  3744. boxX = mathRound(-alignFactor * padding);
  3745. boxY = baseline ? -baselineOffset : 0;
  3746. wrapper.box = box = shape ?
  3747. renderer.symbol(shape, boxX, boxY, wrapper.width, wrapper.height) :
  3748. renderer.rect(boxX, boxY, wrapper.width, wrapper.height, 0, deferredAttr[STROKE_WIDTH]);
  3749. box.add(wrapper);
  3750. }
  3751. // apply the box attributes
  3752. if (!box.isImg) { // #1630
  3753. box.attr(merge({
  3754. width: wrapper.width,
  3755. height: wrapper.height
  3756. }, deferredAttr));
  3757. }
  3758. deferredAttr = null;
  3759. }
  3760. }
  3761. /**
  3762. * This function runs after setting text or padding, but only if padding is changed
  3763. */
  3764. function updateTextPadding() {
  3765. var styles = wrapper.styles,
  3766. textAlign = styles && styles.textAlign,
  3767. x = paddingLeft + padding * (1 - alignFactor),
  3768. y;
  3769. // determin y based on the baseline
  3770. y = baseline ? 0 : baselineOffset;
  3771. // compensate for alignment
  3772. if (defined(width) && (textAlign === 'center' || textAlign === 'right')) {
  3773. x += {center: 0.5, right: 1}[textAlign] * (width - bBox.width);
  3774. }
  3775. // update if anything changed
  3776. if (x !== text.x || y !== text.y) {
  3777. text.attr({
  3778. x: x,
  3779. y: y
  3780. });
  3781. }
  3782. // record current values
  3783. text.x = x;
  3784. text.y = y;
  3785. }
  3786. /**
  3787. * Set a box attribute, or defer it if the box is not yet created
  3788. * @param {Object} key
  3789. * @param {Object} value
  3790. */
  3791. function boxAttr(key, value) {
  3792. if (box) {
  3793. box.attr(key, value);
  3794. } else {
  3795. deferredAttr[key] = value;
  3796. }
  3797. }
  3798. function getSizeAfterAdd() {
  3799. text.add(wrapper);
  3800. wrapper.attr({
  3801. text: str, // alignment is available now
  3802. x: x,
  3803. y: y
  3804. });
  3805. if (box && defined(anchorX)) {
  3806. wrapper.attr({
  3807. anchorX: anchorX,
  3808. anchorY: anchorY
  3809. });
  3810. }
  3811. }
  3812. /**
  3813. * After the text element is added, get the desired size of the border box
  3814. * and add it before the text in the DOM.
  3815. */
  3816. addEvent(wrapper, 'add', getSizeAfterAdd);
  3817. /*
  3818. * Add specific attribute setters.
  3819. */
  3820. // only change local variables
  3821. attrSetters.width = function (value) {
  3822. width = value;
  3823. return false;
  3824. };
  3825. attrSetters.height = function (value) {
  3826. height = value;
  3827. return false;
  3828. };
  3829. attrSetters.padding = function (value) {
  3830. if (defined(value) && value !== padding) {
  3831. padding = value;
  3832. updateTextPadding();
  3833. }
  3834. return false;
  3835. };
  3836. attrSetters.paddingLeft = function (value) {
  3837. if (defined(value) && value !== paddingLeft) {
  3838. paddingLeft = value;
  3839. updateTextPadding();
  3840. }
  3841. return false;
  3842. };
  3843. // change local variable and set attribue as well
  3844. attrSetters.align = function (value) {
  3845. alignFactor = {left: 0, center: 0.5, right: 1}[value];
  3846. return false; // prevent setting text-anchor on the group
  3847. };
  3848. // apply these to the box and the text alike
  3849. attrSetters.text = function (value, key) {
  3850. text.attr(key, value);
  3851. updateBoxSize();
  3852. updateTextPadding();
  3853. return false;
  3854. };
  3855. // apply these to the box but not to the text
  3856. attrSetters[STROKE_WIDTH] = function (value, key) {
  3857. needsBox = true;
  3858. crispAdjust = value % 2 / 2;
  3859. boxAttr(key, value);
  3860. return false;
  3861. };
  3862. attrSetters.stroke = attrSetters.fill = attrSetters.r = function (value, key) {
  3863. if (key === 'fill') {
  3864. needsBox = true;
  3865. }
  3866. boxAttr(key, value);
  3867. return false;
  3868. };
  3869. attrSetters.anchorX = function (value, key) {
  3870. anchorX = value;
  3871. boxAttr(key, value + crispAdjust - wrapperX);
  3872. return false;
  3873. };
  3874. attrSetters.anchorY = function (value, key) {
  3875. anchorY = value;
  3876. boxAttr(key, value - wrapperY);
  3877. return false;
  3878. };
  3879. // rename attributes
  3880. attrSetters.x = function (value) {
  3881. wrapper.x = value; // for animation getter
  3882. value -= alignFactor * ((width || bBox.width) + padding);
  3883. wrapperX = mathRound(value);
  3884. wrapper.attr('translateX', wrapperX);
  3885. return false;
  3886. };
  3887. attrSetters.y = function (value) {
  3888. wrapperY = wrapper.y = mathRound(value);
  3889. wrapper.attr('translateY', wrapperY);
  3890. return false;
  3891. };
  3892. // Redirect certain methods to either the box or the text
  3893. var baseCss = wrapper.css;
  3894. return extend(wrapper, {
  3895. /**
  3896. * Pick up some properties and apply them to the text instead of the wrapper
  3897. */
  3898. css: function (styles) {
  3899. if (styles) {
  3900. var textStyles = {};
  3901. styles = merge(styles); // create a copy to avoid altering the original object (#537)
  3902. each(['fontSize', 'fontWeight', 'fontFamily', 'color', 'lineHeight', 'width', 'textDecoration', 'textShadow'], function (prop) {
  3903. if (styles[prop] !== UNDEFINED) {
  3904. textStyles[prop] = styles[prop];
  3905. delete styles[prop];
  3906. }
  3907. });
  3908. text.css(textStyles);
  3909. }
  3910. return baseCss.call(wrapper, styles);
  3911. },
  3912. /**
  3913. * Return the bounding box of the box, not the group
  3914. */
  3915. getBBox: function () {
  3916. return {
  3917. width: bBox.width + 2 * padding,
  3918. height: bBox.height + 2 * padding,
  3919. x: bBox.x - padding,
  3920. y: bBox.y - padding
  3921. };
  3922. },
  3923. /**
  3924. * Apply the shadow to the box
  3925. */
  3926. shadow: function (b) {
  3927. if (box) {
  3928. box.shadow(b);
  3929. }
  3930. return wrapper;
  3931. },
  3932. /**
  3933. * Destroy and release memory.
  3934. */
  3935. destroy: function () {
  3936. removeEvent(wrapper, 'add', getSizeAfterAdd);
  3937. // Added by button implementation
  3938. removeEvent(wrapper.element, 'mouseenter');
  3939. removeEvent(wrapper.element, 'mouseleave');
  3940. if (text) {
  3941. text = text.destroy();
  3942. }
  3943. if (box) {
  3944. box = box.destroy();
  3945. }
  3946. // Call base implementation to destroy the rest
  3947. SVGElement.prototype.destroy.call(wrapper);
  3948. // Release local pointers (#1298)
  3949. wrapper = renderer = updateBoxSize = updateTextPadding = boxAttr = getSizeAfterAdd = null;
  3950. }
  3951. });
  3952. }
  3953. }; // end SVGRenderer
  3954. // general renderer
  3955. Renderer = SVGRenderer;
  3956. /* ****************************************************************************
  3957. * *
  3958. * START OF INTERNET EXPLORER <= 8 SPECIFIC CODE *
  3959. * *
  3960. * For applications and websites that don't need IE support, like platform *
  3961. * targeted mobile apps and web apps, this code can be removed. *
  3962. * *
  3963. *****************************************************************************/
  3964. /**
  3965. * @constructor
  3966. */
  3967. var VMLRenderer, VMLElement;
  3968. if (!hasSVG && !useCanVG) {
  3969. /**
  3970. * The VML element wrapper.
  3971. */
  3972. Highcharts.VMLElement = VMLElement = {
  3973. /**
  3974. * Initialize a new VML element wrapper. It builds the markup as a string
  3975. * to minimize DOM traffic.
  3976. * @param {Object} renderer
  3977. * @param {Object} nodeName
  3978. */
  3979. init: function (renderer, nodeName) {
  3980. var wrapper = this,
  3981. markup = ['<', nodeName, ' filled="f" stroked="f"'],
  3982. style = ['position: ', ABSOLUTE, ';'],
  3983. isDiv = nodeName === DIV;
  3984. // divs and shapes need size
  3985. if (nodeName === 'shape' || isDiv) {
  3986. style.push('left:0;top:0;width:1px;height:1px;');
  3987. }
  3988. style.push('visibility: ', isDiv ? HIDDEN : VISIBLE);
  3989. markup.push(' style="', style.join(''), '"/>');
  3990. // create element with default attributes and style
  3991. if (nodeName) {
  3992. markup = isDiv || nodeName === 'span' || nodeName === 'img' ?
  3993. markup.join('')
  3994. : renderer.prepVML(markup);
  3995. wrapper.element = createElement(markup);
  3996. }
  3997. wrapper.renderer = renderer;
  3998. wrapper.attrSetters = {};
  3999. },
  4000. /**
  4001. * Add the node to the given parent
  4002. * @param {Object} parent
  4003. */
  4004. add: function (parent) {
  4005. var wrapper = this,
  4006. renderer = wrapper.renderer,
  4007. element = wrapper.element,
  4008. box = renderer.box,
  4009. inverted = parent && parent.inverted,
  4010. // get the parent node
  4011. parentNode = parent ?
  4012. parent.element || parent :
  4013. box;
  4014. // if the parent group is inverted, apply inversion on all children
  4015. if (inverted) { // only on groups
  4016. renderer.invertChild(element, parentNode);
  4017. }
  4018. // append it
  4019. parentNode.appendChild(element);
  4020. // align text after adding to be able to read offset
  4021. wrapper.added = true;
  4022. if (wrapper.alignOnAdd && !wrapper.deferUpdateTransform) {
  4023. wrapper.updateTransform();
  4024. }
  4025. // fire an event for internal hooks
  4026. fireEvent(wrapper, 'add');
  4027. return wrapper;
  4028. },
  4029. /**
  4030. * VML always uses htmlUpdateTransform
  4031. */
  4032. updateTransform: SVGElement.prototype.htmlUpdateTransform,
  4033. /**
  4034. * Set the rotation of a span with oldIE's filter
  4035. */
  4036. setSpanRotation: function (rotation, sintheta, costheta) {
  4037. // Adjust for alignment and rotation. Rotation of useHTML content is not yet implemented
  4038. // but it can probably be implemented for Firefox 3.5+ on user request. FF3.5+
  4039. // has support for CSS3 transform. The getBBox method also needs to be updated
  4040. // to compensate for the rotation, like it currently does for SVG.
  4041. // Test case: http://highcharts.com/tests/?file=text-rotation
  4042. css(this.element, {
  4043. filter: rotation ? ['progid:DXImageTransform.Microsoft.Matrix(M11=', costheta,
  4044. ', M12=', -sintheta, ', M21=', sintheta, ', M22=', costheta,
  4045. ', sizingMethod=\'auto expand\')'].join('') : NONE
  4046. });
  4047. },
  4048. /**
  4049. * Converts a subset of an SVG path definition to its VML counterpart. Takes an array
  4050. * as the parameter and returns a string.
  4051. */
  4052. pathToVML: function (value) {
  4053. // convert paths
  4054. var i = value.length,
  4055. path = [],
  4056. clockwise;
  4057. while (i--) {
  4058. // Multiply by 10 to allow subpixel precision.
  4059. // Substracting half a pixel seems to make the coordinates
  4060. // align with SVG, but this hasn't been tested thoroughly
  4061. if (isNumber(value[i])) {
  4062. path[i] = mathRound(value[i] * 10) - 5;
  4063. } else if (value[i] === 'Z') { // close the path
  4064. path[i] = 'x';
  4065. } else {
  4066. path[i] = value[i];
  4067. // When the start X and end X coordinates of an arc are too close,
  4068. // they are rounded to the same value above. In this case, substract 1 from the end X
  4069. // position. #760, #1371.
  4070. if (value.isArc && (value[i] === 'wa' || value[i] === 'at')) {
  4071. clockwise = value[i] === 'wa' ? 1 : -1; // #1642
  4072. if (path[i + 5] === path[i + 7]) {
  4073. path[i + 7] -= clockwise;
  4074. }
  4075. // Start and end Y (#1410)
  4076. if (path[i + 6] === path[i + 8]) {
  4077. path[i + 8] -= clockwise;
  4078. }
  4079. }
  4080. }
  4081. }
  4082. // Loop up again to handle path shortcuts (#2132)
  4083. /*while (i++ < path.length) {
  4084. if (path[i] === 'H') { // horizontal line to
  4085. path[i] = 'L';
  4086. path.splice(i + 2, 0, path[i - 1]);
  4087. } else if (path[i] === 'V') { // vertical line to
  4088. path[i] = 'L';
  4089. path.splice(i + 1, 0, path[i - 2]);
  4090. }
  4091. }*/
  4092. return path.join(' ') || 'x';
  4093. },
  4094. /**
  4095. * Get or set attributes
  4096. */
  4097. attr: function (hash, val) {
  4098. var wrapper = this,
  4099. key,
  4100. value,
  4101. i,
  4102. result,
  4103. element = wrapper.element || {},
  4104. elemStyle = element.style,
  4105. nodeName = element.nodeName,
  4106. renderer = wrapper.renderer,
  4107. symbolName = wrapper.symbolName,
  4108. hasSetSymbolSize,
  4109. shadows = wrapper.shadows,
  4110. skipAttr,
  4111. attrSetters = wrapper.attrSetters,
  4112. ret = wrapper;
  4113. // single key-value pair
  4114. if (isString(hash) && defined(val)) {
  4115. key = hash;
  4116. hash = {};
  4117. hash[key] = val;
  4118. }
  4119. // used as a getter, val is undefined
  4120. if (isString(hash)) {
  4121. key = hash;
  4122. if (key === 'strokeWidth' || key === 'stroke-width') {
  4123. ret = wrapper.strokeweight;
  4124. } else {
  4125. ret = wrapper[key];
  4126. }
  4127. // setter
  4128. } else {
  4129. for (key in hash) {
  4130. value = hash[key];
  4131. skipAttr = false;
  4132. // check for a specific attribute setter
  4133. result = attrSetters[key] && attrSetters[key].call(wrapper, value, key);
  4134. if (result !== false && value !== null) { // #620
  4135. if (result !== UNDEFINED) {
  4136. value = result; // the attribute setter has returned a new value to set
  4137. }
  4138. // prepare paths
  4139. // symbols
  4140. if (symbolName && /^(x|y|r|start|end|width|height|innerR|anchorX|anchorY)/.test(key)) {
  4141. // if one of the symbol size affecting parameters are changed,
  4142. // check all the others only once for each call to an element's
  4143. // .attr() method
  4144. if (!hasSetSymbolSize) {
  4145. wrapper.symbolAttr(hash);
  4146. hasSetSymbolSize = true;
  4147. }
  4148. skipAttr = true;
  4149. } else if (key === 'd') {
  4150. value = value || [];
  4151. wrapper.d = value.join(' '); // used in getter for animation
  4152. element.path = value = wrapper.pathToVML(value);
  4153. // update shadows
  4154. if (shadows) {
  4155. i = shadows.length;
  4156. while (i--) {
  4157. shadows[i].path = shadows[i].cutOff ? this.cutOffPath(value, shadows[i].cutOff) : value;
  4158. }
  4159. }
  4160. skipAttr = true;
  4161. // handle visibility
  4162. } else if (key === 'visibility') {
  4163. // let the shadow follow the main element
  4164. if (shadows) {
  4165. i = shadows.length;
  4166. while (i--) {
  4167. shadows[i].style[key] = value;
  4168. }
  4169. }
  4170. // Instead of toggling the visibility CSS property, move the div out of the viewport.
  4171. // This works around #61 and #586
  4172. if (nodeName === 'DIV') {
  4173. value = value === HIDDEN ? '-999em' : 0;
  4174. // In order to redraw, IE7 needs the div to be visible when tucked away
  4175. // outside the viewport. So the visibility is actually opposite of
  4176. // the expected value. This applies to the tooltip only.
  4177. if (!docMode8) {
  4178. elemStyle[key] = value ? VISIBLE : HIDDEN;
  4179. }
  4180. key = 'top';
  4181. }
  4182. elemStyle[key] = value;
  4183. skipAttr = true;
  4184. // directly mapped to css
  4185. } else if (key === 'zIndex') {
  4186. if (value) {
  4187. elemStyle[key] = value;
  4188. }
  4189. skipAttr = true;
  4190. // x, y, width, height
  4191. } else if (inArray(key, ['x', 'y', 'width', 'height']) !== -1) {
  4192. wrapper[key] = value; // used in getter
  4193. if (key === 'x' || key === 'y') {
  4194. key = {x: 'left', y: 'top'}[key];
  4195. } else {
  4196. value = mathMax(0, value); // don't set width or height below zero (#311)
  4197. }
  4198. // clipping rectangle special
  4199. if (wrapper.updateClipping) {
  4200. wrapper[key] = value; // the key is now 'left' or 'top' for 'x' and 'y'
  4201. wrapper.updateClipping();
  4202. } else {
  4203. // normal
  4204. elemStyle[key] = value;
  4205. }
  4206. skipAttr = true;
  4207. // class name
  4208. } else if (key === 'class' && nodeName === 'DIV') {
  4209. // IE8 Standards mode has problems retrieving the className
  4210. element.className = value;
  4211. // stroke
  4212. } else if (key === 'stroke') {
  4213. value = renderer.color(value, element, key);
  4214. key = 'strokecolor';
  4215. // stroke width
  4216. } else if (key === 'stroke-width' || key === 'strokeWidth') {
  4217. element.stroked = value ? true : false;
  4218. key = 'strokeweight';
  4219. wrapper[key] = value; // used in getter, issue #113
  4220. if (isNumber(value)) {
  4221. value += PX;
  4222. }
  4223. // dashStyle
  4224. } else if (key === 'dashstyle') {
  4225. var strokeElem = element.getElementsByTagName('stroke')[0] ||
  4226. createElement(renderer.prepVML(['<stroke/>']), null, null, element);
  4227. strokeElem[key] = value || 'solid';
  4228. wrapper.dashstyle = value; /* because changing stroke-width will change the dash length
  4229. and cause an epileptic effect */
  4230. skipAttr = true;
  4231. // fill
  4232. } else if (key === 'fill') {
  4233. if (nodeName === 'SPAN') { // text color
  4234. elemStyle.color = value;
  4235. } else if (nodeName !== 'IMG') { // #1336
  4236. element.filled = value !== NONE ? true : false;
  4237. value = renderer.color(value, element, key, wrapper);
  4238. key = 'fillcolor';
  4239. }
  4240. // opacity: don't bother - animation is too slow and filters introduce artifacts
  4241. } else if (key === 'opacity') {
  4242. /*css(element, {
  4243. opacity: value
  4244. });*/
  4245. skipAttr = true;
  4246. // rotation on VML elements
  4247. } else if (nodeName === 'shape' && key === 'rotation') {
  4248. wrapper[key] = element.style[key] = value; // style is for #1873
  4249. // Correction for the 1x1 size of the shape container. Used in gauge needles.
  4250. element.style.left = -mathRound(mathSin(value * deg2rad) + 1) + PX;
  4251. element.style.top = mathRound(mathCos(value * deg2rad)) + PX;
  4252. // translation for animation
  4253. } else if (key === 'translateX' || key === 'translateY' || key === 'rotation') {
  4254. wrapper[key] = value;
  4255. wrapper.updateTransform();
  4256. skipAttr = true;
  4257. // text for rotated and non-rotated elements
  4258. } else if (key === 'text') {
  4259. this.bBox = null;
  4260. element.innerHTML = value;
  4261. skipAttr = true;
  4262. }
  4263. if (!skipAttr) {
  4264. if (docMode8) { // IE8 setAttribute bug
  4265. element[key] = value;
  4266. } else {
  4267. attr(element, key, value);
  4268. }
  4269. }
  4270. }
  4271. }
  4272. }
  4273. return ret;
  4274. },
  4275. /**
  4276. * Set the element's clipping to a predefined rectangle
  4277. *
  4278. * @param {String} id The id of the clip rectangle
  4279. */
  4280. clip: function (clipRect) {
  4281. var wrapper = this,
  4282. clipMembers,
  4283. cssRet;
  4284. if (clipRect) {
  4285. clipMembers = clipRect.members;
  4286. erase(clipMembers, wrapper); // Ensure unique list of elements (#1258)
  4287. clipMembers.push(wrapper);
  4288. wrapper.destroyClip = function () {
  4289. erase(clipMembers, wrapper);
  4290. };
  4291. cssRet = clipRect.getCSS(wrapper);
  4292. } else {
  4293. if (wrapper.destroyClip) {
  4294. wrapper.destroyClip();
  4295. }
  4296. cssRet = {clip: docMode8 ? 'inherit' : 'rect(auto)'}; // #1214
  4297. }
  4298. return wrapper.css(cssRet);
  4299. },
  4300. /**
  4301. * Set styles for the element
  4302. * @param {Object} styles
  4303. */
  4304. css: SVGElement.prototype.htmlCss,
  4305. /**
  4306. * Removes a child either by removeChild or move to garbageBin.
  4307. * Issue 490; in VML removeChild results in Orphaned nodes according to sIEve, discardElement does not.
  4308. */
  4309. safeRemoveChild: function (element) {
  4310. // discardElement will detach the node from its parent before attaching it
  4311. // to the garbage bin. Therefore it is important that the node is attached and have parent.
  4312. if (element.parentNode) {
  4313. discardElement(element);
  4314. }
  4315. },
  4316. /**
  4317. * Extend element.destroy by removing it from the clip members array
  4318. */
  4319. destroy: function () {
  4320. if (this.destroyClip) {
  4321. this.destroyClip();
  4322. }
  4323. return SVGElement.prototype.destroy.apply(this);
  4324. },
  4325. /**
  4326. * Add an event listener. VML override for normalizing event parameters.
  4327. * @param {String} eventType
  4328. * @param {Function} handler
  4329. */
  4330. on: function (eventType, handler) {
  4331. // simplest possible event model for internal use
  4332. this.element['on' + eventType] = function () {
  4333. var evt = win.event;
  4334. evt.target = evt.srcElement;
  4335. handler(evt);
  4336. };
  4337. return this;
  4338. },
  4339. /**
  4340. * In stacked columns, cut off the shadows so that they don't overlap
  4341. */
  4342. cutOffPath: function (path, length) {
  4343. var len;
  4344. path = path.split(/[ ,]/);
  4345. len = path.length;
  4346. if (len === 9 || len === 11) {
  4347. path[len - 4] = path[len - 2] = pInt(path[len - 2]) - 10 * length;
  4348. }
  4349. return path.join(' ');
  4350. },
  4351. /**
  4352. * Apply a drop shadow by copying elements and giving them different strokes
  4353. * @param {Boolean|Object} shadowOptions
  4354. */
  4355. shadow: function (shadowOptions, group, cutOff) {
  4356. var shadows = [],
  4357. i,
  4358. element = this.element,
  4359. renderer = this.renderer,
  4360. shadow,
  4361. elemStyle = element.style,
  4362. markup,
  4363. path = element.path,
  4364. strokeWidth,
  4365. modifiedPath,
  4366. shadowWidth,
  4367. shadowElementOpacity;
  4368. // some times empty paths are not strings
  4369. if (path && typeof path.value !== 'string') {
  4370. path = 'x';
  4371. }
  4372. modifiedPath = path;
  4373. if (shadowOptions) {
  4374. shadowWidth = pick(shadowOptions.width, 3);
  4375. shadowElementOpacity = (shadowOptions.opacity || 0.15) / shadowWidth;
  4376. for (i = 1; i <= 3; i++) {
  4377. strokeWidth = (shadowWidth * 2) + 1 - (2 * i);
  4378. // Cut off shadows for stacked column items
  4379. if (cutOff) {
  4380. modifiedPath = this.cutOffPath(path.value, strokeWidth + 0.5);
  4381. }
  4382. markup = ['<shape isShadow="true" strokeweight="', strokeWidth,
  4383. '" filled="false" path="', modifiedPath,
  4384. '" coordsize="10 10" style="', element.style.cssText, '" />'];
  4385. shadow = createElement(renderer.prepVML(markup),
  4386. null, {
  4387. left: pInt(elemStyle.left) + pick(shadowOptions.offsetX, 1),
  4388. top: pInt(elemStyle.top) + pick(shadowOptions.offsetY, 1)
  4389. }
  4390. );
  4391. if (cutOff) {
  4392. shadow.cutOff = strokeWidth + 1;
  4393. }
  4394. // apply the opacity
  4395. markup = ['<stroke color="', shadowOptions.color || 'black', '" opacity="', shadowElementOpacity * i, '"/>'];
  4396. createElement(renderer.prepVML(markup), null, null, shadow);
  4397. // insert it
  4398. if (group) {
  4399. group.element.appendChild(shadow);
  4400. } else {
  4401. element.parentNode.insertBefore(shadow, element);
  4402. }
  4403. // record it
  4404. shadows.push(shadow);
  4405. }
  4406. this.shadows = shadows;
  4407. }
  4408. return this;
  4409. }
  4410. };
  4411. VMLElement = extendClass(SVGElement, VMLElement);
  4412. /**
  4413. * The VML renderer
  4414. */
  4415. var VMLRendererExtension = { // inherit SVGRenderer
  4416. Element: VMLElement,
  4417. isIE8: userAgent.indexOf('MSIE 8.0') > -1,
  4418. /**
  4419. * Initialize the VMLRenderer
  4420. * @param {Object} container
  4421. * @param {Number} width
  4422. * @param {Number} height
  4423. */
  4424. init: function (container, width, height) {
  4425. var renderer = this,
  4426. boxWrapper,
  4427. box;
  4428. renderer.alignedObjects = [];
  4429. boxWrapper = renderer.createElement(DIV);
  4430. box = boxWrapper.element;
  4431. box.style.position = RELATIVE; // for freeform drawing using renderer directly
  4432. container.appendChild(boxWrapper.element);
  4433. // generate the containing box
  4434. renderer.isVML = true;
  4435. renderer.box = box;
  4436. renderer.boxWrapper = boxWrapper;
  4437. renderer.setSize(width, height, false);
  4438. // The only way to make IE6 and IE7 print is to use a global namespace. However,
  4439. // with IE8 the only way to make the dynamic shapes visible in screen and print mode
  4440. // seems to be to add the xmlns attribute and the behaviour style inline.
  4441. if (!doc.namespaces.hcv) {
  4442. doc.namespaces.add('hcv', 'urn:schemas-microsoft-com:vml');
  4443. // Setup default CSS (#2153)
  4444. (doc.styleSheets.length ? doc.styleSheets[0] : doc.createStyleSheet()).cssText +=
  4445. 'hcv\\:fill, hcv\\:path, hcv\\:shape, hcv\\:stroke' +
  4446. '{ behavior:url(#default#VML); display: inline-block; } ';
  4447. }
  4448. },
  4449. /**
  4450. * Detect whether the renderer is hidden. This happens when one of the parent elements
  4451. * has display: none
  4452. */
  4453. isHidden: function () {
  4454. return !this.box.offsetWidth;
  4455. },
  4456. /**
  4457. * Define a clipping rectangle. In VML it is accomplished by storing the values
  4458. * for setting the CSS style to all associated members.
  4459. *
  4460. * @param {Number} x
  4461. * @param {Number} y
  4462. * @param {Number} width
  4463. * @param {Number} height
  4464. */
  4465. clipRect: function (x, y, width, height) {
  4466. // create a dummy element
  4467. var clipRect = this.createElement(),
  4468. isObj = isObject(x);
  4469. // mimic a rectangle with its style object for automatic updating in attr
  4470. return extend(clipRect, {
  4471. members: [],
  4472. left: (isObj ? x.x : x) + 1,
  4473. top: (isObj ? x.y : y) + 1,
  4474. width: (isObj ? x.width : width) - 1,
  4475. height: (isObj ? x.height : height) - 1,
  4476. getCSS: function (wrapper) {
  4477. var element = wrapper.element,
  4478. nodeName = element.nodeName,
  4479. isShape = nodeName === 'shape',
  4480. inverted = wrapper.inverted,
  4481. rect = this,
  4482. top = rect.top - (isShape ? element.offsetTop : 0),
  4483. left = rect.left,
  4484. right = left + rect.width,
  4485. bottom = top + rect.height,
  4486. ret = {
  4487. clip: 'rect(' +
  4488. mathRound(inverted ? left : top) + 'px,' +
  4489. mathRound(inverted ? bottom : right) + 'px,' +
  4490. mathRound(inverted ? right : bottom) + 'px,' +
  4491. mathRound(inverted ? top : left) + 'px)'
  4492. };
  4493. // issue 74 workaround
  4494. if (!inverted && docMode8 && nodeName === 'DIV') {
  4495. extend(ret, {
  4496. width: right + PX,
  4497. height: bottom + PX
  4498. });
  4499. }
  4500. return ret;
  4501. },
  4502. // used in attr and animation to update the clipping of all members
  4503. updateClipping: function () {
  4504. each(clipRect.members, function (member) {
  4505. member.css(clipRect.getCSS(member));
  4506. });
  4507. }
  4508. });
  4509. },
  4510. /**
  4511. * Take a color and return it if it's a string, make it a gradient if it's a
  4512. * gradient configuration object, and apply opacity.
  4513. *
  4514. * @param {Object} color The color or config object
  4515. */
  4516. color: function (color, elem, prop, wrapper) {
  4517. var renderer = this,
  4518. colorObject,
  4519. regexRgba = /^rgba/,
  4520. markup,
  4521. fillType,
  4522. ret = NONE;
  4523. // Check for linear or radial gradient
  4524. if (color && color.linearGradient) {
  4525. fillType = 'gradient';
  4526. } else if (color && color.radialGradient) {
  4527. fillType = 'pattern';
  4528. }
  4529. if (fillType) {
  4530. var stopColor,
  4531. stopOpacity,
  4532. gradient = color.linearGradient || color.radialGradient,
  4533. x1,
  4534. y1,
  4535. x2,
  4536. y2,
  4537. opacity1,
  4538. opacity2,
  4539. color1,
  4540. color2,
  4541. fillAttr = '',
  4542. stops = color.stops,
  4543. firstStop,
  4544. lastStop,
  4545. colors = [],
  4546. addFillNode = function () {
  4547. // Add the fill subnode. When colors attribute is used, the meanings of opacity and o:opacity2
  4548. // are reversed.
  4549. markup = ['<fill colors="' + colors.join(',') + '" opacity="', opacity2, '" o:opacity2="', opacity1,
  4550. '" type="', fillType, '" ', fillAttr, 'focus="100%" method="any" />'];
  4551. createElement(renderer.prepVML(markup), null, null, elem);
  4552. };
  4553. // Extend from 0 to 1
  4554. firstStop = stops[0];
  4555. lastStop = stops[stops.length - 1];
  4556. if (firstStop[0] > 0) {
  4557. stops.unshift([
  4558. 0,
  4559. firstStop[1]
  4560. ]);
  4561. }
  4562. if (lastStop[0] < 1) {
  4563. stops.push([
  4564. 1,
  4565. lastStop[1]
  4566. ]);
  4567. }
  4568. // Compute the stops
  4569. each(stops, function (stop, i) {
  4570. if (regexRgba.test(stop[1])) {
  4571. colorObject = Color(stop[1]);
  4572. stopColor = colorObject.get('rgb');
  4573. stopOpacity = colorObject.get('a');
  4574. } else {
  4575. stopColor = stop[1];
  4576. stopOpacity = 1;
  4577. }
  4578. // Build the color attribute
  4579. colors.push((stop[0] * 100) + '% ' + stopColor);
  4580. // Only start and end opacities are allowed, so we use the first and the last
  4581. if (!i) {
  4582. opacity1 = stopOpacity;
  4583. color2 = stopColor;
  4584. } else {
  4585. opacity2 = stopOpacity;
  4586. color1 = stopColor;
  4587. }
  4588. });
  4589. // Apply the gradient to fills only.
  4590. if (prop === 'fill') {
  4591. // Handle linear gradient angle
  4592. if (fillType === 'gradient') {
  4593. x1 = gradient.x1 || gradient[0] || 0;
  4594. y1 = gradient.y1 || gradient[1] || 0;
  4595. x2 = gradient.x2 || gradient[2] || 0;
  4596. y2 = gradient.y2 || gradient[3] || 0;
  4597. fillAttr = 'angle="' + (90 - math.atan(
  4598. (y2 - y1) / // y vector
  4599. (x2 - x1) // x vector
  4600. ) * 180 / mathPI) + '"';
  4601. addFillNode();
  4602. // Radial (circular) gradient
  4603. } else {
  4604. var r = gradient.r,
  4605. sizex = r * 2,
  4606. sizey = r * 2,
  4607. cx = gradient.cx,
  4608. cy = gradient.cy,
  4609. radialReference = elem.radialReference,
  4610. bBox,
  4611. applyRadialGradient = function () {
  4612. if (radialReference) {
  4613. bBox = wrapper.getBBox();
  4614. cx += (radialReference[0] - bBox.x) / bBox.width - 0.5;
  4615. cy += (radialReference[1] - bBox.y) / bBox.height - 0.5;
  4616. sizex *= radialReference[2] / bBox.width;
  4617. sizey *= radialReference[2] / bBox.height;
  4618. }
  4619. fillAttr = 'src="' + defaultOptions.global.VMLRadialGradientURL + '" ' +
  4620. 'size="' + sizex + ',' + sizey + '" ' +
  4621. 'origin="0.5,0.5" ' +
  4622. 'position="' + cx + ',' + cy + '" ' +
  4623. 'color2="' + color2 + '" ';
  4624. addFillNode();
  4625. };
  4626. // Apply radial gradient
  4627. if (wrapper.added) {
  4628. applyRadialGradient();
  4629. } else {
  4630. // We need to know the bounding box to get the size and position right
  4631. addEvent(wrapper, 'add', applyRadialGradient);
  4632. }
  4633. // The fill element's color attribute is broken in IE8 standards mode, so we
  4634. // need to set the parent shape's fillcolor attribute instead.
  4635. ret = color1;
  4636. }
  4637. // Gradients are not supported for VML stroke, return the first color. #722.
  4638. } else {
  4639. ret = stopColor;
  4640. }
  4641. // if the color is an rgba color, split it and add a fill node
  4642. // to hold the opacity component
  4643. } else if (regexRgba.test(color) && elem.tagName !== 'IMG') {
  4644. colorObject = Color(color);
  4645. markup = ['<', prop, ' opacity="', colorObject.get('a'), '"/>'];
  4646. createElement(this.prepVML(markup), null, null, elem);
  4647. ret = colorObject.get('rgb');
  4648. } else {
  4649. var propNodes = elem.getElementsByTagName(prop); // 'stroke' or 'fill' node
  4650. if (propNodes.length) {
  4651. propNodes[0].opacity = 1;
  4652. propNodes[0].type = 'solid';
  4653. }
  4654. ret = color;
  4655. }
  4656. return ret;
  4657. },
  4658. /**
  4659. * Take a VML string and prepare it for either IE8 or IE6/IE7.
  4660. * @param {Array} markup A string array of the VML markup to prepare
  4661. */
  4662. prepVML: function (markup) {
  4663. var vmlStyle = 'display:inline-block;behavior:url(#default#VML);',
  4664. isIE8 = this.isIE8;
  4665. markup = markup.join('');
  4666. if (isIE8) { // add xmlns and style inline
  4667. markup = markup.replace('/>', ' xmlns="urn:schemas-microsoft-com:vml" />');
  4668. if (markup.indexOf('style="') === -1) {
  4669. markup = markup.replace('/>', ' style="' + vmlStyle + '" />');
  4670. } else {
  4671. markup = markup.replace('style="', 'style="' + vmlStyle);
  4672. }
  4673. } else { // add namespace
  4674. markup = markup.replace('<', '<hcv:');
  4675. }
  4676. return markup;
  4677. },
  4678. /**
  4679. * Create rotated and aligned text
  4680. * @param {String} str
  4681. * @param {Number} x
  4682. * @param {Number} y
  4683. */
  4684. text: SVGRenderer.prototype.html,
  4685. /**
  4686. * Create and return a path element
  4687. * @param {Array} path
  4688. */
  4689. path: function (path) {
  4690. var attr = {
  4691. // subpixel precision down to 0.1 (width and height = 1px)
  4692. coordsize: '10 10'
  4693. };
  4694. if (isArray(path)) {
  4695. attr.d = path;
  4696. } else if (isObject(path)) { // attributes
  4697. extend(attr, path);
  4698. }
  4699. // create the shape
  4700. return this.createElement('shape').attr(attr);
  4701. },
  4702. /**
  4703. * Create and return a circle element. In VML circles are implemented as
  4704. * shapes, which is faster than v:oval
  4705. * @param {Number} x
  4706. * @param {Number} y
  4707. * @param {Number} r
  4708. */
  4709. circle: function (x, y, r) {
  4710. var circle = this.symbol('circle');
  4711. if (isObject(x)) {
  4712. r = x.r;
  4713. y = x.y;
  4714. x = x.x;
  4715. }
  4716. circle.isCircle = true; // Causes x and y to mean center (#1682)
  4717. circle.r = r;
  4718. return circle.attr({x: x, y: y});
  4719. },
  4720. /**
  4721. * Create a group using an outer div and an inner v:group to allow rotating
  4722. * and flipping. A simple v:group would have problems with positioning
  4723. * child HTML elements and CSS clip.
  4724. *
  4725. * @param {String} name The name of the group
  4726. */
  4727. g: function (name) {
  4728. var wrapper,
  4729. attribs;
  4730. // set the class name
  4731. if (name) {
  4732. attribs = {'className': PREFIX + name, 'class': PREFIX + name};
  4733. }
  4734. // the div to hold HTML and clipping
  4735. wrapper = this.createElement(DIV).attr(attribs);
  4736. return wrapper;
  4737. },
  4738. /**
  4739. * VML override to create a regular HTML image
  4740. * @param {String} src
  4741. * @param {Number} x
  4742. * @param {Number} y
  4743. * @param {Number} width
  4744. * @param {Number} height
  4745. */
  4746. image: function (src, x, y, width, height) {
  4747. var obj = this.createElement('img')
  4748. .attr({src: src});
  4749. if (arguments.length > 1) {
  4750. obj.attr({
  4751. x: x,
  4752. y: y,
  4753. width: width,
  4754. height: height
  4755. });
  4756. }
  4757. return obj;
  4758. },
  4759. /**
  4760. * VML uses a shape for rect to overcome bugs and rotation problems
  4761. */
  4762. rect: function (x, y, width, height, r, strokeWidth) {
  4763. var wrapper = this.symbol('rect');
  4764. wrapper.r = isObject(x) ? x.r : r;
  4765. //return wrapper.attr(wrapper.crisp(strokeWidth, x, y, mathMax(width, 0), mathMax(height, 0)));
  4766. return wrapper.attr(
  4767. isObject(x) ?
  4768. x :
  4769. // do not crispify when an object is passed in (as in column charts)
  4770. wrapper.crisp(strokeWidth, x, y, mathMax(width, 0), mathMax(height, 0))
  4771. );
  4772. },
  4773. /**
  4774. * In the VML renderer, each child of an inverted div (group) is inverted
  4775. * @param {Object} element
  4776. * @param {Object} parentNode
  4777. */
  4778. invertChild: function (element, parentNode) {
  4779. var parentStyle = parentNode.style;
  4780. css(element, {
  4781. flip: 'x',
  4782. left: pInt(parentStyle.width) - 1,
  4783. top: pInt(parentStyle.height) - 1,
  4784. rotation: -90
  4785. });
  4786. },
  4787. /**
  4788. * Symbol definitions that override the parent SVG renderer's symbols
  4789. *
  4790. */
  4791. symbols: {
  4792. // VML specific arc function
  4793. arc: function (x, y, w, h, options) {
  4794. var start = options.start,
  4795. end = options.end,
  4796. radius = options.r || w || h,
  4797. innerRadius = options.innerR,
  4798. cosStart = mathCos(start),
  4799. sinStart = mathSin(start),
  4800. cosEnd = mathCos(end),
  4801. sinEnd = mathSin(end),
  4802. ret;
  4803. if (end - start === 0) { // no angle, don't show it.
  4804. return ['x'];
  4805. }
  4806. ret = [
  4807. 'wa', // clockwise arc to
  4808. x - radius, // left
  4809. y - radius, // top
  4810. x + radius, // right
  4811. y + radius, // bottom
  4812. x + radius * cosStart, // start x
  4813. y + radius * sinStart, // start y
  4814. x + radius * cosEnd, // end x
  4815. y + radius * sinEnd // end y
  4816. ];
  4817. if (options.open && !innerRadius) {
  4818. ret.push(
  4819. 'e',
  4820. M,
  4821. x,// - innerRadius,
  4822. y// - innerRadius
  4823. );
  4824. }
  4825. ret.push(
  4826. 'at', // anti clockwise arc to
  4827. x - innerRadius, // left
  4828. y - innerRadius, // top
  4829. x + innerRadius, // right
  4830. y + innerRadius, // bottom
  4831. x + innerRadius * cosEnd, // start x
  4832. y + innerRadius * sinEnd, // start y
  4833. x + innerRadius * cosStart, // end x
  4834. y + innerRadius * sinStart, // end y
  4835. 'x', // finish path
  4836. 'e' // close
  4837. );
  4838. ret.isArc = true;
  4839. return ret;
  4840. },
  4841. // Add circle symbol path. This performs significantly faster than v:oval.
  4842. circle: function (x, y, w, h, wrapper) {
  4843. if (wrapper) {
  4844. w = h = 2 * wrapper.r;
  4845. }
  4846. // Center correction, #1682
  4847. if (wrapper && wrapper.isCircle) {
  4848. x -= w / 2;
  4849. y -= h / 2;
  4850. }
  4851. // Return the path
  4852. return [
  4853. 'wa', // clockwisearcto
  4854. x, // left
  4855. y, // top
  4856. x + w, // right
  4857. y + h, // bottom
  4858. x + w, // start x
  4859. y + h / 2, // start y
  4860. x + w, // end x
  4861. y + h / 2, // end y
  4862. //'x', // finish path
  4863. 'e' // close
  4864. ];
  4865. },
  4866. /**
  4867. * Add rectangle symbol path which eases rotation and omits arcsize problems
  4868. * compared to the built-in VML roundrect shape
  4869. *
  4870. * @param {Number} left Left position
  4871. * @param {Number} top Top position
  4872. * @param {Number} r Border radius
  4873. * @param {Object} options Width and height
  4874. */
  4875. rect: function (left, top, width, height, options) {
  4876. var right = left + width,
  4877. bottom = top + height,
  4878. ret,
  4879. r;
  4880. // No radius, return the more lightweight square
  4881. if (!defined(options) || !options.r) {
  4882. ret = SVGRenderer.prototype.symbols.square.apply(0, arguments);
  4883. // Has radius add arcs for the corners
  4884. } else {
  4885. r = mathMin(options.r, width, height);
  4886. ret = [
  4887. M,
  4888. left + r, top,
  4889. L,
  4890. right - r, top,
  4891. 'wa',
  4892. right - 2 * r, top,
  4893. right, top + 2 * r,
  4894. right - r, top,
  4895. right, top + r,
  4896. L,
  4897. right, bottom - r,
  4898. 'wa',
  4899. right - 2 * r, bottom - 2 * r,
  4900. right, bottom,
  4901. right, bottom - r,
  4902. right - r, bottom,
  4903. L,
  4904. left + r, bottom,
  4905. 'wa',
  4906. left, bottom - 2 * r,
  4907. left + 2 * r, bottom,
  4908. left + r, bottom,
  4909. left, bottom - r,
  4910. L,
  4911. left, top + r,
  4912. 'wa',
  4913. left, top,
  4914. left + 2 * r, top + 2 * r,
  4915. left, top + r,
  4916. left + r, top,
  4917. 'x',
  4918. 'e'
  4919. ];
  4920. }
  4921. return ret;
  4922. }
  4923. }
  4924. };
  4925. Highcharts.VMLRenderer = VMLRenderer = function () {
  4926. this.init.apply(this, arguments);
  4927. };
  4928. VMLRenderer.prototype = merge(SVGRenderer.prototype, VMLRendererExtension);
  4929. // general renderer
  4930. Renderer = VMLRenderer;
  4931. }
  4932. /* ****************************************************************************
  4933. * *
  4934. * END OF INTERNET EXPLORER <= 8 SPECIFIC CODE *
  4935. * *
  4936. *****************************************************************************/
  4937. /* ****************************************************************************
  4938. * *
  4939. * START OF ANDROID < 3 SPECIFIC CODE. THIS CAN BE REMOVED IF YOU'RE NOT *
  4940. * TARGETING THAT SYSTEM. *
  4941. * *
  4942. *****************************************************************************/
  4943. var CanVGRenderer,
  4944. CanVGController;
  4945. if (useCanVG) {
  4946. /**
  4947. * The CanVGRenderer is empty from start to keep the source footprint small.
  4948. * When requested, the CanVGController downloads the rest of the source packaged
  4949. * together with the canvg library.
  4950. */
  4951. Highcharts.CanVGRenderer = CanVGRenderer = function () {
  4952. // Override the global SVG namespace to fake SVG/HTML that accepts CSS
  4953. SVG_NS = 'http://www.w3.org/1999/xhtml';
  4954. };
  4955. /**
  4956. * Start with an empty symbols object. This is needed when exporting is used (exporting.src.js will add a few symbols), but
  4957. * the implementation from SvgRenderer will not be merged in until first render.
  4958. */
  4959. CanVGRenderer.prototype.symbols = {};
  4960. /**
  4961. * Handles on demand download of canvg rendering support.
  4962. */
  4963. CanVGController = (function () {
  4964. // List of renderering calls
  4965. var deferredRenderCalls = [];
  4966. /**
  4967. * When downloaded, we are ready to draw deferred charts.
  4968. */
  4969. function drawDeferred() {
  4970. var callLength = deferredRenderCalls.length,
  4971. callIndex;
  4972. // Draw all pending render calls
  4973. for (callIndex = 0; callIndex < callLength; callIndex++) {
  4974. deferredRenderCalls[callIndex]();
  4975. }
  4976. // Clear the list
  4977. deferredRenderCalls = [];
  4978. }
  4979. return {
  4980. push: function (func, scriptLocation) {
  4981. // Only get the script once
  4982. if (deferredRenderCalls.length === 0) {
  4983. getScript(scriptLocation, drawDeferred);
  4984. }
  4985. // Register render call
  4986. deferredRenderCalls.push(func);
  4987. }
  4988. };
  4989. }());
  4990. Renderer = CanVGRenderer;
  4991. } // end CanVGRenderer
  4992. /* ****************************************************************************
  4993. * *
  4994. * END OF ANDROID < 3 SPECIFIC CODE *
  4995. * *
  4996. *****************************************************************************/
  4997. /**
  4998. * The Tick class
  4999. */
  5000. function Tick(axis, pos, type, noLabel) {
  5001. this.axis = axis;
  5002. this.pos = pos;
  5003. this.type = type || '';
  5004. this.isNew = true;
  5005. if (!type && !noLabel) {
  5006. this.addLabel();
  5007. }
  5008. }
  5009. Tick.prototype = {
  5010. /**
  5011. * Write the tick label
  5012. */
  5013. addLabel: function () {
  5014. var tick = this,
  5015. axis = tick.axis,
  5016. options = axis.options,
  5017. chart = axis.chart,
  5018. horiz = axis.horiz,
  5019. categories = axis.categories,
  5020. names = axis.series[0] && axis.series[0].names,
  5021. pos = tick.pos,
  5022. labelOptions = options.labels,
  5023. str,
  5024. tickPositions = axis.tickPositions,
  5025. width = (horiz && categories &&
  5026. !labelOptions.step && !labelOptions.staggerLines &&
  5027. !labelOptions.rotation &&
  5028. chart.plotWidth / tickPositions.length) ||
  5029. (!horiz && (chart.margin[3] || chart.chartWidth * 0.33)), // #1580, #1931
  5030. isFirst = pos === tickPositions[0],
  5031. isLast = pos === tickPositions[tickPositions.length - 1],
  5032. css,
  5033. attr,
  5034. value = categories ?
  5035. pick(categories[pos], names && names[pos], pos) :
  5036. pos,
  5037. label = tick.label,
  5038. tickPositionInfo = tickPositions.info,
  5039. dateTimeLabelFormat;
  5040. // Set the datetime label format. If a higher rank is set for this position, use that. If not,
  5041. // use the general format.
  5042. if (axis.isDatetimeAxis && tickPositionInfo) {
  5043. dateTimeLabelFormat = options.dateTimeLabelFormats[tickPositionInfo.higherRanks[pos] || tickPositionInfo.unitName];
  5044. }
  5045. // set properties for access in render method
  5046. tick.isFirst = isFirst;
  5047. tick.isLast = isLast;
  5048. // get the string
  5049. str = axis.labelFormatter.call({
  5050. axis: axis,
  5051. chart: chart,
  5052. isFirst: isFirst,
  5053. isLast: isLast,
  5054. dateTimeLabelFormat: dateTimeLabelFormat,
  5055. value: axis.isLog ? correctFloat(lin2log(value)) : value
  5056. });
  5057. // prepare CSS
  5058. css = width && {width: mathMax(1, mathRound(width - 2 * (labelOptions.padding || 10))) + PX};
  5059. css = extend(css, labelOptions.style);
  5060. // first call
  5061. if (!defined(label)) {
  5062. attr = {
  5063. align: axis.labelAlign
  5064. };
  5065. if (isNumber(labelOptions.rotation)) {
  5066. attr.rotation = labelOptions.rotation;
  5067. }
  5068. if (width && labelOptions.ellipsis) {
  5069. attr._clipHeight = axis.len / tickPositions.length;
  5070. }
  5071. tick.label =
  5072. defined(str) && labelOptions.enabled ?
  5073. chart.renderer.text(
  5074. str,
  5075. 0,
  5076. 0,
  5077. labelOptions.useHTML
  5078. )
  5079. .attr(attr)
  5080. // without position absolute, IE export sometimes is wrong
  5081. .css(css)
  5082. .add(axis.labelGroup) :
  5083. null;
  5084. // update
  5085. } else if (label) {
  5086. label.attr({
  5087. text: str
  5088. })
  5089. .css(css);
  5090. }
  5091. },
  5092. /**
  5093. * Get the offset height or width of the label
  5094. */
  5095. getLabelSize: function () {
  5096. var label = this.label,
  5097. axis = this.axis;
  5098. return label ?
  5099. ((this.labelBBox = label.getBBox()))[axis.horiz ? 'height' : 'width'] :
  5100. 0;
  5101. },
  5102. /**
  5103. * Find how far the labels extend to the right and left of the tick's x position. Used for anti-collision
  5104. * detection with overflow logic.
  5105. */
  5106. getLabelSides: function () {
  5107. var bBox = this.labelBBox, // assume getLabelSize has run at this point
  5108. axis = this.axis,
  5109. options = axis.options,
  5110. labelOptions = options.labels,
  5111. width = bBox.width,
  5112. leftSide = width * {left: 0, center: 0.5, right: 1}[axis.labelAlign] - labelOptions.x;
  5113. return [-leftSide, width - leftSide];
  5114. },
  5115. /**
  5116. * Handle the label overflow by adjusting the labels to the left and right edge, or
  5117. * hide them if they collide into the neighbour label.
  5118. */
  5119. handleOverflow: function (index, xy) {
  5120. var show = true,
  5121. axis = this.axis,
  5122. chart = axis.chart,
  5123. isFirst = this.isFirst,
  5124. isLast = this.isLast,
  5125. x = xy.x,
  5126. reversed = axis.reversed,
  5127. tickPositions = axis.tickPositions;
  5128. if (isFirst || isLast) {
  5129. var sides = this.getLabelSides(),
  5130. leftSide = sides[0],
  5131. rightSide = sides[1],
  5132. plotLeft = chart.plotLeft,
  5133. plotRight = plotLeft + axis.len,
  5134. neighbour = axis.ticks[tickPositions[index + (isFirst ? 1 : -1)]],
  5135. neighbourEdge = neighbour && neighbour.label.xy && neighbour.label.xy.x + neighbour.getLabelSides()[isFirst ? 0 : 1];
  5136. if ((isFirst && !reversed) || (isLast && reversed)) {
  5137. // Is the label spilling out to the left of the plot area?
  5138. if (x + leftSide < plotLeft) {
  5139. // Align it to plot left
  5140. x = plotLeft - leftSide;
  5141. // Hide it if it now overlaps the neighbour label
  5142. if (neighbour && x + rightSide > neighbourEdge) {
  5143. show = false;
  5144. }
  5145. }
  5146. } else {
  5147. // Is the label spilling out to the right of the plot area?
  5148. if (x + rightSide > plotRight) {
  5149. // Align it to plot right
  5150. x = plotRight - rightSide;
  5151. // Hide it if it now overlaps the neighbour label
  5152. if (neighbour && x + leftSide < neighbourEdge) {
  5153. show = false;
  5154. }
  5155. }
  5156. }
  5157. // Set the modified x position of the label
  5158. xy.x = x;
  5159. }
  5160. return show;
  5161. },
  5162. /**
  5163. * Get the x and y position for ticks and labels
  5164. */
  5165. getPosition: function (horiz, pos, tickmarkOffset, old) {
  5166. var axis = this.axis,
  5167. chart = axis.chart,
  5168. cHeight = (old && chart.oldChartHeight) || chart.chartHeight;
  5169. return {
  5170. x: horiz ?
  5171. axis.translate(pos + tickmarkOffset, null, null, old) + axis.transB :
  5172. axis.left + axis.offset + (axis.opposite ? ((old && chart.oldChartWidth) || chart.chartWidth) - axis.right - axis.left : 0),
  5173. y: horiz ?
  5174. cHeight - axis.bottom + axis.offset - (axis.opposite ? axis.height : 0) :
  5175. cHeight - axis.translate(pos + tickmarkOffset, null, null, old) - axis.transB
  5176. };
  5177. },
  5178. /**
  5179. * Get the x, y position of the tick label
  5180. */
  5181. getLabelPosition: function (x, y, label, horiz, labelOptions, tickmarkOffset, index, step) {
  5182. var axis = this.axis,
  5183. transA = axis.transA,
  5184. reversed = axis.reversed,
  5185. staggerLines = axis.staggerLines,
  5186. baseline = axis.chart.renderer.fontMetrics(labelOptions.style.fontSize).b,
  5187. rotation = labelOptions.rotation;
  5188. x = x + labelOptions.x - (tickmarkOffset && horiz ?
  5189. tickmarkOffset * transA * (reversed ? -1 : 1) : 0);
  5190. y = y + labelOptions.y - (tickmarkOffset && !horiz ?
  5191. tickmarkOffset * transA * (reversed ? 1 : -1) : 0);
  5192. // Correct for rotation (#1764)
  5193. if (rotation && axis.side === 2) {
  5194. y -= baseline - baseline * mathCos(rotation * deg2rad);
  5195. }
  5196. // Vertically centered
  5197. if (!defined(labelOptions.y) && !rotation) { // #1951
  5198. y += baseline - label.getBBox().height / 2;
  5199. }
  5200. // Correct for staggered labels
  5201. if (staggerLines) {
  5202. y += (index / (step || 1) % staggerLines) * (axis.labelOffset / staggerLines);
  5203. }
  5204. return {
  5205. x: x,
  5206. y: y
  5207. };
  5208. },
  5209. /**
  5210. * Extendible method to return the path of the marker
  5211. */
  5212. getMarkPath: function (x, y, tickLength, tickWidth, horiz, renderer) {
  5213. return renderer.crispLine([
  5214. M,
  5215. x,
  5216. y,
  5217. L,
  5218. x + (horiz ? 0 : -tickLength),
  5219. y + (horiz ? tickLength : 0)
  5220. ], tickWidth);
  5221. },
  5222. /**
  5223. * Put everything in place
  5224. *
  5225. * @param index {Number}
  5226. * @param old {Boolean} Use old coordinates to prepare an animation into new position
  5227. */
  5228. render: function (index, old, opacity) {
  5229. var tick = this,
  5230. axis = tick.axis,
  5231. options = axis.options,
  5232. chart = axis.chart,
  5233. renderer = chart.renderer,
  5234. horiz = axis.horiz,
  5235. type = tick.type,
  5236. label = tick.label,
  5237. pos = tick.pos,
  5238. labelOptions = options.labels,
  5239. gridLine = tick.gridLine,
  5240. gridPrefix = type ? type + 'Grid' : 'grid',
  5241. tickPrefix = type ? type + 'Tick' : 'tick',
  5242. gridLineWidth = options[gridPrefix + 'LineWidth'],
  5243. gridLineColor = options[gridPrefix + 'LineColor'],
  5244. dashStyle = options[gridPrefix + 'LineDashStyle'],
  5245. tickLength = options[tickPrefix + 'Length'],
  5246. tickWidth = options[tickPrefix + 'Width'] || 0,
  5247. tickColor = options[tickPrefix + 'Color'],
  5248. tickPosition = options[tickPrefix + 'Position'],
  5249. gridLinePath,
  5250. mark = tick.mark,
  5251. markPath,
  5252. step = labelOptions.step,
  5253. attribs,
  5254. show = true,
  5255. tickmarkOffset = axis.tickmarkOffset,
  5256. xy = tick.getPosition(horiz, pos, tickmarkOffset, old),
  5257. x = xy.x,
  5258. y = xy.y,
  5259. reverseCrisp = ((horiz && x === axis.pos + axis.len) || (!horiz && y === axis.pos)) ? -1 : 1, // #1480, #1687
  5260. staggerLines = axis.staggerLines;
  5261. this.isActive = true;
  5262. // create the grid line
  5263. if (gridLineWidth) {
  5264. gridLinePath = axis.getPlotLinePath(pos + tickmarkOffset, gridLineWidth * reverseCrisp, old, true);
  5265. if (gridLine === UNDEFINED) {
  5266. attribs = {
  5267. stroke: gridLineColor,
  5268. 'stroke-width': gridLineWidth
  5269. };
  5270. if (dashStyle) {
  5271. attribs.dashstyle = dashStyle;
  5272. }
  5273. if (!type) {
  5274. attribs.zIndex = 1;
  5275. }
  5276. if (old) {
  5277. attribs.opacity = 0;
  5278. }
  5279. tick.gridLine = gridLine =
  5280. gridLineWidth ?
  5281. renderer.path(gridLinePath)
  5282. .attr(attribs).add(axis.gridGroup) :
  5283. null;
  5284. }
  5285. // If the parameter 'old' is set, the current call will be followed
  5286. // by another call, therefore do not do any animations this time
  5287. if (!old && gridLine && gridLinePath) {
  5288. gridLine[tick.isNew ? 'attr' : 'animate']({
  5289. d: gridLinePath,
  5290. opacity: opacity
  5291. });
  5292. }
  5293. }
  5294. // create the tick mark
  5295. if (tickWidth && tickLength) {
  5296. // negate the length
  5297. if (tickPosition === 'inside') {
  5298. tickLength = -tickLength;
  5299. }
  5300. if (axis.opposite) {
  5301. tickLength = -tickLength;
  5302. }
  5303. markPath = tick.getMarkPath(x, y, tickLength, tickWidth * reverseCrisp, horiz, renderer);
  5304. if (mark) { // updating
  5305. mark.animate({
  5306. d: markPath,
  5307. opacity: opacity
  5308. });
  5309. } else { // first time
  5310. tick.mark = renderer.path(
  5311. markPath
  5312. ).attr({
  5313. stroke: tickColor,
  5314. 'stroke-width': tickWidth,
  5315. opacity: opacity
  5316. }).add(axis.axisGroup);
  5317. }
  5318. }
  5319. // the label is created on init - now move it into place
  5320. if (label && !isNaN(x)) {
  5321. label.xy = xy = tick.getLabelPosition(x, y, label, horiz, labelOptions, tickmarkOffset, index, step);
  5322. // Apply show first and show last. If the tick is both first and last, it is
  5323. // a single centered tick, in which case we show the label anyway (#2100).
  5324. if ((tick.isFirst && !tick.isLast && !pick(options.showFirstLabel, 1)) ||
  5325. (tick.isLast && !tick.isFirst && !pick(options.showLastLabel, 1))) {
  5326. show = false;
  5327. // Handle label overflow and show or hide accordingly
  5328. } else if (!staggerLines && horiz && labelOptions.overflow === 'justify' && !tick.handleOverflow(index, xy)) {
  5329. show = false;
  5330. }
  5331. // apply step
  5332. if (step && index % step) {
  5333. // show those indices dividable by step
  5334. show = false;
  5335. }
  5336. // Set the new position, and show or hide
  5337. if (show && !isNaN(xy.y)) {
  5338. xy.opacity = opacity;
  5339. label[tick.isNew ? 'attr' : 'animate'](xy);
  5340. tick.isNew = false;
  5341. } else {
  5342. label.attr('y', -9999); // #1338
  5343. }
  5344. }
  5345. },
  5346. /**
  5347. * Destructor for the tick prototype
  5348. */
  5349. destroy: function () {
  5350. destroyObjectProperties(this, this.axis);
  5351. }
  5352. };
  5353. /**
  5354. * The object wrapper for plot lines and plot bands
  5355. * @param {Object} options
  5356. */
  5357. function PlotLineOrBand(axis, options) {
  5358. this.axis = axis;
  5359. if (options) {
  5360. this.options = options;
  5361. this.id = options.id;
  5362. }
  5363. }
  5364. PlotLineOrBand.prototype = {
  5365. /**
  5366. * Render the plot line or plot band. If it is already existing,
  5367. * move it.
  5368. */
  5369. render: function () {
  5370. var plotLine = this,
  5371. axis = plotLine.axis,
  5372. horiz = axis.horiz,
  5373. halfPointRange = (axis.pointRange || 0) / 2,
  5374. options = plotLine.options,
  5375. optionsLabel = options.label,
  5376. label = plotLine.label,
  5377. width = options.width,
  5378. to = options.to,
  5379. from = options.from,
  5380. isBand = defined(from) && defined(to),
  5381. value = options.value,
  5382. dashStyle = options.dashStyle,
  5383. svgElem = plotLine.svgElem,
  5384. path = [],
  5385. addEvent,
  5386. eventType,
  5387. xs,
  5388. ys,
  5389. x,
  5390. y,
  5391. color = options.color,
  5392. zIndex = options.zIndex,
  5393. events = options.events,
  5394. attribs,
  5395. renderer = axis.chart.renderer;
  5396. // logarithmic conversion
  5397. if (axis.isLog) {
  5398. from = log2lin(from);
  5399. to = log2lin(to);
  5400. value = log2lin(value);
  5401. }
  5402. // plot line
  5403. if (width) {
  5404. path = axis.getPlotLinePath(value, width);
  5405. attribs = {
  5406. stroke: color,
  5407. 'stroke-width': width
  5408. };
  5409. if (dashStyle) {
  5410. attribs.dashstyle = dashStyle;
  5411. }
  5412. } else if (isBand) { // plot band
  5413. // keep within plot area
  5414. from = mathMax(from, axis.min - halfPointRange);
  5415. to = mathMin(to, axis.max + halfPointRange);
  5416. path = axis.getPlotBandPath(from, to, options);
  5417. attribs = {
  5418. fill: color
  5419. };
  5420. if (options.borderWidth) {
  5421. attribs.stroke = options.borderColor;
  5422. attribs['stroke-width'] = options.borderWidth;
  5423. }
  5424. } else {
  5425. return;
  5426. }
  5427. // zIndex
  5428. if (defined(zIndex)) {
  5429. attribs.zIndex = zIndex;
  5430. }
  5431. // common for lines and bands
  5432. if (svgElem) {
  5433. if (path) {
  5434. svgElem.animate({
  5435. d: path
  5436. }, null, svgElem.onGetPath);
  5437. } else {
  5438. svgElem.hide();
  5439. svgElem.onGetPath = function () {
  5440. svgElem.show();
  5441. };
  5442. }
  5443. } else if (path && path.length) {
  5444. plotLine.svgElem = svgElem = renderer.path(path)
  5445. .attr(attribs).add();
  5446. // events
  5447. if (events) {
  5448. addEvent = function (eventType) {
  5449. svgElem.on(eventType, function (e) {
  5450. events[eventType].apply(plotLine, [e]);
  5451. });
  5452. };
  5453. for (eventType in events) {
  5454. addEvent(eventType);
  5455. }
  5456. }
  5457. }
  5458. // the plot band/line label
  5459. if (optionsLabel && defined(optionsLabel.text) && path && path.length && axis.width > 0 && axis.height > 0) {
  5460. // apply defaults
  5461. optionsLabel = merge({
  5462. align: horiz && isBand && 'center',
  5463. x: horiz ? !isBand && 4 : 10,
  5464. verticalAlign: !horiz && isBand && 'middle',
  5465. y: horiz ? isBand ? 16 : 10 : isBand ? 6 : -4,
  5466. rotation: horiz && !isBand && 90
  5467. }, optionsLabel);
  5468. // add the SVG element
  5469. if (!label) {
  5470. plotLine.label = label = renderer.text(
  5471. optionsLabel.text,
  5472. 0,
  5473. 0,
  5474. optionsLabel.useHTML
  5475. )
  5476. .attr({
  5477. align: optionsLabel.textAlign || optionsLabel.align,
  5478. rotation: optionsLabel.rotation,
  5479. zIndex: zIndex
  5480. })
  5481. .css(optionsLabel.style)
  5482. .add();
  5483. }
  5484. // get the bounding box and align the label
  5485. xs = [path[1], path[4], pick(path[6], path[1])];
  5486. ys = [path[2], path[5], pick(path[7], path[2])];
  5487. x = arrayMin(xs);
  5488. y = arrayMin(ys);
  5489. label.align(optionsLabel, false, {
  5490. x: x,
  5491. y: y,
  5492. width: arrayMax(xs) - x,
  5493. height: arrayMax(ys) - y
  5494. });
  5495. label.show();
  5496. } else if (label) { // move out of sight
  5497. label.hide();
  5498. }
  5499. // chainable
  5500. return plotLine;
  5501. },
  5502. /**
  5503. * Remove the plot line or band
  5504. */
  5505. destroy: function () {
  5506. // remove it from the lookup
  5507. erase(this.axis.plotLinesAndBands, this);
  5508. delete this.axis;
  5509. destroyObjectProperties(this);
  5510. }
  5511. };
  5512. /**
  5513. * The class for stack items
  5514. */
  5515. function StackItem(axis, options, isNegative, x, stackOption, stacking) {
  5516. var inverted = axis.chart.inverted;
  5517. this.axis = axis;
  5518. // Tells if the stack is negative
  5519. this.isNegative = isNegative;
  5520. // Save the options to be able to style the label
  5521. this.options = options;
  5522. // Save the x value to be able to position the label later
  5523. this.x = x;
  5524. // Initialize total value
  5525. this.total = null;
  5526. // This will keep each points' extremes stored by series.index
  5527. this.points = {};
  5528. // Save the stack option on the series configuration object, and whether to treat it as percent
  5529. this.stack = stackOption;
  5530. this.percent = stacking === 'percent';
  5531. // The align options and text align varies on whether the stack is negative and
  5532. // if the chart is inverted or not.
  5533. // First test the user supplied value, then use the dynamic.
  5534. this.alignOptions = {
  5535. align: options.align || (inverted ? (isNegative ? 'left' : 'right') : 'center'),
  5536. verticalAlign: options.verticalAlign || (inverted ? 'middle' : (isNegative ? 'bottom' : 'top')),
  5537. y: pick(options.y, inverted ? 4 : (isNegative ? 14 : -6)),
  5538. x: pick(options.x, inverted ? (isNegative ? -6 : 6) : 0)
  5539. };
  5540. this.textAlign = options.textAlign || (inverted ? (isNegative ? 'right' : 'left') : 'center');
  5541. }
  5542. StackItem.prototype = {
  5543. destroy: function () {
  5544. destroyObjectProperties(this, this.axis);
  5545. },
  5546. /**
  5547. * Renders the stack total label and adds it to the stack label group.
  5548. */
  5549. render: function (group) {
  5550. var options = this.options,
  5551. formatOption = options.format,
  5552. str = formatOption ?
  5553. format(formatOption, this) :
  5554. options.formatter.call(this); // format the text in the label
  5555. // Change the text to reflect the new total and set visibility to hidden in case the serie is hidden
  5556. if (this.label) {
  5557. this.label.attr({text: str, visibility: HIDDEN});
  5558. // Create new label
  5559. } else {
  5560. this.label =
  5561. this.axis.chart.renderer.text(str, 0, 0, options.useHTML) // dummy positions, actual position updated with setOffset method in columnseries
  5562. .css(options.style) // apply style
  5563. .attr({
  5564. align: this.textAlign, // fix the text-anchor
  5565. rotation: options.rotation, // rotation
  5566. visibility: HIDDEN // hidden until setOffset is called
  5567. })
  5568. .add(group); // add to the labels-group
  5569. }
  5570. },
  5571. /**
  5572. * Sets the offset that the stack has from the x value and repositions the label.
  5573. */
  5574. setOffset: function (xOffset, xWidth) {
  5575. var stackItem = this,
  5576. axis = stackItem.axis,
  5577. chart = axis.chart,
  5578. inverted = chart.inverted,
  5579. neg = this.isNegative, // special treatment is needed for negative stacks
  5580. y = axis.translate(this.percent ? 100 : this.total, 0, 0, 0, 1), // stack value translated mapped to chart coordinates
  5581. yZero = axis.translate(0), // stack origin
  5582. h = mathAbs(y - yZero), // stack height
  5583. x = chart.xAxis[0].translate(this.x) + xOffset, // stack x position
  5584. plotHeight = chart.plotHeight,
  5585. stackBox = { // this is the box for the complete stack
  5586. x: inverted ? (neg ? y : y - h) : x,
  5587. y: inverted ? plotHeight - x - xWidth : (neg ? (plotHeight - y - h) : plotHeight - y),
  5588. width: inverted ? h : xWidth,
  5589. height: inverted ? xWidth : h
  5590. },
  5591. label = this.label,
  5592. alignAttr;
  5593. if (label) {
  5594. label.align(this.alignOptions, null, stackBox); // align the label to the box
  5595. // Set visibility (#678)
  5596. alignAttr = label.alignAttr;
  5597. label.attr({
  5598. visibility: this.options.crop === false || chart.isInsidePlot(alignAttr.x, alignAttr.y) ?
  5599. (hasSVG ? 'inherit' : VISIBLE) :
  5600. HIDDEN
  5601. });
  5602. }
  5603. }
  5604. };
  5605. /**
  5606. * Create a new axis object
  5607. * @param {Object} chart
  5608. * @param {Object} options
  5609. */
  5610. function Axis() {
  5611. this.init.apply(this, arguments);
  5612. }
  5613. Axis.prototype = {
  5614. /**
  5615. * Default options for the X axis - the Y axis has extended defaults
  5616. */
  5617. defaultOptions: {
  5618. // allowDecimals: null,
  5619. // alternateGridColor: null,
  5620. // categories: [],
  5621. dateTimeLabelFormats: {
  5622. millisecond: '%H:%M:%S.%L',
  5623. second: '%H:%M:%S',
  5624. minute: '%H:%M',
  5625. hour: '%H:%M',
  5626. day: '%e. %b',
  5627. week: '%e. %b',
  5628. month: '%b \'%y',
  5629. year: '%Y'
  5630. },
  5631. endOnTick: false,
  5632. gridLineColor: '#C0C0C0',
  5633. // gridLineDashStyle: 'solid',
  5634. // gridLineWidth: 0,
  5635. // reversed: false,
  5636. labels: defaultLabelOptions,
  5637. // { step: null },
  5638. lineColor: '#C0D0E0',
  5639. lineWidth: 1,
  5640. //linkedTo: null,
  5641. //max: undefined,
  5642. //min: undefined,
  5643. minPadding: 0.01,
  5644. maxPadding: 0.01,
  5645. //minRange: null,
  5646. minorGridLineColor: '#E0E0E0',
  5647. // minorGridLineDashStyle: null,
  5648. minorGridLineWidth: 1,
  5649. minorTickColor: '#A0A0A0',
  5650. //minorTickInterval: null,
  5651. minorTickLength: 2,
  5652. minorTickPosition: 'outside', // inside or outside
  5653. //minorTickWidth: 0,
  5654. //opposite: false,
  5655. //offset: 0,
  5656. //plotBands: [{
  5657. // events: {},
  5658. // zIndex: 1,
  5659. // labels: { align, x, verticalAlign, y, style, rotation, textAlign }
  5660. //}],
  5661. //plotLines: [{
  5662. // events: {}
  5663. // dashStyle: {}
  5664. // zIndex:
  5665. // labels: { align, x, verticalAlign, y, style, rotation, textAlign }
  5666. //}],
  5667. //reversed: false,
  5668. // showFirstLabel: true,
  5669. // showLastLabel: true,
  5670. startOfWeek: 1,
  5671. startOnTick: false,
  5672. tickColor: '#C0D0E0',
  5673. //tickInterval: null,
  5674. tickLength: 5,
  5675. tickmarkPlacement: 'between', // on or between
  5676. tickPixelInterval: 100,
  5677. tickPosition: 'outside',
  5678. tickWidth: 1,
  5679. title: {
  5680. //text: null,
  5681. align: 'middle', // low, middle or high
  5682. //margin: 0 for horizontal, 10 for vertical axes,
  5683. //rotation: 0,
  5684. //side: 'outside',
  5685. style: {
  5686. color: '#4d759e',
  5687. //font: defaultFont.replace('normal', 'bold')
  5688. fontWeight: 'bold'
  5689. }
  5690. //x: 0,
  5691. //y: 0
  5692. },
  5693. type: 'linear' // linear, logarithmic or datetime
  5694. },
  5695. /**
  5696. * This options set extends the defaultOptions for Y axes
  5697. */
  5698. defaultYAxisOptions: {
  5699. endOnTick: true,
  5700. gridLineWidth: 1,
  5701. tickPixelInterval: 72,
  5702. showLastLabel: true,
  5703. labels: {
  5704. x: -8,
  5705. y: 3
  5706. },
  5707. lineWidth: 0,
  5708. maxPadding: 0.05,
  5709. minPadding: 0.05,
  5710. startOnTick: true,
  5711. tickWidth: 0,
  5712. title: {
  5713. rotation: 270,
  5714. text: 'Values'
  5715. },
  5716. stackLabels: {
  5717. enabled: false,
  5718. //align: dynamic,
  5719. //y: dynamic,
  5720. //x: dynamic,
  5721. //verticalAlign: dynamic,
  5722. //textAlign: dynamic,
  5723. //rotation: 0,
  5724. formatter: function () {
  5725. return numberFormat(this.total, -1);
  5726. },
  5727. style: defaultLabelOptions.style
  5728. }
  5729. },
  5730. /**
  5731. * These options extend the defaultOptions for left axes
  5732. */
  5733. defaultLeftAxisOptions: {
  5734. labels: {
  5735. x: -8,
  5736. y: null
  5737. },
  5738. title: {
  5739. rotation: 270
  5740. }
  5741. },
  5742. /**
  5743. * These options extend the defaultOptions for right axes
  5744. */
  5745. defaultRightAxisOptions: {
  5746. labels: {
  5747. x: 8,
  5748. y: null
  5749. },
  5750. title: {
  5751. rotation: 90
  5752. }
  5753. },
  5754. /**
  5755. * These options extend the defaultOptions for bottom axes
  5756. */
  5757. defaultBottomAxisOptions: {
  5758. labels: {
  5759. x: 0,
  5760. y: 14
  5761. // overflow: undefined,
  5762. // staggerLines: null
  5763. },
  5764. title: {
  5765. rotation: 0
  5766. }
  5767. },
  5768. /**
  5769. * These options extend the defaultOptions for left axes
  5770. */
  5771. defaultTopAxisOptions: {
  5772. labels: {
  5773. x: 0,
  5774. y: -5
  5775. // overflow: undefined
  5776. // staggerLines: null
  5777. },
  5778. title: {
  5779. rotation: 0
  5780. }
  5781. },
  5782. /**
  5783. * Initialize the axis
  5784. */
  5785. init: function (chart, userOptions) {
  5786. var isXAxis = userOptions.isX,
  5787. axis = this;
  5788. // Flag, is the axis horizontal
  5789. axis.horiz = chart.inverted ? !isXAxis : isXAxis;
  5790. // Flag, isXAxis
  5791. axis.isXAxis = isXAxis;
  5792. axis.xOrY = isXAxis ? 'x' : 'y';
  5793. axis.opposite = userOptions.opposite; // needed in setOptions
  5794. axis.side = axis.horiz ?
  5795. (axis.opposite ? 0 : 2) : // top : bottom
  5796. (axis.opposite ? 1 : 3); // right : left
  5797. axis.setOptions(userOptions);
  5798. var options = this.options,
  5799. type = options.type,
  5800. isDatetimeAxis = type === 'datetime';
  5801. axis.labelFormatter = options.labels.formatter || axis.defaultLabelFormatter; // can be overwritten by dynamic format
  5802. // Flag, stagger lines or not
  5803. axis.userOptions = userOptions;
  5804. //axis.axisTitleMargin = UNDEFINED,// = options.title.margin,
  5805. axis.minPixelPadding = 0;
  5806. //axis.ignoreMinPadding = UNDEFINED; // can be set to true by a column or bar series
  5807. //axis.ignoreMaxPadding = UNDEFINED;
  5808. axis.chart = chart;
  5809. axis.reversed = options.reversed;
  5810. axis.zoomEnabled = options.zoomEnabled !== false;
  5811. // Initial categories
  5812. axis.categories = options.categories || type === 'category';
  5813. // Elements
  5814. //axis.axisGroup = UNDEFINED;
  5815. //axis.gridGroup = UNDEFINED;
  5816. //axis.axisTitle = UNDEFINED;
  5817. //axis.axisLine = UNDEFINED;
  5818. // Shorthand types
  5819. axis.isLog = type === 'logarithmic';
  5820. axis.isDatetimeAxis = isDatetimeAxis;
  5821. // Flag, if axis is linked to another axis
  5822. axis.isLinked = defined(options.linkedTo);
  5823. // Linked axis.
  5824. //axis.linkedParent = UNDEFINED;
  5825. // Tick positions
  5826. //axis.tickPositions = UNDEFINED; // array containing predefined positions
  5827. // Tick intervals
  5828. //axis.tickInterval = UNDEFINED;
  5829. //axis.minorTickInterval = UNDEFINED;
  5830. axis.tickmarkOffset = (axis.categories && options.tickmarkPlacement === 'between') ? 0.5 : 0;
  5831. // Major ticks
  5832. axis.ticks = {};
  5833. // Minor ticks
  5834. axis.minorTicks = {};
  5835. //axis.tickAmount = UNDEFINED;
  5836. // List of plotLines/Bands
  5837. axis.plotLinesAndBands = [];
  5838. // Alternate bands
  5839. axis.alternateBands = {};
  5840. // Axis metrics
  5841. //axis.left = UNDEFINED;
  5842. //axis.top = UNDEFINED;
  5843. //axis.width = UNDEFINED;
  5844. //axis.height = UNDEFINED;
  5845. //axis.bottom = UNDEFINED;
  5846. //axis.right = UNDEFINED;
  5847. //axis.transA = UNDEFINED;
  5848. //axis.transB = UNDEFINED;
  5849. //axis.oldTransA = UNDEFINED;
  5850. axis.len = 0;
  5851. //axis.oldMin = UNDEFINED;
  5852. //axis.oldMax = UNDEFINED;
  5853. //axis.oldUserMin = UNDEFINED;
  5854. //axis.oldUserMax = UNDEFINED;
  5855. //axis.oldAxisLength = UNDEFINED;
  5856. axis.minRange = axis.userMinRange = options.minRange || options.maxZoom;
  5857. axis.range = options.range;
  5858. axis.offset = options.offset || 0;
  5859. // Dictionary for stacks
  5860. axis.stacks = {};
  5861. axis.oldStacks = {};
  5862. // Dictionary for stacks max values
  5863. axis.stackExtremes = {};
  5864. // Min and max in the data
  5865. //axis.dataMin = UNDEFINED,
  5866. //axis.dataMax = UNDEFINED,
  5867. // The axis range
  5868. axis.max = null;
  5869. axis.min = null;
  5870. // User set min and max
  5871. //axis.userMin = UNDEFINED,
  5872. //axis.userMax = UNDEFINED,
  5873. // Run Axis
  5874. var eventType,
  5875. events = axis.options.events;
  5876. // Register
  5877. if (inArray(axis, chart.axes) === -1) { // don't add it again on Axis.update()
  5878. chart.axes.push(axis);
  5879. chart[isXAxis ? 'xAxis' : 'yAxis'].push(axis);
  5880. }
  5881. axis.series = axis.series || []; // populated by Series
  5882. // inverted charts have reversed xAxes as default
  5883. if (chart.inverted && isXAxis && axis.reversed === UNDEFINED) {
  5884. axis.reversed = true;
  5885. }
  5886. axis.removePlotBand = axis.removePlotBandOrLine;
  5887. axis.removePlotLine = axis.removePlotBandOrLine;
  5888. // register event listeners
  5889. for (eventType in events) {
  5890. addEvent(axis, eventType, events[eventType]);
  5891. }
  5892. // extend logarithmic axis
  5893. if (axis.isLog) {
  5894. axis.val2lin = log2lin;
  5895. axis.lin2val = lin2log;
  5896. }
  5897. },
  5898. /**
  5899. * Merge and set options
  5900. */
  5901. setOptions: function (userOptions) {
  5902. this.options = merge(
  5903. this.defaultOptions,
  5904. this.isXAxis ? {} : this.defaultYAxisOptions,
  5905. [this.defaultTopAxisOptions, this.defaultRightAxisOptions,
  5906. this.defaultBottomAxisOptions, this.defaultLeftAxisOptions][this.side],
  5907. merge(
  5908. defaultOptions[this.isXAxis ? 'xAxis' : 'yAxis'], // if set in setOptions (#1053)
  5909. userOptions
  5910. )
  5911. );
  5912. },
  5913. /**
  5914. * Update the axis with a new options structure
  5915. */
  5916. update: function (newOptions, redraw) {
  5917. var chart = this.chart;
  5918. newOptions = chart.options[this.xOrY + 'Axis'][this.options.index] = merge(this.userOptions, newOptions);
  5919. this.destroy(true);
  5920. this._addedPlotLB = this.userMin = this.userMax = UNDEFINED; // #1611, #2306
  5921. this.init(chart, extend(newOptions, {events: UNDEFINED}));
  5922. chart.isDirtyBox = true;
  5923. if (pick(redraw, true)) {
  5924. chart.redraw();
  5925. }
  5926. },
  5927. /**
  5928. * Remove the axis from the chart
  5929. */
  5930. remove: function (redraw) {
  5931. var chart = this.chart,
  5932. key = this.xOrY + 'Axis'; // xAxis or yAxis
  5933. // Remove associated series
  5934. each(this.series, function (series) {
  5935. series.remove(false);
  5936. });
  5937. // Remove the axis
  5938. erase(chart.axes, this);
  5939. erase(chart[key], this);
  5940. chart.options[key].splice(this.options.index, 1);
  5941. each(chart[key], function (axis, i) { // Re-index, #1706
  5942. axis.options.index = i;
  5943. });
  5944. this.destroy();
  5945. chart.isDirtyBox = true;
  5946. if (pick(redraw, true)) {
  5947. chart.redraw();
  5948. }
  5949. },
  5950. /**
  5951. * The default label formatter. The context is a special config object for the label.
  5952. */
  5953. defaultLabelFormatter: function () {
  5954. var axis = this.axis,
  5955. value = this.value,
  5956. categories = axis.categories,
  5957. dateTimeLabelFormat = this.dateTimeLabelFormat,
  5958. numericSymbols = defaultOptions.lang.numericSymbols,
  5959. i = numericSymbols && numericSymbols.length,
  5960. multi,
  5961. ret,
  5962. formatOption = axis.options.labels.format,
  5963. // make sure the same symbol is added for all labels on a linear axis
  5964. numericSymbolDetector = axis.isLog ? value : axis.tickInterval;
  5965. if (formatOption) {
  5966. ret = format(formatOption, this);
  5967. } else if (categories) {
  5968. ret = value;
  5969. } else if (dateTimeLabelFormat) { // datetime axis
  5970. ret = dateFormat(dateTimeLabelFormat, value);
  5971. } else if (i && numericSymbolDetector >= 1000) {
  5972. // Decide whether we should add a numeric symbol like k (thousands) or M (millions).
  5973. // If we are to enable this in tooltip or other places as well, we can move this
  5974. // logic to the numberFormatter and enable it by a parameter.
  5975. while (i-- && ret === UNDEFINED) {
  5976. multi = Math.pow(1000, i + 1);
  5977. if (numericSymbolDetector >= multi && numericSymbols[i] !== null) {
  5978. ret = numberFormat(value / multi, -1) + numericSymbols[i];
  5979. }
  5980. }
  5981. }
  5982. if (ret === UNDEFINED) {
  5983. if (value >= 1000) { // add thousands separators
  5984. ret = numberFormat(value, 0);
  5985. } else { // small numbers
  5986. ret = numberFormat(value, -1);
  5987. }
  5988. }
  5989. return ret;
  5990. },
  5991. /**
  5992. * Get the minimum and maximum for the series of each axis
  5993. */
  5994. getSeriesExtremes: function () {
  5995. var axis = this,
  5996. chart = axis.chart;
  5997. axis.hasVisibleSeries = false;
  5998. // reset dataMin and dataMax in case we're redrawing
  5999. axis.dataMin = axis.dataMax = null;
  6000. // reset cached stacking extremes
  6001. axis.stackExtremes = {};
  6002. axis.buildStacks();
  6003. // loop through this axis' series
  6004. each(axis.series, function (series) {
  6005. if (series.visible || !chart.options.chart.ignoreHiddenSeries) {
  6006. var seriesOptions = series.options,
  6007. xData,
  6008. threshold = seriesOptions.threshold,
  6009. seriesDataMin,
  6010. seriesDataMax;
  6011. axis.hasVisibleSeries = true;
  6012. // Validate threshold in logarithmic axes
  6013. if (axis.isLog && threshold <= 0) {
  6014. threshold = null;
  6015. }
  6016. // Get dataMin and dataMax for X axes
  6017. if (axis.isXAxis) {
  6018. xData = series.xData;
  6019. if (xData.length) {
  6020. axis.dataMin = mathMin(pick(axis.dataMin, xData[0]), arrayMin(xData));
  6021. axis.dataMax = mathMax(pick(axis.dataMax, xData[0]), arrayMax(xData));
  6022. }
  6023. // Get dataMin and dataMax for Y axes, as well as handle stacking and processed data
  6024. } else {
  6025. // Get this particular series extremes
  6026. series.getExtremes();
  6027. seriesDataMax = series.dataMax;
  6028. seriesDataMin = series.dataMin;
  6029. // Get the dataMin and dataMax so far. If percentage is used, the min and max are
  6030. // always 0 and 100. If seriesDataMin and seriesDataMax is null, then series
  6031. // doesn't have active y data, we continue with nulls
  6032. if (defined(seriesDataMin) && defined(seriesDataMax)) {
  6033. axis.dataMin = mathMin(pick(axis.dataMin, seriesDataMin), seriesDataMin);
  6034. axis.dataMax = mathMax(pick(axis.dataMax, seriesDataMax), seriesDataMax);
  6035. }
  6036. // Adjust to threshold
  6037. if (defined(threshold)) {
  6038. if (axis.dataMin >= threshold) {
  6039. axis.dataMin = threshold;
  6040. axis.ignoreMinPadding = true;
  6041. } else if (axis.dataMax < threshold) {
  6042. axis.dataMax = threshold;
  6043. axis.ignoreMaxPadding = true;
  6044. }
  6045. }
  6046. }
  6047. }
  6048. });
  6049. },
  6050. /**
  6051. * Translate from axis value to pixel position on the chart, or back
  6052. *
  6053. */
  6054. translate: function (val, backwards, cvsCoord, old, handleLog, pointPlacement) {
  6055. var axis = this,
  6056. axisLength = axis.len,
  6057. sign = 1,
  6058. cvsOffset = 0,
  6059. localA = old ? axis.oldTransA : axis.transA,
  6060. localMin = old ? axis.oldMin : axis.min,
  6061. returnValue,
  6062. minPixelPadding = axis.minPixelPadding,
  6063. postTranslate = (axis.options.ordinal || (axis.isLog && handleLog)) && axis.lin2val;
  6064. if (!localA) {
  6065. localA = axis.transA;
  6066. }
  6067. // In vertical axes, the canvas coordinates start from 0 at the top like in
  6068. // SVG.
  6069. if (cvsCoord) {
  6070. sign *= -1; // canvas coordinates inverts the value
  6071. cvsOffset = axisLength;
  6072. }
  6073. // Handle reversed axis
  6074. if (axis.reversed) {
  6075. sign *= -1;
  6076. cvsOffset -= sign * axisLength;
  6077. }
  6078. // From pixels to value
  6079. if (backwards) { // reverse translation
  6080. val = val * sign + cvsOffset;
  6081. val -= minPixelPadding;
  6082. returnValue = val / localA + localMin; // from chart pixel to value
  6083. if (postTranslate) { // log and ordinal axes
  6084. returnValue = axis.lin2val(returnValue);
  6085. }
  6086. // From value to pixels
  6087. } else {
  6088. if (postTranslate) { // log and ordinal axes
  6089. val = axis.val2lin(val);
  6090. }
  6091. if (pointPlacement === 'between') {
  6092. pointPlacement = 0.5;
  6093. }
  6094. returnValue = sign * (val - localMin) * localA + cvsOffset + (sign * minPixelPadding) +
  6095. (isNumber(pointPlacement) ? localA * pointPlacement * axis.pointRange : 0);
  6096. }
  6097. return returnValue;
  6098. },
  6099. /**
  6100. * Utility method to translate an axis value to pixel position.
  6101. * @param {Number} value A value in terms of axis units
  6102. * @param {Boolean} paneCoordinates Whether to return the pixel coordinate relative to the chart
  6103. * or just the axis/pane itself.
  6104. */
  6105. toPixels: function (value, paneCoordinates) {
  6106. return this.translate(value, false, !this.horiz, null, true) + (paneCoordinates ? 0 : this.pos);
  6107. },
  6108. /*
  6109. * Utility method to translate a pixel position in to an axis value
  6110. * @param {Number} pixel The pixel value coordinate
  6111. * @param {Boolean} paneCoordiantes Whether the input pixel is relative to the chart or just the
  6112. * axis/pane itself.
  6113. */
  6114. toValue: function (pixel, paneCoordinates) {
  6115. return this.translate(pixel - (paneCoordinates ? 0 : this.pos), true, !this.horiz, null, true);
  6116. },
  6117. /**
  6118. * Create the path for a plot line that goes from the given value on
  6119. * this axis, across the plot to the opposite side
  6120. * @param {Number} value
  6121. * @param {Number} lineWidth Used for calculation crisp line
  6122. * @param {Number] old Use old coordinates (for resizing and rescaling)
  6123. */
  6124. getPlotLinePath: function (value, lineWidth, old, force) {
  6125. var axis = this,
  6126. chart = axis.chart,
  6127. axisLeft = axis.left,
  6128. axisTop = axis.top,
  6129. x1,
  6130. y1,
  6131. x2,
  6132. y2,
  6133. translatedValue = axis.translate(value, null, null, old),
  6134. cHeight = (old && chart.oldChartHeight) || chart.chartHeight,
  6135. cWidth = (old && chart.oldChartWidth) || chart.chartWidth,
  6136. skip,
  6137. transB = axis.transB;
  6138. x1 = x2 = mathRound(translatedValue + transB);
  6139. y1 = y2 = mathRound(cHeight - translatedValue - transB);
  6140. if (isNaN(translatedValue)) { // no min or max
  6141. skip = true;
  6142. } else if (axis.horiz) {
  6143. y1 = axisTop;
  6144. y2 = cHeight - axis.bottom;
  6145. if (x1 < axisLeft || x1 > axisLeft + axis.width) {
  6146. skip = true;
  6147. }
  6148. } else {
  6149. x1 = axisLeft;
  6150. x2 = cWidth - axis.right;
  6151. if (y1 < axisTop || y1 > axisTop + axis.height) {
  6152. skip = true;
  6153. }
  6154. }
  6155. return skip && !force ?
  6156. null :
  6157. chart.renderer.crispLine([M, x1, y1, L, x2, y2], lineWidth || 0);
  6158. },
  6159. /**
  6160. * Create the path for a plot band
  6161. */
  6162. getPlotBandPath: function (from, to) {
  6163. var toPath = this.getPlotLinePath(to),
  6164. path = this.getPlotLinePath(from);
  6165. if (path && toPath) {
  6166. path.push(
  6167. toPath[4],
  6168. toPath[5],
  6169. toPath[1],
  6170. toPath[2]
  6171. );
  6172. } else { // outside the axis area
  6173. path = null;
  6174. }
  6175. return path;
  6176. },
  6177. /**
  6178. * Set the tick positions of a linear axis to round values like whole tens or every five.
  6179. */
  6180. getLinearTickPositions: function (tickInterval, min, max) {
  6181. var pos,
  6182. lastPos,
  6183. roundedMin = correctFloat(mathFloor(min / tickInterval) * tickInterval),
  6184. roundedMax = correctFloat(mathCeil(max / tickInterval) * tickInterval),
  6185. tickPositions = [];
  6186. // Populate the intermediate values
  6187. pos = roundedMin;
  6188. while (pos <= roundedMax) {
  6189. // Place the tick on the rounded value
  6190. tickPositions.push(pos);
  6191. // Always add the raw tickInterval, not the corrected one.
  6192. pos = correctFloat(pos + tickInterval);
  6193. // If the interval is not big enough in the current min - max range to actually increase
  6194. // the loop variable, we need to break out to prevent endless loop. Issue #619
  6195. if (pos === lastPos) {
  6196. break;
  6197. }
  6198. // Record the last value
  6199. lastPos = pos;
  6200. }
  6201. return tickPositions;
  6202. },
  6203. /**
  6204. * Set the tick positions of a logarithmic axis
  6205. */
  6206. getLogTickPositions: function (interval, min, max, minor) {
  6207. var axis = this,
  6208. options = axis.options,
  6209. axisLength = axis.len,
  6210. // Since we use this method for both major and minor ticks,
  6211. // use a local variable and return the result
  6212. positions = [];
  6213. // Reset
  6214. if (!minor) {
  6215. axis._minorAutoInterval = null;
  6216. }
  6217. // First case: All ticks fall on whole logarithms: 1, 10, 100 etc.
  6218. if (interval >= 0.5) {
  6219. interval = mathRound(interval);
  6220. positions = axis.getLinearTickPositions(interval, min, max);
  6221. // Second case: We need intermediary ticks. For example
  6222. // 1, 2, 4, 6, 8, 10, 20, 40 etc.
  6223. } else if (interval >= 0.08) {
  6224. var roundedMin = mathFloor(min),
  6225. intermediate,
  6226. i,
  6227. j,
  6228. len,
  6229. pos,
  6230. lastPos,
  6231. break2;
  6232. if (interval > 0.3) {
  6233. intermediate = [1, 2, 4];
  6234. } else if (interval > 0.15) { // 0.2 equals five minor ticks per 1, 10, 100 etc
  6235. intermediate = [1, 2, 4, 6, 8];
  6236. } else { // 0.1 equals ten minor ticks per 1, 10, 100 etc
  6237. intermediate = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  6238. }
  6239. for (i = roundedMin; i < max + 1 && !break2; i++) {
  6240. len = intermediate.length;
  6241. for (j = 0; j < len && !break2; j++) {
  6242. pos = log2lin(lin2log(i) * intermediate[j]);
  6243. if (pos > min && (!minor || lastPos <= max)) { // #1670
  6244. positions.push(lastPos);
  6245. }
  6246. if (lastPos > max) {
  6247. break2 = true;
  6248. }
  6249. lastPos = pos;
  6250. }
  6251. }
  6252. // Third case: We are so deep in between whole logarithmic values that
  6253. // we might as well handle the tick positions like a linear axis. For
  6254. // example 1.01, 1.02, 1.03, 1.04.
  6255. } else {
  6256. var realMin = lin2log(min),
  6257. realMax = lin2log(max),
  6258. tickIntervalOption = options[minor ? 'minorTickInterval' : 'tickInterval'],
  6259. filteredTickIntervalOption = tickIntervalOption === 'auto' ? null : tickIntervalOption,
  6260. tickPixelIntervalOption = options.tickPixelInterval / (minor ? 5 : 1),
  6261. totalPixelLength = minor ? axisLength / axis.tickPositions.length : axisLength;
  6262. interval = pick(
  6263. filteredTickIntervalOption,
  6264. axis._minorAutoInterval,
  6265. (realMax - realMin) * tickPixelIntervalOption / (totalPixelLength || 1)
  6266. );
  6267. interval = normalizeTickInterval(
  6268. interval,
  6269. null,
  6270. getMagnitude(interval)
  6271. );
  6272. positions = map(axis.getLinearTickPositions(
  6273. interval,
  6274. realMin,
  6275. realMax
  6276. ), log2lin);
  6277. if (!minor) {
  6278. axis._minorAutoInterval = interval / 5;
  6279. }
  6280. }
  6281. // Set the axis-level tickInterval variable
  6282. if (!minor) {
  6283. axis.tickInterval = interval;
  6284. }
  6285. return positions;
  6286. },
  6287. /**
  6288. * Return the minor tick positions. For logarithmic axes, reuse the same logic
  6289. * as for major ticks.
  6290. */
  6291. getMinorTickPositions: function () {
  6292. var axis = this,
  6293. options = axis.options,
  6294. tickPositions = axis.tickPositions,
  6295. minorTickInterval = axis.minorTickInterval,
  6296. minorTickPositions = [],
  6297. pos,
  6298. i,
  6299. len;
  6300. if (axis.isLog) {
  6301. len = tickPositions.length;
  6302. for (i = 1; i < len; i++) {
  6303. minorTickPositions = minorTickPositions.concat(
  6304. axis.getLogTickPositions(minorTickInterval, tickPositions[i - 1], tickPositions[i], true)
  6305. );
  6306. }
  6307. } else if (axis.isDatetimeAxis && options.minorTickInterval === 'auto') { // #1314
  6308. minorTickPositions = minorTickPositions.concat(
  6309. getTimeTicks(
  6310. normalizeTimeTickInterval(minorTickInterval),
  6311. axis.min,
  6312. axis.max,
  6313. options.startOfWeek
  6314. )
  6315. );
  6316. if (minorTickPositions[0] < axis.min) {
  6317. minorTickPositions.shift();
  6318. }
  6319. } else {
  6320. for (pos = axis.min + (tickPositions[0] - axis.min) % minorTickInterval; pos <= axis.max; pos += minorTickInterval) {
  6321. minorTickPositions.push(pos);
  6322. }
  6323. }
  6324. return minorTickPositions;
  6325. },
  6326. /**
  6327. * Adjust the min and max for the minimum range. Keep in mind that the series data is
  6328. * not yet processed, so we don't have information on data cropping and grouping, or
  6329. * updated axis.pointRange or series.pointRange. The data can't be processed until
  6330. * we have finally established min and max.
  6331. */
  6332. adjustForMinRange: function () {
  6333. var axis = this,
  6334. options = axis.options,
  6335. min = axis.min,
  6336. max = axis.max,
  6337. zoomOffset,
  6338. spaceAvailable = axis.dataMax - axis.dataMin >= axis.minRange,
  6339. closestDataRange,
  6340. i,
  6341. distance,
  6342. xData,
  6343. loopLength,
  6344. minArgs,
  6345. maxArgs;
  6346. // Set the automatic minimum range based on the closest point distance
  6347. if (axis.isXAxis && axis.minRange === UNDEFINED && !axis.isLog) {
  6348. if (defined(options.min) || defined(options.max)) {
  6349. axis.minRange = null; // don't do this again
  6350. } else {
  6351. // Find the closest distance between raw data points, as opposed to
  6352. // closestPointRange that applies to processed points (cropped and grouped)
  6353. each(axis.series, function (series) {
  6354. xData = series.xData;
  6355. loopLength = series.xIncrement ? 1 : xData.length - 1;
  6356. for (i = loopLength; i > 0; i--) {
  6357. distance = xData[i] - xData[i - 1];
  6358. if (closestDataRange === UNDEFINED || distance < closestDataRange) {
  6359. closestDataRange = distance;
  6360. }
  6361. }
  6362. });
  6363. axis.minRange = mathMin(closestDataRange * 5, axis.dataMax - axis.dataMin);
  6364. }
  6365. }
  6366. // if minRange is exceeded, adjust
  6367. if (max - min < axis.minRange) {
  6368. var minRange = axis.minRange;
  6369. zoomOffset = (minRange - max + min) / 2;
  6370. // if min and max options have been set, don't go beyond it
  6371. minArgs = [min - zoomOffset, pick(options.min, min - zoomOffset)];
  6372. if (spaceAvailable) { // if space is available, stay within the data range
  6373. minArgs[2] = axis.dataMin;
  6374. }
  6375. min = arrayMax(minArgs);
  6376. maxArgs = [min + minRange, pick(options.max, min + minRange)];
  6377. if (spaceAvailable) { // if space is availabe, stay within the data range
  6378. maxArgs[2] = axis.dataMax;
  6379. }
  6380. max = arrayMin(maxArgs);
  6381. // now if the max is adjusted, adjust the min back
  6382. if (max - min < minRange) {
  6383. minArgs[0] = max - minRange;
  6384. minArgs[1] = pick(options.min, max - minRange);
  6385. min = arrayMax(minArgs);
  6386. }
  6387. }
  6388. // Record modified extremes
  6389. axis.min = min;
  6390. axis.max = max;
  6391. },
  6392. /**
  6393. * Update translation information
  6394. */
  6395. setAxisTranslation: function (saveOld) {
  6396. var axis = this,
  6397. range = axis.max - axis.min,
  6398. pointRange = 0,
  6399. closestPointRange,
  6400. minPointOffset = 0,
  6401. pointRangePadding = 0,
  6402. linkedParent = axis.linkedParent,
  6403. ordinalCorrection,
  6404. transA = axis.transA;
  6405. // adjust translation for padding
  6406. if (axis.isXAxis) {
  6407. if (linkedParent) {
  6408. minPointOffset = linkedParent.minPointOffset;
  6409. pointRangePadding = linkedParent.pointRangePadding;
  6410. } else {
  6411. each(axis.series, function (series) {
  6412. var seriesPointRange = series.pointRange,
  6413. pointPlacement = series.options.pointPlacement,
  6414. seriesClosestPointRange = series.closestPointRange;
  6415. if (seriesPointRange > range) { // #1446
  6416. seriesPointRange = 0;
  6417. }
  6418. pointRange = mathMax(pointRange, seriesPointRange);
  6419. // minPointOffset is the value padding to the left of the axis in order to make
  6420. // room for points with a pointRange, typically columns. When the pointPlacement option
  6421. // is 'between' or 'on', this padding does not apply.
  6422. minPointOffset = mathMax(
  6423. minPointOffset,
  6424. isString(pointPlacement) ? 0 : seriesPointRange / 2
  6425. );
  6426. // Determine the total padding needed to the length of the axis to make room for the
  6427. // pointRange. If the series' pointPlacement is 'on', no padding is added.
  6428. pointRangePadding = mathMax(
  6429. pointRangePadding,
  6430. pointPlacement === 'on' ? 0 : seriesPointRange
  6431. );
  6432. // Set the closestPointRange
  6433. if (!series.noSharedTooltip && defined(seriesClosestPointRange)) {
  6434. closestPointRange = defined(closestPointRange) ?
  6435. mathMin(closestPointRange, seriesClosestPointRange) :
  6436. seriesClosestPointRange;
  6437. }
  6438. });
  6439. }
  6440. // Record minPointOffset and pointRangePadding
  6441. ordinalCorrection = axis.ordinalSlope && closestPointRange ? axis.ordinalSlope / closestPointRange : 1; // #988, #1853
  6442. axis.minPointOffset = minPointOffset = minPointOffset * ordinalCorrection;
  6443. axis.pointRangePadding = pointRangePadding = pointRangePadding * ordinalCorrection;
  6444. // pointRange means the width reserved for each point, like in a column chart
  6445. axis.pointRange = mathMin(pointRange, range);
  6446. // closestPointRange means the closest distance between points. In columns
  6447. // it is mostly equal to pointRange, but in lines pointRange is 0 while closestPointRange
  6448. // is some other value
  6449. axis.closestPointRange = closestPointRange;
  6450. }
  6451. // Secondary values
  6452. if (saveOld) {
  6453. axis.oldTransA = transA;
  6454. }
  6455. axis.translationSlope = axis.transA = transA = axis.len / ((range + pointRangePadding) || 1);
  6456. axis.transB = axis.horiz ? axis.left : axis.bottom; // translation addend
  6457. axis.minPixelPadding = transA * minPointOffset;
  6458. },
  6459. /**
  6460. * Set the tick positions to round values and optionally extend the extremes
  6461. * to the nearest tick
  6462. */
  6463. setTickPositions: function (secondPass) {
  6464. var axis = this,
  6465. chart = axis.chart,
  6466. options = axis.options,
  6467. isLog = axis.isLog,
  6468. isDatetimeAxis = axis.isDatetimeAxis,
  6469. isXAxis = axis.isXAxis,
  6470. isLinked = axis.isLinked,
  6471. tickPositioner = axis.options.tickPositioner,
  6472. maxPadding = options.maxPadding,
  6473. minPadding = options.minPadding,
  6474. length,
  6475. linkedParentExtremes,
  6476. tickIntervalOption = options.tickInterval,
  6477. minTickIntervalOption = options.minTickInterval,
  6478. tickPixelIntervalOption = options.tickPixelInterval,
  6479. tickPositions,
  6480. keepTwoTicksOnly,
  6481. categories = axis.categories;
  6482. // linked axis gets the extremes from the parent axis
  6483. if (isLinked) {
  6484. axis.linkedParent = chart[isXAxis ? 'xAxis' : 'yAxis'][options.linkedTo];
  6485. linkedParentExtremes = axis.linkedParent.getExtremes();
  6486. axis.min = pick(linkedParentExtremes.min, linkedParentExtremes.dataMin);
  6487. axis.max = pick(linkedParentExtremes.max, linkedParentExtremes.dataMax);
  6488. if (options.type !== axis.linkedParent.options.type) {
  6489. error(11, 1); // Can't link axes of different type
  6490. }
  6491. } else { // initial min and max from the extreme data values
  6492. axis.min = pick(axis.userMin, options.min, axis.dataMin);
  6493. axis.max = pick(axis.userMax, options.max, axis.dataMax);
  6494. }
  6495. if (isLog) {
  6496. if (!secondPass && mathMin(axis.min, pick(axis.dataMin, axis.min)) <= 0) { // #978
  6497. error(10, 1); // Can't plot negative values on log axis
  6498. }
  6499. axis.min = correctFloat(log2lin(axis.min)); // correctFloat cures #934
  6500. axis.max = correctFloat(log2lin(axis.max));
  6501. }
  6502. // handle zoomed range
  6503. if (axis.range) {
  6504. axis.userMin = axis.min = mathMax(axis.min, axis.max - axis.range); // #618
  6505. axis.userMax = axis.max;
  6506. if (secondPass) {
  6507. axis.range = null; // don't use it when running setExtremes
  6508. }
  6509. }
  6510. // Hook for adjusting this.min and this.max. Used by bubble series.
  6511. if (axis.beforePadding) {
  6512. axis.beforePadding();
  6513. }
  6514. // adjust min and max for the minimum range
  6515. axis.adjustForMinRange();
  6516. // Pad the values to get clear of the chart's edges. To avoid tickInterval taking the padding
  6517. // into account, we do this after computing tick interval (#1337).
  6518. if (!categories && !axis.usePercentage && !isLinked && defined(axis.min) && defined(axis.max)) {
  6519. length = axis.max - axis.min;
  6520. if (length) {
  6521. if (!defined(options.min) && !defined(axis.userMin) && minPadding && (axis.dataMin < 0 || !axis.ignoreMinPadding)) {
  6522. axis.min -= length * minPadding;
  6523. }
  6524. if (!defined(options.max) && !defined(axis.userMax) && maxPadding && (axis.dataMax > 0 || !axis.ignoreMaxPadding)) {
  6525. axis.max += length * maxPadding;
  6526. }
  6527. }
  6528. }
  6529. // get tickInterval
  6530. if (axis.min === axis.max || axis.min === undefined || axis.max === undefined) {
  6531. axis.tickInterval = 1;
  6532. } else if (isLinked && !tickIntervalOption &&
  6533. tickPixelIntervalOption === axis.linkedParent.options.tickPixelInterval) {
  6534. axis.tickInterval = axis.linkedParent.tickInterval;
  6535. } else {
  6536. axis.tickInterval = pick(
  6537. tickIntervalOption,
  6538. categories ? // for categoried axis, 1 is default, for linear axis use tickPix
  6539. 1 :
  6540. // don't let it be more than the data range
  6541. (axis.max - axis.min) * tickPixelIntervalOption / mathMax(axis.len, tickPixelIntervalOption)
  6542. );
  6543. // For squished axes, set only two ticks
  6544. if (!defined(tickIntervalOption) && axis.len < tickPixelIntervalOption && !this.isRadial) {
  6545. keepTwoTicksOnly = true;
  6546. axis.tickInterval /= 4; // tick extremes closer to the real values
  6547. }
  6548. }
  6549. // Now we're finished detecting min and max, crop and group series data. This
  6550. // is in turn needed in order to find tick positions in ordinal axes.
  6551. if (isXAxis && !secondPass) {
  6552. each(axis.series, function (series) {
  6553. series.processData(axis.min !== axis.oldMin || axis.max !== axis.oldMax);
  6554. });
  6555. }
  6556. // set the translation factor used in translate function
  6557. axis.setAxisTranslation(true);
  6558. // hook for ordinal axes and radial axes
  6559. if (axis.beforeSetTickPositions) {
  6560. axis.beforeSetTickPositions();
  6561. }
  6562. // hook for extensions, used in Highstock ordinal axes
  6563. if (axis.postProcessTickInterval) {
  6564. axis.tickInterval = axis.postProcessTickInterval(axis.tickInterval);
  6565. }
  6566. // In column-like charts, don't cramp in more ticks than there are points (#1943)
  6567. if (axis.pointRange) {
  6568. axis.tickInterval = mathMax(axis.pointRange, axis.tickInterval);
  6569. }
  6570. // Before normalizing the tick interval, handle minimum tick interval. This applies only if tickInterval is not defined.
  6571. if (!tickIntervalOption && axis.tickInterval < minTickIntervalOption) {
  6572. axis.tickInterval = minTickIntervalOption;
  6573. }
  6574. // for linear axes, get magnitude and normalize the interval
  6575. if (!isDatetimeAxis && !isLog) { // linear
  6576. if (!tickIntervalOption) {
  6577. axis.tickInterval = normalizeTickInterval(axis.tickInterval, null, getMagnitude(axis.tickInterval), options);
  6578. }
  6579. }
  6580. // get minorTickInterval
  6581. axis.minorTickInterval = options.minorTickInterval === 'auto' && axis.tickInterval ?
  6582. axis.tickInterval / 5 : options.minorTickInterval;
  6583. // find the tick positions
  6584. axis.tickPositions = tickPositions = options.tickPositions ?
  6585. [].concat(options.tickPositions) : // Work on a copy (#1565)
  6586. (tickPositioner && tickPositioner.apply(axis, [axis.min, axis.max]));
  6587. if (!tickPositions) {
  6588. // Too many ticks
  6589. if (!axis.ordinalPositions && (axis.max - axis.min) / axis.tickInterval > mathMax(2 * axis.len, 200)) {
  6590. error(19, true);
  6591. }
  6592. if (isDatetimeAxis) {
  6593. tickPositions = (axis.getNonLinearTimeTicks || getTimeTicks)(
  6594. normalizeTimeTickInterval(axis.tickInterval, options.units),
  6595. axis.min,
  6596. axis.max,
  6597. options.startOfWeek,
  6598. axis.ordinalPositions,
  6599. axis.closestPointRange,
  6600. true
  6601. );
  6602. } else if (isLog) {
  6603. tickPositions = axis.getLogTickPositions(axis.tickInterval, axis.min, axis.max);
  6604. } else {
  6605. tickPositions = axis.getLinearTickPositions(axis.tickInterval, axis.min, axis.max);
  6606. }
  6607. if (keepTwoTicksOnly) {
  6608. tickPositions.splice(1, tickPositions.length - 2);
  6609. }
  6610. axis.tickPositions = tickPositions;
  6611. }
  6612. if (!isLinked) {
  6613. // reset min/max or remove extremes based on start/end on tick
  6614. var roundedMin = tickPositions[0],
  6615. roundedMax = tickPositions[tickPositions.length - 1],
  6616. minPointOffset = axis.minPointOffset || 0,
  6617. singlePad;
  6618. if (options.startOnTick) {
  6619. axis.min = roundedMin;
  6620. } else if (axis.min - minPointOffset > roundedMin) {
  6621. tickPositions.shift();
  6622. }
  6623. if (options.endOnTick) {
  6624. axis.max = roundedMax;
  6625. } else if (axis.max + minPointOffset < roundedMax) {
  6626. tickPositions.pop();
  6627. }
  6628. // When there is only one point, or all points have the same value on this axis, then min
  6629. // and max are equal and tickPositions.length is 1. In this case, add some padding
  6630. // in order to center the point, but leave it with one tick. #1337.
  6631. if (tickPositions.length === 1) {
  6632. singlePad = 0.001; // The lowest possible number to avoid extra padding on columns
  6633. axis.min -= singlePad;
  6634. axis.max += singlePad;
  6635. }
  6636. }
  6637. },
  6638. /**
  6639. * Set the max ticks of either the x and y axis collection
  6640. */
  6641. setMaxTicks: function () {
  6642. var chart = this.chart,
  6643. maxTicks = chart.maxTicks || {},
  6644. tickPositions = this.tickPositions,
  6645. key = this._maxTicksKey = [this.xOrY, this.pos, this.len].join('-');
  6646. if (!this.isLinked && !this.isDatetimeAxis && tickPositions && tickPositions.length > (maxTicks[key] || 0) && this.options.alignTicks !== false) {
  6647. maxTicks[key] = tickPositions.length;
  6648. }
  6649. chart.maxTicks = maxTicks;
  6650. },
  6651. /**
  6652. * When using multiple axes, adjust the number of ticks to match the highest
  6653. * number of ticks in that group
  6654. */
  6655. adjustTickAmount: function () {
  6656. var axis = this,
  6657. chart = axis.chart,
  6658. key = axis._maxTicksKey,
  6659. tickPositions = axis.tickPositions,
  6660. maxTicks = chart.maxTicks;
  6661. if (maxTicks && maxTicks[key] && !axis.isDatetimeAxis && !axis.categories && !axis.isLinked && axis.options.alignTicks !== false) { // only apply to linear scale
  6662. var oldTickAmount = axis.tickAmount,
  6663. calculatedTickAmount = tickPositions.length,
  6664. tickAmount;
  6665. // set the axis-level tickAmount to use below
  6666. axis.tickAmount = tickAmount = maxTicks[key];
  6667. if (calculatedTickAmount < tickAmount) {
  6668. while (tickPositions.length < tickAmount) {
  6669. tickPositions.push(correctFloat(
  6670. tickPositions[tickPositions.length - 1] + axis.tickInterval
  6671. ));
  6672. }
  6673. axis.transA *= (calculatedTickAmount - 1) / (tickAmount - 1);
  6674. axis.max = tickPositions[tickPositions.length - 1];
  6675. }
  6676. if (defined(oldTickAmount) && tickAmount !== oldTickAmount) {
  6677. axis.isDirty = true;
  6678. }
  6679. }
  6680. },
  6681. /**
  6682. * Set the scale based on data min and max, user set min and max or options
  6683. *
  6684. */
  6685. setScale: function () {
  6686. var axis = this,
  6687. stacks = axis.stacks,
  6688. type,
  6689. i,
  6690. isDirtyData,
  6691. isDirtyAxisLength;
  6692. axis.oldMin = axis.min;
  6693. axis.oldMax = axis.max;
  6694. axis.oldAxisLength = axis.len;
  6695. // set the new axisLength
  6696. axis.setAxisSize();
  6697. //axisLength = horiz ? axisWidth : axisHeight;
  6698. isDirtyAxisLength = axis.len !== axis.oldAxisLength;
  6699. // is there new data?
  6700. each(axis.series, function (series) {
  6701. if (series.isDirtyData || series.isDirty ||
  6702. series.xAxis.isDirty) { // when x axis is dirty, we need new data extremes for y as well
  6703. isDirtyData = true;
  6704. }
  6705. });
  6706. // do we really need to go through all this?
  6707. if (isDirtyAxisLength || isDirtyData || axis.isLinked || axis.forceRedraw ||
  6708. axis.userMin !== axis.oldUserMin || axis.userMax !== axis.oldUserMax) {
  6709. // reset stacks
  6710. if (!axis.isXAxis) {
  6711. for (type in stacks) {
  6712. delete stacks[type];
  6713. }
  6714. }
  6715. axis.forceRedraw = false;
  6716. // get data extremes if needed
  6717. axis.getSeriesExtremes();
  6718. // get fixed positions based on tickInterval
  6719. axis.setTickPositions();
  6720. // record old values to decide whether a rescale is necessary later on (#540)
  6721. axis.oldUserMin = axis.userMin;
  6722. axis.oldUserMax = axis.userMax;
  6723. // Mark as dirty if it is not already set to dirty and extremes have changed. #595.
  6724. if (!axis.isDirty) {
  6725. axis.isDirty = isDirtyAxisLength || axis.min !== axis.oldMin || axis.max !== axis.oldMax;
  6726. }
  6727. } else if (!axis.isXAxis) {
  6728. if (axis.oldStacks) {
  6729. stacks = axis.stacks = axis.oldStacks;
  6730. }
  6731. // reset stacks
  6732. for (type in stacks) {
  6733. for (i in stacks[type]) {
  6734. stacks[type][i].cum = stacks[type][i].total;
  6735. }
  6736. }
  6737. }
  6738. // Set the maximum tick amount
  6739. axis.setMaxTicks();
  6740. },
  6741. /**
  6742. * Set the extremes and optionally redraw
  6743. * @param {Number} newMin
  6744. * @param {Number} newMax
  6745. * @param {Boolean} redraw
  6746. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  6747. * configuration
  6748. * @param {Object} eventArguments
  6749. *
  6750. */
  6751. setExtremes: function (newMin, newMax, redraw, animation, eventArguments) {
  6752. var axis = this,
  6753. chart = axis.chart;
  6754. redraw = pick(redraw, true); // defaults to true
  6755. // Extend the arguments with min and max
  6756. eventArguments = extend(eventArguments, {
  6757. min: newMin,
  6758. max: newMax
  6759. });
  6760. // Fire the event
  6761. fireEvent(axis, 'setExtremes', eventArguments, function () { // the default event handler
  6762. axis.userMin = newMin;
  6763. axis.userMax = newMax;
  6764. axis.eventArgs = eventArguments;
  6765. // Mark for running afterSetExtremes
  6766. axis.isDirtyExtremes = true;
  6767. // redraw
  6768. if (redraw) {
  6769. chart.redraw(animation);
  6770. }
  6771. });
  6772. },
  6773. /**
  6774. * Overridable method for zooming chart. Pulled out in a separate method to allow overriding
  6775. * in stock charts.
  6776. */
  6777. zoom: function (newMin, newMax) {
  6778. // Prevent pinch zooming out of range. Check for defined is for #1946.
  6779. if (!this.allowZoomOutside) {
  6780. if (defined(this.dataMin) && newMin <= this.dataMin) {
  6781. newMin = UNDEFINED;
  6782. }
  6783. if (defined(this.dataMax) && newMax >= this.dataMax) {
  6784. newMax = UNDEFINED;
  6785. }
  6786. }
  6787. // In full view, displaying the reset zoom button is not required
  6788. this.displayBtn = newMin !== UNDEFINED || newMax !== UNDEFINED;
  6789. // Do it
  6790. this.setExtremes(
  6791. newMin,
  6792. newMax,
  6793. false,
  6794. UNDEFINED,
  6795. {trigger: 'zoom'}
  6796. );
  6797. return true;
  6798. },
  6799. /**
  6800. * Update the axis metrics
  6801. */
  6802. setAxisSize: function () {
  6803. var chart = this.chart,
  6804. options = this.options,
  6805. offsetLeft = options.offsetLeft || 0,
  6806. offsetRight = options.offsetRight || 0,
  6807. horiz = this.horiz,
  6808. width,
  6809. height,
  6810. top,
  6811. left;
  6812. // Expose basic values to use in Series object and navigator
  6813. this.left = left = pick(options.left, chart.plotLeft + offsetLeft);
  6814. this.top = top = pick(options.top, chart.plotTop);
  6815. this.width = width = pick(options.width, chart.plotWidth - offsetLeft + offsetRight);
  6816. this.height = height = pick(options.height, chart.plotHeight);
  6817. this.bottom = chart.chartHeight - height - top;
  6818. this.right = chart.chartWidth - width - left;
  6819. // Direction agnostic properties
  6820. this.len = mathMax(horiz ? width : height, 0); // mathMax fixes #905
  6821. this.pos = horiz ? left : top; // distance from SVG origin
  6822. },
  6823. /**
  6824. * Get the actual axis extremes
  6825. */
  6826. getExtremes: function () {
  6827. var axis = this,
  6828. isLog = axis.isLog;
  6829. return {
  6830. min: isLog ? correctFloat(lin2log(axis.min)) : axis.min,
  6831. max: isLog ? correctFloat(lin2log(axis.max)) : axis.max,
  6832. dataMin: axis.dataMin,
  6833. dataMax: axis.dataMax,
  6834. userMin: axis.userMin,
  6835. userMax: axis.userMax
  6836. };
  6837. },
  6838. /**
  6839. * Get the zero plane either based on zero or on the min or max value.
  6840. * Used in bar and area plots
  6841. */
  6842. getThreshold: function (threshold) {
  6843. var axis = this,
  6844. isLog = axis.isLog;
  6845. var realMin = isLog ? lin2log(axis.min) : axis.min,
  6846. realMax = isLog ? lin2log(axis.max) : axis.max;
  6847. if (realMin > threshold || threshold === null) {
  6848. threshold = realMin;
  6849. } else if (realMax < threshold) {
  6850. threshold = realMax;
  6851. }
  6852. return axis.translate(threshold, 0, 1, 0, 1);
  6853. },
  6854. addPlotBand: function (options) {
  6855. this.addPlotBandOrLine(options, 'plotBands');
  6856. },
  6857. addPlotLine: function (options) {
  6858. this.addPlotBandOrLine(options, 'plotLines');
  6859. },
  6860. /**
  6861. * Add a plot band or plot line after render time
  6862. *
  6863. * @param options {Object} The plotBand or plotLine configuration object
  6864. */
  6865. addPlotBandOrLine: function (options, coll) {
  6866. var obj = new PlotLineOrBand(this, options).render(),
  6867. userOptions = this.userOptions;
  6868. if (obj) { // #2189
  6869. // Add it to the user options for exporting and Axis.update
  6870. if (coll) {
  6871. userOptions[coll] = userOptions[coll] || [];
  6872. userOptions[coll].push(options);
  6873. }
  6874. this.plotLinesAndBands.push(obj);
  6875. }
  6876. return obj;
  6877. },
  6878. /**
  6879. * Compute auto alignment for the axis label based on which side the axis is on
  6880. * and the given rotation for the label
  6881. */
  6882. autoLabelAlign: function (rotation) {
  6883. var ret,
  6884. angle = (pick(rotation, 0) - (this.side * 90) + 720) % 360;
  6885. if (angle > 15 && angle < 165) {
  6886. ret = 'right';
  6887. } else if (angle > 195 && angle < 345) {
  6888. ret = 'left';
  6889. } else {
  6890. ret = 'center';
  6891. }
  6892. return ret;
  6893. },
  6894. /**
  6895. * Render the tick labels to a preliminary position to get their sizes
  6896. */
  6897. getOffset: function () {
  6898. var axis = this,
  6899. chart = axis.chart,
  6900. renderer = chart.renderer,
  6901. options = axis.options,
  6902. tickPositions = axis.tickPositions,
  6903. ticks = axis.ticks,
  6904. horiz = axis.horiz,
  6905. side = axis.side,
  6906. invertedSide = chart.inverted ? [1, 0, 3, 2][side] : side,
  6907. hasData,
  6908. showAxis,
  6909. titleOffset = 0,
  6910. titleOffsetOption,
  6911. titleMargin = 0,
  6912. axisTitleOptions = options.title,
  6913. labelOptions = options.labels,
  6914. labelOffset = 0, // reset
  6915. axisOffset = chart.axisOffset,
  6916. clipOffset = chart.clipOffset,
  6917. directionFactor = [-1, 1, 1, -1][side],
  6918. n,
  6919. i,
  6920. autoStaggerLines = 1,
  6921. maxStaggerLines = pick(labelOptions.maxStaggerLines, 5),
  6922. sortedPositions,
  6923. lastRight,
  6924. overlap,
  6925. pos,
  6926. bBox,
  6927. x,
  6928. w,
  6929. lineNo;
  6930. // For reuse in Axis.render
  6931. axis.hasData = hasData = (axis.hasVisibleSeries || (defined(axis.min) && defined(axis.max) && !!tickPositions));
  6932. axis.showAxis = showAxis = hasData || pick(options.showEmpty, true);
  6933. // Set/reset staggerLines
  6934. axis.staggerLines = axis.horiz && labelOptions.staggerLines;
  6935. // Create the axisGroup and gridGroup elements on first iteration
  6936. if (!axis.axisGroup) {
  6937. axis.gridGroup = renderer.g('grid')
  6938. .attr({zIndex: options.gridZIndex || 1})
  6939. .add();
  6940. axis.axisGroup = renderer.g('axis')
  6941. .attr({zIndex: options.zIndex || 2})
  6942. .add();
  6943. axis.labelGroup = renderer.g('axis-labels')
  6944. .attr({zIndex: labelOptions.zIndex || 7})
  6945. .add();
  6946. }
  6947. if (hasData || axis.isLinked) {
  6948. // Set the explicit or automatic label alignment
  6949. axis.labelAlign = pick(labelOptions.align || axis.autoLabelAlign(labelOptions.rotation));
  6950. each(tickPositions, function (pos) {
  6951. if (!ticks[pos]) {
  6952. ticks[pos] = new Tick(axis, pos);
  6953. } else {
  6954. ticks[pos].addLabel(); // update labels depending on tick interval
  6955. }
  6956. });
  6957. // Handle automatic stagger lines
  6958. if (axis.horiz && !axis.staggerLines && maxStaggerLines && !labelOptions.rotation) {
  6959. sortedPositions = axis.reversed ? [].concat(tickPositions).reverse() : tickPositions;
  6960. while (autoStaggerLines < maxStaggerLines) {
  6961. lastRight = [];
  6962. overlap = false;
  6963. for (i = 0; i < sortedPositions.length; i++) {
  6964. pos = sortedPositions[i];
  6965. bBox = ticks[pos].label && ticks[pos].label.getBBox();
  6966. w = bBox ? bBox.width : 0;
  6967. lineNo = i % autoStaggerLines;
  6968. if (w) {
  6969. x = axis.translate(pos); // don't handle log
  6970. if (lastRight[lineNo] !== UNDEFINED && x < lastRight[lineNo]) {
  6971. overlap = true;
  6972. }
  6973. lastRight[lineNo] = x + w;
  6974. }
  6975. }
  6976. if (overlap) {
  6977. autoStaggerLines++;
  6978. } else {
  6979. break;
  6980. }
  6981. }
  6982. if (autoStaggerLines > 1) {
  6983. axis.staggerLines = autoStaggerLines;
  6984. }
  6985. }
  6986. each(tickPositions, function (pos) {
  6987. // left side must be align: right and right side must have align: left for labels
  6988. if (side === 0 || side === 2 || {1: 'left', 3: 'right'}[side] === axis.labelAlign) {
  6989. // get the highest offset
  6990. labelOffset = mathMax(
  6991. ticks[pos].getLabelSize(),
  6992. labelOffset
  6993. );
  6994. }
  6995. });
  6996. if (axis.staggerLines) {
  6997. labelOffset *= axis.staggerLines;
  6998. axis.labelOffset = labelOffset;
  6999. }
  7000. } else { // doesn't have data
  7001. for (n in ticks) {
  7002. ticks[n].destroy();
  7003. delete ticks[n];
  7004. }
  7005. }
  7006. if (axisTitleOptions && axisTitleOptions.text && axisTitleOptions.enabled !== false) {
  7007. if (!axis.axisTitle) {
  7008. axis.axisTitle = renderer.text(
  7009. axisTitleOptions.text,
  7010. 0,
  7011. 0,
  7012. axisTitleOptions.useHTML
  7013. )
  7014. .attr({
  7015. zIndex: 7,
  7016. rotation: axisTitleOptions.rotation || 0,
  7017. align:
  7018. axisTitleOptions.textAlign ||
  7019. {low: 'left', middle: 'center', high: 'right'}[axisTitleOptions.align]
  7020. })
  7021. .css(axisTitleOptions.style)
  7022. .add(axis.axisGroup);
  7023. axis.axisTitle.isNew = true;
  7024. }
  7025. if (showAxis) {
  7026. titleOffset = axis.axisTitle.getBBox()[horiz ? 'height' : 'width'];
  7027. titleMargin = pick(axisTitleOptions.margin, horiz ? 5 : 10);
  7028. titleOffsetOption = axisTitleOptions.offset;
  7029. }
  7030. // hide or show the title depending on whether showEmpty is set
  7031. axis.axisTitle[showAxis ? 'show' : 'hide']();
  7032. }
  7033. // handle automatic or user set offset
  7034. axis.offset = directionFactor * pick(options.offset, axisOffset[side]);
  7035. axis.axisTitleMargin =
  7036. pick(titleOffsetOption,
  7037. labelOffset + titleMargin +
  7038. (side !== 2 && labelOffset && directionFactor * options.labels[horiz ? 'y' : 'x'])
  7039. );
  7040. axisOffset[side] = mathMax(
  7041. axisOffset[side],
  7042. axis.axisTitleMargin + titleOffset + directionFactor * axis.offset
  7043. );
  7044. clipOffset[invertedSide] = mathMax(clipOffset[invertedSide], mathFloor(options.lineWidth / 2) * 2);
  7045. },
  7046. /**
  7047. * Get the path for the axis line
  7048. */
  7049. getLinePath: function (lineWidth) {
  7050. var chart = this.chart,
  7051. opposite = this.opposite,
  7052. offset = this.offset,
  7053. horiz = this.horiz,
  7054. lineLeft = this.left + (opposite ? this.width : 0) + offset,
  7055. lineTop = chart.chartHeight - this.bottom - (opposite ? this.height : 0) + offset;
  7056. if (opposite) {
  7057. lineWidth *= -1; // crispify the other way - #1480, #1687
  7058. }
  7059. return chart.renderer.crispLine([
  7060. M,
  7061. horiz ?
  7062. this.left :
  7063. lineLeft,
  7064. horiz ?
  7065. lineTop :
  7066. this.top,
  7067. L,
  7068. horiz ?
  7069. chart.chartWidth - this.right :
  7070. lineLeft,
  7071. horiz ?
  7072. lineTop :
  7073. chart.chartHeight - this.bottom
  7074. ], lineWidth);
  7075. },
  7076. /**
  7077. * Position the title
  7078. */
  7079. getTitlePosition: function () {
  7080. // compute anchor points for each of the title align options
  7081. var horiz = this.horiz,
  7082. axisLeft = this.left,
  7083. axisTop = this.top,
  7084. axisLength = this.len,
  7085. axisTitleOptions = this.options.title,
  7086. margin = horiz ? axisLeft : axisTop,
  7087. opposite = this.opposite,
  7088. offset = this.offset,
  7089. fontSize = pInt(axisTitleOptions.style.fontSize || 12),
  7090. // the position in the length direction of the axis
  7091. alongAxis = {
  7092. low: margin + (horiz ? 0 : axisLength),
  7093. middle: margin + axisLength / 2,
  7094. high: margin + (horiz ? axisLength : 0)
  7095. }[axisTitleOptions.align],
  7096. // the position in the perpendicular direction of the axis
  7097. offAxis = (horiz ? axisTop + this.height : axisLeft) +
  7098. (horiz ? 1 : -1) * // horizontal axis reverses the margin
  7099. (opposite ? -1 : 1) * // so does opposite axes
  7100. this.axisTitleMargin +
  7101. (this.side === 2 ? fontSize : 0);
  7102. return {
  7103. x: horiz ?
  7104. alongAxis :
  7105. offAxis + (opposite ? this.width : 0) + offset +
  7106. (axisTitleOptions.x || 0), // x
  7107. y: horiz ?
  7108. offAxis - (opposite ? this.height : 0) + offset :
  7109. alongAxis + (axisTitleOptions.y || 0) // y
  7110. };
  7111. },
  7112. /**
  7113. * Render the axis
  7114. */
  7115. render: function () {
  7116. var axis = this,
  7117. chart = axis.chart,
  7118. renderer = chart.renderer,
  7119. options = axis.options,
  7120. isLog = axis.isLog,
  7121. isLinked = axis.isLinked,
  7122. tickPositions = axis.tickPositions,
  7123. axisTitle = axis.axisTitle,
  7124. stacks = axis.stacks,
  7125. ticks = axis.ticks,
  7126. minorTicks = axis.minorTicks,
  7127. alternateBands = axis.alternateBands,
  7128. stackLabelOptions = options.stackLabels,
  7129. alternateGridColor = options.alternateGridColor,
  7130. tickmarkOffset = axis.tickmarkOffset,
  7131. lineWidth = options.lineWidth,
  7132. linePath,
  7133. hasRendered = chart.hasRendered,
  7134. slideInTicks = hasRendered && defined(axis.oldMin) && !isNaN(axis.oldMin),
  7135. hasData = axis.hasData,
  7136. showAxis = axis.showAxis,
  7137. from,
  7138. to;
  7139. // Mark all elements inActive before we go over and mark the active ones
  7140. each([ticks, minorTicks, alternateBands], function (coll) {
  7141. var pos;
  7142. for (pos in coll) {
  7143. coll[pos].isActive = false;
  7144. }
  7145. });
  7146. // If the series has data draw the ticks. Else only the line and title
  7147. if (hasData || isLinked) {
  7148. // minor ticks
  7149. if (axis.minorTickInterval && !axis.categories) {
  7150. each(axis.getMinorTickPositions(), function (pos) {
  7151. if (!minorTicks[pos]) {
  7152. minorTicks[pos] = new Tick(axis, pos, 'minor');
  7153. }
  7154. // render new ticks in old position
  7155. if (slideInTicks && minorTicks[pos].isNew) {
  7156. minorTicks[pos].render(null, true);
  7157. }
  7158. minorTicks[pos].render(null, false, 1);
  7159. });
  7160. }
  7161. // Major ticks. Pull out the first item and render it last so that
  7162. // we can get the position of the neighbour label. #808.
  7163. if (tickPositions.length) { // #1300
  7164. each(tickPositions.slice(1).concat([tickPositions[0]]), function (pos, i) {
  7165. // Reorganize the indices
  7166. i = (i === tickPositions.length - 1) ? 0 : i + 1;
  7167. // linked axes need an extra check to find out if
  7168. if (!isLinked || (pos >= axis.min && pos <= axis.max)) {
  7169. if (!ticks[pos]) {
  7170. ticks[pos] = new Tick(axis, pos);
  7171. }
  7172. // render new ticks in old position
  7173. if (slideInTicks && ticks[pos].isNew) {
  7174. ticks[pos].render(i, true);
  7175. }
  7176. ticks[pos].render(i, false, 1);
  7177. }
  7178. });
  7179. // In a categorized axis, the tick marks are displayed between labels. So
  7180. // we need to add a tick mark and grid line at the left edge of the X axis.
  7181. if (tickmarkOffset && axis.min === 0) {
  7182. if (!ticks[-1]) {
  7183. ticks[-1] = new Tick(axis, -1, null, true);
  7184. }
  7185. ticks[-1].render(-1);
  7186. }
  7187. }
  7188. // alternate grid color
  7189. if (alternateGridColor) {
  7190. each(tickPositions, function (pos, i) {
  7191. if (i % 2 === 0 && pos < axis.max) {
  7192. if (!alternateBands[pos]) {
  7193. alternateBands[pos] = new PlotLineOrBand(axis);
  7194. }
  7195. from = pos + tickmarkOffset; // #949
  7196. to = tickPositions[i + 1] !== UNDEFINED ? tickPositions[i + 1] + tickmarkOffset : axis.max;
  7197. alternateBands[pos].options = {
  7198. from: isLog ? lin2log(from) : from,
  7199. to: isLog ? lin2log(to) : to,
  7200. color: alternateGridColor
  7201. };
  7202. alternateBands[pos].render();
  7203. alternateBands[pos].isActive = true;
  7204. }
  7205. });
  7206. }
  7207. // custom plot lines and bands
  7208. if (!axis._addedPlotLB) { // only first time
  7209. each((options.plotLines || []).concat(options.plotBands || []), function (plotLineOptions) {
  7210. axis.addPlotBandOrLine(plotLineOptions);
  7211. });
  7212. axis._addedPlotLB = true;
  7213. }
  7214. } // end if hasData
  7215. // Remove inactive ticks
  7216. each([ticks, minorTicks, alternateBands], function (coll) {
  7217. var pos,
  7218. i,
  7219. forDestruction = [],
  7220. delay = globalAnimation ? globalAnimation.duration || 500 : 0,
  7221. destroyInactiveItems = function () {
  7222. i = forDestruction.length;
  7223. while (i--) {
  7224. // When resizing rapidly, the same items may be destroyed in different timeouts,
  7225. // or the may be reactivated
  7226. if (coll[forDestruction[i]] && !coll[forDestruction[i]].isActive) {
  7227. coll[forDestruction[i]].destroy();
  7228. delete coll[forDestruction[i]];
  7229. }
  7230. }
  7231. };
  7232. for (pos in coll) {
  7233. if (!coll[pos].isActive) {
  7234. // Render to zero opacity
  7235. coll[pos].render(pos, false, 0);
  7236. coll[pos].isActive = false;
  7237. forDestruction.push(pos);
  7238. }
  7239. }
  7240. // When the objects are finished fading out, destroy them
  7241. if (coll === alternateBands || !chart.hasRendered || !delay) {
  7242. destroyInactiveItems();
  7243. } else if (delay) {
  7244. setTimeout(destroyInactiveItems, delay);
  7245. }
  7246. });
  7247. // Static items. As the axis group is cleared on subsequent calls
  7248. // to render, these items are added outside the group.
  7249. // axis line
  7250. if (lineWidth) {
  7251. linePath = axis.getLinePath(lineWidth);
  7252. if (!axis.axisLine) {
  7253. axis.axisLine = renderer.path(linePath)
  7254. .attr({
  7255. stroke: options.lineColor,
  7256. 'stroke-width': lineWidth,
  7257. zIndex: 7
  7258. })
  7259. .add(axis.axisGroup);
  7260. } else {
  7261. axis.axisLine.animate({d: linePath});
  7262. }
  7263. // show or hide the line depending on options.showEmpty
  7264. axis.axisLine[showAxis ? 'show' : 'hide']();
  7265. }
  7266. if (axisTitle && showAxis) {
  7267. axisTitle[axisTitle.isNew ? 'attr' : 'animate'](
  7268. axis.getTitlePosition()
  7269. );
  7270. axisTitle.isNew = false;
  7271. }
  7272. // Stacked totals:
  7273. if (stackLabelOptions && stackLabelOptions.enabled) {
  7274. var stackKey, oneStack, stackCategory,
  7275. stackTotalGroup = axis.stackTotalGroup;
  7276. // Create a separate group for the stack total labels
  7277. if (!stackTotalGroup) {
  7278. axis.stackTotalGroup = stackTotalGroup =
  7279. renderer.g('stack-labels')
  7280. .attr({
  7281. visibility: VISIBLE,
  7282. zIndex: 6
  7283. })
  7284. .add();
  7285. }
  7286. // plotLeft/Top will change when y axis gets wider so we need to translate the
  7287. // stackTotalGroup at every render call. See bug #506 and #516
  7288. stackTotalGroup.translate(chart.plotLeft, chart.plotTop);
  7289. // Render each stack total
  7290. for (stackKey in stacks) {
  7291. oneStack = stacks[stackKey];
  7292. for (stackCategory in oneStack) {
  7293. oneStack[stackCategory].render(stackTotalGroup);
  7294. }
  7295. }
  7296. }
  7297. // End stacked totals
  7298. axis.isDirty = false;
  7299. },
  7300. /**
  7301. * Remove a plot band or plot line from the chart by id
  7302. * @param {Object} id
  7303. */
  7304. removePlotBandOrLine: function (id) {
  7305. var plotLinesAndBands = this.plotLinesAndBands,
  7306. options = this.options,
  7307. userOptions = this.userOptions,
  7308. i = plotLinesAndBands.length;
  7309. while (i--) {
  7310. if (plotLinesAndBands[i].id === id) {
  7311. plotLinesAndBands[i].destroy();
  7312. }
  7313. }
  7314. each([options.plotLines || [], userOptions.plotLines || [], options.plotBands || [], userOptions.plotBands || []], function (arr) {
  7315. i = arr.length;
  7316. while (i--) {
  7317. if (arr[i].id === id) {
  7318. erase(arr, arr[i]);
  7319. }
  7320. }
  7321. });
  7322. },
  7323. /**
  7324. * Update the axis title by options
  7325. */
  7326. setTitle: function (newTitleOptions, redraw) {
  7327. this.update({title: newTitleOptions}, redraw);
  7328. },
  7329. /**
  7330. * Redraw the axis to reflect changes in the data or axis extremes
  7331. */
  7332. redraw: function () {
  7333. var axis = this,
  7334. chart = axis.chart,
  7335. pointer = chart.pointer;
  7336. // hide tooltip and hover states
  7337. if (pointer.reset) {
  7338. pointer.reset(true);
  7339. }
  7340. // render the axis
  7341. axis.render();
  7342. // move plot lines and bands
  7343. each(axis.plotLinesAndBands, function (plotLine) {
  7344. plotLine.render();
  7345. });
  7346. // mark associated series as dirty and ready for redraw
  7347. each(axis.series, function (series) {
  7348. series.isDirty = true;
  7349. });
  7350. },
  7351. /**
  7352. * Build the stacks from top down
  7353. */
  7354. buildStacks: function () {
  7355. var series = this.series,
  7356. i = series.length;
  7357. if (!this.isXAxis) {
  7358. while (i--) {
  7359. series[i].setStackedPoints();
  7360. }
  7361. // Loop up again to compute percent stack
  7362. if (this.usePercentage) {
  7363. for (i = 0; i < series.length; i++) {
  7364. series[i].setPercentStacks();
  7365. }
  7366. }
  7367. }
  7368. },
  7369. /**
  7370. * Set new axis categories and optionally redraw
  7371. * @param {Array} categories
  7372. * @param {Boolean} redraw
  7373. */
  7374. setCategories: function (categories, redraw) {
  7375. this.update({categories: categories}, redraw);
  7376. },
  7377. /**
  7378. * Destroys an Axis instance.
  7379. */
  7380. destroy: function (keepEvents) {
  7381. var axis = this,
  7382. stacks = axis.stacks,
  7383. stackKey,
  7384. plotLinesAndBands = axis.plotLinesAndBands,
  7385. i;
  7386. // Remove the events
  7387. if (!keepEvents) {
  7388. removeEvent(axis);
  7389. }
  7390. // Destroy each stack total
  7391. for (stackKey in stacks) {
  7392. destroyObjectProperties(stacks[stackKey]);
  7393. stacks[stackKey] = null;
  7394. }
  7395. // Destroy collections
  7396. each([axis.ticks, axis.minorTicks, axis.alternateBands], function (coll) {
  7397. destroyObjectProperties(coll);
  7398. });
  7399. i = plotLinesAndBands.length;
  7400. while (i--) { // #1975
  7401. plotLinesAndBands[i].destroy();
  7402. }
  7403. // Destroy local variables
  7404. each(['stackTotalGroup', 'axisLine', 'axisGroup', 'gridGroup', 'labelGroup', 'axisTitle'], function (prop) {
  7405. if (axis[prop]) {
  7406. axis[prop] = axis[prop].destroy();
  7407. }
  7408. });
  7409. }
  7410. }; // end Axis
  7411. /**
  7412. * The tooltip object
  7413. * @param {Object} chart The chart instance
  7414. * @param {Object} options Tooltip options
  7415. */
  7416. function Tooltip() {
  7417. this.init.apply(this, arguments);
  7418. }
  7419. Tooltip.prototype = {
  7420. init: function (chart, options) {
  7421. var borderWidth = options.borderWidth,
  7422. style = options.style,
  7423. padding = pInt(style.padding);
  7424. // Save the chart and options
  7425. this.chart = chart;
  7426. this.options = options;
  7427. // Keep track of the current series
  7428. //this.currentSeries = UNDEFINED;
  7429. // List of crosshairs
  7430. this.crosshairs = [];
  7431. // Current values of x and y when animating
  7432. this.now = {x: 0, y: 0};
  7433. // The tooltip is initially hidden
  7434. this.isHidden = true;
  7435. // create the label
  7436. this.label = chart.renderer.label('', 0, 0, options.shape, null, null, options.useHTML, null, 'tooltip')
  7437. .attr({
  7438. padding: padding,
  7439. fill: options.backgroundColor,
  7440. 'stroke-width': borderWidth,
  7441. r: options.borderRadius,
  7442. zIndex: 8
  7443. })
  7444. .css(style)
  7445. .css({padding: 0}) // Remove it from VML, the padding is applied as an attribute instead (#1117)
  7446. .add()
  7447. .attr({y: -999}); // #2301
  7448. // When using canVG the shadow shows up as a gray circle
  7449. // even if the tooltip is hidden.
  7450. if (!useCanVG) {
  7451. this.label.shadow(options.shadow);
  7452. }
  7453. // Public property for getting the shared state.
  7454. this.shared = options.shared;
  7455. },
  7456. /**
  7457. * Destroy the tooltip and its elements.
  7458. */
  7459. destroy: function () {
  7460. each(this.crosshairs, function (crosshair) {
  7461. if (crosshair) {
  7462. crosshair.destroy();
  7463. }
  7464. });
  7465. // Destroy and clear local variables
  7466. if (this.label) {
  7467. this.label = this.label.destroy();
  7468. }
  7469. clearTimeout(this.hideTimer);
  7470. clearTimeout(this.tooltipTimeout);
  7471. },
  7472. /**
  7473. * Provide a soft movement for the tooltip
  7474. *
  7475. * @param {Number} x
  7476. * @param {Number} y
  7477. * @private
  7478. */
  7479. move: function (x, y, anchorX, anchorY) {
  7480. var tooltip = this,
  7481. now = tooltip.now,
  7482. animate = tooltip.options.animation !== false && !tooltip.isHidden;
  7483. // get intermediate values for animation
  7484. extend(now, {
  7485. x: animate ? (2 * now.x + x) / 3 : x,
  7486. y: animate ? (now.y + y) / 2 : y,
  7487. anchorX: animate ? (2 * now.anchorX + anchorX) / 3 : anchorX,
  7488. anchorY: animate ? (now.anchorY + anchorY) / 2 : anchorY
  7489. });
  7490. // move to the intermediate value
  7491. tooltip.label.attr(now);
  7492. // run on next tick of the mouse tracker
  7493. if (animate && (mathAbs(x - now.x) > 1 || mathAbs(y - now.y) > 1)) {
  7494. // never allow two timeouts
  7495. clearTimeout(this.tooltipTimeout);
  7496. // set the fixed interval ticking for the smooth tooltip
  7497. this.tooltipTimeout = setTimeout(function () {
  7498. // The interval function may still be running during destroy, so check that the chart is really there before calling.
  7499. if (tooltip) {
  7500. tooltip.move(x, y, anchorX, anchorY);
  7501. }
  7502. }, 32);
  7503. }
  7504. },
  7505. /**
  7506. * Hide the tooltip
  7507. */
  7508. hide: function () {
  7509. var tooltip = this,
  7510. hoverPoints;
  7511. clearTimeout(this.hideTimer); // disallow duplicate timers (#1728, #1766)
  7512. if (!this.isHidden) {
  7513. hoverPoints = this.chart.hoverPoints;
  7514. this.hideTimer = setTimeout(function () {
  7515. tooltip.label.fadeOut();
  7516. tooltip.isHidden = true;
  7517. }, pick(this.options.hideDelay, 500));
  7518. // hide previous hoverPoints and set new
  7519. if (hoverPoints) {
  7520. each(hoverPoints, function (point) {
  7521. point.setState();
  7522. });
  7523. }
  7524. this.chart.hoverPoints = null;
  7525. }
  7526. },
  7527. /**
  7528. * Hide the crosshairs
  7529. */
  7530. hideCrosshairs: function () {
  7531. each(this.crosshairs, function (crosshair) {
  7532. if (crosshair) {
  7533. crosshair.hide();
  7534. }
  7535. });
  7536. },
  7537. /**
  7538. * Extendable method to get the anchor position of the tooltip
  7539. * from a point or set of points
  7540. */
  7541. getAnchor: function (points, mouseEvent) {
  7542. var ret,
  7543. chart = this.chart,
  7544. inverted = chart.inverted,
  7545. plotTop = chart.plotTop,
  7546. plotX = 0,
  7547. plotY = 0,
  7548. yAxis;
  7549. points = splat(points);
  7550. // Pie uses a special tooltipPos
  7551. ret = points[0].tooltipPos;
  7552. // When tooltip follows mouse, relate the position to the mouse
  7553. if (this.followPointer && mouseEvent) {
  7554. if (mouseEvent.chartX === UNDEFINED) {
  7555. mouseEvent = chart.pointer.normalize(mouseEvent);
  7556. }
  7557. ret = [
  7558. mouseEvent.chartX - chart.plotLeft,
  7559. mouseEvent.chartY - plotTop
  7560. ];
  7561. }
  7562. // When shared, use the average position
  7563. if (!ret) {
  7564. each(points, function (point) {
  7565. yAxis = point.series.yAxis;
  7566. plotX += point.plotX;
  7567. plotY += (point.plotLow ? (point.plotLow + point.plotHigh) / 2 : point.plotY) +
  7568. (!inverted && yAxis ? yAxis.top - plotTop : 0); // #1151
  7569. });
  7570. plotX /= points.length;
  7571. plotY /= points.length;
  7572. ret = [
  7573. inverted ? chart.plotWidth - plotY : plotX,
  7574. this.shared && !inverted && points.length > 1 && mouseEvent ?
  7575. mouseEvent.chartY - plotTop : // place shared tooltip next to the mouse (#424)
  7576. inverted ? chart.plotHeight - plotX : plotY
  7577. ];
  7578. }
  7579. return map(ret, mathRound);
  7580. },
  7581. /**
  7582. * Place the tooltip in a chart without spilling over
  7583. * and not covering the point it self.
  7584. */
  7585. getPosition: function (boxWidth, boxHeight, point) {
  7586. // Set up the variables
  7587. var chart = this.chart,
  7588. plotLeft = chart.plotLeft,
  7589. plotTop = chart.plotTop,
  7590. plotWidth = chart.plotWidth,
  7591. plotHeight = chart.plotHeight,
  7592. distance = pick(this.options.distance, 12),
  7593. pointX = point.plotX,
  7594. pointY = point.plotY,
  7595. x = pointX + plotLeft + (chart.inverted ? distance : -boxWidth - distance),
  7596. y = pointY - boxHeight + plotTop + 15, // 15 means the point is 15 pixels up from the bottom of the tooltip
  7597. alignedRight;
  7598. // It is too far to the left, adjust it
  7599. if (x < 7) {
  7600. x = plotLeft + mathMax(pointX, 0) + distance;
  7601. }
  7602. // Test to see if the tooltip is too far to the right,
  7603. // if it is, move it back to be inside and then up to not cover the point.
  7604. if ((x + boxWidth) > (plotLeft + plotWidth)) {
  7605. x -= (x + boxWidth) - (plotLeft + plotWidth);
  7606. y = pointY - boxHeight + plotTop - distance;
  7607. alignedRight = true;
  7608. }
  7609. // If it is now above the plot area, align it to the top of the plot area
  7610. if (y < plotTop + 5) {
  7611. y = plotTop + 5;
  7612. // If the tooltip is still covering the point, move it below instead
  7613. if (alignedRight && pointY >= y && pointY <= (y + boxHeight)) {
  7614. y = pointY + plotTop + distance; // below
  7615. }
  7616. }
  7617. // Now if the tooltip is below the chart, move it up. It's better to cover the
  7618. // point than to disappear outside the chart. #834.
  7619. if (y + boxHeight > plotTop + plotHeight) {
  7620. y = mathMax(plotTop, plotTop + plotHeight - boxHeight - distance); // below
  7621. }
  7622. return {x: x, y: y};
  7623. },
  7624. /**
  7625. * In case no user defined formatter is given, this will be used. Note that the context
  7626. * here is an object holding point, series, x, y etc.
  7627. */
  7628. defaultFormatter: function (tooltip) {
  7629. var items = this.points || splat(this),
  7630. series = items[0].series,
  7631. s;
  7632. // build the header
  7633. s = [series.tooltipHeaderFormatter(items[0])];
  7634. // build the values
  7635. each(items, function (item) {
  7636. series = item.series;
  7637. s.push((series.tooltipFormatter && series.tooltipFormatter(item)) ||
  7638. item.point.tooltipFormatter(series.tooltipOptions.pointFormat));
  7639. });
  7640. // footer
  7641. s.push(tooltip.options.footerFormat || '');
  7642. return s.join('');
  7643. },
  7644. /**
  7645. * Refresh the tooltip's text and position.
  7646. * @param {Object} point
  7647. */
  7648. refresh: function (point, mouseEvent) {
  7649. var tooltip = this,
  7650. chart = tooltip.chart,
  7651. label = tooltip.label,
  7652. options = tooltip.options,
  7653. x,
  7654. y,
  7655. anchor,
  7656. textConfig = {},
  7657. text,
  7658. pointConfig = [],
  7659. formatter = options.formatter || tooltip.defaultFormatter,
  7660. hoverPoints = chart.hoverPoints,
  7661. borderColor,
  7662. crosshairsOptions = options.crosshairs,
  7663. shared = tooltip.shared,
  7664. currentSeries;
  7665. clearTimeout(this.hideTimer);
  7666. // get the reference point coordinates (pie charts use tooltipPos)
  7667. tooltip.followPointer = splat(point)[0].series.tooltipOptions.followPointer;
  7668. anchor = tooltip.getAnchor(point, mouseEvent);
  7669. x = anchor[0];
  7670. y = anchor[1];
  7671. // shared tooltip, array is sent over
  7672. if (shared && !(point.series && point.series.noSharedTooltip)) {
  7673. // hide previous hoverPoints and set new
  7674. chart.hoverPoints = point;
  7675. if (hoverPoints) {
  7676. each(hoverPoints, function (point) {
  7677. point.setState();
  7678. });
  7679. }
  7680. each(point, function (item) {
  7681. item.setState(HOVER_STATE);
  7682. pointConfig.push(item.getLabelConfig());
  7683. });
  7684. textConfig = {
  7685. x: point[0].category,
  7686. y: point[0].y
  7687. };
  7688. textConfig.points = pointConfig;
  7689. point = point[0];
  7690. // single point tooltip
  7691. } else {
  7692. textConfig = point.getLabelConfig();
  7693. }
  7694. text = formatter.call(textConfig, tooltip);
  7695. // register the current series
  7696. currentSeries = point.series;
  7697. // update the inner HTML
  7698. if (text === false) {
  7699. this.hide();
  7700. } else {
  7701. // show it
  7702. if (tooltip.isHidden) {
  7703. stop(label);
  7704. label.attr('opacity', 1).show();
  7705. }
  7706. // update text
  7707. label.attr({
  7708. text: text
  7709. });
  7710. // set the stroke color of the box
  7711. borderColor = options.borderColor || point.color || currentSeries.color || '#606060';
  7712. label.attr({
  7713. stroke: borderColor
  7714. });
  7715. tooltip.updatePosition({plotX: x, plotY: y});
  7716. this.isHidden = false;
  7717. }
  7718. // crosshairs
  7719. if (crosshairsOptions) {
  7720. crosshairsOptions = splat(crosshairsOptions); // [x, y]
  7721. var path,
  7722. i = crosshairsOptions.length,
  7723. attribs,
  7724. axis,
  7725. val,
  7726. series;
  7727. while (i--) {
  7728. series = point.series;
  7729. axis = series[i ? 'yAxis' : 'xAxis'];
  7730. if (crosshairsOptions[i] && axis) {
  7731. val = i ? pick(point.stackY, point.y) : point.x; // #814
  7732. if (axis.isLog) { // #1671
  7733. val = log2lin(val);
  7734. }
  7735. if (i === 1 && series.modifyValue) { // #1205, #2316
  7736. val = series.modifyValue(val);
  7737. }
  7738. path = axis.getPlotLinePath(
  7739. val,
  7740. 1
  7741. );
  7742. if (tooltip.crosshairs[i]) {
  7743. tooltip.crosshairs[i].attr({d: path, visibility: VISIBLE});
  7744. } else {
  7745. attribs = {
  7746. 'stroke-width': crosshairsOptions[i].width || 1,
  7747. stroke: crosshairsOptions[i].color || '#C0C0C0',
  7748. zIndex: crosshairsOptions[i].zIndex || 2
  7749. };
  7750. if (crosshairsOptions[i].dashStyle) {
  7751. attribs.dashstyle = crosshairsOptions[i].dashStyle;
  7752. }
  7753. tooltip.crosshairs[i] = chart.renderer.path(path)
  7754. .attr(attribs)
  7755. .add();
  7756. }
  7757. }
  7758. }
  7759. }
  7760. fireEvent(chart, 'tooltipRefresh', {
  7761. text: text,
  7762. x: x + chart.plotLeft,
  7763. y: y + chart.plotTop,
  7764. borderColor: borderColor
  7765. });
  7766. },
  7767. /**
  7768. * Find the new position and perform the move
  7769. */
  7770. updatePosition: function (point) {
  7771. var chart = this.chart,
  7772. label = this.label,
  7773. pos = (this.options.positioner || this.getPosition).call(
  7774. this,
  7775. label.width,
  7776. label.height,
  7777. point
  7778. );
  7779. // do the move
  7780. this.move(
  7781. mathRound(pos.x),
  7782. mathRound(pos.y),
  7783. point.plotX + chart.plotLeft,
  7784. point.plotY + chart.plotTop
  7785. );
  7786. }
  7787. };
  7788. /**
  7789. * The mouse tracker object. All methods starting with "on" are primary DOM event handlers.
  7790. * Subsequent methods should be named differently from what they are doing.
  7791. * @param {Object} chart The Chart instance
  7792. * @param {Object} options The root options object
  7793. */
  7794. function Pointer(chart, options) {
  7795. this.init(chart, options);
  7796. }
  7797. Pointer.prototype = {
  7798. /**
  7799. * Initialize Pointer
  7800. */
  7801. init: function (chart, options) {
  7802. var chartOptions = options.chart,
  7803. chartEvents = chartOptions.events,
  7804. zoomType = useCanVG ? '' : chartOptions.zoomType,
  7805. inverted = chart.inverted,
  7806. zoomX,
  7807. zoomY;
  7808. // Store references
  7809. this.options = options;
  7810. this.chart = chart;
  7811. // Zoom status
  7812. this.zoomX = zoomX = /x/.test(zoomType);
  7813. this.zoomY = zoomY = /y/.test(zoomType);
  7814. this.zoomHor = (zoomX && !inverted) || (zoomY && inverted);
  7815. this.zoomVert = (zoomY && !inverted) || (zoomX && inverted);
  7816. // Do we need to handle click on a touch device?
  7817. this.runChartClick = chartEvents && !!chartEvents.click;
  7818. this.pinchDown = [];
  7819. this.lastValidTouch = {};
  7820. if (options.tooltip.enabled) {
  7821. chart.tooltip = new Tooltip(chart, options.tooltip);
  7822. }
  7823. this.setDOMEvents();
  7824. },
  7825. /**
  7826. * Add crossbrowser support for chartX and chartY
  7827. * @param {Object} e The event object in standard browsers
  7828. */
  7829. normalize: function (e, chartPosition) {
  7830. var chartX,
  7831. chartY,
  7832. ePos;
  7833. // common IE normalizing
  7834. e = e || win.event;
  7835. if (!e.target) {
  7836. e.target = e.srcElement;
  7837. }
  7838. // Framework specific normalizing (#1165)
  7839. e = washMouseEvent(e);
  7840. // iOS
  7841. ePos = e.touches ? e.touches.item(0) : e;
  7842. // Get mouse position
  7843. if (!chartPosition) {
  7844. this.chartPosition = chartPosition = offset(this.chart.container);
  7845. }
  7846. // chartX and chartY
  7847. if (ePos.pageX === UNDEFINED) { // IE < 9. #886.
  7848. chartX = mathMax(e.x, e.clientX - chartPosition.left); // #2005, #2129: the second case is
  7849. // for IE10 quirks mode within framesets
  7850. chartY = e.y;
  7851. } else {
  7852. chartX = ePos.pageX - chartPosition.left;
  7853. chartY = ePos.pageY - chartPosition.top;
  7854. }
  7855. return extend(e, {
  7856. chartX: mathRound(chartX),
  7857. chartY: mathRound(chartY)
  7858. });
  7859. },
  7860. /**
  7861. * Get the click position in terms of axis values.
  7862. *
  7863. * @param {Object} e A pointer event
  7864. */
  7865. getCoordinates: function (e) {
  7866. var coordinates = {
  7867. xAxis: [],
  7868. yAxis: []
  7869. };
  7870. each(this.chart.axes, function (axis) {
  7871. coordinates[axis.isXAxis ? 'xAxis' : 'yAxis'].push({
  7872. axis: axis,
  7873. value: axis.toValue(e[axis.horiz ? 'chartX' : 'chartY'])
  7874. });
  7875. });
  7876. return coordinates;
  7877. },
  7878. /**
  7879. * Return the index in the tooltipPoints array, corresponding to pixel position in
  7880. * the plot area.
  7881. */
  7882. getIndex: function (e) {
  7883. var chart = this.chart;
  7884. return chart.inverted ?
  7885. chart.plotHeight + chart.plotTop - e.chartY :
  7886. e.chartX - chart.plotLeft;
  7887. },
  7888. /**
  7889. * With line type charts with a single tracker, get the point closest to the mouse.
  7890. * Run Point.onMouseOver and display tooltip for the point or points.
  7891. */
  7892. runPointActions: function (e) {
  7893. var pointer = this,
  7894. chart = pointer.chart,
  7895. series = chart.series,
  7896. tooltip = chart.tooltip,
  7897. point,
  7898. points,
  7899. hoverPoint = chart.hoverPoint,
  7900. hoverSeries = chart.hoverSeries,
  7901. i,
  7902. j,
  7903. distance = chart.chartWidth,
  7904. index = pointer.getIndex(e),
  7905. anchor;
  7906. // shared tooltip
  7907. if (tooltip && pointer.options.tooltip.shared && !(hoverSeries && hoverSeries.noSharedTooltip)) {
  7908. points = [];
  7909. // loop over all series and find the ones with points closest to the mouse
  7910. i = series.length;
  7911. for (j = 0; j < i; j++) {
  7912. if (series[j].visible &&
  7913. series[j].options.enableMouseTracking !== false &&
  7914. !series[j].noSharedTooltip && series[j].tooltipPoints.length) {
  7915. point = series[j].tooltipPoints[index];
  7916. if (point && point.series) { // not a dummy point, #1544
  7917. point._dist = mathAbs(index - point.clientX);
  7918. distance = mathMin(distance, point._dist);
  7919. points.push(point);
  7920. }
  7921. }
  7922. }
  7923. // remove furthest points
  7924. i = points.length;
  7925. while (i--) {
  7926. if (points[i]._dist > distance) {
  7927. points.splice(i, 1);
  7928. }
  7929. }
  7930. // refresh the tooltip if necessary
  7931. if (points.length && (points[0].clientX !== pointer.hoverX)) {
  7932. tooltip.refresh(points, e);
  7933. pointer.hoverX = points[0].clientX;
  7934. }
  7935. }
  7936. // separate tooltip and general mouse events
  7937. if (hoverSeries && hoverSeries.tracker) { // only use for line-type series with common tracker
  7938. // get the point
  7939. point = hoverSeries.tooltipPoints[index];
  7940. // a new point is hovered, refresh the tooltip
  7941. if (point && point !== hoverPoint) {
  7942. // trigger the events
  7943. point.onMouseOver(e);
  7944. }
  7945. } else if (tooltip && tooltip.followPointer && !tooltip.isHidden) {
  7946. anchor = tooltip.getAnchor([{}], e);
  7947. tooltip.updatePosition({plotX: anchor[0], plotY: anchor[1]});
  7948. }
  7949. },
  7950. /**
  7951. * Reset the tracking by hiding the tooltip, the hover series state and the hover point
  7952. *
  7953. * @param allowMove {Boolean} Instead of destroying the tooltip altogether, allow moving it if possible
  7954. */
  7955. reset: function (allowMove) {
  7956. var pointer = this,
  7957. chart = pointer.chart,
  7958. hoverSeries = chart.hoverSeries,
  7959. hoverPoint = chart.hoverPoint,
  7960. tooltip = chart.tooltip,
  7961. tooltipPoints = tooltip && tooltip.shared ? chart.hoverPoints : hoverPoint;
  7962. // Narrow in allowMove
  7963. allowMove = allowMove && tooltip && tooltipPoints;
  7964. // Check if the points have moved outside the plot area, #1003
  7965. if (allowMove && splat(tooltipPoints)[0].plotX === UNDEFINED) {
  7966. allowMove = false;
  7967. }
  7968. // Just move the tooltip, #349
  7969. if (allowMove) {
  7970. tooltip.refresh(tooltipPoints);
  7971. // Full reset
  7972. } else {
  7973. if (hoverPoint) {
  7974. hoverPoint.onMouseOut();
  7975. }
  7976. if (hoverSeries) {
  7977. hoverSeries.onMouseOut();
  7978. }
  7979. if (tooltip) {
  7980. tooltip.hide();
  7981. tooltip.hideCrosshairs();
  7982. }
  7983. pointer.hoverX = null;
  7984. }
  7985. },
  7986. /**
  7987. * Scale series groups to a certain scale and translation
  7988. */
  7989. scaleGroups: function (attribs, clip) {
  7990. var chart = this.chart,
  7991. seriesAttribs;
  7992. // Scale each series
  7993. each(chart.series, function (series) {
  7994. seriesAttribs = attribs || series.getPlotBox(); // #1701
  7995. if (series.xAxis && series.xAxis.zoomEnabled) {
  7996. series.group.attr(seriesAttribs);
  7997. if (series.markerGroup) {
  7998. series.markerGroup.attr(seriesAttribs);
  7999. series.markerGroup.clip(clip ? chart.clipRect : null);
  8000. }
  8001. if (series.dataLabelsGroup) {
  8002. series.dataLabelsGroup.attr(seriesAttribs);
  8003. }
  8004. }
  8005. });
  8006. // Clip
  8007. chart.clipRect.attr(clip || chart.clipBox);
  8008. },
  8009. /**
  8010. * Run translation operations for each direction (horizontal and vertical) independently
  8011. */
  8012. pinchTranslateDirection: function (horiz, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch) {
  8013. var chart = this.chart,
  8014. xy = horiz ? 'x' : 'y',
  8015. XY = horiz ? 'X' : 'Y',
  8016. sChartXY = 'chart' + XY,
  8017. wh = horiz ? 'width' : 'height',
  8018. plotLeftTop = chart['plot' + (horiz ? 'Left' : 'Top')],
  8019. selectionWH,
  8020. selectionXY,
  8021. clipXY,
  8022. scale = 1,
  8023. inverted = chart.inverted,
  8024. bounds = chart.bounds[horiz ? 'h' : 'v'],
  8025. singleTouch = pinchDown.length === 1,
  8026. touch0Start = pinchDown[0][sChartXY],
  8027. touch0Now = touches[0][sChartXY],
  8028. touch1Start = !singleTouch && pinchDown[1][sChartXY],
  8029. touch1Now = !singleTouch && touches[1][sChartXY],
  8030. outOfBounds,
  8031. transformScale,
  8032. scaleKey,
  8033. setScale = function () {
  8034. if (!singleTouch && mathAbs(touch0Start - touch1Start) > 20) { // Don't zoom if fingers are too close on this axis
  8035. scale = mathAbs(touch0Now - touch1Now) / mathAbs(touch0Start - touch1Start);
  8036. }
  8037. clipXY = ((plotLeftTop - touch0Now) / scale) + touch0Start;
  8038. selectionWH = chart['plot' + (horiz ? 'Width' : 'Height')] / scale;
  8039. };
  8040. // Set the scale, first pass
  8041. setScale();
  8042. selectionXY = clipXY; // the clip position (x or y) is altered if out of bounds, the selection position is not
  8043. // Out of bounds
  8044. if (selectionXY < bounds.min) {
  8045. selectionXY = bounds.min;
  8046. outOfBounds = true;
  8047. } else if (selectionXY + selectionWH > bounds.max) {
  8048. selectionXY = bounds.max - selectionWH;
  8049. outOfBounds = true;
  8050. }
  8051. // Is the chart dragged off its bounds, determined by dataMin and dataMax?
  8052. if (outOfBounds) {
  8053. // Modify the touchNow position in order to create an elastic drag movement. This indicates
  8054. // to the user that the chart is responsive but can't be dragged further.
  8055. touch0Now -= 0.8 * (touch0Now - lastValidTouch[xy][0]);
  8056. if (!singleTouch) {
  8057. touch1Now -= 0.8 * (touch1Now - lastValidTouch[xy][1]);
  8058. }
  8059. // Set the scale, second pass to adapt to the modified touchNow positions
  8060. setScale();
  8061. } else {
  8062. lastValidTouch[xy] = [touch0Now, touch1Now];
  8063. }
  8064. // Set geometry for clipping, selection and transformation
  8065. if (!inverted) { // TODO: implement clipping for inverted charts
  8066. clip[xy] = clipXY - plotLeftTop;
  8067. clip[wh] = selectionWH;
  8068. }
  8069. scaleKey = inverted ? (horiz ? 'scaleY' : 'scaleX') : 'scale' + XY;
  8070. transformScale = inverted ? 1 / scale : scale;
  8071. selectionMarker[wh] = selectionWH;
  8072. selectionMarker[xy] = selectionXY;
  8073. transform[scaleKey] = scale;
  8074. transform['translate' + XY] = (transformScale * plotLeftTop) + (touch0Now - (transformScale * touch0Start));
  8075. },
  8076. /**
  8077. * Handle touch events with two touches
  8078. */
  8079. pinch: function (e) {
  8080. var self = this,
  8081. chart = self.chart,
  8082. pinchDown = self.pinchDown,
  8083. followTouchMove = chart.tooltip && chart.tooltip.options.followTouchMove,
  8084. touches = e.touches,
  8085. touchesLength = touches.length,
  8086. lastValidTouch = self.lastValidTouch,
  8087. zoomHor = self.zoomHor || self.pinchHor,
  8088. zoomVert = self.zoomVert || self.pinchVert,
  8089. hasZoom = zoomHor || zoomVert,
  8090. selectionMarker = self.selectionMarker,
  8091. transform = {},
  8092. fireClickEvent = touchesLength === 1 && ((self.inClass(e.target, PREFIX + 'tracker') &&
  8093. chart.runTrackerClick) || chart.runChartClick),
  8094. clip = {};
  8095. // On touch devices, only proceed to trigger click if a handler is defined
  8096. if ((hasZoom || followTouchMove) && !fireClickEvent) {
  8097. e.preventDefault();
  8098. }
  8099. // Normalize each touch
  8100. map(touches, function (e) {
  8101. return self.normalize(e);
  8102. });
  8103. // Register the touch start position
  8104. if (e.type === 'touchstart') {
  8105. each(touches, function (e, i) {
  8106. pinchDown[i] = {chartX: e.chartX, chartY: e.chartY};
  8107. });
  8108. lastValidTouch.x = [pinchDown[0].chartX, pinchDown[1] && pinchDown[1].chartX];
  8109. lastValidTouch.y = [pinchDown[0].chartY, pinchDown[1] && pinchDown[1].chartY];
  8110. // Identify the data bounds in pixels
  8111. each(chart.axes, function (axis) {
  8112. if (axis.zoomEnabled) {
  8113. var bounds = chart.bounds[axis.horiz ? 'h' : 'v'],
  8114. minPixelPadding = axis.minPixelPadding,
  8115. min = axis.toPixels(axis.dataMin),
  8116. max = axis.toPixels(axis.dataMax),
  8117. absMin = mathMin(min, max),
  8118. absMax = mathMax(min, max);
  8119. // Store the bounds for use in the touchmove handler
  8120. bounds.min = mathMin(axis.pos, absMin - minPixelPadding);
  8121. bounds.max = mathMax(axis.pos + axis.len, absMax + minPixelPadding);
  8122. }
  8123. });
  8124. // Event type is touchmove, handle panning and pinching
  8125. } else if (pinchDown.length) { // can be 0 when releasing, if touchend fires first
  8126. // Set the marker
  8127. if (!selectionMarker) {
  8128. self.selectionMarker = selectionMarker = extend({
  8129. destroy: noop
  8130. }, chart.plotBox);
  8131. }
  8132. if (zoomHor) {
  8133. self.pinchTranslateDirection(true, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch);
  8134. }
  8135. if (zoomVert) {
  8136. self.pinchTranslateDirection(false, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch);
  8137. }
  8138. self.hasPinched = hasZoom;
  8139. // Scale and translate the groups to provide visual feedback during pinching
  8140. self.scaleGroups(transform, clip);
  8141. // Optionally move the tooltip on touchmove
  8142. if (!hasZoom && followTouchMove && touchesLength === 1) {
  8143. this.runPointActions(self.normalize(e));
  8144. }
  8145. }
  8146. },
  8147. /**
  8148. * Start a drag operation
  8149. */
  8150. dragStart: function (e) {
  8151. var chart = this.chart;
  8152. // Record the start position
  8153. chart.mouseIsDown = e.type;
  8154. chart.cancelClick = false;
  8155. chart.mouseDownX = this.mouseDownX = e.chartX;
  8156. chart.mouseDownY = this.mouseDownY = e.chartY;
  8157. },
  8158. /**
  8159. * Perform a drag operation in response to a mousemove event while the mouse is down
  8160. */
  8161. drag: function (e) {
  8162. var chart = this.chart,
  8163. chartOptions = chart.options.chart,
  8164. chartX = e.chartX,
  8165. chartY = e.chartY,
  8166. zoomHor = this.zoomHor,
  8167. zoomVert = this.zoomVert,
  8168. plotLeft = chart.plotLeft,
  8169. plotTop = chart.plotTop,
  8170. plotWidth = chart.plotWidth,
  8171. plotHeight = chart.plotHeight,
  8172. clickedInside,
  8173. size,
  8174. mouseDownX = this.mouseDownX,
  8175. mouseDownY = this.mouseDownY;
  8176. // If the mouse is outside the plot area, adjust to cooordinates
  8177. // inside to prevent the selection marker from going outside
  8178. if (chartX < plotLeft) {
  8179. chartX = plotLeft;
  8180. } else if (chartX > plotLeft + plotWidth) {
  8181. chartX = plotLeft + plotWidth;
  8182. }
  8183. if (chartY < plotTop) {
  8184. chartY = plotTop;
  8185. } else if (chartY > plotTop + plotHeight) {
  8186. chartY = plotTop + plotHeight;
  8187. }
  8188. // determine if the mouse has moved more than 10px
  8189. this.hasDragged = Math.sqrt(
  8190. Math.pow(mouseDownX - chartX, 2) +
  8191. Math.pow(mouseDownY - chartY, 2)
  8192. );
  8193. if (this.hasDragged > 10) {
  8194. clickedInside = chart.isInsidePlot(mouseDownX - plotLeft, mouseDownY - plotTop);
  8195. // make a selection
  8196. if (chart.hasCartesianSeries && (this.zoomX || this.zoomY) && clickedInside) {
  8197. if (!this.selectionMarker) {
  8198. this.selectionMarker = chart.renderer.rect(
  8199. plotLeft,
  8200. plotTop,
  8201. zoomHor ? 1 : plotWidth,
  8202. zoomVert ? 1 : plotHeight,
  8203. 0
  8204. )
  8205. .attr({
  8206. fill: chartOptions.selectionMarkerFill || 'rgba(69,114,167,0.25)',
  8207. zIndex: 7
  8208. })
  8209. .add();
  8210. }
  8211. }
  8212. // adjust the width of the selection marker
  8213. if (this.selectionMarker && zoomHor) {
  8214. size = chartX - mouseDownX;
  8215. this.selectionMarker.attr({
  8216. width: mathAbs(size),
  8217. x: (size > 0 ? 0 : size) + mouseDownX
  8218. });
  8219. }
  8220. // adjust the height of the selection marker
  8221. if (this.selectionMarker && zoomVert) {
  8222. size = chartY - mouseDownY;
  8223. this.selectionMarker.attr({
  8224. height: mathAbs(size),
  8225. y: (size > 0 ? 0 : size) + mouseDownY
  8226. });
  8227. }
  8228. // panning
  8229. if (clickedInside && !this.selectionMarker && chartOptions.panning) {
  8230. chart.pan(e, chartOptions.panning);
  8231. }
  8232. }
  8233. },
  8234. /**
  8235. * On mouse up or touch end across the entire document, drop the selection.
  8236. */
  8237. drop: function (e) {
  8238. var chart = this.chart,
  8239. hasPinched = this.hasPinched;
  8240. if (this.selectionMarker) {
  8241. var selectionData = {
  8242. xAxis: [],
  8243. yAxis: [],
  8244. originalEvent: e.originalEvent || e
  8245. },
  8246. selectionBox = this.selectionMarker,
  8247. selectionLeft = selectionBox.x,
  8248. selectionTop = selectionBox.y,
  8249. runZoom;
  8250. // a selection has been made
  8251. if (this.hasDragged || hasPinched) {
  8252. // record each axis' min and max
  8253. each(chart.axes, function (axis) {
  8254. if (axis.zoomEnabled) {
  8255. var horiz = axis.horiz,
  8256. selectionMin = axis.toValue((horiz ? selectionLeft : selectionTop)),
  8257. selectionMax = axis.toValue((horiz ? selectionLeft + selectionBox.width : selectionTop + selectionBox.height));
  8258. if (!isNaN(selectionMin) && !isNaN(selectionMax)) { // #859
  8259. selectionData[axis.xOrY + 'Axis'].push({
  8260. axis: axis,
  8261. min: mathMin(selectionMin, selectionMax), // for reversed axes,
  8262. max: mathMax(selectionMin, selectionMax)
  8263. });
  8264. runZoom = true;
  8265. }
  8266. }
  8267. });
  8268. if (runZoom) {
  8269. fireEvent(chart, 'selection', selectionData, function (args) {
  8270. chart.zoom(extend(args, hasPinched ? {animation: false} : null));
  8271. });
  8272. }
  8273. }
  8274. this.selectionMarker = this.selectionMarker.destroy();
  8275. // Reset scaling preview
  8276. if (hasPinched) {
  8277. this.scaleGroups();
  8278. }
  8279. }
  8280. // Reset all
  8281. if (chart) { // it may be destroyed on mouse up - #877
  8282. css(chart.container, {cursor: chart._cursor});
  8283. chart.cancelClick = this.hasDragged > 10; // #370
  8284. chart.mouseIsDown = this.hasDragged = this.hasPinched = false;
  8285. this.pinchDown = [];
  8286. }
  8287. },
  8288. onContainerMouseDown: function (e) {
  8289. e = this.normalize(e);
  8290. // issue #295, dragging not always working in Firefox
  8291. if (e.preventDefault) {
  8292. e.preventDefault();
  8293. }
  8294. this.dragStart(e);
  8295. },
  8296. onDocumentMouseUp: function (e) {
  8297. this.drop(e);
  8298. },
  8299. /**
  8300. * Special handler for mouse move that will hide the tooltip when the mouse leaves the plotarea.
  8301. * Issue #149 workaround. The mouseleave event does not always fire.
  8302. */
  8303. onDocumentMouseMove: function (e) {
  8304. var chart = this.chart,
  8305. chartPosition = this.chartPosition,
  8306. hoverSeries = chart.hoverSeries;
  8307. e = this.normalize(e, chartPosition);
  8308. // If we're outside, hide the tooltip
  8309. if (chartPosition && hoverSeries && !this.inClass(e.target, 'highcharts-tracker') &&
  8310. !chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop)) {
  8311. this.reset();
  8312. }
  8313. },
  8314. /**
  8315. * When mouse leaves the container, hide the tooltip.
  8316. */
  8317. onContainerMouseLeave: function () {
  8318. this.reset();
  8319. this.chartPosition = null; // also reset the chart position, used in #149 fix
  8320. },
  8321. // The mousemove, touchmove and touchstart event handler
  8322. onContainerMouseMove: function (e) {
  8323. var chart = this.chart;
  8324. // normalize
  8325. e = this.normalize(e);
  8326. // #295
  8327. e.returnValue = false;
  8328. if (chart.mouseIsDown === 'mousedown') {
  8329. this.drag(e);
  8330. }
  8331. // Show the tooltip and run mouse over events (#977)
  8332. if ((this.inClass(e.target, 'highcharts-tracker') ||
  8333. chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop)) && !chart.openMenu) {
  8334. this.runPointActions(e);
  8335. }
  8336. },
  8337. /**
  8338. * Utility to detect whether an element has, or has a parent with, a specific
  8339. * class name. Used on detection of tracker objects and on deciding whether
  8340. * hovering the tooltip should cause the active series to mouse out.
  8341. */
  8342. inClass: function (element, className) {
  8343. var elemClassName;
  8344. while (element) {
  8345. elemClassName = attr(element, 'class');
  8346. if (elemClassName) {
  8347. if (elemClassName.indexOf(className) !== -1) {
  8348. return true;
  8349. } else if (elemClassName.indexOf(PREFIX + 'container') !== -1) {
  8350. return false;
  8351. }
  8352. }
  8353. element = element.parentNode;
  8354. }
  8355. },
  8356. onTrackerMouseOut: function (e) {
  8357. var series = this.chart.hoverSeries;
  8358. if (series && !series.options.stickyTracking && !this.inClass(e.toElement || e.relatedTarget, PREFIX + 'tooltip')) {
  8359. series.onMouseOut();
  8360. }
  8361. },
  8362. onContainerClick: function (e) {
  8363. var chart = this.chart,
  8364. hoverPoint = chart.hoverPoint,
  8365. plotLeft = chart.plotLeft,
  8366. plotTop = chart.plotTop,
  8367. inverted = chart.inverted,
  8368. chartPosition,
  8369. plotX,
  8370. plotY;
  8371. e = this.normalize(e);
  8372. e.cancelBubble = true; // IE specific
  8373. if (!chart.cancelClick) {
  8374. // On tracker click, fire the series and point events. #783, #1583
  8375. if (hoverPoint && this.inClass(e.target, PREFIX + 'tracker')) {
  8376. chartPosition = this.chartPosition;
  8377. plotX = hoverPoint.plotX;
  8378. plotY = hoverPoint.plotY;
  8379. // add page position info
  8380. extend(hoverPoint, {
  8381. pageX: chartPosition.left + plotLeft +
  8382. (inverted ? chart.plotWidth - plotY : plotX),
  8383. pageY: chartPosition.top + plotTop +
  8384. (inverted ? chart.plotHeight - plotX : plotY)
  8385. });
  8386. // the series click event
  8387. fireEvent(hoverPoint.series, 'click', extend(e, {
  8388. point: hoverPoint
  8389. }));
  8390. // the point click event
  8391. if (chart.hoverPoint) { // it may be destroyed (#1844)
  8392. hoverPoint.firePointEvent('click', e);
  8393. }
  8394. // When clicking outside a tracker, fire a chart event
  8395. } else {
  8396. extend(e, this.getCoordinates(e));
  8397. // fire a click event in the chart
  8398. if (chart.isInsidePlot(e.chartX - plotLeft, e.chartY - plotTop)) {
  8399. fireEvent(chart, 'click', e);
  8400. }
  8401. }
  8402. }
  8403. },
  8404. onContainerTouchStart: function (e) {
  8405. var chart = this.chart;
  8406. if (e.touches.length === 1) {
  8407. e = this.normalize(e);
  8408. if (chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop)) {
  8409. // Prevent the click pseudo event from firing unless it is set in the options
  8410. /*if (!chart.runChartClick) {
  8411. e.preventDefault();
  8412. }*/
  8413. // Run mouse events and display tooltip etc
  8414. this.runPointActions(e);
  8415. this.pinch(e);
  8416. } else {
  8417. // Hide the tooltip on touching outside the plot area (#1203)
  8418. this.reset();
  8419. }
  8420. } else if (e.touches.length === 2) {
  8421. this.pinch(e);
  8422. }
  8423. },
  8424. onContainerTouchMove: function (e) {
  8425. if (e.touches.length === 1 || e.touches.length === 2) {
  8426. this.pinch(e);
  8427. }
  8428. },
  8429. onDocumentTouchEnd: function (e) {
  8430. this.drop(e);
  8431. },
  8432. /**
  8433. * Set the JS DOM events on the container and document. This method should contain
  8434. * a one-to-one assignment between methods and their handlers. Any advanced logic should
  8435. * be moved to the handler reflecting the event's name.
  8436. */
  8437. setDOMEvents: function () {
  8438. var pointer = this,
  8439. container = pointer.chart.container,
  8440. events;
  8441. this._events = events = [
  8442. [container, 'onmousedown', 'onContainerMouseDown'],
  8443. [container, 'onmousemove', 'onContainerMouseMove'],
  8444. [container, 'onclick', 'onContainerClick'],
  8445. [container, 'mouseleave', 'onContainerMouseLeave'],
  8446. [doc, 'mousemove', 'onDocumentMouseMove'],
  8447. [doc, 'mouseup', 'onDocumentMouseUp']
  8448. ];
  8449. if (hasTouch) {
  8450. events.push(
  8451. [container, 'ontouchstart', 'onContainerTouchStart'],
  8452. [container, 'ontouchmove', 'onContainerTouchMove'],
  8453. [doc, 'touchend', 'onDocumentTouchEnd']
  8454. );
  8455. }
  8456. each(events, function (eventConfig) {
  8457. // First, create the callback function that in turn calls the method on Pointer
  8458. pointer['_' + eventConfig[2]] = function (e) {
  8459. pointer[eventConfig[2]](e);
  8460. };
  8461. // Now attach the function, either as a direct property or through addEvent
  8462. if (eventConfig[1].indexOf('on') === 0) {
  8463. eventConfig[0][eventConfig[1]] = pointer['_' + eventConfig[2]];
  8464. } else {
  8465. addEvent(eventConfig[0], eventConfig[1], pointer['_' + eventConfig[2]]);
  8466. }
  8467. });
  8468. },
  8469. /**
  8470. * Destroys the Pointer object and disconnects DOM events.
  8471. */
  8472. destroy: function () {
  8473. var pointer = this;
  8474. // Release all DOM events
  8475. each(pointer._events, function (eventConfig) {
  8476. if (eventConfig[1].indexOf('on') === 0) {
  8477. eventConfig[0][eventConfig[1]] = null; // delete breaks oldIE
  8478. } else {
  8479. removeEvent(eventConfig[0], eventConfig[1], pointer['_' + eventConfig[2]]);
  8480. }
  8481. });
  8482. delete pointer._events;
  8483. // memory and CPU leak
  8484. clearInterval(pointer.tooltipTimeout);
  8485. }
  8486. };
  8487. /**
  8488. * The overview of the chart's series
  8489. */
  8490. function Legend(chart, options) {
  8491. this.init(chart, options);
  8492. }
  8493. Legend.prototype = {
  8494. /**
  8495. * Initialize the legend
  8496. */
  8497. init: function (chart, options) {
  8498. var legend = this,
  8499. itemStyle = options.itemStyle,
  8500. padding = pick(options.padding, 8),
  8501. itemMarginTop = options.itemMarginTop || 0;
  8502. this.options = options;
  8503. if (!options.enabled) {
  8504. return;
  8505. }
  8506. legend.baseline = pInt(itemStyle.fontSize) + 3 + itemMarginTop; // used in Series prototype
  8507. legend.itemStyle = itemStyle;
  8508. legend.itemHiddenStyle = merge(itemStyle, options.itemHiddenStyle);
  8509. legend.itemMarginTop = itemMarginTop;
  8510. legend.padding = padding;
  8511. legend.initialItemX = padding;
  8512. legend.initialItemY = padding - 5; // 5 is the number of pixels above the text
  8513. legend.maxItemWidth = 0;
  8514. legend.chart = chart;
  8515. legend.itemHeight = 0;
  8516. legend.lastLineHeight = 0;
  8517. // Render it
  8518. legend.render();
  8519. // move checkboxes
  8520. addEvent(legend.chart, 'endResize', function () {
  8521. legend.positionCheckboxes();
  8522. });
  8523. },
  8524. /**
  8525. * Set the colors for the legend item
  8526. * @param {Object} item A Series or Point instance
  8527. * @param {Object} visible Dimmed or colored
  8528. */
  8529. colorizeItem: function (item, visible) {
  8530. var legend = this,
  8531. options = legend.options,
  8532. legendItem = item.legendItem,
  8533. legendLine = item.legendLine,
  8534. legendSymbol = item.legendSymbol,
  8535. hiddenColor = legend.itemHiddenStyle.color,
  8536. textColor = visible ? options.itemStyle.color : hiddenColor,
  8537. symbolColor = visible ? item.color : hiddenColor,
  8538. markerOptions = item.options && item.options.marker,
  8539. symbolAttr = {
  8540. stroke: symbolColor,
  8541. fill: symbolColor
  8542. },
  8543. key,
  8544. val;
  8545. if (legendItem) {
  8546. legendItem.css({fill: textColor, color: textColor}); // color for #1553, oldIE
  8547. }
  8548. if (legendLine) {
  8549. legendLine.attr({stroke: symbolColor});
  8550. }
  8551. if (legendSymbol) {
  8552. // Apply marker options
  8553. if (markerOptions && legendSymbol.isMarker) { // #585
  8554. markerOptions = item.convertAttribs(markerOptions);
  8555. for (key in markerOptions) {
  8556. val = markerOptions[key];
  8557. if (val !== UNDEFINED) {
  8558. symbolAttr[key] = val;
  8559. }
  8560. }
  8561. }
  8562. legendSymbol.attr(symbolAttr);
  8563. }
  8564. },
  8565. /**
  8566. * Position the legend item
  8567. * @param {Object} item A Series or Point instance
  8568. */
  8569. positionItem: function (item) {
  8570. var legend = this,
  8571. options = legend.options,
  8572. symbolPadding = options.symbolPadding,
  8573. ltr = !options.rtl,
  8574. legendItemPos = item._legendItemPos,
  8575. itemX = legendItemPos[0],
  8576. itemY = legendItemPos[1],
  8577. checkbox = item.checkbox;
  8578. if (item.legendGroup) {
  8579. item.legendGroup.translate(
  8580. ltr ? itemX : legend.legendWidth - itemX - 2 * symbolPadding - 4,
  8581. itemY
  8582. );
  8583. }
  8584. if (checkbox) {
  8585. checkbox.x = itemX;
  8586. checkbox.y = itemY;
  8587. }
  8588. },
  8589. /**
  8590. * Destroy a single legend item
  8591. * @param {Object} item The series or point
  8592. */
  8593. destroyItem: function (item) {
  8594. var checkbox = item.checkbox;
  8595. // destroy SVG elements
  8596. each(['legendItem', 'legendLine', 'legendSymbol', 'legendGroup'], function (key) {
  8597. if (item[key]) {
  8598. item[key] = item[key].destroy();
  8599. }
  8600. });
  8601. if (checkbox) {
  8602. discardElement(item.checkbox);
  8603. }
  8604. },
  8605. /**
  8606. * Destroys the legend.
  8607. */
  8608. destroy: function () {
  8609. var legend = this,
  8610. legendGroup = legend.group,
  8611. box = legend.box;
  8612. if (box) {
  8613. legend.box = box.destroy();
  8614. }
  8615. if (legendGroup) {
  8616. legend.group = legendGroup.destroy();
  8617. }
  8618. },
  8619. /**
  8620. * Position the checkboxes after the width is determined
  8621. */
  8622. positionCheckboxes: function (scrollOffset) {
  8623. var alignAttr = this.group.alignAttr,
  8624. translateY,
  8625. clipHeight = this.clipHeight || this.legendHeight;
  8626. if (alignAttr) {
  8627. translateY = alignAttr.translateY;
  8628. each(this.allItems, function (item) {
  8629. var checkbox = item.checkbox,
  8630. top;
  8631. if (checkbox) {
  8632. top = (translateY + checkbox.y + (scrollOffset || 0) + 3);
  8633. css(checkbox, {
  8634. left: (alignAttr.translateX + item.legendItemWidth + checkbox.x - 20) + PX,
  8635. top: top + PX,
  8636. display: top > translateY - 6 && top < translateY + clipHeight - 6 ? '' : NONE
  8637. });
  8638. }
  8639. });
  8640. }
  8641. },
  8642. /**
  8643. * Render the legend title on top of the legend
  8644. */
  8645. renderTitle: function () {
  8646. var options = this.options,
  8647. padding = this.padding,
  8648. titleOptions = options.title,
  8649. titleHeight = 0,
  8650. bBox;
  8651. if (titleOptions.text) {
  8652. if (!this.title) {
  8653. this.title = this.chart.renderer.label(titleOptions.text, padding - 3, padding - 4, null, null, null, null, null, 'legend-title')
  8654. .attr({zIndex: 1})
  8655. .css(titleOptions.style)
  8656. .add(this.group);
  8657. }
  8658. bBox = this.title.getBBox();
  8659. titleHeight = bBox.height;
  8660. this.offsetWidth = bBox.width; // #1717
  8661. this.contentGroup.attr({translateY: titleHeight});
  8662. }
  8663. this.titleHeight = titleHeight;
  8664. },
  8665. /**
  8666. * Render a single specific legend item
  8667. * @param {Object} item A series or point
  8668. */
  8669. renderItem: function (item) {
  8670. var legend = this,
  8671. chart = legend.chart,
  8672. renderer = chart.renderer,
  8673. options = legend.options,
  8674. horizontal = options.layout === 'horizontal',
  8675. symbolWidth = options.symbolWidth,
  8676. symbolPadding = options.symbolPadding,
  8677. itemStyle = legend.itemStyle,
  8678. itemHiddenStyle = legend.itemHiddenStyle,
  8679. padding = legend.padding,
  8680. itemDistance = horizontal ? pick(options.itemDistance, 8) : 0,
  8681. ltr = !options.rtl,
  8682. itemHeight,
  8683. widthOption = options.width,
  8684. itemMarginBottom = options.itemMarginBottom || 0,
  8685. itemMarginTop = legend.itemMarginTop,
  8686. initialItemX = legend.initialItemX,
  8687. bBox,
  8688. itemWidth,
  8689. li = item.legendItem,
  8690. series = item.series || item,
  8691. itemOptions = series.options,
  8692. showCheckbox = itemOptions.showCheckbox,
  8693. useHTML = options.useHTML;
  8694. if (!li) { // generate it once, later move it
  8695. // Generate the group box
  8696. // A group to hold the symbol and text. Text is to be appended in Legend class.
  8697. item.legendGroup = renderer.g('legend-item')
  8698. .attr({zIndex: 1})
  8699. .add(legend.scrollGroup);
  8700. // Draw the legend symbol inside the group box
  8701. series.drawLegendSymbol(legend, item);
  8702. // Generate the list item text and add it to the group
  8703. item.legendItem = li = renderer.text(
  8704. options.labelFormat ? format(options.labelFormat, item) : options.labelFormatter.call(item),
  8705. ltr ? symbolWidth + symbolPadding : -symbolPadding,
  8706. legend.baseline,
  8707. useHTML
  8708. )
  8709. .css(merge(item.visible ? itemStyle : itemHiddenStyle)) // merge to prevent modifying original (#1021)
  8710. .attr({
  8711. align: ltr ? 'left' : 'right',
  8712. zIndex: 2
  8713. })
  8714. .add(item.legendGroup);
  8715. // Set the events on the item group, or in case of useHTML, the item itself (#1249)
  8716. (useHTML ? li : item.legendGroup).on('mouseover', function () {
  8717. item.setState(HOVER_STATE);
  8718. li.css(legend.options.itemHoverStyle);
  8719. })
  8720. .on('mouseout', function () {
  8721. li.css(item.visible ? itemStyle : itemHiddenStyle);
  8722. item.setState();
  8723. })
  8724. .on('click', function (event) {
  8725. var strLegendItemClick = 'legendItemClick',
  8726. fnLegendItemClick = function () {
  8727. item.setVisible();
  8728. };
  8729. // Pass over the click/touch event. #4.
  8730. event = {
  8731. browserEvent: event
  8732. };
  8733. // click the name or symbol
  8734. if (item.firePointEvent) { // point
  8735. item.firePointEvent(strLegendItemClick, event, fnLegendItemClick);
  8736. } else {
  8737. fireEvent(item, strLegendItemClick, event, fnLegendItemClick);
  8738. }
  8739. });
  8740. // Colorize the items
  8741. legend.colorizeItem(item, item.visible);
  8742. // add the HTML checkbox on top
  8743. if (itemOptions && showCheckbox) {
  8744. item.checkbox = createElement('input', {
  8745. type: 'checkbox',
  8746. checked: item.selected,
  8747. defaultChecked: item.selected // required by IE7
  8748. }, options.itemCheckboxStyle, chart.container);
  8749. addEvent(item.checkbox, 'click', function (event) {
  8750. var target = event.target;
  8751. fireEvent(item, 'checkboxClick', {
  8752. checked: target.checked
  8753. },
  8754. function () {
  8755. item.select();
  8756. }
  8757. );
  8758. });
  8759. }
  8760. }
  8761. // calculate the positions for the next line
  8762. bBox = li.getBBox();
  8763. itemWidth = item.legendItemWidth =
  8764. options.itemWidth || symbolWidth + symbolPadding + bBox.width + itemDistance +
  8765. (showCheckbox ? 20 : 0);
  8766. legend.itemHeight = itemHeight = bBox.height;
  8767. // if the item exceeds the width, start a new line
  8768. if (horizontal && legend.itemX - initialItemX + itemWidth >
  8769. (widthOption || (chart.chartWidth - 2 * padding - initialItemX))) {
  8770. legend.itemX = initialItemX;
  8771. legend.itemY += itemMarginTop + legend.lastLineHeight + itemMarginBottom;
  8772. legend.lastLineHeight = 0; // reset for next line
  8773. }
  8774. // If the item exceeds the height, start a new column
  8775. /*if (!horizontal && legend.itemY + options.y + itemHeight > chart.chartHeight - spacingTop - spacingBottom) {
  8776. legend.itemY = legend.initialItemY;
  8777. legend.itemX += legend.maxItemWidth;
  8778. legend.maxItemWidth = 0;
  8779. }*/
  8780. // Set the edge positions
  8781. legend.maxItemWidth = mathMax(legend.maxItemWidth, itemWidth);
  8782. legend.lastItemY = itemMarginTop + legend.itemY + itemMarginBottom;
  8783. legend.lastLineHeight = mathMax(itemHeight, legend.lastLineHeight); // #915
  8784. // cache the position of the newly generated or reordered items
  8785. item._legendItemPos = [legend.itemX, legend.itemY];
  8786. // advance
  8787. if (horizontal) {
  8788. legend.itemX += itemWidth;
  8789. } else {
  8790. legend.itemY += itemMarginTop + itemHeight + itemMarginBottom;
  8791. legend.lastLineHeight = itemHeight;
  8792. }
  8793. // the width of the widest item
  8794. legend.offsetWidth = widthOption || mathMax(
  8795. (horizontal ? legend.itemX - initialItemX - itemDistance : itemWidth) + padding,
  8796. legend.offsetWidth
  8797. );
  8798. },
  8799. /**
  8800. * Render the legend. This method can be called both before and after
  8801. * chart.render. If called after, it will only rearrange items instead
  8802. * of creating new ones.
  8803. */
  8804. render: function () {
  8805. var legend = this,
  8806. chart = legend.chart,
  8807. renderer = chart.renderer,
  8808. legendGroup = legend.group,
  8809. allItems,
  8810. display,
  8811. legendWidth,
  8812. legendHeight,
  8813. box = legend.box,
  8814. options = legend.options,
  8815. padding = legend.padding,
  8816. legendBorderWidth = options.borderWidth,
  8817. legendBackgroundColor = options.backgroundColor;
  8818. legend.itemX = legend.initialItemX;
  8819. legend.itemY = legend.initialItemY;
  8820. legend.offsetWidth = 0;
  8821. legend.lastItemY = 0;
  8822. if (!legendGroup) {
  8823. legend.group = legendGroup = renderer.g('legend')
  8824. .attr({zIndex: 7})
  8825. .add();
  8826. legend.contentGroup = renderer.g()
  8827. .attr({zIndex: 1}) // above background
  8828. .add(legendGroup);
  8829. legend.scrollGroup = renderer.g()
  8830. .add(legend.contentGroup);
  8831. }
  8832. legend.renderTitle();
  8833. // add each series or point
  8834. allItems = [];
  8835. each(chart.series, function (serie) {
  8836. var seriesOptions = serie.options;
  8837. if (!seriesOptions.showInLegend || defined(seriesOptions.linkedTo)) {
  8838. return;
  8839. }
  8840. // use points or series for the legend item depending on legendType
  8841. allItems = allItems.concat(
  8842. serie.legendItems ||
  8843. (seriesOptions.legendType === 'point' ?
  8844. serie.data :
  8845. serie)
  8846. );
  8847. });
  8848. // sort by legendIndex
  8849. stableSort(allItems, function (a, b) {
  8850. return ((a.options && a.options.legendIndex) || 0) - ((b.options && b.options.legendIndex) || 0);
  8851. });
  8852. // reversed legend
  8853. if (options.reversed) {
  8854. allItems.reverse();
  8855. }
  8856. legend.allItems = allItems;
  8857. legend.display = display = !!allItems.length;
  8858. // render the items
  8859. each(allItems, function (item) {
  8860. legend.renderItem(item);
  8861. });
  8862. // Draw the border
  8863. legendWidth = options.width || legend.offsetWidth;
  8864. legendHeight = legend.lastItemY + legend.lastLineHeight + legend.titleHeight;
  8865. legendHeight = legend.handleOverflow(legendHeight);
  8866. if (legendBorderWidth || legendBackgroundColor) {
  8867. legendWidth += padding;
  8868. legendHeight += padding;
  8869. if (!box) {
  8870. legend.box = box = renderer.rect(
  8871. 0,
  8872. 0,
  8873. legendWidth,
  8874. legendHeight,
  8875. options.borderRadius,
  8876. legendBorderWidth || 0
  8877. ).attr({
  8878. stroke: options.borderColor,
  8879. 'stroke-width': legendBorderWidth || 0,
  8880. fill: legendBackgroundColor || NONE
  8881. })
  8882. .add(legendGroup)
  8883. .shadow(options.shadow);
  8884. box.isNew = true;
  8885. } else if (legendWidth > 0 && legendHeight > 0) {
  8886. box[box.isNew ? 'attr' : 'animate'](
  8887. box.crisp(null, null, null, legendWidth, legendHeight)
  8888. );
  8889. box.isNew = false;
  8890. }
  8891. // hide the border if no items
  8892. box[display ? 'show' : 'hide']();
  8893. }
  8894. legend.legendWidth = legendWidth;
  8895. legend.legendHeight = legendHeight;
  8896. // Now that the legend width and height are established, put the items in the
  8897. // final position
  8898. each(allItems, function (item) {
  8899. legend.positionItem(item);
  8900. });
  8901. // 1.x compatibility: positioning based on style
  8902. /*var props = ['left', 'right', 'top', 'bottom'],
  8903. prop,
  8904. i = 4;
  8905. while (i--) {
  8906. prop = props[i];
  8907. if (options.style[prop] && options.style[prop] !== 'auto') {
  8908. options[i < 2 ? 'align' : 'verticalAlign'] = prop;
  8909. options[i < 2 ? 'x' : 'y'] = pInt(options.style[prop]) * (i % 2 ? -1 : 1);
  8910. }
  8911. }*/
  8912. if (display) {
  8913. legendGroup.align(extend({
  8914. width: legendWidth,
  8915. height: legendHeight
  8916. }, options), true, 'spacingBox');
  8917. }
  8918. if (!chart.isResizing) {
  8919. this.positionCheckboxes();
  8920. }
  8921. },
  8922. /**
  8923. * Set up the overflow handling by adding navigation with up and down arrows below the
  8924. * legend.
  8925. */
  8926. handleOverflow: function (legendHeight) {
  8927. var legend = this,
  8928. chart = this.chart,
  8929. renderer = chart.renderer,
  8930. pageCount,
  8931. options = this.options,
  8932. optionsY = options.y,
  8933. alignTop = options.verticalAlign === 'top',
  8934. spaceHeight = chart.spacingBox.height + (alignTop ? -optionsY : optionsY) - this.padding,
  8935. maxHeight = options.maxHeight,
  8936. clipHeight,
  8937. clipRect = this.clipRect,
  8938. navOptions = options.navigation,
  8939. animation = pick(navOptions.animation, true),
  8940. arrowSize = navOptions.arrowSize || 12,
  8941. nav = this.nav;
  8942. // Adjust the height
  8943. if (options.layout === 'horizontal') {
  8944. spaceHeight /= 2;
  8945. }
  8946. if (maxHeight) {
  8947. spaceHeight = mathMin(spaceHeight, maxHeight);
  8948. }
  8949. // Reset the legend height and adjust the clipping rectangle
  8950. if (legendHeight > spaceHeight && !options.useHTML) {
  8951. this.clipHeight = clipHeight = spaceHeight - 20 - this.titleHeight;
  8952. this.pageCount = pageCount = mathCeil(legendHeight / clipHeight);
  8953. this.currentPage = pick(this.currentPage, 1);
  8954. this.fullHeight = legendHeight;
  8955. // Only apply clipping if needed. Clipping causes blurred legend in PDF export (#1787)
  8956. if (!clipRect) {
  8957. clipRect = legend.clipRect = renderer.clipRect(0, 0, 9999, 0);
  8958. legend.contentGroup.clip(clipRect);
  8959. }
  8960. clipRect.attr({
  8961. height: clipHeight
  8962. });
  8963. // Add navigation elements
  8964. if (!nav) {
  8965. this.nav = nav = renderer.g().attr({zIndex: 1}).add(this.group);
  8966. this.up = renderer.symbol('triangle', 0, 0, arrowSize, arrowSize)
  8967. .on('click', function () {
  8968. legend.scroll(-1, animation);
  8969. })
  8970. .add(nav);
  8971. this.pager = renderer.text('', 15, 10)
  8972. .css(navOptions.style)
  8973. .add(nav);
  8974. this.down = renderer.symbol('triangle-down', 0, 0, arrowSize, arrowSize)
  8975. .on('click', function () {
  8976. legend.scroll(1, animation);
  8977. })
  8978. .add(nav);
  8979. }
  8980. // Set initial position
  8981. legend.scroll(0);
  8982. legendHeight = spaceHeight;
  8983. } else if (nav) {
  8984. clipRect.attr({
  8985. height: chart.chartHeight
  8986. });
  8987. nav.hide();
  8988. this.scrollGroup.attr({
  8989. translateY: 1
  8990. });
  8991. this.clipHeight = 0; // #1379
  8992. }
  8993. return legendHeight;
  8994. },
  8995. /**
  8996. * Scroll the legend by a number of pages
  8997. * @param {Object} scrollBy
  8998. * @param {Object} animation
  8999. */
  9000. scroll: function (scrollBy, animation) {
  9001. var pageCount = this.pageCount,
  9002. currentPage = this.currentPage + scrollBy,
  9003. clipHeight = this.clipHeight,
  9004. navOptions = this.options.navigation,
  9005. activeColor = navOptions.activeColor,
  9006. inactiveColor = navOptions.inactiveColor,
  9007. pager = this.pager,
  9008. padding = this.padding,
  9009. scrollOffset;
  9010. // When resizing while looking at the last page
  9011. if (currentPage > pageCount) {
  9012. currentPage = pageCount;
  9013. }
  9014. if (currentPage > 0) {
  9015. if (animation !== UNDEFINED) {
  9016. setAnimation(animation, this.chart);
  9017. }
  9018. this.nav.attr({
  9019. translateX: padding,
  9020. translateY: clipHeight + 7 + this.titleHeight,
  9021. visibility: VISIBLE
  9022. });
  9023. this.up.attr({
  9024. fill: currentPage === 1 ? inactiveColor : activeColor
  9025. })
  9026. .css({
  9027. cursor: currentPage === 1 ? 'default' : 'pointer'
  9028. });
  9029. pager.attr({
  9030. text: currentPage + '/' + this.pageCount
  9031. });
  9032. this.down.attr({
  9033. x: 18 + this.pager.getBBox().width, // adjust to text width
  9034. fill: currentPage === pageCount ? inactiveColor : activeColor
  9035. })
  9036. .css({
  9037. cursor: currentPage === pageCount ? 'default' : 'pointer'
  9038. });
  9039. scrollOffset = -mathMin(clipHeight * (currentPage - 1), this.fullHeight - clipHeight + padding) + 1;
  9040. this.scrollGroup.animate({
  9041. translateY: scrollOffset
  9042. });
  9043. pager.attr({
  9044. text: currentPage + '/' + pageCount
  9045. });
  9046. this.currentPage = currentPage;
  9047. this.positionCheckboxes(scrollOffset);
  9048. }
  9049. }
  9050. };
  9051. // Workaround for #2030, horizontal legend items not displaying in IE11 Preview.
  9052. // TODO: When IE11 is released, check again for this bug, and remove the fix
  9053. // or make a better one.
  9054. if (/Trident.*?11\.0/.test(userAgent)) {
  9055. wrap(Legend.prototype, 'positionItem', function (proceed, item) {
  9056. var legend = this;
  9057. setTimeout(function () {
  9058. proceed.call(legend, item);
  9059. });
  9060. });
  9061. }
  9062. /**
  9063. * The chart class
  9064. * @param {Object} options
  9065. * @param {Function} callback Function to run when the chart has loaded
  9066. */
  9067. function Chart() {
  9068. this.init.apply(this, arguments);
  9069. }
  9070. Chart.prototype = {
  9071. /**
  9072. * Initialize the chart
  9073. */
  9074. init: function (userOptions, callback) {
  9075. // Handle regular options
  9076. var options,
  9077. seriesOptions = userOptions.series; // skip merging data points to increase performance
  9078. userOptions.series = null;
  9079. options = merge(defaultOptions, userOptions); // do the merge
  9080. options.series = userOptions.series = seriesOptions; // set back the series data
  9081. var optionsChart = options.chart;
  9082. // Create margin & spacing array
  9083. this.margin = this.splashArray('margin', optionsChart);
  9084. this.spacing = this.splashArray('spacing', optionsChart);
  9085. var chartEvents = optionsChart.events;
  9086. //this.runChartClick = chartEvents && !!chartEvents.click;
  9087. this.bounds = {h: {}, v: {}}; // Pixel data bounds for touch zoom
  9088. this.callback = callback;
  9089. this.isResizing = 0;
  9090. this.options = options;
  9091. //chartTitleOptions = UNDEFINED;
  9092. //chartSubtitleOptions = UNDEFINED;
  9093. this.axes = [];
  9094. this.series = [];
  9095. this.hasCartesianSeries = optionsChart.showAxes;
  9096. //this.axisOffset = UNDEFINED;
  9097. //this.maxTicks = UNDEFINED; // handle the greatest amount of ticks on grouped axes
  9098. //this.inverted = UNDEFINED;
  9099. //this.loadingShown = UNDEFINED;
  9100. //this.container = UNDEFINED;
  9101. //this.chartWidth = UNDEFINED;
  9102. //this.chartHeight = UNDEFINED;
  9103. //this.marginRight = UNDEFINED;
  9104. //this.marginBottom = UNDEFINED;
  9105. //this.containerWidth = UNDEFINED;
  9106. //this.containerHeight = UNDEFINED;
  9107. //this.oldChartWidth = UNDEFINED;
  9108. //this.oldChartHeight = UNDEFINED;
  9109. //this.renderTo = UNDEFINED;
  9110. //this.renderToClone = UNDEFINED;
  9111. //this.spacingBox = UNDEFINED
  9112. //this.legend = UNDEFINED;
  9113. // Elements
  9114. //this.chartBackground = UNDEFINED;
  9115. //this.plotBackground = UNDEFINED;
  9116. //this.plotBGImage = UNDEFINED;
  9117. //this.plotBorder = UNDEFINED;
  9118. //this.loadingDiv = UNDEFINED;
  9119. //this.loadingSpan = UNDEFINED;
  9120. var chart = this,
  9121. eventType;
  9122. // Add the chart to the global lookup
  9123. chart.index = charts.length;
  9124. charts.push(chart);
  9125. // Set up auto resize
  9126. if (optionsChart.reflow !== false) {
  9127. addEvent(chart, 'load', function () {
  9128. chart.initReflow();
  9129. });
  9130. }
  9131. // Chart event handlers
  9132. if (chartEvents) {
  9133. for (eventType in chartEvents) {
  9134. addEvent(chart, eventType, chartEvents[eventType]);
  9135. }
  9136. }
  9137. chart.xAxis = [];
  9138. chart.yAxis = [];
  9139. // Expose methods and variables
  9140. chart.animation = useCanVG ? false : pick(optionsChart.animation, true);
  9141. chart.pointCount = 0;
  9142. chart.counters = new ChartCounters();
  9143. chart.firstRender();
  9144. },
  9145. /**
  9146. * Initialize an individual series, called internally before render time
  9147. */
  9148. initSeries: function (options) {
  9149. var chart = this,
  9150. optionsChart = chart.options.chart,
  9151. type = options.type || optionsChart.type || optionsChart.defaultSeriesType,
  9152. series,
  9153. constr = seriesTypes[type];
  9154. // No such series type
  9155. if (!constr) {
  9156. error(17, true);
  9157. }
  9158. series = new constr();
  9159. series.init(this, options);
  9160. return series;
  9161. },
  9162. /**
  9163. * Add a series dynamically after time
  9164. *
  9165. * @param {Object} options The config options
  9166. * @param {Boolean} redraw Whether to redraw the chart after adding. Defaults to true.
  9167. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  9168. * configuration
  9169. *
  9170. * @return {Object} series The newly created series object
  9171. */
  9172. addSeries: function (options, redraw, animation) {
  9173. var series,
  9174. chart = this;
  9175. if (options) {
  9176. redraw = pick(redraw, true); // defaults to true
  9177. fireEvent(chart, 'addSeries', {options: options}, function () {
  9178. series = chart.initSeries(options);
  9179. chart.isDirtyLegend = true; // the series array is out of sync with the display
  9180. chart.linkSeries();
  9181. if (redraw) {
  9182. chart.redraw(animation);
  9183. }
  9184. });
  9185. }
  9186. return series;
  9187. },
  9188. /**
  9189. * Add an axis to the chart
  9190. * @param {Object} options The axis option
  9191. * @param {Boolean} isX Whether it is an X axis or a value axis
  9192. */
  9193. addAxis: function (options, isX, redraw, animation) {
  9194. var key = isX ? 'xAxis' : 'yAxis',
  9195. chartOptions = this.options,
  9196. axis;
  9197. /*jslint unused: false*/
  9198. axis = new Axis(this, merge(options, {
  9199. index: this[key].length,
  9200. isX: isX
  9201. }));
  9202. /*jslint unused: true*/
  9203. // Push the new axis options to the chart options
  9204. chartOptions[key] = splat(chartOptions[key] || {});
  9205. chartOptions[key].push(options);
  9206. if (pick(redraw, true)) {
  9207. this.redraw(animation);
  9208. }
  9209. },
  9210. /**
  9211. * Check whether a given point is within the plot area
  9212. *
  9213. * @param {Number} plotX Pixel x relative to the plot area
  9214. * @param {Number} plotY Pixel y relative to the plot area
  9215. * @param {Boolean} inverted Whether the chart is inverted
  9216. */
  9217. isInsidePlot: function (plotX, plotY, inverted) {
  9218. var x = inverted ? plotY : plotX,
  9219. y = inverted ? plotX : plotY;
  9220. return x >= 0 &&
  9221. x <= this.plotWidth &&
  9222. y >= 0 &&
  9223. y <= this.plotHeight;
  9224. },
  9225. /**
  9226. * Adjust all axes tick amounts
  9227. */
  9228. adjustTickAmounts: function () {
  9229. if (this.options.chart.alignTicks !== false) {
  9230. each(this.axes, function (axis) {
  9231. axis.adjustTickAmount();
  9232. });
  9233. }
  9234. this.maxTicks = null;
  9235. },
  9236. /**
  9237. * Redraw legend, axes or series based on updated data
  9238. *
  9239. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  9240. * configuration
  9241. */
  9242. redraw: function (animation) {
  9243. var chart = this,
  9244. axes = chart.axes,
  9245. series = chart.series,
  9246. pointer = chart.pointer,
  9247. legend = chart.legend,
  9248. redrawLegend = chart.isDirtyLegend,
  9249. hasStackedSeries,
  9250. hasDirtyStacks,
  9251. isDirtyBox = chart.isDirtyBox, // todo: check if it has actually changed?
  9252. seriesLength = series.length,
  9253. i = seriesLength,
  9254. serie,
  9255. renderer = chart.renderer,
  9256. isHiddenChart = renderer.isHidden(),
  9257. afterRedraw = [];
  9258. setAnimation(animation, chart);
  9259. if (isHiddenChart) {
  9260. chart.cloneRenderTo();
  9261. }
  9262. // Adjust title layout (reflow multiline text)
  9263. chart.layOutTitles();
  9264. // link stacked series
  9265. while (i--) {
  9266. serie = series[i];
  9267. if (serie.options.stacking) {
  9268. hasStackedSeries = true;
  9269. if (serie.isDirty) {
  9270. hasDirtyStacks = true;
  9271. break;
  9272. }
  9273. }
  9274. }
  9275. if (hasDirtyStacks) { // mark others as dirty
  9276. i = seriesLength;
  9277. while (i--) {
  9278. serie = series[i];
  9279. if (serie.options.stacking) {
  9280. serie.isDirty = true;
  9281. }
  9282. }
  9283. }
  9284. // handle updated data in the series
  9285. each(series, function (serie) {
  9286. if (serie.isDirty) { // prepare the data so axis can read it
  9287. if (serie.options.legendType === 'point') {
  9288. redrawLegend = true;
  9289. }
  9290. }
  9291. });
  9292. // handle added or removed series
  9293. if (redrawLegend && legend.options.enabled) { // series or pie points are added or removed
  9294. // draw legend graphics
  9295. legend.render();
  9296. chart.isDirtyLegend = false;
  9297. }
  9298. // reset stacks
  9299. if (hasStackedSeries) {
  9300. chart.getStacks();
  9301. }
  9302. if (chart.hasCartesianSeries) {
  9303. if (!chart.isResizing) {
  9304. // reset maxTicks
  9305. chart.maxTicks = null;
  9306. // set axes scales
  9307. each(axes, function (axis) {
  9308. axis.setScale();
  9309. });
  9310. }
  9311. chart.adjustTickAmounts();
  9312. chart.getMargins();
  9313. // If one axis is dirty, all axes must be redrawn (#792, #2169)
  9314. each(axes, function (axis) {
  9315. if (axis.isDirty) {
  9316. isDirtyBox = true;
  9317. }
  9318. });
  9319. // redraw axes
  9320. each(axes, function (axis) {
  9321. // Fire 'afterSetExtremes' only if extremes are set
  9322. if (axis.isDirtyExtremes) { // #821
  9323. axis.isDirtyExtremes = false;
  9324. afterRedraw.push(function () { // prevent a recursive call to chart.redraw() (#1119)
  9325. fireEvent(axis, 'afterSetExtremes', extend(axis.eventArgs, axis.getExtremes())); // #747, #751
  9326. delete axis.eventArgs;
  9327. });
  9328. }
  9329. if (isDirtyBox || hasStackedSeries) {
  9330. axis.redraw();
  9331. }
  9332. });
  9333. }
  9334. // the plot areas size has changed
  9335. if (isDirtyBox) {
  9336. chart.drawChartBox();
  9337. }
  9338. // redraw affected series
  9339. each(series, function (serie) {
  9340. if (serie.isDirty && serie.visible &&
  9341. (!serie.isCartesian || serie.xAxis)) { // issue #153
  9342. serie.redraw();
  9343. }
  9344. });
  9345. // move tooltip or reset
  9346. if (pointer && pointer.reset) {
  9347. pointer.reset(true);
  9348. }
  9349. // redraw if canvas
  9350. renderer.draw();
  9351. // fire the event
  9352. fireEvent(chart, 'redraw'); // jQuery breaks this when calling it from addEvent. Overwrites chart.redraw
  9353. if (isHiddenChart) {
  9354. chart.cloneRenderTo(true);
  9355. }
  9356. // Fire callbacks that are put on hold until after the redraw
  9357. each(afterRedraw, function (callback) {
  9358. callback.call();
  9359. });
  9360. },
  9361. /**
  9362. * Dim the chart and show a loading text or symbol
  9363. * @param {String} str An optional text to show in the loading label instead of the default one
  9364. */
  9365. showLoading: function (str) {
  9366. var chart = this,
  9367. options = chart.options,
  9368. loadingDiv = chart.loadingDiv;
  9369. var loadingOptions = options.loading;
  9370. // create the layer at the first call
  9371. if (!loadingDiv) {
  9372. chart.loadingDiv = loadingDiv = createElement(DIV, {
  9373. className: PREFIX + 'loading'
  9374. }, extend(loadingOptions.style, {
  9375. zIndex: 10,
  9376. display: NONE
  9377. }), chart.container);
  9378. chart.loadingSpan = createElement(
  9379. 'span',
  9380. null,
  9381. loadingOptions.labelStyle,
  9382. loadingDiv
  9383. );
  9384. }
  9385. // update text
  9386. chart.loadingSpan.innerHTML = str || options.lang.loading;
  9387. // show it
  9388. if (!chart.loadingShown) {
  9389. css(loadingDiv, {
  9390. opacity: 0,
  9391. display: '',
  9392. left: chart.plotLeft + PX,
  9393. top: chart.plotTop + PX,
  9394. width: chart.plotWidth + PX,
  9395. height: chart.plotHeight + PX
  9396. });
  9397. animate(loadingDiv, {
  9398. opacity: loadingOptions.style.opacity
  9399. }, {
  9400. duration: loadingOptions.showDuration || 0
  9401. });
  9402. chart.loadingShown = true;
  9403. }
  9404. },
  9405. /**
  9406. * Hide the loading layer
  9407. */
  9408. hideLoading: function () {
  9409. var options = this.options,
  9410. loadingDiv = this.loadingDiv;
  9411. if (loadingDiv) {
  9412. animate(loadingDiv, {
  9413. opacity: 0
  9414. }, {
  9415. duration: options.loading.hideDuration || 100,
  9416. complete: function () {
  9417. css(loadingDiv, {display: NONE});
  9418. }
  9419. });
  9420. }
  9421. this.loadingShown = false;
  9422. },
  9423. /**
  9424. * Get an axis, series or point object by id.
  9425. * @param id {String} The id as given in the configuration options
  9426. */
  9427. get: function (id) {
  9428. var chart = this,
  9429. axes = chart.axes,
  9430. series = chart.series;
  9431. var i,
  9432. j,
  9433. points;
  9434. // search axes
  9435. for (i = 0; i < axes.length; i++) {
  9436. if (axes[i].options.id === id) {
  9437. return axes[i];
  9438. }
  9439. }
  9440. // search series
  9441. for (i = 0; i < series.length; i++) {
  9442. if (series[i].options.id === id) {
  9443. return series[i];
  9444. }
  9445. }
  9446. // search points
  9447. for (i = 0; i < series.length; i++) {
  9448. points = series[i].points || [];
  9449. for (j = 0; j < points.length; j++) {
  9450. if (points[j].id === id) {
  9451. return points[j];
  9452. }
  9453. }
  9454. }
  9455. return null;
  9456. },
  9457. /**
  9458. * Create the Axis instances based on the config options
  9459. */
  9460. getAxes: function () {
  9461. var chart = this,
  9462. options = this.options,
  9463. xAxisOptions = options.xAxis = splat(options.xAxis || {}),
  9464. yAxisOptions = options.yAxis = splat(options.yAxis || {}),
  9465. optionsArray,
  9466. axis;
  9467. // make sure the options are arrays and add some members
  9468. each(xAxisOptions, function (axis, i) {
  9469. axis.index = i;
  9470. axis.isX = true;
  9471. });
  9472. each(yAxisOptions, function (axis, i) {
  9473. axis.index = i;
  9474. });
  9475. // concatenate all axis options into one array
  9476. optionsArray = xAxisOptions.concat(yAxisOptions);
  9477. each(optionsArray, function (axisOptions) {
  9478. axis = new Axis(chart, axisOptions);
  9479. });
  9480. chart.adjustTickAmounts();
  9481. },
  9482. /**
  9483. * Get the currently selected points from all series
  9484. */
  9485. getSelectedPoints: function () {
  9486. var points = [];
  9487. each(this.series, function (serie) {
  9488. points = points.concat(grep(serie.points || [], function (point) {
  9489. return point.selected;
  9490. }));
  9491. });
  9492. return points;
  9493. },
  9494. /**
  9495. * Get the currently selected series
  9496. */
  9497. getSelectedSeries: function () {
  9498. return grep(this.series, function (serie) {
  9499. return serie.selected;
  9500. });
  9501. },
  9502. /**
  9503. * Generate stacks for each series and calculate stacks total values
  9504. */
  9505. getStacks: function () {
  9506. var chart = this;
  9507. // reset stacks for each yAxis
  9508. each(chart.yAxis, function (axis) {
  9509. if (axis.stacks && axis.hasVisibleSeries) {
  9510. axis.oldStacks = axis.stacks;
  9511. }
  9512. });
  9513. each(chart.series, function (series) {
  9514. if (series.options.stacking && (series.visible === true || chart.options.chart.ignoreHiddenSeries === false)) {
  9515. series.stackKey = series.type + pick(series.options.stack, '');
  9516. }
  9517. });
  9518. },
  9519. /**
  9520. * Display the zoom button
  9521. */
  9522. showResetZoom: function () {
  9523. var chart = this,
  9524. lang = defaultOptions.lang,
  9525. btnOptions = chart.options.chart.resetZoomButton,
  9526. theme = btnOptions.theme,
  9527. states = theme.states,
  9528. alignTo = btnOptions.relativeTo === 'chart' ? null : 'plotBox';
  9529. this.resetZoomButton = chart.renderer.button(lang.resetZoom, null, null, function () {
  9530. chart.zoomOut();
  9531. }, theme, states && states.hover)
  9532. .attr({
  9533. align: btnOptions.position.align,
  9534. title: lang.resetZoomTitle
  9535. })
  9536. .add()
  9537. .align(btnOptions.position, false, alignTo);
  9538. },
  9539. /**
  9540. * Zoom out to 1:1
  9541. */
  9542. zoomOut: function () {
  9543. var chart = this;
  9544. fireEvent(chart, 'selection', {resetSelection: true}, function () {
  9545. chart.zoom();
  9546. });
  9547. },
  9548. /**
  9549. * Zoom into a given portion of the chart given by axis coordinates
  9550. * @param {Object} event
  9551. */
  9552. zoom: function (event) {
  9553. var chart = this,
  9554. hasZoomed,
  9555. pointer = chart.pointer,
  9556. displayButton = false,
  9557. resetZoomButton;
  9558. // If zoom is called with no arguments, reset the axes
  9559. if (!event || event.resetSelection) {
  9560. each(chart.axes, function (axis) {
  9561. hasZoomed = axis.zoom();
  9562. });
  9563. } else { // else, zoom in on all axes
  9564. each(event.xAxis.concat(event.yAxis), function (axisData) {
  9565. var axis = axisData.axis,
  9566. isXAxis = axis.isXAxis;
  9567. // don't zoom more than minRange
  9568. if (pointer[isXAxis ? 'zoomX' : 'zoomY'] || pointer[isXAxis ? 'pinchX' : 'pinchY']) {
  9569. hasZoomed = axis.zoom(axisData.min, axisData.max);
  9570. if (axis.displayBtn) {
  9571. displayButton = true;
  9572. }
  9573. }
  9574. });
  9575. }
  9576. // Show or hide the Reset zoom button
  9577. resetZoomButton = chart.resetZoomButton;
  9578. if (displayButton && !resetZoomButton) {
  9579. chart.showResetZoom();
  9580. } else if (!displayButton && isObject(resetZoomButton)) {
  9581. chart.resetZoomButton = resetZoomButton.destroy();
  9582. }
  9583. // Redraw
  9584. if (hasZoomed) {
  9585. chart.redraw(
  9586. pick(chart.options.chart.animation, event && event.animation, chart.pointCount < 100) // animation
  9587. );
  9588. }
  9589. },
  9590. /**
  9591. * Pan the chart by dragging the mouse across the pane. This function is called
  9592. * on mouse move, and the distance to pan is computed from chartX compared to
  9593. * the first chartX position in the dragging operation.
  9594. */
  9595. pan: function (e, panning) {
  9596. var chart = this,
  9597. hoverPoints = chart.hoverPoints,
  9598. doRedraw;
  9599. // remove active points for shared tooltip
  9600. if (hoverPoints) {
  9601. each(hoverPoints, function (point) {
  9602. point.setState();
  9603. });
  9604. }
  9605. each(panning === 'xy' ? [1, 0] : [1], function (isX) { // xy is used in maps
  9606. var mousePos = e[isX ? 'chartX' : 'chartY'],
  9607. axis = chart[isX ? 'xAxis' : 'yAxis'][0],
  9608. startPos = chart[isX ? 'mouseDownX' : 'mouseDownY'],
  9609. halfPointRange = (axis.pointRange || 0) / 2,
  9610. extremes = axis.getExtremes(),
  9611. newMin = axis.toValue(startPos - mousePos, true) + halfPointRange,
  9612. newMax = axis.toValue(startPos + chart[isX ? 'plotWidth' : 'plotHeight'] - mousePos, true) - halfPointRange;
  9613. if (axis.series.length && newMin > mathMin(extremes.dataMin, extremes.min) && newMax < mathMax(extremes.dataMax, extremes.max)) {
  9614. axis.setExtremes(newMin, newMax, false, false, {trigger: 'pan'});
  9615. doRedraw = true;
  9616. }
  9617. chart[isX ? 'mouseDownX' : 'mouseDownY'] = mousePos; // set new reference for next run
  9618. });
  9619. if (doRedraw) {
  9620. chart.redraw(false);
  9621. }
  9622. css(chart.container, {cursor: 'move'});
  9623. },
  9624. /**
  9625. * Show the title and subtitle of the chart
  9626. *
  9627. * @param titleOptions {Object} New title options
  9628. * @param subtitleOptions {Object} New subtitle options
  9629. *
  9630. */
  9631. setTitle: function (titleOptions, subtitleOptions) {
  9632. var chart = this,
  9633. options = chart.options,
  9634. chartTitleOptions,
  9635. chartSubtitleOptions;
  9636. chartTitleOptions = options.title = merge(options.title, titleOptions);
  9637. chartSubtitleOptions = options.subtitle = merge(options.subtitle, subtitleOptions);
  9638. // add title and subtitle
  9639. each([
  9640. ['title', titleOptions, chartTitleOptions],
  9641. ['subtitle', subtitleOptions, chartSubtitleOptions]
  9642. ], function (arr) {
  9643. var name = arr[0],
  9644. title = chart[name],
  9645. titleOptions = arr[1],
  9646. chartTitleOptions = arr[2];
  9647. if (title && titleOptions) {
  9648. chart[name] = title = title.destroy(); // remove old
  9649. }
  9650. if (chartTitleOptions && chartTitleOptions.text && !title) {
  9651. chart[name] = chart.renderer.text(
  9652. chartTitleOptions.text,
  9653. 0,
  9654. 0,
  9655. chartTitleOptions.useHTML
  9656. )
  9657. .attr({
  9658. align: chartTitleOptions.align,
  9659. 'class': PREFIX + name,
  9660. zIndex: chartTitleOptions.zIndex || 4
  9661. })
  9662. .css(chartTitleOptions.style)
  9663. .add();
  9664. }
  9665. });
  9666. chart.layOutTitles();
  9667. },
  9668. /**
  9669. * Lay out the chart titles and cache the full offset height for use in getMargins
  9670. */
  9671. layOutTitles: function () {
  9672. var titleOffset = 0,
  9673. title = this.title,
  9674. subtitle = this.subtitle,
  9675. options = this.options,
  9676. titleOptions = options.title,
  9677. subtitleOptions = options.subtitle,
  9678. autoWidth = this.spacingBox.width - 44; // 44 makes room for default context button
  9679. if (title) {
  9680. title
  9681. .css({width: (titleOptions.width || autoWidth) + PX})
  9682. .align(extend({y: 15}, titleOptions), false, 'spacingBox');
  9683. if (!titleOptions.floating && !titleOptions.verticalAlign) {
  9684. titleOffset = title.getBBox().height;
  9685. // Adjust for browser consistency + backwards compat after #776 fix
  9686. if (titleOffset >= 18 && titleOffset <= 25) {
  9687. titleOffset = 15;
  9688. }
  9689. }
  9690. }
  9691. if (subtitle) {
  9692. subtitle
  9693. .css({width: (subtitleOptions.width || autoWidth) + PX})
  9694. .align(extend({y: titleOffset + titleOptions.margin}, subtitleOptions), false, 'spacingBox');
  9695. if (!subtitleOptions.floating && !subtitleOptions.verticalAlign) {
  9696. titleOffset = mathCeil(titleOffset + subtitle.getBBox().height);
  9697. }
  9698. }
  9699. this.titleOffset = titleOffset; // used in getMargins
  9700. },
  9701. /**
  9702. * Get chart width and height according to options and container size
  9703. */
  9704. getChartSize: function () {
  9705. var chart = this,
  9706. optionsChart = chart.options.chart,
  9707. renderTo = chart.renderToClone || chart.renderTo;
  9708. // get inner width and height from jQuery (#824)
  9709. chart.containerWidth = adapterRun(renderTo, 'width');
  9710. chart.containerHeight = adapterRun(renderTo, 'height');
  9711. chart.chartWidth = mathMax(0, optionsChart.width || chart.containerWidth || 600); // #1393, 1460
  9712. chart.chartHeight = mathMax(0, pick(optionsChart.height,
  9713. // the offsetHeight of an empty container is 0 in standard browsers, but 19 in IE7:
  9714. chart.containerHeight > 19 ? chart.containerHeight : 400));
  9715. },
  9716. /**
  9717. * Create a clone of the chart's renderTo div and place it outside the viewport to allow
  9718. * size computation on chart.render and chart.redraw
  9719. */
  9720. cloneRenderTo: function (revert) {
  9721. var clone = this.renderToClone,
  9722. container = this.container;
  9723. // Destroy the clone and bring the container back to the real renderTo div
  9724. if (revert) {
  9725. if (clone) {
  9726. this.renderTo.appendChild(container);
  9727. discardElement(clone);
  9728. delete this.renderToClone;
  9729. }
  9730. // Set up the clone
  9731. } else {
  9732. if (container && container.parentNode === this.renderTo) {
  9733. this.renderTo.removeChild(container); // do not clone this
  9734. }
  9735. this.renderToClone = clone = this.renderTo.cloneNode(0);
  9736. css(clone, {
  9737. position: ABSOLUTE,
  9738. top: '-9999px',
  9739. display: 'block' // #833
  9740. });
  9741. doc.body.appendChild(clone);
  9742. if (container) {
  9743. clone.appendChild(container);
  9744. }
  9745. }
  9746. },
  9747. /**
  9748. * Get the containing element, determine the size and create the inner container
  9749. * div to hold the chart
  9750. */
  9751. getContainer: function () {
  9752. var chart = this,
  9753. container,
  9754. optionsChart = chart.options.chart,
  9755. chartWidth,
  9756. chartHeight,
  9757. renderTo,
  9758. indexAttrName = 'data-highcharts-chart',
  9759. oldChartIndex,
  9760. containerId;
  9761. chart.renderTo = renderTo = optionsChart.renderTo;
  9762. containerId = PREFIX + idCounter++;
  9763. if (isString(renderTo)) {
  9764. chart.renderTo = renderTo = doc.getElementById(renderTo);
  9765. }
  9766. // Display an error if the renderTo is wrong
  9767. if (!renderTo) {
  9768. error(13, true);
  9769. }
  9770. // If the container already holds a chart, destroy it
  9771. oldChartIndex = pInt(attr(renderTo, indexAttrName));
  9772. if (!isNaN(oldChartIndex) && charts[oldChartIndex]) {
  9773. charts[oldChartIndex].destroy();
  9774. }
  9775. // Make a reference to the chart from the div
  9776. attr(renderTo, indexAttrName, chart.index);
  9777. // remove previous chart
  9778. renderTo.innerHTML = '';
  9779. // If the container doesn't have an offsetWidth, it has or is a child of a node
  9780. // that has display:none. We need to temporarily move it out to a visible
  9781. // state to determine the size, else the legend and tooltips won't render
  9782. // properly
  9783. if (!renderTo.offsetWidth) {
  9784. chart.cloneRenderTo();
  9785. }
  9786. // get the width and height
  9787. chart.getChartSize();
  9788. chartWidth = chart.chartWidth;
  9789. chartHeight = chart.chartHeight;
  9790. // create the inner container
  9791. chart.container = container = createElement(DIV, {
  9792. className: PREFIX + 'container' +
  9793. (optionsChart.className ? ' ' + optionsChart.className : ''),
  9794. id: containerId
  9795. }, extend({
  9796. position: RELATIVE,
  9797. overflow: HIDDEN, // needed for context menu (avoid scrollbars) and
  9798. // content overflow in IE
  9799. width: chartWidth + PX,
  9800. height: chartHeight + PX,
  9801. textAlign: 'left',
  9802. lineHeight: 'normal', // #427
  9803. zIndex: 0, // #1072
  9804. '-webkit-tap-highlight-color': 'rgba(0,0,0,0)'
  9805. }, optionsChart.style),
  9806. chart.renderToClone || renderTo
  9807. );
  9808. // cache the cursor (#1650)
  9809. chart._cursor = container.style.cursor;
  9810. chart.renderer =
  9811. optionsChart.forExport ? // force SVG, used for SVG export
  9812. new SVGRenderer(container, chartWidth, chartHeight, true) :
  9813. new Renderer(container, chartWidth, chartHeight);
  9814. if (useCanVG) {
  9815. // If we need canvg library, extend and configure the renderer
  9816. // to get the tracker for translating mouse events
  9817. chart.renderer.create(chart, container, chartWidth, chartHeight);
  9818. }
  9819. },
  9820. /**
  9821. * Calculate margins by rendering axis labels in a preliminary position. Title,
  9822. * subtitle and legend have already been rendered at this stage, but will be
  9823. * moved into their final positions
  9824. */
  9825. getMargins: function () {
  9826. var chart = this,
  9827. spacing = chart.spacing,
  9828. axisOffset,
  9829. legend = chart.legend,
  9830. margin = chart.margin,
  9831. legendOptions = chart.options.legend,
  9832. legendMargin = pick(legendOptions.margin, 10),
  9833. legendX = legendOptions.x,
  9834. legendY = legendOptions.y,
  9835. align = legendOptions.align,
  9836. verticalAlign = legendOptions.verticalAlign,
  9837. titleOffset = chart.titleOffset;
  9838. chart.resetMargins();
  9839. axisOffset = chart.axisOffset;
  9840. // Adjust for title and subtitle
  9841. if (titleOffset && !defined(margin[0])) {
  9842. chart.plotTop = mathMax(chart.plotTop, titleOffset + chart.options.title.margin + spacing[0]);
  9843. }
  9844. // Adjust for legend
  9845. if (legend.display && !legendOptions.floating) {
  9846. if (align === 'right') { // horizontal alignment handled first
  9847. if (!defined(margin[1])) {
  9848. chart.marginRight = mathMax(
  9849. chart.marginRight,
  9850. legend.legendWidth - legendX + legendMargin + spacing[1]
  9851. );
  9852. }
  9853. } else if (align === 'left') {
  9854. if (!defined(margin[3])) {
  9855. chart.plotLeft = mathMax(
  9856. chart.plotLeft,
  9857. legend.legendWidth + legendX + legendMargin + spacing[3]
  9858. );
  9859. }
  9860. } else if (verticalAlign === 'top') {
  9861. if (!defined(margin[0])) {
  9862. chart.plotTop = mathMax(
  9863. chart.plotTop,
  9864. legend.legendHeight + legendY + legendMargin + spacing[0]
  9865. );
  9866. }
  9867. } else if (verticalAlign === 'bottom') {
  9868. if (!defined(margin[2])) {
  9869. chart.marginBottom = mathMax(
  9870. chart.marginBottom,
  9871. legend.legendHeight - legendY + legendMargin + spacing[2]
  9872. );
  9873. }
  9874. }
  9875. }
  9876. // adjust for scroller
  9877. if (chart.extraBottomMargin) {
  9878. chart.marginBottom += chart.extraBottomMargin;
  9879. }
  9880. if (chart.extraTopMargin) {
  9881. chart.plotTop += chart.extraTopMargin;
  9882. }
  9883. // pre-render axes to get labels offset width
  9884. if (chart.hasCartesianSeries) {
  9885. each(chart.axes, function (axis) {
  9886. axis.getOffset();
  9887. });
  9888. }
  9889. if (!defined(margin[3])) {
  9890. chart.plotLeft += axisOffset[3];
  9891. }
  9892. if (!defined(margin[0])) {
  9893. chart.plotTop += axisOffset[0];
  9894. }
  9895. if (!defined(margin[2])) {
  9896. chart.marginBottom += axisOffset[2];
  9897. }
  9898. if (!defined(margin[1])) {
  9899. chart.marginRight += axisOffset[1];
  9900. }
  9901. chart.setChartSize();
  9902. },
  9903. /**
  9904. * Add the event handlers necessary for auto resizing
  9905. *
  9906. */
  9907. initReflow: function () {
  9908. var chart = this,
  9909. optionsChart = chart.options.chart,
  9910. renderTo = chart.renderTo,
  9911. reflowTimeout;
  9912. function reflow(e) {
  9913. var width = optionsChart.width || adapterRun(renderTo, 'width'),
  9914. height = optionsChart.height || adapterRun(renderTo, 'height'),
  9915. target = e ? e.target : win; // #805 - MooTools doesn't supply e
  9916. // Width and height checks for display:none. Target is doc in IE8 and Opera,
  9917. // win in Firefox, Chrome and IE9.
  9918. if (!chart.hasUserSize && width && height && (target === win || target === doc)) {
  9919. if (width !== chart.containerWidth || height !== chart.containerHeight) {
  9920. clearTimeout(reflowTimeout);
  9921. chart.reflowTimeout = reflowTimeout = setTimeout(function () {
  9922. if (chart.container) { // It may have been destroyed in the meantime (#1257)
  9923. chart.setSize(width, height, false);
  9924. chart.hasUserSize = null;
  9925. }
  9926. }, 100);
  9927. }
  9928. chart.containerWidth = width;
  9929. chart.containerHeight = height;
  9930. }
  9931. }
  9932. chart.reflow = reflow;
  9933. addEvent(win, 'resize', reflow);
  9934. addEvent(chart, 'destroy', function () {
  9935. removeEvent(win, 'resize', reflow);
  9936. });
  9937. },
  9938. /**
  9939. * Resize the chart to a given width and height
  9940. * @param {Number} width
  9941. * @param {Number} height
  9942. * @param {Object|Boolean} animation
  9943. */
  9944. setSize: function (width, height, animation) {
  9945. var chart = this,
  9946. chartWidth,
  9947. chartHeight,
  9948. fireEndResize;
  9949. // Handle the isResizing counter
  9950. chart.isResizing += 1;
  9951. fireEndResize = function () {
  9952. if (chart) {
  9953. fireEvent(chart, 'endResize', null, function () {
  9954. chart.isResizing -= 1;
  9955. });
  9956. }
  9957. };
  9958. // set the animation for the current process
  9959. setAnimation(animation, chart);
  9960. chart.oldChartHeight = chart.chartHeight;
  9961. chart.oldChartWidth = chart.chartWidth;
  9962. if (defined(width)) {
  9963. chart.chartWidth = chartWidth = mathMax(0, mathRound(width));
  9964. chart.hasUserSize = !!chartWidth;
  9965. }
  9966. if (defined(height)) {
  9967. chart.chartHeight = chartHeight = mathMax(0, mathRound(height));
  9968. }
  9969. css(chart.container, {
  9970. width: chartWidth + PX,
  9971. height: chartHeight + PX
  9972. });
  9973. chart.setChartSize(true);
  9974. chart.renderer.setSize(chartWidth, chartHeight, animation);
  9975. // handle axes
  9976. chart.maxTicks = null;
  9977. each(chart.axes, function (axis) {
  9978. axis.isDirty = true;
  9979. axis.setScale();
  9980. });
  9981. // make sure non-cartesian series are also handled
  9982. each(chart.series, function (serie) {
  9983. serie.isDirty = true;
  9984. });
  9985. chart.isDirtyLegend = true; // force legend redraw
  9986. chart.isDirtyBox = true; // force redraw of plot and chart border
  9987. chart.getMargins();
  9988. chart.redraw(animation);
  9989. chart.oldChartHeight = null;
  9990. fireEvent(chart, 'resize');
  9991. // fire endResize and set isResizing back
  9992. // If animation is disabled, fire without delay
  9993. if (globalAnimation === false) {
  9994. fireEndResize();
  9995. } else { // else set a timeout with the animation duration
  9996. setTimeout(fireEndResize, (globalAnimation && globalAnimation.duration) || 500);
  9997. }
  9998. },
  9999. /**
  10000. * Set the public chart properties. This is done before and after the pre-render
  10001. * to determine margin sizes
  10002. */
  10003. setChartSize: function (skipAxes) {
  10004. var chart = this,
  10005. inverted = chart.inverted,
  10006. renderer = chart.renderer,
  10007. chartWidth = chart.chartWidth,
  10008. chartHeight = chart.chartHeight,
  10009. optionsChart = chart.options.chart,
  10010. spacing = chart.spacing,
  10011. clipOffset = chart.clipOffset,
  10012. clipX,
  10013. clipY,
  10014. plotLeft,
  10015. plotTop,
  10016. plotWidth,
  10017. plotHeight,
  10018. plotBorderWidth;
  10019. chart.plotLeft = plotLeft = mathRound(chart.plotLeft);
  10020. chart.plotTop = plotTop = mathRound(chart.plotTop);
  10021. chart.plotWidth = plotWidth = mathMax(0, mathRound(chartWidth - plotLeft - chart.marginRight));
  10022. chart.plotHeight = plotHeight = mathMax(0, mathRound(chartHeight - plotTop - chart.marginBottom));
  10023. chart.plotSizeX = inverted ? plotHeight : plotWidth;
  10024. chart.plotSizeY = inverted ? plotWidth : plotHeight;
  10025. chart.plotBorderWidth = optionsChart.plotBorderWidth || 0;
  10026. // Set boxes used for alignment
  10027. chart.spacingBox = renderer.spacingBox = {
  10028. x: spacing[3],
  10029. y: spacing[0],
  10030. width: chartWidth - spacing[3] - spacing[1],
  10031. height: chartHeight - spacing[0] - spacing[2]
  10032. };
  10033. chart.plotBox = renderer.plotBox = {
  10034. x: plotLeft,
  10035. y: plotTop,
  10036. width: plotWidth,
  10037. height: plotHeight
  10038. };
  10039. plotBorderWidth = 2 * mathFloor(chart.plotBorderWidth / 2);
  10040. clipX = mathCeil(mathMax(plotBorderWidth, clipOffset[3]) / 2);
  10041. clipY = mathCeil(mathMax(plotBorderWidth, clipOffset[0]) / 2);
  10042. chart.clipBox = {
  10043. x: clipX,
  10044. y: clipY,
  10045. width: mathFloor(chart.plotSizeX - mathMax(plotBorderWidth, clipOffset[1]) / 2 - clipX),
  10046. height: mathFloor(chart.plotSizeY - mathMax(plotBorderWidth, clipOffset[2]) / 2 - clipY)
  10047. };
  10048. if (!skipAxes) {
  10049. each(chart.axes, function (axis) {
  10050. axis.setAxisSize();
  10051. axis.setAxisTranslation();
  10052. });
  10053. }
  10054. },
  10055. /**
  10056. * Initial margins before auto size margins are applied
  10057. */
  10058. resetMargins: function () {
  10059. var chart = this,
  10060. spacing = chart.spacing,
  10061. margin = chart.margin;
  10062. chart.plotTop = pick(margin[0], spacing[0]);
  10063. chart.marginRight = pick(margin[1], spacing[1]);
  10064. chart.marginBottom = pick(margin[2], spacing[2]);
  10065. chart.plotLeft = pick(margin[3], spacing[3]);
  10066. chart.axisOffset = [0, 0, 0, 0]; // top, right, bottom, left
  10067. chart.clipOffset = [0, 0, 0, 0];
  10068. },
  10069. /**
  10070. * Draw the borders and backgrounds for chart and plot area
  10071. */
  10072. drawChartBox: function () {
  10073. var chart = this,
  10074. optionsChart = chart.options.chart,
  10075. renderer = chart.renderer,
  10076. chartWidth = chart.chartWidth,
  10077. chartHeight = chart.chartHeight,
  10078. chartBackground = chart.chartBackground,
  10079. plotBackground = chart.plotBackground,
  10080. plotBorder = chart.plotBorder,
  10081. plotBGImage = chart.plotBGImage,
  10082. chartBorderWidth = optionsChart.borderWidth || 0,
  10083. chartBackgroundColor = optionsChart.backgroundColor,
  10084. plotBackgroundColor = optionsChart.plotBackgroundColor,
  10085. plotBackgroundImage = optionsChart.plotBackgroundImage,
  10086. plotBorderWidth = optionsChart.plotBorderWidth || 0,
  10087. mgn,
  10088. bgAttr,
  10089. plotLeft = chart.plotLeft,
  10090. plotTop = chart.plotTop,
  10091. plotWidth = chart.plotWidth,
  10092. plotHeight = chart.plotHeight,
  10093. plotBox = chart.plotBox,
  10094. clipRect = chart.clipRect,
  10095. clipBox = chart.clipBox;
  10096. // Chart area
  10097. mgn = chartBorderWidth + (optionsChart.shadow ? 8 : 0);
  10098. if (chartBorderWidth || chartBackgroundColor) {
  10099. if (!chartBackground) {
  10100. bgAttr = {
  10101. fill: chartBackgroundColor || NONE
  10102. };
  10103. if (chartBorderWidth) { // #980
  10104. bgAttr.stroke = optionsChart.borderColor;
  10105. bgAttr['stroke-width'] = chartBorderWidth;
  10106. }
  10107. chart.chartBackground = renderer.rect(mgn / 2, mgn / 2, chartWidth - mgn, chartHeight - mgn,
  10108. optionsChart.borderRadius, chartBorderWidth)
  10109. .attr(bgAttr)
  10110. .add()
  10111. .shadow(optionsChart.shadow);
  10112. } else { // resize
  10113. chartBackground.animate(
  10114. chartBackground.crisp(null, null, null, chartWidth - mgn, chartHeight - mgn)
  10115. );
  10116. }
  10117. }
  10118. // Plot background
  10119. if (plotBackgroundColor) {
  10120. if (!plotBackground) {
  10121. chart.plotBackground = renderer.rect(plotLeft, plotTop, plotWidth, plotHeight, 0)
  10122. .attr({
  10123. fill: plotBackgroundColor
  10124. })
  10125. .add()
  10126. .shadow(optionsChart.plotShadow);
  10127. } else {
  10128. plotBackground.animate(plotBox);
  10129. }
  10130. }
  10131. if (plotBackgroundImage) {
  10132. if (!plotBGImage) {
  10133. chart.plotBGImage = renderer.image(plotBackgroundImage, plotLeft, plotTop, plotWidth, plotHeight)
  10134. .add();
  10135. } else {
  10136. plotBGImage.animate(plotBox);
  10137. }
  10138. }
  10139. // Plot clip
  10140. if (!clipRect) {
  10141. chart.clipRect = renderer.clipRect(clipBox);
  10142. } else {
  10143. clipRect.animate({
  10144. width: clipBox.width,
  10145. height: clipBox.height
  10146. });
  10147. }
  10148. // Plot area border
  10149. if (plotBorderWidth) {
  10150. if (!plotBorder) {
  10151. chart.plotBorder = renderer.rect(plotLeft, plotTop, plotWidth, plotHeight, 0, -plotBorderWidth)
  10152. .attr({
  10153. stroke: optionsChart.plotBorderColor,
  10154. 'stroke-width': plotBorderWidth,
  10155. zIndex: 1
  10156. })
  10157. .add();
  10158. } else {
  10159. plotBorder.animate(
  10160. plotBorder.crisp(null, plotLeft, plotTop, plotWidth, plotHeight)
  10161. );
  10162. }
  10163. }
  10164. // reset
  10165. chart.isDirtyBox = false;
  10166. },
  10167. /**
  10168. * Detect whether a certain chart property is needed based on inspecting its options
  10169. * and series. This mainly applies to the chart.invert property, and in extensions to
  10170. * the chart.angular and chart.polar properties.
  10171. */
  10172. propFromSeries: function () {
  10173. var chart = this,
  10174. optionsChart = chart.options.chart,
  10175. klass,
  10176. seriesOptions = chart.options.series,
  10177. i,
  10178. value;
  10179. each(['inverted', 'angular', 'polar'], function (key) {
  10180. // The default series type's class
  10181. klass = seriesTypes[optionsChart.type || optionsChart.defaultSeriesType];
  10182. // Get the value from available chart-wide properties
  10183. value = (
  10184. chart[key] || // 1. it is set before
  10185. optionsChart[key] || // 2. it is set in the options
  10186. (klass && klass.prototype[key]) // 3. it's default series class requires it
  10187. );
  10188. // 4. Check if any the chart's series require it
  10189. i = seriesOptions && seriesOptions.length;
  10190. while (!value && i--) {
  10191. klass = seriesTypes[seriesOptions[i].type];
  10192. if (klass && klass.prototype[key]) {
  10193. value = true;
  10194. }
  10195. }
  10196. // Set the chart property
  10197. chart[key] = value;
  10198. });
  10199. },
  10200. /**
  10201. * Link two or more series together. This is done initially from Chart.render,
  10202. * and after Chart.addSeries and Series.remove.
  10203. */
  10204. linkSeries: function () {
  10205. var chart = this,
  10206. chartSeries = chart.series;
  10207. // Reset links
  10208. each(chartSeries, function (series) {
  10209. series.linkedSeries.length = 0;
  10210. });
  10211. // Apply new links
  10212. each(chartSeries, function (series) {
  10213. var linkedTo = series.options.linkedTo;
  10214. if (isString(linkedTo)) {
  10215. if (linkedTo === ':previous') {
  10216. linkedTo = chart.series[series.index - 1];
  10217. } else {
  10218. linkedTo = chart.get(linkedTo);
  10219. }
  10220. if (linkedTo) {
  10221. linkedTo.linkedSeries.push(series);
  10222. series.linkedParent = linkedTo;
  10223. }
  10224. }
  10225. });
  10226. },
  10227. /**
  10228. * Render all graphics for the chart
  10229. */
  10230. render: function () {
  10231. var chart = this,
  10232. axes = chart.axes,
  10233. renderer = chart.renderer,
  10234. options = chart.options;
  10235. var labels = options.labels,
  10236. credits = options.credits,
  10237. creditsHref;
  10238. // Title
  10239. chart.setTitle();
  10240. // Legend
  10241. chart.legend = new Legend(chart, options.legend);
  10242. chart.getStacks(); // render stacks
  10243. // Get margins by pre-rendering axes
  10244. // set axes scales
  10245. each(axes, function (axis) {
  10246. axis.setScale();
  10247. });
  10248. chart.getMargins();
  10249. chart.maxTicks = null; // reset for second pass
  10250. each(axes, function (axis) {
  10251. axis.setTickPositions(true); // update to reflect the new margins
  10252. axis.setMaxTicks();
  10253. });
  10254. chart.adjustTickAmounts();
  10255. chart.getMargins(); // second pass to check for new labels
  10256. // Draw the borders and backgrounds
  10257. chart.drawChartBox();
  10258. // Axes
  10259. if (chart.hasCartesianSeries) {
  10260. each(axes, function (axis) {
  10261. axis.render();
  10262. });
  10263. }
  10264. // The series
  10265. if (!chart.seriesGroup) {
  10266. chart.seriesGroup = renderer.g('series-group')
  10267. .attr({zIndex: 3})
  10268. .add();
  10269. }
  10270. each(chart.series, function (serie) {
  10271. serie.translate();
  10272. serie.setTooltipPoints();
  10273. serie.render();
  10274. });
  10275. // Labels
  10276. if (labels.items) {
  10277. each(labels.items, function (label) {
  10278. var style = extend(labels.style, label.style),
  10279. x = pInt(style.left) + chart.plotLeft,
  10280. y = pInt(style.top) + chart.plotTop + 12;
  10281. // delete to prevent rewriting in IE
  10282. delete style.left;
  10283. delete style.top;
  10284. renderer.text(
  10285. label.html,
  10286. x,
  10287. y
  10288. )
  10289. .attr({zIndex: 2})
  10290. .css(style)
  10291. .add();
  10292. });
  10293. }
  10294. // Credits
  10295. if (credits.enabled && !chart.credits) {
  10296. creditsHref = credits.href;
  10297. chart.credits = renderer.text(
  10298. credits.text,
  10299. 0,
  10300. 0
  10301. )
  10302. .on('click', function () {
  10303. if (creditsHref) {
  10304. location.href = creditsHref;
  10305. }
  10306. })
  10307. .attr({
  10308. align: credits.position.align,
  10309. zIndex: 8
  10310. })
  10311. .css(credits.style)
  10312. .add()
  10313. .align(credits.position);
  10314. }
  10315. // Set flag
  10316. chart.hasRendered = true;
  10317. },
  10318. /**
  10319. * Clean up memory usage
  10320. */
  10321. destroy: function () {
  10322. var chart = this,
  10323. axes = chart.axes,
  10324. series = chart.series,
  10325. container = chart.container,
  10326. i,
  10327. parentNode = container && container.parentNode;
  10328. // fire the chart.destoy event
  10329. fireEvent(chart, 'destroy');
  10330. // Delete the chart from charts lookup array
  10331. charts[chart.index] = UNDEFINED;
  10332. chart.renderTo.removeAttribute('data-highcharts-chart');
  10333. // remove events
  10334. removeEvent(chart);
  10335. // ==== Destroy collections:
  10336. // Destroy axes
  10337. i = axes.length;
  10338. while (i--) {
  10339. axes[i] = axes[i].destroy();
  10340. }
  10341. // Destroy each series
  10342. i = series.length;
  10343. while (i--) {
  10344. series[i] = series[i].destroy();
  10345. }
  10346. // ==== Destroy chart properties:
  10347. each(['title', 'subtitle', 'chartBackground', 'plotBackground', 'plotBGImage',
  10348. 'plotBorder', 'seriesGroup', 'clipRect', 'credits', 'pointer', 'scroller',
  10349. 'rangeSelector', 'legend', 'resetZoomButton', 'tooltip', 'renderer'], function (name) {
  10350. var prop = chart[name];
  10351. if (prop && prop.destroy) {
  10352. chart[name] = prop.destroy();
  10353. }
  10354. });
  10355. // remove container and all SVG
  10356. if (container) { // can break in IE when destroyed before finished loading
  10357. container.innerHTML = '';
  10358. removeEvent(container);
  10359. if (parentNode) {
  10360. discardElement(container);
  10361. }
  10362. }
  10363. // clean it all up
  10364. for (i in chart) {
  10365. delete chart[i];
  10366. }
  10367. },
  10368. /**
  10369. * VML namespaces can't be added until after complete. Listening
  10370. * for Perini's doScroll hack is not enough.
  10371. */
  10372. isReadyToRender: function () {
  10373. var chart = this;
  10374. // Note: in spite of JSLint's complaints, win == win.top is required
  10375. /*jslint eqeq: true*/
  10376. if ((!hasSVG && (win == win.top && doc.readyState !== 'complete')) || (useCanVG && !win.canvg)) {
  10377. /*jslint eqeq: false*/
  10378. if (useCanVG) {
  10379. // Delay rendering until canvg library is downloaded and ready
  10380. CanVGController.push(function () {
  10381. chart.firstRender();
  10382. }, chart.options.global.canvasToolsURL);
  10383. } else {
  10384. doc.attachEvent('onreadystatechange', function () {
  10385. doc.detachEvent('onreadystatechange', chart.firstRender);
  10386. if (doc.readyState === 'complete') {
  10387. chart.firstRender();
  10388. }
  10389. });
  10390. }
  10391. return false;
  10392. }
  10393. return true;
  10394. },
  10395. /**
  10396. * Prepare for first rendering after all data are loaded
  10397. */
  10398. firstRender: function () {
  10399. var chart = this,
  10400. options = chart.options,
  10401. callback = chart.callback;
  10402. // Check whether the chart is ready to render
  10403. if (!chart.isReadyToRender()) {
  10404. return;
  10405. }
  10406. // Create the container
  10407. chart.getContainer();
  10408. // Run an early event after the container and renderer are established
  10409. fireEvent(chart, 'init');
  10410. chart.resetMargins();
  10411. chart.setChartSize();
  10412. // Set the common chart properties (mainly invert) from the given series
  10413. chart.propFromSeries();
  10414. // get axes
  10415. chart.getAxes();
  10416. // Initialize the series
  10417. each(options.series || [], function (serieOptions) {
  10418. chart.initSeries(serieOptions);
  10419. });
  10420. chart.linkSeries();
  10421. // Run an event after axes and series are initialized, but before render. At this stage,
  10422. // the series data is indexed and cached in the xData and yData arrays, so we can access
  10423. // those before rendering. Used in Highstock.
  10424. fireEvent(chart, 'beforeRender');
  10425. // depends on inverted and on margins being set
  10426. chart.pointer = new Pointer(chart, options);
  10427. chart.render();
  10428. // add canvas
  10429. chart.renderer.draw();
  10430. // run callbacks
  10431. if (callback) {
  10432. callback.apply(chart, [chart]);
  10433. }
  10434. each(chart.callbacks, function (fn) {
  10435. fn.apply(chart, [chart]);
  10436. });
  10437. // If the chart was rendered outside the top container, put it back in
  10438. chart.cloneRenderTo(true);
  10439. fireEvent(chart, 'load');
  10440. },
  10441. /**
  10442. * Creates arrays for spacing and margin from given options.
  10443. */
  10444. splashArray: function (target, options) {
  10445. var oVar = options[target],
  10446. tArray = isObject(oVar) ? oVar : [oVar, oVar, oVar, oVar];
  10447. return [pick(options[target + 'Top'], tArray[0]),
  10448. pick(options[target + 'Right'], tArray[1]),
  10449. pick(options[target + 'Bottom'], tArray[2]),
  10450. pick(options[target + 'Left'], tArray[3])];
  10451. }
  10452. }; // end Chart
  10453. // Hook for exporting module
  10454. Chart.prototype.callbacks = [];
  10455. /**
  10456. * The Point object and prototype. Inheritable and used as base for PiePoint
  10457. */
  10458. var Point = function () {
  10459. };
  10460. Point.prototype = {
  10461. /**
  10462. * Initialize the point
  10463. * @param {Object} series The series object containing this point
  10464. * @param {Object} options The data in either number, array or object format
  10465. */
  10466. init: function (series, options, x) {
  10467. var point = this,
  10468. colors;
  10469. point.series = series;
  10470. point.applyOptions(options, x);
  10471. point.pointAttr = {};
  10472. if (series.options.colorByPoint) {
  10473. colors = series.options.colors || series.chart.options.colors;
  10474. point.color = point.color || colors[series.colorCounter++];
  10475. // loop back to zero
  10476. if (series.colorCounter === colors.length) {
  10477. series.colorCounter = 0;
  10478. }
  10479. }
  10480. series.chart.pointCount++;
  10481. return point;
  10482. },
  10483. /**
  10484. * Apply the options containing the x and y data and possible some extra properties.
  10485. * This is called on point init or from point.update.
  10486. *
  10487. * @param {Object} options
  10488. */
  10489. applyOptions: function (options, x) {
  10490. var point = this,
  10491. series = point.series,
  10492. pointValKey = series.pointValKey;
  10493. options = Point.prototype.optionsToObject.call(this, options);
  10494. // copy options directly to point
  10495. extend(point, options);
  10496. point.options = point.options ? extend(point.options, options) : options;
  10497. // For higher dimension series types. For instance, for ranges, point.y is mapped to point.low.
  10498. if (pointValKey) {
  10499. point.y = point[pointValKey];
  10500. }
  10501. // If no x is set by now, get auto incremented value. All points must have an
  10502. // x value, however the y value can be null to create a gap in the series
  10503. if (point.x === UNDEFINED && series) {
  10504. point.x = x === UNDEFINED ? series.autoIncrement() : x;
  10505. }
  10506. return point;
  10507. },
  10508. /**
  10509. * Transform number or array configs into objects
  10510. */
  10511. optionsToObject: function (options) {
  10512. var ret,
  10513. series = this.series,
  10514. pointArrayMap = series.pointArrayMap || ['y'],
  10515. valueCount = pointArrayMap.length,
  10516. firstItemType,
  10517. i = 0,
  10518. j = 0;
  10519. if (typeof options === 'number' || options === null) {
  10520. ret = {y: options};
  10521. } else if (isArray(options)) {
  10522. ret = {};
  10523. // with leading x value
  10524. if (options.length > valueCount) {
  10525. firstItemType = typeof options[0];
  10526. if (firstItemType === 'string') {
  10527. ret.name = options[0];
  10528. } else if (firstItemType === 'number') {
  10529. ret.x = options[0];
  10530. }
  10531. i++;
  10532. }
  10533. while (j < valueCount) {
  10534. ret[pointArrayMap[j++]] = options[i++];
  10535. }
  10536. } else if (typeof options === 'object') {
  10537. ret = options;
  10538. // This is the fastest way to detect if there are individual point dataLabels that need
  10539. // to be considered in drawDataLabels. These can only occur in object configs.
  10540. if (options.dataLabels) {
  10541. series._hasPointLabels = true;
  10542. }
  10543. // Same approach as above for markers
  10544. if (options.marker) {
  10545. series._hasPointMarkers = true;
  10546. }
  10547. }
  10548. return ret;
  10549. },
  10550. /**
  10551. * Destroy a point to clear memory. Its reference still stays in series.data.
  10552. */
  10553. destroy: function () {
  10554. var point = this,
  10555. series = point.series,
  10556. chart = series.chart,
  10557. hoverPoints = chart.hoverPoints,
  10558. prop;
  10559. chart.pointCount--;
  10560. if (hoverPoints) {
  10561. point.setState();
  10562. erase(hoverPoints, point);
  10563. if (!hoverPoints.length) {
  10564. chart.hoverPoints = null;
  10565. }
  10566. }
  10567. if (point === chart.hoverPoint) {
  10568. point.onMouseOut();
  10569. }
  10570. // remove all events
  10571. if (point.graphic || point.dataLabel) { // removeEvent and destroyElements are performance expensive
  10572. removeEvent(point);
  10573. point.destroyElements();
  10574. }
  10575. if (point.legendItem) { // pies have legend items
  10576. chart.legend.destroyItem(point);
  10577. }
  10578. for (prop in point) {
  10579. point[prop] = null;
  10580. }
  10581. },
  10582. /**
  10583. * Destroy SVG elements associated with the point
  10584. */
  10585. destroyElements: function () {
  10586. var point = this,
  10587. props = ['graphic', 'dataLabel', 'dataLabelUpper', 'group', 'connector', 'shadowGroup'],
  10588. prop,
  10589. i = 6;
  10590. while (i--) {
  10591. prop = props[i];
  10592. if (point[prop]) {
  10593. point[prop] = point[prop].destroy();
  10594. }
  10595. }
  10596. },
  10597. /**
  10598. * Return the configuration hash needed for the data label and tooltip formatters
  10599. */
  10600. getLabelConfig: function () {
  10601. var point = this;
  10602. return {
  10603. x: point.category,
  10604. y: point.y,
  10605. key: point.name || point.category,
  10606. series: point.series,
  10607. point: point,
  10608. percentage: point.percentage,
  10609. total: point.total || point.stackTotal
  10610. };
  10611. },
  10612. /**
  10613. * Toggle the selection status of a point
  10614. * @param {Boolean} selected Whether to select or unselect the point.
  10615. * @param {Boolean} accumulate Whether to add to the previous selection. By default,
  10616. * this happens if the control key (Cmd on Mac) was pressed during clicking.
  10617. */
  10618. select: function (selected, accumulate) {
  10619. var point = this,
  10620. series = point.series,
  10621. chart = series.chart;
  10622. selected = pick(selected, !point.selected);
  10623. // fire the event with the defalut handler
  10624. point.firePointEvent(selected ? 'select' : 'unselect', {accumulate: accumulate}, function () {
  10625. point.selected = point.options.selected = selected;
  10626. series.options.data[inArray(point, series.data)] = point.options;
  10627. point.setState(selected && SELECT_STATE);
  10628. // unselect all other points unless Ctrl or Cmd + click
  10629. if (!accumulate) {
  10630. each(chart.getSelectedPoints(), function (loopPoint) {
  10631. if (loopPoint.selected && loopPoint !== point) {
  10632. loopPoint.selected = loopPoint.options.selected = false;
  10633. series.options.data[inArray(loopPoint, series.data)] = loopPoint.options;
  10634. loopPoint.setState(NORMAL_STATE);
  10635. loopPoint.firePointEvent('unselect');
  10636. }
  10637. });
  10638. }
  10639. });
  10640. },
  10641. /**
  10642. * Runs on mouse over the point
  10643. */
  10644. onMouseOver: function (e) {
  10645. var point = this,
  10646. series = point.series,
  10647. chart = series.chart,
  10648. tooltip = chart.tooltip,
  10649. hoverPoint = chart.hoverPoint;
  10650. // set normal state to previous series
  10651. if (hoverPoint && hoverPoint !== point) {
  10652. hoverPoint.onMouseOut();
  10653. }
  10654. // trigger the event
  10655. point.firePointEvent('mouseOver');
  10656. // update the tooltip
  10657. if (tooltip && (!tooltip.shared || series.noSharedTooltip)) {
  10658. tooltip.refresh(point, e);
  10659. }
  10660. // hover this
  10661. point.setState(HOVER_STATE);
  10662. chart.hoverPoint = point;
  10663. },
  10664. /**
  10665. * Runs on mouse out from the point
  10666. */
  10667. onMouseOut: function () {
  10668. var chart = this.series.chart,
  10669. hoverPoints = chart.hoverPoints;
  10670. if (!hoverPoints || inArray(this, hoverPoints) === -1) { // #887
  10671. this.firePointEvent('mouseOut');
  10672. this.setState();
  10673. chart.hoverPoint = null;
  10674. }
  10675. },
  10676. /**
  10677. * Extendable method for formatting each point's tooltip line
  10678. *
  10679. * @return {String} A string to be concatenated in to the common tooltip text
  10680. */
  10681. tooltipFormatter: function (pointFormat) {
  10682. // Insert options for valueDecimals, valuePrefix, and valueSuffix
  10683. var series = this.series,
  10684. seriesTooltipOptions = series.tooltipOptions,
  10685. valueDecimals = pick(seriesTooltipOptions.valueDecimals, ''),
  10686. valuePrefix = seriesTooltipOptions.valuePrefix || '',
  10687. valueSuffix = seriesTooltipOptions.valueSuffix || '';
  10688. // Loop over the point array map and replace unformatted values with sprintf formatting markup
  10689. each(series.pointArrayMap || ['y'], function (key) {
  10690. key = '{point.' + key; // without the closing bracket
  10691. if (valuePrefix || valueSuffix) {
  10692. pointFormat = pointFormat.replace(key + '}', valuePrefix + key + '}' + valueSuffix);
  10693. }
  10694. pointFormat = pointFormat.replace(key + '}', key + ':,.' + valueDecimals + 'f}');
  10695. });
  10696. return format(pointFormat, {
  10697. point: this,
  10698. series: this.series
  10699. });
  10700. },
  10701. /**
  10702. * Update the point with new options (typically x/y data) and optionally redraw the series.
  10703. *
  10704. * @param {Object} options Point options as defined in the series.data array
  10705. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  10706. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  10707. * configuration
  10708. *
  10709. */
  10710. update: function (options, redraw, animation) {
  10711. var point = this,
  10712. series = point.series,
  10713. graphic = point.graphic,
  10714. i,
  10715. data = series.data,
  10716. chart = series.chart,
  10717. seriesOptions = series.options;
  10718. redraw = pick(redraw, true);
  10719. // fire the event with a default handler of doing the update
  10720. point.firePointEvent('update', {options: options}, function () {
  10721. point.applyOptions(options);
  10722. // update visuals
  10723. if (isObject(options)) {
  10724. series.getAttribs();
  10725. if (graphic) {
  10726. if (options.marker && options.marker.symbol) {
  10727. point.graphic = graphic.destroy();
  10728. } else {
  10729. graphic.attr(point.pointAttr[point.state || '']);
  10730. }
  10731. }
  10732. }
  10733. // record changes in the parallel arrays
  10734. i = inArray(point, data);
  10735. series.xData[i] = point.x;
  10736. series.yData[i] = series.toYData ? series.toYData(point) : point.y;
  10737. series.zData[i] = point.z;
  10738. seriesOptions.data[i] = point.options;
  10739. // redraw
  10740. series.isDirty = series.isDirtyData = true;
  10741. if (!series.fixedBox && series.hasCartesianSeries) { // #1906, #2320
  10742. chart.isDirtyBox = true;
  10743. }
  10744. if (seriesOptions.legendType === 'point') { // #1831, #1885
  10745. chart.legend.destroyItem(point);
  10746. }
  10747. if (redraw) {
  10748. chart.redraw(animation);
  10749. }
  10750. });
  10751. },
  10752. /**
  10753. * Remove a point and optionally redraw the series and if necessary the axes
  10754. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  10755. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  10756. * configuration
  10757. */
  10758. remove: function (redraw, animation) {
  10759. var point = this,
  10760. series = point.series,
  10761. points = series.points,
  10762. chart = series.chart,
  10763. i,
  10764. data = series.data;
  10765. setAnimation(animation, chart);
  10766. redraw = pick(redraw, true);
  10767. // fire the event with a default handler of removing the point
  10768. point.firePointEvent('remove', null, function () {
  10769. // splice all the parallel arrays
  10770. i = inArray(point, data);
  10771. if (data.length === points.length) {
  10772. points.splice(i, 1);
  10773. }
  10774. data.splice(i, 1);
  10775. series.options.data.splice(i, 1);
  10776. series.xData.splice(i, 1);
  10777. series.yData.splice(i, 1);
  10778. series.zData.splice(i, 1);
  10779. point.destroy();
  10780. // redraw
  10781. series.isDirty = true;
  10782. series.isDirtyData = true;
  10783. if (redraw) {
  10784. chart.redraw();
  10785. }
  10786. });
  10787. },
  10788. /**
  10789. * Fire an event on the Point object. Must not be renamed to fireEvent, as this
  10790. * causes a name clash in MooTools
  10791. * @param {String} eventType
  10792. * @param {Object} eventArgs Additional event arguments
  10793. * @param {Function} defaultFunction Default event handler
  10794. */
  10795. firePointEvent: function (eventType, eventArgs, defaultFunction) {
  10796. var point = this,
  10797. series = this.series,
  10798. seriesOptions = series.options;
  10799. // load event handlers on demand to save time on mouseover/out
  10800. if (seriesOptions.point.events[eventType] || (point.options && point.options.events && point.options.events[eventType])) {
  10801. this.importEvents();
  10802. }
  10803. // add default handler if in selection mode
  10804. if (eventType === 'click' && seriesOptions.allowPointSelect) {
  10805. defaultFunction = function (event) {
  10806. // Control key is for Windows, meta (= Cmd key) for Mac, Shift for Opera
  10807. point.select(null, event.ctrlKey || event.metaKey || event.shiftKey);
  10808. };
  10809. }
  10810. fireEvent(this, eventType, eventArgs, defaultFunction);
  10811. },
  10812. /**
  10813. * Import events from the series' and point's options. Only do it on
  10814. * demand, to save processing time on hovering.
  10815. */
  10816. importEvents: function () {
  10817. if (!this.hasImportedEvents) {
  10818. var point = this,
  10819. options = merge(point.series.options.point, point.options),
  10820. events = options.events,
  10821. eventType;
  10822. point.events = events;
  10823. for (eventType in events) {
  10824. addEvent(point, eventType, events[eventType]);
  10825. }
  10826. this.hasImportedEvents = true;
  10827. }
  10828. },
  10829. /**
  10830. * Set the point's state
  10831. * @param {String} state
  10832. */
  10833. setState: function (state) {
  10834. var point = this,
  10835. plotX = point.plotX,
  10836. plotY = point.plotY,
  10837. series = point.series,
  10838. stateOptions = series.options.states,
  10839. markerOptions = defaultPlotOptions[series.type].marker && series.options.marker,
  10840. normalDisabled = markerOptions && !markerOptions.enabled,
  10841. markerStateOptions = markerOptions && markerOptions.states[state],
  10842. stateDisabled = markerStateOptions && markerStateOptions.enabled === false,
  10843. stateMarkerGraphic = series.stateMarkerGraphic,
  10844. pointMarker = point.marker || {},
  10845. chart = series.chart,
  10846. radius,
  10847. newSymbol,
  10848. pointAttr = point.pointAttr;
  10849. state = state || NORMAL_STATE; // empty string
  10850. if (
  10851. // already has this state
  10852. state === point.state ||
  10853. // selected points don't respond to hover
  10854. (point.selected && state !== SELECT_STATE) ||
  10855. // series' state options is disabled
  10856. (stateOptions[state] && stateOptions[state].enabled === false) ||
  10857. // point marker's state options is disabled
  10858. (state && (stateDisabled || (normalDisabled && !markerStateOptions.enabled)))
  10859. ) {
  10860. return;
  10861. }
  10862. // apply hover styles to the existing point
  10863. if (point.graphic) {
  10864. radius = markerOptions && point.graphic.symbolName && pointAttr[state].r;
  10865. point.graphic.attr(merge(
  10866. pointAttr[state],
  10867. radius ? { // new symbol attributes (#507, #612)
  10868. x: plotX - radius,
  10869. y: plotY - radius,
  10870. width: 2 * radius,
  10871. height: 2 * radius
  10872. } : {}
  10873. ));
  10874. } else {
  10875. // if a graphic is not applied to each point in the normal state, create a shared
  10876. // graphic for the hover state
  10877. if (state && markerStateOptions) {
  10878. radius = markerStateOptions.radius;
  10879. newSymbol = pointMarker.symbol || series.symbol;
  10880. // If the point has another symbol than the previous one, throw away the
  10881. // state marker graphic and force a new one (#1459)
  10882. if (stateMarkerGraphic && stateMarkerGraphic.currentSymbol !== newSymbol) {
  10883. stateMarkerGraphic = stateMarkerGraphic.destroy();
  10884. }
  10885. // Add a new state marker graphic
  10886. if (!stateMarkerGraphic) {
  10887. series.stateMarkerGraphic = stateMarkerGraphic = chart.renderer.symbol(
  10888. newSymbol,
  10889. plotX - radius,
  10890. plotY - radius,
  10891. 2 * radius,
  10892. 2 * radius
  10893. )
  10894. .attr(pointAttr[state])
  10895. .add(series.markerGroup);
  10896. stateMarkerGraphic.currentSymbol = newSymbol;
  10897. // Move the existing graphic
  10898. } else {
  10899. stateMarkerGraphic.attr({ // #1054
  10900. x: plotX - radius,
  10901. y: plotY - radius
  10902. });
  10903. }
  10904. }
  10905. if (stateMarkerGraphic) {
  10906. stateMarkerGraphic[state && chart.isInsidePlot(plotX, plotY) ? 'show' : 'hide']();
  10907. }
  10908. }
  10909. point.state = state;
  10910. }
  10911. };
  10912. /**
  10913. * @classDescription The base function which all other series types inherit from. The data in the series is stored
  10914. * in various arrays.
  10915. *
  10916. * - First, series.options.data contains all the original config options for
  10917. * each point whether added by options or methods like series.addPoint.
  10918. * - Next, series.data contains those values converted to points, but in case the series data length
  10919. * exceeds the cropThreshold, or if the data is grouped, series.data doesn't contain all the points. It
  10920. * only contains the points that have been created on demand.
  10921. * - Then there's series.points that contains all currently visible point objects. In case of cropping,
  10922. * the cropped-away points are not part of this array. The series.points array starts at series.cropStart
  10923. * compared to series.data and series.options.data. If however the series data is grouped, these can't
  10924. * be correlated one to one.
  10925. * - series.xData and series.processedXData contain clean x values, equivalent to series.data and series.points.
  10926. * - series.yData and series.processedYData contain clean x values, equivalent to series.data and series.points.
  10927. *
  10928. * @param {Object} chart
  10929. * @param {Object} options
  10930. */
  10931. var Series = function () {
  10932. };
  10933. Series.prototype = {
  10934. isCartesian: true,
  10935. type: 'line',
  10936. pointClass: Point,
  10937. sorted: true, // requires the data to be sorted
  10938. requireSorting: true,
  10939. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  10940. stroke: 'lineColor',
  10941. 'stroke-width': 'lineWidth',
  10942. fill: 'fillColor',
  10943. r: 'radius'
  10944. },
  10945. colorCounter: 0,
  10946. init: function (chart, options) {
  10947. var series = this,
  10948. eventType,
  10949. events,
  10950. chartSeries = chart.series;
  10951. series.chart = chart;
  10952. series.options = options = series.setOptions(options); // merge with plotOptions
  10953. series.linkedSeries = [];
  10954. // bind the axes
  10955. series.bindAxes();
  10956. // set some variables
  10957. extend(series, {
  10958. name: options.name,
  10959. state: NORMAL_STATE,
  10960. pointAttr: {},
  10961. visible: options.visible !== false, // true by default
  10962. selected: options.selected === true // false by default
  10963. });
  10964. // special
  10965. if (useCanVG) {
  10966. options.animation = false;
  10967. }
  10968. // register event listeners
  10969. events = options.events;
  10970. for (eventType in events) {
  10971. addEvent(series, eventType, events[eventType]);
  10972. }
  10973. if (
  10974. (events && events.click) ||
  10975. (options.point && options.point.events && options.point.events.click) ||
  10976. options.allowPointSelect
  10977. ) {
  10978. chart.runTrackerClick = true;
  10979. }
  10980. series.getColor();
  10981. series.getSymbol();
  10982. // set the data
  10983. series.setData(options.data, false);
  10984. // Mark cartesian
  10985. if (series.isCartesian) {
  10986. chart.hasCartesianSeries = true;
  10987. }
  10988. // Register it in the chart
  10989. chartSeries.push(series);
  10990. series._i = chartSeries.length - 1;
  10991. // Sort series according to index option (#248, #1123)
  10992. stableSort(chartSeries, function (a, b) {
  10993. return pick(a.options.index, a._i) - pick(b.options.index, a._i);
  10994. });
  10995. each(chartSeries, function (series, i) {
  10996. series.index = i;
  10997. series.name = series.name || 'Series ' + (i + 1);
  10998. });
  10999. },
  11000. /**
  11001. * Set the xAxis and yAxis properties of cartesian series, and register the series
  11002. * in the axis.series array
  11003. */
  11004. bindAxes: function () {
  11005. var series = this,
  11006. seriesOptions = series.options,
  11007. chart = series.chart,
  11008. axisOptions;
  11009. if (series.isCartesian) {
  11010. each(['xAxis', 'yAxis'], function (AXIS) { // repeat for xAxis and yAxis
  11011. each(chart[AXIS], function (axis) { // loop through the chart's axis objects
  11012. axisOptions = axis.options;
  11013. // apply if the series xAxis or yAxis option mathches the number of the
  11014. // axis, or if undefined, use the first axis
  11015. if ((seriesOptions[AXIS] === axisOptions.index) ||
  11016. (seriesOptions[AXIS] !== UNDEFINED && seriesOptions[AXIS] === axisOptions.id) ||
  11017. (seriesOptions[AXIS] === UNDEFINED && axisOptions.index === 0)) {
  11018. // register this series in the axis.series lookup
  11019. axis.series.push(series);
  11020. // set this series.xAxis or series.yAxis reference
  11021. series[AXIS] = axis;
  11022. // mark dirty for redraw
  11023. axis.isDirty = true;
  11024. }
  11025. });
  11026. // The series needs an X and an Y axis
  11027. if (!series[AXIS]) {
  11028. error(18, true);
  11029. }
  11030. });
  11031. }
  11032. },
  11033. /**
  11034. * Return an auto incremented x value based on the pointStart and pointInterval options.
  11035. * This is only used if an x value is not given for the point that calls autoIncrement.
  11036. */
  11037. autoIncrement: function () {
  11038. var series = this,
  11039. options = series.options,
  11040. xIncrement = series.xIncrement;
  11041. xIncrement = pick(xIncrement, options.pointStart, 0);
  11042. series.pointInterval = pick(series.pointInterval, options.pointInterval, 1);
  11043. series.xIncrement = xIncrement + series.pointInterval;
  11044. return xIncrement;
  11045. },
  11046. /**
  11047. * Divide the series data into segments divided by null values.
  11048. */
  11049. getSegments: function () {
  11050. var series = this,
  11051. lastNull = -1,
  11052. segments = [],
  11053. i,
  11054. points = series.points,
  11055. pointsLength = points.length;
  11056. if (pointsLength) { // no action required for []
  11057. // if connect nulls, just remove null points
  11058. if (series.options.connectNulls) {
  11059. i = pointsLength;
  11060. while (i--) {
  11061. if (points[i].y === null) {
  11062. points.splice(i, 1);
  11063. }
  11064. }
  11065. if (points.length) {
  11066. segments = [points];
  11067. }
  11068. // else, split on null points
  11069. } else {
  11070. each(points, function (point, i) {
  11071. if (point.y === null) {
  11072. if (i > lastNull + 1) {
  11073. segments.push(points.slice(lastNull + 1, i));
  11074. }
  11075. lastNull = i;
  11076. } else if (i === pointsLength - 1) { // last value
  11077. segments.push(points.slice(lastNull + 1, i + 1));
  11078. }
  11079. });
  11080. }
  11081. }
  11082. // register it
  11083. series.segments = segments;
  11084. },
  11085. /**
  11086. * Set the series options by merging from the options tree
  11087. * @param {Object} itemOptions
  11088. */
  11089. setOptions: function (itemOptions) {
  11090. var chart = this.chart,
  11091. chartOptions = chart.options,
  11092. plotOptions = chartOptions.plotOptions,
  11093. typeOptions = plotOptions[this.type],
  11094. options;
  11095. this.userOptions = itemOptions;
  11096. options = merge(
  11097. typeOptions,
  11098. plotOptions.series,
  11099. itemOptions
  11100. );
  11101. // the tooltip options are merged between global and series specific options
  11102. this.tooltipOptions = merge(chartOptions.tooltip, options.tooltip);
  11103. // Delte marker object if not allowed (#1125)
  11104. if (typeOptions.marker === null) {
  11105. delete options.marker;
  11106. }
  11107. return options;
  11108. },
  11109. /**
  11110. * Get the series' color
  11111. */
  11112. getColor: function () {
  11113. var options = this.options,
  11114. userOptions = this.userOptions,
  11115. defaultColors = this.chart.options.colors,
  11116. counters = this.chart.counters,
  11117. color,
  11118. colorIndex;
  11119. color = options.color || defaultPlotOptions[this.type].color;
  11120. if (!color && !options.colorByPoint) {
  11121. if (defined(userOptions._colorIndex)) { // after Series.update()
  11122. colorIndex = userOptions._colorIndex;
  11123. } else {
  11124. userOptions._colorIndex = counters.color;
  11125. colorIndex = counters.color++;
  11126. }
  11127. color = defaultColors[colorIndex];
  11128. }
  11129. this.color = color;
  11130. counters.wrapColor(defaultColors.length);
  11131. },
  11132. /**
  11133. * Get the series' symbol
  11134. */
  11135. getSymbol: function () {
  11136. var series = this,
  11137. userOptions = series.userOptions,
  11138. seriesMarkerOption = series.options.marker,
  11139. chart = series.chart,
  11140. defaultSymbols = chart.options.symbols,
  11141. counters = chart.counters,
  11142. symbolIndex;
  11143. series.symbol = seriesMarkerOption.symbol;
  11144. if (!series.symbol) {
  11145. if (defined(userOptions._symbolIndex)) { // after Series.update()
  11146. symbolIndex = userOptions._symbolIndex;
  11147. } else {
  11148. userOptions._symbolIndex = counters.symbol;
  11149. symbolIndex = counters.symbol++;
  11150. }
  11151. series.symbol = defaultSymbols[symbolIndex];
  11152. }
  11153. // don't substract radius in image symbols (#604)
  11154. if (/^url/.test(series.symbol)) {
  11155. seriesMarkerOption.radius = 0;
  11156. }
  11157. counters.wrapSymbol(defaultSymbols.length);
  11158. },
  11159. /**
  11160. * Get the series' symbol in the legend. This method should be overridable to create custom
  11161. * symbols through Highcharts.seriesTypes[type].prototype.drawLegendSymbols.
  11162. *
  11163. * @param {Object} legend The legend object
  11164. */
  11165. drawLegendSymbol: function (legend) {
  11166. var options = this.options,
  11167. markerOptions = options.marker,
  11168. radius,
  11169. legendOptions = legend.options,
  11170. legendSymbol,
  11171. symbolWidth = legendOptions.symbolWidth,
  11172. renderer = this.chart.renderer,
  11173. legendItemGroup = this.legendGroup,
  11174. verticalCenter = legend.baseline - mathRound(renderer.fontMetrics(legendOptions.itemStyle.fontSize).b * 0.3),
  11175. attr;
  11176. // Draw the line
  11177. if (options.lineWidth) {
  11178. attr = {
  11179. 'stroke-width': options.lineWidth
  11180. };
  11181. if (options.dashStyle) {
  11182. attr.dashstyle = options.dashStyle;
  11183. }
  11184. this.legendLine = renderer.path([
  11185. M,
  11186. 0,
  11187. verticalCenter,
  11188. L,
  11189. symbolWidth,
  11190. verticalCenter
  11191. ])
  11192. .attr(attr)
  11193. .add(legendItemGroup);
  11194. }
  11195. // Draw the marker
  11196. if (markerOptions && markerOptions.enabled) {
  11197. radius = markerOptions.radius;
  11198. this.legendSymbol = legendSymbol = renderer.symbol(
  11199. this.symbol,
  11200. (symbolWidth / 2) - radius,
  11201. verticalCenter - radius,
  11202. 2 * radius,
  11203. 2 * radius
  11204. )
  11205. .add(legendItemGroup);
  11206. legendSymbol.isMarker = true;
  11207. }
  11208. },
  11209. /**
  11210. * Add a point dynamically after chart load time
  11211. * @param {Object} options Point options as given in series.data
  11212. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  11213. * @param {Boolean} shift If shift is true, a point is shifted off the start
  11214. * of the series as one is appended to the end.
  11215. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  11216. * configuration
  11217. */
  11218. addPoint: function (options, redraw, shift, animation) {
  11219. var series = this,
  11220. seriesOptions = series.options,
  11221. data = series.data,
  11222. graph = series.graph,
  11223. area = series.area,
  11224. chart = series.chart,
  11225. xData = series.xData,
  11226. yData = series.yData,
  11227. zData = series.zData,
  11228. names = series.names,
  11229. currentShift = (graph && graph.shift) || 0,
  11230. dataOptions = seriesOptions.data,
  11231. point,
  11232. isInTheMiddle,
  11233. x,
  11234. i;
  11235. setAnimation(animation, chart);
  11236. // Make graph animate sideways
  11237. if (shift) {
  11238. each([graph, area, series.graphNeg, series.areaNeg], function (shape) {
  11239. if (shape) {
  11240. shape.shift = currentShift + 1;
  11241. }
  11242. });
  11243. }
  11244. if (area) {
  11245. area.isArea = true; // needed in animation, both with and without shift
  11246. }
  11247. // Optional redraw, defaults to true
  11248. redraw = pick(redraw, true);
  11249. // Get options and push the point to xData, yData and series.options. In series.generatePoints
  11250. // the Point instance will be created on demand and pushed to the series.data array.
  11251. point = {series: series};
  11252. series.pointClass.prototype.applyOptions.apply(point, [options]);
  11253. x = point.x;
  11254. // Get the insertion point
  11255. i = xData.length;
  11256. if (series.requireSorting && x < xData[i - 1]) {
  11257. isInTheMiddle = true;
  11258. while (i && xData[i - 1] > x) {
  11259. i--;
  11260. }
  11261. }
  11262. xData.splice(i, 0, x);
  11263. yData.splice(i, 0, series.toYData ? series.toYData(point) : point.y);
  11264. zData.splice(i, 0, point.z);
  11265. if (names) {
  11266. names[x] = point.name;
  11267. }
  11268. dataOptions.splice(i, 0, options);
  11269. if (isInTheMiddle) {
  11270. series.data.splice(i, 0, null);
  11271. series.processData();
  11272. }
  11273. // Generate points to be added to the legend (#1329)
  11274. if (seriesOptions.legendType === 'point') {
  11275. series.generatePoints();
  11276. }
  11277. // Shift the first point off the parallel arrays
  11278. // todo: consider series.removePoint(i) method
  11279. if (shift) {
  11280. if (data[0] && data[0].remove) {
  11281. data[0].remove(false);
  11282. } else {
  11283. data.shift();
  11284. xData.shift();
  11285. yData.shift();
  11286. zData.shift();
  11287. dataOptions.shift();
  11288. }
  11289. }
  11290. // redraw
  11291. series.isDirty = true;
  11292. series.isDirtyData = true;
  11293. if (redraw) {
  11294. series.getAttribs(); // #1937
  11295. chart.redraw();
  11296. }
  11297. },
  11298. /**
  11299. * Replace the series data with a new set of data
  11300. * @param {Object} data
  11301. * @param {Object} redraw
  11302. */
  11303. setData: function (data, redraw) {
  11304. var series = this,
  11305. oldData = series.points,
  11306. options = series.options,
  11307. chart = series.chart,
  11308. firstPoint = null,
  11309. xAxis = series.xAxis,
  11310. names = xAxis && xAxis.categories && !xAxis.categories.length ? [] : null,
  11311. i;
  11312. // reset properties
  11313. series.xIncrement = null;
  11314. series.pointRange = xAxis && xAxis.categories ? 1 : options.pointRange;
  11315. series.colorCounter = 0; // for series with colorByPoint (#1547)
  11316. // parallel arrays
  11317. var xData = [],
  11318. yData = [],
  11319. zData = [],
  11320. dataLength = data ? data.length : [],
  11321. turboThreshold = pick(options.turboThreshold, 1000),
  11322. pt,
  11323. pointArrayMap = series.pointArrayMap,
  11324. valueCount = pointArrayMap && pointArrayMap.length,
  11325. hasToYData = !!series.toYData;
  11326. // In turbo mode, only one- or twodimensional arrays of numbers are allowed. The
  11327. // first value is tested, and we assume that all the rest are defined the same
  11328. // way. Although the 'for' loops are similar, they are repeated inside each
  11329. // if-else conditional for max performance.
  11330. if (turboThreshold && dataLength > turboThreshold) {
  11331. // find the first non-null point
  11332. i = 0;
  11333. while (firstPoint === null && i < dataLength) {
  11334. firstPoint = data[i];
  11335. i++;
  11336. }
  11337. if (isNumber(firstPoint)) { // assume all points are numbers
  11338. var x = pick(options.pointStart, 0),
  11339. pointInterval = pick(options.pointInterval, 1);
  11340. for (i = 0; i < dataLength; i++) {
  11341. xData[i] = x;
  11342. yData[i] = data[i];
  11343. x += pointInterval;
  11344. }
  11345. series.xIncrement = x;
  11346. } else if (isArray(firstPoint)) { // assume all points are arrays
  11347. if (valueCount) { // [x, low, high] or [x, o, h, l, c]
  11348. for (i = 0; i < dataLength; i++) {
  11349. pt = data[i];
  11350. xData[i] = pt[0];
  11351. yData[i] = pt.slice(1, valueCount + 1);
  11352. }
  11353. } else { // [x, y]
  11354. for (i = 0; i < dataLength; i++) {
  11355. pt = data[i];
  11356. xData[i] = pt[0];
  11357. yData[i] = pt[1];
  11358. }
  11359. }
  11360. } else {
  11361. error(12); // Highcharts expects configs to be numbers or arrays in turbo mode
  11362. }
  11363. } else {
  11364. for (i = 0; i < dataLength; i++) {
  11365. if (data[i] !== UNDEFINED) { // stray commas in oldIE
  11366. pt = {series: series};
  11367. series.pointClass.prototype.applyOptions.apply(pt, [data[i]]);
  11368. xData[i] = pt.x;
  11369. yData[i] = hasToYData ? series.toYData(pt) : pt.y;
  11370. zData[i] = pt.z;
  11371. if (names && pt.name) {
  11372. names[pt.x] = pt.name; // #2046
  11373. }
  11374. }
  11375. }
  11376. }
  11377. // Forgetting to cast strings to numbers is a common caveat when handling CSV or JSON
  11378. if (isString(yData[0])) {
  11379. error(14, true);
  11380. }
  11381. series.data = [];
  11382. series.options.data = data;
  11383. series.xData = xData;
  11384. series.yData = yData;
  11385. series.zData = zData;
  11386. series.names = names;
  11387. // destroy old points
  11388. i = (oldData && oldData.length) || 0;
  11389. while (i--) {
  11390. if (oldData[i] && oldData[i].destroy) {
  11391. oldData[i].destroy();
  11392. }
  11393. }
  11394. // reset minRange (#878)
  11395. if (xAxis) {
  11396. xAxis.minRange = xAxis.userMinRange;
  11397. }
  11398. // redraw
  11399. series.isDirty = series.isDirtyData = chart.isDirtyBox = true;
  11400. if (pick(redraw, true)) {
  11401. chart.redraw(false);
  11402. }
  11403. },
  11404. /**
  11405. * Remove a series and optionally redraw the chart
  11406. *
  11407. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  11408. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  11409. * configuration
  11410. */
  11411. remove: function (redraw, animation) {
  11412. var series = this,
  11413. chart = series.chart;
  11414. redraw = pick(redraw, true);
  11415. if (!series.isRemoving) { /* prevent triggering native event in jQuery
  11416. (calling the remove function from the remove event) */
  11417. series.isRemoving = true;
  11418. // fire the event with a default handler of removing the point
  11419. fireEvent(series, 'remove', null, function () {
  11420. // destroy elements
  11421. series.destroy();
  11422. // redraw
  11423. chart.isDirtyLegend = chart.isDirtyBox = true;
  11424. chart.linkSeries();
  11425. if (redraw) {
  11426. chart.redraw(animation);
  11427. }
  11428. });
  11429. }
  11430. series.isRemoving = false;
  11431. },
  11432. /**
  11433. * Process the data by cropping away unused data points if the series is longer
  11434. * than the crop threshold. This saves computing time for lage series.
  11435. */
  11436. processData: function (force) {
  11437. var series = this,
  11438. processedXData = series.xData, // copied during slice operation below
  11439. processedYData = series.yData,
  11440. dataLength = processedXData.length,
  11441. croppedData,
  11442. cropStart = 0,
  11443. cropped,
  11444. distance,
  11445. closestPointRange,
  11446. xAxis = series.xAxis,
  11447. i, // loop variable
  11448. options = series.options,
  11449. cropThreshold = options.cropThreshold,
  11450. isCartesian = series.isCartesian;
  11451. // If the series data or axes haven't changed, don't go through this. Return false to pass
  11452. // the message on to override methods like in data grouping.
  11453. if (isCartesian && !series.isDirty && !xAxis.isDirty && !series.yAxis.isDirty && !force) {
  11454. return false;
  11455. }
  11456. // optionally filter out points outside the plot area
  11457. if (isCartesian && series.sorted && (!cropThreshold || dataLength > cropThreshold || series.forceCrop)) {
  11458. var min = xAxis.min,
  11459. max = xAxis.max;
  11460. // it's outside current extremes
  11461. if (processedXData[dataLength - 1] < min || processedXData[0] > max) {
  11462. processedXData = [];
  11463. processedYData = [];
  11464. // only crop if it's actually spilling out
  11465. } else if (processedXData[0] < min || processedXData[dataLength - 1] > max) {
  11466. croppedData = this.cropData(series.xData, series.yData, min, max);
  11467. processedXData = croppedData.xData;
  11468. processedYData = croppedData.yData;
  11469. cropStart = croppedData.start;
  11470. cropped = true;
  11471. }
  11472. }
  11473. // Find the closest distance between processed points
  11474. for (i = processedXData.length - 1; i >= 0; i--) {
  11475. distance = processedXData[i] - processedXData[i - 1];
  11476. if (distance > 0 && (closestPointRange === UNDEFINED || distance < closestPointRange)) {
  11477. closestPointRange = distance;
  11478. // Unsorted data is not supported by the line tooltip, as well as data grouping and
  11479. // navigation in Stock charts (#725) and width calculation of columns (#1900)
  11480. } else if (distance < 0 && series.requireSorting) {
  11481. error(15);
  11482. }
  11483. }
  11484. // Record the properties
  11485. series.cropped = cropped; // undefined or true
  11486. series.cropStart = cropStart;
  11487. series.processedXData = processedXData;
  11488. series.processedYData = processedYData;
  11489. if (options.pointRange === null) { // null means auto, as for columns, candlesticks and OHLC
  11490. series.pointRange = closestPointRange || 1;
  11491. }
  11492. series.closestPointRange = closestPointRange;
  11493. },
  11494. /**
  11495. * Iterate over xData and crop values between min and max. Returns object containing crop start/end
  11496. * cropped xData with corresponding part of yData, dataMin and dataMax within the cropped range
  11497. */
  11498. cropData: function (xData, yData, min, max) {
  11499. var dataLength = xData.length,
  11500. cropStart = 0,
  11501. cropEnd = dataLength,
  11502. cropShoulder = pick(this.cropShoulder, 1), // line-type series need one point outside
  11503. i;
  11504. // iterate up to find slice start
  11505. for (i = 0; i < dataLength; i++) {
  11506. if (xData[i] >= min) {
  11507. cropStart = mathMax(0, i - cropShoulder);
  11508. break;
  11509. }
  11510. }
  11511. // proceed to find slice end
  11512. for (; i < dataLength; i++) {
  11513. if (xData[i] > max) {
  11514. cropEnd = i + cropShoulder;
  11515. break;
  11516. }
  11517. }
  11518. return {
  11519. xData: xData.slice(cropStart, cropEnd),
  11520. yData: yData.slice(cropStart, cropEnd),
  11521. start: cropStart,
  11522. end: cropEnd
  11523. };
  11524. },
  11525. /**
  11526. * Generate the data point after the data has been processed by cropping away
  11527. * unused points and optionally grouped in Highcharts Stock.
  11528. */
  11529. generatePoints: function () {
  11530. var series = this,
  11531. options = series.options,
  11532. dataOptions = options.data,
  11533. data = series.data,
  11534. dataLength,
  11535. processedXData = series.processedXData,
  11536. processedYData = series.processedYData,
  11537. pointClass = series.pointClass,
  11538. processedDataLength = processedXData.length,
  11539. cropStart = series.cropStart || 0,
  11540. cursor,
  11541. hasGroupedData = series.hasGroupedData,
  11542. point,
  11543. points = [],
  11544. i;
  11545. if (!data && !hasGroupedData) {
  11546. var arr = [];
  11547. arr.length = dataOptions.length;
  11548. data = series.data = arr;
  11549. }
  11550. for (i = 0; i < processedDataLength; i++) {
  11551. cursor = cropStart + i;
  11552. if (!hasGroupedData) {
  11553. if (data[cursor]) {
  11554. point = data[cursor];
  11555. } else if (dataOptions[cursor] !== UNDEFINED) { // #970
  11556. data[cursor] = point = (new pointClass()).init(series, dataOptions[cursor], processedXData[i]);
  11557. }
  11558. points[i] = point;
  11559. } else {
  11560. // splat the y data in case of ohlc data array
  11561. points[i] = (new pointClass()).init(series, [processedXData[i]].concat(splat(processedYData[i])));
  11562. }
  11563. }
  11564. // Hide cropped-away points - this only runs when the number of points is above cropThreshold, or when
  11565. // swithching view from non-grouped data to grouped data (#637)
  11566. if (data && (processedDataLength !== (dataLength = data.length) || hasGroupedData)) {
  11567. for (i = 0; i < dataLength; i++) {
  11568. if (i === cropStart && !hasGroupedData) { // when has grouped data, clear all points
  11569. i += processedDataLength;
  11570. }
  11571. if (data[i]) {
  11572. data[i].destroyElements();
  11573. data[i].plotX = UNDEFINED; // #1003
  11574. }
  11575. }
  11576. }
  11577. series.data = data;
  11578. series.points = points;
  11579. },
  11580. /**
  11581. * Adds series' points value to corresponding stack
  11582. */
  11583. setStackedPoints: function () {
  11584. if (!this.options.stacking || (this.visible !== true && this.chart.options.chart.ignoreHiddenSeries !== false)) {
  11585. return;
  11586. }
  11587. var series = this,
  11588. xData = series.processedXData,
  11589. yData = series.processedYData,
  11590. stackedYData = [],
  11591. yDataLength = yData.length,
  11592. seriesOptions = series.options,
  11593. threshold = seriesOptions.threshold,
  11594. stackOption = seriesOptions.stack,
  11595. stacking = seriesOptions.stacking,
  11596. stackKey = series.stackKey,
  11597. negKey = '-' + stackKey,
  11598. negStacks = series.negStacks,
  11599. yAxis = series.yAxis,
  11600. stacks = yAxis.stacks,
  11601. oldStacks = yAxis.oldStacks,
  11602. isNegative,
  11603. stack,
  11604. other,
  11605. key,
  11606. i,
  11607. x,
  11608. y;
  11609. // loop over the non-null y values and read them into a local array
  11610. for (i = 0; i < yDataLength; i++) {
  11611. x = xData[i];
  11612. y = yData[i];
  11613. // Read stacked values into a stack based on the x value,
  11614. // the sign of y and the stack key. Stacking is also handled for null values (#739)
  11615. isNegative = negStacks && y < threshold;
  11616. key = isNegative ? negKey : stackKey;
  11617. // Create empty object for this stack if it doesn't exist yet
  11618. if (!stacks[key]) {
  11619. stacks[key] = {};
  11620. }
  11621. // Initialize StackItem for this x
  11622. if (!stacks[key][x]) {
  11623. if (oldStacks[key] && oldStacks[key][x]) {
  11624. stacks[key][x] = oldStacks[key][x];
  11625. stacks[key][x].total = null;
  11626. } else {
  11627. stacks[key][x] = new StackItem(yAxis, yAxis.options.stackLabels, isNegative, x, stackOption, stacking);
  11628. }
  11629. }
  11630. // If the StackItem doesn't exist, create it first
  11631. stack = stacks[key][x];
  11632. stack.points[series.index] = [stack.cum || 0];
  11633. // Add value to the stack total
  11634. if (stacking === 'percent') {
  11635. // Percent stacked column, totals are the same for the positive and negative stacks
  11636. other = isNegative ? stackKey : negKey;
  11637. if (negStacks && stacks[other] && stacks[other][x]) {
  11638. other = stacks[other][x];
  11639. stack.total = other.total = mathMax(other.total, stack.total) + mathAbs(y) || 0;
  11640. // Percent stacked areas
  11641. } else {
  11642. stack.total += mathAbs(y) || 0;
  11643. }
  11644. } else {
  11645. stack.total += y || 0;
  11646. }
  11647. stack.cum = (stack.cum || 0) + (y || 0);
  11648. stack.points[series.index].push(stack.cum);
  11649. stackedYData[i] = stack.cum;
  11650. }
  11651. if (stacking === 'percent') {
  11652. yAxis.usePercentage = true;
  11653. }
  11654. this.stackedYData = stackedYData; // To be used in getExtremes
  11655. // Reset old stacks
  11656. yAxis.oldStacks = {};
  11657. },
  11658. /**
  11659. * Iterate over all stacks and compute the absolute values to percent
  11660. */
  11661. setPercentStacks: function () {
  11662. var series = this,
  11663. stackKey = series.stackKey,
  11664. stacks = series.yAxis.stacks;
  11665. each([stackKey, '-' + stackKey], function (key) {
  11666. var i = series.xData.length,
  11667. x,
  11668. stack,
  11669. pointExtremes,
  11670. totalFactor;
  11671. while (i--) {
  11672. x = series.xData[i];
  11673. stack = stacks[key] && stacks[key][x];
  11674. pointExtremes = stack && stack.points[series.index];
  11675. if (pointExtremes) {
  11676. totalFactor = stack.total ? 100 / stack.total : 0;
  11677. pointExtremes[0] = correctFloat(pointExtremes[0] * totalFactor); // Y bottom value
  11678. pointExtremes[1] = correctFloat(pointExtremes[1] * totalFactor); // Y value
  11679. series.stackedYData[i] = pointExtremes[1];
  11680. }
  11681. }
  11682. });
  11683. },
  11684. /**
  11685. * Calculate Y extremes for visible data
  11686. */
  11687. getExtremes: function () {
  11688. var xAxis = this.xAxis,
  11689. yAxis = this.yAxis,
  11690. xData = this.processedXData,
  11691. yData = this.stackedYData || this.processedYData,
  11692. yDataLength = yData.length,
  11693. activeYData = [],
  11694. activeCounter = 0,
  11695. xExtremes = xAxis.getExtremes(), // #2117, need to compensate for log X axis
  11696. xMin = xExtremes.min,
  11697. xMax = xExtremes.max,
  11698. validValue,
  11699. withinRange,
  11700. dataMin,
  11701. dataMax,
  11702. x,
  11703. y,
  11704. i,
  11705. j;
  11706. for (i = 0; i < yDataLength; i++) {
  11707. x = xData[i];
  11708. y = yData[i];
  11709. // For points within the visible range, including the first point outside the
  11710. // visible range, consider y extremes
  11711. validValue = y !== null && y !== UNDEFINED && (!yAxis.isLog || (y.length || y > 0));
  11712. withinRange = this.getExtremesFromAll || this.cropped || ((xData[i + 1] || x) >= xMin &&
  11713. (xData[i - 1] || x) <= xMax);
  11714. if (validValue && withinRange) {
  11715. j = y.length;
  11716. if (j) { // array, like ohlc or range data
  11717. while (j--) {
  11718. if (y[j] !== null) {
  11719. activeYData[activeCounter++] = y[j];
  11720. }
  11721. }
  11722. } else {
  11723. activeYData[activeCounter++] = y;
  11724. }
  11725. }
  11726. }
  11727. this.dataMin = pick(dataMin, arrayMin(activeYData));
  11728. this.dataMax = pick(dataMax, arrayMax(activeYData));
  11729. },
  11730. /**
  11731. * Translate data points from raw data values to chart specific positioning data
  11732. * needed later in drawPoints, drawGraph and drawTracker.
  11733. */
  11734. translate: function () {
  11735. if (!this.processedXData) { // hidden series
  11736. this.processData();
  11737. }
  11738. this.generatePoints();
  11739. var series = this,
  11740. options = series.options,
  11741. stacking = options.stacking,
  11742. xAxis = series.xAxis,
  11743. categories = xAxis.categories,
  11744. yAxis = series.yAxis,
  11745. points = series.points,
  11746. dataLength = points.length,
  11747. hasModifyValue = !!series.modifyValue,
  11748. i,
  11749. pointPlacement = options.pointPlacement,
  11750. dynamicallyPlaced = pointPlacement === 'between' || isNumber(pointPlacement),
  11751. threshold = options.threshold;
  11752. // Translate each point
  11753. for (i = 0; i < dataLength; i++) {
  11754. var point = points[i],
  11755. xValue = point.x,
  11756. yValue = point.y,
  11757. yBottom = point.low,
  11758. stack = yAxis.stacks[(series.negStacks && yValue < threshold ? '-' : '') + series.stackKey],
  11759. pointStack,
  11760. stackValues;
  11761. // Discard disallowed y values for log axes
  11762. if (yAxis.isLog && yValue <= 0) {
  11763. point.y = yValue = null;
  11764. }
  11765. // Get the plotX translation
  11766. point.plotX = xAxis.translate(xValue, 0, 0, 0, 1, pointPlacement, this.type === 'flags'); // Math.round fixes #591
  11767. // Calculate the bottom y value for stacked series
  11768. if (stacking && series.visible && stack && stack[xValue]) {
  11769. pointStack = stack[xValue];
  11770. stackValues = pointStack.points[series.index];
  11771. yBottom = stackValues[0];
  11772. yValue = stackValues[1];
  11773. if (yBottom === 0) {
  11774. yBottom = pick(threshold, yAxis.min);
  11775. }
  11776. if (yAxis.isLog && yBottom <= 0) { // #1200, #1232
  11777. yBottom = null;
  11778. }
  11779. point.percentage = stacking === 'percent' && yValue;
  11780. point.total = point.stackTotal = pointStack.total;
  11781. point.stackY = yValue;
  11782. // Place the stack label
  11783. pointStack.setOffset(series.pointXOffset || 0, series.barW || 0);
  11784. }
  11785. // Set translated yBottom or remove it
  11786. point.yBottom = defined(yBottom) ?
  11787. yAxis.translate(yBottom, 0, 1, 0, 1) :
  11788. null;
  11789. // general hook, used for Highstock compare mode
  11790. if (hasModifyValue) {
  11791. yValue = series.modifyValue(yValue, point);
  11792. }
  11793. // Set the the plotY value, reset it for redraws
  11794. point.plotY = (typeof yValue === 'number' && yValue !== Infinity) ?
  11795. //mathRound(yAxis.translate(yValue, 0, 1, 0, 1) * 10) / 10 : // Math.round fixes #591
  11796. yAxis.translate(yValue, 0, 1, 0, 1) :
  11797. UNDEFINED;
  11798. // Set client related positions for mouse tracking
  11799. point.clientX = dynamicallyPlaced ? xAxis.translate(xValue, 0, 0, 0, 1) : point.plotX; // #1514
  11800. point.negative = point.y < (threshold || 0);
  11801. // some API data
  11802. point.category = categories && categories[point.x] !== UNDEFINED ?
  11803. categories[point.x] : point.x;
  11804. }
  11805. // now that we have the cropped data, build the segments
  11806. series.getSegments();
  11807. },
  11808. /**
  11809. * Memoize tooltip texts and positions
  11810. */
  11811. setTooltipPoints: function (renew) {
  11812. var series = this,
  11813. points = [],
  11814. pointsLength,
  11815. low,
  11816. high,
  11817. xAxis = series.xAxis,
  11818. xExtremes = xAxis && xAxis.getExtremes(),
  11819. axisLength = xAxis ? (xAxis.tooltipLen || xAxis.len) : series.chart.plotSizeX, // tooltipLen and tooltipPosName used in polar
  11820. point,
  11821. pointX,
  11822. nextPoint,
  11823. i,
  11824. tooltipPoints = []; // a lookup array for each pixel in the x dimension
  11825. // don't waste resources if tracker is disabled
  11826. if (series.options.enableMouseTracking === false) {
  11827. return;
  11828. }
  11829. // renew
  11830. if (renew) {
  11831. series.tooltipPoints = null;
  11832. }
  11833. // concat segments to overcome null values
  11834. each(series.segments || series.points, function (segment) {
  11835. points = points.concat(segment);
  11836. });
  11837. // Reverse the points in case the X axis is reversed
  11838. if (xAxis && xAxis.reversed) {
  11839. points = points.reverse();
  11840. }
  11841. // Polar needs additional shaping
  11842. if (series.orderTooltipPoints) {
  11843. series.orderTooltipPoints(points);
  11844. }
  11845. // Assign each pixel position to the nearest point
  11846. pointsLength = points.length;
  11847. for (i = 0; i < pointsLength; i++) {
  11848. point = points[i];
  11849. pointX = point.x;
  11850. if (pointX >= xExtremes.min && pointX <= xExtremes.max) { // #1149
  11851. nextPoint = points[i + 1];
  11852. // Set this range's low to the last range's high plus one
  11853. low = high === UNDEFINED ? 0 : high + 1;
  11854. // Now find the new high
  11855. high = points[i + 1] ?
  11856. mathMin(mathMax(0, mathFloor( // #2070
  11857. (point.clientX + (nextPoint ? (nextPoint.wrappedClientX || nextPoint.clientX) : axisLength)) / 2
  11858. )), axisLength) :
  11859. axisLength;
  11860. while (low >= 0 && low <= high) {
  11861. tooltipPoints[low++] = point;
  11862. }
  11863. }
  11864. }
  11865. series.tooltipPoints = tooltipPoints;
  11866. },
  11867. /**
  11868. * Format the header of the tooltip
  11869. */
  11870. tooltipHeaderFormatter: function (point) {
  11871. var series = this,
  11872. tooltipOptions = series.tooltipOptions,
  11873. xDateFormat = tooltipOptions.xDateFormat,
  11874. dateTimeLabelFormats = tooltipOptions.dateTimeLabelFormats,
  11875. xAxis = series.xAxis,
  11876. isDateTime = xAxis && xAxis.options.type === 'datetime',
  11877. headerFormat = tooltipOptions.headerFormat,
  11878. closestPointRange = xAxis && xAxis.closestPointRange,
  11879. n;
  11880. // Guess the best date format based on the closest point distance (#568)
  11881. if (isDateTime && !xDateFormat) {
  11882. if (closestPointRange) {
  11883. for (n in timeUnits) {
  11884. if (timeUnits[n] >= closestPointRange) {
  11885. xDateFormat = dateTimeLabelFormats[n];
  11886. break;
  11887. }
  11888. }
  11889. } else {
  11890. xDateFormat = dateTimeLabelFormats.day;
  11891. }
  11892. }
  11893. // Insert the header date format if any
  11894. if (isDateTime && xDateFormat && isNumber(point.key)) {
  11895. headerFormat = headerFormat.replace('{point.key}', '{point.key:' + xDateFormat + '}');
  11896. }
  11897. return format(headerFormat, {
  11898. point: point,
  11899. series: series
  11900. });
  11901. },
  11902. /**
  11903. * Series mouse over handler
  11904. */
  11905. onMouseOver: function () {
  11906. var series = this,
  11907. chart = series.chart,
  11908. hoverSeries = chart.hoverSeries;
  11909. // set normal state to previous series
  11910. if (hoverSeries && hoverSeries !== series) {
  11911. hoverSeries.onMouseOut();
  11912. }
  11913. // trigger the event, but to save processing time,
  11914. // only if defined
  11915. if (series.options.events.mouseOver) {
  11916. fireEvent(series, 'mouseOver');
  11917. }
  11918. // hover this
  11919. series.setState(HOVER_STATE);
  11920. chart.hoverSeries = series;
  11921. },
  11922. /**
  11923. * Series mouse out handler
  11924. */
  11925. onMouseOut: function () {
  11926. // trigger the event only if listeners exist
  11927. var series = this,
  11928. options = series.options,
  11929. chart = series.chart,
  11930. tooltip = chart.tooltip,
  11931. hoverPoint = chart.hoverPoint;
  11932. // trigger mouse out on the point, which must be in this series
  11933. if (hoverPoint) {
  11934. hoverPoint.onMouseOut();
  11935. }
  11936. // fire the mouse out event
  11937. if (series && options.events.mouseOut) {
  11938. fireEvent(series, 'mouseOut');
  11939. }
  11940. // hide the tooltip
  11941. if (tooltip && !options.stickyTracking && (!tooltip.shared || series.noSharedTooltip)) {
  11942. tooltip.hide();
  11943. }
  11944. // set normal state
  11945. series.setState();
  11946. chart.hoverSeries = null;
  11947. },
  11948. /**
  11949. * Animate in the series
  11950. */
  11951. animate: function (init) {
  11952. var series = this,
  11953. chart = series.chart,
  11954. renderer = chart.renderer,
  11955. clipRect,
  11956. markerClipRect,
  11957. animation = series.options.animation,
  11958. clipBox = chart.clipBox,
  11959. inverted = chart.inverted,
  11960. sharedClipKey;
  11961. // Animation option is set to true
  11962. if (animation && !isObject(animation)) {
  11963. animation = defaultPlotOptions[series.type].animation;
  11964. }
  11965. sharedClipKey = '_sharedClip' + animation.duration + animation.easing;
  11966. // Initialize the animation. Set up the clipping rectangle.
  11967. if (init) {
  11968. // If a clipping rectangle with the same properties is currently present in the chart, use that.
  11969. clipRect = chart[sharedClipKey];
  11970. markerClipRect = chart[sharedClipKey + 'm'];
  11971. if (!clipRect) {
  11972. chart[sharedClipKey] = clipRect = renderer.clipRect(
  11973. extend(clipBox, {width: 0})
  11974. );
  11975. chart[sharedClipKey + 'm'] = markerClipRect = renderer.clipRect(
  11976. -99, // include the width of the first marker
  11977. inverted ? -chart.plotLeft : -chart.plotTop,
  11978. 99,
  11979. inverted ? chart.chartWidth : chart.chartHeight
  11980. );
  11981. }
  11982. series.group.clip(clipRect);
  11983. series.markerGroup.clip(markerClipRect);
  11984. series.sharedClipKey = sharedClipKey;
  11985. // Run the animation
  11986. } else {
  11987. clipRect = chart[sharedClipKey];
  11988. if (clipRect) {
  11989. clipRect.animate({
  11990. width: chart.plotSizeX
  11991. }, animation);
  11992. chart[sharedClipKey + 'm'].animate({
  11993. width: chart.plotSizeX + 99
  11994. }, animation);
  11995. }
  11996. // Delete this function to allow it only once
  11997. series.animate = null;
  11998. // Call the afterAnimate function on animation complete (but don't overwrite the animation.complete option
  11999. // which should be available to the user).
  12000. series.animationTimeout = setTimeout(function () {
  12001. series.afterAnimate();
  12002. }, animation.duration);
  12003. }
  12004. },
  12005. /**
  12006. * This runs after animation to land on the final plot clipping
  12007. */
  12008. afterAnimate: function () {
  12009. var chart = this.chart,
  12010. sharedClipKey = this.sharedClipKey,
  12011. group = this.group;
  12012. if (group && this.options.clip !== false) {
  12013. group.clip(chart.clipRect);
  12014. this.markerGroup.clip(); // no clip
  12015. }
  12016. // Remove the shared clipping rectancgle when all series are shown
  12017. setTimeout(function () {
  12018. if (sharedClipKey && chart[sharedClipKey]) {
  12019. chart[sharedClipKey] = chart[sharedClipKey].destroy();
  12020. chart[sharedClipKey + 'm'] = chart[sharedClipKey + 'm'].destroy();
  12021. }
  12022. }, 100);
  12023. },
  12024. /**
  12025. * Draw the markers
  12026. */
  12027. drawPoints: function () {
  12028. var series = this,
  12029. pointAttr,
  12030. points = series.points,
  12031. chart = series.chart,
  12032. plotX,
  12033. plotY,
  12034. i,
  12035. point,
  12036. radius,
  12037. symbol,
  12038. isImage,
  12039. graphic,
  12040. options = series.options,
  12041. seriesMarkerOptions = options.marker,
  12042. pointMarkerOptions,
  12043. enabled,
  12044. isInside,
  12045. markerGroup = series.markerGroup;
  12046. if (seriesMarkerOptions.enabled || series._hasPointMarkers) {
  12047. i = points.length;
  12048. while (i--) {
  12049. point = points[i];
  12050. plotX = mathFloor(point.plotX); // #1843
  12051. plotY = point.plotY;
  12052. graphic = point.graphic;
  12053. pointMarkerOptions = point.marker || {};
  12054. enabled = (seriesMarkerOptions.enabled && pointMarkerOptions.enabled === UNDEFINED) || pointMarkerOptions.enabled;
  12055. isInside = chart.isInsidePlot(mathRound(plotX), plotY, chart.inverted); // #1858
  12056. // only draw the point if y is defined
  12057. if (enabled && plotY !== UNDEFINED && !isNaN(plotY) && point.y !== null) {
  12058. // shortcuts
  12059. pointAttr = point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE];
  12060. radius = pointAttr.r;
  12061. symbol = pick(pointMarkerOptions.symbol, series.symbol);
  12062. isImage = symbol.indexOf('url') === 0;
  12063. if (graphic) { // update
  12064. graphic
  12065. .attr({ // Since the marker group isn't clipped, each individual marker must be toggled
  12066. visibility: isInside ? (hasSVG ? 'inherit' : VISIBLE) : HIDDEN
  12067. })
  12068. .animate(extend({
  12069. x: plotX - radius,
  12070. y: plotY - radius
  12071. }, graphic.symbolName ? { // don't apply to image symbols #507
  12072. width: 2 * radius,
  12073. height: 2 * radius
  12074. } : {}));
  12075. } else if (isInside && (radius > 0 || isImage)) {
  12076. point.graphic = graphic = chart.renderer.symbol(
  12077. symbol,
  12078. plotX - radius,
  12079. plotY - radius,
  12080. 2 * radius,
  12081. 2 * radius
  12082. )
  12083. .attr(pointAttr)
  12084. .add(markerGroup);
  12085. }
  12086. } else if (graphic) {
  12087. point.graphic = graphic.destroy(); // #1269
  12088. }
  12089. }
  12090. }
  12091. },
  12092. /**
  12093. * Convert state properties from API naming conventions to SVG attributes
  12094. *
  12095. * @param {Object} options API options object
  12096. * @param {Object} base1 SVG attribute object to inherit from
  12097. * @param {Object} base2 Second level SVG attribute object to inherit from
  12098. */
  12099. convertAttribs: function (options, base1, base2, base3) {
  12100. var conversion = this.pointAttrToOptions,
  12101. attr,
  12102. option,
  12103. obj = {};
  12104. options = options || {};
  12105. base1 = base1 || {};
  12106. base2 = base2 || {};
  12107. base3 = base3 || {};
  12108. for (attr in conversion) {
  12109. option = conversion[attr];
  12110. obj[attr] = pick(options[option], base1[attr], base2[attr], base3[attr]);
  12111. }
  12112. return obj;
  12113. },
  12114. /**
  12115. * Get the state attributes. Each series type has its own set of attributes
  12116. * that are allowed to change on a point's state change. Series wide attributes are stored for
  12117. * all series, and additionally point specific attributes are stored for all
  12118. * points with individual marker options. If such options are not defined for the point,
  12119. * a reference to the series wide attributes is stored in point.pointAttr.
  12120. */
  12121. getAttribs: function () {
  12122. var series = this,
  12123. seriesOptions = series.options,
  12124. normalOptions = defaultPlotOptions[series.type].marker ? seriesOptions.marker : seriesOptions,
  12125. stateOptions = normalOptions.states,
  12126. stateOptionsHover = stateOptions[HOVER_STATE],
  12127. pointStateOptionsHover,
  12128. seriesColor = series.color,
  12129. normalDefaults = {
  12130. stroke: seriesColor,
  12131. fill: seriesColor
  12132. },
  12133. points = series.points || [], // #927
  12134. i,
  12135. point,
  12136. seriesPointAttr = [],
  12137. pointAttr,
  12138. pointAttrToOptions = series.pointAttrToOptions,
  12139. hasPointSpecificOptions,
  12140. negativeColor = seriesOptions.negativeColor,
  12141. defaultLineColor = normalOptions.lineColor,
  12142. key;
  12143. // series type specific modifications
  12144. if (seriesOptions.marker) { // line, spline, area, areaspline, scatter
  12145. // if no hover radius is given, default to normal radius + 2
  12146. stateOptionsHover.radius = stateOptionsHover.radius || normalOptions.radius + 2;
  12147. stateOptionsHover.lineWidth = stateOptionsHover.lineWidth || normalOptions.lineWidth + 1;
  12148. } else { // column, bar, pie
  12149. // if no hover color is given, brighten the normal color
  12150. stateOptionsHover.color = stateOptionsHover.color ||
  12151. Color(stateOptionsHover.color || seriesColor)
  12152. .brighten(stateOptionsHover.brightness).get();
  12153. }
  12154. // general point attributes for the series normal state
  12155. seriesPointAttr[NORMAL_STATE] = series.convertAttribs(normalOptions, normalDefaults);
  12156. // HOVER_STATE and SELECT_STATE states inherit from normal state except the default radius
  12157. each([HOVER_STATE, SELECT_STATE], function (state) {
  12158. seriesPointAttr[state] =
  12159. series.convertAttribs(stateOptions[state], seriesPointAttr[NORMAL_STATE]);
  12160. });
  12161. // set it
  12162. series.pointAttr = seriesPointAttr;
  12163. // Generate the point-specific attribute collections if specific point
  12164. // options are given. If not, create a referance to the series wide point
  12165. // attributes
  12166. i = points.length;
  12167. while (i--) {
  12168. point = points[i];
  12169. normalOptions = (point.options && point.options.marker) || point.options;
  12170. if (normalOptions && normalOptions.enabled === false) {
  12171. normalOptions.radius = 0;
  12172. }
  12173. if (point.negative && negativeColor) {
  12174. point.color = point.fillColor = negativeColor;
  12175. }
  12176. hasPointSpecificOptions = seriesOptions.colorByPoint || point.color; // #868
  12177. // check if the point has specific visual options
  12178. if (point.options) {
  12179. for (key in pointAttrToOptions) {
  12180. if (defined(normalOptions[pointAttrToOptions[key]])) {
  12181. hasPointSpecificOptions = true;
  12182. }
  12183. }
  12184. }
  12185. // a specific marker config object is defined for the individual point:
  12186. // create it's own attribute collection
  12187. if (hasPointSpecificOptions) {
  12188. normalOptions = normalOptions || {};
  12189. pointAttr = [];
  12190. stateOptions = normalOptions.states || {}; // reassign for individual point
  12191. pointStateOptionsHover = stateOptions[HOVER_STATE] = stateOptions[HOVER_STATE] || {};
  12192. // Handle colors for column and pies
  12193. if (!seriesOptions.marker) { // column, bar, point
  12194. // if no hover color is given, brighten the normal color
  12195. pointStateOptionsHover.color =
  12196. Color(pointStateOptionsHover.color || point.color)
  12197. .brighten(pointStateOptionsHover.brightness ||
  12198. stateOptionsHover.brightness).get();
  12199. }
  12200. // normal point state inherits series wide normal state
  12201. pointAttr[NORMAL_STATE] = series.convertAttribs(extend({
  12202. color: point.color, // #868
  12203. fillColor: point.color, // Individual point color or negative color markers (#2219)
  12204. lineColor: defaultLineColor === null ? point.color : UNDEFINED // Bubbles take point color, line markers use white
  12205. }, normalOptions), seriesPointAttr[NORMAL_STATE]);
  12206. // inherit from point normal and series hover
  12207. pointAttr[HOVER_STATE] = series.convertAttribs(
  12208. stateOptions[HOVER_STATE],
  12209. seriesPointAttr[HOVER_STATE],
  12210. pointAttr[NORMAL_STATE]
  12211. );
  12212. // inherit from point normal and series hover
  12213. pointAttr[SELECT_STATE] = series.convertAttribs(
  12214. stateOptions[SELECT_STATE],
  12215. seriesPointAttr[SELECT_STATE],
  12216. pointAttr[NORMAL_STATE]
  12217. );
  12218. // no marker config object is created: copy a reference to the series-wide
  12219. // attribute collection
  12220. } else {
  12221. pointAttr = seriesPointAttr;
  12222. }
  12223. point.pointAttr = pointAttr;
  12224. }
  12225. },
  12226. /**
  12227. * Update the series with a new set of options
  12228. */
  12229. update: function (newOptions, redraw) {
  12230. var chart = this.chart,
  12231. // must use user options when changing type because this.options is merged
  12232. // in with type specific plotOptions
  12233. oldOptions = this.userOptions,
  12234. oldType = this.type,
  12235. proto = seriesTypes[oldType].prototype,
  12236. n;
  12237. // Do the merge, with some forced options
  12238. newOptions = merge(oldOptions, {
  12239. animation: false,
  12240. index: this.index,
  12241. pointStart: this.xData[0] // when updating after addPoint
  12242. }, {data: this.options.data}, newOptions);
  12243. // Destroy the series and reinsert methods from the type prototype
  12244. this.remove(false);
  12245. for (n in proto) { // Overwrite series-type specific methods (#2270)
  12246. if (proto.hasOwnProperty(n)) {
  12247. this[n] = UNDEFINED;
  12248. }
  12249. }
  12250. extend(this, seriesTypes[newOptions.type || oldType].prototype);
  12251. this.init(chart, newOptions);
  12252. if (pick(redraw, true)) {
  12253. chart.redraw(false);
  12254. }
  12255. },
  12256. /**
  12257. * Clear DOM objects and free up memory
  12258. */
  12259. destroy: function () {
  12260. var series = this,
  12261. chart = series.chart,
  12262. issue134 = /AppleWebKit\/533/.test(userAgent),
  12263. destroy,
  12264. i,
  12265. data = series.data || [],
  12266. point,
  12267. prop,
  12268. axis;
  12269. // add event hook
  12270. fireEvent(series, 'destroy');
  12271. // remove all events
  12272. removeEvent(series);
  12273. // erase from axes
  12274. each(['xAxis', 'yAxis'], function (AXIS) {
  12275. axis = series[AXIS];
  12276. if (axis) {
  12277. erase(axis.series, series);
  12278. axis.isDirty = axis.forceRedraw = true;
  12279. axis.stacks = {}; // Rebuild stacks when updating (#2229)
  12280. }
  12281. });
  12282. // remove legend items
  12283. if (series.legendItem) {
  12284. series.chart.legend.destroyItem(series);
  12285. }
  12286. // destroy all points with their elements
  12287. i = data.length;
  12288. while (i--) {
  12289. point = data[i];
  12290. if (point && point.destroy) {
  12291. point.destroy();
  12292. }
  12293. }
  12294. series.points = null;
  12295. // Clear the animation timeout if we are destroying the series during initial animation
  12296. clearTimeout(series.animationTimeout);
  12297. // destroy all SVGElements associated to the series
  12298. each(['area', 'graph', 'dataLabelsGroup', 'group', 'markerGroup', 'tracker',
  12299. 'graphNeg', 'areaNeg', 'posClip', 'negClip'], function (prop) {
  12300. if (series[prop]) {
  12301. // issue 134 workaround
  12302. destroy = issue134 && prop === 'group' ?
  12303. 'hide' :
  12304. 'destroy';
  12305. series[prop][destroy]();
  12306. }
  12307. });
  12308. // remove from hoverSeries
  12309. if (chart.hoverSeries === series) {
  12310. chart.hoverSeries = null;
  12311. }
  12312. erase(chart.series, series);
  12313. // clear all members
  12314. for (prop in series) {
  12315. delete series[prop];
  12316. }
  12317. },
  12318. /**
  12319. * Draw the data labels
  12320. */
  12321. drawDataLabels: function () {
  12322. var series = this,
  12323. seriesOptions = series.options,
  12324. options = seriesOptions.dataLabels,
  12325. points = series.points,
  12326. pointOptions,
  12327. generalOptions,
  12328. str,
  12329. dataLabelsGroup;
  12330. if (options.enabled || series._hasPointLabels) {
  12331. // Process default alignment of data labels for columns
  12332. if (series.dlProcessOptions) {
  12333. series.dlProcessOptions(options);
  12334. }
  12335. // Create a separate group for the data labels to avoid rotation
  12336. dataLabelsGroup = series.plotGroup(
  12337. 'dataLabelsGroup',
  12338. 'data-labels',
  12339. series.visible ? VISIBLE : HIDDEN,
  12340. options.zIndex || 6
  12341. );
  12342. // Make the labels for each point
  12343. generalOptions = options;
  12344. each(points, function (point) {
  12345. var enabled,
  12346. dataLabel = point.dataLabel,
  12347. labelConfig,
  12348. attr,
  12349. name,
  12350. rotation,
  12351. connector = point.connector,
  12352. isNew = true;
  12353. // Determine if each data label is enabled
  12354. pointOptions = point.options && point.options.dataLabels;
  12355. enabled = pick(pointOptions && pointOptions.enabled, generalOptions.enabled); // #2282
  12356. // If the point is outside the plot area, destroy it. #678, #820
  12357. if (dataLabel && !enabled) {
  12358. point.dataLabel = dataLabel.destroy();
  12359. // Individual labels are disabled if the are explicitly disabled
  12360. // in the point options, or if they fall outside the plot area.
  12361. } else if (enabled) {
  12362. // Create individual options structure that can be extended without
  12363. // affecting others
  12364. options = merge(generalOptions, pointOptions);
  12365. rotation = options.rotation;
  12366. // Get the string
  12367. labelConfig = point.getLabelConfig();
  12368. str = options.format ?
  12369. format(options.format, labelConfig) :
  12370. options.formatter.call(labelConfig, options);
  12371. // Determine the color
  12372. options.style.color = pick(options.color, options.style.color, series.color, 'black');
  12373. // update existing label
  12374. if (dataLabel) {
  12375. if (defined(str)) {
  12376. dataLabel
  12377. .attr({
  12378. text: str
  12379. });
  12380. isNew = false;
  12381. } else { // #1437 - the label is shown conditionally
  12382. point.dataLabel = dataLabel = dataLabel.destroy();
  12383. if (connector) {
  12384. point.connector = connector.destroy();
  12385. }
  12386. }
  12387. // create new label
  12388. } else if (defined(str)) {
  12389. attr = {
  12390. //align: align,
  12391. fill: options.backgroundColor,
  12392. stroke: options.borderColor,
  12393. 'stroke-width': options.borderWidth,
  12394. r: options.borderRadius || 0,
  12395. rotation: rotation,
  12396. padding: options.padding,
  12397. zIndex: 1
  12398. };
  12399. // Remove unused attributes (#947)
  12400. for (name in attr) {
  12401. if (attr[name] === UNDEFINED) {
  12402. delete attr[name];
  12403. }
  12404. }
  12405. dataLabel = point.dataLabel = series.chart.renderer[rotation ? 'text' : 'label']( // labels don't support rotation
  12406. str,
  12407. 0,
  12408. -999,
  12409. null,
  12410. null,
  12411. null,
  12412. options.useHTML
  12413. )
  12414. .attr(attr)
  12415. .css(options.style)
  12416. .add(dataLabelsGroup)
  12417. .shadow(options.shadow);
  12418. }
  12419. if (dataLabel) {
  12420. // Now the data label is created and placed at 0,0, so we need to align it
  12421. series.alignDataLabel(point, dataLabel, options, null, isNew);
  12422. }
  12423. }
  12424. });
  12425. }
  12426. },
  12427. /**
  12428. * Align each individual data label
  12429. */
  12430. alignDataLabel: function (point, dataLabel, options, alignTo, isNew) {
  12431. var chart = this.chart,
  12432. inverted = chart.inverted,
  12433. plotX = pick(point.plotX, -999),
  12434. plotY = pick(point.plotY, -999),
  12435. bBox = dataLabel.getBBox(),
  12436. visible = this.visible && chart.isInsidePlot(point.plotX, point.plotY, inverted),
  12437. alignAttr; // the final position;
  12438. if (visible) {
  12439. // The alignment box is a singular point
  12440. alignTo = extend({
  12441. x: inverted ? chart.plotWidth - plotY : plotX,
  12442. y: mathRound(inverted ? chart.plotHeight - plotX : plotY),
  12443. width: 0,
  12444. height: 0
  12445. }, alignTo);
  12446. // Add the text size for alignment calculation
  12447. extend(options, {
  12448. width: bBox.width,
  12449. height: bBox.height
  12450. });
  12451. // Allow a hook for changing alignment in the last moment, then do the alignment
  12452. if (options.rotation) { // Fancy box alignment isn't supported for rotated text
  12453. alignAttr = {
  12454. align: options.align,
  12455. x: alignTo.x + options.x + alignTo.width / 2,
  12456. y: alignTo.y + options.y + alignTo.height / 2
  12457. };
  12458. dataLabel[isNew ? 'attr' : 'animate'](alignAttr);
  12459. } else {
  12460. dataLabel.align(options, null, alignTo);
  12461. alignAttr = dataLabel.alignAttr;
  12462. // Handle justify or crop
  12463. if (pick(options.overflow, 'justify') === 'justify') { // docs: overflow: justify, also crop only applies when not justify
  12464. this.justifyDataLabel(dataLabel, options, alignAttr, bBox, alignTo, isNew);
  12465. } else if (pick(options.crop, true)) {
  12466. // Now check that the data label is within the plot area
  12467. visible = chart.isInsidePlot(alignAttr.x, alignAttr.y) && chart.isInsidePlot(alignAttr.x + bBox.width, alignAttr.y + bBox.height);
  12468. }
  12469. }
  12470. }
  12471. // Show or hide based on the final aligned position
  12472. if (!visible) {
  12473. dataLabel.attr({y: -999});
  12474. }
  12475. },
  12476. /**
  12477. * If data labels fall partly outside the plot area, align them back in, in a way that
  12478. * doesn't hide the point.
  12479. */
  12480. justifyDataLabel: function (dataLabel, options, alignAttr, bBox, alignTo, isNew) {
  12481. var chart = this.chart,
  12482. align = options.align,
  12483. verticalAlign = options.verticalAlign,
  12484. off,
  12485. justified;
  12486. // Off left
  12487. off = alignAttr.x;
  12488. if (off < 0) {
  12489. if (align === 'right') {
  12490. options.align = 'left';
  12491. } else {
  12492. options.x = -off;
  12493. }
  12494. justified = true;
  12495. }
  12496. // Off right
  12497. off = alignAttr.x + bBox.width;
  12498. if (off > chart.plotWidth) {
  12499. if (align === 'left') {
  12500. options.align = 'right';
  12501. } else {
  12502. options.x = chart.plotWidth - off;
  12503. }
  12504. justified = true;
  12505. }
  12506. // Off top
  12507. off = alignAttr.y;
  12508. if (off < 0) {
  12509. if (verticalAlign === 'bottom') {
  12510. options.verticalAlign = 'top';
  12511. } else {
  12512. options.y = -off;
  12513. }
  12514. justified = true;
  12515. }
  12516. // Off bottom
  12517. off = alignAttr.y + bBox.height;
  12518. if (off > chart.plotHeight) {
  12519. if (verticalAlign === 'top') {
  12520. options.verticalAlign = 'bottom';
  12521. } else {
  12522. options.y = chart.plotHeight - off;
  12523. }
  12524. justified = true;
  12525. }
  12526. if (justified) {
  12527. dataLabel.placed = !isNew;
  12528. dataLabel.align(options, null, alignTo);
  12529. }
  12530. },
  12531. /**
  12532. * Return the graph path of a segment
  12533. */
  12534. getSegmentPath: function (segment) {
  12535. var series = this,
  12536. segmentPath = [],
  12537. step = series.options.step;
  12538. // build the segment line
  12539. each(segment, function (point, i) {
  12540. var plotX = point.plotX,
  12541. plotY = point.plotY,
  12542. lastPoint;
  12543. if (series.getPointSpline) { // generate the spline as defined in the SplineSeries object
  12544. segmentPath.push.apply(segmentPath, series.getPointSpline(segment, point, i));
  12545. } else {
  12546. // moveTo or lineTo
  12547. segmentPath.push(i ? L : M);
  12548. // step line?
  12549. if (step && i) {
  12550. lastPoint = segment[i - 1];
  12551. if (step === 'right') {
  12552. segmentPath.push(
  12553. lastPoint.plotX,
  12554. plotY
  12555. );
  12556. } else if (step === 'center') {
  12557. segmentPath.push(
  12558. (lastPoint.plotX + plotX) / 2,
  12559. lastPoint.plotY,
  12560. (lastPoint.plotX + plotX) / 2,
  12561. plotY
  12562. );
  12563. } else {
  12564. segmentPath.push(
  12565. plotX,
  12566. lastPoint.plotY
  12567. );
  12568. }
  12569. }
  12570. // normal line to next point
  12571. segmentPath.push(
  12572. point.plotX,
  12573. point.plotY
  12574. );
  12575. }
  12576. });
  12577. return segmentPath;
  12578. },
  12579. /**
  12580. * Get the graph path
  12581. */
  12582. getGraphPath: function () {
  12583. var series = this,
  12584. graphPath = [],
  12585. segmentPath,
  12586. singlePoints = []; // used in drawTracker
  12587. // Divide into segments and build graph and area paths
  12588. each(series.segments, function (segment) {
  12589. segmentPath = series.getSegmentPath(segment);
  12590. // add the segment to the graph, or a single point for tracking
  12591. if (segment.length > 1) {
  12592. graphPath = graphPath.concat(segmentPath);
  12593. } else {
  12594. singlePoints.push(segment[0]);
  12595. }
  12596. });
  12597. // Record it for use in drawGraph and drawTracker, and return graphPath
  12598. series.singlePoints = singlePoints;
  12599. series.graphPath = graphPath;
  12600. return graphPath;
  12601. },
  12602. /**
  12603. * Draw the actual graph
  12604. */
  12605. drawGraph: function () {
  12606. var series = this,
  12607. options = this.options,
  12608. props = [['graph', options.lineColor || this.color]],
  12609. lineWidth = options.lineWidth,
  12610. dashStyle = options.dashStyle,
  12611. graphPath = this.getGraphPath(),
  12612. negativeColor = options.negativeColor;
  12613. if (negativeColor) {
  12614. props.push(['graphNeg', negativeColor]);
  12615. }
  12616. // draw the graph
  12617. each(props, function (prop, i) {
  12618. var graphKey = prop[0],
  12619. graph = series[graphKey],
  12620. attribs;
  12621. if (graph) {
  12622. stop(graph); // cancel running animations, #459
  12623. graph.animate({d: graphPath});
  12624. } else if (lineWidth && graphPath.length) { // #1487
  12625. attribs = {
  12626. stroke: prop[1],
  12627. 'stroke-width': lineWidth,
  12628. zIndex: 1 // #1069
  12629. };
  12630. if (dashStyle) {
  12631. attribs.dashstyle = dashStyle;
  12632. } else {
  12633. attribs['stroke-linecap'] = attribs['stroke-linejoin'] = 'round';
  12634. }
  12635. series[graphKey] = series.chart.renderer.path(graphPath)
  12636. .attr(attribs)
  12637. .add(series.group)
  12638. .shadow(!i && options.shadow);
  12639. }
  12640. });
  12641. },
  12642. /**
  12643. * Clip the graphs into the positive and negative coloured graphs
  12644. */
  12645. clipNeg: function () {
  12646. var options = this.options,
  12647. chart = this.chart,
  12648. renderer = chart.renderer,
  12649. negativeColor = options.negativeColor || options.negativeFillColor,
  12650. translatedThreshold,
  12651. posAttr,
  12652. negAttr,
  12653. graph = this.graph,
  12654. area = this.area,
  12655. posClip = this.posClip,
  12656. negClip = this.negClip,
  12657. chartWidth = chart.chartWidth,
  12658. chartHeight = chart.chartHeight,
  12659. chartSizeMax = mathMax(chartWidth, chartHeight),
  12660. yAxis = this.yAxis,
  12661. above,
  12662. below;
  12663. if (negativeColor && (graph || area)) {
  12664. translatedThreshold = mathRound(yAxis.toPixels(options.threshold || 0, true));
  12665. above = {
  12666. x: 0,
  12667. y: 0,
  12668. width: chartSizeMax,
  12669. height: translatedThreshold
  12670. };
  12671. below = {
  12672. x: 0,
  12673. y: translatedThreshold,
  12674. width: chartSizeMax,
  12675. height: chartSizeMax
  12676. };
  12677. if (chart.inverted) {
  12678. above.height = below.y = chart.plotWidth - translatedThreshold;
  12679. if (renderer.isVML) {
  12680. above = {
  12681. x: chart.plotWidth - translatedThreshold - chart.plotLeft,
  12682. y: 0,
  12683. width: chartWidth,
  12684. height: chartHeight
  12685. };
  12686. below = {
  12687. x: translatedThreshold + chart.plotLeft - chartWidth,
  12688. y: 0,
  12689. width: chart.plotLeft + translatedThreshold,
  12690. height: chartWidth
  12691. };
  12692. }
  12693. }
  12694. if (yAxis.reversed) {
  12695. posAttr = below;
  12696. negAttr = above;
  12697. } else {
  12698. posAttr = above;
  12699. negAttr = below;
  12700. }
  12701. if (posClip) { // update
  12702. posClip.animate(posAttr);
  12703. negClip.animate(negAttr);
  12704. } else {
  12705. this.posClip = posClip = renderer.clipRect(posAttr);
  12706. this.negClip = negClip = renderer.clipRect(negAttr);
  12707. if (graph && this.graphNeg) {
  12708. graph.clip(posClip);
  12709. this.graphNeg.clip(negClip);
  12710. }
  12711. if (area) {
  12712. area.clip(posClip);
  12713. this.areaNeg.clip(negClip);
  12714. }
  12715. }
  12716. }
  12717. },
  12718. /**
  12719. * Initialize and perform group inversion on series.group and series.markerGroup
  12720. */
  12721. invertGroups: function () {
  12722. var series = this,
  12723. chart = series.chart;
  12724. // Pie, go away (#1736)
  12725. if (!series.xAxis) {
  12726. return;
  12727. }
  12728. // A fixed size is needed for inversion to work
  12729. function setInvert() {
  12730. var size = {
  12731. width: series.yAxis.len,
  12732. height: series.xAxis.len
  12733. };
  12734. each(['group', 'markerGroup'], function (groupName) {
  12735. if (series[groupName]) {
  12736. series[groupName].attr(size).invert();
  12737. }
  12738. });
  12739. }
  12740. addEvent(chart, 'resize', setInvert); // do it on resize
  12741. addEvent(series, 'destroy', function () {
  12742. removeEvent(chart, 'resize', setInvert);
  12743. });
  12744. // Do it now
  12745. setInvert(); // do it now
  12746. // On subsequent render and redraw, just do setInvert without setting up events again
  12747. series.invertGroups = setInvert;
  12748. },
  12749. /**
  12750. * General abstraction for creating plot groups like series.group, series.dataLabelsGroup and
  12751. * series.markerGroup. On subsequent calls, the group will only be adjusted to the updated plot size.
  12752. */
  12753. plotGroup: function (prop, name, visibility, zIndex, parent) {
  12754. var group = this[prop],
  12755. isNew = !group;
  12756. // Generate it on first call
  12757. if (isNew) {
  12758. this[prop] = group = this.chart.renderer.g(name)
  12759. .attr({
  12760. visibility: visibility,
  12761. zIndex: zIndex || 0.1 // IE8 needs this
  12762. })
  12763. .add(parent);
  12764. }
  12765. // Place it on first and subsequent (redraw) calls
  12766. group[isNew ? 'attr' : 'animate'](this.getPlotBox());
  12767. return group;
  12768. },
  12769. /**
  12770. * Get the translation and scale for the plot area of this series
  12771. */
  12772. getPlotBox: function () {
  12773. return {
  12774. translateX: this.xAxis ? this.xAxis.left : this.chart.plotLeft,
  12775. translateY: this.yAxis ? this.yAxis.top : this.chart.plotTop,
  12776. scaleX: 1, // #1623
  12777. scaleY: 1
  12778. };
  12779. },
  12780. /**
  12781. * Render the graph and markers
  12782. */
  12783. render: function () {
  12784. var series = this,
  12785. chart = series.chart,
  12786. group,
  12787. options = series.options,
  12788. animation = options.animation,
  12789. doAnimation = animation && !!series.animate &&
  12790. chart.renderer.isSVG, // this animation doesn't work in IE8 quirks when the group div is hidden,
  12791. // and looks bad in other oldIE
  12792. visibility = series.visible ? VISIBLE : HIDDEN,
  12793. zIndex = options.zIndex,
  12794. hasRendered = series.hasRendered,
  12795. chartSeriesGroup = chart.seriesGroup;
  12796. // the group
  12797. group = series.plotGroup(
  12798. 'group',
  12799. 'series',
  12800. visibility,
  12801. zIndex,
  12802. chartSeriesGroup
  12803. );
  12804. series.markerGroup = series.plotGroup(
  12805. 'markerGroup',
  12806. 'markers',
  12807. visibility,
  12808. zIndex,
  12809. chartSeriesGroup
  12810. );
  12811. // initiate the animation
  12812. if (doAnimation) {
  12813. series.animate(true);
  12814. }
  12815. // cache attributes for shapes
  12816. series.getAttribs();
  12817. // SVGRenderer needs to know this before drawing elements (#1089, #1795)
  12818. group.inverted = series.isCartesian ? chart.inverted : false;
  12819. // draw the graph if any
  12820. if (series.drawGraph) {
  12821. series.drawGraph();
  12822. series.clipNeg();
  12823. }
  12824. // draw the data labels (inn pies they go before the points)
  12825. series.drawDataLabels();
  12826. // draw the points
  12827. series.drawPoints();
  12828. // draw the mouse tracking area
  12829. if (series.options.enableMouseTracking !== false) {
  12830. series.drawTracker();
  12831. }
  12832. // Handle inverted series and tracker groups
  12833. if (chart.inverted) {
  12834. series.invertGroups();
  12835. }
  12836. // Initial clipping, must be defined after inverting groups for VML
  12837. if (options.clip !== false && !series.sharedClipKey && !hasRendered) {
  12838. group.clip(chart.clipRect);
  12839. }
  12840. // Run the animation
  12841. if (doAnimation) {
  12842. series.animate();
  12843. } else if (!hasRendered) {
  12844. series.afterAnimate();
  12845. }
  12846. series.isDirty = series.isDirtyData = false; // means data is in accordance with what you see
  12847. // (See #322) series.isDirty = series.isDirtyData = false; // means data is in accordance with what you see
  12848. series.hasRendered = true;
  12849. },
  12850. /**
  12851. * Redraw the series after an update in the axes.
  12852. */
  12853. redraw: function () {
  12854. var series = this,
  12855. chart = series.chart,
  12856. wasDirtyData = series.isDirtyData, // cache it here as it is set to false in render, but used after
  12857. group = series.group,
  12858. xAxis = series.xAxis,
  12859. yAxis = series.yAxis;
  12860. // reposition on resize
  12861. if (group) {
  12862. if (chart.inverted) {
  12863. group.attr({
  12864. width: chart.plotWidth,
  12865. height: chart.plotHeight
  12866. });
  12867. }
  12868. group.animate({
  12869. translateX: pick(xAxis && xAxis.left, chart.plotLeft),
  12870. translateY: pick(yAxis && yAxis.top, chart.plotTop)
  12871. });
  12872. }
  12873. series.translate();
  12874. series.setTooltipPoints(true);
  12875. series.render();
  12876. if (wasDirtyData) {
  12877. fireEvent(series, 'updatedData');
  12878. }
  12879. },
  12880. /**
  12881. * Set the state of the graph
  12882. */
  12883. setState: function (state) {
  12884. var series = this,
  12885. options = series.options,
  12886. graph = series.graph,
  12887. graphNeg = series.graphNeg,
  12888. stateOptions = options.states,
  12889. lineWidth = options.lineWidth,
  12890. attribs;
  12891. state = state || NORMAL_STATE;
  12892. if (series.state !== state) {
  12893. series.state = state;
  12894. if (stateOptions[state] && stateOptions[state].enabled === false) {
  12895. return;
  12896. }
  12897. if (state) {
  12898. lineWidth = stateOptions[state].lineWidth || lineWidth + 1;
  12899. }
  12900. if (graph && !graph.dashstyle) { // hover is turned off for dashed lines in VML
  12901. attribs = {
  12902. 'stroke-width': lineWidth
  12903. };
  12904. // use attr because animate will cause any other animation on the graph to stop
  12905. graph.attr(attribs);
  12906. if (graphNeg) {
  12907. graphNeg.attr(attribs);
  12908. }
  12909. }
  12910. }
  12911. },
  12912. /**
  12913. * Set the visibility of the graph
  12914. *
  12915. * @param vis {Boolean} True to show the series, false to hide. If UNDEFINED,
  12916. * the visibility is toggled.
  12917. */
  12918. setVisible: function (vis, redraw) {
  12919. var series = this,
  12920. chart = series.chart,
  12921. legendItem = series.legendItem,
  12922. showOrHide,
  12923. ignoreHiddenSeries = chart.options.chart.ignoreHiddenSeries,
  12924. oldVisibility = series.visible;
  12925. // if called without an argument, toggle visibility
  12926. series.visible = vis = series.userOptions.visible = vis === UNDEFINED ? !oldVisibility : vis;
  12927. showOrHide = vis ? 'show' : 'hide';
  12928. // show or hide elements
  12929. each(['group', 'dataLabelsGroup', 'markerGroup', 'tracker'], function (key) {
  12930. if (series[key]) {
  12931. series[key][showOrHide]();
  12932. }
  12933. });
  12934. // hide tooltip (#1361)
  12935. if (chart.hoverSeries === series) {
  12936. series.onMouseOut();
  12937. }
  12938. if (legendItem) {
  12939. chart.legend.colorizeItem(series, vis);
  12940. }
  12941. // rescale or adapt to resized chart
  12942. series.isDirty = true;
  12943. // in a stack, all other series are affected
  12944. if (series.options.stacking) {
  12945. each(chart.series, function (otherSeries) {
  12946. if (otherSeries.options.stacking && otherSeries.visible) {
  12947. otherSeries.isDirty = true;
  12948. }
  12949. });
  12950. }
  12951. // show or hide linked series
  12952. each(series.linkedSeries, function (otherSeries) {
  12953. otherSeries.setVisible(vis, false);
  12954. });
  12955. if (ignoreHiddenSeries) {
  12956. chart.isDirtyBox = true;
  12957. }
  12958. if (redraw !== false) {
  12959. chart.redraw();
  12960. }
  12961. fireEvent(series, showOrHide);
  12962. },
  12963. /**
  12964. * Show the graph
  12965. */
  12966. show: function () {
  12967. this.setVisible(true);
  12968. },
  12969. /**
  12970. * Hide the graph
  12971. */
  12972. hide: function () {
  12973. this.setVisible(false);
  12974. },
  12975. /**
  12976. * Set the selected state of the graph
  12977. *
  12978. * @param selected {Boolean} True to select the series, false to unselect. If
  12979. * UNDEFINED, the selection state is toggled.
  12980. */
  12981. select: function (selected) {
  12982. var series = this;
  12983. // if called without an argument, toggle
  12984. series.selected = selected = (selected === UNDEFINED) ? !series.selected : selected;
  12985. if (series.checkbox) {
  12986. series.checkbox.checked = selected;
  12987. }
  12988. fireEvent(series, selected ? 'select' : 'unselect');
  12989. },
  12990. /**
  12991. * Draw the tracker object that sits above all data labels and markers to
  12992. * track mouse events on the graph or points. For the line type charts
  12993. * the tracker uses the same graphPath, but with a greater stroke width
  12994. * for better control.
  12995. */
  12996. drawTracker: function () {
  12997. var series = this,
  12998. options = series.options,
  12999. trackByArea = options.trackByArea,
  13000. trackerPath = [].concat(trackByArea ? series.areaPath : series.graphPath),
  13001. trackerPathLength = trackerPath.length,
  13002. chart = series.chart,
  13003. pointer = chart.pointer,
  13004. renderer = chart.renderer,
  13005. snap = chart.options.tooltip.snap,
  13006. tracker = series.tracker,
  13007. cursor = options.cursor,
  13008. css = cursor && {cursor: cursor},
  13009. singlePoints = series.singlePoints,
  13010. singlePoint,
  13011. i,
  13012. onMouseOver = function () {
  13013. if (chart.hoverSeries !== series) {
  13014. series.onMouseOver();
  13015. }
  13016. };
  13017. // Extend end points. A better way would be to use round linecaps,
  13018. // but those are not clickable in VML.
  13019. if (trackerPathLength && !trackByArea) {
  13020. i = trackerPathLength + 1;
  13021. while (i--) {
  13022. if (trackerPath[i] === M) { // extend left side
  13023. trackerPath.splice(i + 1, 0, trackerPath[i + 1] - snap, trackerPath[i + 2], L);
  13024. }
  13025. if ((i && trackerPath[i] === M) || i === trackerPathLength) { // extend right side
  13026. trackerPath.splice(i, 0, L, trackerPath[i - 2] + snap, trackerPath[i - 1]);
  13027. }
  13028. }
  13029. }
  13030. // handle single points
  13031. for (i = 0; i < singlePoints.length; i++) {
  13032. singlePoint = singlePoints[i];
  13033. trackerPath.push(M, singlePoint.plotX - snap, singlePoint.plotY,
  13034. L, singlePoint.plotX + snap, singlePoint.plotY);
  13035. }
  13036. // draw the tracker
  13037. if (tracker) {
  13038. tracker.attr({d: trackerPath});
  13039. } else { // create
  13040. series.tracker = renderer.path(trackerPath)
  13041. .attr({
  13042. 'stroke-linejoin': 'round', // #1225
  13043. visibility: series.visible ? VISIBLE : HIDDEN,
  13044. stroke: TRACKER_FILL,
  13045. fill: trackByArea ? TRACKER_FILL : NONE,
  13046. 'stroke-width': options.lineWidth + (trackByArea ? 0 : 2 * snap),
  13047. zIndex: 2
  13048. })
  13049. .add(series.group);
  13050. // The tracker is added to the series group, which is clipped, but is covered
  13051. // by the marker group. So the marker group also needs to capture events.
  13052. each([series.tracker, series.markerGroup], function (tracker) {
  13053. tracker.addClass(PREFIX + 'tracker')
  13054. .on('mouseover', onMouseOver)
  13055. .on('mouseout', function (e) {
  13056. pointer.onTrackerMouseOut(e);
  13057. })
  13058. .css(css);
  13059. if (hasTouch) {
  13060. tracker.on('touchstart', onMouseOver);
  13061. }
  13062. });
  13063. }
  13064. }
  13065. }; // end Series prototype
  13066. /**
  13067. * LineSeries object
  13068. */
  13069. var LineSeries = extendClass(Series);
  13070. seriesTypes.line = LineSeries;
  13071. /**
  13072. * Set the default options for area
  13073. */
  13074. defaultPlotOptions.area = merge(defaultSeriesOptions, {
  13075. threshold: 0
  13076. // trackByArea: false,
  13077. // lineColor: null, // overrides color, but lets fillColor be unaltered
  13078. // fillOpacity: 0.75,
  13079. // fillColor: null
  13080. });
  13081. /**
  13082. * AreaSeries object
  13083. */
  13084. var AreaSeries = extendClass(Series, {
  13085. type: 'area',
  13086. /**
  13087. * For stacks, don't split segments on null values. Instead, draw null values with
  13088. * no marker. Also insert dummy points for any X position that exists in other series
  13089. * in the stack.
  13090. */
  13091. getSegments: function () {
  13092. var segments = [],
  13093. segment = [],
  13094. keys = [],
  13095. xAxis = this.xAxis,
  13096. yAxis = this.yAxis,
  13097. stack = yAxis.stacks[this.stackKey],
  13098. pointMap = {},
  13099. plotX,
  13100. plotY,
  13101. points = this.points,
  13102. connectNulls = this.options.connectNulls,
  13103. val,
  13104. i,
  13105. x;
  13106. if (this.options.stacking && !this.cropped) { // cropped causes artefacts in Stock, and perf issue
  13107. // Create a map where we can quickly look up the points by their X value.
  13108. for (i = 0; i < points.length; i++) {
  13109. pointMap[points[i].x] = points[i];
  13110. }
  13111. // Sort the keys (#1651)
  13112. for (x in stack) {
  13113. keys.push(+x);
  13114. }
  13115. keys.sort(function (a, b) {
  13116. return a - b;
  13117. });
  13118. each(keys, function (x) {
  13119. if (connectNulls && (!pointMap[x] || pointMap[x].y === null)) { // #1836
  13120. return;
  13121. // The point exists, push it to the segment
  13122. } else if (pointMap[x]) {
  13123. segment.push(pointMap[x]);
  13124. // There is no point for this X value in this series, so we
  13125. // insert a dummy point in order for the areas to be drawn
  13126. // correctly.
  13127. } else {
  13128. plotX = xAxis.translate(x);
  13129. val = stack[x].percent ? (stack[x].total ? stack[x].cum * 100 / stack[x].total : 0) : stack[x].cum; // #1991
  13130. plotY = yAxis.toPixels(val, true);
  13131. segment.push({
  13132. y: null,
  13133. plotX: plotX,
  13134. clientX: plotX,
  13135. plotY: plotY,
  13136. yBottom: plotY,
  13137. onMouseOver: noop
  13138. });
  13139. }
  13140. });
  13141. if (segment.length) {
  13142. segments.push(segment);
  13143. }
  13144. } else {
  13145. Series.prototype.getSegments.call(this);
  13146. segments = this.segments;
  13147. }
  13148. this.segments = segments;
  13149. },
  13150. /**
  13151. * Extend the base Series getSegmentPath method by adding the path for the area.
  13152. * This path is pushed to the series.areaPath property.
  13153. */
  13154. getSegmentPath: function (segment) {
  13155. var segmentPath = Series.prototype.getSegmentPath.call(this, segment), // call base method
  13156. areaSegmentPath = [].concat(segmentPath), // work on a copy for the area path
  13157. i,
  13158. options = this.options,
  13159. segLength = segmentPath.length,
  13160. translatedThreshold = this.yAxis.getThreshold(options.threshold), // #2181
  13161. yBottom;
  13162. if (segLength === 3) { // for animation from 1 to two points
  13163. areaSegmentPath.push(L, segmentPath[1], segmentPath[2]);
  13164. }
  13165. if (options.stacking && !this.closedStacks) {
  13166. // Follow stack back. Todo: implement areaspline. A general solution could be to
  13167. // reverse the entire graphPath of the previous series, though may be hard with
  13168. // splines and with series with different extremes
  13169. for (i = segment.length - 1; i >= 0; i--) {
  13170. yBottom = pick(segment[i].yBottom, translatedThreshold);
  13171. // step line?
  13172. if (i < segment.length - 1 && options.step) {
  13173. areaSegmentPath.push(segment[i + 1].plotX, yBottom);
  13174. }
  13175. areaSegmentPath.push(segment[i].plotX, yBottom);
  13176. }
  13177. } else { // follow zero line back
  13178. this.closeSegment(areaSegmentPath, segment, translatedThreshold);
  13179. }
  13180. this.areaPath = this.areaPath.concat(areaSegmentPath);
  13181. return segmentPath;
  13182. },
  13183. /**
  13184. * Extendable method to close the segment path of an area. This is overridden in polar
  13185. * charts.
  13186. */
  13187. closeSegment: function (path, segment, translatedThreshold) {
  13188. path.push(
  13189. L,
  13190. segment[segment.length - 1].plotX,
  13191. translatedThreshold,
  13192. L,
  13193. segment[0].plotX,
  13194. translatedThreshold
  13195. );
  13196. },
  13197. /**
  13198. * Draw the graph and the underlying area. This method calls the Series base
  13199. * function and adds the area. The areaPath is calculated in the getSegmentPath
  13200. * method called from Series.prototype.drawGraph.
  13201. */
  13202. drawGraph: function () {
  13203. // Define or reset areaPath
  13204. this.areaPath = [];
  13205. // Call the base method
  13206. Series.prototype.drawGraph.apply(this);
  13207. // Define local variables
  13208. var series = this,
  13209. areaPath = this.areaPath,
  13210. options = this.options,
  13211. negativeColor = options.negativeColor,
  13212. negativeFillColor = options.negativeFillColor,
  13213. props = [['area', this.color, options.fillColor]]; // area name, main color, fill color
  13214. if (negativeColor || negativeFillColor) {
  13215. props.push(['areaNeg', negativeColor, negativeFillColor]);
  13216. }
  13217. each(props, function (prop) {
  13218. var areaKey = prop[0],
  13219. area = series[areaKey];
  13220. // Create or update the area
  13221. if (area) { // update
  13222. area.animate({d: areaPath});
  13223. } else { // create
  13224. series[areaKey] = series.chart.renderer.path(areaPath)
  13225. .attr({
  13226. fill: pick(
  13227. prop[2],
  13228. Color(prop[1]).setOpacity(pick(options.fillOpacity, 0.75)).get()
  13229. ),
  13230. zIndex: 0 // #1069
  13231. }).add(series.group);
  13232. }
  13233. });
  13234. },
  13235. /**
  13236. * Get the series' symbol in the legend
  13237. *
  13238. * @param {Object} legend The legend object
  13239. * @param {Object} item The series (this) or point
  13240. */
  13241. drawLegendSymbol: function (legend, item) {
  13242. item.legendSymbol = this.chart.renderer.rect(
  13243. 0,
  13244. legend.baseline - 11,
  13245. legend.options.symbolWidth,
  13246. 12,
  13247. 2
  13248. ).attr({
  13249. zIndex: 3
  13250. }).add(item.legendGroup);
  13251. }
  13252. });
  13253. seriesTypes.area = AreaSeries;
  13254. /**
  13255. * Set the default options for spline
  13256. */
  13257. defaultPlotOptions.spline = merge(defaultSeriesOptions);
  13258. /**
  13259. * SplineSeries object
  13260. */
  13261. var SplineSeries = extendClass(Series, {
  13262. type: 'spline',
  13263. /**
  13264. * Get the spline segment from a given point's previous neighbour to the given point
  13265. */
  13266. getPointSpline: function (segment, point, i) {
  13267. var smoothing = 1.5, // 1 means control points midway between points, 2 means 1/3 from the point, 3 is 1/4 etc
  13268. denom = smoothing + 1,
  13269. plotX = point.plotX,
  13270. plotY = point.plotY,
  13271. lastPoint = segment[i - 1],
  13272. nextPoint = segment[i + 1],
  13273. leftContX,
  13274. leftContY,
  13275. rightContX,
  13276. rightContY,
  13277. ret;
  13278. // find control points
  13279. if (lastPoint && nextPoint) {
  13280. var lastX = lastPoint.plotX,
  13281. lastY = lastPoint.plotY,
  13282. nextX = nextPoint.plotX,
  13283. nextY = nextPoint.plotY,
  13284. correction;
  13285. leftContX = (smoothing * plotX + lastX) / denom;
  13286. leftContY = (smoothing * plotY + lastY) / denom;
  13287. rightContX = (smoothing * plotX + nextX) / denom;
  13288. rightContY = (smoothing * plotY + nextY) / denom;
  13289. // have the two control points make a straight line through main point
  13290. correction = ((rightContY - leftContY) * (rightContX - plotX)) /
  13291. (rightContX - leftContX) + plotY - rightContY;
  13292. leftContY += correction;
  13293. rightContY += correction;
  13294. // to prevent false extremes, check that control points are between
  13295. // neighbouring points' y values
  13296. if (leftContY > lastY && leftContY > plotY) {
  13297. leftContY = mathMax(lastY, plotY);
  13298. rightContY = 2 * plotY - leftContY; // mirror of left control point
  13299. } else if (leftContY < lastY && leftContY < plotY) {
  13300. leftContY = mathMin(lastY, plotY);
  13301. rightContY = 2 * plotY - leftContY;
  13302. }
  13303. if (rightContY > nextY && rightContY > plotY) {
  13304. rightContY = mathMax(nextY, plotY);
  13305. leftContY = 2 * plotY - rightContY;
  13306. } else if (rightContY < nextY && rightContY < plotY) {
  13307. rightContY = mathMin(nextY, plotY);
  13308. leftContY = 2 * plotY - rightContY;
  13309. }
  13310. // record for drawing in next point
  13311. point.rightContX = rightContX;
  13312. point.rightContY = rightContY;
  13313. }
  13314. // Visualize control points for debugging
  13315. /*
  13316. if (leftContX) {
  13317. this.chart.renderer.circle(leftContX + this.chart.plotLeft, leftContY + this.chart.plotTop, 2)
  13318. .attr({
  13319. stroke: 'red',
  13320. 'stroke-width': 1,
  13321. fill: 'none'
  13322. })
  13323. .add();
  13324. this.chart.renderer.path(['M', leftContX + this.chart.plotLeft, leftContY + this.chart.plotTop,
  13325. 'L', plotX + this.chart.plotLeft, plotY + this.chart.plotTop])
  13326. .attr({
  13327. stroke: 'red',
  13328. 'stroke-width': 1
  13329. })
  13330. .add();
  13331. this.chart.renderer.circle(rightContX + this.chart.plotLeft, rightContY + this.chart.plotTop, 2)
  13332. .attr({
  13333. stroke: 'green',
  13334. 'stroke-width': 1,
  13335. fill: 'none'
  13336. })
  13337. .add();
  13338. this.chart.renderer.path(['M', rightContX + this.chart.plotLeft, rightContY + this.chart.plotTop,
  13339. 'L', plotX + this.chart.plotLeft, plotY + this.chart.plotTop])
  13340. .attr({
  13341. stroke: 'green',
  13342. 'stroke-width': 1
  13343. })
  13344. .add();
  13345. }
  13346. */
  13347. // moveTo or lineTo
  13348. if (!i) {
  13349. ret = [M, plotX, plotY];
  13350. } else { // curve from last point to this
  13351. ret = [
  13352. 'C',
  13353. lastPoint.rightContX || lastPoint.plotX,
  13354. lastPoint.rightContY || lastPoint.plotY,
  13355. leftContX || plotX,
  13356. leftContY || plotY,
  13357. plotX,
  13358. plotY
  13359. ];
  13360. lastPoint.rightContX = lastPoint.rightContY = null; // reset for updating series later
  13361. }
  13362. return ret;
  13363. }
  13364. });
  13365. seriesTypes.spline = SplineSeries;
  13366. /**
  13367. * Set the default options for areaspline
  13368. */
  13369. defaultPlotOptions.areaspline = merge(defaultPlotOptions.area);
  13370. /**
  13371. * AreaSplineSeries object
  13372. */
  13373. var areaProto = AreaSeries.prototype,
  13374. AreaSplineSeries = extendClass(SplineSeries, {
  13375. type: 'areaspline',
  13376. closedStacks: true, // instead of following the previous graph back, follow the threshold back
  13377. // Mix in methods from the area series
  13378. getSegmentPath: areaProto.getSegmentPath,
  13379. closeSegment: areaProto.closeSegment,
  13380. drawGraph: areaProto.drawGraph,
  13381. drawLegendSymbol: areaProto.drawLegendSymbol
  13382. });
  13383. seriesTypes.areaspline = AreaSplineSeries;
  13384. /**
  13385. * Set the default options for column
  13386. */
  13387. defaultPlotOptions.column = merge(defaultSeriesOptions, {
  13388. borderColor: '#FFFFFF',
  13389. borderWidth: 1,
  13390. borderRadius: 0,
  13391. //colorByPoint: undefined,
  13392. groupPadding: 0.2,
  13393. //grouping: true,
  13394. marker: null, // point options are specified in the base options
  13395. pointPadding: 0.1,
  13396. //pointWidth: null,
  13397. minPointLength: 0,
  13398. cropThreshold: 50, // when there are more points, they will not animate out of the chart on xAxis.setExtremes
  13399. pointRange: null, // null means auto, meaning 1 in a categorized axis and least distance between points if not categories
  13400. states: {
  13401. hover: {
  13402. brightness: 0.1,
  13403. shadow: false
  13404. },
  13405. select: {
  13406. color: '#C0C0C0',
  13407. borderColor: '#000000',
  13408. shadow: false
  13409. }
  13410. },
  13411. dataLabels: {
  13412. align: null, // auto
  13413. verticalAlign: null, // auto
  13414. y: null
  13415. },
  13416. stickyTracking: false,
  13417. threshold: 0
  13418. });
  13419. /**
  13420. * ColumnSeries object
  13421. */
  13422. var ColumnSeries = extendClass(Series, {
  13423. type: 'column',
  13424. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  13425. stroke: 'borderColor',
  13426. 'stroke-width': 'borderWidth',
  13427. fill: 'color',
  13428. r: 'borderRadius'
  13429. },
  13430. cropShoulder: 0,
  13431. trackerGroups: ['group', 'dataLabelsGroup'],
  13432. negStacks: true, // use separate negative stacks, unlike area stacks where a negative
  13433. // point is substracted from previous (#1910)
  13434. /**
  13435. * Initialize the series
  13436. */
  13437. init: function () {
  13438. Series.prototype.init.apply(this, arguments);
  13439. var series = this,
  13440. chart = series.chart;
  13441. // if the series is added dynamically, force redraw of other
  13442. // series affected by a new column
  13443. if (chart.hasRendered) {
  13444. each(chart.series, function (otherSeries) {
  13445. if (otherSeries.type === series.type) {
  13446. otherSeries.isDirty = true;
  13447. }
  13448. });
  13449. }
  13450. },
  13451. /**
  13452. * Return the width and x offset of the columns adjusted for grouping, groupPadding, pointPadding,
  13453. * pointWidth etc.
  13454. */
  13455. getColumnMetrics: function () {
  13456. var series = this,
  13457. options = series.options,
  13458. xAxis = series.xAxis,
  13459. yAxis = series.yAxis,
  13460. reversedXAxis = xAxis.reversed,
  13461. stackKey,
  13462. stackGroups = {},
  13463. columnIndex,
  13464. columnCount = 0;
  13465. // Get the total number of column type series.
  13466. // This is called on every series. Consider moving this logic to a
  13467. // chart.orderStacks() function and call it on init, addSeries and removeSeries
  13468. if (options.grouping === false) {
  13469. columnCount = 1;
  13470. } else {
  13471. each(series.chart.series, function (otherSeries) {
  13472. var otherOptions = otherSeries.options,
  13473. otherYAxis = otherSeries.yAxis;
  13474. if (otherSeries.type === series.type && otherSeries.visible &&
  13475. yAxis.len === otherYAxis.len && yAxis.pos === otherYAxis.pos) { // #642, #2086
  13476. if (otherOptions.stacking) {
  13477. stackKey = otherSeries.stackKey;
  13478. if (stackGroups[stackKey] === UNDEFINED) {
  13479. stackGroups[stackKey] = columnCount++;
  13480. }
  13481. columnIndex = stackGroups[stackKey];
  13482. } else if (otherOptions.grouping !== false) { // #1162
  13483. columnIndex = columnCount++;
  13484. }
  13485. otherSeries.columnIndex = columnIndex;
  13486. }
  13487. });
  13488. }
  13489. var categoryWidth = mathMin(
  13490. mathAbs(xAxis.transA) * (xAxis.ordinalSlope || options.pointRange || xAxis.closestPointRange || 1),
  13491. xAxis.len // #1535
  13492. ),
  13493. groupPadding = categoryWidth * options.groupPadding,
  13494. groupWidth = categoryWidth - 2 * groupPadding,
  13495. pointOffsetWidth = groupWidth / columnCount,
  13496. optionPointWidth = options.pointWidth,
  13497. pointPadding = defined(optionPointWidth) ? (pointOffsetWidth - optionPointWidth) / 2 :
  13498. pointOffsetWidth * options.pointPadding,
  13499. pointWidth = pick(optionPointWidth, pointOffsetWidth - 2 * pointPadding), // exact point width, used in polar charts
  13500. colIndex = (reversedXAxis ?
  13501. columnCount - (series.columnIndex || 0) : // #1251
  13502. series.columnIndex) || 0,
  13503. pointXOffset = pointPadding + (groupPadding + colIndex *
  13504. pointOffsetWidth - (categoryWidth / 2)) *
  13505. (reversedXAxis ? -1 : 1);
  13506. // Save it for reading in linked series (Error bars particularly)
  13507. return (series.columnMetrics = {
  13508. width: pointWidth,
  13509. offset: pointXOffset
  13510. });
  13511. },
  13512. /**
  13513. * Translate each point to the plot area coordinate system and find shape positions
  13514. */
  13515. translate: function () {
  13516. var series = this,
  13517. chart = series.chart,
  13518. options = series.options,
  13519. borderWidth = options.borderWidth,
  13520. yAxis = series.yAxis,
  13521. threshold = options.threshold,
  13522. translatedThreshold = series.translatedThreshold = yAxis.getThreshold(threshold),
  13523. minPointLength = pick(options.minPointLength, 5),
  13524. metrics = series.getColumnMetrics(),
  13525. pointWidth = metrics.width,
  13526. seriesBarW = series.barW = mathCeil(mathMax(pointWidth, 1 + 2 * borderWidth)), // rounded and postprocessed for border width
  13527. pointXOffset = series.pointXOffset = metrics.offset,
  13528. xCrisp = -(borderWidth % 2 ? 0.5 : 0),
  13529. yCrisp = borderWidth % 2 ? 0.5 : 1;
  13530. if (chart.renderer.isVML && chart.inverted) {
  13531. yCrisp += 1;
  13532. }
  13533. Series.prototype.translate.apply(series);
  13534. // record the new values
  13535. each(series.points, function (point) {
  13536. var yBottom = pick(point.yBottom, translatedThreshold),
  13537. plotY = mathMin(mathMax(-999 - yBottom, point.plotY), yAxis.len + 999 + yBottom), // Don't draw too far outside plot area (#1303, #2241)
  13538. barX = point.plotX + pointXOffset,
  13539. barW = seriesBarW,
  13540. barY = mathMin(plotY, yBottom),
  13541. right,
  13542. bottom,
  13543. fromTop,
  13544. fromLeft,
  13545. barH = mathMax(plotY, yBottom) - barY;
  13546. // Handle options.minPointLength
  13547. if (mathAbs(barH) < minPointLength) {
  13548. if (minPointLength) {
  13549. barH = minPointLength;
  13550. barY =
  13551. mathRound(mathAbs(barY - translatedThreshold) > minPointLength ? // stacked
  13552. yBottom - minPointLength : // keep position
  13553. translatedThreshold - (yAxis.translate(point.y, 0, 1, 0, 1) <= translatedThreshold ? minPointLength : 0)); // use exact yAxis.translation (#1485)
  13554. }
  13555. }
  13556. // Cache for access in polar
  13557. point.barX = barX;
  13558. point.pointWidth = pointWidth;
  13559. // Round off to obtain crisp edges
  13560. fromLeft = mathAbs(barX) < 0.5;
  13561. right = mathRound(barX + barW) + xCrisp;
  13562. barX = mathRound(barX) + xCrisp;
  13563. barW = right - barX;
  13564. fromTop = mathAbs(barY) < 0.5;
  13565. bottom = mathRound(barY + barH) + yCrisp;
  13566. barY = mathRound(barY) + yCrisp;
  13567. barH = bottom - barY;
  13568. // Top and left edges are exceptions
  13569. if (fromLeft) {
  13570. barX += 1;
  13571. barW -= 1;
  13572. }
  13573. if (fromTop) {
  13574. barY -= 1;
  13575. barH += 1;
  13576. }
  13577. // Register shape type and arguments to be used in drawPoints
  13578. point.shapeType = 'rect';
  13579. point.shapeArgs = {
  13580. x: barX,
  13581. y: barY,
  13582. width: barW,
  13583. height: barH
  13584. };
  13585. });
  13586. },
  13587. getSymbol: noop,
  13588. /**
  13589. * Use a solid rectangle like the area series types
  13590. */
  13591. drawLegendSymbol: AreaSeries.prototype.drawLegendSymbol,
  13592. /**
  13593. * Columns have no graph
  13594. */
  13595. drawGraph: noop,
  13596. /**
  13597. * Draw the columns. For bars, the series.group is rotated, so the same coordinates
  13598. * apply for columns and bars. This method is inherited by scatter series.
  13599. *
  13600. */
  13601. drawPoints: function () {
  13602. var series = this,
  13603. options = series.options,
  13604. renderer = series.chart.renderer,
  13605. shapeArgs;
  13606. // draw the columns
  13607. each(series.points, function (point) {
  13608. var plotY = point.plotY,
  13609. graphic = point.graphic;
  13610. if (plotY !== UNDEFINED && !isNaN(plotY) && point.y !== null) {
  13611. shapeArgs = point.shapeArgs;
  13612. if (graphic) { // update
  13613. stop(graphic);
  13614. graphic.animate(merge(shapeArgs));
  13615. } else {
  13616. point.graphic = graphic = renderer[point.shapeType](shapeArgs)
  13617. .attr(point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE])
  13618. .add(series.group)
  13619. .shadow(options.shadow, null, options.stacking && !options.borderRadius);
  13620. }
  13621. } else if (graphic) {
  13622. point.graphic = graphic.destroy(); // #1269
  13623. }
  13624. });
  13625. },
  13626. /**
  13627. * Add tracking event listener to the series group, so the point graphics
  13628. * themselves act as trackers
  13629. */
  13630. drawTracker: function () {
  13631. var series = this,
  13632. chart = series.chart,
  13633. pointer = chart.pointer,
  13634. cursor = series.options.cursor,
  13635. css = cursor && {cursor: cursor},
  13636. onMouseOver = function (e) {
  13637. var target = e.target,
  13638. point;
  13639. if (chart.hoverSeries !== series) {
  13640. series.onMouseOver();
  13641. }
  13642. while (target && !point) {
  13643. point = target.point;
  13644. target = target.parentNode;
  13645. }
  13646. if (point !== UNDEFINED && point !== chart.hoverPoint) { // undefined on graph in scatterchart
  13647. point.onMouseOver(e);
  13648. }
  13649. };
  13650. // Add reference to the point
  13651. each(series.points, function (point) {
  13652. if (point.graphic) {
  13653. point.graphic.element.point = point;
  13654. }
  13655. if (point.dataLabel) {
  13656. point.dataLabel.element.point = point;
  13657. }
  13658. });
  13659. // Add the event listeners, we need to do this only once
  13660. if (!series._hasTracking) {
  13661. each(series.trackerGroups, function (key) {
  13662. if (series[key]) { // we don't always have dataLabelsGroup
  13663. series[key]
  13664. .addClass(PREFIX + 'tracker')
  13665. .on('mouseover', onMouseOver)
  13666. .on('mouseout', function (e) {
  13667. pointer.onTrackerMouseOut(e);
  13668. })
  13669. .css(css);
  13670. if (hasTouch) {
  13671. series[key].on('touchstart', onMouseOver);
  13672. }
  13673. }
  13674. });
  13675. series._hasTracking = true;
  13676. }
  13677. },
  13678. /**
  13679. * Override the basic data label alignment by adjusting for the position of the column
  13680. */
  13681. alignDataLabel: function (point, dataLabel, options, alignTo, isNew) {
  13682. var chart = this.chart,
  13683. inverted = chart.inverted,
  13684. dlBox = point.dlBox || point.shapeArgs, // data label box for alignment
  13685. below = point.below || (point.plotY > pick(this.translatedThreshold, chart.plotSizeY)),
  13686. inside = pick(options.inside, !!this.options.stacking); // draw it inside the box?
  13687. // Align to the column itself, or the top of it
  13688. if (dlBox) { // Area range uses this method but not alignTo
  13689. alignTo = merge(dlBox);
  13690. if (inverted) {
  13691. alignTo = {
  13692. x: chart.plotWidth - alignTo.y - alignTo.height,
  13693. y: chart.plotHeight - alignTo.x - alignTo.width,
  13694. width: alignTo.height,
  13695. height: alignTo.width
  13696. };
  13697. }
  13698. // Compute the alignment box
  13699. if (!inside) {
  13700. if (inverted) {
  13701. alignTo.x += below ? 0 : alignTo.width;
  13702. alignTo.width = 0;
  13703. } else {
  13704. alignTo.y += below ? alignTo.height : 0;
  13705. alignTo.height = 0;
  13706. }
  13707. }
  13708. }
  13709. // When alignment is undefined (typically columns and bars), display the individual
  13710. // point below or above the point depending on the threshold
  13711. options.align = pick(
  13712. options.align,
  13713. !inverted || inside ? 'center' : below ? 'right' : 'left'
  13714. );
  13715. options.verticalAlign = pick(
  13716. options.verticalAlign,
  13717. inverted || inside ? 'middle' : below ? 'top' : 'bottom'
  13718. );
  13719. // Call the parent method
  13720. Series.prototype.alignDataLabel.call(this, point, dataLabel, options, alignTo, isNew);
  13721. },
  13722. /**
  13723. * Animate the column heights one by one from zero
  13724. * @param {Boolean} init Whether to initialize the animation or run it
  13725. */
  13726. animate: function (init) {
  13727. var series = this,
  13728. yAxis = this.yAxis,
  13729. options = series.options,
  13730. inverted = this.chart.inverted,
  13731. attr = {},
  13732. translatedThreshold;
  13733. if (hasSVG) { // VML is too slow anyway
  13734. if (init) {
  13735. attr.scaleY = 0.001;
  13736. translatedThreshold = mathMin(yAxis.pos + yAxis.len, mathMax(yAxis.pos, yAxis.toPixels(options.threshold)));
  13737. if (inverted) {
  13738. attr.translateX = translatedThreshold - yAxis.len;
  13739. } else {
  13740. attr.translateY = translatedThreshold;
  13741. }
  13742. series.group.attr(attr);
  13743. } else { // run the animation
  13744. attr.scaleY = 1;
  13745. attr[inverted ? 'translateX' : 'translateY'] = yAxis.pos;
  13746. series.group.animate(attr, series.options.animation);
  13747. // delete this function to allow it only once
  13748. series.animate = null;
  13749. }
  13750. }
  13751. },
  13752. /**
  13753. * Remove this series from the chart
  13754. */
  13755. remove: function () {
  13756. var series = this,
  13757. chart = series.chart;
  13758. // column and bar series affects other series of the same type
  13759. // as they are either stacked or grouped
  13760. if (chart.hasRendered) {
  13761. each(chart.series, function (otherSeries) {
  13762. if (otherSeries.type === series.type) {
  13763. otherSeries.isDirty = true;
  13764. }
  13765. });
  13766. }
  13767. Series.prototype.remove.apply(series, arguments);
  13768. }
  13769. });
  13770. seriesTypes.column = ColumnSeries;
  13771. /**
  13772. * Set the default options for bar
  13773. */
  13774. defaultPlotOptions.bar = merge(defaultPlotOptions.column);
  13775. /**
  13776. * The Bar series class
  13777. */
  13778. var BarSeries = extendClass(ColumnSeries, {
  13779. type: 'bar',
  13780. inverted: true
  13781. });
  13782. seriesTypes.bar = BarSeries;
  13783. /**
  13784. * Set the default options for scatter
  13785. */
  13786. defaultPlotOptions.scatter = merge(defaultSeriesOptions, {
  13787. lineWidth: 0,
  13788. tooltip: {
  13789. headerFormat: '<span style="font-size: 10px; color:{series.color}">{series.name}</span><br/>',
  13790. pointFormat: 'x: <b>{point.x}</b><br/>y: <b>{point.y}</b><br/>',
  13791. followPointer: true
  13792. },
  13793. stickyTracking: false
  13794. });
  13795. /**
  13796. * The scatter series class
  13797. */
  13798. var ScatterSeries = extendClass(Series, {
  13799. type: 'scatter',
  13800. sorted: false,
  13801. requireSorting: false,
  13802. noSharedTooltip: true,
  13803. trackerGroups: ['markerGroup'],
  13804. drawTracker: ColumnSeries.prototype.drawTracker,
  13805. setTooltipPoints: noop
  13806. });
  13807. seriesTypes.scatter = ScatterSeries;
  13808. /**
  13809. * Set the default options for pie
  13810. */
  13811. defaultPlotOptions.pie = merge(defaultSeriesOptions, {
  13812. borderColor: '#FFFFFF',
  13813. borderWidth: 1,
  13814. center: [null, null],
  13815. clip: false,
  13816. colorByPoint: true, // always true for pies
  13817. dataLabels: {
  13818. // align: null,
  13819. // connectorWidth: 1,
  13820. // connectorColor: point.color,
  13821. // connectorPadding: 5,
  13822. distance: 30,
  13823. enabled: true,
  13824. formatter: function () {
  13825. return this.point.name;
  13826. }
  13827. // softConnector: true,
  13828. //y: 0
  13829. },
  13830. ignoreHiddenPoint: true,
  13831. //innerSize: 0,
  13832. legendType: 'point',
  13833. marker: null, // point options are specified in the base options
  13834. size: null,
  13835. showInLegend: false,
  13836. slicedOffset: 10,
  13837. states: {
  13838. hover: {
  13839. brightness: 0.1,
  13840. shadow: false
  13841. }
  13842. },
  13843. stickyTracking: false,
  13844. tooltip: {
  13845. followPointer: true
  13846. }
  13847. });
  13848. /**
  13849. * Extended point object for pies
  13850. */
  13851. var PiePoint = extendClass(Point, {
  13852. /**
  13853. * Initiate the pie slice
  13854. */
  13855. init: function () {
  13856. Point.prototype.init.apply(this, arguments);
  13857. var point = this,
  13858. toggleSlice;
  13859. // Disallow negative values (#1530)
  13860. if (point.y < 0) {
  13861. point.y = null;
  13862. }
  13863. //visible: options.visible !== false,
  13864. extend(point, {
  13865. visible: point.visible !== false,
  13866. name: pick(point.name, 'Slice')
  13867. });
  13868. // add event listener for select
  13869. toggleSlice = function (e) {
  13870. point.slice(e.type === 'select');
  13871. };
  13872. addEvent(point, 'select', toggleSlice);
  13873. addEvent(point, 'unselect', toggleSlice);
  13874. return point;
  13875. },
  13876. /**
  13877. * Toggle the visibility of the pie slice
  13878. * @param {Boolean} vis Whether to show the slice or not. If undefined, the
  13879. * visibility is toggled
  13880. */
  13881. setVisible: function (vis) {
  13882. var point = this,
  13883. series = point.series,
  13884. chart = series.chart,
  13885. method;
  13886. // if called without an argument, toggle visibility
  13887. point.visible = point.options.visible = vis = vis === UNDEFINED ? !point.visible : vis;
  13888. series.options.data[inArray(point, series.data)] = point.options; // update userOptions.data
  13889. method = vis ? 'show' : 'hide';
  13890. // Show and hide associated elements
  13891. each(['graphic', 'dataLabel', 'connector', 'shadowGroup'], function (key) {
  13892. if (point[key]) {
  13893. point[key][method]();
  13894. }
  13895. });
  13896. if (point.legendItem) {
  13897. chart.legend.colorizeItem(point, vis);
  13898. }
  13899. // Handle ignore hidden slices
  13900. if (!series.isDirty && series.options.ignoreHiddenPoint) {
  13901. series.isDirty = true;
  13902. chart.redraw();
  13903. }
  13904. },
  13905. /**
  13906. * Set or toggle whether the slice is cut out from the pie
  13907. * @param {Boolean} sliced When undefined, the slice state is toggled
  13908. * @param {Boolean} redraw Whether to redraw the chart. True by default.
  13909. */
  13910. slice: function (sliced, redraw, animation) {
  13911. var point = this,
  13912. series = point.series,
  13913. chart = series.chart,
  13914. translation;
  13915. setAnimation(animation, chart);
  13916. // redraw is true by default
  13917. redraw = pick(redraw, true);
  13918. // if called without an argument, toggle
  13919. point.sliced = point.options.sliced = sliced = defined(sliced) ? sliced : !point.sliced;
  13920. series.options.data[inArray(point, series.data)] = point.options; // update userOptions.data
  13921. translation = sliced ? point.slicedTranslation : {
  13922. translateX: 0,
  13923. translateY: 0
  13924. };
  13925. point.graphic.animate(translation);
  13926. if (point.shadowGroup) {
  13927. point.shadowGroup.animate(translation);
  13928. }
  13929. }
  13930. });
  13931. /**
  13932. * The Pie series class
  13933. */
  13934. var PieSeries = {
  13935. type: 'pie',
  13936. isCartesian: false,
  13937. pointClass: PiePoint,
  13938. requireSorting: false,
  13939. noSharedTooltip: true,
  13940. trackerGroups: ['group', 'dataLabelsGroup'],
  13941. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  13942. stroke: 'borderColor',
  13943. 'stroke-width': 'borderWidth',
  13944. fill: 'color'
  13945. },
  13946. /**
  13947. * Pies have one color each point
  13948. */
  13949. getColor: noop,
  13950. /**
  13951. * Animate the pies in
  13952. */
  13953. animate: function (init) {
  13954. var series = this,
  13955. points = series.points,
  13956. startAngleRad = series.startAngleRad;
  13957. if (!init) {
  13958. each(points, function (point) {
  13959. var graphic = point.graphic,
  13960. args = point.shapeArgs;
  13961. if (graphic) {
  13962. // start values
  13963. graphic.attr({
  13964. r: series.center[3] / 2, // animate from inner radius (#779)
  13965. start: startAngleRad,
  13966. end: startAngleRad
  13967. });
  13968. // animate
  13969. graphic.animate({
  13970. r: args.r,
  13971. start: args.start,
  13972. end: args.end
  13973. }, series.options.animation);
  13974. }
  13975. });
  13976. // delete this function to allow it only once
  13977. series.animate = null;
  13978. }
  13979. },
  13980. /**
  13981. * Extend the basic setData method by running processData and generatePoints immediately,
  13982. * in order to access the points from the legend.
  13983. */
  13984. setData: function (data, redraw) {
  13985. Series.prototype.setData.call(this, data, false);
  13986. this.processData();
  13987. this.generatePoints();
  13988. if (pick(redraw, true)) {
  13989. this.chart.redraw();
  13990. }
  13991. },
  13992. /**
  13993. * Extend the generatePoints method by adding total and percentage properties to each point
  13994. */
  13995. generatePoints: function () {
  13996. var i,
  13997. total = 0,
  13998. points,
  13999. len,
  14000. point,
  14001. ignoreHiddenPoint = this.options.ignoreHiddenPoint;
  14002. Series.prototype.generatePoints.call(this);
  14003. // Populate local vars
  14004. points = this.points;
  14005. len = points.length;
  14006. // Get the total sum
  14007. for (i = 0; i < len; i++) {
  14008. point = points[i];
  14009. total += (ignoreHiddenPoint && !point.visible) ? 0 : point.y;
  14010. }
  14011. this.total = total;
  14012. // Set each point's properties
  14013. for (i = 0; i < len; i++) {
  14014. point = points[i];
  14015. point.percentage = total > 0 ? (point.y / total) * 100 : 0;
  14016. point.total = total;
  14017. }
  14018. },
  14019. /**
  14020. * Get the center of the pie based on the size and center options relative to the
  14021. * plot area. Borrowed by the polar and gauge series types.
  14022. */
  14023. getCenter: function () {
  14024. var options = this.options,
  14025. chart = this.chart,
  14026. slicingRoom = 2 * (options.slicedOffset || 0),
  14027. handleSlicingRoom,
  14028. plotWidth = chart.plotWidth - 2 * slicingRoom,
  14029. plotHeight = chart.plotHeight - 2 * slicingRoom,
  14030. centerOption = options.center,
  14031. positions = [pick(centerOption[0], '50%'), pick(centerOption[1], '50%'), options.size || '100%', options.innerSize || 0],
  14032. smallestSize = mathMin(plotWidth, plotHeight),
  14033. isPercent;
  14034. return map(positions, function (length, i) {
  14035. isPercent = /%$/.test(length);
  14036. handleSlicingRoom = i < 2 || (i === 2 && isPercent);
  14037. return (isPercent ?
  14038. // i == 0: centerX, relative to width
  14039. // i == 1: centerY, relative to height
  14040. // i == 2: size, relative to smallestSize
  14041. // i == 4: innerSize, relative to smallestSize
  14042. [plotWidth, plotHeight, smallestSize, smallestSize][i] *
  14043. pInt(length) / 100 :
  14044. length) + (handleSlicingRoom ? slicingRoom : 0);
  14045. });
  14046. },
  14047. /**
  14048. * Do translation for pie slices
  14049. */
  14050. translate: function (positions) {
  14051. this.generatePoints();
  14052. var series = this,
  14053. cumulative = 0,
  14054. precision = 1000, // issue #172
  14055. options = series.options,
  14056. slicedOffset = options.slicedOffset,
  14057. connectorOffset = slicedOffset + options.borderWidth,
  14058. start,
  14059. end,
  14060. angle,
  14061. startAngle = options.startAngle || 0,
  14062. startAngleRad = series.startAngleRad = mathPI / 180 * (startAngle - 90),
  14063. endAngleRad = series.endAngleRad = mathPI / 180 * ((options.endAngle || (startAngle + 360)) - 90), // docs
  14064. circ = endAngleRad - startAngleRad, //2 * mathPI,
  14065. points = series.points,
  14066. radiusX, // the x component of the radius vector for a given point
  14067. radiusY,
  14068. labelDistance = options.dataLabels.distance,
  14069. ignoreHiddenPoint = options.ignoreHiddenPoint,
  14070. i,
  14071. len = points.length,
  14072. point;
  14073. // Get positions - either an integer or a percentage string must be given.
  14074. // If positions are passed as a parameter, we're in a recursive loop for adjusting
  14075. // space for data labels.
  14076. if (!positions) {
  14077. series.center = positions = series.getCenter();
  14078. }
  14079. // utility for getting the x value from a given y, used for anticollision logic in data labels
  14080. series.getX = function (y, left) {
  14081. angle = math.asin((y - positions[1]) / (positions[2] / 2 + labelDistance));
  14082. return positions[0] +
  14083. (left ? -1 : 1) *
  14084. (mathCos(angle) * (positions[2] / 2 + labelDistance));
  14085. };
  14086. // Calculate the geometry for each point
  14087. for (i = 0; i < len; i++) {
  14088. point = points[i];
  14089. // set start and end angle
  14090. start = startAngleRad + (cumulative * circ);
  14091. if (!ignoreHiddenPoint || point.visible) {
  14092. cumulative += point.percentage / 100;
  14093. }
  14094. end = startAngleRad + (cumulative * circ);
  14095. // set the shape
  14096. point.shapeType = 'arc';
  14097. point.shapeArgs = {
  14098. x: positions[0],
  14099. y: positions[1],
  14100. r: positions[2] / 2,
  14101. innerR: positions[3] / 2,
  14102. start: mathRound(start * precision) / precision,
  14103. end: mathRound(end * precision) / precision
  14104. };
  14105. // center for the sliced out slice
  14106. angle = (end + start) / 2;
  14107. if (angle > 0.75 * circ) {
  14108. angle -= 2 * mathPI;
  14109. }
  14110. point.slicedTranslation = {
  14111. translateX: mathRound(mathCos(angle) * slicedOffset),
  14112. translateY: mathRound(mathSin(angle) * slicedOffset)
  14113. };
  14114. // set the anchor point for tooltips
  14115. radiusX = mathCos(angle) * positions[2] / 2;
  14116. radiusY = mathSin(angle) * positions[2] / 2;
  14117. point.tooltipPos = [
  14118. positions[0] + radiusX * 0.7,
  14119. positions[1] + radiusY * 0.7
  14120. ];
  14121. point.half = angle < -mathPI / 2 || angle > mathPI / 2 ? 1 : 0;
  14122. point.angle = angle;
  14123. // set the anchor point for data labels
  14124. connectorOffset = mathMin(connectorOffset, labelDistance / 2); // #1678
  14125. point.labelPos = [
  14126. positions[0] + radiusX + mathCos(angle) * labelDistance, // first break of connector
  14127. positions[1] + radiusY + mathSin(angle) * labelDistance, // a/a
  14128. positions[0] + radiusX + mathCos(angle) * connectorOffset, // second break, right outside pie
  14129. positions[1] + radiusY + mathSin(angle) * connectorOffset, // a/a
  14130. positions[0] + radiusX, // landing point for connector
  14131. positions[1] + radiusY, // a/a
  14132. labelDistance < 0 ? // alignment
  14133. 'center' :
  14134. point.half ? 'right' : 'left', // alignment
  14135. angle // center angle
  14136. ];
  14137. }
  14138. },
  14139. setTooltipPoints: noop,
  14140. drawGraph: null,
  14141. /**
  14142. * Draw the data points
  14143. */
  14144. drawPoints: function () {
  14145. var series = this,
  14146. chart = series.chart,
  14147. renderer = chart.renderer,
  14148. groupTranslation,
  14149. //center,
  14150. graphic,
  14151. //group,
  14152. shadow = series.options.shadow,
  14153. shadowGroup,
  14154. shapeArgs;
  14155. if (shadow && !series.shadowGroup) {
  14156. series.shadowGroup = renderer.g('shadow')
  14157. .add(series.group);
  14158. }
  14159. // draw the slices
  14160. each(series.points, function (point) {
  14161. graphic = point.graphic;
  14162. shapeArgs = point.shapeArgs;
  14163. shadowGroup = point.shadowGroup;
  14164. // put the shadow behind all points
  14165. if (shadow && !shadowGroup) {
  14166. shadowGroup = point.shadowGroup = renderer.g('shadow')
  14167. .add(series.shadowGroup);
  14168. }
  14169. // if the point is sliced, use special translation, else use plot area traslation
  14170. groupTranslation = point.sliced ? point.slicedTranslation : {
  14171. translateX: 0,
  14172. translateY: 0
  14173. };
  14174. //group.translate(groupTranslation[0], groupTranslation[1]);
  14175. if (shadowGroup) {
  14176. shadowGroup.attr(groupTranslation);
  14177. }
  14178. // draw the slice
  14179. if (graphic) {
  14180. graphic.animate(extend(shapeArgs, groupTranslation));
  14181. } else {
  14182. point.graphic = graphic = renderer.arc(shapeArgs)
  14183. .setRadialReference(series.center)
  14184. .attr(
  14185. point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE]
  14186. )
  14187. .attr({'stroke-linejoin': 'round'})
  14188. .attr(groupTranslation)
  14189. .add(series.group)
  14190. .shadow(shadow, shadowGroup);
  14191. }
  14192. // detect point specific visibility
  14193. if (point.visible === false) {
  14194. point.setVisible(false);
  14195. }
  14196. });
  14197. },
  14198. /**
  14199. * Utility for sorting data labels
  14200. */
  14201. sortByAngle: function (points, sign) {
  14202. points.sort(function (a, b) {
  14203. return a.angle !== undefined && (b.angle - a.angle) * sign;
  14204. });
  14205. },
  14206. /**
  14207. * Override the base drawDataLabels method by pie specific functionality
  14208. */
  14209. drawDataLabels: function () {
  14210. var series = this,
  14211. data = series.data,
  14212. point,
  14213. chart = series.chart,
  14214. options = series.options.dataLabels,
  14215. connectorPadding = pick(options.connectorPadding, 10),
  14216. connectorWidth = pick(options.connectorWidth, 1),
  14217. plotWidth = chart.plotWidth,
  14218. plotHeight = chart.plotHeight,
  14219. connector,
  14220. connectorPath,
  14221. softConnector = pick(options.softConnector, true),
  14222. distanceOption = options.distance,
  14223. seriesCenter = series.center,
  14224. radius = seriesCenter[2] / 2,
  14225. centerY = seriesCenter[1],
  14226. outside = distanceOption > 0,
  14227. dataLabel,
  14228. dataLabelWidth,
  14229. labelPos,
  14230. labelHeight,
  14231. halves = [// divide the points into right and left halves for anti collision
  14232. [], // right
  14233. [] // left
  14234. ],
  14235. x,
  14236. y,
  14237. visibility,
  14238. rankArr,
  14239. i,
  14240. j,
  14241. overflow = [0, 0, 0, 0], // top, right, bottom, left
  14242. sort = function (a, b) {
  14243. return b.y - a.y;
  14244. };
  14245. // get out if not enabled
  14246. if (!series.visible || (!options.enabled && !series._hasPointLabels)) {
  14247. return;
  14248. }
  14249. // run parent method
  14250. Series.prototype.drawDataLabels.apply(series);
  14251. // arrange points for detection collision
  14252. each(data, function (point) {
  14253. if (point.dataLabel) { // it may have been cancelled in the base method (#407)
  14254. halves[point.half].push(point);
  14255. }
  14256. });
  14257. // assume equal label heights
  14258. i = 0;
  14259. while (!labelHeight && data[i]) { // #1569
  14260. labelHeight = data[i] && data[i].dataLabel && (data[i].dataLabel.getBBox().height || 21); // 21 is for #968
  14261. i++;
  14262. }
  14263. /* Loop over the points in each half, starting from the top and bottom
  14264. * of the pie to detect overlapping labels.
  14265. */
  14266. i = 2;
  14267. while (i--) {
  14268. var slots = [],
  14269. slotsLength,
  14270. usedSlots = [],
  14271. points = halves[i],
  14272. pos,
  14273. length = points.length,
  14274. slotIndex;
  14275. // Sort by angle
  14276. series.sortByAngle(points, i - 0.5);
  14277. // Only do anti-collision when we are outside the pie and have connectors (#856)
  14278. if (distanceOption > 0) {
  14279. // build the slots
  14280. for (pos = centerY - radius - distanceOption; pos <= centerY + radius + distanceOption; pos += labelHeight) {
  14281. slots.push(pos);
  14282. // visualize the slot
  14283. /*
  14284. var slotX = series.getX(pos, i) + chart.plotLeft - (i ? 100 : 0),
  14285. slotY = pos + chart.plotTop;
  14286. if (!isNaN(slotX)) {
  14287. chart.renderer.rect(slotX, slotY - 7, 100, labelHeight, 1)
  14288. .attr({
  14289. 'stroke-width': 1,
  14290. stroke: 'silver'
  14291. })
  14292. .add();
  14293. chart.renderer.text('Slot '+ (slots.length - 1), slotX, slotY + 4)
  14294. .attr({
  14295. fill: 'silver'
  14296. }).add();
  14297. }
  14298. */
  14299. }
  14300. slotsLength = slots.length;
  14301. // if there are more values than available slots, remove lowest values
  14302. if (length > slotsLength) {
  14303. // create an array for sorting and ranking the points within each quarter
  14304. rankArr = [].concat(points);
  14305. rankArr.sort(sort);
  14306. j = length;
  14307. while (j--) {
  14308. rankArr[j].rank = j;
  14309. }
  14310. j = length;
  14311. while (j--) {
  14312. if (points[j].rank >= slotsLength) {
  14313. points.splice(j, 1);
  14314. }
  14315. }
  14316. length = points.length;
  14317. }
  14318. // The label goes to the nearest open slot, but not closer to the edge than
  14319. // the label's index.
  14320. for (j = 0; j < length; j++) {
  14321. point = points[j];
  14322. labelPos = point.labelPos;
  14323. var closest = 9999,
  14324. distance,
  14325. slotI;
  14326. // find the closest slot index
  14327. for (slotI = 0; slotI < slotsLength; slotI++) {
  14328. distance = mathAbs(slots[slotI] - labelPos[1]);
  14329. if (distance < closest) {
  14330. closest = distance;
  14331. slotIndex = slotI;
  14332. }
  14333. }
  14334. // if that slot index is closer to the edges of the slots, move it
  14335. // to the closest appropriate slot
  14336. if (slotIndex < j && slots[j] !== null) { // cluster at the top
  14337. slotIndex = j;
  14338. } else if (slotsLength < length - j + slotIndex && slots[j] !== null) { // cluster at the bottom
  14339. slotIndex = slotsLength - length + j;
  14340. while (slots[slotIndex] === null) { // make sure it is not taken
  14341. slotIndex++;
  14342. }
  14343. } else {
  14344. // Slot is taken, find next free slot below. In the next run, the next slice will find the
  14345. // slot above these, because it is the closest one
  14346. while (slots[slotIndex] === null) { // make sure it is not taken
  14347. slotIndex++;
  14348. }
  14349. }
  14350. usedSlots.push({i: slotIndex, y: slots[slotIndex]});
  14351. slots[slotIndex] = null; // mark as taken
  14352. }
  14353. // sort them in order to fill in from the top
  14354. usedSlots.sort(sort);
  14355. }
  14356. // now the used slots are sorted, fill them up sequentially
  14357. for (j = 0; j < length; j++) {
  14358. var slot, naturalY;
  14359. point = points[j];
  14360. labelPos = point.labelPos;
  14361. dataLabel = point.dataLabel;
  14362. visibility = point.visible === false ? HIDDEN : VISIBLE;
  14363. naturalY = labelPos[1];
  14364. if (distanceOption > 0) {
  14365. slot = usedSlots.pop();
  14366. slotIndex = slot.i;
  14367. // if the slot next to currrent slot is free, the y value is allowed
  14368. // to fall back to the natural position
  14369. y = slot.y;
  14370. if ((naturalY > y && slots[slotIndex + 1] !== null) ||
  14371. (naturalY < y && slots[slotIndex - 1] !== null)) {
  14372. y = naturalY;
  14373. }
  14374. } else {
  14375. y = naturalY;
  14376. }
  14377. // get the x - use the natural x position for first and last slot, to prevent the top
  14378. // and botton slice connectors from touching each other on either side
  14379. x = options.justify ?
  14380. seriesCenter[0] + (i ? -1 : 1) * (radius + distanceOption) :
  14381. series.getX(slotIndex === 0 || slotIndex === slots.length - 1 ? naturalY : y, i);
  14382. // Record the placement and visibility
  14383. dataLabel._attr = {
  14384. visibility: visibility,
  14385. align: labelPos[6]
  14386. };
  14387. dataLabel._pos = {
  14388. x: x + options.x +
  14389. ({left: connectorPadding, right: -connectorPadding}[labelPos[6]] || 0),
  14390. y: y + options.y - 10 // 10 is for the baseline (label vs text)
  14391. };
  14392. dataLabel.connX = x;
  14393. dataLabel.connY = y;
  14394. // Detect overflowing data labels
  14395. if (this.options.size === null) {
  14396. dataLabelWidth = dataLabel.width;
  14397. // Overflow left
  14398. if (x - dataLabelWidth < connectorPadding) {
  14399. overflow[3] = mathMax(mathRound(dataLabelWidth - x + connectorPadding), overflow[3]);
  14400. // Overflow right
  14401. } else if (x + dataLabelWidth > plotWidth - connectorPadding) {
  14402. overflow[1] = mathMax(mathRound(x + dataLabelWidth - plotWidth + connectorPadding), overflow[1]);
  14403. }
  14404. // Overflow top
  14405. if (y - labelHeight / 2 < 0) {
  14406. overflow[0] = mathMax(mathRound(-y + labelHeight / 2), overflow[0]);
  14407. // Overflow left
  14408. } else if (y + labelHeight / 2 > plotHeight) {
  14409. overflow[2] = mathMax(mathRound(y + labelHeight / 2 - plotHeight), overflow[2]);
  14410. }
  14411. }
  14412. } // for each point
  14413. } // for each half
  14414. // Do not apply the final placement and draw the connectors until we have verified
  14415. // that labels are not spilling over.
  14416. if (arrayMax(overflow) === 0 || this.verifyDataLabelOverflow(overflow)) {
  14417. // Place the labels in the final position
  14418. this.placeDataLabels();
  14419. // Draw the connectors
  14420. if (outside && connectorWidth) {
  14421. each(this.points, function (point) {
  14422. connector = point.connector;
  14423. labelPos = point.labelPos;
  14424. dataLabel = point.dataLabel;
  14425. if (dataLabel && dataLabel._pos) {
  14426. visibility = dataLabel._attr.visibility;
  14427. x = dataLabel.connX;
  14428. y = dataLabel.connY;
  14429. connectorPath = softConnector ? [
  14430. M,
  14431. x + (labelPos[6] === 'left' ? 5 : -5), y, // end of the string at the label
  14432. 'C',
  14433. x, y, // first break, next to the label
  14434. 2 * labelPos[2] - labelPos[4], 2 * labelPos[3] - labelPos[5],
  14435. labelPos[2], labelPos[3], // second break
  14436. L,
  14437. labelPos[4], labelPos[5] // base
  14438. ] : [
  14439. M,
  14440. x + (labelPos[6] === 'left' ? 5 : -5), y, // end of the string at the label
  14441. L,
  14442. labelPos[2], labelPos[3], // second break
  14443. L,
  14444. labelPos[4], labelPos[5] // base
  14445. ];
  14446. if (connector) {
  14447. connector.animate({d: connectorPath});
  14448. connector.attr('visibility', visibility);
  14449. } else {
  14450. point.connector = connector = series.chart.renderer.path(connectorPath).attr({
  14451. 'stroke-width': connectorWidth,
  14452. stroke: options.connectorColor || point.color || '#606060',
  14453. visibility: visibility
  14454. })
  14455. .add(series.group);
  14456. }
  14457. } else if (connector) {
  14458. point.connector = connector.destroy();
  14459. }
  14460. });
  14461. }
  14462. }
  14463. },
  14464. /**
  14465. * Verify whether the data labels are allowed to draw, or we should run more translation and data
  14466. * label positioning to keep them inside the plot area. Returns true when data labels are ready
  14467. * to draw.
  14468. */
  14469. verifyDataLabelOverflow: function (overflow) {
  14470. var center = this.center,
  14471. options = this.options,
  14472. centerOption = options.center,
  14473. minSize = options.minSize || 80,
  14474. newSize = minSize,
  14475. ret;
  14476. // Handle horizontal size and center
  14477. if (centerOption[0] !== null) { // Fixed center
  14478. newSize = mathMax(center[2] - mathMax(overflow[1], overflow[3]), minSize);
  14479. } else { // Auto center
  14480. newSize = mathMax(
  14481. center[2] - overflow[1] - overflow[3], // horizontal overflow
  14482. minSize
  14483. );
  14484. center[0] += (overflow[3] - overflow[1]) / 2; // horizontal center
  14485. }
  14486. // Handle vertical size and center
  14487. if (centerOption[1] !== null) { // Fixed center
  14488. newSize = mathMax(mathMin(newSize, center[2] - mathMax(overflow[0], overflow[2])), minSize);
  14489. } else { // Auto center
  14490. newSize = mathMax(
  14491. mathMin(
  14492. newSize,
  14493. center[2] - overflow[0] - overflow[2] // vertical overflow
  14494. ),
  14495. minSize
  14496. );
  14497. center[1] += (overflow[0] - overflow[2]) / 2; // vertical center
  14498. }
  14499. // If the size must be decreased, we need to run translate and drawDataLabels again
  14500. if (newSize < center[2]) {
  14501. center[2] = newSize;
  14502. this.translate(center);
  14503. each(this.points, function (point) {
  14504. if (point.dataLabel) {
  14505. point.dataLabel._pos = null; // reset
  14506. }
  14507. });
  14508. this.drawDataLabels();
  14509. // Else, return true to indicate that the pie and its labels is within the plot area
  14510. } else {
  14511. ret = true;
  14512. }
  14513. return ret;
  14514. },
  14515. /**
  14516. * Perform the final placement of the data labels after we have verified that they
  14517. * fall within the plot area.
  14518. */
  14519. placeDataLabels: function () {
  14520. each(this.points, function (point) {
  14521. var dataLabel = point.dataLabel,
  14522. _pos;
  14523. if (dataLabel) {
  14524. _pos = dataLabel._pos;
  14525. if (_pos) {
  14526. dataLabel.attr(dataLabel._attr);
  14527. dataLabel[dataLabel.moved ? 'animate' : 'attr'](_pos);
  14528. dataLabel.moved = true;
  14529. } else if (dataLabel) {
  14530. dataLabel.attr({y: -999});
  14531. }
  14532. }
  14533. });
  14534. },
  14535. alignDataLabel: noop,
  14536. /**
  14537. * Draw point specific tracker objects. Inherit directly from column series.
  14538. */
  14539. drawTracker: ColumnSeries.prototype.drawTracker,
  14540. /**
  14541. * Use a simple symbol from column prototype
  14542. */
  14543. drawLegendSymbol: AreaSeries.prototype.drawLegendSymbol,
  14544. /**
  14545. * Pies don't have point marker symbols
  14546. */
  14547. getSymbol: noop
  14548. };
  14549. PieSeries = extendClass(Series, PieSeries);
  14550. seriesTypes.pie = PieSeries;
  14551. // global variables
  14552. extend(Highcharts, {
  14553. // Constructors
  14554. Axis: Axis,
  14555. Chart: Chart,
  14556. Color: Color,
  14557. Legend: Legend,
  14558. Pointer: Pointer,
  14559. Point: Point,
  14560. Tick: Tick,
  14561. Tooltip: Tooltip,
  14562. Renderer: Renderer,
  14563. Series: Series,
  14564. SVGElement: SVGElement,
  14565. SVGRenderer: SVGRenderer,
  14566. // Various
  14567. arrayMin: arrayMin,
  14568. arrayMax: arrayMax,
  14569. charts: charts,
  14570. dateFormat: dateFormat,
  14571. format: format,
  14572. pathAnim: pathAnim,
  14573. getOptions: getOptions,
  14574. hasBidiBug: hasBidiBug,
  14575. isTouchDevice: isTouchDevice,
  14576. numberFormat: numberFormat,
  14577. seriesTypes: seriesTypes,
  14578. setOptions: setOptions,
  14579. addEvent: addEvent,
  14580. removeEvent: removeEvent,
  14581. createElement: createElement,
  14582. discardElement: discardElement,
  14583. css: css,
  14584. each: each,
  14585. extend: extend,
  14586. map: map,
  14587. merge: merge,
  14588. pick: pick,
  14589. splat: splat,
  14590. extendClass: extendClass,
  14591. pInt: pInt,
  14592. wrap: wrap,
  14593. svg: hasSVG,
  14594. canvas: useCanVG,
  14595. vml: !hasSVG && !useCanVG,
  14596. product: PRODUCT,
  14597. version: VERSION
  14598. });
  14599. }());