Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

uPlot.iife.js 112KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835
  1. /**
  2. * Copyright (c) 2021, Leon Sorokin
  3. * All rights reserved. (MIT Licensed)
  4. *
  5. * uPlot.js (μPlot)
  6. * A small, fast chart for time series, lines, areas, ohlc & bars
  7. * https://github.com/leeoniya/uPlot (v1.6.13)
  8. */
  9. var uPlot = (function () {
  10. 'use strict';
  11. var FEAT_TIME = true;
  12. // binary search for index of closest value
  13. function closestIdx(num, arr, lo, hi) {
  14. var mid;
  15. lo = lo || 0;
  16. hi = hi || arr.length - 1;
  17. var bitwise = hi <= 2147483647;
  18. while (hi - lo > 1) {
  19. mid = bitwise ? (lo + hi) >> 1 : floor((lo + hi) / 2);
  20. if (arr[mid] < num)
  21. { lo = mid; }
  22. else
  23. { hi = mid; }
  24. }
  25. if (num - arr[lo] <= arr[hi] - num)
  26. { return lo; }
  27. return hi;
  28. }
  29. function nonNullIdx(data, _i0, _i1, dir) {
  30. for (var i = dir == 1 ? _i0 : _i1; i >= _i0 && i <= _i1; i += dir) {
  31. if (data[i] != null)
  32. { return i; }
  33. }
  34. return -1;
  35. }
  36. function getMinMax(data, _i0, _i1, sorted) {
  37. // console.log("getMinMax()");
  38. var _min = inf;
  39. var _max = -inf;
  40. if (sorted == 1) {
  41. _min = data[_i0];
  42. _max = data[_i1];
  43. }
  44. else if (sorted == -1) {
  45. _min = data[_i1];
  46. _max = data[_i0];
  47. }
  48. else {
  49. for (var i = _i0; i <= _i1; i++) {
  50. if (data[i] != null) {
  51. _min = min(_min, data[i]);
  52. _max = max(_max, data[i]);
  53. }
  54. }
  55. }
  56. return [_min, _max];
  57. }
  58. function getMinMaxLog(data, _i0, _i1) {
  59. // console.log("getMinMax()");
  60. var _min = inf;
  61. var _max = -inf;
  62. for (var i = _i0; i <= _i1; i++) {
  63. if (data[i] > 0) {
  64. _min = min(_min, data[i]);
  65. _max = max(_max, data[i]);
  66. }
  67. }
  68. return [
  69. _min == inf ? 1 : _min,
  70. _max == -inf ? 10 : _max ];
  71. }
  72. var _fixedTuple = [0, 0];
  73. function fixIncr(minIncr, maxIncr, minExp, maxExp) {
  74. _fixedTuple[0] = minExp < 0 ? roundDec(minIncr, -minExp) : minIncr;
  75. _fixedTuple[1] = maxExp < 0 ? roundDec(maxIncr, -maxExp) : maxIncr;
  76. return _fixedTuple;
  77. }
  78. function rangeLog(min, max, base, fullMags) {
  79. var logFn = base == 10 ? log10 : log2;
  80. if (min == max) {
  81. min /= base;
  82. max *= base;
  83. }
  84. var minExp, maxExp, minMaxIncrs;
  85. if (fullMags) {
  86. minExp = floor(logFn(min));
  87. maxExp = ceil(logFn(max));
  88. minMaxIncrs = fixIncr(pow(base, minExp), pow(base, maxExp), minExp, maxExp);
  89. min = minMaxIncrs[0];
  90. max = minMaxIncrs[1];
  91. }
  92. else {
  93. minExp = floor(logFn(abs(min)));
  94. maxExp = floor(logFn(abs(max)));
  95. minMaxIncrs = fixIncr(pow(base, minExp), pow(base, maxExp), minExp, maxExp);
  96. min = incrRoundDn(min, minMaxIncrs[0]);
  97. max = incrRoundUp(max, minMaxIncrs[1]);
  98. }
  99. return [min, max];
  100. }
  101. function rangeAsinh(min, max, base, fullMags) {
  102. var minMax = rangeLog(min, max, base, fullMags);
  103. if (min == 0)
  104. { minMax[0] = 0; }
  105. if (max == 0)
  106. { minMax[1] = 0; }
  107. return minMax;
  108. }
  109. var rangePad = 0.1;
  110. var autoRangePart = {
  111. mode: 3,
  112. pad: rangePad,
  113. };
  114. var _eqRangePart = {
  115. pad: 0,
  116. soft: null,
  117. mode: 0,
  118. };
  119. var _eqRange = {
  120. min: _eqRangePart,
  121. max: _eqRangePart,
  122. };
  123. // this ensures that non-temporal/numeric y-axes get multiple-snapped padding added above/below
  124. // TODO: also account for incrs when snapping to ensure top of axis gets a tick & value
  125. function rangeNum(_min, _max, mult, extra) {
  126. if (isObj(mult))
  127. { return _rangeNum(_min, _max, mult); }
  128. _eqRangePart.pad = mult;
  129. _eqRangePart.soft = extra ? 0 : null;
  130. _eqRangePart.mode = extra ? 3 : 0;
  131. return _rangeNum(_min, _max, _eqRange);
  132. }
  133. // nullish coalesce
  134. function ifNull(lh, rh) {
  135. return lh == null ? rh : lh;
  136. }
  137. function _rangeNum(_min, _max, cfg) {
  138. var cmin = cfg.min;
  139. var cmax = cfg.max;
  140. var padMin = ifNull(cmin.pad, 0);
  141. var padMax = ifNull(cmax.pad, 0);
  142. var hardMin = ifNull(cmin.hard, -inf);
  143. var hardMax = ifNull(cmax.hard, inf);
  144. var softMin = ifNull(cmin.soft, inf);
  145. var softMax = ifNull(cmax.soft, -inf);
  146. var softMinMode = ifNull(cmin.mode, 0);
  147. var softMaxMode = ifNull(cmax.mode, 0);
  148. var delta = _max - _min;
  149. // this handles situations like 89.7, 89.69999999999999
  150. // by assuming 0.001x deltas are precision errors
  151. // if (delta > 0 && delta < abs(_max) / 1e3)
  152. // delta = 0;
  153. // treat data as flat if delta is less than 1 billionth
  154. if (delta < 1e-9) {
  155. delta = 0;
  156. // if soft mode is 2 and all vals are flat at 0, avoid the 0.1 * 1e3 fallback
  157. // this prevents 0,0,0 from ranging to -100,100 when softMin/softMax are -1,1
  158. if (_min == 0 || _max == 0) {
  159. delta = 1e-9;
  160. if (softMinMode == 2 && softMin != inf)
  161. { padMin = 0; }
  162. if (softMaxMode == 2 && softMax != -inf)
  163. { padMax = 0; }
  164. }
  165. }
  166. var nonZeroDelta = delta || abs(_max) || 1e3;
  167. var mag = log10(nonZeroDelta);
  168. var base = pow(10, floor(mag));
  169. var _padMin = nonZeroDelta * (delta == 0 ? (_min == 0 ? .1 : 1) : padMin);
  170. var _newMin = roundDec(incrRoundDn(_min - _padMin, base/10), 9);
  171. var _softMin = _min >= softMin && (softMinMode == 1 || softMinMode == 3 && _newMin <= softMin || softMinMode == 2 && _newMin >= softMin) ? softMin : inf;
  172. var minLim = max(hardMin, _newMin < _softMin && _min >= _softMin ? _softMin : min(_softMin, _newMin));
  173. var _padMax = nonZeroDelta * (delta == 0 ? (_max == 0 ? .1 : 1) : padMax);
  174. var _newMax = roundDec(incrRoundUp(_max + _padMax, base/10), 9);
  175. var _softMax = _max <= softMax && (softMaxMode == 1 || softMaxMode == 3 && _newMax >= softMax || softMaxMode == 2 && _newMax <= softMax) ? softMax : -inf;
  176. var maxLim = min(hardMax, _newMax > _softMax && _max <= _softMax ? _softMax : max(_softMax, _newMax));
  177. if (minLim == maxLim && minLim == 0)
  178. { maxLim = 100; }
  179. return [minLim, maxLim];
  180. }
  181. // alternative: https://stackoverflow.com/a/2254896
  182. var fmtNum = new Intl.NumberFormat(navigator.language).format;
  183. var M = Math;
  184. var PI = M.PI;
  185. var abs = M.abs;
  186. var floor = M.floor;
  187. var round = M.round;
  188. var ceil = M.ceil;
  189. var min = M.min;
  190. var max = M.max;
  191. var pow = M.pow;
  192. var log10 = M.log10;
  193. var log2 = M.log2;
  194. var sinh = (v, linthresh) => {
  195. if ( linthresh === void 0 ) linthresh = 1;
  196. return M.sinh(v / linthresh);
  197. };
  198. var asinh = (v, linthresh) => {
  199. if ( linthresh === void 0 ) linthresh = 1;
  200. return M.asinh(v / linthresh);
  201. };
  202. var inf = Infinity;
  203. function incrRound(num, incr) {
  204. return round(num/incr)*incr;
  205. }
  206. function clamp(num, _min, _max) {
  207. return min(max(num, _min), _max);
  208. }
  209. function fnOrSelf(v) {
  210. return typeof v == "function" ? v : () => v;
  211. }
  212. var retArg0 = _0 => _0;
  213. var retArg1 = (_0, _1) => _1;
  214. var retNull = _ => null;
  215. var retTrue = _ => true;
  216. var retEq = (a, b) => a == b;
  217. function incrRoundUp(num, incr) {
  218. return ceil(num/incr)*incr;
  219. }
  220. function incrRoundDn(num, incr) {
  221. return floor(num/incr)*incr;
  222. }
  223. function roundDec(val, dec) {
  224. return round(val * (dec = Math.pow( 10, dec ))) / dec;
  225. }
  226. var fixedDec = new Map();
  227. function guessDec(num) {
  228. return ((""+num).split(".")[1] || "").length;
  229. }
  230. function genIncrs(base, minExp, maxExp, mults) {
  231. var incrs = [];
  232. var multDec = mults.map(guessDec);
  233. for (var exp = minExp; exp < maxExp; exp++) {
  234. var expa = abs(exp);
  235. var mag = roundDec(pow(base, exp), expa);
  236. for (var i = 0; i < mults.length; i++) {
  237. var _incr = mults[i] * mag;
  238. var dec = (_incr >= 0 && exp >= 0 ? 0 : expa) + (exp >= multDec[i] ? 0 : multDec[i]);
  239. var incr = roundDec(_incr, dec);
  240. incrs.push(incr);
  241. fixedDec.set(incr, dec);
  242. }
  243. }
  244. return incrs;
  245. }
  246. //export const assign = Object.assign;
  247. var EMPTY_OBJ = {};
  248. var nullNullTuple = [null, null];
  249. var isArr = Array.isArray;
  250. function isStr(v) {
  251. return typeof v == 'string';
  252. }
  253. function isObj(v) {
  254. var is = false;
  255. if (v != null) {
  256. var c = v.constructor;
  257. is = c == null || c == Object;
  258. }
  259. return is;
  260. }
  261. function fastIsObj(v) {
  262. return v != null && typeof v == 'object';
  263. }
  264. function copy(o, _isObj) {
  265. _isObj = _isObj || isObj;
  266. var out;
  267. if (isArr(o))
  268. { out = o.map(v => copy(v, _isObj)); }
  269. else if (_isObj(o)) {
  270. out = {};
  271. for (var k in o)
  272. { out[k] = copy(o[k], _isObj); }
  273. }
  274. else
  275. { out = o; }
  276. return out;
  277. }
  278. function assign(targ) {
  279. var args = arguments;
  280. for (var i = 1; i < args.length; i++) {
  281. var src = args[i];
  282. for (var key in src) {
  283. if (isObj(targ[key]))
  284. { assign(targ[key], copy(src[key])); }
  285. else
  286. { targ[key] = copy(src[key]); }
  287. }
  288. }
  289. return targ;
  290. }
  291. // nullModes
  292. var NULL_REMOVE = 0; // nulls are converted to undefined (e.g. for spanGaps: true)
  293. var NULL_RETAIN = 1; // nulls are retained, with alignment artifacts set to undefined (default)
  294. var NULL_EXPAND = 2; // nulls are expanded to include any adjacent alignment artifacts
  295. // sets undefined values to nulls when adjacent to existing nulls (minesweeper)
  296. function nullExpand(yVals, nullIdxs, alignedLen) {
  297. for (var i = 0, xi = (void 0), lastNullIdx = -1; i < nullIdxs.length; i++) {
  298. var nullIdx = nullIdxs[i];
  299. if (nullIdx > lastNullIdx) {
  300. xi = nullIdx - 1;
  301. while (xi >= 0 && yVals[xi] == null)
  302. { yVals[xi--] = null; }
  303. xi = nullIdx + 1;
  304. while (xi < alignedLen && yVals[xi] == null)
  305. { yVals[lastNullIdx = xi++] = null; }
  306. }
  307. }
  308. }
  309. // nullModes is a tables-matched array indicating how to treat nulls in each series
  310. // output is sorted ASC on the joined field (table[0]) and duplicate join values are collapsed
  311. function join(tables, nullModes) {
  312. var xVals = new Set();
  313. for (var ti = 0; ti < tables.length; ti++) {
  314. var t = tables[ti];
  315. var xs = t[0];
  316. var len = xs.length;
  317. for (var i = 0; i < len; i++)
  318. { xVals.add(xs[i]); }
  319. }
  320. var data = [Array.from(xVals).sort((a, b) => a - b)];
  321. var alignedLen = data[0].length;
  322. var xIdxs = new Map();
  323. for (var i$1 = 0; i$1 < alignedLen; i$1++)
  324. { xIdxs.set(data[0][i$1], i$1); }
  325. for (var ti$1 = 0; ti$1 < tables.length; ti$1++) {
  326. var t$1 = tables[ti$1];
  327. var xs$1 = t$1[0];
  328. for (var si = 1; si < t$1.length; si++) {
  329. var ys = t$1[si];
  330. var yVals = Array(alignedLen).fill(undefined);
  331. var nullMode = nullModes ? nullModes[ti$1][si] : NULL_RETAIN;
  332. var nullIdxs = [];
  333. for (var i$2 = 0; i$2 < ys.length; i$2++) {
  334. var yVal = ys[i$2];
  335. var alignedIdx = xIdxs.get(xs$1[i$2]);
  336. if (yVal === null) {
  337. if (nullMode != NULL_REMOVE) {
  338. yVals[alignedIdx] = yVal;
  339. if (nullMode == NULL_EXPAND)
  340. { nullIdxs.push(alignedIdx); }
  341. }
  342. }
  343. else
  344. { yVals[alignedIdx] = yVal; }
  345. }
  346. nullExpand(yVals, nullIdxs, alignedLen);
  347. data.push(yVals);
  348. }
  349. }
  350. return data;
  351. }
  352. var microTask = typeof queueMicrotask == "undefined" ? fn => Promise.resolve().then(fn) : queueMicrotask;
  353. var WIDTH = "width";
  354. var HEIGHT = "height";
  355. var TOP = "top";
  356. var BOTTOM = "bottom";
  357. var LEFT = "left";
  358. var RIGHT = "right";
  359. var hexBlack = "#000";
  360. var transparent = hexBlack + "0";
  361. var mousemove = "mousemove";
  362. var mousedown = "mousedown";
  363. var mouseup = "mouseup";
  364. var mouseenter = "mouseenter";
  365. var mouseleave = "mouseleave";
  366. var dblclick = "dblclick";
  367. var resize = "resize";
  368. var scroll = "scroll";
  369. var change = "change";
  370. var ddpxchange = "dppxchange";
  371. var pre = "u-";
  372. var UPLOT = "uplot";
  373. var ORI_HZ = pre + "hz";
  374. var ORI_VT = pre + "vt";
  375. var TITLE = pre + "title";
  376. var WRAP = pre + "wrap";
  377. var UNDER = pre + "under";
  378. var OVER = pre + "over";
  379. var OFF = pre + "off";
  380. var SELECT = pre + "select";
  381. var CURSOR_X = pre + "cursor-x";
  382. var CURSOR_Y = pre + "cursor-y";
  383. var CURSOR_PT = pre + "cursor-pt";
  384. var LEGEND = pre + "legend";
  385. var LEGEND_LIVE = pre + "live";
  386. var LEGEND_INLINE = pre + "inline";
  387. var LEGEND_THEAD = pre + "thead";
  388. var LEGEND_SERIES = pre + "series";
  389. var LEGEND_MARKER = pre + "marker";
  390. var LEGEND_LABEL = pre + "label";
  391. var LEGEND_VALUE = pre + "value";
  392. var doc = document;
  393. var win = window;
  394. var pxRatio;
  395. var query;
  396. function setPxRatio() {
  397. pxRatio = devicePixelRatio;
  398. query && off(change, query, setPxRatio);
  399. query = matchMedia(("screen and (min-resolution: " + (pxRatio - 0.001) + "dppx) and (max-resolution: " + (pxRatio + 0.001) + "dppx)"));
  400. on(change, query, setPxRatio);
  401. win.dispatchEvent(new CustomEvent(ddpxchange));
  402. }
  403. function addClass(el, c) {
  404. if (c != null) {
  405. var cl = el.classList;
  406. !cl.contains(c) && cl.add(c);
  407. }
  408. }
  409. function remClass(el, c) {
  410. var cl = el.classList;
  411. cl.contains(c) && cl.remove(c);
  412. }
  413. function setStylePx(el, name, value) {
  414. el.style[name] = value + "px";
  415. }
  416. function placeTag(tag, cls, targ, refEl) {
  417. var el = doc.createElement(tag);
  418. if (cls != null)
  419. { addClass(el, cls); }
  420. if (targ != null)
  421. { targ.insertBefore(el, refEl); }
  422. return el;
  423. }
  424. function placeDiv(cls, targ) {
  425. return placeTag("div", cls, targ);
  426. }
  427. function trans(el, xPos, yPos, xMax, yMax) {
  428. el.style.transform = "translate(" + xPos + "px," + yPos + "px)";
  429. if (xPos < 0 || yPos < 0 || xPos > xMax || yPos > yMax)
  430. { addClass(el, OFF); }
  431. else
  432. { remClass(el, OFF); }
  433. }
  434. var evOpts = {passive: true};
  435. var evOpts2 = assign({capture: true}, evOpts);
  436. function on(ev, el, cb, capt) {
  437. el.addEventListener(ev, cb, capt ? evOpts2 : evOpts);
  438. }
  439. function off(ev, el, cb, capt) {
  440. el.removeEventListener(ev, cb, capt ? evOpts2 : evOpts);
  441. }
  442. setPxRatio();
  443. var months = [
  444. "January",
  445. "February",
  446. "March",
  447. "April",
  448. "May",
  449. "June",
  450. "July",
  451. "August",
  452. "September",
  453. "October",
  454. "November",
  455. "December" ];
  456. var days = [
  457. "Sunday",
  458. "Monday",
  459. "Tuesday",
  460. "Wednesday",
  461. "Thursday",
  462. "Friday",
  463. "Saturday" ];
  464. function slice3(str) {
  465. return str.slice(0, 3);
  466. }
  467. var days3 = days.map(slice3);
  468. var months3 = months.map(slice3);
  469. var engNames = {
  470. MMMM: months,
  471. MMM: months3,
  472. WWWW: days,
  473. WWW: days3,
  474. };
  475. function zeroPad2(int) {
  476. return (int < 10 ? '0' : '') + int;
  477. }
  478. function zeroPad3(int) {
  479. return (int < 10 ? '00' : int < 100 ? '0' : '') + int;
  480. }
  481. /*
  482. function suffix(int) {
  483. let mod10 = int % 10;
  484. return int + (
  485. mod10 == 1 && int != 11 ? "st" :
  486. mod10 == 2 && int != 12 ? "nd" :
  487. mod10 == 3 && int != 13 ? "rd" : "th"
  488. );
  489. }
  490. */
  491. var subs = {
  492. // 2019
  493. YYYY: d => d.getFullYear(),
  494. // 19
  495. YY: d => (d.getFullYear()+'').slice(2),
  496. // July
  497. MMMM: (d, names) => names.MMMM[d.getMonth()],
  498. // Jul
  499. MMM: (d, names) => names.MMM[d.getMonth()],
  500. // 07
  501. MM: d => zeroPad2(d.getMonth()+1),
  502. // 7
  503. M: d => d.getMonth()+1,
  504. // 09
  505. DD: d => zeroPad2(d.getDate()),
  506. // 9
  507. D: d => d.getDate(),
  508. // Monday
  509. WWWW: (d, names) => names.WWWW[d.getDay()],
  510. // Mon
  511. WWW: (d, names) => names.WWW[d.getDay()],
  512. // 03
  513. HH: d => zeroPad2(d.getHours()),
  514. // 3
  515. H: d => d.getHours(),
  516. // 9 (12hr, unpadded)
  517. h: d => {var h = d.getHours(); return h == 0 ? 12 : h > 12 ? h - 12 : h;},
  518. // AM
  519. AA: d => d.getHours() >= 12 ? 'PM' : 'AM',
  520. // am
  521. aa: d => d.getHours() >= 12 ? 'pm' : 'am',
  522. // a
  523. a: d => d.getHours() >= 12 ? 'p' : 'a',
  524. // 09
  525. mm: d => zeroPad2(d.getMinutes()),
  526. // 9
  527. m: d => d.getMinutes(),
  528. // 09
  529. ss: d => zeroPad2(d.getSeconds()),
  530. // 9
  531. s: d => d.getSeconds(),
  532. // 374
  533. fff: d => zeroPad3(d.getMilliseconds()),
  534. };
  535. function fmtDate(tpl, names) {
  536. names = names || engNames;
  537. var parts = [];
  538. var R = /\{([a-z]+)\}|[^{]+/gi, m;
  539. while (m = R.exec(tpl))
  540. { parts.push(m[0][0] == '{' ? subs[m[1]] : m[0]); }
  541. return d => {
  542. var out = '';
  543. for (var i = 0; i < parts.length; i++)
  544. { out += typeof parts[i] == "string" ? parts[i] : parts[i](d, names); }
  545. return out;
  546. }
  547. }
  548. var localTz = new Intl.DateTimeFormat().resolvedOptions().timeZone;
  549. // https://stackoverflow.com/questions/15141762/how-to-initialize-a-javascript-date-to-a-particular-time-zone/53652131#53652131
  550. function tzDate(date, tz) {
  551. var date2;
  552. // perf optimization
  553. if (tz == 'UTC' || tz == 'Etc/UTC')
  554. { date2 = new Date(+date + date.getTimezoneOffset() * 6e4); }
  555. else if (tz == localTz)
  556. { date2 = date; }
  557. else {
  558. date2 = new Date(date.toLocaleString('en-US', {timeZone: tz}));
  559. date2.setMilliseconds(date.getMilliseconds());
  560. }
  561. return date2;
  562. }
  563. //export const series = [];
  564. // default formatters:
  565. var onlyWhole = v => v % 1 == 0;
  566. var allMults = [1,2,2.5,5];
  567. // ...0.01, 0.02, 0.025, 0.05, 0.1, 0.2, 0.25, 0.5
  568. var decIncrs = genIncrs(10, -16, 0, allMults);
  569. // 1, 2, 2.5, 5, 10, 20, 25, 50...
  570. var oneIncrs = genIncrs(10, 0, 16, allMults);
  571. // 1, 2, 5, 10, 20, 25, 50...
  572. var wholeIncrs = oneIncrs.filter(onlyWhole);
  573. var numIncrs = decIncrs.concat(oneIncrs);
  574. var NL = "\n";
  575. var yyyy = "{YYYY}";
  576. var NLyyyy = NL + yyyy;
  577. var md = "{M}/{D}";
  578. var NLmd = NL + md;
  579. var NLmdyy = NLmd + "/{YY}";
  580. var aa = "{aa}";
  581. var hmm = "{h}:{mm}";
  582. var hmmaa = hmm + aa;
  583. var NLhmmaa = NL + hmmaa;
  584. var ss = ":{ss}";
  585. var _ = null;
  586. function genTimeStuffs(ms) {
  587. var s = ms * 1e3,
  588. m = s * 60,
  589. h = m * 60,
  590. d = h * 24,
  591. mo = d * 30,
  592. y = d * 365;
  593. // min of 1e-3 prevents setting a temporal x ticks too small since Date objects cannot advance ticks smaller than 1ms
  594. var subSecIncrs = ms == 1 ? genIncrs(10, 0, 3, allMults).filter(onlyWhole) : genIncrs(10, -3, 0, allMults);
  595. var timeIncrs = subSecIncrs.concat([
  596. // minute divisors (# of secs)
  597. s,
  598. s * 5,
  599. s * 10,
  600. s * 15,
  601. s * 30,
  602. // hour divisors (# of mins)
  603. m,
  604. m * 5,
  605. m * 10,
  606. m * 15,
  607. m * 30,
  608. // day divisors (# of hrs)
  609. h,
  610. h * 2,
  611. h * 3,
  612. h * 4,
  613. h * 6,
  614. h * 8,
  615. h * 12,
  616. // month divisors TODO: need more?
  617. d,
  618. d * 2,
  619. d * 3,
  620. d * 4,
  621. d * 5,
  622. d * 6,
  623. d * 7,
  624. d * 8,
  625. d * 9,
  626. d * 10,
  627. d * 15,
  628. // year divisors (# months, approx)
  629. mo,
  630. mo * 2,
  631. mo * 3,
  632. mo * 4,
  633. mo * 6,
  634. // century divisors
  635. y,
  636. y * 2,
  637. y * 5,
  638. y * 10,
  639. y * 25,
  640. y * 50,
  641. y * 100 ]);
  642. // [0]: minimum num secs in the tick incr
  643. // [1]: default tick format
  644. // [2-7]: rollover tick formats
  645. // [8]: mode: 0: replace [1] -> [2-7], 1: concat [1] + [2-7]
  646. var _timeAxisStamps = [
  647. // tick incr default year month day hour min sec mode
  648. [y, yyyy, _, _, _, _, _, _, 1],
  649. [d * 28, "{MMM}", NLyyyy, _, _, _, _, _, 1],
  650. [d, md, NLyyyy, _, _, _, _, _, 1],
  651. [h, "{h}" + aa, NLmdyy, _, NLmd, _, _, _, 1],
  652. [m, hmmaa, NLmdyy, _, NLmd, _, _, _, 1],
  653. [s, ss, NLmdyy + " " + hmmaa, _, NLmd + " " + hmmaa, _, NLhmmaa, _, 1],
  654. [ms, ss + ".{fff}", NLmdyy + " " + hmmaa, _, NLmd + " " + hmmaa, _, NLhmmaa, _, 1] ];
  655. // the ensures that axis ticks, values & grid are aligned to logical temporal breakpoints and not an arbitrary timestamp
  656. // https://www.timeanddate.com/time/dst/
  657. // https://www.timeanddate.com/time/dst/2019.html
  658. // https://www.epochconverter.com/timezones
  659. function timeAxisSplits(tzDate) {
  660. return (self, axisIdx, scaleMin, scaleMax, foundIncr, foundSpace) => {
  661. var splits = [];
  662. var isYr = foundIncr >= y;
  663. var isMo = foundIncr >= mo && foundIncr < y;
  664. // get the timezone-adjusted date
  665. var minDate = tzDate(scaleMin);
  666. var minDateTs = roundDec(minDate * ms, 3);
  667. // get ts of 12am (this lands us at or before the original scaleMin)
  668. var minMin = mkDate(minDate.getFullYear(), isYr ? 0 : minDate.getMonth(), isMo || isYr ? 1 : minDate.getDate());
  669. var minMinTs = roundDec(minMin * ms, 3);
  670. if (isMo || isYr) {
  671. var moIncr = isMo ? foundIncr / mo : 0;
  672. var yrIncr = isYr ? foundIncr / y : 0;
  673. // let tzOffset = scaleMin - minDateTs; // needed?
  674. var split = minDateTs == minMinTs ? minDateTs : roundDec(mkDate(minMin.getFullYear() + yrIncr, minMin.getMonth() + moIncr, 1) * ms, 3);
  675. var splitDate = new Date(round(split / ms));
  676. var baseYear = splitDate.getFullYear();
  677. var baseMonth = splitDate.getMonth();
  678. for (var i = 0; split <= scaleMax; i++) {
  679. var next = mkDate(baseYear + yrIncr * i, baseMonth + moIncr * i, 1);
  680. var offs = next - tzDate(roundDec(next * ms, 3));
  681. split = roundDec((+next + offs) * ms, 3);
  682. if (split <= scaleMax)
  683. { splits.push(split); }
  684. }
  685. }
  686. else {
  687. var incr0 = foundIncr >= d ? d : foundIncr;
  688. var tzOffset = floor(scaleMin) - floor(minDateTs);
  689. var split$1 = minMinTs + tzOffset + incrRoundUp(minDateTs - minMinTs, incr0);
  690. splits.push(split$1);
  691. var date0 = tzDate(split$1);
  692. var prevHour = date0.getHours() + (date0.getMinutes() / m) + (date0.getSeconds() / h);
  693. var incrHours = foundIncr / h;
  694. var minSpace = self.axes[axisIdx]._space;
  695. var pctSpace = foundSpace / minSpace;
  696. while (1) {
  697. split$1 = roundDec(split$1 + foundIncr, ms == 1 ? 0 : 3);
  698. if (split$1 > scaleMax)
  699. { break; }
  700. if (incrHours > 1) {
  701. var expectedHour = floor(roundDec(prevHour + incrHours, 6)) % 24;
  702. var splitDate$1 = tzDate(split$1);
  703. var actualHour = splitDate$1.getHours();
  704. var dstShift = actualHour - expectedHour;
  705. if (dstShift > 1)
  706. { dstShift = -1; }
  707. split$1 -= dstShift * h;
  708. prevHour = (prevHour + incrHours) % 24;
  709. // add a tick only if it's further than 70% of the min allowed label spacing
  710. var prevSplit = splits[splits.length - 1];
  711. var pctIncr = roundDec((split$1 - prevSplit) / foundIncr, 3);
  712. if (pctIncr * pctSpace >= .7)
  713. { splits.push(split$1); }
  714. }
  715. else
  716. { splits.push(split$1); }
  717. }
  718. }
  719. return splits;
  720. }
  721. }
  722. return [
  723. timeIncrs,
  724. _timeAxisStamps,
  725. timeAxisSplits ];
  726. }
  727. var ref = genTimeStuffs(1);
  728. var timeIncrsMs = ref[0];
  729. var _timeAxisStampsMs = ref[1];
  730. var timeAxisSplitsMs = ref[2];
  731. var ref$1 = genTimeStuffs(1e-3);
  732. var timeIncrsS = ref$1[0];
  733. var _timeAxisStampsS = ref$1[1];
  734. var timeAxisSplitsS = ref$1[2];
  735. // base 2
  736. genIncrs(2, -53, 53, [1]);
  737. /*
  738. console.log({
  739. decIncrs,
  740. oneIncrs,
  741. wholeIncrs,
  742. numIncrs,
  743. timeIncrs,
  744. fixedDec,
  745. });
  746. */
  747. function timeAxisStamps(stampCfg, fmtDate) {
  748. return stampCfg.map(s => s.map((v, i) =>
  749. i == 0 || i == 8 || v == null ? v : fmtDate(i == 1 || s[8] == 0 ? v : s[1] + v)
  750. ));
  751. }
  752. // TODO: will need to accept spaces[] and pull incr into the loop when grid will be non-uniform, eg for log scales.
  753. // currently we ignore this for months since they're *nearly* uniform and the added complexity is not worth it
  754. function timeAxisVals(tzDate, stamps) {
  755. return (self, splits, axisIdx, foundSpace, foundIncr) => {
  756. var s = stamps.find(s => foundIncr >= s[0]) || stamps[stamps.length - 1];
  757. // these track boundaries when a full label is needed again
  758. var prevYear;
  759. var prevMnth;
  760. var prevDate;
  761. var prevHour;
  762. var prevMins;
  763. var prevSecs;
  764. return splits.map(split => {
  765. var date = tzDate(split);
  766. var newYear = date.getFullYear();
  767. var newMnth = date.getMonth();
  768. var newDate = date.getDate();
  769. var newHour = date.getHours();
  770. var newMins = date.getMinutes();
  771. var newSecs = date.getSeconds();
  772. var stamp = (
  773. newYear != prevYear && s[2] ||
  774. newMnth != prevMnth && s[3] ||
  775. newDate != prevDate && s[4] ||
  776. newHour != prevHour && s[5] ||
  777. newMins != prevMins && s[6] ||
  778. newSecs != prevSecs && s[7] ||
  779. s[1]
  780. );
  781. prevYear = newYear;
  782. prevMnth = newMnth;
  783. prevDate = newDate;
  784. prevHour = newHour;
  785. prevMins = newMins;
  786. prevSecs = newSecs;
  787. return stamp(date);
  788. });
  789. }
  790. }
  791. // for when axis.values is defined as a static fmtDate template string
  792. function timeAxisVal(tzDate, dateTpl) {
  793. var stamp = fmtDate(dateTpl);
  794. return (self, splits, axisIdx, foundSpace, foundIncr) => splits.map(split => stamp(tzDate(split)));
  795. }
  796. function mkDate(y, m, d) {
  797. return new Date(y, m, d);
  798. }
  799. function timeSeriesStamp(stampCfg, fmtDate) {
  800. return fmtDate(stampCfg);
  801. }
  802. var _timeSeriesStamp = '{YYYY}-{MM}-{DD} {h}:{mm}{aa}';
  803. function timeSeriesVal(tzDate, stamp) {
  804. return (self, val) => stamp(tzDate(val));
  805. }
  806. function legendStroke(self, seriesIdx) {
  807. var s = self.series[seriesIdx];
  808. return s.width ? s.stroke(self, seriesIdx) : s.points.width ? s.points.stroke(self, seriesIdx) : null;
  809. }
  810. function legendFill(self, seriesIdx) {
  811. return self.series[seriesIdx].fill(self, seriesIdx);
  812. }
  813. var legendOpts = {
  814. show: true,
  815. live: true,
  816. isolate: false,
  817. markers: {
  818. show: true,
  819. width: 2,
  820. stroke: legendStroke,
  821. fill: legendFill,
  822. dash: "solid",
  823. },
  824. idx: null,
  825. values: [],
  826. };
  827. function cursorPointShow(self, si) {
  828. var o = self.cursor.points;
  829. var pt = placeDiv();
  830. var stroke = o.stroke(self, si);
  831. var fill = o.fill(self, si);
  832. pt.style.background = fill || stroke;
  833. var size = o.size(self, si);
  834. var width = o.width(self, si, size);
  835. if (width)
  836. { pt.style.border = width + "px solid " + stroke; }
  837. var mar = size / -2;
  838. setStylePx(pt, WIDTH, size);
  839. setStylePx(pt, HEIGHT, size);
  840. setStylePx(pt, "marginLeft", mar);
  841. setStylePx(pt, "marginTop", mar);
  842. return pt;
  843. }
  844. function cursorPointFill(self, si) {
  845. var s = self.series[si];
  846. return s.stroke(self, si);
  847. }
  848. function cursorPointStroke(self, si) {
  849. var s = self.series[si];
  850. return s.stroke(self, si);
  851. }
  852. function cursorPointSize(self, si) {
  853. var s = self.series[si];
  854. return ptDia(s.width, 1);
  855. }
  856. function dataIdx(self, seriesIdx, cursorIdx) {
  857. return cursorIdx;
  858. }
  859. var moveTuple = [0,0];
  860. function cursorMove(self, mouseLeft1, mouseTop1) {
  861. moveTuple[0] = mouseLeft1;
  862. moveTuple[1] = mouseTop1;
  863. return moveTuple;
  864. }
  865. function filtBtn0(self, targ, handle) {
  866. return e => {
  867. e.button == 0 && handle(e);
  868. };
  869. }
  870. function passThru(self, targ, handle) {
  871. return handle;
  872. }
  873. var cursorOpts = {
  874. show: true,
  875. x: true,
  876. y: true,
  877. lock: false,
  878. move: cursorMove,
  879. points: {
  880. show: cursorPointShow,
  881. size: cursorPointSize,
  882. width: 0,
  883. stroke: cursorPointStroke,
  884. fill: cursorPointFill,
  885. },
  886. bind: {
  887. mousedown: filtBtn0,
  888. mouseup: filtBtn0,
  889. click: filtBtn0,
  890. dblclick: filtBtn0,
  891. mousemove: passThru,
  892. mouseleave: passThru,
  893. mouseenter: passThru,
  894. },
  895. drag: {
  896. setScale: true,
  897. x: true,
  898. y: false,
  899. dist: 0,
  900. uni: null,
  901. _x: false,
  902. _y: false,
  903. },
  904. focus: {
  905. prox: -1,
  906. },
  907. left: -10,
  908. top: -10,
  909. idx: null,
  910. dataIdx: dataIdx,
  911. idxs: [],
  912. };
  913. var grid = {
  914. show: true,
  915. stroke: "rgba(0,0,0,0.07)",
  916. width: 2,
  917. // dash: [],
  918. filter: retArg1,
  919. };
  920. var ticks = assign({}, grid, {size: 10});
  921. var font = '12px system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"';
  922. var labelFont = "bold " + font;
  923. var lineMult = 1.5; // font-size multiplier
  924. var xAxisOpts = {
  925. show: true,
  926. scale: "x",
  927. stroke: hexBlack,
  928. space: 50,
  929. gap: 5,
  930. size: 50,
  931. labelSize: 30,
  932. labelFont: labelFont,
  933. side: 2,
  934. // class: "x-vals",
  935. // incrs: timeIncrs,
  936. // values: timeVals,
  937. // filter: retArg1,
  938. grid: grid,
  939. ticks: ticks,
  940. font: font,
  941. rotate: 0,
  942. };
  943. var numSeriesLabel = "Value";
  944. var timeSeriesLabel = "Time";
  945. var xSeriesOpts = {
  946. show: true,
  947. scale: "x",
  948. auto: false,
  949. sorted: 1,
  950. // label: "Time",
  951. // value: v => stamp(new Date(v * 1e3)),
  952. // internal caches
  953. min: inf,
  954. max: -inf,
  955. idxs: [],
  956. };
  957. function numAxisVals(self, splits, axisIdx, foundSpace, foundIncr) {
  958. return splits.map(v => v == null ? "" : fmtNum(v));
  959. }
  960. function numAxisSplits(self, axisIdx, scaleMin, scaleMax, foundIncr, foundSpace, forceMin) {
  961. var splits = [];
  962. var numDec = fixedDec.get(foundIncr) || 0;
  963. scaleMin = forceMin ? scaleMin : roundDec(incrRoundUp(scaleMin, foundIncr), numDec);
  964. for (var val = scaleMin; val <= scaleMax; val = roundDec(val + foundIncr, numDec))
  965. { splits.push(Object.is(val, -0) ? 0 : val); } // coalesces -0
  966. return splits;
  967. }
  968. // this doesnt work for sin, which needs to come off from 0 independently in pos and neg dirs
  969. function logAxisSplits(self, axisIdx, scaleMin, scaleMax, foundIncr, foundSpace, forceMin) {
  970. var splits = [];
  971. var logBase = self.scales[self.axes[axisIdx].scale].log;
  972. var logFn = logBase == 10 ? log10 : log2;
  973. var exp = floor(logFn(scaleMin));
  974. foundIncr = pow(logBase, exp);
  975. if (exp < 0)
  976. { foundIncr = roundDec(foundIncr, -exp); }
  977. var split = scaleMin;
  978. do {
  979. splits.push(split);
  980. split = roundDec(split + foundIncr, fixedDec.get(foundIncr));
  981. if (split >= foundIncr * logBase)
  982. { foundIncr = split; }
  983. } while (split <= scaleMax);
  984. return splits;
  985. }
  986. function asinhAxisSplits(self, axisIdx, scaleMin, scaleMax, foundIncr, foundSpace, forceMin) {
  987. var sc = self.scales[self.axes[axisIdx].scale];
  988. var linthresh = sc.asinh;
  989. var posSplits = scaleMax > linthresh ? logAxisSplits(self, axisIdx, max(linthresh, scaleMin), scaleMax, foundIncr) : [linthresh];
  990. var zero = scaleMax >= 0 && scaleMin <= 0 ? [0] : [];
  991. var negSplits = scaleMin < -linthresh ? logAxisSplits(self, axisIdx, max(linthresh, -scaleMax), -scaleMin, foundIncr): [linthresh];
  992. return negSplits.reverse().map(v => -v).concat(zero, posSplits);
  993. }
  994. var RE_ALL = /./;
  995. var RE_12357 = /[12357]/;
  996. var RE_125 = /[125]/;
  997. var RE_1 = /1/;
  998. function logAxisValsFilt(self, splits, axisIdx, foundSpace, foundIncr) {
  999. var axis = self.axes[axisIdx];
  1000. var scaleKey = axis.scale;
  1001. var sc = self.scales[scaleKey];
  1002. if (sc.distr == 3 && sc.log == 2)
  1003. { return splits; }
  1004. var valToPos = self.valToPos;
  1005. var minSpace = axis._space;
  1006. var _10 = valToPos(10, scaleKey);
  1007. var re = (
  1008. valToPos(9, scaleKey) - _10 >= minSpace ? RE_ALL :
  1009. valToPos(7, scaleKey) - _10 >= minSpace ? RE_12357 :
  1010. valToPos(5, scaleKey) - _10 >= minSpace ? RE_125 :
  1011. RE_1
  1012. );
  1013. return splits.map(v => ((sc.distr == 4 && v == 0) || re.test(v)) ? v : null);
  1014. }
  1015. function numSeriesVal(self, val) {
  1016. return val == null ? "" : fmtNum(val);
  1017. }
  1018. var yAxisOpts = {
  1019. show: true,
  1020. scale: "y",
  1021. stroke: hexBlack,
  1022. space: 30,
  1023. gap: 5,
  1024. size: 50,
  1025. labelSize: 30,
  1026. labelFont: labelFont,
  1027. side: 3,
  1028. // class: "y-vals",
  1029. // incrs: numIncrs,
  1030. // values: (vals, space) => vals,
  1031. // filter: retArg1,
  1032. grid: grid,
  1033. ticks: ticks,
  1034. font: font,
  1035. rotate: 0,
  1036. };
  1037. // takes stroke width
  1038. function ptDia(width, mult) {
  1039. var dia = 3 + (width || 1) * 2;
  1040. return roundDec(dia * mult, 3);
  1041. }
  1042. function seriesPoints(self, si) {
  1043. var ref = self.series[0];
  1044. var scale = ref.scale;
  1045. var idxs = ref.idxs;
  1046. var xData = self.data[0];
  1047. var p0 = self.valToPos(xData[idxs[0]], scale, true);
  1048. var p1 = self.valToPos(xData[idxs[1]], scale, true);
  1049. var dim = abs(p1 - p0);
  1050. var s = self.series[si];
  1051. // const dia = ptDia(s.width, pxRatio);
  1052. var maxPts = dim / (s.points.space * pxRatio);
  1053. return idxs[1] - idxs[0] <= maxPts;
  1054. }
  1055. function seriesFillTo(self, seriesIdx, dataMin, dataMax) {
  1056. var scale = self.scales[self.series[seriesIdx].scale];
  1057. var isUpperBandEdge = self.bands && self.bands.some(b => b.series[0] == seriesIdx);
  1058. return scale.distr == 3 || isUpperBandEdge ? scale.min : 0;
  1059. }
  1060. var ySeriesOpts = {
  1061. scale: "y",
  1062. auto: true,
  1063. sorted: 0,
  1064. show: true,
  1065. band: false,
  1066. spanGaps: false,
  1067. alpha: 1,
  1068. points: {
  1069. show: seriesPoints,
  1070. filter: null,
  1071. // stroke: "#000",
  1072. // fill: "#fff",
  1073. // width: 1,
  1074. // size: 10,
  1075. },
  1076. // label: "Value",
  1077. // value: v => v,
  1078. values: null,
  1079. // internal caches
  1080. min: inf,
  1081. max: -inf,
  1082. idxs: [],
  1083. path: null,
  1084. clip: null,
  1085. };
  1086. function clampScale(self, val, scaleMin, scaleMax, scaleKey) {
  1087. /*
  1088. if (val < 0) {
  1089. let cssHgt = self.bbox.height / pxRatio;
  1090. let absPos = self.valToPos(abs(val), scaleKey);
  1091. let fromBtm = cssHgt - absPos;
  1092. return self.posToVal(cssHgt + fromBtm, scaleKey);
  1093. }
  1094. */
  1095. return scaleMin / 10;
  1096. }
  1097. var xScaleOpts = {
  1098. time: FEAT_TIME,
  1099. auto: true,
  1100. distr: 1,
  1101. log: 10,
  1102. asinh: 1,
  1103. min: null,
  1104. max: null,
  1105. dir: 1,
  1106. ori: 0,
  1107. };
  1108. var yScaleOpts = assign({}, xScaleOpts, {
  1109. time: false,
  1110. ori: 1,
  1111. });
  1112. var syncs = {};
  1113. function _sync(key, opts) {
  1114. var s = syncs[key];
  1115. if (!s) {
  1116. s = {
  1117. key: key,
  1118. plots: [],
  1119. sub: function sub(plot) {
  1120. s.plots.push(plot);
  1121. },
  1122. unsub: function unsub(plot) {
  1123. s.plots = s.plots.filter(c => c != plot);
  1124. },
  1125. pub: function pub(type, self, x, y, w, h, i) {
  1126. for (var j = 0; j < s.plots.length; j++)
  1127. { s.plots[j] != self && s.plots[j].pub(type, self, x, y, w, h, i); }
  1128. },
  1129. };
  1130. if (key != null)
  1131. { syncs[key] = s; }
  1132. }
  1133. return s;
  1134. }
  1135. var BAND_CLIP_FILL = 1 << 0;
  1136. var BAND_CLIP_STROKE = 1 << 1;
  1137. function orient(u, seriesIdx, cb) {
  1138. var series = u.series[seriesIdx];
  1139. var scales = u.scales;
  1140. var bbox = u.bbox;
  1141. var scaleX = scales[u.series[0].scale];
  1142. var dx = u._data[0],
  1143. dy = u._data[seriesIdx],
  1144. sx = scaleX,
  1145. sy = scales[series.scale],
  1146. l = bbox.left,
  1147. t = bbox.top,
  1148. w = bbox.width,
  1149. h = bbox.height,
  1150. H = u.valToPosH,
  1151. V = u.valToPosV;
  1152. return (sx.ori == 0
  1153. ? cb(
  1154. series,
  1155. dx,
  1156. dy,
  1157. sx,
  1158. sy,
  1159. H,
  1160. V,
  1161. l,
  1162. t,
  1163. w,
  1164. h,
  1165. moveToH,
  1166. lineToH,
  1167. rectH,
  1168. arcH,
  1169. bezierCurveToH
  1170. )
  1171. : cb(
  1172. series,
  1173. dx,
  1174. dy,
  1175. sx,
  1176. sy,
  1177. V,
  1178. H,
  1179. t,
  1180. l,
  1181. h,
  1182. w,
  1183. moveToV,
  1184. lineToV,
  1185. rectV,
  1186. arcV,
  1187. bezierCurveToV
  1188. )
  1189. );
  1190. }
  1191. // creates inverted band clip path (towards from stroke path -> yMax)
  1192. function clipBandLine(self, seriesIdx, idx0, idx1, strokePath) {
  1193. return orient(self, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
  1194. var dir = scaleX.dir * (scaleX.ori == 0 ? 1 : -1);
  1195. var lineTo = scaleX.ori == 0 ? lineToH : lineToV;
  1196. var frIdx, toIdx;
  1197. if (dir == 1) {
  1198. frIdx = idx0;
  1199. toIdx = idx1;
  1200. }
  1201. else {
  1202. frIdx = idx1;
  1203. toIdx = idx0;
  1204. }
  1205. // path start
  1206. var x0 = incrRound(valToPosX(dataX[frIdx], scaleX, xDim, xOff), 0.5);
  1207. var y0 = incrRound(valToPosY(dataY[frIdx], scaleY, yDim, yOff), 0.5);
  1208. // path end x
  1209. var x1 = incrRound(valToPosX(dataX[toIdx], scaleX, xDim, xOff), 0.5);
  1210. // upper y limit
  1211. var yLimit = incrRound(valToPosY(scaleY.max, scaleY, yDim, yOff), 0.5);
  1212. var clip = new Path2D(strokePath);
  1213. lineTo(clip, x1, yLimit);
  1214. lineTo(clip, x0, yLimit);
  1215. lineTo(clip, x0, y0);
  1216. return clip;
  1217. });
  1218. }
  1219. function clipGaps(gaps, ori, plotLft, plotTop, plotWid, plotHgt) {
  1220. var clip = null;
  1221. // create clip path (invert gaps and non-gaps)
  1222. if (gaps.length > 0) {
  1223. clip = new Path2D();
  1224. var rect = ori == 0 ? rectH : rectV;
  1225. var prevGapEnd = plotLft;
  1226. for (var i = 0; i < gaps.length; i++) {
  1227. var g = gaps[i];
  1228. rect(clip, prevGapEnd, plotTop, g[0] - prevGapEnd, plotTop + plotHgt);
  1229. prevGapEnd = g[1];
  1230. }
  1231. rect(clip, prevGapEnd, plotTop, plotLft + plotWid - prevGapEnd, plotTop + plotHgt);
  1232. }
  1233. return clip;
  1234. }
  1235. function addGap(gaps, fromX, toX) {
  1236. if (toX > fromX) {
  1237. var prevGap = gaps[gaps.length - 1];
  1238. if (prevGap && prevGap[0] == fromX) // TODO: gaps must be encoded at stroke widths?
  1239. { prevGap[1] = toX; }
  1240. else
  1241. { gaps.push([fromX, toX]); }
  1242. }
  1243. }
  1244. function pxRoundGen(pxAlign) {
  1245. return pxAlign == 0 ? retArg0 : pxAlign == 1 ? round : v => incrRound(v, pxAlign);
  1246. }
  1247. // orientation-inverting canvas functions
  1248. function moveToH(p, x, y) { p.moveTo(x, y); }
  1249. function moveToV(p, y, x) { p.moveTo(x, y); }
  1250. function lineToH(p, x, y) { p.lineTo(x, y); }
  1251. function lineToV(p, y, x) { p.lineTo(x, y); }
  1252. function rectH(p, x, y, w, h) { p.rect(x, y, w, h); }
  1253. function rectV(p, y, x, h, w) { p.rect(x, y, w, h); }
  1254. function arcH(p, x, y, r, startAngle, endAngle) { p.arc(x, y, r, startAngle, endAngle); }
  1255. function arcV(p, y, x, r, startAngle, endAngle) { p.arc(x, y, r, startAngle, endAngle); }
  1256. function bezierCurveToH(p, bp1x, bp1y, bp2x, bp2y, p2x, p2y) { p.bezierCurveTo(bp1x, bp1y, bp2x, bp2y, p2x, p2y); }function bezierCurveToV(p, bp1y, bp1x, bp2y, bp2x, p2y, p2x) { p.bezierCurveTo(bp1x, bp1y, bp2x, bp2y, p2x, p2y); }
  1257. function _drawAcc(lineTo) {
  1258. return (stroke, accX, minY, maxY, inY, outY) => {
  1259. if (minY != maxY) {
  1260. if (inY != minY && outY != minY)
  1261. { lineTo(stroke, accX, minY); }
  1262. if (inY != maxY && outY != maxY)
  1263. { lineTo(stroke, accX, maxY); }
  1264. lineTo(stroke, accX, outY);
  1265. }
  1266. };
  1267. }
  1268. var drawAccH = _drawAcc(lineToH);
  1269. var drawAccV = _drawAcc(lineToV);
  1270. function linear() {
  1271. return (u, seriesIdx, idx0, idx1) => {
  1272. return orient(u, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
  1273. var pxRound = series.pxRound;
  1274. var lineTo, drawAcc;
  1275. if (scaleX.ori == 0) {
  1276. lineTo = lineToH;
  1277. drawAcc = drawAccH;
  1278. }
  1279. else {
  1280. lineTo = lineToV;
  1281. drawAcc = drawAccV;
  1282. }
  1283. var dir = scaleX.dir * (scaleX.ori == 0 ? 1 : -1);
  1284. var _paths = {stroke: new Path2D(), fill: null, clip: null, band: null, gaps: [], flags: BAND_CLIP_FILL};
  1285. var stroke = _paths.stroke;
  1286. var minY = inf,
  1287. maxY = -inf,
  1288. inY, outY, outX, drawnAtX;
  1289. var gaps = _paths.gaps;
  1290. var accX = pxRound(valToPosX(dataX[dir == 1 ? idx0 : idx1], scaleX, xDim, xOff));
  1291. var accGaps = false;
  1292. // data edges
  1293. var lftIdx = nonNullIdx(dataY, idx0, idx1, 1 * dir);
  1294. var rgtIdx = nonNullIdx(dataY, idx0, idx1, -1 * dir);
  1295. var lftX = incrRound(valToPosX(dataX[lftIdx], scaleX, xDim, xOff), 0.5);
  1296. var rgtX = incrRound(valToPosX(dataX[rgtIdx], scaleX, xDim, xOff), 0.5);
  1297. if (lftX > xOff)
  1298. { addGap(gaps, xOff, lftX); }
  1299. for (var i = dir == 1 ? idx0 : idx1; i >= idx0 && i <= idx1; i += dir) {
  1300. var x = pxRound(valToPosX(dataX[i], scaleX, xDim, xOff));
  1301. if (x == accX) {
  1302. if (dataY[i] != null) {
  1303. outY = pxRound(valToPosY(dataY[i], scaleY, yDim, yOff));
  1304. if (minY == inf) {
  1305. lineTo(stroke, x, outY);
  1306. inY = outY;
  1307. }
  1308. minY = min(outY, minY);
  1309. maxY = max(outY, maxY);
  1310. }
  1311. else if (!accGaps && dataY[i] === null)
  1312. { accGaps = true; }
  1313. }
  1314. else {
  1315. var _addGap = false;
  1316. if (minY != inf) {
  1317. drawAcc(stroke, accX, minY, maxY, inY, outY);
  1318. outX = drawnAtX = accX;
  1319. }
  1320. else if (accGaps) {
  1321. _addGap = true;
  1322. accGaps = false;
  1323. }
  1324. if (dataY[i] != null) {
  1325. outY = pxRound(valToPosY(dataY[i], scaleY, yDim, yOff));
  1326. lineTo(stroke, x, outY);
  1327. minY = maxY = inY = outY;
  1328. // prior pixel can have data but still start a gap if ends with null
  1329. if (x - accX > 1 && dataY[i - dir] === null)
  1330. { _addGap = true; }
  1331. }
  1332. else {
  1333. minY = inf;
  1334. maxY = -inf;
  1335. if (!accGaps && dataY[i] === null)
  1336. { accGaps = true; }
  1337. }
  1338. _addGap && addGap(gaps, outX, x);
  1339. accX = x;
  1340. }
  1341. }
  1342. if (minY != inf && minY != maxY && drawnAtX != accX)
  1343. { drawAcc(stroke, accX, minY, maxY, inY, outY); }
  1344. if (rgtX < xOff + xDim)
  1345. { addGap(gaps, rgtX, xOff + xDim); }
  1346. if (series.fill != null) {
  1347. var fill = _paths.fill = new Path2D(stroke);
  1348. var fillTo = pxRound(valToPosY(series.fillTo(u, seriesIdx, series.min, series.max), scaleY, yDim, yOff));
  1349. lineTo(fill, rgtX, fillTo);
  1350. lineTo(fill, lftX, fillTo);
  1351. }
  1352. if (!series.spanGaps)
  1353. { _paths.clip = clipGaps(gaps, scaleX.ori, xOff, yOff, xDim, yDim); }
  1354. if (u.bands.length > 0) {
  1355. // ADDL OPT: only create band clips for series that are band lower edges
  1356. // if (b.series[1] == i && _paths.band == null)
  1357. _paths.band = clipBandLine(u, seriesIdx, idx0, idx1, stroke);
  1358. }
  1359. return _paths;
  1360. });
  1361. };
  1362. }
  1363. function stepped(opts) {
  1364. var align = ifNull(opts.align, 1);
  1365. // whether to draw ascenders/descenders at null/gap bondaries
  1366. var ascDesc = ifNull(opts.ascDesc, false);
  1367. return (u, seriesIdx, idx0, idx1) => {
  1368. return orient(u, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
  1369. var pxRound = series.pxRound;
  1370. var lineTo = scaleX.ori == 0 ? lineToH : lineToV;
  1371. var _paths = {stroke: new Path2D(), fill: null, clip: null, band: null, gaps: [], flags: BAND_CLIP_FILL};
  1372. var stroke = _paths.stroke;
  1373. var _dir = 1 * scaleX.dir * (scaleX.ori == 0 ? 1 : -1);
  1374. idx0 = nonNullIdx(dataY, idx0, idx1, 1);
  1375. idx1 = nonNullIdx(dataY, idx0, idx1, -1);
  1376. var gaps = _paths.gaps;
  1377. var inGap = false;
  1378. var prevYPos = pxRound(valToPosY(dataY[_dir == 1 ? idx0 : idx1], scaleY, yDim, yOff));
  1379. var firstXPos = pxRound(valToPosX(dataX[_dir == 1 ? idx0 : idx1], scaleX, xDim, xOff));
  1380. var prevXPos = firstXPos;
  1381. lineTo(stroke, firstXPos, prevYPos);
  1382. for (var i = _dir == 1 ? idx0 : idx1; i >= idx0 && i <= idx1; i += _dir) {
  1383. var yVal1 = dataY[i];
  1384. var x1 = pxRound(valToPosX(dataX[i], scaleX, xDim, xOff));
  1385. if (yVal1 == null) {
  1386. if (yVal1 === null) {
  1387. addGap(gaps, prevXPos, x1);
  1388. inGap = true;
  1389. }
  1390. continue;
  1391. }
  1392. var y1 = pxRound(valToPosY(yVal1, scaleY, yDim, yOff));
  1393. if (inGap) {
  1394. addGap(gaps, prevXPos, x1);
  1395. // don't clip vertical extenders
  1396. if (prevYPos != y1) {
  1397. var halfStroke = (series.width * pxRatio) / 2;
  1398. var lastGap = gaps[gaps.length - 1];
  1399. lastGap[0] += (ascDesc || align == 1) ? halfStroke : -halfStroke;
  1400. lastGap[1] -= (ascDesc || align == -1) ? halfStroke : -halfStroke;
  1401. }
  1402. inGap = false;
  1403. }
  1404. if (align == 1)
  1405. { lineTo(stroke, x1, prevYPos); }
  1406. else
  1407. { lineTo(stroke, prevXPos, y1); }
  1408. lineTo(stroke, x1, y1);
  1409. prevYPos = y1;
  1410. prevXPos = x1;
  1411. }
  1412. if (series.fill != null) {
  1413. var fill = _paths.fill = new Path2D(stroke);
  1414. var fillTo = series.fillTo(u, seriesIdx, series.min, series.max);
  1415. var minY = pxRound(valToPosY(fillTo, scaleY, yDim, yOff));
  1416. lineTo(fill, prevXPos, minY);
  1417. lineTo(fill, firstXPos, minY);
  1418. }
  1419. if (!series.spanGaps)
  1420. { _paths.clip = clipGaps(gaps, scaleX.ori, xOff, yOff, xDim, yDim); }
  1421. if (u.bands.length > 0) {
  1422. // ADDL OPT: only create band clips for series that are band lower edges
  1423. // if (b.series[1] == i && _paths.band == null)
  1424. _paths.band = clipBandLine(u, seriesIdx, idx0, idx1, stroke);
  1425. }
  1426. return _paths;
  1427. });
  1428. };
  1429. }
  1430. function bars(opts) {
  1431. opts = opts || EMPTY_OBJ;
  1432. var size = ifNull(opts.size, [0.6, inf]);
  1433. var align = opts.align || 0;
  1434. var extraGap = (opts.gap || 0) * pxRatio;
  1435. var gapFactor = 1 - size[0];
  1436. var maxWidth = ifNull(size[1], inf) * pxRatio;
  1437. return (u, seriesIdx, idx0, idx1) => {
  1438. return orient(u, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
  1439. var pxRound = series.pxRound;
  1440. var _dir = scaleX.dir * (scaleX.ori == 0 ? 1 : -1);
  1441. var rect = scaleX.ori == 0 ? rectH : rectV;
  1442. var colWid = xDim;
  1443. if (dataX.length > 1) {
  1444. // scan full dataset for smallest adjacent delta
  1445. // will not work properly for non-linear x scales, since does not do expensive valToPosX calcs till end
  1446. for (var i = 1, minDelta = Infinity; i < dataX.length; i++) {
  1447. var delta = abs(dataX[i] - dataX[i-1]);
  1448. if (delta < minDelta) {
  1449. minDelta = delta;
  1450. colWid = abs(valToPosX(dataX[i], scaleX, xDim, xOff) - valToPosX(dataX[i-1], scaleX, xDim, xOff));
  1451. }
  1452. }
  1453. }
  1454. var gapWid = colWid * gapFactor;
  1455. var fillToY = series.fillTo(u, seriesIdx, series.min, series.max);
  1456. var y0Pos = valToPosY(fillToY, scaleY, yDim, yOff);
  1457. var strokeWidth = pxRound(series.width * pxRatio);
  1458. var barWid = pxRound(min(maxWidth, colWid - gapWid) - strokeWidth - extraGap);
  1459. var xShift = (align == 0 ? barWid / 2 : align == _dir ? 0 : barWid) - align * _dir * extraGap / 2;
  1460. var _paths = {stroke: new Path2D(), fill: null, clip: null, band: null, gaps: null, flags: BAND_CLIP_FILL | BAND_CLIP_STROKE};
  1461. var hasBands = u.bands.length > 0;
  1462. var yLimit;
  1463. if (hasBands) {
  1464. // ADDL OPT: only create band clips for series that are band lower edges
  1465. // if (b.series[1] == i && _paths.band == null)
  1466. _paths.band = new Path2D();
  1467. yLimit = incrRound(valToPosY(scaleY.max, scaleY, yDim, yOff), 0.5);
  1468. }
  1469. var stroke = _paths.stroke;
  1470. var band = _paths.band;
  1471. for (var i$1 = _dir == 1 ? idx0 : idx1; i$1 >= idx0 && i$1 <= idx1; i$1 += _dir) {
  1472. var yVal = dataY[i$1];
  1473. // interpolate upwards band clips
  1474. if (yVal == null) {
  1475. if (hasBands) {
  1476. // simple, but inefficient bi-directinal linear scans on each iteration
  1477. var prevNonNull = nonNullIdx(dataY, _dir == 1 ? idx0 : idx1, i$1, -_dir);
  1478. var nextNonNull = nonNullIdx(dataY, i$1, _dir == 1 ? idx1 : idx0, _dir);
  1479. var prevVal = dataY[prevNonNull];
  1480. var nextVal = dataY[nextNonNull];
  1481. yVal = prevVal + (i$1 - prevNonNull) / (nextNonNull - prevNonNull) * (nextVal - prevVal);
  1482. }
  1483. else
  1484. { continue; }
  1485. }
  1486. var xVal = scaleX.distr == 2 ? i$1 : dataX[i$1];
  1487. // TODO: all xPos can be pre-computed once for all series in aligned set
  1488. var xPos = valToPosX(xVal, scaleX, xDim, xOff);
  1489. var yPos = valToPosY(yVal, scaleY, yDim, yOff);
  1490. var lft = pxRound(xPos - xShift);
  1491. var btm = pxRound(max(yPos, y0Pos));
  1492. var top = pxRound(min(yPos, y0Pos));
  1493. var barHgt = btm - top;
  1494. dataY[i$1] != null && rect(stroke, lft, top, barWid, barHgt);
  1495. if (hasBands) {
  1496. btm = top;
  1497. top = yLimit;
  1498. barHgt = btm - top;
  1499. rect(band, lft, top, barWid, barHgt);
  1500. }
  1501. }
  1502. if (series.fill != null)
  1503. { _paths.fill = new Path2D(stroke); }
  1504. return _paths;
  1505. });
  1506. };
  1507. }
  1508. function splineInterp(interp, opts) {
  1509. return (u, seriesIdx, idx0, idx1) => {
  1510. return orient(u, seriesIdx, (series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
  1511. var pxRound = series.pxRound;
  1512. var moveTo, bezierCurveTo, lineTo;
  1513. if (scaleX.ori == 0) {
  1514. moveTo = moveToH;
  1515. lineTo = lineToH;
  1516. bezierCurveTo = bezierCurveToH;
  1517. }
  1518. else {
  1519. moveTo = moveToV;
  1520. lineTo = lineToV;
  1521. bezierCurveTo = bezierCurveToV;
  1522. }
  1523. var _dir = 1 * scaleX.dir * (scaleX.ori == 0 ? 1 : -1);
  1524. idx0 = nonNullIdx(dataY, idx0, idx1, 1);
  1525. idx1 = nonNullIdx(dataY, idx0, idx1, -1);
  1526. var gaps = [];
  1527. var inGap = false;
  1528. var firstXPos = pxRound(valToPosX(dataX[_dir == 1 ? idx0 : idx1], scaleX, xDim, xOff));
  1529. var prevXPos = firstXPos;
  1530. var xCoords = [];
  1531. var yCoords = [];
  1532. for (var i = _dir == 1 ? idx0 : idx1; i >= idx0 && i <= idx1; i += _dir) {
  1533. var yVal = dataY[i];
  1534. var xVal = dataX[i];
  1535. var xPos = valToPosX(xVal, scaleX, xDim, xOff);
  1536. if (yVal == null) {
  1537. if (yVal === null) {
  1538. addGap(gaps, prevXPos, xPos);
  1539. inGap = true;
  1540. }
  1541. continue;
  1542. }
  1543. else {
  1544. if (inGap) {
  1545. addGap(gaps, prevXPos, xPos);
  1546. inGap = false;
  1547. }
  1548. xCoords.push((prevXPos = xPos));
  1549. yCoords.push(valToPosY(dataY[i], scaleY, yDim, yOff));
  1550. }
  1551. }
  1552. var _paths = {stroke: interp(xCoords, yCoords, moveTo, lineTo, bezierCurveTo, pxRound), fill: null, clip: null, band: null, gaps: gaps, flags: BAND_CLIP_FILL};
  1553. var stroke = _paths.stroke;
  1554. if (series.fill != null && stroke != null) {
  1555. var fill = _paths.fill = new Path2D(stroke);
  1556. var fillTo = series.fillTo(u, seriesIdx, series.min, series.max);
  1557. var minY = pxRound(valToPosY(fillTo, scaleY, yDim, yOff));
  1558. lineTo(fill, prevXPos, minY);
  1559. lineTo(fill, firstXPos, minY);
  1560. }
  1561. if (!series.spanGaps)
  1562. { _paths.clip = clipGaps(gaps, scaleX.ori, xOff, yOff, xDim, yDim); }
  1563. if (u.bands.length > 0) {
  1564. // ADDL OPT: only create band clips for series that are band lower edges
  1565. // if (b.series[1] == i && _paths.band == null)
  1566. _paths.band = clipBandLine(u, seriesIdx, idx0, idx1, stroke);
  1567. }
  1568. return _paths;
  1569. // if FEAT_PATHS: false in rollup.config.js
  1570. // u.ctx.save();
  1571. // u.ctx.beginPath();
  1572. // u.ctx.rect(u.bbox.left, u.bbox.top, u.bbox.width, u.bbox.height);
  1573. // u.ctx.clip();
  1574. // u.ctx.strokeStyle = u.series[sidx].stroke;
  1575. // u.ctx.stroke(stroke);
  1576. // u.ctx.fillStyle = u.series[sidx].fill;
  1577. // u.ctx.fill(fill);
  1578. // u.ctx.restore();
  1579. // return null;
  1580. });
  1581. };
  1582. }
  1583. function monotoneCubic(opts) {
  1584. return splineInterp(_monotoneCubic);
  1585. }
  1586. // Monotone Cubic Spline interpolation, adapted from the Chartist.js implementation:
  1587. // https://github.com/gionkunz/chartist-js/blob/e7e78201bffe9609915e5e53cfafa29a5d6c49f9/src/scripts/interpolation.js#L240-L369
  1588. function _monotoneCubic(xs, ys, moveTo, lineTo, bezierCurveTo, pxRound) {
  1589. var n = xs.length;
  1590. if (n < 2)
  1591. { return null; }
  1592. var path = new Path2D();
  1593. moveTo(path, xs[0], ys[0]);
  1594. if (n == 2)
  1595. { lineTo(path, xs[1], ys[1]); }
  1596. else {
  1597. var ms = Array(n),
  1598. ds = Array(n - 1),
  1599. dys = Array(n - 1),
  1600. dxs = Array(n - 1);
  1601. // calc deltas and derivative
  1602. for (var i = 0; i < n - 1; i++) {
  1603. dys[i] = ys[i + 1] - ys[i];
  1604. dxs[i] = xs[i + 1] - xs[i];
  1605. ds[i] = dys[i] / dxs[i];
  1606. }
  1607. // determine desired slope (m) at each point using Fritsch-Carlson method
  1608. // http://math.stackexchange.com/questions/45218/implementation-of-monotone-cubic-interpolation
  1609. ms[0] = ds[0];
  1610. for (var i$1 = 1; i$1 < n - 1; i$1++) {
  1611. if (ds[i$1] === 0 || ds[i$1 - 1] === 0 || (ds[i$1 - 1] > 0) !== (ds[i$1] > 0))
  1612. { ms[i$1] = 0; }
  1613. else {
  1614. ms[i$1] = 3 * (dxs[i$1 - 1] + dxs[i$1]) / (
  1615. (2 * dxs[i$1] + dxs[i$1 - 1]) / ds[i$1 - 1] +
  1616. (dxs[i$1] + 2 * dxs[i$1 - 1]) / ds[i$1]
  1617. );
  1618. if (!isFinite(ms[i$1]))
  1619. { ms[i$1] = 0; }
  1620. }
  1621. }
  1622. ms[n - 1] = ds[n - 2];
  1623. for (var i$2 = 0; i$2 < n - 1; i$2++) {
  1624. bezierCurveTo(
  1625. path,
  1626. xs[i$2] + dxs[i$2] / 3,
  1627. ys[i$2] + ms[i$2] * dxs[i$2] / 3,
  1628. xs[i$2 + 1] - dxs[i$2] / 3,
  1629. ys[i$2 + 1] - ms[i$2 + 1] * dxs[i$2] / 3,
  1630. xs[i$2 + 1],
  1631. ys[i$2 + 1]
  1632. );
  1633. }
  1634. }
  1635. return path;
  1636. }
  1637. var cursorPlots = new Set();
  1638. function invalidateRects() {
  1639. cursorPlots.forEach(u => {
  1640. u.syncRect(true);
  1641. });
  1642. }
  1643. on(resize, win, invalidateRects);
  1644. on(scroll, win, invalidateRects, true);
  1645. var linearPath = linear() ;
  1646. function setDefaults(d, xo, yo, initY) {
  1647. var d2 = initY ? [d[0], d[1]].concat(d.slice(2)) : [d[0]].concat(d.slice(1));
  1648. return d2.map((o, i) => setDefault(o, i, xo, yo));
  1649. }
  1650. function setDefault(o, i, xo, yo) {
  1651. return assign({}, (i == 0 ? xo : yo), o);
  1652. }
  1653. function snapNumX(self, dataMin, dataMax) {
  1654. return dataMin == null ? nullNullTuple : [dataMin, dataMax];
  1655. }
  1656. var snapTimeX = snapNumX;
  1657. // this ensures that non-temporal/numeric y-axes get multiple-snapped padding added above/below
  1658. // TODO: also account for incrs when snapping to ensure top of axis gets a tick & value
  1659. function snapNumY(self, dataMin, dataMax) {
  1660. return dataMin == null ? nullNullTuple : rangeNum(dataMin, dataMax, rangePad, true);
  1661. }
  1662. function snapLogY(self, dataMin, dataMax, scale) {
  1663. return dataMin == null ? nullNullTuple : rangeLog(dataMin, dataMax, self.scales[scale].log, false);
  1664. }
  1665. var snapLogX = snapLogY;
  1666. function snapAsinhY(self, dataMin, dataMax, scale) {
  1667. return dataMin == null ? nullNullTuple : rangeAsinh(dataMin, dataMax, self.scales[scale].log, false);
  1668. }
  1669. var snapAsinhX = snapAsinhY;
  1670. // dim is logical (getClientBoundingRect) pixels, not canvas pixels
  1671. function findIncr(min, max, incrs, dim, minSpace) {
  1672. var pxPerUnit = dim / (max - min);
  1673. var minDec = (""+floor(min)).length;
  1674. for (var i = 0; i < incrs.length; i++) {
  1675. var space = incrs[i] * pxPerUnit;
  1676. var incrDec = incrs[i] < 10 ? fixedDec.get(incrs[i]) : 0;
  1677. if (space >= minSpace && minDec + incrDec < 17)
  1678. { return [incrs[i], space]; }
  1679. }
  1680. return [0, 0];
  1681. }
  1682. function pxRatioFont(font) {
  1683. var fontSize, fontSizeCss;
  1684. font = font.replace(/(\d+)px/, (m, p1) => (fontSize = round((fontSizeCss = +p1) * pxRatio)) + 'px');
  1685. return [font, fontSize, fontSizeCss];
  1686. }
  1687. function syncFontSize(axis) {
  1688. if (axis.show) {
  1689. [axis.font, axis.labelFont].forEach(f => {
  1690. var size = roundDec(f[2] * pxRatio, 1);
  1691. f[0] = f[0].replace(/[0-9.]+px/, size + 'px');
  1692. f[1] = size;
  1693. });
  1694. }
  1695. }
  1696. function uPlot(opts, data, then) {
  1697. var self = {};
  1698. // TODO: cache denoms & mins scale.cache = {r, min, }
  1699. function getValPct(val, scale) {
  1700. var _val = (
  1701. scale.distr == 3 ? log10(val > 0 ? val : scale.clamp(self, val, scale.min, scale.max, scale.key)) :
  1702. scale.distr == 4 ? asinh(val, scale.asinh) :
  1703. val
  1704. );
  1705. return (_val - scale._min) / (scale._max - scale._min);
  1706. }
  1707. function getHPos(val, scale, dim, off) {
  1708. var pct = getValPct(val, scale);
  1709. return off + dim * (scale.dir == -1 ? (1 - pct) : pct);
  1710. }
  1711. function getVPos(val, scale, dim, off) {
  1712. var pct = getValPct(val, scale);
  1713. return off + dim * (scale.dir == -1 ? pct : (1 - pct));
  1714. }
  1715. function getPos(val, scale, dim, off) {
  1716. return scale.ori == 0 ? getHPos(val, scale, dim, off) : getVPos(val, scale, dim, off);
  1717. }
  1718. self.valToPosH = getHPos;
  1719. self.valToPosV = getVPos;
  1720. var ready = false;
  1721. self.status = 0;
  1722. var root = self.root = placeDiv(UPLOT);
  1723. if (opts.id != null)
  1724. { root.id = opts.id; }
  1725. addClass(root, opts.class);
  1726. if (opts.title) {
  1727. var title = placeDiv(TITLE, root);
  1728. title.textContent = opts.title;
  1729. }
  1730. var can = placeTag("canvas");
  1731. var ctx = self.ctx = can.getContext("2d");
  1732. var wrap = placeDiv(WRAP, root);
  1733. var under = self.under = placeDiv(UNDER, wrap);
  1734. wrap.appendChild(can);
  1735. var over = self.over = placeDiv(OVER, wrap);
  1736. opts = copy(opts);
  1737. var pxAlign = +ifNull(opts.pxAlign, 1);
  1738. var pxRound = pxRoundGen(pxAlign);
  1739. (opts.plugins || []).forEach(p => {
  1740. if (p.opts)
  1741. { opts = p.opts(self, opts) || opts; }
  1742. });
  1743. var ms = opts.ms || 1e-3;
  1744. var series = self.series = setDefaults(opts.series || [], xSeriesOpts, ySeriesOpts, false);
  1745. var axes = self.axes = setDefaults(opts.axes || [], xAxisOpts, yAxisOpts, true);
  1746. var scales = self.scales = {};
  1747. var bands = self.bands = opts.bands || [];
  1748. bands.forEach(b => {
  1749. b.fill = fnOrSelf(b.fill || null);
  1750. });
  1751. var xScaleKey = series[0].scale;
  1752. var drawOrderMap = {
  1753. axes: drawAxesGrid,
  1754. series: drawSeries,
  1755. };
  1756. var drawOrder = (opts.drawOrder || ["axes", "series"]).map(key => drawOrderMap[key]);
  1757. function initScale(scaleKey) {
  1758. var sc = scales[scaleKey];
  1759. if (sc == null) {
  1760. var scaleOpts = (opts.scales || EMPTY_OBJ)[scaleKey] || EMPTY_OBJ;
  1761. if (scaleOpts.from != null) {
  1762. // ensure parent is initialized
  1763. initScale(scaleOpts.from);
  1764. // dependent scales inherit
  1765. scales[scaleKey] = assign({}, scales[scaleOpts.from], scaleOpts);
  1766. }
  1767. else {
  1768. sc = scales[scaleKey] = assign({}, (scaleKey == xScaleKey ? xScaleOpts : yScaleOpts), scaleOpts);
  1769. sc.key = scaleKey;
  1770. var isTime = sc.time;
  1771. var rn = sc.range;
  1772. var rangeIsArr = isArr(rn);
  1773. if (scaleKey != xScaleKey) {
  1774. // if range array has null limits, it should be auto
  1775. if (rangeIsArr && (rn[0] == null || rn[1] == null)) {
  1776. rn = {
  1777. min: rn[0] == null ? autoRangePart : {
  1778. mode: 1,
  1779. hard: rn[0],
  1780. soft: rn[0],
  1781. },
  1782. max: rn[1] == null ? autoRangePart : {
  1783. mode: 1,
  1784. hard: rn[1],
  1785. soft: rn[1],
  1786. },
  1787. };
  1788. rangeIsArr = false;
  1789. }
  1790. if (!rangeIsArr && isObj(rn)) {
  1791. var cfg = rn;
  1792. // this is similar to snapNumY
  1793. rn = (self, dataMin, dataMax) => dataMin == null ? nullNullTuple : rangeNum(dataMin, dataMax, cfg);
  1794. }
  1795. }
  1796. sc.range = fnOrSelf(rn || (isTime ? snapTimeX : scaleKey == xScaleKey ?
  1797. (sc.distr == 3 ? snapLogX : sc.distr == 4 ? snapAsinhX : snapNumX) :
  1798. (sc.distr == 3 ? snapLogY : sc.distr == 4 ? snapAsinhY : snapNumY)
  1799. ));
  1800. sc.auto = fnOrSelf(rangeIsArr ? false : sc.auto);
  1801. sc.clamp = fnOrSelf(sc.clamp || clampScale);
  1802. // caches for expensive ops like asinh() & log()
  1803. sc._min = sc._max = null;
  1804. }
  1805. }
  1806. }
  1807. initScale("x");
  1808. initScale("y");
  1809. series.forEach(s => {
  1810. initScale(s.scale);
  1811. });
  1812. axes.forEach(a => {
  1813. initScale(a.scale);
  1814. });
  1815. for (var k in opts.scales)
  1816. { initScale(k); }
  1817. var scaleX = scales[xScaleKey];
  1818. var xScaleDistr = scaleX.distr;
  1819. var valToPosX, valToPosY, moveTo, arc;
  1820. if (scaleX.ori == 0) {
  1821. addClass(root, ORI_HZ);
  1822. valToPosX = getHPos;
  1823. valToPosY = getVPos;
  1824. moveTo = moveToH;
  1825. arc = arcH;
  1826. /*
  1827. updOriDims = () => {
  1828. xDimCan = plotWid;
  1829. xOffCan = plotLft;
  1830. yDimCan = plotHgt;
  1831. yOffCan = plotTop;
  1832. xDimCss = plotWidCss;
  1833. xOffCss = plotLftCss;
  1834. yDimCss = plotHgtCss;
  1835. yOffCss = plotTopCss;
  1836. };
  1837. */
  1838. }
  1839. else {
  1840. addClass(root, ORI_VT);
  1841. valToPosX = getVPos;
  1842. valToPosY = getHPos;
  1843. moveTo = moveToV;
  1844. arc = arcV;
  1845. /*
  1846. updOriDims = () => {
  1847. xDimCan = plotHgt;
  1848. xOffCan = plotTop;
  1849. yDimCan = plotWid;
  1850. yOffCan = plotLft;
  1851. xDimCss = plotHgtCss;
  1852. xOffCss = plotTopCss;
  1853. yDimCss = plotWidCss;
  1854. yOffCss = plotLftCss;
  1855. };
  1856. */
  1857. }
  1858. var pendScales = {};
  1859. // explicitly-set initial scales
  1860. for (var k$1 in scales) {
  1861. var sc = scales[k$1];
  1862. if (sc.min != null || sc.max != null) {
  1863. pendScales[k$1] = {min: sc.min, max: sc.max};
  1864. sc.min = sc.max = null;
  1865. }
  1866. }
  1867. // self.tz = opts.tz || Intl.DateTimeFormat().resolvedOptions().timeZone;
  1868. var _tzDate = (opts.tzDate || (ts => new Date(round(ts / ms))));
  1869. var _fmtDate = (opts.fmtDate || fmtDate);
  1870. var _timeAxisSplits = (ms == 1 ? timeAxisSplitsMs(_tzDate) : timeAxisSplitsS(_tzDate));
  1871. var _timeAxisVals = timeAxisVals(_tzDate, timeAxisStamps((ms == 1 ? _timeAxisStampsMs : _timeAxisStampsS), _fmtDate));
  1872. var _timeSeriesVal = timeSeriesVal(_tzDate, timeSeriesStamp(_timeSeriesStamp, _fmtDate));
  1873. var legend = (self.legend = assign({}, legendOpts, opts.legend));
  1874. var showLegend = legend.show;
  1875. var markers = legend.markers;
  1876. {
  1877. markers.width = fnOrSelf(markers.width);
  1878. markers.dash = fnOrSelf(markers.dash);
  1879. markers.stroke = fnOrSelf(markers.stroke);
  1880. markers.fill = fnOrSelf(markers.fill);
  1881. }
  1882. var legendEl;
  1883. var legendRows = [];
  1884. var legendCells = [];
  1885. var legendCols;
  1886. var multiValLegend = false;
  1887. var NULL_LEGEND_VALUES = {};
  1888. if (legend.live) {
  1889. var getMultiVals = series[1] ? series[1].values : null;
  1890. multiValLegend = getMultiVals != null;
  1891. legendCols = multiValLegend ? getMultiVals(self, 1, 0) : {_: 0};
  1892. for (var k$2 in legendCols)
  1893. { NULL_LEGEND_VALUES[k$2] = "--"; }
  1894. }
  1895. if (showLegend) {
  1896. legendEl = placeTag("table", LEGEND, root);
  1897. if (multiValLegend) {
  1898. var head = placeTag("tr", LEGEND_THEAD, legendEl);
  1899. placeTag("th", null, head);
  1900. for (var key in legendCols)
  1901. { placeTag("th", LEGEND_LABEL, head).textContent = key; }
  1902. }
  1903. else {
  1904. addClass(legendEl, LEGEND_INLINE);
  1905. legend.live && addClass(legendEl, LEGEND_LIVE);
  1906. }
  1907. }
  1908. var son = {show: true};
  1909. var soff = {show: false};
  1910. function initLegendRow(s, i) {
  1911. if (i == 0 && (multiValLegend || !legend.live))
  1912. { return nullNullTuple; }
  1913. var cells = [];
  1914. var row = placeTag("tr", LEGEND_SERIES, legendEl, legendEl.childNodes[i]);
  1915. addClass(row, s.class);
  1916. if (!s.show)
  1917. { addClass(row, OFF); }
  1918. var label = placeTag("th", null, row);
  1919. if (markers.show) {
  1920. var indic = placeDiv(LEGEND_MARKER, label);
  1921. if (i > 0) {
  1922. var width = markers.width(self, i);
  1923. if (width)
  1924. { indic.style.border = width + "px " + markers.dash(self, i) + " " + markers.stroke(self, i); }
  1925. indic.style.background = markers.fill(self, i);
  1926. }
  1927. }
  1928. var text = placeDiv(LEGEND_LABEL, label);
  1929. text.textContent = s.label;
  1930. if (i > 0) {
  1931. if (!markers.show)
  1932. { text.style.color = s.width > 0 ? markers.stroke(self, i) : markers.fill(self, i); }
  1933. onMouse("click", label, e => {
  1934. if (cursor._lock)
  1935. { return; }
  1936. var seriesIdx = series.indexOf(s);
  1937. if (e.ctrlKey != legend.isolate) {
  1938. // if any other series is shown, isolate this one. else show all
  1939. var isolate = series.some((s, i) => i > 0 && i != seriesIdx && s.show);
  1940. series.forEach((s, i) => {
  1941. i > 0 && setSeries(i, isolate ? (i == seriesIdx ? son : soff) : son, syncOpts.setSeries);
  1942. });
  1943. }
  1944. else
  1945. { setSeries(seriesIdx, {show: !s.show}, syncOpts.setSeries); }
  1946. });
  1947. if (cursorFocus) {
  1948. onMouse(mouseenter, label, e => {
  1949. if (cursor._lock)
  1950. { return; }
  1951. setSeries(series.indexOf(s), FOCUS_TRUE, syncOpts.setSeries);
  1952. });
  1953. }
  1954. }
  1955. for (var key in legendCols) {
  1956. var v = placeTag("td", LEGEND_VALUE, row);
  1957. v.textContent = "--";
  1958. cells.push(v);
  1959. }
  1960. return [row, cells];
  1961. }
  1962. var mouseListeners = new Map();
  1963. function onMouse(ev, targ, fn) {
  1964. var targListeners = mouseListeners.get(targ) || {};
  1965. var listener = cursor.bind[ev](self, targ, fn);
  1966. if (listener) {
  1967. on(ev, targ, targListeners[ev] = listener);
  1968. mouseListeners.set(targ, targListeners);
  1969. }
  1970. }
  1971. function offMouse(ev, targ, fn) {
  1972. var targListeners = mouseListeners.get(targ) || {};
  1973. for (var k in targListeners) {
  1974. if (ev == null || k == ev) {
  1975. off(k, targ, targListeners[k]);
  1976. delete targListeners[k];
  1977. }
  1978. }
  1979. if (ev == null)
  1980. { mouseListeners.delete(targ); }
  1981. }
  1982. var fullWidCss = 0;
  1983. var fullHgtCss = 0;
  1984. var plotWidCss = 0;
  1985. var plotHgtCss = 0;
  1986. // plot margins to account for axes
  1987. var plotLftCss = 0;
  1988. var plotTopCss = 0;
  1989. var plotLft = 0;
  1990. var plotTop = 0;
  1991. var plotWid = 0;
  1992. var plotHgt = 0;
  1993. self.bbox = {};
  1994. var shouldSetScales = false;
  1995. var shouldSetSize = false;
  1996. var shouldConvergeSize = false;
  1997. var shouldSetCursor = false;
  1998. var shouldSetLegend = false;
  1999. function _setSize(width, height, force) {
  2000. if (force || (width != self.width || height != self.height))
  2001. { calcSize(width, height); }
  2002. resetYSeries(false);
  2003. shouldConvergeSize = true;
  2004. shouldSetSize = true;
  2005. shouldSetCursor = cursor.left >= 0;
  2006. shouldSetLegend = true;
  2007. commit();
  2008. }
  2009. function calcSize(width, height) {
  2010. // log("calcSize()", arguments);
  2011. self.width = fullWidCss = plotWidCss = width;
  2012. self.height = fullHgtCss = plotHgtCss = height;
  2013. plotLftCss = plotTopCss = 0;
  2014. calcPlotRect();
  2015. calcAxesRects();
  2016. var bb = self.bbox;
  2017. plotLft = bb.left = incrRound(plotLftCss * pxRatio, 0.5);
  2018. plotTop = bb.top = incrRound(plotTopCss * pxRatio, 0.5);
  2019. plotWid = bb.width = incrRound(plotWidCss * pxRatio, 0.5);
  2020. plotHgt = bb.height = incrRound(plotHgtCss * pxRatio, 0.5);
  2021. // updOriDims();
  2022. }
  2023. // ensures size calc convergence
  2024. var CYCLE_LIMIT = 3;
  2025. function convergeSize() {
  2026. var converged = false;
  2027. var cycleNum = 0;
  2028. while (!converged) {
  2029. cycleNum++;
  2030. var axesConverged = axesCalc(cycleNum);
  2031. var paddingConverged = paddingCalc(cycleNum);
  2032. converged = cycleNum == CYCLE_LIMIT || (axesConverged && paddingConverged);
  2033. if (!converged) {
  2034. calcSize(self.width, self.height);
  2035. shouldSetSize = true;
  2036. }
  2037. }
  2038. }
  2039. function setSize(ref) {
  2040. var width = ref.width;
  2041. var height = ref.height;
  2042. _setSize(width, height);
  2043. }
  2044. self.setSize = setSize;
  2045. // accumulate axis offsets, reduce canvas width
  2046. function calcPlotRect() {
  2047. // easements for edge labels
  2048. var hasTopAxis = false;
  2049. var hasBtmAxis = false;
  2050. var hasRgtAxis = false;
  2051. var hasLftAxis = false;
  2052. axes.forEach((axis, i) => {
  2053. if (axis.show && axis._show) {
  2054. var side = axis.side;
  2055. var _size = axis._size;
  2056. var isVt = side % 2;
  2057. var labelSize = axis.labelSize = (axis.label != null ? (axis.labelSize || 30) : 0);
  2058. var fullSize = _size + labelSize;
  2059. if (fullSize > 0) {
  2060. if (isVt) {
  2061. plotWidCss -= fullSize;
  2062. if (side == 3) {
  2063. plotLftCss += fullSize;
  2064. hasLftAxis = true;
  2065. }
  2066. else
  2067. { hasRgtAxis = true; }
  2068. }
  2069. else {
  2070. plotHgtCss -= fullSize;
  2071. if (side == 0) {
  2072. plotTopCss += fullSize;
  2073. hasTopAxis = true;
  2074. }
  2075. else
  2076. { hasBtmAxis = true; }
  2077. }
  2078. }
  2079. }
  2080. });
  2081. sidesWithAxes[0] = hasTopAxis;
  2082. sidesWithAxes[1] = hasRgtAxis;
  2083. sidesWithAxes[2] = hasBtmAxis;
  2084. sidesWithAxes[3] = hasLftAxis;
  2085. // hz padding
  2086. plotWidCss -= _padding[1] + _padding[3];
  2087. plotLftCss += _padding[3];
  2088. // vt padding
  2089. plotHgtCss -= _padding[2] + _padding[0];
  2090. plotTopCss += _padding[0];
  2091. }
  2092. function calcAxesRects() {
  2093. // will accum +
  2094. var off1 = plotLftCss + plotWidCss;
  2095. var off2 = plotTopCss + plotHgtCss;
  2096. // will accum -
  2097. var off3 = plotLftCss;
  2098. var off0 = plotTopCss;
  2099. function incrOffset(side, size) {
  2100. switch (side) {
  2101. case 1: off1 += size; return off1 - size;
  2102. case 2: off2 += size; return off2 - size;
  2103. case 3: off3 -= size; return off3 + size;
  2104. case 0: off0 -= size; return off0 + size;
  2105. }
  2106. }
  2107. axes.forEach((axis, i) => {
  2108. if (axis.show && axis._show) {
  2109. var side = axis.side;
  2110. axis._pos = incrOffset(side, axis._size);
  2111. if (axis.label != null)
  2112. { axis._lpos = incrOffset(side, axis.labelSize); }
  2113. }
  2114. });
  2115. }
  2116. var cursor = (self.cursor = assign({}, cursorOpts, opts.cursor));
  2117. {
  2118. cursor._lock = false;
  2119. var points = cursor.points;
  2120. points.show = fnOrSelf(points.show);
  2121. points.size = fnOrSelf(points.size);
  2122. points.stroke = fnOrSelf(points.stroke);
  2123. points.width = fnOrSelf(points.width);
  2124. points.fill = fnOrSelf(points.fill);
  2125. }
  2126. var focus = self.focus = assign({}, opts.focus || {alpha: 0.3}, cursor.focus);
  2127. var cursorFocus = focus.prox >= 0;
  2128. // series-intersection markers
  2129. var cursorPts = [null];
  2130. function initCursorPt(s, si) {
  2131. if (si > 0) {
  2132. var pt = cursor.points.show(self, si);
  2133. if (pt) {
  2134. addClass(pt, CURSOR_PT);
  2135. addClass(pt, s.class);
  2136. trans(pt, -10, -10, plotWidCss, plotHgtCss);
  2137. over.insertBefore(pt, cursorPts[si]);
  2138. return pt;
  2139. }
  2140. }
  2141. }
  2142. function initSeries(s, i) {
  2143. var isTime = scales[s.scale].time;
  2144. var sv = s.value;
  2145. s.value = isTime ? (isStr(sv) ? timeSeriesVal(_tzDate, timeSeriesStamp(sv, _fmtDate)) : sv || _timeSeriesVal) : sv || numSeriesVal;
  2146. s.label = s.label || (isTime ? timeSeriesLabel : numSeriesLabel);
  2147. if (i > 0) {
  2148. s.width = s.width == null ? 1 : s.width;
  2149. s.paths = s.paths || linearPath || retNull;
  2150. s.fillTo = fnOrSelf(s.fillTo || seriesFillTo);
  2151. s.pxAlign = +ifNull(s.pxAlign, pxAlign);
  2152. s.pxRound = pxRoundGen(s.pxAlign);
  2153. s.stroke = fnOrSelf(s.stroke || null);
  2154. s.fill = fnOrSelf(s.fill || null);
  2155. s._stroke = s._fill = s._paths = s._focus = null;
  2156. var _ptDia = ptDia(s.width, 1);
  2157. var points = s.points = assign({}, {
  2158. size: _ptDia,
  2159. width: max(1, _ptDia * .2),
  2160. stroke: s.stroke,
  2161. space: _ptDia * 2,
  2162. _stroke: null,
  2163. _fill: null,
  2164. }, s.points);
  2165. points.show = fnOrSelf(points.show);
  2166. points.filter = fnOrSelf(points.filter);
  2167. points.fill = fnOrSelf(points.fill);
  2168. points.stroke = fnOrSelf(points.stroke);
  2169. }
  2170. if (showLegend) {
  2171. var rowCells = initLegendRow(s, i);
  2172. legendRows.splice(i, 0, rowCells[0]);
  2173. legendCells.splice(i, 0, rowCells[1]);
  2174. legend.values.push(null); // NULL_LEGEND_VALS not yet avil here :(
  2175. }
  2176. if (cursor.show) {
  2177. cursor.idxs.splice(i, 0, null);
  2178. var pt = initCursorPt(s, i);
  2179. pt && cursorPts.splice(i, 0, pt);
  2180. }
  2181. }
  2182. function addSeries(opts, si) {
  2183. si = si == null ? series.length : si;
  2184. opts = setDefault(opts, si, xSeriesOpts, ySeriesOpts);
  2185. series.splice(si, 0, opts);
  2186. initSeries(series[si], si);
  2187. }
  2188. self.addSeries = addSeries;
  2189. function delSeries(i) {
  2190. series.splice(i, 1);
  2191. if (showLegend) {
  2192. legend.values.splice(i, 1);
  2193. legendCells.splice(i, 1);
  2194. var tr = legendRows.splice(i, 1)[0];
  2195. offMouse(null, tr.firstChild);
  2196. tr.remove();
  2197. }
  2198. if (cursor.show) {
  2199. cursor.idxs.splice(i, 1);
  2200. cursorPts.length > 1 && cursorPts.splice(i, 1)[0].remove();
  2201. }
  2202. // TODO: de-init no-longer-needed scales?
  2203. }
  2204. self.delSeries = delSeries;
  2205. var sidesWithAxes = [false, false, false, false];
  2206. function initAxis(axis, i) {
  2207. axis._show = axis.show;
  2208. if (axis.show) {
  2209. var isVt = axis.side % 2;
  2210. var sc = scales[axis.scale];
  2211. // this can occur if all series specify non-default scales
  2212. if (sc == null) {
  2213. axis.scale = isVt ? series[1].scale : xScaleKey;
  2214. sc = scales[axis.scale];
  2215. }
  2216. // also set defaults for incrs & values based on axis distr
  2217. var isTime = sc.time;
  2218. axis.size = fnOrSelf(axis.size);
  2219. axis.space = fnOrSelf(axis.space);
  2220. axis.rotate = fnOrSelf(axis.rotate);
  2221. axis.incrs = fnOrSelf(axis.incrs || ( sc.distr == 2 ? wholeIncrs : (isTime ? (ms == 1 ? timeIncrsMs : timeIncrsS) : numIncrs)));
  2222. axis.splits = fnOrSelf(axis.splits || (isTime && sc.distr == 1 ? _timeAxisSplits : sc.distr == 3 ? logAxisSplits : sc.distr == 4 ? asinhAxisSplits : numAxisSplits));
  2223. axis.stroke = fnOrSelf(axis.stroke);
  2224. axis.grid.stroke = fnOrSelf(axis.grid.stroke);
  2225. axis.ticks.stroke = fnOrSelf(axis.ticks.stroke);
  2226. var av = axis.values;
  2227. axis.values = (
  2228. // static array of tick values
  2229. isArr(av) && !isArr(av[0]) ? fnOrSelf(av) :
  2230. // temporal
  2231. isTime ? (
  2232. // config array of fmtDate string tpls
  2233. isArr(av) ?
  2234. timeAxisVals(_tzDate, timeAxisStamps(av, _fmtDate)) :
  2235. // fmtDate string tpl
  2236. isStr(av) ?
  2237. timeAxisVal(_tzDate, av) :
  2238. av || _timeAxisVals
  2239. ) : av || numAxisVals
  2240. );
  2241. axis.filter = fnOrSelf(axis.filter || ( sc.distr >= 3 ? logAxisValsFilt : retArg1));
  2242. axis.font = pxRatioFont(axis.font);
  2243. axis.labelFont = pxRatioFont(axis.labelFont);
  2244. axis._size = axis.size(self, null, i, 0);
  2245. axis._space =
  2246. axis._rotate =
  2247. axis._incrs =
  2248. axis._found = // foundIncrSpace
  2249. axis._splits =
  2250. axis._values = null;
  2251. if (axis._size > 0)
  2252. { sidesWithAxes[i] = true; }
  2253. }
  2254. }
  2255. function autoPadSide(self, side, sidesWithAxes, cycleNum) {
  2256. var hasTopAxis = sidesWithAxes[0];
  2257. var hasRgtAxis = sidesWithAxes[1];
  2258. var hasBtmAxis = sidesWithAxes[2];
  2259. var hasLftAxis = sidesWithAxes[3];
  2260. var ori = side % 2;
  2261. var size = 0;
  2262. if (ori == 0 && (hasLftAxis || hasRgtAxis))
  2263. { size = (side == 0 && !hasTopAxis || side == 2 && !hasBtmAxis ? round(xAxisOpts.size / 3) : 0); }
  2264. if (ori == 1 && (hasTopAxis || hasBtmAxis))
  2265. { size = (side == 1 && !hasRgtAxis || side == 3 && !hasLftAxis ? round(yAxisOpts.size / 2) : 0); }
  2266. return size;
  2267. }
  2268. var padding = self.padding = (opts.padding || [autoPadSide,autoPadSide,autoPadSide,autoPadSide]).map(p => fnOrSelf(ifNull(p, autoPadSide)));
  2269. var _padding = self._padding = padding.map((p, i) => p(self, i, sidesWithAxes, 0));
  2270. var dataLen;
  2271. // rendered data window
  2272. var i0 = null;
  2273. var i1 = null;
  2274. var idxs = series[0].idxs;
  2275. var data0 = null;
  2276. var viaAutoScaleX = false;
  2277. function setData(_data, _resetScales) {
  2278. data = (_data || []).slice();
  2279. data[0] = data[0] || [];
  2280. self.data = data.slice();
  2281. data0 = data[0];
  2282. dataLen = data0.length;
  2283. if (xScaleDistr == 2)
  2284. { data[0] = data0.map((v, i) => i); }
  2285. self._data = data;
  2286. resetYSeries(true);
  2287. fire("setData");
  2288. if (_resetScales !== false) {
  2289. var xsc = scaleX;
  2290. if (xsc.auto(self, viaAutoScaleX))
  2291. { autoScaleX(); }
  2292. else
  2293. { _setScale(xScaleKey, xsc.min, xsc.max); }
  2294. shouldSetCursor = cursor.left >= 0;
  2295. shouldSetLegend = true;
  2296. commit();
  2297. }
  2298. }
  2299. self.setData = setData;
  2300. function autoScaleX() {
  2301. var assign, assign$1, assign$2;
  2302. viaAutoScaleX = true;
  2303. var _min, _max;
  2304. if (dataLen > 0) {
  2305. i0 = idxs[0] = 0;
  2306. i1 = idxs[1] = dataLen - 1;
  2307. _min = data[0][i0];
  2308. _max = data[0][i1];
  2309. if (xScaleDistr == 2) {
  2310. _min = i0;
  2311. _max = i1;
  2312. }
  2313. else if (dataLen == 1) {
  2314. if (xScaleDistr == 3)
  2315. { (assign = rangeLog(_min, _min, scaleX.log, false), _min = assign[0], _max = assign[1]); }
  2316. else if (xScaleDistr == 4)
  2317. { (assign$1 = rangeAsinh(_min, _min, scaleX.log, false), _min = assign$1[0], _max = assign$1[1]); }
  2318. else if (scaleX.time)
  2319. { _max = _min + round(86400 / ms); }
  2320. else
  2321. { (assign$2 = rangeNum(_min, _max, rangePad, true), _min = assign$2[0], _max = assign$2[1]); }
  2322. }
  2323. }
  2324. else {
  2325. i0 = idxs[0] = _min = null;
  2326. i1 = idxs[1] = _max = null;
  2327. }
  2328. _setScale(xScaleKey, _min, _max);
  2329. }
  2330. function setCtxStyle(stroke, width, dash, cap, fill) {
  2331. ctx.strokeStyle = stroke || transparent;
  2332. ctx.lineWidth = width;
  2333. ctx.lineJoin = "round";
  2334. ctx.lineCap = cap || "butt"; // (‿|‿)
  2335. ctx.setLineDash(dash || []);
  2336. ctx.fillStyle = fill || transparent;
  2337. }
  2338. function setScales() {
  2339. // log("setScales()", arguments);
  2340. // wip scales
  2341. var wipScales = copy(scales, fastIsObj);
  2342. for (var k in wipScales) {
  2343. var wsc = wipScales[k];
  2344. var psc = pendScales[k];
  2345. if (psc != null && psc.min != null) {
  2346. assign(wsc, psc);
  2347. // explicitly setting the x-scale invalidates everything (acts as redraw)
  2348. if (k == xScaleKey)
  2349. { resetYSeries(true); }
  2350. }
  2351. else if (k != xScaleKey) {
  2352. if (dataLen == 0 && wsc.from == null) {
  2353. var minMax = wsc.range(self, null, null, k);
  2354. wsc.min = minMax[0];
  2355. wsc.max = minMax[1];
  2356. }
  2357. else {
  2358. wsc.min = inf;
  2359. wsc.max = -inf;
  2360. }
  2361. }
  2362. }
  2363. if (dataLen > 0) {
  2364. // pre-range y-scales from y series' data values
  2365. series.forEach((s, i) => {
  2366. var k = s.scale;
  2367. var wsc = wipScales[k];
  2368. var psc = pendScales[k];
  2369. if (i == 0) {
  2370. var minMax = wsc.range(self, wsc.min, wsc.max, k);
  2371. wsc.min = minMax[0];
  2372. wsc.max = minMax[1];
  2373. i0 = closestIdx(wsc.min, data[0]);
  2374. i1 = closestIdx(wsc.max, data[0]);
  2375. // closest indices can be outside of view
  2376. if (data[0][i0] < wsc.min)
  2377. { i0++; }
  2378. if (data[0][i1] > wsc.max)
  2379. { i1--; }
  2380. s.min = data0[i0];
  2381. s.max = data0[i1];
  2382. }
  2383. else if (s.show && s.auto && wsc.auto(self, viaAutoScaleX) && (psc == null || psc.min == null)) {
  2384. // only run getMinMax() for invalidated series data, else reuse
  2385. var minMax$1 = s.min == null ? (wsc.distr == 3 ? getMinMaxLog(data[i], i0, i1) : getMinMax(data[i], i0, i1, s.sorted)) : [s.min, s.max];
  2386. // initial min/max
  2387. wsc.min = min(wsc.min, s.min = minMax$1[0]);
  2388. wsc.max = max(wsc.max, s.max = minMax$1[1]);
  2389. }
  2390. s.idxs[0] = i0;
  2391. s.idxs[1] = i1;
  2392. });
  2393. // range independent scales
  2394. for (var k$1 in wipScales) {
  2395. var wsc$1 = wipScales[k$1];
  2396. var psc$1 = pendScales[k$1];
  2397. if (wsc$1.from == null && (psc$1 == null || psc$1.min == null)) {
  2398. var minMax$1 = wsc$1.range(
  2399. self,
  2400. wsc$1.min == inf ? null : wsc$1.min,
  2401. wsc$1.max == -inf ? null : wsc$1.max,
  2402. k$1
  2403. );
  2404. wsc$1.min = minMax$1[0];
  2405. wsc$1.max = minMax$1[1];
  2406. }
  2407. }
  2408. }
  2409. // range dependent scales
  2410. for (var k$2 in wipScales) {
  2411. var wsc$2 = wipScales[k$2];
  2412. if (wsc$2.from != null) {
  2413. var base = wipScales[wsc$2.from];
  2414. var minMax$2 = wsc$2.range(self, base.min, base.max, k$2);
  2415. wsc$2.min = minMax$2[0];
  2416. wsc$2.max = minMax$2[1];
  2417. }
  2418. }
  2419. var changed = {};
  2420. var anyChanged = false;
  2421. for (var k$3 in wipScales) {
  2422. var wsc$3 = wipScales[k$3];
  2423. var sc = scales[k$3];
  2424. if (sc.min != wsc$3.min || sc.max != wsc$3.max) {
  2425. sc.min = wsc$3.min;
  2426. sc.max = wsc$3.max;
  2427. var distr = sc.distr;
  2428. sc._min = distr == 3 ? log10(sc.min) : distr == 4 ? asinh(sc.min, sc.asinh) : sc.min;
  2429. sc._max = distr == 3 ? log10(sc.max) : distr == 4 ? asinh(sc.max, sc.asinh) : sc.max;
  2430. changed[k$3] = anyChanged = true;
  2431. }
  2432. }
  2433. if (anyChanged) {
  2434. // invalidate paths of all series on changed scales
  2435. series.forEach(s => {
  2436. if (changed[s.scale])
  2437. { s._paths = null; }
  2438. });
  2439. for (var k$4 in changed) {
  2440. shouldConvergeSize = true;
  2441. fire("setScale", k$4);
  2442. }
  2443. if (cursor.show)
  2444. { shouldSetCursor = cursor.left >= 0; }
  2445. }
  2446. for (var k$5 in pendScales)
  2447. { pendScales[k$5] = null; }
  2448. }
  2449. // TODO: drawWrap(si, drawPoints) (save, restore, translate, clip)
  2450. function drawPoints(si, idxs) {
  2451. // log("drawPoints()", arguments);
  2452. var s = series[si];
  2453. var p = s.points;
  2454. var _pxRound = s.pxRound;
  2455. var width = roundDec(p.width * pxRatio, 3);
  2456. var offset = (width % 2) / 2;
  2457. var isStroked = p.width > 0;
  2458. var rad = (p.size - p.width) / 2 * pxRatio;
  2459. var dia = roundDec(rad * 2, 3);
  2460. var _pxAlign = s.pxAlign == 1;
  2461. _pxAlign && ctx.translate(offset, offset);
  2462. ctx.save();
  2463. ctx.beginPath();
  2464. ctx.rect(
  2465. plotLft - dia,
  2466. plotTop - dia,
  2467. plotWid + dia * 2,
  2468. plotHgt + dia * 2
  2469. );
  2470. ctx.clip();
  2471. ctx.globalAlpha = s.alpha;
  2472. var path = new Path2D();
  2473. var scaleY = scales[s.scale];
  2474. var xDim, xOff, yDim, yOff;
  2475. if (scaleX.ori == 0) {
  2476. xDim = plotWid;
  2477. xOff = plotLft;
  2478. yDim = plotHgt;
  2479. yOff = plotTop;
  2480. }
  2481. else {
  2482. xDim = plotHgt;
  2483. xOff = plotTop;
  2484. yDim = plotWid;
  2485. yOff = plotLft;
  2486. }
  2487. var drawPoint = pi => {
  2488. if (data[si][pi] != null) {
  2489. var x = _pxRound(valToPosX(data[0][pi], scaleX, xDim, xOff));
  2490. var y = _pxRound(valToPosY(data[si][pi], scaleY, yDim, yOff));
  2491. moveTo(path, x + rad, y);
  2492. arc(path, x, y, rad, 0, PI * 2);
  2493. }
  2494. };
  2495. if (idxs)
  2496. { idxs.forEach(drawPoint); }
  2497. else {
  2498. for (var pi = i0; pi <= i1; pi++)
  2499. { drawPoint(pi); }
  2500. }
  2501. var _stroke = p._stroke = p.stroke(self, si);
  2502. var _fill = p._fill = p.fill(self, si);
  2503. setCtxStyle(
  2504. _stroke,
  2505. width,
  2506. p.dash,
  2507. p.cap,
  2508. _fill || (isStroked ? "#fff" : s._stroke)
  2509. );
  2510. ctx.fill(path);
  2511. isStroked && ctx.stroke(path);
  2512. ctx.globalAlpha = 1;
  2513. ctx.restore();
  2514. _pxAlign && ctx.translate(-offset, -offset);
  2515. }
  2516. // grabs the nearest indices with y data outside of x-scale limits
  2517. function getOuterIdxs(ydata) {
  2518. var _i0 = clamp(i0 - 1, 0, dataLen - 1);
  2519. var _i1 = clamp(i1 + 1, 0, dataLen - 1);
  2520. while (ydata[_i0] == null && _i0 > 0)
  2521. { _i0--; }
  2522. while (ydata[_i1] == null && _i1 < dataLen - 1)
  2523. { _i1++; }
  2524. return [_i0, _i1];
  2525. }
  2526. function drawSeries() {
  2527. if (dataLen > 0) {
  2528. series.forEach((s, i) => {
  2529. if (i > 0 && s.show && s._paths == null) {
  2530. var _idxs = getOuterIdxs(data[i]);
  2531. s._paths = s.paths(self, i, _idxs[0], _idxs[1]);
  2532. }
  2533. });
  2534. series.forEach((s, i) => {
  2535. if (i > 0 && s.show) {
  2536. s._paths && drawPath(i);
  2537. {
  2538. var show = s.points.show(self, i, i0, i1);
  2539. var idxs = s.points.filter(self, i, show, s._paths ? s._paths.gaps : null);
  2540. if (show || idxs)
  2541. { drawPoints(i, idxs); }
  2542. }
  2543. fire("drawSeries", i);
  2544. }
  2545. });
  2546. }
  2547. }
  2548. function drawPath(si) {
  2549. var s = series[si];
  2550. var ref = s._paths;
  2551. var stroke = ref.stroke;
  2552. var fill = ref.fill;
  2553. var clip = ref.clip;
  2554. var flags = ref.flags;
  2555. var width = roundDec(s.width * pxRatio, 3);
  2556. var offset = (width % 2) / 2;
  2557. var strokeStyle = s._stroke = s.stroke(self, si);
  2558. var fillStyle = s._fill = s.fill(self, si);
  2559. ctx.globalAlpha = s.alpha;
  2560. var _pxAlign = s.pxAlign == 1;
  2561. _pxAlign && ctx.translate(offset, offset);
  2562. ctx.save();
  2563. var lft = plotLft,
  2564. top = plotTop,
  2565. wid = plotWid,
  2566. hgt = plotHgt;
  2567. var halfWid = width * pxRatio / 2;
  2568. if (s.min == 0)
  2569. { hgt += halfWid; }
  2570. if (s.max == 0) {
  2571. top -= halfWid;
  2572. hgt += halfWid;
  2573. }
  2574. ctx.beginPath();
  2575. ctx.rect(lft, top, wid, hgt);
  2576. ctx.clip();
  2577. clip && ctx.clip(clip);
  2578. fillStroke(si, strokeStyle, width, s.dash, s.cap, fillStyle, stroke, fill, flags);
  2579. ctx.restore();
  2580. _pxAlign && ctx.translate(-offset, -offset);
  2581. ctx.globalAlpha = 1;
  2582. }
  2583. function fillStroke(si, strokeStyle, lineWidth, lineDash, lineCap, fillStyle, strokePath, fillPath, flags) {
  2584. var didStrokeFill = false;
  2585. // for all bands where this series is the top edge, create upwards clips using the bottom edges
  2586. // and apply clips + fill with band fill or dfltFill
  2587. bands.forEach((b, bi) => {
  2588. // isUpperEdge?
  2589. if (b.series[0] == si) {
  2590. var lowerEdge = series[b.series[1]];
  2591. var clip = (lowerEdge._paths || EMPTY_OBJ).band;
  2592. ctx.save();
  2593. var _fillStyle = null;
  2594. // hasLowerEdge?
  2595. if (lowerEdge.show && clip)
  2596. { _fillStyle = b.fill(self, bi) || fillStyle; }
  2597. else
  2598. { clip = null; }
  2599. strokeFill(strokeStyle, lineWidth, lineDash, lineCap, _fillStyle, strokePath, fillPath, clip, flags);
  2600. ctx.restore();
  2601. didStrokeFill = true;
  2602. }
  2603. });
  2604. if (!didStrokeFill)
  2605. { strokeFill(strokeStyle, lineWidth, lineDash, lineCap, fillStyle, strokePath, fillPath, null, flags); }
  2606. }
  2607. var CLIP_FILL_STROKE = BAND_CLIP_FILL | BAND_CLIP_STROKE;
  2608. function strokeFill(strokeStyle, lineWidth, lineDash, lineCap, fillStyle, strokePath, fillPath, clip, flags) {
  2609. setCtxStyle(strokeStyle, lineWidth, lineDash, lineCap, fillStyle);
  2610. if (clip) {
  2611. if ((flags & CLIP_FILL_STROKE) == CLIP_FILL_STROKE) {
  2612. ctx.clip(clip);
  2613. doFill(fillStyle, fillPath);
  2614. doStroke(strokeStyle, strokePath, lineWidth);
  2615. }
  2616. else if (flags & BAND_CLIP_STROKE) {
  2617. doFill(fillStyle, fillPath);
  2618. ctx.clip(clip);
  2619. doStroke(strokeStyle, strokePath, lineWidth);
  2620. }
  2621. else if (flags & BAND_CLIP_FILL) {
  2622. ctx.save();
  2623. ctx.clip(clip);
  2624. doFill(fillStyle, fillPath);
  2625. ctx.restore();
  2626. doStroke(strokeStyle, strokePath, lineWidth);
  2627. }
  2628. }
  2629. else {
  2630. doFill(fillStyle, fillPath);
  2631. doStroke(strokeStyle, strokePath, lineWidth);
  2632. }
  2633. }
  2634. function doStroke(strokeStyle, strokePath, lineWidth) {
  2635. strokeStyle && strokePath && lineWidth && ctx.stroke(strokePath);
  2636. }
  2637. function doFill(fillStyle, fillPath) {
  2638. fillStyle && fillPath && ctx.fill(fillPath);
  2639. }
  2640. function getIncrSpace(axisIdx, min, max, fullDim) {
  2641. var axis = axes[axisIdx];
  2642. var incrSpace;
  2643. if (fullDim <= 0)
  2644. { incrSpace = [0, 0]; }
  2645. else {
  2646. var minSpace = axis._space = axis.space(self, axisIdx, min, max, fullDim);
  2647. var incrs = axis._incrs = axis.incrs(self, axisIdx, min, max, fullDim, minSpace);
  2648. incrSpace = axis._found = findIncr(min, max, incrs, fullDim, minSpace);
  2649. }
  2650. return incrSpace;
  2651. }
  2652. function drawOrthoLines(offs, filts, ori, side, pos0, len, width, stroke, dash, cap) {
  2653. var offset = (width % 2) / 2;
  2654. pxAlign == 1 && ctx.translate(offset, offset);
  2655. setCtxStyle(stroke, width, dash, cap);
  2656. ctx.beginPath();
  2657. var x0, y0, x1, y1, pos1 = pos0 + (side == 0 || side == 3 ? -len : len);
  2658. if (ori == 0) {
  2659. y0 = pos0;
  2660. y1 = pos1;
  2661. }
  2662. else {
  2663. x0 = pos0;
  2664. x1 = pos1;
  2665. }
  2666. offs.forEach((off, i) => {
  2667. if (filts[i] == null)
  2668. { return; }
  2669. if (ori == 0)
  2670. { x0 = x1 = off; }
  2671. else
  2672. { y0 = y1 = off; }
  2673. ctx.moveTo(x0, y0);
  2674. ctx.lineTo(x1, y1);
  2675. });
  2676. ctx.stroke();
  2677. pxAlign == 1 && ctx.translate(-offset, -offset);
  2678. }
  2679. function axesCalc(cycleNum) {
  2680. // log("axesCalc()", arguments);
  2681. var converged = true;
  2682. axes.forEach((axis, i) => {
  2683. if (!axis.show)
  2684. { return; }
  2685. var scale = scales[axis.scale];
  2686. if (scale.min == null) {
  2687. if (axis._show) {
  2688. converged = false;
  2689. axis._show = false;
  2690. resetYSeries(false);
  2691. }
  2692. return;
  2693. }
  2694. else {
  2695. if (!axis._show) {
  2696. converged = false;
  2697. axis._show = true;
  2698. resetYSeries(false);
  2699. }
  2700. }
  2701. var side = axis.side;
  2702. var ori = side % 2;
  2703. var min = scale.min;
  2704. var max = scale.max; // // should this toggle them ._show = false
  2705. var ref = getIncrSpace(i, min, max, ori == 0 ? plotWidCss : plotHgtCss);
  2706. var _incr = ref[0];
  2707. var _space = ref[1];
  2708. if (_space == 0)
  2709. { return; }
  2710. // if we're using index positions, force first tick to match passed index
  2711. var forceMin = scale.distr == 2;
  2712. var _splits = axis._splits = axis.splits(self, i, min, max, _incr, _space, forceMin);
  2713. // tick labels
  2714. // BOO this assumes a specific data/series
  2715. var splits = scale.distr == 2 ? _splits.map(i => data0[i]) : _splits;
  2716. var incr = scale.distr == 2 ? data0[_splits[1]] - data0[_splits[0]] : _incr;
  2717. var values = axis._values = axis.values(self, axis.filter(self, splits, i, _space, incr), i, _space, incr);
  2718. // rotating of labels only supported on bottom x axis
  2719. axis._rotate = side == 2 ? axis.rotate(self, values, i, _space) : 0;
  2720. var oldSize = axis._size;
  2721. axis._size = ceil(axis.size(self, values, i, cycleNum));
  2722. if (oldSize != null && axis._size != oldSize) // ready && ?
  2723. { converged = false; }
  2724. });
  2725. return converged;
  2726. }
  2727. function paddingCalc(cycleNum) {
  2728. var converged = true;
  2729. padding.forEach((p, i) => {
  2730. var _p = p(self, i, sidesWithAxes, cycleNum);
  2731. if (_p != _padding[i])
  2732. { converged = false; }
  2733. _padding[i] = _p;
  2734. });
  2735. return converged;
  2736. }
  2737. function drawAxesGrid() {
  2738. axes.forEach((axis, i) => {
  2739. if (!axis.show || !axis._show)
  2740. { return; }
  2741. var scale = scales[axis.scale];
  2742. var side = axis.side;
  2743. var ori = side % 2;
  2744. var plotDim = ori == 0 ? plotWid : plotHgt;
  2745. var plotOff = ori == 0 ? plotLft : plotTop;
  2746. var axisGap = round(axis.gap * pxRatio);
  2747. var ref = axis._found;
  2748. var _incr = ref[0];
  2749. var _space = ref[1];
  2750. var x, y;
  2751. var fillStyle = axis.stroke(self, i);
  2752. // axis label
  2753. if (axis.label) {
  2754. ctx.save();
  2755. var baseLpos = round(axis._lpos * pxRatio);
  2756. if (ori == 1) {
  2757. x = y = 0;
  2758. ctx.translate(
  2759. baseLpos,
  2760. round(plotTop + plotHgt / 2)
  2761. );
  2762. ctx.rotate((side == 3 ? -PI : PI) / 2);
  2763. }
  2764. else {
  2765. x = round(plotLft + plotWid / 2);
  2766. y = baseLpos;
  2767. }
  2768. ctx.font = axis.labelFont[0];
  2769. ctx.fillStyle = fillStyle;
  2770. ctx.textAlign = "center";
  2771. ctx.textBaseline = side == 2 ? TOP : BOTTOM;
  2772. ctx.fillText(axis.label, x, y);
  2773. ctx.restore();
  2774. }
  2775. if (_space == 0)
  2776. { return; }
  2777. var _splits = axis._splits;
  2778. // tick labels
  2779. // BOO this assumes a specific data/series
  2780. var splits = scale.distr == 2 ? _splits.map(i => data0[i]) : _splits;
  2781. var incr = scale.distr == 2 ? data0[_splits[1]] - data0[_splits[0]] : _incr;
  2782. var ticks = axis.ticks;
  2783. var tickSize = ticks.show ? round(ticks.size * pxRatio) : 0;
  2784. // rotating of labels only supported on bottom x axis
  2785. var angle = axis._rotate * -PI/180;
  2786. var basePos = pxRound(axis._pos * pxRatio);
  2787. var shiftAmt = tickSize + axisGap;
  2788. var shiftDir = ori == 0 && side == 0 || ori == 1 && side == 3 ? -1 : 1;
  2789. var finalPos = basePos + shiftAmt * shiftDir;
  2790. y = ori == 0 ? finalPos : 0;
  2791. x = ori == 1 ? finalPos : 0;
  2792. ctx.font = axis.font[0];
  2793. ctx.fillStyle = fillStyle;
  2794. ctx.textAlign = axis.align == 1 ? LEFT :
  2795. axis.align == 2 ? RIGHT :
  2796. angle > 0 ? LEFT :
  2797. angle < 0 ? RIGHT :
  2798. ori == 0 ? "center" : side == 3 ? RIGHT : LEFT;
  2799. ctx.textBaseline = angle ||
  2800. ori == 1 ? "middle" : side == 2 ? TOP : BOTTOM;
  2801. var lineHeight = axis.font[1] * lineMult;
  2802. var canOffs = _splits.map(val => pxRound(getPos(val, scale, plotDim, plotOff)));
  2803. axis._values.forEach((val, i) => {
  2804. if (val == null)
  2805. { return; }
  2806. if (ori == 0)
  2807. { x = canOffs[i]; }
  2808. else
  2809. { y = canOffs[i]; }
  2810. (""+val).split(/\n/gm).forEach((text, j) => {
  2811. if (angle) {
  2812. ctx.save();
  2813. ctx.translate(x, y + j * lineHeight);
  2814. ctx.rotate(angle);
  2815. ctx.fillText(text, 0, 0);
  2816. ctx.restore();
  2817. }
  2818. else
  2819. { ctx.fillText(text, x, y + j * lineHeight); }
  2820. });
  2821. });
  2822. // ticks
  2823. if (ticks.show) {
  2824. drawOrthoLines(
  2825. canOffs,
  2826. ticks.filter(self, splits, i, _space, incr),
  2827. ori,
  2828. side,
  2829. basePos,
  2830. tickSize,
  2831. roundDec(ticks.width * pxRatio, 3),
  2832. ticks.stroke(self, i),
  2833. ticks.dash,
  2834. ticks.cap
  2835. );
  2836. }
  2837. // grid
  2838. var grid = axis.grid;
  2839. if (grid.show) {
  2840. drawOrthoLines(
  2841. canOffs,
  2842. grid.filter(self, splits, i, _space, incr),
  2843. ori,
  2844. ori == 0 ? 2 : 1,
  2845. ori == 0 ? plotTop : plotLft,
  2846. ori == 0 ? plotHgt : plotWid,
  2847. roundDec(grid.width * pxRatio, 3),
  2848. grid.stroke(self, i),
  2849. grid.dash,
  2850. grid.cap
  2851. );
  2852. }
  2853. });
  2854. fire("drawAxes");
  2855. }
  2856. function resetYSeries(minMax) {
  2857. // log("resetYSeries()", arguments);
  2858. series.forEach((s, i) => {
  2859. if (i > 0) {
  2860. s._paths = null;
  2861. if (minMax) {
  2862. s.min = null;
  2863. s.max = null;
  2864. }
  2865. }
  2866. });
  2867. }
  2868. var queuedCommit = false;
  2869. function commit() {
  2870. if (!queuedCommit) {
  2871. microTask(_commit);
  2872. queuedCommit = true;
  2873. }
  2874. }
  2875. function _commit() {
  2876. // log("_commit()", arguments);
  2877. if (shouldSetScales) {
  2878. setScales();
  2879. shouldSetScales = false;
  2880. }
  2881. if (shouldConvergeSize) {
  2882. convergeSize();
  2883. shouldConvergeSize = false;
  2884. }
  2885. if (shouldSetSize) {
  2886. setStylePx(under, LEFT, plotLftCss);
  2887. setStylePx(under, TOP, plotTopCss);
  2888. setStylePx(under, WIDTH, plotWidCss);
  2889. setStylePx(under, HEIGHT, plotHgtCss);
  2890. setStylePx(over, LEFT, plotLftCss);
  2891. setStylePx(over, TOP, plotTopCss);
  2892. setStylePx(over, WIDTH, plotWidCss);
  2893. setStylePx(over, HEIGHT, plotHgtCss);
  2894. setStylePx(wrap, WIDTH, fullWidCss);
  2895. setStylePx(wrap, HEIGHT, fullHgtCss);
  2896. can.width = round(fullWidCss * pxRatio);
  2897. can.height = round(fullHgtCss * pxRatio);
  2898. syncRect(false);
  2899. fire("setSize");
  2900. shouldSetSize = false;
  2901. }
  2902. if (fullWidCss > 0 && fullHgtCss > 0) {
  2903. ctx.clearRect(0, 0, can.width, can.height);
  2904. fire("drawClear");
  2905. drawOrder.forEach(fn => fn());
  2906. fire("draw");
  2907. }
  2908. // if (shouldSetSelect) {
  2909. // TODO: update .u-select metrics (if visible)
  2910. // setStylePx(selectDiv, TOP, select.top = 0);
  2911. // setStylePx(selectDiv, LEFT, select.left = 0);
  2912. // setStylePx(selectDiv, WIDTH, select.width = 0);
  2913. // setStylePx(selectDiv, HEIGHT, select.height = 0);
  2914. // shouldSetSelect = false;
  2915. // }
  2916. if (cursor.show && shouldSetCursor) {
  2917. updateCursor();
  2918. shouldSetCursor = false;
  2919. }
  2920. // if (FEAT_LEGEND && legend.show && legend.live && shouldSetLegend) {}
  2921. if (!ready) {
  2922. ready = true;
  2923. self.status = 1;
  2924. fire("ready");
  2925. }
  2926. viaAutoScaleX = false;
  2927. queuedCommit = false;
  2928. }
  2929. self.redraw = (rebuildPaths, recalcAxes) => {
  2930. shouldConvergeSize = recalcAxes || false;
  2931. if (rebuildPaths !== false)
  2932. { _setScale(xScaleKey, scaleX.min, scaleX.max); }
  2933. else
  2934. { commit(); }
  2935. };
  2936. // redraw() => setScale('x', scales.x.min, scales.x.max);
  2937. // explicit, never re-ranged (is this actually true? for x and y)
  2938. function setScale(key, opts) {
  2939. var sc = scales[key];
  2940. if (sc.from == null) {
  2941. if (dataLen == 0) {
  2942. var minMax = sc.range(self, opts.min, opts.max, key);
  2943. opts.min = minMax[0];
  2944. opts.max = minMax[1];
  2945. }
  2946. if (opts.min > opts.max) {
  2947. var _min = opts.min;
  2948. opts.min = opts.max;
  2949. opts.max = _min;
  2950. }
  2951. if (dataLen > 1 && opts.min != null && opts.max != null && opts.max - opts.min < 1e-16)
  2952. { return; }
  2953. if (key == xScaleKey) {
  2954. if (sc.distr == 2 && dataLen > 0) {
  2955. opts.min = closestIdx(opts.min, data[0]);
  2956. opts.max = closestIdx(opts.max, data[0]);
  2957. }
  2958. }
  2959. // log("setScale()", arguments);
  2960. pendScales[key] = opts;
  2961. shouldSetScales = true;
  2962. commit();
  2963. }
  2964. }
  2965. self.setScale = setScale;
  2966. // INTERACTION
  2967. var xCursor;
  2968. var yCursor;
  2969. var vCursor;
  2970. var hCursor;
  2971. // starting position before cursor.move
  2972. var rawMouseLeft0;
  2973. var rawMouseTop0;
  2974. // starting position
  2975. var mouseLeft0;
  2976. var mouseTop0;
  2977. // current position before cursor.move
  2978. var rawMouseLeft1;
  2979. var rawMouseTop1;
  2980. // current position
  2981. var mouseLeft1;
  2982. var mouseTop1;
  2983. var dragging = false;
  2984. var drag = cursor.drag;
  2985. var dragX = drag.x;
  2986. var dragY = drag.y;
  2987. if (cursor.show) {
  2988. if (cursor.x)
  2989. { xCursor = placeDiv(CURSOR_X, over); }
  2990. if (cursor.y)
  2991. { yCursor = placeDiv(CURSOR_Y, over); }
  2992. if (scaleX.ori == 0) {
  2993. vCursor = xCursor;
  2994. hCursor = yCursor;
  2995. }
  2996. else {
  2997. vCursor = yCursor;
  2998. hCursor = xCursor;
  2999. }
  3000. mouseLeft1 = cursor.left;
  3001. mouseTop1 = cursor.top;
  3002. }
  3003. var select = self.select = assign({
  3004. show: true,
  3005. over: true,
  3006. left: 0,
  3007. width: 0,
  3008. top: 0,
  3009. height: 0,
  3010. }, opts.select);
  3011. var selectDiv = select.show ? placeDiv(SELECT, select.over ? over : under) : null;
  3012. function setSelect(opts, _fire) {
  3013. if (select.show) {
  3014. for (var prop in opts)
  3015. { setStylePx(selectDiv, prop, select[prop] = opts[prop]); }
  3016. _fire !== false && fire("setSelect");
  3017. }
  3018. }
  3019. self.setSelect = setSelect;
  3020. function toggleDOM(i, onOff) {
  3021. var s = series[i];
  3022. var label = showLegend ? legendRows[i] : null;
  3023. if (s.show)
  3024. { label && remClass(label, OFF); }
  3025. else {
  3026. label && addClass(label, OFF);
  3027. cursorPts.length > 1 && trans(cursorPts[i], -10, -10, plotWidCss, plotHgtCss);
  3028. }
  3029. }
  3030. function _setScale(key, min, max) {
  3031. setScale(key, {min: min, max: max});
  3032. }
  3033. function setSeries(i, opts, pub) {
  3034. // log("setSeries()", arguments);
  3035. var s = series[i];
  3036. if (opts.focus != null)
  3037. { setFocus(i); }
  3038. if (opts.show != null) {
  3039. s.show = opts.show;
  3040. toggleDOM(i, opts.show);
  3041. _setScale(s.scale, null, null);
  3042. commit();
  3043. }
  3044. fire("setSeries", i, opts);
  3045. pub && pubSync("setSeries", self, i, opts);
  3046. }
  3047. self.setSeries = setSeries;
  3048. function setBand(bi, opts) {
  3049. assign(bands[bi], opts);
  3050. }
  3051. function addBand(opts, bi) {
  3052. opts.fill = fnOrSelf(opts.fill || null);
  3053. bi = bi == null ? bands.length : bi;
  3054. bands.splice(bi, 0, opts);
  3055. }
  3056. function delBand(bi) {
  3057. if (bi == null)
  3058. { bands.length = 0; }
  3059. else
  3060. { bands.splice(bi, 1); }
  3061. }
  3062. self.addBand = addBand;
  3063. self.setBand = setBand;
  3064. self.delBand = delBand;
  3065. function setAlpha(i, value) {
  3066. series[i].alpha = value;
  3067. if (cursor.show && cursorPts[i])
  3068. { cursorPts[i].style.opacity = value; }
  3069. if (showLegend && legendRows[i])
  3070. { legendRows[i].style.opacity = value; }
  3071. }
  3072. // y-distance
  3073. var closestDist;
  3074. var closestSeries;
  3075. var focusedSeries;
  3076. var FOCUS_TRUE = {focus: true};
  3077. var FOCUS_FALSE = {focus: false};
  3078. function setFocus(i) {
  3079. if (i != focusedSeries) {
  3080. // log("setFocus()", arguments);
  3081. var allFocused = i == null;
  3082. var _setAlpha = focus.alpha != 1;
  3083. series.forEach((s, i2) => {
  3084. var isFocused = allFocused || i2 == 0 || i2 == i;
  3085. s._focus = allFocused ? null : isFocused;
  3086. _setAlpha && setAlpha(i2, isFocused ? 1 : focus.alpha);
  3087. });
  3088. focusedSeries = i;
  3089. _setAlpha && commit();
  3090. }
  3091. }
  3092. if (showLegend && cursorFocus) {
  3093. on(mouseleave, legendEl, e => {
  3094. if (cursor._lock)
  3095. { return; }
  3096. setSeries(null, FOCUS_FALSE, syncOpts.setSeries);
  3097. updateCursor();
  3098. });
  3099. }
  3100. function posToVal(pos, scale, can) {
  3101. var sc = scales[scale];
  3102. if (can)
  3103. { pos = pos / pxRatio - (sc.ori == 1 ? plotTopCss : plotLftCss); }
  3104. var dim = plotWidCss;
  3105. if (sc.ori == 1) {
  3106. dim = plotHgtCss;
  3107. pos = dim - pos;
  3108. }
  3109. if (sc.dir == -1)
  3110. { pos = dim - pos; }
  3111. var _min = sc._min,
  3112. _max = sc._max,
  3113. pct = pos / dim;
  3114. var sv = _min + (_max - _min) * pct;
  3115. var distr = sc.distr;
  3116. return (
  3117. distr == 3 ? pow(10, sv) :
  3118. distr == 4 ? sinh(sv, sc.asinh) :
  3119. sv
  3120. );
  3121. }
  3122. function closestIdxFromXpos(pos, can) {
  3123. var v = posToVal(pos, xScaleKey, can);
  3124. return closestIdx(v, data[0], i0, i1);
  3125. }
  3126. self.valToIdx = val => closestIdx(val, data[0]);
  3127. self.posToIdx = closestIdxFromXpos;
  3128. self.posToVal = posToVal;
  3129. self.valToPos = (val, scale, can) => (
  3130. scales[scale].ori == 0 ?
  3131. getHPos(val, scales[scale],
  3132. can ? plotWid : plotWidCss,
  3133. can ? plotLft : 0
  3134. ) :
  3135. getVPos(val, scales[scale],
  3136. can ? plotHgt : plotHgtCss,
  3137. can ? plotTop : 0
  3138. )
  3139. );
  3140. // defers calling expensive functions
  3141. function batch(fn) {
  3142. fn(self);
  3143. commit();
  3144. }
  3145. self.batch = batch;
  3146. (self.setCursor = (opts, _fire) => {
  3147. mouseLeft1 = opts.left;
  3148. mouseTop1 = opts.top;
  3149. // assign(cursor, opts);
  3150. updateCursor(null, null, _fire);
  3151. });
  3152. function setSelH(off, dim) {
  3153. setStylePx(selectDiv, LEFT, select.left = off);
  3154. setStylePx(selectDiv, WIDTH, select.width = dim);
  3155. }
  3156. function setSelV(off, dim) {
  3157. setStylePx(selectDiv, TOP, select.top = off);
  3158. setStylePx(selectDiv, HEIGHT, select.height = dim);
  3159. }
  3160. var setSelX = scaleX.ori == 0 ? setSelH : setSelV;
  3161. var setSelY = scaleX.ori == 1 ? setSelH : setSelV;
  3162. function syncLegend() {
  3163. if (showLegend && legend.live) {
  3164. for (var i = 0; i < series.length; i++) {
  3165. if (i == 0 && multiValLegend)
  3166. { continue; }
  3167. var vals = legend.values[i];
  3168. var j = 0;
  3169. for (var k in vals)
  3170. { legendCells[i][j++].firstChild.nodeValue = vals[k]; }
  3171. }
  3172. }
  3173. }
  3174. function setLegend(opts, _fire) {
  3175. if (opts != null) {
  3176. var idx = opts.idx;
  3177. legend.idx = idx;
  3178. series.forEach((s, sidx) => {
  3179. (sidx > 0 || !multiValLegend) && setLegendValues(sidx, idx);
  3180. });
  3181. }
  3182. if (showLegend && legend.live)
  3183. { syncLegend(); }
  3184. shouldSetLegend = false;
  3185. _fire !== false && fire("setLegend");
  3186. }
  3187. self.setLegend = setLegend;
  3188. function setLegendValues(sidx, idx) {
  3189. var val;
  3190. if (idx == null)
  3191. { val = NULL_LEGEND_VALUES; }
  3192. else {
  3193. var s = series[sidx];
  3194. var src = sidx == 0 && xScaleDistr == 2 ? data0 : data[sidx];
  3195. val = multiValLegend ? s.values(self, sidx, idx) : {_: s.value(self, src[idx], sidx, idx)};
  3196. }
  3197. legend.values[sidx] = val;
  3198. }
  3199. function updateCursor(ts, src, _fire) {
  3200. var assign;
  3201. // ts == null && log("updateCursor()", arguments);
  3202. rawMouseLeft1 = mouseLeft1;
  3203. rawMouseTop1 = mouseTop1;
  3204. (assign = cursor.move(self, mouseLeft1, mouseTop1), mouseLeft1 = assign[0], mouseTop1 = assign[1]);
  3205. if (cursor.show) {
  3206. vCursor && trans(vCursor, round(mouseLeft1), 0, plotWidCss, plotHgtCss);
  3207. hCursor && trans(hCursor, 0, round(mouseTop1), plotWidCss, plotHgtCss);
  3208. }
  3209. var idx;
  3210. var idxChanged = false;
  3211. // when zooming to an x scale range between datapoints the binary search
  3212. // for nearest min/max indices results in this condition. cheap hack :D
  3213. var noDataInRange = i0 > i1;
  3214. closestDist = inf;
  3215. // TODO: extract
  3216. var xDim = scaleX.ori == 0 ? plotWidCss : plotHgtCss;
  3217. var yDim = scaleX.ori == 1 ? plotWidCss : plotHgtCss;
  3218. // if cursor hidden, hide points & clear legend vals
  3219. if (mouseLeft1 < 0 || dataLen == 0 || noDataInRange) {
  3220. idx = null;
  3221. for (var i = 0; i < series.length; i++) {
  3222. if (i > 0) {
  3223. cursorPts.length > 1 && trans(cursorPts[i], -10, -10, plotWidCss, plotHgtCss);
  3224. }
  3225. }
  3226. if (cursorFocus)
  3227. { setSeries(null, FOCUS_TRUE, syncOpts.setSeries); }
  3228. if (legend.live) {
  3229. idxChanged = true;
  3230. for (var i$1 = 0; i$1 < series.length; i$1++)
  3231. { legend.values[i$1] = NULL_LEGEND_VALUES; }
  3232. }
  3233. }
  3234. else {
  3235. // let pctY = 1 - (y / rect.height);
  3236. var mouseXPos = scaleX.ori == 0 ? mouseLeft1 : mouseTop1;
  3237. var valAtPosX = posToVal(mouseXPos, xScaleKey);
  3238. idx = closestIdx(valAtPosX, data[0], i0, i1);
  3239. var xPos = incrRoundUp(valToPosX(data[0][idx], scaleX, xDim, 0), 0.5);
  3240. for (var i$2 = 0; i$2 < series.length; i$2++) {
  3241. var s = series[i$2];
  3242. var idx2 = cursor.dataIdx(self, i$2, idx, valAtPosX);
  3243. cursor.idxs[i$2] = idx2;
  3244. var xPos2 = idx2 == idx ? xPos : incrRoundUp(valToPosX(data[0][idx2], scaleX, xDim, 0), 0.5);
  3245. if (i$2 > 0 && s.show) {
  3246. var valAtIdx = data[i$2][idx2];
  3247. var yPos = valAtIdx == null ? -10 : incrRoundUp(valToPosY(valAtIdx, scales[s.scale], yDim, 0), 0.5);
  3248. if (yPos > 0) {
  3249. var dist = abs(yPos - mouseTop1);
  3250. if (dist <= closestDist) {
  3251. closestDist = dist;
  3252. closestSeries = i$2;
  3253. }
  3254. }
  3255. var hPos = (void 0), vPos = (void 0);
  3256. if (scaleX.ori == 0) {
  3257. hPos = xPos2;
  3258. vPos = yPos;
  3259. }
  3260. else {
  3261. hPos = yPos;
  3262. vPos = xPos2;
  3263. }
  3264. cursorPts.length > 1 && trans(cursorPts[i$2], hPos, vPos, plotWidCss, plotHgtCss);
  3265. }
  3266. if (legend.live) {
  3267. if ((idx2 == cursor.idx && !shouldSetLegend) || i$2 == 0 && multiValLegend)
  3268. { continue; }
  3269. idxChanged = true;
  3270. setLegendValues(i$2, idx2);
  3271. }
  3272. }
  3273. }
  3274. if (idxChanged) {
  3275. legend.idx = idx;
  3276. setLegend();
  3277. }
  3278. // nit: cursor.drag.setSelect is assumed always true
  3279. if (select.show && dragging) {
  3280. if (src != null) {
  3281. var ref = syncOpts.scales;
  3282. var xKey = ref[0];
  3283. var yKey = ref[1];
  3284. var ref$1 = syncOpts.match;
  3285. var matchXKeys = ref$1[0];
  3286. var matchYKeys = ref$1[1];
  3287. var ref$2 = src.cursor.sync.scales;
  3288. var xKeySrc = ref$2[0];
  3289. var yKeySrc = ref$2[1];
  3290. // match the dragX/dragY implicitness/explicitness of src
  3291. var sdrag = src.cursor.drag;
  3292. dragX = sdrag._x;
  3293. dragY = sdrag._y;
  3294. var ref$3 = src.select;
  3295. var left = ref$3.left;
  3296. var top = ref$3.top;
  3297. var width = ref$3.width;
  3298. var height = ref$3.height;
  3299. var sori = src.scales[xKey].ori;
  3300. var sPosToVal = src.posToVal;
  3301. var sOff, sDim, sc, a, b;
  3302. var matchingX = xKey != null && matchXKeys(xKey, xKeySrc);
  3303. var matchingY = yKey != null && matchYKeys(yKey, yKeySrc);
  3304. if (matchingX) {
  3305. if (sori == 0) {
  3306. sOff = left;
  3307. sDim = width;
  3308. }
  3309. else {
  3310. sOff = top;
  3311. sDim = height;
  3312. }
  3313. if (dragX) {
  3314. sc = scales[xKey];
  3315. a = valToPosX(sPosToVal(sOff, xKeySrc), sc, xDim, 0);
  3316. b = valToPosX(sPosToVal(sOff + sDim, xKeySrc), sc, xDim, 0);
  3317. setSelX(min(a,b), abs(b-a));
  3318. }
  3319. else
  3320. { setSelX(0, xDim); }
  3321. if (!matchingY)
  3322. { setSelY(0, yDim); }
  3323. }
  3324. if (matchingY) {
  3325. if (sori == 1) {
  3326. sOff = left;
  3327. sDim = width;
  3328. }
  3329. else {
  3330. sOff = top;
  3331. sDim = height;
  3332. }
  3333. if (dragY) {
  3334. sc = scales[yKey];
  3335. a = valToPosY(sPosToVal(sOff, yKeySrc), sc, yDim, 0);
  3336. b = valToPosY(sPosToVal(sOff + sDim, yKeySrc), sc, yDim, 0);
  3337. setSelY(min(a,b), abs(b-a));
  3338. }
  3339. else
  3340. { setSelY(0, yDim); }
  3341. if (!matchingX)
  3342. { setSelX(0, xDim); }
  3343. }
  3344. }
  3345. else {
  3346. var rawDX = abs(rawMouseLeft1 - rawMouseLeft0);
  3347. var rawDY = abs(rawMouseTop1 - rawMouseTop0);
  3348. if (scaleX.ori == 1) {
  3349. var _rawDX = rawDX;
  3350. rawDX = rawDY;
  3351. rawDY = _rawDX;
  3352. }
  3353. dragX = drag.x && rawDX >= drag.dist;
  3354. dragY = drag.y && rawDY >= drag.dist;
  3355. var uni = drag.uni;
  3356. if (uni != null) {
  3357. // only calc drag status if they pass the dist thresh
  3358. if (dragX && dragY) {
  3359. dragX = rawDX >= uni;
  3360. dragY = rawDY >= uni;
  3361. // force unidirectionality when both are under uni limit
  3362. if (!dragX && !dragY) {
  3363. if (rawDY > rawDX)
  3364. { dragY = true; }
  3365. else
  3366. { dragX = true; }
  3367. }
  3368. }
  3369. }
  3370. else if (drag.x && drag.y && (dragX || dragY))
  3371. // if omni with no uni then both dragX / dragY should be true if either is true
  3372. { dragX = dragY = true; }
  3373. var p0, p1;
  3374. if (dragX) {
  3375. if (scaleX.ori == 0) {
  3376. p0 = mouseLeft0;
  3377. p1 = mouseLeft1;
  3378. }
  3379. else {
  3380. p0 = mouseTop0;
  3381. p1 = mouseTop1;
  3382. }
  3383. setSelX(min(p0, p1), abs(p1 - p0));
  3384. if (!dragY)
  3385. { setSelY(0, yDim); }
  3386. }
  3387. if (dragY) {
  3388. if (scaleX.ori == 1) {
  3389. p0 = mouseLeft0;
  3390. p1 = mouseLeft1;
  3391. }
  3392. else {
  3393. p0 = mouseTop0;
  3394. p1 = mouseTop1;
  3395. }
  3396. setSelY(min(p0, p1), abs(p1 - p0));
  3397. if (!dragX)
  3398. { setSelX(0, xDim); }
  3399. }
  3400. // the drag didn't pass the dist requirement
  3401. if (!dragX && !dragY) {
  3402. setSelX(0, 0);
  3403. setSelY(0, 0);
  3404. }
  3405. }
  3406. }
  3407. cursor.idx = idx;
  3408. cursor.left = mouseLeft1;
  3409. cursor.top = mouseTop1;
  3410. drag._x = dragX;
  3411. drag._y = dragY;
  3412. // if ts is present, means we're implicitly syncing own cursor
  3413. if (ts != null) {
  3414. if (syncKey != null) {
  3415. var ref$4 = syncOpts.scales;
  3416. var xSyncKey = ref$4[0];
  3417. var ySyncKey = ref$4[1];
  3418. syncOpts.values[0] = xSyncKey != null ? posToVal(scaleX.ori == 0 ? mouseLeft1 : mouseTop1, xSyncKey) : null;
  3419. syncOpts.values[1] = ySyncKey != null ? posToVal(scaleX.ori == 1 ? mouseLeft1 : mouseTop1, ySyncKey) : null;
  3420. }
  3421. // this is not technically a "mousemove" event, since it's debounced, rename to setCursor?
  3422. // since this is internal, we can tweak it later
  3423. pubSync(mousemove, self, mouseLeft1, mouseTop1, plotWidCss, plotHgtCss, idx);
  3424. if (cursorFocus) {
  3425. var o = syncOpts.setSeries;
  3426. var p = focus.prox;
  3427. if (focusedSeries == null) {
  3428. if (closestDist <= p)
  3429. { setSeries(closestSeries, FOCUS_TRUE, o); }
  3430. }
  3431. else {
  3432. if (closestDist > p)
  3433. { setSeries(null, FOCUS_TRUE, o); }
  3434. else if (closestSeries != focusedSeries)
  3435. { setSeries(closestSeries, FOCUS_TRUE, o); }
  3436. }
  3437. }
  3438. }
  3439. ready && _fire !== false && fire("setCursor");
  3440. }
  3441. var rect = null;
  3442. function syncRect(defer) {
  3443. if (defer === true)
  3444. { rect = null; }
  3445. else {
  3446. rect = over.getBoundingClientRect();
  3447. fire("syncRect", rect);
  3448. }
  3449. }
  3450. function mouseMove(e, src, _l, _t, _w, _h, _i) {
  3451. if (cursor._lock)
  3452. { return; }
  3453. cacheMouse(e, src, _l, _t, _w, _h, _i, false, e != null);
  3454. if (e != null)
  3455. { updateCursor(1); }
  3456. else
  3457. { updateCursor(null, src); }
  3458. }
  3459. function cacheMouse(e, src, _l, _t, _w, _h, _i, initial, snap) {
  3460. var assign;
  3461. if (rect == null)
  3462. { syncRect(false); }
  3463. if (e != null) {
  3464. _l = e.clientX - rect.left;
  3465. _t = e.clientY - rect.top;
  3466. }
  3467. else {
  3468. if (_l < 0 || _t < 0) {
  3469. mouseLeft1 = -10;
  3470. mouseTop1 = -10;
  3471. return;
  3472. }
  3473. var ref = syncOpts.scales;
  3474. var xKey = ref[0];
  3475. var yKey = ref[1];
  3476. var syncOptsSrc = src.cursor.sync;
  3477. var ref$1 = syncOptsSrc.values;
  3478. var xValSrc = ref$1[0];
  3479. var yValSrc = ref$1[1];
  3480. var ref$2 = syncOptsSrc.scales;
  3481. var xKeySrc = ref$2[0];
  3482. var yKeySrc = ref$2[1];
  3483. var ref$3 = syncOpts.match;
  3484. var matchXKeys = ref$3[0];
  3485. var matchYKeys = ref$3[1];
  3486. var rotSrc = src.scales[xKeySrc].ori == 1;
  3487. var xDim = scaleX.ori == 0 ? plotWidCss : plotHgtCss,
  3488. yDim = scaleX.ori == 1 ? plotWidCss : plotHgtCss,
  3489. _xDim = rotSrc ? _h : _w,
  3490. _yDim = rotSrc ? _w : _h,
  3491. _xPos = rotSrc ? _t : _l,
  3492. _yPos = rotSrc ? _l : _t;
  3493. if (xKeySrc != null)
  3494. { _l = matchXKeys(xKey, xKeySrc) ? getPos(xValSrc, scales[xKey], xDim, 0) : -10; }
  3495. else
  3496. { _l = xDim * (_xPos/_xDim); }
  3497. if (yKeySrc != null)
  3498. { _t = matchYKeys(yKey, yKeySrc) ? getPos(yValSrc, scales[yKey], yDim, 0) : -10; }
  3499. else
  3500. { _t = yDim * (_yPos/_yDim); }
  3501. if (scaleX.ori == 1) {
  3502. var _l$1 = _l;
  3503. _l = _t;
  3504. _t = _l$1;
  3505. }
  3506. }
  3507. if (snap) {
  3508. if (_l <= 1 || _l >= plotWidCss - 1)
  3509. { _l = incrRound(_l, plotWidCss); }
  3510. if (_t <= 1 || _t >= plotHgtCss - 1)
  3511. { _t = incrRound(_t, plotHgtCss); }
  3512. }
  3513. if (initial) {
  3514. rawMouseLeft0 = _l;
  3515. rawMouseTop0 = _t;
  3516. (assign = cursor.move(self, _l, _t), mouseLeft0 = assign[0], mouseTop0 = assign[1]);
  3517. }
  3518. else {
  3519. mouseLeft1 = _l;
  3520. mouseTop1 = _t;
  3521. }
  3522. }
  3523. function hideSelect() {
  3524. setSelect({
  3525. width: 0,
  3526. height: 0,
  3527. }, false);
  3528. }
  3529. function mouseDown(e, src, _l, _t, _w, _h, _i) {
  3530. dragging = true;
  3531. dragX = dragY = drag._x = drag._y = false;
  3532. cacheMouse(e, src, _l, _t, _w, _h, _i, true, false);
  3533. if (e != null) {
  3534. onMouse(mouseup, doc, mouseUp);
  3535. pubSync(mousedown, self, mouseLeft0, mouseTop0, plotWidCss, plotHgtCss, null);
  3536. }
  3537. }
  3538. function mouseUp(e, src, _l, _t, _w, _h, _i) {
  3539. dragging = drag._x = drag._y = false;
  3540. cacheMouse(e, src, _l, _t, _w, _h, _i, false, true);
  3541. var left = select.left;
  3542. var top = select.top;
  3543. var width = select.width;
  3544. var height = select.height;
  3545. var hasSelect = width > 0 || height > 0;
  3546. hasSelect && setSelect(select);
  3547. if (drag.setScale && hasSelect) {
  3548. // if (syncKey != null) {
  3549. // dragX = drag.x;
  3550. // dragY = drag.y;
  3551. // }
  3552. var xOff = left,
  3553. xDim = width,
  3554. yOff = top,
  3555. yDim = height;
  3556. if (scaleX.ori == 1) {
  3557. xOff = top,
  3558. xDim = height,
  3559. yOff = left,
  3560. yDim = width;
  3561. }
  3562. if (dragX) {
  3563. _setScale(xScaleKey,
  3564. posToVal(xOff, xScaleKey),
  3565. posToVal(xOff + xDim, xScaleKey)
  3566. );
  3567. }
  3568. if (dragY) {
  3569. for (var k in scales) {
  3570. var sc = scales[k];
  3571. if (k != xScaleKey && sc.from == null && sc.min != inf) {
  3572. _setScale(k,
  3573. posToVal(yOff + yDim, k),
  3574. posToVal(yOff, k)
  3575. );
  3576. }
  3577. }
  3578. }
  3579. hideSelect();
  3580. }
  3581. else if (cursor.lock) {
  3582. cursor._lock = !cursor._lock;
  3583. if (!cursor._lock)
  3584. { updateCursor(); }
  3585. }
  3586. if (e != null) {
  3587. offMouse(mouseup, doc);
  3588. pubSync(mouseup, self, mouseLeft1, mouseTop1, plotWidCss, plotHgtCss, null);
  3589. }
  3590. }
  3591. function mouseLeave(e, src, _l, _t, _w, _h, _i) {
  3592. if (!cursor._lock) {
  3593. var _dragging = dragging;
  3594. if (dragging) {
  3595. // handle case when mousemove aren't fired all the way to edges by browser
  3596. var snapH = true;
  3597. var snapV = true;
  3598. var snapProx = 10;
  3599. var dragH, dragV;
  3600. if (scaleX.ori == 0) {
  3601. dragH = dragX;
  3602. dragV = dragY;
  3603. }
  3604. else {
  3605. dragH = dragY;
  3606. dragV = dragX;
  3607. }
  3608. if (dragH && dragV) {
  3609. // maybe omni corner snap
  3610. snapH = mouseLeft1 <= snapProx || mouseLeft1 >= plotWidCss - snapProx;
  3611. snapV = mouseTop1 <= snapProx || mouseTop1 >= plotHgtCss - snapProx;
  3612. }
  3613. if (dragH && snapH)
  3614. { mouseLeft1 = mouseLeft1 < mouseLeft0 ? 0 : plotWidCss; }
  3615. if (dragV && snapV)
  3616. { mouseTop1 = mouseTop1 < mouseTop0 ? 0 : plotHgtCss; }
  3617. updateCursor(1);
  3618. dragging = false;
  3619. }
  3620. mouseLeft1 = -10;
  3621. mouseTop1 = -10;
  3622. // passing a non-null timestamp to force sync/mousemove event
  3623. updateCursor(1);
  3624. if (_dragging)
  3625. { dragging = _dragging; }
  3626. }
  3627. }
  3628. function dblClick(e, src, _l, _t, _w, _h, _i) {
  3629. autoScaleX();
  3630. hideSelect();
  3631. if (e != null)
  3632. { pubSync(dblclick, self, mouseLeft1, mouseTop1, plotWidCss, plotHgtCss, null); }
  3633. }
  3634. function syncPxRatio() {
  3635. axes.forEach(syncFontSize);
  3636. _setSize(self.width, self.height, true);
  3637. }
  3638. on(ddpxchange, win, syncPxRatio);
  3639. // internal pub/sub
  3640. var events = {};
  3641. events.mousedown = mouseDown;
  3642. events.mousemove = mouseMove;
  3643. events.mouseup = mouseUp;
  3644. events.dblclick = dblClick;
  3645. events["setSeries"] = (e, src, idx, opts) => {
  3646. setSeries(idx, opts);
  3647. };
  3648. if (cursor.show) {
  3649. onMouse(mousedown, over, mouseDown);
  3650. onMouse(mousemove, over, mouseMove);
  3651. onMouse(mouseenter, over, syncRect);
  3652. onMouse(mouseleave, over, mouseLeave);
  3653. onMouse(dblclick, over, dblClick);
  3654. cursorPlots.add(self);
  3655. self.syncRect = syncRect;
  3656. }
  3657. // external on/off
  3658. var hooks = self.hooks = opts.hooks || {};
  3659. function fire(evName, a1, a2) {
  3660. if (evName in hooks) {
  3661. hooks[evName].forEach(fn => {
  3662. fn.call(null, self, a1, a2);
  3663. });
  3664. }
  3665. }
  3666. (opts.plugins || []).forEach(p => {
  3667. for (var evName in p.hooks)
  3668. { hooks[evName] = (hooks[evName] || []).concat(p.hooks[evName]); }
  3669. });
  3670. var syncOpts = assign({
  3671. key: null,
  3672. setSeries: false,
  3673. filters: {
  3674. pub: retTrue,
  3675. sub: retTrue,
  3676. },
  3677. scales: [xScaleKey, series[1] ? series[1].scale : null],
  3678. match: [retEq, retEq],
  3679. values: [null, null],
  3680. }, cursor.sync);
  3681. (cursor.sync = syncOpts);
  3682. var syncKey = syncOpts.key;
  3683. var sync = _sync(syncKey);
  3684. function pubSync(type, src, x, y, w, h, i) {
  3685. if (syncOpts.filters.pub(type, src, x, y, w, h, i))
  3686. { sync.pub(type, src, x, y, w, h, i); }
  3687. }
  3688. sync.sub(self);
  3689. function pub(type, src, x, y, w, h, i) {
  3690. if (syncOpts.filters.sub(type, src, x, y, w, h, i))
  3691. { events[type](null, src, x, y, w, h, i); }
  3692. }
  3693. (self.pub = pub);
  3694. function destroy() {
  3695. sync.unsub(self);
  3696. cursorPlots.delete(self);
  3697. mouseListeners.clear();
  3698. off(ddpxchange, win, syncPxRatio);
  3699. root.remove();
  3700. fire("destroy");
  3701. }
  3702. self.destroy = destroy;
  3703. function _init() {
  3704. fire("init", opts, data);
  3705. setData(data || opts.data, false);
  3706. if (pendScales[xScaleKey])
  3707. { setScale(xScaleKey, pendScales[xScaleKey]); }
  3708. else
  3709. { autoScaleX(); }
  3710. _setSize(opts.width, opts.height);
  3711. updateCursor();
  3712. setSelect(select, false);
  3713. }
  3714. series.forEach(initSeries);
  3715. axes.forEach(initAxis);
  3716. if (then) {
  3717. if (then instanceof HTMLElement) {
  3718. then.appendChild(root);
  3719. _init();
  3720. }
  3721. else
  3722. { then(self, _init); }
  3723. }
  3724. else
  3725. { _init(); }
  3726. return self;
  3727. }
  3728. uPlot.assign = assign;
  3729. uPlot.fmtNum = fmtNum;
  3730. uPlot.rangeNum = rangeNum;
  3731. uPlot.rangeLog = rangeLog;
  3732. uPlot.rangeAsinh = rangeAsinh;
  3733. uPlot.orient = orient;
  3734. {
  3735. uPlot.join = join;
  3736. }
  3737. {
  3738. uPlot.fmtDate = fmtDate;
  3739. uPlot.tzDate = tzDate;
  3740. }
  3741. {
  3742. uPlot.sync = _sync;
  3743. }
  3744. {
  3745. uPlot.addGap = addGap;
  3746. uPlot.clipGaps = clipGaps;
  3747. var paths = uPlot.paths = {};
  3748. (paths.linear = linear);
  3749. (paths.stepped = stepped);
  3750. (paths.bars = bars);
  3751. (paths.spline = monotoneCubic);
  3752. }
  3753. return uPlot;
  3754. }());