u-charts.js 193 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283
  1. /*
  2. * uCharts v1.9.3.20190922
  3. * uni-app平台高性能跨全端图表,支持H5、APP、小程序(微信/支付宝/百度/头条/QQ/360)
  4. * Copyright (c) 2019 QIUN秋云 https://www.ucharts.cn All rights reserved.
  5. * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  6. *
  7. * uCharts官方网站
  8. * https://www.uCharts.cn
  9. *
  10. * 开源地址:
  11. * https://gitee.com/uCharts/uCharts
  12. *
  13. * uni-app插件市场地址:
  14. * http://ext.dcloud.net.cn/plugin?id=271
  15. *
  16. */
  17. 'use strict';
  18. var config = {
  19. yAxisWidth: 15,
  20. yAxisSplit: 5,
  21. xAxisHeight: 15,
  22. xAxisLineHeight: 15,
  23. legendHeight: 15,
  24. yAxisTitleWidth: 15,
  25. padding: [10, 10, 10, 10],
  26. pixelRatio: 1,
  27. rotate: false,
  28. columePadding: 3,
  29. fontSize: 13,
  30. //dataPointShape: ['diamond', 'circle', 'triangle', 'rect'],
  31. dataPointShape: ['circle', 'circle', 'circle', 'circle'],
  32. colors: ['#1890ff', '#2fc25b', '#facc14', '#f04864', '#8543e0', '#90ed7d'],
  33. pieChartLinePadding: 15,
  34. pieChartTextPadding: 5,
  35. xAxisTextPadding: 3,
  36. titleColor: '#333333',
  37. titleFontSize: 20,
  38. subtitleColor: '#999999',
  39. subtitleFontSize: 15,
  40. unitFontSize: 11,
  41. toolTipPadding: 3,
  42. toolTipBackground: '#000000',
  43. toolTipOpacity: 0.7,
  44. toolTipLineHeight: 20,
  45. radarLabelTextMargin: 15,
  46. gaugeLabelTextMargin: 15,
  47. // slam todo
  48. arcbarDate: '',
  49. molecularUnit: 'ka',
  50. denominatorUnit: '0',
  51. enableBottomSolid: false,
  52. };
  53. let assign = function(target, ...varArgs) {
  54. if (target == null) {
  55. throw new TypeError('Cannot convert undefined or null to object');
  56. }
  57. if (!varArgs || varArgs.length <= 0) {
  58. return target;
  59. }
  60. // 深度合并对象
  61. function deepAssign(obj1, obj2) {
  62. for (let key in obj2) {
  63. obj1[key] = obj1[key] && obj1[key].toString() === "[object Object]" ?
  64. deepAssign(obj1[key], obj2[key]) : obj1[key] = obj2[key];
  65. }
  66. return obj1;
  67. }
  68. varArgs.forEach(val => {
  69. target = deepAssign(target, val);
  70. });
  71. return target;
  72. };
  73. var util = {
  74. toFixed: function toFixed(num, limit) {
  75. limit = limit || 2;
  76. if (this.isFloat(num)) {
  77. num = num.toFixed(limit);
  78. }
  79. return num;
  80. },
  81. isFloat: function isFloat(num) {
  82. return num % 1 !== 0;
  83. },
  84. approximatelyEqual: function approximatelyEqual(num1, num2) {
  85. return Math.abs(num1 - num2) < 1e-10;
  86. },
  87. isSameSign: function isSameSign(num1, num2) {
  88. return Math.abs(num1) === num1 && Math.abs(num2) === num2 || Math.abs(num1) !== num1 && Math.abs(
  89. num2) !== num2;
  90. },
  91. isSameXCoordinateArea: function isSameXCoordinateArea(p1, p2) {
  92. return this.isSameSign(p1.x, p2.x);
  93. },
  94. isCollision: function isCollision(obj1, obj2) {
  95. obj1.end = {};
  96. obj1.end.x = obj1.start.x + obj1.width;
  97. obj1.end.y = obj1.start.y - obj1.height;
  98. obj2.end = {};
  99. obj2.end.x = obj2.start.x + obj2.width;
  100. obj2.end.y = obj2.start.y - obj2.height;
  101. var flag = obj2.start.x > obj1.end.x || obj2.end.x < obj1.start.x || obj2.end.y > obj1.start.y || obj2
  102. .start.y <
  103. obj1.end.y;
  104. return !flag;
  105. }
  106. };
  107. //兼容H5点击事件
  108. function getH5Offset(e) {
  109. e.mp = {
  110. changedTouches: []
  111. };
  112. e.mp.changedTouches.push({
  113. x: e.offsetX,
  114. y: e.offsetY
  115. });
  116. return e;
  117. }
  118. // hex 转 rgba
  119. function hexToRgb(hexValue, opc) {
  120. var rgx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  121. var hex = hexValue.replace(rgx, function(m, r, g, b) {
  122. return r + r + g + g + b + b;
  123. });
  124. var rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  125. var r = parseInt(rgb[1], 16);
  126. var g = parseInt(rgb[2], 16);
  127. var b = parseInt(rgb[3], 16);
  128. return 'rgba(' + r + ',' + g + ',' + b + ',' + opc + ')';
  129. }
  130. function findRange(num, type, limit) {
  131. if (isNaN(num)) {
  132. throw new Error('[uCharts] unvalid series data!');
  133. }
  134. limit = limit || 10;
  135. type = type ? type : 'upper';
  136. var multiple = 1;
  137. while (limit < 1) {
  138. limit *= 10;
  139. multiple *= 10;
  140. }
  141. if (type === 'upper') {
  142. num = Math.ceil(num * multiple);
  143. } else {
  144. num = Math.floor(num * multiple);
  145. }
  146. while (num % limit !== 0) {
  147. if (type === 'upper') {
  148. num++;
  149. } else {
  150. num--;
  151. }
  152. }
  153. return num / multiple;
  154. }
  155. function calCandleMA(dayArr, nameArr, colorArr, kdata) {
  156. let seriesTemp = [];
  157. for (let k = 0; k < dayArr.length; k++) {
  158. let seriesItem = {
  159. data: [],
  160. name: nameArr[k],
  161. color: colorArr[k]
  162. };
  163. for (let i = 0, len = kdata.length; i < len; i++) {
  164. if (i < dayArr[k]) {
  165. seriesItem.data.push(null);
  166. continue;
  167. }
  168. let sum = 0;
  169. for (let j = 0; j < dayArr[k]; j++) {
  170. sum += kdata[i - j][1];
  171. }
  172. seriesItem.data.push(+(sum / dayArr[k]).toFixed(3));
  173. }
  174. seriesTemp.push(seriesItem);
  175. }
  176. return seriesTemp;
  177. }
  178. function calValidDistance(self, distance, chartData, config, opts) {
  179. var dataChartAreaWidth = opts.width - opts.area[1] - opts.area[3];
  180. var dataChartWidth = chartData.eachSpacing * (opts.chartData.xAxisData.xAxisPoints.length - 1);
  181. var validDistance = distance;
  182. if (distance >= 0) {
  183. validDistance = 0;
  184. self.event.trigger('scrollLeft');
  185. } else if (Math.abs(distance) >= dataChartWidth - dataChartAreaWidth) {
  186. validDistance = dataChartAreaWidth - dataChartWidth;
  187. self.event.trigger('scrollRight');
  188. }
  189. return validDistance;
  190. }
  191. function isInAngleRange(angle, startAngle, endAngle) {
  192. function adjust(angle) {
  193. while (angle < 0) {
  194. angle += 2 * Math.PI;
  195. }
  196. while (angle > 2 * Math.PI) {
  197. angle -= 2 * Math.PI;
  198. }
  199. return angle;
  200. }
  201. angle = adjust(angle);
  202. startAngle = adjust(startAngle);
  203. endAngle = adjust(endAngle);
  204. if (startAngle > endAngle) {
  205. endAngle += 2 * Math.PI;
  206. if (angle < startAngle) {
  207. angle += 2 * Math.PI;
  208. }
  209. }
  210. return angle >= startAngle && angle <= endAngle;
  211. }
  212. function calRotateTranslate(x, y, h) {
  213. var xv = x;
  214. var yv = h - y;
  215. var transX = xv + (h - yv - xv) / Math.sqrt(2);
  216. transX *= -1;
  217. var transY = (h - yv) * (Math.sqrt(2) - 1) - (h - yv - xv) / Math.sqrt(2);
  218. return {
  219. transX: transX,
  220. transY: transY
  221. };
  222. }
  223. function createCurveControlPoints(points, i) {
  224. function isNotMiddlePoint(points, i) {
  225. if (points[i - 1] && points[i + 1]) {
  226. return points[i].y >= Math.max(points[i - 1].y, points[i + 1].y) || points[i].y <= Math.min(points[i - 1].y,
  227. points[
  228. i + 1].y);
  229. } else {
  230. return false;
  231. }
  232. }
  233. var a = 0.2;
  234. var b = 0.2;
  235. var pAx = null;
  236. var pAy = null;
  237. var pBx = null;
  238. var pBy = null;
  239. if (i < 1) {
  240. pAx = points[0].x + (points[1].x - points[0].x) * a;
  241. pAy = points[0].y + (points[1].y - points[0].y) * a;
  242. } else {
  243. pAx = points[i].x + (points[i + 1].x - points[i - 1].x) * a;
  244. pAy = points[i].y + (points[i + 1].y - points[i - 1].y) * a;
  245. }
  246. if (i > points.length - 3) {
  247. var last = points.length - 1;
  248. pBx = points[last].x - (points[last].x - points[last - 1].x) * b;
  249. pBy = points[last].y - (points[last].y - points[last - 1].y) * b;
  250. } else {
  251. pBx = points[i + 1].x - (points[i + 2].x - points[i].x) * b;
  252. pBy = points[i + 1].y - (points[i + 2].y - points[i].y) * b;
  253. }
  254. if (isNotMiddlePoint(points, i + 1)) {
  255. pBy = points[i + 1].y;
  256. }
  257. if (isNotMiddlePoint(points, i)) {
  258. pAy = points[i].y;
  259. }
  260. if (pAy >= Math.max(points[i].y, points[i + 1].y) || pAy <= Math.min(points[i].y, points[i + 1].y)) {
  261. pAy = points[i].y;
  262. }
  263. if (pBy >= Math.max(points[i].y, points[i + 1].y) || pBy <= Math.min(points[i].y, points[i + 1].y)) {
  264. pBy = points[i + 1].y;
  265. }
  266. return {
  267. ctrA: {
  268. x: pAx,
  269. y: pAy
  270. },
  271. ctrB: {
  272. x: pBx,
  273. y: pBy
  274. }
  275. };
  276. }
  277. function convertCoordinateOrigin(x, y, center) {
  278. return {
  279. x: center.x + x,
  280. y: center.y - y
  281. };
  282. }
  283. function avoidCollision(obj, target) {
  284. if (target) {
  285. // is collision test
  286. while (util.isCollision(obj, target)) {
  287. if (obj.start.x > 0) {
  288. obj.start.y--;
  289. } else if (obj.start.x < 0) {
  290. obj.start.y++;
  291. } else {
  292. if (obj.start.y > 0) {
  293. obj.start.y++;
  294. } else {
  295. obj.start.y--;
  296. }
  297. }
  298. }
  299. }
  300. return obj;
  301. }
  302. function fillSeries(series, opts, config) {
  303. var index = 0;
  304. return series.map(function(item) {
  305. if (!item.color) {
  306. item.color = config.colors[index];
  307. index = (index + 1) % config.colors.length;
  308. }
  309. if (!item.index) {
  310. item.index = 0;
  311. }
  312. if (!item.type) {
  313. item.type = opts.type;
  314. }
  315. if (typeof item.show == "undefined") {
  316. item.show = true;
  317. }
  318. if (!item.type) {
  319. item.type = opts.type;
  320. }
  321. if (!item.pointShape) {
  322. item.pointShape = "circle";
  323. }
  324. if (!item.legendShape) {
  325. switch (item.type) {
  326. case 'line':
  327. item.legendShape = "line";
  328. break;
  329. case 'column':
  330. item.legendShape = "rect";
  331. break;
  332. case 'area':
  333. item.legendShape = "triangle";
  334. break;
  335. default:
  336. item.legendShape = "circle";
  337. }
  338. }
  339. return item;
  340. });
  341. }
  342. function getDataRange(minData, maxData) {
  343. var limit = 0;
  344. var range = maxData - minData;
  345. if (range >= 10000) {
  346. limit = 1000;
  347. } else if (range >= 1000) {
  348. limit = 100;
  349. } else if (range >= 100) {
  350. limit = 10;
  351. } else if (range >= 10) {
  352. limit = 5;
  353. } else if (range >= 1) {
  354. limit = 1;
  355. } else if (range >= 0.1) {
  356. limit = 0.1;
  357. } else if (range >= 0.01) {
  358. limit = 0.01;
  359. } else if (range >= 0.001) {
  360. limit = 0.001;
  361. } else if (range >= 0.0001) {
  362. limit = 0.0001;
  363. } else if (range >= 0.00001) {
  364. limit = 0.00001;
  365. } else {
  366. limit = 0.000001;
  367. }
  368. return {
  369. minRange: findRange(minData, 'lower', limit),
  370. maxRange: findRange(maxData, 'upper', limit)
  371. };
  372. }
  373. function measureText(text) {
  374. var fontSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : config.fontSize;
  375. text = String(text);
  376. var text = text.split('');
  377. var width = 0;
  378. for (let i = 0; i < text.length; i++) {
  379. let item = text[i];
  380. if (/[a-zA-Z]/.test(item)) {
  381. width += 7;
  382. } else if (/[0-9]/.test(item)) {
  383. width += 5.5;
  384. } else if (/\./.test(item)) {
  385. width += 2.7;
  386. } else if (/-/.test(item)) {
  387. width += 3.25;
  388. } else if (/[\u4e00-\u9fa5]/.test(item)) {
  389. width += 10;
  390. } else if (/\(|\)/.test(item)) {
  391. width += 3.73;
  392. } else if (/\s/.test(item)) {
  393. width += 2.5;
  394. } else if (/%/.test(item)) {
  395. width += 8;
  396. } else {
  397. width += 10;
  398. }
  399. }
  400. return width * fontSize / 10;
  401. }
  402. function dataCombine(series) {
  403. return series.reduce(function(a, b) {
  404. return (a.data ? a.data : a).concat(b.data);
  405. }, []);
  406. }
  407. function dataCombineStack(series, len) {
  408. var sum = new Array(len);
  409. for (var j = 0; j < sum.length; j++) {
  410. sum[j] = 0;
  411. }
  412. for (var i = 0; i < series.length; i++) {
  413. for (var j = 0; j < sum.length; j++) {
  414. sum[j] += series[i].data[j];
  415. }
  416. }
  417. return series.reduce(function(a, b) {
  418. return (a.data ? a.data : a).concat(b.data).concat(sum);
  419. }, []);
  420. }
  421. function getTouches(touches, opts, e) {
  422. let x, y;
  423. if (touches.clientX) {
  424. if (opts.rotate) {
  425. y = opts.height - touches.clientX * opts.pixelRatio;
  426. x = (touches.pageY - e.currentTarget.offsetTop - (opts.height / opts.pixelRatio / 2) * (opts.pixelRatio -
  427. 1)) *
  428. opts.pixelRatio;
  429. } else {
  430. x = touches.clientX * opts.pixelRatio;
  431. y = (touches.pageY - e.currentTarget.offsetTop - (opts.height / opts.pixelRatio / 2) * (opts.pixelRatio -
  432. 1)) *
  433. opts.pixelRatio;
  434. }
  435. } else {
  436. if (opts.rotate) {
  437. y = opts.height - touches.x * opts.pixelRatio;
  438. x = touches.y * opts.pixelRatio;
  439. } else {
  440. x = touches.x * opts.pixelRatio;
  441. y = touches.y * opts.pixelRatio;
  442. }
  443. }
  444. return {
  445. x: x,
  446. y: y
  447. }
  448. }
  449. function getSeriesDataItem(series, index) {
  450. var data = [];
  451. for (let i = 0; i < series.length; i++) {
  452. let item = series[i];
  453. if (item.data[index] !== null && typeof item.data[index] !== 'undefined' && item.show) {
  454. let seriesItem = {};
  455. seriesItem.color = item.color;
  456. seriesItem.type = item.type;
  457. seriesItem.style = item.style;
  458. seriesItem.pointShape = item.pointShape;
  459. seriesItem.disableLegend = item.disableLegend;
  460. seriesItem.name = item.name;
  461. seriesItem.show = item.show;
  462. seriesItem.data = item.format ? item.format(item.data[index]) : item.data[index];
  463. data.push(seriesItem);
  464. }
  465. }
  466. return data;
  467. }
  468. function getMaxTextListLength(list) {
  469. var lengthList = list.map(function(item) {
  470. return measureText(item);
  471. });
  472. return Math.max.apply(null, lengthList);
  473. }
  474. function getRadarCoordinateSeries(length) {
  475. var eachAngle = 2 * Math.PI / length;
  476. var CoordinateSeries = [];
  477. for (var i = 0; i < length; i++) {
  478. CoordinateSeries.push(eachAngle * i);
  479. }
  480. return CoordinateSeries.map(function(item) {
  481. return -1 * item + Math.PI / 2;
  482. });
  483. }
  484. function getToolTipData(seriesData, calPoints, index, categories) {
  485. var option = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
  486. var textList = seriesData.map(function(item) {
  487. let titleText = [];
  488. if (categories) {
  489. titleText = categories;
  490. } else {
  491. titleText = item.data;
  492. }
  493. return {
  494. text: option.format ? option.format(item, titleText[index]) : item.name + ': ' + item.data,
  495. color: item.color
  496. };
  497. });
  498. var validCalPoints = [];
  499. var offset = {
  500. x: 0,
  501. y: 0
  502. };
  503. for (let i = 0; i < calPoints.length; i++) {
  504. let points = calPoints[i];
  505. if (typeof points[index] !== 'undefined' && points[index] !== null) {
  506. validCalPoints.push(points[index]);
  507. }
  508. }
  509. for (let i = 0; i < validCalPoints.length; i++) {
  510. let item = validCalPoints[i];
  511. offset.x = Math.round(item.x);
  512. offset.y += item.y;
  513. }
  514. offset.y /= validCalPoints.length;
  515. return {
  516. textList: textList,
  517. offset: offset
  518. };
  519. }
  520. function getMixToolTipData(seriesData, calPoints, index, categories) {
  521. var option = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
  522. var textList = seriesData.map(function(item) {
  523. return {
  524. text: option.format ? option.format(item, categories[index]) : item.name + ': ' + item.data,
  525. color: item.color,
  526. disableLegend: item.disableLegend ? true : false
  527. };
  528. });
  529. textList = textList.filter(function(item) {
  530. if (item.disableLegend !== true) {
  531. return item;
  532. }
  533. });
  534. var validCalPoints = [];
  535. var offset = {
  536. x: 0,
  537. y: 0
  538. };
  539. for (let i = 0; i < calPoints.length; i++) {
  540. let points = calPoints[i];
  541. if (typeof points[index] !== 'undefined' && points[index] !== null) {
  542. validCalPoints.push(points[index]);
  543. }
  544. }
  545. for (let i = 0; i < validCalPoints.length; i++) {
  546. let item = validCalPoints[i];
  547. offset.x = Math.round(item.x);
  548. offset.y += item.y;
  549. }
  550. offset.y /= validCalPoints.length;
  551. return {
  552. textList: textList,
  553. offset: offset
  554. };
  555. }
  556. function getCandleToolTipData(series, seriesData, calPoints, index, categories, extra) {
  557. var option = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : {};
  558. let upColor = extra.color.upFill;
  559. let downColor = extra.color.downFill;
  560. //颜色顺序为开盘,收盘,最低,最高
  561. let color = [upColor, upColor, downColor, upColor];
  562. var textList = [];
  563. let text0 = {
  564. text: categories[index],
  565. color: null
  566. };
  567. textList.push(text0);
  568. seriesData.map(function(item) {
  569. if (index == 0 && item.data[1] - item.data[0] < 0) {
  570. color[1] = downColor;
  571. } else {
  572. if (item.data[0] < series[index - 1][1]) {
  573. color[0] = downColor;
  574. }
  575. if (item.data[1] < item.data[0]) {
  576. color[1] = downColor;
  577. }
  578. if (item.data[2] > series[index - 1][1]) {
  579. color[2] = upColor;
  580. }
  581. if (item.data[3] < series[index - 1][1]) {
  582. color[3] = downColor;
  583. }
  584. }
  585. let text1 = {
  586. text: '开盘:' + item.data[0],
  587. color: color[0]
  588. };
  589. let text2 = {
  590. text: '收盘:' + item.data[1],
  591. color: color[1]
  592. };
  593. let text3 = {
  594. text: '最低:' + item.data[2],
  595. color: color[2]
  596. };
  597. let text4 = {
  598. text: '最高:' + item.data[3],
  599. color: color[3]
  600. };
  601. textList.push(text1, text2, text3, text4);
  602. });
  603. var validCalPoints = [];
  604. var offset = {
  605. x: 0,
  606. y: 0
  607. };
  608. for (let i = 0; i < calPoints.length; i++) {
  609. let points = calPoints[i];
  610. if (typeof points[index] !== 'undefined' && points[index] !== null) {
  611. validCalPoints.push(points[index]);
  612. }
  613. }
  614. offset.x = Math.round(validCalPoints[0][0].x);
  615. return {
  616. textList: textList,
  617. offset: offset
  618. };
  619. }
  620. function filterSeries(series) {
  621. let tempSeries = [];
  622. for (let i = 0; i < series.length; i++) {
  623. if (series[i].show == true) {
  624. tempSeries.push(series[i])
  625. }
  626. }
  627. return tempSeries;
  628. }
  629. function findCurrentIndex(currentPoints, calPoints, opts, config) {
  630. var offset = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
  631. var currentIndex = -1;
  632. var spacing = 0;
  633. let xAxisPoints = [];
  634. for (let i = 0; i < calPoints[0].length; i++) {
  635. xAxisPoints.push(calPoints[0][i].x)
  636. }
  637. // slam todo
  638. if ((opts.type == 'line' || opts.type == 'area') && opts.xAxis.boundaryGap == 'justify') {
  639. spacing = opts.chartData.eachSpacing / 2;
  640. }
  641. if (!opts.categories) {
  642. spacing = 0
  643. }
  644. if (isInExactChartArea(currentPoints, opts, config)) {
  645. xAxisPoints.forEach(function(item, index) {
  646. if (currentPoints.x + offset + spacing > item) {
  647. currentIndex = index;
  648. }
  649. });
  650. }
  651. return currentIndex;
  652. }
  653. function findLegendIndex(currentPoints, legendData, opts) {
  654. let currentIndex = -1;
  655. if (isInExactLegendArea(currentPoints, legendData.area)) {
  656. let points = legendData.points;
  657. let index = -1;
  658. for (let i = 0, len = points.length; i < len; i++) {
  659. let item = points[i];
  660. for (let j = 0; j < item.length; j++) {
  661. index += 1;
  662. let area = item[j]['area'];
  663. if (currentPoints.x > area[0] && currentPoints.x < area[2] && currentPoints.y > area[1] && currentPoints
  664. .y < area[3]) {
  665. currentIndex = index;
  666. break;
  667. }
  668. }
  669. }
  670. return currentIndex;
  671. }
  672. return currentIndex;
  673. }
  674. function isInExactLegendArea(currentPoints, area) {
  675. return currentPoints.x > area.start.x && currentPoints.x < area.end.x && currentPoints.y > area.start.y &&
  676. currentPoints.y < area.end.y;
  677. }
  678. function isInExactChartArea(currentPoints, opts, config) {
  679. return currentPoints.x <= opts.width - opts.area[1] + 10 && currentPoints.x >= opts.area[3] - 10 && currentPoints
  680. .y >=
  681. opts.area[0] && currentPoints.y <= opts.height - opts.area[2];
  682. }
  683. function findRadarChartCurrentIndex(currentPoints, radarData, count) {
  684. var eachAngleArea = 2 * Math.PI / count;
  685. var currentIndex = -1;
  686. if (isInExactPieChartArea(currentPoints, radarData.center, radarData.radius)) {
  687. var fixAngle = function fixAngle(angle) {
  688. if (angle < 0) {
  689. angle += 2 * Math.PI;
  690. }
  691. if (angle > 2 * Math.PI) {
  692. angle -= 2 * Math.PI;
  693. }
  694. return angle;
  695. };
  696. var angle = Math.atan2(radarData.center.y - currentPoints.y, currentPoints.x - radarData.center.x);
  697. angle = -1 * angle;
  698. if (angle < 0) {
  699. angle += 2 * Math.PI;
  700. }
  701. var angleList = radarData.angleList.map(function(item) {
  702. item = fixAngle(-1 * item);
  703. return item;
  704. });
  705. angleList.forEach(function(item, index) {
  706. var rangeStart = fixAngle(item - eachAngleArea / 2);
  707. var rangeEnd = fixAngle(item + eachAngleArea / 2);
  708. if (rangeEnd < rangeStart) {
  709. rangeEnd += 2 * Math.PI;
  710. }
  711. if (angle >= rangeStart && angle <= rangeEnd || angle + 2 * Math.PI >= rangeStart && angle + 2 *
  712. Math.PI <=
  713. rangeEnd) {
  714. currentIndex = index;
  715. }
  716. });
  717. }
  718. return currentIndex;
  719. }
  720. function findFunnelChartCurrentIndex(currentPoints, funnelData) {
  721. var currentIndex = -1;
  722. for (var i = 0, len = funnelData.series.length; i < len; i++) {
  723. var item = funnelData.series[i];
  724. if (currentPoints.x > item.funnelArea[0] && currentPoints.x < item.funnelArea[2] && currentPoints.y > item
  725. .funnelArea[
  726. 1] && currentPoints.y < item.funnelArea[3]) {
  727. currentIndex = i;
  728. break;
  729. }
  730. }
  731. return currentIndex;
  732. }
  733. function findWordChartCurrentIndex(currentPoints, wordData) {
  734. var currentIndex = -1;
  735. for (var i = 0, len = wordData.length; i < len; i++) {
  736. var item = wordData[i];
  737. if (currentPoints.x > item.area[0] && currentPoints.x < item.area[2] && currentPoints.y > item.area[1] &&
  738. currentPoints.y < item.area[3]) {
  739. currentIndex = i;
  740. break;
  741. }
  742. }
  743. return currentIndex;
  744. }
  745. function findMapChartCurrentIndex(currentPoints, opts) {
  746. var currentIndex = -1;
  747. var cData = opts.chartData.mapData;
  748. var data = opts.series;
  749. var tmp = pointToCoordinate(currentPoints.y, currentPoints.x, cData.bounds, cData.scale, cData.xoffset, cData
  750. .yoffset);
  751. var poi = [tmp.x, tmp.y];
  752. for (var i = 0, len = data.length; i < len; i++) {
  753. var item = data[i].geometry.coordinates;
  754. if (isPoiWithinPoly(poi, item)) {
  755. currentIndex = i;
  756. break;
  757. }
  758. }
  759. return currentIndex;
  760. }
  761. function findPieChartCurrentIndex(currentPoints, pieData) {
  762. var currentIndex = -1;
  763. if (isInExactPieChartArea(currentPoints, pieData.center, pieData.radius)) {
  764. var angle = Math.atan2(pieData.center.y - currentPoints.y, currentPoints.x - pieData.center.x);
  765. angle = -angle;
  766. for (var i = 0, len = pieData.series.length; i < len; i++) {
  767. var item = pieData.series[i];
  768. if (isInAngleRange(angle, item._start_, item._start_ + item._proportion_ * 2 * Math.PI)) {
  769. currentIndex = i;
  770. break;
  771. }
  772. }
  773. }
  774. return currentIndex;
  775. }
  776. function isInExactPieChartArea(currentPoints, center, radius) {
  777. return Math.pow(currentPoints.x - center.x, 2) + Math.pow(currentPoints.y - center.y, 2) <= Math.pow(radius, 2);
  778. }
  779. function splitPoints(points) {
  780. var newPoints = [];
  781. var items = [];
  782. points.forEach(function(item, index) {
  783. if (item !== null) {
  784. items.push(item);
  785. } else {
  786. if (items.length) {
  787. newPoints.push(items);
  788. }
  789. items = [];
  790. }
  791. });
  792. if (items.length) {
  793. newPoints.push(items);
  794. }
  795. return newPoints;
  796. }
  797. function calLegendData(series, opts, config, chartData) {
  798. let legendData = {
  799. area: {
  800. start: {
  801. x: 0,
  802. y: 0
  803. },
  804. end: {
  805. x: 0,
  806. y: 0
  807. },
  808. width: 0,
  809. height: 0,
  810. wholeWidth: 0,
  811. wholeHeight: 0
  812. },
  813. points: [],
  814. widthArr: [],
  815. heightArr: []
  816. };
  817. if (opts.legend.show === false) {
  818. chartData.legendData = legendData;
  819. return legendData;
  820. }
  821. let padding = opts.legend.padding;
  822. let margin = opts.legend.margin;
  823. let fontSize = opts.legend.fontSize;
  824. let shapeWidth = 15 * opts.pixelRatio;
  825. let shapeRight = 5 * opts.pixelRatio;
  826. let lineHeight = Math.max(opts.legend.lineHeight * opts.pixelRatio, fontSize);
  827. if (opts.legend.position == 'top' || opts.legend.position == 'bottom') {
  828. let legendList = [];
  829. let widthCount = 0;
  830. let widthCountArr = [];
  831. let currentRow = [];
  832. for (let i = 0; i < series.length; i++) {
  833. let item = series[i];
  834. let itemWidth = shapeWidth + shapeRight + measureText(item.name || 'undefined', fontSize) + opts.legend
  835. .itemGap;
  836. if (widthCount + itemWidth > opts.width - opts.padding[1] - opts.padding[3]) {
  837. legendList.push(currentRow);
  838. widthCountArr.push(widthCount - opts.legend.itemGap);
  839. widthCount = itemWidth;
  840. currentRow = [item];
  841. } else {
  842. widthCount += itemWidth;
  843. currentRow.push(item);
  844. }
  845. }
  846. if (currentRow.length) {
  847. legendList.push(currentRow);
  848. widthCountArr.push(widthCount - opts.legend.itemGap);
  849. legendData.widthArr = widthCountArr;
  850. let legendWidth = Math.max.apply(null, widthCountArr);
  851. switch (opts.legend.float) {
  852. case 'left':
  853. legendData.area.start.x = opts.padding[3];
  854. legendData.area.end.x = opts.padding[3] + 2 * padding;
  855. break;
  856. case 'right':
  857. legendData.area.start.x = opts.width - opts.padding[1] - legendWidth - 2 * padding;
  858. legendData.area.end.x = opts.width - opts.padding[1];
  859. break;
  860. default:
  861. legendData.area.start.x = (opts.width - legendWidth) / 2 - padding;
  862. legendData.area.end.x = (opts.width + legendWidth) / 2 + padding;
  863. }
  864. legendData.area.width = legendWidth + 2 * padding;
  865. legendData.area.wholeWidth = legendWidth + 2 * padding;
  866. legendData.area.height = legendList.length * lineHeight + 2 * padding;
  867. legendData.area.wholeHeight = legendList.length * lineHeight + 2 * padding + 2 * margin;
  868. legendData.points = legendList;
  869. }
  870. } else {
  871. let len = series.length;
  872. let maxHeight = opts.height - opts.padding[0] - opts.padding[2] - 2 * margin - 2 * padding;
  873. let maxLength = Math.min(Math.floor(maxHeight / lineHeight), len);
  874. legendData.area.height = maxLength * lineHeight + padding * 2;
  875. legendData.area.wholeHeight = maxLength * lineHeight + padding * 2;
  876. switch (opts.legend.float) {
  877. case 'top':
  878. legendData.area.start.y = opts.padding[0] + margin;
  879. legendData.area.end.y = opts.padding[0] + margin + legendData.area.height;
  880. break;
  881. case 'bottom':
  882. legendData.area.start.y = opts.height - opts.padding[2] - margin - legendData.area.height;
  883. legendData.area.end.y = opts.height - opts.padding[2] - margin;
  884. break;
  885. default:
  886. legendData.area.start.y = (opts.height - legendData.area.height) / 2;
  887. legendData.area.end.y = (opts.height + legendData.area.height) / 2;
  888. }
  889. let lineNum = len % maxLength === 0 ? len / maxLength : Math.floor((len / maxLength) + 1);
  890. let currentRow = [];
  891. for (let i = 0; i < lineNum; i++) {
  892. let temp = series.slice(i * maxLength, i * maxLength + maxLength);
  893. currentRow.push(temp);
  894. }
  895. legendData.points = currentRow;
  896. if (currentRow.length) {
  897. for (let i = 0; i < currentRow.length; i++) {
  898. let item = currentRow[i];
  899. let maxWidth = 0;
  900. for (let j = 0; j < item.length; j++) {
  901. let itemWidth = shapeWidth + shapeRight + measureText(item[j].name || 'undefined', fontSize) + opts
  902. .legend.itemGap;
  903. if (itemWidth > maxWidth) {
  904. maxWidth = itemWidth;
  905. }
  906. }
  907. legendData.widthArr.push(maxWidth);
  908. legendData.heightArr.push(item.length * lineHeight + padding * 2);
  909. }
  910. let legendWidth = 0
  911. for (let i = 0; i < legendData.widthArr.length; i++) {
  912. legendWidth += legendData.widthArr[i];
  913. }
  914. legendData.area.width = legendWidth - opts.legend.itemGap + 2 * padding;
  915. legendData.area.wholeWidth = legendData.area.width + padding;
  916. }
  917. }
  918. switch (opts.legend.position) {
  919. case 'top':
  920. legendData.area.start.y = opts.padding[0] + margin;
  921. legendData.area.end.y = opts.padding[0] + margin + legendData.area.height;
  922. break;
  923. case 'bottom':
  924. legendData.area.start.y = opts.height - opts.padding[2] - legendData.area.height - margin;
  925. legendData.area.end.y = opts.height - opts.padding[2] - margin;
  926. break;
  927. case 'left':
  928. legendData.area.start.x = opts.padding[3];
  929. legendData.area.end.x = opts.padding[3] + legendData.area.width;
  930. break;
  931. case 'right':
  932. legendData.area.start.x = opts.width - opts.padding[1] - legendData.area.width;
  933. legendData.area.end.x = opts.width - opts.padding[1];
  934. break;
  935. }
  936. chartData.legendData = legendData;
  937. return legendData;
  938. }
  939. function calCategoriesData(categories, opts, config, eachSpacing) {
  940. var result = {
  941. angle: 0,
  942. xAxisHeight: config.xAxisHeight
  943. };
  944. var categoriesTextLenth = categories.map(function(item) {
  945. return measureText(item, opts.xAxis.fontSize || config.fontSize);
  946. });
  947. var maxTextLength = Math.max.apply(this, categoriesTextLenth);
  948. if (opts.xAxis.rotateLabel == true && maxTextLength + 2 * config.xAxisTextPadding > eachSpacing) {
  949. result.angle = 45 * Math.PI / 180;
  950. result.xAxisHeight = 2 * config.xAxisTextPadding + maxTextLength * Math.sin(result.angle);
  951. }
  952. return result;
  953. }
  954. function getXAxisTextList(series, opts, config) {
  955. var index = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : -1;
  956. var data = dataCombine(series);
  957. var sorted = [];
  958. // remove null from data
  959. data = data.filter(function(item) {
  960. //return item !== null;
  961. if (typeof item === 'object' && item !== null) {
  962. if (item.constructor == Array) {
  963. return item !== null;
  964. } else {
  965. return item.value !== null;
  966. }
  967. } else {
  968. return item !== null;
  969. }
  970. });
  971. data.map(function(item) {
  972. if (typeof item === 'object') {
  973. if (item.constructor == Array) {
  974. if (opts.type == 'candle') {
  975. item.map(function(subitem) {
  976. sorted.push(subitem);
  977. })
  978. } else {
  979. sorted.push(item[0]);
  980. }
  981. } else {
  982. sorted.push(item.value);
  983. }
  984. } else {
  985. sorted.push(item);
  986. }
  987. })
  988. var minData = 0;
  989. var maxData = 0;
  990. if (sorted.length > 0) {
  991. minData = Math.min.apply(this, sorted);
  992. maxData = Math.max.apply(this, sorted);
  993. }
  994. //为了兼容v1.9.0之前的项目
  995. if (index > -1) {
  996. if (typeof opts.xAxis.data[index].min === 'number') {
  997. minData = Math.min(opts.xAxis.data[index].min, minData);
  998. }
  999. if (typeof opts.xAxis.data[index].max === 'number') {
  1000. maxData = Math.max(opts.xAxis.data[index].max, maxData);
  1001. }
  1002. } else {
  1003. if (typeof opts.xAxis.min === 'number') {
  1004. minData = Math.min(opts.xAxis.min, minData);
  1005. }
  1006. if (typeof opts.xAxis.max === 'number') {
  1007. maxData = Math.max(opts.xAxis.max, maxData);
  1008. }
  1009. }
  1010. if (minData === maxData) {
  1011. var rangeSpan = maxData || 10;
  1012. maxData += rangeSpan;
  1013. }
  1014. var dataRange = getDataRange(minData, maxData);
  1015. var minRange = dataRange.minRange;
  1016. var maxRange = dataRange.maxRange;
  1017. var range = [];
  1018. var eachRange = (maxRange - minRange) / opts.xAxis.splitNumber;
  1019. for (var i = 0; i <= opts.xAxis.splitNumber; i++) {
  1020. range.push(minRange + eachRange * i);
  1021. }
  1022. return range;
  1023. }
  1024. function calXAxisData(series, opts, config) {
  1025. var result = {
  1026. angle: 0,
  1027. xAxisHeight: config.xAxisHeight
  1028. };
  1029. result.ranges = getXAxisTextList(series, opts, config);
  1030. result.rangesFormat = result.ranges.map(function(item) {
  1031. item = opts.xAxis.format ? opts.xAxis.format(item) : util.toFixed(item, 2);
  1032. return item;
  1033. });
  1034. var xAxisScaleValues = result.ranges.map(function(item) {
  1035. // 如果刻度值是浮点数,则保留两位小数
  1036. item = util.toFixed(item, 2);
  1037. // 若有自定义格式则调用自定义的格式化函数
  1038. item = opts.xAxis.format ? opts.xAxis.format(Number(item)) : item;
  1039. return item;
  1040. });
  1041. result = Object.assign(result, getXAxisPoints(xAxisScaleValues, opts, config));
  1042. // 计算X轴刻度的属性譬如每个刻度的间隔,刻度的起始点\结束点以及总长
  1043. var eachSpacing = result.eachSpacing;
  1044. var textLength = xAxisScaleValues.map(function(item) {
  1045. return measureText(item);
  1046. });
  1047. // get max length of categories text
  1048. var maxTextLength = Math.max.apply(this, textLength);
  1049. // 如果刻度值文本内容过长,则将其逆时针旋转45°
  1050. if (maxTextLength + 2 * config.xAxisTextPadding > eachSpacing) {
  1051. result.angle = 45 * Math.PI / 180;
  1052. result.xAxisHeight = 2 * config.xAxisTextPadding + maxTextLength * Math.sin(result.angle);
  1053. }
  1054. if (opts.xAxis.disabled === true) {
  1055. result.xAxisHeight = 0;
  1056. }
  1057. return result;
  1058. }
  1059. function getRadarDataPoints(angleList, center, radius, series, opts) {
  1060. var process = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 1;
  1061. var radarOption = opts.extra.radar || {};
  1062. radarOption.max = radarOption.max || 0;
  1063. var maxData = Math.max(radarOption.max, Math.max.apply(null, dataCombine(series)));
  1064. var data = [];
  1065. for (let i = 0; i < series.length; i++) {
  1066. let each = series[i];
  1067. let listItem = {};
  1068. listItem.color = each.color;
  1069. listItem.legendShape = each.legendShape;
  1070. listItem.pointShape = each.pointShape;
  1071. listItem.data = [];
  1072. each.data.forEach(function(item, index) {
  1073. let tmp = {};
  1074. tmp.angle = angleList[index];
  1075. tmp.proportion = item / maxData;
  1076. tmp.position = convertCoordinateOrigin(radius * tmp.proportion * process * Math.cos(tmp.angle),
  1077. radius * tmp.proportion *
  1078. process * Math.sin(tmp.angle), center);
  1079. listItem.data.push(tmp);
  1080. });
  1081. data.push(listItem);
  1082. }
  1083. return data;
  1084. }
  1085. function getPieDataPoints(series, radius) {
  1086. var process = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
  1087. var count = 0;
  1088. var _start_ = 0;
  1089. for (let i = 0; i < series.length; i++) {
  1090. let item = series[i];
  1091. item.data = item.data === null ? 0 : item.data;
  1092. count += item.data;
  1093. }
  1094. for (let i = 0; i < series.length; i++) {
  1095. let item = series[i];
  1096. item.data = item.data === null ? 0 : item.data;
  1097. if (count === 0) {
  1098. item._proportion_ = 1 / series.length * process;
  1099. } else {
  1100. item._proportion_ = item.data / count * process;
  1101. }
  1102. item._radius_ = radius;
  1103. }
  1104. for (let i = 0; i < series.length; i++) {
  1105. let item = series[i];
  1106. item._start_ = _start_;
  1107. _start_ += 2 * item._proportion_ * Math.PI;
  1108. }
  1109. return series;
  1110. }
  1111. function getFunnelDataPoints(series, radius) {
  1112. var process = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
  1113. series = series.sort(function(a, b) {
  1114. return parseInt(b.data) - parseInt(a.data);
  1115. });
  1116. for (let i = 0; i < series.length; i++) {
  1117. series[i].radius = series[i].data / series[0].data * radius * process;
  1118. series[i]._proportion_ = series[i].data / series[0].data;
  1119. }
  1120. return series.reverse();
  1121. }
  1122. function getRoseDataPoints(series, type, minRadius, radius) {
  1123. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  1124. var count = 0;
  1125. var _start_ = 0;
  1126. var dataArr = [];
  1127. for (let i = 0; i < series.length; i++) {
  1128. let item = series[i];
  1129. item.data = item.data === null ? 0 : item.data;
  1130. count += item.data;
  1131. dataArr.push(item.data);
  1132. }
  1133. var minData = Math.min.apply(null, dataArr);
  1134. var maxData = Math.max.apply(null, dataArr);
  1135. var radiusLength = radius - minRadius;
  1136. for (let i = 0; i < series.length; i++) {
  1137. let item = series[i];
  1138. item.data = item.data === null ? 0 : item.data;
  1139. if (count === 0 || type == 'area') {
  1140. item._proportion_ = item.data / count * process;
  1141. item._rose_proportion_ = 1 / series.length * process;
  1142. } else {
  1143. item._proportion_ = item.data / count * process;
  1144. item._rose_proportion_ = item.data / count * process;
  1145. }
  1146. item._radius_ = minRadius + radiusLength * ((item.data - minData) / (maxData - minData));
  1147. }
  1148. for (let i = 0; i < series.length; i++) {
  1149. let item = series[i];
  1150. item._start_ = _start_;
  1151. _start_ += 2 * item._rose_proportion_ * Math.PI;
  1152. }
  1153. return series;
  1154. }
  1155. function getArcbarDataPoints(series, arcbarOption) {
  1156. var process = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
  1157. if (process == 1) {
  1158. process = 0.999999;
  1159. }
  1160. for (let i = 0; i < series.length; i++) {
  1161. let item = series[i];
  1162. item.data = item.data === null ? 0 : item.data;
  1163. let totalAngle;
  1164. if (arcbarOption.type == 'circle') {
  1165. totalAngle = 2;
  1166. } else {
  1167. if (arcbarOption.endAngle < arcbarOption.startAngle) {
  1168. totalAngle = 2 + arcbarOption.endAngle - arcbarOption.startAngle;
  1169. } else {
  1170. totalAngle = arcbarOption.startAngle - arcbarOption.endAngle;
  1171. }
  1172. }
  1173. item._proportion_ = totalAngle * item.data * process + arcbarOption.startAngle;
  1174. if (item._proportion_ >= 2) {
  1175. item._proportion_ = item._proportion_ % 2;
  1176. }
  1177. }
  1178. return series;
  1179. }
  1180. function getGaugeAxisPoints(categories, startAngle, endAngle) {
  1181. let totalAngle = startAngle - endAngle + 1;
  1182. let tempStartAngle = startAngle;
  1183. for (let i = 0; i < categories.length; i++) {
  1184. categories[i].value = categories[i].value === null ? 0 : categories[i].value;
  1185. categories[i]._startAngle_ = tempStartAngle;
  1186. categories[i]._endAngle_ = totalAngle * categories[i].value + startAngle;
  1187. if (categories[i]._endAngle_ >= 2) {
  1188. categories[i]._endAngle_ = categories[i]._endAngle_ % 2;
  1189. }
  1190. tempStartAngle = categories[i]._endAngle_;
  1191. }
  1192. return categories;
  1193. }
  1194. function getGaugeDataPoints(series, categories, gaugeOption) {
  1195. let process = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1;
  1196. for (let i = 0; i < series.length; i++) {
  1197. let item = series[i];
  1198. item.data = item.data === null ? 0 : item.data;
  1199. if (gaugeOption.pointer.color == 'auto') {
  1200. for (let i = 0; i < categories.length; i++) {
  1201. if (item.data <= categories[i].value) {
  1202. item.color = categories[i].color;
  1203. break;
  1204. }
  1205. }
  1206. } else {
  1207. item.color = gaugeOption.pointer.color;
  1208. }
  1209. let totalAngle = gaugeOption.startAngle - gaugeOption.endAngle + 1;
  1210. item._endAngle_ = totalAngle * item.data + gaugeOption.startAngle;
  1211. item._oldAngle_ = gaugeOption.oldAngle;
  1212. if (gaugeOption.oldAngle < gaugeOption.endAngle) {
  1213. item._oldAngle_ += 2;
  1214. }
  1215. if (item.data >= gaugeOption.oldData) {
  1216. item._proportion_ = (item._endAngle_ - item._oldAngle_) * process + gaugeOption.oldAngle;
  1217. } else {
  1218. item._proportion_ = item._oldAngle_ - (item._oldAngle_ - item._endAngle_) * process;
  1219. }
  1220. if (item._proportion_ >= 2) {
  1221. item._proportion_ = item._proportion_ % 2;
  1222. }
  1223. }
  1224. return series;
  1225. }
  1226. function getPieTextMaxLength(series) {
  1227. series = getPieDataPoints(series);
  1228. let maxLength = 0;
  1229. for (let i = 0; i < series.length; i++) {
  1230. let item = series[i];
  1231. let text = item.format ? item.format(+item._proportion_.toFixed(2)) : util.toFixed(item._proportion_ * 100) +
  1232. '%';
  1233. maxLength = Math.max(maxLength, measureText(text));
  1234. }
  1235. return maxLength;
  1236. }
  1237. function fixColumeData(points, eachSpacing, columnLen, index, config, opts) {
  1238. return points.map(function(item) {
  1239. if (item === null) {
  1240. return null;
  1241. }
  1242. item.width = Math.ceil((eachSpacing - 2 * config.columePadding) / columnLen);
  1243. if (opts.extra.column && opts.extra.column.width && +opts.extra.column.width > 0) {
  1244. item.width = Math.min(item.width, +opts.extra.column.width);
  1245. }
  1246. if (item.width <= 0) {
  1247. item.width = 1;
  1248. }
  1249. item.x += (index + 0.5 - columnLen / 2) * item.width;
  1250. return item;
  1251. });
  1252. }
  1253. function fixColumeMeterData(points, eachSpacing, columnLen, index, config, opts, border) {
  1254. return points.map(function(item) {
  1255. if (item === null) {
  1256. return null;
  1257. }
  1258. item.width = Math.ceil((eachSpacing - 2 * config.columePadding) / 2);
  1259. if (opts.extra.column && opts.extra.column.width && +opts.extra.column.width > 0) {
  1260. item.width = Math.min(item.width, +opts.extra.column.width);
  1261. }
  1262. if (index > 0) {
  1263. item.width -= 2 * border;
  1264. }
  1265. return item;
  1266. });
  1267. }
  1268. function fixColumeStackData(points, eachSpacing, columnLen, index, config, opts, series) {
  1269. return points.map(function(item, indexn) {
  1270. if (item === null) {
  1271. return null;
  1272. }
  1273. item.width = Math.ceil((eachSpacing - 2 * config.columePadding) / 2);
  1274. if (opts.extra.column && opts.extra.column.width && +opts.extra.column.width > 0) {
  1275. item.width = Math.min(item.width, +opts.extra.column.width);
  1276. }
  1277. return item;
  1278. });
  1279. }
  1280. function getXAxisPoints(categories, opts, config) {
  1281. var spacingValid = opts.width - opts.area[1] - opts.area[3];
  1282. var dataCount = opts.enableScroll ? Math.min(opts.xAxis.itemCount, categories.length) : categories.length;
  1283. if ((opts.type == 'line' || opts.type == 'area') && dataCount > 1 && opts.xAxis.boundaryGap == 'justify') {
  1284. dataCount -= 1;
  1285. }
  1286. var eachSpacing = spacingValid / dataCount;
  1287. var xAxisPoints = [];
  1288. var startX = opts.area[3];
  1289. var endX = opts.width - opts.area[1];
  1290. categories.forEach(function(item, index) {
  1291. xAxisPoints.push(startX + index * eachSpacing);
  1292. });
  1293. if (opts.xAxis.boundaryGap !== 'justify') {
  1294. if (opts.enableScroll === true) {
  1295. xAxisPoints.push(startX + categories.length * eachSpacing);
  1296. } else {
  1297. xAxisPoints.push(endX);
  1298. }
  1299. }
  1300. return {
  1301. xAxisPoints: xAxisPoints,
  1302. startX: startX,
  1303. endX: endX,
  1304. eachSpacing: eachSpacing
  1305. };
  1306. }
  1307. function getCandleDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config) {
  1308. var process = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : 1;
  1309. var points = [];
  1310. var validHeight = opts.height - opts.area[0] - opts.area[2];
  1311. data.forEach(function(item, index) {
  1312. if (item === null) {
  1313. points.push(null);
  1314. } else {
  1315. var cPoints = [];
  1316. item.forEach(function(items, indexs) {
  1317. var point = {};
  1318. point.x = xAxisPoints[index] + Math.round(eachSpacing / 2);
  1319. var value = items.value || items;
  1320. var height = validHeight * (value - minRange) / (maxRange - minRange);
  1321. height *= process;
  1322. point.y = opts.height - Math.round(height) - opts.area[2];
  1323. cPoints.push(point);
  1324. });
  1325. points.push(cPoints);
  1326. }
  1327. });
  1328. return points;
  1329. }
  1330. function getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config) {
  1331. var process = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : 1;
  1332. var boundaryGap = 'center';
  1333. if (opts.type == 'line' || opts.type == 'area') {
  1334. boundaryGap = opts.xAxis.boundaryGap;
  1335. }
  1336. var points = [];
  1337. var validHeight = opts.height - opts.area[0] - opts.area[2];
  1338. var validWidth = opts.width - opts.area[1] - opts.area[3];
  1339. data.forEach(function(item, index) {
  1340. if (item === null) {
  1341. points.push(null);
  1342. } else {
  1343. var point = {};
  1344. point.color = item.color;
  1345. point.x = xAxisPoints[index];
  1346. var value = item;
  1347. if (typeof item === 'object' && item !== null) {
  1348. if (item.constructor == Array) {
  1349. let xranges, xminRange, xmaxRange;
  1350. xranges = [].concat(opts.chartData.xAxisData.ranges);
  1351. xminRange = xranges.shift();
  1352. xmaxRange = xranges.pop();
  1353. value = item[1];
  1354. point.x = opts.area[3] + validWidth * (item[0] - xminRange) / (xmaxRange - xminRange);
  1355. } else {
  1356. value = item.value;
  1357. }
  1358. }
  1359. if (boundaryGap == 'center') {
  1360. point.x += Math.round(eachSpacing / 2);
  1361. }
  1362. var height = validHeight * (value - minRange) / (maxRange - minRange);
  1363. height *= process;
  1364. point.y = opts.height - Math.round(height) - opts.area[2];
  1365. points.push(point);
  1366. }
  1367. });
  1368. return points;
  1369. }
  1370. function getStackDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, seriesIndex,
  1371. stackSeries) {
  1372. var process = arguments.length > 9 && arguments[9] !== undefined ? arguments[9] : 1;
  1373. var points = [];
  1374. var validHeight = opts.height - opts.area[0] - opts.area[2];
  1375. data.forEach(function(item, index) {
  1376. if (item === null) {
  1377. points.push(null);
  1378. } else {
  1379. var point = {};
  1380. point.color = item.color;
  1381. point.x = xAxisPoints[index] + Math.round(eachSpacing / 2);
  1382. if (seriesIndex > 0) {
  1383. var value = 0;
  1384. for (let i = 0; i <= seriesIndex; i++) {
  1385. value += stackSeries[i].data[index];
  1386. }
  1387. var value0 = value - item;
  1388. var height = validHeight * (value - minRange) / (maxRange - minRange);
  1389. var height0 = validHeight * (value0 - minRange) / (maxRange - minRange);
  1390. } else {
  1391. var value = item;
  1392. var height = validHeight * (value - minRange) / (maxRange - minRange);
  1393. var height0 = 0;
  1394. }
  1395. var heightc = height0;
  1396. height *= process;
  1397. heightc *= process;
  1398. point.y = opts.height - Math.round(height) - opts.area[2];
  1399. point.y0 = opts.height - Math.round(heightc) - opts.area[2];
  1400. points.push(point);
  1401. }
  1402. });
  1403. return points;
  1404. }
  1405. function getYAxisTextList(series, opts, config, stack) {
  1406. var index = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : -1;
  1407. var data;
  1408. if (stack == 'stack') {
  1409. data = dataCombineStack(series, opts.categories.length);
  1410. } else {
  1411. data = dataCombine(series);
  1412. }
  1413. var sorted = [];
  1414. // remove null from data
  1415. data = data.filter(function(item) {
  1416. //return item !== null;
  1417. if (typeof item === 'object' && item !== null) {
  1418. if (item.constructor == Array) {
  1419. return item !== null;
  1420. } else {
  1421. return item.value !== null;
  1422. }
  1423. } else {
  1424. return item !== null;
  1425. }
  1426. });
  1427. data.map(function(item) {
  1428. if (typeof item === 'object') {
  1429. if (item.constructor == Array) {
  1430. if (opts.type == 'candle') {
  1431. item.map(function(subitem) {
  1432. sorted.push(subitem);
  1433. })
  1434. } else {
  1435. sorted.push(item[1]);
  1436. }
  1437. } else {
  1438. sorted.push(item.value);
  1439. }
  1440. } else {
  1441. sorted.push(item);
  1442. }
  1443. })
  1444. var minData = 0;
  1445. var maxData = 0;
  1446. if (sorted.length > 0) {
  1447. minData = Math.min.apply(this, sorted);
  1448. maxData = Math.max.apply(this, sorted);
  1449. }
  1450. //为了兼容v1.9.0之前的项目
  1451. if (index > -1) {
  1452. if (typeof opts.yAxis.data[index].min === 'number') {
  1453. minData = Math.min(opts.yAxis.data[index].min, minData);
  1454. }
  1455. if (typeof opts.yAxis.data[index].max === 'number') {
  1456. maxData = Math.max(opts.yAxis.data[index].max, maxData);
  1457. }
  1458. } else {
  1459. if (typeof opts.yAxis.min === 'number') {
  1460. minData = Math.min(opts.yAxis.min, minData);
  1461. }
  1462. if (typeof opts.yAxis.max === 'number') {
  1463. maxData = Math.max(opts.yAxis.max, maxData);
  1464. }
  1465. }
  1466. if (minData === maxData) {
  1467. var rangeSpan = maxData || 10;
  1468. maxData += rangeSpan;
  1469. }
  1470. var dataRange = getDataRange(minData, maxData);
  1471. var minRange = dataRange.minRange;
  1472. var maxRange = dataRange.maxRange;
  1473. var range = [];
  1474. var eachRange = (maxRange - minRange) / opts.yAxis.splitNumber;
  1475. for (var i = 0; i <= opts.yAxis.splitNumber; i++) {
  1476. range.push(minRange + eachRange * i);
  1477. }
  1478. return range.reverse();
  1479. }
  1480. function calYAxisData(series, opts, config) {
  1481. //堆叠图重算Y轴
  1482. var columnstyle = assign({}, {
  1483. type: ""
  1484. }, opts.extra.column);
  1485. //如果是多Y轴,重新计算
  1486. var YLength = opts.yAxis.data.length;
  1487. var newSeries = new Array(YLength);
  1488. if (YLength > 0) {
  1489. for (let i = 0; i < YLength; i++) {
  1490. newSeries[i] = [];
  1491. for (let j = 0; j < series.length; j++) {
  1492. if (series[j].index == i) {
  1493. newSeries[i].push(series[j]);
  1494. }
  1495. }
  1496. }
  1497. var rangesArr = new Array(YLength);
  1498. var rangesFormatArr = new Array(YLength);
  1499. var yAxisWidthArr = new Array(YLength);
  1500. for (let i = 0; i < YLength; i++) {
  1501. let yData = opts.yAxis.data[i];
  1502. //如果总开关不显示,强制每个Y轴为不显示
  1503. if (opts.yAxis.disabled == true) {
  1504. yData.disabled = true;
  1505. }
  1506. rangesArr[i] = getYAxisTextList(newSeries[i], opts, config, columnstyle.type, i);
  1507. let yAxisFontSizes = yData.fontSize || config.fontSize;
  1508. yAxisWidthArr[i] = {
  1509. position: yData.position ? yData.position : 'left',
  1510. width: 0
  1511. };
  1512. rangesFormatArr[i] = rangesArr[i].map(function(items) {
  1513. items = util.toFixed(items, 6);
  1514. items = yData.format ? yData.format(Number(items)) : items;
  1515. yAxisWidthArr[i].width = Math.max(yAxisWidthArr[i].width, measureText(items, yAxisFontSizes) +
  1516. 5);
  1517. return items;
  1518. });
  1519. let calibration = yData.calibration ? 4 * opts.pixelRatio : 0;
  1520. yAxisWidthArr[i].width += calibration + 3 * opts.pixelRatio;
  1521. if (yData.disabled === true) {
  1522. yAxisWidthArr[i].width = 0;
  1523. }
  1524. }
  1525. } else {
  1526. var rangesArr = new Array(1);
  1527. var rangesFormatArr = new Array(1);
  1528. var yAxisWidthArr = new Array(1);
  1529. rangesArr[0] = getYAxisTextList(series, opts, config, columnstyle.type);
  1530. yAxisWidthArr[0] = {
  1531. position: 'left',
  1532. width: 0
  1533. };
  1534. var yAxisFontSize = opts.yAxis.fontSize || config.fontSize;
  1535. rangesFormatArr[0] = rangesArr[0].map(function(item) {
  1536. item = util.toFixed(item, 6);
  1537. item = opts.yAxis.format ? opts.yAxis.format(Number(item)) : item;
  1538. yAxisWidthArr[0].width = Math.max(yAxisWidthArr[0].width, measureText(item, yAxisFontSize) + 5);
  1539. return item;
  1540. });
  1541. yAxisWidthArr[0].width += 3 * opts.pixelRatio;
  1542. if (opts.yAxis.disabled === true) {
  1543. yAxisWidthArr[0] = {
  1544. position: 'left',
  1545. width: 0
  1546. };
  1547. opts.yAxis.data[0] = {
  1548. disabled: true
  1549. };
  1550. } else {
  1551. opts.yAxis.data[0] = {
  1552. disabled: false,
  1553. position: 'left',
  1554. max: opts.yAxis.max,
  1555. min: opts.yAxis.min,
  1556. format: opts.yAxis.format
  1557. };
  1558. }
  1559. }
  1560. return {
  1561. rangesFormat: rangesFormatArr,
  1562. ranges: rangesArr,
  1563. yAxisWidth: yAxisWidthArr
  1564. };
  1565. }
  1566. function calTooltipYAxisData(point, series, opts, config, eachSpacing) {
  1567. let ranges = [].concat(opts.chartData.yAxisData.ranges);
  1568. let spacingValid = opts.height - opts.area[0] - opts.area[2];
  1569. let minAxis = opts.area[0];
  1570. let items = [];
  1571. for (let i = 0; i < ranges.length; i++) {
  1572. let maxVal = ranges[i].shift();
  1573. let minVal = ranges[i].pop();
  1574. let item = maxVal - (maxVal - minVal) * (point - minAxis) / spacingValid;
  1575. item = opts.yAxis.data[i].format ? opts.yAxis.data[i].format(Number(item)) : item.toFixed(0);
  1576. items.push(String(item))
  1577. }
  1578. return items;
  1579. }
  1580. function calMarkLineData(points, opts) {
  1581. let minRange, maxRange;
  1582. let spacingValid = opts.height - opts.area[0] - opts.area[2];
  1583. for (let i = 0; i < points.length; i++) {
  1584. points[i].yAxisIndex = points[i].yAxisIndex ? points[i].yAxisIndex : 0;
  1585. let range = [].concat(opts.chartData.yAxisData.ranges[points[i].yAxisIndex]);
  1586. minRange = range.pop();
  1587. maxRange = range.shift();
  1588. let height = spacingValid * (points[i].value - minRange) / (maxRange - minRange);
  1589. points[i].y = opts.height - Math.round(height) - opts.area[2];
  1590. }
  1591. return points;
  1592. }
  1593. function contextRotate(context, opts) {
  1594. if (opts.rotateLock !== true) {
  1595. context.translate(opts.height, 0);
  1596. context.rotate(90 * Math.PI / 180);
  1597. } else if (opts._rotate_ !== true) {
  1598. context.translate(opts.height, 0);
  1599. context.rotate(90 * Math.PI / 180);
  1600. opts._rotate_ = true;
  1601. }
  1602. }
  1603. function drawPointShape(points, color, shape, context, opts) {
  1604. context.beginPath();
  1605. if (opts.dataPointShapeType == 'hollow') {
  1606. context.setStrokeStyle(color);
  1607. context.setFillStyle(opts.background);
  1608. context.setLineWidth(2 * opts.pixelRatio);
  1609. } else {
  1610. context.setStrokeStyle("#ffffff");
  1611. context.setFillStyle(color);
  1612. context.setLineWidth(1 * opts.pixelRatio);
  1613. }
  1614. if (shape === 'diamond') {
  1615. points.forEach(function(item, index) {
  1616. if (item !== null) {
  1617. context.moveTo(item.x, item.y - 4.5);
  1618. context.lineTo(item.x - 4.5, item.y);
  1619. context.lineTo(item.x, item.y + 4.5);
  1620. context.lineTo(item.x + 4.5, item.y);
  1621. context.lineTo(item.x, item.y - 4.5);
  1622. }
  1623. });
  1624. } else if (shape === 'circle') {
  1625. points.forEach(function(item, index) {
  1626. if (item !== null) {
  1627. context.moveTo(item.x + 2.5 * opts.pixelRatio, item.y);
  1628. context.arc(item.x, item.y, 3 * opts.pixelRatio, 0, 2 * Math.PI, false);
  1629. }
  1630. });
  1631. } else if (shape === 'rect') {
  1632. points.forEach(function(item, index) {
  1633. if (item !== null) {
  1634. context.moveTo(item.x - 3.5, item.y - 3.5);
  1635. context.rect(item.x - 3.5, item.y - 3.5, 7, 7);
  1636. }
  1637. });
  1638. } else if (shape === 'triangle') {
  1639. points.forEach(function(item, index) {
  1640. if (item !== null) {
  1641. context.moveTo(item.x, item.y - 4.5);
  1642. context.lineTo(item.x - 4.5, item.y + 4.5);
  1643. context.lineTo(item.x + 4.5, item.y + 4.5);
  1644. context.lineTo(item.x, item.y - 4.5);
  1645. }
  1646. });
  1647. }
  1648. context.closePath();
  1649. context.fill();
  1650. context.stroke();
  1651. }
  1652. function drawMyRingTitle(opts, config, context, center) {
  1653. var titlefontSize = opts.title.fontSize || config.titleFontSize;
  1654. var subtitlefontSize = opts.subtitle.fontSize || config.subtitleFontSize;
  1655. var unitFontSize = opts.title.unitFontSize || config.unitFontSize;
  1656. var title = opts.title.name || '0';
  1657. var subtitle = opts.subtitle.name || '';
  1658. var titleFontColor = opts.title.color || config.titleColor;
  1659. var subtitleFontColor = opts.subtitle.color || config.subtitleColor;
  1660. var titleHeight = title ? titlefontSize : 0;
  1661. var subtitleHeight = subtitle ? subtitlefontSize : 0;
  1662. var margin = 5;
  1663. //slam todo
  1664. var arcbarDate = opts.title.arcbarDate || config.arcbarDate;
  1665. // console.log("arcbarDate=",arcbarDate);
  1666. var molecularUnit = opts.title.molecularUnit || config.molecularUnit;
  1667. var denominatorUnit = opts.title.denominatorUnit || config.denominatorUnit;
  1668. if (subtitle) {
  1669. var textWidth = measureText(subtitle, subtitlefontSize);
  1670. var startX = center.x - textWidth / 2 + (opts.subtitle.offsetX || 0);
  1671. var startY = center.y + subtitlefontSize / 2 + (opts.subtitle.offsetY || 0);
  1672. if (title) {
  1673. startY += (titleHeight + margin) / 2;
  1674. }
  1675. context.beginPath();
  1676. context.setFontSize(subtitlefontSize);
  1677. context.setFillStyle(subtitleFontColor);
  1678. context.fillText(subtitle, startX, startY);
  1679. context.closePath();
  1680. context.stroke();
  1681. }
  1682. //为零时候显示零
  1683. if (title) {
  1684. var _textWidth = measureText(title, titlefontSize);
  1685. var _startX = center.x - _textWidth / 2 + (opts.title.offsetX || 0);
  1686. var _startY = center.y + titlefontSize / 2 + (opts.title.offsetY || 0);
  1687. if (subtitle) {
  1688. _startY -= (subtitleHeight + margin) / 2;
  1689. }
  1690. var _moleWidth = measureText(molecularUnit, titlefontSize * 0.39);
  1691. var _moleStartX = _startX + _textWidth;
  1692. var _denomiStartX = _moleStartX + _moleWidth;
  1693. // 日期的位置
  1694. var _textDateWidth = measureText(arcbarDate, unitFontSize);
  1695. var _dateStartX = center.x - _textDateWidth / 2 + 5;
  1696. var _dateY = 30;
  1697. context.beginPath();
  1698. context.setFontSize(titlefontSize);
  1699. context.setFillStyle(titleFontColor);
  1700. context.fillText(title, _startX, _startY - _dateY);
  1701. context.closePath();
  1702. context.stroke();
  1703. context.beginPath();
  1704. context.setFontSize(unitFontSize);
  1705. context.setFillStyle(titleFontColor);
  1706. // context.fillText(arcbarDate, _dateStartX, _startY-40);
  1707. //+"/"+denominatorUnit
  1708. context.fillText(molecularUnit + "/" + denominatorUnit, _moleStartX + 5, _startY - _dateY);
  1709. // context.fillText("/", _denomiStartX + 5, _startY - _dateY);
  1710. // context.fillText(denominatorUnit, _denomiStartX + 10, _startY - _dateY);
  1711. context.closePath();
  1712. context.stroke();
  1713. }
  1714. // console.log("molecularUnit", molecularUnit);
  1715. }
  1716. function drawRingTitle(opts, config, context, center) {
  1717. var titlefontSize = opts.title.fontSize || config.titleFontSize;
  1718. var subtitlefontSize = opts.subtitle.fontSize || config.subtitleFontSize;
  1719. var title = opts.title.name || '';
  1720. var subtitle = opts.subtitle.name || '';
  1721. var titleFontColor = opts.title.color || config.titleColor;
  1722. var subtitleFontColor = opts.subtitle.color || config.subtitleColor;
  1723. var titleHeight = title ? titlefontSize : 0;
  1724. var subtitleHeight = subtitle ? subtitlefontSize : 0;
  1725. var margin = 5;
  1726. if (subtitle) {
  1727. var textWidth = measureText(subtitle, subtitlefontSize);
  1728. var startX = center.x - textWidth / 2 + (opts.subtitle.offsetX || 0);
  1729. var startY = center.y + subtitlefontSize / 2 + (opts.subtitle.offsetY || 0);
  1730. if (title) {
  1731. startY += (titleHeight + margin) / 2;
  1732. }
  1733. context.beginPath();
  1734. context.setFontSize(subtitlefontSize);
  1735. context.setFillStyle(subtitleFontColor);
  1736. context.fillText(subtitle, startX, startY);
  1737. context.closePath();
  1738. context.stroke();
  1739. }
  1740. if (title) {
  1741. var _textWidth = measureText(title, titlefontSize);
  1742. var _startX = center.x - _textWidth / 2 + (opts.title.offsetX || 0);
  1743. var _startY = center.y + titlefontSize / 2 + (opts.title.offsetY || 0);
  1744. if (subtitle) {
  1745. _startY -= (subtitleHeight + margin) / 2;
  1746. }
  1747. context.beginPath();
  1748. context.setFontSize(titlefontSize);
  1749. context.setFillStyle(titleFontColor);
  1750. context.fillText(title, _startX, _startY);
  1751. context.closePath();
  1752. context.stroke();
  1753. }
  1754. }
  1755. function drawPointText(points, series, config, context) {
  1756. // 绘制数据文案
  1757. var data = series.data;
  1758. points.forEach(function(item, index) {
  1759. if (item !== null) {
  1760. //var formatVal = series.format ? series.format(data[index]) : data[index];
  1761. context.beginPath();
  1762. context.setFontSize(series.textSize || config.fontSize);
  1763. context.setFillStyle(series.textColor || '#666666');
  1764. var value = data[index]
  1765. if (typeof data[index] === 'object' && data[index] !== null) {
  1766. if (data[index].constructor == Array) {
  1767. value = data[index][1];
  1768. } else {
  1769. value = data[index].value
  1770. }
  1771. }
  1772. var formatVal = series.format ? series.format(value) : value;
  1773. context.fillText(String(formatVal), item.x - measureText(formatVal, series.textSize || config
  1774. .fontSize) / 2, item.y -
  1775. 4);
  1776. context.closePath();
  1777. context.stroke();
  1778. }
  1779. });
  1780. }
  1781. function drawGaugeLabel(gaugeOption, radius, centerPosition, opts, config, context) {
  1782. radius -= gaugeOption.width / 2 + config.gaugeLabelTextMargin;
  1783. let totalAngle = gaugeOption.startAngle - gaugeOption.endAngle + 1;
  1784. let splitAngle = totalAngle / gaugeOption.splitLine.splitNumber;
  1785. let totalNumber = gaugeOption.endNumber - gaugeOption.startNumber;
  1786. let splitNumber = totalNumber / gaugeOption.splitLine.splitNumber;
  1787. let nowAngle = gaugeOption.startAngle;
  1788. let nowNumber = gaugeOption.startNumber;
  1789. for (let i = 0; i < gaugeOption.splitLine.splitNumber + 1; i++) {
  1790. var pos = {
  1791. x: radius * Math.cos(nowAngle * Math.PI),
  1792. y: radius * Math.sin(nowAngle * Math.PI)
  1793. };
  1794. var labelText = gaugeOption.labelFormat ? gaugeOption.labelFormat(nowNumber) : nowNumber;
  1795. pos.x += centerPosition.x - measureText(labelText) / 2;
  1796. pos.y += centerPosition.y;
  1797. var startX = pos.x;
  1798. var startY = pos.y;
  1799. context.beginPath();
  1800. context.setFontSize(config.fontSize);
  1801. context.setFillStyle(gaugeOption.labelColor || '#666666');
  1802. context.fillText(labelText, startX, startY + config.fontSize / 2);
  1803. context.closePath();
  1804. context.stroke();
  1805. nowAngle += splitAngle;
  1806. if (nowAngle >= 2) {
  1807. nowAngle = nowAngle % 2;
  1808. }
  1809. nowNumber += splitNumber;
  1810. }
  1811. }
  1812. function drawRadarLabel(angleList, radius, centerPosition, opts, config, context) {
  1813. var radarOption = opts.extra.radar || {};
  1814. radius += config.radarLabelTextMargin;
  1815. angleList.forEach(function(angle, index) {
  1816. var pos = {
  1817. x: radius * Math.cos(angle),
  1818. y: radius * Math.sin(angle)
  1819. };
  1820. var posRelativeCanvas = convertCoordinateOrigin(pos.x, pos.y, centerPosition);
  1821. var startX = posRelativeCanvas.x;
  1822. var startY = posRelativeCanvas.y;
  1823. if (util.approximatelyEqual(pos.x, 0)) {
  1824. startX -= measureText(opts.categories[index] || '') / 2;
  1825. } else if (pos.x < 0) {
  1826. startX -= measureText(opts.categories[index] || '');
  1827. }
  1828. context.beginPath();
  1829. context.setFontSize(config.fontSize);
  1830. context.setFillStyle(radarOption.labelColor || '#666666');
  1831. context.fillText(opts.categories[index] || '', startX, startY + config.fontSize / 2);
  1832. context.closePath();
  1833. context.stroke();
  1834. });
  1835. }
  1836. function drawPieText(series, opts, config, context, radius, center) {
  1837. var lineRadius = config.pieChartLinePadding;
  1838. var textObjectCollection = [];
  1839. var lastTextObject = null;
  1840. var seriesConvert = series.map(function(item) {
  1841. var text = item.format ? item.format(+item._proportion_.toFixed(2)) : util.toFixed(item._proportion_
  1842. .toFixed(4) *
  1843. 100) + '%';
  1844. if (item._rose_proportion_) item._proportion_ = item._rose_proportion_;
  1845. var arc = 2 * Math.PI - (item._start_ + 2 * Math.PI * item._proportion_ / 2);
  1846. var color = item.color;
  1847. var radius = item._radius_;
  1848. return {
  1849. arc: arc,
  1850. text: text,
  1851. color: color,
  1852. radius: radius,
  1853. textColor: item.textColor,
  1854. textSize: item.textSize,
  1855. };
  1856. });
  1857. for (let i = 0; i < seriesConvert.length; i++) {
  1858. let item = seriesConvert[i];
  1859. // line end
  1860. let orginX1 = Math.cos(item.arc) * (item.radius + lineRadius);
  1861. let orginY1 = Math.sin(item.arc) * (item.radius + lineRadius);
  1862. // line start
  1863. let orginX2 = Math.cos(item.arc) * item.radius;
  1864. let orginY2 = Math.sin(item.arc) * item.radius;
  1865. // text start
  1866. let orginX3 = orginX1 >= 0 ? orginX1 + config.pieChartTextPadding : orginX1 - config.pieChartTextPadding;
  1867. let orginY3 = orginY1;
  1868. let textWidth = measureText(item.text, item.textSize || config.fontSize);
  1869. let startY = orginY3;
  1870. if (lastTextObject && util.isSameXCoordinateArea(lastTextObject.start, {
  1871. x: orginX3
  1872. })) {
  1873. if (orginX3 > 0) {
  1874. startY = Math.min(orginY3, lastTextObject.start.y);
  1875. } else if (orginX1 < 0) {
  1876. startY = Math.max(orginY3, lastTextObject.start.y);
  1877. } else {
  1878. if (orginY3 > 0) {
  1879. startY = Math.max(orginY3, lastTextObject.start.y);
  1880. } else {
  1881. startY = Math.min(orginY3, lastTextObject.start.y);
  1882. }
  1883. }
  1884. }
  1885. if (orginX3 < 0) {
  1886. orginX3 -= textWidth;
  1887. }
  1888. let textObject = {
  1889. lineStart: {
  1890. x: orginX2,
  1891. y: orginY2
  1892. },
  1893. lineEnd: {
  1894. x: orginX1,
  1895. y: orginY1
  1896. },
  1897. start: {
  1898. x: orginX3,
  1899. y: startY
  1900. },
  1901. width: textWidth,
  1902. height: config.fontSize,
  1903. text: item.text,
  1904. color: item.color,
  1905. textColor: item.textColor,
  1906. textSize: item.textSize
  1907. };
  1908. lastTextObject = avoidCollision(textObject, lastTextObject);
  1909. textObjectCollection.push(lastTextObject);
  1910. }
  1911. for (let i = 0; i < textObjectCollection.length; i++) {
  1912. let item = textObjectCollection[i];
  1913. let lineStartPoistion = convertCoordinateOrigin(item.lineStart.x, item.lineStart.y, center);
  1914. let lineEndPoistion = convertCoordinateOrigin(item.lineEnd.x, item.lineEnd.y, center);
  1915. let textPosition = convertCoordinateOrigin(item.start.x, item.start.y, center);
  1916. context.setLineWidth(1 * opts.pixelRatio);
  1917. context.setFontSize(config.fontSize);
  1918. context.beginPath();
  1919. context.setStrokeStyle(item.color);
  1920. context.setFillStyle(item.color);
  1921. context.moveTo(lineStartPoistion.x, lineStartPoistion.y);
  1922. let curveStartX = item.start.x < 0 ? textPosition.x + item.width : textPosition.x;
  1923. let textStartX = item.start.x < 0 ? textPosition.x - 5 : textPosition.x + 5;
  1924. context.quadraticCurveTo(lineEndPoistion.x, lineEndPoistion.y, curveStartX, textPosition.y);
  1925. context.moveTo(lineStartPoistion.x, lineStartPoistion.y);
  1926. context.stroke();
  1927. context.closePath();
  1928. context.beginPath();
  1929. context.moveTo(textPosition.x + item.width, textPosition.y);
  1930. context.arc(curveStartX, textPosition.y, 2, 0, 2 * Math.PI);
  1931. context.closePath();
  1932. context.fill();
  1933. context.beginPath();
  1934. context.setFontSize(item.textSize || config.fontSize);
  1935. context.setFillStyle(item.textColor || '#666666');
  1936. context.fillText(item.text, textStartX, textPosition.y + 3);
  1937. context.closePath();
  1938. context.stroke();
  1939. context.closePath();
  1940. }
  1941. }
  1942. function drawToolTipSplitLine(offsetX, opts, config, context) {
  1943. var toolTipOption = opts.extra.tooltip || {};
  1944. toolTipOption.gridType = toolTipOption.gridType == undefined ? 'solid' : toolTipOption.gridType;
  1945. toolTipOption.dashLength = toolTipOption.dashLength == undefined ? 4 : toolTipOption.dashLength;
  1946. var startY = opts.area[0];
  1947. var endY = opts.height - opts.area[2];
  1948. if (toolTipOption.gridType == 'dash') {
  1949. context.setLineDash([toolTipOption.dashLength, toolTipOption.dashLength]);
  1950. }
  1951. context.setStrokeStyle(toolTipOption.gridColor || '#cccccc');
  1952. context.setLineWidth(1 * opts.pixelRatio);
  1953. context.beginPath();
  1954. context.moveTo(offsetX, startY);
  1955. context.lineTo(offsetX, endY);
  1956. context.stroke();
  1957. context.setLineDash([]);
  1958. if (toolTipOption.xAxisLabel) {
  1959. let labelText = opts.categories[opts.tooltip.index];
  1960. context.setFontSize(config.fontSize);
  1961. let textWidth = measureText(labelText, config.fontSize);
  1962. let textX = offsetX - 0.5 * textWidth;
  1963. let textY = endY;
  1964. context.beginPath();
  1965. context.setFillStyle(hexToRgb(toolTipOption.labelBgColor || config.toolTipBackground, toolTipOption
  1966. .labelBgOpacity ||
  1967. config.toolTipOpacity));
  1968. context.setStrokeStyle(toolTipOption.labelBgColor || config.toolTipBackground);
  1969. context.setLineWidth(1 * opts.pixelRatio);
  1970. context.rect(textX - config.toolTipPadding, textY, textWidth + 2 * config.toolTipPadding, config.fontSize + 2 *
  1971. config.toolTipPadding);
  1972. context.closePath();
  1973. context.stroke();
  1974. context.fill();
  1975. context.beginPath();
  1976. context.setFontSize(config.fontSize);
  1977. context.setFillStyle(toolTipOption.labelFontColor || config.fontColor);
  1978. context.fillText(String(labelText), textX, textY + config.toolTipPadding + config.fontSize);
  1979. context.closePath();
  1980. context.stroke();
  1981. }
  1982. }
  1983. function drawMarkLine(opts, config, context) {
  1984. let markLineOption = assign({}, {
  1985. type: 'solid',
  1986. dashLength: 4,
  1987. data: []
  1988. }, opts.extra.markLine);
  1989. let startX = opts.area[3];
  1990. let endX = opts.width - opts.area[1];
  1991. let points = calMarkLineData(markLineOption.data, opts);
  1992. for (let i = 0; i < points.length; i++) {
  1993. let item = assign({}, {
  1994. lineColor: '#DE4A42',
  1995. showLabel: false,
  1996. labelFontColor: '#666666',
  1997. labelBgColor: '#DFE8FF',
  1998. labelBgOpacity: 0.8,
  1999. yAxisIndex: 0
  2000. }, points[i]);
  2001. if (markLineOption.type == 'dash') {
  2002. context.setLineDash([markLineOption.dashLength, markLineOption.dashLength]);
  2003. }
  2004. context.setStrokeStyle(item.lineColor);
  2005. context.setLineWidth(1 * opts.pixelRatio);
  2006. context.beginPath();
  2007. context.moveTo(startX, item.y);
  2008. context.lineTo(endX, item.y);
  2009. context.stroke();
  2010. context.setLineDash([]);
  2011. if (item.showLabel) {
  2012. let labelText = opts.yAxis.format ? opts.yAxis.format(Number(item.value)) : item.value;
  2013. context.setFontSize(config.fontSize);
  2014. let textWidth = measureText(labelText, config.fontSize);
  2015. let bgStartX = opts.padding[3] + config.yAxisTitleWidth - config.toolTipPadding;
  2016. let bgEndX = Math.max(opts.area[3], textWidth + config.toolTipPadding * 2);
  2017. let bgWidth = bgEndX - bgStartX;
  2018. let textX = bgStartX + (bgWidth - textWidth) / 2;
  2019. let textY = item.y;
  2020. context.setFillStyle(hexToRgb(item.labelBgColor, item.labelBgOpacity));
  2021. context.setStrokeStyle(item.labelBgColor);
  2022. context.setLineWidth(1 * opts.pixelRatio);
  2023. context.beginPath();
  2024. context.rect(bgStartX, textY - 0.5 * config.fontSize - config.toolTipPadding, bgWidth, config.fontSize + 2 *
  2025. config.toolTipPadding);
  2026. context.closePath();
  2027. context.stroke();
  2028. context.fill();
  2029. context.beginPath();
  2030. context.setFontSize(config.fontSize);
  2031. context.setFillStyle(item.labelFontColor);
  2032. context.fillText(String(labelText), textX, textY + 0.5 * config.fontSize);
  2033. context.stroke();
  2034. }
  2035. }
  2036. }
  2037. function drawToolTipHorizentalLine(opts, config, context, eachSpacing, xAxisPoints) {
  2038. var toolTipOption = assign({}, {
  2039. gridType: 'solid',
  2040. dashLength: 4
  2041. }, opts.extra.tooltip);
  2042. var startX = opts.area[3];
  2043. var endX = opts.width - opts.area[1];
  2044. if (toolTipOption.gridType == 'dash') {
  2045. context.setLineDash([toolTipOption.dashLength, toolTipOption.dashLength]);
  2046. }
  2047. context.setStrokeStyle(toolTipOption.gridColor || '#cccccc');
  2048. context.setLineWidth(1 * opts.pixelRatio);
  2049. context.beginPath();
  2050. context.moveTo(startX, opts.tooltip.offset.y);
  2051. context.lineTo(endX, opts.tooltip.offset.y);
  2052. context.stroke();
  2053. context.setLineDash([]);
  2054. if (toolTipOption.yAxisLabel) {
  2055. let labelText = calTooltipYAxisData(opts.tooltip.offset.y, opts.series, opts, config, eachSpacing);
  2056. let widthArr = opts.chartData.yAxisData.yAxisWidth;
  2057. let tStartLeft = opts.area[3];
  2058. let tStartRight = opts.width - opts.area[1];
  2059. for (let i = 0; i < labelText.length; i++) {
  2060. context.setFontSize(config.fontSize);
  2061. let textWidth = measureText(labelText[i], config.fontSize);
  2062. let bgStartX, bgEndX, bgWidth;
  2063. if (widthArr[i].position == 'left') {
  2064. bgStartX = tStartLeft - widthArr[i].width;
  2065. bgEndX = Math.max(bgStartX, bgStartX + textWidth + config.toolTipPadding * 2);
  2066. } else {
  2067. bgStartX = tStartRight;
  2068. bgEndX = Math.max(bgStartX + widthArr[i].width, bgStartX + textWidth + config.toolTipPadding * 2);
  2069. }
  2070. bgWidth = bgEndX - bgStartX;
  2071. let textX = bgStartX + (bgWidth - textWidth) / 2;
  2072. let textY = opts.tooltip.offset.y;
  2073. context.beginPath();
  2074. context.setFillStyle(hexToRgb(toolTipOption.labelBgColor || config.toolTipBackground, toolTipOption
  2075. .labelBgOpacity ||
  2076. config.toolTipOpacity));
  2077. context.setStrokeStyle(toolTipOption.labelBgColor || config.toolTipBackground);
  2078. context.setLineWidth(1 * opts.pixelRatio);
  2079. context.rect(bgStartX, textY - 0.5 * config.fontSize - config.toolTipPadding, bgWidth, config.fontSize + 2 *
  2080. config.toolTipPadding);
  2081. context.closePath();
  2082. context.stroke();
  2083. context.fill();
  2084. context.beginPath();
  2085. context.setFontSize(config.fontSize);
  2086. context.setFillStyle(toolTipOption.labelFontColor || config.fontColor);
  2087. context.fillText(labelText[i], textX, textY + 0.5 * config.fontSize);
  2088. context.closePath();
  2089. context.stroke();
  2090. if (widthArr[i].position == 'left') {
  2091. tStartLeft -= (widthArr[i].width + opts.yAxis.padding);
  2092. } else {
  2093. tStartRight += widthArr[i].width + opts.yAxis.padding;
  2094. }
  2095. }
  2096. }
  2097. }
  2098. function drawToolTipSplitArea(offsetX, opts, config, context, eachSpacing) {
  2099. var toolTipOption = assign({}, {
  2100. activeBgColor: '#000000',
  2101. activeBgOpacity: 0.08
  2102. }, opts.extra.tooltip);
  2103. var startY = opts.area[0];
  2104. var endY = opts.height - opts.area[2];
  2105. context.beginPath();
  2106. context.setFillStyle(hexToRgb(toolTipOption.activeBgColor, toolTipOption.activeBgOpacity));
  2107. context.rect(offsetX - eachSpacing / 2, startY, eachSpacing, endY - startY);
  2108. context.closePath();
  2109. context.fill();
  2110. }
  2111. function drawToolTip(textList, offset, opts, config, context, eachSpacing, xAxisPoints) {
  2112. var toolTipOption = assign({}, {
  2113. showBox: true,
  2114. bgColor: '#000000',
  2115. bgOpacity: 0.7,
  2116. fontColor: '#FFFFFF'
  2117. }, opts.extra.tooltip);
  2118. var legendWidth = 4 * opts.pixelRatio;
  2119. var legendMarginRight = 5 * opts.pixelRatio;
  2120. var arrowWidth = 8 * opts.pixelRatio;
  2121. var isOverRightBorder = false;
  2122. if (opts.type == 'line' || opts.type == 'area' || opts.type == 'candle' || opts.type == 'mix') {
  2123. drawToolTipSplitLine(opts.tooltip.offset.x, opts, config, context);
  2124. }
  2125. offset = assign({
  2126. x: 0,
  2127. y: 0
  2128. }, offset);
  2129. offset.y -= 8 * opts.pixelRatio;
  2130. var textWidth = textList.map(function(item) {
  2131. return measureText(item.text, config.fontSize);
  2132. });
  2133. var toolTipWidth = legendWidth + legendMarginRight + 4 * config.toolTipPadding + Math.max.apply(null, textWidth);
  2134. var toolTipHeight = 2 * config.toolTipPadding + textList.length * config.toolTipLineHeight;
  2135. if (toolTipOption.showBox == false) {
  2136. return
  2137. }
  2138. // if beyond the right border
  2139. if (offset.x - Math.abs(opts._scrollDistance_) + arrowWidth + toolTipWidth > opts.width) {
  2140. isOverRightBorder = true;
  2141. }
  2142. if (toolTipHeight + offset.y > opts.height) {
  2143. offset.y = opts.height - toolTipHeight;
  2144. }
  2145. // draw background rect
  2146. context.beginPath();
  2147. context.setFillStyle(hexToRgb(toolTipOption.bgColor || config.toolTipBackground, toolTipOption.bgOpacity || config
  2148. .toolTipOpacity));
  2149. if (isOverRightBorder) {
  2150. context.moveTo(offset.x, offset.y + 10 * opts.pixelRatio);
  2151. context.lineTo(offset.x - arrowWidth, offset.y + 10 * opts.pixelRatio - 5 * opts.pixelRatio);
  2152. context.lineTo(offset.x - arrowWidth, offset.y);
  2153. context.lineTo(offset.x - arrowWidth - Math.round(toolTipWidth), offset.y);
  2154. context.lineTo(offset.x - arrowWidth - Math.round(toolTipWidth), offset.y + toolTipHeight);
  2155. context.lineTo(offset.x - arrowWidth, offset.y + toolTipHeight);
  2156. context.lineTo(offset.x - arrowWidth, offset.y + 10 * opts.pixelRatio + 5 * opts.pixelRatio);
  2157. context.lineTo(offset.x, offset.y + 10 * opts.pixelRatio);
  2158. } else {
  2159. context.moveTo(offset.x, offset.y + 10 * opts.pixelRatio);
  2160. context.lineTo(offset.x + arrowWidth, offset.y + 10 * opts.pixelRatio - 5 * opts.pixelRatio);
  2161. context.lineTo(offset.x + arrowWidth, offset.y);
  2162. context.lineTo(offset.x + arrowWidth + Math.round(toolTipWidth), offset.y);
  2163. context.lineTo(offset.x + arrowWidth + Math.round(toolTipWidth), offset.y + toolTipHeight);
  2164. context.lineTo(offset.x + arrowWidth, offset.y + toolTipHeight);
  2165. context.lineTo(offset.x + arrowWidth, offset.y + 10 * opts.pixelRatio + 5 * opts.pixelRatio);
  2166. context.lineTo(offset.x, offset.y + 10 * opts.pixelRatio);
  2167. }
  2168. context.closePath();
  2169. context.fill();
  2170. // draw legend
  2171. textList.forEach(function(item, index) {
  2172. if (item.color !== null) {
  2173. context.beginPath();
  2174. context.setFillStyle(item.color);
  2175. var startX = offset.x + arrowWidth + 2 * config.toolTipPadding;
  2176. var startY = offset.y + (config.toolTipLineHeight - config.fontSize) / 2 + config
  2177. .toolTipLineHeight * index +
  2178. config.toolTipPadding + 1;
  2179. if (isOverRightBorder) {
  2180. startX = offset.x - toolTipWidth - arrowWidth + 2 * config.toolTipPadding;
  2181. }
  2182. context.fillRect(startX, startY, legendWidth, config.fontSize);
  2183. context.closePath();
  2184. }
  2185. });
  2186. // draw text list
  2187. textList.forEach(function(item, index) {
  2188. var startX = offset.x + arrowWidth + 2 * config.toolTipPadding + legendWidth + legendMarginRight;
  2189. if (isOverRightBorder) {
  2190. startX = offset.x - toolTipWidth - arrowWidth + 2 * config.toolTipPadding + +legendWidth +
  2191. legendMarginRight;
  2192. }
  2193. var startY = offset.y + (config.toolTipLineHeight - config.fontSize) / 2 + config.toolTipLineHeight *
  2194. index +
  2195. config.toolTipPadding;
  2196. context.beginPath();
  2197. context.setFontSize(config.fontSize);
  2198. context.setFillStyle(toolTipOption.fontColor);
  2199. context.fillText(item.text, startX, startY + config.fontSize);
  2200. context.closePath();
  2201. context.stroke();
  2202. });
  2203. }
  2204. function drawYAxisTitle(title, opts, config, context) {
  2205. var startX = config.xAxisHeight + (opts.height - config.xAxisHeight - measureText(title)) / 2;
  2206. context.save();
  2207. context.beginPath();
  2208. context.setFontSize(config.fontSize);
  2209. context.setFillStyle(opts.yAxis.titleFontColor || '#333333');
  2210. context.translate(0, opts.height);
  2211. context.rotate(-90 * Math.PI / 180);
  2212. context.fillText(title, startX, opts.padding[3] + 0.5 * config.fontSize);
  2213. context.closePath();
  2214. context.stroke();
  2215. context.restore();
  2216. }
  2217. function drawColumnDataPoints(series, opts, config, context) {
  2218. let process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  2219. let xAxisData = opts.chartData.xAxisData,
  2220. xAxisPoints = xAxisData.xAxisPoints,
  2221. eachSpacing = xAxisData.eachSpacing;
  2222. let columnOption = assign({}, {
  2223. type: 'group',
  2224. width: eachSpacing / 2,
  2225. meter: {
  2226. border: 4,
  2227. fillColor: '#FFFFFF'
  2228. }
  2229. }, opts.extra.column);
  2230. let calPoints = [];
  2231. context.save();
  2232. let leftNum = -2;
  2233. let rightNum = xAxisPoints.length + 2;
  2234. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  2235. context.translate(opts._scrollDistance_, 0);
  2236. leftNum = Math.floor(-opts._scrollDistance_ / eachSpacing) - 2;
  2237. rightNum = leftNum + opts.xAxis.itemCount + 4;
  2238. }
  2239. if (opts.tooltip && opts.tooltip.textList && opts.tooltip.textList.length && process === 1) {
  2240. drawToolTipSplitArea(opts.tooltip.offset.x, opts, config, context, eachSpacing);
  2241. }
  2242. series.forEach(function(eachSeries, seriesIndex) {
  2243. let ranges, minRange, maxRange;
  2244. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2245. minRange = ranges.pop();
  2246. maxRange = ranges.shift();
  2247. var data = eachSeries.data;
  2248. switch (columnOption.type) {
  2249. case 'group':
  2250. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config,
  2251. process);
  2252. var tooltipPoints = getStackDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts,
  2253. config,
  2254. seriesIndex, series, process);
  2255. calPoints.push(tooltipPoints);
  2256. points = fixColumeData(points, eachSpacing, series.length, seriesIndex, config, opts);
  2257. for (let i = 0; i < points.length; i++) {
  2258. let item = points[i];
  2259. if (item !== null && i > leftNum && i < rightNum) {
  2260. context.beginPath();
  2261. context.setStrokeStyle(item.color || eachSeries.color);
  2262. context.setLineWidth(1)
  2263. context.setFillStyle(item.color || eachSeries.color);
  2264. var startX = item.x - item.width / 2;
  2265. var height = opts.height - item.y - opts.area[2];
  2266. context.moveTo(startX - 1, item.y);
  2267. context.lineTo(startX + item.width - 2, item.y);
  2268. context.lineTo(startX + item.width - 2, opts.height - opts.area[2]);
  2269. context.lineTo(startX, opts.height - opts.area[2]);
  2270. context.lineTo(startX, item.y);
  2271. context.closePath();
  2272. context.stroke();
  2273. context.fill();
  2274. }
  2275. };
  2276. break;
  2277. case 'stack':
  2278. // 绘制堆叠数据图
  2279. var points = getStackDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts,
  2280. config, seriesIndex,
  2281. series, process);
  2282. calPoints.push(points);
  2283. points = fixColumeStackData(points, eachSpacing, series.length, seriesIndex, config, opts,
  2284. series);
  2285. for (let i = 0; i < points.length; i++) {
  2286. let item = points[i];
  2287. if (item !== null && i > leftNum && i < rightNum) {
  2288. context.beginPath();
  2289. context.setFillStyle(item.color || eachSeries.color);
  2290. var startX = item.x - item.width / 2 + 1;
  2291. var height = opts.height - item.y - opts.area[2];
  2292. var height0 = opts.height - item.y0 - opts.area[2];
  2293. if (seriesIndex > 0) {
  2294. height -= height0;
  2295. }
  2296. context.moveTo(startX, item.y);
  2297. context.fillRect(startX, item.y, item.width - 2, height);
  2298. context.closePath();
  2299. context.fill();
  2300. }
  2301. };
  2302. break;
  2303. case 'meter':
  2304. // 绘制温度计数据图
  2305. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config,
  2306. process);
  2307. calPoints.push(points);
  2308. points = fixColumeMeterData(points, eachSpacing, series.length, seriesIndex, config, opts,
  2309. columnOption.meter.border);
  2310. if (seriesIndex == 0) {
  2311. for (let i = 0; i < points.length; i++) {
  2312. let item = points[i];
  2313. if (item !== null && i > leftNum && i < rightNum) {
  2314. //画背景颜色
  2315. context.beginPath();
  2316. context.setFillStyle(columnOption.meter.fillColor);
  2317. var startX = item.x - item.width / 2;
  2318. var height = opts.height - item.y - opts.area[2];
  2319. context.moveTo(startX, item.y);
  2320. context.fillRect(startX, item.y, item.width, height);
  2321. context.closePath();
  2322. context.fill();
  2323. //画边框线
  2324. if (columnOption.meter.border > 0) {
  2325. context.beginPath();
  2326. context.setStrokeStyle(eachSeries.color);
  2327. context.setLineWidth(columnOption.meter.border * opts.pixelRatio);
  2328. context.moveTo(startX + columnOption.meter.border * 0.5, item.y + height);
  2329. context.lineTo(startX + columnOption.meter.border * 0.5, item.y + columnOption
  2330. .meter.border * 0.5);
  2331. context.lineTo(startX + item.width - columnOption.meter.border * 0.5, item.y +
  2332. columnOption.meter.border *
  2333. 0.5);
  2334. context.lineTo(startX + item.width - columnOption.meter.border * 0.5, item.y +
  2335. height);
  2336. context.stroke();
  2337. }
  2338. }
  2339. };
  2340. } else {
  2341. for (let i = 0; i < points.length; i++) {
  2342. let item = points[i];
  2343. if (item !== null && i > leftNum && i < rightNum) {
  2344. context.beginPath();
  2345. context.setFillStyle(item.color || eachSeries.color);
  2346. var startX = item.x - item.width / 2;
  2347. var height = opts.height - item.y - opts.area[2];
  2348. context.moveTo(startX, item.y);
  2349. context.fillRect(startX, item.y, item.width, height);
  2350. context.closePath();
  2351. context.fill();
  2352. }
  2353. };
  2354. }
  2355. break;
  2356. }
  2357. });
  2358. if (opts.dataLabel !== false && process === 1) {
  2359. series.forEach(function(eachSeries, seriesIndex) {
  2360. let ranges, minRange, maxRange;
  2361. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2362. minRange = ranges.pop();
  2363. maxRange = ranges.shift();
  2364. var data = eachSeries.data;
  2365. switch (columnOption.type) {
  2366. case 'group':
  2367. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config,
  2368. process);
  2369. points = fixColumeData(points, eachSpacing, series.length, seriesIndex, config, opts);
  2370. drawPointText(points, eachSeries, config, context);
  2371. break;
  2372. case 'stack':
  2373. var points = getStackDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts,
  2374. config, seriesIndex,
  2375. series, process);
  2376. drawPointText(points, eachSeries, config, context);
  2377. break;
  2378. case 'meter':
  2379. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config,
  2380. process);
  2381. drawPointText(points, eachSeries, config, context);
  2382. break;
  2383. }
  2384. });
  2385. }
  2386. context.restore();
  2387. return {
  2388. xAxisPoints: xAxisPoints,
  2389. calPoints: calPoints,
  2390. eachSpacing: eachSpacing
  2391. };
  2392. }
  2393. function drawCandleDataPoints(series, seriesMA, opts, config, context) {
  2394. var process = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 1;
  2395. var candleOption = assign({}, {
  2396. color: {},
  2397. average: {}
  2398. }, opts.extra.candle);
  2399. candleOption.color = assign({}, {
  2400. upLine: '#f04864',
  2401. upFill: '#f04864',
  2402. downLine: '#2fc25b',
  2403. downFill: '#2fc25b'
  2404. }, candleOption.color);
  2405. candleOption.average = assign({}, {
  2406. show: false,
  2407. name: [],
  2408. day: [],
  2409. color: config.colors
  2410. }, candleOption.average);
  2411. opts.extra.candle = candleOption;
  2412. let xAxisData = opts.chartData.xAxisData,
  2413. xAxisPoints = xAxisData.xAxisPoints,
  2414. eachSpacing = xAxisData.eachSpacing;
  2415. let calPoints = [];
  2416. context.save();
  2417. let leftNum = -2;
  2418. let rightNum = xAxisPoints.length + 2;
  2419. let leftSpace = 0;
  2420. let rightSpace = opts.width + eachSpacing;
  2421. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  2422. context.translate(opts._scrollDistance_, 0);
  2423. leftNum = Math.floor(-opts._scrollDistance_ / eachSpacing) - 2;
  2424. rightNum = leftNum + opts.xAxis.itemCount + 4;
  2425. leftSpace = -opts._scrollDistance_ - eachSpacing + opts.area[3];
  2426. rightSpace = leftSpace + (opts.xAxis.itemCount + 4) * eachSpacing;
  2427. }
  2428. //画均线
  2429. if (candleOption.average.show) {
  2430. seriesMA.forEach(function(eachSeries, seriesIndex) {
  2431. let ranges, minRange, maxRange;
  2432. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2433. minRange = ranges.pop();
  2434. maxRange = ranges.shift();
  2435. var data = eachSeries.data;
  2436. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config,
  2437. process);
  2438. var splitPointList = splitPoints(points);
  2439. for (let i = 0; i < splitPointList.length; i++) {
  2440. let points = splitPointList[i];
  2441. context.beginPath();
  2442. context.setStrokeStyle(eachSeries.color);
  2443. context.setLineWidth(1);
  2444. if (points.length === 1) {
  2445. context.moveTo(points[0].x, points[0].y);
  2446. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  2447. } else {
  2448. context.moveTo(points[0].x, points[0].y);
  2449. let startPoint = 0;
  2450. for (let j = 0; j < points.length; j++) {
  2451. let item = points[j];
  2452. if (startPoint == 0 && item.x > leftSpace) {
  2453. context.moveTo(item.x, item.y);
  2454. startPoint = 1;
  2455. }
  2456. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2457. var ctrlPoint = createCurveControlPoints(points, j - 1);
  2458. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x,
  2459. ctrlPoint.ctrB.y, item.x, item.y);
  2460. }
  2461. }
  2462. context.moveTo(points[0].x, points[0].y);
  2463. }
  2464. context.closePath();
  2465. context.stroke();
  2466. }
  2467. });
  2468. }
  2469. //画K线
  2470. series.forEach(function(eachSeries, seriesIndex) {
  2471. let ranges, minRange, maxRange;
  2472. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2473. minRange = ranges.pop();
  2474. maxRange = ranges.shift();
  2475. var data = eachSeries.data;
  2476. var points = getCandleDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config,
  2477. process);
  2478. calPoints.push(points);
  2479. var splitPointList = splitPoints(points);
  2480. for (let i = 0; i < splitPointList[0].length; i++) {
  2481. if (i > leftNum && i < rightNum) {
  2482. let item = splitPointList[0][i];
  2483. context.beginPath();
  2484. //如果上涨
  2485. if (data[i][1] - data[i][0] > 0) {
  2486. context.setStrokeStyle(candleOption.color.upLine);
  2487. context.setFillStyle(candleOption.color.upFill);
  2488. context.setLineWidth(1 * opts.pixelRatio);
  2489. context.moveTo(item[3].x, item[3].y); //顶点
  2490. context.lineTo(item[1].x, item[1].y); //收盘中间点
  2491. context.lineTo(item[1].x - eachSpacing / 4, item[1].y); //收盘左侧点
  2492. context.lineTo(item[0].x - eachSpacing / 4, item[0].y); //开盘左侧点
  2493. context.lineTo(item[0].x, item[0].y); //开盘中间点
  2494. context.lineTo(item[2].x, item[2].y); //底点
  2495. context.lineTo(item[0].x, item[0].y); //开盘中间点
  2496. context.lineTo(item[0].x + eachSpacing / 4, item[0].y); //开盘右侧点
  2497. context.lineTo(item[1].x + eachSpacing / 4, item[1].y); //收盘右侧点
  2498. context.lineTo(item[1].x, item[1].y); //收盘中间点
  2499. context.moveTo(item[3].x, item[3].y); //顶点
  2500. } else {
  2501. context.setStrokeStyle(candleOption.color.downLine);
  2502. context.setFillStyle(candleOption.color.downFill);
  2503. context.setLineWidth(1 * opts.pixelRatio);
  2504. context.moveTo(item[3].x, item[3].y); //顶点
  2505. context.lineTo(item[0].x, item[0].y); //开盘中间点
  2506. context.lineTo(item[0].x - eachSpacing / 4, item[0].y); //开盘左侧点
  2507. context.lineTo(item[1].x - eachSpacing / 4, item[1].y); //收盘左侧点
  2508. context.lineTo(item[1].x, item[1].y); //收盘中间点
  2509. context.lineTo(item[2].x, item[2].y); //底点
  2510. context.lineTo(item[1].x, item[1].y); //收盘中间点
  2511. context.lineTo(item[1].x + eachSpacing / 4, item[1].y); //收盘右侧点
  2512. context.lineTo(item[0].x + eachSpacing / 4, item[0].y); //开盘右侧点
  2513. context.lineTo(item[0].x, item[0].y); //开盘中间点
  2514. context.moveTo(item[3].x, item[3].y); //顶点
  2515. }
  2516. context.closePath();
  2517. context.fill();
  2518. context.stroke();
  2519. }
  2520. }
  2521. });
  2522. context.restore();
  2523. return {
  2524. xAxisPoints: xAxisPoints,
  2525. calPoints: calPoints,
  2526. eachSpacing: eachSpacing
  2527. };
  2528. }
  2529. function drawAreaDataPoints(series, opts, config, context) {
  2530. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  2531. var areaOption = assign({}, {
  2532. type: 'straight',
  2533. opacity: 0.2,
  2534. addLine: false,
  2535. width: 2,
  2536. gradient: false
  2537. }, opts.extra.area);
  2538. let xAxisData = opts.chartData.xAxisData,
  2539. xAxisPoints = xAxisData.xAxisPoints,
  2540. eachSpacing = xAxisData.eachSpacing;
  2541. let endY = opts.height - opts.area[2];
  2542. let calPoints = [];
  2543. // endY -=20;
  2544. context.save();
  2545. let leftSpace = 0;
  2546. let rightSpace = opts.width + eachSpacing;
  2547. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  2548. context.translate(opts._scrollDistance_, 0);
  2549. leftSpace = -opts._scrollDistance_ - eachSpacing + opts.area[3];
  2550. rightSpace = leftSpace + (opts.xAxis.itemCount + 4) * eachSpacing;
  2551. }
  2552. series.forEach(function(eachSeries, seriesIndex) {
  2553. let ranges, minRange, maxRange;
  2554. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2555. minRange = ranges.pop();
  2556. maxRange = ranges.shift();
  2557. let data = eachSeries.data;
  2558. let points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2559. calPoints.push(points);
  2560. let splitPointList = splitPoints(points);
  2561. for (let i = 0; i < splitPointList.length; i++) {
  2562. let points = splitPointList[i];
  2563. // 绘制区域数
  2564. context.beginPath();
  2565. context.setStrokeStyle(hexToRgb(eachSeries.color, areaOption.opacity));
  2566. if (areaOption.gradient) {
  2567. let gradient = context.createLinearGradient(0, opts.area[0], 0, opts.height - opts.area[2]);
  2568. gradient.addColorStop('0', hexToRgb(eachSeries.color, areaOption.opacity));
  2569. gradient.addColorStop('1.0', hexToRgb("#FFFFFF", 0.1));
  2570. context.setFillStyle(gradient);
  2571. } else {
  2572. context.setFillStyle(hexToRgb(eachSeries.color, areaOption.opacity));
  2573. }
  2574. context.setLineWidth(areaOption.width * opts.pixelRatio);
  2575. if (points.length > 1) {
  2576. let firstPoint = points[0];
  2577. let lastPoint = points[points.length - 1];
  2578. context.moveTo(firstPoint.x, firstPoint.y);
  2579. let startPoint = 0;
  2580. if (areaOption.type === 'curve') {
  2581. for (let j = 0; j < points.length; j++) {
  2582. let item = points[j];
  2583. if (startPoint == 0 && item.x > leftSpace) {
  2584. context.moveTo(item.x, item.y);
  2585. startPoint = 1;
  2586. }
  2587. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2588. let ctrlPoint = createCurveControlPoints(points, j - 1);
  2589. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x,
  2590. ctrlPoint.ctrB.y, item.x, item.y);
  2591. }
  2592. };
  2593. } else {
  2594. for (let j = 0; j < points.length; j++) {
  2595. let item = points[j];
  2596. if (startPoint == 0 && item.x > leftSpace) {
  2597. context.moveTo(item.x, item.y);
  2598. startPoint = 1;
  2599. }
  2600. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2601. context.lineTo(item.x, item.y);
  2602. }
  2603. };
  2604. }
  2605. context.lineTo(lastPoint.x, endY);
  2606. context.lineTo(firstPoint.x, endY);
  2607. context.lineTo(firstPoint.x, firstPoint.y);
  2608. } else {
  2609. let item = points[0];
  2610. context.moveTo(item.x - eachSpacing / 2, item.y);
  2611. context.lineTo(item.x + eachSpacing / 2, item.y);
  2612. context.lineTo(item.x + eachSpacing / 2, endY);
  2613. context.lineTo(item.x - eachSpacing / 2, endY);
  2614. context.moveTo(item.x - eachSpacing / 2, item.y);
  2615. //slam todo
  2616. //加个左右射线
  2617. context.lineWidth = 0.3;
  2618. context.beginPath();
  2619. context.moveTo(item.x, item.y)
  2620. context.lineTo(item.x + opts.width / 2 - 30, item.y);
  2621. context.stroke();
  2622. context.beginPath();
  2623. context.moveTo(item.x, item.y)
  2624. context.lineTo(item.x - opts.width / 2 + 30, item.y);
  2625. context.stroke();
  2626. }
  2627. context.closePath();
  2628. //slam todo 只有一个点时候,不渲染渐变颜色
  2629. if (points.length > 1) {
  2630. context.fill();
  2631. }
  2632. //画连线
  2633. if (areaOption.addLine) {
  2634. if (eachSeries.lineType == 'dash') {
  2635. let dashLength = eachSeries.dashLength ? eachSeries.dashLength : 8;
  2636. dashLength *= opts.pixelRatio;
  2637. context.setLineDash([dashLength, dashLength]);
  2638. }
  2639. context.beginPath();
  2640. context.setStrokeStyle(eachSeries.color);
  2641. context.setLineWidth(areaOption.width * opts.pixelRatio);
  2642. if (points.length === 1) {
  2643. context.moveTo(points[0].x, points[0].y);
  2644. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  2645. } else {
  2646. context.moveTo(points[0].x, points[0].y);
  2647. let startPoint = 0;
  2648. if (areaOption.type === 'curve') {
  2649. for (let j = 0; j < points.length; j++) {
  2650. let item = points[j];
  2651. if (startPoint == 0 && item.x > leftSpace) {
  2652. context.moveTo(item.x, item.y);
  2653. startPoint = 1;
  2654. }
  2655. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2656. let ctrlPoint = createCurveControlPoints(points, j - 1);
  2657. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x,
  2658. ctrlPoint.ctrB.y, item.x, item.y);
  2659. }
  2660. };
  2661. } else {
  2662. for (let j = 0; j < points.length; j++) {
  2663. let item = points[j];
  2664. if (startPoint == 0 && item.x > leftSpace) {
  2665. context.moveTo(item.x, item.y);
  2666. startPoint = 1;
  2667. }
  2668. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2669. context.lineTo(item.x, item.y);
  2670. }
  2671. };
  2672. }
  2673. context.moveTo(points[0].x, points[0].y);
  2674. }
  2675. context.stroke();
  2676. context.setLineDash([]);
  2677. }
  2678. }
  2679. if (opts.enableBottomSolid || config.enableBottomSolid) {
  2680. // slam todo 绘制底部线条
  2681. //下边框
  2682. context.beginPath();
  2683. context.moveTo(10, endY);
  2684. context.lineTo(opts.width / 2 - 7, endY);
  2685. context.lineTo(opts.width / 2, endY - 14);
  2686. context.lineTo(opts.width / 2 + 7, endY);
  2687. context.lineTo(opts.width - 10, endY);
  2688. // context.closePath(); //闭合路径
  2689. context.lineWidth = 0.3; //线的边框为10像素
  2690. context.strokeStyle = 'grey';
  2691. context.stroke(); //绘制定义的图形
  2692. }
  2693. //画点
  2694. if (opts.dataPointShape !== false) {
  2695. drawPointShape(points, eachSeries.color, eachSeries.pointShape, context, opts);
  2696. }
  2697. });
  2698. if (opts.dataLabel !== false && process === 1) {
  2699. series.forEach(function(eachSeries, seriesIndex) {
  2700. let ranges, minRange, maxRange;
  2701. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2702. minRange = ranges.pop();
  2703. maxRange = ranges.shift();
  2704. var data = eachSeries.data;
  2705. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config,
  2706. process);
  2707. drawPointText(points, eachSeries, config, context);
  2708. });
  2709. }
  2710. context.restore();
  2711. return {
  2712. xAxisPoints: xAxisPoints,
  2713. calPoints: calPoints,
  2714. eachSpacing: eachSpacing
  2715. };
  2716. }
  2717. function drawLineDataPoints(series, opts, config, context) {
  2718. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  2719. var lineOption = assign({}, {
  2720. type: 'straight',
  2721. width: 2
  2722. }, opts.extra.line);
  2723. lineOption.width *= opts.pixelRatio;
  2724. let xAxisData = opts.chartData.xAxisData,
  2725. xAxisPoints = xAxisData.xAxisPoints,
  2726. eachSpacing = xAxisData.eachSpacing;
  2727. var calPoints = [];
  2728. context.save();
  2729. let leftSpace = 0;
  2730. let rightSpace = opts.width + eachSpacing;
  2731. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  2732. context.translate(opts._scrollDistance_, 0);
  2733. leftSpace = -opts._scrollDistance_ - eachSpacing + opts.area[3];
  2734. rightSpace = leftSpace + (opts.xAxis.itemCount + 4) * eachSpacing;
  2735. }
  2736. series.forEach(function(eachSeries, seriesIndex) {
  2737. let ranges, minRange, maxRange;
  2738. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2739. minRange = ranges.pop();
  2740. maxRange = ranges.shift();
  2741. var data = eachSeries.data;
  2742. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2743. calPoints.push(points);
  2744. var splitPointList = splitPoints(points);
  2745. if (eachSeries.lineType == 'dash') {
  2746. let dashLength = eachSeries.dashLength ? eachSeries.dashLength : 8;
  2747. dashLength *= opts.pixelRatio;
  2748. context.setLineDash([dashLength, dashLength]);
  2749. }
  2750. context.beginPath();
  2751. context.setStrokeStyle(eachSeries.color);
  2752. context.setLineWidth(lineOption.width);
  2753. splitPointList.forEach(function(points, index) {
  2754. if (points.length === 1) {
  2755. context.moveTo(points[0].x, points[0].y);
  2756. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  2757. } else {
  2758. context.moveTo(points[0].x, points[0].y);
  2759. let startPoint = 0;
  2760. if (lineOption.type === 'curve') {
  2761. for (let j = 0; j < points.length; j++) {
  2762. let item = points[j];
  2763. if (startPoint == 0 && item.x > leftSpace) {
  2764. context.moveTo(item.x, item.y);
  2765. startPoint = 1;
  2766. }
  2767. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2768. var ctrlPoint = createCurveControlPoints(points, j - 1);
  2769. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x,
  2770. ctrlPoint.ctrB.y, item.x, item.y);
  2771. }
  2772. };
  2773. } else {
  2774. for (let j = 0; j < points.length; j++) {
  2775. let item = points[j];
  2776. if (startPoint == 0 && item.x > leftSpace) {
  2777. context.moveTo(item.x, item.y);
  2778. startPoint = 1;
  2779. }
  2780. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2781. context.lineTo(item.x, item.y);
  2782. }
  2783. };
  2784. }
  2785. context.moveTo(points[0].x, points[0].y);
  2786. }
  2787. });
  2788. context.stroke();
  2789. context.setLineDash([]);
  2790. if (opts.dataPointShape !== false) {
  2791. drawPointShape(points, eachSeries.color, eachSeries.pointShape, context, opts);
  2792. }
  2793. });
  2794. if (opts.dataLabel !== false && process === 1) {
  2795. series.forEach(function(eachSeries, seriesIndex) {
  2796. let ranges, minRange, maxRange;
  2797. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2798. minRange = ranges.pop();
  2799. maxRange = ranges.shift();
  2800. var data = eachSeries.data;
  2801. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config,
  2802. process);
  2803. drawPointText(points, eachSeries, config, context);
  2804. });
  2805. }
  2806. context.restore();
  2807. return {
  2808. xAxisPoints: xAxisPoints,
  2809. calPoints: calPoints,
  2810. eachSpacing: eachSpacing
  2811. };
  2812. }
  2813. function drawMixDataPoints(series, opts, config, context) {
  2814. let process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  2815. let xAxisData = opts.chartData.xAxisData,
  2816. xAxisPoints = xAxisData.xAxisPoints,
  2817. eachSpacing = xAxisData.eachSpacing;
  2818. let endY = opts.height - opts.area[2];
  2819. let calPoints = [];
  2820. var columnIndex = 0;
  2821. var columnLength = 0;
  2822. series.forEach(function(eachSeries, seriesIndex) {
  2823. if (eachSeries.type == 'column') {
  2824. columnLength += 1;
  2825. }
  2826. });
  2827. context.save();
  2828. let leftNum = -2;
  2829. let rightNum = xAxisPoints.length + 2;
  2830. let leftSpace = 0;
  2831. let rightSpace = opts.width + eachSpacing;
  2832. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  2833. context.translate(opts._scrollDistance_, 0);
  2834. leftNum = Math.floor(-opts._scrollDistance_ / eachSpacing) - 2;
  2835. rightNum = leftNum + opts.xAxis.itemCount + 4;
  2836. leftSpace = -opts._scrollDistance_ - eachSpacing + opts.area[3];
  2837. rightSpace = leftSpace + (opts.xAxis.itemCount + 4) * eachSpacing;
  2838. }
  2839. series.forEach(function(eachSeries, seriesIndex) {
  2840. let ranges, minRange, maxRange;
  2841. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2842. minRange = ranges.pop();
  2843. maxRange = ranges.shift();
  2844. var data = eachSeries.data;
  2845. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2846. calPoints.push(points);
  2847. // 绘制柱状数据图
  2848. if (eachSeries.type == 'column') {
  2849. points = fixColumeData(points, eachSpacing, columnLength, columnIndex, config, opts);
  2850. for (let i = 0; i < points.length; i++) {
  2851. let item = points[i];
  2852. if (item !== null && i > leftNum && i < rightNum) {
  2853. context.beginPath();
  2854. context.setStrokeStyle(item.color || eachSeries.color);
  2855. context.setLineWidth(1)
  2856. context.setFillStyle(item.color || eachSeries.color);
  2857. var startX = item.x - item.width / 2;
  2858. var height = opts.height - item.y - opts.area[2];
  2859. context.moveTo(startX, item.y);
  2860. context.moveTo(startX - 1, item.y);
  2861. context.lineTo(startX + item.width - 2, item.y);
  2862. context.lineTo(startX + item.width - 2, opts.height - opts.area[2]);
  2863. context.lineTo(startX, opts.height - opts.area[2]);
  2864. context.lineTo(startX, item.y);
  2865. context.closePath();
  2866. context.stroke();
  2867. context.fill();
  2868. context.closePath();
  2869. context.fill();
  2870. }
  2871. }
  2872. columnIndex += 1;
  2873. }
  2874. //绘制区域图数据
  2875. if (eachSeries.type == 'area') {
  2876. let splitPointList = splitPoints(points);
  2877. for (let i = 0; i < splitPointList.length; i++) {
  2878. let points = splitPointList[i];
  2879. // 绘制区域数据
  2880. context.beginPath();
  2881. context.setStrokeStyle(eachSeries.color);
  2882. context.setFillStyle(hexToRgb(eachSeries.color, 0.2));
  2883. context.setLineWidth(2 * opts.pixelRatio);
  2884. if (points.length > 1) {
  2885. var firstPoint = points[0];
  2886. let lastPoint = points[points.length - 1];
  2887. context.moveTo(firstPoint.x, firstPoint.y);
  2888. let startPoint = 0;
  2889. if (eachSeries.style === 'curve') {
  2890. for (let j = 0; j < points.length; j++) {
  2891. let item = points[j];
  2892. if (startPoint == 0 && item.x > leftSpace) {
  2893. context.moveTo(item.x, item.y);
  2894. startPoint = 1;
  2895. }
  2896. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2897. var ctrlPoint = createCurveControlPoints(points, j - 1);
  2898. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x,
  2899. ctrlPoint.ctrB.y, item.x, item.y);
  2900. }
  2901. };
  2902. } else {
  2903. for (let j = 0; j < points.length; j++) {
  2904. let item = points[j];
  2905. if (startPoint == 0 && item.x > leftSpace) {
  2906. context.moveTo(item.x, item.y);
  2907. startPoint = 1;
  2908. }
  2909. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2910. context.lineTo(item.x, item.y);
  2911. }
  2912. };
  2913. }
  2914. context.lineTo(lastPoint.x, endY);
  2915. context.lineTo(firstPoint.x, endY);
  2916. context.lineTo(firstPoint.x, firstPoint.y);
  2917. } else {
  2918. let item = points[0];
  2919. context.moveTo(item.x - eachSpacing / 2, item.y);
  2920. context.lineTo(item.x + eachSpacing / 2, item.y);
  2921. context.lineTo(item.x + eachSpacing / 2, endY);
  2922. context.lineTo(item.x - eachSpacing / 2, endY);
  2923. context.moveTo(item.x - eachSpacing / 2, item.y);
  2924. }
  2925. context.closePath();
  2926. context.fill();
  2927. }
  2928. }
  2929. // 绘制折线数据图
  2930. if (eachSeries.type == 'line') {
  2931. var splitPointList = splitPoints(points);
  2932. splitPointList.forEach(function(points, index) {
  2933. if (eachSeries.lineType == 'dash') {
  2934. let dashLength = eachSeries.dashLength ? eachSeries.dashLength : 8;
  2935. dashLength *= opts.pixelRatio;
  2936. context.setLineDash([dashLength, dashLength]);
  2937. }
  2938. context.beginPath();
  2939. context.setStrokeStyle(eachSeries.color);
  2940. context.setLineWidth(2 * opts.pixelRatio);
  2941. if (points.length === 1) {
  2942. context.moveTo(points[0].x, points[0].y);
  2943. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  2944. } else {
  2945. context.moveTo(points[0].x, points[0].y);
  2946. let startPoint = 0;
  2947. if (eachSeries.style == 'curve') {
  2948. for (let j = 0; j < points.length; j++) {
  2949. let item = points[j];
  2950. if (startPoint == 0 && item.x > leftSpace) {
  2951. context.moveTo(item.x, item.y);
  2952. startPoint = 1;
  2953. }
  2954. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2955. var ctrlPoint = createCurveControlPoints(points, j - 1);
  2956. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB
  2957. .x, ctrlPoint.ctrB.y, item.x, item.y);
  2958. }
  2959. }
  2960. } else {
  2961. for (let j = 0; j < points.length; j++) {
  2962. let item = points[j];
  2963. if (startPoint == 0 && item.x > leftSpace) {
  2964. context.moveTo(item.x, item.y);
  2965. startPoint = 1;
  2966. }
  2967. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2968. context.lineTo(item.x, item.y);
  2969. }
  2970. }
  2971. }
  2972. context.moveTo(points[0].x, points[0].y);
  2973. }
  2974. context.stroke();
  2975. context.setLineDash([]);
  2976. });
  2977. }
  2978. // 绘制点数据图
  2979. if (eachSeries.type == 'point') {
  2980. eachSeries.addPoint = true;
  2981. }
  2982. if (eachSeries.addPoint == true && eachSeries.type !== 'column') {
  2983. drawPointShape(points, eachSeries.color, eachSeries.pointShape, context, opts);
  2984. }
  2985. });
  2986. if (opts.dataLabel !== false && process === 1) {
  2987. var columnIndex = 0;
  2988. series.forEach(function(eachSeries, seriesIndex) {
  2989. let ranges, minRange, maxRange;
  2990. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2991. minRange = ranges.pop();
  2992. maxRange = ranges.shift();
  2993. var data = eachSeries.data;
  2994. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config,
  2995. process);
  2996. if (eachSeries.type !== 'column') {
  2997. drawPointText(points, eachSeries, config, context);
  2998. } else {
  2999. points = fixColumeData(points, eachSpacing, columnLength, columnIndex, config, opts);
  3000. drawPointText(points, eachSeries, config, context);
  3001. columnIndex += 1;
  3002. }
  3003. });
  3004. }
  3005. context.restore();
  3006. return {
  3007. xAxisPoints: xAxisPoints,
  3008. calPoints: calPoints,
  3009. eachSpacing: eachSpacing,
  3010. }
  3011. }
  3012. function drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints) {
  3013. var toolTipOption = opts.extra.tooltip || {};
  3014. if (toolTipOption.horizentalLine && opts.tooltip && process === 1 && (opts.type == 'line' || opts.type == 'area' ||
  3015. opts.type == 'column' || opts.type == 'candle' || opts.type == 'mix')) {
  3016. drawToolTipHorizentalLine(opts, config, context, eachSpacing, xAxisPoints)
  3017. }
  3018. context.save();
  3019. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  3020. context.translate(opts._scrollDistance_, 0);
  3021. }
  3022. if (opts.tooltip && opts.tooltip.textList && opts.tooltip.textList.length && process === 1) {
  3023. drawToolTip(opts.tooltip.textList, opts.tooltip.offset, opts, config, context, eachSpacing, xAxisPoints);
  3024. }
  3025. context.restore();
  3026. }
  3027. function drawXAxis(categories, opts, config, context) {
  3028. let xAxisData = opts.chartData.xAxisData,
  3029. xAxisPoints = xAxisData.xAxisPoints,
  3030. startX = xAxisData.startX,
  3031. endX = xAxisData.endX,
  3032. eachSpacing = xAxisData.eachSpacing;
  3033. var boundaryGap = 'center';
  3034. if (opts.type == 'line' || opts.type == 'area') {
  3035. boundaryGap = opts.xAxis.boundaryGap;
  3036. }
  3037. var startY = opts.height - opts.area[2];
  3038. var endY = opts.area[0];
  3039. // if(opts.type == 'area'){
  3040. // console.log("===",startY,endY);
  3041. // startY -=10 ;
  3042. // endY += 10 ;
  3043. // }
  3044. //绘制滚动条
  3045. if (opts.enableScroll && opts.xAxis.scrollShow) {
  3046. var scrollY = opts.height - opts.area[2] + config.xAxisHeight;
  3047. var scrollScreenWidth = endX - startX;
  3048. var scrollTotalWidth = eachSpacing * (xAxisPoints.length - 1);
  3049. var scrollWidth = scrollScreenWidth * scrollScreenWidth / scrollTotalWidth;
  3050. var scrollLeft = 0;
  3051. if (opts._scrollDistance_) {
  3052. scrollLeft = -opts._scrollDistance_ * (scrollScreenWidth) / scrollTotalWidth;
  3053. }
  3054. context.beginPath();
  3055. context.setLineCap('round');
  3056. context.setLineWidth(6 * opts.pixelRatio);
  3057. context.setStrokeStyle(opts.xAxis.scrollBackgroundColor || "#EFEBEF");
  3058. context.moveTo(startX, scrollY);
  3059. context.lineTo(endX, scrollY);
  3060. context.stroke();
  3061. context.closePath();
  3062. context.beginPath();
  3063. context.setLineCap('round');
  3064. context.setLineWidth(6 * opts.pixelRatio);
  3065. context.setStrokeStyle(opts.xAxis.scrollColor || "#A6A6A6");
  3066. context.moveTo(startX + scrollLeft, scrollY);
  3067. context.lineTo(startX + scrollLeft + scrollWidth, scrollY);
  3068. context.stroke();
  3069. context.closePath();
  3070. context.setLineCap('butt');
  3071. }
  3072. context.save();
  3073. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0) {
  3074. context.translate(opts._scrollDistance_, 0);
  3075. }
  3076. //绘制X轴刻度线
  3077. if (opts.xAxis.calibration === true) {
  3078. context.setStrokeStyle(opts.xAxis.gridColor || "#cccccc");
  3079. context.setLineCap('butt');
  3080. context.setLineWidth(1 * opts.pixelRatio);
  3081. xAxisPoints.forEach(function(item, index) {
  3082. if (index > 0) {
  3083. context.beginPath();
  3084. //eachSpacing/2
  3085. context.moveTo(item - eachSpacing / 2, startY);
  3086. //eachSpacing/2
  3087. context.lineTo(item - eachSpacing / 2, startY - 3 * opts.pixelRatio);
  3088. context.closePath();
  3089. context.stroke();
  3090. }
  3091. });
  3092. }
  3093. //绘制X轴网格
  3094. if (opts.xAxis.disableGrid !== true) {
  3095. context.setStrokeStyle(opts.xAxis.gridColor || "#cccccc");
  3096. context.setLineCap('butt');
  3097. context.setLineWidth(1 * opts.pixelRatio);
  3098. if (opts.xAxis.gridType == 'dash') {
  3099. context.setLineDash([opts.xAxis.dashLength, opts.xAxis.dashLength]);
  3100. }
  3101. opts.xAxis.gridEval = opts.xAxis.gridEval || 1;
  3102. xAxisPoints.forEach(function(item, index) {
  3103. if (index % opts.xAxis.gridEval == 0) {
  3104. context.beginPath();
  3105. context.moveTo(item, startY);
  3106. context.lineTo(item, endY);
  3107. context.stroke();
  3108. }
  3109. });
  3110. context.setLineDash([]);
  3111. }
  3112. //绘制X轴文案
  3113. if (opts.xAxis.disabled !== true) {
  3114. // 对X轴列表做抽稀处理
  3115. //默认全部显示X轴标签
  3116. let maxXAxisListLength = categories.length;
  3117. //如果设置了X轴单屏数量
  3118. if (opts.xAxis.labelCount) {
  3119. //如果设置X轴密度
  3120. if (opts.xAxis.itemCount) {
  3121. maxXAxisListLength = Math.ceil(categories.length / opts.xAxis.itemCount * opts.xAxis.labelCount);
  3122. } else {
  3123. maxXAxisListLength = opts.xAxis.labelCount;
  3124. }
  3125. maxXAxisListLength -= 1;
  3126. }
  3127. let ratio = Math.ceil(categories.length / maxXAxisListLength);
  3128. let newCategories = [];
  3129. let cgLength = categories.length;
  3130. for (let i = 0; i < cgLength; i++) {
  3131. if (i % ratio !== 0) {
  3132. newCategories.push("");
  3133. } else {
  3134. newCategories.push(categories[i]);
  3135. }
  3136. }
  3137. newCategories[cgLength - 1] = categories[cgLength - 1];
  3138. var xAxisFontSize = opts.xAxis.fontSize || config.fontSize;
  3139. if (config._xAxisTextAngle_ === 0) {
  3140. newCategories.forEach(function(item, index) {
  3141. var offset = -measureText(String(item), xAxisFontSize) / 2;
  3142. if (boundaryGap == 'center') {
  3143. offset += eachSpacing / 2;
  3144. }
  3145. var scrollHeight = 0;
  3146. if (opts.xAxis.scrollShow) {
  3147. scrollHeight = 6 * opts.pixelRatio;
  3148. }
  3149. context.beginPath();
  3150. context.setFontSize(xAxisFontSize);
  3151. context.setFillStyle(opts.xAxis.fontColor || '#666666');
  3152. context.fillText(String(item), xAxisPoints[index] + offset, startY + xAxisFontSize + (config
  3153. .xAxisHeight -
  3154. scrollHeight - xAxisFontSize) / 2);
  3155. context.closePath();
  3156. context.stroke();
  3157. });
  3158. } else {
  3159. newCategories.forEach(function(item, index) {
  3160. context.save();
  3161. context.beginPath();
  3162. context.setFontSize(xAxisFontSize);
  3163. context.setFillStyle(opts.xAxis.fontColor || '#666666');
  3164. var textWidth = measureText(String(item), xAxisFontSize);
  3165. var offset = -textWidth;
  3166. if (boundaryGap == 'center') {
  3167. offset += eachSpacing / 2;
  3168. }
  3169. var _calRotateTranslate = calRotateTranslate(xAxisPoints[index] + eachSpacing / 2, startY +
  3170. xAxisFontSize / 2 + 5,
  3171. opts.height),
  3172. transX = _calRotateTranslate.transX,
  3173. transY = _calRotateTranslate.transY;
  3174. context.rotate(-1 * config._xAxisTextAngle_);
  3175. context.translate(transX, transY);
  3176. context.fillText(String(item), xAxisPoints[index] + offset, startY + xAxisFontSize + 5);
  3177. context.closePath();
  3178. context.stroke();
  3179. context.restore();
  3180. });
  3181. }
  3182. }
  3183. context.restore();
  3184. //绘制X轴轴线
  3185. if (opts.xAxis.axisLine) {
  3186. context.beginPath();
  3187. context.setStrokeStyle(opts.xAxis.axisLineColor);
  3188. context.setLineWidth(1 * opts.pixelRatio);
  3189. context.moveTo(startX, opts.height - opts.area[2]);
  3190. context.lineTo(endX, opts.height - opts.area[2]);
  3191. context.stroke();
  3192. }
  3193. }
  3194. function drawYAxisGrid(categories, opts, config, context) {
  3195. if (opts.yAxis.disableGrid === true) {
  3196. return;
  3197. }
  3198. let spacingValid = opts.height - opts.area[0] - opts.area[2];
  3199. let eachSpacing = spacingValid / opts.yAxis.splitNumber;
  3200. let startX = opts.area[3];
  3201. let xAxisPoints = opts.chartData.xAxisData.xAxisPoints,
  3202. xAxiseachSpacing = opts.chartData.xAxisData.eachSpacing;
  3203. let TotalWidth = xAxiseachSpacing * (xAxisPoints.length - 1);
  3204. let endX = startX + TotalWidth;
  3205. let points = [];
  3206. for (let i = 0; i < opts.yAxis.splitNumber + 1; i++) {
  3207. points.push(opts.height - opts.area[2] - eachSpacing * i);
  3208. }
  3209. context.save();
  3210. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0) {
  3211. context.translate(opts._scrollDistance_, 0);
  3212. }
  3213. if (opts.yAxis.gridType == 'dash') {
  3214. context.setLineDash([opts.yAxis.dashLength, opts.yAxis.dashLength]);
  3215. }
  3216. context.setStrokeStyle(opts.yAxis.gridColor);
  3217. context.setLineWidth(1 * opts.pixelRatio);
  3218. points.forEach(function(item, index) {
  3219. context.beginPath();
  3220. context.moveTo(startX, item);
  3221. context.lineTo(endX, item);
  3222. context.stroke();
  3223. });
  3224. context.setLineDash([]);
  3225. context.restore();
  3226. }
  3227. function drawYAxis(series, opts, config, context) {
  3228. if (opts.yAxis.disabled === true) {
  3229. return;
  3230. }
  3231. var spacingValid = opts.height - opts.area[0] - opts.area[2];
  3232. var eachSpacing = spacingValid / opts.yAxis.splitNumber;
  3233. var startX = opts.area[3];
  3234. var endX = opts.width - opts.area[1];
  3235. var endY = opts.height - opts.area[2];
  3236. var fillEndY = endY + config.xAxisHeight;
  3237. if (opts.xAxis.scrollShow) {
  3238. fillEndY -= 3 * opts.pixelRatio;
  3239. }
  3240. if (opts.xAxis.rotateLabel) {
  3241. fillEndY = opts.height - opts.area[2] + 3;
  3242. }
  3243. // set YAxis background
  3244. context.beginPath();
  3245. context.setFillStyle(opts.background || '#ffffff');
  3246. if (opts._scrollDistance_ < 0) {
  3247. context.fillRect(0, 0, startX, fillEndY);
  3248. }
  3249. if (opts.enableScroll == true) {
  3250. context.fillRect(endX, 0, opts.width, fillEndY);
  3251. }
  3252. context.closePath();
  3253. context.stroke();
  3254. var points = [];
  3255. for (let i = 0; i <= opts.yAxis.splitNumber; i++) {
  3256. points.push(opts.area[0] + eachSpacing * i);
  3257. }
  3258. let tStartLeft = opts.area[3];
  3259. let tStartRight = opts.width - opts.area[1];
  3260. for (let i = 0; i < opts.yAxis.data.length; i++) {
  3261. let yData = opts.yAxis.data[i];
  3262. if (yData.disabled !== true) {
  3263. let rangesFormat = opts.chartData.yAxisData.rangesFormat[i];
  3264. let yAxisFontSize = yData.fontSize || config.fontSize;
  3265. let yAxisWidth = opts.chartData.yAxisData.yAxisWidth[i];
  3266. //画Y轴刻度及文案
  3267. rangesFormat.forEach(function(item, index) {
  3268. var pos = points[index] ? points[index] : endY;
  3269. context.beginPath();
  3270. context.setFontSize(yAxisFontSize);
  3271. context.setLineWidth(1 * opts.pixelRatio);
  3272. context.setStrokeStyle(yData.axisLineColor || '#cccccc');
  3273. context.setFillStyle(yData.fontColor || '#666666');
  3274. if (yAxisWidth.position == 'left') {
  3275. context.fillText(String(item), tStartLeft - yAxisWidth.width, pos + yAxisFontSize / 2);
  3276. //画刻度线
  3277. if (yData.calibration == true) {
  3278. context.moveTo(tStartLeft, pos);
  3279. context.lineTo(tStartLeft - 3 * opts.pixelRatio, pos);
  3280. }
  3281. } else {
  3282. context.fillText(String(item), tStartRight + 4 * opts.pixelRatio, pos + yAxisFontSize / 2);
  3283. //画刻度线
  3284. if (yData.calibration == true) {
  3285. context.moveTo(tStartRight, pos);
  3286. context.lineTo(tStartRight + 3 * opts.pixelRatio, pos);
  3287. }
  3288. }
  3289. context.closePath();
  3290. context.stroke();
  3291. });
  3292. //画Y轴轴线
  3293. if (yData.axisLine !== false && !opts.yAxis.disableLine) {
  3294. context.beginPath();
  3295. context.setStrokeStyle(yData.axisLineColor || '#cccccc');
  3296. context.setLineWidth(1 * opts.pixelRatio);
  3297. if (yAxisWidth.position == 'left') {
  3298. context.moveTo(tStartLeft, opts.height - opts.area[2]);
  3299. context.lineTo(tStartLeft, opts.area[0]);
  3300. } else {
  3301. context.moveTo(tStartRight, opts.height - opts.area[2]);
  3302. context.lineTo(tStartRight, opts.area[0]);
  3303. }
  3304. context.stroke();
  3305. }
  3306. //画Y轴标题
  3307. if (opts.yAxis.showTitle) {
  3308. let titleFontSize = yData.titleFontSize || config.fontSize;
  3309. let title = yData.title;
  3310. context.beginPath();
  3311. context.setFontSize(titleFontSize);
  3312. context.setFillStyle(yData.titleFontColor || '#666666');
  3313. if (yAxisWidth.position == 'left') {
  3314. context.fillText(title, tStartLeft - measureText(title, titleFontSize) / 2, opts.area[0] - 10 * opts
  3315. .pixelRatio);
  3316. } else {
  3317. context.fillText(title, tStartRight - measureText(title, titleFontSize) / 2, opts.area[0] - 10 *
  3318. opts.pixelRatio);
  3319. }
  3320. context.closePath();
  3321. context.stroke();
  3322. }
  3323. if (yAxisWidth.position == 'left') {
  3324. tStartLeft -= (yAxisWidth.width + opts.yAxis.padding);
  3325. } else {
  3326. tStartRight += yAxisWidth.width + opts.yAxis.padding;
  3327. }
  3328. }
  3329. }
  3330. }
  3331. function drawLegend(series, opts, config, context, chartData) {
  3332. if (opts.legend.show === false) {
  3333. return;
  3334. }
  3335. let legendData = chartData.legendData;
  3336. let legendList = legendData.points;
  3337. let legendArea = legendData.area;
  3338. let padding = opts.legend.padding;
  3339. let fontSize = opts.legend.fontSize;
  3340. let shapeWidth = 15 * opts.pixelRatio;
  3341. let shapeRight = 5 * opts.pixelRatio;
  3342. let itemGap = opts.legend.itemGap;
  3343. let lineHeight = Math.max(opts.legend.lineHeight * opts.pixelRatio, fontSize);
  3344. //画背景及边框
  3345. context.beginPath();
  3346. context.setLineWidth(opts.legend.borderWidth);
  3347. context.setStrokeStyle(opts.legend.borderColor);
  3348. context.setFillStyle(opts.legend.backgroundColor);
  3349. context.moveTo(legendArea.start.x, legendArea.start.y);
  3350. context.rect(legendArea.start.x, legendArea.start.y, legendArea.width, legendArea.height);
  3351. context.closePath();
  3352. context.fill();
  3353. context.stroke();
  3354. legendList.forEach(function(itemList, listIndex) {
  3355. let width = 0;
  3356. let height = 0;
  3357. width = legendData.widthArr[listIndex];
  3358. height = legendData.heightArr[listIndex];
  3359. let startX = 0;
  3360. let startY = 0;
  3361. if (opts.legend.position == 'top' || opts.legend.position == 'bottom') {
  3362. startX = legendArea.start.x + (legendArea.width - width) / 2;
  3363. startY = legendArea.start.y + padding + listIndex * lineHeight;
  3364. } else {
  3365. if (listIndex == 0) {
  3366. width = 0;
  3367. } else {
  3368. width = legendData.widthArr[listIndex - 1];
  3369. }
  3370. startX = legendArea.start.x + padding + width;
  3371. startY = legendArea.start.y + padding + (legendArea.height - height) / 2;
  3372. }
  3373. context.setFontSize(config.fontSize);
  3374. for (let i = 0; i < itemList.length; i++) {
  3375. let item = itemList[i];
  3376. item.area = [0, 0, 0, 0];
  3377. item.area[0] = startX;
  3378. item.area[1] = startY;
  3379. item.area[3] = startY + lineHeight;
  3380. context.beginPath();
  3381. context.setLineWidth(1 * opts.pixelRatio);
  3382. context.setStrokeStyle(item.show ? item.color : opts.legend.hiddenColor);
  3383. context.setFillStyle(item.show ? item.color : opts.legend.hiddenColor);
  3384. switch (item.legendShape) {
  3385. case 'line':
  3386. context.moveTo(startX, startY + 0.5 * lineHeight - 2 * opts.pixelRatio);
  3387. context.fillRect(startX, startY + 0.5 * lineHeight - 2 * opts.pixelRatio, 15 * opts
  3388. .pixelRatio, 4 * opts.pixelRatio);
  3389. break;
  3390. case 'triangle':
  3391. context.moveTo(startX + 7.5 * opts.pixelRatio, startY + 0.5 * lineHeight - 5 * opts
  3392. .pixelRatio);
  3393. context.lineTo(startX + 2.5 * opts.pixelRatio, startY + 0.5 * lineHeight + 5 * opts
  3394. .pixelRatio);
  3395. context.lineTo(startX + 12.5 * opts.pixelRatio, startY + 0.5 * lineHeight + 5 * opts
  3396. .pixelRatio);
  3397. context.lineTo(startX + 7.5 * opts.pixelRatio, startY + 0.5 * lineHeight - 5 * opts
  3398. .pixelRatio);
  3399. break;
  3400. case 'diamond':
  3401. context.moveTo(startX + 7.5 * opts.pixelRatio, startY + 0.5 * lineHeight - 5 * opts
  3402. .pixelRatio);
  3403. context.lineTo(startX + 2.5 * opts.pixelRatio, startY + 0.5 * lineHeight);
  3404. context.lineTo(startX + 7.5 * opts.pixelRatio, startY + 0.5 * lineHeight + 5 * opts
  3405. .pixelRatio);
  3406. context.lineTo(startX + 12.5 * opts.pixelRatio, startY + 0.5 * lineHeight);
  3407. context.lineTo(startX + 7.5 * opts.pixelRatio, startY + 0.5 * lineHeight - 5 * opts
  3408. .pixelRatio);
  3409. break;
  3410. case 'circle':
  3411. context.moveTo(startX + 7.5 * opts.pixelRatio, startY + 0.5 * lineHeight);
  3412. context.arc(startX + 7.5 * opts.pixelRatio, startY + 0.5 * lineHeight, 5 * opts.pixelRatio,
  3413. 0, 2 * Math.PI);
  3414. break;
  3415. case 'rect':
  3416. context.moveTo(startX, startY + 0.5 * lineHeight - 5 * opts.pixelRatio);
  3417. context.fillRect(startX, startY + 0.5 * lineHeight - 5 * opts.pixelRatio, 15 * opts
  3418. .pixelRatio, 10 * opts.pixelRatio);
  3419. break;
  3420. default:
  3421. context.moveTo(startX, startY + 0.5 * lineHeight - 5 * opts.pixelRatio);
  3422. context.fillRect(startX, startY + 0.5 * lineHeight - 5 * opts.pixelRatio, 15 * opts
  3423. .pixelRatio, 10 * opts.pixelRatio);
  3424. }
  3425. context.closePath();
  3426. context.fill();
  3427. context.stroke();
  3428. startX += shapeWidth + shapeRight;
  3429. let fontTrans = 0.5 * lineHeight + 0.5 * fontSize - 2;
  3430. context.beginPath();
  3431. context.setFontSize(fontSize);
  3432. context.setFillStyle(item.show ? opts.legend.fontColor : opts.legend.hiddenColor);
  3433. context.fillText(item.name, startX, startY + fontTrans);
  3434. context.closePath();
  3435. context.stroke();
  3436. if (opts.legend.position == 'top' || opts.legend.position == 'bottom') {
  3437. startX += measureText(item.name, fontSize) + itemGap;
  3438. item.area[2] = startX;
  3439. } else {
  3440. item.area[2] = startX + measureText(item.name, fontSize) + itemGap;;
  3441. startX -= shapeWidth + shapeRight;
  3442. startY += lineHeight;
  3443. }
  3444. }
  3445. });
  3446. }
  3447. function drawPieDataPoints(series, opts, config, context) {
  3448. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  3449. var pieOption = assign({}, {
  3450. activeOpacity: 0.5,
  3451. activeRadius: 10 * opts.pixelRatio,
  3452. offsetAngle: 0,
  3453. labelWidth: 15 * opts.pixelRatio,
  3454. ringWidth: 0,
  3455. border: false,
  3456. borderWidth: 2,
  3457. borderColor: '#FFFFFF'
  3458. }, opts.extra.pie);
  3459. var centerPosition = {
  3460. x: opts.area[3] + (opts.width - opts.area[1] - opts.area[3]) / 2,
  3461. y: opts.area[0] + (opts.height - opts.area[0] - opts.area[2]) / 2
  3462. };
  3463. if (config.pieChartLinePadding == 0) {
  3464. config.pieChartLinePadding = pieOption.activeRadius;
  3465. }
  3466. var radius = Math.min((opts.width - opts.area[1] - opts.area[3]) / 2 - config.pieChartLinePadding - config
  3467. .pieChartTextPadding -
  3468. config._pieTextMaxLength_, (opts.height - opts.area[0] - opts.area[2]) / 2 - config.pieChartLinePadding -
  3469. config.pieChartTextPadding
  3470. );
  3471. series = getPieDataPoints(series, radius, process);
  3472. var activeRadius = pieOption.activeRadius;
  3473. series = series.map(function(eachSeries) {
  3474. eachSeries._start_ += (pieOption.offsetAngle) * Math.PI / 180;
  3475. return eachSeries;
  3476. });
  3477. series.forEach(function(eachSeries, seriesIndex) {
  3478. if (opts.tooltip) {
  3479. if (opts.tooltip.index == seriesIndex) {
  3480. context.beginPath();
  3481. context.setFillStyle(hexToRgb(eachSeries.color, opts.extra.pie.activeOpacity || 0.5));
  3482. context.moveTo(centerPosition.x, centerPosition.y);
  3483. context.arc(centerPosition.x, centerPosition.y, eachSeries._radius_ + activeRadius, eachSeries
  3484. ._start_,
  3485. eachSeries._start_ + 2 *
  3486. eachSeries._proportion_ * Math.PI);
  3487. context.closePath();
  3488. context.fill();
  3489. }
  3490. }
  3491. context.beginPath();
  3492. context.setLineWidth(pieOption.borderWidth * opts.pixelRatio);
  3493. context.lineJoin = "round";
  3494. context.setStrokeStyle(pieOption.borderColor);
  3495. context.setFillStyle(eachSeries.color);
  3496. context.moveTo(centerPosition.x, centerPosition.y);
  3497. context.arc(centerPosition.x, centerPosition.y, eachSeries._radius_, eachSeries._start_, eachSeries
  3498. ._start_ + 2 *
  3499. eachSeries._proportion_ * Math.PI);
  3500. context.closePath();
  3501. context.fill();
  3502. if (pieOption.border == true) {
  3503. context.stroke();
  3504. }
  3505. });
  3506. if (opts.type === 'ring') {
  3507. var innerPieWidth = radius * 0.6;
  3508. if (typeof opts.extra.pie.ringWidth === 'number' && opts.extra.pie.ringWidth > 0) {
  3509. innerPieWidth = Math.max(0, radius - opts.extra.pie.ringWidth);
  3510. }
  3511. context.beginPath();
  3512. context.setFillStyle(opts.background || '#ffffff');
  3513. context.moveTo(centerPosition.x, centerPosition.y);
  3514. context.arc(centerPosition.x, centerPosition.y, innerPieWidth, 0, 2 * Math.PI);
  3515. context.closePath();
  3516. context.fill();
  3517. }
  3518. if (opts.dataLabel !== false && process === 1) {
  3519. var valid = false;
  3520. for (var i = 0, len = series.length; i < len; i++) {
  3521. if (series[i].data > 0) {
  3522. valid = true;
  3523. break;
  3524. }
  3525. }
  3526. if (valid) {
  3527. drawPieText(series, opts, config, context, radius, centerPosition);
  3528. }
  3529. }
  3530. if (process === 1 && opts.type === 'ring') {
  3531. drawRingTitle(opts, config, context, centerPosition);
  3532. }
  3533. return {
  3534. center: centerPosition,
  3535. radius: radius,
  3536. series: series
  3537. };
  3538. }
  3539. function drawRoseDataPoints(series, opts, config, context) {
  3540. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  3541. var roseOption = assign({}, {
  3542. type: 'area',
  3543. activeOpacity: 0.5,
  3544. activeRadius: 10 * opts.pixelRatio,
  3545. offsetAngle: 0,
  3546. labelWidth: 15 * opts.pixelRatio,
  3547. border: false,
  3548. borderWidth: 2,
  3549. borderColor: '#FFFFFF'
  3550. }, opts.extra.rose);
  3551. if (config.pieChartLinePadding == 0) {
  3552. config.pieChartLinePadding = roseOption.activeRadius;
  3553. }
  3554. var centerPosition = {
  3555. x: opts.area[3] + (opts.width - opts.area[1] - opts.area[3]) / 2,
  3556. y: opts.area[0] + (opts.height - opts.area[0] - opts.area[2]) / 2
  3557. };
  3558. var radius = Math.min((opts.width - opts.area[1] - opts.area[3]) / 2 - config.pieChartLinePadding - config
  3559. .pieChartTextPadding -
  3560. config._pieTextMaxLength_, (opts.height - opts.area[0] - opts.area[2]) / 2 - config.pieChartLinePadding -
  3561. config.pieChartTextPadding
  3562. );
  3563. var minRadius = roseOption.minRadius || radius * 0.5;
  3564. series = getRoseDataPoints(series, roseOption.type, minRadius, radius, process);
  3565. var activeRadius = roseOption.activeRadius;
  3566. series = series.map(function(eachSeries) {
  3567. eachSeries._start_ += (roseOption.offsetAngle || 0) * Math.PI / 180;
  3568. return eachSeries;
  3569. });
  3570. series.forEach(function(eachSeries, seriesIndex) {
  3571. if (opts.tooltip) {
  3572. if (opts.tooltip.index == seriesIndex) {
  3573. context.beginPath();
  3574. context.setFillStyle(hexToRgb(eachSeries.color, roseOption.activeOpacity || 0.5));
  3575. context.moveTo(centerPosition.x, centerPosition.y);
  3576. context.arc(centerPosition.x, centerPosition.y, activeRadius + eachSeries._radius_, eachSeries
  3577. ._start_,
  3578. eachSeries._start_ + 2 * eachSeries._rose_proportion_ * Math.PI);
  3579. context.closePath();
  3580. context.fill();
  3581. }
  3582. }
  3583. context.beginPath();
  3584. context.setLineWidth(roseOption.borderWidth * opts.pixelRatio);
  3585. context.lineJoin = "round";
  3586. context.setStrokeStyle(roseOption.borderColor);
  3587. context.setFillStyle(eachSeries.color);
  3588. context.moveTo(centerPosition.x, centerPosition.y);
  3589. context.arc(centerPosition.x, centerPosition.y, eachSeries._radius_, eachSeries._start_, eachSeries
  3590. ._start_ + 2 *
  3591. eachSeries._rose_proportion_ * Math.PI);
  3592. context.closePath();
  3593. context.fill();
  3594. if (roseOption.border == true) {
  3595. context.stroke();
  3596. }
  3597. });
  3598. if (opts.dataLabel !== false && process === 1) {
  3599. var valid = false;
  3600. for (var i = 0, len = series.length; i < len; i++) {
  3601. if (series[i].data > 0) {
  3602. valid = true;
  3603. break;
  3604. }
  3605. }
  3606. if (valid) {
  3607. drawPieText(series, opts, config, context, radius, centerPosition);
  3608. }
  3609. }
  3610. return {
  3611. center: centerPosition,
  3612. radius: radius,
  3613. series: series
  3614. };
  3615. }
  3616. function drawMyArcbarDataPoints(series, opts, config, context) {
  3617. var title = opts.title.name || '';
  3618. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  3619. var arcbarOption = assign({}, {
  3620. startAngle: 0.75,
  3621. endAngle: 0.25,
  3622. type: 'default',
  3623. width: 12 * opts.pixelRatio,
  3624. gap: 2 * opts.pixelRatio
  3625. }, opts.extra.arcbar);
  3626. series = getArcbarDataPoints(series, arcbarOption, process);
  3627. var centerPosition;
  3628. if (arcbarOption.center) {
  3629. centerPosition = arcbarOption.center;
  3630. } else {
  3631. centerPosition = {
  3632. x: opts.width / 2,
  3633. y: opts.height / 2
  3634. };
  3635. }
  3636. var radius;
  3637. if (arcbarOption.radius) {
  3638. radius = arcbarOption.radius;
  3639. } else {
  3640. radius = Math.min(centerPosition.x, centerPosition.y);
  3641. radius -= 5 * opts.pixelRatio;
  3642. radius -= arcbarOption.width / 2;
  3643. }
  3644. //偏移
  3645. centerPosition.x += 20;
  3646. centerPosition.y += 10;
  3647. //中心圆形背景
  3648. var innerRadius = radius - arcbarOption.width;
  3649. var pieRadius = radius - arcbarOption.width * 2;
  3650. context.beginPath();
  3651. // let gradient = context.createLinearGradient(
  3652. // centerPosition.x,
  3653. // centerPosition.y - pieRadius,
  3654. // centerPosition.x,
  3655. // centerPosition.y + pieRadius);
  3656. // //配置渐变填充(起点:中心点向上减半径;结束点中心点向下加半径)
  3657. // gradient.addColorStop('0', hexToRgb(series[0].color, 0.3));
  3658. // gradient.addColorStop('1.0', hexToRgb("#FFFFFF", 0.1));
  3659. context.setFillStyle(arcbarOption.backgroundMiddle);
  3660. context.arc(centerPosition.x, centerPosition.y, pieRadius, 0, 2 * Math.PI, false);
  3661. context.fill();
  3662. //画进度条背景
  3663. // context.setLineWidth(arcbarOption.width);
  3664. // context.setStrokeStyle(hexToRgb(series[0].color, 0.3));
  3665. // context.setLineCap('round');
  3666. // context.beginPath();
  3667. // context.arc(centerPosition.x, centerPosition.y, innerRadius, arcbarOption.startAngle * Math.PI,
  3668. // arcbarOption.endAngle *
  3669. // Math.PI, false);
  3670. // context.stroke();
  3671. for (let i = 0; i < series.length; i++) {
  3672. let eachSeries = series[i];
  3673. //背景颜色
  3674. // 创建线性渐变
  3675. var grd = context.createLinearGradient(0, 0, 0, opts.height);
  3676. grd.addColorStop(0, arcbarOption.backgroundColor);
  3677. grd.addColorStop(0.5, arcbarOption.backgroundColor);
  3678. grd.addColorStop(0.8, arcbarOption.backgroundEndColor);
  3679. // 填充渐变
  3680. context.setLineWidth(arcbarOption.width * 0.3);
  3681. context.setStrokeStyle(grd || '#E9E9E9');
  3682. context.setLineCap('round');
  3683. context.beginPath();
  3684. let _radius = radius - (arcbarOption.width + arcbarOption.gap) * i;
  3685. if (arcbarOption.type == 'default') {
  3686. context.arc(centerPosition.x, centerPosition.y, _radius, arcbarOption.startAngle *
  3687. Math.PI, arcbarOption.endAngle * Math.PI, false);
  3688. } else {
  3689. context.arc(centerPosition.x, centerPosition.y, _radius, 0, 2 * Math.PI,
  3690. false);
  3691. }
  3692. context.stroke();
  3693. //进度条
  3694. var grd2 = context.createLinearGradient(0, 0, 0, opts.height);
  3695. grd2.addColorStop(0, arcbarOption.lineColor);
  3696. grd2.addColorStop(0.75, arcbarOption.lineColor);
  3697. grd2.addColorStop(0.8, arcbarOption.lineEndColor);
  3698. context.setLineWidth(arcbarOption.width);
  3699. context.setStrokeStyle(grd2 || '#E9E9E9');
  3700. context.setLineCap('round');
  3701. context.beginPath();
  3702. context.arc(centerPosition.x, centerPosition.y, _radius, arcbarOption.startAngle *
  3703. Math.PI, eachSeries._proportion_ * Math.PI, false);
  3704. context.stroke();
  3705. //绘制顶点圆
  3706. if (title) {
  3707. context.setLineWidth(arcbarOption.width * 0.3);
  3708. context.setStrokeStyle('rgba(142, 142, 251, 255)');
  3709. context.setLineCap('round');
  3710. context.beginPath();
  3711. let _endPointX = Math.cos(eachSeries._proportion_ * Math.PI) * _radius + centerPosition.x;
  3712. let _endPointY = Math.sin(eachSeries._proportion_ * Math.PI) * _radius + centerPosition.y;
  3713. context.arc(_endPointX, _endPointY, 1, 0, 2 * Math.PI, false);
  3714. context.fillStyle = 'rgba(142, 142, 251, 255)';
  3715. context.fill();
  3716. context.stroke();
  3717. }
  3718. }
  3719. //绘制提示点
  3720. context.setLineWidth(6);
  3721. context.setStrokeStyle('#E9E9E9');
  3722. context.setLineCap('round');
  3723. let _radius = radius + (arcbarOption.width + arcbarOption.gap) * 2;
  3724. let _tipPointX = 0;
  3725. let _tipPointY = 0;
  3726. for (let i = 0, j = -1.2; i < 5; i++, j += 0.35) {
  3727. // 左边第一个点
  3728. context.beginPath();
  3729. _tipPointX = Math.cos(j * Math.PI) * (_radius - 5) + centerPosition.x;
  3730. _tipPointY = Math.sin(j * Math.PI) * (_radius - 5) + centerPosition.y;
  3731. context.arc(_tipPointX, _tipPointY, 1, 0, 2 * Math.PI, false);
  3732. // context.fill();
  3733. context.stroke();
  3734. }
  3735. //提示点圆环
  3736. context.setLineWidth(1);
  3737. context.setStrokeStyle(hexToRgb('#E9E9E9', 0.2));
  3738. context.setLineCap('round');
  3739. context.beginPath();
  3740. context.arc(centerPosition.x, centerPosition.y, _radius - 5,
  3741. arcbarOption.startAngle * Math.PI,
  3742. arcbarOption.endAngle * Math.PI, false);
  3743. context.stroke();
  3744. drawMyRingTitle(opts, config, context, centerPosition);
  3745. return {
  3746. center: centerPosition,
  3747. radius: radius,
  3748. series: series
  3749. };
  3750. }
  3751. function drawMyGradualChangeArcbarDataPoints(series, opts, config, context) {
  3752. var title = opts.title.name || '';
  3753. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  3754. var arcbarOption = assign({}, {
  3755. startAngle: 0.75,
  3756. endAngle: 0.25,
  3757. type: 'default',
  3758. width: 12 * opts.pixelRatio,
  3759. gap: 2 * opts.pixelRatio
  3760. }, opts.extra.arcbar);
  3761. series = getArcbarDataPoints(series, arcbarOption, process);
  3762. var centerPosition;
  3763. if (arcbarOption.center) {
  3764. centerPosition = arcbarOption.center;
  3765. } else {
  3766. centerPosition = {
  3767. x: opts.width / 2,
  3768. y: opts.height / 2
  3769. };
  3770. }
  3771. var radius;
  3772. if (arcbarOption.radius) {
  3773. radius = arcbarOption.radius;
  3774. } else {
  3775. radius = Math.min(centerPosition.x, centerPosition.y);
  3776. radius -= 5 * opts.pixelRatio;
  3777. radius -= arcbarOption.width / 2;
  3778. }
  3779. //偏移
  3780. centerPosition.x += 20;
  3781. centerPosition.y += 10;
  3782. //中心圆形背景
  3783. var innerRadius = radius - arcbarOption.width;
  3784. var pieRadius = radius + 11; //radius - arcbarOption.width * 2;
  3785. context.beginPath();
  3786. context.setFillStyle(arcbarOption.backgroundMiddle);
  3787. context.arc(centerPosition.x, centerPosition.y, pieRadius, 0, 2 * Math.PI, false);
  3788. context.fill();
  3789. for (let i = 0; i < series.length; i++) {
  3790. let eachSeries = series[i];
  3791. // eachSeries._proportion_ = 2.25;
  3792. //背景颜色
  3793. // 创建线性渐变
  3794. var grd = context.createLinearGradient(0, 0, 0, opts.height);
  3795. grd.addColorStop(0, arcbarOption.backgroundColor);
  3796. grd.addColorStop(0.5, arcbarOption.backgroundColor);
  3797. grd.addColorStop(0.8, arcbarOption.backgroundEndColor);
  3798. // 填充渐变
  3799. context.setLineWidth(arcbarOption.width * 0.3);
  3800. context.setStrokeStyle(grd || '#E9E9E9');
  3801. context.setLineCap('round');
  3802. context.beginPath();
  3803. let _radius = radius - (arcbarOption.width + arcbarOption.gap) * i;
  3804. if (arcbarOption.type == 'default') {
  3805. context.arc(centerPosition.x, centerPosition.y, _radius, arcbarOption.startAngle *
  3806. Math.PI, arcbarOption.endAngle * Math.PI, false);
  3807. } else {
  3808. context.arc(centerPosition.x, centerPosition.y, _radius, 0, 2 * Math.PI,
  3809. false);
  3810. }
  3811. context.stroke();
  3812. //进度条
  3813. var grd2 = context.createLinearGradient(0, 0, 0, opts.height);
  3814. grd2.addColorStop(0, arcbarOption.lineColor);
  3815. grd2.addColorStop(0.3, arcbarOption.lineColor);
  3816. grd2.addColorStop(0.8, arcbarOption.lineEndColor);
  3817. context.setLineWidth(arcbarOption.width);
  3818. context.setStrokeStyle(grd2 || '#E9E9E9');
  3819. context.setLineCap('round');
  3820. context.beginPath();
  3821. context.arc(centerPosition.x, centerPosition.y, _radius, arcbarOption.startAngle *
  3822. Math.PI, eachSeries._proportion_ * Math.PI, false);
  3823. context.stroke();
  3824. //绘制顶点圆
  3825. if (title) {
  3826. context.setLineWidth(arcbarOption.width * 0.3);
  3827. context.setStrokeStyle('rgba(142, 142, 251, 255)');
  3828. context.setLineCap('round');
  3829. context.beginPath();
  3830. let _endPointX = Math.cos(eachSeries._proportion_ * Math.PI) * _radius + centerPosition.x;
  3831. let _endPointY = Math.sin(eachSeries._proportion_ * Math.PI) * _radius + centerPosition.y;
  3832. context.arc(_endPointX, _endPointY, 1, 0, 2 * Math.PI, false);
  3833. context.fillStyle = 'rgba(142, 142, 251, 255)';
  3834. context.fill();
  3835. context.stroke();
  3836. }
  3837. }
  3838. //绘制提示点
  3839. // context.setLineWidth(6);
  3840. // context.setStrokeStyle('#E9E9E9');
  3841. // context.setLineCap('round');
  3842. let _radius = radius + (arcbarOption.width + arcbarOption.gap) * 2;
  3843. // let _tipPointX = 0;
  3844. // let _tipPointY = 0;
  3845. // for (let i = 0, j = -1.2; i < 5; i++, j += 0.35) {
  3846. // // 左边第一个点
  3847. // context.beginPath();
  3848. // _tipPointX = Math.cos(j * Math.PI) * (_radius - 5) + centerPosition.x;
  3849. // _tipPointY = Math.sin(j * Math.PI) * (_radius - 5) + centerPosition.y;
  3850. // context.arc(_tipPointX, _tipPointY, 1, 0, 2 * Math.PI, false);
  3851. // // context.fill();
  3852. // context.stroke();
  3853. // }
  3854. //提示点圆环
  3855. context.setLineWidth(1);
  3856. context.setStrokeStyle(hexToRgb('#E9E9E9', 0.2));
  3857. context.setLineCap('round');
  3858. context.beginPath();
  3859. context.arc(centerPosition.x, centerPosition.y, _radius +2 ,0,2 * Math.PI, false);
  3860. context.stroke();
  3861. return {
  3862. center: centerPosition,
  3863. radius: radius,
  3864. series: series
  3865. };
  3866. }
  3867. function drawArcbarDataPoints(series, opts, config, context) {
  3868. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  3869. var arcbarOption = assign({}, {
  3870. startAngle: 0.75,
  3871. endAngle: 0.25,
  3872. type: 'default',
  3873. width: 12 * opts.pixelRatio,
  3874. gap: 2 * opts.pixelRatio
  3875. }, opts.extra.arcbar);
  3876. series = getArcbarDataPoints(series, arcbarOption, process);
  3877. var centerPosition;
  3878. if (arcbarOption.center) {
  3879. centerPosition = arcbarOption.center;
  3880. } else {
  3881. centerPosition = {
  3882. x: opts.width / 2,
  3883. y: opts.height / 2
  3884. };
  3885. }
  3886. var radius;
  3887. if (arcbarOption.radius) {
  3888. radius = arcbarOption.radius;
  3889. } else {
  3890. radius = Math.min(centerPosition.x, centerPosition.y);
  3891. radius -= 5 * opts.pixelRatio;
  3892. radius -= arcbarOption.width / 2;
  3893. }
  3894. for (let i = 0; i < series.length; i++) {
  3895. let eachSeries = series[i];
  3896. //背景颜色
  3897. context.setLineWidth(arcbarOption.width);
  3898. context.setStrokeStyle(arcbarOption.backgroundColor || '#E9E9E9');
  3899. context.setLineCap('round');
  3900. context.beginPath();
  3901. if (arcbarOption.type == 'default') {
  3902. context.arc(centerPosition.x, centerPosition.y, radius - (arcbarOption.width + arcbarOption.gap) * i,
  3903. arcbarOption.startAngle *
  3904. Math.PI, arcbarOption.endAngle * Math.PI, false);
  3905. } else {
  3906. context.arc(centerPosition.x, centerPosition.y, radius - (arcbarOption.width + arcbarOption.gap) * i, 0, 2 *
  3907. Math.PI,
  3908. false);
  3909. }
  3910. context.stroke();
  3911. //进度条
  3912. context.setLineWidth(arcbarOption.width);
  3913. context.setStrokeStyle(eachSeries.color);
  3914. context.setLineCap('round');
  3915. context.beginPath();
  3916. context.arc(centerPosition.x, centerPosition.y, radius - (arcbarOption.width + arcbarOption.gap) * i,
  3917. arcbarOption.startAngle *
  3918. Math.PI, eachSeries._proportion_ * Math.PI, false);
  3919. context.stroke();
  3920. }
  3921. drawRingTitle(opts, config, context, centerPosition);
  3922. return {
  3923. center: centerPosition,
  3924. radius: radius,
  3925. series: series
  3926. };
  3927. }
  3928. function drawGaugeDataPoints(categories, series, opts, config, context) {
  3929. var process = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 1;
  3930. var gaugeOption = assign({}, {
  3931. type: 'default',
  3932. startAngle: 0.75,
  3933. endAngle: 0.25,
  3934. width: 15,
  3935. splitLine: {
  3936. fixRadius: 0,
  3937. splitNumber: 10,
  3938. width: 15,
  3939. color: '#FFFFFF',
  3940. childNumber: 5,
  3941. childWidth: 5
  3942. },
  3943. pointer: {
  3944. width: 15,
  3945. color: 'auto'
  3946. }
  3947. }, opts.extra.gauge);
  3948. if (gaugeOption.oldAngle == undefined) {
  3949. gaugeOption.oldAngle = gaugeOption.startAngle;
  3950. }
  3951. if (gaugeOption.oldData == undefined) {
  3952. gaugeOption.oldData = 0;
  3953. }
  3954. categories = getGaugeAxisPoints(categories, gaugeOption.startAngle, gaugeOption.endAngle);
  3955. var centerPosition = {
  3956. x: opts.width / 2,
  3957. y: opts.height / 2
  3958. };
  3959. var radius = Math.min(centerPosition.x, centerPosition.y);
  3960. radius -= 5 * opts.pixelRatio;
  3961. radius -= gaugeOption.width / 2;
  3962. var innerRadius = radius - gaugeOption.width;
  3963. var totalAngle = 0;
  3964. //判断仪表盘的样式:default百度样式,progress新样式
  3965. if (gaugeOption.type == 'progress') {
  3966. //## 第一步画中心圆形背景和进度条背景
  3967. //中心圆形背景
  3968. var pieRadius = radius - gaugeOption.width * 3;
  3969. context.beginPath();
  3970. let gradient = context.createLinearGradient(centerPosition.x, centerPosition.y - pieRadius, centerPosition.x,
  3971. centerPosition.y + pieRadius);
  3972. //配置渐变填充(起点:中心点向上减半径;结束点中心点向下加半径)
  3973. gradient.addColorStop('0', hexToRgb(series[0].color, 0.3));
  3974. gradient.addColorStop('1.0', hexToRgb("#FFFFFF", 0.1));
  3975. context.setFillStyle(gradient);
  3976. context.arc(centerPosition.x, centerPosition.y, pieRadius, 0, 2 * Math.PI, false);
  3977. context.fill();
  3978. //画进度条背景
  3979. context.setLineWidth(gaugeOption.width);
  3980. context.setStrokeStyle(hexToRgb(series[0].color, 0.3));
  3981. context.setLineCap('round');
  3982. context.beginPath();
  3983. context.arc(centerPosition.x, centerPosition.y, innerRadius, gaugeOption.startAngle * Math.PI, gaugeOption
  3984. .endAngle *
  3985. Math.PI, false);
  3986. context.stroke();
  3987. //## 第二步画刻度线
  3988. totalAngle = gaugeOption.startAngle - gaugeOption.endAngle + 1;
  3989. let splitAngle = totalAngle / gaugeOption.splitLine.splitNumber;
  3990. let childAngle = totalAngle / gaugeOption.splitLine.splitNumber / gaugeOption.splitLine.childNumber;
  3991. let startX = -radius - gaugeOption.width * 0.5 - gaugeOption.splitLine.fixRadius;
  3992. let endX = -radius - gaugeOption.width - gaugeOption.splitLine.fixRadius + gaugeOption.splitLine.width;
  3993. context.save();
  3994. context.translate(centerPosition.x, centerPosition.y);
  3995. context.rotate((gaugeOption.startAngle - 1) * Math.PI);
  3996. let len = gaugeOption.splitLine.splitNumber * gaugeOption.splitLine.childNumber + 1;
  3997. let proc = series[0].data * process;
  3998. for (let i = 0; i < len; i++) {
  3999. context.beginPath();
  4000. //刻度线随进度变色
  4001. if (proc > (i / len)) {
  4002. context.setStrokeStyle(hexToRgb(series[0].color, 1));
  4003. } else {
  4004. context.setStrokeStyle(hexToRgb(series[0].color, 0.3));
  4005. }
  4006. context.setLineWidth(3 * opts.pixelRatio);
  4007. context.moveTo(startX, 0);
  4008. context.lineTo(endX, 0);
  4009. context.stroke();
  4010. context.rotate(childAngle * Math.PI);
  4011. }
  4012. context.restore();
  4013. //## 第三步画进度条
  4014. series = getArcbarDataPoints(series, gaugeOption, process);
  4015. context.setLineWidth(gaugeOption.width);
  4016. context.setStrokeStyle(series[0].color);
  4017. context.setLineCap('round');
  4018. context.beginPath();
  4019. context.arc(centerPosition.x, centerPosition.y, innerRadius, gaugeOption.startAngle * Math.PI, series[0]
  4020. ._proportion_ *
  4021. Math.PI, false);
  4022. context.stroke();
  4023. //## 第四步画指针
  4024. let pointerRadius = radius - gaugeOption.width * 2.5;
  4025. context.save();
  4026. context.translate(centerPosition.x, centerPosition.y);
  4027. context.rotate((series[0]._proportion_ - 1) * Math.PI);
  4028. context.beginPath();
  4029. context.setLineWidth(gaugeOption.width / 3);
  4030. let gradient3 = context.createLinearGradient(0, -pointerRadius * 0.6, 0, pointerRadius * 0.6);
  4031. gradient3.addColorStop('0', hexToRgb('#FFFFFF', 0));
  4032. gradient3.addColorStop('0.5', hexToRgb(series[0].color, 1));
  4033. gradient3.addColorStop('1.0', hexToRgb('#FFFFFF', 0));
  4034. context.setStrokeStyle(gradient3);
  4035. context.arc(0, 0, pointerRadius, 0.85 * Math.PI, 1.15 * Math.PI, false);
  4036. context.stroke();
  4037. context.beginPath();
  4038. context.setLineWidth(1);
  4039. context.setStrokeStyle(series[0].color);
  4040. context.setFillStyle(series[0].color);
  4041. context.moveTo(-pointerRadius - gaugeOption.width / 3 / 2, -4);
  4042. context.lineTo(-pointerRadius - gaugeOption.width / 3 / 2 - 4, 0);
  4043. context.lineTo(-pointerRadius - gaugeOption.width / 3 / 2, 4);
  4044. context.lineTo(-pointerRadius - gaugeOption.width / 3 / 2, -4);
  4045. context.stroke();
  4046. context.fill();
  4047. context.restore();
  4048. //default百度样式
  4049. } else {
  4050. //画背景
  4051. context.setLineWidth(gaugeOption.width);
  4052. context.setLineCap('butt');
  4053. for (let i = 0; i < categories.length; i++) {
  4054. let eachCategories = categories[i];
  4055. context.beginPath();
  4056. context.setStrokeStyle(eachCategories.color);
  4057. context.arc(centerPosition.x, centerPosition.y, radius, eachCategories._startAngle_ * Math.PI,
  4058. eachCategories._endAngle_ *
  4059. Math.PI, false);
  4060. context.stroke();
  4061. }
  4062. context.save();
  4063. //画刻度线
  4064. totalAngle = gaugeOption.startAngle - gaugeOption.endAngle + 1;
  4065. let splitAngle = totalAngle / gaugeOption.splitLine.splitNumber;
  4066. let childAngle = totalAngle / gaugeOption.splitLine.splitNumber / gaugeOption.splitLine.childNumber;
  4067. let startX = -radius - gaugeOption.width * 0.5 - gaugeOption.splitLine.fixRadius;
  4068. let endX = -radius - gaugeOption.width * 0.5 - gaugeOption.splitLine.fixRadius + gaugeOption.splitLine.width;
  4069. let childendX = -radius - gaugeOption.width * 0.5 - gaugeOption.splitLine.fixRadius + gaugeOption.splitLine
  4070. .childWidth;
  4071. context.translate(centerPosition.x, centerPosition.y);
  4072. context.rotate((gaugeOption.startAngle - 1) * Math.PI);
  4073. for (let i = 0; i < gaugeOption.splitLine.splitNumber + 1; i++) {
  4074. context.beginPath();
  4075. context.setStrokeStyle(gaugeOption.splitLine.color);
  4076. context.setLineWidth(2 * opts.pixelRatio);
  4077. context.moveTo(startX, 0);
  4078. context.lineTo(endX, 0);
  4079. context.stroke();
  4080. context.rotate(splitAngle * Math.PI);
  4081. }
  4082. context.restore();
  4083. context.save();
  4084. context.translate(centerPosition.x, centerPosition.y);
  4085. context.rotate((gaugeOption.startAngle - 1) * Math.PI);
  4086. for (let i = 0; i < gaugeOption.splitLine.splitNumber * gaugeOption.splitLine.childNumber + 1; i++) {
  4087. context.beginPath();
  4088. context.setStrokeStyle(gaugeOption.splitLine.color);
  4089. context.setLineWidth(1 * opts.pixelRatio);
  4090. context.moveTo(startX, 0);
  4091. context.lineTo(childendX, 0);
  4092. context.stroke();
  4093. context.rotate(childAngle * Math.PI);
  4094. }
  4095. context.restore();
  4096. //画指针
  4097. series = getGaugeDataPoints(series, categories, gaugeOption, process);
  4098. for (let i = 0; i < series.length; i++) {
  4099. let eachSeries = series[i];
  4100. context.save();
  4101. context.translate(centerPosition.x, centerPosition.y);
  4102. context.rotate((eachSeries._proportion_ - 1) * Math.PI);
  4103. context.beginPath();
  4104. context.setFillStyle(eachSeries.color);
  4105. context.moveTo(gaugeOption.pointer.width, 0);
  4106. context.lineTo(0, -gaugeOption.pointer.width / 2);
  4107. context.lineTo(-innerRadius, 0);
  4108. context.lineTo(0, gaugeOption.pointer.width / 2);
  4109. context.lineTo(gaugeOption.pointer.width, 0);
  4110. context.closePath();
  4111. context.fill();
  4112. context.beginPath();
  4113. context.setFillStyle('#FFFFFF');
  4114. context.arc(0, 0, gaugeOption.pointer.width / 6, 0, 2 * Math.PI, false);
  4115. context.fill();
  4116. context.restore();
  4117. }
  4118. if (opts.dataLabel !== false) {
  4119. drawGaugeLabel(gaugeOption, radius, centerPosition, opts, config, context);
  4120. }
  4121. }
  4122. //画仪表盘标题,副标题
  4123. drawRingTitle(opts, config, context, centerPosition);
  4124. if (process === 1 && opts.type === 'gauge') {
  4125. opts.extra.gauge.oldAngle = series[0]._proportion_;
  4126. opts.extra.gauge.oldData = series[0].data;
  4127. }
  4128. return {
  4129. center: centerPosition,
  4130. radius: radius,
  4131. innerRadius: innerRadius,
  4132. categories: categories,
  4133. totalAngle: totalAngle
  4134. };
  4135. }
  4136. function drawRadarDataPoints(series, opts, config, context) {
  4137. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  4138. var radarOption = assign({}, {
  4139. gridColor: '#cccccc',
  4140. labelColor: '#666666',
  4141. opacity: 0.2,
  4142. gridCount: 3
  4143. }, opts.extra.radar);
  4144. var coordinateAngle = getRadarCoordinateSeries(opts.categories.length);
  4145. var centerPosition = {
  4146. x: opts.area[3] + (opts.width - opts.area[1] - opts.area[3]) / 2,
  4147. y: opts.area[0] + (opts.height - opts.area[0] - opts.area[2]) / 2
  4148. };
  4149. var radius = Math.min(centerPosition.x - (getMaxTextListLength(opts.categories) + config.radarLabelTextMargin),
  4150. centerPosition.y - config.radarLabelTextMargin);
  4151. //TODO逻辑不对
  4152. radius -= opts.padding[1];
  4153. // draw grid
  4154. context.beginPath();
  4155. context.setLineWidth(1 * opts.pixelRatio);
  4156. context.setStrokeStyle(radarOption.gridColor);
  4157. coordinateAngle.forEach(function(angle) {
  4158. var pos = convertCoordinateOrigin(radius * Math.cos(angle), radius * Math.sin(angle), centerPosition);
  4159. context.moveTo(centerPosition.x, centerPosition.y);
  4160. context.lineTo(pos.x, pos.y);
  4161. });
  4162. context.stroke();
  4163. context.closePath();
  4164. // draw split line grid
  4165. var _loop = function _loop(i) {
  4166. var startPos = {};
  4167. context.beginPath();
  4168. context.setLineWidth(1 * opts.pixelRatio);
  4169. context.setStrokeStyle(radarOption.gridColor);
  4170. coordinateAngle.forEach(function(angle, index) {
  4171. var pos = convertCoordinateOrigin(radius / radarOption.gridCount * i * Math.cos(angle), radius /
  4172. radarOption.gridCount *
  4173. i * Math.sin(angle), centerPosition);
  4174. if (index === 0) {
  4175. startPos = pos;
  4176. context.moveTo(pos.x, pos.y);
  4177. } else {
  4178. context.lineTo(pos.x, pos.y);
  4179. }
  4180. });
  4181. context.lineTo(startPos.x, startPos.y);
  4182. context.stroke();
  4183. context.closePath();
  4184. };
  4185. for (var i = 1; i <= radarOption.gridCount; i++) {
  4186. _loop(i);
  4187. }
  4188. var radarDataPoints = getRadarDataPoints(coordinateAngle, centerPosition, radius, series, opts, process);
  4189. radarDataPoints.forEach(function(eachSeries, seriesIndex) {
  4190. // 绘制区域数据
  4191. context.beginPath();
  4192. context.setFillStyle(hexToRgb(eachSeries.color, radarOption.opacity));
  4193. eachSeries.data.forEach(function(item, index) {
  4194. if (index === 0) {
  4195. context.moveTo(item.position.x, item.position.y);
  4196. } else {
  4197. context.lineTo(item.position.x, item.position.y);
  4198. }
  4199. });
  4200. context.closePath();
  4201. context.fill();
  4202. if (opts.dataPointShape !== false) {
  4203. var points = eachSeries.data.map(function(item) {
  4204. return item.position;
  4205. });
  4206. drawPointShape(points, eachSeries.color, eachSeries.pointShape, context, opts);
  4207. }
  4208. });
  4209. // draw label text
  4210. drawRadarLabel(coordinateAngle, radius, centerPosition, opts, config, context);
  4211. return {
  4212. center: centerPosition,
  4213. radius: radius,
  4214. angleList: coordinateAngle
  4215. };
  4216. }
  4217. function normalInt(min, max, iter) {
  4218. iter = iter == 0 ? 1 : iter;
  4219. var arr = [];
  4220. for (var i = 0; i < iter; i++) {
  4221. arr[i] = Math.random();
  4222. };
  4223. return Math.floor(arr.reduce(function(i, j) {
  4224. return i + j
  4225. }) / iter * (max - min)) + min;
  4226. };
  4227. function collisionNew(area, points, width, height) {
  4228. var isIn = false;
  4229. for (let i = 0; i < points.length; i++) {
  4230. if (points[i].area) {
  4231. if (area[3] < points[i].area[1] || area[0] > points[i].area[2] || area[1] > points[i].area[3] || area[2] <
  4232. points[i]
  4233. .area[0]) {
  4234. if (area[0] < 0 || area[1] < 0 || area[2] > width || area[3] > height) {
  4235. isIn = true;
  4236. break;
  4237. } else {
  4238. isIn = false;
  4239. }
  4240. } else {
  4241. isIn = true;
  4242. break;
  4243. }
  4244. }
  4245. }
  4246. return isIn;
  4247. };
  4248. function getBoundingBox(data) {
  4249. var bounds = {},
  4250. coords;
  4251. bounds.xMin = 180;
  4252. bounds.xMax = 0;
  4253. bounds.yMin = 90;
  4254. bounds.yMax = 0
  4255. for (var i = 0; i < data.length; i++) {
  4256. var coorda = data[i].geometry.coordinates
  4257. for (var k = 0; k < coorda.length; k++) {
  4258. coords = coorda[k];
  4259. if (coords.length == 1) {
  4260. coords = coords[0]
  4261. }
  4262. for (var j = 0; j < coords.length; j++) {
  4263. var longitude = coords[j][0];
  4264. var latitude = coords[j][1];
  4265. var point = {
  4266. x: longitude,
  4267. y: latitude
  4268. }
  4269. bounds.xMin = bounds.xMin < point.x ? bounds.xMin : point.x;
  4270. bounds.xMax = bounds.xMax > point.x ? bounds.xMax : point.x;
  4271. bounds.yMin = bounds.yMin < point.y ? bounds.yMin : point.y;
  4272. bounds.yMax = bounds.yMax > point.y ? bounds.yMax : point.y;
  4273. }
  4274. }
  4275. }
  4276. return bounds;
  4277. }
  4278. function coordinateToPoint(latitude, longitude, bounds, scale, xoffset, yoffset) {
  4279. return {
  4280. x: (longitude - bounds.xMin) * scale + xoffset,
  4281. y: (bounds.yMax - latitude) * scale + yoffset
  4282. };
  4283. }
  4284. function pointToCoordinate(pointY, pointX, bounds, scale, xoffset, yoffset) {
  4285. return {
  4286. x: (pointX - xoffset) / scale + bounds.xMin,
  4287. y: bounds.yMax - (pointY - yoffset) / scale
  4288. };
  4289. }
  4290. function isRayIntersectsSegment(poi, s_poi, e_poi) {
  4291. if (s_poi[1] == e_poi[1]) {
  4292. return false;
  4293. }
  4294. if (s_poi[1] > poi[1] && e_poi[1] > poi[1]) {
  4295. return false;
  4296. }
  4297. if (s_poi[1] < poi[1] && e_poi[1] < poi[1]) {
  4298. return false;
  4299. }
  4300. if (s_poi[1] == poi[1] && e_poi[1] > poi[1]) {
  4301. return false;
  4302. }
  4303. if (e_poi[1] == poi[1] && s_poi[1] > poi[1]) {
  4304. return false;
  4305. }
  4306. if (s_poi[0] < poi[0] && e_poi[1] < poi[1]) {
  4307. return false;
  4308. }
  4309. let xseg = e_poi[0] - (e_poi[0] - s_poi[0]) * (e_poi[1] - poi[1]) / (e_poi[1] - s_poi[1]);
  4310. if (xseg < poi[0]) {
  4311. return false;
  4312. } else {
  4313. return true;
  4314. }
  4315. }
  4316. function isPoiWithinPoly(poi, poly) {
  4317. let sinsc = 0;
  4318. for (let i = 0; i < poly.length; i++) {
  4319. let epoly = poly[i][0];
  4320. if (poly.length == 1) {
  4321. epoly = poly[i][0]
  4322. }
  4323. for (let j = 0; j < epoly.length - 1; j++) {
  4324. let s_poi = epoly[j];
  4325. let e_poi = epoly[j + 1];
  4326. if (isRayIntersectsSegment(poi, s_poi, e_poi)) {
  4327. sinsc += 1;
  4328. }
  4329. }
  4330. }
  4331. if (sinsc % 2 == 1) {
  4332. return true;
  4333. } else {
  4334. return false;
  4335. }
  4336. }
  4337. function drawMapDataPoints(series, opts, config, context) {
  4338. var mapOption = assign({}, {
  4339. border: true,
  4340. borderWidth: 1,
  4341. borderColor: '#666666',
  4342. fillOpacity: 0.6,
  4343. activeBorderColor: '#f04864',
  4344. activeFillColor: '#facc14',
  4345. activeFillOpacity: 1
  4346. }, opts.extra.map);
  4347. var coords, point;
  4348. var data = series;
  4349. var bounds = getBoundingBox(data);
  4350. var xScale = opts.width / Math.abs(bounds.xMax - bounds.xMin);
  4351. var yScale = opts.height / Math.abs(bounds.yMax - bounds.yMin);
  4352. var scale = xScale < yScale ? xScale : yScale;
  4353. var xoffset = opts.width / 2 - Math.abs(bounds.xMax - bounds.xMin) / 2 * scale;
  4354. var yoffset = opts.height / 2 - Math.abs(bounds.yMax - bounds.yMin) / 2 * scale;
  4355. context.beginPath();
  4356. context.clearRect(0, 0, opts.width, opts.height);
  4357. context.setFillStyle(opts.background || '#FFFFFF');
  4358. context.rect(0, 0, opts.width, opts.height);
  4359. context.fill();
  4360. for (var i = 0; i < data.length; i++) {
  4361. context.beginPath();
  4362. context.setLineWidth(mapOption.borderWidth * opts.pixelRatio);
  4363. context.setStrokeStyle(mapOption.borderColor);
  4364. context.setFillStyle(hexToRgb(series[i].color, mapOption.fillOpacity));
  4365. if (opts.tooltip) {
  4366. if (opts.tooltip.index == i) {
  4367. context.setStrokeStyle(mapOption.activeBorderColor);
  4368. context.setFillStyle(hexToRgb(mapOption.activeFillColor, mapOption.activeFillOpacity));
  4369. }
  4370. }
  4371. var coorda = data[i].geometry.coordinates
  4372. for (var k = 0; k < coorda.length; k++) {
  4373. coords = coorda[k];
  4374. if (coords.length == 1) {
  4375. coords = coords[0]
  4376. }
  4377. for (var j = 0; j < coords.length; j++) {
  4378. point = coordinateToPoint(coords[j][1], coords[j][0], bounds, scale, xoffset, yoffset)
  4379. if (j === 0) {
  4380. context.beginPath();
  4381. context.moveTo(point.x, point.y);
  4382. } else {
  4383. context.lineTo(point.x, point.y);
  4384. }
  4385. }
  4386. context.fill();
  4387. if (mapOption.border == true) {
  4388. context.stroke();
  4389. }
  4390. }
  4391. if (opts.dataLabel == true) {
  4392. var centerPoint = data[i].properties.centroid;
  4393. if (centerPoint) {
  4394. point = coordinateToPoint(centerPoint[1], centerPoint[0], bounds, scale, xoffset, yoffset);
  4395. let fontSize = data[i].textSize || config.fontSize;
  4396. let text = data[i].properties.name;
  4397. context.beginPath();
  4398. context.setFontSize(fontSize)
  4399. context.setFillStyle(data[i].textColor || '#666666')
  4400. context.fillText(text, point.x - measureText(text, fontSize) / 2, point.y + fontSize / 2);
  4401. context.closePath();
  4402. context.stroke();
  4403. }
  4404. }
  4405. }
  4406. opts.chartData.mapData = {
  4407. bounds: bounds,
  4408. scale: scale,
  4409. xoffset: xoffset,
  4410. yoffset: yoffset
  4411. }
  4412. drawToolTipBridge(opts, config, context, 1);
  4413. context.draw();
  4414. }
  4415. function getWordCloudPoint(opts, type) {
  4416. let points = opts.series.sort(function(a, b) {
  4417. return parseInt(b.textSize) - parseInt(a.textSize);
  4418. });
  4419. switch (type) {
  4420. case 'normal':
  4421. for (let i = 0; i < points.length; i++) {
  4422. let text = points[i].name;
  4423. let tHeight = points[i].textSize;
  4424. let tWidth = measureText(text, tHeight);
  4425. let x, y;
  4426. let area;
  4427. let breaknum = 0;
  4428. while (true) {
  4429. breaknum++;
  4430. x = normalInt(-opts.width / 2, opts.width / 2, 5) - tWidth / 2;
  4431. y = normalInt(-opts.height / 2, opts.height / 2, 5) + tHeight / 2;
  4432. area = [x - 5 + opts.width / 2, y - 5 - tHeight + opts.height / 2, x + tWidth + 5 + opts.width / 2,
  4433. y + 5 + opts.height /
  4434. 2
  4435. ];
  4436. let isCollision = collisionNew(area, points, opts.width, opts.height);
  4437. if (!isCollision) break;
  4438. if (breaknum == 1000) {
  4439. area = [-100, -100, -100, -100];
  4440. break;
  4441. }
  4442. };
  4443. points[i].area = area;
  4444. }
  4445. break;
  4446. case 'vertical':
  4447. function Spin() {
  4448. //获取均匀随机值,是否旋转,旋转的概率为(1-0.5)
  4449. if (Math.random() > 0.7) {
  4450. return true;
  4451. } else {
  4452. return false
  4453. };
  4454. };
  4455. for (let i = 0; i < points.length; i++) {
  4456. let text = points[i].name;
  4457. let tHeight = points[i].textSize;
  4458. let tWidth = measureText(text, tHeight);
  4459. let isSpin = Spin();
  4460. let x, y, area, areav;
  4461. let breaknum = 0;
  4462. while (true) {
  4463. breaknum++;
  4464. let isCollision;
  4465. if (isSpin) {
  4466. x = normalInt(-opts.width / 2, opts.width / 2, 5) - tWidth / 2;
  4467. y = normalInt(-opts.height / 2, opts.height / 2, 5) + tHeight / 2;
  4468. area = [y - 5 - tWidth + opts.width / 2, (-x - 5 + opts.height / 2), y + 5 + opts.width / 2, (-
  4469. x + tHeight + 5 +
  4470. opts.height / 2)];
  4471. areav = [opts.width - (opts.width / 2 - opts.height / 2) - (-x + tHeight + 5 + opts.height /
  4472. 2) - 5, (opts.height /
  4473. 2 - opts.width / 2) + (y - 5 - tWidth + opts.width / 2) - 5, opts.width - (opts
  4474. .width / 2 - opts.height / 2) -
  4475. (-x + tHeight + 5 + opts.height / 2) + tHeight, (opts.height / 2 - opts.width / 2) + (
  4476. y - 5 - tWidth + opts.width /
  4477. 2) + tWidth + 5
  4478. ];
  4479. isCollision = collisionNew(areav, points, opts.height, opts.width);
  4480. } else {
  4481. x = normalInt(-opts.width / 2, opts.width / 2, 5) - tWidth / 2;
  4482. y = normalInt(-opts.height / 2, opts.height / 2, 5) + tHeight / 2;
  4483. area = [x - 5 + opts.width / 2, y - 5 - tHeight + opts.height / 2, x + tWidth + 5 + opts.width /
  4484. 2, y + 5 + opts.height /
  4485. 2
  4486. ];
  4487. isCollision = collisionNew(area, points, opts.width, opts.height);
  4488. }
  4489. if (!isCollision) break;
  4490. if (breaknum == 1000) {
  4491. area = [-1000, -1000, -1000, -1000];
  4492. break;
  4493. }
  4494. };
  4495. if (isSpin) {
  4496. points[i].area = areav;
  4497. points[i].areav = area;
  4498. } else {
  4499. points[i].area = area;
  4500. }
  4501. points[i].rotate = isSpin;
  4502. };
  4503. break;
  4504. }
  4505. return points;
  4506. }
  4507. function drawWordCloudDataPoints(series, opts, config, context) {
  4508. let process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  4509. let wordOption = assign({}, {
  4510. type: 'normal',
  4511. autoColors: true
  4512. }, opts.extra.word);
  4513. context.beginPath();
  4514. context.setFillStyle(opts.background || '#FFFFFF');
  4515. context.rect(0, 0, opts.width, opts.height);
  4516. context.fill();
  4517. context.save();
  4518. let points = opts.chartData.wordCloudData;
  4519. context.translate(opts.width / 2, opts.height / 2);
  4520. for (let i = 0; i < points.length; i++) {
  4521. context.save();
  4522. if (points[i].rotate) {
  4523. context.rotate(90 * Math.PI / 180);
  4524. }
  4525. let text = points[i].name;
  4526. let tHeight = points[i].textSize;
  4527. let tWidth = measureText(text, tHeight);
  4528. context.beginPath();
  4529. context.setStrokeStyle(points[i].color);
  4530. context.setFillStyle(points[i].color);
  4531. context.setFontSize(tHeight);
  4532. if (points[i].rotate) {
  4533. if (points[i].areav[0] > 0) {
  4534. if (opts.tooltip) {
  4535. if (opts.tooltip.index == i) {
  4536. context.strokeText(text, (points[i].areav[0] + 5 - opts.width / 2) * process - tWidth * (1 -
  4537. process) / 2, (
  4538. points[i].areav[1] + 5 + tHeight - opts.height / 2) * process);
  4539. } else {
  4540. context.fillText(text, (points[i].areav[0] + 5 - opts.width / 2) * process - tWidth * (1 -
  4541. process) / 2, (points[
  4542. i].areav[1] + 5 + tHeight - opts.height / 2) * process);
  4543. }
  4544. } else {
  4545. context.fillText(text, (points[i].areav[0] + 5 - opts.width / 2) * process - tWidth * (1 -
  4546. process) / 2, (points[i]
  4547. .areav[1] + 5 + tHeight - opts.height / 2) * process);
  4548. }
  4549. }
  4550. } else {
  4551. if (points[i].area[0] > 0) {
  4552. if (opts.tooltip) {
  4553. if (opts.tooltip.index == i) {
  4554. context.strokeText(text, (points[i].area[0] + 5 - opts.width / 2) * process - tWidth * (1 -
  4555. process) / 2, (points[
  4556. i].area[1] + 5 + tHeight - opts.height / 2) * process);
  4557. } else {
  4558. context.fillText(text, (points[i].area[0] + 5 - opts.width / 2) * process - tWidth * (1 -
  4559. process) / 2, (points[i]
  4560. .area[1] + 5 + tHeight - opts.height / 2) * process);
  4561. }
  4562. } else {
  4563. context.fillText(text, (points[i].area[0] + 5 - opts.width / 2) * process - tWidth * (1 - process) /
  4564. 2, (points[i]
  4565. .area[1] + 5 + tHeight - opts.height / 2) * process);
  4566. }
  4567. }
  4568. }
  4569. context.stroke();
  4570. context.restore();
  4571. }
  4572. context.restore();
  4573. }
  4574. function drawFunnelDataPoints(series, opts, config, context) {
  4575. let process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  4576. let funnelOption = assign({}, {
  4577. activeWidth: 10,
  4578. activeOpacity: 0.3,
  4579. border: false,
  4580. borderWidth: 2,
  4581. borderColor: '#FFFFFF',
  4582. fillOpacity: 1,
  4583. labelAlign: 'right'
  4584. }, opts.extra.funnel);
  4585. let eachSpacing = (opts.height - opts.area[0] - opts.area[2]) / series.length;
  4586. let centerPosition = {
  4587. x: opts.area[3] + (opts.width - opts.area[1] - opts.area[3]) / 2,
  4588. y: opts.height - opts.area[2]
  4589. };
  4590. let activeWidth = funnelOption.activeWidth;
  4591. let radius = Math.min((opts.width - opts.area[1] - opts.area[3]) / 2 - activeWidth, (opts.height - opts.area[0] -
  4592. opts
  4593. .area[2]) / 2 - activeWidth);
  4594. series = getFunnelDataPoints(series, radius, process);
  4595. context.save();
  4596. context.translate(centerPosition.x, centerPosition.y);
  4597. for (let i = 0; i < series.length; i++) {
  4598. if (i == 0) {
  4599. if (opts.tooltip) {
  4600. if (opts.tooltip.index == i) {
  4601. context.beginPath();
  4602. context.setFillStyle(hexToRgb(series[i].color, funnelOption.activeOpacity));
  4603. context.moveTo(-activeWidth, 0);
  4604. context.lineTo(-series[i].radius - activeWidth, -eachSpacing);
  4605. context.lineTo(series[i].radius + activeWidth, -eachSpacing);
  4606. context.lineTo(activeWidth, 0);
  4607. context.lineTo(-activeWidth, 0);
  4608. context.closePath();
  4609. context.fill();
  4610. }
  4611. }
  4612. series[i].funnelArea = [centerPosition.x - series[i].radius, centerPosition.y - eachSpacing, centerPosition
  4613. .x +
  4614. series[i].radius, centerPosition.y
  4615. ];
  4616. context.beginPath();
  4617. context.setLineWidth(funnelOption.borderWidth * opts.pixelRatio);
  4618. context.setStrokeStyle(funnelOption.borderColor);
  4619. context.setFillStyle(hexToRgb(series[i].color, funnelOption.fillOpacity));
  4620. context.moveTo(0, 0);
  4621. context.lineTo(-series[i].radius, -eachSpacing);
  4622. context.lineTo(series[i].radius, -eachSpacing);
  4623. context.lineTo(0, 0);
  4624. context.closePath();
  4625. context.fill();
  4626. if (funnelOption.border == true) {
  4627. context.stroke();
  4628. }
  4629. } else {
  4630. if (opts.tooltip) {
  4631. if (opts.tooltip.index == i) {
  4632. context.beginPath();
  4633. context.setFillStyle(hexToRgb(series[i].color, funnelOption.activeOpacity));
  4634. context.moveTo(0, 0);
  4635. context.lineTo(-series[i - 1].radius - activeWidth, 0);
  4636. context.lineTo(-series[i].radius - activeWidth, -eachSpacing);
  4637. context.lineTo(series[i].radius + activeWidth, -eachSpacing);
  4638. context.lineTo(series[i - 1].radius + activeWidth, 0);
  4639. context.lineTo(0, 0);
  4640. context.closePath();
  4641. context.fill();
  4642. }
  4643. }
  4644. series[i].funnelArea = [centerPosition.x - series[i].radius, centerPosition.y - eachSpacing * (i + 1),
  4645. centerPosition.x + series[i].radius, centerPosition.y - eachSpacing * i
  4646. ];
  4647. context.beginPath();
  4648. context.setLineWidth(funnelOption.borderWidth * opts.pixelRatio);
  4649. context.setStrokeStyle(funnelOption.borderColor);
  4650. context.setFillStyle(hexToRgb(series[i].color, funnelOption.fillOpacity));
  4651. context.moveTo(0, 0);
  4652. context.lineTo(-series[i - 1].radius, 0);
  4653. context.lineTo(-series[i].radius, -eachSpacing);
  4654. context.lineTo(series[i].radius, -eachSpacing);
  4655. context.lineTo(series[i - 1].radius, 0);
  4656. context.lineTo(0, 0);
  4657. context.closePath();
  4658. context.fill();
  4659. if (funnelOption.border == true) {
  4660. context.stroke();
  4661. }
  4662. }
  4663. context.translate(0, -eachSpacing)
  4664. }
  4665. context.restore();
  4666. if (opts.dataLabel !== false && process === 1) {
  4667. drawFunnelText(series, opts, context, eachSpacing, funnelOption.labelAlign, activeWidth, centerPosition);
  4668. }
  4669. return {
  4670. center: centerPosition,
  4671. radius: radius,
  4672. series: series
  4673. };
  4674. }
  4675. function drawFunnelText(series, opts, context, eachSpacing, labelAlign, activeWidth, centerPosition) {
  4676. for (let i = 0; i < series.length; i++) {
  4677. let item = series[i];
  4678. let startX, endX, startY, fontSize;
  4679. let text = item.format ? item.format(+item._proportion_.toFixed(2)) : util.toFixed(item._proportion_ * 100) +
  4680. '%';
  4681. if (labelAlign == 'right') {
  4682. if (i == 0) {
  4683. startX = (item.funnelArea[2] + centerPosition.x) / 2;
  4684. } else {
  4685. startX = (item.funnelArea[2] + series[i - 1].funnelArea[2]) / 2;
  4686. }
  4687. endX = startX + activeWidth * 2;
  4688. startY = item.funnelArea[1] + eachSpacing / 2;
  4689. fontSize = item.textSize || opts.fontSize;
  4690. context.setLineWidth(1 * opts.pixelRatio);
  4691. context.setStrokeStyle(item.color);
  4692. context.setFillStyle(item.color);
  4693. context.beginPath();
  4694. context.moveTo(startX, startY);
  4695. context.lineTo(endX, startY);
  4696. context.stroke();
  4697. context.closePath();
  4698. context.beginPath();
  4699. context.moveTo(endX, startY);
  4700. context.arc(endX, startY, 2, 0, 2 * Math.PI);
  4701. context.closePath();
  4702. context.fill();
  4703. context.beginPath();
  4704. context.setFontSize(fontSize);
  4705. context.setFillStyle(item.textColor || '#666666');
  4706. context.fillText(text, endX + 5, startY + fontSize / 2 - 2);
  4707. context.closePath();
  4708. context.stroke();
  4709. context.closePath();
  4710. } else {
  4711. if (i == 0) {
  4712. startX = (item.funnelArea[0] + centerPosition.x) / 2;
  4713. } else {
  4714. startX = (item.funnelArea[0] + series[i - 1].funnelArea[0]) / 2;
  4715. }
  4716. endX = startX - activeWidth * 2;
  4717. startY = item.funnelArea[1] + eachSpacing / 2;
  4718. fontSize = item.textSize || opts.fontSize;
  4719. context.setLineWidth(1 * opts.pixelRatio);
  4720. context.setStrokeStyle(item.color);
  4721. context.setFillStyle(item.color);
  4722. context.beginPath();
  4723. context.moveTo(startX, startY);
  4724. context.lineTo(endX, startY);
  4725. context.stroke();
  4726. context.closePath();
  4727. context.beginPath();
  4728. context.moveTo(endX, startY);
  4729. context.arc(endX, startY, 2, 0, 2 * Math.PI);
  4730. context.closePath();
  4731. context.fill();
  4732. context.beginPath();
  4733. context.setFontSize(fontSize);
  4734. context.setFillStyle(item.textColor || '#666666');
  4735. context.fillText(text, endX - 5 - measureText(text), startY + fontSize / 2 - 2);
  4736. context.closePath();
  4737. context.stroke();
  4738. context.closePath();
  4739. }
  4740. }
  4741. }
  4742. function drawCanvas(opts, context) {
  4743. context.draw();
  4744. }
  4745. var Timing = {
  4746. easeIn: function easeIn(pos) {
  4747. return Math.pow(pos, 3);
  4748. },
  4749. easeOut: function easeOut(pos) {
  4750. return Math.pow(pos - 1, 3) + 1;
  4751. },
  4752. easeInOut: function easeInOut(pos) {
  4753. if ((pos /= 0.5) < 1) {
  4754. return 0.5 * Math.pow(pos, 3);
  4755. } else {
  4756. return 0.5 * (Math.pow(pos - 2, 3) + 2);
  4757. }
  4758. },
  4759. linear: function linear(pos) {
  4760. return pos;
  4761. }
  4762. };
  4763. function Animation(opts) {
  4764. this.isStop = false;
  4765. opts.duration = typeof opts.duration === 'undefined' ? 1000 : opts.duration;
  4766. opts.timing = opts.timing || 'linear';
  4767. var delay = 17;
  4768. function createAnimationFrame() {
  4769. if (typeof setTimeout !== 'undefined') {
  4770. return function(step, delay) {
  4771. setTimeout(function() {
  4772. var timeStamp = +new Date();
  4773. step(timeStamp);
  4774. }, delay);
  4775. };
  4776. } else if (typeof requestAnimationFrame !== 'undefined') {
  4777. return requestAnimationFrame;
  4778. } else {
  4779. return function(step) {
  4780. step(null);
  4781. };
  4782. }
  4783. };
  4784. var animationFrame = createAnimationFrame();
  4785. var startTimeStamp = null;
  4786. var _step = function step(timestamp) {
  4787. if (timestamp === null || this.isStop === true) {
  4788. opts.onProcess && opts.onProcess(1);
  4789. opts.onAnimationFinish && opts.onAnimationFinish();
  4790. return;
  4791. }
  4792. if (startTimeStamp === null) {
  4793. startTimeStamp = timestamp;
  4794. }
  4795. if (timestamp - startTimeStamp < opts.duration) {
  4796. var process = (timestamp - startTimeStamp) / opts.duration;
  4797. var timingFunction = Timing[opts.timing];
  4798. process = timingFunction(process);
  4799. opts.onProcess && opts.onProcess(process);
  4800. animationFrame(_step, delay);
  4801. } else {
  4802. opts.onProcess && opts.onProcess(1);
  4803. opts.onAnimationFinish && opts.onAnimationFinish();
  4804. }
  4805. };
  4806. _step = _step.bind(this);
  4807. animationFrame(_step, delay);
  4808. }
  4809. // stop animation immediately
  4810. // and tigger onAnimationFinish
  4811. Animation.prototype.stop = function() {
  4812. this.isStop = true;
  4813. };
  4814. function drawCharts(type, opts, config, context) {
  4815. var _this = this;
  4816. var series = opts.series;
  4817. var categories = opts.categories;
  4818. series = fillSeries(series, opts, config);
  4819. var duration = opts.animation ? opts.duration : 0;
  4820. _this.animationInstance && _this.animationInstance.stop();
  4821. var seriesMA = null;
  4822. if (type == 'candle') {
  4823. let average = assign({}, opts.extra.candle.average);
  4824. if (average.show) {
  4825. seriesMA = calCandleMA(average.day, average.name, average.color, series[0].data);
  4826. seriesMA = fillSeries(seriesMA, opts, config);
  4827. opts.seriesMA = seriesMA;
  4828. } else if (opts.seriesMA) {
  4829. seriesMA = opts.seriesMA = fillSeries(opts.seriesMA, opts, config);
  4830. } else {
  4831. seriesMA = series;
  4832. }
  4833. } else {
  4834. seriesMA = series;
  4835. }
  4836. /* 过滤掉show=false的series */
  4837. opts._series_ = series = filterSeries(series);
  4838. //重新计算图表区域
  4839. opts.area = new Array(4);
  4840. //复位绘图区域
  4841. for (let j = 0; j < 4; j++) {
  4842. opts.area[j] = opts.padding[j];
  4843. }
  4844. //通过计算三大区域:图例、X轴、Y轴的大小,确定绘图区域
  4845. var _calLegendData = calLegendData(seriesMA, opts, config, opts.chartData),
  4846. legendHeight = _calLegendData.area.wholeHeight,
  4847. legendWidth = _calLegendData.area.wholeWidth;
  4848. switch (opts.legend.position) {
  4849. case 'top':
  4850. opts.area[0] += legendHeight;
  4851. break;
  4852. case 'bottom':
  4853. opts.area[2] += legendHeight;
  4854. break;
  4855. case 'left':
  4856. opts.area[3] += legendWidth;
  4857. break;
  4858. case 'right':
  4859. opts.area[1] += legendWidth;
  4860. break;
  4861. }
  4862. let _calYAxisData = {},
  4863. yAxisWidth = 0;
  4864. if (opts.type === 'line' || opts.type === 'column' || opts.type === 'area' || opts.type === 'mix' || opts.type ===
  4865. 'candle') {
  4866. _calYAxisData = calYAxisData(series, opts, config);
  4867. yAxisWidth = _calYAxisData.yAxisWidth;
  4868. //如果显示Y轴标题
  4869. if (opts.yAxis.showTitle) {
  4870. let maxTitleHeight = 0;
  4871. for (let i = 0; i < opts.yAxis.data.length; i++) {
  4872. maxTitleHeight = Math.max(maxTitleHeight, opts.yAxis.data[i].titleFontSize ? opts.yAxis.data[i]
  4873. .titleFontSize :
  4874. config.fontSize)
  4875. }
  4876. opts.area[0] += (maxTitleHeight + 6) * opts.pixelRatio;
  4877. }
  4878. let rightIndex = 0,
  4879. leftIndex = 0;
  4880. //计算主绘图区域左右位置
  4881. for (let i = 0; i < yAxisWidth.length; i++) {
  4882. if (yAxisWidth[i].position == 'left') {
  4883. if (leftIndex > 0) {
  4884. opts.area[3] += yAxisWidth[i].width + opts.yAxis.padding;
  4885. } else {
  4886. opts.area[3] += yAxisWidth[i].width;
  4887. }
  4888. leftIndex += 1;
  4889. } else {
  4890. if (rightIndex > 0) {
  4891. opts.area[1] += yAxisWidth[i].width + opts.yAxis.padding;
  4892. } else {
  4893. opts.area[1] += yAxisWidth[i].width;
  4894. }
  4895. rightIndex += 1;
  4896. }
  4897. }
  4898. } else {
  4899. config.yAxisWidth = yAxisWidth;
  4900. }
  4901. opts.chartData.yAxisData = _calYAxisData;
  4902. if (opts.categories && opts.categories.length) {
  4903. opts.chartData.xAxisData = getXAxisPoints(opts.categories, opts, config);
  4904. let _calCategoriesData = calCategoriesData(opts.categories, opts, config, opts.chartData.xAxisData.eachSpacing),
  4905. xAxisHeight = _calCategoriesData.xAxisHeight,
  4906. angle = _calCategoriesData.angle;
  4907. config.xAxisHeight = xAxisHeight;
  4908. config._xAxisTextAngle_ = angle;
  4909. opts.area[2] += xAxisHeight;
  4910. opts.chartData.categoriesData = _calCategoriesData;
  4911. } else {
  4912. if (opts.type === 'line' || opts.type === 'area' || opts.type === 'points') {
  4913. opts.chartData.xAxisData = calXAxisData(series, opts, config);
  4914. categories = opts.chartData.xAxisData.rangesFormat;
  4915. let _calCategoriesData = calCategoriesData(categories, opts, config, opts.chartData.xAxisData.eachSpacing),
  4916. xAxisHeight = _calCategoriesData.xAxisHeight,
  4917. angle = _calCategoriesData.angle;
  4918. config.xAxisHeight = xAxisHeight;
  4919. config._xAxisTextAngle_ = angle;
  4920. opts.area[2] += xAxisHeight;
  4921. opts.chartData.categoriesData = _calCategoriesData;
  4922. } else {
  4923. opts.chartData.xAxisData = {
  4924. xAxisPoints: []
  4925. };
  4926. }
  4927. }
  4928. //计算右对齐偏移距离
  4929. if (opts.enableScroll && opts.xAxis.scrollAlign == 'right' && opts._scrollDistance_ === undefined) {
  4930. let offsetLeft = 0,
  4931. xAxisPoints = opts.chartData.xAxisData.xAxisPoints,
  4932. startX = opts.chartData.xAxisData.startX,
  4933. endX = opts.chartData.xAxisData.endX,
  4934. eachSpacing = opts.chartData.xAxisData.eachSpacing;
  4935. let totalWidth = eachSpacing * (xAxisPoints.length - 1);
  4936. let screenWidth = endX - startX;
  4937. offsetLeft = screenWidth - totalWidth;
  4938. _this.scrollOption = {
  4939. currentOffset: offsetLeft,
  4940. startTouchX: offsetLeft,
  4941. distance: 0,
  4942. lastMoveTime: 0
  4943. };
  4944. opts._scrollDistance_ = offsetLeft;
  4945. }
  4946. if (type === 'pie' || type === 'ring' || type === 'rose') {
  4947. config._pieTextMaxLength_ = opts.dataLabel === false ? 0 : getPieTextMaxLength(seriesMA);
  4948. }
  4949. switch (type) {
  4950. case 'word':
  4951. let wordOption = assign({}, {
  4952. type: 'normal',
  4953. autoColors: true
  4954. }, opts.extra.word);
  4955. if (opts.updateData == true || opts.updateData == undefined) {
  4956. opts.chartData.wordCloudData = getWordCloudPoint(opts, wordOption.type);
  4957. }
  4958. this.animationInstance = new Animation({
  4959. timing: 'easeInOut',
  4960. duration: duration,
  4961. onProcess: function(process) {
  4962. context.clearRect(0, 0, opts.width, opts.height);
  4963. if (opts.rotate) {
  4964. contextRotate(context, opts);
  4965. }
  4966. drawWordCloudDataPoints(series, opts, config, context, process);
  4967. drawCanvas(opts, context);
  4968. },
  4969. onAnimationFinish: function onAnimationFinish() {
  4970. _this.event.trigger('renderComplete');
  4971. }
  4972. });
  4973. break;
  4974. case 'map':
  4975. context.clearRect(0, 0, opts.width, opts.height);
  4976. drawMapDataPoints(series, opts, config, context);
  4977. break;
  4978. case 'funnel':
  4979. this.animationInstance = new Animation({
  4980. timing: 'easeInOut',
  4981. duration: duration,
  4982. onProcess: function(process) {
  4983. context.clearRect(0, 0, opts.width, opts.height);
  4984. if (opts.rotate) {
  4985. contextRotate(context, opts);
  4986. }
  4987. opts.chartData.funnelData = drawFunnelDataPoints(series, opts, config, context,
  4988. process);
  4989. drawLegend(opts.series, opts, config, context, opts.chartData);
  4990. drawToolTipBridge(opts, config, context, process);
  4991. drawCanvas(opts, context);
  4992. },
  4993. onAnimationFinish: function onAnimationFinish() {
  4994. _this.event.trigger('renderComplete');
  4995. }
  4996. });
  4997. break;
  4998. case 'line':
  4999. this.animationInstance = new Animation({
  5000. timing: 'easeIn',
  5001. duration: duration,
  5002. onProcess: function onProcess(process) {
  5003. context.clearRect(0, 0, opts.width, opts.height);
  5004. if (opts.rotate) {
  5005. contextRotate(context, opts);
  5006. }
  5007. drawYAxisGrid(categories, opts, config, context);
  5008. drawXAxis(categories, opts, config, context);
  5009. var _drawLineDataPoints = drawLineDataPoints(series, opts, config, context, process),
  5010. xAxisPoints = _drawLineDataPoints.xAxisPoints,
  5011. calPoints = _drawLineDataPoints.calPoints,
  5012. eachSpacing = _drawLineDataPoints.eachSpacing;
  5013. opts.chartData.xAxisPoints = xAxisPoints;
  5014. opts.chartData.calPoints = calPoints;
  5015. opts.chartData.eachSpacing = eachSpacing;
  5016. drawYAxis(series, opts, config, context);
  5017. if (opts.enableMarkLine !== false && process === 1) {
  5018. drawMarkLine(opts, config, context);
  5019. }
  5020. drawLegend(opts.series, opts, config, context, opts.chartData);
  5021. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  5022. drawCanvas(opts, context);
  5023. },
  5024. onAnimationFinish: function onAnimationFinish() {
  5025. _this.event.trigger('renderComplete');
  5026. }
  5027. });
  5028. break;
  5029. case 'mix':
  5030. this.animationInstance = new Animation({
  5031. timing: 'easeIn',
  5032. duration: duration,
  5033. onProcess: function onProcess(process) {
  5034. context.clearRect(0, 0, opts.width, opts.height);
  5035. if (opts.rotate) {
  5036. contextRotate(context, opts);
  5037. }
  5038. drawYAxisGrid(categories, opts, config, context);
  5039. drawXAxis(categories, opts, config, context);
  5040. var _drawMixDataPoints = drawMixDataPoints(series, opts, config, context, process),
  5041. xAxisPoints = _drawMixDataPoints.xAxisPoints,
  5042. calPoints = _drawMixDataPoints.calPoints,
  5043. eachSpacing = _drawMixDataPoints.eachSpacing;
  5044. opts.chartData.xAxisPoints = xAxisPoints;
  5045. opts.chartData.calPoints = calPoints;
  5046. opts.chartData.eachSpacing = eachSpacing;
  5047. drawYAxis(series, opts, config, context);
  5048. if (opts.enableMarkLine !== false && process === 1) {
  5049. drawMarkLine(opts, config, context);
  5050. }
  5051. drawLegend(opts.series, opts, config, context, opts.chartData);
  5052. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  5053. drawCanvas(opts, context);
  5054. },
  5055. onAnimationFinish: function onAnimationFinish() {
  5056. _this.event.trigger('renderComplete');
  5057. }
  5058. });
  5059. break;
  5060. case 'column':
  5061. this.animationInstance = new Animation({
  5062. timing: 'easeIn',
  5063. duration: duration,
  5064. onProcess: function onProcess(process) {
  5065. context.clearRect(0, 0, opts.width, opts.height);
  5066. if (opts.rotate) {
  5067. contextRotate(context, opts);
  5068. }
  5069. drawYAxisGrid(categories, opts, config, context);
  5070. drawXAxis(categories, opts, config, context);
  5071. var _drawColumnDataPoints = drawColumnDataPoints(series, opts, config, context,
  5072. process),
  5073. xAxisPoints = _drawColumnDataPoints.xAxisPoints,
  5074. calPoints = _drawColumnDataPoints.calPoints,
  5075. eachSpacing = _drawColumnDataPoints.eachSpacing;
  5076. opts.chartData.xAxisPoints = xAxisPoints;
  5077. opts.chartData.calPoints = calPoints;
  5078. opts.chartData.eachSpacing = eachSpacing;
  5079. drawYAxis(series, opts, config, context);
  5080. if (opts.enableMarkLine !== false && process === 1) {
  5081. drawMarkLine(opts, config, context);
  5082. }
  5083. drawLegend(opts.series, opts, config, context, opts.chartData);
  5084. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  5085. drawCanvas(opts, context);
  5086. },
  5087. onAnimationFinish: function onAnimationFinish() {
  5088. _this.event.trigger('renderComplete');
  5089. }
  5090. });
  5091. break;
  5092. // slam TODO myArea
  5093. case 'area':
  5094. this.animationInstance = new Animation({
  5095. timing: 'easeIn',
  5096. duration: duration,
  5097. onProcess: function onProcess(process) {
  5098. context.clearRect(0, 0, opts.width, opts.height);
  5099. if (opts.rotate) {
  5100. contextRotate(context, opts);
  5101. }
  5102. drawYAxisGrid(categories, opts, config, context);
  5103. drawXAxis(categories, opts, config, context);
  5104. var _drawAreaDataPoints = drawAreaDataPoints(series, opts, config, context, process),
  5105. xAxisPoints = _drawAreaDataPoints.xAxisPoints,
  5106. calPoints = _drawAreaDataPoints.calPoints,
  5107. eachSpacing = _drawAreaDataPoints.eachSpacing;
  5108. opts.chartData.xAxisPoints = xAxisPoints;
  5109. opts.chartData.calPoints = calPoints;
  5110. opts.chartData.eachSpacing = eachSpacing;
  5111. drawYAxis(series, opts, config, context);
  5112. if (opts.enableMarkLine !== false && process === 1) {
  5113. drawMarkLine(opts, config, context);
  5114. }
  5115. drawLegend(opts.series, opts, config, context, opts.chartData);
  5116. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  5117. drawCanvas(opts, context);
  5118. },
  5119. onAnimationFinish: function onAnimationFinish() {
  5120. _this.event.trigger('renderComplete');
  5121. }
  5122. });
  5123. break;
  5124. case 'ring':
  5125. case 'pie':
  5126. this.animationInstance = new Animation({
  5127. timing: 'easeInOut',
  5128. duration: duration,
  5129. onProcess: function onProcess(process) {
  5130. context.clearRect(0, 0, opts.width, opts.height);
  5131. if (opts.rotate) {
  5132. contextRotate(context, opts);
  5133. }
  5134. opts.chartData.pieData = drawPieDataPoints(series, opts, config, context, process);
  5135. drawLegend(opts.series, opts, config, context, opts.chartData);
  5136. drawToolTipBridge(opts, config, context, process);
  5137. drawCanvas(opts, context);
  5138. },
  5139. onAnimationFinish: function onAnimationFinish() {
  5140. _this.event.trigger('renderComplete');
  5141. }
  5142. });
  5143. break;
  5144. case 'rose':
  5145. this.animationInstance = new Animation({
  5146. timing: 'easeInOut',
  5147. duration: duration,
  5148. onProcess: function onProcess(process) {
  5149. context.clearRect(0, 0, opts.width, opts.height);
  5150. if (opts.rotate) {
  5151. contextRotate(context, opts);
  5152. }
  5153. opts.chartData.pieData = drawRoseDataPoints(series, opts, config, context, process);
  5154. drawLegend(opts.series, opts, config, context, opts.chartData);
  5155. drawToolTipBridge(opts, config, context, process);
  5156. drawCanvas(opts, context);
  5157. },
  5158. onAnimationFinish: function onAnimationFinish() {
  5159. _this.event.trigger('renderComplete');
  5160. }
  5161. });
  5162. break;
  5163. case 'radar':
  5164. this.animationInstance = new Animation({
  5165. timing: 'easeInOut',
  5166. duration: duration,
  5167. onProcess: function onProcess(process) {
  5168. context.clearRect(0, 0, opts.width, opts.height);
  5169. if (opts.rotate) {
  5170. contextRotate(context, opts);
  5171. }
  5172. opts.chartData.radarData = drawRadarDataPoints(series, opts, config, context, process);
  5173. drawLegend(opts.series, opts, config, context, opts.chartData);
  5174. drawToolTipBridge(opts, config, context, process);
  5175. drawCanvas(opts, context);
  5176. },
  5177. onAnimationFinish: function onAnimationFinish() {
  5178. _this.event.trigger('renderComplete');
  5179. }
  5180. });
  5181. break;
  5182. case 'arcbar':
  5183. this.animationInstance = new Animation({
  5184. timing: 'easeInOut',
  5185. duration: duration,
  5186. onProcess: function onProcess(process) {
  5187. context.clearRect(0, 0, opts.width, opts.height);
  5188. if (opts.rotate) {
  5189. contextRotate(context, opts);
  5190. }
  5191. opts.chartData.arcbarData = drawArcbarDataPoints(series, opts, config, context,
  5192. process);
  5193. drawCanvas(opts, context);
  5194. },
  5195. onAnimationFinish: function onAnimationFinish() {
  5196. _this.event.trigger('renderComplete');
  5197. }
  5198. });
  5199. break;
  5200. /**
  5201. * 自定义arcbar
  5202. * slamb todo
  5203. * */
  5204. case 'myArcbar':
  5205. this.animationInstance = new Animation({
  5206. timing: 'easeInOut',
  5207. duration: duration,
  5208. onProcess: function onProcess(process) {
  5209. context.clearRect(0, 0, opts.width, opts.height);
  5210. if (opts.rotate) {
  5211. contextRotate(context, opts);
  5212. }
  5213. opts.chartData.arcbarData = drawMyArcbarDataPoints(series, opts, config, context,
  5214. process);
  5215. drawCanvas(opts, context);
  5216. },
  5217. onAnimationFinish: function onAnimationFinish() {
  5218. _this.event.trigger('renderComplete');
  5219. }
  5220. });
  5221. break;
  5222. case 'myGradualChangeArcbar':
  5223. this.animationInstance = new Animation({
  5224. timing: 'easeInOut',
  5225. duration: duration,
  5226. onProcess: function onProcess(process) {
  5227. context.clearRect(0, 0, opts.width, opts.height);
  5228. if (opts.rotate) {
  5229. contextRotate(context, opts);
  5230. }
  5231. opts.chartData.arcbarData = drawMyGradualChangeArcbarDataPoints(series, opts, config,
  5232. context,
  5233. process);
  5234. drawCanvas(opts, context);
  5235. },
  5236. onAnimationFinish: function onAnimationFinish() {
  5237. _this.event.trigger('renderComplete');
  5238. }
  5239. });
  5240. break;
  5241. case 'gauge':
  5242. this.animationInstance = new Animation({
  5243. timing: 'easeInOut',
  5244. duration: duration,
  5245. onProcess: function onProcess(process) {
  5246. context.clearRect(0, 0, opts.width, opts.height);
  5247. if (opts.rotate) {
  5248. contextRotate(context, opts);
  5249. }
  5250. opts.chartData.gaugeData = drawGaugeDataPoints(categories, series, opts, config,
  5251. context, process);
  5252. drawCanvas(opts, context);
  5253. },
  5254. onAnimationFinish: function onAnimationFinish() {
  5255. _this.event.trigger('renderComplete');
  5256. }
  5257. });
  5258. break;
  5259. case 'candle':
  5260. this.animationInstance = new Animation({
  5261. timing: 'easeIn',
  5262. duration: duration,
  5263. onProcess: function onProcess(process) {
  5264. context.clearRect(0, 0, opts.width, opts.height);
  5265. if (opts.rotate) {
  5266. contextRotate(context, opts);
  5267. }
  5268. drawYAxisGrid(categories, opts, config, context);
  5269. drawXAxis(categories, opts, config, context);
  5270. var _drawCandleDataPoints = drawCandleDataPoints(series, seriesMA, opts, config,
  5271. context, process),
  5272. xAxisPoints = _drawCandleDataPoints.xAxisPoints,
  5273. calPoints = _drawCandleDataPoints.calPoints,
  5274. eachSpacing = _drawCandleDataPoints.eachSpacing;
  5275. opts.chartData.xAxisPoints = xAxisPoints;
  5276. opts.chartData.calPoints = calPoints;
  5277. opts.chartData.eachSpacing = eachSpacing;
  5278. drawYAxis(series, opts, config, context);
  5279. if (opts.enableMarkLine !== false && process === 1) {
  5280. drawMarkLine(opts, config, context);
  5281. }
  5282. if (seriesMA) {
  5283. drawLegend(seriesMA, opts, config, context, opts.chartData);
  5284. } else {
  5285. drawLegend(opts.series, opts, config, context, opts.chartData);
  5286. }
  5287. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  5288. drawCanvas(opts, context);
  5289. },
  5290. onAnimationFinish: function onAnimationFinish() {
  5291. _this.event.trigger('renderComplete');
  5292. }
  5293. });
  5294. break;
  5295. }
  5296. }
  5297. // simple event implement
  5298. function Event() {
  5299. this.events = {};
  5300. }
  5301. Event.prototype.addEventListener = function(type, listener) {
  5302. this.events[type] = this.events[type] || [];
  5303. this.events[type].push(listener);
  5304. };
  5305. Event.prototype.trigger = function() {
  5306. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  5307. args[_key] = arguments[_key];
  5308. }
  5309. var type = args[0];
  5310. var params = args.slice(1);
  5311. if (!!this.events[type]) {
  5312. this.events[type].forEach(function(listener) {
  5313. try {
  5314. listener.apply(null, params);
  5315. } catch (e) {
  5316. console.error(e);
  5317. }
  5318. });
  5319. }
  5320. };
  5321. var Charts = function Charts(opts) {
  5322. opts.pixelRatio = opts.pixelRatio ? opts.pixelRatio : 1;
  5323. opts.fontSize = opts.fontSize ? opts.fontSize * opts.pixelRatio : 13 * opts.pixelRatio;
  5324. opts.title = assign({}, opts.title);
  5325. opts.subtitle = assign({}, opts.subtitle);
  5326. opts.duration = opts.duration ? opts.duration : 1000;
  5327. opts.yAxis = assign({}, {
  5328. data: [],
  5329. showTitle: false,
  5330. disabled: false,
  5331. disableGrid: false,
  5332. splitNumber: 5,
  5333. gridType: 'solid',
  5334. dashLength: 4 * opts.pixelRatio,
  5335. gridColor: '#cccccc',
  5336. padding: 10,
  5337. fontColor: '#666666'
  5338. }, opts.yAxis);
  5339. opts.yAxis.dashLength *= opts.pixelRatio;
  5340. opts.yAxis.padding *= opts.pixelRatio;
  5341. opts.xAxis = assign({}, {
  5342. rotateLabel: false,
  5343. type: 'calibration',
  5344. gridType: 'solid',
  5345. dashLength: 4,
  5346. scrollAlign: 'left',
  5347. boundaryGap: 'center',
  5348. axisLine: true,
  5349. axisLineColor: '#cccccc'
  5350. }, opts.xAxis);
  5351. opts.xAxis.dashLength *= opts.pixelRatio;
  5352. opts.legend = assign({}, {
  5353. show: true,
  5354. position: 'bottom',
  5355. float: 'center',
  5356. backgroundColor: 'rgba(0,0,0,0)',
  5357. borderColor: 'rgba(0,0,0,0)',
  5358. borderWidth: 0,
  5359. padding: 5,
  5360. margin: 5,
  5361. itemGap: 10,
  5362. fontSize: opts.fontSize,
  5363. lineHeight: opts.fontSize,
  5364. fontColor: '#333333',
  5365. format: {},
  5366. hiddenColor: '#CECECE'
  5367. }, opts.legend);
  5368. opts.legend.borderWidth = opts.legend.borderWidth * opts.pixelRatio;
  5369. opts.legend.itemGap = opts.legend.itemGap * opts.pixelRatio;
  5370. opts.legend.padding = opts.legend.padding * opts.pixelRatio;
  5371. opts.legend.margin = opts.legend.margin * opts.pixelRatio;
  5372. opts.extra = assign({}, opts.extra);
  5373. opts.rotate = opts.rotate ? true : false;
  5374. opts.animation = opts.animation ? true : false;
  5375. opts.rotate = opts.rotate ? true : false;
  5376. let config$$1 = JSON.parse(JSON.stringify(config));
  5377. config$$1.colors = opts.colors ? opts.colors : config$$1.colors;
  5378. config$$1.yAxisTitleWidth = opts.yAxis.disabled !== true && opts.yAxis.title ? config$$1.yAxisTitleWidth : 0;
  5379. if (opts.type == 'pie' || opts.type == 'ring') {
  5380. config$$1.pieChartLinePadding = opts.dataLabel === false ? 0 : opts.extra.pie.labelWidth * opts
  5381. .pixelRatio ||
  5382. config$$1.pieChartLinePadding * opts.pixelRatio;
  5383. }
  5384. if (opts.type == 'rose') {
  5385. config$$1.pieChartLinePadding = opts.dataLabel === false ? 0 : opts.extra.rose.labelWidth * opts
  5386. .pixelRatio ||
  5387. config$$1.pieChartLinePadding * opts.pixelRatio;
  5388. }
  5389. config$$1.pieChartTextPadding = opts.dataLabel === false ? 0 : config$$1.pieChartTextPadding * opts.pixelRatio;
  5390. config$$1.yAxisSplit = opts.yAxis.splitNumber ? opts.yAxis.splitNumber : config.yAxisSplit;
  5391. //屏幕旋转
  5392. config$$1.rotate = opts.rotate;
  5393. if (opts.rotate) {
  5394. let tempWidth = opts.width;
  5395. let tempHeight = opts.height;
  5396. opts.width = tempHeight;
  5397. opts.height = tempWidth;
  5398. }
  5399. //适配高分屏
  5400. opts.padding = opts.padding ? opts.padding : config$$1.padding;
  5401. for (let i = 0; i < 4; i++) {
  5402. opts.padding[i] *= opts.pixelRatio;
  5403. }
  5404. config$$1.yAxisWidth = config.yAxisWidth * opts.pixelRatio;
  5405. config$$1.xAxisHeight = config.xAxisHeight * opts.pixelRatio;
  5406. if (opts.enableScroll && opts.xAxis.scrollShow) {
  5407. config$$1.xAxisHeight += 6 * opts.pixelRatio;
  5408. }
  5409. config$$1.xAxisLineHeight = config.xAxisLineHeight * opts.pixelRatio;
  5410. config$$1.fontSize = opts.fontSize;
  5411. config$$1.titleFontSize = config.titleFontSize * opts.pixelRatio;
  5412. config$$1.subtitleFontSize = config.subtitleFontSize * opts.pixelRatio;
  5413. config$$1.toolTipPadding = config.toolTipPadding * opts.pixelRatio;
  5414. config$$1.toolTipLineHeight = config.toolTipLineHeight * opts.pixelRatio;
  5415. config$$1.columePadding = config.columePadding * opts.pixelRatio;
  5416. opts.$this = opts.$this ? opts.$this : this;
  5417. this.context = uni.createCanvasContext(opts.canvasId, opts.$this);
  5418. /* 兼容原生H5
  5419. this.context = document.getElementById(opts.canvasId).getContext("2d");
  5420. this.context.setStrokeStyle = function(e){ return this.strokeStyle=e; }
  5421. this.context.setLineWidth = function(e){ return this.lineWidth=e; }
  5422. this.context.setLineCap = function(e){ return this.lineCap=e; }
  5423. this.context.setFontSize = function(e){ return this.font=e+"px sans-serif"; }
  5424. this.context.setFillStyle = function(e){ return this.fillStyle=e; }
  5425. this.context.draw = function(){ }
  5426. */
  5427. opts.chartData = {};
  5428. this.event = new Event();
  5429. this.scrollOption = {
  5430. currentOffset: 0,
  5431. startTouchX: 0,
  5432. distance: 0,
  5433. lastMoveTime: 0
  5434. };
  5435. this.opts = opts;
  5436. this.config = config$$1;
  5437. drawCharts.call(this, opts.type, opts, config$$1, this.context);
  5438. };
  5439. Charts.prototype.updateData = function() {
  5440. let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  5441. this.opts = assign({}, this.opts, data);
  5442. this.opts.updateData = true;
  5443. let scrollPosition = data.scrollPosition || 'current';
  5444. switch (scrollPosition) {
  5445. case 'current':
  5446. this.opts._scrollDistance_ = this.scrollOption.currentOffset;
  5447. break;
  5448. case 'left':
  5449. this.opts._scrollDistance_ = 0;
  5450. this.scrollOption = {
  5451. currentOffset: 0,
  5452. startTouchX: 0,
  5453. distance: 0,
  5454. lastMoveTime: 0
  5455. };
  5456. break;
  5457. case 'right':
  5458. let _calYAxisData = calYAxisData(this.opts.series, this.opts, this.config),
  5459. yAxisWidth = _calYAxisData.yAxisWidth;
  5460. this.config.yAxisWidth = yAxisWidth;
  5461. let offsetLeft = 0;
  5462. let _getXAxisPoints0 = getXAxisPoints(this.opts.categories, this.opts, this.config),
  5463. xAxisPoints = _getXAxisPoints0.xAxisPoints,
  5464. startX = _getXAxisPoints0.startX,
  5465. endX = _getXAxisPoints0.endX,
  5466. eachSpacing = _getXAxisPoints0.eachSpacing;
  5467. let totalWidth = eachSpacing * (xAxisPoints.length - 1);
  5468. let screenWidth = endX - startX;
  5469. offsetLeft = screenWidth - totalWidth;
  5470. this.scrollOption = {
  5471. currentOffset: offsetLeft,
  5472. startTouchX: offsetLeft,
  5473. distance: 0,
  5474. lastMoveTime: 0
  5475. };
  5476. this.opts._scrollDistance_ = offsetLeft;
  5477. break;
  5478. }
  5479. drawCharts.call(this, this.opts.type, this.opts, this.config, this.context);
  5480. };
  5481. Charts.prototype.zoom = function() {
  5482. var val = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.opts.xAxis.itemCount;
  5483. if (this.opts.enableScroll !== true) {
  5484. console.log('请启用滚动条后使用!')
  5485. return;
  5486. }
  5487. //当前屏幕中间点
  5488. let centerPoint = Math.round(Math.abs(this.scrollOption.currentOffset) / this.opts.chartData.eachSpacing) + Math
  5489. .round(
  5490. this.opts.xAxis.itemCount / 2);
  5491. this.opts.animation = false;
  5492. this.opts.xAxis.itemCount = val.itemCount;
  5493. //重新计算x轴偏移距离
  5494. let _calYAxisData = calYAxisData(this.opts.series, this.opts, this.config),
  5495. yAxisWidth = _calYAxisData.yAxisWidth;
  5496. this.config.yAxisWidth = yAxisWidth;
  5497. let offsetLeft = 0;
  5498. let _getXAxisPoints0 = getXAxisPoints(this.opts.categories, this.opts, this.config),
  5499. xAxisPoints = _getXAxisPoints0.xAxisPoints,
  5500. startX = _getXAxisPoints0.startX,
  5501. endX = _getXAxisPoints0.endX,
  5502. eachSpacing = _getXAxisPoints0.eachSpacing;
  5503. let centerLeft = eachSpacing * centerPoint;
  5504. let screenWidth = endX - startX;
  5505. let MaxLeft = screenWidth - eachSpacing * (xAxisPoints.length - 1);
  5506. offsetLeft = screenWidth / 2 - centerLeft;
  5507. if (offsetLeft > 0) {
  5508. offsetLeft = 0;
  5509. }
  5510. if (offsetLeft < MaxLeft) {
  5511. offsetLeft = MaxLeft;
  5512. }
  5513. this.scrollOption = {
  5514. currentOffset: offsetLeft,
  5515. startTouchX: offsetLeft,
  5516. distance: 0,
  5517. lastMoveTime: 0
  5518. };
  5519. this.opts._scrollDistance_ = offsetLeft;
  5520. drawCharts.call(this, this.opts.type, this.opts, this.config, this.context);
  5521. };
  5522. Charts.prototype.stopAnimation = function() {
  5523. this.animationInstance && this.animationInstance.stop();
  5524. };
  5525. Charts.prototype.addEventListener = function(type, listener) {
  5526. this.event.addEventListener(type, listener);
  5527. };
  5528. Charts.prototype.getCurrentDataIndex = function(e) {
  5529. var touches = null;
  5530. if (e.changedTouches) {
  5531. touches = e.changedTouches[0];
  5532. } else {
  5533. touches = e.mp.changedTouches[0];
  5534. }
  5535. if (touches) {
  5536. let _touches$ = getTouches(touches, this.opts, e);
  5537. if (this.opts.type === 'pie' || this.opts.type === 'ring' || this.opts.type === 'rose') {
  5538. return findPieChartCurrentIndex({
  5539. x: _touches$.x,
  5540. y: _touches$.y
  5541. }, this.opts.chartData.pieData);
  5542. } else if (this.opts.type === 'radar') {
  5543. return findRadarChartCurrentIndex({
  5544. x: _touches$.x,
  5545. y: _touches$.y
  5546. }, this.opts.chartData.radarData, this.opts.categories.length);
  5547. } else if (this.opts.type === 'funnel') {
  5548. return findFunnelChartCurrentIndex({
  5549. x: _touches$.x,
  5550. y: _touches$.y
  5551. }, this.opts.chartData.funnelData);
  5552. } else if (this.opts.type === 'map') {
  5553. return findMapChartCurrentIndex({
  5554. x: _touches$.x,
  5555. y: _touches$.y
  5556. }, this.opts);
  5557. } else if (this.opts.type === 'word') {
  5558. return findWordChartCurrentIndex({
  5559. x: _touches$.x,
  5560. y: _touches$.y
  5561. }, this.opts.chartData.wordCloudData);
  5562. } else {
  5563. return findCurrentIndex({
  5564. x: _touches$.x,
  5565. y: _touches$.y
  5566. }, this.opts.chartData.calPoints, this.opts, this.config, Math.abs(this.scrollOption
  5567. .currentOffset));
  5568. }
  5569. }
  5570. return -1;
  5571. };
  5572. Charts.prototype.getLegendDataIndex = function(e) {
  5573. var touches = null;
  5574. if (e.changedTouches) {
  5575. touches = e.changedTouches[0];
  5576. } else {
  5577. touches = e.mp.changedTouches[0];
  5578. }
  5579. if (touches) {
  5580. let _touches$ = getTouches(touches, this.opts, e);
  5581. return findLegendIndex({
  5582. x: _touches$.x,
  5583. y: _touches$.y
  5584. }, this.opts.chartData.legendData);
  5585. }
  5586. return -1;
  5587. };
  5588. Charts.prototype.touchLegend = function(e) {
  5589. var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  5590. var touches = null;
  5591. if (e.changedTouches) {
  5592. touches = e.changedTouches[0];
  5593. } else {
  5594. touches = e.mp.changedTouches[0];
  5595. }
  5596. if (touches) {
  5597. var _touches$ = getTouches(touches, this.opts, e);
  5598. var index = this.getLegendDataIndex(e);
  5599. if (index >= 0) {
  5600. this.opts.series[index].show = !this.opts.series[index].show;
  5601. this.opts.animation = option.animation ? true : false;
  5602. this.opts._scrollDistance_ = this.scrollOption.currentOffset;
  5603. drawCharts.call(this, this.opts.type, this.opts, this.config, this.context);
  5604. }
  5605. }
  5606. };
  5607. Charts.prototype.showToolTip = function(e) {
  5608. var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  5609. var touches = null;
  5610. if (e.changedTouches) {
  5611. touches = e.changedTouches[0];
  5612. } else {
  5613. touches = e.mp.changedTouches[0];
  5614. }
  5615. if (!touches) {
  5616. console.log("touchError");
  5617. }
  5618. var _touches$ = getTouches(touches, this.opts, e);
  5619. var currentOffset = this.scrollOption.currentOffset;
  5620. var opts = assign({}, this.opts, {
  5621. _scrollDistance_: currentOffset,
  5622. animation: false
  5623. });
  5624. if (this.opts.type === 'line' || this.opts.type === 'area' || this.opts.type === 'column') {
  5625. var index = this.getCurrentDataIndex(e);
  5626. if (index > -1) {
  5627. var seriesData = getSeriesDataItem(this.opts.series, index);
  5628. if (seriesData.length !== 0) {
  5629. var _getToolTipData = getToolTipData(seriesData, this.opts.chartData.calPoints, index, this.opts
  5630. .categories,
  5631. option),
  5632. textList = _getToolTipData.textList,
  5633. offset = _getToolTipData.offset;
  5634. offset.y = _touches$.y;
  5635. opts.tooltip = {
  5636. textList: textList,
  5637. offset: offset,
  5638. option: option,
  5639. index: index
  5640. };
  5641. }
  5642. }
  5643. drawCharts.call(this, opts.type, opts, this.config, this.context);
  5644. }
  5645. if (this.opts.type === 'mix') {
  5646. var index = this.getCurrentDataIndex(e);
  5647. if (index > -1) {
  5648. var currentOffset = this.scrollOption.currentOffset;
  5649. var opts = assign({}, this.opts, {
  5650. _scrollDistance_: currentOffset,
  5651. animation: false
  5652. });
  5653. var seriesData = getSeriesDataItem(this.opts.series, index);
  5654. if (seriesData.length !== 0) {
  5655. var _getMixToolTipData = getMixToolTipData(seriesData, this.opts.chartData.calPoints, index, this
  5656. .opts.categories,
  5657. option),
  5658. textList = _getMixToolTipData.textList,
  5659. offset = _getMixToolTipData.offset;
  5660. offset.y = _touches$.y;
  5661. opts.tooltip = {
  5662. textList: textList,
  5663. offset: offset,
  5664. option: option,
  5665. index: index
  5666. };
  5667. }
  5668. }
  5669. drawCharts.call(this, opts.type, opts, this.config, this.context);
  5670. }
  5671. if (this.opts.type === 'candle') {
  5672. var index = this.getCurrentDataIndex(e);
  5673. if (index > -1) {
  5674. var currentOffset = this.scrollOption.currentOffset;
  5675. var opts = assign({}, this.opts, {
  5676. _scrollDistance_: currentOffset,
  5677. animation: false
  5678. });
  5679. var seriesData = getSeriesDataItem(this.opts.series, index);
  5680. if (seriesData.length !== 0) {
  5681. var _getToolTipData = getCandleToolTipData(this.opts.series[0].data, seriesData, this.opts.chartData
  5682. .calPoints,
  5683. index, this.opts.categories, this.opts.extra.candle, option),
  5684. textList = _getToolTipData.textList,
  5685. offset = _getToolTipData.offset;
  5686. offset.y = _touches$.y;
  5687. opts.tooltip = {
  5688. textList: textList,
  5689. offset: offset,
  5690. option: option,
  5691. index: index
  5692. };
  5693. }
  5694. }
  5695. drawCharts.call(this, opts.type, opts, this.config, this.context);
  5696. }
  5697. if (this.opts.type === 'pie' || this.opts.type === 'ring' || this.opts.type === 'rose' || this.opts.type ===
  5698. 'funnel') {
  5699. var index = this.getCurrentDataIndex(e);
  5700. if (index > -1) {
  5701. var currentOffset = this.scrollOption.currentOffset;
  5702. var opts = assign({}, this.opts, {
  5703. _scrollDistance_: currentOffset,
  5704. animation: false
  5705. });
  5706. var seriesData = this.opts._series_[index];
  5707. var textList = [{
  5708. text: option.format ? option.format(seriesData) : seriesData.name + ': ' + seriesData.data,
  5709. color: seriesData.color
  5710. }];
  5711. var offset = {
  5712. x: _touches$.x,
  5713. y: _touches$.y
  5714. };
  5715. opts.tooltip = {
  5716. textList: textList,
  5717. offset: offset,
  5718. option: option,
  5719. index: index
  5720. };
  5721. }
  5722. drawCharts.call(this, opts.type, opts, this.config, this.context);
  5723. }
  5724. if (this.opts.type === 'map' || this.opts.type === 'word') {
  5725. var index = this.getCurrentDataIndex(e);
  5726. if (index > -1) {
  5727. var currentOffset = this.scrollOption.currentOffset;
  5728. var opts = assign({}, this.opts, {
  5729. _scrollDistance_: currentOffset,
  5730. animation: false
  5731. });
  5732. var seriesData = this.opts._series_[index];
  5733. var textList = [{
  5734. text: option.format ? option.format(seriesData) : seriesData.properties.name,
  5735. color: seriesData.color
  5736. }];
  5737. var offset = {
  5738. x: _touches$.x,
  5739. y: _touches$.y
  5740. };
  5741. opts.tooltip = {
  5742. textList: textList,
  5743. offset: offset,
  5744. option: option,
  5745. index: index
  5746. };
  5747. }
  5748. opts.updateData = false;
  5749. drawCharts.call(this, opts.type, opts, this.config, this.context);
  5750. }
  5751. if (this.opts.type === 'radar') {
  5752. var index = this.getCurrentDataIndex(e);
  5753. if (index > -1) {
  5754. var currentOffset = this.scrollOption.currentOffset;
  5755. var opts = assign({}, this.opts, {
  5756. _scrollDistance_: currentOffset,
  5757. animation: false
  5758. });
  5759. var seriesData = getSeriesDataItem(this.opts.series, index);
  5760. if (seriesData.length !== 0) {
  5761. var textList = seriesData.map(function(item) {
  5762. return {
  5763. text: option.format ? option.format(item) : item.name + ': ' + item.data,
  5764. color: item.color
  5765. };
  5766. });
  5767. var offset = {
  5768. x: _touches$.x,
  5769. y: _touches$.y
  5770. };
  5771. opts.tooltip = {
  5772. textList: textList,
  5773. offset: offset,
  5774. option: option,
  5775. index: index
  5776. };
  5777. }
  5778. }
  5779. drawCharts.call(this, opts.type, opts, this.config, this.context);
  5780. }
  5781. };
  5782. Charts.prototype.translate = function(distance) {
  5783. this.scrollOption = {
  5784. currentOffset: distance,
  5785. startTouchX: distance,
  5786. distance: 0,
  5787. lastMoveTime: 0
  5788. };
  5789. let opts = assign({}, this.opts, {
  5790. _scrollDistance_: distance,
  5791. animation: false
  5792. });
  5793. drawCharts.call(this, this.opts.type, opts, this.config, this.context);
  5794. };
  5795. Charts.prototype.scrollStart = function(e) {
  5796. var touches = null;
  5797. if (e.changedTouches) {
  5798. touches = e.changedTouches[0];
  5799. } else {
  5800. touches = e.mp.changedTouches[0];
  5801. }
  5802. var _touches$ = getTouches(touches, this.opts, e);
  5803. if (touches && this.opts.enableScroll === true) {
  5804. this.scrollOption.startTouchX = _touches$.x;
  5805. }
  5806. };
  5807. Charts.prototype.scroll = function(e) {
  5808. if (this.scrollOption.lastMoveTime === 0) {
  5809. this.scrollOption.lastMoveTime = Date.now();
  5810. }
  5811. let Limit = this.opts.extra.touchMoveLimit || 20;
  5812. let currMoveTime = Date.now();
  5813. let duration = currMoveTime - this.scrollOption.lastMoveTime;
  5814. if (duration < Math.floor(1000 / Limit)) return;
  5815. this.scrollOption.lastMoveTime = currMoveTime;
  5816. var touches = null;
  5817. if (e.changedTouches) {
  5818. touches = e.changedTouches[0];
  5819. } else {
  5820. touches = e.mp.changedTouches[0];
  5821. }
  5822. if (touches && this.opts.enableScroll === true) {
  5823. var _touches$ = getTouches(touches, this.opts, e);
  5824. var _distance;
  5825. _distance = _touches$.x - this.scrollOption.startTouchX;
  5826. var currentOffset = this.scrollOption.currentOffset;
  5827. var validDistance = calValidDistance(this, currentOffset + _distance, this.opts.chartData, this.config, this
  5828. .opts);
  5829. this.scrollOption.distance = _distance = validDistance - currentOffset;
  5830. var opts = assign({}, this.opts, {
  5831. _scrollDistance_: currentOffset + _distance,
  5832. animation: false
  5833. });
  5834. drawCharts.call(this, opts.type, opts, this.config, this.context);
  5835. return currentOffset + _distance;
  5836. }
  5837. };
  5838. Charts.prototype.scrollEnd = function(e) {
  5839. if (this.opts.enableScroll === true) {
  5840. var _scrollOption = this.scrollOption,
  5841. currentOffset = _scrollOption.currentOffset,
  5842. distance = _scrollOption.distance;
  5843. this.scrollOption.currentOffset = currentOffset + distance;
  5844. this.scrollOption.distance = 0;
  5845. }
  5846. };
  5847. if (typeof module === "object" && typeof module.exports === "object") {
  5848. module.exports = Charts;
  5849. //export default Charts;//建议使用nodejs的module导出方式,如报错请使用export方式导出
  5850. }