highcharts.src.js 418 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320
  1. // ==ClosureCompiler==
  2. // @compilation_level SIMPLE_OPTIMIZATIONS
  3. /**
  4. * @license Highcharts JS v3.0.2 (2013-06-05)
  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. charts = [],
  52. PRODUCT = 'Highcharts',
  53. VERSION = '3.0.2',
  54. // some constants for frequently used strings
  55. DIV = 'div',
  56. ABSOLUTE = 'absolute',
  57. RELATIVE = 'relative',
  58. HIDDEN = 'hidden',
  59. PREFIX = 'highcharts-',
  60. VISIBLE = 'visible',
  61. PX = 'px',
  62. NONE = 'none',
  63. M = 'M',
  64. L = 'L',
  65. /*
  66. * Empirical lowest possible opacities for TRACKER_FILL
  67. * IE6: 0.002
  68. * IE7: 0.002
  69. * IE8: 0.002
  70. * IE9: 0.00000000001 (unlimited)
  71. * IE10: 0.0001 (exporting only)
  72. * FF: 0.00000000001 (unlimited)
  73. * Chrome: 0.000001
  74. * Safari: 0.000001
  75. * Opera: 0.00000000001 (unlimited)
  76. */
  77. TRACKER_FILL = 'rgba(192,192,192,' + (hasSVG ? 0.0001 : 0.002) + ')', // invisible but clickable
  78. //TRACKER_FILL = 'rgba(192,192,192,0.5)',
  79. NORMAL_STATE = '',
  80. HOVER_STATE = 'hover',
  81. SELECT_STATE = 'select',
  82. MILLISECOND = 'millisecond',
  83. SECOND = 'second',
  84. MINUTE = 'minute',
  85. HOUR = 'hour',
  86. DAY = 'day',
  87. WEEK = 'week',
  88. MONTH = 'month',
  89. YEAR = 'year',
  90. // constants for attributes
  91. LINEAR_GRADIENT = 'linearGradient',
  92. STOPS = 'stops',
  93. STROKE_WIDTH = 'stroke-width',
  94. // time methods, changed based on whether or not UTC is used
  95. makeTime,
  96. getMinutes,
  97. getHours,
  98. getDay,
  99. getDate,
  100. getMonth,
  101. getFullYear,
  102. setMinutes,
  103. setHours,
  104. setDate,
  105. setMonth,
  106. setFullYear,
  107. // lookup over the types and the associated classes
  108. seriesTypes = {};
  109. // The Highcharts namespace
  110. win.Highcharts = win.Highcharts ? error(16, true) : {};
  111. /**
  112. * Extend an object with the members of another
  113. * @param {Object} a The object to be extended
  114. * @param {Object} b The object to add to the first one
  115. */
  116. function extend(a, b) {
  117. var n;
  118. if (!a) {
  119. a = {};
  120. }
  121. for (n in b) {
  122. a[n] = b[n];
  123. }
  124. return a;
  125. }
  126. /**
  127. * Deep merge two or more objects and return a third object.
  128. * Previously this function redirected to jQuery.extend(true), but this had two limitations.
  129. * First, it deep merged arrays, which lead to workarounds in Highcharts. Second,
  130. * it copied properties from extended prototypes.
  131. */
  132. function merge() {
  133. var i,
  134. len = arguments.length,
  135. ret = {},
  136. doCopy = function (copy, original) {
  137. var value, key;
  138. for (key in original) {
  139. if (original.hasOwnProperty(key)) {
  140. value = original[key];
  141. // An object is replacing a primitive
  142. if (typeof copy !== 'object') {
  143. copy = {};
  144. }
  145. // Copy the contents of objects, but not arrays or DOM nodes
  146. if (value && typeof value === 'object' && Object.prototype.toString.call(value) !== '[object Array]'
  147. && typeof value.nodeType !== 'number') {
  148. copy[key] = doCopy(copy[key] || {}, value);
  149. // Primitives and arrays are copied over directly
  150. } else {
  151. copy[key] = original[key];
  152. }
  153. }
  154. }
  155. return copy;
  156. };
  157. // For each argument, extend the return
  158. for (i = 0; i < len; i++) {
  159. ret = doCopy(ret, arguments[i]);
  160. }
  161. return ret;
  162. }
  163. /**
  164. * Take an array and turn into a hash with even number arguments as keys and odd numbers as
  165. * values. Allows creating constants for commonly used style properties, attributes etc.
  166. * Avoid it in performance critical situations like looping
  167. */
  168. function hash() {
  169. var i = 0,
  170. args = arguments,
  171. length = args.length,
  172. obj = {};
  173. for (; i < length; i++) {
  174. obj[args[i++]] = args[i];
  175. }
  176. return obj;
  177. }
  178. /**
  179. * Shortcut for parseInt
  180. * @param {Object} s
  181. * @param {Number} mag Magnitude
  182. */
  183. function pInt(s, mag) {
  184. return parseInt(s, mag || 10);
  185. }
  186. /**
  187. * Check for string
  188. * @param {Object} s
  189. */
  190. function isString(s) {
  191. return typeof s === 'string';
  192. }
  193. /**
  194. * Check for object
  195. * @param {Object} obj
  196. */
  197. function isObject(obj) {
  198. return typeof obj === 'object';
  199. }
  200. /**
  201. * Check for array
  202. * @param {Object} obj
  203. */
  204. function isArray(obj) {
  205. return Object.prototype.toString.call(obj) === '[object Array]';
  206. }
  207. /**
  208. * Check for number
  209. * @param {Object} n
  210. */
  211. function isNumber(n) {
  212. return typeof n === 'number';
  213. }
  214. function log2lin(num) {
  215. return math.log(num) / math.LN10;
  216. }
  217. function lin2log(num) {
  218. return math.pow(10, num);
  219. }
  220. /**
  221. * Remove last occurence of an item from an array
  222. * @param {Array} arr
  223. * @param {Mixed} item
  224. */
  225. function erase(arr, item) {
  226. var i = arr.length;
  227. while (i--) {
  228. if (arr[i] === item) {
  229. arr.splice(i, 1);
  230. break;
  231. }
  232. }
  233. //return arr;
  234. }
  235. /**
  236. * Returns true if the object is not null or undefined. Like MooTools' $.defined.
  237. * @param {Object} obj
  238. */
  239. function defined(obj) {
  240. return obj !== UNDEFINED && obj !== null;
  241. }
  242. /**
  243. * Set or get an attribute or an object of attributes. Can't use jQuery attr because
  244. * it attempts to set expando properties on the SVG element, which is not allowed.
  245. *
  246. * @param {Object} elem The DOM element to receive the attribute(s)
  247. * @param {String|Object} prop The property or an abject of key-value pairs
  248. * @param {String} value The value if a single property is set
  249. */
  250. function attr(elem, prop, value) {
  251. var key,
  252. setAttribute = 'setAttribute',
  253. ret;
  254. // if the prop is a string
  255. if (isString(prop)) {
  256. // set the value
  257. if (defined(value)) {
  258. elem[setAttribute](prop, value);
  259. // get the value
  260. } else if (elem && elem.getAttribute) { // elem not defined when printing pie demo...
  261. ret = elem.getAttribute(prop);
  262. }
  263. // else if prop is defined, it is a hash of key/value pairs
  264. } else if (defined(prop) && isObject(prop)) {
  265. for (key in prop) {
  266. elem[setAttribute](key, prop[key]);
  267. }
  268. }
  269. return ret;
  270. }
  271. /**
  272. * Check if an element is an array, and if not, make it into an array. Like
  273. * MooTools' $.splat.
  274. */
  275. function splat(obj) {
  276. return isArray(obj) ? obj : [obj];
  277. }
  278. /**
  279. * Return the first value that is defined. Like MooTools' $.pick.
  280. */
  281. function pick() {
  282. var args = arguments,
  283. i,
  284. arg,
  285. length = args.length;
  286. for (i = 0; i < length; i++) {
  287. arg = args[i];
  288. if (typeof arg !== 'undefined' && arg !== null) {
  289. return arg;
  290. }
  291. }
  292. }
  293. /**
  294. * Set CSS on a given element
  295. * @param {Object} el
  296. * @param {Object} styles Style object with camel case property names
  297. */
  298. function css(el, styles) {
  299. if (isIE) {
  300. if (styles && styles.opacity !== UNDEFINED) {
  301. styles.filter = 'alpha(opacity=' + (styles.opacity * 100) + ')';
  302. }
  303. }
  304. extend(el.style, styles);
  305. }
  306. /**
  307. * Utility function to create element with attributes and styles
  308. * @param {Object} tag
  309. * @param {Object} attribs
  310. * @param {Object} styles
  311. * @param {Object} parent
  312. * @param {Object} nopad
  313. */
  314. function createElement(tag, attribs, styles, parent, nopad) {
  315. var el = doc.createElement(tag);
  316. if (attribs) {
  317. extend(el, attribs);
  318. }
  319. if (nopad) {
  320. css(el, {padding: 0, border: NONE, margin: 0});
  321. }
  322. if (styles) {
  323. css(el, styles);
  324. }
  325. if (parent) {
  326. parent.appendChild(el);
  327. }
  328. return el;
  329. }
  330. /**
  331. * Extend a prototyped class by new members
  332. * @param {Object} parent
  333. * @param {Object} members
  334. */
  335. function extendClass(parent, members) {
  336. var object = function () {};
  337. object.prototype = new parent();
  338. extend(object.prototype, members);
  339. return object;
  340. }
  341. /**
  342. * Format a number and return a string based on input settings
  343. * @param {Number} number The input number to format
  344. * @param {Number} decimals The amount of decimals
  345. * @param {String} decPoint The decimal point, defaults to the one given in the lang options
  346. * @param {String} thousandsSep The thousands separator, defaults to the one given in the lang options
  347. */
  348. function numberFormat(number, decimals, decPoint, thousandsSep) {
  349. var lang = defaultOptions.lang,
  350. // http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_number_format/
  351. n = number,
  352. c = decimals === -1 ?
  353. ((n || 0).toString().split('.')[1] || '').length : // preserve decimals
  354. (isNaN(decimals = mathAbs(decimals)) ? 2 : decimals),
  355. d = decPoint === undefined ? lang.decimalPoint : decPoint,
  356. t = thousandsSep === undefined ? lang.thousandsSep : thousandsSep,
  357. s = n < 0 ? "-" : "",
  358. i = String(pInt(n = mathAbs(+n || 0).toFixed(c))),
  359. j = i.length > 3 ? i.length % 3 : 0;
  360. return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) +
  361. (c ? d + mathAbs(n - i).toFixed(c).slice(2) : "");
  362. }
  363. /**
  364. * Pad a string to a given length by adding 0 to the beginning
  365. * @param {Number} number
  366. * @param {Number} length
  367. */
  368. function pad(number, length) {
  369. // Create an array of the remaining length +1 and join it with 0's
  370. return new Array((length || 2) + 1 - String(number).length).join(0) + number;
  371. }
  372. /**
  373. * Wrap a method with extended functionality, preserving the original function
  374. * @param {Object} obj The context object that the method belongs to
  375. * @param {String} method The name of the method to extend
  376. * @param {Function} func A wrapper function callback. This function is called with the same arguments
  377. * as the original function, except that the original function is unshifted and passed as the first
  378. * argument.
  379. */
  380. function wrap(obj, method, func) {
  381. var proceed = obj[method];
  382. obj[method] = function () {
  383. var args = Array.prototype.slice.call(arguments);
  384. args.unshift(proceed);
  385. return func.apply(this, args);
  386. };
  387. }
  388. /**
  389. * Based on http://www.php.net/manual/en/function.strftime.php
  390. * @param {String} format
  391. * @param {Number} timestamp
  392. * @param {Boolean} capitalize
  393. */
  394. dateFormat = function (format, timestamp, capitalize) {
  395. if (!defined(timestamp) || isNaN(timestamp)) {
  396. return 'Invalid date';
  397. }
  398. format = pick(format, '%Y-%m-%d %H:%M:%S');
  399. var date = new Date(timestamp),
  400. key, // used in for constuct below
  401. // get the basic time values
  402. hours = date[getHours](),
  403. day = date[getDay](),
  404. dayOfMonth = date[getDate](),
  405. month = date[getMonth](),
  406. fullYear = date[getFullYear](),
  407. lang = defaultOptions.lang,
  408. langWeekdays = lang.weekdays,
  409. // List all format keys. Custom formats can be added from the outside.
  410. replacements = extend({
  411. // Day
  412. 'a': langWeekdays[day].substr(0, 3), // Short weekday, like 'Mon'
  413. 'A': langWeekdays[day], // Long weekday, like 'Monday'
  414. 'd': pad(dayOfMonth), // Two digit day of the month, 01 to 31
  415. 'e': dayOfMonth, // Day of the month, 1 through 31
  416. // Week (none implemented)
  417. //'W': weekNumber(),
  418. // Month
  419. 'b': lang.shortMonths[month], // Short month, like 'Jan'
  420. 'B': lang.months[month], // Long month, like 'January'
  421. 'm': pad(month + 1), // Two digit month number, 01 through 12
  422. // Year
  423. 'y': fullYear.toString().substr(2, 2), // Two digits year, like 09 for 2009
  424. 'Y': fullYear, // Four digits year, like 2009
  425. // Time
  426. 'H': pad(hours), // Two digits hours in 24h format, 00 through 23
  427. 'I': pad((hours % 12) || 12), // Two digits hours in 12h format, 00 through 11
  428. 'l': (hours % 12) || 12, // Hours in 12h format, 1 through 12
  429. 'M': pad(date[getMinutes]()), // Two digits minutes, 00 through 59
  430. 'p': hours < 12 ? 'AM' : 'PM', // Upper case AM or PM
  431. 'P': hours < 12 ? 'am' : 'pm', // Lower case AM or PM
  432. 'S': pad(date.getSeconds()), // Two digits seconds, 00 through 59
  433. 'L': pad(mathRound(timestamp % 1000), 3) // Milliseconds (naming from Ruby)
  434. }, Highcharts.dateFormats);
  435. // do the replaces
  436. for (key in replacements) {
  437. while (format.indexOf('%' + key) !== -1) { // regex would do it in one line, but this is faster
  438. format = format.replace('%' + key, typeof replacements[key] === 'function' ? replacements[key](timestamp) : replacements[key]);
  439. }
  440. }
  441. // Optionally capitalize the string and return
  442. return capitalize ? format.substr(0, 1).toUpperCase() + format.substr(1) : format;
  443. };
  444. /**
  445. * Format a single variable. Similar to sprintf, without the % prefix.
  446. */
  447. function formatSingle(format, val) {
  448. var floatRegex = /f$/,
  449. decRegex = /\.([0-9])/,
  450. lang = defaultOptions.lang,
  451. decimals;
  452. if (floatRegex.test(format)) { // float
  453. decimals = format.match(decRegex);
  454. decimals = decimals ? decimals[1] : -1;
  455. val = numberFormat(
  456. val,
  457. decimals,
  458. lang.decimalPoint,
  459. format.indexOf(',') > -1 ? lang.thousandsSep : ''
  460. );
  461. } else {
  462. val = dateFormat(format, val);
  463. }
  464. return val;
  465. }
  466. /**
  467. * Format a string according to a subset of the rules of Python's String.format method.
  468. */
  469. function format(str, ctx) {
  470. var splitter = '{',
  471. isInside = false,
  472. segment,
  473. valueAndFormat,
  474. path,
  475. i,
  476. len,
  477. ret = [],
  478. val,
  479. index;
  480. while ((index = str.indexOf(splitter)) !== -1) {
  481. segment = str.slice(0, index);
  482. if (isInside) { // we're on the closing bracket looking back
  483. valueAndFormat = segment.split(':');
  484. path = valueAndFormat.shift().split('.'); // get first and leave format
  485. len = path.length;
  486. val = ctx;
  487. // Assign deeper paths
  488. for (i = 0; i < len; i++) {
  489. val = val[path[i]];
  490. }
  491. // Format the replacement
  492. if (valueAndFormat.length) {
  493. val = formatSingle(valueAndFormat.join(':'), val);
  494. }
  495. // Push the result and advance the cursor
  496. ret.push(val);
  497. } else {
  498. ret.push(segment);
  499. }
  500. str = str.slice(index + 1); // the rest
  501. isInside = !isInside; // toggle
  502. splitter = isInside ? '}' : '{'; // now look for next matching bracket
  503. }
  504. ret.push(str);
  505. return ret.join('');
  506. }
  507. /**
  508. * Take an interval and normalize it to multiples of 1, 2, 2.5 and 5
  509. * @param {Number} interval
  510. * @param {Array} multiples
  511. * @param {Number} magnitude
  512. * @param {Object} options
  513. */
  514. function normalizeTickInterval(interval, multiples, magnitude, options) {
  515. var normalized, i;
  516. // round to a tenfold of 1, 2, 2.5 or 5
  517. magnitude = pick(magnitude, 1);
  518. normalized = interval / magnitude;
  519. // multiples for a linear scale
  520. if (!multiples) {
  521. multiples = [1, 2, 2.5, 5, 10];
  522. // the allowDecimals option
  523. if (options && options.allowDecimals === false) {
  524. if (magnitude === 1) {
  525. multiples = [1, 2, 5, 10];
  526. } else if (magnitude <= 0.1) {
  527. multiples = [1 / magnitude];
  528. }
  529. }
  530. }
  531. // normalize the interval to the nearest multiple
  532. for (i = 0; i < multiples.length; i++) {
  533. interval = multiples[i];
  534. if (normalized <= (multiples[i] + (multiples[i + 1] || multiples[i])) / 2) {
  535. break;
  536. }
  537. }
  538. // multiply back to the correct magnitude
  539. interval *= magnitude;
  540. return interval;
  541. }
  542. /**
  543. * Get a normalized tick interval for dates. Returns a configuration object with
  544. * unit range (interval), count and name. Used to prepare data for getTimeTicks.
  545. * Previously this logic was part of getTimeTicks, but as getTimeTicks now runs
  546. * of segments in stock charts, the normalizing logic was extracted in order to
  547. * prevent it for running over again for each segment having the same interval.
  548. * #662, #697.
  549. */
  550. function normalizeTimeTickInterval(tickInterval, unitsOption) {
  551. var units = unitsOption || [[
  552. MILLISECOND, // unit name
  553. [1, 2, 5, 10, 20, 25, 50, 100, 200, 500] // allowed multiples
  554. ], [
  555. SECOND,
  556. [1, 2, 5, 10, 15, 30]
  557. ], [
  558. MINUTE,
  559. [1, 2, 5, 10, 15, 30]
  560. ], [
  561. HOUR,
  562. [1, 2, 3, 4, 6, 8, 12]
  563. ], [
  564. DAY,
  565. [1, 2]
  566. ], [
  567. WEEK,
  568. [1, 2]
  569. ], [
  570. MONTH,
  571. [1, 2, 3, 4, 6]
  572. ], [
  573. YEAR,
  574. null
  575. ]],
  576. unit = units[units.length - 1], // default unit is years
  577. interval = timeUnits[unit[0]],
  578. multiples = unit[1],
  579. count,
  580. i;
  581. // loop through the units to find the one that best fits the tickInterval
  582. for (i = 0; i < units.length; i++) {
  583. unit = units[i];
  584. interval = timeUnits[unit[0]];
  585. multiples = unit[1];
  586. if (units[i + 1]) {
  587. // lessThan is in the middle between the highest multiple and the next unit.
  588. var lessThan = (interval * multiples[multiples.length - 1] +
  589. timeUnits[units[i + 1][0]]) / 2;
  590. // break and keep the current unit
  591. if (tickInterval <= lessThan) {
  592. break;
  593. }
  594. }
  595. }
  596. // prevent 2.5 years intervals, though 25, 250 etc. are allowed
  597. if (interval === timeUnits[YEAR] && tickInterval < 5 * interval) {
  598. multiples = [1, 2, 5];
  599. }
  600. // prevent 2.5 years intervals, though 25, 250 etc. are allowed
  601. if (interval === timeUnits[YEAR] && tickInterval < 5 * interval) {
  602. multiples = [1, 2, 5];
  603. }
  604. // get the count
  605. count = normalizeTickInterval(tickInterval / interval, multiples);
  606. return {
  607. unitRange: interval,
  608. count: count,
  609. unitName: unit[0]
  610. };
  611. }
  612. /**
  613. * Set the tick positions to a time unit that makes sense, for example
  614. * on the first of each month or on every Monday. Return an array
  615. * with the time positions. Used in datetime axes as well as for grouping
  616. * data on a datetime axis.
  617. *
  618. * @param {Object} normalizedInterval The interval in axis values (ms) and the count
  619. * @param {Number} min The minimum in axis values
  620. * @param {Number} max The maximum in axis values
  621. * @param {Number} startOfWeek
  622. */
  623. function getTimeTicks(normalizedInterval, min, max, startOfWeek) {
  624. var tickPositions = [],
  625. i,
  626. higherRanks = {},
  627. useUTC = defaultOptions.global.useUTC,
  628. minYear, // used in months and years as a basis for Date.UTC()
  629. minDate = new Date(min),
  630. interval = normalizedInterval.unitRange,
  631. count = normalizedInterval.count;
  632. if (defined(min)) { // #1300
  633. if (interval >= timeUnits[SECOND]) { // second
  634. minDate.setMilliseconds(0);
  635. minDate.setSeconds(interval >= timeUnits[MINUTE] ? 0 :
  636. count * mathFloor(minDate.getSeconds() / count));
  637. }
  638. if (interval >= timeUnits[MINUTE]) { // minute
  639. minDate[setMinutes](interval >= timeUnits[HOUR] ? 0 :
  640. count * mathFloor(minDate[getMinutes]() / count));
  641. }
  642. if (interval >= timeUnits[HOUR]) { // hour
  643. minDate[setHours](interval >= timeUnits[DAY] ? 0 :
  644. count * mathFloor(minDate[getHours]() / count));
  645. }
  646. if (interval >= timeUnits[DAY]) { // day
  647. minDate[setDate](interval >= timeUnits[MONTH] ? 1 :
  648. count * mathFloor(minDate[getDate]() / count));
  649. }
  650. if (interval >= timeUnits[MONTH]) { // month
  651. minDate[setMonth](interval >= timeUnits[YEAR] ? 0 :
  652. count * mathFloor(minDate[getMonth]() / count));
  653. minYear = minDate[getFullYear]();
  654. }
  655. if (interval >= timeUnits[YEAR]) { // year
  656. minYear -= minYear % count;
  657. minDate[setFullYear](minYear);
  658. }
  659. // week is a special case that runs outside the hierarchy
  660. if (interval === timeUnits[WEEK]) {
  661. // get start of current week, independent of count
  662. minDate[setDate](minDate[getDate]() - minDate[getDay]() +
  663. pick(startOfWeek, 1));
  664. }
  665. // get tick positions
  666. i = 1;
  667. minYear = minDate[getFullYear]();
  668. var time = minDate.getTime(),
  669. minMonth = minDate[getMonth](),
  670. minDateDate = minDate[getDate](),
  671. timezoneOffset = useUTC ?
  672. 0 :
  673. (24 * 3600 * 1000 + minDate.getTimezoneOffset() * 60 * 1000) % (24 * 3600 * 1000); // #950
  674. // iterate and add tick positions at appropriate values
  675. while (time < max) {
  676. tickPositions.push(time);
  677. // if the interval is years, use Date.UTC to increase years
  678. if (interval === timeUnits[YEAR]) {
  679. time = makeTime(minYear + i * count, 0);
  680. // if the interval is months, use Date.UTC to increase months
  681. } else if (interval === timeUnits[MONTH]) {
  682. time = makeTime(minYear, minMonth + i * count);
  683. // if we're using global time, the interval is not fixed as it jumps
  684. // one hour at the DST crossover
  685. } else if (!useUTC && (interval === timeUnits[DAY] || interval === timeUnits[WEEK])) {
  686. time = makeTime(minYear, minMonth, minDateDate +
  687. i * count * (interval === timeUnits[DAY] ? 1 : 7));
  688. // else, the interval is fixed and we use simple addition
  689. } else {
  690. time += interval * count;
  691. }
  692. i++;
  693. }
  694. // push the last time
  695. tickPositions.push(time);
  696. // mark new days if the time is dividible by day (#1649, #1760)
  697. each(grep(tickPositions, function (time) {
  698. return interval <= timeUnits[HOUR] && time % timeUnits[DAY] === timezoneOffset;
  699. }), function (time) {
  700. higherRanks[time] = DAY;
  701. });
  702. }
  703. // record information on the chosen unit - for dynamic label formatter
  704. tickPositions.info = extend(normalizedInterval, {
  705. higherRanks: higherRanks,
  706. totalRange: interval * count
  707. });
  708. return tickPositions;
  709. }
  710. /**
  711. * Helper class that contains variuos counters that are local to the chart.
  712. */
  713. function ChartCounters() {
  714. this.color = 0;
  715. this.symbol = 0;
  716. }
  717. ChartCounters.prototype = {
  718. /**
  719. * Wraps the color counter if it reaches the specified length.
  720. */
  721. wrapColor: function (length) {
  722. if (this.color >= length) {
  723. this.color = 0;
  724. }
  725. },
  726. /**
  727. * Wraps the symbol counter if it reaches the specified length.
  728. */
  729. wrapSymbol: function (length) {
  730. if (this.symbol >= length) {
  731. this.symbol = 0;
  732. }
  733. }
  734. };
  735. /**
  736. * Utility method that sorts an object array and keeping the order of equal items.
  737. * ECMA script standard does not specify the behaviour when items are equal.
  738. */
  739. function stableSort(arr, sortFunction) {
  740. var length = arr.length,
  741. sortValue,
  742. i;
  743. // Add index to each item
  744. for (i = 0; i < length; i++) {
  745. arr[i].ss_i = i; // stable sort index
  746. }
  747. arr.sort(function (a, b) {
  748. sortValue = sortFunction(a, b);
  749. return sortValue === 0 ? a.ss_i - b.ss_i : sortValue;
  750. });
  751. // Remove index from items
  752. for (i = 0; i < length; i++) {
  753. delete arr[i].ss_i; // stable sort index
  754. }
  755. }
  756. /**
  757. * Non-recursive method to find the lowest member of an array. Math.min raises a maximum
  758. * call stack size exceeded error in Chrome when trying to apply more than 150.000 points. This
  759. * method is slightly slower, but safe.
  760. */
  761. function arrayMin(data) {
  762. var i = data.length,
  763. min = data[0];
  764. while (i--) {
  765. if (data[i] < min) {
  766. min = data[i];
  767. }
  768. }
  769. return min;
  770. }
  771. /**
  772. * Non-recursive method to find the lowest member of an array. Math.min raises a maximum
  773. * call stack size exceeded error in Chrome when trying to apply more than 150.000 points. This
  774. * method is slightly slower, but safe.
  775. */
  776. function arrayMax(data) {
  777. var i = data.length,
  778. max = data[0];
  779. while (i--) {
  780. if (data[i] > max) {
  781. max = data[i];
  782. }
  783. }
  784. return max;
  785. }
  786. /**
  787. * Utility method that destroys any SVGElement or VMLElement that are properties on the given object.
  788. * It loops all properties and invokes destroy if there is a destroy method. The property is
  789. * then delete'ed.
  790. * @param {Object} The object to destroy properties on
  791. * @param {Object} Exception, do not destroy this property, only delete it.
  792. */
  793. function destroyObjectProperties(obj, except) {
  794. var n;
  795. for (n in obj) {
  796. // If the object is non-null and destroy is defined
  797. if (obj[n] && obj[n] !== except && obj[n].destroy) {
  798. // Invoke the destroy
  799. obj[n].destroy();
  800. }
  801. // Delete the property from the object.
  802. delete obj[n];
  803. }
  804. }
  805. /**
  806. * Discard an element by moving it to the bin and delete
  807. * @param {Object} The HTML node to discard
  808. */
  809. function discardElement(element) {
  810. // create a garbage bin element, not part of the DOM
  811. if (!garbageBin) {
  812. garbageBin = createElement(DIV);
  813. }
  814. // move the node and empty bin
  815. if (element) {
  816. garbageBin.appendChild(element);
  817. }
  818. garbageBin.innerHTML = '';
  819. }
  820. /**
  821. * Provide error messages for debugging, with links to online explanation
  822. */
  823. function error(code, stop) {
  824. var msg = 'Highcharts error #' + code + ': www.highcharts.com/errors/' + code;
  825. if (stop) {
  826. throw msg;
  827. } else if (win.console) {
  828. console.log(msg);
  829. }
  830. }
  831. /**
  832. * Fix JS round off float errors
  833. * @param {Number} num
  834. */
  835. function correctFloat(num) {
  836. return parseFloat(
  837. num.toPrecision(14)
  838. );
  839. }
  840. /**
  841. * Set the global animation to either a given value, or fall back to the
  842. * given chart's animation option
  843. * @param {Object} animation
  844. * @param {Object} chart
  845. */
  846. function setAnimation(animation, chart) {
  847. globalAnimation = pick(animation, chart.animation);
  848. }
  849. /**
  850. * The time unit lookup
  851. */
  852. /*jslint white: true*/
  853. timeUnits = hash(
  854. MILLISECOND, 1,
  855. SECOND, 1000,
  856. MINUTE, 60000,
  857. HOUR, 3600000,
  858. DAY, 24 * 3600000,
  859. WEEK, 7 * 24 * 3600000,
  860. MONTH, 31 * 24 * 3600000,
  861. YEAR, 31556952000
  862. );
  863. /*jslint white: false*/
  864. /**
  865. * Path interpolation algorithm used across adapters
  866. */
  867. pathAnim = {
  868. /**
  869. * Prepare start and end values so that the path can be animated one to one
  870. */
  871. init: function (elem, fromD, toD) {
  872. fromD = fromD || '';
  873. var shift = elem.shift,
  874. bezier = fromD.indexOf('C') > -1,
  875. numParams = bezier ? 7 : 3,
  876. endLength,
  877. slice,
  878. i,
  879. start = fromD.split(' '),
  880. end = [].concat(toD), // copy
  881. startBaseLine,
  882. endBaseLine,
  883. sixify = function (arr) { // in splines make move points have six parameters like bezier curves
  884. i = arr.length;
  885. while (i--) {
  886. if (arr[i] === M) {
  887. arr.splice(i + 1, 0, arr[i + 1], arr[i + 2], arr[i + 1], arr[i + 2]);
  888. }
  889. }
  890. };
  891. if (bezier) {
  892. sixify(start);
  893. sixify(end);
  894. }
  895. // pull out the base lines before padding
  896. if (elem.isArea) {
  897. startBaseLine = start.splice(start.length - 6, 6);
  898. endBaseLine = end.splice(end.length - 6, 6);
  899. }
  900. // if shifting points, prepend a dummy point to the end path
  901. if (shift <= end.length / numParams) {
  902. while (shift--) {
  903. end = [].concat(end).splice(0, numParams).concat(end);
  904. }
  905. }
  906. elem.shift = 0; // reset for following animations
  907. // copy and append last point until the length matches the end length
  908. if (start.length) {
  909. endLength = end.length;
  910. while (start.length < endLength) {
  911. //bezier && sixify(start);
  912. slice = [].concat(start).splice(start.length - numParams, numParams);
  913. if (bezier) { // disable first control point
  914. slice[numParams - 6] = slice[numParams - 2];
  915. slice[numParams - 5] = slice[numParams - 1];
  916. }
  917. start = start.concat(slice);
  918. }
  919. }
  920. if (startBaseLine) { // append the base lines for areas
  921. start = start.concat(startBaseLine);
  922. end = end.concat(endBaseLine);
  923. }
  924. return [start, end];
  925. },
  926. /**
  927. * Interpolate each value of the path and return the array
  928. */
  929. step: function (start, end, pos, complete) {
  930. var ret = [],
  931. i = start.length,
  932. startVal;
  933. if (pos === 1) { // land on the final path without adjustment points appended in the ends
  934. ret = complete;
  935. } else if (i === end.length && pos < 1) {
  936. while (i--) {
  937. startVal = parseFloat(start[i]);
  938. ret[i] =
  939. isNaN(startVal) ? // a letter instruction like M or L
  940. start[i] :
  941. pos * (parseFloat(end[i] - startVal)) + startVal;
  942. }
  943. } else { // if animation is finished or length not matching, land on right value
  944. ret = end;
  945. }
  946. return ret;
  947. }
  948. };
  949. (function ($) {
  950. /**
  951. * The default HighchartsAdapter for jQuery
  952. */
  953. win.HighchartsAdapter = win.HighchartsAdapter || ($ && {
  954. /**
  955. * Initialize the adapter by applying some extensions to jQuery
  956. */
  957. init: function (pathAnim) {
  958. // extend the animate function to allow SVG animations
  959. var Fx = $.fx,
  960. Step = Fx.step,
  961. dSetter,
  962. Tween = $.Tween,
  963. propHooks = Tween && Tween.propHooks,
  964. opacityHook = $.cssHooks.opacity;
  965. /*jslint unparam: true*//* allow unused param x in this function */
  966. $.extend($.easing, {
  967. easeOutQuad: function (x, t, b, c, d) {
  968. return -c * (t /= d) * (t - 2) + b;
  969. }
  970. });
  971. /*jslint unparam: false*/
  972. // extend some methods to check for elem.attr, which means it is a Highcharts SVG object
  973. $.each(['cur', '_default', 'width', 'height', 'opacity'], function (i, fn) {
  974. var obj = Step,
  975. base,
  976. elem;
  977. // Handle different parent objects
  978. if (fn === 'cur') {
  979. obj = Fx.prototype; // 'cur', the getter, relates to Fx.prototype
  980. } else if (fn === '_default' && Tween) { // jQuery 1.8 model
  981. obj = propHooks[fn];
  982. fn = 'set';
  983. }
  984. // Overwrite the method
  985. base = obj[fn];
  986. if (base) { // step.width and step.height don't exist in jQuery < 1.7
  987. // create the extended function replacement
  988. obj[fn] = function (fx) {
  989. // Fx.prototype.cur does not use fx argument
  990. fx = i ? fx : this;
  991. // shortcut
  992. elem = fx.elem;
  993. // Fx.prototype.cur returns the current value. The other ones are setters
  994. // and returning a value has no effect.
  995. return elem.attr ? // is SVG element wrapper
  996. elem.attr(fx.prop, fn === 'cur' ? UNDEFINED : fx.now) : // apply the SVG wrapper's method
  997. base.apply(this, arguments); // use jQuery's built-in method
  998. };
  999. }
  1000. });
  1001. // Extend the opacity getter, needed for fading opacity with IE9 and jQuery 1.10+
  1002. wrap(opacityHook, 'get', function (proceed, elem, computed) {
  1003. return elem.attr ? (elem.opacity || 0) : proceed.call(this, elem, computed);
  1004. });
  1005. // Define the setter function for d (path definitions)
  1006. dSetter = function (fx) {
  1007. var elem = fx.elem,
  1008. ends;
  1009. // Normally start and end should be set in state == 0, but sometimes,
  1010. // for reasons unknown, this doesn't happen. Perhaps state == 0 is skipped
  1011. // in these cases
  1012. if (!fx.started) {
  1013. ends = pathAnim.init(elem, elem.d, elem.toD);
  1014. fx.start = ends[0];
  1015. fx.end = ends[1];
  1016. fx.started = true;
  1017. }
  1018. // interpolate each value of the path
  1019. elem.attr('d', pathAnim.step(fx.start, fx.end, fx.pos, elem.toD));
  1020. };
  1021. // jQuery 1.8 style
  1022. if (Tween) {
  1023. propHooks.d = {
  1024. set: dSetter
  1025. };
  1026. // pre 1.8
  1027. } else {
  1028. // animate paths
  1029. Step.d = dSetter;
  1030. }
  1031. /**
  1032. * Utility for iterating over an array. Parameters are reversed compared to jQuery.
  1033. * @param {Array} arr
  1034. * @param {Function} fn
  1035. */
  1036. this.each = Array.prototype.forEach ?
  1037. function (arr, fn) { // modern browsers
  1038. return Array.prototype.forEach.call(arr, fn);
  1039. } :
  1040. function (arr, fn) { // legacy
  1041. var i = 0,
  1042. len = arr.length;
  1043. for (; i < len; i++) {
  1044. if (fn.call(arr[i], arr[i], i, arr) === false) {
  1045. return i;
  1046. }
  1047. }
  1048. };
  1049. /**
  1050. * Register Highcharts as a plugin in the respective framework
  1051. */
  1052. $.fn.highcharts = function () {
  1053. var constr = 'Chart', // default constructor
  1054. args = arguments,
  1055. options,
  1056. ret,
  1057. chart;
  1058. if (isString(args[0])) {
  1059. constr = args[0];
  1060. args = Array.prototype.slice.call(args, 1);
  1061. }
  1062. options = args[0];
  1063. // Create the chart
  1064. if (options !== UNDEFINED) {
  1065. /*jslint unused:false*/
  1066. options.chart = options.chart || {};
  1067. options.chart.renderTo = this[0];
  1068. chart = new Highcharts[constr](options, args[1]);
  1069. ret = this;
  1070. /*jslint unused:true*/
  1071. }
  1072. // When called without parameters or with the return argument, get a predefined chart
  1073. if (options === UNDEFINED) {
  1074. ret = charts[attr(this[0], 'data-highcharts-chart')];
  1075. }
  1076. return ret;
  1077. };
  1078. },
  1079. /**
  1080. * Downloads a script and executes a callback when done.
  1081. * @param {String} scriptLocation
  1082. * @param {Function} callback
  1083. */
  1084. getScript: $.getScript,
  1085. /**
  1086. * Return the index of an item in an array, or -1 if not found
  1087. */
  1088. inArray: $.inArray,
  1089. /**
  1090. * A direct link to jQuery methods. MooTools and Prototype adapters must be implemented for each case of method.
  1091. * @param {Object} elem The HTML element
  1092. * @param {String} method Which method to run on the wrapped element
  1093. */
  1094. adapterRun: function (elem, method) {
  1095. return $(elem)[method]();
  1096. },
  1097. /**
  1098. * Filter an array
  1099. */
  1100. grep: $.grep,
  1101. /**
  1102. * Map an array
  1103. * @param {Array} arr
  1104. * @param {Function} fn
  1105. */
  1106. map: function (arr, fn) {
  1107. //return jQuery.map(arr, fn);
  1108. var results = [],
  1109. i = 0,
  1110. len = arr.length;
  1111. for (; i < len; i++) {
  1112. results[i] = fn.call(arr[i], arr[i], i, arr);
  1113. }
  1114. return results;
  1115. },
  1116. /**
  1117. * Get the position of an element relative to the top left of the page
  1118. */
  1119. offset: function (el) {
  1120. return $(el).offset();
  1121. },
  1122. /**
  1123. * Add an event listener
  1124. * @param {Object} el A HTML element or custom object
  1125. * @param {String} event The event type
  1126. * @param {Function} fn The event handler
  1127. */
  1128. addEvent: function (el, event, fn) {
  1129. $(el).bind(event, fn);
  1130. },
  1131. /**
  1132. * Remove event added with addEvent
  1133. * @param {Object} el The object
  1134. * @param {String} eventType The event type. Leave blank to remove all events.
  1135. * @param {Function} handler The function to remove
  1136. */
  1137. removeEvent: function (el, eventType, handler) {
  1138. // workaround for jQuery issue with unbinding custom events:
  1139. // http://forum.jQuery.com/topic/javascript-error-when-unbinding-a-custom-event-using-jQuery-1-4-2
  1140. var func = doc.removeEventListener ? 'removeEventListener' : 'detachEvent';
  1141. if (doc[func] && el && !el[func]) {
  1142. el[func] = function () {};
  1143. }
  1144. $(el).unbind(eventType, handler);
  1145. },
  1146. /**
  1147. * Fire an event on a custom object
  1148. * @param {Object} el
  1149. * @param {String} type
  1150. * @param {Object} eventArguments
  1151. * @param {Function} defaultFunction
  1152. */
  1153. fireEvent: function (el, type, eventArguments, defaultFunction) {
  1154. var event = $.Event(type),
  1155. detachedType = 'detached' + type,
  1156. defaultPrevented;
  1157. // Remove warnings in Chrome when accessing layerX and layerY. Although Highcharts
  1158. // never uses these properties, Chrome includes them in the default click event and
  1159. // raises the warning when they are copied over in the extend statement below.
  1160. //
  1161. // To avoid problems in IE (see #1010) where we cannot delete the properties and avoid
  1162. // testing if they are there (warning in chrome) the only option is to test if running IE.
  1163. if (!isIE && eventArguments) {
  1164. delete eventArguments.layerX;
  1165. delete eventArguments.layerY;
  1166. }
  1167. extend(event, eventArguments);
  1168. // Prevent jQuery from triggering the object method that is named the
  1169. // same as the event. For example, if the event is 'select', jQuery
  1170. // attempts calling el.select and it goes into a loop.
  1171. if (el[type]) {
  1172. el[detachedType] = el[type];
  1173. el[type] = null;
  1174. }
  1175. // Wrap preventDefault and stopPropagation in try/catch blocks in
  1176. // order to prevent JS errors when cancelling events on non-DOM
  1177. // objects. #615.
  1178. /*jslint unparam: true*/
  1179. $.each(['preventDefault', 'stopPropagation'], function (i, fn) {
  1180. var base = event[fn];
  1181. event[fn] = function () {
  1182. try {
  1183. base.call(event);
  1184. } catch (e) {
  1185. if (fn === 'preventDefault') {
  1186. defaultPrevented = true;
  1187. }
  1188. }
  1189. };
  1190. });
  1191. /*jslint unparam: false*/
  1192. // trigger it
  1193. $(el).trigger(event);
  1194. // attach the method
  1195. if (el[detachedType]) {
  1196. el[type] = el[detachedType];
  1197. el[detachedType] = null;
  1198. }
  1199. if (defaultFunction && !event.isDefaultPrevented() && !defaultPrevented) {
  1200. defaultFunction(event);
  1201. }
  1202. },
  1203. /**
  1204. * Extension method needed for MooTools
  1205. */
  1206. washMouseEvent: function (e) {
  1207. var ret = e.originalEvent || e;
  1208. // computed by jQuery, needed by IE8
  1209. if (ret.pageX === UNDEFINED) { // #1236
  1210. ret.pageX = e.pageX;
  1211. ret.pageY = e.pageY;
  1212. }
  1213. return ret;
  1214. },
  1215. /**
  1216. * Animate a HTML element or SVG element wrapper
  1217. * @param {Object} el
  1218. * @param {Object} params
  1219. * @param {Object} options jQuery-like animation options: duration, easing, callback
  1220. */
  1221. animate: function (el, params, options) {
  1222. var $el = $(el);
  1223. if (!el.style) {
  1224. el.style = {}; // #1881
  1225. }
  1226. if (params.d) {
  1227. el.toD = params.d; // keep the array form for paths, used in $.fx.step.d
  1228. params.d = 1; // because in jQuery, animating to an array has a different meaning
  1229. }
  1230. $el.stop();
  1231. $el.animate(params, options);
  1232. },
  1233. /**
  1234. * Stop running animation
  1235. */
  1236. stop: function (el) {
  1237. $(el).stop();
  1238. }
  1239. });
  1240. }(win.jQuery));
  1241. // check for a custom HighchartsAdapter defined prior to this file
  1242. var globalAdapter = win.HighchartsAdapter,
  1243. adapter = globalAdapter || {};
  1244. // Initialize the adapter
  1245. if (globalAdapter) {
  1246. globalAdapter.init.call(globalAdapter, pathAnim);
  1247. }
  1248. // Utility functions. If the HighchartsAdapter is not defined, adapter is an empty object
  1249. // and all the utility functions will be null. In that case they are populated by the
  1250. // default adapters below.
  1251. var adapterRun = adapter.adapterRun,
  1252. getScript = adapter.getScript,
  1253. inArray = adapter.inArray,
  1254. each = adapter.each,
  1255. grep = adapter.grep,
  1256. offset = adapter.offset,
  1257. map = adapter.map,
  1258. addEvent = adapter.addEvent,
  1259. removeEvent = adapter.removeEvent,
  1260. fireEvent = adapter.fireEvent,
  1261. washMouseEvent = adapter.washMouseEvent,
  1262. animate = adapter.animate,
  1263. stop = adapter.stop;
  1264. /* ****************************************************************************
  1265. * Handle the options *
  1266. *****************************************************************************/
  1267. var
  1268. defaultLabelOptions = {
  1269. enabled: true,
  1270. // rotation: 0,
  1271. align: 'center',
  1272. x: 0,
  1273. y: 15,
  1274. /*formatter: function () {
  1275. return this.value;
  1276. },*/
  1277. style: {
  1278. color: '#666',
  1279. cursor: 'default',
  1280. fontSize: '11px',
  1281. lineHeight: '14px'
  1282. }
  1283. };
  1284. defaultOptions = {
  1285. colors: ['#2f7ed8', '#0d233a', '#8bbc21', '#910000', '#1aadce', '#492970',
  1286. '#f28f43', '#77a1e5', '#c42525', '#a6c96a'],
  1287. symbols: ['circle', 'diamond', 'square', 'triangle', 'triangle-down'],
  1288. lang: {
  1289. loading: 'Loading...',
  1290. months: ['January', 'February', 'March', 'April', 'May', 'June', 'July',
  1291. 'August', 'September', 'October', 'November', 'December'],
  1292. shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  1293. weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
  1294. decimalPoint: '.',
  1295. numericSymbols: ['k', 'M', 'G', 'T', 'P', 'E'], // SI prefixes used in axis labels
  1296. resetZoom: 'Reset zoom',
  1297. resetZoomTitle: 'Reset zoom level 1:1',
  1298. thousandsSep: ','
  1299. },
  1300. global: {
  1301. useUTC: true,
  1302. canvasToolsURL: 'http://code.highcharts.com/3.0.2/modules/canvas-tools.js',
  1303. VMLRadialGradientURL: 'http://code.highcharts.com/3.0.2/gfx/vml-radial-gradient.png'
  1304. },
  1305. chart: {
  1306. //animation: true,
  1307. //alignTicks: false,
  1308. //reflow: true,
  1309. //className: null,
  1310. //events: { load, selection },
  1311. //margin: [null],
  1312. //marginTop: null,
  1313. //marginRight: null,
  1314. //marginBottom: null,
  1315. //marginLeft: null,
  1316. borderColor: '#4572A7',
  1317. //borderWidth: 0,
  1318. borderRadius: 5,
  1319. defaultSeriesType: 'line',
  1320. ignoreHiddenSeries: true,
  1321. //inverted: false,
  1322. //shadow: false,
  1323. spacingTop: 10,
  1324. spacingRight: 10,
  1325. spacingBottom: 15,
  1326. spacingLeft: 10,
  1327. style: {
  1328. fontFamily: '"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif', // default font
  1329. fontSize: '12px'
  1330. },
  1331. backgroundColor: '#FFFFFF',
  1332. //plotBackgroundColor: null,
  1333. plotBorderColor: '#C0C0C0',
  1334. //plotBorderWidth: 0,
  1335. //plotShadow: false,
  1336. //zoomType: ''
  1337. resetZoomButton: {
  1338. theme: {
  1339. zIndex: 20
  1340. },
  1341. position: {
  1342. align: 'right',
  1343. x: -10,
  1344. //verticalAlign: 'top',
  1345. y: 10
  1346. }
  1347. // relativeTo: 'plot'
  1348. }
  1349. },
  1350. title: {
  1351. text: 'Chart title',
  1352. align: 'center',
  1353. // floating: false,
  1354. // margin: 15,
  1355. // x: 0,
  1356. // verticalAlign: 'top',
  1357. y: 15,
  1358. style: {
  1359. color: '#274b6d',//#3E576F',
  1360. fontSize: '16px'
  1361. }
  1362. },
  1363. subtitle: {
  1364. text: '',
  1365. align: 'center',
  1366. // floating: false
  1367. // x: 0,
  1368. // verticalAlign: 'top',
  1369. y: 30,
  1370. style: {
  1371. color: '#4d759e'
  1372. }
  1373. },
  1374. plotOptions: {
  1375. line: { // base series options
  1376. allowPointSelect: false,
  1377. showCheckbox: false,
  1378. animation: {
  1379. duration: 1000
  1380. },
  1381. //connectNulls: false,
  1382. //cursor: 'default',
  1383. //clip: true,
  1384. //dashStyle: null,
  1385. //enableMouseTracking: true,
  1386. events: {},
  1387. //legendIndex: 0,
  1388. lineWidth: 2,
  1389. //shadow: false,
  1390. // stacking: null,
  1391. marker: {
  1392. enabled: true,
  1393. //symbol: null,
  1394. lineWidth: 0,
  1395. radius: 4,
  1396. lineColor: '#FFFFFF',
  1397. //fillColor: null,
  1398. states: { // states for a single point
  1399. hover: {
  1400. enabled: true
  1401. //radius: base + 2
  1402. },
  1403. select: {
  1404. fillColor: '#FFFFFF',
  1405. lineColor: '#000000',
  1406. lineWidth: 2
  1407. }
  1408. }
  1409. },
  1410. point: {
  1411. events: {}
  1412. },
  1413. dataLabels: merge(defaultLabelOptions, {
  1414. enabled: false,
  1415. formatter: function () {
  1416. return numberFormat(this.y, -1);
  1417. },
  1418. verticalAlign: 'bottom', // above singular point
  1419. y: 0
  1420. // backgroundColor: undefined,
  1421. // borderColor: undefined,
  1422. // borderRadius: undefined,
  1423. // borderWidth: undefined,
  1424. // padding: 3,
  1425. // shadow: false
  1426. }),
  1427. cropThreshold: 300, // draw points outside the plot area when the number of points is less than this
  1428. pointRange: 0,
  1429. //pointStart: 0,
  1430. //pointInterval: 1,
  1431. showInLegend: true,
  1432. states: { // states for the entire series
  1433. hover: {
  1434. //enabled: false,
  1435. //lineWidth: base + 1,
  1436. marker: {
  1437. // lineWidth: base + 1,
  1438. // radius: base + 1
  1439. }
  1440. },
  1441. select: {
  1442. marker: {}
  1443. }
  1444. },
  1445. stickyTracking: true
  1446. //tooltip: {
  1447. //pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b>'
  1448. //valueDecimals: null,
  1449. //xDateFormat: '%A, %b %e, %Y',
  1450. //valuePrefix: '',
  1451. //ySuffix: ''
  1452. //}
  1453. // turboThreshold: 1000
  1454. // zIndex: null
  1455. }
  1456. },
  1457. labels: {
  1458. //items: [],
  1459. style: {
  1460. //font: defaultFont,
  1461. position: ABSOLUTE,
  1462. color: '#3E576F'
  1463. }
  1464. },
  1465. legend: {
  1466. enabled: true,
  1467. align: 'center',
  1468. //floating: false,
  1469. layout: 'horizontal',
  1470. labelFormatter: function () {
  1471. return this.name;
  1472. },
  1473. borderWidth: 1,
  1474. borderColor: '#909090',
  1475. borderRadius: 5,
  1476. navigation: {
  1477. // animation: true,
  1478. activeColor: '#274b6d',
  1479. // arrowSize: 12
  1480. inactiveColor: '#CCC'
  1481. // style: {} // text styles
  1482. },
  1483. // margin: 10,
  1484. // reversed: false,
  1485. shadow: false,
  1486. // backgroundColor: null,
  1487. /*style: {
  1488. padding: '5px'
  1489. },*/
  1490. itemStyle: {
  1491. cursor: 'pointer',
  1492. color: '#274b6d',
  1493. fontSize: '12px'
  1494. },
  1495. itemHoverStyle: {
  1496. //cursor: 'pointer', removed as of #601
  1497. color: '#000'
  1498. },
  1499. itemHiddenStyle: {
  1500. color: '#CCC'
  1501. },
  1502. itemCheckboxStyle: {
  1503. position: ABSOLUTE,
  1504. width: '13px', // for IE precision
  1505. height: '13px'
  1506. },
  1507. // itemWidth: undefined,
  1508. symbolWidth: 16,
  1509. symbolPadding: 5,
  1510. verticalAlign: 'bottom',
  1511. // width: undefined,
  1512. x: 0,
  1513. y: 0,
  1514. title: {
  1515. //text: null,
  1516. style: {
  1517. fontWeight: 'bold'
  1518. }
  1519. }
  1520. },
  1521. loading: {
  1522. // hideDuration: 100,
  1523. labelStyle: {
  1524. fontWeight: 'bold',
  1525. position: RELATIVE,
  1526. top: '1em'
  1527. },
  1528. // showDuration: 0,
  1529. style: {
  1530. position: ABSOLUTE,
  1531. backgroundColor: 'white',
  1532. opacity: 0.5,
  1533. textAlign: 'center'
  1534. }
  1535. },
  1536. tooltip: {
  1537. enabled: true,
  1538. animation: hasSVG,
  1539. //crosshairs: null,
  1540. backgroundColor: 'rgba(255, 255, 255, .85)',
  1541. borderWidth: 1,
  1542. borderRadius: 3,
  1543. dateTimeLabelFormats: {
  1544. millisecond: '%A, %b %e, %H:%M:%S.%L',
  1545. second: '%A, %b %e, %H:%M:%S',
  1546. minute: '%A, %b %e, %H:%M',
  1547. hour: '%A, %b %e, %H:%M',
  1548. day: '%A, %b %e, %Y',
  1549. week: 'Week from %A, %b %e, %Y',
  1550. month: '%B %Y',
  1551. year: '%Y'
  1552. },
  1553. //formatter: defaultFormatter,
  1554. headerFormat: '<span style="font-size: 10px">{point.key}</span><br/>',
  1555. pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b><br/>',
  1556. shadow: true,
  1557. //shared: false,
  1558. snap: isTouchDevice ? 25 : 10,
  1559. style: {
  1560. color: '#333333',
  1561. cursor: 'default',
  1562. fontSize: '12px',
  1563. padding: '8px',
  1564. whiteSpace: 'nowrap'
  1565. }
  1566. //xDateFormat: '%A, %b %e, %Y',
  1567. //valueDecimals: null,
  1568. //valuePrefix: '',
  1569. //valueSuffix: ''
  1570. },
  1571. credits: {
  1572. enabled: true,
  1573. text: '',
  1574. // href: 'http://www.highcharts.com',
  1575. position: {
  1576. align: 'right',
  1577. x: -10,
  1578. verticalAlign: 'bottom',
  1579. y: -5
  1580. },
  1581. style: {
  1582. cursor: 'pointer',
  1583. color: '#909090',
  1584. fontSize: '9px'
  1585. }
  1586. }
  1587. };
  1588. // Series defaults
  1589. var defaultPlotOptions = defaultOptions.plotOptions,
  1590. defaultSeriesOptions = defaultPlotOptions.line;
  1591. // set the default time methods
  1592. setTimeMethods();
  1593. /**
  1594. * Set the time methods globally based on the useUTC option. Time method can be either
  1595. * local time or UTC (default).
  1596. */
  1597. function setTimeMethods() {
  1598. var useUTC = defaultOptions.global.useUTC,
  1599. GET = useUTC ? 'getUTC' : 'get',
  1600. SET = useUTC ? 'setUTC' : 'set';
  1601. makeTime = useUTC ? Date.UTC : function (year, month, date, hours, minutes, seconds) {
  1602. return new Date(
  1603. year,
  1604. month,
  1605. pick(date, 1),
  1606. pick(hours, 0),
  1607. pick(minutes, 0),
  1608. pick(seconds, 0)
  1609. ).getTime();
  1610. };
  1611. getMinutes = GET + 'Minutes';
  1612. getHours = GET + 'Hours';
  1613. getDay = GET + 'Day';
  1614. getDate = GET + 'Date';
  1615. getMonth = GET + 'Month';
  1616. getFullYear = GET + 'FullYear';
  1617. setMinutes = SET + 'Minutes';
  1618. setHours = SET + 'Hours';
  1619. setDate = SET + 'Date';
  1620. setMonth = SET + 'Month';
  1621. setFullYear = SET + 'FullYear';
  1622. }
  1623. /**
  1624. * Merge the default options with custom options and return the new options structure
  1625. * @param {Object} options The new custom options
  1626. */
  1627. function setOptions(options) {
  1628. // Pull out axis options and apply them to the respective default axis options
  1629. /*defaultXAxisOptions = merge(defaultXAxisOptions, options.xAxis);
  1630. defaultYAxisOptions = merge(defaultYAxisOptions, options.yAxis);
  1631. options.xAxis = options.yAxis = UNDEFINED;*/
  1632. // Merge in the default options
  1633. defaultOptions = merge(defaultOptions, options);
  1634. // Apply UTC
  1635. setTimeMethods();
  1636. return defaultOptions;
  1637. }
  1638. /**
  1639. * Get the updated default options. Merely exposing defaultOptions for outside modules
  1640. * isn't enough because the setOptions method creates a new object.
  1641. */
  1642. function getOptions() {
  1643. return defaultOptions;
  1644. }
  1645. /**
  1646. * Handle color operations. The object methods are chainable.
  1647. * @param {String} input The input color in either rbga or hex format
  1648. */
  1649. var Color = function (input) {
  1650. // declare variables
  1651. var rgba = [], result, stops;
  1652. /**
  1653. * Parse the input color to rgba array
  1654. * @param {String} input
  1655. */
  1656. function init(input) {
  1657. // Gradients
  1658. if (input && input.stops) {
  1659. stops = map(input.stops, function (stop) {
  1660. return Color(stop[1]);
  1661. });
  1662. // Solid colors
  1663. } else {
  1664. // rgba
  1665. 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);
  1666. if (result) {
  1667. rgba = [pInt(result[1]), pInt(result[2]), pInt(result[3]), parseFloat(result[4], 10)];
  1668. } else {
  1669. // hex
  1670. result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(input);
  1671. if (result) {
  1672. rgba = [pInt(result[1], 16), pInt(result[2], 16), pInt(result[3], 16), 1];
  1673. } else {
  1674. // rgb
  1675. result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(input);
  1676. if (result) {
  1677. rgba = [pInt(result[1]), pInt(result[2]), pInt(result[3]), 1];
  1678. }
  1679. }
  1680. }
  1681. }
  1682. }
  1683. /**
  1684. * Return the color a specified format
  1685. * @param {String} format
  1686. */
  1687. function get(format) {
  1688. var ret;
  1689. if (stops) {
  1690. ret = merge(input);
  1691. ret.stops = [].concat(ret.stops);
  1692. each(stops, function (stop, i) {
  1693. ret.stops[i] = [ret.stops[i][0], stop.get(format)];
  1694. });
  1695. // it's NaN if gradient colors on a column chart
  1696. } else if (rgba && !isNaN(rgba[0])) {
  1697. if (format === 'rgb') {
  1698. ret = 'rgb(' + rgba[0] + ',' + rgba[1] + ',' + rgba[2] + ')';
  1699. } else if (format === 'a') {
  1700. ret = rgba[3];
  1701. } else {
  1702. ret = 'rgba(' + rgba.join(',') + ')';
  1703. }
  1704. } else {
  1705. ret = input;
  1706. }
  1707. return ret;
  1708. }
  1709. /**
  1710. * Brighten the color
  1711. * @param {Number} alpha
  1712. */
  1713. function brighten(alpha) {
  1714. if (stops) {
  1715. each(stops, function (stop) {
  1716. stop.brighten(alpha);
  1717. });
  1718. } else if (isNumber(alpha) && alpha !== 0) {
  1719. var i;
  1720. for (i = 0; i < 3; i++) {
  1721. rgba[i] += pInt(alpha * 255);
  1722. if (rgba[i] < 0) {
  1723. rgba[i] = 0;
  1724. }
  1725. if (rgba[i] > 255) {
  1726. rgba[i] = 255;
  1727. }
  1728. }
  1729. }
  1730. return this;
  1731. }
  1732. /**
  1733. * Set the color's opacity to a given alpha value
  1734. * @param {Number} alpha
  1735. */
  1736. function setOpacity(alpha) {
  1737. rgba[3] = alpha;
  1738. return this;
  1739. }
  1740. // initialize: parse the input
  1741. init(input);
  1742. // public methods
  1743. return {
  1744. get: get,
  1745. brighten: brighten,
  1746. rgba: rgba,
  1747. setOpacity: setOpacity
  1748. };
  1749. };
  1750. /**
  1751. * A wrapper object for SVG elements
  1752. */
  1753. function SVGElement() {}
  1754. SVGElement.prototype = {
  1755. /**
  1756. * Initialize the SVG renderer
  1757. * @param {Object} renderer
  1758. * @param {String} nodeName
  1759. */
  1760. init: function (renderer, nodeName) {
  1761. var wrapper = this;
  1762. wrapper.element = nodeName === 'span' ?
  1763. createElement(nodeName) :
  1764. doc.createElementNS(SVG_NS, nodeName);
  1765. wrapper.renderer = renderer;
  1766. /**
  1767. * A collection of attribute setters. These methods, if defined, are called right before a certain
  1768. * attribute is set on an element wrapper. Returning false prevents the default attribute
  1769. * setter to run. Returning a value causes the default setter to set that value. Used in
  1770. * Renderer.label.
  1771. */
  1772. wrapper.attrSetters = {};
  1773. },
  1774. /**
  1775. * Default base for animation
  1776. */
  1777. opacity: 1,
  1778. /**
  1779. * Animate a given attribute
  1780. * @param {Object} params
  1781. * @param {Number} options The same options as in jQuery animation
  1782. * @param {Function} complete Function to perform at the end of animation
  1783. */
  1784. animate: function (params, options, complete) {
  1785. var animOptions = pick(options, globalAnimation, true);
  1786. stop(this); // stop regardless of animation actually running, or reverting to .attr (#607)
  1787. if (animOptions) {
  1788. animOptions = merge(animOptions);
  1789. if (complete) { // allows using a callback with the global animation without overwriting it
  1790. animOptions.complete = complete;
  1791. }
  1792. animate(this, params, animOptions);
  1793. } else {
  1794. this.attr(params);
  1795. if (complete) {
  1796. complete();
  1797. }
  1798. }
  1799. },
  1800. /**
  1801. * Set or get a given attribute
  1802. * @param {Object|String} hash
  1803. * @param {Mixed|Undefined} val
  1804. */
  1805. attr: function (hash, val) {
  1806. var wrapper = this,
  1807. key,
  1808. value,
  1809. result,
  1810. i,
  1811. child,
  1812. element = wrapper.element,
  1813. nodeName = element.nodeName.toLowerCase(), // Android2 requires lower for "text"
  1814. renderer = wrapper.renderer,
  1815. skipAttr,
  1816. titleNode,
  1817. attrSetters = wrapper.attrSetters,
  1818. shadows = wrapper.shadows,
  1819. hasSetSymbolSize,
  1820. doTransform,
  1821. ret = wrapper;
  1822. // single key-value pair
  1823. if (isString(hash) && defined(val)) {
  1824. key = hash;
  1825. hash = {};
  1826. hash[key] = val;
  1827. }
  1828. // used as a getter: first argument is a string, second is undefined
  1829. if (isString(hash)) {
  1830. key = hash;
  1831. if (nodeName === 'circle') {
  1832. key = { x: 'cx', y: 'cy' }[key] || key;
  1833. } else if (key === 'strokeWidth') {
  1834. key = 'stroke-width';
  1835. }
  1836. ret = attr(element, key) || wrapper[key] || 0;
  1837. if (key !== 'd' && key !== 'visibility') { // 'd' is string in animation step
  1838. ret = parseFloat(ret);
  1839. }
  1840. // setter
  1841. } else {
  1842. for (key in hash) {
  1843. skipAttr = false; // reset
  1844. value = hash[key];
  1845. // check for a specific attribute setter
  1846. result = attrSetters[key] && attrSetters[key].call(wrapper, value, key);
  1847. if (result !== false) {
  1848. if (result !== UNDEFINED) {
  1849. value = result; // the attribute setter has returned a new value to set
  1850. }
  1851. // paths
  1852. if (key === 'd') {
  1853. if (value && value.join) { // join path
  1854. value = value.join(' ');
  1855. }
  1856. if (/(NaN| {2}|^$)/.test(value)) {
  1857. value = 'M 0 0';
  1858. }
  1859. //wrapper.d = value; // shortcut for animations
  1860. // update child tspans x values
  1861. } else if (key === 'x' && nodeName === 'text') {
  1862. for (i = 0; i < element.childNodes.length; i++) {
  1863. child = element.childNodes[i];
  1864. // if the x values are equal, the tspan represents a linebreak
  1865. if (attr(child, 'x') === attr(element, 'x')) {
  1866. //child.setAttribute('x', value);
  1867. attr(child, 'x', value);
  1868. }
  1869. }
  1870. } else if (wrapper.rotation && (key === 'x' || key === 'y')) {
  1871. doTransform = true;
  1872. // apply gradients
  1873. } else if (key === 'fill') {
  1874. value = renderer.color(value, element, key);
  1875. // circle x and y
  1876. } else if (nodeName === 'circle' && (key === 'x' || key === 'y')) {
  1877. key = { x: 'cx', y: 'cy' }[key] || key;
  1878. // rectangle border radius
  1879. } else if (nodeName === 'rect' && key === 'r') {
  1880. attr(element, {
  1881. rx: value,
  1882. ry: value
  1883. });
  1884. skipAttr = true;
  1885. // translation and text rotation
  1886. } else if (key === 'translateX' || key === 'translateY' || key === 'rotation' ||
  1887. key === 'verticalAlign' || key === 'scaleX' || key === 'scaleY') {
  1888. doTransform = true;
  1889. skipAttr = true;
  1890. // apply opacity as subnode (required by legacy WebKit and Batik)
  1891. } else if (key === 'stroke') {
  1892. value = renderer.color(value, element, key);
  1893. // emulate VML's dashstyle implementation
  1894. } else if (key === 'dashstyle') {
  1895. key = 'stroke-dasharray';
  1896. value = value && value.toLowerCase();
  1897. if (value === 'solid') {
  1898. value = NONE;
  1899. } else if (value) {
  1900. value = value
  1901. .replace('shortdashdotdot', '3,1,1,1,1,1,')
  1902. .replace('shortdashdot', '3,1,1,1')
  1903. .replace('shortdot', '1,1,')
  1904. .replace('shortdash', '3,1,')
  1905. .replace('longdash', '8,3,')
  1906. .replace(/dot/g, '1,3,')
  1907. .replace('dash', '4,3,')
  1908. .replace(/,$/, '')
  1909. .split(','); // ending comma
  1910. i = value.length;
  1911. while (i--) {
  1912. value[i] = pInt(value[i]) * hash['stroke-width'];
  1913. }
  1914. value = value.join(',');
  1915. }
  1916. // IE9/MooTools combo: MooTools returns objects instead of numbers and IE9 Beta 2
  1917. // is unable to cast them. Test again with final IE9.
  1918. } else if (key === 'width') {
  1919. value = pInt(value);
  1920. // Text alignment
  1921. } else if (key === 'align') {
  1922. key = 'text-anchor';
  1923. value = { left: 'start', center: 'middle', right: 'end' }[value];
  1924. // Title requires a subnode, #431
  1925. } else if (key === 'title') {
  1926. titleNode = element.getElementsByTagName('title')[0];
  1927. if (!titleNode) {
  1928. titleNode = doc.createElementNS(SVG_NS, 'title');
  1929. element.appendChild(titleNode);
  1930. }
  1931. titleNode.textContent = value;
  1932. }
  1933. // jQuery animate changes case
  1934. if (key === 'strokeWidth') {
  1935. key = 'stroke-width';
  1936. }
  1937. // In Chrome/Win < 6 as well as Batik, the stroke attribute can't be set when the stroke-
  1938. // width is 0. #1369
  1939. if (key === 'stroke-width' || key === 'stroke') {
  1940. wrapper[key] = value;
  1941. // Only apply the stroke attribute if the stroke width is defined and larger than 0
  1942. if (wrapper.stroke && wrapper['stroke-width']) {
  1943. attr(element, 'stroke', wrapper.stroke);
  1944. attr(element, 'stroke-width', wrapper['stroke-width']);
  1945. wrapper.hasStroke = true;
  1946. } else if (key === 'stroke-width' && value === 0 && wrapper.hasStroke) {
  1947. element.removeAttribute('stroke');
  1948. wrapper.hasStroke = false;
  1949. }
  1950. skipAttr = true;
  1951. }
  1952. // symbols
  1953. if (wrapper.symbolName && /^(x|y|width|height|r|start|end|innerR|anchorX|anchorY)/.test(key)) {
  1954. if (!hasSetSymbolSize) {
  1955. wrapper.symbolAttr(hash);
  1956. hasSetSymbolSize = true;
  1957. }
  1958. skipAttr = true;
  1959. }
  1960. // let the shadow follow the main element
  1961. if (shadows && /^(width|height|visibility|x|y|d|transform)$/.test(key)) {
  1962. i = shadows.length;
  1963. while (i--) {
  1964. attr(
  1965. shadows[i],
  1966. key,
  1967. key === 'height' ?
  1968. mathMax(value - (shadows[i].cutHeight || 0), 0) :
  1969. value
  1970. );
  1971. }
  1972. }
  1973. // validate heights
  1974. if ((key === 'width' || key === 'height') && nodeName === 'rect' && value < 0) {
  1975. value = 0;
  1976. }
  1977. // Record for animation and quick access without polling the DOM
  1978. wrapper[key] = value;
  1979. if (key === 'text') {
  1980. // Delete bBox memo when the text changes
  1981. if (value !== wrapper.textStr) {
  1982. delete wrapper.bBox;
  1983. }
  1984. wrapper.textStr = value;
  1985. if (wrapper.added) {
  1986. renderer.buildText(wrapper);
  1987. }
  1988. } else if (!skipAttr) {
  1989. attr(element, key, value);
  1990. }
  1991. }
  1992. }
  1993. // Update transform. Do this outside the loop to prevent redundant updating for batch setting
  1994. // of attributes.
  1995. if (doTransform) {
  1996. wrapper.updateTransform();
  1997. }
  1998. }
  1999. return ret;
  2000. },
  2001. /**
  2002. * Add a class name to an element
  2003. */
  2004. addClass: function (className) {
  2005. attr(this.element, 'class', attr(this.element, 'class') + ' ' + className);
  2006. return this;
  2007. },
  2008. /* hasClass and removeClass are not (yet) needed
  2009. hasClass: function (className) {
  2010. return attr(this.element, 'class').indexOf(className) !== -1;
  2011. },
  2012. removeClass: function (className) {
  2013. attr(this.element, 'class', attr(this.element, 'class').replace(className, ''));
  2014. return this;
  2015. },
  2016. */
  2017. /**
  2018. * If one of the symbol size affecting parameters are changed,
  2019. * check all the others only once for each call to an element's
  2020. * .attr() method
  2021. * @param {Object} hash
  2022. */
  2023. symbolAttr: function (hash) {
  2024. var wrapper = this;
  2025. each(['x', 'y', 'r', 'start', 'end', 'width', 'height', 'innerR', 'anchorX', 'anchorY'], function (key) {
  2026. wrapper[key] = pick(hash[key], wrapper[key]);
  2027. });
  2028. wrapper.attr({
  2029. d: wrapper.renderer.symbols[wrapper.symbolName](
  2030. wrapper.x,
  2031. wrapper.y,
  2032. wrapper.width,
  2033. wrapper.height,
  2034. wrapper
  2035. )
  2036. });
  2037. },
  2038. /**
  2039. * Apply a clipping path to this object
  2040. * @param {String} id
  2041. */
  2042. clip: function (clipRect) {
  2043. return this.attr('clip-path', clipRect ? 'url(' + this.renderer.url + '#' + clipRect.id + ')' : NONE);
  2044. },
  2045. /**
  2046. * Calculate the coordinates needed for drawing a rectangle crisply and return the
  2047. * calculated attributes
  2048. * @param {Number} strokeWidth
  2049. * @param {Number} x
  2050. * @param {Number} y
  2051. * @param {Number} width
  2052. * @param {Number} height
  2053. */
  2054. crisp: function (strokeWidth, x, y, width, height) {
  2055. var wrapper = this,
  2056. key,
  2057. attribs = {},
  2058. values = {},
  2059. normalizer;
  2060. strokeWidth = strokeWidth || wrapper.strokeWidth || (wrapper.attr && wrapper.attr('stroke-width')) || 0;
  2061. normalizer = mathRound(strokeWidth) % 2 / 2; // mathRound because strokeWidth can sometimes have roundoff errors
  2062. // normalize for crisp edges
  2063. values.x = mathFloor(x || wrapper.x || 0) + normalizer;
  2064. values.y = mathFloor(y || wrapper.y || 0) + normalizer;
  2065. values.width = mathFloor((width || wrapper.width || 0) - 2 * normalizer);
  2066. values.height = mathFloor((height || wrapper.height || 0) - 2 * normalizer);
  2067. values.strokeWidth = strokeWidth;
  2068. for (key in values) {
  2069. if (wrapper[key] !== values[key]) { // only set attribute if changed
  2070. wrapper[key] = attribs[key] = values[key];
  2071. }
  2072. }
  2073. return attribs;
  2074. },
  2075. /**
  2076. * Set styles for the element
  2077. * @param {Object} styles
  2078. */
  2079. css: function (styles) {
  2080. /*jslint unparam: true*//* allow unused param a in the regexp function below */
  2081. var elemWrapper = this,
  2082. elem = elemWrapper.element,
  2083. textWidth = styles && styles.width && elem.nodeName.toLowerCase() === 'text',
  2084. n,
  2085. serializedCss = '',
  2086. hyphenate = function (a, b) { return '-' + b.toLowerCase(); };
  2087. /*jslint unparam: false*/
  2088. // convert legacy
  2089. if (styles && styles.color) {
  2090. styles.fill = styles.color;
  2091. }
  2092. // Merge the new styles with the old ones
  2093. styles = extend(
  2094. elemWrapper.styles,
  2095. styles
  2096. );
  2097. // store object
  2098. elemWrapper.styles = styles;
  2099. // Don't handle line wrap on canvas
  2100. if (useCanVG && textWidth) {
  2101. delete styles.width;
  2102. }
  2103. // serialize and set style attribute
  2104. if (isIE && !hasSVG) { // legacy IE doesn't support setting style attribute
  2105. if (textWidth) {
  2106. delete styles.width;
  2107. }
  2108. css(elemWrapper.element, styles);
  2109. } else {
  2110. for (n in styles) {
  2111. serializedCss += n.replace(/([A-Z])/g, hyphenate) + ':' + styles[n] + ';';
  2112. }
  2113. attr(elem, 'style', serializedCss); // #1881
  2114. }
  2115. // re-build text
  2116. if (textWidth && elemWrapper.added) {
  2117. elemWrapper.renderer.buildText(elemWrapper);
  2118. }
  2119. return elemWrapper;
  2120. },
  2121. /**
  2122. * Add an event listener
  2123. * @param {String} eventType
  2124. * @param {Function} handler
  2125. */
  2126. on: function (eventType, handler) {
  2127. // touch
  2128. if (hasTouch && eventType === 'click') {
  2129. this.element.ontouchstart = function (e) {
  2130. e.preventDefault();
  2131. handler();
  2132. };
  2133. }
  2134. // simplest possible event model for internal use
  2135. this.element['on' + eventType] = handler;
  2136. return this;
  2137. },
  2138. /**
  2139. * Set the coordinates needed to draw a consistent radial gradient across
  2140. * pie slices regardless of positioning inside the chart. The format is
  2141. * [centerX, centerY, diameter] in pixels.
  2142. */
  2143. setRadialReference: function (coordinates) {
  2144. this.element.radialReference = coordinates;
  2145. return this;
  2146. },
  2147. /**
  2148. * Move an object and its children by x and y values
  2149. * @param {Number} x
  2150. * @param {Number} y
  2151. */
  2152. translate: function (x, y) {
  2153. return this.attr({
  2154. translateX: x,
  2155. translateY: y
  2156. });
  2157. },
  2158. /**
  2159. * Invert a group, rotate and flip
  2160. */
  2161. invert: function () {
  2162. var wrapper = this;
  2163. wrapper.inverted = true;
  2164. wrapper.updateTransform();
  2165. return wrapper;
  2166. },
  2167. /**
  2168. * Apply CSS to HTML elements. This is used in text within SVG rendering and
  2169. * by the VML renderer
  2170. */
  2171. htmlCss: function (styles) {
  2172. var wrapper = this,
  2173. element = wrapper.element,
  2174. textWidth = styles && element.tagName === 'SPAN' && styles.width;
  2175. if (textWidth) {
  2176. delete styles.width;
  2177. wrapper.textWidth = textWidth;
  2178. wrapper.updateTransform();
  2179. }
  2180. wrapper.styles = extend(wrapper.styles, styles);
  2181. css(wrapper.element, styles);
  2182. return wrapper;
  2183. },
  2184. /**
  2185. * VML and useHTML method for calculating the bounding box based on offsets
  2186. * @param {Boolean} refresh Whether to force a fresh value from the DOM or to
  2187. * use the cached value
  2188. *
  2189. * @return {Object} A hash containing values for x, y, width and height
  2190. */
  2191. htmlGetBBox: function () {
  2192. var wrapper = this,
  2193. element = wrapper.element,
  2194. bBox = wrapper.bBox;
  2195. // faking getBBox in exported SVG in legacy IE
  2196. if (!bBox) {
  2197. // faking getBBox in exported SVG in legacy IE (is this a duplicate of the fix for #1079?)
  2198. if (element.nodeName === 'text') {
  2199. element.style.position = ABSOLUTE;
  2200. }
  2201. bBox = wrapper.bBox = {
  2202. x: element.offsetLeft,
  2203. y: element.offsetTop,
  2204. width: element.offsetWidth,
  2205. height: element.offsetHeight
  2206. };
  2207. }
  2208. return bBox;
  2209. },
  2210. /**
  2211. * VML override private method to update elements based on internal
  2212. * properties based on SVG transform
  2213. */
  2214. htmlUpdateTransform: function () {
  2215. // aligning non added elements is expensive
  2216. if (!this.added) {
  2217. this.alignOnAdd = true;
  2218. return;
  2219. }
  2220. var wrapper = this,
  2221. renderer = wrapper.renderer,
  2222. elem = wrapper.element,
  2223. translateX = wrapper.translateX || 0,
  2224. translateY = wrapper.translateY || 0,
  2225. x = wrapper.x || 0,
  2226. y = wrapper.y || 0,
  2227. align = wrapper.textAlign || 'left',
  2228. alignCorrection = { left: 0, center: 0.5, right: 1 }[align],
  2229. nonLeft = align && align !== 'left',
  2230. shadows = wrapper.shadows;
  2231. // apply translate
  2232. css(elem, {
  2233. marginLeft: translateX,
  2234. marginTop: translateY
  2235. });
  2236. if (shadows) { // used in labels/tooltip
  2237. each(shadows, function (shadow) {
  2238. css(shadow, {
  2239. marginLeft: translateX + 1,
  2240. marginTop: translateY + 1
  2241. });
  2242. });
  2243. }
  2244. // apply inversion
  2245. if (wrapper.inverted) { // wrapper is a group
  2246. each(elem.childNodes, function (child) {
  2247. renderer.invertChild(child, elem);
  2248. });
  2249. }
  2250. if (elem.tagName === 'SPAN') {
  2251. var width, height,
  2252. rotation = wrapper.rotation,
  2253. baseline,
  2254. radians = 0,
  2255. costheta = 1,
  2256. sintheta = 0,
  2257. quad,
  2258. textWidth = pInt(wrapper.textWidth),
  2259. xCorr = wrapper.xCorr || 0,
  2260. yCorr = wrapper.yCorr || 0,
  2261. currentTextTransform = [rotation, align, elem.innerHTML, wrapper.textWidth].join(','),
  2262. rotationStyle = {},
  2263. cssTransformKey;
  2264. if (currentTextTransform !== wrapper.cTT) { // do the calculations and DOM access only if properties changed
  2265. if (defined(rotation)) {
  2266. if (renderer.isSVG) { // #916
  2267. cssTransformKey = isIE ? '-ms-transform' : isWebKit ? '-webkit-transform' : isFirefox ? 'MozTransform' : isOpera ? '-o-transform' : '';
  2268. rotationStyle[cssTransformKey] = rotationStyle.transform = 'rotate(' + rotation + 'deg)';
  2269. } else {
  2270. radians = rotation * deg2rad; // deg to rad
  2271. costheta = mathCos(radians);
  2272. sintheta = mathSin(radians);
  2273. // Adjust for alignment and rotation. Rotation of useHTML content is not yet implemented
  2274. // but it can probably be implemented for Firefox 3.5+ on user request. FF3.5+
  2275. // has support for CSS3 transform. The getBBox method also needs to be updated
  2276. // to compensate for the rotation, like it currently does for SVG.
  2277. // Test case: http://highcharts.com/tests/?file=text-rotation
  2278. rotationStyle.filter = rotation ? ['progid:DXImageTransform.Microsoft.Matrix(M11=', costheta,
  2279. ', M12=', -sintheta, ', M21=', sintheta, ', M22=', costheta,
  2280. ', sizingMethod=\'auto expand\')'].join('') : NONE;
  2281. }
  2282. css(elem, rotationStyle);
  2283. }
  2284. width = pick(wrapper.elemWidth, elem.offsetWidth);
  2285. height = pick(wrapper.elemHeight, elem.offsetHeight);
  2286. // update textWidth
  2287. if (width > textWidth && /[ \-]/.test(elem.textContent || elem.innerText)) { // #983, #1254
  2288. css(elem, {
  2289. width: textWidth + PX,
  2290. display: 'block',
  2291. whiteSpace: 'normal'
  2292. });
  2293. width = textWidth;
  2294. }
  2295. // correct x and y
  2296. baseline = renderer.fontMetrics(elem.style.fontSize).b;
  2297. xCorr = costheta < 0 && -width;
  2298. yCorr = sintheta < 0 && -height;
  2299. // correct for baseline and corners spilling out after rotation
  2300. quad = costheta * sintheta < 0;
  2301. xCorr += sintheta * baseline * (quad ? 1 - alignCorrection : alignCorrection);
  2302. yCorr -= costheta * baseline * (rotation ? (quad ? alignCorrection : 1 - alignCorrection) : 1);
  2303. // correct for the length/height of the text
  2304. if (nonLeft) {
  2305. xCorr -= width * alignCorrection * (costheta < 0 ? -1 : 1);
  2306. if (rotation) {
  2307. yCorr -= height * alignCorrection * (sintheta < 0 ? -1 : 1);
  2308. }
  2309. css(elem, {
  2310. textAlign: align
  2311. });
  2312. }
  2313. // record correction
  2314. wrapper.xCorr = xCorr;
  2315. wrapper.yCorr = yCorr;
  2316. }
  2317. // apply position with correction
  2318. css(elem, {
  2319. left: (x + xCorr) + PX,
  2320. top: (y + yCorr) + PX
  2321. });
  2322. // force reflow in webkit to apply the left and top on useHTML element (#1249)
  2323. if (isWebKit) {
  2324. height = elem.offsetHeight; // assigned to height for JSLint purpose
  2325. }
  2326. // record current text transform
  2327. wrapper.cTT = currentTextTransform;
  2328. }
  2329. },
  2330. /**
  2331. * Private method to update the transform attribute based on internal
  2332. * properties
  2333. */
  2334. updateTransform: function () {
  2335. var wrapper = this,
  2336. translateX = wrapper.translateX || 0,
  2337. translateY = wrapper.translateY || 0,
  2338. scaleX = wrapper.scaleX,
  2339. scaleY = wrapper.scaleY,
  2340. inverted = wrapper.inverted,
  2341. rotation = wrapper.rotation,
  2342. transform;
  2343. // flipping affects translate as adjustment for flipping around the group's axis
  2344. if (inverted) {
  2345. translateX += wrapper.attr('width');
  2346. translateY += wrapper.attr('height');
  2347. }
  2348. // Apply translate. Nearly all transformed elements have translation, so instead
  2349. // of checking for translate = 0, do it always (#1767, #1846).
  2350. transform = ['translate(' + translateX + ',' + translateY + ')'];
  2351. // apply rotation
  2352. if (inverted) {
  2353. transform.push('rotate(90) scale(-1,1)');
  2354. } else if (rotation) { // text rotation
  2355. transform.push('rotate(' + rotation + ' ' + (wrapper.x || 0) + ' ' + (wrapper.y || 0) + ')');
  2356. }
  2357. // apply scale
  2358. if (defined(scaleX) || defined(scaleY)) {
  2359. transform.push('scale(' + pick(scaleX, 1) + ' ' + pick(scaleY, 1) + ')');
  2360. }
  2361. if (transform.length) {
  2362. attr(wrapper.element, 'transform', transform.join(' '));
  2363. }
  2364. },
  2365. /**
  2366. * Bring the element to the front
  2367. */
  2368. toFront: function () {
  2369. var element = this.element;
  2370. element.parentNode.appendChild(element);
  2371. return this;
  2372. },
  2373. /**
  2374. * Break down alignment options like align, verticalAlign, x and y
  2375. * to x and y relative to the chart.
  2376. *
  2377. * @param {Object} alignOptions
  2378. * @param {Boolean} alignByTranslate
  2379. * @param {String[Object} box The box to align to, needs a width and height. When the
  2380. * box is a string, it refers to an object in the Renderer. For example, when
  2381. * box is 'spacingBox', it refers to Renderer.spacingBox which holds width, height
  2382. * x and y properties.
  2383. *
  2384. */
  2385. align: function (alignOptions, alignByTranslate, box) {
  2386. var align,
  2387. vAlign,
  2388. x,
  2389. y,
  2390. attribs = {},
  2391. alignTo,
  2392. renderer = this.renderer,
  2393. alignedObjects = renderer.alignedObjects;
  2394. // First call on instanciate
  2395. if (alignOptions) {
  2396. this.alignOptions = alignOptions;
  2397. this.alignByTranslate = alignByTranslate;
  2398. if (!box || isString(box)) { // boxes other than renderer handle this internally
  2399. this.alignTo = alignTo = box || 'renderer';
  2400. erase(alignedObjects, this); // prevent duplicates, like legendGroup after resize
  2401. alignedObjects.push(this);
  2402. box = null; // reassign it below
  2403. }
  2404. // When called on resize, no arguments are supplied
  2405. } else {
  2406. alignOptions = this.alignOptions;
  2407. alignByTranslate = this.alignByTranslate;
  2408. alignTo = this.alignTo;
  2409. }
  2410. box = pick(box, renderer[alignTo], renderer);
  2411. // Assign variables
  2412. align = alignOptions.align;
  2413. vAlign = alignOptions.verticalAlign;
  2414. x = (box.x || 0) + (alignOptions.x || 0); // default: left align
  2415. y = (box.y || 0) + (alignOptions.y || 0); // default: top align
  2416. // Align
  2417. if (align === 'right' || align === 'center') {
  2418. x += (box.width - (alignOptions.width || 0)) /
  2419. { right: 1, center: 2 }[align];
  2420. }
  2421. attribs[alignByTranslate ? 'translateX' : 'x'] = mathRound(x);
  2422. // Vertical align
  2423. if (vAlign === 'bottom' || vAlign === 'middle') {
  2424. y += (box.height - (alignOptions.height || 0)) /
  2425. ({ bottom: 1, middle: 2 }[vAlign] || 1);
  2426. }
  2427. attribs[alignByTranslate ? 'translateY' : 'y'] = mathRound(y);
  2428. // Animate only if already placed
  2429. this[this.placed ? 'animate' : 'attr'](attribs);
  2430. this.placed = true;
  2431. this.alignAttr = attribs;
  2432. return this;
  2433. },
  2434. /**
  2435. * Get the bounding box (width, height, x and y) for the element
  2436. */
  2437. getBBox: function () {
  2438. var wrapper = this,
  2439. bBox = wrapper.bBox,
  2440. renderer = wrapper.renderer,
  2441. width,
  2442. height,
  2443. rotation = wrapper.rotation,
  2444. element = wrapper.element,
  2445. styles = wrapper.styles,
  2446. rad = rotation * deg2rad;
  2447. if (!bBox) {
  2448. // SVG elements
  2449. if (element.namespaceURI === SVG_NS || renderer.forExport) {
  2450. try { // Fails in Firefox if the container has display: none.
  2451. bBox = element.getBBox ?
  2452. // SVG: use extend because IE9 is not allowed to change width and height in case
  2453. // of rotation (below)
  2454. extend({}, element.getBBox()) :
  2455. // Canvas renderer and legacy IE in export mode
  2456. {
  2457. width: element.offsetWidth,
  2458. height: element.offsetHeight
  2459. };
  2460. } catch (e) {}
  2461. // If the bBox is not set, the try-catch block above failed. The other condition
  2462. // is for Opera that returns a width of -Infinity on hidden elements.
  2463. if (!bBox || bBox.width < 0) {
  2464. bBox = { width: 0, height: 0 };
  2465. }
  2466. // VML Renderer or useHTML within SVG
  2467. } else {
  2468. bBox = wrapper.htmlGetBBox();
  2469. }
  2470. // True SVG elements as well as HTML elements in modern browsers using the .useHTML option
  2471. // need to compensated for rotation
  2472. if (renderer.isSVG) {
  2473. width = bBox.width;
  2474. height = bBox.height;
  2475. // Workaround for wrong bounding box in IE9 and IE10 (#1101, #1505, #1669)
  2476. if (isIE && styles && styles.fontSize === '11px' && height.toPrecision(3) === '22.7') {
  2477. bBox.height = height = 14;
  2478. }
  2479. // Adjust for rotated text
  2480. if (rotation) {
  2481. bBox.width = mathAbs(height * mathSin(rad)) + mathAbs(width * mathCos(rad));
  2482. bBox.height = mathAbs(height * mathCos(rad)) + mathAbs(width * mathSin(rad));
  2483. }
  2484. }
  2485. wrapper.bBox = bBox;
  2486. }
  2487. return bBox;
  2488. },
  2489. /**
  2490. * Show the element
  2491. */
  2492. show: function () {
  2493. return this.attr({ visibility: VISIBLE });
  2494. },
  2495. /**
  2496. * Hide the element
  2497. */
  2498. hide: function () {
  2499. return this.attr({ visibility: HIDDEN });
  2500. },
  2501. fadeOut: function (duration) {
  2502. var elemWrapper = this;
  2503. elemWrapper.animate({
  2504. opacity: 0
  2505. }, {
  2506. duration: duration || 150,
  2507. complete: function () {
  2508. elemWrapper.hide();
  2509. }
  2510. });
  2511. },
  2512. /**
  2513. * Add the element
  2514. * @param {Object|Undefined} parent Can be an element, an element wrapper or undefined
  2515. * to append the element to the renderer.box.
  2516. */
  2517. add: function (parent) {
  2518. var renderer = this.renderer,
  2519. parentWrapper = parent || renderer,
  2520. parentNode = parentWrapper.element || renderer.box,
  2521. childNodes = parentNode.childNodes,
  2522. element = this.element,
  2523. zIndex = attr(element, 'zIndex'),
  2524. otherElement,
  2525. otherZIndex,
  2526. i,
  2527. inserted;
  2528. if (parent) {
  2529. this.parentGroup = parent;
  2530. }
  2531. // mark as inverted
  2532. this.parentInverted = parent && parent.inverted;
  2533. // build formatted text
  2534. if (this.textStr !== undefined) {
  2535. renderer.buildText(this);
  2536. }
  2537. // mark the container as having z indexed children
  2538. if (zIndex) {
  2539. parentWrapper.handleZ = true;
  2540. zIndex = pInt(zIndex);
  2541. }
  2542. // insert according to this and other elements' zIndex
  2543. if (parentWrapper.handleZ) { // this element or any of its siblings has a z index
  2544. for (i = 0; i < childNodes.length; i++) {
  2545. otherElement = childNodes[i];
  2546. otherZIndex = attr(otherElement, 'zIndex');
  2547. if (otherElement !== element && (
  2548. // insert before the first element with a higher zIndex
  2549. pInt(otherZIndex) > zIndex ||
  2550. // if no zIndex given, insert before the first element with a zIndex
  2551. (!defined(zIndex) && defined(otherZIndex))
  2552. )) {
  2553. parentNode.insertBefore(element, otherElement);
  2554. inserted = true;
  2555. break;
  2556. }
  2557. }
  2558. }
  2559. // default: append at the end
  2560. if (!inserted) {
  2561. parentNode.appendChild(element);
  2562. }
  2563. // mark as added
  2564. this.added = true;
  2565. // fire an event for internal hooks
  2566. fireEvent(this, 'add');
  2567. return this;
  2568. },
  2569. /**
  2570. * Removes a child either by removeChild or move to garbageBin.
  2571. * Issue 490; in VML removeChild results in Orphaned nodes according to sIEve, discardElement does not.
  2572. */
  2573. safeRemoveChild: function (element) {
  2574. var parentNode = element.parentNode;
  2575. if (parentNode) {
  2576. parentNode.removeChild(element);
  2577. }
  2578. },
  2579. /**
  2580. * Destroy the element and element wrapper
  2581. */
  2582. destroy: function () {
  2583. var wrapper = this,
  2584. element = wrapper.element || {},
  2585. shadows = wrapper.shadows,
  2586. key,
  2587. i;
  2588. // remove events
  2589. element.onclick = element.onmouseout = element.onmouseover = element.onmousemove = element.point = null;
  2590. stop(wrapper); // stop running animations
  2591. if (wrapper.clipPath) {
  2592. wrapper.clipPath = wrapper.clipPath.destroy();
  2593. }
  2594. // Destroy stops in case this is a gradient object
  2595. if (wrapper.stops) {
  2596. for (i = 0; i < wrapper.stops.length; i++) {
  2597. wrapper.stops[i] = wrapper.stops[i].destroy();
  2598. }
  2599. wrapper.stops = null;
  2600. }
  2601. // remove element
  2602. wrapper.safeRemoveChild(element);
  2603. // destroy shadows
  2604. if (shadows) {
  2605. each(shadows, function (shadow) {
  2606. wrapper.safeRemoveChild(shadow);
  2607. });
  2608. }
  2609. // remove from alignObjects
  2610. if (wrapper.alignTo) {
  2611. erase(wrapper.renderer.alignedObjects, wrapper);
  2612. }
  2613. for (key in wrapper) {
  2614. delete wrapper[key];
  2615. }
  2616. return null;
  2617. },
  2618. /**
  2619. * Add a shadow to the element. Must be done after the element is added to the DOM
  2620. * @param {Boolean|Object} shadowOptions
  2621. */
  2622. shadow: function (shadowOptions, group, cutOff) {
  2623. var shadows = [],
  2624. i,
  2625. shadow,
  2626. element = this.element,
  2627. strokeWidth,
  2628. shadowWidth,
  2629. shadowElementOpacity,
  2630. // compensate for inverted plot area
  2631. transform;
  2632. if (shadowOptions) {
  2633. shadowWidth = pick(shadowOptions.width, 3);
  2634. shadowElementOpacity = (shadowOptions.opacity || 0.15) / shadowWidth;
  2635. transform = this.parentInverted ?
  2636. '(-1,-1)' :
  2637. '(' + pick(shadowOptions.offsetX, 1) + ', ' + pick(shadowOptions.offsetY, 1) + ')';
  2638. for (i = 1; i <= shadowWidth; i++) {
  2639. shadow = element.cloneNode(0);
  2640. strokeWidth = (shadowWidth * 2) + 1 - (2 * i);
  2641. attr(shadow, {
  2642. 'isShadow': 'true',
  2643. 'stroke': shadowOptions.color || 'black',
  2644. 'stroke-opacity': shadowElementOpacity * i,
  2645. 'stroke-width': strokeWidth,
  2646. 'transform': 'translate' + transform,
  2647. 'fill': NONE
  2648. });
  2649. if (cutOff) {
  2650. attr(shadow, 'height', mathMax(attr(shadow, 'height') - strokeWidth, 0));
  2651. shadow.cutHeight = strokeWidth;
  2652. }
  2653. if (group) {
  2654. group.element.appendChild(shadow);
  2655. } else {
  2656. element.parentNode.insertBefore(shadow, element);
  2657. }
  2658. shadows.push(shadow);
  2659. }
  2660. this.shadows = shadows;
  2661. }
  2662. return this;
  2663. }
  2664. };
  2665. /**
  2666. * The default SVG renderer
  2667. */
  2668. var SVGRenderer = function () {
  2669. this.init.apply(this, arguments);
  2670. };
  2671. SVGRenderer.prototype = {
  2672. Element: SVGElement,
  2673. /**
  2674. * Initialize the SVGRenderer
  2675. * @param {Object} container
  2676. * @param {Number} width
  2677. * @param {Number} height
  2678. * @param {Boolean} forExport
  2679. */
  2680. init: function (container, width, height, forExport) {
  2681. var renderer = this,
  2682. loc = location,
  2683. boxWrapper,
  2684. desc;
  2685. boxWrapper = renderer.createElement('svg')
  2686. .attr({
  2687. xmlns: SVG_NS,
  2688. version: '1.1'
  2689. });
  2690. container.appendChild(boxWrapper.element);
  2691. // object properties
  2692. renderer.isSVG = true;
  2693. renderer.box = boxWrapper.element;
  2694. renderer.boxWrapper = boxWrapper;
  2695. renderer.alignedObjects = [];
  2696. // Page url used for internal references. #24, #672, #1070
  2697. renderer.url = (isFirefox || isWebKit) && doc.getElementsByTagName('base').length ?
  2698. loc.href
  2699. .replace(/#.*?$/, '') // remove the hash
  2700. .replace(/([\('\)])/g, '\\$1') // escape parantheses and quotes
  2701. .replace(/ /g, '%20') : // replace spaces (needed for Safari only)
  2702. '';
  2703. // Add description
  2704. desc = this.createElement('desc').add();
  2705. desc.element.appendChild(doc.createTextNode('Created with ' + PRODUCT + ' ' + VERSION));
  2706. renderer.defs = this.createElement('defs').add();
  2707. renderer.forExport = forExport;
  2708. renderer.gradients = {}; // Object where gradient SvgElements are stored
  2709. renderer.setSize(width, height, false);
  2710. // Issue 110 workaround:
  2711. // In Firefox, if a div is positioned by percentage, its pixel position may land
  2712. // between pixels. The container itself doesn't display this, but an SVG element
  2713. // inside this container will be drawn at subpixel precision. In order to draw
  2714. // sharp lines, this must be compensated for. This doesn't seem to work inside
  2715. // iframes though (like in jsFiddle).
  2716. var subPixelFix, rect;
  2717. if (isFirefox && container.getBoundingClientRect) {
  2718. renderer.subPixelFix = subPixelFix = function () {
  2719. css(container, { left: 0, top: 0 });
  2720. rect = container.getBoundingClientRect();
  2721. css(container, {
  2722. left: (mathCeil(rect.left) - rect.left) + PX,
  2723. top: (mathCeil(rect.top) - rect.top) + PX
  2724. });
  2725. };
  2726. // run the fix now
  2727. subPixelFix();
  2728. // run it on resize
  2729. addEvent(win, 'resize', subPixelFix);
  2730. }
  2731. },
  2732. /**
  2733. * Detect whether the renderer is hidden. This happens when one of the parent elements
  2734. * has display: none. #608.
  2735. */
  2736. isHidden: function () {
  2737. return !this.boxWrapper.getBBox().width;
  2738. },
  2739. /**
  2740. * Destroys the renderer and its allocated members.
  2741. */
  2742. destroy: function () {
  2743. var renderer = this,
  2744. rendererDefs = renderer.defs;
  2745. renderer.box = null;
  2746. renderer.boxWrapper = renderer.boxWrapper.destroy();
  2747. // Call destroy on all gradient elements
  2748. destroyObjectProperties(renderer.gradients || {});
  2749. renderer.gradients = null;
  2750. // Defs are null in VMLRenderer
  2751. // Otherwise, destroy them here.
  2752. if (rendererDefs) {
  2753. renderer.defs = rendererDefs.destroy();
  2754. }
  2755. // Remove sub pixel fix handler
  2756. // We need to check that there is a handler, otherwise all functions that are registered for event 'resize' are removed
  2757. // See issue #982
  2758. if (renderer.subPixelFix) {
  2759. removeEvent(win, 'resize', renderer.subPixelFix);
  2760. }
  2761. renderer.alignedObjects = null;
  2762. return null;
  2763. },
  2764. /**
  2765. * Create a wrapper for an SVG element
  2766. * @param {Object} nodeName
  2767. */
  2768. createElement: function (nodeName) {
  2769. var wrapper = new this.Element();
  2770. wrapper.init(this, nodeName);
  2771. return wrapper;
  2772. },
  2773. /**
  2774. * Dummy function for use in canvas renderer
  2775. */
  2776. draw: function () {},
  2777. /**
  2778. * Parse a simple HTML string into SVG tspans
  2779. *
  2780. * @param {Object} textNode The parent text SVG node
  2781. */
  2782. buildText: function (wrapper) {
  2783. var textNode = wrapper.element,
  2784. renderer = this,
  2785. forExport = renderer.forExport,
  2786. lines = pick(wrapper.textStr, '').toString()
  2787. .replace(/<(b|strong)>/g, '<span style="font-weight:bold">')
  2788. .replace(/<(i|em)>/g, '<span style="font-style:italic">')
  2789. .replace(/<a/g, '<span')
  2790. .replace(/<\/(b|strong|i|em|a)>/g, '</span>')
  2791. .split(/<br.*?>/g),
  2792. childNodes = textNode.childNodes,
  2793. styleRegex = /style="([^"]+)"/,
  2794. hrefRegex = /href="([^"]+)"/,
  2795. parentX = attr(textNode, 'x'),
  2796. textStyles = wrapper.styles,
  2797. width = textStyles && textStyles.width && pInt(textStyles.width),
  2798. textLineHeight = textStyles && textStyles.lineHeight,
  2799. i = childNodes.length;
  2800. /// remove old text
  2801. while (i--) {
  2802. textNode.removeChild(childNodes[i]);
  2803. }
  2804. if (width && !wrapper.added) {
  2805. this.box.appendChild(textNode); // attach it to the DOM to read offset width
  2806. }
  2807. // remove empty line at end
  2808. if (lines[lines.length - 1] === '') {
  2809. lines.pop();
  2810. }
  2811. // build the lines
  2812. each(lines, function (line, lineNo) {
  2813. var spans, spanNo = 0;
  2814. line = line.replace(/<span/g, '|||<span').replace(/<\/span>/g, '</span>|||');
  2815. spans = line.split('|||');
  2816. each(spans, function (span) {
  2817. if (span !== '' || spans.length === 1) {
  2818. var attributes = {},
  2819. tspan = doc.createElementNS(SVG_NS, 'tspan'),
  2820. spanStyle; // #390
  2821. if (styleRegex.test(span)) {
  2822. spanStyle = span.match(styleRegex)[1].replace(/(;| |^)color([ :])/, '$1fill$2');
  2823. attr(tspan, 'style', spanStyle);
  2824. }
  2825. if (hrefRegex.test(span) && !forExport) { // Not for export - #1529
  2826. attr(tspan, 'onclick', 'location.href=\"' + span.match(hrefRegex)[1] + '\"');
  2827. css(tspan, { cursor: 'pointer' });
  2828. }
  2829. span = (span.replace(/<(.|\n)*?>/g, '') || ' ')
  2830. .replace(/&lt;/g, '<')
  2831. .replace(/&gt;/g, '>');
  2832. // add the text node
  2833. tspan.appendChild(doc.createTextNode(span));
  2834. if (!spanNo) { // first span in a line, align it to the left
  2835. attributes.x = parentX;
  2836. } else {
  2837. attributes.dx = 0; // #16
  2838. }
  2839. // add attributes
  2840. attr(tspan, attributes);
  2841. // first span on subsequent line, add the line height
  2842. if (!spanNo && lineNo) {
  2843. // allow getting the right offset height in exporting in IE
  2844. if (!hasSVG && forExport) {
  2845. css(tspan, { display: 'block' });
  2846. }
  2847. // Set the line height based on the font size of either
  2848. // the text element or the tspan element
  2849. attr(
  2850. tspan,
  2851. 'dy',
  2852. textLineHeight || renderer.fontMetrics(
  2853. /px$/.test(tspan.style.fontSize) ?
  2854. tspan.style.fontSize :
  2855. textStyles.fontSize
  2856. ).h,
  2857. // Safari 6.0.2 - too optimized for its own good (#1539)
  2858. // TODO: revisit this with future versions of Safari
  2859. isWebKit && tspan.offsetHeight
  2860. );
  2861. }
  2862. // Append it
  2863. textNode.appendChild(tspan);
  2864. spanNo++;
  2865. // check width and apply soft breaks
  2866. if (width) {
  2867. var words = span.replace(/([^\^])-/g, '$1- ').split(' '), // #1273
  2868. tooLong,
  2869. actualWidth,
  2870. rest = [];
  2871. while (words.length || rest.length) {
  2872. delete wrapper.bBox; // delete cache
  2873. actualWidth = wrapper.getBBox().width;
  2874. tooLong = actualWidth > width;
  2875. if (!tooLong || words.length === 1) { // new line needed
  2876. words = rest;
  2877. rest = [];
  2878. if (words.length) {
  2879. tspan = doc.createElementNS(SVG_NS, 'tspan');
  2880. attr(tspan, {
  2881. dy: textLineHeight || 16,
  2882. x: parentX
  2883. });
  2884. if (spanStyle) { // #390
  2885. attr(tspan, 'style', spanStyle);
  2886. }
  2887. textNode.appendChild(tspan);
  2888. if (actualWidth > width) { // a single word is pressing it out
  2889. width = actualWidth;
  2890. }
  2891. }
  2892. } else { // append to existing line tspan
  2893. tspan.removeChild(tspan.firstChild);
  2894. rest.unshift(words.pop());
  2895. }
  2896. if (words.length) {
  2897. tspan.appendChild(doc.createTextNode(words.join(' ').replace(/- /g, '-')));
  2898. }
  2899. }
  2900. }
  2901. }
  2902. });
  2903. });
  2904. },
  2905. /**
  2906. * Create a button with preset states
  2907. * @param {String} text
  2908. * @param {Number} x
  2909. * @param {Number} y
  2910. * @param {Function} callback
  2911. * @param {Object} normalState
  2912. * @param {Object} hoverState
  2913. * @param {Object} pressedState
  2914. */
  2915. button: function (text, x, y, callback, normalState, hoverState, pressedState) {
  2916. var label = this.label(text, x, y, null, null, null, null, null, 'button'),
  2917. curState = 0,
  2918. stateOptions,
  2919. stateStyle,
  2920. normalStyle,
  2921. hoverStyle,
  2922. pressedStyle,
  2923. STYLE = 'style',
  2924. verticalGradient = { x1: 0, y1: 0, x2: 0, y2: 1 };
  2925. // Normal state - prepare the attributes
  2926. normalState = merge({
  2927. 'stroke-width': 1,
  2928. stroke: '#CCCCCC',
  2929. fill: {
  2930. linearGradient: verticalGradient,
  2931. stops: [
  2932. [0, '#FEFEFE'],
  2933. [1, '#F6F6F6']
  2934. ]
  2935. },
  2936. r: 2,
  2937. padding: 5,
  2938. style: {
  2939. color: 'black'
  2940. }
  2941. }, normalState);
  2942. normalStyle = normalState[STYLE];
  2943. delete normalState[STYLE];
  2944. // Hover state
  2945. hoverState = merge(normalState, {
  2946. stroke: '#68A',
  2947. fill: {
  2948. linearGradient: verticalGradient,
  2949. stops: [
  2950. [0, '#FFF'],
  2951. [1, '#ACF']
  2952. ]
  2953. }
  2954. }, hoverState);
  2955. hoverStyle = hoverState[STYLE];
  2956. delete hoverState[STYLE];
  2957. // Pressed state
  2958. pressedState = merge(normalState, {
  2959. stroke: '#68A',
  2960. fill: {
  2961. linearGradient: verticalGradient,
  2962. stops: [
  2963. [0, '#9BD'],
  2964. [1, '#CDF']
  2965. ]
  2966. }
  2967. }, pressedState);
  2968. pressedStyle = pressedState[STYLE];
  2969. delete pressedState[STYLE];
  2970. // add the events
  2971. addEvent(label.element, 'mouseenter', function () {
  2972. label.attr(hoverState)
  2973. .css(hoverStyle);
  2974. });
  2975. addEvent(label.element, 'mouseleave', function () {
  2976. stateOptions = [normalState, hoverState, pressedState][curState];
  2977. stateStyle = [normalStyle, hoverStyle, pressedStyle][curState];
  2978. label.attr(stateOptions)
  2979. .css(stateStyle);
  2980. });
  2981. label.setState = function (state) {
  2982. curState = state;
  2983. if (!state) {
  2984. label.attr(normalState)
  2985. .css(normalStyle);
  2986. } else if (state === 2) {
  2987. label.attr(pressedState)
  2988. .css(pressedStyle);
  2989. }
  2990. };
  2991. return label
  2992. .on('click', function () {
  2993. callback.call(label);
  2994. })
  2995. .attr(normalState)
  2996. .css(extend({ cursor: 'default' }, normalStyle));
  2997. },
  2998. /**
  2999. * Make a straight line crisper by not spilling out to neighbour pixels
  3000. * @param {Array} points
  3001. * @param {Number} width
  3002. */
  3003. crispLine: function (points, width) {
  3004. // points format: [M, 0, 0, L, 100, 0]
  3005. // normalize to a crisp line
  3006. if (points[1] === points[4]) {
  3007. // Substract due to #1129. Now bottom and left axis gridlines behave the same.
  3008. points[1] = points[4] = mathRound(points[1]) - (width % 2 / 2);
  3009. }
  3010. if (points[2] === points[5]) {
  3011. points[2] = points[5] = mathRound(points[2]) + (width % 2 / 2);
  3012. }
  3013. return points;
  3014. },
  3015. /**
  3016. * Draw a path
  3017. * @param {Array} path An SVG path in array form
  3018. */
  3019. path: function (path) {
  3020. var attr = {
  3021. fill: NONE
  3022. };
  3023. if (isArray(path)) {
  3024. attr.d = path;
  3025. } else if (isObject(path)) { // attributes
  3026. extend(attr, path);
  3027. }
  3028. return this.createElement('path').attr(attr);
  3029. },
  3030. /**
  3031. * Draw and return an SVG circle
  3032. * @param {Number} x The x position
  3033. * @param {Number} y The y position
  3034. * @param {Number} r The radius
  3035. */
  3036. circle: function (x, y, r) {
  3037. var attr = isObject(x) ?
  3038. x :
  3039. {
  3040. x: x,
  3041. y: y,
  3042. r: r
  3043. };
  3044. return this.createElement('circle').attr(attr);
  3045. },
  3046. /**
  3047. * Draw and return an arc
  3048. * @param {Number} x X position
  3049. * @param {Number} y Y position
  3050. * @param {Number} r Radius
  3051. * @param {Number} innerR Inner radius like used in donut charts
  3052. * @param {Number} start Starting angle
  3053. * @param {Number} end Ending angle
  3054. */
  3055. arc: function (x, y, r, innerR, start, end) {
  3056. // arcs are defined as symbols for the ability to set
  3057. // attributes in attr and animate
  3058. if (isObject(x)) {
  3059. y = x.y;
  3060. r = x.r;
  3061. innerR = x.innerR;
  3062. start = x.start;
  3063. end = x.end;
  3064. x = x.x;
  3065. }
  3066. return this.symbol('arc', x || 0, y || 0, r || 0, r || 0, {
  3067. innerR: innerR || 0,
  3068. start: start || 0,
  3069. end: end || 0
  3070. });
  3071. },
  3072. /**
  3073. * Draw and return a rectangle
  3074. * @param {Number} x Left position
  3075. * @param {Number} y Top position
  3076. * @param {Number} width
  3077. * @param {Number} height
  3078. * @param {Number} r Border corner radius
  3079. * @param {Number} strokeWidth A stroke width can be supplied to allow crisp drawing
  3080. */
  3081. rect: function (x, y, width, height, r, strokeWidth) {
  3082. r = isObject(x) ? x.r : r;
  3083. var wrapper = this.createElement('rect').attr({
  3084. rx: r,
  3085. ry: r,
  3086. fill: NONE
  3087. });
  3088. return wrapper.attr(
  3089. isObject(x) ?
  3090. x :
  3091. // do not crispify when an object is passed in (as in column charts)
  3092. wrapper.crisp(strokeWidth, x, y, mathMax(width, 0), mathMax(height, 0))
  3093. );
  3094. },
  3095. /**
  3096. * Resize the box and re-align all aligned elements
  3097. * @param {Object} width
  3098. * @param {Object} height
  3099. * @param {Boolean} animate
  3100. *
  3101. */
  3102. setSize: function (width, height, animate) {
  3103. var renderer = this,
  3104. alignedObjects = renderer.alignedObjects,
  3105. i = alignedObjects.length;
  3106. renderer.width = width;
  3107. renderer.height = height;
  3108. renderer.boxWrapper[pick(animate, true) ? 'animate' : 'attr']({
  3109. width: width,
  3110. height: height
  3111. });
  3112. while (i--) {
  3113. alignedObjects[i].align();
  3114. }
  3115. },
  3116. /**
  3117. * Create a group
  3118. * @param {String} name The group will be given a class name of 'highcharts-{name}'.
  3119. * This can be used for styling and scripting.
  3120. */
  3121. g: function (name) {
  3122. var elem = this.createElement('g');
  3123. return defined(name) ? elem.attr({ 'class': PREFIX + name }) : elem;
  3124. },
  3125. /**
  3126. * Display an image
  3127. * @param {String} src
  3128. * @param {Number} x
  3129. * @param {Number} y
  3130. * @param {Number} width
  3131. * @param {Number} height
  3132. */
  3133. image: function (src, x, y, width, height) {
  3134. var attribs = {
  3135. preserveAspectRatio: NONE
  3136. },
  3137. elemWrapper;
  3138. // optional properties
  3139. if (arguments.length > 1) {
  3140. extend(attribs, {
  3141. x: x,
  3142. y: y,
  3143. width: width,
  3144. height: height
  3145. });
  3146. }
  3147. elemWrapper = this.createElement('image').attr(attribs);
  3148. // set the href in the xlink namespace
  3149. if (elemWrapper.element.setAttributeNS) {
  3150. elemWrapper.element.setAttributeNS('http://www.w3.org/1999/xlink',
  3151. 'href', src);
  3152. } else {
  3153. // could be exporting in IE
  3154. // using href throws "not supported" in ie7 and under, requries regex shim to fix later
  3155. elemWrapper.element.setAttribute('hc-svg-href', src);
  3156. }
  3157. return elemWrapper;
  3158. },
  3159. /**
  3160. * Draw a symbol out of pre-defined shape paths from the namespace 'symbol' object.
  3161. *
  3162. * @param {Object} symbol
  3163. * @param {Object} x
  3164. * @param {Object} y
  3165. * @param {Object} radius
  3166. * @param {Object} options
  3167. */
  3168. symbol: function (symbol, x, y, width, height, options) {
  3169. var obj,
  3170. // get the symbol definition function
  3171. symbolFn = this.symbols[symbol],
  3172. // check if there's a path defined for this symbol
  3173. path = symbolFn && symbolFn(
  3174. mathRound(x),
  3175. mathRound(y),
  3176. width,
  3177. height,
  3178. options
  3179. ),
  3180. imageElement,
  3181. imageRegex = /^url\((.*?)\)$/,
  3182. imageSrc,
  3183. imageSize,
  3184. centerImage;
  3185. if (path) {
  3186. obj = this.path(path);
  3187. // expando properties for use in animate and attr
  3188. extend(obj, {
  3189. symbolName: symbol,
  3190. x: x,
  3191. y: y,
  3192. width: width,
  3193. height: height
  3194. });
  3195. if (options) {
  3196. extend(obj, options);
  3197. }
  3198. // image symbols
  3199. } else if (imageRegex.test(symbol)) {
  3200. // On image load, set the size and position
  3201. centerImage = function (img, size) {
  3202. if (img.element) { // it may be destroyed in the meantime (#1390)
  3203. img.attr({
  3204. width: size[0],
  3205. height: size[1]
  3206. });
  3207. if (!img.alignByTranslate) { // #185
  3208. img.translate(
  3209. mathRound((width - size[0]) / 2), // #1378
  3210. mathRound((height - size[1]) / 2)
  3211. );
  3212. }
  3213. }
  3214. };
  3215. imageSrc = symbol.match(imageRegex)[1];
  3216. imageSize = symbolSizes[imageSrc];
  3217. // Ireate the image synchronously, add attribs async
  3218. obj = this.image(imageSrc)
  3219. .attr({
  3220. x: x,
  3221. y: y
  3222. });
  3223. obj.isImg = true;
  3224. if (imageSize) {
  3225. centerImage(obj, imageSize);
  3226. } else {
  3227. // Initialize image to be 0 size so export will still function if there's no cached sizes.
  3228. //
  3229. obj.attr({ width: 0, height: 0 });
  3230. // Create a dummy JavaScript image to get the width and height. Due to a bug in IE < 8,
  3231. // the created element must be assigned to a variable in order to load (#292).
  3232. imageElement = createElement('img', {
  3233. onload: function () {
  3234. centerImage(obj, symbolSizes[imageSrc] = [this.width, this.height]);
  3235. },
  3236. src: imageSrc
  3237. });
  3238. }
  3239. }
  3240. return obj;
  3241. },
  3242. /**
  3243. * An extendable collection of functions for defining symbol paths.
  3244. */
  3245. symbols: {
  3246. 'circle': function (x, y, w, h) {
  3247. var cpw = 0.166 * w;
  3248. return [
  3249. M, x + w / 2, y,
  3250. 'C', x + w + cpw, y, x + w + cpw, y + h, x + w / 2, y + h,
  3251. 'C', x - cpw, y + h, x - cpw, y, x + w / 2, y,
  3252. 'Z'
  3253. ];
  3254. },
  3255. 'square': function (x, y, w, h) {
  3256. return [
  3257. M, x, y,
  3258. L, x + w, y,
  3259. x + w, y + h,
  3260. x, y + h,
  3261. 'Z'
  3262. ];
  3263. },
  3264. 'triangle': function (x, y, w, h) {
  3265. return [
  3266. M, x + w / 2, y,
  3267. L, x + w, y + h,
  3268. x, y + h,
  3269. 'Z'
  3270. ];
  3271. },
  3272. 'triangle-down': function (x, y, w, h) {
  3273. return [
  3274. M, x, y,
  3275. L, x + w, y,
  3276. x + w / 2, y + h,
  3277. 'Z'
  3278. ];
  3279. },
  3280. 'diamond': function (x, y, w, h) {
  3281. return [
  3282. M, x + w / 2, y,
  3283. L, x + w, y + h / 2,
  3284. x + w / 2, y + h,
  3285. x, y + h / 2,
  3286. 'Z'
  3287. ];
  3288. },
  3289. 'arc': function (x, y, w, h, options) {
  3290. var start = options.start,
  3291. radius = options.r || w || h,
  3292. end = options.end - 0.001, // to prevent cos and sin of start and end from becoming equal on 360 arcs (related: #1561)
  3293. innerRadius = options.innerR,
  3294. open = options.open,
  3295. cosStart = mathCos(start),
  3296. sinStart = mathSin(start),
  3297. cosEnd = mathCos(end),
  3298. sinEnd = mathSin(end),
  3299. longArc = options.end - start < mathPI ? 0 : 1;
  3300. return [
  3301. M,
  3302. x + radius * cosStart,
  3303. y + radius * sinStart,
  3304. 'A', // arcTo
  3305. radius, // x radius
  3306. radius, // y radius
  3307. 0, // slanting
  3308. longArc, // long or short arc
  3309. 1, // clockwise
  3310. x + radius * cosEnd,
  3311. y + radius * sinEnd,
  3312. open ? M : L,
  3313. x + innerRadius * cosEnd,
  3314. y + innerRadius * sinEnd,
  3315. 'A', // arcTo
  3316. innerRadius, // x radius
  3317. innerRadius, // y radius
  3318. 0, // slanting
  3319. longArc, // long or short arc
  3320. 0, // clockwise
  3321. x + innerRadius * cosStart,
  3322. y + innerRadius * sinStart,
  3323. open ? '' : 'Z' // close
  3324. ];
  3325. }
  3326. },
  3327. /**
  3328. * Define a clipping rectangle
  3329. * @param {String} id
  3330. * @param {Number} x
  3331. * @param {Number} y
  3332. * @param {Number} width
  3333. * @param {Number} height
  3334. */
  3335. clipRect: function (x, y, width, height) {
  3336. var wrapper,
  3337. id = PREFIX + idCounter++,
  3338. clipPath = this.createElement('clipPath').attr({
  3339. id: id
  3340. }).add(this.defs);
  3341. wrapper = this.rect(x, y, width, height, 0).add(clipPath);
  3342. wrapper.id = id;
  3343. wrapper.clipPath = clipPath;
  3344. return wrapper;
  3345. },
  3346. /**
  3347. * Take a color and return it if it's a string, make it a gradient if it's a
  3348. * gradient configuration object. Prior to Highstock, an array was used to define
  3349. * a linear gradient with pixel positions relative to the SVG. In newer versions
  3350. * we change the coordinates to apply relative to the shape, using coordinates
  3351. * 0-1 within the shape. To preserve backwards compatibility, linearGradient
  3352. * in this definition is an object of x1, y1, x2 and y2.
  3353. *
  3354. * @param {Object} color The color or config object
  3355. */
  3356. color: function (color, elem, prop) {
  3357. var renderer = this,
  3358. colorObject,
  3359. regexRgba = /^rgba/,
  3360. gradName,
  3361. gradAttr,
  3362. gradients,
  3363. gradientObject,
  3364. stops,
  3365. stopColor,
  3366. stopOpacity,
  3367. radialReference,
  3368. n,
  3369. id,
  3370. key = [];
  3371. // Apply linear or radial gradients
  3372. if (color && color.linearGradient) {
  3373. gradName = 'linearGradient';
  3374. } else if (color && color.radialGradient) {
  3375. gradName = 'radialGradient';
  3376. }
  3377. if (gradName) {
  3378. gradAttr = color[gradName];
  3379. gradients = renderer.gradients;
  3380. stops = color.stops;
  3381. radialReference = elem.radialReference;
  3382. // Keep < 2.2 kompatibility
  3383. if (isArray(gradAttr)) {
  3384. color[gradName] = gradAttr = {
  3385. x1: gradAttr[0],
  3386. y1: gradAttr[1],
  3387. x2: gradAttr[2],
  3388. y2: gradAttr[3],
  3389. gradientUnits: 'userSpaceOnUse'
  3390. };
  3391. }
  3392. // Correct the radial gradient for the radial reference system
  3393. if (gradName === 'radialGradient' && radialReference && !defined(gradAttr.gradientUnits)) {
  3394. gradAttr = merge(gradAttr, {
  3395. cx: (radialReference[0] - radialReference[2] / 2) + gradAttr.cx * radialReference[2],
  3396. cy: (radialReference[1] - radialReference[2] / 2) + gradAttr.cy * radialReference[2],
  3397. r: gradAttr.r * radialReference[2],
  3398. gradientUnits: 'userSpaceOnUse'
  3399. });
  3400. }
  3401. // Build the unique key to detect whether we need to create a new element (#1282)
  3402. for (n in gradAttr) {
  3403. if (n !== 'id') {
  3404. key.push(n, gradAttr[n]);
  3405. }
  3406. }
  3407. for (n in stops) {
  3408. key.push(stops[n]);
  3409. }
  3410. key = key.join(',');
  3411. // Check if a gradient object with the same config object is created within this renderer
  3412. if (gradients[key]) {
  3413. id = gradients[key].id;
  3414. } else {
  3415. // Set the id and create the element
  3416. gradAttr.id = id = PREFIX + idCounter++;
  3417. gradients[key] = gradientObject = renderer.createElement(gradName)
  3418. .attr(gradAttr)
  3419. .add(renderer.defs);
  3420. // The gradient needs to keep a list of stops to be able to destroy them
  3421. gradientObject.stops = [];
  3422. each(stops, function (stop) {
  3423. var stopObject;
  3424. if (regexRgba.test(stop[1])) {
  3425. colorObject = Color(stop[1]);
  3426. stopColor = colorObject.get('rgb');
  3427. stopOpacity = colorObject.get('a');
  3428. } else {
  3429. stopColor = stop[1];
  3430. stopOpacity = 1;
  3431. }
  3432. stopObject = renderer.createElement('stop').attr({
  3433. offset: stop[0],
  3434. 'stop-color': stopColor,
  3435. 'stop-opacity': stopOpacity
  3436. }).add(gradientObject);
  3437. // Add the stop element to the gradient
  3438. gradientObject.stops.push(stopObject);
  3439. });
  3440. }
  3441. // Return the reference to the gradient object
  3442. return 'url(' + renderer.url + '#' + id + ')';
  3443. // Webkit and Batik can't show rgba.
  3444. } else if (regexRgba.test(color)) {
  3445. colorObject = Color(color);
  3446. attr(elem, prop + '-opacity', colorObject.get('a'));
  3447. return colorObject.get('rgb');
  3448. } else {
  3449. // Remove the opacity attribute added above. Does not throw if the attribute is not there.
  3450. elem.removeAttribute(prop + '-opacity');
  3451. return color;
  3452. }
  3453. },
  3454. /**
  3455. * Add text to the SVG object
  3456. * @param {String} str
  3457. * @param {Number} x Left position
  3458. * @param {Number} y Top position
  3459. * @param {Boolean} useHTML Use HTML to render the text
  3460. */
  3461. text: function (str, x, y, useHTML) {
  3462. // declare variables
  3463. var renderer = this,
  3464. defaultChartStyle = defaultOptions.chart.style,
  3465. fakeSVG = useCanVG || (!hasSVG && renderer.forExport),
  3466. wrapper;
  3467. if (useHTML && !renderer.forExport) {
  3468. return renderer.html(str, x, y);
  3469. }
  3470. x = mathRound(pick(x, 0));
  3471. y = mathRound(pick(y, 0));
  3472. wrapper = renderer.createElement('text')
  3473. .attr({
  3474. x: x,
  3475. y: y,
  3476. text: str
  3477. })
  3478. .css({
  3479. fontFamily: defaultChartStyle.fontFamily,
  3480. fontSize: defaultChartStyle.fontSize
  3481. });
  3482. // Prevent wrapping from creating false offsetWidths in export in legacy IE (#1079, #1063)
  3483. if (fakeSVG) {
  3484. wrapper.css({
  3485. position: ABSOLUTE
  3486. });
  3487. }
  3488. wrapper.x = x;
  3489. wrapper.y = y;
  3490. return wrapper;
  3491. },
  3492. /**
  3493. * Create HTML text node. This is used by the VML renderer as well as the SVG
  3494. * renderer through the useHTML option.
  3495. *
  3496. * @param {String} str
  3497. * @param {Number} x
  3498. * @param {Number} y
  3499. */
  3500. html: function (str, x, y) {
  3501. var defaultChartStyle = defaultOptions.chart.style,
  3502. wrapper = this.createElement('span'),
  3503. attrSetters = wrapper.attrSetters,
  3504. element = wrapper.element,
  3505. renderer = wrapper.renderer;
  3506. // Text setter
  3507. attrSetters.text = function (value) {
  3508. if (value !== element.innerHTML) {
  3509. delete this.bBox;
  3510. }
  3511. element.innerHTML = value;
  3512. return false;
  3513. };
  3514. // Various setters which rely on update transform
  3515. attrSetters.x = attrSetters.y = attrSetters.align = function (value, key) {
  3516. if (key === 'align') {
  3517. key = 'textAlign'; // Do not overwrite the SVGElement.align method. Same as VML.
  3518. }
  3519. wrapper[key] = value;
  3520. wrapper.htmlUpdateTransform();
  3521. return false;
  3522. };
  3523. // Set the default attributes
  3524. wrapper.attr({
  3525. text: str,
  3526. x: mathRound(x),
  3527. y: mathRound(y)
  3528. })
  3529. .css({
  3530. position: ABSOLUTE,
  3531. whiteSpace: 'nowrap',
  3532. fontFamily: defaultChartStyle.fontFamily,
  3533. fontSize: defaultChartStyle.fontSize
  3534. });
  3535. // Use the HTML specific .css method
  3536. wrapper.css = wrapper.htmlCss;
  3537. // This is specific for HTML within SVG
  3538. if (renderer.isSVG) {
  3539. wrapper.add = function (svgGroupWrapper) {
  3540. var htmlGroup,
  3541. container = renderer.box.parentNode,
  3542. parentGroup,
  3543. parents = [];
  3544. // Create a mock group to hold the HTML elements
  3545. if (svgGroupWrapper) {
  3546. htmlGroup = svgGroupWrapper.div;
  3547. if (!htmlGroup) {
  3548. // Read the parent chain into an array and read from top down
  3549. parentGroup = svgGroupWrapper;
  3550. while (parentGroup) {
  3551. parents.push(parentGroup);
  3552. // Move up to the next parent group
  3553. parentGroup = parentGroup.parentGroup;
  3554. }
  3555. // Ensure dynamically updating position when any parent is translated
  3556. each(parents.reverse(), function (parentGroup) {
  3557. var htmlGroupStyle;
  3558. // Create a HTML div and append it to the parent div to emulate
  3559. // the SVG group structure
  3560. htmlGroup = parentGroup.div = parentGroup.div || createElement(DIV, {
  3561. className: attr(parentGroup.element, 'class')
  3562. }, {
  3563. position: ABSOLUTE,
  3564. left: (parentGroup.translateX || 0) + PX,
  3565. top: (parentGroup.translateY || 0) + PX
  3566. }, htmlGroup || container); // the top group is appended to container
  3567. // Shortcut
  3568. htmlGroupStyle = htmlGroup.style;
  3569. // Set listeners to update the HTML div's position whenever the SVG group
  3570. // position is changed
  3571. extend(parentGroup.attrSetters, {
  3572. translateX: function (value) {
  3573. htmlGroupStyle.left = value + PX;
  3574. },
  3575. translateY: function (value) {
  3576. htmlGroupStyle.top = value + PX;
  3577. },
  3578. visibility: function (value, key) {
  3579. htmlGroupStyle[key] = value;
  3580. }
  3581. });
  3582. });
  3583. }
  3584. } else {
  3585. htmlGroup = container;
  3586. }
  3587. htmlGroup.appendChild(element);
  3588. // Shared with VML:
  3589. wrapper.added = true;
  3590. if (wrapper.alignOnAdd) {
  3591. wrapper.htmlUpdateTransform();
  3592. }
  3593. return wrapper;
  3594. };
  3595. }
  3596. return wrapper;
  3597. },
  3598. /**
  3599. * Utility to return the baseline offset and total line height from the font size
  3600. */
  3601. fontMetrics: function (fontSize) {
  3602. fontSize = pInt(fontSize || 11);
  3603. // Empirical values found by comparing font size and bounding box height.
  3604. // Applies to the default font family. http://jsfiddle.net/highcharts/7xvn7/
  3605. var lineHeight = fontSize < 24 ? fontSize + 4 : mathRound(fontSize * 1.2),
  3606. baseline = mathRound(lineHeight * 0.8);
  3607. return {
  3608. h: lineHeight,
  3609. b: baseline
  3610. };
  3611. },
  3612. /**
  3613. * Add a label, a text item that can hold a colored or gradient background
  3614. * as well as a border and shadow.
  3615. * @param {string} str
  3616. * @param {Number} x
  3617. * @param {Number} y
  3618. * @param {String} shape
  3619. * @param {Number} anchorX In case the shape has a pointer, like a flag, this is the
  3620. * coordinates it should be pinned to
  3621. * @param {Number} anchorY
  3622. * @param {Boolean} baseline Whether to position the label relative to the text baseline,
  3623. * like renderer.text, or to the upper border of the rectangle.
  3624. * @param {String} className Class name for the group
  3625. */
  3626. label: function (str, x, y, shape, anchorX, anchorY, useHTML, baseline, className) {
  3627. var renderer = this,
  3628. wrapper = renderer.g(className),
  3629. text = renderer.text('', 0, 0, useHTML)
  3630. .attr({
  3631. zIndex: 1
  3632. }),
  3633. //.add(wrapper),
  3634. box,
  3635. bBox,
  3636. alignFactor = 0,
  3637. padding = 3,
  3638. paddingLeft = 0,
  3639. width,
  3640. height,
  3641. wrapperX,
  3642. wrapperY,
  3643. crispAdjust = 0,
  3644. deferredAttr = {},
  3645. baselineOffset,
  3646. attrSetters = wrapper.attrSetters,
  3647. needsBox;
  3648. /**
  3649. * This function runs after the label is added to the DOM (when the bounding box is
  3650. * available), and after the text of the label is updated to detect the new bounding
  3651. * box and reflect it in the border box.
  3652. */
  3653. function updateBoxSize() {
  3654. var boxX,
  3655. boxY,
  3656. style = text.element.style;
  3657. bBox = (width === undefined || height === undefined || wrapper.styles.textAlign) &&
  3658. text.getBBox();
  3659. wrapper.width = (width || bBox.width || 0) + 2 * padding + paddingLeft;
  3660. wrapper.height = (height || bBox.height || 0) + 2 * padding;
  3661. // update the label-scoped y offset
  3662. baselineOffset = padding + renderer.fontMetrics(style && style.fontSize).b;
  3663. if (needsBox) {
  3664. // create the border box if it is not already present
  3665. if (!box) {
  3666. boxX = mathRound(-alignFactor * padding);
  3667. boxY = baseline ? -baselineOffset : 0;
  3668. wrapper.box = box = shape ?
  3669. renderer.symbol(shape, boxX, boxY, wrapper.width, wrapper.height) :
  3670. renderer.rect(boxX, boxY, wrapper.width, wrapper.height, 0, deferredAttr[STROKE_WIDTH]);
  3671. box.add(wrapper);
  3672. }
  3673. // apply the box attributes
  3674. if (!box.isImg) { // #1630
  3675. box.attr(merge({
  3676. width: wrapper.width,
  3677. height: wrapper.height
  3678. }, deferredAttr));
  3679. }
  3680. deferredAttr = null;
  3681. }
  3682. }
  3683. /**
  3684. * This function runs after setting text or padding, but only if padding is changed
  3685. */
  3686. function updateTextPadding() {
  3687. var styles = wrapper.styles,
  3688. textAlign = styles && styles.textAlign,
  3689. x = paddingLeft + padding * (1 - alignFactor),
  3690. y;
  3691. // determin y based on the baseline
  3692. y = baseline ? 0 : baselineOffset;
  3693. // compensate for alignment
  3694. if (defined(width) && (textAlign === 'center' || textAlign === 'right')) {
  3695. x += { center: 0.5, right: 1 }[textAlign] * (width - bBox.width);
  3696. }
  3697. // update if anything changed
  3698. if (x !== text.x || y !== text.y) {
  3699. text.attr({
  3700. x: x,
  3701. y: y
  3702. });
  3703. }
  3704. // record current values
  3705. text.x = x;
  3706. text.y = y;
  3707. }
  3708. /**
  3709. * Set a box attribute, or defer it if the box is not yet created
  3710. * @param {Object} key
  3711. * @param {Object} value
  3712. */
  3713. function boxAttr(key, value) {
  3714. if (box) {
  3715. box.attr(key, value);
  3716. } else {
  3717. deferredAttr[key] = value;
  3718. }
  3719. }
  3720. function getSizeAfterAdd() {
  3721. text.add(wrapper);
  3722. wrapper.attr({
  3723. text: str, // alignment is available now
  3724. x: x,
  3725. y: y
  3726. });
  3727. if (box && defined(anchorX)) {
  3728. wrapper.attr({
  3729. anchorX: anchorX,
  3730. anchorY: anchorY
  3731. });
  3732. }
  3733. }
  3734. /**
  3735. * After the text element is added, get the desired size of the border box
  3736. * and add it before the text in the DOM.
  3737. */
  3738. addEvent(wrapper, 'add', getSizeAfterAdd);
  3739. /*
  3740. * Add specific attribute setters.
  3741. */
  3742. // only change local variables
  3743. attrSetters.width = function (value) {
  3744. width = value;
  3745. return false;
  3746. };
  3747. attrSetters.height = function (value) {
  3748. height = value;
  3749. return false;
  3750. };
  3751. attrSetters.padding = function (value) {
  3752. if (defined(value) && value !== padding) {
  3753. padding = value;
  3754. updateTextPadding();
  3755. }
  3756. return false;
  3757. };
  3758. attrSetters.paddingLeft = function (value) {
  3759. if (defined(value) && value !== paddingLeft) {
  3760. paddingLeft = value;
  3761. updateTextPadding();
  3762. }
  3763. return false;
  3764. };
  3765. // change local variable and set attribue as well
  3766. attrSetters.align = function (value) {
  3767. alignFactor = { left: 0, center: 0.5, right: 1 }[value];
  3768. return false; // prevent setting text-anchor on the group
  3769. };
  3770. // apply these to the box and the text alike
  3771. attrSetters.text = function (value, key) {
  3772. text.attr(key, value);
  3773. updateBoxSize();
  3774. updateTextPadding();
  3775. return false;
  3776. };
  3777. // apply these to the box but not to the text
  3778. attrSetters[STROKE_WIDTH] = function (value, key) {
  3779. needsBox = true;
  3780. crispAdjust = value % 2 / 2;
  3781. boxAttr(key, value);
  3782. return false;
  3783. };
  3784. attrSetters.stroke = attrSetters.fill = attrSetters.r = function (value, key) {
  3785. if (key === 'fill') {
  3786. needsBox = true;
  3787. }
  3788. boxAttr(key, value);
  3789. return false;
  3790. };
  3791. attrSetters.anchorX = function (value, key) {
  3792. anchorX = value;
  3793. boxAttr(key, value + crispAdjust - wrapperX);
  3794. return false;
  3795. };
  3796. attrSetters.anchorY = function (value, key) {
  3797. anchorY = value;
  3798. boxAttr(key, value - wrapperY);
  3799. return false;
  3800. };
  3801. // rename attributes
  3802. attrSetters.x = function (value) {
  3803. wrapper.x = value; // for animation getter
  3804. value -= alignFactor * ((width || bBox.width) + padding);
  3805. wrapperX = mathRound(value);
  3806. wrapper.attr('translateX', wrapperX);
  3807. return false;
  3808. };
  3809. attrSetters.y = function (value) {
  3810. wrapperY = wrapper.y = mathRound(value);
  3811. wrapper.attr('translateY', wrapperY);
  3812. return false;
  3813. };
  3814. // Redirect certain methods to either the box or the text
  3815. var baseCss = wrapper.css;
  3816. return extend(wrapper, {
  3817. /**
  3818. * Pick up some properties and apply them to the text instead of the wrapper
  3819. */
  3820. css: function (styles) {
  3821. if (styles) {
  3822. var textStyles = {};
  3823. styles = merge(styles); // create a copy to avoid altering the original object (#537)
  3824. each(['fontSize', 'fontWeight', 'fontFamily', 'color', 'lineHeight', 'width', 'textDecoration'], function (prop) {
  3825. if (styles[prop] !== UNDEFINED) {
  3826. textStyles[prop] = styles[prop];
  3827. delete styles[prop];
  3828. }
  3829. });
  3830. text.css(textStyles);
  3831. }
  3832. return baseCss.call(wrapper, styles);
  3833. },
  3834. /**
  3835. * Return the bounding box of the box, not the group
  3836. */
  3837. getBBox: function () {
  3838. return {
  3839. width: bBox.width + 2 * padding,
  3840. height: bBox.height + 2 * padding,
  3841. x: bBox.x - padding,
  3842. y: bBox.y - padding
  3843. };
  3844. },
  3845. /**
  3846. * Apply the shadow to the box
  3847. */
  3848. shadow: function (b) {
  3849. if (box) {
  3850. box.shadow(b);
  3851. }
  3852. return wrapper;
  3853. },
  3854. /**
  3855. * Destroy and release memory.
  3856. */
  3857. destroy: function () {
  3858. removeEvent(wrapper, 'add', getSizeAfterAdd);
  3859. // Added by button implementation
  3860. removeEvent(wrapper.element, 'mouseenter');
  3861. removeEvent(wrapper.element, 'mouseleave');
  3862. if (text) {
  3863. text = text.destroy();
  3864. }
  3865. if (box) {
  3866. box = box.destroy();
  3867. }
  3868. // Call base implementation to destroy the rest
  3869. SVGElement.prototype.destroy.call(wrapper);
  3870. // Release local pointers (#1298)
  3871. wrapper = renderer = updateBoxSize = updateTextPadding = boxAttr = getSizeAfterAdd = null;
  3872. }
  3873. });
  3874. }
  3875. }; // end SVGRenderer
  3876. // general renderer
  3877. Renderer = SVGRenderer;
  3878. /* ****************************************************************************
  3879. * *
  3880. * START OF INTERNET EXPLORER <= 8 SPECIFIC CODE *
  3881. * *
  3882. * For applications and websites that don't need IE support, like platform *
  3883. * targeted mobile apps and web apps, this code can be removed. *
  3884. * *
  3885. *****************************************************************************/
  3886. /**
  3887. * @constructor
  3888. */
  3889. var VMLRenderer, VMLElement;
  3890. if (!hasSVG && !useCanVG) {
  3891. /**
  3892. * The VML element wrapper.
  3893. */
  3894. Highcharts.VMLElement = VMLElement = {
  3895. /**
  3896. * Initialize a new VML element wrapper. It builds the markup as a string
  3897. * to minimize DOM traffic.
  3898. * @param {Object} renderer
  3899. * @param {Object} nodeName
  3900. */
  3901. init: function (renderer, nodeName) {
  3902. var wrapper = this,
  3903. markup = ['<', nodeName, ' filled="f" stroked="f"'],
  3904. style = ['position: ', ABSOLUTE, ';'],
  3905. isDiv = nodeName === DIV;
  3906. // divs and shapes need size
  3907. if (nodeName === 'shape' || isDiv) {
  3908. style.push('left:0;top:0;width:1px;height:1px;');
  3909. }
  3910. style.push('visibility: ', isDiv ? HIDDEN : VISIBLE);
  3911. markup.push(' style="', style.join(''), '"/>');
  3912. // create element with default attributes and style
  3913. if (nodeName) {
  3914. markup = isDiv || nodeName === 'span' || nodeName === 'img' ?
  3915. markup.join('')
  3916. : renderer.prepVML(markup);
  3917. wrapper.element = createElement(markup);
  3918. }
  3919. wrapper.renderer = renderer;
  3920. wrapper.attrSetters = {};
  3921. },
  3922. /**
  3923. * Add the node to the given parent
  3924. * @param {Object} parent
  3925. */
  3926. add: function (parent) {
  3927. var wrapper = this,
  3928. renderer = wrapper.renderer,
  3929. element = wrapper.element,
  3930. box = renderer.box,
  3931. inverted = parent && parent.inverted,
  3932. // get the parent node
  3933. parentNode = parent ?
  3934. parent.element || parent :
  3935. box;
  3936. // if the parent group is inverted, apply inversion on all children
  3937. if (inverted) { // only on groups
  3938. renderer.invertChild(element, parentNode);
  3939. }
  3940. // append it
  3941. parentNode.appendChild(element);
  3942. // align text after adding to be able to read offset
  3943. wrapper.added = true;
  3944. if (wrapper.alignOnAdd && !wrapper.deferUpdateTransform) {
  3945. wrapper.updateTransform();
  3946. }
  3947. // fire an event for internal hooks
  3948. fireEvent(wrapper, 'add');
  3949. return wrapper;
  3950. },
  3951. /**
  3952. * VML always uses htmlUpdateTransform
  3953. */
  3954. updateTransform: SVGElement.prototype.htmlUpdateTransform,
  3955. /**
  3956. * Get or set attributes
  3957. */
  3958. attr: function (hash, val) {
  3959. var wrapper = this,
  3960. key,
  3961. value,
  3962. i,
  3963. result,
  3964. element = wrapper.element || {},
  3965. elemStyle = element.style,
  3966. nodeName = element.nodeName,
  3967. renderer = wrapper.renderer,
  3968. symbolName = wrapper.symbolName,
  3969. hasSetSymbolSize,
  3970. shadows = wrapper.shadows,
  3971. skipAttr,
  3972. attrSetters = wrapper.attrSetters,
  3973. ret = wrapper;
  3974. // single key-value pair
  3975. if (isString(hash) && defined(val)) {
  3976. key = hash;
  3977. hash = {};
  3978. hash[key] = val;
  3979. }
  3980. // used as a getter, val is undefined
  3981. if (isString(hash)) {
  3982. key = hash;
  3983. if (key === 'strokeWidth' || key === 'stroke-width') {
  3984. ret = wrapper.strokeweight;
  3985. } else {
  3986. ret = wrapper[key];
  3987. }
  3988. // setter
  3989. } else {
  3990. for (key in hash) {
  3991. value = hash[key];
  3992. skipAttr = false;
  3993. // check for a specific attribute setter
  3994. result = attrSetters[key] && attrSetters[key].call(wrapper, value, key);
  3995. if (result !== false && value !== null) { // #620
  3996. if (result !== UNDEFINED) {
  3997. value = result; // the attribute setter has returned a new value to set
  3998. }
  3999. // prepare paths
  4000. // symbols
  4001. if (symbolName && /^(x|y|r|start|end|width|height|innerR|anchorX|anchorY)/.test(key)) {
  4002. // if one of the symbol size affecting parameters are changed,
  4003. // check all the others only once for each call to an element's
  4004. // .attr() method
  4005. if (!hasSetSymbolSize) {
  4006. wrapper.symbolAttr(hash);
  4007. hasSetSymbolSize = true;
  4008. }
  4009. skipAttr = true;
  4010. } else if (key === 'd') {
  4011. value = value || [];
  4012. wrapper.d = value.join(' '); // used in getter for animation
  4013. // convert paths
  4014. i = value.length;
  4015. var convertedPath = [],
  4016. clockwise;
  4017. while (i--) {
  4018. // Multiply by 10 to allow subpixel precision.
  4019. // Substracting half a pixel seems to make the coordinates
  4020. // align with SVG, but this hasn't been tested thoroughly
  4021. if (isNumber(value[i])) {
  4022. convertedPath[i] = mathRound(value[i] * 10) - 5;
  4023. } else if (value[i] === 'Z') { // close the path
  4024. convertedPath[i] = 'x';
  4025. } else {
  4026. convertedPath[i] = value[i];
  4027. // When the start X and end X coordinates of an arc are too close,
  4028. // they are rounded to the same value above. In this case, substract 1 from the end X
  4029. // position. #760, #1371.
  4030. if (value.isArc && (value[i] === 'wa' || value[i] === 'at')) {
  4031. clockwise = value[i] === 'wa' ? 1 : -1; // #1642
  4032. if (convertedPath[i + 5] === convertedPath[i + 7]) {
  4033. convertedPath[i + 7] -= clockwise;
  4034. }
  4035. // Start and end Y (#1410)
  4036. if (convertedPath[i + 6] === convertedPath[i + 8]) {
  4037. convertedPath[i + 8] -= clockwise;
  4038. }
  4039. }
  4040. }
  4041. }
  4042. value = convertedPath.join(' ') || 'x';
  4043. element.path = value;
  4044. // update shadows
  4045. if (shadows) {
  4046. i = shadows.length;
  4047. while (i--) {
  4048. shadows[i].path = shadows[i].cutOff ? this.cutOffPath(value, shadows[i].cutOff) : value;
  4049. }
  4050. }
  4051. skipAttr = true;
  4052. // handle visibility
  4053. } else if (key === 'visibility') {
  4054. // let the shadow follow the main element
  4055. if (shadows) {
  4056. i = shadows.length;
  4057. while (i--) {
  4058. shadows[i].style[key] = value;
  4059. }
  4060. }
  4061. // Instead of toggling the visibility CSS property, move the div out of the viewport.
  4062. // This works around #61 and #586
  4063. if (nodeName === 'DIV') {
  4064. value = value === HIDDEN ? '-999em' : 0;
  4065. // In order to redraw, IE7 needs the div to be visible when tucked away
  4066. // outside the viewport. So the visibility is actually opposite of
  4067. // the expected value. This applies to the tooltip only.
  4068. if (!docMode8) {
  4069. elemStyle[key] = value ? VISIBLE : HIDDEN;
  4070. }
  4071. key = 'top';
  4072. }
  4073. elemStyle[key] = value;
  4074. skipAttr = true;
  4075. // directly mapped to css
  4076. } else if (key === 'zIndex') {
  4077. if (value) {
  4078. elemStyle[key] = value;
  4079. }
  4080. skipAttr = true;
  4081. // x, y, width, height
  4082. } else if (inArray(key, ['x', 'y', 'width', 'height']) !== -1) {
  4083. wrapper[key] = value; // used in getter
  4084. if (key === 'x' || key === 'y') {
  4085. key = { x: 'left', y: 'top' }[key];
  4086. } else {
  4087. value = mathMax(0, value); // don't set width or height below zero (#311)
  4088. }
  4089. // clipping rectangle special
  4090. if (wrapper.updateClipping) {
  4091. wrapper[key] = value; // the key is now 'left' or 'top' for 'x' and 'y'
  4092. wrapper.updateClipping();
  4093. } else {
  4094. // normal
  4095. elemStyle[key] = value;
  4096. }
  4097. skipAttr = true;
  4098. // class name
  4099. } else if (key === 'class' && nodeName === 'DIV') {
  4100. // IE8 Standards mode has problems retrieving the className
  4101. element.className = value;
  4102. // stroke
  4103. } else if (key === 'stroke') {
  4104. value = renderer.color(value, element, key);
  4105. key = 'strokecolor';
  4106. // stroke width
  4107. } else if (key === 'stroke-width' || key === 'strokeWidth') {
  4108. element.stroked = value ? true : false;
  4109. key = 'strokeweight';
  4110. wrapper[key] = value; // used in getter, issue #113
  4111. if (isNumber(value)) {
  4112. value += PX;
  4113. }
  4114. // dashStyle
  4115. } else if (key === 'dashstyle') {
  4116. var strokeElem = element.getElementsByTagName('stroke')[0] ||
  4117. createElement(renderer.prepVML(['<stroke/>']), null, null, element);
  4118. strokeElem[key] = value || 'solid';
  4119. wrapper.dashstyle = value; /* because changing stroke-width will change the dash length
  4120. and cause an epileptic effect */
  4121. skipAttr = true;
  4122. // fill
  4123. } else if (key === 'fill') {
  4124. if (nodeName === 'SPAN') { // text color
  4125. elemStyle.color = value;
  4126. } else if (nodeName !== 'IMG') { // #1336
  4127. element.filled = value !== NONE ? true : false;
  4128. value = renderer.color(value, element, key, wrapper);
  4129. key = 'fillcolor';
  4130. }
  4131. // opacity: don't bother - animation is too slow and filters introduce artifacts
  4132. } else if (key === 'opacity') {
  4133. /*css(element, {
  4134. opacity: value
  4135. });*/
  4136. skipAttr = true;
  4137. // rotation on VML elements
  4138. } else if (nodeName === 'shape' && key === 'rotation') {
  4139. wrapper[key] = value;
  4140. // Correction for the 1x1 size of the shape container. Used in gauge needles.
  4141. element.style.left = -mathRound(mathSin(value * deg2rad) + 1) + PX;
  4142. element.style.top = mathRound(mathCos(value * deg2rad)) + PX;
  4143. // translation for animation
  4144. } else if (key === 'translateX' || key === 'translateY' || key === 'rotation') {
  4145. wrapper[key] = value;
  4146. wrapper.updateTransform();
  4147. skipAttr = true;
  4148. // text for rotated and non-rotated elements
  4149. } else if (key === 'text') {
  4150. this.bBox = null;
  4151. element.innerHTML = value;
  4152. skipAttr = true;
  4153. }
  4154. if (!skipAttr) {
  4155. if (docMode8) { // IE8 setAttribute bug
  4156. element[key] = value;
  4157. } else {
  4158. attr(element, key, value);
  4159. }
  4160. }
  4161. }
  4162. }
  4163. }
  4164. return ret;
  4165. },
  4166. /**
  4167. * Set the element's clipping to a predefined rectangle
  4168. *
  4169. * @param {String} id The id of the clip rectangle
  4170. */
  4171. clip: function (clipRect) {
  4172. var wrapper = this,
  4173. clipMembers,
  4174. cssRet;
  4175. if (clipRect) {
  4176. clipMembers = clipRect.members;
  4177. erase(clipMembers, wrapper); // Ensure unique list of elements (#1258)
  4178. clipMembers.push(wrapper);
  4179. wrapper.destroyClip = function () {
  4180. erase(clipMembers, wrapper);
  4181. };
  4182. cssRet = clipRect.getCSS(wrapper);
  4183. } else {
  4184. if (wrapper.destroyClip) {
  4185. wrapper.destroyClip();
  4186. }
  4187. cssRet = { clip: docMode8 ? 'inherit' : 'rect(auto)' }; // #1214
  4188. }
  4189. return wrapper.css(cssRet);
  4190. },
  4191. /**
  4192. * Set styles for the element
  4193. * @param {Object} styles
  4194. */
  4195. css: SVGElement.prototype.htmlCss,
  4196. /**
  4197. * Removes a child either by removeChild or move to garbageBin.
  4198. * Issue 490; in VML removeChild results in Orphaned nodes according to sIEve, discardElement does not.
  4199. */
  4200. safeRemoveChild: function (element) {
  4201. // discardElement will detach the node from its parent before attaching it
  4202. // to the garbage bin. Therefore it is important that the node is attached and have parent.
  4203. if (element.parentNode) {
  4204. discardElement(element);
  4205. }
  4206. },
  4207. /**
  4208. * Extend element.destroy by removing it from the clip members array
  4209. */
  4210. destroy: function () {
  4211. if (this.destroyClip) {
  4212. this.destroyClip();
  4213. }
  4214. return SVGElement.prototype.destroy.apply(this);
  4215. },
  4216. /**
  4217. * Add an event listener. VML override for normalizing event parameters.
  4218. * @param {String} eventType
  4219. * @param {Function} handler
  4220. */
  4221. on: function (eventType, handler) {
  4222. // simplest possible event model for internal use
  4223. this.element['on' + eventType] = function () {
  4224. var evt = win.event;
  4225. evt.target = evt.srcElement;
  4226. handler(evt);
  4227. };
  4228. return this;
  4229. },
  4230. /**
  4231. * In stacked columns, cut off the shadows so that they don't overlap
  4232. */
  4233. cutOffPath: function (path, length) {
  4234. var len;
  4235. path = path.split(/[ ,]/);
  4236. len = path.length;
  4237. if (len === 9 || len === 11) {
  4238. path[len - 4] = path[len - 2] = pInt(path[len - 2]) - 10 * length;
  4239. }
  4240. return path.join(' ');
  4241. },
  4242. /**
  4243. * Apply a drop shadow by copying elements and giving them different strokes
  4244. * @param {Boolean|Object} shadowOptions
  4245. */
  4246. shadow: function (shadowOptions, group, cutOff) {
  4247. var shadows = [],
  4248. i,
  4249. element = this.element,
  4250. renderer = this.renderer,
  4251. shadow,
  4252. elemStyle = element.style,
  4253. markup,
  4254. path = element.path,
  4255. strokeWidth,
  4256. modifiedPath,
  4257. shadowWidth,
  4258. shadowElementOpacity;
  4259. // some times empty paths are not strings
  4260. if (path && typeof path.value !== 'string') {
  4261. path = 'x';
  4262. }
  4263. modifiedPath = path;
  4264. if (shadowOptions) {
  4265. shadowWidth = pick(shadowOptions.width, 3);
  4266. shadowElementOpacity = (shadowOptions.opacity || 0.15) / shadowWidth;
  4267. for (i = 1; i <= 3; i++) {
  4268. strokeWidth = (shadowWidth * 2) + 1 - (2 * i);
  4269. // Cut off shadows for stacked column items
  4270. if (cutOff) {
  4271. modifiedPath = this.cutOffPath(path.value, strokeWidth + 0.5);
  4272. }
  4273. markup = ['<shape isShadow="true" strokeweight="', strokeWidth,
  4274. '" filled="false" path="', modifiedPath,
  4275. '" coordsize="10 10" style="', element.style.cssText, '" />'];
  4276. shadow = createElement(renderer.prepVML(markup),
  4277. null, {
  4278. left: pInt(elemStyle.left) + pick(shadowOptions.offsetX, 1),
  4279. top: pInt(elemStyle.top) + pick(shadowOptions.offsetY, 1)
  4280. }
  4281. );
  4282. if (cutOff) {
  4283. shadow.cutOff = strokeWidth + 1;
  4284. }
  4285. // apply the opacity
  4286. markup = ['<stroke color="', shadowOptions.color || 'black', '" opacity="', shadowElementOpacity * i, '"/>'];
  4287. createElement(renderer.prepVML(markup), null, null, shadow);
  4288. // insert it
  4289. if (group) {
  4290. group.element.appendChild(shadow);
  4291. } else {
  4292. element.parentNode.insertBefore(shadow, element);
  4293. }
  4294. // record it
  4295. shadows.push(shadow);
  4296. }
  4297. this.shadows = shadows;
  4298. }
  4299. return this;
  4300. }
  4301. };
  4302. VMLElement = extendClass(SVGElement, VMLElement);
  4303. /**
  4304. * The VML renderer
  4305. */
  4306. var VMLRendererExtension = { // inherit SVGRenderer
  4307. Element: VMLElement,
  4308. isIE8: userAgent.indexOf('MSIE 8.0') > -1,
  4309. /**
  4310. * Initialize the VMLRenderer
  4311. * @param {Object} container
  4312. * @param {Number} width
  4313. * @param {Number} height
  4314. */
  4315. init: function (container, width, height) {
  4316. var renderer = this,
  4317. boxWrapper,
  4318. box;
  4319. renderer.alignedObjects = [];
  4320. boxWrapper = renderer.createElement(DIV);
  4321. box = boxWrapper.element;
  4322. box.style.position = RELATIVE; // for freeform drawing using renderer directly
  4323. container.appendChild(boxWrapper.element);
  4324. // generate the containing box
  4325. renderer.isVML = true;
  4326. renderer.box = box;
  4327. renderer.boxWrapper = boxWrapper;
  4328. renderer.setSize(width, height, false);
  4329. // The only way to make IE6 and IE7 print is to use a global namespace. However,
  4330. // with IE8 the only way to make the dynamic shapes visible in screen and print mode
  4331. // seems to be to add the xmlns attribute and the behaviour style inline.
  4332. if (!doc.namespaces.hcv) {
  4333. doc.namespaces.add('hcv', 'urn:schemas-microsoft-com:vml');
  4334. // setup default css
  4335. doc.createStyleSheet().cssText =
  4336. 'hcv\\:fill, hcv\\:path, hcv\\:shape, hcv\\:stroke' +
  4337. '{ behavior:url(#default#VML); display: inline-block; } ';
  4338. }
  4339. },
  4340. /**
  4341. * Detect whether the renderer is hidden. This happens when one of the parent elements
  4342. * has display: none
  4343. */
  4344. isHidden: function () {
  4345. return !this.box.offsetWidth;
  4346. },
  4347. /**
  4348. * Define a clipping rectangle. In VML it is accomplished by storing the values
  4349. * for setting the CSS style to all associated members.
  4350. *
  4351. * @param {Number} x
  4352. * @param {Number} y
  4353. * @param {Number} width
  4354. * @param {Number} height
  4355. */
  4356. clipRect: function (x, y, width, height) {
  4357. // create a dummy element
  4358. var clipRect = this.createElement(),
  4359. isObj = isObject(x);
  4360. // mimic a rectangle with its style object for automatic updating in attr
  4361. return extend(clipRect, {
  4362. members: [],
  4363. left: isObj ? x.x : x,
  4364. top: isObj ? x.y : y,
  4365. width: isObj ? x.width : width,
  4366. height: isObj ? x.height : height,
  4367. getCSS: function (wrapper) {
  4368. var element = wrapper.element,
  4369. nodeName = element.nodeName,
  4370. isShape = nodeName === 'shape',
  4371. inverted = wrapper.inverted,
  4372. rect = this,
  4373. top = rect.top - (isShape ? element.offsetTop : 0),
  4374. left = rect.left,
  4375. right = left + rect.width,
  4376. bottom = top + rect.height,
  4377. ret = {
  4378. clip: 'rect(' +
  4379. mathRound(inverted ? left : top) + 'px,' +
  4380. mathRound(inverted ? bottom : right) + 'px,' +
  4381. mathRound(inverted ? right : bottom) + 'px,' +
  4382. mathRound(inverted ? top : left) + 'px)'
  4383. };
  4384. // issue 74 workaround
  4385. if (!inverted && docMode8 && nodeName === 'DIV') {
  4386. extend(ret, {
  4387. width: right + PX,
  4388. height: bottom + PX
  4389. });
  4390. }
  4391. return ret;
  4392. },
  4393. // used in attr and animation to update the clipping of all members
  4394. updateClipping: function () {
  4395. each(clipRect.members, function (member) {
  4396. member.css(clipRect.getCSS(member));
  4397. });
  4398. }
  4399. });
  4400. },
  4401. /**
  4402. * Take a color and return it if it's a string, make it a gradient if it's a
  4403. * gradient configuration object, and apply opacity.
  4404. *
  4405. * @param {Object} color The color or config object
  4406. */
  4407. color: function (color, elem, prop, wrapper) {
  4408. var renderer = this,
  4409. colorObject,
  4410. regexRgba = /^rgba/,
  4411. markup,
  4412. fillType,
  4413. ret = NONE;
  4414. // Check for linear or radial gradient
  4415. if (color && color.linearGradient) {
  4416. fillType = 'gradient';
  4417. } else if (color && color.radialGradient) {
  4418. fillType = 'pattern';
  4419. }
  4420. if (fillType) {
  4421. var stopColor,
  4422. stopOpacity,
  4423. gradient = color.linearGradient || color.radialGradient,
  4424. x1,
  4425. y1,
  4426. x2,
  4427. y2,
  4428. opacity1,
  4429. opacity2,
  4430. color1,
  4431. color2,
  4432. fillAttr = '',
  4433. stops = color.stops,
  4434. firstStop,
  4435. lastStop,
  4436. colors = [],
  4437. addFillNode = function () {
  4438. // Add the fill subnode. When colors attribute is used, the meanings of opacity and o:opacity2
  4439. // are reversed.
  4440. markup = ['<fill colors="' + colors.join(',') + '" opacity="', opacity2, '" o:opacity2="', opacity1,
  4441. '" type="', fillType, '" ', fillAttr, 'focus="100%" method="any" />'];
  4442. createElement(renderer.prepVML(markup), null, null, elem);
  4443. };
  4444. // Extend from 0 to 1
  4445. firstStop = stops[0];
  4446. lastStop = stops[stops.length - 1];
  4447. if (firstStop[0] > 0) {
  4448. stops.unshift([
  4449. 0,
  4450. firstStop[1]
  4451. ]);
  4452. }
  4453. if (lastStop[0] < 1) {
  4454. stops.push([
  4455. 1,
  4456. lastStop[1]
  4457. ]);
  4458. }
  4459. // Compute the stops
  4460. each(stops, function (stop, i) {
  4461. if (regexRgba.test(stop[1])) {
  4462. colorObject = Color(stop[1]);
  4463. stopColor = colorObject.get('rgb');
  4464. stopOpacity = colorObject.get('a');
  4465. } else {
  4466. stopColor = stop[1];
  4467. stopOpacity = 1;
  4468. }
  4469. // Build the color attribute
  4470. colors.push((stop[0] * 100) + '% ' + stopColor);
  4471. // Only start and end opacities are allowed, so we use the first and the last
  4472. if (!i) {
  4473. opacity1 = stopOpacity;
  4474. color2 = stopColor;
  4475. } else {
  4476. opacity2 = stopOpacity;
  4477. color1 = stopColor;
  4478. }
  4479. });
  4480. // Apply the gradient to fills only.
  4481. if (prop === 'fill') {
  4482. // Handle linear gradient angle
  4483. if (fillType === 'gradient') {
  4484. x1 = gradient.x1 || gradient[0] || 0;
  4485. y1 = gradient.y1 || gradient[1] || 0;
  4486. x2 = gradient.x2 || gradient[2] || 0;
  4487. y2 = gradient.y2 || gradient[3] || 0;
  4488. fillAttr = 'angle="' + (90 - math.atan(
  4489. (y2 - y1) / // y vector
  4490. (x2 - x1) // x vector
  4491. ) * 180 / mathPI) + '"';
  4492. addFillNode();
  4493. // Radial (circular) gradient
  4494. } else {
  4495. var r = gradient.r,
  4496. sizex = r * 2,
  4497. sizey = r * 2,
  4498. cx = gradient.cx,
  4499. cy = gradient.cy,
  4500. radialReference = elem.radialReference,
  4501. bBox,
  4502. applyRadialGradient = function () {
  4503. if (radialReference) {
  4504. bBox = wrapper.getBBox();
  4505. cx += (radialReference[0] - bBox.x) / bBox.width - 0.5;
  4506. cy += (radialReference[1] - bBox.y) / bBox.height - 0.5;
  4507. sizex *= radialReference[2] / bBox.width;
  4508. sizey *= radialReference[2] / bBox.height;
  4509. }
  4510. fillAttr = 'src="' + defaultOptions.global.VMLRadialGradientURL + '" ' +
  4511. 'size="' + sizex + ',' + sizey + '" ' +
  4512. 'origin="0.5,0.5" ' +
  4513. 'position="' + cx + ',' + cy + '" ' +
  4514. 'color2="' + color2 + '" ';
  4515. addFillNode();
  4516. };
  4517. // Apply radial gradient
  4518. if (wrapper.added) {
  4519. applyRadialGradient();
  4520. } else {
  4521. // We need to know the bounding box to get the size and position right
  4522. addEvent(wrapper, 'add', applyRadialGradient);
  4523. }
  4524. // The fill element's color attribute is broken in IE8 standards mode, so we
  4525. // need to set the parent shape's fillcolor attribute instead.
  4526. ret = color1;
  4527. }
  4528. // Gradients are not supported for VML stroke, return the first color. #722.
  4529. } else {
  4530. ret = stopColor;
  4531. }
  4532. // if the color is an rgba color, split it and add a fill node
  4533. // to hold the opacity component
  4534. } else if (regexRgba.test(color) && elem.tagName !== 'IMG') {
  4535. colorObject = Color(color);
  4536. markup = ['<', prop, ' opacity="', colorObject.get('a'), '"/>'];
  4537. createElement(this.prepVML(markup), null, null, elem);
  4538. ret = colorObject.get('rgb');
  4539. } else {
  4540. var propNodes = elem.getElementsByTagName(prop); // 'stroke' or 'fill' node
  4541. if (propNodes.length) {
  4542. propNodes[0].opacity = 1;
  4543. propNodes[0].type = 'solid';
  4544. }
  4545. ret = color;
  4546. }
  4547. return ret;
  4548. },
  4549. /**
  4550. * Take a VML string and prepare it for either IE8 or IE6/IE7.
  4551. * @param {Array} markup A string array of the VML markup to prepare
  4552. */
  4553. prepVML: function (markup) {
  4554. var vmlStyle = 'display:inline-block;behavior:url(#default#VML);',
  4555. isIE8 = this.isIE8;
  4556. markup = markup.join('');
  4557. if (isIE8) { // add xmlns and style inline
  4558. markup = markup.replace('/>', ' xmlns="urn:schemas-microsoft-com:vml" />');
  4559. if (markup.indexOf('style="') === -1) {
  4560. markup = markup.replace('/>', ' style="' + vmlStyle + '" />');
  4561. } else {
  4562. markup = markup.replace('style="', 'style="' + vmlStyle);
  4563. }
  4564. } else { // add namespace
  4565. markup = markup.replace('<', '<hcv:');
  4566. }
  4567. return markup;
  4568. },
  4569. /**
  4570. * Create rotated and aligned text
  4571. * @param {String} str
  4572. * @param {Number} x
  4573. * @param {Number} y
  4574. */
  4575. text: SVGRenderer.prototype.html,
  4576. /**
  4577. * Create and return a path element
  4578. * @param {Array} path
  4579. */
  4580. path: function (path) {
  4581. var attr = {
  4582. // subpixel precision down to 0.1 (width and height = 1px)
  4583. coordsize: '10 10'
  4584. };
  4585. if (isArray(path)) {
  4586. attr.d = path;
  4587. } else if (isObject(path)) { // attributes
  4588. extend(attr, path);
  4589. }
  4590. // create the shape
  4591. return this.createElement('shape').attr(attr);
  4592. },
  4593. /**
  4594. * Create and return a circle element. In VML circles are implemented as
  4595. * shapes, which is faster than v:oval
  4596. * @param {Number} x
  4597. * @param {Number} y
  4598. * @param {Number} r
  4599. */
  4600. circle: function (x, y, r) {
  4601. var circle = this.symbol('circle');
  4602. if (isObject(x)) {
  4603. r = x.r;
  4604. y = x.y;
  4605. x = x.x;
  4606. }
  4607. circle.isCircle = true; // Causes x and y to mean center (#1682)
  4608. return circle.attr({ x: x, y: y, width: 2 * r, height: 2 * r });
  4609. },
  4610. /**
  4611. * Create a group using an outer div and an inner v:group to allow rotating
  4612. * and flipping. A simple v:group would have problems with positioning
  4613. * child HTML elements and CSS clip.
  4614. *
  4615. * @param {String} name The name of the group
  4616. */
  4617. g: function (name) {
  4618. var wrapper,
  4619. attribs;
  4620. // set the class name
  4621. if (name) {
  4622. attribs = { 'className': PREFIX + name, 'class': PREFIX + name };
  4623. }
  4624. // the div to hold HTML and clipping
  4625. wrapper = this.createElement(DIV).attr(attribs);
  4626. return wrapper;
  4627. },
  4628. /**
  4629. * VML override to create a regular HTML image
  4630. * @param {String} src
  4631. * @param {Number} x
  4632. * @param {Number} y
  4633. * @param {Number} width
  4634. * @param {Number} height
  4635. */
  4636. image: function (src, x, y, width, height) {
  4637. var obj = this.createElement('img')
  4638. .attr({ src: src });
  4639. if (arguments.length > 1) {
  4640. obj.attr({
  4641. x: x,
  4642. y: y,
  4643. width: width,
  4644. height: height
  4645. });
  4646. }
  4647. return obj;
  4648. },
  4649. /**
  4650. * VML uses a shape for rect to overcome bugs and rotation problems
  4651. */
  4652. rect: function (x, y, width, height, r, strokeWidth) {
  4653. if (isObject(x)) {
  4654. y = x.y;
  4655. width = x.width;
  4656. height = x.height;
  4657. strokeWidth = x.strokeWidth;
  4658. x = x.x;
  4659. }
  4660. var wrapper = this.symbol('rect');
  4661. wrapper.r = r;
  4662. return wrapper.attr(wrapper.crisp(strokeWidth, x, y, mathMax(width, 0), mathMax(height, 0)));
  4663. },
  4664. /**
  4665. * In the VML renderer, each child of an inverted div (group) is inverted
  4666. * @param {Object} element
  4667. * @param {Object} parentNode
  4668. */
  4669. invertChild: function (element, parentNode) {
  4670. var parentStyle = parentNode.style;
  4671. css(element, {
  4672. flip: 'x',
  4673. left: pInt(parentStyle.width) - 1,
  4674. top: pInt(parentStyle.height) - 1,
  4675. rotation: -90
  4676. });
  4677. },
  4678. /**
  4679. * Symbol definitions that override the parent SVG renderer's symbols
  4680. *
  4681. */
  4682. symbols: {
  4683. // VML specific arc function
  4684. arc: function (x, y, w, h, options) {
  4685. var start = options.start,
  4686. end = options.end,
  4687. radius = options.r || w || h,
  4688. innerRadius = options.innerR,
  4689. cosStart = mathCos(start),
  4690. sinStart = mathSin(start),
  4691. cosEnd = mathCos(end),
  4692. sinEnd = mathSin(end),
  4693. ret;
  4694. if (end - start === 0) { // no angle, don't show it.
  4695. return ['x'];
  4696. }
  4697. ret = [
  4698. 'wa', // clockwise arc to
  4699. x - radius, // left
  4700. y - radius, // top
  4701. x + radius, // right
  4702. y + radius, // bottom
  4703. x + radius * cosStart, // start x
  4704. y + radius * sinStart, // start y
  4705. x + radius * cosEnd, // end x
  4706. y + radius * sinEnd // end y
  4707. ];
  4708. if (options.open && !innerRadius) {
  4709. ret.push(
  4710. 'e',
  4711. M,
  4712. x,// - innerRadius,
  4713. y// - innerRadius
  4714. );
  4715. }
  4716. ret.push(
  4717. 'at', // anti clockwise arc to
  4718. x - innerRadius, // left
  4719. y - innerRadius, // top
  4720. x + innerRadius, // right
  4721. y + innerRadius, // bottom
  4722. x + innerRadius * cosEnd, // start x
  4723. y + innerRadius * sinEnd, // start y
  4724. x + innerRadius * cosStart, // end x
  4725. y + innerRadius * sinStart, // end y
  4726. 'x', // finish path
  4727. 'e' // close
  4728. );
  4729. ret.isArc = true;
  4730. return ret;
  4731. },
  4732. // Add circle symbol path. This performs significantly faster than v:oval.
  4733. circle: function (x, y, w, h, wrapper) {
  4734. // Center correction, #1682
  4735. if (wrapper && wrapper.isCircle) {
  4736. x -= w / 2;
  4737. y -= h / 2;
  4738. }
  4739. // Return the path
  4740. return [
  4741. 'wa', // clockwisearcto
  4742. x, // left
  4743. y, // top
  4744. x + w, // right
  4745. y + h, // bottom
  4746. x + w, // start x
  4747. y + h / 2, // start y
  4748. x + w, // end x
  4749. y + h / 2, // end y
  4750. //'x', // finish path
  4751. 'e' // close
  4752. ];
  4753. },
  4754. /**
  4755. * Add rectangle symbol path which eases rotation and omits arcsize problems
  4756. * compared to the built-in VML roundrect shape
  4757. *
  4758. * @param {Number} left Left position
  4759. * @param {Number} top Top position
  4760. * @param {Number} r Border radius
  4761. * @param {Object} options Width and height
  4762. */
  4763. rect: function (left, top, width, height, options) {
  4764. var right = left + width,
  4765. bottom = top + height,
  4766. ret,
  4767. r;
  4768. // No radius, return the more lightweight square
  4769. if (!defined(options) || !options.r) {
  4770. ret = SVGRenderer.prototype.symbols.square.apply(0, arguments);
  4771. // Has radius add arcs for the corners
  4772. } else {
  4773. r = mathMin(options.r, width, height);
  4774. ret = [
  4775. M,
  4776. left + r, top,
  4777. L,
  4778. right - r, top,
  4779. 'wa',
  4780. right - 2 * r, top,
  4781. right, top + 2 * r,
  4782. right - r, top,
  4783. right, top + r,
  4784. L,
  4785. right, bottom - r,
  4786. 'wa',
  4787. right - 2 * r, bottom - 2 * r,
  4788. right, bottom,
  4789. right, bottom - r,
  4790. right - r, bottom,
  4791. L,
  4792. left + r, bottom,
  4793. 'wa',
  4794. left, bottom - 2 * r,
  4795. left + 2 * r, bottom,
  4796. left + r, bottom,
  4797. left, bottom - r,
  4798. L,
  4799. left, top + r,
  4800. 'wa',
  4801. left, top,
  4802. left + 2 * r, top + 2 * r,
  4803. left, top + r,
  4804. left + r, top,
  4805. 'x',
  4806. 'e'
  4807. ];
  4808. }
  4809. return ret;
  4810. }
  4811. }
  4812. };
  4813. Highcharts.VMLRenderer = VMLRenderer = function () {
  4814. this.init.apply(this, arguments);
  4815. };
  4816. VMLRenderer.prototype = merge(SVGRenderer.prototype, VMLRendererExtension);
  4817. // general renderer
  4818. Renderer = VMLRenderer;
  4819. }
  4820. /* ****************************************************************************
  4821. * *
  4822. * END OF INTERNET EXPLORER <= 8 SPECIFIC CODE *
  4823. * *
  4824. *****************************************************************************/
  4825. /* ****************************************************************************
  4826. * *
  4827. * START OF ANDROID < 3 SPECIFIC CODE. THIS CAN BE REMOVED IF YOU'RE NOT *
  4828. * TARGETING THAT SYSTEM. *
  4829. * *
  4830. *****************************************************************************/
  4831. var CanVGRenderer,
  4832. CanVGController;
  4833. if (useCanVG) {
  4834. /**
  4835. * The CanVGRenderer is empty from start to keep the source footprint small.
  4836. * When requested, the CanVGController downloads the rest of the source packaged
  4837. * together with the canvg library.
  4838. */
  4839. Highcharts.CanVGRenderer = CanVGRenderer = function () {
  4840. // Override the global SVG namespace to fake SVG/HTML that accepts CSS
  4841. SVG_NS = 'http://www.w3.org/1999/xhtml';
  4842. };
  4843. /**
  4844. * Start with an empty symbols object. This is needed when exporting is used (exporting.src.js will add a few symbols), but
  4845. * the implementation from SvgRenderer will not be merged in until first render.
  4846. */
  4847. CanVGRenderer.prototype.symbols = {};
  4848. /**
  4849. * Handles on demand download of canvg rendering support.
  4850. */
  4851. CanVGController = (function () {
  4852. // List of renderering calls
  4853. var deferredRenderCalls = [];
  4854. /**
  4855. * When downloaded, we are ready to draw deferred charts.
  4856. */
  4857. function drawDeferred() {
  4858. var callLength = deferredRenderCalls.length,
  4859. callIndex;
  4860. // Draw all pending render calls
  4861. for (callIndex = 0; callIndex < callLength; callIndex++) {
  4862. deferredRenderCalls[callIndex]();
  4863. }
  4864. // Clear the list
  4865. deferredRenderCalls = [];
  4866. }
  4867. return {
  4868. push: function (func, scriptLocation) {
  4869. // Only get the script once
  4870. if (deferredRenderCalls.length === 0) {
  4871. getScript(scriptLocation, drawDeferred);
  4872. }
  4873. // Register render call
  4874. deferredRenderCalls.push(func);
  4875. }
  4876. };
  4877. }());
  4878. Renderer = CanVGRenderer;
  4879. } // end CanVGRenderer
  4880. /* ****************************************************************************
  4881. * *
  4882. * END OF ANDROID < 3 SPECIFIC CODE *
  4883. * *
  4884. *****************************************************************************/
  4885. /**
  4886. * The Tick class
  4887. */
  4888. function Tick(axis, pos, type, noLabel) {
  4889. this.axis = axis;
  4890. this.pos = pos;
  4891. this.type = type || '';
  4892. this.isNew = true;
  4893. if (!type && !noLabel) {
  4894. this.addLabel();
  4895. }
  4896. }
  4897. Tick.prototype = {
  4898. /**
  4899. * Write the tick label
  4900. */
  4901. addLabel: function () {
  4902. var tick = this,
  4903. axis = tick.axis,
  4904. options = axis.options,
  4905. chart = axis.chart,
  4906. horiz = axis.horiz,
  4907. categories = axis.categories,
  4908. names = axis.series[0] && axis.series[0].names,
  4909. pos = tick.pos,
  4910. labelOptions = options.labels,
  4911. str,
  4912. tickPositions = axis.tickPositions,
  4913. width = (horiz && categories &&
  4914. !labelOptions.step && !labelOptions.staggerLines &&
  4915. !labelOptions.rotation &&
  4916. chart.plotWidth / tickPositions.length) ||
  4917. (!horiz && (chart.optionsMarginLeft || chart.plotWidth / 2)), // #1580
  4918. isFirst = pos === tickPositions[0],
  4919. isLast = pos === tickPositions[tickPositions.length - 1],
  4920. css,
  4921. attr,
  4922. value = categories ?
  4923. pick(categories[pos], names && names[pos], pos) :
  4924. pos,
  4925. label = tick.label,
  4926. tickPositionInfo = tickPositions.info,
  4927. dateTimeLabelFormat;
  4928. // Set the datetime label format. If a higher rank is set for this position, use that. If not,
  4929. // use the general format.
  4930. if (axis.isDatetimeAxis && tickPositionInfo) {
  4931. dateTimeLabelFormat = options.dateTimeLabelFormats[tickPositionInfo.higherRanks[pos] || tickPositionInfo.unitName];
  4932. }
  4933. // set properties for access in render method
  4934. tick.isFirst = isFirst;
  4935. tick.isLast = isLast;
  4936. // get the string
  4937. str = axis.labelFormatter.call({
  4938. axis: axis,
  4939. chart: chart,
  4940. isFirst: isFirst,
  4941. isLast: isLast,
  4942. dateTimeLabelFormat: dateTimeLabelFormat,
  4943. value: axis.isLog ? correctFloat(lin2log(value)) : value
  4944. });
  4945. // prepare CSS
  4946. css = width && { width: mathMax(1, mathRound(width - 2 * (labelOptions.padding || 10))) + PX };
  4947. css = extend(css, labelOptions.style);
  4948. // first call
  4949. if (!defined(label)) {
  4950. attr = {
  4951. align: labelOptions.align
  4952. };
  4953. if (isNumber(labelOptions.rotation)) {
  4954. attr.rotation = labelOptions.rotation;
  4955. }
  4956. tick.label =
  4957. defined(str) && labelOptions.enabled ?
  4958. chart.renderer.text(
  4959. str,
  4960. 0,
  4961. 0,
  4962. labelOptions.useHTML
  4963. )
  4964. .attr(attr)
  4965. // without position absolute, IE export sometimes is wrong
  4966. .css(css)
  4967. .add(axis.labelGroup) :
  4968. null;
  4969. // update
  4970. } else if (label) {
  4971. label.attr({
  4972. text: str
  4973. })
  4974. .css(css);
  4975. }
  4976. },
  4977. /**
  4978. * Get the offset height or width of the label
  4979. */
  4980. getLabelSize: function () {
  4981. var label = this.label,
  4982. axis = this.axis;
  4983. return label ?
  4984. ((this.labelBBox = label.getBBox()))[axis.horiz ? 'height' : 'width'] :
  4985. 0;
  4986. },
  4987. /**
  4988. * Find how far the labels extend to the right and left of the tick's x position. Used for anti-collision
  4989. * detection with overflow logic.
  4990. */
  4991. getLabelSides: function () {
  4992. var bBox = this.labelBBox, // assume getLabelSize has run at this point
  4993. axis = this.axis,
  4994. options = axis.options,
  4995. labelOptions = options.labels,
  4996. width = bBox.width,
  4997. leftSide = width * { left: 0, center: 0.5, right: 1 }[labelOptions.align] - labelOptions.x;
  4998. return [-leftSide, width - leftSide];
  4999. },
  5000. /**
  5001. * Handle the label overflow by adjusting the labels to the left and right edge, or
  5002. * hide them if they collide into the neighbour label.
  5003. */
  5004. handleOverflow: function (index, xy) {
  5005. var show = true,
  5006. axis = this.axis,
  5007. chart = axis.chart,
  5008. isFirst = this.isFirst,
  5009. isLast = this.isLast,
  5010. x = xy.x,
  5011. reversed = axis.reversed,
  5012. tickPositions = axis.tickPositions;
  5013. if (isFirst || isLast) {
  5014. var sides = this.getLabelSides(),
  5015. leftSide = sides[0],
  5016. rightSide = sides[1],
  5017. plotLeft = chart.plotLeft,
  5018. plotRight = plotLeft + axis.len,
  5019. neighbour = axis.ticks[tickPositions[index + (isFirst ? 1 : -1)]],
  5020. neighbourEdge = neighbour && neighbour.label.xy && neighbour.label.xy.x + neighbour.getLabelSides()[isFirst ? 0 : 1];
  5021. if ((isFirst && !reversed) || (isLast && reversed)) {
  5022. // Is the label spilling out to the left of the plot area?
  5023. if (x + leftSide < plotLeft) {
  5024. // Align it to plot left
  5025. x = plotLeft - leftSide;
  5026. // Hide it if it now overlaps the neighbour label
  5027. if (neighbour && x + rightSide > neighbourEdge) {
  5028. show = false;
  5029. }
  5030. }
  5031. } else {
  5032. // Is the label spilling out to the right of the plot area?
  5033. if (x + rightSide > plotRight) {
  5034. // Align it to plot right
  5035. x = plotRight - rightSide;
  5036. // Hide it if it now overlaps the neighbour label
  5037. if (neighbour && x + leftSide < neighbourEdge) {
  5038. show = false;
  5039. }
  5040. }
  5041. }
  5042. // Set the modified x position of the label
  5043. xy.x = x;
  5044. }
  5045. return show;
  5046. },
  5047. /**
  5048. * Get the x and y position for ticks and labels
  5049. */
  5050. getPosition: function (horiz, pos, tickmarkOffset, old) {
  5051. var axis = this.axis,
  5052. chart = axis.chart,
  5053. cHeight = (old && chart.oldChartHeight) || chart.chartHeight;
  5054. return {
  5055. x: horiz ?
  5056. axis.translate(pos + tickmarkOffset, null, null, old) + axis.transB :
  5057. axis.left + axis.offset + (axis.opposite ? ((old && chart.oldChartWidth) || chart.chartWidth) - axis.right - axis.left : 0),
  5058. y: horiz ?
  5059. cHeight - axis.bottom + axis.offset - (axis.opposite ? axis.height : 0) :
  5060. cHeight - axis.translate(pos + tickmarkOffset, null, null, old) - axis.transB
  5061. };
  5062. },
  5063. /**
  5064. * Get the x, y position of the tick label
  5065. */
  5066. getLabelPosition: function (x, y, label, horiz, labelOptions, tickmarkOffset, index, step) {
  5067. var axis = this.axis,
  5068. transA = axis.transA,
  5069. reversed = axis.reversed,
  5070. staggerLines = axis.staggerLines;
  5071. x = x + labelOptions.x - (tickmarkOffset && horiz ?
  5072. tickmarkOffset * transA * (reversed ? -1 : 1) : 0);
  5073. y = y + labelOptions.y - (tickmarkOffset && !horiz ?
  5074. tickmarkOffset * transA * (reversed ? 1 : -1) : 0);
  5075. // Vertically centered
  5076. if (!defined(labelOptions.y)) {
  5077. y += pInt(label.styles.lineHeight) * 0.9 - label.getBBox().height / 2;
  5078. }
  5079. // Correct for staggered labels
  5080. if (staggerLines) {
  5081. y += (index / (step || 1) % staggerLines) * 16;
  5082. }
  5083. return {
  5084. x: x,
  5085. y: y
  5086. };
  5087. },
  5088. /**
  5089. * Extendible method to return the path of the marker
  5090. */
  5091. getMarkPath: function (x, y, tickLength, tickWidth, horiz, renderer) {
  5092. return renderer.crispLine([
  5093. M,
  5094. x,
  5095. y,
  5096. L,
  5097. x + (horiz ? 0 : -tickLength),
  5098. y + (horiz ? tickLength : 0)
  5099. ], tickWidth);
  5100. },
  5101. /**
  5102. * Put everything in place
  5103. *
  5104. * @param index {Number}
  5105. * @param old {Boolean} Use old coordinates to prepare an animation into new position
  5106. */
  5107. render: function (index, old, opacity) {
  5108. var tick = this,
  5109. axis = tick.axis,
  5110. options = axis.options,
  5111. chart = axis.chart,
  5112. renderer = chart.renderer,
  5113. horiz = axis.horiz,
  5114. type = tick.type,
  5115. label = tick.label,
  5116. pos = tick.pos,
  5117. labelOptions = options.labels,
  5118. gridLine = tick.gridLine,
  5119. gridPrefix = type ? type + 'Grid' : 'grid',
  5120. tickPrefix = type ? type + 'Tick' : 'tick',
  5121. gridLineWidth = options[gridPrefix + 'LineWidth'],
  5122. gridLineColor = options[gridPrefix + 'LineColor'],
  5123. dashStyle = options[gridPrefix + 'LineDashStyle'],
  5124. tickLength = options[tickPrefix + 'Length'],
  5125. tickWidth = options[tickPrefix + 'Width'] || 0,
  5126. tickColor = options[tickPrefix + 'Color'],
  5127. tickPosition = options[tickPrefix + 'Position'],
  5128. gridLinePath,
  5129. mark = tick.mark,
  5130. markPath,
  5131. step = labelOptions.step,
  5132. attribs,
  5133. show = true,
  5134. tickmarkOffset = axis.tickmarkOffset,
  5135. xy = tick.getPosition(horiz, pos, tickmarkOffset, old),
  5136. x = xy.x,
  5137. y = xy.y,
  5138. reverseCrisp = ((horiz && x === axis.pos) || (!horiz && y === axis.pos + axis.len)) ? -1 : 1, // #1480
  5139. staggerLines = axis.staggerLines;
  5140. this.isActive = true;
  5141. // create the grid line
  5142. if (gridLineWidth) {
  5143. gridLinePath = axis.getPlotLinePath(pos + tickmarkOffset, gridLineWidth * reverseCrisp, old, true);
  5144. if (gridLine === UNDEFINED) {
  5145. attribs = {
  5146. stroke: gridLineColor,
  5147. 'stroke-width': gridLineWidth
  5148. };
  5149. if (dashStyle) {
  5150. attribs.dashstyle = dashStyle;
  5151. }
  5152. if (!type) {
  5153. attribs.zIndex = 1;
  5154. }
  5155. if (old) {
  5156. attribs.opacity = 0;
  5157. }
  5158. tick.gridLine = gridLine =
  5159. gridLineWidth ?
  5160. renderer.path(gridLinePath)
  5161. .attr(attribs).add(axis.gridGroup) :
  5162. null;
  5163. }
  5164. // If the parameter 'old' is set, the current call will be followed
  5165. // by another call, therefore do not do any animations this time
  5166. if (!old && gridLine && gridLinePath) {
  5167. gridLine[tick.isNew ? 'attr' : 'animate']({
  5168. d: gridLinePath,
  5169. opacity: opacity
  5170. });
  5171. }
  5172. }
  5173. // create the tick mark
  5174. if (tickWidth && tickLength) {
  5175. // negate the length
  5176. if (tickPosition === 'inside') {
  5177. tickLength = -tickLength;
  5178. }
  5179. if (axis.opposite) {
  5180. tickLength = -tickLength;
  5181. }
  5182. markPath = tick.getMarkPath(x, y, tickLength, tickWidth * reverseCrisp, horiz, renderer);
  5183. if (mark) { // updating
  5184. mark.animate({
  5185. d: markPath,
  5186. opacity: opacity
  5187. });
  5188. } else { // first time
  5189. tick.mark = renderer.path(
  5190. markPath
  5191. ).attr({
  5192. stroke: tickColor,
  5193. 'stroke-width': tickWidth,
  5194. opacity: opacity
  5195. }).add(axis.axisGroup);
  5196. }
  5197. }
  5198. // the label is created on init - now move it into place
  5199. if (label && !isNaN(x)) {
  5200. label.xy = xy = tick.getLabelPosition(x, y, label, horiz, labelOptions, tickmarkOffset, index, step);
  5201. // apply show first and show last
  5202. if ((tick.isFirst && !pick(options.showFirstLabel, 1)) ||
  5203. (tick.isLast && !pick(options.showLastLabel, 1))) {
  5204. show = false;
  5205. // Handle label overflow and show or hide accordingly
  5206. } else if (!staggerLines && horiz && labelOptions.overflow === 'justify' && !tick.handleOverflow(index, xy)) {
  5207. show = false;
  5208. }
  5209. // apply step
  5210. if (step && index % step) {
  5211. // show those indices dividable by step
  5212. show = false;
  5213. }
  5214. // Set the new position, and show or hide
  5215. if (show && !isNaN(xy.y)) {
  5216. xy.opacity = opacity;
  5217. label[tick.isNew ? 'attr' : 'animate'](xy);
  5218. tick.isNew = false;
  5219. } else {
  5220. label.attr('y', -9999); // #1338
  5221. }
  5222. }
  5223. },
  5224. /**
  5225. * Destructor for the tick prototype
  5226. */
  5227. destroy: function () {
  5228. destroyObjectProperties(this, this.axis);
  5229. }
  5230. };
  5231. /**
  5232. * The object wrapper for plot lines and plot bands
  5233. * @param {Object} options
  5234. */
  5235. function PlotLineOrBand(axis, options) {
  5236. this.axis = axis;
  5237. if (options) {
  5238. this.options = options;
  5239. this.id = options.id;
  5240. }
  5241. }
  5242. PlotLineOrBand.prototype = {
  5243. /**
  5244. * Render the plot line or plot band. If it is already existing,
  5245. * move it.
  5246. */
  5247. render: function () {
  5248. var plotLine = this,
  5249. axis = plotLine.axis,
  5250. horiz = axis.horiz,
  5251. halfPointRange = (axis.pointRange || 0) / 2,
  5252. options = plotLine.options,
  5253. optionsLabel = options.label,
  5254. label = plotLine.label,
  5255. width = options.width,
  5256. to = options.to,
  5257. from = options.from,
  5258. isBand = defined(from) && defined(to),
  5259. value = options.value,
  5260. dashStyle = options.dashStyle,
  5261. svgElem = plotLine.svgElem,
  5262. path = [],
  5263. addEvent,
  5264. eventType,
  5265. xs,
  5266. ys,
  5267. x,
  5268. y,
  5269. color = options.color,
  5270. zIndex = options.zIndex,
  5271. events = options.events,
  5272. attribs,
  5273. renderer = axis.chart.renderer;
  5274. // logarithmic conversion
  5275. if (axis.isLog) {
  5276. from = log2lin(from);
  5277. to = log2lin(to);
  5278. value = log2lin(value);
  5279. }
  5280. // plot line
  5281. if (width) {
  5282. path = axis.getPlotLinePath(value, width);
  5283. attribs = {
  5284. stroke: color,
  5285. 'stroke-width': width
  5286. };
  5287. if (dashStyle) {
  5288. attribs.dashstyle = dashStyle;
  5289. }
  5290. } else if (isBand) { // plot band
  5291. // keep within plot area
  5292. from = mathMax(from, axis.min - halfPointRange);
  5293. to = mathMin(to, axis.max + halfPointRange);
  5294. path = axis.getPlotBandPath(from, to, options);
  5295. attribs = {
  5296. fill: color
  5297. };
  5298. if (options.borderWidth) {
  5299. attribs.stroke = options.borderColor;
  5300. attribs['stroke-width'] = options.borderWidth;
  5301. }
  5302. } else {
  5303. return;
  5304. }
  5305. // zIndex
  5306. if (defined(zIndex)) {
  5307. attribs.zIndex = zIndex;
  5308. }
  5309. // common for lines and bands
  5310. if (svgElem) {
  5311. if (path) {
  5312. svgElem.animate({
  5313. d: path
  5314. }, null, svgElem.onGetPath);
  5315. } else {
  5316. svgElem.hide();
  5317. svgElem.onGetPath = function () {
  5318. svgElem.show();
  5319. };
  5320. }
  5321. } else if (path && path.length) {
  5322. plotLine.svgElem = svgElem = renderer.path(path)
  5323. .attr(attribs).add();
  5324. // events
  5325. if (events) {
  5326. addEvent = function (eventType) {
  5327. svgElem.on(eventType, function (e) {
  5328. events[eventType].apply(plotLine, [e]);
  5329. });
  5330. };
  5331. for (eventType in events) {
  5332. addEvent(eventType);
  5333. }
  5334. }
  5335. }
  5336. // the plot band/line label
  5337. if (optionsLabel && defined(optionsLabel.text) && path && path.length && axis.width > 0 && axis.height > 0) {
  5338. // apply defaults
  5339. optionsLabel = merge({
  5340. align: horiz && isBand && 'center',
  5341. x: horiz ? !isBand && 4 : 10,
  5342. verticalAlign : !horiz && isBand && 'middle',
  5343. y: horiz ? isBand ? 16 : 10 : isBand ? 6 : -4,
  5344. rotation: horiz && !isBand && 90
  5345. }, optionsLabel);
  5346. // add the SVG element
  5347. if (!label) {
  5348. plotLine.label = label = renderer.text(
  5349. optionsLabel.text,
  5350. 0,
  5351. 0
  5352. )
  5353. .attr({
  5354. align: optionsLabel.textAlign || optionsLabel.align,
  5355. rotation: optionsLabel.rotation,
  5356. zIndex: zIndex
  5357. })
  5358. .css(optionsLabel.style)
  5359. .add();
  5360. }
  5361. // get the bounding box and align the label
  5362. xs = [path[1], path[4], pick(path[6], path[1])];
  5363. ys = [path[2], path[5], pick(path[7], path[2])];
  5364. x = arrayMin(xs);
  5365. y = arrayMin(ys);
  5366. label.align(optionsLabel, false, {
  5367. x: x,
  5368. y: y,
  5369. width: arrayMax(xs) - x,
  5370. height: arrayMax(ys) - y
  5371. });
  5372. label.show();
  5373. } else if (label) { // move out of sight
  5374. label.hide();
  5375. }
  5376. // chainable
  5377. return plotLine;
  5378. },
  5379. /**
  5380. * Remove the plot line or band
  5381. */
  5382. destroy: function () {
  5383. var plotLine = this,
  5384. axis = plotLine.axis;
  5385. // remove it from the lookup
  5386. erase(axis.plotLinesAndBands, plotLine);
  5387. destroyObjectProperties(plotLine, this.axis);
  5388. }
  5389. };
  5390. /**
  5391. * The class for stack items
  5392. */
  5393. function StackItem(axis, options, isNegative, x, stackOption, stacking) {
  5394. var inverted = axis.chart.inverted;
  5395. this.axis = axis;
  5396. // Tells if the stack is negative
  5397. this.isNegative = isNegative;
  5398. // Save the options to be able to style the label
  5399. this.options = options;
  5400. // Save the x value to be able to position the label later
  5401. this.x = x;
  5402. // Save the stack option on the series configuration object, and whether to treat it as percent
  5403. this.stack = stackOption;
  5404. this.percent = stacking === 'percent';
  5405. // The align options and text align varies on whether the stack is negative and
  5406. // if the chart is inverted or not.
  5407. // First test the user supplied value, then use the dynamic.
  5408. this.alignOptions = {
  5409. align: options.align || (inverted ? (isNegative ? 'left' : 'right') : 'center'),
  5410. verticalAlign: options.verticalAlign || (inverted ? 'middle' : (isNegative ? 'bottom' : 'top')),
  5411. y: pick(options.y, inverted ? 4 : (isNegative ? 14 : -6)),
  5412. x: pick(options.x, inverted ? (isNegative ? -6 : 6) : 0)
  5413. };
  5414. this.textAlign = options.textAlign || (inverted ? (isNegative ? 'right' : 'left') : 'center');
  5415. }
  5416. StackItem.prototype = {
  5417. destroy: function () {
  5418. destroyObjectProperties(this, this.axis);
  5419. },
  5420. /**
  5421. * Sets the total of this stack. Should be called when a serie is hidden or shown
  5422. * since that will affect the total of other stacks.
  5423. */
  5424. setTotal: function (total) {
  5425. this.total = total;
  5426. this.cum = total;
  5427. },
  5428. /**
  5429. * Renders the stack total label and adds it to the stack label group.
  5430. */
  5431. render: function (group) {
  5432. var options = this.options,
  5433. formatOption = options.format, // docs: added stackLabel.format option
  5434. str = formatOption ?
  5435. format(formatOption, this) :
  5436. options.formatter.call(this); // format the text in the label
  5437. // Change the text to reflect the new total and set visibility to hidden in case the serie is hidden
  5438. if (this.label) {
  5439. this.label.attr({text: str, visibility: HIDDEN});
  5440. // Create new label
  5441. } else {
  5442. this.label =
  5443. this.axis.chart.renderer.text(str, 0, 0, options.useHTML) // dummy positions, actual position updated with setOffset method in columnseries
  5444. .css(options.style) // apply style
  5445. .attr({
  5446. align: this.textAlign, // fix the text-anchor
  5447. rotation: options.rotation, // rotation
  5448. visibility: HIDDEN // hidden until setOffset is called
  5449. })
  5450. .add(group); // add to the labels-group
  5451. }
  5452. },
  5453. /**
  5454. * Sets the offset that the stack has from the x value and repositions the label.
  5455. */
  5456. setOffset: function (xOffset, xWidth) {
  5457. var stackItem = this,
  5458. axis = stackItem.axis,
  5459. chart = axis.chart,
  5460. inverted = chart.inverted,
  5461. neg = this.isNegative, // special treatment is needed for negative stacks
  5462. y = axis.translate(this.percent ? 100 : this.total, 0, 0, 0, 1), // stack value translated mapped to chart coordinates
  5463. yZero = axis.translate(0), // stack origin
  5464. h = mathAbs(y - yZero), // stack height
  5465. x = chart.xAxis[0].translate(this.x) + xOffset, // stack x position
  5466. plotHeight = chart.plotHeight,
  5467. stackBox = { // this is the box for the complete stack
  5468. x: inverted ? (neg ? y : y - h) : x,
  5469. y: inverted ? plotHeight - x - xWidth : (neg ? (plotHeight - y - h) : plotHeight - y),
  5470. width: inverted ? h : xWidth,
  5471. height: inverted ? xWidth : h
  5472. },
  5473. label = this.label,
  5474. alignAttr;
  5475. if (label) {
  5476. label.align(this.alignOptions, null, stackBox); // align the label to the box
  5477. // Set visibility (#678)
  5478. alignAttr = label.alignAttr;
  5479. label.attr({
  5480. visibility: this.options.crop === false || chart.isInsidePlot(alignAttr.x, alignAttr.y) ?
  5481. (hasSVG ? 'inherit' : VISIBLE) :
  5482. HIDDEN
  5483. });
  5484. }
  5485. }
  5486. };
  5487. /**
  5488. * Create a new axis object
  5489. * @param {Object} chart
  5490. * @param {Object} options
  5491. */
  5492. function Axis() {
  5493. this.init.apply(this, arguments);
  5494. }
  5495. Axis.prototype = {
  5496. /**
  5497. * Default options for the X axis - the Y axis has extended defaults
  5498. */
  5499. defaultOptions: {
  5500. // allowDecimals: null,
  5501. // alternateGridColor: null,
  5502. // categories: [],
  5503. dateTimeLabelFormats: {
  5504. millisecond: '%H:%M:%S.%L',
  5505. second: '%H:%M:%S',
  5506. minute: '%H:%M',
  5507. hour: '%H:%M',
  5508. day: '%e. %b',
  5509. week: '%e. %b',
  5510. month: '%b \'%y',
  5511. year: '%Y'
  5512. },
  5513. endOnTick: false,
  5514. gridLineColor: '#C0C0C0',
  5515. // gridLineDashStyle: 'solid',
  5516. // gridLineWidth: 0,
  5517. // reversed: false,
  5518. labels: defaultLabelOptions,
  5519. // { step: null },
  5520. lineColor: '#C0D0E0',
  5521. lineWidth: 1,
  5522. //linkedTo: null,
  5523. //max: undefined,
  5524. //min: undefined,
  5525. minPadding: 0.01,
  5526. maxPadding: 0.01,
  5527. //minRange: null,
  5528. minorGridLineColor: '#E0E0E0',
  5529. // minorGridLineDashStyle: null,
  5530. minorGridLineWidth: 1,
  5531. minorTickColor: '#A0A0A0',
  5532. //minorTickInterval: null,
  5533. minorTickLength: 2,
  5534. minorTickPosition: 'outside', // inside or outside
  5535. //minorTickWidth: 0,
  5536. //opposite: false,
  5537. //offset: 0,
  5538. //plotBands: [{
  5539. // events: {},
  5540. // zIndex: 1,
  5541. // labels: { align, x, verticalAlign, y, style, rotation, textAlign }
  5542. //}],
  5543. //plotLines: [{
  5544. // events: {}
  5545. // dashStyle: {}
  5546. // zIndex:
  5547. // labels: { align, x, verticalAlign, y, style, rotation, textAlign }
  5548. //}],
  5549. //reversed: false,
  5550. // showFirstLabel: true,
  5551. // showLastLabel: true,
  5552. startOfWeek: 1,
  5553. startOnTick: false,
  5554. tickColor: '#C0D0E0',
  5555. //tickInterval: null,
  5556. tickLength: 5,
  5557. tickmarkPlacement: 'between', // on or between
  5558. tickPixelInterval: 100,
  5559. tickPosition: 'outside',
  5560. tickWidth: 1,
  5561. title: {
  5562. //text: null,
  5563. align: 'middle', // low, middle or high
  5564. //margin: 0 for horizontal, 10 for vertical axes,
  5565. //rotation: 0,
  5566. //side: 'outside',
  5567. style: {
  5568. color: '#4d759e',
  5569. //font: defaultFont.replace('normal', 'bold')
  5570. fontWeight: 'bold'
  5571. }
  5572. //x: 0,
  5573. //y: 0
  5574. },
  5575. type: 'linear' // linear, logarithmic or datetime
  5576. },
  5577. /**
  5578. * This options set extends the defaultOptions for Y axes
  5579. */
  5580. defaultYAxisOptions: {
  5581. endOnTick: true,
  5582. gridLineWidth: 1,
  5583. tickPixelInterval: 72,
  5584. showLastLabel: true,
  5585. labels: {
  5586. align: 'right',
  5587. x: -8,
  5588. y: 3
  5589. },
  5590. lineWidth: 0,
  5591. maxPadding: 0.05,
  5592. minPadding: 0.05,
  5593. startOnTick: true,
  5594. tickWidth: 0,
  5595. title: {
  5596. rotation: 270,
  5597. text: 'Values'
  5598. },
  5599. stackLabels: {
  5600. enabled: false,
  5601. //align: dynamic,
  5602. //y: dynamic,
  5603. //x: dynamic,
  5604. //verticalAlign: dynamic,
  5605. //textAlign: dynamic,
  5606. //rotation: 0,
  5607. formatter: function () {
  5608. return numberFormat(this.total, -1);
  5609. },
  5610. style: defaultLabelOptions.style
  5611. }
  5612. },
  5613. /**
  5614. * These options extend the defaultOptions for left axes
  5615. */
  5616. defaultLeftAxisOptions: {
  5617. labels: {
  5618. align: 'right',
  5619. x: -8,
  5620. y: null
  5621. },
  5622. title: {
  5623. rotation: 270
  5624. }
  5625. },
  5626. /**
  5627. * These options extend the defaultOptions for right axes
  5628. */
  5629. defaultRightAxisOptions: {
  5630. labels: {
  5631. align: 'left',
  5632. x: 8,
  5633. y: null
  5634. },
  5635. title: {
  5636. rotation: 90
  5637. }
  5638. },
  5639. /**
  5640. * These options extend the defaultOptions for bottom axes
  5641. */
  5642. defaultBottomAxisOptions: {
  5643. labels: {
  5644. align: 'center',
  5645. x: 0,
  5646. y: 14
  5647. // overflow: undefined,
  5648. // staggerLines: null
  5649. },
  5650. title: {
  5651. rotation: 0
  5652. }
  5653. },
  5654. /**
  5655. * These options extend the defaultOptions for left axes
  5656. */
  5657. defaultTopAxisOptions: {
  5658. labels: {
  5659. align: 'center',
  5660. x: 0,
  5661. y: -5
  5662. // overflow: undefined
  5663. // staggerLines: null
  5664. },
  5665. title: {
  5666. rotation: 0
  5667. }
  5668. },
  5669. /**
  5670. * Initialize the axis
  5671. */
  5672. init: function (chart, userOptions) {
  5673. var isXAxis = userOptions.isX,
  5674. axis = this;
  5675. // Flag, is the axis horizontal
  5676. axis.horiz = chart.inverted ? !isXAxis : isXAxis;
  5677. // Flag, isXAxis
  5678. axis.isXAxis = isXAxis;
  5679. axis.xOrY = isXAxis ? 'x' : 'y';
  5680. axis.opposite = userOptions.opposite; // needed in setOptions
  5681. axis.side = axis.horiz ?
  5682. (axis.opposite ? 0 : 2) : // top : bottom
  5683. (axis.opposite ? 1 : 3); // right : left
  5684. axis.setOptions(userOptions);
  5685. var options = this.options,
  5686. type = options.type,
  5687. isDatetimeAxis = type === 'datetime';
  5688. axis.labelFormatter = options.labels.formatter || axis.defaultLabelFormatter; // can be overwritten by dynamic format
  5689. // Flag, stagger lines or not
  5690. axis.staggerLines = axis.horiz && options.labels.staggerLines;
  5691. axis.userOptions = userOptions;
  5692. //axis.axisTitleMargin = UNDEFINED,// = options.title.margin,
  5693. axis.minPixelPadding = 0;
  5694. //axis.ignoreMinPadding = UNDEFINED; // can be set to true by a column or bar series
  5695. //axis.ignoreMaxPadding = UNDEFINED;
  5696. axis.chart = chart;
  5697. axis.reversed = options.reversed;
  5698. axis.zoomEnabled = options.zoomEnabled !== false;
  5699. // Initial categories
  5700. axis.categories = options.categories || type === 'category';
  5701. // Elements
  5702. //axis.axisGroup = UNDEFINED;
  5703. //axis.gridGroup = UNDEFINED;
  5704. //axis.axisTitle = UNDEFINED;
  5705. //axis.axisLine = UNDEFINED;
  5706. // Shorthand types
  5707. axis.isLog = type === 'logarithmic';
  5708. axis.isDatetimeAxis = isDatetimeAxis;
  5709. // Flag, if axis is linked to another axis
  5710. axis.isLinked = defined(options.linkedTo);
  5711. // Linked axis.
  5712. //axis.linkedParent = UNDEFINED;
  5713. // Flag if percentage mode
  5714. //axis.usePercentage = UNDEFINED;
  5715. // Tick positions
  5716. //axis.tickPositions = UNDEFINED; // array containing predefined positions
  5717. // Tick intervals
  5718. //axis.tickInterval = UNDEFINED;
  5719. //axis.minorTickInterval = UNDEFINED;
  5720. axis.tickmarkOffset = (axis.categories && options.tickmarkPlacement === 'between') ? 0.5 : 0;
  5721. // Major ticks
  5722. axis.ticks = {};
  5723. // Minor ticks
  5724. axis.minorTicks = {};
  5725. //axis.tickAmount = UNDEFINED;
  5726. // List of plotLines/Bands
  5727. axis.plotLinesAndBands = [];
  5728. // Alternate bands
  5729. axis.alternateBands = {};
  5730. // Axis metrics
  5731. //axis.left = UNDEFINED;
  5732. //axis.top = UNDEFINED;
  5733. //axis.width = UNDEFINED;
  5734. //axis.height = UNDEFINED;
  5735. //axis.bottom = UNDEFINED;
  5736. //axis.right = UNDEFINED;
  5737. //axis.transA = UNDEFINED;
  5738. //axis.transB = UNDEFINED;
  5739. //axis.oldTransA = UNDEFINED;
  5740. axis.len = 0;
  5741. //axis.oldMin = UNDEFINED;
  5742. //axis.oldMax = UNDEFINED;
  5743. //axis.oldUserMin = UNDEFINED;
  5744. //axis.oldUserMax = UNDEFINED;
  5745. //axis.oldAxisLength = UNDEFINED;
  5746. axis.minRange = axis.userMinRange = options.minRange || options.maxZoom;
  5747. axis.range = options.range;
  5748. axis.offset = options.offset || 0;
  5749. // Dictionary for stacks
  5750. axis.stacks = {};
  5751. axis._stacksTouched = 0;
  5752. // Min and max in the data
  5753. //axis.dataMin = UNDEFINED,
  5754. //axis.dataMax = UNDEFINED,
  5755. // The axis range
  5756. axis.max = null;
  5757. axis.min = null;
  5758. // User set min and max
  5759. //axis.userMin = UNDEFINED,
  5760. //axis.userMax = UNDEFINED,
  5761. // Run Axis
  5762. var eventType,
  5763. events = axis.options.events;
  5764. // Register
  5765. if (inArray(axis, chart.axes) === -1) { // don't add it again on Axis.update()
  5766. chart.axes.push(axis);
  5767. chart[isXAxis ? 'xAxis' : 'yAxis'].push(axis);
  5768. }
  5769. axis.series = axis.series || []; // populated by Series
  5770. // inverted charts have reversed xAxes as default
  5771. if (chart.inverted && isXAxis && axis.reversed === UNDEFINED) {
  5772. axis.reversed = true;
  5773. }
  5774. axis.removePlotBand = axis.removePlotBandOrLine;
  5775. axis.removePlotLine = axis.removePlotBandOrLine;
  5776. // register event listeners
  5777. for (eventType in events) {
  5778. addEvent(axis, eventType, events[eventType]);
  5779. }
  5780. // extend logarithmic axis
  5781. if (axis.isLog) {
  5782. axis.val2lin = log2lin;
  5783. axis.lin2val = lin2log;
  5784. }
  5785. },
  5786. /**
  5787. * Merge and set options
  5788. */
  5789. setOptions: function (userOptions) {
  5790. this.options = merge(
  5791. this.defaultOptions,
  5792. this.isXAxis ? {} : this.defaultYAxisOptions,
  5793. [this.defaultTopAxisOptions, this.defaultRightAxisOptions,
  5794. this.defaultBottomAxisOptions, this.defaultLeftAxisOptions][this.side],
  5795. merge(
  5796. defaultOptions[this.isXAxis ? 'xAxis' : 'yAxis'], // if set in setOptions (#1053)
  5797. userOptions
  5798. )
  5799. );
  5800. },
  5801. /**
  5802. * Update the axis with a new options structure
  5803. */
  5804. update: function (newOptions, redraw) {
  5805. var chart = this.chart;
  5806. newOptions = chart.options[this.xOrY + 'Axis'][this.options.index] = merge(this.userOptions, newOptions);
  5807. this.destroy();
  5808. this._addedPlotLB = false; // #1611
  5809. this.init(chart, newOptions);
  5810. chart.isDirtyBox = true;
  5811. if (pick(redraw, true)) {
  5812. chart.redraw();
  5813. }
  5814. },
  5815. /**
  5816. * Remove the axis from the chart
  5817. */
  5818. remove: function (redraw) {
  5819. var chart = this.chart,
  5820. key = this.xOrY + 'Axis'; // xAxis or yAxis
  5821. // Remove associated series
  5822. each(this.series, function (series) {
  5823. series.remove(false);
  5824. });
  5825. // Remove the axis
  5826. erase(chart.axes, this);
  5827. erase(chart[key], this);
  5828. chart.options[key].splice(this.options.index, 1);
  5829. each(chart[key], function (axis, i) { // Re-index, #1706
  5830. axis.options.index = i;
  5831. });
  5832. this.destroy();
  5833. chart.isDirtyBox = true;
  5834. if (pick(redraw, true)) {
  5835. chart.redraw();
  5836. }
  5837. },
  5838. /**
  5839. * The default label formatter. The context is a special config object for the label.
  5840. */
  5841. defaultLabelFormatter: function () {
  5842. var axis = this.axis,
  5843. value = this.value,
  5844. categories = axis.categories,
  5845. dateTimeLabelFormat = this.dateTimeLabelFormat,
  5846. numericSymbols = defaultOptions.lang.numericSymbols,
  5847. i = numericSymbols && numericSymbols.length,
  5848. multi,
  5849. ret,
  5850. formatOption = axis.options.labels.format,
  5851. // make sure the same symbol is added for all labels on a linear axis
  5852. numericSymbolDetector = axis.isLog ? value : axis.tickInterval;
  5853. if (formatOption) {
  5854. ret = format(formatOption, this);
  5855. } else if (categories) {
  5856. ret = value;
  5857. } else if (dateTimeLabelFormat) { // datetime axis
  5858. ret = dateFormat(dateTimeLabelFormat, value);
  5859. } else if (i && numericSymbolDetector >= 1000) {
  5860. // Decide whether we should add a numeric symbol like k (thousands) or M (millions).
  5861. // If we are to enable this in tooltip or other places as well, we can move this
  5862. // logic to the numberFormatter and enable it by a parameter.
  5863. while (i-- && ret === UNDEFINED) {
  5864. multi = Math.pow(1000, i + 1);
  5865. if (numericSymbolDetector >= multi && numericSymbols[i] !== null) {
  5866. ret = numberFormat(value / multi, -1) + numericSymbols[i];
  5867. }
  5868. }
  5869. }
  5870. if (ret === UNDEFINED) {
  5871. if (value >= 1000) { // add thousands separators
  5872. ret = numberFormat(value, 0);
  5873. } else { // small numbers
  5874. ret = numberFormat(value, -1);
  5875. }
  5876. }
  5877. return ret;
  5878. },
  5879. /**
  5880. * Get the minimum and maximum for the series of each axis
  5881. */
  5882. getSeriesExtremes: function () {
  5883. var axis = this,
  5884. chart = axis.chart,
  5885. stacks = axis.stacks,
  5886. posStack = [],
  5887. negStack = [],
  5888. stacksTouched = axis._stacksTouched = axis._stacksTouched + 1,
  5889. type,
  5890. i;
  5891. axis.hasVisibleSeries = false;
  5892. // reset dataMin and dataMax in case we're redrawing
  5893. axis.dataMin = axis.dataMax = null;
  5894. // loop through this axis' series
  5895. each(axis.series, function (series) {
  5896. if (series.visible || !chart.options.chart.ignoreHiddenSeries) {
  5897. var seriesOptions = series.options,
  5898. stacking,
  5899. posPointStack,
  5900. negPointStack,
  5901. stackKey,
  5902. stackOption,
  5903. negKey,
  5904. xData,
  5905. yData,
  5906. x,
  5907. y,
  5908. threshold = seriesOptions.threshold,
  5909. yDataLength,
  5910. activeYData = [],
  5911. seriesDataMin,
  5912. seriesDataMax,
  5913. activeCounter = 0;
  5914. axis.hasVisibleSeries = true;
  5915. // Validate threshold in logarithmic axes
  5916. if (axis.isLog && threshold <= 0) {
  5917. threshold = seriesOptions.threshold = null;
  5918. }
  5919. // Get dataMin and dataMax for X axes
  5920. if (axis.isXAxis) {
  5921. xData = series.xData;
  5922. if (xData.length) {
  5923. axis.dataMin = mathMin(pick(axis.dataMin, xData[0]), arrayMin(xData));
  5924. axis.dataMax = mathMax(pick(axis.dataMax, xData[0]), arrayMax(xData));
  5925. }
  5926. // Get dataMin and dataMax for Y axes, as well as handle stacking and processed data
  5927. } else {
  5928. var isNegative,
  5929. pointStack,
  5930. key,
  5931. cropped = series.cropped,
  5932. xExtremes = series.xAxis.getExtremes(),
  5933. //findPointRange,
  5934. //pointRange,
  5935. j,
  5936. hasModifyValue = !!series.modifyValue;
  5937. // Handle stacking
  5938. stacking = seriesOptions.stacking;
  5939. axis.usePercentage = stacking === 'percent';
  5940. // create a stack for this particular series type
  5941. if (stacking) {
  5942. stackOption = seriesOptions.stack;
  5943. stackKey = series.type + pick(stackOption, '');
  5944. negKey = '-' + stackKey;
  5945. series.stackKey = stackKey; // used in translate
  5946. posPointStack = posStack[stackKey] || []; // contains the total values for each x
  5947. posStack[stackKey] = posPointStack;
  5948. negPointStack = negStack[negKey] || [];
  5949. negStack[negKey] = negPointStack;
  5950. }
  5951. if (axis.usePercentage) {
  5952. axis.dataMin = 0;
  5953. axis.dataMax = 99;
  5954. }
  5955. // processData can alter series.pointRange, so this goes after
  5956. //findPointRange = series.pointRange === null;
  5957. xData = series.processedXData;
  5958. yData = series.processedYData;
  5959. yDataLength = yData.length;
  5960. // loop over the non-null y values and read them into a local array
  5961. for (i = 0; i < yDataLength; i++) {
  5962. x = xData[i];
  5963. y = yData[i];
  5964. // Read stacked values into a stack based on the x value,
  5965. // the sign of y and the stack key. Stacking is also handled for null values (#739)
  5966. if (stacking) {
  5967. isNegative = y < threshold;
  5968. pointStack = isNegative ? negPointStack : posPointStack;
  5969. key = isNegative ? negKey : stackKey;
  5970. // Set the stack value and y for extremes
  5971. if (defined(pointStack[x])) { // we're adding to the stack
  5972. pointStack[x] = correctFloat(pointStack[x] + y);
  5973. y = [y, pointStack[x]]; // consider both the actual value and the stack (#1376)
  5974. } else { // it's the first point in the stack
  5975. pointStack[x] = y;
  5976. }
  5977. // add the series
  5978. if (!stacks[key]) {
  5979. stacks[key] = {};
  5980. }
  5981. // If the StackItem is there, just update the values,
  5982. // if not, create one first
  5983. if (!stacks[key][x]) {
  5984. stacks[key][x] = new StackItem(axis, axis.options.stackLabels, isNegative, x, stackOption, stacking);
  5985. }
  5986. stacks[key][x].setTotal(pointStack[x]);
  5987. stacks[key][x].touched = stacksTouched;
  5988. }
  5989. // Handle non null values
  5990. if (y !== null && y !== UNDEFINED && (!axis.isLog || (y.length || y > 0))) {
  5991. // general hook, used for Highstock compare values feature
  5992. if (hasModifyValue) {
  5993. y = series.modifyValue(y);
  5994. }
  5995. // For points within the visible range, including the first point outside the
  5996. // visible range, consider y extremes
  5997. if (series.getExtremesFromAll || cropped || ((xData[i + 1] || x) >= xExtremes.min &&
  5998. (xData[i - 1] || x) <= xExtremes.max)) {
  5999. j = y.length;
  6000. if (j) { // array, like ohlc or range data
  6001. while (j--) {
  6002. if (y[j] !== null) {
  6003. activeYData[activeCounter++] = y[j];
  6004. }
  6005. }
  6006. } else {
  6007. activeYData[activeCounter++] = y;
  6008. }
  6009. }
  6010. }
  6011. }
  6012. // Get the dataMin and dataMax so far. If percentage is used, the min and max are
  6013. // always 0 and 100. If the length of activeYData is 0, continue with null values.
  6014. if (!axis.usePercentage && activeYData.length) {
  6015. series.dataMin = seriesDataMin = arrayMin(activeYData);
  6016. series.dataMax = seriesDataMax = arrayMax(activeYData);
  6017. axis.dataMin = mathMin(pick(axis.dataMin, seriesDataMin), seriesDataMin);
  6018. axis.dataMax = mathMax(pick(axis.dataMax, seriesDataMax), seriesDataMax);
  6019. }
  6020. // Adjust to threshold
  6021. if (defined(threshold)) {
  6022. if (axis.dataMin >= threshold) {
  6023. axis.dataMin = threshold;
  6024. axis.ignoreMinPadding = true;
  6025. } else if (axis.dataMax < threshold) {
  6026. axis.dataMax = threshold;
  6027. axis.ignoreMaxPadding = true;
  6028. }
  6029. }
  6030. }
  6031. }
  6032. });
  6033. // Destroy unused stacks (#1044)
  6034. for (type in stacks) {
  6035. for (i in stacks[type]) {
  6036. if (stacks[type][i].touched < stacksTouched) {
  6037. stacks[type][i].destroy();
  6038. delete stacks[type][i];
  6039. }
  6040. }
  6041. }
  6042. },
  6043. /**
  6044. * Translate from axis value to pixel position on the chart, or back
  6045. *
  6046. */
  6047. translate: function (val, backwards, cvsCoord, old, handleLog, pointPlacementBetween) {
  6048. var axis = this,
  6049. axisLength = axis.len,
  6050. sign = 1,
  6051. cvsOffset = 0,
  6052. localA = old ? axis.oldTransA : axis.transA,
  6053. localMin = old ? axis.oldMin : axis.min,
  6054. returnValue,
  6055. minPixelPadding = axis.minPixelPadding,
  6056. postTranslate = (axis.options.ordinal || (axis.isLog && handleLog)) && axis.lin2val;
  6057. if (!localA) {
  6058. localA = axis.transA;
  6059. }
  6060. // In vertical axes, the canvas coordinates start from 0 at the top like in
  6061. // SVG.
  6062. if (cvsCoord) {
  6063. sign *= -1; // canvas coordinates inverts the value
  6064. cvsOffset = axisLength;
  6065. }
  6066. // Handle reversed axis
  6067. if (axis.reversed) {
  6068. sign *= -1;
  6069. cvsOffset -= sign * axisLength;
  6070. }
  6071. // From pixels to value
  6072. if (backwards) { // reverse translation
  6073. val = val * sign + cvsOffset;
  6074. val -= minPixelPadding;
  6075. returnValue = val / localA + localMin; // from chart pixel to value
  6076. if (postTranslate) { // log and ordinal axes
  6077. returnValue = axis.lin2val(returnValue);
  6078. }
  6079. // From value to pixels
  6080. } else {
  6081. if (postTranslate) { // log and ordinal axes
  6082. val = axis.val2lin(val);
  6083. }
  6084. returnValue = sign * (val - localMin) * localA + cvsOffset + (sign * minPixelPadding) +
  6085. (pointPlacementBetween ? localA * axis.pointRange / 2 : 0);
  6086. }
  6087. return returnValue;
  6088. },
  6089. /**
  6090. * Utility method to translate an axis value to pixel position.
  6091. * @param {Number} value A value in terms of axis units
  6092. * @param {Boolean} paneCoordinates Whether to return the pixel coordinate relative to the chart
  6093. * or just the axis/pane itself.
  6094. */
  6095. toPixels: function (value, paneCoordinates) {
  6096. return this.translate(value, false, !this.horiz, null, true) + (paneCoordinates ? 0 : this.pos);
  6097. },
  6098. /*
  6099. * Utility method to translate a pixel position in to an axis value
  6100. * @param {Number} pixel The pixel value coordinate
  6101. * @param {Boolean} paneCoordiantes Whether the input pixel is relative to the chart or just the
  6102. * axis/pane itself.
  6103. */
  6104. toValue: function (pixel, paneCoordinates) {
  6105. return this.translate(pixel - (paneCoordinates ? 0 : this.pos), true, !this.horiz, null, true);
  6106. },
  6107. /**
  6108. * Create the path for a plot line that goes from the given value on
  6109. * this axis, across the plot to the opposite side
  6110. * @param {Number} value
  6111. * @param {Number} lineWidth Used for calculation crisp line
  6112. * @param {Number] old Use old coordinates (for resizing and rescaling)
  6113. */
  6114. getPlotLinePath: function (value, lineWidth, old, force) {
  6115. var axis = this,
  6116. chart = axis.chart,
  6117. axisLeft = axis.left,
  6118. axisTop = axis.top,
  6119. x1,
  6120. y1,
  6121. x2,
  6122. y2,
  6123. translatedValue = axis.translate(value, null, null, old),
  6124. cHeight = (old && chart.oldChartHeight) || chart.chartHeight,
  6125. cWidth = (old && chart.oldChartWidth) || chart.chartWidth,
  6126. skip,
  6127. transB = axis.transB;
  6128. x1 = x2 = mathRound(translatedValue + transB);
  6129. y1 = y2 = mathRound(cHeight - translatedValue - transB);
  6130. if (isNaN(translatedValue)) { // no min or max
  6131. skip = true;
  6132. } else if (axis.horiz) {
  6133. y1 = axisTop;
  6134. y2 = cHeight - axis.bottom;
  6135. if (x1 < axisLeft || x1 > axisLeft + axis.width) {
  6136. skip = true;
  6137. }
  6138. } else {
  6139. x1 = axisLeft;
  6140. x2 = cWidth - axis.right;
  6141. if (y1 < axisTop || y1 > axisTop + axis.height) {
  6142. skip = true;
  6143. }
  6144. }
  6145. return skip && !force ?
  6146. null :
  6147. chart.renderer.crispLine([M, x1, y1, L, x2, y2], lineWidth || 0);
  6148. },
  6149. /**
  6150. * Create the path for a plot band
  6151. */
  6152. getPlotBandPath: function (from, to) {
  6153. var toPath = this.getPlotLinePath(to),
  6154. path = this.getPlotLinePath(from);
  6155. if (path && toPath) {
  6156. path.push(
  6157. toPath[4],
  6158. toPath[5],
  6159. toPath[1],
  6160. toPath[2]
  6161. );
  6162. } else { // outside the axis area
  6163. path = null;
  6164. }
  6165. return path;
  6166. },
  6167. /**
  6168. * Set the tick positions of a linear axis to round values like whole tens or every five.
  6169. */
  6170. getLinearTickPositions: function (tickInterval, min, max) {
  6171. var pos,
  6172. lastPos,
  6173. roundedMin = correctFloat(mathFloor(min / tickInterval) * tickInterval),
  6174. roundedMax = correctFloat(mathCeil(max / tickInterval) * tickInterval),
  6175. tickPositions = [];
  6176. // Populate the intermediate values
  6177. pos = roundedMin;
  6178. while (pos <= roundedMax) {
  6179. // Place the tick on the rounded value
  6180. tickPositions.push(pos);
  6181. // Always add the raw tickInterval, not the corrected one.
  6182. pos = correctFloat(pos + tickInterval);
  6183. // If the interval is not big enough in the current min - max range to actually increase
  6184. // the loop variable, we need to break out to prevent endless loop. Issue #619
  6185. if (pos === lastPos) {
  6186. break;
  6187. }
  6188. // Record the last value
  6189. lastPos = pos;
  6190. }
  6191. return tickPositions;
  6192. },
  6193. /**
  6194. * Set the tick positions of a logarithmic axis
  6195. */
  6196. getLogTickPositions: function (interval, min, max, minor) {
  6197. var axis = this,
  6198. options = axis.options,
  6199. axisLength = axis.len,
  6200. // Since we use this method for both major and minor ticks,
  6201. // use a local variable and return the result
  6202. positions = [];
  6203. // Reset
  6204. if (!minor) {
  6205. axis._minorAutoInterval = null;
  6206. }
  6207. // First case: All ticks fall on whole logarithms: 1, 10, 100 etc.
  6208. if (interval >= 0.5) {
  6209. interval = mathRound(interval);
  6210. positions = axis.getLinearTickPositions(interval, min, max);
  6211. // Second case: We need intermediary ticks. For example
  6212. // 1, 2, 4, 6, 8, 10, 20, 40 etc.
  6213. } else if (interval >= 0.08) {
  6214. var roundedMin = mathFloor(min),
  6215. intermediate,
  6216. i,
  6217. j,
  6218. len,
  6219. pos,
  6220. lastPos,
  6221. break2;
  6222. if (interval > 0.3) {
  6223. intermediate = [1, 2, 4];
  6224. } else if (interval > 0.15) { // 0.2 equals five minor ticks per 1, 10, 100 etc
  6225. intermediate = [1, 2, 4, 6, 8];
  6226. } else { // 0.1 equals ten minor ticks per 1, 10, 100 etc
  6227. intermediate = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  6228. }
  6229. for (i = roundedMin; i < max + 1 && !break2; i++) {
  6230. len = intermediate.length;
  6231. for (j = 0; j < len && !break2; j++) {
  6232. pos = log2lin(lin2log(i) * intermediate[j]);
  6233. if (pos > min && (!minor || lastPos <= max)) { // #1670
  6234. positions.push(lastPos);
  6235. }
  6236. if (lastPos > max) {
  6237. break2 = true;
  6238. }
  6239. lastPos = pos;
  6240. }
  6241. }
  6242. // Third case: We are so deep in between whole logarithmic values that
  6243. // we might as well handle the tick positions like a linear axis. For
  6244. // example 1.01, 1.02, 1.03, 1.04.
  6245. } else {
  6246. var realMin = lin2log(min),
  6247. realMax = lin2log(max),
  6248. tickIntervalOption = options[minor ? 'minorTickInterval' : 'tickInterval'],
  6249. filteredTickIntervalOption = tickIntervalOption === 'auto' ? null : tickIntervalOption,
  6250. tickPixelIntervalOption = options.tickPixelInterval / (minor ? 5 : 1),
  6251. totalPixelLength = minor ? axisLength / axis.tickPositions.length : axisLength;
  6252. interval = pick(
  6253. filteredTickIntervalOption,
  6254. axis._minorAutoInterval,
  6255. (realMax - realMin) * tickPixelIntervalOption / (totalPixelLength || 1)
  6256. );
  6257. interval = normalizeTickInterval(
  6258. interval,
  6259. null,
  6260. math.pow(10, mathFloor(math.log(interval) / math.LN10))
  6261. );
  6262. positions = map(axis.getLinearTickPositions(
  6263. interval,
  6264. realMin,
  6265. realMax
  6266. ), log2lin);
  6267. if (!minor) {
  6268. axis._minorAutoInterval = interval / 5;
  6269. }
  6270. }
  6271. // Set the axis-level tickInterval variable
  6272. if (!minor) {
  6273. axis.tickInterval = interval;
  6274. }
  6275. return positions;
  6276. },
  6277. /**
  6278. * Return the minor tick positions. For logarithmic axes, reuse the same logic
  6279. * as for major ticks.
  6280. */
  6281. getMinorTickPositions: function () {
  6282. var axis = this,
  6283. options = axis.options,
  6284. tickPositions = axis.tickPositions,
  6285. minorTickInterval = axis.minorTickInterval,
  6286. minorTickPositions = [],
  6287. pos,
  6288. i,
  6289. len;
  6290. if (axis.isLog) {
  6291. len = tickPositions.length;
  6292. for (i = 1; i < len; i++) {
  6293. minorTickPositions = minorTickPositions.concat(
  6294. axis.getLogTickPositions(minorTickInterval, tickPositions[i - 1], tickPositions[i], true)
  6295. );
  6296. }
  6297. } else if (axis.isDatetimeAxis && options.minorTickInterval === 'auto') { // #1314
  6298. minorTickPositions = minorTickPositions.concat(
  6299. getTimeTicks(
  6300. normalizeTimeTickInterval(minorTickInterval),
  6301. axis.min,
  6302. axis.max,
  6303. options.startOfWeek
  6304. )
  6305. );
  6306. if (minorTickPositions[0] < axis.min) {
  6307. minorTickPositions.shift();
  6308. }
  6309. } else {
  6310. for (pos = axis.min + (tickPositions[0] - axis.min) % minorTickInterval; pos <= axis.max; pos += minorTickInterval) {
  6311. minorTickPositions.push(pos);
  6312. }
  6313. }
  6314. return minorTickPositions;
  6315. },
  6316. /**
  6317. * Adjust the min and max for the minimum range. Keep in mind that the series data is
  6318. * not yet processed, so we don't have information on data cropping and grouping, or
  6319. * updated axis.pointRange or series.pointRange. The data can't be processed until
  6320. * we have finally established min and max.
  6321. */
  6322. adjustForMinRange: function () {
  6323. var axis = this,
  6324. options = axis.options,
  6325. min = axis.min,
  6326. max = axis.max,
  6327. zoomOffset,
  6328. spaceAvailable = axis.dataMax - axis.dataMin >= axis.minRange,
  6329. closestDataRange,
  6330. i,
  6331. distance,
  6332. xData,
  6333. loopLength,
  6334. minArgs,
  6335. maxArgs;
  6336. // Set the automatic minimum range based on the closest point distance
  6337. if (axis.isXAxis && axis.minRange === UNDEFINED && !axis.isLog) {
  6338. if (defined(options.min) || defined(options.max)) {
  6339. axis.minRange = null; // don't do this again
  6340. } else {
  6341. // Find the closest distance between raw data points, as opposed to
  6342. // closestPointRange that applies to processed points (cropped and grouped)
  6343. each(axis.series, function (series) {
  6344. xData = series.xData;
  6345. loopLength = series.xIncrement ? 1 : xData.length - 1;
  6346. for (i = loopLength; i > 0; i--) {
  6347. distance = xData[i] - xData[i - 1];
  6348. if (closestDataRange === UNDEFINED || distance < closestDataRange) {
  6349. closestDataRange = distance;
  6350. }
  6351. }
  6352. });
  6353. axis.minRange = mathMin(closestDataRange * 5, axis.dataMax - axis.dataMin);
  6354. }
  6355. }
  6356. // if minRange is exceeded, adjust
  6357. if (max - min < axis.minRange) {
  6358. var minRange = axis.minRange;
  6359. zoomOffset = (minRange - max + min) / 2;
  6360. // if min and max options have been set, don't go beyond it
  6361. minArgs = [min - zoomOffset, pick(options.min, min - zoomOffset)];
  6362. if (spaceAvailable) { // if space is available, stay within the data range
  6363. minArgs[2] = axis.dataMin;
  6364. }
  6365. min = arrayMax(minArgs);
  6366. maxArgs = [min + minRange, pick(options.max, min + minRange)];
  6367. if (spaceAvailable) { // if space is availabe, stay within the data range
  6368. maxArgs[2] = axis.dataMax;
  6369. }
  6370. max = arrayMin(maxArgs);
  6371. // now if the max is adjusted, adjust the min back
  6372. if (max - min < minRange) {
  6373. minArgs[0] = max - minRange;
  6374. minArgs[1] = pick(options.min, max - minRange);
  6375. min = arrayMax(minArgs);
  6376. }
  6377. }
  6378. // Record modified extremes
  6379. axis.min = min;
  6380. axis.max = max;
  6381. },
  6382. /**
  6383. * Update translation information
  6384. */
  6385. setAxisTranslation: function (saveOld) {
  6386. var axis = this,
  6387. range = axis.max - axis.min,
  6388. pointRange = 0,
  6389. closestPointRange,
  6390. minPointOffset = 0,
  6391. pointRangePadding = 0,
  6392. linkedParent = axis.linkedParent,
  6393. ordinalCorrection,
  6394. transA = axis.transA;
  6395. // adjust translation for padding
  6396. if (axis.isXAxis) {
  6397. if (linkedParent) {
  6398. minPointOffset = linkedParent.minPointOffset;
  6399. pointRangePadding = linkedParent.pointRangePadding;
  6400. } else {
  6401. each(axis.series, function (series) {
  6402. var seriesPointRange = series.pointRange,
  6403. pointPlacement = series.options.pointPlacement,
  6404. seriesClosestPointRange = series.closestPointRange;
  6405. if (seriesPointRange > range) { // #1446
  6406. seriesPointRange = 0;
  6407. }
  6408. pointRange = mathMax(pointRange, seriesPointRange);
  6409. // minPointOffset is the value padding to the left of the axis in order to make
  6410. // room for points with a pointRange, typically columns. When the pointPlacement option
  6411. // is 'between' or 'on', this padding does not apply.
  6412. minPointOffset = mathMax(
  6413. minPointOffset,
  6414. pointPlacement ? 0 : seriesPointRange / 2
  6415. );
  6416. // Determine the total padding needed to the length of the axis to make room for the
  6417. // pointRange. If the series' pointPlacement is 'on', no padding is added.
  6418. pointRangePadding = mathMax(
  6419. pointRangePadding,
  6420. pointPlacement === 'on' ? 0 : seriesPointRange
  6421. );
  6422. // Set the closestPointRange
  6423. if (!series.noSharedTooltip && defined(seriesClosestPointRange)) {
  6424. closestPointRange = defined(closestPointRange) ?
  6425. mathMin(closestPointRange, seriesClosestPointRange) :
  6426. seriesClosestPointRange;
  6427. }
  6428. });
  6429. }
  6430. // Record minPointOffset and pointRangePadding
  6431. ordinalCorrection = axis.ordinalSlope && closestPointRange ? axis.ordinalSlope / closestPointRange : 1; // #988, #1853
  6432. axis.minPointOffset = minPointOffset = minPointOffset * ordinalCorrection;
  6433. axis.pointRangePadding = pointRangePadding = pointRangePadding * ordinalCorrection;
  6434. // pointRange means the width reserved for each point, like in a column chart
  6435. axis.pointRange = mathMin(pointRange, range);
  6436. // closestPointRange means the closest distance between points. In columns
  6437. // it is mostly equal to pointRange, but in lines pointRange is 0 while closestPointRange
  6438. // is some other value
  6439. axis.closestPointRange = closestPointRange;
  6440. }
  6441. // Secondary values
  6442. if (saveOld) {
  6443. axis.oldTransA = transA;
  6444. }
  6445. axis.translationSlope = axis.transA = transA = axis.len / ((range + pointRangePadding) || 1);
  6446. axis.transB = axis.horiz ? axis.left : axis.bottom; // translation addend
  6447. axis.minPixelPadding = transA * minPointOffset;
  6448. },
  6449. /**
  6450. * Set the tick positions to round values and optionally extend the extremes
  6451. * to the nearest tick
  6452. */
  6453. setTickPositions: function (secondPass) {
  6454. var axis = this,
  6455. chart = axis.chart,
  6456. options = axis.options,
  6457. isLog = axis.isLog,
  6458. isDatetimeAxis = axis.isDatetimeAxis,
  6459. isXAxis = axis.isXAxis,
  6460. isLinked = axis.isLinked,
  6461. tickPositioner = axis.options.tickPositioner,
  6462. magnitude,
  6463. maxPadding = options.maxPadding,
  6464. minPadding = options.minPadding,
  6465. length,
  6466. linkedParentExtremes,
  6467. tickIntervalOption = options.tickInterval,
  6468. minTickIntervalOption = options.minTickInterval,
  6469. tickPixelIntervalOption = options.tickPixelInterval,
  6470. tickPositions,
  6471. categories = axis.categories;
  6472. // linked axis gets the extremes from the parent axis
  6473. if (isLinked) {
  6474. axis.linkedParent = chart[isXAxis ? 'xAxis' : 'yAxis'][options.linkedTo];
  6475. linkedParentExtremes = axis.linkedParent.getExtremes();
  6476. axis.min = pick(linkedParentExtremes.min, linkedParentExtremes.dataMin);
  6477. axis.max = pick(linkedParentExtremes.max, linkedParentExtremes.dataMax);
  6478. if (options.type !== axis.linkedParent.options.type) {
  6479. error(11, 1); // Can't link axes of different type
  6480. }
  6481. } else { // initial min and max from the extreme data values
  6482. axis.min = pick(axis.userMin, options.min, axis.dataMin);
  6483. axis.max = pick(axis.userMax, options.max, axis.dataMax);
  6484. }
  6485. if (isLog) {
  6486. if (!secondPass && mathMin(axis.min, pick(axis.dataMin, axis.min)) <= 0) { // #978
  6487. error(10, 1); // Can't plot negative values on log axis
  6488. }
  6489. axis.min = correctFloat(log2lin(axis.min)); // correctFloat cures #934
  6490. axis.max = correctFloat(log2lin(axis.max));
  6491. }
  6492. // handle zoomed range
  6493. if (axis.range) {
  6494. axis.userMin = axis.min = mathMax(axis.min, axis.max - axis.range); // #618
  6495. axis.userMax = axis.max;
  6496. if (secondPass) {
  6497. axis.range = null; // don't use it when running setExtremes
  6498. }
  6499. }
  6500. // Hook for adjusting this.min and this.max. Used by bubble series.
  6501. if (axis.beforePadding) {
  6502. axis.beforePadding();
  6503. }
  6504. // adjust min and max for the minimum range
  6505. axis.adjustForMinRange();
  6506. // Pad the values to get clear of the chart's edges. To avoid tickInterval taking the padding
  6507. // into account, we do this after computing tick interval (#1337).
  6508. if (!categories && !axis.usePercentage && !isLinked && defined(axis.min) && defined(axis.max)) {
  6509. length = axis.max - axis.min;
  6510. if (length) {
  6511. if (!defined(options.min) && !defined(axis.userMin) && minPadding && (axis.dataMin < 0 || !axis.ignoreMinPadding)) {
  6512. axis.min -= length * minPadding;
  6513. }
  6514. if (!defined(options.max) && !defined(axis.userMax) && maxPadding && (axis.dataMax > 0 || !axis.ignoreMaxPadding)) {
  6515. axis.max += length * maxPadding;
  6516. }
  6517. }
  6518. }
  6519. // get tickInterval
  6520. if (axis.min === axis.max || axis.min === undefined || axis.max === undefined) {
  6521. axis.tickInterval = 1;
  6522. } else if (isLinked && !tickIntervalOption &&
  6523. tickPixelIntervalOption === axis.linkedParent.options.tickPixelInterval) {
  6524. axis.tickInterval = axis.linkedParent.tickInterval;
  6525. } else {
  6526. axis.tickInterval = pick(
  6527. tickIntervalOption,
  6528. categories ? // for categoried axis, 1 is default, for linear axis use tickPix
  6529. 1 :
  6530. (axis.max - axis.min) * tickPixelIntervalOption / (axis.len || 1)
  6531. );
  6532. }
  6533. // Now we're finished detecting min and max, crop and group series data. This
  6534. // is in turn needed in order to find tick positions in ordinal axes.
  6535. if (isXAxis && !secondPass) {
  6536. each(axis.series, function (series) {
  6537. series.processData(axis.min !== axis.oldMin || axis.max !== axis.oldMax);
  6538. });
  6539. }
  6540. // set the translation factor used in translate function
  6541. axis.setAxisTranslation(true);
  6542. // hook for ordinal axes and radial axes
  6543. if (axis.beforeSetTickPositions) {
  6544. axis.beforeSetTickPositions();
  6545. }
  6546. // hook for extensions, used in Highstock ordinal axes
  6547. if (axis.postProcessTickInterval) {
  6548. axis.tickInterval = axis.postProcessTickInterval(axis.tickInterval);
  6549. }
  6550. // Before normalizing the tick interval, handle minimum tick interval. This applies only if tickInterval is not defined.
  6551. if (!tickIntervalOption && axis.tickInterval < minTickIntervalOption) {
  6552. axis.tickInterval = minTickIntervalOption;
  6553. }
  6554. // for linear axes, get magnitude and normalize the interval
  6555. if (!isDatetimeAxis && !isLog) { // linear
  6556. magnitude = math.pow(10, mathFloor(math.log(axis.tickInterval) / math.LN10));
  6557. if (!tickIntervalOption) {
  6558. axis.tickInterval = normalizeTickInterval(axis.tickInterval, null, magnitude, options);
  6559. }
  6560. }
  6561. // get minorTickInterval
  6562. axis.minorTickInterval = options.minorTickInterval === 'auto' && axis.tickInterval ?
  6563. axis.tickInterval / 5 : options.minorTickInterval;
  6564. // find the tick positions
  6565. axis.tickPositions = tickPositions = options.tickPositions ?
  6566. [].concat(options.tickPositions) : // Work on a copy (#1565)
  6567. (tickPositioner && tickPositioner.apply(axis, [axis.min, axis.max]));
  6568. if (!tickPositions) {
  6569. if (isDatetimeAxis) {
  6570. tickPositions = (axis.getNonLinearTimeTicks || getTimeTicks)(
  6571. normalizeTimeTickInterval(axis.tickInterval, options.units),
  6572. axis.min,
  6573. axis.max,
  6574. options.startOfWeek,
  6575. axis.ordinalPositions,
  6576. axis.closestPointRange,
  6577. true
  6578. );
  6579. } else if (isLog) {
  6580. tickPositions = axis.getLogTickPositions(axis.tickInterval, axis.min, axis.max);
  6581. } else {
  6582. tickPositions = axis.getLinearTickPositions(axis.tickInterval, axis.min, axis.max);
  6583. }
  6584. axis.tickPositions = tickPositions;
  6585. }
  6586. if (!isLinked) {
  6587. // reset min/max or remove extremes based on start/end on tick
  6588. var roundedMin = tickPositions[0],
  6589. roundedMax = tickPositions[tickPositions.length - 1],
  6590. minPointOffset = axis.minPointOffset || 0,
  6591. singlePad;
  6592. if (options.startOnTick) {
  6593. axis.min = roundedMin;
  6594. } else if (axis.min - minPointOffset > roundedMin) {
  6595. tickPositions.shift();
  6596. }
  6597. if (options.endOnTick) {
  6598. axis.max = roundedMax;
  6599. } else if (axis.max + minPointOffset < roundedMax) {
  6600. tickPositions.pop();
  6601. }
  6602. // When there is only one point, or all points have the same value on this axis, then min
  6603. // and max are equal and tickPositions.length is 1. In this case, add some padding
  6604. // in order to center the point, but leave it with one tick. #1337.
  6605. if (tickPositions.length === 1) {
  6606. singlePad = 0.001; // The lowest possible number to avoid extra padding on columns
  6607. axis.min -= singlePad;
  6608. axis.max += singlePad;
  6609. }
  6610. }
  6611. },
  6612. /**
  6613. * Set the max ticks of either the x and y axis collection
  6614. */
  6615. setMaxTicks: function () {
  6616. var chart = this.chart,
  6617. maxTicks = chart.maxTicks || {},
  6618. tickPositions = this.tickPositions,
  6619. key = this._maxTicksKey = [this.xOrY, this.pos, this.len].join('-');
  6620. if (!this.isLinked && !this.isDatetimeAxis && tickPositions && tickPositions.length > (maxTicks[key] || 0) && this.options.alignTicks !== false) {
  6621. maxTicks[key] = tickPositions.length;
  6622. }
  6623. chart.maxTicks = maxTicks;
  6624. },
  6625. /**
  6626. * When using multiple axes, adjust the number of ticks to match the highest
  6627. * number of ticks in that group
  6628. */
  6629. adjustTickAmount: function () {
  6630. var axis = this,
  6631. chart = axis.chart,
  6632. key = axis._maxTicksKey,
  6633. tickPositions = axis.tickPositions,
  6634. maxTicks = chart.maxTicks;
  6635. if (maxTicks && maxTicks[key] && !axis.isDatetimeAxis && !axis.categories && !axis.isLinked && axis.options.alignTicks !== false) { // only apply to linear scale
  6636. var oldTickAmount = axis.tickAmount,
  6637. calculatedTickAmount = tickPositions.length,
  6638. tickAmount;
  6639. // set the axis-level tickAmount to use below
  6640. axis.tickAmount = tickAmount = maxTicks[key];
  6641. if (calculatedTickAmount < tickAmount) {
  6642. while (tickPositions.length < tickAmount) {
  6643. tickPositions.push(correctFloat(
  6644. tickPositions[tickPositions.length - 1] + axis.tickInterval
  6645. ));
  6646. }
  6647. axis.transA *= (calculatedTickAmount - 1) / (tickAmount - 1);
  6648. axis.max = tickPositions[tickPositions.length - 1];
  6649. }
  6650. if (defined(oldTickAmount) && tickAmount !== oldTickAmount) {
  6651. axis.isDirty = true;
  6652. }
  6653. }
  6654. },
  6655. /**
  6656. * Set the scale based on data min and max, user set min and max or options
  6657. *
  6658. */
  6659. setScale: function () {
  6660. var axis = this,
  6661. stacks = axis.stacks,
  6662. type,
  6663. i,
  6664. isDirtyData,
  6665. isDirtyAxisLength;
  6666. axis.oldMin = axis.min;
  6667. axis.oldMax = axis.max;
  6668. axis.oldAxisLength = axis.len;
  6669. // set the new axisLength
  6670. axis.setAxisSize();
  6671. //axisLength = horiz ? axisWidth : axisHeight;
  6672. isDirtyAxisLength = axis.len !== axis.oldAxisLength;
  6673. // is there new data?
  6674. each(axis.series, function (series) {
  6675. if (series.isDirtyData || series.isDirty ||
  6676. series.xAxis.isDirty) { // when x axis is dirty, we need new data extremes for y as well
  6677. isDirtyData = true;
  6678. }
  6679. });
  6680. // do we really need to go through all this?
  6681. if (isDirtyAxisLength || isDirtyData || axis.isLinked || axis.forceRedraw ||
  6682. axis.userMin !== axis.oldUserMin || axis.userMax !== axis.oldUserMax) {
  6683. axis.forceRedraw = false;
  6684. // get data extremes if needed
  6685. axis.getSeriesExtremes();
  6686. // get fixed positions based on tickInterval
  6687. axis.setTickPositions();
  6688. // record old values to decide whether a rescale is necessary later on (#540)
  6689. axis.oldUserMin = axis.userMin;
  6690. axis.oldUserMax = axis.userMax;
  6691. // Mark as dirty if it is not already set to dirty and extremes have changed. #595.
  6692. if (!axis.isDirty) {
  6693. axis.isDirty = isDirtyAxisLength || axis.min !== axis.oldMin || axis.max !== axis.oldMax;
  6694. }
  6695. }
  6696. // reset stacks
  6697. if (!axis.isXAxis) {
  6698. for (type in stacks) {
  6699. for (i in stacks[type]) {
  6700. stacks[type][i].cum = stacks[type][i].total;
  6701. }
  6702. }
  6703. }
  6704. // Set the maximum tick amount
  6705. axis.setMaxTicks();
  6706. },
  6707. /**
  6708. * Set the extremes and optionally redraw
  6709. * @param {Number} newMin
  6710. * @param {Number} newMax
  6711. * @param {Boolean} redraw
  6712. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  6713. * configuration
  6714. * @param {Object} eventArguments
  6715. *
  6716. */
  6717. setExtremes: function (newMin, newMax, redraw, animation, eventArguments) {
  6718. var axis = this,
  6719. chart = axis.chart;
  6720. redraw = pick(redraw, true); // defaults to true
  6721. // Extend the arguments with min and max
  6722. eventArguments = extend(eventArguments, {
  6723. min: newMin,
  6724. max: newMax
  6725. });
  6726. // Fire the event
  6727. fireEvent(axis, 'setExtremes', eventArguments, function () { // the default event handler
  6728. axis.userMin = newMin;
  6729. axis.userMax = newMax;
  6730. // Mark for running afterSetExtremes
  6731. axis.isDirtyExtremes = true;
  6732. // redraw
  6733. if (redraw) {
  6734. chart.redraw(animation);
  6735. }
  6736. });
  6737. },
  6738. /**
  6739. * Overridable method for zooming chart. Pulled out in a separate method to allow overriding
  6740. * in stock charts.
  6741. */
  6742. zoom: function (newMin, newMax) {
  6743. // Prevent pinch zooming out of range
  6744. if (!this.allowZoomOutside) {
  6745. if (newMin <= this.dataMin) {
  6746. newMin = UNDEFINED;
  6747. }
  6748. if (newMax >= this.dataMax) {
  6749. newMax = UNDEFINED;
  6750. }
  6751. }
  6752. // In full view, displaying the reset zoom button is not required
  6753. this.displayBtn = newMin !== UNDEFINED || newMax !== UNDEFINED;
  6754. // Do it
  6755. this.setExtremes(
  6756. newMin,
  6757. newMax,
  6758. false,
  6759. UNDEFINED,
  6760. { trigger: 'zoom' }
  6761. );
  6762. return true;
  6763. },
  6764. /**
  6765. * Update the axis metrics
  6766. */
  6767. setAxisSize: function () {
  6768. var chart = this.chart,
  6769. options = this.options,
  6770. offsetLeft = options.offsetLeft || 0,
  6771. offsetRight = options.offsetRight || 0,
  6772. horiz = this.horiz,
  6773. width,
  6774. height,
  6775. top,
  6776. left;
  6777. // Expose basic values to use in Series object and navigator
  6778. this.left = left = pick(options.left, chart.plotLeft + offsetLeft);
  6779. this.top = top = pick(options.top, chart.plotTop);
  6780. this.width = width = pick(options.width, chart.plotWidth - offsetLeft + offsetRight);
  6781. this.height = height = pick(options.height, chart.plotHeight);
  6782. this.bottom = chart.chartHeight - height - top;
  6783. this.right = chart.chartWidth - width - left;
  6784. // Direction agnostic properties
  6785. this.len = mathMax(horiz ? width : height, 0); // mathMax fixes #905
  6786. this.pos = horiz ? left : top; // distance from SVG origin
  6787. },
  6788. /**
  6789. * Get the actual axis extremes
  6790. */
  6791. getExtremes: function () {
  6792. var axis = this,
  6793. isLog = axis.isLog;
  6794. return {
  6795. min: isLog ? correctFloat(lin2log(axis.min)) : axis.min,
  6796. max: isLog ? correctFloat(lin2log(axis.max)) : axis.max,
  6797. dataMin: axis.dataMin,
  6798. dataMax: axis.dataMax,
  6799. userMin: axis.userMin,
  6800. userMax: axis.userMax
  6801. };
  6802. },
  6803. /**
  6804. * Get the zero plane either based on zero or on the min or max value.
  6805. * Used in bar and area plots
  6806. */
  6807. getThreshold: function (threshold) {
  6808. var axis = this,
  6809. isLog = axis.isLog;
  6810. var realMin = isLog ? lin2log(axis.min) : axis.min,
  6811. realMax = isLog ? lin2log(axis.max) : axis.max;
  6812. if (realMin > threshold || threshold === null) {
  6813. threshold = realMin;
  6814. } else if (realMax < threshold) {
  6815. threshold = realMax;
  6816. }
  6817. return axis.translate(threshold, 0, 1, 0, 1);
  6818. },
  6819. addPlotBand: function (options) {
  6820. this.addPlotBandOrLine(options, 'plotBands');
  6821. },
  6822. addPlotLine: function (options) {
  6823. this.addPlotBandOrLine(options, 'plotLines');
  6824. },
  6825. /**
  6826. * Add a plot band or plot line after render time
  6827. *
  6828. * @param options {Object} The plotBand or plotLine configuration object
  6829. */
  6830. addPlotBandOrLine: function (options, coll) {
  6831. var obj = new PlotLineOrBand(this, options).render(),
  6832. userOptions = this.userOptions;
  6833. // Add it to the user options for exporting and Axis.update
  6834. if (coll) {
  6835. userOptions[coll] = userOptions[coll] || [];
  6836. userOptions[coll].push(options);
  6837. }
  6838. this.plotLinesAndBands.push(obj);
  6839. return obj;
  6840. },
  6841. /**
  6842. * Render the tick labels to a preliminary position to get their sizes
  6843. */
  6844. getOffset: function () {
  6845. var axis = this,
  6846. chart = axis.chart,
  6847. renderer = chart.renderer,
  6848. options = axis.options,
  6849. tickPositions = axis.tickPositions,
  6850. ticks = axis.ticks,
  6851. horiz = axis.horiz,
  6852. side = axis.side,
  6853. invertedSide = chart.inverted ? [1, 0, 3, 2][side] : side,
  6854. hasData,
  6855. showAxis,
  6856. titleOffset = 0,
  6857. titleOffsetOption,
  6858. titleMargin = 0,
  6859. axisTitleOptions = options.title,
  6860. labelOptions = options.labels,
  6861. labelOffset = 0, // reset
  6862. axisOffset = chart.axisOffset,
  6863. clipOffset = chart.clipOffset,
  6864. directionFactor = [-1, 1, 1, -1][side],
  6865. n;
  6866. // For reuse in Axis.render
  6867. axis.hasData = hasData = (axis.hasVisibleSeries || (defined(axis.min) && defined(axis.max) && !!tickPositions));
  6868. axis.showAxis = showAxis = hasData || pick(options.showEmpty, true);
  6869. // Create the axisGroup and gridGroup elements on first iteration
  6870. if (!axis.axisGroup) {
  6871. axis.gridGroup = renderer.g('grid')
  6872. .attr({ zIndex: options.gridZIndex || 1 })
  6873. .add();
  6874. axis.axisGroup = renderer.g('axis')
  6875. .attr({ zIndex: options.zIndex || 2 })
  6876. .add();
  6877. axis.labelGroup = renderer.g('axis-labels')
  6878. .attr({ zIndex: labelOptions.zIndex || 7 })
  6879. .add();
  6880. }
  6881. if (hasData || axis.isLinked) {
  6882. each(tickPositions, function (pos) {
  6883. if (!ticks[pos]) {
  6884. ticks[pos] = new Tick(axis, pos);
  6885. } else {
  6886. ticks[pos].addLabel(); // update labels depending on tick interval
  6887. }
  6888. });
  6889. each(tickPositions, function (pos) {
  6890. // left side must be align: right and right side must have align: left for labels
  6891. if (side === 0 || side === 2 || { 1: 'left', 3: 'right' }[side] === labelOptions.align) {
  6892. // get the highest offset
  6893. labelOffset = mathMax(
  6894. ticks[pos].getLabelSize(),
  6895. labelOffset
  6896. );
  6897. }
  6898. });
  6899. if (axis.staggerLines) {
  6900. labelOffset += (axis.staggerLines - 1) * 16;
  6901. }
  6902. } else { // doesn't have data
  6903. for (n in ticks) {
  6904. ticks[n].destroy();
  6905. delete ticks[n];
  6906. }
  6907. }
  6908. if (axisTitleOptions && axisTitleOptions.text && axisTitleOptions.enabled !== false) {
  6909. if (!axis.axisTitle) {
  6910. axis.axisTitle = renderer.text(
  6911. axisTitleOptions.text,
  6912. 0,
  6913. 0,
  6914. axisTitleOptions.useHTML
  6915. )
  6916. .attr({
  6917. zIndex: 7,
  6918. rotation: axisTitleOptions.rotation || 0,
  6919. align:
  6920. axisTitleOptions.textAlign ||
  6921. { low: 'left', middle: 'center', high: 'right' }[axisTitleOptions.align]
  6922. })
  6923. .css(axisTitleOptions.style)
  6924. .add(axis.axisGroup);
  6925. axis.axisTitle.isNew = true;
  6926. }
  6927. if (showAxis) {
  6928. titleOffset = axis.axisTitle.getBBox()[horiz ? 'height' : 'width'];
  6929. titleMargin = pick(axisTitleOptions.margin, horiz ? 5 : 10);
  6930. titleOffsetOption = axisTitleOptions.offset;
  6931. }
  6932. // hide or show the title depending on whether showEmpty is set
  6933. axis.axisTitle[showAxis ? 'show' : 'hide']();
  6934. }
  6935. // handle automatic or user set offset
  6936. axis.offset = directionFactor * pick(options.offset, axisOffset[side]);
  6937. axis.axisTitleMargin =
  6938. pick(titleOffsetOption,
  6939. labelOffset + titleMargin +
  6940. (side !== 2 && labelOffset && directionFactor * options.labels[horiz ? 'y' : 'x'])
  6941. );
  6942. axisOffset[side] = mathMax(
  6943. axisOffset[side],
  6944. axis.axisTitleMargin + titleOffset + directionFactor * axis.offset
  6945. );
  6946. clipOffset[invertedSide] = mathMax(clipOffset[invertedSide], options.lineWidth);
  6947. },
  6948. /**
  6949. * Get the path for the axis line
  6950. */
  6951. getLinePath: function (lineWidth) {
  6952. var chart = this.chart,
  6953. opposite = this.opposite,
  6954. offset = this.offset,
  6955. horiz = this.horiz,
  6956. lineLeft = this.left + (opposite ? this.width : 0) + offset,
  6957. lineTop = chart.chartHeight - this.bottom - (opposite ? this.height : 0) + offset;
  6958. this.lineTop = lineTop; // used by flag series
  6959. if (!opposite) {
  6960. lineWidth *= -1; // crispify the other way - #1480
  6961. }
  6962. return chart.renderer.crispLine([
  6963. M,
  6964. horiz ?
  6965. this.left :
  6966. lineLeft,
  6967. horiz ?
  6968. lineTop :
  6969. this.top,
  6970. L,
  6971. horiz ?
  6972. chart.chartWidth - this.right :
  6973. lineLeft,
  6974. horiz ?
  6975. lineTop :
  6976. chart.chartHeight - this.bottom
  6977. ], lineWidth);
  6978. },
  6979. /**
  6980. * Position the title
  6981. */
  6982. getTitlePosition: function () {
  6983. // compute anchor points for each of the title align options
  6984. var horiz = this.horiz,
  6985. axisLeft = this.left,
  6986. axisTop = this.top,
  6987. axisLength = this.len,
  6988. axisTitleOptions = this.options.title,
  6989. margin = horiz ? axisLeft : axisTop,
  6990. opposite = this.opposite,
  6991. offset = this.offset,
  6992. fontSize = pInt(axisTitleOptions.style.fontSize || 12),
  6993. // the position in the length direction of the axis
  6994. alongAxis = {
  6995. low: margin + (horiz ? 0 : axisLength),
  6996. middle: margin + axisLength / 2,
  6997. high: margin + (horiz ? axisLength : 0)
  6998. }[axisTitleOptions.align],
  6999. // the position in the perpendicular direction of the axis
  7000. offAxis = (horiz ? axisTop + this.height : axisLeft) +
  7001. (horiz ? 1 : -1) * // horizontal axis reverses the margin
  7002. (opposite ? -1 : 1) * // so does opposite axes
  7003. this.axisTitleMargin +
  7004. (this.side === 2 ? fontSize : 0);
  7005. return {
  7006. x: horiz ?
  7007. alongAxis :
  7008. offAxis + (opposite ? this.width : 0) + offset +
  7009. (axisTitleOptions.x || 0), // x
  7010. y: horiz ?
  7011. offAxis - (opposite ? this.height : 0) + offset :
  7012. alongAxis + (axisTitleOptions.y || 0) // y
  7013. };
  7014. },
  7015. /**
  7016. * Render the axis
  7017. */
  7018. render: function () {
  7019. var axis = this,
  7020. chart = axis.chart,
  7021. renderer = chart.renderer,
  7022. options = axis.options,
  7023. isLog = axis.isLog,
  7024. isLinked = axis.isLinked,
  7025. tickPositions = axis.tickPositions,
  7026. axisTitle = axis.axisTitle,
  7027. stacks = axis.stacks,
  7028. ticks = axis.ticks,
  7029. minorTicks = axis.minorTicks,
  7030. alternateBands = axis.alternateBands,
  7031. stackLabelOptions = options.stackLabels,
  7032. alternateGridColor = options.alternateGridColor,
  7033. tickmarkOffset = axis.tickmarkOffset,
  7034. lineWidth = options.lineWidth,
  7035. linePath,
  7036. hasRendered = chart.hasRendered,
  7037. slideInTicks = hasRendered && defined(axis.oldMin) && !isNaN(axis.oldMin),
  7038. hasData = axis.hasData,
  7039. showAxis = axis.showAxis,
  7040. from,
  7041. to;
  7042. // Mark all elements inActive before we go over and mark the active ones
  7043. each([ticks, minorTicks, alternateBands], function (coll) {
  7044. var pos;
  7045. for (pos in coll) {
  7046. coll[pos].isActive = false;
  7047. }
  7048. });
  7049. // If the series has data draw the ticks. Else only the line and title
  7050. if (hasData || isLinked) {
  7051. // minor ticks
  7052. if (axis.minorTickInterval && !axis.categories) {
  7053. each(axis.getMinorTickPositions(), function (pos) {
  7054. if (!minorTicks[pos]) {
  7055. minorTicks[pos] = new Tick(axis, pos, 'minor');
  7056. }
  7057. // render new ticks in old position
  7058. if (slideInTicks && minorTicks[pos].isNew) {
  7059. minorTicks[pos].render(null, true);
  7060. }
  7061. minorTicks[pos].render(null, false, 1);
  7062. });
  7063. }
  7064. // Major ticks. Pull out the first item and render it last so that
  7065. // we can get the position of the neighbour label. #808.
  7066. if (tickPositions.length) { // #1300
  7067. each(tickPositions.slice(1).concat([tickPositions[0]]), function (pos, i) {
  7068. // Reorganize the indices
  7069. i = (i === tickPositions.length - 1) ? 0 : i + 1;
  7070. // linked axes need an extra check to find out if
  7071. if (!isLinked || (pos >= axis.min && pos <= axis.max)) {
  7072. if (!ticks[pos]) {
  7073. ticks[pos] = new Tick(axis, pos);
  7074. }
  7075. // render new ticks in old position
  7076. if (slideInTicks && ticks[pos].isNew) {
  7077. ticks[pos].render(i, true);
  7078. }
  7079. ticks[pos].render(i, false, 1);
  7080. }
  7081. });
  7082. // In a categorized axis, the tick marks are displayed between labels. So
  7083. // we need to add a tick mark and grid line at the left edge of the X axis.
  7084. if (tickmarkOffset && axis.min === 0) {
  7085. if (!ticks[-1]) {
  7086. ticks[-1] = new Tick(axis, -1, null, true);
  7087. }
  7088. ticks[-1].render(-1);
  7089. }
  7090. }
  7091. // alternate grid color
  7092. if (alternateGridColor) {
  7093. each(tickPositions, function (pos, i) {
  7094. if (i % 2 === 0 && pos < axis.max) {
  7095. if (!alternateBands[pos]) {
  7096. alternateBands[pos] = new PlotLineOrBand(axis);
  7097. }
  7098. from = pos + tickmarkOffset; // #949
  7099. to = tickPositions[i + 1] !== UNDEFINED ? tickPositions[i + 1] + tickmarkOffset : axis.max;
  7100. alternateBands[pos].options = {
  7101. from: isLog ? lin2log(from) : from,
  7102. to: isLog ? lin2log(to) : to,
  7103. color: alternateGridColor
  7104. };
  7105. alternateBands[pos].render();
  7106. alternateBands[pos].isActive = true;
  7107. }
  7108. });
  7109. }
  7110. // custom plot lines and bands
  7111. if (!axis._addedPlotLB) { // only first time
  7112. each((options.plotLines || []).concat(options.plotBands || []), function (plotLineOptions) {
  7113. axis.addPlotBandOrLine(plotLineOptions);
  7114. });
  7115. axis._addedPlotLB = true;
  7116. }
  7117. } // end if hasData
  7118. // Remove inactive ticks
  7119. each([ticks, minorTicks, alternateBands], function (coll) {
  7120. var pos,
  7121. i,
  7122. forDestruction = [],
  7123. delay = globalAnimation ? globalAnimation.duration || 500 : 0,
  7124. destroyInactiveItems = function () {
  7125. i = forDestruction.length;
  7126. while (i--) {
  7127. // When resizing rapidly, the same items may be destroyed in different timeouts,
  7128. // or the may be reactivated
  7129. if (coll[forDestruction[i]] && !coll[forDestruction[i]].isActive) {
  7130. coll[forDestruction[i]].destroy();
  7131. delete coll[forDestruction[i]];
  7132. }
  7133. }
  7134. };
  7135. for (pos in coll) {
  7136. if (!coll[pos].isActive) {
  7137. // Render to zero opacity
  7138. coll[pos].render(pos, false, 0);
  7139. coll[pos].isActive = false;
  7140. forDestruction.push(pos);
  7141. }
  7142. }
  7143. // When the objects are finished fading out, destroy them
  7144. if (coll === alternateBands || !chart.hasRendered || !delay) {
  7145. destroyInactiveItems();
  7146. } else if (delay) {
  7147. setTimeout(destroyInactiveItems, delay);
  7148. }
  7149. });
  7150. // Static items. As the axis group is cleared on subsequent calls
  7151. // to render, these items are added outside the group.
  7152. // axis line
  7153. if (lineWidth) {
  7154. linePath = axis.getLinePath(lineWidth);
  7155. if (!axis.axisLine) {
  7156. axis.axisLine = renderer.path(linePath)
  7157. .attr({
  7158. stroke: options.lineColor,
  7159. 'stroke-width': lineWidth,
  7160. zIndex: 7
  7161. })
  7162. .add(axis.axisGroup);
  7163. } else {
  7164. axis.axisLine.animate({ d: linePath });
  7165. }
  7166. // show or hide the line depending on options.showEmpty
  7167. axis.axisLine[showAxis ? 'show' : 'hide']();
  7168. }
  7169. if (axisTitle && showAxis) {
  7170. axisTitle[axisTitle.isNew ? 'attr' : 'animate'](
  7171. axis.getTitlePosition()
  7172. );
  7173. axisTitle.isNew = false;
  7174. }
  7175. // Stacked totals:
  7176. if (stackLabelOptions && stackLabelOptions.enabled) {
  7177. var stackKey, oneStack, stackCategory,
  7178. stackTotalGroup = axis.stackTotalGroup;
  7179. // Create a separate group for the stack total labels
  7180. if (!stackTotalGroup) {
  7181. axis.stackTotalGroup = stackTotalGroup =
  7182. renderer.g('stack-labels')
  7183. .attr({
  7184. visibility: VISIBLE,
  7185. zIndex: 6
  7186. })
  7187. .add();
  7188. }
  7189. // plotLeft/Top will change when y axis gets wider so we need to translate the
  7190. // stackTotalGroup at every render call. See bug #506 and #516
  7191. stackTotalGroup.translate(chart.plotLeft, chart.plotTop);
  7192. // Render each stack total
  7193. for (stackKey in stacks) {
  7194. oneStack = stacks[stackKey];
  7195. for (stackCategory in oneStack) {
  7196. oneStack[stackCategory].render(stackTotalGroup);
  7197. }
  7198. }
  7199. }
  7200. // End stacked totals
  7201. axis.isDirty = false;
  7202. },
  7203. /**
  7204. * Remove a plot band or plot line from the chart by id
  7205. * @param {Object} id
  7206. */
  7207. removePlotBandOrLine: function (id) {
  7208. var plotLinesAndBands = this.plotLinesAndBands,
  7209. i = plotLinesAndBands.length;
  7210. while (i--) {
  7211. if (plotLinesAndBands[i].id === id) {
  7212. plotLinesAndBands[i].destroy();
  7213. }
  7214. }
  7215. },
  7216. /**
  7217. * Update the axis title by options
  7218. */
  7219. setTitle: function (newTitleOptions, redraw) {
  7220. this.update({ title: newTitleOptions }, redraw);
  7221. },
  7222. /**
  7223. * Redraw the axis to reflect changes in the data or axis extremes
  7224. */
  7225. redraw: function () {
  7226. var axis = this,
  7227. chart = axis.chart,
  7228. pointer = chart.pointer;
  7229. // hide tooltip and hover states
  7230. if (pointer.reset) {
  7231. pointer.reset(true);
  7232. }
  7233. // render the axis
  7234. axis.render();
  7235. // move plot lines and bands
  7236. each(axis.plotLinesAndBands, function (plotLine) {
  7237. plotLine.render();
  7238. });
  7239. // mark associated series as dirty and ready for redraw
  7240. each(axis.series, function (series) {
  7241. series.isDirty = true;
  7242. });
  7243. },
  7244. /**
  7245. * Set new axis categories and optionally redraw
  7246. * @param {Array} categories
  7247. * @param {Boolean} redraw
  7248. */
  7249. setCategories: function (categories, redraw) {
  7250. this.update({ categories: categories }, redraw);
  7251. },
  7252. /**
  7253. * Destroys an Axis instance.
  7254. */
  7255. destroy: function () {
  7256. var axis = this,
  7257. stacks = axis.stacks,
  7258. stackKey;
  7259. // Remove the events
  7260. removeEvent(axis);
  7261. // Destroy each stack total
  7262. for (stackKey in stacks) {
  7263. destroyObjectProperties(stacks[stackKey]);
  7264. stacks[stackKey] = null;
  7265. }
  7266. // Destroy collections
  7267. each([axis.ticks, axis.minorTicks, axis.alternateBands, axis.plotLinesAndBands], function (coll) {
  7268. destroyObjectProperties(coll);
  7269. });
  7270. // Destroy local variables
  7271. each(['stackTotalGroup', 'axisLine', 'axisGroup', 'gridGroup', 'labelGroup', 'axisTitle'], function (prop) {
  7272. if (axis[prop]) {
  7273. axis[prop] = axis[prop].destroy();
  7274. }
  7275. });
  7276. }
  7277. }; // end Axis
  7278. /**
  7279. * The tooltip object
  7280. * @param {Object} chart The chart instance
  7281. * @param {Object} options Tooltip options
  7282. */
  7283. function Tooltip() {
  7284. this.init.apply(this, arguments);
  7285. }
  7286. Tooltip.prototype = {
  7287. init: function (chart, options) {
  7288. var borderWidth = options.borderWidth,
  7289. style = options.style,
  7290. padding = pInt(style.padding);
  7291. // Save the chart and options
  7292. this.chart = chart;
  7293. this.options = options;
  7294. // Keep track of the current series
  7295. //this.currentSeries = UNDEFINED;
  7296. // List of crosshairs
  7297. this.crosshairs = [];
  7298. // Current values of x and y when animating
  7299. this.now = { x: 0, y: 0 };
  7300. // The tooltip is initially hidden
  7301. this.isHidden = true;
  7302. // create the label
  7303. this.label = chart.renderer.label('', 0, 0, options.shape, null, null, options.useHTML, null, 'tooltip')
  7304. .attr({
  7305. padding: padding,
  7306. fill: options.backgroundColor,
  7307. 'stroke-width': borderWidth,
  7308. r: options.borderRadius,
  7309. zIndex: 8
  7310. })
  7311. .css(style)
  7312. .css({ padding: 0 }) // Remove it from VML, the padding is applied as an attribute instead (#1117)
  7313. .hide()
  7314. .add();
  7315. // When using canVG the shadow shows up as a gray circle
  7316. // even if the tooltip is hidden.
  7317. if (!useCanVG) {
  7318. this.label.shadow(options.shadow);
  7319. }
  7320. // Public property for getting the shared state.
  7321. this.shared = options.shared;
  7322. },
  7323. /**
  7324. * Destroy the tooltip and its elements.
  7325. */
  7326. destroy: function () {
  7327. each(this.crosshairs, function (crosshair) {
  7328. if (crosshair) {
  7329. crosshair.destroy();
  7330. }
  7331. });
  7332. // Destroy and clear local variables
  7333. if (this.label) {
  7334. this.label = this.label.destroy();
  7335. }
  7336. clearTimeout(this.hideTimer);
  7337. clearTimeout(this.tooltipTimeout);
  7338. },
  7339. /**
  7340. * Provide a soft movement for the tooltip
  7341. *
  7342. * @param {Number} x
  7343. * @param {Number} y
  7344. * @private
  7345. */
  7346. move: function (x, y, anchorX, anchorY) {
  7347. var tooltip = this,
  7348. now = tooltip.now,
  7349. animate = tooltip.options.animation !== false && !tooltip.isHidden;
  7350. // get intermediate values for animation
  7351. extend(now, {
  7352. x: animate ? (2 * now.x + x) / 3 : x,
  7353. y: animate ? (now.y + y) / 2 : y,
  7354. anchorX: animate ? (2 * now.anchorX + anchorX) / 3 : anchorX,
  7355. anchorY: animate ? (now.anchorY + anchorY) / 2 : anchorY
  7356. });
  7357. // move to the intermediate value
  7358. tooltip.label.attr(now);
  7359. // run on next tick of the mouse tracker
  7360. if (animate && (mathAbs(x - now.x) > 1 || mathAbs(y - now.y) > 1)) {
  7361. // never allow two timeouts
  7362. clearTimeout(this.tooltipTimeout);
  7363. // set the fixed interval ticking for the smooth tooltip
  7364. this.tooltipTimeout = setTimeout(function () {
  7365. // The interval function may still be running during destroy, so check that the chart is really there before calling.
  7366. if (tooltip) {
  7367. tooltip.move(x, y, anchorX, anchorY);
  7368. }
  7369. }, 32);
  7370. }
  7371. },
  7372. /**
  7373. * Hide the tooltip
  7374. */
  7375. hide: function () {
  7376. var tooltip = this,
  7377. hoverPoints;
  7378. clearTimeout(this.hideTimer); // disallow duplicate timers (#1728, #1766)
  7379. if (!this.isHidden) {
  7380. hoverPoints = this.chart.hoverPoints;
  7381. this.hideTimer = setTimeout(function () {
  7382. tooltip.label.fadeOut();
  7383. tooltip.isHidden = true;
  7384. }, pick(this.options.hideDelay, 500));
  7385. // hide previous hoverPoints and set new
  7386. if (hoverPoints) {
  7387. each(hoverPoints, function (point) {
  7388. point.setState();
  7389. });
  7390. }
  7391. this.chart.hoverPoints = null;
  7392. }
  7393. },
  7394. /**
  7395. * Hide the crosshairs
  7396. */
  7397. hideCrosshairs: function () {
  7398. each(this.crosshairs, function (crosshair) {
  7399. if (crosshair) {
  7400. crosshair.hide();
  7401. }
  7402. });
  7403. },
  7404. /**
  7405. * Extendable method to get the anchor position of the tooltip
  7406. * from a point or set of points
  7407. */
  7408. getAnchor: function (points, mouseEvent) {
  7409. var ret,
  7410. chart = this.chart,
  7411. inverted = chart.inverted,
  7412. plotTop = chart.plotTop,
  7413. plotX = 0,
  7414. plotY = 0,
  7415. yAxis;
  7416. points = splat(points);
  7417. // Pie uses a special tooltipPos
  7418. ret = points[0].tooltipPos;
  7419. // When tooltip follows mouse, relate the position to the mouse
  7420. if (this.followPointer && mouseEvent) {
  7421. if (mouseEvent.chartX === UNDEFINED) {
  7422. mouseEvent = chart.pointer.normalize(mouseEvent);
  7423. }
  7424. ret = [
  7425. mouseEvent.chartX - chart.plotLeft,
  7426. mouseEvent.chartY - plotTop
  7427. ];
  7428. }
  7429. // When shared, use the average position
  7430. if (!ret) {
  7431. each(points, function (point) {
  7432. yAxis = point.series.yAxis;
  7433. plotX += point.plotX;
  7434. plotY += (point.plotLow ? (point.plotLow + point.plotHigh) / 2 : point.plotY) +
  7435. (!inverted && yAxis ? yAxis.top - plotTop : 0); // #1151
  7436. });
  7437. plotX /= points.length;
  7438. plotY /= points.length;
  7439. ret = [
  7440. inverted ? chart.plotWidth - plotY : plotX,
  7441. this.shared && !inverted && points.length > 1 && mouseEvent ?
  7442. mouseEvent.chartY - plotTop : // place shared tooltip next to the mouse (#424)
  7443. inverted ? chart.plotHeight - plotX : plotY
  7444. ];
  7445. }
  7446. return map(ret, mathRound);
  7447. },
  7448. /**
  7449. * Place the tooltip in a chart without spilling over
  7450. * and not covering the point it self.
  7451. */
  7452. getPosition: function (boxWidth, boxHeight, point) {
  7453. // Set up the variables
  7454. var chart = this.chart,
  7455. plotLeft = chart.plotLeft,
  7456. plotTop = chart.plotTop,
  7457. plotWidth = chart.plotWidth,
  7458. plotHeight = chart.plotHeight,
  7459. distance = pick(this.options.distance, 12),
  7460. pointX = point.plotX,
  7461. pointY = point.plotY,
  7462. x = pointX + plotLeft + (chart.inverted ? distance : -boxWidth - distance),
  7463. y = pointY - boxHeight + plotTop + 15, // 15 means the point is 15 pixels up from the bottom of the tooltip
  7464. alignedRight;
  7465. // It is too far to the left, adjust it
  7466. if (x < 7) {
  7467. x = plotLeft + mathMax(pointX, 0) + distance;
  7468. }
  7469. // Test to see if the tooltip is too far to the right,
  7470. // if it is, move it back to be inside and then up to not cover the point.
  7471. if ((x + boxWidth) > (plotLeft + plotWidth)) {
  7472. x -= (x + boxWidth) - (plotLeft + plotWidth);
  7473. y = pointY - boxHeight + plotTop - distance;
  7474. alignedRight = true;
  7475. }
  7476. // If it is now above the plot area, align it to the top of the plot area
  7477. if (y < plotTop + 5) {
  7478. y = plotTop + 5;
  7479. // If the tooltip is still covering the point, move it below instead
  7480. if (alignedRight && pointY >= y && pointY <= (y + boxHeight)) {
  7481. y = pointY + plotTop + distance; // below
  7482. }
  7483. }
  7484. // Now if the tooltip is below the chart, move it up. It's better to cover the
  7485. // point than to disappear outside the chart. #834.
  7486. if (y + boxHeight > plotTop + plotHeight) {
  7487. y = mathMax(plotTop, plotTop + plotHeight - boxHeight - distance); // below
  7488. }
  7489. return {x: x, y: y};
  7490. },
  7491. /**
  7492. * In case no user defined formatter is given, this will be used. Note that the context
  7493. * here is an object holding point, series, x, y etc.
  7494. */
  7495. defaultFormatter: function (tooltip) {
  7496. var items = this.points || splat(this),
  7497. series = items[0].series,
  7498. s;
  7499. // build the header
  7500. s = [series.tooltipHeaderFormatter(items[0])];
  7501. // build the values
  7502. each(items, function (item) {
  7503. series = item.series;
  7504. s.push((series.tooltipFormatter && series.tooltipFormatter(item)) ||
  7505. item.point.tooltipFormatter(series.tooltipOptions.pointFormat));
  7506. });
  7507. // footer
  7508. s.push(tooltip.options.footerFormat || '');
  7509. return s.join('');
  7510. },
  7511. /**
  7512. * Refresh the tooltip's text and position.
  7513. * @param {Object} point
  7514. */
  7515. refresh: function (point, mouseEvent) {
  7516. var tooltip = this,
  7517. chart = tooltip.chart,
  7518. label = tooltip.label,
  7519. options = tooltip.options,
  7520. x,
  7521. y,
  7522. show,
  7523. anchor,
  7524. textConfig = {},
  7525. text,
  7526. pointConfig = [],
  7527. formatter = options.formatter || tooltip.defaultFormatter,
  7528. hoverPoints = chart.hoverPoints,
  7529. borderColor,
  7530. crosshairsOptions = options.crosshairs,
  7531. shared = tooltip.shared,
  7532. currentSeries;
  7533. clearTimeout(this.hideTimer);
  7534. // get the reference point coordinates (pie charts use tooltipPos)
  7535. tooltip.followPointer = splat(point)[0].series.tooltipOptions.followPointer;
  7536. anchor = tooltip.getAnchor(point, mouseEvent);
  7537. x = anchor[0];
  7538. y = anchor[1];
  7539. // shared tooltip, array is sent over
  7540. if (shared && !(point.series && point.series.noSharedTooltip)) {
  7541. // hide previous hoverPoints and set new
  7542. chart.hoverPoints = point;
  7543. if (hoverPoints) {
  7544. each(hoverPoints, function (point) {
  7545. point.setState();
  7546. });
  7547. }
  7548. each(point, function (item) {
  7549. item.setState(HOVER_STATE);
  7550. pointConfig.push(item.getLabelConfig());
  7551. });
  7552. textConfig = {
  7553. x: point[0].category,
  7554. y: point[0].y
  7555. };
  7556. textConfig.points = pointConfig;
  7557. point = point[0];
  7558. // single point tooltip
  7559. } else {
  7560. textConfig = point.getLabelConfig();
  7561. }
  7562. text = formatter.call(textConfig, tooltip);
  7563. // register the current series
  7564. currentSeries = point.series;
  7565. // For line type series, hide tooltip if the point falls outside the plot
  7566. show = shared || !currentSeries.isCartesian || currentSeries.tooltipOutsidePlot || chart.isInsidePlot(x, y);
  7567. // update the inner HTML
  7568. if (text === false || !show) {
  7569. this.hide();
  7570. } else {
  7571. // show it
  7572. if (tooltip.isHidden) {
  7573. stop(label);
  7574. label.attr('opacity', 1).show();
  7575. }
  7576. // update text
  7577. label.attr({
  7578. text: text
  7579. });
  7580. // set the stroke color of the box
  7581. borderColor = options.borderColor || point.color || currentSeries.color || '#606060';
  7582. label.attr({
  7583. stroke: borderColor
  7584. });
  7585. tooltip.updatePosition({ plotX: x, plotY: y });
  7586. this.isHidden = false;
  7587. }
  7588. // crosshairs
  7589. if (crosshairsOptions) {
  7590. crosshairsOptions = splat(crosshairsOptions); // [x, y]
  7591. var path,
  7592. i = crosshairsOptions.length,
  7593. attribs,
  7594. axis,
  7595. val;
  7596. while (i--) {
  7597. axis = point.series[i ? 'yAxis' : 'xAxis'];
  7598. if (crosshairsOptions[i] && axis) {
  7599. val = i ? pick(point.stackY, point.y) : point.x; // #814
  7600. if (axis.isLog) { // #1671
  7601. val = log2lin(val);
  7602. }
  7603. path = axis.getPlotLinePath(
  7604. val,
  7605. 1
  7606. );
  7607. if (tooltip.crosshairs[i]) {
  7608. tooltip.crosshairs[i].attr({ d: path, visibility: VISIBLE });
  7609. } else {
  7610. attribs = {
  7611. 'stroke-width': crosshairsOptions[i].width || 1,
  7612. stroke: crosshairsOptions[i].color || '#C0C0C0',
  7613. zIndex: crosshairsOptions[i].zIndex || 2
  7614. };
  7615. if (crosshairsOptions[i].dashStyle) {
  7616. attribs.dashstyle = crosshairsOptions[i].dashStyle;
  7617. }
  7618. tooltip.crosshairs[i] = chart.renderer.path(path)
  7619. .attr(attribs)
  7620. .add();
  7621. }
  7622. }
  7623. }
  7624. }
  7625. fireEvent(chart, 'tooltipRefresh', {
  7626. text: text,
  7627. x: x + chart.plotLeft,
  7628. y: y + chart.plotTop,
  7629. borderColor: borderColor
  7630. });
  7631. },
  7632. /**
  7633. * Find the new position and perform the move
  7634. */
  7635. updatePosition: function (point) {
  7636. var chart = this.chart,
  7637. label = this.label,
  7638. pos = (this.options.positioner || this.getPosition).call(
  7639. this,
  7640. label.width,
  7641. label.height,
  7642. point
  7643. );
  7644. // do the move
  7645. this.move(
  7646. mathRound(pos.x),
  7647. mathRound(pos.y),
  7648. point.plotX + chart.plotLeft,
  7649. point.plotY + chart.plotTop
  7650. );
  7651. }
  7652. };
  7653. /**
  7654. * The mouse tracker object. All methods starting with "on" are primary DOM event handlers.
  7655. * Subsequent methods should be named differently from what they are doing.
  7656. * @param {Object} chart The Chart instance
  7657. * @param {Object} options The root options object
  7658. */
  7659. function Pointer(chart, options) {
  7660. this.init(chart, options);
  7661. }
  7662. Pointer.prototype = {
  7663. /**
  7664. * Initialize Pointer
  7665. */
  7666. init: function (chart, options) {
  7667. var zoomType = useCanVG ? '' : options.chart.zoomType,
  7668. inverted = chart.inverted,
  7669. zoomX,
  7670. zoomY;
  7671. // Store references
  7672. this.options = options;
  7673. this.chart = chart;
  7674. // Zoom status
  7675. this.zoomX = zoomX = /x/.test(zoomType);
  7676. this.zoomY = zoomY = /y/.test(zoomType);
  7677. this.zoomHor = (zoomX && !inverted) || (zoomY && inverted);
  7678. this.zoomVert = (zoomY && !inverted) || (zoomX && inverted);
  7679. this.pinchDown = [];
  7680. this.lastValidTouch = {};
  7681. if (options.tooltip.enabled) {
  7682. chart.tooltip = new Tooltip(chart, options.tooltip);
  7683. }
  7684. this.setDOMEvents();
  7685. },
  7686. /**
  7687. * Add crossbrowser support for chartX and chartY
  7688. * @param {Object} e The event object in standard browsers
  7689. */
  7690. normalize: function (e) {
  7691. var chartPosition,
  7692. chartX,
  7693. chartY,
  7694. ePos;
  7695. // common IE normalizing
  7696. e = e || win.event;
  7697. if (!e.target) {
  7698. e.target = e.srcElement;
  7699. }
  7700. // Framework specific normalizing (#1165)
  7701. e = washMouseEvent(e);
  7702. // iOS
  7703. ePos = e.touches ? e.touches.item(0) : e;
  7704. // get mouse position
  7705. this.chartPosition = chartPosition = offset(this.chart.container);
  7706. // chartX and chartY
  7707. if (ePos.pageX === UNDEFINED) { // IE < 9. #886.
  7708. chartX = e.x;
  7709. chartY = e.y;
  7710. } else {
  7711. chartX = ePos.pageX - chartPosition.left;
  7712. chartY = ePos.pageY - chartPosition.top;
  7713. }
  7714. return extend(e, {
  7715. chartX: mathRound(chartX),
  7716. chartY: mathRound(chartY)
  7717. });
  7718. },
  7719. /**
  7720. * Get the click position in terms of axis values.
  7721. *
  7722. * @param {Object} e A pointer event
  7723. */
  7724. getCoordinates: function (e) {
  7725. var coordinates = {
  7726. xAxis: [],
  7727. yAxis: []
  7728. };
  7729. each(this.chart.axes, function (axis) {
  7730. coordinates[axis.isXAxis ? 'xAxis' : 'yAxis'].push({
  7731. axis: axis,
  7732. value: axis.toValue(e[axis.horiz ? 'chartX' : 'chartY'])
  7733. });
  7734. });
  7735. return coordinates;
  7736. },
  7737. /**
  7738. * Return the index in the tooltipPoints array, corresponding to pixel position in
  7739. * the plot area.
  7740. */
  7741. getIndex: function (e) {
  7742. var chart = this.chart;
  7743. return chart.inverted ?
  7744. chart.plotHeight + chart.plotTop - e.chartY :
  7745. e.chartX - chart.plotLeft;
  7746. },
  7747. /**
  7748. * With line type charts with a single tracker, get the point closest to the mouse.
  7749. * Run Point.onMouseOver and display tooltip for the point or points.
  7750. */
  7751. runPointActions: function (e) {
  7752. var pointer = this,
  7753. chart = pointer.chart,
  7754. series = chart.series,
  7755. tooltip = chart.tooltip,
  7756. point,
  7757. points,
  7758. hoverPoint = chart.hoverPoint,
  7759. hoverSeries = chart.hoverSeries,
  7760. i,
  7761. j,
  7762. distance = chart.chartWidth,
  7763. index = pointer.getIndex(e),
  7764. anchor;
  7765. // shared tooltip
  7766. if (tooltip && pointer.options.tooltip.shared && !(hoverSeries && hoverSeries.noSharedTooltip)) {
  7767. points = [];
  7768. // loop over all series and find the ones with points closest to the mouse
  7769. i = series.length;
  7770. for (j = 0; j < i; j++) {
  7771. if (series[j].visible &&
  7772. series[j].options.enableMouseTracking !== false &&
  7773. !series[j].noSharedTooltip && series[j].tooltipPoints.length) {
  7774. point = series[j].tooltipPoints[index];
  7775. if (point.series) { // not a dummy point, #1544
  7776. point._dist = mathAbs(index - point.clientX);
  7777. distance = mathMin(distance, point._dist);
  7778. points.push(point);
  7779. }
  7780. }
  7781. }
  7782. // remove furthest points
  7783. i = points.length;
  7784. while (i--) {
  7785. if (points[i]._dist > distance) {
  7786. points.splice(i, 1);
  7787. }
  7788. }
  7789. // refresh the tooltip if necessary
  7790. if (points.length && (points[0].clientX !== pointer.hoverX)) {
  7791. tooltip.refresh(points, e);
  7792. pointer.hoverX = points[0].clientX;
  7793. }
  7794. }
  7795. // separate tooltip and general mouse events
  7796. if (hoverSeries && hoverSeries.tracker) { // only use for line-type series with common tracker
  7797. // get the point
  7798. point = hoverSeries.tooltipPoints[index];
  7799. // a new point is hovered, refresh the tooltip
  7800. if (point && point !== hoverPoint) {
  7801. // trigger the events
  7802. point.onMouseOver(e);
  7803. }
  7804. } else if (tooltip && tooltip.followPointer && !tooltip.isHidden) {
  7805. anchor = tooltip.getAnchor([{}], e);
  7806. tooltip.updatePosition({ plotX: anchor[0], plotY: anchor[1] });
  7807. }
  7808. },
  7809. /**
  7810. * Reset the tracking by hiding the tooltip, the hover series state and the hover point
  7811. *
  7812. * @param allowMove {Boolean} Instead of destroying the tooltip altogether, allow moving it if possible
  7813. */
  7814. reset: function (allowMove) {
  7815. var pointer = this,
  7816. chart = pointer.chart,
  7817. hoverSeries = chart.hoverSeries,
  7818. hoverPoint = chart.hoverPoint,
  7819. tooltip = chart.tooltip,
  7820. tooltipPoints = tooltip && tooltip.shared ? chart.hoverPoints : hoverPoint;
  7821. // Narrow in allowMove
  7822. allowMove = allowMove && tooltip && tooltipPoints;
  7823. // Check if the points have moved outside the plot area, #1003
  7824. if (allowMove && splat(tooltipPoints)[0].plotX === UNDEFINED) {
  7825. allowMove = false;
  7826. }
  7827. // Just move the tooltip, #349
  7828. if (allowMove) {
  7829. tooltip.refresh(tooltipPoints);
  7830. // Full reset
  7831. } else {
  7832. if (hoverPoint) {
  7833. hoverPoint.onMouseOut();
  7834. }
  7835. if (hoverSeries) {
  7836. hoverSeries.onMouseOut();
  7837. }
  7838. if (tooltip) {
  7839. tooltip.hide();
  7840. tooltip.hideCrosshairs();
  7841. }
  7842. pointer.hoverX = null;
  7843. }
  7844. },
  7845. /**
  7846. * Scale series groups to a certain scale and translation
  7847. */
  7848. scaleGroups: function (attribs, clip) {
  7849. var chart = this.chart;
  7850. // Scale each series
  7851. each(chart.series, function (series) {
  7852. if (series.xAxis && series.xAxis.zoomEnabled) {
  7853. series.group.attr(attribs);
  7854. if (series.markerGroup) {
  7855. series.markerGroup.attr(attribs);
  7856. series.markerGroup.clip(clip ? chart.clipRect : null);
  7857. }
  7858. if (series.dataLabelsGroup) {
  7859. series.dataLabelsGroup.attr(attribs);
  7860. }
  7861. }
  7862. });
  7863. // Clip
  7864. chart.clipRect.attr(clip || chart.clipBox);
  7865. },
  7866. /**
  7867. * Run translation operations for each direction (horizontal and vertical) independently
  7868. */
  7869. pinchTranslateDirection: function (horiz, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch) {
  7870. var chart = this.chart,
  7871. xy = horiz ? 'x' : 'y',
  7872. XY = horiz ? 'X' : 'Y',
  7873. sChartXY = 'chart' + XY,
  7874. wh = horiz ? 'width' : 'height',
  7875. plotLeftTop = chart['plot' + (horiz ? 'Left' : 'Top')],
  7876. selectionWH,
  7877. selectionXY,
  7878. clipXY,
  7879. scale = 1,
  7880. inverted = chart.inverted,
  7881. bounds = chart.bounds[horiz ? 'h' : 'v'],
  7882. singleTouch = pinchDown.length === 1,
  7883. touch0Start = pinchDown[0][sChartXY],
  7884. touch0Now = touches[0][sChartXY],
  7885. touch1Start = !singleTouch && pinchDown[1][sChartXY],
  7886. touch1Now = !singleTouch && touches[1][sChartXY],
  7887. outOfBounds,
  7888. transformScale,
  7889. scaleKey,
  7890. setScale = function () {
  7891. if (!singleTouch && mathAbs(touch0Start - touch1Start) > 20) { // Don't zoom if fingers are too close on this axis
  7892. scale = mathAbs(touch0Now - touch1Now) / mathAbs(touch0Start - touch1Start);
  7893. }
  7894. clipXY = ((plotLeftTop - touch0Now) / scale) + touch0Start;
  7895. selectionWH = chart['plot' + (horiz ? 'Width' : 'Height')] / scale;
  7896. };
  7897. // Set the scale, first pass
  7898. setScale();
  7899. selectionXY = clipXY; // the clip position (x or y) is altered if out of bounds, the selection position is not
  7900. // Out of bounds
  7901. if (selectionXY < bounds.min) {
  7902. selectionXY = bounds.min;
  7903. outOfBounds = true;
  7904. } else if (selectionXY + selectionWH > bounds.max) {
  7905. selectionXY = bounds.max - selectionWH;
  7906. outOfBounds = true;
  7907. }
  7908. // Is the chart dragged off its bounds, determined by dataMin and dataMax?
  7909. if (outOfBounds) {
  7910. // Modify the touchNow position in order to create an elastic drag movement. This indicates
  7911. // to the user that the chart is responsive but can't be dragged further.
  7912. touch0Now -= 0.8 * (touch0Now - lastValidTouch[xy][0]);
  7913. if (!singleTouch) {
  7914. touch1Now -= 0.8 * (touch1Now - lastValidTouch[xy][1]);
  7915. }
  7916. // Set the scale, second pass to adapt to the modified touchNow positions
  7917. setScale();
  7918. } else {
  7919. lastValidTouch[xy] = [touch0Now, touch1Now];
  7920. }
  7921. // Set geometry for clipping, selection and transformation
  7922. if (!inverted) { // TODO: implement clipping for inverted charts
  7923. clip[xy] = clipXY - plotLeftTop;
  7924. clip[wh] = selectionWH;
  7925. }
  7926. scaleKey = inverted ? (horiz ? 'scaleY' : 'scaleX') : 'scale' + XY;
  7927. transformScale = inverted ? 1 / scale : scale;
  7928. selectionMarker[wh] = selectionWH;
  7929. selectionMarker[xy] = selectionXY;
  7930. transform[scaleKey] = scale;
  7931. transform['translate' + XY] = (transformScale * plotLeftTop) + (touch0Now - (transformScale * touch0Start));
  7932. },
  7933. /**
  7934. * Handle touch events with two touches
  7935. */
  7936. pinch: function (e) {
  7937. var self = this,
  7938. chart = self.chart,
  7939. pinchDown = self.pinchDown,
  7940. followTouchMove = chart.tooltip && chart.tooltip.options.followTouchMove,
  7941. touches = e.touches,
  7942. touchesLength = touches.length,
  7943. lastValidTouch = self.lastValidTouch,
  7944. zoomHor = self.zoomHor || self.pinchHor,
  7945. zoomVert = self.zoomVert || self.pinchVert,
  7946. hasZoom = zoomHor || zoomVert,
  7947. selectionMarker = self.selectionMarker,
  7948. transform = {},
  7949. clip = {};
  7950. // On touch devices, only proceed to trigger click if a handler is defined
  7951. if (e.type === 'touchstart') {
  7952. if (followTouchMove || hasZoom) {
  7953. e.preventDefault();
  7954. }
  7955. }
  7956. // Normalize each touch
  7957. map(touches, function (e) {
  7958. return self.normalize(e);
  7959. });
  7960. // Register the touch start position
  7961. if (e.type === 'touchstart') {
  7962. each(touches, function (e, i) {
  7963. pinchDown[i] = { chartX: e.chartX, chartY: e.chartY };
  7964. });
  7965. lastValidTouch.x = [pinchDown[0].chartX, pinchDown[1] && pinchDown[1].chartX];
  7966. lastValidTouch.y = [pinchDown[0].chartY, pinchDown[1] && pinchDown[1].chartY];
  7967. // Identify the data bounds in pixels
  7968. each(chart.axes, function (axis) {
  7969. if (axis.zoomEnabled) {
  7970. var bounds = chart.bounds[axis.horiz ? 'h' : 'v'],
  7971. minPixelPadding = axis.minPixelPadding,
  7972. min = axis.toPixels(axis.dataMin),
  7973. max = axis.toPixels(axis.dataMax),
  7974. absMin = mathMin(min, max),
  7975. absMax = mathMax(min, max);
  7976. // Store the bounds for use in the touchmove handler
  7977. bounds.min = mathMin(axis.pos, absMin - minPixelPadding);
  7978. bounds.max = mathMax(axis.pos + axis.len, absMax + minPixelPadding);
  7979. }
  7980. });
  7981. // Event type is touchmove, handle panning and pinching
  7982. } else if (pinchDown.length) { // can be 0 when releasing, if touchend fires first
  7983. // Set the marker
  7984. if (!selectionMarker) {
  7985. self.selectionMarker = selectionMarker = extend({
  7986. destroy: noop
  7987. }, chart.plotBox);
  7988. }
  7989. if (zoomHor) {
  7990. self.pinchTranslateDirection(true, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch);
  7991. }
  7992. if (zoomVert) {
  7993. self.pinchTranslateDirection(false, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch);
  7994. }
  7995. self.hasPinched = hasZoom;
  7996. // Scale and translate the groups to provide visual feedback during pinching
  7997. self.scaleGroups(transform, clip);
  7998. // Optionally move the tooltip on touchmove
  7999. if (!hasZoom && followTouchMove && touchesLength === 1) {
  8000. this.runPointActions(self.normalize(e));
  8001. }
  8002. }
  8003. },
  8004. /**
  8005. * Start a drag operation
  8006. */
  8007. dragStart: function (e) {
  8008. var chart = this.chart;
  8009. // Record the start position
  8010. chart.mouseIsDown = e.type;
  8011. chart.cancelClick = false;
  8012. chart.mouseDownX = this.mouseDownX = e.chartX;
  8013. this.mouseDownY = e.chartY;
  8014. },
  8015. /**
  8016. * Perform a drag operation in response to a mousemove event while the mouse is down
  8017. */
  8018. drag: function (e) {
  8019. var chart = this.chart,
  8020. chartOptions = chart.options.chart,
  8021. chartX = e.chartX,
  8022. chartY = e.chartY,
  8023. zoomHor = this.zoomHor,
  8024. zoomVert = this.zoomVert,
  8025. plotLeft = chart.plotLeft,
  8026. plotTop = chart.plotTop,
  8027. plotWidth = chart.plotWidth,
  8028. plotHeight = chart.plotHeight,
  8029. clickedInside,
  8030. size,
  8031. mouseDownX = this.mouseDownX,
  8032. mouseDownY = this.mouseDownY;
  8033. // If the mouse is outside the plot area, adjust to cooordinates
  8034. // inside to prevent the selection marker from going outside
  8035. if (chartX < plotLeft) {
  8036. chartX = plotLeft;
  8037. } else if (chartX > plotLeft + plotWidth) {
  8038. chartX = plotLeft + plotWidth;
  8039. }
  8040. if (chartY < plotTop) {
  8041. chartY = plotTop;
  8042. } else if (chartY > plotTop + plotHeight) {
  8043. chartY = plotTop + plotHeight;
  8044. }
  8045. // determine if the mouse has moved more than 10px
  8046. this.hasDragged = Math.sqrt(
  8047. Math.pow(mouseDownX - chartX, 2) +
  8048. Math.pow(mouseDownY - chartY, 2)
  8049. );
  8050. if (this.hasDragged > 10) {
  8051. clickedInside = chart.isInsidePlot(mouseDownX - plotLeft, mouseDownY - plotTop);
  8052. // make a selection
  8053. if (chart.hasCartesianSeries && (this.zoomX || this.zoomY) && clickedInside) {
  8054. if (!this.selectionMarker) {
  8055. this.selectionMarker = chart.renderer.rect(
  8056. plotLeft,
  8057. plotTop,
  8058. zoomHor ? 1 : plotWidth,
  8059. zoomVert ? 1 : plotHeight,
  8060. 0
  8061. )
  8062. .attr({
  8063. fill: chartOptions.selectionMarkerFill || 'rgba(69,114,167,0.25)',
  8064. zIndex: 7
  8065. })
  8066. .add();
  8067. }
  8068. }
  8069. // adjust the width of the selection marker
  8070. if (this.selectionMarker && zoomHor) {
  8071. size = chartX - mouseDownX;
  8072. this.selectionMarker.attr({
  8073. width: mathAbs(size),
  8074. x: (size > 0 ? 0 : size) + mouseDownX
  8075. });
  8076. }
  8077. // adjust the height of the selection marker
  8078. if (this.selectionMarker && zoomVert) {
  8079. size = chartY - mouseDownY;
  8080. this.selectionMarker.attr({
  8081. height: mathAbs(size),
  8082. y: (size > 0 ? 0 : size) + mouseDownY
  8083. });
  8084. }
  8085. // panning
  8086. if (clickedInside && !this.selectionMarker && chartOptions.panning) {
  8087. chart.pan(chartX);
  8088. }
  8089. }
  8090. },
  8091. /**
  8092. * On mouse up or touch end across the entire document, drop the selection.
  8093. */
  8094. drop: function (e) {
  8095. var chart = this.chart,
  8096. hasPinched = this.hasPinched;
  8097. if (this.selectionMarker) {
  8098. var selectionData = {
  8099. xAxis: [],
  8100. yAxis: [],
  8101. originalEvent: e.originalEvent || e
  8102. },
  8103. selectionBox = this.selectionMarker,
  8104. selectionLeft = selectionBox.x,
  8105. selectionTop = selectionBox.y,
  8106. runZoom;
  8107. // a selection has been made
  8108. if (this.hasDragged || hasPinched) {
  8109. // record each axis' min and max
  8110. each(chart.axes, function (axis) {
  8111. if (axis.zoomEnabled) {
  8112. var horiz = axis.horiz,
  8113. selectionMin = axis.toValue((horiz ? selectionLeft : selectionTop)),
  8114. selectionMax = axis.toValue((horiz ? selectionLeft + selectionBox.width : selectionTop + selectionBox.height));
  8115. if (!isNaN(selectionMin) && !isNaN(selectionMax)) { // #859
  8116. selectionData[axis.xOrY + 'Axis'].push({
  8117. axis: axis,
  8118. min: mathMin(selectionMin, selectionMax), // for reversed axes,
  8119. max: mathMax(selectionMin, selectionMax)
  8120. });
  8121. runZoom = true;
  8122. }
  8123. }
  8124. });
  8125. if (runZoom) {
  8126. fireEvent(chart, 'selection', selectionData, function (args) {
  8127. chart.zoom(extend(args, hasPinched ? { animation: false } : null));
  8128. });
  8129. }
  8130. }
  8131. this.selectionMarker = this.selectionMarker.destroy();
  8132. // Reset scaling preview
  8133. if (hasPinched) {
  8134. this.scaleGroups({
  8135. translateX: chart.plotLeft,
  8136. translateY: chart.plotTop,
  8137. scaleX: 1,
  8138. scaleY: 1
  8139. });
  8140. }
  8141. }
  8142. // Reset all
  8143. if (chart) { // it may be destroyed on mouse up - #877
  8144. css(chart.container, { cursor: chart._cursor });
  8145. chart.cancelClick = this.hasDragged > 10; // #370
  8146. chart.mouseIsDown = this.hasDragged = this.hasPinched = false;
  8147. this.pinchDown = [];
  8148. }
  8149. },
  8150. onContainerMouseDown: function (e) {
  8151. e = this.normalize(e);
  8152. // issue #295, dragging not always working in Firefox
  8153. if (e.preventDefault) {
  8154. e.preventDefault();
  8155. }
  8156. this.dragStart(e);
  8157. },
  8158. onDocumentMouseUp: function (e) {
  8159. this.drop(e);
  8160. },
  8161. /**
  8162. * Special handler for mouse move that will hide the tooltip when the mouse leaves the plotarea.
  8163. * Issue #149 workaround. The mouseleave event does not always fire.
  8164. */
  8165. onDocumentMouseMove: function (e) {
  8166. var chart = this.chart,
  8167. chartPosition = this.chartPosition,
  8168. hoverSeries = chart.hoverSeries;
  8169. // Get e.pageX and e.pageY back in MooTools
  8170. e = washMouseEvent(e);
  8171. // If we're outside, hide the tooltip
  8172. if (chartPosition && hoverSeries && hoverSeries.isCartesian &&
  8173. !chart.isInsidePlot(e.pageX - chartPosition.left - chart.plotLeft,
  8174. e.pageY - chartPosition.top - chart.plotTop)) {
  8175. this.reset();
  8176. }
  8177. },
  8178. /**
  8179. * When mouse leaves the container, hide the tooltip.
  8180. */
  8181. onContainerMouseLeave: function () {
  8182. this.reset();
  8183. this.chartPosition = null; // also reset the chart position, used in #149 fix
  8184. },
  8185. // The mousemove, touchmove and touchstart event handler
  8186. onContainerMouseMove: function (e) {
  8187. var chart = this.chart;
  8188. // normalize
  8189. e = this.normalize(e);
  8190. // #295
  8191. e.returnValue = false;
  8192. if (chart.mouseIsDown === 'mousedown') {
  8193. this.drag(e);
  8194. }
  8195. // Show the tooltip and run mouse over events (#977)
  8196. if (chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop) && !chart.openMenu) {
  8197. this.runPointActions(e);
  8198. }
  8199. },
  8200. /**
  8201. * Utility to detect whether an element has, or has a parent with, a specific
  8202. * class name. Used on detection of tracker objects and on deciding whether
  8203. * hovering the tooltip should cause the active series to mouse out.
  8204. */
  8205. inClass: function (element, className) {
  8206. var elemClassName;
  8207. while (element) {
  8208. elemClassName = attr(element, 'class');
  8209. if (elemClassName) {
  8210. if (elemClassName.indexOf(className) !== -1) {
  8211. return true;
  8212. } else if (elemClassName.indexOf(PREFIX + 'container') !== -1) {
  8213. return false;
  8214. }
  8215. }
  8216. element = element.parentNode;
  8217. }
  8218. },
  8219. onTrackerMouseOut: function (e) {
  8220. var series = this.chart.hoverSeries;
  8221. if (series && !series.options.stickyTracking && !this.inClass(e.toElement || e.relatedTarget, PREFIX + 'tooltip')) {
  8222. series.onMouseOut();
  8223. }
  8224. },
  8225. onContainerClick: function (e) {
  8226. var chart = this.chart,
  8227. hoverPoint = chart.hoverPoint,
  8228. plotLeft = chart.plotLeft,
  8229. plotTop = chart.plotTop,
  8230. inverted = chart.inverted,
  8231. chartPosition,
  8232. plotX,
  8233. plotY;
  8234. e = this.normalize(e);
  8235. e.cancelBubble = true; // IE specific
  8236. if (!chart.cancelClick) {
  8237. // On tracker click, fire the series and point events. #783, #1583
  8238. if (hoverPoint && this.inClass(e.target, PREFIX + 'tracker')) {
  8239. chartPosition = this.chartPosition;
  8240. plotX = hoverPoint.plotX;
  8241. plotY = hoverPoint.plotY;
  8242. // add page position info
  8243. extend(hoverPoint, {
  8244. pageX: chartPosition.left + plotLeft +
  8245. (inverted ? chart.plotWidth - plotY : plotX),
  8246. pageY: chartPosition.top + plotTop +
  8247. (inverted ? chart.plotHeight - plotX : plotY)
  8248. });
  8249. // the series click event
  8250. fireEvent(hoverPoint.series, 'click', extend(e, {
  8251. point: hoverPoint
  8252. }));
  8253. // the point click event
  8254. if (chart.hoverPoint) { // it may be destroyed (#1844)
  8255. hoverPoint.firePointEvent('click', e);
  8256. }
  8257. // When clicking outside a tracker, fire a chart event
  8258. } else {
  8259. extend(e, this.getCoordinates(e));
  8260. // fire a click event in the chart
  8261. if (chart.isInsidePlot(e.chartX - plotLeft, e.chartY - plotTop)) {
  8262. fireEvent(chart, 'click', e);
  8263. }
  8264. }
  8265. }
  8266. },
  8267. onContainerTouchStart: function (e) {
  8268. var chart = this.chart;
  8269. if (e.touches.length === 1) {
  8270. e = this.normalize(e);
  8271. if (chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop)) {
  8272. // Prevent the click pseudo event from firing unless it is set in the options
  8273. /*if (!chart.runChartClick) {
  8274. e.preventDefault();
  8275. }*/
  8276. // Run mouse events and display tooltip etc
  8277. this.runPointActions(e);
  8278. this.pinch(e);
  8279. }
  8280. } else if (e.touches.length === 2) {
  8281. this.pinch(e);
  8282. }
  8283. },
  8284. onContainerTouchMove: function (e) {
  8285. if (e.touches.length === 1 || e.touches.length === 2) {
  8286. this.pinch(e);
  8287. }
  8288. },
  8289. onDocumentTouchEnd: function (e) {
  8290. this.drop(e);
  8291. },
  8292. /**
  8293. * Set the JS DOM events on the container and document. This method should contain
  8294. * a one-to-one assignment between methods and their handlers. Any advanced logic should
  8295. * be moved to the handler reflecting the event's name.
  8296. */
  8297. setDOMEvents: function () {
  8298. var pointer = this,
  8299. container = pointer.chart.container,
  8300. events;
  8301. this._events = events = [
  8302. [container, 'onmousedown', 'onContainerMouseDown'],
  8303. [container, 'onmousemove', 'onContainerMouseMove'],
  8304. [container, 'onclick', 'onContainerClick'],
  8305. [container, 'mouseleave', 'onContainerMouseLeave'],
  8306. [doc, 'mousemove', 'onDocumentMouseMove'],
  8307. [doc, 'mouseup', 'onDocumentMouseUp']
  8308. ];
  8309. if (hasTouch) {
  8310. events.push(
  8311. [container, 'ontouchstart', 'onContainerTouchStart'],
  8312. [container, 'ontouchmove', 'onContainerTouchMove'],
  8313. [doc, 'touchend', 'onDocumentTouchEnd']
  8314. );
  8315. }
  8316. each(events, function (eventConfig) {
  8317. // First, create the callback function that in turn calls the method on Pointer
  8318. pointer['_' + eventConfig[2]] = function (e) {
  8319. pointer[eventConfig[2]](e);
  8320. };
  8321. // Now attach the function, either as a direct property or through addEvent
  8322. if (eventConfig[1].indexOf('on') === 0) {
  8323. eventConfig[0][eventConfig[1]] = pointer['_' + eventConfig[2]];
  8324. } else {
  8325. addEvent(eventConfig[0], eventConfig[1], pointer['_' + eventConfig[2]]);
  8326. }
  8327. });
  8328. },
  8329. /**
  8330. * Destroys the Pointer object and disconnects DOM events.
  8331. */
  8332. destroy: function () {
  8333. var pointer = this;
  8334. // Release all DOM events
  8335. each(pointer._events, function (eventConfig) {
  8336. if (eventConfig[1].indexOf('on') === 0) {
  8337. eventConfig[0][eventConfig[1]] = null; // delete breaks oldIE
  8338. } else {
  8339. removeEvent(eventConfig[0], eventConfig[1], pointer['_' + eventConfig[2]]);
  8340. }
  8341. });
  8342. delete pointer._events;
  8343. // memory and CPU leak
  8344. clearInterval(pointer.tooltipTimeout);
  8345. }
  8346. };
  8347. /**
  8348. * The overview of the chart's series
  8349. */
  8350. function Legend(chart, options) {
  8351. this.init(chart, options);
  8352. }
  8353. Legend.prototype = {
  8354. /**
  8355. * Initialize the legend
  8356. */
  8357. init: function (chart, options) {
  8358. var legend = this,
  8359. itemStyle = options.itemStyle,
  8360. padding = pick(options.padding, 8),
  8361. itemMarginTop = options.itemMarginTop || 0;
  8362. this.options = options;
  8363. if (!options.enabled) {
  8364. return;
  8365. }
  8366. legend.baseline = pInt(itemStyle.fontSize) + 3 + itemMarginTop; // used in Series prototype
  8367. legend.itemStyle = itemStyle;
  8368. legend.itemHiddenStyle = merge(itemStyle, options.itemHiddenStyle);
  8369. legend.itemMarginTop = itemMarginTop;
  8370. legend.padding = padding;
  8371. legend.initialItemX = padding;
  8372. legend.initialItemY = padding - 5; // 5 is the number of pixels above the text
  8373. legend.maxItemWidth = 0;
  8374. legend.chart = chart;
  8375. legend.itemHeight = 0;
  8376. legend.lastLineHeight = 0;
  8377. // Render it
  8378. legend.render();
  8379. // move checkboxes
  8380. addEvent(legend.chart, 'endResize', function () {
  8381. legend.positionCheckboxes();
  8382. });
  8383. },
  8384. /**
  8385. * Set the colors for the legend item
  8386. * @param {Object} item A Series or Point instance
  8387. * @param {Object} visible Dimmed or colored
  8388. */
  8389. colorizeItem: function (item, visible) {
  8390. var legend = this,
  8391. options = legend.options,
  8392. legendItem = item.legendItem,
  8393. legendLine = item.legendLine,
  8394. legendSymbol = item.legendSymbol,
  8395. hiddenColor = legend.itemHiddenStyle.color,
  8396. textColor = visible ? options.itemStyle.color : hiddenColor,
  8397. symbolColor = visible ? item.color : hiddenColor,
  8398. markerOptions = item.options && item.options.marker,
  8399. symbolAttr = {
  8400. stroke: symbolColor,
  8401. fill: symbolColor
  8402. },
  8403. key,
  8404. val;
  8405. if (legendItem) {
  8406. legendItem.css({ fill: textColor, color: textColor }); // color for #1553, oldIE
  8407. }
  8408. if (legendLine) {
  8409. legendLine.attr({ stroke: symbolColor });
  8410. }
  8411. if (legendSymbol) {
  8412. // Apply marker options
  8413. if (markerOptions) {
  8414. markerOptions = item.convertAttribs(markerOptions);
  8415. for (key in markerOptions) {
  8416. val = markerOptions[key];
  8417. if (val !== UNDEFINED) {
  8418. symbolAttr[key] = val;
  8419. }
  8420. }
  8421. }
  8422. legendSymbol.attr(symbolAttr);
  8423. }
  8424. },
  8425. /**
  8426. * Position the legend item
  8427. * @param {Object} item A Series or Point instance
  8428. */
  8429. positionItem: function (item) {
  8430. var legend = this,
  8431. options = legend.options,
  8432. symbolPadding = options.symbolPadding,
  8433. ltr = !options.rtl,
  8434. legendItemPos = item._legendItemPos,
  8435. itemX = legendItemPos[0],
  8436. itemY = legendItemPos[1],
  8437. checkbox = item.checkbox;
  8438. if (item.legendGroup) {
  8439. item.legendGroup.translate(
  8440. ltr ? itemX : legend.legendWidth - itemX - 2 * symbolPadding - 4,
  8441. itemY
  8442. );
  8443. }
  8444. if (checkbox) {
  8445. checkbox.x = itemX;
  8446. checkbox.y = itemY;
  8447. }
  8448. },
  8449. /**
  8450. * Destroy a single legend item
  8451. * @param {Object} item The series or point
  8452. */
  8453. destroyItem: function (item) {
  8454. var checkbox = item.checkbox;
  8455. // destroy SVG elements
  8456. each(['legendItem', 'legendLine', 'legendSymbol', 'legendGroup'], function (key) {
  8457. if (item[key]) {
  8458. item[key].destroy();
  8459. }
  8460. });
  8461. if (checkbox) {
  8462. discardElement(item.checkbox);
  8463. }
  8464. },
  8465. /**
  8466. * Destroys the legend.
  8467. */
  8468. destroy: function () {
  8469. var legend = this,
  8470. legendGroup = legend.group,
  8471. box = legend.box;
  8472. if (box) {
  8473. legend.box = box.destroy();
  8474. }
  8475. if (legendGroup) {
  8476. legend.group = legendGroup.destroy();
  8477. }
  8478. },
  8479. /**
  8480. * Position the checkboxes after the width is determined
  8481. */
  8482. positionCheckboxes: function (scrollOffset) {
  8483. var alignAttr = this.group.alignAttr,
  8484. translateY,
  8485. clipHeight = this.clipHeight || this.legendHeight;
  8486. if (alignAttr) {
  8487. translateY = alignAttr.translateY;
  8488. each(this.allItems, function (item) {
  8489. var checkbox = item.checkbox,
  8490. top;
  8491. if (checkbox) {
  8492. top = (translateY + checkbox.y + (scrollOffset || 0) + 3);
  8493. css(checkbox, {
  8494. left: (alignAttr.translateX + item.legendItemWidth + checkbox.x - 20) + PX,
  8495. top: top + PX,
  8496. display: top > translateY - 6 && top < translateY + clipHeight - 6 ? '' : NONE
  8497. });
  8498. }
  8499. });
  8500. }
  8501. },
  8502. /**
  8503. * Render the legend title on top of the legend
  8504. */
  8505. renderTitle: function () {
  8506. var options = this.options,
  8507. padding = this.padding,
  8508. titleOptions = options.title,
  8509. titleHeight = 0;
  8510. if (titleOptions.text) {
  8511. if (!this.title) {
  8512. this.title = this.chart.renderer.label(titleOptions.text, padding - 3, padding - 4, null, null, null, null, null, 'legend-title')
  8513. .attr({ zIndex: 1 })
  8514. .css(titleOptions.style)
  8515. .add(this.group);
  8516. }
  8517. titleHeight = this.title.getBBox().height;
  8518. this.contentGroup.attr({ translateY: titleHeight });
  8519. }
  8520. this.titleHeight = titleHeight;
  8521. },
  8522. /**
  8523. * Render a single specific legend item
  8524. * @param {Object} item A series or point
  8525. */
  8526. renderItem: function (item) {
  8527. var legend = this,
  8528. chart = legend.chart,
  8529. renderer = chart.renderer,
  8530. options = legend.options,
  8531. horizontal = options.layout === 'horizontal',
  8532. symbolWidth = options.symbolWidth,
  8533. symbolPadding = options.symbolPadding,
  8534. itemStyle = legend.itemStyle,
  8535. itemHiddenStyle = legend.itemHiddenStyle,
  8536. padding = legend.padding,
  8537. ltr = !options.rtl,
  8538. itemHeight,
  8539. widthOption = options.width,
  8540. itemMarginBottom = options.itemMarginBottom || 0,
  8541. itemMarginTop = legend.itemMarginTop,
  8542. initialItemX = legend.initialItemX,
  8543. bBox,
  8544. itemWidth,
  8545. li = item.legendItem,
  8546. series = item.series || item,
  8547. itemOptions = series.options,
  8548. showCheckbox = itemOptions.showCheckbox,
  8549. useHTML = options.useHTML;
  8550. if (!li) { // generate it once, later move it
  8551. // Generate the group box
  8552. // A group to hold the symbol and text. Text is to be appended in Legend class.
  8553. item.legendGroup = renderer.g('legend-item')
  8554. .attr({ zIndex: 1 })
  8555. .add(legend.scrollGroup);
  8556. // Draw the legend symbol inside the group box
  8557. series.drawLegendSymbol(legend, item);
  8558. // Generate the list item text and add it to the group
  8559. item.legendItem = li = renderer.text(
  8560. options.labelFormat ? format(options.labelFormat, item) : options.labelFormatter.call(item),
  8561. ltr ? symbolWidth + symbolPadding : -symbolPadding,
  8562. legend.baseline,
  8563. useHTML
  8564. )
  8565. .css(merge(item.visible ? itemStyle : itemHiddenStyle)) // merge to prevent modifying original (#1021)
  8566. .attr({
  8567. align: ltr ? 'left' : 'right',
  8568. zIndex: 2
  8569. })
  8570. .add(item.legendGroup);
  8571. // Set the events on the item group, or in case of useHTML, the item itself (#1249)
  8572. (useHTML ? li : item.legendGroup).on('mouseover', function () {
  8573. item.setState(HOVER_STATE);
  8574. li.css(legend.options.itemHoverStyle);
  8575. })
  8576. .on('mouseout', function () {
  8577. li.css(item.visible ? itemStyle : itemHiddenStyle);
  8578. item.setState();
  8579. })
  8580. .on('click', function (event) {
  8581. var strLegendItemClick = 'legendItemClick',
  8582. fnLegendItemClick = function () {
  8583. item.setVisible();
  8584. };
  8585. // Pass over the click/touch event. #4.
  8586. event = {
  8587. browserEvent: event
  8588. };
  8589. // click the name or symbol
  8590. if (item.firePointEvent) { // point
  8591. item.firePointEvent(strLegendItemClick, event, fnLegendItemClick);
  8592. } else {
  8593. fireEvent(item, strLegendItemClick, event, fnLegendItemClick);
  8594. }
  8595. });
  8596. // Colorize the items
  8597. legend.colorizeItem(item, item.visible);
  8598. // add the HTML checkbox on top
  8599. if (itemOptions && showCheckbox) {
  8600. item.checkbox = createElement('input', {
  8601. type: 'checkbox',
  8602. checked: item.selected,
  8603. defaultChecked: item.selected // required by IE7
  8604. }, options.itemCheckboxStyle, chart.container);
  8605. addEvent(item.checkbox, 'click', function (event) {
  8606. var target = event.target;
  8607. fireEvent(item, 'checkboxClick', {
  8608. checked: target.checked
  8609. },
  8610. function () {
  8611. item.select();
  8612. }
  8613. );
  8614. });
  8615. }
  8616. }
  8617. // calculate the positions for the next line
  8618. bBox = li.getBBox();
  8619. itemWidth = item.legendItemWidth =
  8620. options.itemWidth || symbolWidth + symbolPadding + bBox.width + padding +
  8621. (showCheckbox ? 20 : 0);
  8622. legend.itemHeight = itemHeight = bBox.height;
  8623. // if the item exceeds the width, start a new line
  8624. if (horizontal && legend.itemX - initialItemX + itemWidth >
  8625. (widthOption || (chart.chartWidth - 2 * padding - initialItemX))) {
  8626. legend.itemX = initialItemX;
  8627. legend.itemY += itemMarginTop + legend.lastLineHeight + itemMarginBottom;
  8628. legend.lastLineHeight = 0; // reset for next line
  8629. }
  8630. // If the item exceeds the height, start a new column
  8631. /*if (!horizontal && legend.itemY + options.y + itemHeight > chart.chartHeight - spacingTop - spacingBottom) {
  8632. legend.itemY = legend.initialItemY;
  8633. legend.itemX += legend.maxItemWidth;
  8634. legend.maxItemWidth = 0;
  8635. }*/
  8636. // Set the edge positions
  8637. legend.maxItemWidth = mathMax(legend.maxItemWidth, itemWidth);
  8638. legend.lastItemY = itemMarginTop + legend.itemY + itemMarginBottom;
  8639. legend.lastLineHeight = mathMax(itemHeight, legend.lastLineHeight); // #915
  8640. // cache the position of the newly generated or reordered items
  8641. item._legendItemPos = [legend.itemX, legend.itemY];
  8642. // advance
  8643. if (horizontal) {
  8644. legend.itemX += itemWidth;
  8645. } else {
  8646. legend.itemY += itemMarginTop + itemHeight + itemMarginBottom;
  8647. legend.lastLineHeight = itemHeight;
  8648. }
  8649. // the width of the widest item
  8650. legend.offsetWidth = widthOption || mathMax(
  8651. horizontal ? legend.itemX - initialItemX : itemWidth,
  8652. legend.offsetWidth
  8653. );
  8654. },
  8655. /**
  8656. * Render the legend. This method can be called both before and after
  8657. * chart.render. If called after, it will only rearrange items instead
  8658. * of creating new ones.
  8659. */
  8660. render: function () {
  8661. var legend = this,
  8662. chart = legend.chart,
  8663. renderer = chart.renderer,
  8664. legendGroup = legend.group,
  8665. allItems,
  8666. display,
  8667. legendWidth,
  8668. legendHeight,
  8669. box = legend.box,
  8670. options = legend.options,
  8671. padding = legend.padding,
  8672. legendBorderWidth = options.borderWidth,
  8673. legendBackgroundColor = options.backgroundColor;
  8674. legend.itemX = legend.initialItemX;
  8675. legend.itemY = legend.initialItemY;
  8676. legend.offsetWidth = 0;
  8677. legend.lastItemY = 0;
  8678. if (!legendGroup) {
  8679. legend.group = legendGroup = renderer.g('legend')
  8680. .attr({ zIndex: 7 })
  8681. .add();
  8682. legend.contentGroup = renderer.g()
  8683. .attr({ zIndex: 1 }) // above background
  8684. .add(legendGroup);
  8685. legend.scrollGroup = renderer.g()
  8686. .add(legend.contentGroup);
  8687. }
  8688. legend.renderTitle();
  8689. // add each series or point
  8690. allItems = [];
  8691. each(chart.series, function (serie) {
  8692. var seriesOptions = serie.options;
  8693. if (!seriesOptions.showInLegend || defined(seriesOptions.linkedTo)) {
  8694. return;
  8695. }
  8696. // use points or series for the legend item depending on legendType
  8697. allItems = allItems.concat(
  8698. serie.legendItems ||
  8699. (seriesOptions.legendType === 'point' ?
  8700. serie.data :
  8701. serie)
  8702. );
  8703. });
  8704. // sort by legendIndex
  8705. stableSort(allItems, function (a, b) {
  8706. return ((a.options && a.options.legendIndex) || 0) - ((b.options && b.options.legendIndex) || 0);
  8707. });
  8708. // reversed legend
  8709. if (options.reversed) {
  8710. allItems.reverse();
  8711. }
  8712. legend.allItems = allItems;
  8713. legend.display = display = !!allItems.length;
  8714. // render the items
  8715. each(allItems, function (item) {
  8716. legend.renderItem(item);
  8717. });
  8718. // Draw the border
  8719. legendWidth = options.width || legend.offsetWidth;
  8720. legendHeight = legend.lastItemY + legend.lastLineHeight + legend.titleHeight;
  8721. legendHeight = legend.handleOverflow(legendHeight);
  8722. if (legendBorderWidth || legendBackgroundColor) {
  8723. legendWidth += padding;
  8724. legendHeight += padding;
  8725. if (!box) {
  8726. legend.box = box = renderer.rect(
  8727. 0,
  8728. 0,
  8729. legendWidth,
  8730. legendHeight,
  8731. options.borderRadius,
  8732. legendBorderWidth || 0
  8733. ).attr({
  8734. stroke: options.borderColor,
  8735. 'stroke-width': legendBorderWidth || 0,
  8736. fill: legendBackgroundColor || NONE
  8737. })
  8738. .add(legendGroup)
  8739. .shadow(options.shadow);
  8740. box.isNew = true;
  8741. } else if (legendWidth > 0 && legendHeight > 0) {
  8742. box[box.isNew ? 'attr' : 'animate'](
  8743. box.crisp(null, null, null, legendWidth, legendHeight)
  8744. );
  8745. box.isNew = false;
  8746. }
  8747. // hide the border if no items
  8748. box[display ? 'show' : 'hide']();
  8749. }
  8750. legend.legendWidth = legendWidth;
  8751. legend.legendHeight = legendHeight;
  8752. // Now that the legend width and height are established, put the items in the
  8753. // final position
  8754. each(allItems, function (item) {
  8755. legend.positionItem(item);
  8756. });
  8757. // 1.x compatibility: positioning based on style
  8758. /*var props = ['left', 'right', 'top', 'bottom'],
  8759. prop,
  8760. i = 4;
  8761. while (i--) {
  8762. prop = props[i];
  8763. if (options.style[prop] && options.style[prop] !== 'auto') {
  8764. options[i < 2 ? 'align' : 'verticalAlign'] = prop;
  8765. options[i < 2 ? 'x' : 'y'] = pInt(options.style[prop]) * (i % 2 ? -1 : 1);
  8766. }
  8767. }*/
  8768. if (display) {
  8769. legendGroup.align(extend({
  8770. width: legendWidth,
  8771. height: legendHeight
  8772. }, options), true, 'spacingBox');
  8773. }
  8774. if (!chart.isResizing) {
  8775. this.positionCheckboxes();
  8776. }
  8777. },
  8778. /**
  8779. * Set up the overflow handling by adding navigation with up and down arrows below the
  8780. * legend.
  8781. */
  8782. handleOverflow: function (legendHeight) {
  8783. var legend = this,
  8784. chart = this.chart,
  8785. renderer = chart.renderer,
  8786. pageCount,
  8787. options = this.options,
  8788. optionsY = options.y,
  8789. alignTop = options.verticalAlign === 'top',
  8790. spaceHeight = chart.spacingBox.height + (alignTop ? -optionsY : optionsY) - this.padding,
  8791. maxHeight = options.maxHeight,
  8792. clipHeight,
  8793. clipRect = this.clipRect,
  8794. navOptions = options.navigation,
  8795. animation = pick(navOptions.animation, true),
  8796. arrowSize = navOptions.arrowSize || 12,
  8797. nav = this.nav;
  8798. // Adjust the height
  8799. if (options.layout === 'horizontal') {
  8800. spaceHeight /= 2;
  8801. }
  8802. if (maxHeight) {
  8803. spaceHeight = mathMin(spaceHeight, maxHeight);
  8804. }
  8805. // Reset the legend height and adjust the clipping rectangle
  8806. if (legendHeight > spaceHeight && !options.useHTML) {
  8807. this.clipHeight = clipHeight = spaceHeight - 20 - this.titleHeight;
  8808. this.pageCount = pageCount = mathCeil(legendHeight / clipHeight);
  8809. this.currentPage = pick(this.currentPage, 1);
  8810. this.fullHeight = legendHeight;
  8811. // Only apply clipping if needed. Clipping causes blurred legend in PDF export (#1787)
  8812. if (!clipRect) {
  8813. clipRect = legend.clipRect = renderer.clipRect(0, 0, 9999, 0);
  8814. legend.contentGroup.clip(clipRect);
  8815. }
  8816. clipRect.attr({
  8817. height: clipHeight
  8818. });
  8819. // Add navigation elements
  8820. if (!nav) {
  8821. this.nav = nav = renderer.g().attr({ zIndex: 1 }).add(this.group);
  8822. this.up = renderer.symbol('triangle', 0, 0, arrowSize, arrowSize)
  8823. .on('click', function () {
  8824. legend.scroll(-1, animation);
  8825. })
  8826. .add(nav);
  8827. this.pager = renderer.text('', 15, 10)
  8828. .css(navOptions.style)
  8829. .add(nav);
  8830. this.down = renderer.symbol('triangle-down', 0, 0, arrowSize, arrowSize)
  8831. .on('click', function () {
  8832. legend.scroll(1, animation);
  8833. })
  8834. .add(nav);
  8835. }
  8836. // Set initial position
  8837. legend.scroll(0);
  8838. legendHeight = spaceHeight;
  8839. } else if (nav) {
  8840. clipRect.attr({
  8841. height: chart.chartHeight
  8842. });
  8843. nav.hide();
  8844. this.scrollGroup.attr({
  8845. translateY: 1
  8846. });
  8847. this.clipHeight = 0; // #1379
  8848. }
  8849. return legendHeight;
  8850. },
  8851. /**
  8852. * Scroll the legend by a number of pages
  8853. * @param {Object} scrollBy
  8854. * @param {Object} animation
  8855. */
  8856. scroll: function (scrollBy, animation) {
  8857. var pageCount = this.pageCount,
  8858. currentPage = this.currentPage + scrollBy,
  8859. clipHeight = this.clipHeight,
  8860. navOptions = this.options.navigation,
  8861. activeColor = navOptions.activeColor,
  8862. inactiveColor = navOptions.inactiveColor,
  8863. pager = this.pager,
  8864. padding = this.padding,
  8865. scrollOffset;
  8866. // When resizing while looking at the last page
  8867. if (currentPage > pageCount) {
  8868. currentPage = pageCount;
  8869. }
  8870. if (currentPage > 0) {
  8871. if (animation !== UNDEFINED) {
  8872. setAnimation(animation, this.chart);
  8873. }
  8874. this.nav.attr({
  8875. translateX: padding,
  8876. translateY: clipHeight + 7 + this.titleHeight,
  8877. visibility: VISIBLE
  8878. });
  8879. this.up.attr({
  8880. fill: currentPage === 1 ? inactiveColor : activeColor
  8881. })
  8882. .css({
  8883. cursor: currentPage === 1 ? 'default' : 'pointer'
  8884. });
  8885. pager.attr({
  8886. text: currentPage + '/' + this.pageCount
  8887. });
  8888. this.down.attr({
  8889. x: 18 + this.pager.getBBox().width, // adjust to text width
  8890. fill: currentPage === pageCount ? inactiveColor : activeColor
  8891. })
  8892. .css({
  8893. cursor: currentPage === pageCount ? 'default' : 'pointer'
  8894. });
  8895. scrollOffset = -mathMin(clipHeight * (currentPage - 1), this.fullHeight - clipHeight + padding) + 1;
  8896. this.scrollGroup.animate({
  8897. translateY: scrollOffset
  8898. });
  8899. pager.attr({
  8900. text: currentPage + '/' + pageCount
  8901. });
  8902. this.currentPage = currentPage;
  8903. this.positionCheckboxes(scrollOffset);
  8904. }
  8905. }
  8906. };
  8907. /**
  8908. * The chart class
  8909. * @param {Object} options
  8910. * @param {Function} callback Function to run when the chart has loaded
  8911. */
  8912. function Chart() {
  8913. this.init.apply(this, arguments);
  8914. }
  8915. Chart.prototype = {
  8916. /**
  8917. * Initialize the chart
  8918. */
  8919. init: function (userOptions, callback) {
  8920. // Handle regular options
  8921. var options,
  8922. seriesOptions = userOptions.series; // skip merging data points to increase performance
  8923. userOptions.series = null;
  8924. options = merge(defaultOptions, userOptions); // do the merge
  8925. options.series = userOptions.series = seriesOptions; // set back the series data
  8926. var optionsChart = options.chart,
  8927. optionsMargin = optionsChart.margin,
  8928. margin = isObject(optionsMargin) ?
  8929. optionsMargin :
  8930. [optionsMargin, optionsMargin, optionsMargin, optionsMargin];
  8931. this.optionsMarginTop = pick(optionsChart.marginTop, margin[0]);
  8932. this.optionsMarginRight = pick(optionsChart.marginRight, margin[1]);
  8933. this.optionsMarginBottom = pick(optionsChart.marginBottom, margin[2]);
  8934. this.optionsMarginLeft = pick(optionsChart.marginLeft, margin[3]);
  8935. var chartEvents = optionsChart.events;
  8936. this.runChartClick = chartEvents && !!chartEvents.click;
  8937. this.bounds = { h: {}, v: {} }; // Pixel data bounds for touch zoom
  8938. this.callback = callback;
  8939. this.isResizing = 0;
  8940. this.options = options;
  8941. //chartTitleOptions = UNDEFINED;
  8942. //chartSubtitleOptions = UNDEFINED;
  8943. this.axes = [];
  8944. this.series = [];
  8945. this.hasCartesianSeries = optionsChart.showAxes;
  8946. //this.axisOffset = UNDEFINED;
  8947. //this.maxTicks = UNDEFINED; // handle the greatest amount of ticks on grouped axes
  8948. //this.inverted = UNDEFINED;
  8949. //this.loadingShown = UNDEFINED;
  8950. //this.container = UNDEFINED;
  8951. //this.chartWidth = UNDEFINED;
  8952. //this.chartHeight = UNDEFINED;
  8953. //this.marginRight = UNDEFINED;
  8954. //this.marginBottom = UNDEFINED;
  8955. //this.containerWidth = UNDEFINED;
  8956. //this.containerHeight = UNDEFINED;
  8957. //this.oldChartWidth = UNDEFINED;
  8958. //this.oldChartHeight = UNDEFINED;
  8959. //this.renderTo = UNDEFINED;
  8960. //this.renderToClone = UNDEFINED;
  8961. //this.spacingBox = UNDEFINED
  8962. //this.legend = UNDEFINED;
  8963. // Elements
  8964. //this.chartBackground = UNDEFINED;
  8965. //this.plotBackground = UNDEFINED;
  8966. //this.plotBGImage = UNDEFINED;
  8967. //this.plotBorder = UNDEFINED;
  8968. //this.loadingDiv = UNDEFINED;
  8969. //this.loadingSpan = UNDEFINED;
  8970. var chart = this,
  8971. eventType;
  8972. // Add the chart to the global lookup
  8973. chart.index = charts.length;
  8974. charts.push(chart);
  8975. // Set up auto resize
  8976. if (optionsChart.reflow !== false) {
  8977. addEvent(chart, 'load', function () {
  8978. chart.initReflow();
  8979. });
  8980. }
  8981. // Chart event handlers
  8982. if (chartEvents) {
  8983. for (eventType in chartEvents) {
  8984. addEvent(chart, eventType, chartEvents[eventType]);
  8985. }
  8986. }
  8987. chart.xAxis = [];
  8988. chart.yAxis = [];
  8989. // Expose methods and variables
  8990. chart.animation = useCanVG ? false : pick(optionsChart.animation, true);
  8991. chart.pointCount = 0;
  8992. chart.counters = new ChartCounters();
  8993. chart.firstRender();
  8994. },
  8995. /**
  8996. * Initialize an individual series, called internally before render time
  8997. */
  8998. initSeries: function (options) {
  8999. var chart = this,
  9000. optionsChart = chart.options.chart,
  9001. type = options.type || optionsChart.type || optionsChart.defaultSeriesType,
  9002. series,
  9003. constr = seriesTypes[type];
  9004. // No such series type
  9005. if (!constr) {
  9006. error(17, true);
  9007. }
  9008. series = new constr();
  9009. series.init(this, options);
  9010. return series;
  9011. },
  9012. /**
  9013. * Add a series dynamically after time
  9014. *
  9015. * @param {Object} options The config options
  9016. * @param {Boolean} redraw Whether to redraw the chart after adding. Defaults to true.
  9017. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  9018. * configuration
  9019. *
  9020. * @return {Object} series The newly created series object
  9021. */
  9022. addSeries: function (options, redraw, animation) {
  9023. var series,
  9024. chart = this;
  9025. if (options) {
  9026. redraw = pick(redraw, true); // defaults to true
  9027. fireEvent(chart, 'addSeries', { options: options }, function () {
  9028. series = chart.initSeries(options);
  9029. chart.isDirtyLegend = true; // the series array is out of sync with the display
  9030. if (redraw) {
  9031. chart.redraw(animation);
  9032. }
  9033. });
  9034. }
  9035. return series;
  9036. },
  9037. /**
  9038. * Add an axis to the chart
  9039. * @param {Object} options The axis option
  9040. * @param {Boolean} isX Whether it is an X axis or a value axis
  9041. */
  9042. addAxis: function (options, isX, redraw, animation) {
  9043. var key = isX ? 'xAxis' : 'yAxis',
  9044. chartOptions = this.options,
  9045. axis;
  9046. /*jslint unused: false*/
  9047. axis = new Axis(this, merge(options, {
  9048. index: this[key].length
  9049. }));
  9050. /*jslint unused: true*/
  9051. // Push the new axis options to the chart options
  9052. chartOptions[key] = splat(chartOptions[key] || {});
  9053. chartOptions[key].push(options);
  9054. if (pick(redraw, true)) {
  9055. this.redraw(animation);
  9056. }
  9057. },
  9058. /**
  9059. * Check whether a given point is within the plot area
  9060. *
  9061. * @param {Number} plotX Pixel x relative to the plot area
  9062. * @param {Number} plotY Pixel y relative to the plot area
  9063. * @param {Boolean} inverted Whether the chart is inverted
  9064. */
  9065. isInsidePlot: function (plotX, plotY, inverted) {
  9066. var x = inverted ? plotY : plotX,
  9067. y = inverted ? plotX : plotY;
  9068. return x >= 0 &&
  9069. x <= this.plotWidth &&
  9070. y >= 0 &&
  9071. y <= this.plotHeight;
  9072. },
  9073. /**
  9074. * Adjust all axes tick amounts
  9075. */
  9076. adjustTickAmounts: function () {
  9077. if (this.options.chart.alignTicks !== false) {
  9078. each(this.axes, function (axis) {
  9079. axis.adjustTickAmount();
  9080. });
  9081. }
  9082. this.maxTicks = null;
  9083. },
  9084. /**
  9085. * Redraw legend, axes or series based on updated data
  9086. *
  9087. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  9088. * configuration
  9089. */
  9090. redraw: function (animation) {
  9091. var chart = this,
  9092. axes = chart.axes,
  9093. series = chart.series,
  9094. pointer = chart.pointer,
  9095. legend = chart.legend,
  9096. redrawLegend = chart.isDirtyLegend,
  9097. hasStackedSeries,
  9098. isDirtyBox = chart.isDirtyBox, // todo: check if it has actually changed?
  9099. seriesLength = series.length,
  9100. i = seriesLength,
  9101. serie,
  9102. renderer = chart.renderer,
  9103. isHiddenChart = renderer.isHidden(),
  9104. afterRedraw = [];
  9105. setAnimation(animation, chart);
  9106. if (isHiddenChart) {
  9107. chart.cloneRenderTo();
  9108. }
  9109. // link stacked series
  9110. while (i--) {
  9111. serie = series[i];
  9112. if (serie.isDirty && serie.options.stacking) {
  9113. hasStackedSeries = true;
  9114. break;
  9115. }
  9116. }
  9117. if (hasStackedSeries) { // mark others as dirty
  9118. i = seriesLength;
  9119. while (i--) {
  9120. serie = series[i];
  9121. if (serie.options.stacking) {
  9122. serie.isDirty = true;
  9123. }
  9124. }
  9125. }
  9126. // handle updated data in the series
  9127. each(series, function (serie) {
  9128. if (serie.isDirty) { // prepare the data so axis can read it
  9129. if (serie.options.legendType === 'point') {
  9130. redrawLegend = true;
  9131. }
  9132. }
  9133. });
  9134. // handle added or removed series
  9135. if (redrawLegend && legend.options.enabled) { // series or pie points are added or removed
  9136. // draw legend graphics
  9137. legend.render();
  9138. chart.isDirtyLegend = false;
  9139. }
  9140. if (chart.hasCartesianSeries) {
  9141. if (!chart.isResizing) {
  9142. // reset maxTicks
  9143. chart.maxTicks = null;
  9144. // set axes scales
  9145. each(axes, function (axis) {
  9146. axis.setScale();
  9147. });
  9148. }
  9149. chart.adjustTickAmounts();
  9150. chart.getMargins();
  9151. // redraw axes
  9152. each(axes, function (axis) {
  9153. // Fire 'afterSetExtremes' only if extremes are set
  9154. if (axis.isDirtyExtremes) { // #821
  9155. axis.isDirtyExtremes = false;
  9156. afterRedraw.push(function () { // prevent a recursive call to chart.redraw() (#1119)
  9157. fireEvent(axis, 'afterSetExtremes', axis.getExtremes()); // #747, #751
  9158. });
  9159. }
  9160. if (axis.isDirty || isDirtyBox || hasStackedSeries) {
  9161. axis.redraw();
  9162. isDirtyBox = true; // #792
  9163. }
  9164. });
  9165. }
  9166. // the plot areas size has changed
  9167. if (isDirtyBox) {
  9168. chart.drawChartBox();
  9169. }
  9170. // redraw affected series
  9171. each(series, function (serie) {
  9172. if (serie.isDirty && serie.visible &&
  9173. (!serie.isCartesian || serie.xAxis)) { // issue #153
  9174. serie.redraw();
  9175. }
  9176. });
  9177. // move tooltip or reset
  9178. if (pointer && pointer.reset) {
  9179. pointer.reset(true);
  9180. }
  9181. // redraw if canvas
  9182. renderer.draw();
  9183. // fire the event
  9184. fireEvent(chart, 'redraw'); // jQuery breaks this when calling it from addEvent. Overwrites chart.redraw
  9185. if (isHiddenChart) {
  9186. chart.cloneRenderTo(true);
  9187. }
  9188. // Fire callbacks that are put on hold until after the redraw
  9189. each(afterRedraw, function (callback) {
  9190. callback.call();
  9191. });
  9192. },
  9193. /**
  9194. * Dim the chart and show a loading text or symbol
  9195. * @param {String} str An optional text to show in the loading label instead of the default one
  9196. */
  9197. showLoading: function (str) {
  9198. var chart = this,
  9199. options = chart.options,
  9200. loadingDiv = chart.loadingDiv;
  9201. var loadingOptions = options.loading;
  9202. // create the layer at the first call
  9203. if (!loadingDiv) {
  9204. chart.loadingDiv = loadingDiv = createElement(DIV, {
  9205. className: PREFIX + 'loading'
  9206. }, extend(loadingOptions.style, {
  9207. zIndex: 10,
  9208. display: NONE
  9209. }), chart.container);
  9210. chart.loadingSpan = createElement(
  9211. 'span',
  9212. null,
  9213. loadingOptions.labelStyle,
  9214. loadingDiv
  9215. );
  9216. }
  9217. // update text
  9218. chart.loadingSpan.innerHTML = str || options.lang.loading;
  9219. // show it
  9220. if (!chart.loadingShown) {
  9221. css(loadingDiv, {
  9222. opacity: 0,
  9223. display: '',
  9224. left: chart.plotLeft + PX,
  9225. top: chart.plotTop + PX,
  9226. width: chart.plotWidth + PX,
  9227. height: chart.plotHeight + PX
  9228. });
  9229. animate(loadingDiv, {
  9230. opacity: loadingOptions.style.opacity
  9231. }, {
  9232. duration: loadingOptions.showDuration || 0
  9233. });
  9234. chart.loadingShown = true;
  9235. }
  9236. },
  9237. /**
  9238. * Hide the loading layer
  9239. */
  9240. hideLoading: function () {
  9241. var options = this.options,
  9242. loadingDiv = this.loadingDiv;
  9243. if (loadingDiv) {
  9244. animate(loadingDiv, {
  9245. opacity: 0
  9246. }, {
  9247. duration: options.loading.hideDuration || 100,
  9248. complete: function () {
  9249. css(loadingDiv, { display: NONE });
  9250. }
  9251. });
  9252. }
  9253. this.loadingShown = false;
  9254. },
  9255. /**
  9256. * Get an axis, series or point object by id.
  9257. * @param id {String} The id as given in the configuration options
  9258. */
  9259. get: function (id) {
  9260. var chart = this,
  9261. axes = chart.axes,
  9262. series = chart.series;
  9263. var i,
  9264. j,
  9265. points;
  9266. // search axes
  9267. for (i = 0; i < axes.length; i++) {
  9268. if (axes[i].options.id === id) {
  9269. return axes[i];
  9270. }
  9271. }
  9272. // search series
  9273. for (i = 0; i < series.length; i++) {
  9274. if (series[i].options.id === id) {
  9275. return series[i];
  9276. }
  9277. }
  9278. // search points
  9279. for (i = 0; i < series.length; i++) {
  9280. points = series[i].points || [];
  9281. for (j = 0; j < points.length; j++) {
  9282. if (points[j].id === id) {
  9283. return points[j];
  9284. }
  9285. }
  9286. }
  9287. return null;
  9288. },
  9289. /**
  9290. * Create the Axis instances based on the config options
  9291. */
  9292. getAxes: function () {
  9293. var chart = this,
  9294. options = this.options,
  9295. xAxisOptions = options.xAxis = splat(options.xAxis || {}),
  9296. yAxisOptions = options.yAxis = splat(options.yAxis || {}),
  9297. optionsArray,
  9298. axis;
  9299. // make sure the options are arrays and add some members
  9300. each(xAxisOptions, function (axis, i) {
  9301. axis.index = i;
  9302. axis.isX = true;
  9303. });
  9304. each(yAxisOptions, function (axis, i) {
  9305. axis.index = i;
  9306. });
  9307. // concatenate all axis options into one array
  9308. optionsArray = xAxisOptions.concat(yAxisOptions);
  9309. each(optionsArray, function (axisOptions) {
  9310. axis = new Axis(chart, axisOptions);
  9311. });
  9312. chart.adjustTickAmounts();
  9313. },
  9314. /**
  9315. * Get the currently selected points from all series
  9316. */
  9317. getSelectedPoints: function () {
  9318. var points = [];
  9319. each(this.series, function (serie) {
  9320. points = points.concat(grep(serie.points || [], function (point) {
  9321. return point.selected;
  9322. }));
  9323. });
  9324. return points;
  9325. },
  9326. /**
  9327. * Get the currently selected series
  9328. */
  9329. getSelectedSeries: function () {
  9330. return grep(this.series, function (serie) {
  9331. return serie.selected;
  9332. });
  9333. },
  9334. /**
  9335. * Display the zoom button
  9336. */
  9337. showResetZoom: function () {
  9338. var chart = this,
  9339. lang = defaultOptions.lang,
  9340. btnOptions = chart.options.chart.resetZoomButton,
  9341. theme = btnOptions.theme,
  9342. states = theme.states,
  9343. alignTo = btnOptions.relativeTo === 'chart' ? null : 'plotBox';
  9344. this.resetZoomButton = chart.renderer.button(lang.resetZoom, null, null, function () { chart.zoomOut(); }, theme, states && states.hover)
  9345. .attr({
  9346. align: btnOptions.position.align,
  9347. title: lang.resetZoomTitle
  9348. })
  9349. .add()
  9350. .align(btnOptions.position, false, alignTo);
  9351. },
  9352. /**
  9353. * Zoom out to 1:1
  9354. */
  9355. zoomOut: function () {
  9356. var chart = this;
  9357. fireEvent(chart, 'selection', { resetSelection: true }, function () {
  9358. chart.zoom();
  9359. });
  9360. },
  9361. /**
  9362. * Zoom into a given portion of the chart given by axis coordinates
  9363. * @param {Object} event
  9364. */
  9365. zoom: function (event) {
  9366. var chart = this,
  9367. hasZoomed,
  9368. pointer = chart.pointer,
  9369. displayButton = false,
  9370. resetZoomButton;
  9371. // If zoom is called with no arguments, reset the axes
  9372. if (!event || event.resetSelection) {
  9373. each(chart.axes, function (axis) {
  9374. hasZoomed = axis.zoom();
  9375. });
  9376. } else { // else, zoom in on all axes
  9377. each(event.xAxis.concat(event.yAxis), function (axisData) {
  9378. var axis = axisData.axis,
  9379. isXAxis = axis.isXAxis;
  9380. // don't zoom more than minRange
  9381. if (pointer[isXAxis ? 'zoomX' : 'zoomY'] || pointer[isXAxis ? 'pinchX' : 'pinchY']) {
  9382. hasZoomed = axis.zoom(axisData.min, axisData.max);
  9383. if (axis.displayBtn) {
  9384. displayButton = true;
  9385. }
  9386. }
  9387. });
  9388. }
  9389. // Show or hide the Reset zoom button
  9390. resetZoomButton = chart.resetZoomButton;
  9391. if (displayButton && !resetZoomButton) {
  9392. chart.showResetZoom();
  9393. } else if (!displayButton && isObject(resetZoomButton)) {
  9394. chart.resetZoomButton = resetZoomButton.destroy();
  9395. }
  9396. // Redraw
  9397. if (hasZoomed) {
  9398. chart.redraw(
  9399. pick(chart.options.chart.animation, event && event.animation, chart.pointCount < 100) // animation
  9400. );
  9401. }
  9402. },
  9403. /**
  9404. * Pan the chart by dragging the mouse across the pane. This function is called
  9405. * on mouse move, and the distance to pan is computed from chartX compared to
  9406. * the first chartX position in the dragging operation.
  9407. */
  9408. pan: function (chartX) {
  9409. var chart = this,
  9410. xAxis = chart.xAxis[0],
  9411. mouseDownX = chart.mouseDownX,
  9412. halfPointRange = xAxis.pointRange / 2,
  9413. extremes = xAxis.getExtremes(),
  9414. newMin = xAxis.translate(mouseDownX - chartX, true) + halfPointRange,
  9415. newMax = xAxis.translate(mouseDownX + chart.plotWidth - chartX, true) - halfPointRange,
  9416. hoverPoints = chart.hoverPoints;
  9417. // remove active points for shared tooltip
  9418. if (hoverPoints) {
  9419. each(hoverPoints, function (point) {
  9420. point.setState();
  9421. });
  9422. }
  9423. if (xAxis.series.length && newMin > mathMin(extremes.dataMin, extremes.min) && newMax < mathMax(extremes.dataMax, extremes.max)) {
  9424. xAxis.setExtremes(newMin, newMax, true, false, { trigger: 'pan' });
  9425. }
  9426. chart.mouseDownX = chartX; // set new reference for next run
  9427. css(chart.container, { cursor: 'move' });
  9428. },
  9429. /**
  9430. * Show the title and subtitle of the chart
  9431. *
  9432. * @param titleOptions {Object} New title options
  9433. * @param subtitleOptions {Object} New subtitle options
  9434. *
  9435. */
  9436. setTitle: function (titleOptions, subtitleOptions) {
  9437. var chart = this,
  9438. options = chart.options,
  9439. chartTitleOptions,
  9440. chartSubtitleOptions;
  9441. chartTitleOptions = options.title = merge(options.title, titleOptions);
  9442. chartSubtitleOptions = options.subtitle = merge(options.subtitle, subtitleOptions);
  9443. // add title and subtitle
  9444. each([
  9445. ['title', titleOptions, chartTitleOptions],
  9446. ['subtitle', subtitleOptions, chartSubtitleOptions]
  9447. ], function (arr) {
  9448. var name = arr[0],
  9449. title = chart[name],
  9450. titleOptions = arr[1],
  9451. chartTitleOptions = arr[2];
  9452. if (title && titleOptions) {
  9453. chart[name] = title = title.destroy(); // remove old
  9454. }
  9455. if (chartTitleOptions && chartTitleOptions.text && !title) {
  9456. chart[name] = chart.renderer.text(
  9457. chartTitleOptions.text,
  9458. 0,
  9459. 0,
  9460. chartTitleOptions.useHTML
  9461. )
  9462. .attr({
  9463. align: chartTitleOptions.align,
  9464. 'class': PREFIX + name,
  9465. zIndex: chartTitleOptions.zIndex || 4
  9466. })
  9467. .css(chartTitleOptions.style)
  9468. .add()
  9469. .align(chartTitleOptions, false, 'spacingBox');
  9470. }
  9471. });
  9472. },
  9473. /**
  9474. * Get chart width and height according to options and container size
  9475. */
  9476. getChartSize: function () {
  9477. var chart = this,
  9478. optionsChart = chart.options.chart,
  9479. renderTo = chart.renderToClone || chart.renderTo;
  9480. // get inner width and height from jQuery (#824)
  9481. chart.containerWidth = adapterRun(renderTo, 'width');
  9482. chart.containerHeight = adapterRun(renderTo, 'height');
  9483. chart.chartWidth = mathMax(0, optionsChart.width || chart.containerWidth || 600); // #1393, 1460
  9484. chart.chartHeight = mathMax(0, pick(optionsChart.height,
  9485. // the offsetHeight of an empty container is 0 in standard browsers, but 19 in IE7:
  9486. chart.containerHeight > 19 ? chart.containerHeight : 400));
  9487. },
  9488. /**
  9489. * Create a clone of the chart's renderTo div and place it outside the viewport to allow
  9490. * size computation on chart.render and chart.redraw
  9491. */
  9492. cloneRenderTo: function (revert) {
  9493. var clone = this.renderToClone,
  9494. container = this.container;
  9495. // Destroy the clone and bring the container back to the real renderTo div
  9496. if (revert) {
  9497. if (clone) {
  9498. this.renderTo.appendChild(container);
  9499. discardElement(clone);
  9500. delete this.renderToClone;
  9501. }
  9502. // Set up the clone
  9503. } else {
  9504. if (container) {
  9505. this.renderTo.removeChild(container); // do not clone this
  9506. }
  9507. this.renderToClone = clone = this.renderTo.cloneNode(0);
  9508. css(clone, {
  9509. position: ABSOLUTE,
  9510. top: '-9999px',
  9511. display: 'block' // #833
  9512. });
  9513. doc.body.appendChild(clone);
  9514. if (container) {
  9515. clone.appendChild(container);
  9516. }
  9517. }
  9518. },
  9519. /**
  9520. * Get the containing element, determine the size and create the inner container
  9521. * div to hold the chart
  9522. */
  9523. getContainer: function () {
  9524. var chart = this,
  9525. container,
  9526. optionsChart = chart.options.chart,
  9527. chartWidth,
  9528. chartHeight,
  9529. renderTo,
  9530. indexAttrName = 'data-highcharts-chart',
  9531. oldChartIndex,
  9532. containerId;
  9533. chart.renderTo = renderTo = optionsChart.renderTo;
  9534. containerId = PREFIX + idCounter++;
  9535. if (isString(renderTo)) {
  9536. chart.renderTo = renderTo = doc.getElementById(renderTo);
  9537. }
  9538. // Display an error if the renderTo is wrong
  9539. if (!renderTo) {
  9540. error(13, true);
  9541. }
  9542. // If the container already holds a chart, destroy it
  9543. oldChartIndex = pInt(attr(renderTo, indexAttrName));
  9544. if (!isNaN(oldChartIndex) && charts[oldChartIndex]) {
  9545. charts[oldChartIndex].destroy();
  9546. }
  9547. // Make a reference to the chart from the div
  9548. attr(renderTo, indexAttrName, chart.index);
  9549. // remove previous chart
  9550. renderTo.innerHTML = '';
  9551. // If the container doesn't have an offsetWidth, it has or is a child of a node
  9552. // that has display:none. We need to temporarily move it out to a visible
  9553. // state to determine the size, else the legend and tooltips won't render
  9554. // properly
  9555. if (!renderTo.offsetWidth) {
  9556. chart.cloneRenderTo();
  9557. }
  9558. // get the width and height
  9559. chart.getChartSize();
  9560. chartWidth = chart.chartWidth;
  9561. chartHeight = chart.chartHeight;
  9562. // create the inner container
  9563. chart.container = container = createElement(DIV, {
  9564. className: PREFIX + 'container' +
  9565. (optionsChart.className ? ' ' + optionsChart.className : ''),
  9566. id: containerId
  9567. }, extend({
  9568. position: RELATIVE,
  9569. overflow: HIDDEN, // needed for context menu (avoid scrollbars) and
  9570. // content overflow in IE
  9571. width: chartWidth + PX,
  9572. height: chartHeight + PX,
  9573. textAlign: 'left',
  9574. lineHeight: 'normal', // #427
  9575. zIndex: 0, // #1072
  9576. '-webkit-tap-highlight-color': 'rgba(0,0,0,0)'
  9577. }, optionsChart.style),
  9578. chart.renderToClone || renderTo
  9579. );
  9580. // cache the cursor (#1650)
  9581. chart._cursor = container.style.cursor;
  9582. chart.renderer =
  9583. optionsChart.forExport ? // force SVG, used for SVG export
  9584. new SVGRenderer(container, chartWidth, chartHeight, true) :
  9585. new Renderer(container, chartWidth, chartHeight);
  9586. if (useCanVG) {
  9587. // If we need canvg library, extend and configure the renderer
  9588. // to get the tracker for translating mouse events
  9589. chart.renderer.create(chart, container, chartWidth, chartHeight);
  9590. }
  9591. },
  9592. /**
  9593. * Calculate margins by rendering axis labels in a preliminary position. Title,
  9594. * subtitle and legend have already been rendered at this stage, but will be
  9595. * moved into their final positions
  9596. */
  9597. getMargins: function () {
  9598. var chart = this,
  9599. optionsChart = chart.options.chart,
  9600. spacingTop = optionsChart.spacingTop,
  9601. spacingRight = optionsChart.spacingRight,
  9602. spacingBottom = optionsChart.spacingBottom,
  9603. spacingLeft = optionsChart.spacingLeft,
  9604. axisOffset,
  9605. legend = chart.legend,
  9606. optionsMarginTop = chart.optionsMarginTop,
  9607. optionsMarginLeft = chart.optionsMarginLeft,
  9608. optionsMarginRight = chart.optionsMarginRight,
  9609. optionsMarginBottom = chart.optionsMarginBottom,
  9610. chartTitleOptions = chart.options.title,
  9611. chartSubtitleOptions = chart.options.subtitle,
  9612. legendOptions = chart.options.legend,
  9613. legendMargin = pick(legendOptions.margin, 10),
  9614. legendX = legendOptions.x,
  9615. legendY = legendOptions.y,
  9616. align = legendOptions.align,
  9617. verticalAlign = legendOptions.verticalAlign,
  9618. titleOffset;
  9619. chart.resetMargins();
  9620. axisOffset = chart.axisOffset;
  9621. // adjust for title and subtitle
  9622. if ((chart.title || chart.subtitle) && !defined(chart.optionsMarginTop)) {
  9623. titleOffset = mathMax(
  9624. (chart.title && !chartTitleOptions.floating && !chartTitleOptions.verticalAlign && chartTitleOptions.y) || 0,
  9625. (chart.subtitle && !chartSubtitleOptions.floating && !chartSubtitleOptions.verticalAlign && chartSubtitleOptions.y) || 0
  9626. );
  9627. if (titleOffset) {
  9628. chart.plotTop = mathMax(chart.plotTop, titleOffset + pick(chartTitleOptions.margin, 15) + spacingTop);
  9629. }
  9630. }
  9631. // adjust for legend
  9632. if (legend.display && !legendOptions.floating) {
  9633. if (align === 'right') { // horizontal alignment handled first
  9634. if (!defined(optionsMarginRight)) {
  9635. chart.marginRight = mathMax(
  9636. chart.marginRight,
  9637. legend.legendWidth - legendX + legendMargin + spacingRight
  9638. );
  9639. }
  9640. } else if (align === 'left') {
  9641. if (!defined(optionsMarginLeft)) {
  9642. chart.plotLeft = mathMax(
  9643. chart.plotLeft,
  9644. legend.legendWidth + legendX + legendMargin + spacingLeft
  9645. );
  9646. }
  9647. } else if (verticalAlign === 'top') {
  9648. if (!defined(optionsMarginTop)) {
  9649. chart.plotTop = mathMax(
  9650. chart.plotTop,
  9651. legend.legendHeight + legendY + legendMargin + spacingTop
  9652. );
  9653. }
  9654. } else if (verticalAlign === 'bottom') {
  9655. if (!defined(optionsMarginBottom)) {
  9656. chart.marginBottom = mathMax(
  9657. chart.marginBottom,
  9658. legend.legendHeight - legendY + legendMargin + spacingBottom
  9659. );
  9660. }
  9661. }
  9662. }
  9663. // adjust for scroller
  9664. if (chart.extraBottomMargin) {
  9665. chart.marginBottom += chart.extraBottomMargin;
  9666. }
  9667. if (chart.extraTopMargin) {
  9668. chart.plotTop += chart.extraTopMargin;
  9669. }
  9670. // pre-render axes to get labels offset width
  9671. if (chart.hasCartesianSeries) {
  9672. each(chart.axes, function (axis) {
  9673. axis.getOffset();
  9674. });
  9675. }
  9676. if (!defined(optionsMarginLeft)) {
  9677. chart.plotLeft += axisOffset[3];
  9678. }
  9679. if (!defined(optionsMarginTop)) {
  9680. chart.plotTop += axisOffset[0];
  9681. }
  9682. if (!defined(optionsMarginBottom)) {
  9683. chart.marginBottom += axisOffset[2];
  9684. }
  9685. if (!defined(optionsMarginRight)) {
  9686. chart.marginRight += axisOffset[1];
  9687. }
  9688. chart.setChartSize();
  9689. },
  9690. /**
  9691. * Add the event handlers necessary for auto resizing
  9692. *
  9693. */
  9694. initReflow: function () {
  9695. var chart = this,
  9696. optionsChart = chart.options.chart,
  9697. renderTo = chart.renderTo,
  9698. reflowTimeout;
  9699. function reflow(e) {
  9700. var width = optionsChart.width || adapterRun(renderTo, 'width'),
  9701. height = optionsChart.height || adapterRun(renderTo, 'height'),
  9702. target = e ? e.target : win; // #805 - MooTools doesn't supply e
  9703. // Width and height checks for display:none. Target is doc in IE8 and Opera,
  9704. // win in Firefox, Chrome and IE9.
  9705. if (!chart.hasUserSize && width && height && (target === win || target === doc)) {
  9706. if (width !== chart.containerWidth || height !== chart.containerHeight) {
  9707. clearTimeout(reflowTimeout);
  9708. chart.reflowTimeout = reflowTimeout = setTimeout(function () {
  9709. if (chart.container) { // It may have been destroyed in the meantime (#1257)
  9710. chart.setSize(width, height, false);
  9711. chart.hasUserSize = null;
  9712. }
  9713. }, 100);
  9714. }
  9715. chart.containerWidth = width;
  9716. chart.containerHeight = height;
  9717. }
  9718. }
  9719. addEvent(win, 'resize', reflow);
  9720. addEvent(chart, 'destroy', function () {
  9721. removeEvent(win, 'resize', reflow);
  9722. });
  9723. },
  9724. /**
  9725. * Resize the chart to a given width and height
  9726. * @param {Number} width
  9727. * @param {Number} height
  9728. * @param {Object|Boolean} animation
  9729. */
  9730. setSize: function (width, height, animation) {
  9731. var chart = this,
  9732. chartWidth,
  9733. chartHeight,
  9734. fireEndResize;
  9735. // Handle the isResizing counter
  9736. chart.isResizing += 1;
  9737. fireEndResize = function () {
  9738. if (chart) {
  9739. fireEvent(chart, 'endResize', null, function () {
  9740. chart.isResizing -= 1;
  9741. });
  9742. }
  9743. };
  9744. // set the animation for the current process
  9745. setAnimation(animation, chart);
  9746. chart.oldChartHeight = chart.chartHeight;
  9747. chart.oldChartWidth = chart.chartWidth;
  9748. if (defined(width)) {
  9749. chart.chartWidth = chartWidth = mathMax(0, mathRound(width));
  9750. chart.hasUserSize = !!chartWidth;
  9751. }
  9752. if (defined(height)) {
  9753. chart.chartHeight = chartHeight = mathMax(0, mathRound(height));
  9754. }
  9755. css(chart.container, {
  9756. width: chartWidth + PX,
  9757. height: chartHeight + PX
  9758. });
  9759. chart.setChartSize(true);
  9760. chart.renderer.setSize(chartWidth, chartHeight, animation);
  9761. // handle axes
  9762. chart.maxTicks = null;
  9763. each(chart.axes, function (axis) {
  9764. axis.isDirty = true;
  9765. axis.setScale();
  9766. });
  9767. // make sure non-cartesian series are also handled
  9768. each(chart.series, function (serie) {
  9769. serie.isDirty = true;
  9770. });
  9771. chart.isDirtyLegend = true; // force legend redraw
  9772. chart.isDirtyBox = true; // force redraw of plot and chart border
  9773. chart.getMargins();
  9774. chart.redraw(animation);
  9775. chart.oldChartHeight = null;
  9776. fireEvent(chart, 'resize');
  9777. // fire endResize and set isResizing back
  9778. // If animation is disabled, fire without delay
  9779. if (globalAnimation === false) {
  9780. fireEndResize();
  9781. } else { // else set a timeout with the animation duration
  9782. setTimeout(fireEndResize, (globalAnimation && globalAnimation.duration) || 500);
  9783. }
  9784. },
  9785. /**
  9786. * Set the public chart properties. This is done before and after the pre-render
  9787. * to determine margin sizes
  9788. */
  9789. setChartSize: function (skipAxes) {
  9790. var chart = this,
  9791. inverted = chart.inverted,
  9792. renderer = chart.renderer,
  9793. chartWidth = chart.chartWidth,
  9794. chartHeight = chart.chartHeight,
  9795. optionsChart = chart.options.chart,
  9796. spacingTop = optionsChart.spacingTop,
  9797. spacingRight = optionsChart.spacingRight,
  9798. spacingBottom = optionsChart.spacingBottom,
  9799. spacingLeft = optionsChart.spacingLeft,
  9800. clipOffset = chart.clipOffset,
  9801. clipX,
  9802. clipY,
  9803. plotLeft,
  9804. plotTop,
  9805. plotWidth,
  9806. plotHeight,
  9807. plotBorderWidth;
  9808. chart.plotLeft = plotLeft = mathRound(chart.plotLeft);
  9809. chart.plotTop = plotTop = mathRound(chart.plotTop);
  9810. chart.plotWidth = plotWidth = mathMax(0, mathRound(chartWidth - plotLeft - chart.marginRight));
  9811. chart.plotHeight = plotHeight = mathMax(0, mathRound(chartHeight - plotTop - chart.marginBottom));
  9812. chart.plotSizeX = inverted ? plotHeight : plotWidth;
  9813. chart.plotSizeY = inverted ? plotWidth : plotHeight;
  9814. chart.plotBorderWidth = plotBorderWidth = optionsChart.plotBorderWidth || 0;
  9815. // Set boxes used for alignment
  9816. chart.spacingBox = renderer.spacingBox = {
  9817. x: spacingLeft,
  9818. y: spacingTop,
  9819. width: chartWidth - spacingLeft - spacingRight,
  9820. height: chartHeight - spacingTop - spacingBottom
  9821. };
  9822. chart.plotBox = renderer.plotBox = {
  9823. x: plotLeft,
  9824. y: plotTop,
  9825. width: plotWidth,
  9826. height: plotHeight
  9827. };
  9828. clipX = mathCeil(mathMax(plotBorderWidth, clipOffset[3]) / 2);
  9829. clipY = mathCeil(mathMax(plotBorderWidth, clipOffset[0]) / 2);
  9830. chart.clipBox = {
  9831. x: clipX,
  9832. y: clipY,
  9833. width: mathFloor(chart.plotSizeX - mathMax(plotBorderWidth, clipOffset[1]) / 2 - clipX),
  9834. height: mathFloor(chart.plotSizeY - mathMax(plotBorderWidth, clipOffset[2]) / 2 - clipY)
  9835. };
  9836. if (!skipAxes) {
  9837. each(chart.axes, function (axis) {
  9838. axis.setAxisSize();
  9839. axis.setAxisTranslation();
  9840. });
  9841. }
  9842. },
  9843. /**
  9844. * Initial margins before auto size margins are applied
  9845. */
  9846. resetMargins: function () {
  9847. var chart = this,
  9848. optionsChart = chart.options.chart,
  9849. spacingTop = optionsChart.spacingTop,
  9850. spacingRight = optionsChart.spacingRight,
  9851. spacingBottom = optionsChart.spacingBottom,
  9852. spacingLeft = optionsChart.spacingLeft;
  9853. chart.plotTop = pick(chart.optionsMarginTop, spacingTop);
  9854. chart.marginRight = pick(chart.optionsMarginRight, spacingRight);
  9855. chart.marginBottom = pick(chart.optionsMarginBottom, spacingBottom);
  9856. chart.plotLeft = pick(chart.optionsMarginLeft, spacingLeft);
  9857. chart.axisOffset = [0, 0, 0, 0]; // top, right, bottom, left
  9858. chart.clipOffset = [0, 0, 0, 0];
  9859. },
  9860. /**
  9861. * Draw the borders and backgrounds for chart and plot area
  9862. */
  9863. drawChartBox: function () {
  9864. var chart = this,
  9865. optionsChart = chart.options.chart,
  9866. renderer = chart.renderer,
  9867. chartWidth = chart.chartWidth,
  9868. chartHeight = chart.chartHeight,
  9869. chartBackground = chart.chartBackground,
  9870. plotBackground = chart.plotBackground,
  9871. plotBorder = chart.plotBorder,
  9872. plotBGImage = chart.plotBGImage,
  9873. chartBorderWidth = optionsChart.borderWidth || 0,
  9874. chartBackgroundColor = optionsChart.backgroundColor,
  9875. plotBackgroundColor = optionsChart.plotBackgroundColor,
  9876. plotBackgroundImage = optionsChart.plotBackgroundImage,
  9877. plotBorderWidth = optionsChart.plotBorderWidth || 0,
  9878. mgn,
  9879. bgAttr,
  9880. plotLeft = chart.plotLeft,
  9881. plotTop = chart.plotTop,
  9882. plotWidth = chart.plotWidth,
  9883. plotHeight = chart.plotHeight,
  9884. plotBox = chart.plotBox,
  9885. clipRect = chart.clipRect,
  9886. clipBox = chart.clipBox;
  9887. // Chart area
  9888. mgn = chartBorderWidth + (optionsChart.shadow ? 8 : 0);
  9889. if (chartBorderWidth || chartBackgroundColor) {
  9890. if (!chartBackground) {
  9891. bgAttr = {
  9892. fill: chartBackgroundColor || NONE
  9893. };
  9894. if (chartBorderWidth) { // #980
  9895. bgAttr.stroke = optionsChart.borderColor;
  9896. bgAttr['stroke-width'] = chartBorderWidth;
  9897. }
  9898. chart.chartBackground = renderer.rect(mgn / 2, mgn / 2, chartWidth - mgn, chartHeight - mgn,
  9899. optionsChart.borderRadius, chartBorderWidth)
  9900. .attr(bgAttr)
  9901. .add()
  9902. .shadow(optionsChart.shadow);
  9903. } else { // resize
  9904. chartBackground.animate(
  9905. chartBackground.crisp(null, null, null, chartWidth - mgn, chartHeight - mgn)
  9906. );
  9907. }
  9908. }
  9909. // Plot background
  9910. if (plotBackgroundColor) {
  9911. if (!plotBackground) {
  9912. chart.plotBackground = renderer.rect(plotLeft, plotTop, plotWidth, plotHeight, 0)
  9913. .attr({
  9914. fill: plotBackgroundColor
  9915. })
  9916. .add()
  9917. .shadow(optionsChart.plotShadow);
  9918. } else {
  9919. plotBackground.animate(plotBox);
  9920. }
  9921. }
  9922. if (plotBackgroundImage) {
  9923. if (!plotBGImage) {
  9924. chart.plotBGImage = renderer.image(plotBackgroundImage, plotLeft, plotTop, plotWidth, plotHeight)
  9925. .add();
  9926. } else {
  9927. plotBGImage.animate(plotBox);
  9928. }
  9929. }
  9930. // Plot clip
  9931. if (!clipRect) {
  9932. chart.clipRect = renderer.clipRect(clipBox);
  9933. } else {
  9934. clipRect.animate({
  9935. width: clipBox.width,
  9936. height: clipBox.height
  9937. });
  9938. }
  9939. // Plot area border
  9940. if (plotBorderWidth) {
  9941. if (!plotBorder) {
  9942. chart.plotBorder = renderer.rect(plotLeft, plotTop, plotWidth, plotHeight, 0, plotBorderWidth)
  9943. .attr({
  9944. stroke: optionsChart.plotBorderColor,
  9945. 'stroke-width': plotBorderWidth,
  9946. zIndex: 1
  9947. })
  9948. .add();
  9949. } else {
  9950. plotBorder.animate(
  9951. plotBorder.crisp(null, plotLeft, plotTop, plotWidth, plotHeight)
  9952. );
  9953. }
  9954. }
  9955. // reset
  9956. chart.isDirtyBox = false;
  9957. },
  9958. /**
  9959. * Detect whether a certain chart property is needed based on inspecting its options
  9960. * and series. This mainly applies to the chart.invert property, and in extensions to
  9961. * the chart.angular and chart.polar properties.
  9962. */
  9963. propFromSeries: function () {
  9964. var chart = this,
  9965. optionsChart = chart.options.chart,
  9966. klass,
  9967. seriesOptions = chart.options.series,
  9968. i,
  9969. value;
  9970. each(['inverted', 'angular', 'polar'], function (key) {
  9971. // The default series type's class
  9972. klass = seriesTypes[optionsChart.type || optionsChart.defaultSeriesType];
  9973. // Get the value from available chart-wide properties
  9974. value = (
  9975. chart[key] || // 1. it is set before
  9976. optionsChart[key] || // 2. it is set in the options
  9977. (klass && klass.prototype[key]) // 3. it's default series class requires it
  9978. );
  9979. // 4. Check if any the chart's series require it
  9980. i = seriesOptions && seriesOptions.length;
  9981. while (!value && i--) {
  9982. klass = seriesTypes[seriesOptions[i].type];
  9983. if (klass && klass.prototype[key]) {
  9984. value = true;
  9985. }
  9986. }
  9987. // Set the chart property
  9988. chart[key] = value;
  9989. });
  9990. },
  9991. /**
  9992. * Render all graphics for the chart
  9993. */
  9994. render: function () {
  9995. var chart = this,
  9996. axes = chart.axes,
  9997. renderer = chart.renderer,
  9998. options = chart.options;
  9999. var labels = options.labels,
  10000. credits = options.credits,
  10001. creditsHref;
  10002. // Title
  10003. chart.setTitle();
  10004. // Legend
  10005. chart.legend = new Legend(chart, options.legend);
  10006. // Get margins by pre-rendering axes
  10007. // set axes scales
  10008. each(axes, function (axis) {
  10009. axis.setScale();
  10010. });
  10011. chart.getMargins();
  10012. chart.maxTicks = null; // reset for second pass
  10013. each(axes, function (axis) {
  10014. axis.setTickPositions(true); // update to reflect the new margins
  10015. axis.setMaxTicks();
  10016. });
  10017. chart.adjustTickAmounts();
  10018. chart.getMargins(); // second pass to check for new labels
  10019. // Draw the borders and backgrounds
  10020. chart.drawChartBox();
  10021. // Axes
  10022. if (chart.hasCartesianSeries) {
  10023. each(axes, function (axis) {
  10024. axis.render();
  10025. });
  10026. }
  10027. // The series
  10028. if (!chart.seriesGroup) {
  10029. chart.seriesGroup = renderer.g('series-group')
  10030. .attr({ zIndex: 3 })
  10031. .add();
  10032. }
  10033. each(chart.series, function (serie) {
  10034. serie.translate();
  10035. serie.setTooltipPoints();
  10036. serie.render();
  10037. });
  10038. // Labels
  10039. if (labels.items) {
  10040. each(labels.items, function (label) {
  10041. var style = extend(labels.style, label.style),
  10042. x = pInt(style.left) + chart.plotLeft,
  10043. y = pInt(style.top) + chart.plotTop + 12;
  10044. // delete to prevent rewriting in IE
  10045. delete style.left;
  10046. delete style.top;
  10047. renderer.text(
  10048. label.html,
  10049. x,
  10050. y
  10051. )
  10052. .attr({ zIndex: 2 })
  10053. .css(style)
  10054. .add();
  10055. });
  10056. }
  10057. // Credits
  10058. if (credits.enabled && !chart.credits) {
  10059. creditsHref = credits.href;
  10060. chart.credits = renderer.text(
  10061. credits.text,
  10062. 0,
  10063. 0
  10064. )
  10065. .on('click', function () {
  10066. if (creditsHref) {
  10067. location.href = creditsHref;
  10068. }
  10069. })
  10070. .attr({
  10071. align: credits.position.align,
  10072. zIndex: 8
  10073. })
  10074. .css(credits.style)
  10075. .add()
  10076. .align(credits.position);
  10077. }
  10078. // Set flag
  10079. chart.hasRendered = true;
  10080. },
  10081. /**
  10082. * Clean up memory usage
  10083. */
  10084. destroy: function () {
  10085. var chart = this,
  10086. axes = chart.axes,
  10087. series = chart.series,
  10088. container = chart.container,
  10089. i,
  10090. parentNode = container && container.parentNode;
  10091. // fire the chart.destoy event
  10092. fireEvent(chart, 'destroy');
  10093. // Delete the chart from charts lookup array
  10094. charts[chart.index] = UNDEFINED;
  10095. chart.renderTo.removeAttribute('data-highcharts-chart');
  10096. // remove events
  10097. removeEvent(chart);
  10098. // ==== Destroy collections:
  10099. // Destroy axes
  10100. i = axes.length;
  10101. while (i--) {
  10102. axes[i] = axes[i].destroy();
  10103. }
  10104. // Destroy each series
  10105. i = series.length;
  10106. while (i--) {
  10107. series[i] = series[i].destroy();
  10108. }
  10109. // ==== Destroy chart properties:
  10110. each(['title', 'subtitle', 'chartBackground', 'plotBackground', 'plotBGImage',
  10111. 'plotBorder', 'seriesGroup', 'clipRect', 'credits', 'pointer', 'scroller',
  10112. 'rangeSelector', 'legend', 'resetZoomButton', 'tooltip', 'renderer'], function (name) {
  10113. var prop = chart[name];
  10114. if (prop && prop.destroy) {
  10115. chart[name] = prop.destroy();
  10116. }
  10117. });
  10118. // remove container and all SVG
  10119. if (container) { // can break in IE when destroyed before finished loading
  10120. container.innerHTML = '';
  10121. removeEvent(container);
  10122. if (parentNode) {
  10123. discardElement(container);
  10124. }
  10125. }
  10126. // clean it all up
  10127. for (i in chart) {
  10128. delete chart[i];
  10129. }
  10130. },
  10131. /**
  10132. * VML namespaces can't be added until after complete. Listening
  10133. * for Perini's doScroll hack is not enough.
  10134. */
  10135. isReadyToRender: function () {
  10136. var chart = this;
  10137. // Note: in spite of JSLint's complaints, win == win.top is required
  10138. /*jslint eqeq: true*/
  10139. if ((!hasSVG && (win == win.top && doc.readyState !== 'complete')) || (useCanVG && !win.canvg)) {
  10140. /*jslint eqeq: false*/
  10141. if (useCanVG) {
  10142. // Delay rendering until canvg library is downloaded and ready
  10143. CanVGController.push(function () { chart.firstRender(); }, chart.options.global.canvasToolsURL);
  10144. } else {
  10145. doc.attachEvent('onreadystatechange', function () {
  10146. doc.detachEvent('onreadystatechange', chart.firstRender);
  10147. if (doc.readyState === 'complete') {
  10148. chart.firstRender();
  10149. }
  10150. });
  10151. }
  10152. return false;
  10153. }
  10154. return true;
  10155. },
  10156. /**
  10157. * Prepare for first rendering after all data are loaded
  10158. */
  10159. firstRender: function () {
  10160. var chart = this,
  10161. options = chart.options,
  10162. callback = chart.callback;
  10163. // Check whether the chart is ready to render
  10164. if (!chart.isReadyToRender()) {
  10165. return;
  10166. }
  10167. // Create the container
  10168. chart.getContainer();
  10169. // Run an early event after the container and renderer are established
  10170. fireEvent(chart, 'init');
  10171. chart.resetMargins();
  10172. chart.setChartSize();
  10173. // Set the common chart properties (mainly invert) from the given series
  10174. chart.propFromSeries();
  10175. // get axes
  10176. chart.getAxes();
  10177. // Initialize the series
  10178. each(options.series || [], function (serieOptions) {
  10179. chart.initSeries(serieOptions);
  10180. });
  10181. // Run an event after axes and series are initialized, but before render. At this stage,
  10182. // the series data is indexed and cached in the xData and yData arrays, so we can access
  10183. // those before rendering. Used in Highstock.
  10184. fireEvent(chart, 'beforeRender');
  10185. // depends on inverted and on margins being set
  10186. chart.pointer = new Pointer(chart, options);
  10187. chart.render();
  10188. // add canvas
  10189. chart.renderer.draw();
  10190. // run callbacks
  10191. if (callback) {
  10192. callback.apply(chart, [chart]);
  10193. }
  10194. each(chart.callbacks, function (fn) {
  10195. fn.apply(chart, [chart]);
  10196. });
  10197. // If the chart was rendered outside the top container, put it back in
  10198. chart.cloneRenderTo(true);
  10199. fireEvent(chart, 'load');
  10200. }
  10201. }; // end Chart
  10202. // Hook for exporting module
  10203. Chart.prototype.callbacks = [];
  10204. /**
  10205. * The Point object and prototype. Inheritable and used as base for PiePoint
  10206. */
  10207. var Point = function () {};
  10208. Point.prototype = {
  10209. /**
  10210. * Initialize the point
  10211. * @param {Object} series The series object containing this point
  10212. * @param {Object} options The data in either number, array or object format
  10213. */
  10214. init: function (series, options, x) {
  10215. var point = this,
  10216. colors;
  10217. point.series = series;
  10218. point.applyOptions(options, x);
  10219. point.pointAttr = {};
  10220. if (series.options.colorByPoint) {
  10221. colors = series.options.colors || series.chart.options.colors;
  10222. point.color = point.color || colors[series.colorCounter++];
  10223. // loop back to zero
  10224. if (series.colorCounter === colors.length) {
  10225. series.colorCounter = 0;
  10226. }
  10227. }
  10228. series.chart.pointCount++;
  10229. return point;
  10230. },
  10231. /**
  10232. * Apply the options containing the x and y data and possible some extra properties.
  10233. * This is called on point init or from point.update.
  10234. *
  10235. * @param {Object} options
  10236. */
  10237. applyOptions: function (options, x) {
  10238. var point = this,
  10239. series = point.series,
  10240. pointValKey = series.pointValKey;
  10241. options = Point.prototype.optionsToObject.call(this, options);
  10242. // copy options directly to point
  10243. extend(point, options);
  10244. point.options = point.options ? extend(point.options, options) : options;
  10245. // For higher dimension series types. For instance, for ranges, point.y is mapped to point.low.
  10246. if (pointValKey) {
  10247. point.y = point[pointValKey];
  10248. }
  10249. // If no x is set by now, get auto incremented value. All points must have an
  10250. // x value, however the y value can be null to create a gap in the series
  10251. if (point.x === UNDEFINED && series) {
  10252. point.x = x === UNDEFINED ? series.autoIncrement() : x;
  10253. }
  10254. return point;
  10255. },
  10256. /**
  10257. * Transform number or array configs into objects
  10258. */
  10259. optionsToObject: function (options) {
  10260. var ret,
  10261. series = this.series,
  10262. pointArrayMap = series.pointArrayMap || ['y'],
  10263. valueCount = pointArrayMap.length,
  10264. firstItemType,
  10265. i = 0,
  10266. j = 0;
  10267. if (typeof options === 'number' || options === null) {
  10268. ret = { y: options };
  10269. } else if (isArray(options)) {
  10270. ret = {};
  10271. // with leading x value
  10272. if (options.length > valueCount) {
  10273. firstItemType = typeof options[0];
  10274. if (firstItemType === 'string') {
  10275. ret.name = options[0];
  10276. } else if (firstItemType === 'number') {
  10277. ret.x = options[0];
  10278. }
  10279. i++;
  10280. }
  10281. while (j < valueCount) {
  10282. ret[pointArrayMap[j++]] = options[i++];
  10283. }
  10284. } else if (typeof options === 'object') {
  10285. ret = options;
  10286. // This is the fastest way to detect if there are individual point dataLabels that need
  10287. // to be considered in drawDataLabels. These can only occur in object configs.
  10288. if (options.dataLabels) {
  10289. series._hasPointLabels = true;
  10290. }
  10291. // Same approach as above for markers
  10292. if (options.marker) {
  10293. series._hasPointMarkers = true;
  10294. }
  10295. }
  10296. return ret;
  10297. },
  10298. /**
  10299. * Destroy a point to clear memory. Its reference still stays in series.data.
  10300. */
  10301. destroy: function () {
  10302. var point = this,
  10303. series = point.series,
  10304. chart = series.chart,
  10305. hoverPoints = chart.hoverPoints,
  10306. prop;
  10307. chart.pointCount--;
  10308. if (hoverPoints) {
  10309. point.setState();
  10310. erase(hoverPoints, point);
  10311. if (!hoverPoints.length) {
  10312. chart.hoverPoints = null;
  10313. }
  10314. }
  10315. if (point === chart.hoverPoint) {
  10316. point.onMouseOut();
  10317. }
  10318. // remove all events
  10319. if (point.graphic || point.dataLabel) { // removeEvent and destroyElements are performance expensive
  10320. removeEvent(point);
  10321. point.destroyElements();
  10322. }
  10323. if (point.legendItem) { // pies have legend items
  10324. chart.legend.destroyItem(point);
  10325. }
  10326. for (prop in point) {
  10327. point[prop] = null;
  10328. }
  10329. },
  10330. /**
  10331. * Destroy SVG elements associated with the point
  10332. */
  10333. destroyElements: function () {
  10334. var point = this,
  10335. props = ['graphic', 'dataLabel', 'dataLabelUpper', 'group', 'connector', 'shadowGroup'],
  10336. prop,
  10337. i = 6;
  10338. while (i--) {
  10339. prop = props[i];
  10340. if (point[prop]) {
  10341. point[prop] = point[prop].destroy();
  10342. }
  10343. }
  10344. },
  10345. /**
  10346. * Return the configuration hash needed for the data label and tooltip formatters
  10347. */
  10348. getLabelConfig: function () {
  10349. var point = this;
  10350. return {
  10351. x: point.category,
  10352. y: point.y,
  10353. key: point.name || point.category,
  10354. series: point.series,
  10355. point: point,
  10356. percentage: point.percentage,
  10357. total: point.total || point.stackTotal
  10358. };
  10359. },
  10360. /**
  10361. * Toggle the selection status of a point
  10362. * @param {Boolean} selected Whether to select or unselect the point.
  10363. * @param {Boolean} accumulate Whether to add to the previous selection. By default,
  10364. * this happens if the control key (Cmd on Mac) was pressed during clicking.
  10365. */
  10366. select: function (selected, accumulate) {
  10367. var point = this,
  10368. series = point.series,
  10369. chart = series.chart;
  10370. selected = pick(selected, !point.selected);
  10371. // fire the event with the defalut handler
  10372. point.firePointEvent(selected ? 'select' : 'unselect', { accumulate: accumulate }, function () {
  10373. point.selected = point.options.selected = selected;
  10374. series.options.data[inArray(point, series.data)] = point.options;
  10375. point.setState(selected && SELECT_STATE);
  10376. // unselect all other points unless Ctrl or Cmd + click
  10377. if (!accumulate) {
  10378. each(chart.getSelectedPoints(), function (loopPoint) {
  10379. if (loopPoint.selected && loopPoint !== point) {
  10380. loopPoint.selected = loopPoint.options.selected = false;
  10381. series.options.data[inArray(loopPoint, series.data)] = loopPoint.options;
  10382. loopPoint.setState(NORMAL_STATE);
  10383. loopPoint.firePointEvent('unselect');
  10384. }
  10385. });
  10386. }
  10387. });
  10388. },
  10389. /**
  10390. * Runs on mouse over the point
  10391. */
  10392. onMouseOver: function (e) {
  10393. var point = this,
  10394. series = point.series,
  10395. chart = series.chart,
  10396. tooltip = chart.tooltip,
  10397. hoverPoint = chart.hoverPoint;
  10398. // set normal state to previous series
  10399. if (hoverPoint && hoverPoint !== point) {
  10400. hoverPoint.onMouseOut();
  10401. }
  10402. // trigger the event
  10403. point.firePointEvent('mouseOver');
  10404. // update the tooltip
  10405. if (tooltip && (!tooltip.shared || series.noSharedTooltip)) {
  10406. tooltip.refresh(point, e);
  10407. }
  10408. // hover this
  10409. point.setState(HOVER_STATE);
  10410. chart.hoverPoint = point;
  10411. },
  10412. /**
  10413. * Runs on mouse out from the point
  10414. */
  10415. onMouseOut: function () {
  10416. var chart = this.series.chart,
  10417. hoverPoints = chart.hoverPoints;
  10418. if (!hoverPoints || inArray(this, hoverPoints) === -1) { // #887
  10419. this.firePointEvent('mouseOut');
  10420. this.setState();
  10421. chart.hoverPoint = null;
  10422. }
  10423. },
  10424. /**
  10425. * Extendable method for formatting each point's tooltip line
  10426. *
  10427. * @return {String} A string to be concatenated in to the common tooltip text
  10428. */
  10429. tooltipFormatter: function (pointFormat) {
  10430. // Insert options for valueDecimals, valuePrefix, and valueSuffix
  10431. var series = this.series,
  10432. seriesTooltipOptions = series.tooltipOptions,
  10433. valueDecimals = pick(seriesTooltipOptions.valueDecimals, ''),
  10434. valuePrefix = seriesTooltipOptions.valuePrefix || '',
  10435. valueSuffix = seriesTooltipOptions.valueSuffix || '';
  10436. // Loop over the point array map and replace unformatted values with sprintf formatting markup
  10437. each(series.pointArrayMap || ['y'], function (key) {
  10438. key = '{point.' + key; // without the closing bracket
  10439. if (valuePrefix || valueSuffix) {
  10440. pointFormat = pointFormat.replace(key + '}', valuePrefix + key + '}' + valueSuffix);
  10441. }
  10442. pointFormat = pointFormat.replace(key + '}', key + ':,.' + valueDecimals + 'f}');
  10443. });
  10444. return format(pointFormat, {
  10445. point: this,
  10446. series: this.series
  10447. });
  10448. },
  10449. /**
  10450. * Update the point with new options (typically x/y data) and optionally redraw the series.
  10451. *
  10452. * @param {Object} options Point options as defined in the series.data array
  10453. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  10454. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  10455. * configuration
  10456. *
  10457. */
  10458. update: function (options, redraw, animation) {
  10459. var point = this,
  10460. series = point.series,
  10461. graphic = point.graphic,
  10462. i,
  10463. data = series.data,
  10464. chart = series.chart;
  10465. redraw = pick(redraw, true);
  10466. // fire the event with a default handler of doing the update
  10467. point.firePointEvent('update', { options: options }, function () {
  10468. point.applyOptions(options);
  10469. // update visuals
  10470. if (isObject(options)) {
  10471. series.getAttribs();
  10472. if (graphic) {
  10473. graphic.attr(point.pointAttr[series.state]);
  10474. }
  10475. }
  10476. // record changes in the parallel arrays
  10477. i = inArray(point, data);
  10478. series.xData[i] = point.x;
  10479. series.yData[i] = series.toYData ? series.toYData(point) : point.y;
  10480. series.zData[i] = point.z;
  10481. series.options.data[i] = point.options;
  10482. // redraw
  10483. series.isDirty = true;
  10484. series.isDirtyData = true;
  10485. if (redraw) {
  10486. chart.redraw(animation);
  10487. }
  10488. });
  10489. },
  10490. /**
  10491. * Remove a point and optionally redraw the series and if necessary the axes
  10492. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  10493. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  10494. * configuration
  10495. */
  10496. remove: function (redraw, animation) {
  10497. var point = this,
  10498. series = point.series,
  10499. chart = series.chart,
  10500. i,
  10501. data = series.data;
  10502. setAnimation(animation, chart);
  10503. redraw = pick(redraw, true);
  10504. // fire the event with a default handler of removing the point
  10505. point.firePointEvent('remove', null, function () {
  10506. // splice all the parallel arrays
  10507. i = inArray(point, data);
  10508. data.splice(i, 1);
  10509. series.options.data.splice(i, 1);
  10510. series.xData.splice(i, 1);
  10511. series.yData.splice(i, 1);
  10512. series.zData.splice(i, 1);
  10513. point.destroy();
  10514. // redraw
  10515. series.isDirty = true;
  10516. series.isDirtyData = true;
  10517. if (redraw) {
  10518. chart.redraw();
  10519. }
  10520. });
  10521. },
  10522. /**
  10523. * Fire an event on the Point object. Must not be renamed to fireEvent, as this
  10524. * causes a name clash in MooTools
  10525. * @param {String} eventType
  10526. * @param {Object} eventArgs Additional event arguments
  10527. * @param {Function} defaultFunction Default event handler
  10528. */
  10529. firePointEvent: function (eventType, eventArgs, defaultFunction) {
  10530. var point = this,
  10531. series = this.series,
  10532. seriesOptions = series.options;
  10533. // load event handlers on demand to save time on mouseover/out
  10534. if (seriesOptions.point.events[eventType] || (point.options && point.options.events && point.options.events[eventType])) {
  10535. this.importEvents();
  10536. }
  10537. // add default handler if in selection mode
  10538. if (eventType === 'click' && seriesOptions.allowPointSelect) {
  10539. defaultFunction = function (event) {
  10540. // Control key is for Windows, meta (= Cmd key) for Mac, Shift for Opera
  10541. point.select(null, event.ctrlKey || event.metaKey || event.shiftKey);
  10542. };
  10543. }
  10544. fireEvent(this, eventType, eventArgs, defaultFunction);
  10545. },
  10546. /**
  10547. * Import events from the series' and point's options. Only do it on
  10548. * demand, to save processing time on hovering.
  10549. */
  10550. importEvents: function () {
  10551. if (!this.hasImportedEvents) {
  10552. var point = this,
  10553. options = merge(point.series.options.point, point.options),
  10554. events = options.events,
  10555. eventType;
  10556. point.events = events;
  10557. for (eventType in events) {
  10558. addEvent(point, eventType, events[eventType]);
  10559. }
  10560. this.hasImportedEvents = true;
  10561. }
  10562. },
  10563. /**
  10564. * Set the point's state
  10565. * @param {String} state
  10566. */
  10567. setState: function (state) {
  10568. var point = this,
  10569. plotX = point.plotX,
  10570. plotY = point.plotY,
  10571. series = point.series,
  10572. stateOptions = series.options.states,
  10573. markerOptions = defaultPlotOptions[series.type].marker && series.options.marker,
  10574. normalDisabled = markerOptions && !markerOptions.enabled,
  10575. markerStateOptions = markerOptions && markerOptions.states[state],
  10576. stateDisabled = markerStateOptions && markerStateOptions.enabled === false,
  10577. stateMarkerGraphic = series.stateMarkerGraphic,
  10578. pointMarker = point.marker || {},
  10579. chart = series.chart,
  10580. radius,
  10581. newSymbol,
  10582. pointAttr = point.pointAttr;
  10583. state = state || NORMAL_STATE; // empty string
  10584. if (
  10585. // already has this state
  10586. state === point.state ||
  10587. // selected points don't respond to hover
  10588. (point.selected && state !== SELECT_STATE) ||
  10589. // series' state options is disabled
  10590. (stateOptions[state] && stateOptions[state].enabled === false) ||
  10591. // point marker's state options is disabled
  10592. (state && (stateDisabled || (normalDisabled && !markerStateOptions.enabled)))
  10593. ) {
  10594. return;
  10595. }
  10596. // apply hover styles to the existing point
  10597. if (point.graphic) {
  10598. radius = markerOptions && point.graphic.symbolName && pointAttr[state].r;
  10599. point.graphic.attr(merge(
  10600. pointAttr[state],
  10601. radius ? { // new symbol attributes (#507, #612)
  10602. x: plotX - radius,
  10603. y: plotY - radius,
  10604. width: 2 * radius,
  10605. height: 2 * radius
  10606. } : {}
  10607. ));
  10608. } else {
  10609. // if a graphic is not applied to each point in the normal state, create a shared
  10610. // graphic for the hover state
  10611. if (state && markerStateOptions) {
  10612. radius = markerStateOptions.radius;
  10613. newSymbol = pointMarker.symbol || series.symbol;
  10614. // If the point has another symbol than the previous one, throw away the
  10615. // state marker graphic and force a new one (#1459)
  10616. if (stateMarkerGraphic && stateMarkerGraphic.currentSymbol !== newSymbol) {
  10617. stateMarkerGraphic = stateMarkerGraphic.destroy();
  10618. }
  10619. // Add a new state marker graphic
  10620. if (!stateMarkerGraphic) {
  10621. series.stateMarkerGraphic = stateMarkerGraphic = chart.renderer.symbol(
  10622. newSymbol,
  10623. plotX - radius,
  10624. plotY - radius,
  10625. 2 * radius,
  10626. 2 * radius
  10627. )
  10628. .attr(pointAttr[state])
  10629. .add(series.markerGroup);
  10630. stateMarkerGraphic.currentSymbol = newSymbol;
  10631. // Move the existing graphic
  10632. } else {
  10633. stateMarkerGraphic.attr({ // #1054
  10634. x: plotX - radius,
  10635. y: plotY - radius
  10636. });
  10637. }
  10638. }
  10639. if (stateMarkerGraphic) {
  10640. stateMarkerGraphic[state && chart.isInsidePlot(plotX, plotY) ? 'show' : 'hide']();
  10641. }
  10642. }
  10643. point.state = state;
  10644. }
  10645. };
  10646. /**
  10647. * @classDescription The base function which all other series types inherit from. The data in the series is stored
  10648. * in various arrays.
  10649. *
  10650. * - First, series.options.data contains all the original config options for
  10651. * each point whether added by options or methods like series.addPoint.
  10652. * - Next, series.data contains those values converted to points, but in case the series data length
  10653. * exceeds the cropThreshold, or if the data is grouped, series.data doesn't contain all the points. It
  10654. * only contains the points that have been created on demand.
  10655. * - Then there's series.points that contains all currently visible point objects. In case of cropping,
  10656. * the cropped-away points are not part of this array. The series.points array starts at series.cropStart
  10657. * compared to series.data and series.options.data. If however the series data is grouped, these can't
  10658. * be correlated one to one.
  10659. * - series.xData and series.processedXData contain clean x values, equivalent to series.data and series.points.
  10660. * - series.yData and series.processedYData contain clean x values, equivalent to series.data and series.points.
  10661. *
  10662. * @param {Object} chart
  10663. * @param {Object} options
  10664. */
  10665. var Series = function () {};
  10666. Series.prototype = {
  10667. isCartesian: true,
  10668. type: 'line',
  10669. pointClass: Point,
  10670. sorted: true, // requires the data to be sorted
  10671. requireSorting: true,
  10672. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  10673. stroke: 'lineColor',
  10674. 'stroke-width': 'lineWidth',
  10675. fill: 'fillColor',
  10676. r: 'radius'
  10677. },
  10678. colorCounter: 0,
  10679. init: function (chart, options) {
  10680. var series = this,
  10681. eventType,
  10682. events,
  10683. linkedTo,
  10684. chartSeries = chart.series;
  10685. series.chart = chart;
  10686. series.options = options = series.setOptions(options); // merge with plotOptions
  10687. // bind the axes
  10688. series.bindAxes();
  10689. // set some variables
  10690. extend(series, {
  10691. name: options.name,
  10692. state: NORMAL_STATE,
  10693. pointAttr: {},
  10694. visible: options.visible !== false, // true by default
  10695. selected: options.selected === true // false by default
  10696. });
  10697. // special
  10698. if (useCanVG) {
  10699. options.animation = false;
  10700. }
  10701. // register event listeners
  10702. events = options.events;
  10703. for (eventType in events) {
  10704. addEvent(series, eventType, events[eventType]);
  10705. }
  10706. if (
  10707. (events && events.click) ||
  10708. (options.point && options.point.events && options.point.events.click) ||
  10709. options.allowPointSelect
  10710. ) {
  10711. chart.runTrackerClick = true;
  10712. }
  10713. series.getColor();
  10714. series.getSymbol();
  10715. // set the data
  10716. series.setData(options.data, false);
  10717. // Mark cartesian
  10718. if (series.isCartesian) {
  10719. chart.hasCartesianSeries = true;
  10720. }
  10721. // Register it in the chart
  10722. chartSeries.push(series);
  10723. series._i = chartSeries.length - 1;
  10724. // Sort series according to index option (#248, #1123)
  10725. stableSort(chartSeries, function (a, b) {
  10726. return pick(a.options.index, a._i) - pick(b.options.index, a._i);
  10727. });
  10728. each(chartSeries, function (series, i) {
  10729. series.index = i;
  10730. series.name = series.name || 'Series ' + (i + 1);
  10731. });
  10732. // Linked series
  10733. linkedTo = options.linkedTo;
  10734. series.linkedSeries = [];
  10735. if (isString(linkedTo)) {
  10736. if (linkedTo === ':previous') {
  10737. linkedTo = chartSeries[series.index - 1];
  10738. } else {
  10739. linkedTo = chart.get(linkedTo);
  10740. }
  10741. if (linkedTo) {
  10742. linkedTo.linkedSeries.push(series);
  10743. series.linkedParent = linkedTo;
  10744. }
  10745. }
  10746. },
  10747. /**
  10748. * Set the xAxis and yAxis properties of cartesian series, and register the series
  10749. * in the axis.series array
  10750. */
  10751. bindAxes: function () {
  10752. var series = this,
  10753. seriesOptions = series.options,
  10754. chart = series.chart,
  10755. axisOptions;
  10756. if (series.isCartesian) {
  10757. each(['xAxis', 'yAxis'], function (AXIS) { // repeat for xAxis and yAxis
  10758. each(chart[AXIS], function (axis) { // loop through the chart's axis objects
  10759. axisOptions = axis.options;
  10760. // apply if the series xAxis or yAxis option mathches the number of the
  10761. // axis, or if undefined, use the first axis
  10762. if ((seriesOptions[AXIS] === axisOptions.index) ||
  10763. (seriesOptions[AXIS] !== UNDEFINED && seriesOptions[AXIS] === axisOptions.id) ||
  10764. (seriesOptions[AXIS] === UNDEFINED && axisOptions.index === 0)) {
  10765. // register this series in the axis.series lookup
  10766. axis.series.push(series);
  10767. // set this series.xAxis or series.yAxis reference
  10768. series[AXIS] = axis;
  10769. // mark dirty for redraw
  10770. axis.isDirty = true;
  10771. }
  10772. });
  10773. // The series needs an X and an Y axis
  10774. if (!series[AXIS]) {
  10775. error(18, true);
  10776. }
  10777. });
  10778. }
  10779. },
  10780. /**
  10781. * Return an auto incremented x value based on the pointStart and pointInterval options.
  10782. * This is only used if an x value is not given for the point that calls autoIncrement.
  10783. */
  10784. autoIncrement: function () {
  10785. var series = this,
  10786. options = series.options,
  10787. xIncrement = series.xIncrement;
  10788. xIncrement = pick(xIncrement, options.pointStart, 0);
  10789. series.pointInterval = pick(series.pointInterval, options.pointInterval, 1);
  10790. series.xIncrement = xIncrement + series.pointInterval;
  10791. return xIncrement;
  10792. },
  10793. /**
  10794. * Divide the series data into segments divided by null values.
  10795. */
  10796. getSegments: function () {
  10797. var series = this,
  10798. lastNull = -1,
  10799. segments = [],
  10800. i,
  10801. points = series.points,
  10802. pointsLength = points.length;
  10803. if (pointsLength) { // no action required for []
  10804. // if connect nulls, just remove null points
  10805. if (series.options.connectNulls) {
  10806. i = pointsLength;
  10807. while (i--) {
  10808. if (points[i].y === null) {
  10809. points.splice(i, 1);
  10810. }
  10811. }
  10812. if (points.length) {
  10813. segments = [points];
  10814. }
  10815. // else, split on null points
  10816. } else {
  10817. each(points, function (point, i) {
  10818. if (point.y === null) {
  10819. if (i > lastNull + 1) {
  10820. segments.push(points.slice(lastNull + 1, i));
  10821. }
  10822. lastNull = i;
  10823. } else if (i === pointsLength - 1) { // last value
  10824. segments.push(points.slice(lastNull + 1, i + 1));
  10825. }
  10826. });
  10827. }
  10828. }
  10829. // register it
  10830. series.segments = segments;
  10831. },
  10832. /**
  10833. * Set the series options by merging from the options tree
  10834. * @param {Object} itemOptions
  10835. */
  10836. setOptions: function (itemOptions) {
  10837. var chart = this.chart,
  10838. chartOptions = chart.options,
  10839. plotOptions = chartOptions.plotOptions,
  10840. typeOptions = plotOptions[this.type],
  10841. options;
  10842. this.userOptions = itemOptions;
  10843. options = merge(
  10844. typeOptions,
  10845. plotOptions.series,
  10846. itemOptions
  10847. );
  10848. // the tooltip options are merged between global and series specific options
  10849. this.tooltipOptions = merge(chartOptions.tooltip, options.tooltip);
  10850. // Delte marker object if not allowed (#1125)
  10851. if (typeOptions.marker === null) {
  10852. delete options.marker;
  10853. }
  10854. return options;
  10855. },
  10856. /**
  10857. * Get the series' color
  10858. */
  10859. getColor: function () {
  10860. var options = this.options,
  10861. userOptions = this.userOptions,
  10862. defaultColors = this.chart.options.colors,
  10863. counters = this.chart.counters,
  10864. color,
  10865. colorIndex;
  10866. color = options.color || defaultPlotOptions[this.type].color;
  10867. if (!color && !options.colorByPoint) {
  10868. if (defined(userOptions._colorIndex)) { // after Series.update()
  10869. colorIndex = userOptions._colorIndex;
  10870. } else {
  10871. userOptions._colorIndex = counters.color;
  10872. colorIndex = counters.color++;
  10873. }
  10874. color = defaultColors[colorIndex];
  10875. }
  10876. this.color = color;
  10877. counters.wrapColor(defaultColors.length);
  10878. },
  10879. /**
  10880. * Get the series' symbol
  10881. */
  10882. getSymbol: function () {
  10883. var series = this,
  10884. userOptions = series.userOptions,
  10885. seriesMarkerOption = series.options.marker,
  10886. chart = series.chart,
  10887. defaultSymbols = chart.options.symbols,
  10888. counters = chart.counters,
  10889. symbolIndex;
  10890. series.symbol = seriesMarkerOption.symbol;
  10891. if (!series.symbol) {
  10892. if (defined(userOptions._symbolIndex)) { // after Series.update()
  10893. symbolIndex = userOptions._symbolIndex;
  10894. } else {
  10895. userOptions._symbolIndex = counters.symbol;
  10896. symbolIndex = counters.symbol++;
  10897. }
  10898. series.symbol = defaultSymbols[symbolIndex];
  10899. }
  10900. // don't substract radius in image symbols (#604)
  10901. if (/^url/.test(series.symbol)) {
  10902. seriesMarkerOption.radius = 0;
  10903. }
  10904. counters.wrapSymbol(defaultSymbols.length);
  10905. },
  10906. /**
  10907. * Get the series' symbol in the legend. This method should be overridable to create custom
  10908. * symbols through Highcharts.seriesTypes[type].prototype.drawLegendSymbols.
  10909. *
  10910. * @param {Object} legend The legend object
  10911. */
  10912. drawLegendSymbol: function (legend) {
  10913. var options = this.options,
  10914. markerOptions = options.marker,
  10915. radius,
  10916. legendOptions = legend.options,
  10917. legendSymbol,
  10918. symbolWidth = legendOptions.symbolWidth,
  10919. renderer = this.chart.renderer,
  10920. legendItemGroup = this.legendGroup,
  10921. baseline = legend.baseline,
  10922. attr;
  10923. // Draw the line
  10924. if (options.lineWidth) {
  10925. attr = {
  10926. 'stroke-width': options.lineWidth
  10927. };
  10928. if (options.dashStyle) {
  10929. attr.dashstyle = options.dashStyle;
  10930. }
  10931. this.legendLine = renderer.path([
  10932. M,
  10933. 0,
  10934. baseline - 4,
  10935. L,
  10936. symbolWidth,
  10937. baseline - 4
  10938. ])
  10939. .attr(attr)
  10940. .add(legendItemGroup);
  10941. }
  10942. // Draw the marker
  10943. if (markerOptions && markerOptions.enabled) {
  10944. radius = markerOptions.radius;
  10945. this.legendSymbol = legendSymbol = renderer.symbol(
  10946. this.symbol,
  10947. (symbolWidth / 2) - radius,
  10948. baseline - 4 - radius,
  10949. 2 * radius,
  10950. 2 * radius
  10951. )
  10952. .add(legendItemGroup);
  10953. }
  10954. },
  10955. /**
  10956. * Add a point dynamically after chart load time
  10957. * @param {Object} options Point options as given in series.data
  10958. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  10959. * @param {Boolean} shift If shift is true, a point is shifted off the start
  10960. * of the series as one is appended to the end.
  10961. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  10962. * configuration
  10963. */
  10964. addPoint: function (options, redraw, shift, animation) {
  10965. var series = this,
  10966. seriesOptions = series.options,
  10967. data = series.data,
  10968. graph = series.graph,
  10969. area = series.area,
  10970. chart = series.chart,
  10971. xData = series.xData,
  10972. yData = series.yData,
  10973. zData = series.zData,
  10974. names = series.names,
  10975. currentShift = (graph && graph.shift) || 0,
  10976. dataOptions = seriesOptions.data,
  10977. point;
  10978. setAnimation(animation, chart);
  10979. // Make graph animate sideways
  10980. if (graph && shift) {
  10981. graph.shift = currentShift + 1;
  10982. }
  10983. if (area) {
  10984. if (shift) { // #780
  10985. area.shift = currentShift + 1;
  10986. }
  10987. area.isArea = true; // needed in animation, both with and without shift
  10988. }
  10989. // Optional redraw, defaults to true
  10990. redraw = pick(redraw, true);
  10991. // Get options and push the point to xData, yData and series.options. In series.generatePoints
  10992. // the Point instance will be created on demand and pushed to the series.data array.
  10993. point = { series: series };
  10994. series.pointClass.prototype.applyOptions.apply(point, [options]);
  10995. xData.push(point.x);
  10996. yData.push(series.toYData ? series.toYData(point) : point.y);
  10997. zData.push(point.z);
  10998. if (names) {
  10999. names[point.x] = point.name;
  11000. }
  11001. dataOptions.push(options);
  11002. // Generate points to be added to the legend (#1329)
  11003. if (seriesOptions.legendType === 'point') {
  11004. series.generatePoints();
  11005. }
  11006. // Shift the first point off the parallel arrays
  11007. // todo: consider series.removePoint(i) method
  11008. if (shift) {
  11009. if (data[0] && data[0].remove) {
  11010. data[0].remove(false);
  11011. } else {
  11012. data.shift();
  11013. xData.shift();
  11014. yData.shift();
  11015. zData.shift();
  11016. dataOptions.shift();
  11017. }
  11018. }
  11019. series.getAttribs();
  11020. // redraw
  11021. series.isDirty = true;
  11022. series.isDirtyData = true;
  11023. if (redraw) {
  11024. chart.redraw();
  11025. }
  11026. },
  11027. /**
  11028. * Replace the series data with a new set of data
  11029. * @param {Object} data
  11030. * @param {Object} redraw
  11031. */
  11032. setData: function (data, redraw) {
  11033. var series = this,
  11034. oldData = series.points,
  11035. options = series.options,
  11036. chart = series.chart,
  11037. firstPoint = null,
  11038. xAxis = series.xAxis,
  11039. names = xAxis && xAxis.categories && !xAxis.categories.length ? [] : null,
  11040. i;
  11041. // reset properties
  11042. series.xIncrement = null;
  11043. series.pointRange = xAxis && xAxis.categories ? 1 : options.pointRange;
  11044. series.colorCounter = 0; // for series with colorByPoint (#1547)
  11045. // parallel arrays
  11046. var xData = [],
  11047. yData = [],
  11048. zData = [],
  11049. dataLength = data ? data.length : [],
  11050. turboThreshold = options.turboThreshold || 1000,
  11051. pt,
  11052. pointArrayMap = series.pointArrayMap,
  11053. valueCount = pointArrayMap && pointArrayMap.length,
  11054. hasToYData = !!series.toYData;
  11055. // In turbo mode, only one- or twodimensional arrays of numbers are allowed. The
  11056. // first value is tested, and we assume that all the rest are defined the same
  11057. // way. Although the 'for' loops are similar, they are repeated inside each
  11058. // if-else conditional for max performance.
  11059. if (dataLength > turboThreshold) {
  11060. // find the first non-null point
  11061. i = 0;
  11062. while (firstPoint === null && i < dataLength) {
  11063. firstPoint = data[i];
  11064. i++;
  11065. }
  11066. if (isNumber(firstPoint)) { // assume all points are numbers
  11067. var x = pick(options.pointStart, 0),
  11068. pointInterval = pick(options.pointInterval, 1);
  11069. for (i = 0; i < dataLength; i++) {
  11070. xData[i] = x;
  11071. yData[i] = data[i];
  11072. x += pointInterval;
  11073. }
  11074. series.xIncrement = x;
  11075. } else if (isArray(firstPoint)) { // assume all points are arrays
  11076. if (valueCount) { // [x, low, high] or [x, o, h, l, c]
  11077. for (i = 0; i < dataLength; i++) {
  11078. pt = data[i];
  11079. xData[i] = pt[0];
  11080. yData[i] = pt.slice(1, valueCount + 1);
  11081. }
  11082. } else { // [x, y]
  11083. for (i = 0; i < dataLength; i++) {
  11084. pt = data[i];
  11085. xData[i] = pt[0];
  11086. yData[i] = pt[1];
  11087. }
  11088. }
  11089. } /* else {
  11090. error(12); // Highcharts expects configs to be numbers or arrays in turbo mode
  11091. }*/
  11092. } else {
  11093. for (i = 0; i < dataLength; i++) {
  11094. if (data[i] !== UNDEFINED) { // stray commas in oldIE
  11095. pt = { series: series };
  11096. series.pointClass.prototype.applyOptions.apply(pt, [data[i]]);
  11097. xData[i] = pt.x;
  11098. yData[i] = hasToYData ? series.toYData(pt) : pt.y;
  11099. zData[i] = pt.z;
  11100. if (names && pt.name) {
  11101. names[i] = pt.name;
  11102. }
  11103. }
  11104. }
  11105. }
  11106. // Unsorted data is not supported by the line tooltip as well as data grouping and
  11107. // navigation in Stock charts (#725)
  11108. if (series.requireSorting && xData.length > 1 && xData[1] < xData[0]) {
  11109. error(15);
  11110. }
  11111. // Forgetting to cast strings to numbers is a common caveat when handling CSV or JSON
  11112. if (isString(yData[0])) {
  11113. error(14, true);
  11114. }
  11115. series.data = [];
  11116. series.options.data = data;
  11117. series.xData = xData;
  11118. series.yData = yData;
  11119. series.zData = zData;
  11120. series.names = names;
  11121. // destroy old points
  11122. i = (oldData && oldData.length) || 0;
  11123. while (i--) {
  11124. if (oldData[i] && oldData[i].destroy) {
  11125. oldData[i].destroy();
  11126. }
  11127. }
  11128. // reset minRange (#878)
  11129. if (xAxis) {
  11130. xAxis.minRange = xAxis.userMinRange;
  11131. }
  11132. // redraw
  11133. series.isDirty = series.isDirtyData = chart.isDirtyBox = true;
  11134. if (pick(redraw, true)) {
  11135. chart.redraw(false);
  11136. }
  11137. },
  11138. /**
  11139. * Remove a series and optionally redraw the chart
  11140. *
  11141. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  11142. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  11143. * configuration
  11144. */
  11145. remove: function (redraw, animation) {
  11146. var series = this,
  11147. chart = series.chart;
  11148. redraw = pick(redraw, true);
  11149. if (!series.isRemoving) { /* prevent triggering native event in jQuery
  11150. (calling the remove function from the remove event) */
  11151. series.isRemoving = true;
  11152. // fire the event with a default handler of removing the point
  11153. fireEvent(series, 'remove', null, function () {
  11154. // destroy elements
  11155. series.destroy();
  11156. // redraw
  11157. chart.isDirtyLegend = chart.isDirtyBox = true;
  11158. if (redraw) {
  11159. chart.redraw(animation);
  11160. }
  11161. });
  11162. }
  11163. series.isRemoving = false;
  11164. },
  11165. /**
  11166. * Process the data by cropping away unused data points if the series is longer
  11167. * than the crop threshold. This saves computing time for lage series.
  11168. */
  11169. processData: function (force) {
  11170. var series = this,
  11171. processedXData = series.xData, // copied during slice operation below
  11172. processedYData = series.yData,
  11173. dataLength = processedXData.length,
  11174. cropStart = 0,
  11175. cropEnd = dataLength,
  11176. cropped,
  11177. distance,
  11178. closestPointRange,
  11179. xAxis = series.xAxis,
  11180. i, // loop variable
  11181. options = series.options,
  11182. cropThreshold = options.cropThreshold,
  11183. isCartesian = series.isCartesian;
  11184. // If the series data or axes haven't changed, don't go through this. Return false to pass
  11185. // the message on to override methods like in data grouping.
  11186. if (isCartesian && !series.isDirty && !xAxis.isDirty && !series.yAxis.isDirty && !force) {
  11187. return false;
  11188. }
  11189. // optionally filter out points outside the plot area
  11190. if (isCartesian && series.sorted && (!cropThreshold || dataLength > cropThreshold || series.forceCrop)) {
  11191. var extremes = xAxis.getExtremes(),
  11192. min = extremes.min,
  11193. max = extremes.max;
  11194. // it's outside current extremes
  11195. if (processedXData[dataLength - 1] < min || processedXData[0] > max) {
  11196. processedXData = [];
  11197. processedYData = [];
  11198. // only crop if it's actually spilling out
  11199. } else if (processedXData[0] < min || processedXData[dataLength - 1] > max) {
  11200. // iterate up to find slice start
  11201. for (i = 0; i < dataLength; i++) {
  11202. if (processedXData[i] >= min) {
  11203. cropStart = mathMax(0, i - 1);
  11204. break;
  11205. }
  11206. }
  11207. // proceed to find slice end
  11208. for (; i < dataLength; i++) {
  11209. if (processedXData[i] > max) {
  11210. cropEnd = i + 1;
  11211. break;
  11212. }
  11213. }
  11214. processedXData = processedXData.slice(cropStart, cropEnd);
  11215. processedYData = processedYData.slice(cropStart, cropEnd);
  11216. cropped = true;
  11217. }
  11218. }
  11219. // Find the closest distance between processed points
  11220. for (i = processedXData.length - 1; i > 0; i--) {
  11221. distance = processedXData[i] - processedXData[i - 1];
  11222. if (distance > 0 && (closestPointRange === UNDEFINED || distance < closestPointRange)) {
  11223. closestPointRange = distance;
  11224. }
  11225. }
  11226. // Record the properties
  11227. series.cropped = cropped; // undefined or true
  11228. series.cropStart = cropStart;
  11229. series.processedXData = processedXData;
  11230. series.processedYData = processedYData;
  11231. if (options.pointRange === null) { // null means auto, as for columns, candlesticks and OHLC
  11232. series.pointRange = closestPointRange || 1;
  11233. }
  11234. series.closestPointRange = closestPointRange;
  11235. },
  11236. /**
  11237. * Generate the data point after the data has been processed by cropping away
  11238. * unused points and optionally grouped in Highcharts Stock.
  11239. */
  11240. generatePoints: function () {
  11241. var series = this,
  11242. options = series.options,
  11243. dataOptions = options.data,
  11244. data = series.data,
  11245. dataLength,
  11246. processedXData = series.processedXData,
  11247. processedYData = series.processedYData,
  11248. pointClass = series.pointClass,
  11249. processedDataLength = processedXData.length,
  11250. cropStart = series.cropStart || 0,
  11251. cursor,
  11252. hasGroupedData = series.hasGroupedData,
  11253. point,
  11254. points = [],
  11255. i;
  11256. if (!data && !hasGroupedData) {
  11257. var arr = [];
  11258. arr.length = dataOptions.length;
  11259. data = series.data = arr;
  11260. }
  11261. for (i = 0; i < processedDataLength; i++) {
  11262. cursor = cropStart + i;
  11263. if (!hasGroupedData) {
  11264. if (data[cursor]) {
  11265. point = data[cursor];
  11266. } else if (dataOptions[cursor] !== UNDEFINED) { // #970
  11267. data[cursor] = point = (new pointClass()).init(series, dataOptions[cursor], processedXData[i]);
  11268. }
  11269. points[i] = point;
  11270. } else {
  11271. // splat the y data in case of ohlc data array
  11272. points[i] = (new pointClass()).init(series, [processedXData[i]].concat(splat(processedYData[i])));
  11273. }
  11274. }
  11275. // Hide cropped-away points - this only runs when the number of points is above cropThreshold, or when
  11276. // swithching view from non-grouped data to grouped data (#637)
  11277. if (data && (processedDataLength !== (dataLength = data.length) || hasGroupedData)) {
  11278. for (i = 0; i < dataLength; i++) {
  11279. if (i === cropStart && !hasGroupedData) { // when has grouped data, clear all points
  11280. i += processedDataLength;
  11281. }
  11282. if (data[i]) {
  11283. data[i].destroyElements();
  11284. data[i].plotX = UNDEFINED; // #1003
  11285. }
  11286. }
  11287. }
  11288. series.data = data;
  11289. series.points = points;
  11290. },
  11291. /**
  11292. * Translate data points from raw data values to chart specific positioning data
  11293. * needed later in drawPoints, drawGraph and drawTracker.
  11294. */
  11295. translate: function () {
  11296. if (!this.processedXData) { // hidden series
  11297. this.processData();
  11298. }
  11299. this.generatePoints();
  11300. var series = this,
  11301. options = series.options,
  11302. stacking = options.stacking,
  11303. xAxis = series.xAxis,
  11304. categories = xAxis.categories,
  11305. yAxis = series.yAxis,
  11306. points = series.points,
  11307. dataLength = points.length,
  11308. hasModifyValue = !!series.modifyValue,
  11309. isBottomSeries,
  11310. allStackSeries,
  11311. i,
  11312. placeBetween = options.pointPlacement === 'between',
  11313. threshold = options.threshold;
  11314. //nextSeriesDown;
  11315. // Is it the last visible series? (#809, #1722).
  11316. // TODO: After merging in the 'stacking' branch, this logic should probably be moved to Chart.getStacks
  11317. allStackSeries = yAxis.series.sort(function (a, b) {
  11318. return a.index - b.index;
  11319. });
  11320. i = allStackSeries.length;
  11321. while (i--) {
  11322. if (allStackSeries[i].visible) {
  11323. if (allStackSeries[i] === series) { // #809
  11324. isBottomSeries = true;
  11325. }
  11326. break;
  11327. }
  11328. }
  11329. // Translate each point
  11330. for (i = 0; i < dataLength; i++) {
  11331. var point = points[i],
  11332. xValue = point.x,
  11333. yValue = point.y,
  11334. yBottom = point.low,
  11335. stack = yAxis.stacks[(yValue < threshold ? '-' : '') + series.stackKey],
  11336. pointStack,
  11337. pointStackTotal;
  11338. // Discard disallowed y values for log axes
  11339. if (yAxis.isLog && yValue <= 0) {
  11340. point.y = yValue = null;
  11341. }
  11342. // Get the plotX translation
  11343. point.plotX = xAxis.translate(xValue, 0, 0, 0, 1, placeBetween); // Math.round fixes #591
  11344. // Calculate the bottom y value for stacked series
  11345. if (stacking && series.visible && stack && stack[xValue]) {
  11346. pointStack = stack[xValue];
  11347. pointStackTotal = pointStack.total;
  11348. pointStack.cum = yBottom = pointStack.cum - yValue; // start from top
  11349. yValue = yBottom + yValue;
  11350. if (isBottomSeries) {
  11351. yBottom = pick(threshold, yAxis.min);
  11352. }
  11353. if (yAxis.isLog && yBottom <= 0) { // #1200, #1232
  11354. yBottom = null;
  11355. }
  11356. if (stacking === 'percent') {
  11357. yBottom = pointStackTotal ? yBottom * 100 / pointStackTotal : 0;
  11358. yValue = pointStackTotal ? yValue * 100 / pointStackTotal : 0;
  11359. }
  11360. point.percentage = pointStackTotal ? point.y * 100 / pointStackTotal : 0;
  11361. point.total = point.stackTotal = pointStackTotal;
  11362. point.stackY = yValue;
  11363. }
  11364. // Set translated yBottom or remove it
  11365. point.yBottom = defined(yBottom) ?
  11366. yAxis.translate(yBottom, 0, 1, 0, 1) :
  11367. null;
  11368. // general hook, used for Highstock compare mode
  11369. if (hasModifyValue) {
  11370. yValue = series.modifyValue(yValue, point);
  11371. }
  11372. // Set the the plotY value, reset it for redraws
  11373. point.plotY = (typeof yValue === 'number' && yValue !== Infinity) ?
  11374. mathRound(yAxis.translate(yValue, 0, 1, 0, 1) * 10) / 10 : // Math.round fixes #591
  11375. UNDEFINED;
  11376. // Set client related positions for mouse tracking
  11377. point.clientX = placeBetween ? xAxis.translate(xValue, 0, 0, 0, 1) : point.plotX; // #1514
  11378. point.negative = point.y < (threshold || 0);
  11379. // some API data
  11380. point.category = categories && categories[point.x] !== UNDEFINED ?
  11381. categories[point.x] : point.x;
  11382. }
  11383. // now that we have the cropped data, build the segments
  11384. series.getSegments();
  11385. },
  11386. /**
  11387. * Memoize tooltip texts and positions
  11388. */
  11389. setTooltipPoints: function (renew) {
  11390. var series = this,
  11391. points = [],
  11392. pointsLength,
  11393. low,
  11394. high,
  11395. xAxis = series.xAxis,
  11396. axisLength = xAxis ? (xAxis.tooltipLen || xAxis.len) : series.chart.plotSizeX, // tooltipLen and tooltipPosName used in polar
  11397. point,
  11398. i,
  11399. tooltipPoints = []; // a lookup array for each pixel in the x dimension
  11400. // don't waste resources if tracker is disabled
  11401. if (series.options.enableMouseTracking === false) {
  11402. return;
  11403. }
  11404. // renew
  11405. if (renew) {
  11406. series.tooltipPoints = null;
  11407. }
  11408. // concat segments to overcome null values
  11409. each(series.segments || series.points, function (segment) {
  11410. points = points.concat(segment);
  11411. });
  11412. // Reverse the points in case the X axis is reversed
  11413. if (xAxis && xAxis.reversed) {
  11414. points = points.reverse();
  11415. }
  11416. // Assign each pixel position to the nearest point
  11417. pointsLength = points.length;
  11418. for (i = 0; i < pointsLength; i++) {
  11419. point = points[i];
  11420. // Set this range's low to the last range's high plus one
  11421. low = points[i - 1] ? high + 1 : 0;
  11422. // Now find the new high
  11423. high = points[i + 1] ?
  11424. mathMax(0, mathFloor((point.clientX + (points[i + 1] ? points[i + 1].clientX : axisLength)) / 2)) :
  11425. axisLength;
  11426. while (low >= 0 && low <= high) {
  11427. tooltipPoints[low++] = point;
  11428. }
  11429. }
  11430. series.tooltipPoints = tooltipPoints;
  11431. },
  11432. /**
  11433. * Format the header of the tooltip
  11434. */
  11435. tooltipHeaderFormatter: function (point) {
  11436. var series = this,
  11437. tooltipOptions = series.tooltipOptions,
  11438. xDateFormat = tooltipOptions.xDateFormat,
  11439. dateTimeLabelFormats = tooltipOptions.dateTimeLabelFormats,
  11440. xAxis = series.xAxis,
  11441. isDateTime = xAxis && xAxis.options.type === 'datetime',
  11442. headerFormat = tooltipOptions.headerFormat,
  11443. closestPointRange = xAxis && xAxis.closestPointRange,
  11444. n;
  11445. // Guess the best date format based on the closest point distance (#568)
  11446. if (isDateTime && !xDateFormat) {
  11447. if (closestPointRange) {
  11448. for (n in timeUnits) {
  11449. if (timeUnits[n] >= closestPointRange) {
  11450. xDateFormat = dateTimeLabelFormats[n];
  11451. break;
  11452. }
  11453. }
  11454. } else {
  11455. xDateFormat = dateTimeLabelFormats.day;
  11456. }
  11457. }
  11458. // Insert the header date format if any
  11459. if (isDateTime && xDateFormat && isNumber(point.key)) {
  11460. headerFormat = headerFormat.replace('{point.key}', '{point.key:' + xDateFormat + '}');
  11461. }
  11462. return format(headerFormat, {
  11463. point: point,
  11464. series: series
  11465. });
  11466. },
  11467. /**
  11468. * Series mouse over handler
  11469. */
  11470. onMouseOver: function () {
  11471. var series = this,
  11472. chart = series.chart,
  11473. hoverSeries = chart.hoverSeries;
  11474. // set normal state to previous series
  11475. if (hoverSeries && hoverSeries !== series) {
  11476. hoverSeries.onMouseOut();
  11477. }
  11478. // trigger the event, but to save processing time,
  11479. // only if defined
  11480. if (series.options.events.mouseOver) {
  11481. fireEvent(series, 'mouseOver');
  11482. }
  11483. // hover this
  11484. series.setState(HOVER_STATE);
  11485. chart.hoverSeries = series;
  11486. },
  11487. /**
  11488. * Series mouse out handler
  11489. */
  11490. onMouseOut: function () {
  11491. // trigger the event only if listeners exist
  11492. var series = this,
  11493. options = series.options,
  11494. chart = series.chart,
  11495. tooltip = chart.tooltip,
  11496. hoverPoint = chart.hoverPoint;
  11497. // trigger mouse out on the point, which must be in this series
  11498. if (hoverPoint) {
  11499. hoverPoint.onMouseOut();
  11500. }
  11501. // fire the mouse out event
  11502. if (series && options.events.mouseOut) {
  11503. fireEvent(series, 'mouseOut');
  11504. }
  11505. // hide the tooltip
  11506. if (tooltip && !options.stickyTracking && (!tooltip.shared || series.noSharedTooltip)) {
  11507. tooltip.hide();
  11508. }
  11509. // set normal state
  11510. series.setState();
  11511. chart.hoverSeries = null;
  11512. },
  11513. /**
  11514. * Animate in the series
  11515. */
  11516. animate: function (init) {
  11517. var series = this,
  11518. chart = series.chart,
  11519. renderer = chart.renderer,
  11520. clipRect,
  11521. markerClipRect,
  11522. animation = series.options.animation,
  11523. clipBox = chart.clipBox,
  11524. inverted = chart.inverted,
  11525. sharedClipKey;
  11526. // Animation option is set to true
  11527. if (animation && !isObject(animation)) {
  11528. animation = defaultPlotOptions[series.type].animation;
  11529. }
  11530. sharedClipKey = '_sharedClip' + animation.duration + animation.easing;
  11531. // Initialize the animation. Set up the clipping rectangle.
  11532. if (init) {
  11533. // If a clipping rectangle with the same properties is currently present in the chart, use that.
  11534. clipRect = chart[sharedClipKey];
  11535. markerClipRect = chart[sharedClipKey + 'm'];
  11536. if (!clipRect) {
  11537. chart[sharedClipKey] = clipRect = renderer.clipRect(
  11538. extend(clipBox, { width: 0 })
  11539. );
  11540. chart[sharedClipKey + 'm'] = markerClipRect = renderer.clipRect(
  11541. -99, // include the width of the first marker
  11542. inverted ? -chart.plotLeft : -chart.plotTop,
  11543. 99,
  11544. inverted ? chart.chartWidth : chart.chartHeight
  11545. );
  11546. }
  11547. series.group.clip(clipRect);
  11548. series.markerGroup.clip(markerClipRect);
  11549. series.sharedClipKey = sharedClipKey;
  11550. // Run the animation
  11551. } else {
  11552. clipRect = chart[sharedClipKey];
  11553. if (clipRect) {
  11554. clipRect.animate({
  11555. width: chart.plotSizeX
  11556. }, animation);
  11557. chart[sharedClipKey + 'm'].animate({
  11558. width: chart.plotSizeX + 99
  11559. }, animation);
  11560. }
  11561. // Delete this function to allow it only once
  11562. series.animate = null;
  11563. // Call the afterAnimate function on animation complete (but don't overwrite the animation.complete option
  11564. // which should be available to the user).
  11565. series.animationTimeout = setTimeout(function () {
  11566. series.afterAnimate();
  11567. }, animation.duration);
  11568. }
  11569. },
  11570. /**
  11571. * This runs after animation to land on the final plot clipping
  11572. */
  11573. afterAnimate: function () {
  11574. var chart = this.chart,
  11575. sharedClipKey = this.sharedClipKey,
  11576. group = this.group;
  11577. if (group && this.options.clip !== false) {
  11578. group.clip(chart.clipRect);
  11579. this.markerGroup.clip(); // no clip
  11580. }
  11581. // Remove the shared clipping rectancgle when all series are shown
  11582. setTimeout(function () {
  11583. if (sharedClipKey && chart[sharedClipKey]) {
  11584. chart[sharedClipKey] = chart[sharedClipKey].destroy();
  11585. chart[sharedClipKey + 'm'] = chart[sharedClipKey + 'm'].destroy();
  11586. }
  11587. }, 100);
  11588. },
  11589. /**
  11590. * Draw the markers
  11591. */
  11592. drawPoints: function () {
  11593. var series = this,
  11594. pointAttr,
  11595. points = series.points,
  11596. chart = series.chart,
  11597. plotX,
  11598. plotY,
  11599. i,
  11600. point,
  11601. radius,
  11602. symbol,
  11603. isImage,
  11604. graphic,
  11605. options = series.options,
  11606. seriesMarkerOptions = options.marker,
  11607. pointMarkerOptions,
  11608. enabled,
  11609. isInside,
  11610. markerGroup = series.markerGroup;
  11611. if (seriesMarkerOptions.enabled || series._hasPointMarkers) {
  11612. i = points.length;
  11613. while (i--) {
  11614. point = points[i];
  11615. plotX = point.plotX;
  11616. plotY = point.plotY;
  11617. graphic = point.graphic;
  11618. pointMarkerOptions = point.marker || {};
  11619. enabled = (seriesMarkerOptions.enabled && pointMarkerOptions.enabled === UNDEFINED) || pointMarkerOptions.enabled;
  11620. isInside = chart.isInsidePlot(mathRound(plotX), plotY, chart.inverted); // #1858
  11621. // only draw the point if y is defined
  11622. if (enabled && plotY !== UNDEFINED && !isNaN(plotY) && point.y !== null) {
  11623. // shortcuts
  11624. pointAttr = point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE];
  11625. radius = pointAttr.r;
  11626. symbol = pick(pointMarkerOptions.symbol, series.symbol);
  11627. isImage = symbol.indexOf('url') === 0;
  11628. if (graphic) { // update
  11629. graphic
  11630. .attr({ // Since the marker group isn't clipped, each individual marker must be toggled
  11631. visibility: isInside ? (hasSVG ? 'inherit' : VISIBLE) : HIDDEN
  11632. })
  11633. .animate(extend({
  11634. x: plotX - radius,
  11635. y: plotY - radius
  11636. }, graphic.symbolName ? { // don't apply to image symbols #507
  11637. width: 2 * radius,
  11638. height: 2 * radius
  11639. } : {}));
  11640. } else if (isInside && (radius > 0 || isImage)) {
  11641. point.graphic = graphic = chart.renderer.symbol(
  11642. symbol,
  11643. plotX - radius,
  11644. plotY - radius,
  11645. 2 * radius,
  11646. 2 * radius
  11647. )
  11648. .attr(pointAttr)
  11649. .add(markerGroup);
  11650. }
  11651. } else if (graphic) {
  11652. point.graphic = graphic.destroy(); // #1269
  11653. }
  11654. }
  11655. }
  11656. },
  11657. /**
  11658. * Convert state properties from API naming conventions to SVG attributes
  11659. *
  11660. * @param {Object} options API options object
  11661. * @param {Object} base1 SVG attribute object to inherit from
  11662. * @param {Object} base2 Second level SVG attribute object to inherit from
  11663. */
  11664. convertAttribs: function (options, base1, base2, base3) {
  11665. var conversion = this.pointAttrToOptions,
  11666. attr,
  11667. option,
  11668. obj = {};
  11669. options = options || {};
  11670. base1 = base1 || {};
  11671. base2 = base2 || {};
  11672. base3 = base3 || {};
  11673. for (attr in conversion) {
  11674. option = conversion[attr];
  11675. obj[attr] = pick(options[option], base1[attr], base2[attr], base3[attr]);
  11676. }
  11677. return obj;
  11678. },
  11679. /**
  11680. * Get the state attributes. Each series type has its own set of attributes
  11681. * that are allowed to change on a point's state change. Series wide attributes are stored for
  11682. * all series, and additionally point specific attributes are stored for all
  11683. * points with individual marker options. If such options are not defined for the point,
  11684. * a reference to the series wide attributes is stored in point.pointAttr.
  11685. */
  11686. getAttribs: function () {
  11687. var series = this,
  11688. seriesOptions = series.options,
  11689. normalOptions = defaultPlotOptions[series.type].marker ? seriesOptions.marker : seriesOptions,
  11690. stateOptions = normalOptions.states,
  11691. stateOptionsHover = stateOptions[HOVER_STATE],
  11692. pointStateOptionsHover,
  11693. seriesColor = series.color,
  11694. normalDefaults = {
  11695. stroke: seriesColor,
  11696. fill: seriesColor
  11697. },
  11698. points = series.points || [], // #927
  11699. i,
  11700. point,
  11701. seriesPointAttr = [],
  11702. pointAttr,
  11703. pointAttrToOptions = series.pointAttrToOptions,
  11704. hasPointSpecificOptions,
  11705. negativeColor = seriesOptions.negativeColor,
  11706. key;
  11707. // series type specific modifications
  11708. if (seriesOptions.marker) { // line, spline, area, areaspline, scatter
  11709. // if no hover radius is given, default to normal radius + 2
  11710. stateOptionsHover.radius = stateOptionsHover.radius || normalOptions.radius + 2;
  11711. stateOptionsHover.lineWidth = stateOptionsHover.lineWidth || normalOptions.lineWidth + 1;
  11712. } else { // column, bar, pie
  11713. // if no hover color is given, brighten the normal color
  11714. stateOptionsHover.color = stateOptionsHover.color ||
  11715. Color(stateOptionsHover.color || seriesColor)
  11716. .brighten(stateOptionsHover.brightness).get();
  11717. }
  11718. // general point attributes for the series normal state
  11719. seriesPointAttr[NORMAL_STATE] = series.convertAttribs(normalOptions, normalDefaults);
  11720. // HOVER_STATE and SELECT_STATE states inherit from normal state except the default radius
  11721. each([HOVER_STATE, SELECT_STATE], function (state) {
  11722. seriesPointAttr[state] =
  11723. series.convertAttribs(stateOptions[state], seriesPointAttr[NORMAL_STATE]);
  11724. });
  11725. // set it
  11726. series.pointAttr = seriesPointAttr;
  11727. // Generate the point-specific attribute collections if specific point
  11728. // options are given. If not, create a referance to the series wide point
  11729. // attributes
  11730. i = points.length;
  11731. while (i--) {
  11732. point = points[i];
  11733. normalOptions = (point.options && point.options.marker) || point.options;
  11734. if (normalOptions && normalOptions.enabled === false) {
  11735. normalOptions.radius = 0;
  11736. }
  11737. if (point.negative && negativeColor) {
  11738. point.color = point.fillColor = negativeColor;
  11739. }
  11740. hasPointSpecificOptions = seriesOptions.colorByPoint || point.color; // #868
  11741. // check if the point has specific visual options
  11742. if (point.options) {
  11743. for (key in pointAttrToOptions) {
  11744. if (defined(normalOptions[pointAttrToOptions[key]])) {
  11745. hasPointSpecificOptions = true;
  11746. }
  11747. }
  11748. }
  11749. // a specific marker config object is defined for the individual point:
  11750. // create it's own attribute collection
  11751. if (hasPointSpecificOptions) {
  11752. normalOptions = normalOptions || {};
  11753. pointAttr = [];
  11754. stateOptions = normalOptions.states || {}; // reassign for individual point
  11755. pointStateOptionsHover = stateOptions[HOVER_STATE] = stateOptions[HOVER_STATE] || {};
  11756. // Handle colors for column and pies
  11757. if (!seriesOptions.marker) { // column, bar, point
  11758. // if no hover color is given, brighten the normal color
  11759. pointStateOptionsHover.color =
  11760. Color(pointStateOptionsHover.color || point.color)
  11761. .brighten(pointStateOptionsHover.brightness ||
  11762. stateOptionsHover.brightness).get();
  11763. }
  11764. // normal point state inherits series wide normal state
  11765. pointAttr[NORMAL_STATE] = series.convertAttribs(extend({
  11766. color: point.color // #868
  11767. }, normalOptions), seriesPointAttr[NORMAL_STATE]);
  11768. // inherit from point normal and series hover
  11769. pointAttr[HOVER_STATE] = series.convertAttribs(
  11770. stateOptions[HOVER_STATE],
  11771. seriesPointAttr[HOVER_STATE],
  11772. pointAttr[NORMAL_STATE]
  11773. );
  11774. // inherit from point normal and series hover
  11775. pointAttr[SELECT_STATE] = series.convertAttribs(
  11776. stateOptions[SELECT_STATE],
  11777. seriesPointAttr[SELECT_STATE],
  11778. pointAttr[NORMAL_STATE]
  11779. );
  11780. // Force the fill to negativeColor on markers
  11781. if (point.negative && seriesOptions.marker && negativeColor) {
  11782. pointAttr[NORMAL_STATE].fill = pointAttr[HOVER_STATE].fill = pointAttr[SELECT_STATE].fill =
  11783. series.convertAttribs({ fillColor: negativeColor }).fill;
  11784. }
  11785. // no marker config object is created: copy a reference to the series-wide
  11786. // attribute collection
  11787. } else {
  11788. pointAttr = seriesPointAttr;
  11789. }
  11790. point.pointAttr = pointAttr;
  11791. }
  11792. },
  11793. /**
  11794. * Update the series with a new set of options
  11795. */
  11796. update: function (newOptions, redraw) {
  11797. var chart = this.chart,
  11798. // must use user options when changing type because this.options is merged
  11799. // in with type specific plotOptions
  11800. oldOptions = this.userOptions,
  11801. oldType = this.type;
  11802. // Do the merge, with some forced options
  11803. newOptions = merge(oldOptions, {
  11804. animation: false,
  11805. index: this.index,
  11806. pointStart: this.xData[0] // when updating after addPoint
  11807. }, newOptions);
  11808. // Destroy the series and reinsert methods from the type prototype
  11809. this.remove(false);
  11810. extend(this, seriesTypes[newOptions.type || oldType].prototype);
  11811. this.init(chart, newOptions);
  11812. if (pick(redraw, true)) {
  11813. chart.redraw(false);
  11814. }
  11815. },
  11816. /**
  11817. * Clear DOM objects and free up memory
  11818. */
  11819. destroy: function () {
  11820. var series = this,
  11821. chart = series.chart,
  11822. issue134 = /AppleWebKit\/533/.test(userAgent),
  11823. destroy,
  11824. i,
  11825. data = series.data || [],
  11826. point,
  11827. prop,
  11828. axis;
  11829. // add event hook
  11830. fireEvent(series, 'destroy');
  11831. // remove all events
  11832. removeEvent(series);
  11833. // erase from axes
  11834. each(['xAxis', 'yAxis'], function (AXIS) {
  11835. axis = series[AXIS];
  11836. if (axis) {
  11837. erase(axis.series, series);
  11838. axis.isDirty = axis.forceRedraw = true;
  11839. }
  11840. });
  11841. // remove legend items
  11842. if (series.legendItem) {
  11843. series.chart.legend.destroyItem(series);
  11844. }
  11845. // destroy all points with their elements
  11846. i = data.length;
  11847. while (i--) {
  11848. point = data[i];
  11849. if (point && point.destroy) {
  11850. point.destroy();
  11851. }
  11852. }
  11853. series.points = null;
  11854. // Clear the animation timeout if we are destroying the series during initial animation
  11855. clearTimeout(series.animationTimeout);
  11856. // destroy all SVGElements associated to the series
  11857. each(['area', 'graph', 'dataLabelsGroup', 'group', 'markerGroup', 'tracker',
  11858. 'graphNeg', 'areaNeg', 'posClip', 'negClip'], function (prop) {
  11859. if (series[prop]) {
  11860. // issue 134 workaround
  11861. destroy = issue134 && prop === 'group' ?
  11862. 'hide' :
  11863. 'destroy';
  11864. series[prop][destroy]();
  11865. }
  11866. });
  11867. // remove from hoverSeries
  11868. if (chart.hoverSeries === series) {
  11869. chart.hoverSeries = null;
  11870. }
  11871. erase(chart.series, series);
  11872. // clear all members
  11873. for (prop in series) {
  11874. delete series[prop];
  11875. }
  11876. },
  11877. /**
  11878. * Draw the data labels
  11879. */
  11880. drawDataLabels: function () {
  11881. var series = this,
  11882. seriesOptions = series.options,
  11883. options = seriesOptions.dataLabels,
  11884. points = series.points,
  11885. pointOptions,
  11886. generalOptions,
  11887. str,
  11888. dataLabelsGroup;
  11889. if (options.enabled || series._hasPointLabels) {
  11890. // Process default alignment of data labels for columns
  11891. if (series.dlProcessOptions) {
  11892. series.dlProcessOptions(options);
  11893. }
  11894. // Create a separate group for the data labels to avoid rotation
  11895. dataLabelsGroup = series.plotGroup(
  11896. 'dataLabelsGroup',
  11897. 'data-labels',
  11898. series.visible ? VISIBLE : HIDDEN,
  11899. options.zIndex || 6
  11900. );
  11901. // Make the labels for each point
  11902. generalOptions = options;
  11903. each(points, function (point) {
  11904. var enabled,
  11905. dataLabel = point.dataLabel,
  11906. labelConfig,
  11907. attr,
  11908. name,
  11909. rotation,
  11910. connector = point.connector,
  11911. isNew = true;
  11912. // Determine if each data label is enabled
  11913. pointOptions = point.options && point.options.dataLabels;
  11914. enabled = generalOptions.enabled || (pointOptions && pointOptions.enabled);
  11915. // If the point is outside the plot area, destroy it. #678, #820
  11916. if (dataLabel && !enabled) {
  11917. point.dataLabel = dataLabel.destroy();
  11918. // Individual labels are disabled if the are explicitly disabled
  11919. // in the point options, or if they fall outside the plot area.
  11920. } else if (enabled) {
  11921. rotation = options.rotation;
  11922. // Create individual options structure that can be extended without
  11923. // affecting others
  11924. options = merge(generalOptions, pointOptions);
  11925. // Get the string
  11926. labelConfig = point.getLabelConfig();
  11927. str = options.format ?
  11928. format(options.format, labelConfig) :
  11929. options.formatter.call(labelConfig, options);
  11930. // Determine the color
  11931. options.style.color = pick(options.color, options.style.color, series.color, 'black');
  11932. // update existing label
  11933. if (dataLabel) {
  11934. if (defined(str)) {
  11935. dataLabel
  11936. .attr({
  11937. text: str
  11938. });
  11939. isNew = false;
  11940. } else { // #1437 - the label is shown conditionally
  11941. point.dataLabel = dataLabel = dataLabel.destroy();
  11942. if (connector) {
  11943. point.connector = connector.destroy();
  11944. }
  11945. }
  11946. // create new label
  11947. } else if (defined(str)) {
  11948. attr = {
  11949. //align: align,
  11950. fill: options.backgroundColor,
  11951. stroke: options.borderColor,
  11952. 'stroke-width': options.borderWidth,
  11953. r: options.borderRadius || 0,
  11954. rotation: rotation,
  11955. padding: options.padding,
  11956. zIndex: 1
  11957. };
  11958. // Remove unused attributes (#947)
  11959. for (name in attr) {
  11960. if (attr[name] === UNDEFINED) {
  11961. delete attr[name];
  11962. }
  11963. }
  11964. dataLabel = point.dataLabel = series.chart.renderer[rotation ? 'text' : 'label']( // labels don't support rotation
  11965. str,
  11966. 0,
  11967. -999,
  11968. null,
  11969. null,
  11970. null,
  11971. options.useHTML
  11972. )
  11973. .attr(attr)
  11974. .css(options.style)
  11975. .add(dataLabelsGroup)
  11976. .shadow(options.shadow);
  11977. }
  11978. if (dataLabel) {
  11979. // Now the data label is created and placed at 0,0, so we need to align it
  11980. series.alignDataLabel(point, dataLabel, options, null, isNew);
  11981. }
  11982. }
  11983. });
  11984. }
  11985. },
  11986. /**
  11987. * Align each individual data label
  11988. */
  11989. alignDataLabel: function (point, dataLabel, options, alignTo, isNew) {
  11990. var chart = this.chart,
  11991. inverted = chart.inverted,
  11992. plotX = pick(point.plotX, -999),
  11993. plotY = pick(point.plotY, -999),
  11994. bBox = dataLabel.getBBox(),
  11995. alignAttr; // the final position;
  11996. // The alignment box is a singular point
  11997. alignTo = extend({
  11998. x: inverted ? chart.plotWidth - plotY : plotX,
  11999. y: mathRound(inverted ? chart.plotHeight - plotX : plotY),
  12000. width: 0,
  12001. height: 0
  12002. }, alignTo);
  12003. // Add the text size for alignment calculation
  12004. extend(options, {
  12005. width: bBox.width,
  12006. height: bBox.height
  12007. });
  12008. // Allow a hook for changing alignment in the last moment, then do the alignment
  12009. if (options.rotation) { // Fancy box alignment isn't supported for rotated text
  12010. alignAttr = {
  12011. align: options.align,
  12012. x: alignTo.x + options.x + alignTo.width / 2,
  12013. y: alignTo.y + options.y + alignTo.height / 2
  12014. };
  12015. dataLabel[isNew ? 'attr' : 'animate'](alignAttr);
  12016. } else {
  12017. dataLabel.align(options, null, alignTo);
  12018. alignAttr = dataLabel.alignAttr;
  12019. }
  12020. // Show or hide based on the final aligned position
  12021. dataLabel.attr({
  12022. visibility: options.crop === false || /*chart.isInsidePlot(alignAttr.x, alignAttr.y) || */chart.isInsidePlot(plotX, plotY, inverted) ?
  12023. (chart.renderer.isSVG ? 'inherit' : VISIBLE) :
  12024. HIDDEN
  12025. });
  12026. },
  12027. /**
  12028. * Return the graph path of a segment
  12029. */
  12030. getSegmentPath: function (segment) {
  12031. var series = this,
  12032. segmentPath = [],
  12033. step = series.options.step;
  12034. // build the segment line
  12035. each(segment, function (point, i) {
  12036. var plotX = point.plotX,
  12037. plotY = point.plotY,
  12038. lastPoint;
  12039. if (series.getPointSpline) { // generate the spline as defined in the SplineSeries object
  12040. segmentPath.push.apply(segmentPath, series.getPointSpline(segment, point, i));
  12041. } else {
  12042. // moveTo or lineTo
  12043. segmentPath.push(i ? L : M);
  12044. // step line?
  12045. if (step && i) {
  12046. lastPoint = segment[i - 1];
  12047. if (step === 'right') {
  12048. segmentPath.push(
  12049. lastPoint.plotX,
  12050. plotY
  12051. );
  12052. } else if (step === 'center') {
  12053. segmentPath.push(
  12054. (lastPoint.plotX + plotX) / 2,
  12055. lastPoint.plotY,
  12056. (lastPoint.plotX + plotX) / 2,
  12057. plotY
  12058. );
  12059. } else {
  12060. segmentPath.push(
  12061. plotX,
  12062. lastPoint.plotY
  12063. );
  12064. }
  12065. }
  12066. // normal line to next point
  12067. segmentPath.push(
  12068. point.plotX,
  12069. point.plotY
  12070. );
  12071. }
  12072. });
  12073. return segmentPath;
  12074. },
  12075. /**
  12076. * Get the graph path
  12077. */
  12078. getGraphPath: function () {
  12079. var series = this,
  12080. graphPath = [],
  12081. segmentPath,
  12082. singlePoints = []; // used in drawTracker
  12083. // Divide into segments and build graph and area paths
  12084. each(series.segments, function (segment) {
  12085. segmentPath = series.getSegmentPath(segment);
  12086. // add the segment to the graph, or a single point for tracking
  12087. if (segment.length > 1) {
  12088. graphPath = graphPath.concat(segmentPath);
  12089. } else {
  12090. singlePoints.push(segment[0]);
  12091. }
  12092. });
  12093. // Record it for use in drawGraph and drawTracker, and return graphPath
  12094. series.singlePoints = singlePoints;
  12095. series.graphPath = graphPath;
  12096. return graphPath;
  12097. },
  12098. /**
  12099. * Draw the actual graph
  12100. */
  12101. drawGraph: function () {
  12102. var series = this,
  12103. options = this.options,
  12104. props = [['graph', options.lineColor || this.color]],
  12105. lineWidth = options.lineWidth,
  12106. dashStyle = options.dashStyle,
  12107. graphPath = this.getGraphPath(),
  12108. negativeColor = options.negativeColor;
  12109. if (negativeColor) {
  12110. props.push(['graphNeg', negativeColor]);
  12111. }
  12112. // draw the graph
  12113. each(props, function (prop, i) {
  12114. var graphKey = prop[0],
  12115. graph = series[graphKey],
  12116. attribs;
  12117. if (graph) {
  12118. stop(graph); // cancel running animations, #459
  12119. graph.animate({ d: graphPath });
  12120. } else if (lineWidth && graphPath.length) { // #1487
  12121. attribs = {
  12122. stroke: prop[1],
  12123. 'stroke-width': lineWidth,
  12124. zIndex: 1 // #1069
  12125. };
  12126. if (dashStyle) {
  12127. attribs.dashstyle = dashStyle;
  12128. }
  12129. series[graphKey] = series.chart.renderer.path(graphPath)
  12130. .attr(attribs)
  12131. .add(series.group)
  12132. .shadow(!i && options.shadow);
  12133. }
  12134. });
  12135. },
  12136. /**
  12137. * Clip the graphs into the positive and negative coloured graphs
  12138. */
  12139. clipNeg: function () {
  12140. var options = this.options,
  12141. chart = this.chart,
  12142. renderer = chart.renderer,
  12143. negativeColor = options.negativeColor,
  12144. translatedThreshold,
  12145. posAttr,
  12146. negAttr,
  12147. graph = this.graph,
  12148. area = this.area,
  12149. posClip = this.posClip,
  12150. negClip = this.negClip,
  12151. chartWidth = chart.chartWidth,
  12152. chartHeight = chart.chartHeight,
  12153. chartSizeMax = mathMax(chartWidth, chartHeight),
  12154. above,
  12155. below;
  12156. if (negativeColor && (graph || area)) {
  12157. translatedThreshold = mathCeil(this.yAxis.len - this.yAxis.translate(options.threshold || 0));
  12158. above = {
  12159. x: 0,
  12160. y: 0,
  12161. width: chartSizeMax,
  12162. height: translatedThreshold
  12163. };
  12164. below = {
  12165. x: 0,
  12166. y: translatedThreshold,
  12167. width: chartSizeMax,
  12168. height: chartSizeMax - translatedThreshold
  12169. };
  12170. if (chart.inverted && renderer.isVML) {
  12171. above = {
  12172. x: chart.plotWidth - translatedThreshold - chart.plotLeft,
  12173. y: 0,
  12174. width: chartWidth,
  12175. height: chartHeight
  12176. };
  12177. below = {
  12178. x: translatedThreshold + chart.plotLeft - chartWidth,
  12179. y: 0,
  12180. width: chart.plotLeft + translatedThreshold,
  12181. height: chartWidth
  12182. };
  12183. }
  12184. if (this.yAxis.reversed) {
  12185. posAttr = below;
  12186. negAttr = above;
  12187. } else {
  12188. posAttr = above;
  12189. negAttr = below;
  12190. }
  12191. if (posClip) { // update
  12192. posClip.animate(posAttr);
  12193. negClip.animate(negAttr);
  12194. } else {
  12195. this.posClip = posClip = renderer.clipRect(posAttr);
  12196. this.negClip = negClip = renderer.clipRect(negAttr);
  12197. if (graph) {
  12198. graph.clip(posClip);
  12199. this.graphNeg.clip(negClip);
  12200. }
  12201. if (area) {
  12202. area.clip(posClip);
  12203. this.areaNeg.clip(negClip);
  12204. }
  12205. }
  12206. }
  12207. },
  12208. /**
  12209. * Initialize and perform group inversion on series.group and series.markerGroup
  12210. */
  12211. invertGroups: function () {
  12212. var series = this,
  12213. chart = series.chart;
  12214. // Pie, go away (#1736)
  12215. if (!series.xAxis) {
  12216. return;
  12217. }
  12218. // A fixed size is needed for inversion to work
  12219. function setInvert() {
  12220. var size = {
  12221. width: series.yAxis.len,
  12222. height: series.xAxis.len
  12223. };
  12224. each(['group', 'markerGroup'], function (groupName) {
  12225. if (series[groupName]) {
  12226. series[groupName].attr(size).invert();
  12227. }
  12228. });
  12229. }
  12230. addEvent(chart, 'resize', setInvert); // do it on resize
  12231. addEvent(series, 'destroy', function () {
  12232. removeEvent(chart, 'resize', setInvert);
  12233. });
  12234. // Do it now
  12235. setInvert(); // do it now
  12236. // On subsequent render and redraw, just do setInvert without setting up events again
  12237. series.invertGroups = setInvert;
  12238. },
  12239. /**
  12240. * General abstraction for creating plot groups like series.group, series.dataLabelsGroup and
  12241. * series.markerGroup. On subsequent calls, the group will only be adjusted to the updated plot size.
  12242. */
  12243. plotGroup: function (prop, name, visibility, zIndex, parent) {
  12244. var group = this[prop],
  12245. isNew = !group,
  12246. chart = this.chart,
  12247. xAxis = this.xAxis,
  12248. yAxis = this.yAxis;
  12249. // Generate it on first call
  12250. if (isNew) {
  12251. this[prop] = group = chart.renderer.g(name)
  12252. .attr({
  12253. visibility: visibility,
  12254. zIndex: zIndex || 0.1 // IE8 needs this
  12255. })
  12256. .add(parent);
  12257. }
  12258. // Place it on first and subsequent (redraw) calls
  12259. group[isNew ? 'attr' : 'animate']({
  12260. translateX: xAxis ? xAxis.left : chart.plotLeft,
  12261. translateY: yAxis ? yAxis.top : chart.plotTop,
  12262. scaleX: 1, // #1623
  12263. scaleY: 1
  12264. });
  12265. return group;
  12266. },
  12267. /**
  12268. * Render the graph and markers
  12269. */
  12270. render: function () {
  12271. var series = this,
  12272. chart = series.chart,
  12273. group,
  12274. options = series.options,
  12275. animation = options.animation,
  12276. doAnimation = animation && !!series.animate &&
  12277. chart.renderer.isSVG, // this animation doesn't work in IE8 quirks when the group div is hidden,
  12278. // and looks bad in other oldIE
  12279. visibility = series.visible ? VISIBLE : HIDDEN,
  12280. zIndex = options.zIndex,
  12281. hasRendered = series.hasRendered,
  12282. chartSeriesGroup = chart.seriesGroup;
  12283. // the group
  12284. group = series.plotGroup(
  12285. 'group',
  12286. 'series',
  12287. visibility,
  12288. zIndex,
  12289. chartSeriesGroup
  12290. );
  12291. series.markerGroup = series.plotGroup(
  12292. 'markerGroup',
  12293. 'markers',
  12294. visibility,
  12295. zIndex,
  12296. chartSeriesGroup
  12297. );
  12298. // initiate the animation
  12299. if (doAnimation) {
  12300. series.animate(true);
  12301. }
  12302. // cache attributes for shapes
  12303. series.getAttribs();
  12304. // SVGRenderer needs to know this before drawing elements (#1089, #1795)
  12305. group.inverted = series.isCartesian ? chart.inverted : false;
  12306. // draw the graph if any
  12307. if (series.drawGraph) {
  12308. series.drawGraph();
  12309. series.clipNeg();
  12310. }
  12311. // draw the data labels (inn pies they go before the points)
  12312. series.drawDataLabels();
  12313. // draw the points
  12314. series.drawPoints();
  12315. // draw the mouse tracking area
  12316. if (series.options.enableMouseTracking !== false) {
  12317. series.drawTracker();
  12318. }
  12319. // Handle inverted series and tracker groups
  12320. if (chart.inverted) {
  12321. series.invertGroups();
  12322. }
  12323. // Initial clipping, must be defined after inverting groups for VML
  12324. if (options.clip !== false && !series.sharedClipKey && !hasRendered) {
  12325. group.clip(chart.clipRect);
  12326. }
  12327. // Run the animation
  12328. if (doAnimation) {
  12329. series.animate();
  12330. } else if (!hasRendered) {
  12331. series.afterAnimate();
  12332. }
  12333. series.isDirty = series.isDirtyData = false; // means data is in accordance with what you see
  12334. // (See #322) series.isDirty = series.isDirtyData = false; // means data is in accordance with what you see
  12335. series.hasRendered = true;
  12336. },
  12337. /**
  12338. * Redraw the series after an update in the axes.
  12339. */
  12340. redraw: function () {
  12341. var series = this,
  12342. chart = series.chart,
  12343. wasDirtyData = series.isDirtyData, // cache it here as it is set to false in render, but used after
  12344. group = series.group,
  12345. xAxis = series.xAxis,
  12346. yAxis = series.yAxis;
  12347. // reposition on resize
  12348. if (group) {
  12349. if (chart.inverted) {
  12350. group.attr({
  12351. width: chart.plotWidth,
  12352. height: chart.plotHeight
  12353. });
  12354. }
  12355. group.animate({
  12356. translateX: pick(xAxis && xAxis.left, chart.plotLeft),
  12357. translateY: pick(yAxis && yAxis.top, chart.plotTop)
  12358. });
  12359. }
  12360. series.translate();
  12361. series.setTooltipPoints(true);
  12362. series.render();
  12363. if (wasDirtyData) {
  12364. fireEvent(series, 'updatedData');
  12365. }
  12366. },
  12367. /**
  12368. * Set the state of the graph
  12369. */
  12370. setState: function (state) {
  12371. var series = this,
  12372. options = series.options,
  12373. graph = series.graph,
  12374. graphNeg = series.graphNeg,
  12375. stateOptions = options.states,
  12376. lineWidth = options.lineWidth,
  12377. attribs;
  12378. state = state || NORMAL_STATE;
  12379. if (series.state !== state) {
  12380. series.state = state;
  12381. if (stateOptions[state] && stateOptions[state].enabled === false) {
  12382. return;
  12383. }
  12384. if (state) {
  12385. lineWidth = stateOptions[state].lineWidth || lineWidth + 1;
  12386. }
  12387. if (graph && !graph.dashstyle) { // hover is turned off for dashed lines in VML
  12388. attribs = {
  12389. 'stroke-width': lineWidth
  12390. };
  12391. // use attr because animate will cause any other animation on the graph to stop
  12392. graph.attr(attribs);
  12393. if (graphNeg) {
  12394. graphNeg.attr(attribs);
  12395. }
  12396. }
  12397. }
  12398. },
  12399. /**
  12400. * Set the visibility of the graph
  12401. *
  12402. * @param vis {Boolean} True to show the series, false to hide. If UNDEFINED,
  12403. * the visibility is toggled.
  12404. */
  12405. setVisible: function (vis, redraw) {
  12406. var series = this,
  12407. chart = series.chart,
  12408. legendItem = series.legendItem,
  12409. showOrHide,
  12410. ignoreHiddenSeries = chart.options.chart.ignoreHiddenSeries,
  12411. oldVisibility = series.visible;
  12412. // if called without an argument, toggle visibility
  12413. series.visible = vis = series.userOptions.visible = vis === UNDEFINED ? !oldVisibility : vis;
  12414. showOrHide = vis ? 'show' : 'hide';
  12415. // show or hide elements
  12416. each(['group', 'dataLabelsGroup', 'markerGroup', 'tracker'], function (key) {
  12417. if (series[key]) {
  12418. series[key][showOrHide]();
  12419. }
  12420. });
  12421. // hide tooltip (#1361)
  12422. if (chart.hoverSeries === series) {
  12423. series.onMouseOut();
  12424. }
  12425. if (legendItem) {
  12426. chart.legend.colorizeItem(series, vis);
  12427. }
  12428. // rescale or adapt to resized chart
  12429. series.isDirty = true;
  12430. // in a stack, all other series are affected
  12431. if (series.options.stacking) {
  12432. each(chart.series, function (otherSeries) {
  12433. if (otherSeries.options.stacking && otherSeries.visible) {
  12434. otherSeries.isDirty = true;
  12435. }
  12436. });
  12437. }
  12438. // show or hide linked series
  12439. each(series.linkedSeries, function (otherSeries) {
  12440. otherSeries.setVisible(vis, false);
  12441. });
  12442. if (ignoreHiddenSeries) {
  12443. chart.isDirtyBox = true;
  12444. }
  12445. if (redraw !== false) {
  12446. chart.redraw();
  12447. }
  12448. fireEvent(series, showOrHide);
  12449. },
  12450. /**
  12451. * Show the graph
  12452. */
  12453. show: function () {
  12454. this.setVisible(true);
  12455. },
  12456. /**
  12457. * Hide the graph
  12458. */
  12459. hide: function () {
  12460. this.setVisible(false);
  12461. },
  12462. /**
  12463. * Set the selected state of the graph
  12464. *
  12465. * @param selected {Boolean} True to select the series, false to unselect. If
  12466. * UNDEFINED, the selection state is toggled.
  12467. */
  12468. select: function (selected) {
  12469. var series = this;
  12470. // if called without an argument, toggle
  12471. series.selected = selected = (selected === UNDEFINED) ? !series.selected : selected;
  12472. if (series.checkbox) {
  12473. series.checkbox.checked = selected;
  12474. }
  12475. fireEvent(series, selected ? 'select' : 'unselect');
  12476. },
  12477. /**
  12478. * Draw the tracker object that sits above all data labels and markers to
  12479. * track mouse events on the graph or points. For the line type charts
  12480. * the tracker uses the same graphPath, but with a greater stroke width
  12481. * for better control.
  12482. */
  12483. drawTracker: function () {
  12484. var series = this,
  12485. options = series.options,
  12486. trackByArea = options.trackByArea,
  12487. trackerPath = [].concat(trackByArea ? series.areaPath : series.graphPath),
  12488. trackerPathLength = trackerPath.length,
  12489. chart = series.chart,
  12490. pointer = chart.pointer,
  12491. renderer = chart.renderer,
  12492. snap = chart.options.tooltip.snap,
  12493. tracker = series.tracker,
  12494. cursor = options.cursor,
  12495. css = cursor && { cursor: cursor },
  12496. singlePoints = series.singlePoints,
  12497. singlePoint,
  12498. i,
  12499. onMouseOver = function () {
  12500. if (chart.hoverSeries !== series) {
  12501. series.onMouseOver();
  12502. }
  12503. };
  12504. // Extend end points. A better way would be to use round linecaps,
  12505. // but those are not clickable in VML.
  12506. if (trackerPathLength && !trackByArea) {
  12507. i = trackerPathLength + 1;
  12508. while (i--) {
  12509. if (trackerPath[i] === M) { // extend left side
  12510. trackerPath.splice(i + 1, 0, trackerPath[i + 1] - snap, trackerPath[i + 2], L);
  12511. }
  12512. if ((i && trackerPath[i] === M) || i === trackerPathLength) { // extend right side
  12513. trackerPath.splice(i, 0, L, trackerPath[i - 2] + snap, trackerPath[i - 1]);
  12514. }
  12515. }
  12516. }
  12517. // handle single points
  12518. for (i = 0; i < singlePoints.length; i++) {
  12519. singlePoint = singlePoints[i];
  12520. trackerPath.push(M, singlePoint.plotX - snap, singlePoint.plotY,
  12521. L, singlePoint.plotX + snap, singlePoint.plotY);
  12522. }
  12523. // draw the tracker
  12524. if (tracker) {
  12525. tracker.attr({ d: trackerPath });
  12526. } else { // create
  12527. series.tracker = tracker = renderer.path(trackerPath)
  12528. .attr({
  12529. 'class': PREFIX + 'tracker',
  12530. 'stroke-linejoin': 'round', // #1225
  12531. visibility: series.visible ? VISIBLE : HIDDEN,
  12532. stroke: TRACKER_FILL,
  12533. fill: trackByArea ? TRACKER_FILL : NONE,
  12534. 'stroke-width' : options.lineWidth + (trackByArea ? 0 : 2 * snap),
  12535. zIndex: 2
  12536. })
  12537. .addClass(PREFIX + 'tracker')
  12538. .on('mouseover', onMouseOver)
  12539. .on('mouseout', function (e) { pointer.onTrackerMouseOut(e); })
  12540. .css(css)
  12541. .add(series.markerGroup);
  12542. if (hasTouch) {
  12543. tracker.on('touchstart', onMouseOver);
  12544. }
  12545. }
  12546. }
  12547. }; // end Series prototype
  12548. /**
  12549. * LineSeries object
  12550. */
  12551. var LineSeries = extendClass(Series);
  12552. seriesTypes.line = LineSeries;
  12553. /**
  12554. * Set the default options for area
  12555. */
  12556. defaultPlotOptions.area = merge(defaultSeriesOptions, {
  12557. threshold: 0
  12558. // trackByArea: false,
  12559. // lineColor: null, // overrides color, but lets fillColor be unaltered
  12560. // fillOpacity: 0.75,
  12561. // fillColor: null
  12562. });
  12563. /**
  12564. * AreaSeries object
  12565. */
  12566. var AreaSeries = extendClass(Series, {
  12567. type: 'area',
  12568. /**
  12569. * For stacks, don't split segments on null values. Instead, draw null values with
  12570. * no marker. Also insert dummy points for any X position that exists in other series
  12571. * in the stack.
  12572. */
  12573. getSegments: function () {
  12574. var segments = [],
  12575. segment = [],
  12576. keys = [],
  12577. xAxis = this.xAxis,
  12578. yAxis = this.yAxis,
  12579. stack = yAxis.stacks[this.stackKey],
  12580. pointMap = {},
  12581. plotX,
  12582. plotY,
  12583. points = this.points,
  12584. i,
  12585. x;
  12586. if (this.options.stacking && !this.cropped) { // cropped causes artefacts in Stock, and perf issue
  12587. // Create a map where we can quickly look up the points by their X value.
  12588. for (i = 0; i < points.length; i++) {
  12589. pointMap[points[i].x] = points[i];
  12590. }
  12591. // Sort the keys (#1651)
  12592. for (x in stack) {
  12593. keys.push(+x);
  12594. }
  12595. keys.sort(function (a, b) {
  12596. return a - b;
  12597. });
  12598. each(keys, function (x) {
  12599. // The point exists, push it to the segment
  12600. if (pointMap[x]) {
  12601. segment.push(pointMap[x]);
  12602. // There is no point for this X value in this series, so we
  12603. // insert a dummy point in order for the areas to be drawn
  12604. // correctly.
  12605. } else {
  12606. plotX = xAxis.translate(x);
  12607. plotY = yAxis.toPixels(stack[x].cum, true);
  12608. segment.push({
  12609. y: null,
  12610. plotX: plotX,
  12611. clientX: plotX,
  12612. plotY: plotY,
  12613. yBottom: plotY,
  12614. onMouseOver: noop
  12615. });
  12616. }
  12617. });
  12618. if (segment.length) {
  12619. segments.push(segment);
  12620. }
  12621. } else {
  12622. Series.prototype.getSegments.call(this);
  12623. segments = this.segments;
  12624. }
  12625. this.segments = segments;
  12626. },
  12627. /**
  12628. * Extend the base Series getSegmentPath method by adding the path for the area.
  12629. * This path is pushed to the series.areaPath property.
  12630. */
  12631. getSegmentPath: function (segment) {
  12632. var segmentPath = Series.prototype.getSegmentPath.call(this, segment), // call base method
  12633. areaSegmentPath = [].concat(segmentPath), // work on a copy for the area path
  12634. i,
  12635. options = this.options,
  12636. segLength = segmentPath.length;
  12637. if (segLength === 3) { // for animation from 1 to two points
  12638. areaSegmentPath.push(L, segmentPath[1], segmentPath[2]);
  12639. }
  12640. if (options.stacking && !this.closedStacks) {
  12641. // Follow stack back. Todo: implement areaspline. A general solution could be to
  12642. // reverse the entire graphPath of the previous series, though may be hard with
  12643. // splines and with series with different extremes
  12644. for (i = segment.length - 1; i >= 0; i--) {
  12645. // step line?
  12646. if (i < segment.length - 1 && options.step) {
  12647. areaSegmentPath.push(segment[i + 1].plotX, segment[i].yBottom);
  12648. }
  12649. areaSegmentPath.push(segment[i].plotX, segment[i].yBottom);
  12650. }
  12651. } else { // follow zero line back
  12652. this.closeSegment(areaSegmentPath, segment);
  12653. }
  12654. this.areaPath = this.areaPath.concat(areaSegmentPath);
  12655. return segmentPath;
  12656. },
  12657. /**
  12658. * Extendable method to close the segment path of an area. This is overridden in polar
  12659. * charts.
  12660. */
  12661. closeSegment: function (path, segment) {
  12662. var translatedThreshold = this.yAxis.getThreshold(this.options.threshold);
  12663. path.push(
  12664. L,
  12665. segment[segment.length - 1].plotX,
  12666. translatedThreshold,
  12667. L,
  12668. segment[0].plotX,
  12669. translatedThreshold
  12670. );
  12671. },
  12672. /**
  12673. * Draw the graph and the underlying area. This method calls the Series base
  12674. * function and adds the area. The areaPath is calculated in the getSegmentPath
  12675. * method called from Series.prototype.drawGraph.
  12676. */
  12677. drawGraph: function () {
  12678. // Define or reset areaPath
  12679. this.areaPath = [];
  12680. // Call the base method
  12681. Series.prototype.drawGraph.apply(this);
  12682. // Define local variables
  12683. var series = this,
  12684. areaPath = this.areaPath,
  12685. options = this.options,
  12686. negativeColor = options.negativeColor,
  12687. props = [['area', this.color, options.fillColor]]; // area name, main color, fill color
  12688. if (negativeColor) {
  12689. props.push(['areaNeg', options.negativeColor, options.negativeFillColor]);
  12690. }
  12691. each(props, function (prop) {
  12692. var areaKey = prop[0],
  12693. area = series[areaKey];
  12694. // Create or update the area
  12695. if (area) { // update
  12696. area.animate({ d: areaPath });
  12697. } else { // create
  12698. series[areaKey] = series.chart.renderer.path(areaPath)
  12699. .attr({
  12700. fill: pick(
  12701. prop[2],
  12702. Color(prop[1]).setOpacity(options.fillOpacity || 0.75).get()
  12703. ),
  12704. zIndex: 0 // #1069
  12705. }).add(series.group);
  12706. }
  12707. });
  12708. },
  12709. /**
  12710. * Get the series' symbol in the legend
  12711. *
  12712. * @param {Object} legend The legend object
  12713. * @param {Object} item The series (this) or point
  12714. */
  12715. drawLegendSymbol: function (legend, item) {
  12716. item.legendSymbol = this.chart.renderer.rect(
  12717. 0,
  12718. legend.baseline - 11,
  12719. legend.options.symbolWidth,
  12720. 12,
  12721. 2
  12722. ).attr({
  12723. zIndex: 3
  12724. }).add(item.legendGroup);
  12725. }
  12726. });
  12727. seriesTypes.area = AreaSeries;/**
  12728. * Set the default options for spline
  12729. */
  12730. defaultPlotOptions.spline = merge(defaultSeriesOptions);
  12731. /**
  12732. * SplineSeries object
  12733. */
  12734. var SplineSeries = extendClass(Series, {
  12735. type: 'spline',
  12736. /**
  12737. * Get the spline segment from a given point's previous neighbour to the given point
  12738. */
  12739. getPointSpline: function (segment, point, i) {
  12740. var smoothing = 1.5, // 1 means control points midway between points, 2 means 1/3 from the point, 3 is 1/4 etc
  12741. denom = smoothing + 1,
  12742. plotX = point.plotX,
  12743. plotY = point.plotY,
  12744. lastPoint = segment[i - 1],
  12745. nextPoint = segment[i + 1],
  12746. leftContX,
  12747. leftContY,
  12748. rightContX,
  12749. rightContY,
  12750. ret;
  12751. // find control points
  12752. if (lastPoint && nextPoint) {
  12753. var lastX = lastPoint.plotX,
  12754. lastY = lastPoint.plotY,
  12755. nextX = nextPoint.plotX,
  12756. nextY = nextPoint.plotY,
  12757. correction;
  12758. leftContX = (smoothing * plotX + lastX) / denom;
  12759. leftContY = (smoothing * plotY + lastY) / denom;
  12760. rightContX = (smoothing * plotX + nextX) / denom;
  12761. rightContY = (smoothing * plotY + nextY) / denom;
  12762. // have the two control points make a straight line through main point
  12763. correction = ((rightContY - leftContY) * (rightContX - plotX)) /
  12764. (rightContX - leftContX) + plotY - rightContY;
  12765. leftContY += correction;
  12766. rightContY += correction;
  12767. // to prevent false extremes, check that control points are between
  12768. // neighbouring points' y values
  12769. if (leftContY > lastY && leftContY > plotY) {
  12770. leftContY = mathMax(lastY, plotY);
  12771. rightContY = 2 * plotY - leftContY; // mirror of left control point
  12772. } else if (leftContY < lastY && leftContY < plotY) {
  12773. leftContY = mathMin(lastY, plotY);
  12774. rightContY = 2 * plotY - leftContY;
  12775. }
  12776. if (rightContY > nextY && rightContY > plotY) {
  12777. rightContY = mathMax(nextY, plotY);
  12778. leftContY = 2 * plotY - rightContY;
  12779. } else if (rightContY < nextY && rightContY < plotY) {
  12780. rightContY = mathMin(nextY, plotY);
  12781. leftContY = 2 * plotY - rightContY;
  12782. }
  12783. // record for drawing in next point
  12784. point.rightContX = rightContX;
  12785. point.rightContY = rightContY;
  12786. }
  12787. // Visualize control points for debugging
  12788. /*
  12789. if (leftContX) {
  12790. this.chart.renderer.circle(leftContX + this.chart.plotLeft, leftContY + this.chart.plotTop, 2)
  12791. .attr({
  12792. stroke: 'red',
  12793. 'stroke-width': 1,
  12794. fill: 'none'
  12795. })
  12796. .add();
  12797. this.chart.renderer.path(['M', leftContX + this.chart.plotLeft, leftContY + this.chart.plotTop,
  12798. 'L', plotX + this.chart.plotLeft, plotY + this.chart.plotTop])
  12799. .attr({
  12800. stroke: 'red',
  12801. 'stroke-width': 1
  12802. })
  12803. .add();
  12804. this.chart.renderer.circle(rightContX + this.chart.plotLeft, rightContY + this.chart.plotTop, 2)
  12805. .attr({
  12806. stroke: 'green',
  12807. 'stroke-width': 1,
  12808. fill: 'none'
  12809. })
  12810. .add();
  12811. this.chart.renderer.path(['M', rightContX + this.chart.plotLeft, rightContY + this.chart.plotTop,
  12812. 'L', plotX + this.chart.plotLeft, plotY + this.chart.plotTop])
  12813. .attr({
  12814. stroke: 'green',
  12815. 'stroke-width': 1
  12816. })
  12817. .add();
  12818. }
  12819. */
  12820. // moveTo or lineTo
  12821. if (!i) {
  12822. ret = [M, plotX, plotY];
  12823. } else { // curve from last point to this
  12824. ret = [
  12825. 'C',
  12826. lastPoint.rightContX || lastPoint.plotX,
  12827. lastPoint.rightContY || lastPoint.plotY,
  12828. leftContX || plotX,
  12829. leftContY || plotY,
  12830. plotX,
  12831. plotY
  12832. ];
  12833. lastPoint.rightContX = lastPoint.rightContY = null; // reset for updating series later
  12834. }
  12835. return ret;
  12836. }
  12837. });
  12838. seriesTypes.spline = SplineSeries;
  12839. /**
  12840. * Set the default options for areaspline
  12841. */
  12842. defaultPlotOptions.areaspline = merge(defaultPlotOptions.area);
  12843. /**
  12844. * AreaSplineSeries object
  12845. */
  12846. var areaProto = AreaSeries.prototype,
  12847. AreaSplineSeries = extendClass(SplineSeries, {
  12848. type: 'areaspline',
  12849. closedStacks: true, // instead of following the previous graph back, follow the threshold back
  12850. // Mix in methods from the area series
  12851. getSegmentPath: areaProto.getSegmentPath,
  12852. closeSegment: areaProto.closeSegment,
  12853. drawGraph: areaProto.drawGraph
  12854. });
  12855. seriesTypes.areaspline = AreaSplineSeries;
  12856. /**
  12857. * Set the default options for column
  12858. */
  12859. defaultPlotOptions.column = merge(defaultSeriesOptions, {
  12860. borderColor: '#FFFFFF',
  12861. borderWidth: 1,
  12862. borderRadius: 0,
  12863. //colorByPoint: undefined,
  12864. groupPadding: 0.2,
  12865. //grouping: true,
  12866. marker: null, // point options are specified in the base options
  12867. pointPadding: 0.1,
  12868. //pointWidth: null,
  12869. minPointLength: 0,
  12870. cropThreshold: 50, // when there are more points, they will not animate out of the chart on xAxis.setExtremes
  12871. pointRange: null, // null means auto, meaning 1 in a categorized axis and least distance between points if not categories
  12872. states: {
  12873. hover: {
  12874. brightness: 0.1,
  12875. shadow: false
  12876. },
  12877. select: {
  12878. color: '#C0C0C0',
  12879. borderColor: '#000000',
  12880. shadow: false
  12881. }
  12882. },
  12883. dataLabels: {
  12884. align: null, // auto
  12885. verticalAlign: null, // auto
  12886. y: null
  12887. },
  12888. stickyTracking: false,
  12889. threshold: 0
  12890. });
  12891. /**
  12892. * ColumnSeries object
  12893. */
  12894. var ColumnSeries = extendClass(Series, {
  12895. type: 'column',
  12896. tooltipOutsidePlot: true,
  12897. requireSorting: false,
  12898. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  12899. stroke: 'borderColor',
  12900. 'stroke-width': 'borderWidth',
  12901. fill: 'color',
  12902. r: 'borderRadius'
  12903. },
  12904. trackerGroups: ['group', 'dataLabelsGroup'],
  12905. /**
  12906. * Initialize the series
  12907. */
  12908. init: function () {
  12909. Series.prototype.init.apply(this, arguments);
  12910. var series = this,
  12911. chart = series.chart;
  12912. // if the series is added dynamically, force redraw of other
  12913. // series affected by a new column
  12914. if (chart.hasRendered) {
  12915. each(chart.series, function (otherSeries) {
  12916. if (otherSeries.type === series.type) {
  12917. otherSeries.isDirty = true;
  12918. }
  12919. });
  12920. }
  12921. },
  12922. /**
  12923. * Return the width and x offset of the columns adjusted for grouping, groupPadding, pointPadding,
  12924. * pointWidth etc.
  12925. */
  12926. getColumnMetrics: function () {
  12927. var series = this,
  12928. chart = series.chart,
  12929. options = series.options,
  12930. xAxis = this.xAxis,
  12931. reversedXAxis = xAxis.reversed,
  12932. stackKey,
  12933. stackGroups = {},
  12934. columnIndex,
  12935. columnCount = 0;
  12936. // Get the total number of column type series.
  12937. // This is called on every series. Consider moving this logic to a
  12938. // chart.orderStacks() function and call it on init, addSeries and removeSeries
  12939. if (options.grouping === false) {
  12940. columnCount = 1;
  12941. } else {
  12942. each(chart.series, function (otherSeries) {
  12943. var otherOptions = otherSeries.options;
  12944. if (otherSeries.type === series.type && otherSeries.visible &&
  12945. series.options.group === otherOptions.group) { // used in Stock charts navigator series
  12946. if (otherOptions.stacking) {
  12947. stackKey = otherSeries.stackKey;
  12948. if (stackGroups[stackKey] === UNDEFINED) {
  12949. stackGroups[stackKey] = columnCount++;
  12950. }
  12951. columnIndex = stackGroups[stackKey];
  12952. } else if (otherOptions.grouping !== false) { // #1162
  12953. columnIndex = columnCount++;
  12954. }
  12955. otherSeries.columnIndex = columnIndex;
  12956. }
  12957. });
  12958. }
  12959. var categoryWidth = mathMin(
  12960. mathAbs(xAxis.transA) * (xAxis.ordinalSlope || options.pointRange || xAxis.closestPointRange || 1),
  12961. xAxis.len // #1535
  12962. ),
  12963. groupPadding = categoryWidth * options.groupPadding,
  12964. groupWidth = categoryWidth - 2 * groupPadding,
  12965. pointOffsetWidth = groupWidth / columnCount,
  12966. optionPointWidth = options.pointWidth,
  12967. pointPadding = defined(optionPointWidth) ? (pointOffsetWidth - optionPointWidth) / 2 :
  12968. pointOffsetWidth * options.pointPadding,
  12969. pointWidth = pick(optionPointWidth, pointOffsetWidth - 2 * pointPadding), // exact point width, used in polar charts
  12970. colIndex = (reversedXAxis ?
  12971. columnCount - (series.columnIndex || 0) : // #1251
  12972. series.columnIndex) || 0,
  12973. pointXOffset = pointPadding + (groupPadding + colIndex *
  12974. pointOffsetWidth - (categoryWidth / 2)) *
  12975. (reversedXAxis ? -1 : 1);
  12976. // Save it for reading in linked series (Error bars particularly)
  12977. return (series.columnMetrics = {
  12978. width: pointWidth,
  12979. offset: pointXOffset
  12980. });
  12981. },
  12982. /**
  12983. * Translate each point to the plot area coordinate system and find shape positions
  12984. */
  12985. translate: function () {
  12986. var series = this,
  12987. chart = series.chart,
  12988. options = series.options,
  12989. stacking = options.stacking,
  12990. borderWidth = options.borderWidth,
  12991. yAxis = series.yAxis,
  12992. threshold = options.threshold,
  12993. translatedThreshold = series.translatedThreshold = yAxis.getThreshold(threshold),
  12994. minPointLength = pick(options.minPointLength, 5),
  12995. metrics = series.getColumnMetrics(),
  12996. pointWidth = metrics.width,
  12997. barW = mathCeil(mathMax(pointWidth, 1 + 2 * borderWidth)), // rounded and postprocessed for border width
  12998. pointXOffset = metrics.offset;
  12999. Series.prototype.translate.apply(series);
  13000. // record the new values
  13001. each(series.points, function (point) {
  13002. var plotY = mathMin(mathMax(-999, point.plotY), yAxis.len + 999), // Don't draw too far outside plot area (#1303)
  13003. yBottom = pick(point.yBottom, translatedThreshold),
  13004. barX = point.plotX + pointXOffset,
  13005. barY = mathCeil(mathMin(plotY, yBottom)),
  13006. barH = mathCeil(mathMax(plotY, yBottom) - barY),
  13007. stack = yAxis.stacks[(point.y < 0 ? '-' : '') + series.stackKey],
  13008. shapeArgs;
  13009. // Record the offset'ed position and width of the bar to be able to align the stacking total correctly
  13010. if (stacking && series.visible && stack && stack[point.x]) {
  13011. stack[point.x].setOffset(pointXOffset, barW);
  13012. }
  13013. // handle options.minPointLength
  13014. if (mathAbs(barH) < minPointLength) {
  13015. if (minPointLength) {
  13016. barH = minPointLength;
  13017. barY =
  13018. mathAbs(barY - translatedThreshold) > minPointLength ? // stacked
  13019. yBottom - minPointLength : // keep position
  13020. translatedThreshold - (yAxis.translate(point.y, 0, 1, 0, 1) <= translatedThreshold ? minPointLength : 0); // use exact yAxis.translation (#1485)
  13021. }
  13022. }
  13023. point.barX = barX;
  13024. point.pointWidth = pointWidth;
  13025. // create shape type and shape args that are reused in drawPoints and drawTracker
  13026. point.shapeType = 'rect';
  13027. point.shapeArgs = shapeArgs = chart.renderer.Element.prototype.crisp.call(0, borderWidth, barX, barY, barW, barH);
  13028. if (borderWidth % 2) { // correct for shorting in crisp method, visible in stacked columns with 1px border
  13029. shapeArgs.y -= 1;
  13030. shapeArgs.height += 1;
  13031. }
  13032. });
  13033. },
  13034. getSymbol: noop,
  13035. /**
  13036. * Use a solid rectangle like the area series types
  13037. */
  13038. drawLegendSymbol: AreaSeries.prototype.drawLegendSymbol,
  13039. /**
  13040. * Columns have no graph
  13041. */
  13042. drawGraph: noop,
  13043. /**
  13044. * Draw the columns. For bars, the series.group is rotated, so the same coordinates
  13045. * apply for columns and bars. This method is inherited by scatter series.
  13046. *
  13047. */
  13048. drawPoints: function () {
  13049. var series = this,
  13050. options = series.options,
  13051. renderer = series.chart.renderer,
  13052. shapeArgs;
  13053. // draw the columns
  13054. each(series.points, function (point) {
  13055. var plotY = point.plotY,
  13056. graphic = point.graphic;
  13057. if (plotY !== UNDEFINED && !isNaN(plotY) && point.y !== null) {
  13058. shapeArgs = point.shapeArgs;
  13059. if (graphic) { // update
  13060. stop(graphic);
  13061. graphic.animate(merge(shapeArgs));
  13062. } else {
  13063. point.graphic = graphic = renderer[point.shapeType](shapeArgs)
  13064. .attr(point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE])
  13065. .add(series.group)
  13066. .shadow(options.shadow, null, options.stacking && !options.borderRadius);
  13067. }
  13068. } else if (graphic) {
  13069. point.graphic = graphic.destroy(); // #1269
  13070. }
  13071. });
  13072. },
  13073. /**
  13074. * Add tracking event listener to the series group, so the point graphics
  13075. * themselves act as trackers
  13076. */
  13077. drawTracker: function () {
  13078. var series = this,
  13079. pointer = series.chart.pointer,
  13080. cursor = series.options.cursor,
  13081. css = cursor && { cursor: cursor },
  13082. onMouseOver = function (e) {
  13083. var target = e.target,
  13084. point;
  13085. series.onMouseOver();
  13086. while (target && !point) {
  13087. point = target.point;
  13088. target = target.parentNode;
  13089. }
  13090. if (point !== UNDEFINED) { // undefined on graph in scatterchart
  13091. point.onMouseOver(e);
  13092. }
  13093. };
  13094. // Add reference to the point
  13095. each(series.points, function (point) {
  13096. if (point.graphic) {
  13097. point.graphic.element.point = point;
  13098. }
  13099. if (point.dataLabel) {
  13100. point.dataLabel.element.point = point;
  13101. }
  13102. });
  13103. // Add the event listeners, we need to do this only once
  13104. if (!series._hasTracking) {
  13105. each(series.trackerGroups, function (key) {
  13106. if (series[key]) { // we don't always have dataLabelsGroup
  13107. series[key]
  13108. .addClass(PREFIX + 'tracker')
  13109. .on('mouseover', onMouseOver)
  13110. .on('mouseout', function (e) { pointer.onTrackerMouseOut(e); })
  13111. .css(css);
  13112. if (hasTouch) {
  13113. series[key].on('touchstart', onMouseOver);
  13114. }
  13115. }
  13116. });
  13117. } else {
  13118. series._hasTracking = true;
  13119. }
  13120. },
  13121. /**
  13122. * Override the basic data label alignment by adjusting for the position of the column
  13123. */
  13124. alignDataLabel: function (point, dataLabel, options, alignTo, isNew) {
  13125. var chart = this.chart,
  13126. inverted = chart.inverted,
  13127. dlBox = point.dlBox || point.shapeArgs, // data label box for alignment
  13128. below = point.below || (point.plotY > pick(this.translatedThreshold, chart.plotSizeY)),
  13129. inside = pick(options.inside, !!this.options.stacking); // draw it inside the box?
  13130. // Align to the column itself, or the top of it
  13131. if (dlBox) { // Area range uses this method but not alignTo
  13132. alignTo = merge(dlBox);
  13133. if (inverted) {
  13134. alignTo = {
  13135. x: chart.plotWidth - alignTo.y - alignTo.height,
  13136. y: chart.plotHeight - alignTo.x - alignTo.width,
  13137. width: alignTo.height,
  13138. height: alignTo.width
  13139. };
  13140. }
  13141. // Compute the alignment box
  13142. if (!inside) {
  13143. if (inverted) {
  13144. alignTo.x += below ? 0 : alignTo.width;
  13145. alignTo.width = 0;
  13146. } else {
  13147. alignTo.y += below ? alignTo.height : 0;
  13148. alignTo.height = 0;
  13149. }
  13150. }
  13151. }
  13152. // When alignment is undefined (typically columns and bars), display the individual
  13153. // point below or above the point depending on the threshold
  13154. options.align = pick(
  13155. options.align,
  13156. !inverted || inside ? 'center' : below ? 'right' : 'left'
  13157. );
  13158. options.verticalAlign = pick(
  13159. options.verticalAlign,
  13160. inverted || inside ? 'middle' : below ? 'top' : 'bottom'
  13161. );
  13162. // Call the parent method
  13163. Series.prototype.alignDataLabel.call(this, point, dataLabel, options, alignTo, isNew);
  13164. },
  13165. /**
  13166. * Animate the column heights one by one from zero
  13167. * @param {Boolean} init Whether to initialize the animation or run it
  13168. */
  13169. animate: function (init) {
  13170. var series = this,
  13171. yAxis = this.yAxis,
  13172. options = series.options,
  13173. inverted = this.chart.inverted,
  13174. attr = {},
  13175. translatedThreshold;
  13176. if (hasSVG) { // VML is too slow anyway
  13177. if (init) {
  13178. attr.scaleY = 0.001;
  13179. translatedThreshold = mathMin(yAxis.pos + yAxis.len, mathMax(yAxis.pos, yAxis.toPixels(options.threshold)));
  13180. if (inverted) {
  13181. attr.translateX = translatedThreshold - yAxis.len;
  13182. } else {
  13183. attr.translateY = translatedThreshold;
  13184. }
  13185. series.group.attr(attr);
  13186. } else { // run the animation
  13187. attr.scaleY = 1;
  13188. attr[inverted ? 'translateX' : 'translateY'] = yAxis.pos;
  13189. series.group.animate(attr, series.options.animation);
  13190. // delete this function to allow it only once
  13191. series.animate = null;
  13192. }
  13193. }
  13194. },
  13195. /**
  13196. * Remove this series from the chart
  13197. */
  13198. remove: function () {
  13199. var series = this,
  13200. chart = series.chart;
  13201. // column and bar series affects other series of the same type
  13202. // as they are either stacked or grouped
  13203. if (chart.hasRendered) {
  13204. each(chart.series, function (otherSeries) {
  13205. if (otherSeries.type === series.type) {
  13206. otherSeries.isDirty = true;
  13207. }
  13208. });
  13209. }
  13210. Series.prototype.remove.apply(series, arguments);
  13211. }
  13212. });
  13213. seriesTypes.column = ColumnSeries;
  13214. /**
  13215. * Set the default options for bar
  13216. */
  13217. defaultPlotOptions.bar = merge(defaultPlotOptions.column);
  13218. /**
  13219. * The Bar series class
  13220. */
  13221. var BarSeries = extendClass(ColumnSeries, {
  13222. type: 'bar',
  13223. inverted: true
  13224. });
  13225. seriesTypes.bar = BarSeries;
  13226. /**
  13227. * Set the default options for scatter
  13228. */
  13229. defaultPlotOptions.scatter = merge(defaultSeriesOptions, {
  13230. lineWidth: 0,
  13231. tooltip: {
  13232. headerFormat: '<span style="font-size: 10px; color:{series.color}">{series.name}</span><br/>',
  13233. pointFormat: 'x: <b>{point.x}</b><br/>y: <b>{point.y}</b><br/>',
  13234. followPointer: true
  13235. },
  13236. stickyTracking: false
  13237. });
  13238. /**
  13239. * The scatter series class
  13240. */
  13241. var ScatterSeries = extendClass(Series, {
  13242. type: 'scatter',
  13243. sorted: false,
  13244. requireSorting: false,
  13245. noSharedTooltip: true,
  13246. trackerGroups: ['markerGroup'],
  13247. drawTracker: ColumnSeries.prototype.drawTracker,
  13248. setTooltipPoints: noop
  13249. });
  13250. seriesTypes.scatter = ScatterSeries;
  13251. /**
  13252. * Set the default options for pie
  13253. */
  13254. defaultPlotOptions.pie = merge(defaultSeriesOptions, {
  13255. borderColor: '#FFFFFF',
  13256. borderWidth: 1,
  13257. center: [null, null],
  13258. clip: false,
  13259. colorByPoint: true, // always true for pies
  13260. dataLabels: {
  13261. // align: null,
  13262. // connectorWidth: 1,
  13263. // connectorColor: point.color,
  13264. // connectorPadding: 5,
  13265. distance: 30,
  13266. enabled: true,
  13267. formatter: function () {
  13268. return this.point.name;
  13269. }
  13270. // softConnector: true,
  13271. //y: 0
  13272. },
  13273. ignoreHiddenPoint: true,
  13274. //innerSize: 0,
  13275. legendType: 'point',
  13276. marker: null, // point options are specified in the base options
  13277. size: null,
  13278. showInLegend: false,
  13279. slicedOffset: 10,
  13280. states: {
  13281. hover: {
  13282. brightness: 0.1,
  13283. shadow: false
  13284. }
  13285. },
  13286. stickyTracking: false,
  13287. tooltip: {
  13288. followPointer: true
  13289. }
  13290. });
  13291. /**
  13292. * Extended point object for pies
  13293. */
  13294. var PiePoint = extendClass(Point, {
  13295. /**
  13296. * Initiate the pie slice
  13297. */
  13298. init: function () {
  13299. Point.prototype.init.apply(this, arguments);
  13300. var point = this,
  13301. toggleSlice;
  13302. // Disallow negative values (#1530)
  13303. if (point.y < 0) {
  13304. point.y = null;
  13305. }
  13306. //visible: options.visible !== false,
  13307. extend(point, {
  13308. visible: point.visible !== false,
  13309. name: pick(point.name, 'Slice')
  13310. });
  13311. // add event listener for select
  13312. toggleSlice = function () {
  13313. point.slice();
  13314. };
  13315. addEvent(point, 'select', toggleSlice);
  13316. addEvent(point, 'unselect', toggleSlice);
  13317. return point;
  13318. },
  13319. /**
  13320. * Toggle the visibility of the pie slice
  13321. * @param {Boolean} vis Whether to show the slice or not. If undefined, the
  13322. * visibility is toggled
  13323. */
  13324. setVisible: function (vis) {
  13325. var point = this,
  13326. series = point.series,
  13327. chart = series.chart,
  13328. method;
  13329. // if called without an argument, toggle visibility
  13330. point.visible = point.options.visible = vis = vis === UNDEFINED ? !point.visible : vis;
  13331. series.options.data[inArray(point, series.data)] = point.options; // update userOptions.data
  13332. method = vis ? 'show' : 'hide';
  13333. // Show and hide associated elements
  13334. each(['graphic', 'dataLabel', 'connector', 'shadowGroup'], function (key) {
  13335. if (point[key]) {
  13336. point[key][method]();
  13337. }
  13338. });
  13339. if (point.legendItem) {
  13340. chart.legend.colorizeItem(point, vis);
  13341. }
  13342. // Handle ignore hidden slices
  13343. if (!series.isDirty && series.options.ignoreHiddenPoint) {
  13344. series.isDirty = true;
  13345. chart.redraw();
  13346. }
  13347. },
  13348. /**
  13349. * Set or toggle whether the slice is cut out from the pie
  13350. * @param {Boolean} sliced When undefined, the slice state is toggled
  13351. * @param {Boolean} redraw Whether to redraw the chart. True by default.
  13352. */
  13353. slice: function (sliced, redraw, animation) {
  13354. var point = this,
  13355. series = point.series,
  13356. chart = series.chart,
  13357. translation;
  13358. setAnimation(animation, chart);
  13359. // redraw is true by default
  13360. redraw = pick(redraw, true);
  13361. // if called without an argument, toggle
  13362. point.sliced = point.options.sliced = sliced = defined(sliced) ? sliced : !point.sliced;
  13363. series.options.data[inArray(point, series.data)] = point.options; // update userOptions.data
  13364. translation = sliced ? point.slicedTranslation : {
  13365. translateX: 0,
  13366. translateY: 0
  13367. };
  13368. point.graphic.animate(translation);
  13369. if (point.shadowGroup) {
  13370. point.shadowGroup.animate(translation);
  13371. }
  13372. }
  13373. });
  13374. /**
  13375. * The Pie series class
  13376. */
  13377. var PieSeries = {
  13378. type: 'pie',
  13379. isCartesian: false,
  13380. pointClass: PiePoint,
  13381. requireSorting: false,
  13382. noSharedTooltip: true,
  13383. trackerGroups: ['group', 'dataLabelsGroup'],
  13384. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  13385. stroke: 'borderColor',
  13386. 'stroke-width': 'borderWidth',
  13387. fill: 'color'
  13388. },
  13389. /**
  13390. * Pies have one color each point
  13391. */
  13392. getColor: noop,
  13393. /**
  13394. * Animate the pies in
  13395. */
  13396. animate: function (init) {
  13397. var series = this,
  13398. points = series.points,
  13399. startAngleRad = series.startAngleRad;
  13400. if (!init) {
  13401. each(points, function (point) {
  13402. var graphic = point.graphic,
  13403. args = point.shapeArgs;
  13404. if (graphic) {
  13405. // start values
  13406. graphic.attr({
  13407. r: series.center[3] / 2, // animate from inner radius (#779)
  13408. start: startAngleRad,
  13409. end: startAngleRad
  13410. });
  13411. // animate
  13412. graphic.animate({
  13413. r: args.r,
  13414. start: args.start,
  13415. end: args.end
  13416. }, series.options.animation);
  13417. }
  13418. });
  13419. // delete this function to allow it only once
  13420. series.animate = null;
  13421. }
  13422. },
  13423. /**
  13424. * Extend the basic setData method by running processData and generatePoints immediately,
  13425. * in order to access the points from the legend.
  13426. */
  13427. setData: function (data, redraw) {
  13428. Series.prototype.setData.call(this, data, false);
  13429. this.processData();
  13430. this.generatePoints();
  13431. if (pick(redraw, true)) {
  13432. this.chart.redraw();
  13433. }
  13434. },
  13435. /**
  13436. * Get the center of the pie based on the size and center options relative to the
  13437. * plot area. Borrowed by the polar and gauge series types.
  13438. */
  13439. getCenter: function () {
  13440. var options = this.options,
  13441. chart = this.chart,
  13442. slicingRoom = 2 * (options.slicedOffset || 0),
  13443. handleSlicingRoom,
  13444. plotWidth = chart.plotWidth - 2 * slicingRoom,
  13445. plotHeight = chart.plotHeight - 2 * slicingRoom,
  13446. centerOption = options.center,
  13447. positions = [pick(centerOption[0], '50%'), pick(centerOption[1], '50%'), options.size || '100%', options.innerSize || 0],
  13448. smallestSize = mathMin(plotWidth, plotHeight),
  13449. isPercent;
  13450. return map(positions, function (length, i) {
  13451. isPercent = /%$/.test(length);
  13452. handleSlicingRoom = i < 2 || (i === 2 && isPercent);
  13453. return (isPercent ?
  13454. // i == 0: centerX, relative to width
  13455. // i == 1: centerY, relative to height
  13456. // i == 2: size, relative to smallestSize
  13457. // i == 4: innerSize, relative to smallestSize
  13458. [plotWidth, plotHeight, smallestSize, smallestSize][i] *
  13459. pInt(length) / 100 :
  13460. length) + (handleSlicingRoom ? slicingRoom : 0);
  13461. });
  13462. },
  13463. /**
  13464. * Do translation for pie slices
  13465. */
  13466. translate: function (positions) {
  13467. this.generatePoints();
  13468. var total = 0,
  13469. series = this,
  13470. cumulative = 0,
  13471. precision = 1000, // issue #172
  13472. options = series.options,
  13473. slicedOffset = options.slicedOffset,
  13474. connectorOffset = slicedOffset + options.borderWidth,
  13475. start,
  13476. end,
  13477. angle,
  13478. startAngleRad = series.startAngleRad = mathPI / 180 * ((options.startAngle || 0) % 360 - 90),
  13479. points = series.points,
  13480. circ = 2 * mathPI,
  13481. fraction,
  13482. radiusX, // the x component of the radius vector for a given point
  13483. radiusY,
  13484. labelDistance = options.dataLabels.distance,
  13485. ignoreHiddenPoint = options.ignoreHiddenPoint,
  13486. i,
  13487. len = points.length,
  13488. point;
  13489. // Get positions - either an integer or a percentage string must be given.
  13490. // If positions are passed as a parameter, we're in a recursive loop for adjusting
  13491. // space for data labels.
  13492. if (!positions) {
  13493. series.center = positions = series.getCenter();
  13494. }
  13495. // utility for getting the x value from a given y, used for anticollision logic in data labels
  13496. series.getX = function (y, left) {
  13497. angle = math.asin((y - positions[1]) / (positions[2] / 2 + labelDistance));
  13498. return positions[0] +
  13499. (left ? -1 : 1) *
  13500. (mathCos(angle) * (positions[2] / 2 + labelDistance));
  13501. };
  13502. // get the total sum
  13503. for (i = 0; i < len; i++) {
  13504. point = points[i];
  13505. total += (ignoreHiddenPoint && !point.visible) ? 0 : point.y;
  13506. }
  13507. // Calculate the geometry for each point
  13508. for (i = 0; i < len; i++) {
  13509. point = points[i];
  13510. // set start and end angle
  13511. fraction = total ? point.y / total : 0;
  13512. start = mathRound((startAngleRad + (cumulative * circ)) * precision) / precision;
  13513. if (!ignoreHiddenPoint || point.visible) {
  13514. cumulative += fraction;
  13515. }
  13516. end = mathRound((startAngleRad + (cumulative * circ)) * precision) / precision;
  13517. // set the shape
  13518. point.shapeType = 'arc';
  13519. point.shapeArgs = {
  13520. x: positions[0],
  13521. y: positions[1],
  13522. r: positions[2] / 2,
  13523. innerR: positions[3] / 2,
  13524. start: start,
  13525. end: end
  13526. };
  13527. // center for the sliced out slice
  13528. angle = (end + start) / 2;
  13529. if (angle > 0.75 * circ) {
  13530. angle -= 2 * mathPI;
  13531. }
  13532. point.slicedTranslation = {
  13533. translateX: mathRound(mathCos(angle) * slicedOffset),
  13534. translateY: mathRound(mathSin(angle) * slicedOffset)
  13535. };
  13536. // set the anchor point for tooltips
  13537. radiusX = mathCos(angle) * positions[2] / 2;
  13538. radiusY = mathSin(angle) * positions[2] / 2;
  13539. point.tooltipPos = [
  13540. positions[0] + radiusX * 0.7,
  13541. positions[1] + radiusY * 0.7
  13542. ];
  13543. point.half = angle < circ / 4 ? 0 : 1;
  13544. point.angle = angle;
  13545. // set the anchor point for data labels
  13546. connectorOffset = mathMin(connectorOffset, labelDistance / 2); // #1678
  13547. point.labelPos = [
  13548. positions[0] + radiusX + mathCos(angle) * labelDistance, // first break of connector
  13549. positions[1] + radiusY + mathSin(angle) * labelDistance, // a/a
  13550. positions[0] + radiusX + mathCos(angle) * connectorOffset, // second break, right outside pie
  13551. positions[1] + radiusY + mathSin(angle) * connectorOffset, // a/a
  13552. positions[0] + radiusX, // landing point for connector
  13553. positions[1] + radiusY, // a/a
  13554. labelDistance < 0 ? // alignment
  13555. 'center' :
  13556. point.half ? 'right' : 'left', // alignment
  13557. angle // center angle
  13558. ];
  13559. // API properties
  13560. point.percentage = fraction * 100;
  13561. point.total = total;
  13562. }
  13563. this.setTooltipPoints();
  13564. },
  13565. drawGraph: null,
  13566. /**
  13567. * Draw the data points
  13568. */
  13569. drawPoints: function () {
  13570. var series = this,
  13571. chart = series.chart,
  13572. renderer = chart.renderer,
  13573. groupTranslation,
  13574. //center,
  13575. graphic,
  13576. //group,
  13577. shadow = series.options.shadow,
  13578. shadowGroup,
  13579. shapeArgs;
  13580. if (shadow && !series.shadowGroup) {
  13581. series.shadowGroup = renderer.g('shadow')
  13582. .add(series.group);
  13583. }
  13584. // draw the slices
  13585. each(series.points, function (point) {
  13586. graphic = point.graphic;
  13587. shapeArgs = point.shapeArgs;
  13588. shadowGroup = point.shadowGroup;
  13589. // put the shadow behind all points
  13590. if (shadow && !shadowGroup) {
  13591. shadowGroup = point.shadowGroup = renderer.g('shadow')
  13592. .add(series.shadowGroup);
  13593. }
  13594. // if the point is sliced, use special translation, else use plot area traslation
  13595. groupTranslation = point.sliced ? point.slicedTranslation : {
  13596. translateX: 0,
  13597. translateY: 0
  13598. };
  13599. //group.translate(groupTranslation[0], groupTranslation[1]);
  13600. if (shadowGroup) {
  13601. shadowGroup.attr(groupTranslation);
  13602. }
  13603. // draw the slice
  13604. if (graphic) {
  13605. graphic.animate(extend(shapeArgs, groupTranslation));
  13606. } else {
  13607. point.graphic = graphic = renderer.arc(shapeArgs)
  13608. .setRadialReference(series.center)
  13609. .attr(
  13610. point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE]
  13611. )
  13612. .attr({ 'stroke-linejoin': 'round' })
  13613. .attr(groupTranslation)
  13614. .add(series.group)
  13615. .shadow(shadow, shadowGroup);
  13616. }
  13617. // detect point specific visibility
  13618. if (point.visible === false) {
  13619. point.setVisible(false);
  13620. }
  13621. });
  13622. },
  13623. /**
  13624. * Override the base drawDataLabels method by pie specific functionality
  13625. */
  13626. drawDataLabels: function () {
  13627. var series = this,
  13628. data = series.data,
  13629. point,
  13630. chart = series.chart,
  13631. options = series.options.dataLabels,
  13632. connectorPadding = pick(options.connectorPadding, 10),
  13633. connectorWidth = pick(options.connectorWidth, 1),
  13634. plotWidth = chart.plotWidth,
  13635. plotHeight = chart.plotHeight,
  13636. connector,
  13637. connectorPath,
  13638. softConnector = pick(options.softConnector, true),
  13639. distanceOption = options.distance,
  13640. seriesCenter = series.center,
  13641. radius = seriesCenter[2] / 2,
  13642. centerY = seriesCenter[1],
  13643. outside = distanceOption > 0,
  13644. dataLabel,
  13645. dataLabelWidth,
  13646. labelPos,
  13647. labelHeight,
  13648. halves = [// divide the points into right and left halves for anti collision
  13649. [], // right
  13650. [] // left
  13651. ],
  13652. x,
  13653. y,
  13654. visibility,
  13655. rankArr,
  13656. i,
  13657. j,
  13658. overflow = [0, 0, 0, 0], // top, right, bottom, left
  13659. sort = function (a, b) {
  13660. return b.y - a.y;
  13661. },
  13662. sortByAngle = function (points, sign) {
  13663. points.sort(function (a, b) {
  13664. return a.angle !== undefined && (b.angle - a.angle) * sign;
  13665. });
  13666. };
  13667. // get out if not enabled
  13668. if (!options.enabled && !series._hasPointLabels) {
  13669. return;
  13670. }
  13671. // run parent method
  13672. Series.prototype.drawDataLabels.apply(series);
  13673. // arrange points for detection collision
  13674. each(data, function (point) {
  13675. if (point.dataLabel) { // it may have been cancelled in the base method (#407)
  13676. halves[point.half].push(point);
  13677. }
  13678. });
  13679. // assume equal label heights
  13680. i = 0;
  13681. while (!labelHeight && data[i]) { // #1569
  13682. labelHeight = data[i] && data[i].dataLabel && (data[i].dataLabel.getBBox().height || 21); // 21 is for #968
  13683. i++;
  13684. }
  13685. /* Loop over the points in each half, starting from the top and bottom
  13686. * of the pie to detect overlapping labels.
  13687. */
  13688. i = 2;
  13689. while (i--) {
  13690. var slots = [],
  13691. slotsLength,
  13692. usedSlots = [],
  13693. points = halves[i],
  13694. pos,
  13695. length = points.length,
  13696. slotIndex;
  13697. // Sort by angle
  13698. sortByAngle(points, i - 0.5);
  13699. // Only do anti-collision when we are outside the pie and have connectors (#856)
  13700. if (distanceOption > 0) {
  13701. // build the slots
  13702. for (pos = centerY - radius - distanceOption; pos <= centerY + radius + distanceOption; pos += labelHeight) {
  13703. slots.push(pos);
  13704. // visualize the slot
  13705. /*
  13706. var slotX = series.getX(pos, i) + chart.plotLeft - (i ? 100 : 0),
  13707. slotY = pos + chart.plotTop;
  13708. if (!isNaN(slotX)) {
  13709. chart.renderer.rect(slotX, slotY - 7, 100, labelHeight, 1)
  13710. .attr({
  13711. 'stroke-width': 1,
  13712. stroke: 'silver'
  13713. })
  13714. .add();
  13715. chart.renderer.text('Slot '+ (slots.length - 1), slotX, slotY + 4)
  13716. .attr({
  13717. fill: 'silver'
  13718. }).add();
  13719. }
  13720. */
  13721. }
  13722. slotsLength = slots.length;
  13723. // if there are more values than available slots, remove lowest values
  13724. if (length > slotsLength) {
  13725. // create an array for sorting and ranking the points within each quarter
  13726. rankArr = [].concat(points);
  13727. rankArr.sort(sort);
  13728. j = length;
  13729. while (j--) {
  13730. rankArr[j].rank = j;
  13731. }
  13732. j = length;
  13733. while (j--) {
  13734. if (points[j].rank >= slotsLength) {
  13735. points.splice(j, 1);
  13736. }
  13737. }
  13738. length = points.length;
  13739. }
  13740. // The label goes to the nearest open slot, but not closer to the edge than
  13741. // the label's index.
  13742. for (j = 0; j < length; j++) {
  13743. point = points[j];
  13744. labelPos = point.labelPos;
  13745. var closest = 9999,
  13746. distance,
  13747. slotI;
  13748. // find the closest slot index
  13749. for (slotI = 0; slotI < slotsLength; slotI++) {
  13750. distance = mathAbs(slots[slotI] - labelPos[1]);
  13751. if (distance < closest) {
  13752. closest = distance;
  13753. slotIndex = slotI;
  13754. }
  13755. }
  13756. // if that slot index is closer to the edges of the slots, move it
  13757. // to the closest appropriate slot
  13758. if (slotIndex < j && slots[j] !== null) { // cluster at the top
  13759. slotIndex = j;
  13760. } else if (slotsLength < length - j + slotIndex && slots[j] !== null) { // cluster at the bottom
  13761. slotIndex = slotsLength - length + j;
  13762. while (slots[slotIndex] === null) { // make sure it is not taken
  13763. slotIndex++;
  13764. }
  13765. } else {
  13766. // Slot is taken, find next free slot below. In the next run, the next slice will find the
  13767. // slot above these, because it is the closest one
  13768. while (slots[slotIndex] === null) { // make sure it is not taken
  13769. slotIndex++;
  13770. }
  13771. }
  13772. usedSlots.push({ i: slotIndex, y: slots[slotIndex] });
  13773. slots[slotIndex] = null; // mark as taken
  13774. }
  13775. // sort them in order to fill in from the top
  13776. usedSlots.sort(sort);
  13777. }
  13778. // now the used slots are sorted, fill them up sequentially
  13779. for (j = 0; j < length; j++) {
  13780. var slot, naturalY;
  13781. point = points[j];
  13782. labelPos = point.labelPos;
  13783. dataLabel = point.dataLabel;
  13784. visibility = point.visible === false ? HIDDEN : VISIBLE;
  13785. naturalY = labelPos[1];
  13786. if (distanceOption > 0) {
  13787. slot = usedSlots.pop();
  13788. slotIndex = slot.i;
  13789. // if the slot next to currrent slot is free, the y value is allowed
  13790. // to fall back to the natural position
  13791. y = slot.y;
  13792. if ((naturalY > y && slots[slotIndex + 1] !== null) ||
  13793. (naturalY < y && slots[slotIndex - 1] !== null)) {
  13794. y = naturalY;
  13795. }
  13796. } else {
  13797. y = naturalY;
  13798. }
  13799. // get the x - use the natural x position for first and last slot, to prevent the top
  13800. // and botton slice connectors from touching each other on either side
  13801. x = options.justify ?
  13802. seriesCenter[0] + (i ? -1 : 1) * (radius + distanceOption) :
  13803. series.getX(slotIndex === 0 || slotIndex === slots.length - 1 ? naturalY : y, i);
  13804. // Record the placement and visibility
  13805. dataLabel._attr = {
  13806. visibility: visibility,
  13807. align: labelPos[6]
  13808. };
  13809. dataLabel._pos = {
  13810. x: x + options.x +
  13811. ({ left: connectorPadding, right: -connectorPadding }[labelPos[6]] || 0),
  13812. y: y + options.y - 10 // 10 is for the baseline (label vs text)
  13813. };
  13814. dataLabel.connX = x;
  13815. dataLabel.connY = y;
  13816. // Detect overflowing data labels
  13817. if (this.options.size === null) {
  13818. dataLabelWidth = dataLabel.width;
  13819. // Overflow left
  13820. if (x - dataLabelWidth < connectorPadding) {
  13821. overflow[3] = mathMax(mathRound(dataLabelWidth - x + connectorPadding), overflow[3]);
  13822. // Overflow right
  13823. } else if (x + dataLabelWidth > plotWidth - connectorPadding) {
  13824. overflow[1] = mathMax(mathRound(x + dataLabelWidth - plotWidth + connectorPadding), overflow[1]);
  13825. }
  13826. // Overflow top
  13827. if (y - labelHeight / 2 < 0) {
  13828. overflow[0] = mathMax(mathRound(-y + labelHeight / 2), overflow[0]);
  13829. // Overflow left
  13830. } else if (y + labelHeight / 2 > plotHeight) {
  13831. overflow[2] = mathMax(mathRound(y + labelHeight / 2 - plotHeight), overflow[2]);
  13832. }
  13833. }
  13834. } // for each point
  13835. } // for each half
  13836. // Do not apply the final placement and draw the connectors until we have verified
  13837. // that labels are not spilling over.
  13838. if (arrayMax(overflow) === 0 || this.verifyDataLabelOverflow(overflow)) {
  13839. // Place the labels in the final position
  13840. this.placeDataLabels();
  13841. // Draw the connectors
  13842. if (outside && connectorWidth) {
  13843. each(this.points, function (point) {
  13844. connector = point.connector;
  13845. labelPos = point.labelPos;
  13846. dataLabel = point.dataLabel;
  13847. if (dataLabel && dataLabel._pos) {
  13848. visibility = dataLabel._attr.visibility;
  13849. x = dataLabel.connX;
  13850. y = dataLabel.connY;
  13851. connectorPath = softConnector ? [
  13852. M,
  13853. x + (labelPos[6] === 'left' ? 5 : -5), y, // end of the string at the label
  13854. 'C',
  13855. x, y, // first break, next to the label
  13856. 2 * labelPos[2] - labelPos[4], 2 * labelPos[3] - labelPos[5],
  13857. labelPos[2], labelPos[3], // second break
  13858. L,
  13859. labelPos[4], labelPos[5] // base
  13860. ] : [
  13861. M,
  13862. x + (labelPos[6] === 'left' ? 5 : -5), y, // end of the string at the label
  13863. L,
  13864. labelPos[2], labelPos[3], // second break
  13865. L,
  13866. labelPos[4], labelPos[5] // base
  13867. ];
  13868. if (connector) {
  13869. connector.animate({ d: connectorPath });
  13870. connector.attr('visibility', visibility);
  13871. } else {
  13872. point.connector = connector = series.chart.renderer.path(connectorPath).attr({
  13873. 'stroke-width': connectorWidth,
  13874. stroke: options.connectorColor || point.color || '#606060',
  13875. visibility: visibility
  13876. })
  13877. .add(series.group);
  13878. }
  13879. } else if (connector) {
  13880. point.connector = connector.destroy();
  13881. }
  13882. });
  13883. }
  13884. }
  13885. },
  13886. /**
  13887. * Verify whether the data labels are allowed to draw, or we should run more translation and data
  13888. * label positioning to keep them inside the plot area. Returns true when data labels are ready
  13889. * to draw.
  13890. */
  13891. verifyDataLabelOverflow: function (overflow) {
  13892. var center = this.center,
  13893. options = this.options,
  13894. centerOption = options.center,
  13895. minSize = options.minSize || 80,
  13896. newSize = minSize,
  13897. ret;
  13898. // Handle horizontal size and center
  13899. if (centerOption[0] !== null) { // Fixed center
  13900. newSize = mathMax(center[2] - mathMax(overflow[1], overflow[3]), minSize);
  13901. } else { // Auto center
  13902. newSize = mathMax(
  13903. center[2] - overflow[1] - overflow[3], // horizontal overflow
  13904. minSize
  13905. );
  13906. center[0] += (overflow[3] - overflow[1]) / 2; // horizontal center
  13907. }
  13908. // Handle vertical size and center
  13909. if (centerOption[1] !== null) { // Fixed center
  13910. newSize = mathMax(mathMin(newSize, center[2] - mathMax(overflow[0], overflow[2])), minSize);
  13911. } else { // Auto center
  13912. newSize = mathMax(
  13913. mathMin(
  13914. newSize,
  13915. center[2] - overflow[0] - overflow[2] // vertical overflow
  13916. ),
  13917. minSize
  13918. );
  13919. center[1] += (overflow[0] - overflow[2]) / 2; // vertical center
  13920. }
  13921. // If the size must be decreased, we need to run translate and drawDataLabels again
  13922. if (newSize < center[2]) {
  13923. center[2] = newSize;
  13924. this.translate(center);
  13925. each(this.points, function (point) {
  13926. if (point.dataLabel) {
  13927. point.dataLabel._pos = null; // reset
  13928. }
  13929. });
  13930. this.drawDataLabels();
  13931. // Else, return true to indicate that the pie and its labels is within the plot area
  13932. } else {
  13933. ret = true;
  13934. }
  13935. return ret;
  13936. },
  13937. /**
  13938. * Perform the final placement of the data labels after we have verified that they
  13939. * fall within the plot area.
  13940. */
  13941. placeDataLabels: function () {
  13942. each(this.points, function (point) {
  13943. var dataLabel = point.dataLabel,
  13944. _pos;
  13945. if (dataLabel) {
  13946. _pos = dataLabel._pos;
  13947. if (_pos) {
  13948. dataLabel.attr(dataLabel._attr);
  13949. dataLabel[dataLabel.moved ? 'animate' : 'attr'](_pos);
  13950. dataLabel.moved = true;
  13951. } else if (dataLabel) {
  13952. dataLabel.attr({ y: -999 });
  13953. }
  13954. }
  13955. });
  13956. },
  13957. alignDataLabel: noop,
  13958. /**
  13959. * Draw point specific tracker objects. Inherit directly from column series.
  13960. */
  13961. drawTracker: ColumnSeries.prototype.drawTracker,
  13962. /**
  13963. * Use a simple symbol from column prototype
  13964. */
  13965. drawLegendSymbol: AreaSeries.prototype.drawLegendSymbol,
  13966. /**
  13967. * Pies don't have point marker symbols
  13968. */
  13969. getSymbol: noop
  13970. };
  13971. PieSeries = extendClass(Series, PieSeries);
  13972. seriesTypes.pie = PieSeries;
  13973. // global variables
  13974. extend(Highcharts, {
  13975. // Constructors
  13976. Axis: Axis,
  13977. Chart: Chart,
  13978. Color: Color,
  13979. Legend: Legend,
  13980. Pointer: Pointer,
  13981. Point: Point,
  13982. Tick: Tick,
  13983. Tooltip: Tooltip,
  13984. Renderer: Renderer,
  13985. Series: Series,
  13986. SVGElement: SVGElement,
  13987. SVGRenderer: SVGRenderer,
  13988. // Various
  13989. arrayMin: arrayMin,
  13990. arrayMax: arrayMax,
  13991. charts: charts,
  13992. dateFormat: dateFormat,
  13993. format: format,
  13994. pathAnim: pathAnim,
  13995. getOptions: getOptions,
  13996. hasBidiBug: hasBidiBug,
  13997. isTouchDevice: isTouchDevice,
  13998. numberFormat: numberFormat,
  13999. seriesTypes: seriesTypes,
  14000. setOptions: setOptions,
  14001. addEvent: addEvent,
  14002. removeEvent: removeEvent,
  14003. createElement: createElement,
  14004. discardElement: discardElement,
  14005. css: css,
  14006. each: each,
  14007. extend: extend,
  14008. map: map,
  14009. merge: merge,
  14010. pick: pick,
  14011. splat: splat,
  14012. extendClass: extendClass,
  14013. pInt: pInt,
  14014. wrap: wrap,
  14015. svg: hasSVG,
  14016. canvas: useCanVG,
  14017. vml: !hasSVG && !useCanVG,
  14018. product: PRODUCT,
  14019. version: VERSION
  14020. });
  14021. }());