implot.cpp 236 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061
  1. // MIT License
  2. // Copyright (c) 2021 Evan Pezent
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. // ImPlot v0.11 WIP
  19. /*
  20. API BREAKING CHANGES
  21. ====================
  22. Occasionally introducing changes that are breaking the API. We try to make the breakage minor and easy to fix.
  23. Below is a change-log of API breaking changes only. If you are using one of the functions listed, expect to have to fix some code.
  24. When you are not sure about a old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all implot files.
  25. You can read releases logs https://github.com/epezent/implot/releases for more details.
  26. - 2021/03/08 (0.9) - SetColormap and PushColormap(ImVec4*) were removed. Use AddColormap for custom colormap support. LerpColormap was changed to SampleColormap.
  27. ShowColormapScale was changed to ColormapScale and requires additional arguments.
  28. - 2021/03/07 (0.9) - The signature of ShowColormapScale was modified to accept a ImVec2 size.
  29. - 2021/02/28 (0.9) - BeginLegendDragDropSource was changed to BeginDragDropSourceItem with a number of other drag and drop improvements.
  30. - 2021/01/18 (0.9) - The default behavior for opening context menus was change from double right-click to single right-click. ImPlotInputMap and related functions were moved
  31. to implot_internal.h due to its immaturity.
  32. - 2020/10/16 (0.8) - ImPlotStyleVar_InfoPadding was changed to ImPlotStyleVar_MousePosPadding
  33. - 2020/09/10 (0.8) - The single array versions of PlotLine, PlotScatter, PlotStems, and PlotShaded were given additional arguments for x-scale and x0.
  34. - 2020/09/07 (0.8) - Plotting functions which accept a custom getter function pointer have been post-fixed with a G (e.g. PlotLineG)
  35. - 2020/09/06 (0.7) - Several flags under ImPlotFlags and ImPlotAxisFlags were inverted (e.g. ImPlotFlags_Legend -> ImPlotFlags_NoLegend) so that the default flagset
  36. is simply 0. This more closely matches ImGui's style and makes it easier to enable non-default but commonly used flags (e.g. ImPlotAxisFlags_Time).
  37. - 2020/08/28 (0.5) - ImPlotMarker_ can no longer be combined with bitwise OR, |. This features caused unecessary slow-down, and almost no one used it.
  38. - 2020/08/25 (0.5) - ImPlotAxisFlags_Scientific was removed. Logarithmic axes automatically uses scientific notation.
  39. - 2020/08/17 (0.5) - PlotText was changed so that text is centered horizontally and vertically about the desired point.
  40. - 2020/08/16 (0.5) - An ImPlotContext must be explicitly created and destroyed now with `CreateContext` and `DestroyContext`. Previously, the context was statically initialized in this source file.
  41. - 2020/06/13 (0.4) - The flags `ImPlotAxisFlag_Adaptive` and `ImPlotFlags_Cull` were removed. Both are now done internally by default.
  42. - 2020/06/03 (0.3) - The signature and behavior of PlotPieChart was changed so that data with sum less than 1 can optionally be normalized. The label format can now be specified as well.
  43. - 2020/06/01 (0.3) - SetPalette was changed to `SetColormap` for consistency with other plotting libraries. `RestorePalette` was removed. Use `SetColormap(ImPlotColormap_Default)`.
  44. - 2020/05/31 (0.3) - Plot functions taking custom ImVec2* getters were removed. Use the ImPlotPoint* getter versions instead.
  45. - 2020/05/29 (0.3) - The signature of ImPlotLimits::Contains was changed to take two doubles instead of ImVec2
  46. - 2020/05/16 (0.2) - All plotting functions were reverted to being prefixed with "Plot" to maintain a consistent VerbNoun style. `Plot` was split into `PlotLine`
  47. and `PlotScatter` (however, `PlotLine` can still be used to plot scatter points as `Plot` did before.). `Bar` is not `PlotBars`, to indicate
  48. that multiple bars will be plotted.
  49. - 2020/05/13 (0.2) - `ImMarker` was change to `ImPlotMarker` and `ImAxisFlags` was changed to `ImPlotAxisFlags`.
  50. - 2020/05/11 (0.2) - `ImPlotFlags_Selection` was changed to `ImPlotFlags_BoxSelect`
  51. - 2020/05/11 (0.2) - The namespace ImGui:: was replaced with ImPlot::. As a result, the following additional changes were made:
  52. - Functions that were prefixed or decorated with the word "Plot" have been truncated. E.g., `ImGui::PlotBars` is now just `ImPlot::Bar`.
  53. It should be fairly obvious what was what.
  54. - Some functions have been given names that would have otherwise collided with the ImGui namespace. This has been done to maintain a consistent
  55. style with ImGui. E.g., 'ImGui::PushPlotStyleVar` is now 'ImPlot::PushStyleVar'.
  56. - 2020/05/10 (0.2) - The following function/struct names were changes:
  57. - ImPlotRange -> ImPlotLimits
  58. - GetPlotRange() -> GetPlotLimits()
  59. - SetNextPlotRange -> SetNextPlotLimits
  60. - SetNextPlotRangeX -> SetNextPlotLimitsX
  61. - SetNextPlotRangeY -> SetNextPlotLimitsY
  62. - 2020/05/10 (0.2) - Plot queries are pixel based by default. Query rects that maintain relative plot position have been removed. This was done to support multi-y-axis.
  63. */
  64. #include "implot.h"
  65. #include "implot_internal.h"
  66. #ifdef _MSC_VER
  67. #define sprintf sprintf_s
  68. #endif
  69. // Support for pre-1.82 versions. Users on 1.82+ can use 0 (default) flags to mean "all corners" but in order to support older versions we are more explicit.
  70. #if (IMGUI_VERSION_NUM < 18102) && !defined(ImDrawFlags_RoundCornersAll)
  71. #define ImDrawFlags_RoundCornersAll ImDrawCornerFlags_All
  72. #endif
  73. // Global plot context
  74. ImPlotContext* GImPlot = NULL;
  75. //-----------------------------------------------------------------------------
  76. // Struct Implementations
  77. //-----------------------------------------------------------------------------
  78. ImPlotInputMap::ImPlotInputMap() {
  79. PanButton = ImGuiMouseButton_Left;
  80. PanMod = ImGuiKeyModFlags_None;
  81. FitButton = ImGuiMouseButton_Left;
  82. ContextMenuButton = ImGuiMouseButton_Right;
  83. BoxSelectButton = ImGuiMouseButton_Right;
  84. BoxSelectMod = ImGuiKeyModFlags_None;
  85. BoxSelectCancelButton = ImGuiMouseButton_Left;
  86. QueryButton = ImGuiMouseButton_Middle;
  87. QueryMod = ImGuiKeyModFlags_None;
  88. QueryToggleMod = ImGuiKeyModFlags_Ctrl;
  89. HorizontalMod = ImGuiKeyModFlags_Alt;
  90. VerticalMod = ImGuiKeyModFlags_Shift;
  91. }
  92. ImPlotStyle::ImPlotStyle() {
  93. LineWeight = 1;
  94. Marker = ImPlotMarker_None;
  95. MarkerSize = 4;
  96. MarkerWeight = 1;
  97. FillAlpha = 1;
  98. ErrorBarSize = 5;
  99. ErrorBarWeight = 1.5f;
  100. DigitalBitHeight = 8;
  101. DigitalBitGap = 4;
  102. PlotBorderSize = 1;
  103. MinorAlpha = 0.25f;
  104. MajorTickLen = ImVec2(10,10);
  105. MinorTickLen = ImVec2(5,5);
  106. MajorTickSize = ImVec2(1,1);
  107. MinorTickSize = ImVec2(1,1);
  108. MajorGridSize = ImVec2(1,1);
  109. MinorGridSize = ImVec2(1,1);
  110. PlotPadding = ImVec2(10,10);
  111. LabelPadding = ImVec2(5,5);
  112. LegendPadding = ImVec2(10,10);
  113. LegendInnerPadding = ImVec2(5,5);
  114. LegendSpacing = ImVec2(5,0);
  115. MousePosPadding = ImVec2(10,10);
  116. AnnotationPadding = ImVec2(2,2);
  117. FitPadding = ImVec2(0,0);
  118. PlotDefaultSize = ImVec2(400,300);
  119. PlotMinSize = ImVec2(200,150);
  120. ImPlot::StyleColorsAuto(this);
  121. Colormap = ImPlotColormap_Deep;
  122. AntiAliasedLines = false;
  123. UseLocalTime = false;
  124. Use24HourClock = false;
  125. UseISO8601 = false;
  126. }
  127. //-----------------------------------------------------------------------------
  128. // Style
  129. //-----------------------------------------------------------------------------
  130. namespace ImPlot {
  131. const char* GetStyleColorName(ImPlotCol col) {
  132. static const char* col_names[] = {
  133. "Line",
  134. "Fill",
  135. "MarkerOutline",
  136. "MarkerFill",
  137. "ErrorBar",
  138. "FrameBg",
  139. "PlotBg",
  140. "PlotBorder",
  141. "LegendBg",
  142. "LegendBorder",
  143. "LegendText",
  144. "TitleText",
  145. "InlayText",
  146. "XAxis",
  147. "XAxisGrid",
  148. "YAxis",
  149. "YAxisGrid",
  150. "YAxis2",
  151. "YAxisGrid2",
  152. "YAxis3",
  153. "YAxisGrid3",
  154. "Selection",
  155. "Query",
  156. "Crosshairs"
  157. };
  158. return col_names[col];
  159. }
  160. const char* GetMarkerName(ImPlotMarker marker) {
  161. switch (marker) {
  162. case ImPlotMarker_None: return "None";
  163. case ImPlotMarker_Circle: return "Circle";
  164. case ImPlotMarker_Square: return "Square";
  165. case ImPlotMarker_Diamond: return "Diamond";
  166. case ImPlotMarker_Up: return "Up";
  167. case ImPlotMarker_Down: return "Down";
  168. case ImPlotMarker_Left: return "Left";
  169. case ImPlotMarker_Right: return "Right";
  170. case ImPlotMarker_Cross: return "Cross";
  171. case ImPlotMarker_Plus: return "Plus";
  172. case ImPlotMarker_Asterisk: return "Asterisk";
  173. default: return "";
  174. }
  175. }
  176. ImVec4 GetAutoColor(ImPlotCol idx) {
  177. ImVec4 col(0,0,0,1);
  178. switch(idx) {
  179. case ImPlotCol_Line: return col; // these are plot dependent!
  180. case ImPlotCol_Fill: return col; // these are plot dependent!
  181. case ImPlotCol_MarkerOutline: return col; // these are plot dependent!
  182. case ImPlotCol_MarkerFill: return col; // these are plot dependent!
  183. case ImPlotCol_ErrorBar: return ImGui::GetStyleColorVec4(ImGuiCol_Text);
  184. case ImPlotCol_FrameBg: return ImGui::GetStyleColorVec4(ImGuiCol_FrameBg);
  185. case ImPlotCol_PlotBg: return ImGui::GetStyleColorVec4(ImGuiCol_WindowBg);
  186. case ImPlotCol_PlotBorder: return ImGui::GetStyleColorVec4(ImGuiCol_Border);
  187. case ImPlotCol_LegendBg: return ImGui::GetStyleColorVec4(ImGuiCol_PopupBg);
  188. case ImPlotCol_LegendBorder: return GetStyleColorVec4(ImPlotCol_PlotBorder);
  189. case ImPlotCol_LegendText: return GetStyleColorVec4(ImPlotCol_InlayText);
  190. case ImPlotCol_TitleText: return ImGui::GetStyleColorVec4(ImGuiCol_Text);
  191. case ImPlotCol_InlayText: return ImGui::GetStyleColorVec4(ImGuiCol_Text);
  192. case ImPlotCol_XAxis: return ImGui::GetStyleColorVec4(ImGuiCol_Text);
  193. case ImPlotCol_XAxisGrid: return GetStyleColorVec4(ImPlotCol_XAxis) * ImVec4(1,1,1,0.25f);
  194. case ImPlotCol_YAxis: return ImGui::GetStyleColorVec4(ImGuiCol_Text);
  195. case ImPlotCol_YAxisGrid: return GetStyleColorVec4(ImPlotCol_YAxis) * ImVec4(1,1,1,0.25f);
  196. case ImPlotCol_YAxis2: return ImGui::GetStyleColorVec4(ImGuiCol_Text);
  197. case ImPlotCol_YAxisGrid2: return GetStyleColorVec4(ImPlotCol_YAxis2) * ImVec4(1,1,1,0.25f);
  198. case ImPlotCol_YAxis3: return ImGui::GetStyleColorVec4(ImGuiCol_Text);
  199. case ImPlotCol_YAxisGrid3: return GetStyleColorVec4(ImPlotCol_YAxis3) * ImVec4(1,1,1,0.25f);
  200. case ImPlotCol_Selection: return ImVec4(1,1,0,1);
  201. case ImPlotCol_Query: return ImVec4(0,1,0,1);
  202. case ImPlotCol_Crosshairs: return GetStyleColorVec4(ImPlotCol_PlotBorder);
  203. default: return col;
  204. }
  205. }
  206. struct ImPlotStyleVarInfo {
  207. ImGuiDataType Type;
  208. ImU32 Count;
  209. ImU32 Offset;
  210. void* GetVarPtr(ImPlotStyle* style) const { return (void*)((unsigned char*)style + Offset); }
  211. };
  212. static const ImPlotStyleVarInfo GPlotStyleVarInfo[] =
  213. {
  214. { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, LineWeight) }, // ImPlotStyleVar_LineWeight
  215. { ImGuiDataType_S32, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, Marker) }, // ImPlotStyleVar_Marker
  216. { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, MarkerSize) }, // ImPlotStyleVar_MarkerSize
  217. { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, MarkerWeight) }, // ImPlotStyleVar_MarkerWeight
  218. { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, FillAlpha) }, // ImPlotStyleVar_FillAlpha
  219. { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, ErrorBarSize) }, // ImPlotStyleVar_ErrorBarSize
  220. { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, ErrorBarWeight) }, // ImPlotStyleVar_ErrorBarWeight
  221. { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, DigitalBitHeight) }, // ImPlotStyleVar_DigitalBitHeight
  222. { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, DigitalBitGap) }, // ImPlotStyleVar_DigitalBitGap
  223. { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, PlotBorderSize) }, // ImPlotStyleVar_PlotBorderSize
  224. { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImPlotStyle, MinorAlpha) }, // ImPlotStyleVar_MinorAlpha
  225. { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, MajorTickLen) }, // ImPlotStyleVar_MajorTickLen
  226. { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, MinorTickLen) }, // ImPlotStyleVar_MinorTickLen
  227. { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, MajorTickSize) }, // ImPlotStyleVar_MajorTickSize
  228. { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, MinorTickSize) }, // ImPlotStyleVar_MinorTickSize
  229. { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, MajorGridSize) }, // ImPlotStyleVar_MajorGridSize
  230. { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, MinorGridSize) }, // ImPlotStyleVar_MinorGridSize
  231. { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, PlotPadding) }, // ImPlotStyleVar_PlotPadding
  232. { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, LabelPadding) }, // ImPlotStyleVar_LabelPaddine
  233. { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, LegendPadding) }, // ImPlotStyleVar_LegendPadding
  234. { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, LegendInnerPadding) }, // ImPlotStyleVar_LegendInnerPadding
  235. { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, LegendSpacing) }, // ImPlotStyleVar_LegendSpacing
  236. { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, MousePosPadding) }, // ImPlotStyleVar_MousePosPadding
  237. { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, AnnotationPadding) }, // ImPlotStyleVar_AnnotationPadding
  238. { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, FitPadding) }, // ImPlotStyleVar_FitPadding
  239. { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, PlotDefaultSize) }, // ImPlotStyleVar_PlotDefaultSize
  240. { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImPlotStyle, PlotMinSize) } // ImPlotStyleVar_PlotMinSize
  241. };
  242. static const ImPlotStyleVarInfo* GetPlotStyleVarInfo(ImPlotStyleVar idx) {
  243. IM_ASSERT(idx >= 0 && idx < ImPlotStyleVar_COUNT);
  244. IM_ASSERT(IM_ARRAYSIZE(GPlotStyleVarInfo) == ImPlotStyleVar_COUNT);
  245. return &GPlotStyleVarInfo[idx];
  246. }
  247. //-----------------------------------------------------------------------------
  248. // Generic Helpers
  249. //-----------------------------------------------------------------------------
  250. void AddTextVertical(ImDrawList *DrawList, ImVec2 pos, ImU32 col, const char *text_begin, const char* text_end) {
  251. // the code below is based loosely on ImFont::RenderText
  252. if (!text_end)
  253. text_end = text_begin + strlen(text_begin);
  254. ImGuiContext& g = *GImGui;
  255. ImFont* font = g.Font;
  256. // Align to be pixel perfect
  257. pos.x = IM_FLOOR(pos.x);
  258. pos.y = IM_FLOOR(pos.y);
  259. const float scale = g.FontSize / font->FontSize;
  260. const char* s = text_begin;
  261. int chars_exp = (int)(text_end - s);
  262. int chars_rnd = 0;
  263. const int vtx_count_max = chars_exp * 4;
  264. const int idx_count_max = chars_exp * 6;
  265. DrawList->PrimReserve(idx_count_max, vtx_count_max);
  266. while (s < text_end) {
  267. unsigned int c = (unsigned int)*s;
  268. if (c < 0x80) {
  269. s += 1;
  270. }
  271. else {
  272. s += ImTextCharFromUtf8(&c, s, text_end);
  273. if (c == 0) // Malformed UTF-8?
  274. break;
  275. }
  276. const ImFontGlyph * glyph = font->FindGlyph((ImWchar)c);
  277. if (glyph == NULL) {
  278. continue;
  279. }
  280. DrawList->PrimQuadUV(pos + ImVec2(glyph->Y0, -glyph->X0) * scale, pos + ImVec2(glyph->Y0, -glyph->X1) * scale,
  281. pos + ImVec2(glyph->Y1, -glyph->X1) * scale, pos + ImVec2(glyph->Y1, -glyph->X0) * scale,
  282. ImVec2(glyph->U0, glyph->V0), ImVec2(glyph->U1, glyph->V0),
  283. ImVec2(glyph->U1, glyph->V1), ImVec2(glyph->U0, glyph->V1),
  284. col);
  285. pos.y -= glyph->AdvanceX * scale;
  286. chars_rnd++;
  287. }
  288. // Give back unused vertices
  289. int chars_skp = chars_exp-chars_rnd;
  290. DrawList->PrimUnreserve(chars_skp*6, chars_skp*4);
  291. }
  292. void AddTextCentered(ImDrawList* DrawList, ImVec2 top_center, ImU32 col, const char* text_begin, const char* text_end) {
  293. float txt_ht = ImGui::GetTextLineHeight();
  294. const char* title_end = ImGui::FindRenderedTextEnd(text_begin, text_end);
  295. ImVec2 text_size;
  296. float y = 0;
  297. while (const char* tmp = (const char*)memchr(text_begin, '\n', title_end-text_begin)) {
  298. text_size = ImGui::CalcTextSize(text_begin,tmp,true);
  299. DrawList->AddText(ImVec2(top_center.x - text_size.x * 0.5f, top_center.y+y),col,text_begin,tmp);
  300. text_begin = tmp + 1;
  301. y += txt_ht;
  302. }
  303. text_size = ImGui::CalcTextSize(text_begin,title_end,true);
  304. DrawList->AddText(ImVec2(top_center.x - text_size.x * 0.5f, top_center.y+y),col,text_begin,title_end);
  305. }
  306. double NiceNum(double x, bool round) {
  307. double f; /* fractional part of x */
  308. double nf; /* nice, rounded fraction */
  309. int expv = (int)floor(ImLog10(x));
  310. f = x / ImPow(10.0, (double)expv); /* between 1 and 10 */
  311. if (round)
  312. if (f < 1.5)
  313. nf = 1;
  314. else if (f < 3)
  315. nf = 2;
  316. else if (f < 7)
  317. nf = 5;
  318. else
  319. nf = 10;
  320. else if (f <= 1)
  321. nf = 1;
  322. else if (f <= 2)
  323. nf = 2;
  324. else if (f <= 5)
  325. nf = 5;
  326. else
  327. nf = 10;
  328. return nf * ImPow(10.0, expv);
  329. }
  330. //-----------------------------------------------------------------------------
  331. // Context Utils
  332. //-----------------------------------------------------------------------------
  333. void SetImGuiContext(ImGuiContext* ctx) {
  334. ImGui::SetCurrentContext(ctx);
  335. }
  336. ImPlotContext* CreateContext() {
  337. ImPlotContext* ctx = IM_NEW(ImPlotContext)();
  338. Initialize(ctx);
  339. if (GImPlot == NULL)
  340. SetCurrentContext(ctx);
  341. return ctx;
  342. }
  343. void DestroyContext(ImPlotContext* ctx) {
  344. if (ctx == NULL)
  345. ctx = GImPlot;
  346. if (GImPlot == ctx)
  347. SetCurrentContext(NULL);
  348. IM_DELETE(ctx);
  349. }
  350. ImPlotContext* GetCurrentContext() {
  351. return GImPlot;
  352. }
  353. void SetCurrentContext(ImPlotContext* ctx) {
  354. GImPlot = ctx;
  355. }
  356. #define IMPLOT_APPEND_CMAP(name, qual) ctx->ColormapData.Append(#name, name, sizeof(name)/sizeof(ImU32), qual)
  357. #define IM_RGB(r,g,b) IM_COL32(r,g,b,255)
  358. void Initialize(ImPlotContext* ctx) {
  359. ResetCtxForNextPlot(ctx);
  360. ResetCtxForNextAlignedPlots(ctx);
  361. ResetCtxForNextSubplot(ctx);
  362. const ImU32 Deep[] = {4289753676, 4283598045, 4285048917, 4283584196, 4289950337, 4284512403, 4291005402, 4287401100, 4285839820, 4291671396 };
  363. const ImU32 Dark[] = {4280031972, 4290281015, 4283084621, 4288892568, 4278222847, 4281597951, 4280833702, 4290740727, 4288256409 };
  364. const ImU32 Pastel[] = {4289639675, 4293119411, 4291161036, 4293184478, 4289124862, 4291624959, 4290631909, 4293712637, 4294111986 };
  365. const ImU32 Paired[] = {4293119554, 4290017311, 4287291314, 4281114675, 4288256763, 4280031971, 4285513725, 4278222847, 4292260554, 4288298346, 4288282623, 4280834481};
  366. const ImU32 Viridis[] = {4283695428, 4285867080, 4287054913, 4287455029, 4287526954, 4287402273, 4286883874, 4285579076, 4283552122, 4280737725, 4280674301 };
  367. const ImU32 Plasma[] = {4287039501, 4288480321, 4289200234, 4288941455, 4287638193, 4286072780, 4284638433, 4283139314, 4281771772, 4280667900, 4280416752 };
  368. const ImU32 Hot[] = {4278190144, 4278190208, 4278190271, 4278190335, 4278206719, 4278223103, 4278239231, 4278255615, 4283826175, 4289396735, 4294967295 };
  369. const ImU32 Cool[] = {4294967040, 4294960666, 4294954035, 4294947661, 4294941030, 4294934656, 4294928025, 4294921651, 4294915020, 4294908646, 4294902015 };
  370. const ImU32 Pink[] = {4278190154, 4282532475, 4284308894, 4285690554, 4286879686, 4287870160, 4288794330, 4289651940, 4291685869, 4293392118, 4294967295 };
  371. const ImU32 Jet[] = {4289331200, 4294901760, 4294923520, 4294945280, 4294967040, 4289396565, 4283826090, 4278255615, 4278233855, 4278212095, 4278190335 };
  372. const ImU32 Twilight[] = {IM_RGB(226,217,226),IM_RGB(166,191,202),IM_RGB(109,144,192),IM_RGB(95,88,176),IM_RGB(83,30,124),IM_RGB(47,20,54),IM_RGB(100,25,75),IM_RGB(159,60,80),IM_RGB(192,117,94),IM_RGB(208,179,158),IM_RGB(226,217,226)};
  373. const ImU32 RdBu[] = {IM_RGB(103,0,31),IM_RGB(178,24,43),IM_RGB(214,96,77),IM_RGB(244,165,130),IM_RGB(253,219,199),IM_RGB(247,247,247),IM_RGB(209,229,240),IM_RGB(146,197,222),IM_RGB(67,147,195),IM_RGB(33,102,172),IM_RGB(5,48,97)};
  374. const ImU32 BrBG[] = {IM_RGB(84,48,5),IM_RGB(140,81,10),IM_RGB(191,129,45),IM_RGB(223,194,125),IM_RGB(246,232,195),IM_RGB(245,245,245),IM_RGB(199,234,229),IM_RGB(128,205,193),IM_RGB(53,151,143),IM_RGB(1,102,94),IM_RGB(0,60,48)};
  375. const ImU32 PiYG[] = {IM_RGB(142,1,82),IM_RGB(197,27,125),IM_RGB(222,119,174),IM_RGB(241,182,218),IM_RGB(253,224,239),IM_RGB(247,247,247),IM_RGB(230,245,208),IM_RGB(184,225,134),IM_RGB(127,188,65),IM_RGB(77,146,33),IM_RGB(39,100,25)};
  376. const ImU32 Spectral[] = {IM_RGB(158,1,66),IM_RGB(213,62,79),IM_RGB(244,109,67),IM_RGB(253,174,97),IM_RGB(254,224,139),IM_RGB(255,255,191),IM_RGB(230,245,152),IM_RGB(171,221,164),IM_RGB(102,194,165),IM_RGB(50,136,189),IM_RGB(94,79,162)};
  377. const ImU32 Greys[] = {IM_COL32_WHITE, IM_COL32_BLACK };
  378. IMPLOT_APPEND_CMAP(Deep, true);
  379. IMPLOT_APPEND_CMAP(Dark, true);
  380. IMPLOT_APPEND_CMAP(Pastel, true);
  381. IMPLOT_APPEND_CMAP(Paired, true);
  382. IMPLOT_APPEND_CMAP(Viridis, false);
  383. IMPLOT_APPEND_CMAP(Plasma, false);
  384. IMPLOT_APPEND_CMAP(Hot, false);
  385. IMPLOT_APPEND_CMAP(Cool, false);
  386. IMPLOT_APPEND_CMAP(Pink, false);
  387. IMPLOT_APPEND_CMAP(Jet, false);
  388. IMPLOT_APPEND_CMAP(Twilight, false);
  389. IMPLOT_APPEND_CMAP(RdBu, false);
  390. IMPLOT_APPEND_CMAP(BrBG, false);
  391. IMPLOT_APPEND_CMAP(PiYG, false);
  392. IMPLOT_APPEND_CMAP(Spectral, false);
  393. IMPLOT_APPEND_CMAP(Greys, false);
  394. }
  395. void ResetCtxForNextPlot(ImPlotContext* ctx) {
  396. // end child window if it was made
  397. if (ctx->ChildWindowMade)
  398. ImGui::EndChild();
  399. ctx->ChildWindowMade = false;
  400. // reset the next plot/item data
  401. ctx->NextPlotData.Reset();
  402. ctx->NextItemData.Reset();
  403. // reset ticks/labels
  404. ctx->XTicks.Reset();
  405. for (int i = 0; i < 3; ++i)
  406. ctx->YTicks[i].Reset();
  407. // reset labels
  408. ctx->Annotations.Reset();
  409. // reset extents/fit
  410. ctx->FitThisFrame = false;
  411. ctx->FitX = false;
  412. ctx->ExtentsX.Min = HUGE_VAL;
  413. ctx->ExtentsX.Max = -HUGE_VAL;
  414. for (int i = 0; i < IMPLOT_Y_AXES; i++) {
  415. ctx->ExtentsY[i].Min = HUGE_VAL;
  416. ctx->ExtentsY[i].Max = -HUGE_VAL;
  417. ctx->FitY[i] = false;
  418. }
  419. // reset digital plot items count
  420. ctx->DigitalPlotItemCnt = 0;
  421. ctx->DigitalPlotOffset = 0;
  422. // nullify plot
  423. ctx->CurrentPlot = NULL;
  424. ctx->CurrentItem = NULL;
  425. ctx->PreviousItem = NULL;
  426. }
  427. void ResetCtxForNextAlignedPlots(ImPlotContext* ctx) {
  428. ctx->CurrentAlignmentH = NULL;
  429. ctx->CurrentAlignmentV = NULL;
  430. }
  431. void ResetCtxForNextSubplot(ImPlotContext* ctx) {
  432. ctx->CurrentSubplot = NULL;
  433. ctx->CurrentAlignmentH = NULL;
  434. ctx->CurrentAlignmentV = NULL;
  435. }
  436. //-----------------------------------------------------------------------------
  437. // Plot Utils
  438. //-----------------------------------------------------------------------------
  439. ImPlotPlot* GetPlot(const char* title) {
  440. ImGuiWindow* Window = GImGui->CurrentWindow;
  441. const ImGuiID ID = Window->GetID(title);
  442. return GImPlot->Plots.GetByKey(ID);
  443. }
  444. ImPlotPlot* GetCurrentPlot() {
  445. return GImPlot->CurrentPlot;
  446. }
  447. void BustPlotCache() {
  448. GImPlot->Plots.Clear();
  449. GImPlot->Subplots.Clear();
  450. }
  451. void PushLinkedAxis(ImPlotAxis& axis) {
  452. if (axis.LinkedMin) { *axis.LinkedMin = axis.Range.Min; }
  453. if (axis.LinkedMax) { *axis.LinkedMax = axis.Range.Max; }
  454. }
  455. void PullLinkedAxis(ImPlotAxis& axis) {
  456. if (axis.LinkedMin) { axis.SetMin(*axis.LinkedMin,true); }
  457. if (axis.LinkedMax) { axis.SetMax(*axis.LinkedMax,true); }
  458. }
  459. //-----------------------------------------------------------------------------
  460. // Coordinate Utils
  461. //-----------------------------------------------------------------------------
  462. void UpdateTransformCache() {
  463. ImPlotContext& gp = *GImPlot;
  464. ImPlotPlot& plot = *gp.CurrentPlot;
  465. // get pixels for transforms
  466. for (int i = 0; i < IMPLOT_Y_AXES; i++) {
  467. gp.PixelRange[i] = ImRect(plot.XAxis.IsInverted() ? plot.PlotRect.Max.x : plot.PlotRect.Min.x,
  468. plot.YAxis[i].IsInverted() ? plot.PlotRect.Min.y : plot.PlotRect.Max.y,
  469. plot.XAxis.IsInverted() ? plot.PlotRect.Min.x : plot.PlotRect.Max.x,
  470. plot.YAxis[i].IsInverted() ? plot.PlotRect.Max.y : plot.PlotRect.Min.y);
  471. gp.My[i] = (gp.PixelRange[i].Max.y - gp.PixelRange[i].Min.y) / plot.YAxis[i].Range.Size();
  472. }
  473. gp.LogDenX = plot.XAxis.IsLog() ? ImLog10(plot.XAxis.Range.Max / plot.XAxis.Range.Min) : 0;
  474. for (int i = 0; i < IMPLOT_Y_AXES; i++)
  475. gp.LogDenY[i] = plot.YAxis[i].IsLog() ? ImLog10(plot.YAxis[i].Range.Max / plot.YAxis[i].Range.Min) : 0;
  476. gp.Mx = (gp.PixelRange[0].Max.x - gp.PixelRange[0].Min.x) / plot.XAxis.Range.Size();
  477. }
  478. ImPlotPoint PixelsToPlot(float x, float y, ImPlotYAxis y_axis_in) {
  479. ImPlotContext& gp = *GImPlot;
  480. ImPlotPlot& plot = *gp.CurrentPlot;
  481. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "PixelsToPlot() needs to be called between BeginPlot() and EndPlot()!");
  482. const ImPlotYAxis y_axis = y_axis_in >= 0 ? y_axis_in : plot.CurrentYAxis;
  483. ImPlotPoint plt;
  484. plt.x = (x - gp.PixelRange[y_axis].Min.x) / gp.Mx + plot.XAxis.Range.Min;
  485. plt.y = (y - gp.PixelRange[y_axis].Min.y) / gp.My[y_axis] + plot.YAxis[y_axis].Range.Min;
  486. if (ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_LogScale)) {
  487. double t = (plt.x - plot.XAxis.Range.Min) / plot.XAxis.Range.Size();
  488. plt.x = ImPow(10, t * gp.LogDenX) * plot.XAxis.Range.Min;
  489. }
  490. if (ImHasFlag(plot.YAxis[y_axis].Flags, ImPlotAxisFlags_LogScale)) {
  491. double t = (plt.y - plot.YAxis[y_axis].Range.Min) / plot.YAxis[y_axis].Range.Size();
  492. plt.y = ImPow(10, t * gp.LogDenY[y_axis]) * plot.YAxis[y_axis].Range.Min;
  493. }
  494. return plt;
  495. }
  496. ImPlotPoint PixelsToPlot(const ImVec2& pix, ImPlotYAxis y_axis) {
  497. return PixelsToPlot(pix.x, pix.y, y_axis);
  498. }
  499. ImVec2 PlotToPixels(double x, double y, ImPlotYAxis y_axis_in) {
  500. ImPlotContext& gp = *GImPlot;
  501. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "PlotToPixels() needs to be called between BeginPlot() and EndPlot()!");
  502. const ImPlotYAxis y_axis = y_axis_in >= 0 ? y_axis_in : gp.CurrentPlot->CurrentYAxis;
  503. ImVec2 pix;
  504. if (ImHasFlag(gp.CurrentPlot->XAxis.Flags, ImPlotAxisFlags_LogScale)) {
  505. x = x <= 0.0 ? IMPLOT_LOG_ZERO : x;
  506. double t = ImLog10(x / gp.CurrentPlot->XAxis.Range.Min) / gp.LogDenX;
  507. x = ImLerp(gp.CurrentPlot->XAxis.Range.Min, gp.CurrentPlot->XAxis.Range.Max, (float)t);
  508. }
  509. if (ImHasFlag(gp.CurrentPlot->YAxis[y_axis].Flags, ImPlotAxisFlags_LogScale)) {
  510. y = y <= 0.0 ? IMPLOT_LOG_ZERO : y;
  511. double t = ImLog10(y / gp.CurrentPlot->YAxis[y_axis].Range.Min) / gp.LogDenY[y_axis];
  512. y = ImLerp(gp.CurrentPlot->YAxis[y_axis].Range.Min, gp.CurrentPlot->YAxis[y_axis].Range.Max, (float)t);
  513. }
  514. pix.x = (float)(gp.PixelRange[y_axis].Min.x + gp.Mx * (x - gp.CurrentPlot->XAxis.Range.Min));
  515. pix.y = (float)(gp.PixelRange[y_axis].Min.y + gp.My[y_axis] * (y - gp.CurrentPlot->YAxis[y_axis].Range.Min));
  516. return pix;
  517. }
  518. ImVec2 PlotToPixels(const ImPlotPoint& plt, ImPlotYAxis y_axis) {
  519. return PlotToPixels(plt.x, plt.y, y_axis);
  520. }
  521. //-----------------------------------------------------------------------------
  522. // Legend Utils
  523. //-----------------------------------------------------------------------------
  524. ImVec2 GetLocationPos(const ImRect& outer_rect, const ImVec2& inner_size, ImPlotLocation loc, const ImVec2& pad) {
  525. ImVec2 pos;
  526. if (ImHasFlag(loc, ImPlotLocation_West) && !ImHasFlag(loc, ImPlotLocation_East))
  527. pos.x = outer_rect.Min.x + pad.x;
  528. else if (!ImHasFlag(loc, ImPlotLocation_West) && ImHasFlag(loc, ImPlotLocation_East))
  529. pos.x = outer_rect.Max.x - pad.x - inner_size.x;
  530. else
  531. pos.x = outer_rect.GetCenter().x - inner_size.x * 0.5f;
  532. // legend reference point y
  533. if (ImHasFlag(loc, ImPlotLocation_North) && !ImHasFlag(loc, ImPlotLocation_South))
  534. pos.y = outer_rect.Min.y + pad.y;
  535. else if (!ImHasFlag(loc, ImPlotLocation_North) && ImHasFlag(loc, ImPlotLocation_South))
  536. pos.y = outer_rect.Max.y - pad.y - inner_size.y;
  537. else
  538. pos.y = outer_rect.GetCenter().y - inner_size.y * 0.5f;
  539. pos.x = IM_ROUND(pos.x);
  540. pos.y = IM_ROUND(pos.y);
  541. return pos;
  542. }
  543. ImVec2 CalcLegendSize(ImPlotItemGroup& items, const ImVec2& pad, const ImVec2& spacing, ImPlotOrientation orn) {
  544. // vars
  545. const int nItems = items.GetLegendCount();
  546. const float txt_ht = ImGui::GetTextLineHeight();
  547. const float icon_size = txt_ht;
  548. // get label max width
  549. float max_label_width = 0;
  550. float sum_label_width = 0;
  551. for (int i = 0; i < nItems; ++i) {
  552. const char* label = items.GetLegendLabel(i);
  553. const float label_width = ImGui::CalcTextSize(label, NULL, true).x;
  554. max_label_width = label_width > max_label_width ? label_width : max_label_width;
  555. sum_label_width += label_width;
  556. }
  557. // calc legend size
  558. const ImVec2 legend_size = orn == ImPlotOrientation_Vertical ?
  559. ImVec2(pad.x * 2 + icon_size + max_label_width, pad.y * 2 + nItems * txt_ht + (nItems - 1) * spacing.y) :
  560. ImVec2(pad.x * 2 + icon_size * nItems + sum_label_width + (nItems - 1) * spacing.x, pad.y * 2 + txt_ht);
  561. return legend_size;
  562. }
  563. bool ShowLegendEntries(ImPlotItemGroup& items, const ImRect& legend_bb, bool hovered, const ImVec2& pad, const ImVec2& spacing, ImPlotOrientation orn, ImDrawList& DrawList) {
  564. ImGuiIO& IO = ImGui::GetIO();
  565. // vars
  566. const float txt_ht = ImGui::GetTextLineHeight();
  567. const float icon_size = txt_ht;
  568. const float icon_shrink = 2;
  569. ImU32 col_txt = GetStyleColorU32(ImPlotCol_LegendText);
  570. ImU32 col_txt_dis = ImAlphaU32(col_txt, 0.25f);
  571. // render each legend item
  572. float sum_label_width = 0;
  573. bool any_item_hovered = false;
  574. for (int i = 0; i < items.GetLegendCount(); ++i) {
  575. ImPlotItem* item = items.GetLegendItem(i);
  576. const char* label = items.GetLegendLabel(i);
  577. const float label_width = ImGui::CalcTextSize(label, NULL, true).x;
  578. const ImVec2 top_left = orn == ImPlotOrientation_Vertical ?
  579. legend_bb.Min + pad + ImVec2(0, i * (txt_ht + spacing.y)) :
  580. legend_bb.Min + pad + ImVec2(i * (icon_size + spacing.x) + sum_label_width, 0);
  581. sum_label_width += label_width;
  582. ImRect icon_bb;
  583. icon_bb.Min = top_left + ImVec2(icon_shrink,icon_shrink);
  584. icon_bb.Max = top_left + ImVec2(icon_size - icon_shrink, icon_size - icon_shrink);
  585. ImRect label_bb;
  586. label_bb.Min = top_left;
  587. label_bb.Max = top_left + ImVec2(label_width + icon_size, icon_size);
  588. ImU32 col_txt_hl;
  589. ImU32 col_item = ImAlphaU32(item->Color,1);
  590. // ImGui::ItemAdd(icon_bb, item->ID, &icon_bb);
  591. // ImGui::KeepAliveID(item->ID);
  592. bool icon_hov = false;
  593. bool icon_hld = false;
  594. bool icon_clk = ImGui::ButtonBehavior(icon_bb, item->ID, &icon_hov, &icon_hld);
  595. if (icon_clk)
  596. item->Show = !item->Show;
  597. if (icon_hov || label_bb.Contains(IO.MousePos)) {
  598. item->LegendHovered = true;
  599. col_txt_hl = ImMixU32(col_txt, col_item, 64);
  600. any_item_hovered = true;
  601. }
  602. else {
  603. col_txt_hl = ImGui::GetColorU32(col_txt);
  604. }
  605. ImU32 col_icon;
  606. if (icon_hld)
  607. col_icon = item->Show ? ImAlphaU32(col_item,0.5f) : ImGui::GetColorU32(ImGuiCol_TextDisabled, 0.5f);
  608. else if (icon_hov)
  609. col_icon = item->Show ? ImAlphaU32(col_item,0.75f) : ImGui::GetColorU32(ImGuiCol_TextDisabled, 0.75f);
  610. else
  611. col_icon = item->Show ? col_item : col_txt_dis;
  612. DrawList.AddRectFilled(icon_bb.Min, icon_bb.Max, col_icon, 1);
  613. const char* text_display_end = ImGui::FindRenderedTextEnd(label, NULL);
  614. if (label != text_display_end)
  615. DrawList.AddText(top_left + ImVec2(icon_size, 0), item->Show ? col_txt_hl : col_txt_dis, label, text_display_end);
  616. }
  617. return hovered && !any_item_hovered;
  618. }
  619. //-----------------------------------------------------------------------------
  620. // Tick Utils
  621. //-----------------------------------------------------------------------------
  622. static const float TICK_FILL_X = 0.8f;
  623. static const float TICK_FILL_Y = 1.0f;
  624. void AddTicksDefault(const ImPlotRange& range, float pix, ImPlotOrientation orn, ImPlotTickCollection& ticks, const char* fmt) {
  625. const int idx0 = ticks.Size;
  626. const int nMinor = 10;
  627. const int nMajor = ImMax(2, (int)IM_ROUND(pix / (orn == ImPlotOrientation_Horizontal ? 400.0f : 300.0f)));
  628. const double nice_range = NiceNum(range.Size() * 0.99, false);
  629. const double interval = NiceNum(nice_range / (nMajor - 1), true);
  630. const double graphmin = floor(range.Min / interval) * interval;
  631. const double graphmax = ceil(range.Max / interval) * interval;
  632. bool first_major_set = false;
  633. int first_major_idx = 0;
  634. ImVec2 total_size(0,0);
  635. for (double major = graphmin; major < graphmax + 0.5 * interval; major += interval) {
  636. // is this zero? combat zero formatting issues
  637. if (major-interval < 0 && major+interval > 0)
  638. major = 0;
  639. if (range.Contains(major)) {
  640. if (!first_major_set) {
  641. first_major_idx = ticks.Size;
  642. first_major_set = true;
  643. }
  644. total_size += ticks.Append(major, true, true, fmt).LabelSize;
  645. }
  646. for (int i = 1; i < nMinor; ++i) {
  647. double minor = major + i * interval / nMinor;
  648. if (range.Contains(minor)) {
  649. total_size += ticks.Append(minor, false, true, fmt).LabelSize;
  650. }
  651. }
  652. }
  653. // prune if necessary
  654. if ((orn == ImPlotOrientation_Horizontal && total_size.x > pix*TICK_FILL_X) || (orn == ImPlotOrientation_Vertical && total_size.y > pix*TICK_FILL_Y)) {
  655. for (int i = first_major_idx-1; i >= idx0; i -= 2)
  656. ticks.Ticks[i].ShowLabel = false;
  657. for (int i = first_major_idx+1; i < ticks.Size; i += 2)
  658. ticks.Ticks[i].ShowLabel = false;
  659. }
  660. }
  661. void AddTicksLogarithmic(const ImPlotRange& range, float pix, ImPlotOrientation orn, ImPlotTickCollection& ticks, const char* fmt) {
  662. if (range.Min <= 0 || range.Max <= 0)
  663. return;
  664. const int nMajor = orn == ImPlotOrientation_Horizontal ? ImMax(2, (int)IM_ROUND(pix * 0.01f)) : ImMax(2, (int)IM_ROUND(pix * 0.02f));
  665. double log_min = ImLog10(range.Min);
  666. double log_max = ImLog10(range.Max);
  667. int exp_step = ImMax(1,(int)(log_max - log_min) / nMajor);
  668. int exp_min = (int)log_min;
  669. int exp_max = (int)log_max;
  670. if (exp_step != 1) {
  671. while(exp_step % 3 != 0) exp_step++; // make step size multiple of three
  672. while(exp_min % exp_step != 0) exp_min--; // decrease exp_min until exp_min + N * exp_step will be 0
  673. }
  674. for (int e = exp_min - exp_step; e < (exp_max + exp_step); e += exp_step) {
  675. double major1 = ImPow(10, (double)(e));
  676. double major2 = ImPow(10, (double)(e + 1));
  677. double interval = (major2 - major1) / 9;
  678. if (major1 >= (range.Min - DBL_EPSILON) && major1 <= (range.Max + DBL_EPSILON))
  679. ticks.Append(major1, true, true, fmt);
  680. for (int j = 0; j < exp_step; ++j) {
  681. major1 = ImPow(10, (double)(e+j));
  682. major2 = ImPow(10, (double)(e+j+1));
  683. interval = (major2 - major1) / 9;
  684. for (int i = 1; i < (9 + (int)(j < (exp_step - 1))); ++i) {
  685. double minor = major1 + i * interval;
  686. if (minor >= (range.Min - DBL_EPSILON) && minor <= (range.Max + DBL_EPSILON))
  687. ticks.Append(minor, false, false, fmt);
  688. }
  689. }
  690. }
  691. }
  692. void AddTicksCustom(const double* values, const char* const labels[], int n, ImPlotTickCollection& ticks, const char* fmt) {
  693. for (int i = 0; i < n; ++i) {
  694. if (labels != NULL) {
  695. ImPlotTick tick(values[i], false, true);
  696. tick.TextOffset = ticks.TextBuffer.size();
  697. ticks.TextBuffer.append(labels[i], labels[i] + strlen(labels[i]) + 1);
  698. tick.LabelSize = ImGui::CalcTextSize(labels[i]);
  699. ticks.Append(tick);
  700. }
  701. else {
  702. ticks.Append(values[i], false, true, fmt);
  703. }
  704. }
  705. }
  706. //-----------------------------------------------------------------------------
  707. // Time Ticks and Utils
  708. //-----------------------------------------------------------------------------
  709. // this may not be thread safe?
  710. static const double TimeUnitSpans[ImPlotTimeUnit_COUNT] = {
  711. 0.000001,
  712. 0.001,
  713. 1,
  714. 60,
  715. 3600,
  716. 86400,
  717. 2629800,
  718. 31557600
  719. };
  720. inline ImPlotTimeUnit GetUnitForRange(double range) {
  721. static double cutoffs[ImPlotTimeUnit_COUNT] = {0.001, 1, 60, 3600, 86400, 2629800, 31557600, IMPLOT_MAX_TIME};
  722. for (int i = 0; i < ImPlotTimeUnit_COUNT; ++i) {
  723. if (range <= cutoffs[i])
  724. return (ImPlotTimeUnit)i;
  725. }
  726. return ImPlotTimeUnit_Yr;
  727. }
  728. inline int LowerBoundStep(int max_divs, const int* divs, const int* step, int size) {
  729. if (max_divs < divs[0])
  730. return 0;
  731. for (int i = 1; i < size; ++i) {
  732. if (max_divs < divs[i])
  733. return step[i-1];
  734. }
  735. return step[size-1];
  736. }
  737. inline int GetTimeStep(int max_divs, ImPlotTimeUnit unit) {
  738. if (unit == ImPlotTimeUnit_Ms || unit == ImPlotTimeUnit_Us) {
  739. static const int step[] = {500,250,200,100,50,25,20,10,5,2,1};
  740. static const int divs[] = {2,4,5,10,20,40,50,100,200,500,1000};
  741. return LowerBoundStep(max_divs, divs, step, 11);
  742. }
  743. if (unit == ImPlotTimeUnit_S || unit == ImPlotTimeUnit_Min) {
  744. static const int step[] = {30,15,10,5,1};
  745. static const int divs[] = {2,4,6,12,60};
  746. return LowerBoundStep(max_divs, divs, step, 5);
  747. }
  748. else if (unit == ImPlotTimeUnit_Hr) {
  749. static const int step[] = {12,6,3,2,1};
  750. static const int divs[] = {2,4,8,12,24};
  751. return LowerBoundStep(max_divs, divs, step, 5);
  752. }
  753. else if (unit == ImPlotTimeUnit_Day) {
  754. static const int step[] = {14,7,2,1};
  755. static const int divs[] = {2,4,14,28};
  756. return LowerBoundStep(max_divs, divs, step, 4);
  757. }
  758. else if (unit == ImPlotTimeUnit_Mo) {
  759. static const int step[] = {6,3,2,1};
  760. static const int divs[] = {2,4,6,12};
  761. return LowerBoundStep(max_divs, divs, step, 4);
  762. }
  763. return 0;
  764. }
  765. ImPlotTime MkGmtTime(struct tm *ptm) {
  766. ImPlotTime t;
  767. #ifdef _WIN32
  768. t.S = _mkgmtime(ptm);
  769. #else
  770. t.S = timegm(ptm);
  771. #endif
  772. if (t.S < 0)
  773. t.S = 0;
  774. return t;
  775. }
  776. tm* GetGmtTime(const ImPlotTime& t, tm* ptm)
  777. {
  778. #ifdef _WIN32
  779. if (gmtime_s(ptm, &t.S) == 0)
  780. return ptm;
  781. else
  782. return NULL;
  783. #else
  784. return gmtime_r(&t.S, ptm);
  785. #endif
  786. }
  787. ImPlotTime MkLocTime(struct tm *ptm) {
  788. ImPlotTime t;
  789. t.S = mktime(ptm);
  790. if (t.S < 0)
  791. t.S = 0;
  792. return t;
  793. }
  794. tm* GetLocTime(const ImPlotTime& t, tm* ptm) {
  795. #ifdef _WIN32
  796. if (localtime_s(ptm, &t.S) == 0)
  797. return ptm;
  798. else
  799. return NULL;
  800. #else
  801. return localtime_r(&t.S, ptm);
  802. #endif
  803. }
  804. inline ImPlotTime MkTime(struct tm *ptm) {
  805. if (GetStyle().UseLocalTime)
  806. return MkLocTime(ptm);
  807. else
  808. return MkGmtTime(ptm);
  809. }
  810. inline tm* GetTime(const ImPlotTime& t, tm* ptm) {
  811. if (GetStyle().UseLocalTime)
  812. return GetLocTime(t,ptm);
  813. else
  814. return GetGmtTime(t,ptm);
  815. }
  816. ImPlotTime MakeTime(int year, int month, int day, int hour, int min, int sec, int us) {
  817. tm& Tm = GImPlot->Tm;
  818. int yr = year - 1900;
  819. if (yr < 0)
  820. yr = 0;
  821. sec = sec + us / 1000000;
  822. us = us % 1000000;
  823. Tm.tm_sec = sec;
  824. Tm.tm_min = min;
  825. Tm.tm_hour = hour;
  826. Tm.tm_mday = day;
  827. Tm.tm_mon = month;
  828. Tm.tm_year = yr;
  829. ImPlotTime t = MkTime(&Tm);
  830. t.Us = us;
  831. return t;
  832. }
  833. int GetYear(const ImPlotTime& t) {
  834. tm& Tm = GImPlot->Tm;
  835. GetTime(t, &Tm);
  836. return Tm.tm_year + 1900;
  837. }
  838. ImPlotTime AddTime(const ImPlotTime& t, ImPlotTimeUnit unit, int count) {
  839. tm& Tm = GImPlot->Tm;
  840. ImPlotTime t_out = t;
  841. switch(unit) {
  842. case ImPlotTimeUnit_Us: t_out.Us += count; break;
  843. case ImPlotTimeUnit_Ms: t_out.Us += count * 1000; break;
  844. case ImPlotTimeUnit_S: t_out.S += count; break;
  845. case ImPlotTimeUnit_Min: t_out.S += count * 60; break;
  846. case ImPlotTimeUnit_Hr: t_out.S += count * 3600; break;
  847. case ImPlotTimeUnit_Day: t_out.S += count * 86400; break;
  848. case ImPlotTimeUnit_Mo: for (int i = 0; i < abs(count); ++i) {
  849. GetTime(t_out, &Tm);
  850. if (count > 0)
  851. t_out.S += 86400 * GetDaysInMonth(Tm.tm_year + 1900, Tm.tm_mon);
  852. else if (count < 0)
  853. t_out.S -= 86400 * GetDaysInMonth(Tm.tm_year + 1900 - (Tm.tm_mon == 0 ? 1 : 0), Tm.tm_mon == 0 ? 11 : Tm.tm_mon - 1); // NOT WORKING
  854. }
  855. break;
  856. case ImPlotTimeUnit_Yr: for (int i = 0; i < abs(count); ++i) {
  857. if (count > 0)
  858. t_out.S += 86400 * (365 + (int)IsLeapYear(GetYear(t_out)));
  859. else if (count < 0)
  860. t_out.S -= 86400 * (365 + (int)IsLeapYear(GetYear(t_out) - 1));
  861. // this is incorrect if leap year and we are past Feb 28
  862. }
  863. break;
  864. default: break;
  865. }
  866. t_out.RollOver();
  867. return t_out;
  868. }
  869. ImPlotTime FloorTime(const ImPlotTime& t, ImPlotTimeUnit unit) {
  870. GetTime(t, &GImPlot->Tm);
  871. switch (unit) {
  872. case ImPlotTimeUnit_S: return ImPlotTime(t.S, 0);
  873. case ImPlotTimeUnit_Ms: return ImPlotTime(t.S, (t.Us / 1000) * 1000);
  874. case ImPlotTimeUnit_Us: return t;
  875. case ImPlotTimeUnit_Yr: GImPlot->Tm.tm_mon = 0; // fall-through
  876. case ImPlotTimeUnit_Mo: GImPlot->Tm.tm_mday = 1; // fall-through
  877. case ImPlotTimeUnit_Day: GImPlot->Tm.tm_hour = 0; // fall-through
  878. case ImPlotTimeUnit_Hr: GImPlot->Tm.tm_min = 0; // fall-through
  879. case ImPlotTimeUnit_Min: GImPlot->Tm.tm_sec = 0; break;
  880. default: return t;
  881. }
  882. return MkTime(&GImPlot->Tm);
  883. }
  884. ImPlotTime CeilTime(const ImPlotTime& t, ImPlotTimeUnit unit) {
  885. return AddTime(FloorTime(t, unit), unit, 1);
  886. }
  887. ImPlotTime RoundTime(const ImPlotTime& t, ImPlotTimeUnit unit) {
  888. ImPlotTime t1 = FloorTime(t, unit);
  889. ImPlotTime t2 = AddTime(t1,unit,1);
  890. if (t1.S == t2.S)
  891. return t.Us - t1.Us < t2.Us - t.Us ? t1 : t2;
  892. return t.S - t1.S < t2.S - t.S ? t1 : t2;
  893. }
  894. ImPlotTime CombineDateTime(const ImPlotTime& date_part, const ImPlotTime& tod_part) {
  895. tm& Tm = GImPlot->Tm;
  896. GetTime(date_part, &GImPlot->Tm);
  897. int y = Tm.tm_year;
  898. int m = Tm.tm_mon;
  899. int d = Tm.tm_mday;
  900. GetTime(tod_part, &GImPlot->Tm);
  901. Tm.tm_year = y;
  902. Tm.tm_mon = m;
  903. Tm.tm_mday = d;
  904. ImPlotTime t = MkTime(&Tm);
  905. t.Us = tod_part.Us;
  906. return t;
  907. }
  908. static const char* MONTH_NAMES[] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
  909. static const char* WD_ABRVS[] = {"Su","Mo","Tu","We","Th","Fr","Sa"};
  910. static const char* MONTH_ABRVS[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  911. int FormatTime(const ImPlotTime& t, char* buffer, int size, ImPlotTimeFmt fmt, bool use_24_hr_clk) {
  912. tm& Tm = GImPlot->Tm;
  913. GetTime(t, &Tm);
  914. const int us = t.Us % 1000;
  915. const int ms = t.Us / 1000;
  916. const int sec = Tm.tm_sec;
  917. const int min = Tm.tm_min;
  918. if (use_24_hr_clk) {
  919. const int hr = Tm.tm_hour;
  920. switch(fmt) {
  921. case ImPlotTimeFmt_Us: return snprintf(buffer, size, ".%03d %03d", ms, us);
  922. case ImPlotTimeFmt_SUs: return snprintf(buffer, size, ":%02d.%03d %03d", sec, ms, us);
  923. case ImPlotTimeFmt_SMs: return snprintf(buffer, size, ":%02d.%03d", sec, ms);
  924. case ImPlotTimeFmt_S: return snprintf(buffer, size, ":%02d", sec);
  925. case ImPlotTimeFmt_HrMinSMs: return snprintf(buffer, size, "%02d:%02d:%02d.%03d", hr, min, sec, ms);
  926. case ImPlotTimeFmt_HrMinS: return snprintf(buffer, size, "%02d:%02d:%02d", hr, min, sec);
  927. case ImPlotTimeFmt_HrMin: return snprintf(buffer, size, "%02d:%02d", hr, min);
  928. case ImPlotTimeFmt_Hr: return snprintf(buffer, size, "%02d:00", hr);
  929. default: return 0;
  930. }
  931. }
  932. else {
  933. const char* ap = Tm.tm_hour < 12 ? "am" : "pm";
  934. const int hr = (Tm.tm_hour == 0 || Tm.tm_hour == 12) ? 12 : Tm.tm_hour % 12;
  935. switch(fmt) {
  936. case ImPlotTimeFmt_Us: return snprintf(buffer, size, ".%03d %03d", ms, us);
  937. case ImPlotTimeFmt_SUs: return snprintf(buffer, size, ":%02d.%03d %03d", sec, ms, us);
  938. case ImPlotTimeFmt_SMs: return snprintf(buffer, size, ":%02d.%03d", sec, ms);
  939. case ImPlotTimeFmt_S: return snprintf(buffer, size, ":%02d", sec);
  940. case ImPlotTimeFmt_HrMinSMs: return snprintf(buffer, size, "%d:%02d:%02d.%03d%s", hr, min, sec, ms, ap);
  941. case ImPlotTimeFmt_HrMinS: return snprintf(buffer, size, "%d:%02d:%02d%s", hr, min, sec, ap);
  942. case ImPlotTimeFmt_HrMin: return snprintf(buffer, size, "%d:%02d%s", hr, min, ap);
  943. case ImPlotTimeFmt_Hr: return snprintf(buffer, size, "%d%s", hr, ap);
  944. default: return 0;
  945. }
  946. }
  947. }
  948. int FormatDate(const ImPlotTime& t, char* buffer, int size, ImPlotDateFmt fmt, bool use_iso_8601) {
  949. tm& Tm = GImPlot->Tm;
  950. GetTime(t, &Tm);
  951. const int day = Tm.tm_mday;
  952. const int mon = Tm.tm_mon + 1;
  953. const int year = Tm.tm_year + 1900;
  954. const int yr = year % 100;
  955. if (use_iso_8601) {
  956. switch (fmt) {
  957. case ImPlotDateFmt_DayMo: return snprintf(buffer, size, "--%02d-%02d", mon, day);
  958. case ImPlotDateFmt_DayMoYr: return snprintf(buffer, size, "%d-%02d-%02d", year, mon, day);
  959. case ImPlotDateFmt_MoYr: return snprintf(buffer, size, "%d-%02d", year, mon);
  960. case ImPlotDateFmt_Mo: return snprintf(buffer, size, "--%02d", mon);
  961. case ImPlotDateFmt_Yr: return snprintf(buffer, size, "%d", year);
  962. default: return 0;
  963. }
  964. }
  965. else {
  966. switch (fmt) {
  967. case ImPlotDateFmt_DayMo: return snprintf(buffer, size, "%d/%d", mon, day);
  968. case ImPlotDateFmt_DayMoYr: return snprintf(buffer, size, "%d/%d/%02d", mon, day, yr);
  969. case ImPlotDateFmt_MoYr: return snprintf(buffer, size, "%s %d", MONTH_ABRVS[Tm.tm_mon], year);
  970. case ImPlotDateFmt_Mo: return snprintf(buffer, size, "%s", MONTH_ABRVS[Tm.tm_mon]);
  971. case ImPlotDateFmt_Yr: return snprintf(buffer, size, "%d", year);
  972. default: return 0;
  973. }
  974. }
  975. }
  976. int FormatDateTime(const ImPlotTime& t, char* buffer, int size, ImPlotDateTimeFmt fmt) {
  977. int written = 0;
  978. if (fmt.Date != ImPlotDateFmt_None)
  979. written += FormatDate(t, buffer, size, fmt.Date, fmt.UseISO8601);
  980. if (fmt.Time != ImPlotTimeFmt_None) {
  981. if (fmt.Date != ImPlotDateFmt_None)
  982. buffer[written++] = ' ';
  983. written += FormatTime(t, &buffer[written], size - written, fmt.Time, fmt.Use24HourClock);
  984. }
  985. return written;
  986. }
  987. inline float GetDateTimeWidth(ImPlotDateTimeFmt fmt) {
  988. static const ImPlotTime t_max_width = MakeTime(2888, 12, 22, 12, 58, 58, 888888); // best guess at time that maximizes pixel width
  989. char buffer[32];
  990. FormatDateTime(t_max_width, buffer, 32, fmt);
  991. return ImGui::CalcTextSize(buffer).x;
  992. }
  993. void LabelTickTime(ImPlotTick& tick, ImGuiTextBuffer& buffer, const ImPlotTime& t, ImPlotDateTimeFmt fmt) {
  994. char temp[32];
  995. if (tick.ShowLabel) {
  996. tick.TextOffset = buffer.size();
  997. FormatDateTime(t, temp, 32, fmt);
  998. buffer.append(temp, temp + strlen(temp) + 1);
  999. tick.LabelSize = ImGui::CalcTextSize(buffer.Buf.Data + tick.TextOffset);
  1000. }
  1001. }
  1002. inline bool TimeLabelSame(const char* l1, const char* l2) {
  1003. size_t len1 = strlen(l1);
  1004. size_t len2 = strlen(l2);
  1005. size_t n = len1 < len2 ? len1 : len2;
  1006. return strcmp(l1 + len1 - n, l2 + len2 - n) == 0;
  1007. }
  1008. static const ImPlotDateTimeFmt TimeFormatLevel0[ImPlotTimeUnit_COUNT] = {
  1009. ImPlotDateTimeFmt(ImPlotDateFmt_None, ImPlotTimeFmt_Us),
  1010. ImPlotDateTimeFmt(ImPlotDateFmt_None, ImPlotTimeFmt_SMs),
  1011. ImPlotDateTimeFmt(ImPlotDateFmt_None, ImPlotTimeFmt_S),
  1012. ImPlotDateTimeFmt(ImPlotDateFmt_None, ImPlotTimeFmt_HrMin),
  1013. ImPlotDateTimeFmt(ImPlotDateFmt_None, ImPlotTimeFmt_Hr),
  1014. ImPlotDateTimeFmt(ImPlotDateFmt_DayMo, ImPlotTimeFmt_None),
  1015. ImPlotDateTimeFmt(ImPlotDateFmt_Mo, ImPlotTimeFmt_None),
  1016. ImPlotDateTimeFmt(ImPlotDateFmt_Yr, ImPlotTimeFmt_None)
  1017. };
  1018. static const ImPlotDateTimeFmt TimeFormatLevel1[ImPlotTimeUnit_COUNT] = {
  1019. ImPlotDateTimeFmt(ImPlotDateFmt_None, ImPlotTimeFmt_HrMin),
  1020. ImPlotDateTimeFmt(ImPlotDateFmt_None, ImPlotTimeFmt_HrMinS),
  1021. ImPlotDateTimeFmt(ImPlotDateFmt_None, ImPlotTimeFmt_HrMin),
  1022. ImPlotDateTimeFmt(ImPlotDateFmt_None, ImPlotTimeFmt_HrMin),
  1023. ImPlotDateTimeFmt(ImPlotDateFmt_DayMoYr, ImPlotTimeFmt_None),
  1024. ImPlotDateTimeFmt(ImPlotDateFmt_DayMoYr, ImPlotTimeFmt_None),
  1025. ImPlotDateTimeFmt(ImPlotDateFmt_Yr, ImPlotTimeFmt_None),
  1026. ImPlotDateTimeFmt(ImPlotDateFmt_Yr, ImPlotTimeFmt_None)
  1027. };
  1028. static const ImPlotDateTimeFmt TimeFormatLevel1First[ImPlotTimeUnit_COUNT] = {
  1029. ImPlotDateTimeFmt(ImPlotDateFmt_DayMoYr, ImPlotTimeFmt_HrMinS),
  1030. ImPlotDateTimeFmt(ImPlotDateFmt_DayMoYr, ImPlotTimeFmt_HrMinS),
  1031. ImPlotDateTimeFmt(ImPlotDateFmt_DayMoYr, ImPlotTimeFmt_HrMin),
  1032. ImPlotDateTimeFmt(ImPlotDateFmt_DayMoYr, ImPlotTimeFmt_HrMin),
  1033. ImPlotDateTimeFmt(ImPlotDateFmt_DayMoYr, ImPlotTimeFmt_None),
  1034. ImPlotDateTimeFmt(ImPlotDateFmt_DayMoYr, ImPlotTimeFmt_None),
  1035. ImPlotDateTimeFmt(ImPlotDateFmt_Yr, ImPlotTimeFmt_None),
  1036. ImPlotDateTimeFmt(ImPlotDateFmt_Yr, ImPlotTimeFmt_None)
  1037. };
  1038. static const ImPlotDateTimeFmt TimeFormatMouseCursor[ImPlotTimeUnit_COUNT] = {
  1039. ImPlotDateTimeFmt(ImPlotDateFmt_None, ImPlotTimeFmt_Us),
  1040. ImPlotDateTimeFmt(ImPlotDateFmt_None, ImPlotTimeFmt_SUs),
  1041. ImPlotDateTimeFmt(ImPlotDateFmt_None, ImPlotTimeFmt_SMs),
  1042. ImPlotDateTimeFmt(ImPlotDateFmt_None, ImPlotTimeFmt_HrMinS),
  1043. ImPlotDateTimeFmt(ImPlotDateFmt_None, ImPlotTimeFmt_HrMin),
  1044. ImPlotDateTimeFmt(ImPlotDateFmt_DayMo, ImPlotTimeFmt_Hr),
  1045. ImPlotDateTimeFmt(ImPlotDateFmt_DayMoYr, ImPlotTimeFmt_None),
  1046. ImPlotDateTimeFmt(ImPlotDateFmt_MoYr, ImPlotTimeFmt_None)
  1047. };
  1048. inline ImPlotDateTimeFmt GetDateTimeFmt(const ImPlotDateTimeFmt* ctx, ImPlotTimeUnit idx) {
  1049. ImPlotStyle& style = GetStyle();
  1050. ImPlotDateTimeFmt fmt = ctx[idx];
  1051. fmt.UseISO8601 = style.UseISO8601;
  1052. fmt.Use24HourClock = style.Use24HourClock;
  1053. return fmt;
  1054. }
  1055. void AddTicksTime(const ImPlotRange& range, float plot_width, ImPlotTickCollection& ticks) {
  1056. // get units for level 0 and level 1 labels
  1057. const ImPlotTimeUnit unit0 = GetUnitForRange(range.Size() / (plot_width / 100)); // level = 0 (top)
  1058. const ImPlotTimeUnit unit1 = unit0 + 1; // level = 1 (bottom)
  1059. // get time format specs
  1060. const ImPlotDateTimeFmt fmt0 = GetDateTimeFmt(TimeFormatLevel0, unit0);
  1061. const ImPlotDateTimeFmt fmt1 = GetDateTimeFmt(TimeFormatLevel1, unit1);
  1062. const ImPlotDateTimeFmt fmtf = GetDateTimeFmt(TimeFormatLevel1First, unit1);
  1063. // min max times
  1064. const ImPlotTime t_min = ImPlotTime::FromDouble(range.Min);
  1065. const ImPlotTime t_max = ImPlotTime::FromDouble(range.Max);
  1066. // maximum allowable density of labels
  1067. const float max_density = 0.5f;
  1068. // book keeping
  1069. const char* last_major = NULL;
  1070. if (unit0 != ImPlotTimeUnit_Yr) {
  1071. // pixels per major (level 1) division
  1072. const float pix_per_major_div = plot_width / (float)(range.Size() / TimeUnitSpans[unit1]);
  1073. // nominal pixels taken up by labels
  1074. const float fmt0_width = GetDateTimeWidth(fmt0);
  1075. const float fmt1_width = GetDateTimeWidth(fmt1);
  1076. const float fmtf_width = GetDateTimeWidth(fmtf);
  1077. // the maximum number of minor (level 0) labels that can fit between major (level 1) divisions
  1078. const int minor_per_major = (int)(max_density * pix_per_major_div / fmt0_width);
  1079. // the minor step size (level 0)
  1080. const int step = GetTimeStep(minor_per_major, unit0);
  1081. // generate ticks
  1082. ImPlotTime t1 = FloorTime(ImPlotTime::FromDouble(range.Min), unit1);
  1083. while (t1 < t_max) {
  1084. // get next major
  1085. const ImPlotTime t2 = AddTime(t1, unit1, 1);
  1086. // add major tick
  1087. if (t1 >= t_min && t1 <= t_max) {
  1088. // minor level 0 tick
  1089. ImPlotTick tick_min(t1.ToDouble(),true,true);
  1090. tick_min.Level = 0;
  1091. LabelTickTime(tick_min,ticks.TextBuffer,t1,fmt0);
  1092. ticks.Append(tick_min);
  1093. // major level 1 tick
  1094. ImPlotTick tick_maj(t1.ToDouble(),true,true);
  1095. tick_maj.Level = 1;
  1096. LabelTickTime(tick_maj,ticks.TextBuffer,t1, last_major == NULL ? fmtf : fmt1);
  1097. const char* this_major = ticks.TextBuffer.Buf.Data + tick_maj.TextOffset;
  1098. if (last_major && TimeLabelSame(last_major,this_major))
  1099. tick_maj.ShowLabel = false;
  1100. last_major = this_major;
  1101. ticks.Append(tick_maj);
  1102. }
  1103. // add minor ticks up until next major
  1104. if (minor_per_major > 1 && (t_min <= t2 && t1 <= t_max)) {
  1105. ImPlotTime t12 = AddTime(t1, unit0, step);
  1106. while (t12 < t2) {
  1107. float px_to_t2 = (float)((t2 - t12).ToDouble()/range.Size()) * plot_width;
  1108. if (t12 >= t_min && t12 <= t_max) {
  1109. ImPlotTick tick(t12.ToDouble(),false,px_to_t2 >= fmt0_width);
  1110. tick.Level = 0;
  1111. LabelTickTime(tick,ticks.TextBuffer,t12,fmt0);
  1112. ticks.Append(tick);
  1113. if (last_major == NULL && px_to_t2 >= fmt0_width && px_to_t2 >= (fmt1_width + fmtf_width) / 2) {
  1114. ImPlotTick tick_maj(t12.ToDouble(),true,true);
  1115. tick_maj.Level = 1;
  1116. LabelTickTime(tick_maj,ticks.TextBuffer,t12,fmtf);
  1117. last_major = ticks.TextBuffer.Buf.Data + tick_maj.TextOffset;
  1118. ticks.Append(tick_maj);
  1119. }
  1120. }
  1121. t12 = AddTime(t12, unit0, step);
  1122. }
  1123. }
  1124. t1 = t2;
  1125. }
  1126. }
  1127. else {
  1128. const ImPlotDateTimeFmt fmty = GetDateTimeFmt(TimeFormatLevel0, ImPlotTimeUnit_Yr);
  1129. const float label_width = GetDateTimeWidth(fmty);
  1130. const int max_labels = (int)(max_density * plot_width / label_width);
  1131. const int year_min = GetYear(t_min);
  1132. const int year_max = GetYear(CeilTime(t_max, ImPlotTimeUnit_Yr));
  1133. const double nice_range = NiceNum((year_max - year_min)*0.99,false);
  1134. const double interval = NiceNum(nice_range / (max_labels - 1), true);
  1135. const int graphmin = (int)(floor(year_min / interval) * interval);
  1136. const int graphmax = (int)(ceil(year_max / interval) * interval);
  1137. const int step = (int)interval <= 0 ? 1 : (int)interval;
  1138. for (int y = graphmin; y < graphmax; y += step) {
  1139. ImPlotTime t = MakeTime(y);
  1140. if (t >= t_min && t <= t_max) {
  1141. ImPlotTick tick(t.ToDouble(), true, true);
  1142. tick.Level = 0;
  1143. LabelTickTime(tick, ticks.TextBuffer, t, fmty);
  1144. ticks.Append(tick);
  1145. }
  1146. }
  1147. }
  1148. }
  1149. //-----------------------------------------------------------------------------
  1150. // Axis Utils
  1151. //-----------------------------------------------------------------------------
  1152. static inline int AxisPrecision(const ImPlotAxis& axis, const ImPlotTickCollection& ticks) {
  1153. const double range = ticks.Size > 1 ? (ticks.Ticks[1].PlotPos - ticks.Ticks[0].PlotPos) : axis.Range.Size();
  1154. return Precision(range);
  1155. }
  1156. static inline double RoundAxisValue(const ImPlotAxis& axis, const ImPlotTickCollection& ticks, double value) {
  1157. return RoundTo(value, AxisPrecision(axis,ticks));
  1158. }
  1159. int LabelAxisValue(const ImPlotAxis& axis, const ImPlotTickCollection& ticks, double value, char* buff, int size) {
  1160. ImPlotContext& gp = *GImPlot;
  1161. if (ImHasFlag(axis.Flags, ImPlotAxisFlags_Time)) {
  1162. ImPlotTimeUnit unit = (axis.Orientation == ImPlotOrientation_Horizontal)
  1163. ? GetUnitForRange(axis.Range.Size() / (gp.CurrentPlot->PlotRect.GetWidth() / 100))
  1164. : GetUnitForRange(axis.Range.Size() / (gp.CurrentPlot->PlotRect.GetHeight() / 100));
  1165. return FormatDateTime(ImPlotTime::FromDouble(value), buff, size, GetDateTimeFmt(TimeFormatMouseCursor, unit));
  1166. }
  1167. else {
  1168. double range = ticks.Size > 1 ? (ticks.Ticks[1].PlotPos - ticks.Ticks[0].PlotPos) : axis.Range.Size();
  1169. return snprintf(buff, size, "%.*f", Precision(range), value);
  1170. }
  1171. }
  1172. void UpdateAxisColors(int axis_flag, ImPlotAxis* axis) {
  1173. const ImVec4 col_label = GetStyleColorVec4(axis_flag);
  1174. const ImVec4 col_grid = GetStyleColorVec4(axis_flag + 1);
  1175. axis->ColorMaj = ImGui::GetColorU32(col_grid);
  1176. axis->ColorMin = ImGui::GetColorU32(col_grid*ImVec4(1,1,1,GImPlot->Style.MinorAlpha));
  1177. axis->ColorTxt = ImGui::GetColorU32(col_label);
  1178. }
  1179. //-----------------------------------------------------------------------------
  1180. // RENDERING
  1181. //-----------------------------------------------------------------------------
  1182. static inline void RenderGridLinesX(ImDrawList& DrawList, const ImPlotTickCollection& ticks, const ImRect& rect, ImU32 col_maj, ImU32 col_min, float size_maj, float size_min) {
  1183. const float density = ticks.Size / rect.GetWidth();
  1184. ImVec4 col_min4 = ImGui::ColorConvertU32ToFloat4(col_min);
  1185. col_min4.w *= ImClamp(ImRemap(density, 0.1f, 0.2f, 1.0f, 0.0f), 0.0f, 1.0f);
  1186. col_min = ImGui::ColorConvertFloat4ToU32(col_min4);
  1187. for (int t = 0; t < ticks.Size; t++) {
  1188. const ImPlotTick& xt = ticks.Ticks[t];
  1189. if (xt.Level == 0) {
  1190. if (xt.Major)
  1191. DrawList.AddLine(ImVec2(xt.PixelPos, rect.Min.y), ImVec2(xt.PixelPos, rect.Max.y), col_maj, size_maj);
  1192. else if (density < 0.2f)
  1193. DrawList.AddLine(ImVec2(xt.PixelPos, rect.Min.y), ImVec2(xt.PixelPos, rect.Max.y), col_min, size_min);
  1194. }
  1195. }
  1196. }
  1197. static inline void RenderGridLinesY(ImDrawList& DrawList, const ImPlotTickCollection& ticks, const ImRect& rect, ImU32 col_maj, ImU32 col_min, float size_maj, float size_min) {
  1198. const float density = ticks.Size / rect.GetHeight();
  1199. ImVec4 col_min4 = ImGui::ColorConvertU32ToFloat4(col_min);
  1200. col_min4.w *= ImClamp(ImRemap(density, 0.1f, 0.2f, 1.0f, 0.0f), 0.0f, 1.0f);
  1201. col_min = ImGui::ColorConvertFloat4ToU32(col_min4);
  1202. for (int t = 0; t < ticks.Size; t++) {
  1203. const ImPlotTick& yt = ticks.Ticks[t];
  1204. if (yt.Major)
  1205. DrawList.AddLine(ImVec2(rect.Min.x, yt.PixelPos), ImVec2(rect.Max.x, yt.PixelPos), col_maj, size_maj);
  1206. else if (density < 0.2f)
  1207. DrawList.AddLine(ImVec2(rect.Min.x, yt.PixelPos), ImVec2(rect.Max.x, yt.PixelPos), col_min, size_min);
  1208. }
  1209. }
  1210. static inline void RenderSelectionRect(ImDrawList& DrawList, const ImVec2& p_min, const ImVec2& p_max, const ImVec4& col) {
  1211. const ImU32 col_bg = ImGui::GetColorU32(col * ImVec4(1,1,1,0.25f));
  1212. const ImU32 col_bd = ImGui::GetColorU32(col);
  1213. DrawList.AddRectFilled(p_min, p_max, col_bg);
  1214. DrawList.AddRect(p_min, p_max, col_bd);
  1215. }
  1216. //-----------------------------------------------------------------------------
  1217. // Input Handling
  1218. //-----------------------------------------------------------------------------
  1219. void HandlePlotInput(ImPlotPlot& plot) {
  1220. ImGuiContext& G = *GImGui;
  1221. ImPlotContext& gp = *GImPlot;
  1222. ImGuiIO& IO = ImGui::GetIO();
  1223. const bool any_hov_y_axis_region = plot.YAxis[0].AllHovered || plot.YAxis[1].AllHovered || plot.YAxis[2].AllHovered;
  1224. bool hov_query = false;
  1225. if (plot.PlotHovered && plot.Queried && !plot.Querying) {
  1226. ImRect bb_query = plot.QueryRect;
  1227. bb_query.Min += plot.PlotRect.Min;
  1228. bb_query.Max += plot.PlotRect.Min;
  1229. hov_query = bb_query.Contains(IO.MousePos);
  1230. }
  1231. // QUERY DRAG -------------------------------------------------------------
  1232. if (plot.DraggingQuery && (IO.MouseReleased[gp.InputMap.PanButton] || !IO.MouseDown[gp.InputMap.PanButton])) {
  1233. plot.DraggingQuery = false;
  1234. }
  1235. if (plot.DraggingQuery) {
  1236. ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeAll);
  1237. plot.QueryRect.Min += IO.MouseDelta;
  1238. plot.QueryRect.Max += IO.MouseDelta;
  1239. }
  1240. if (plot.PlotHovered && hov_query && !plot.DraggingQuery && !plot.Selecting && !plot.Items.Legend.Hovered) {
  1241. ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeAll);
  1242. const bool any_y_dragging = plot.YAxis[0].Dragging || plot.YAxis[1].Dragging || plot.YAxis[2].Dragging;
  1243. if (IO.MouseDown[gp.InputMap.PanButton] && !plot.XAxis.Dragging && !any_y_dragging) {
  1244. plot.DraggingQuery = true;
  1245. }
  1246. }
  1247. // DRAG INPUT -------------------------------------------------------------
  1248. const bool axis_equal = ImHasFlag(plot.Flags, ImPlotFlags_Equal);
  1249. // end drags
  1250. if (plot.XAxis.Dragging && (IO.MouseReleased[gp.InputMap.PanButton] || !IO.MouseDown[gp.InputMap.PanButton])) {
  1251. plot.XAxis.Dragging = false;
  1252. G.IO.MouseDragMaxDistanceSqr[0] = 0;
  1253. }
  1254. for (int i = 0; i < IMPLOT_Y_AXES; i++) {
  1255. if (plot.YAxis[i].Dragging && (IO.MouseReleased[gp.InputMap.PanButton] || !IO.MouseDown[gp.InputMap.PanButton])) {
  1256. plot.YAxis[i].Dragging = false;
  1257. G.IO.MouseDragMaxDistanceSqr[0] = 0;
  1258. }
  1259. }
  1260. const bool any_y_dragging = plot.YAxis[0].Dragging || plot.YAxis[1].Dragging || plot.YAxis[2].Dragging;
  1261. bool drag_in_progress = plot.XAxis.Dragging || any_y_dragging;
  1262. // do drag
  1263. if (drag_in_progress) {
  1264. UpdateTransformCache();
  1265. bool equal_dragged = false;
  1266. // special case for axis equal and both x and y0 hovered
  1267. if (axis_equal && !plot.XAxis.IsInputLocked() && plot.XAxis.Dragging && !plot.YAxis[0].IsInputLocked() && plot.YAxis[0].Dragging) {
  1268. ImPlotPoint plot_tl = PixelsToPlot(plot.PlotRect.Min - IO.MouseDelta, 0);
  1269. ImPlotPoint plot_br = PixelsToPlot(plot.PlotRect.Max - IO.MouseDelta, 0);
  1270. plot.XAxis.SetMin(plot.XAxis.IsInverted() ? plot_br.x : plot_tl.x);
  1271. plot.XAxis.SetMax(plot.XAxis.IsInverted() ? plot_tl.x : plot_br.x);
  1272. plot.YAxis[0].SetMin(plot.YAxis[0].IsInverted() ? plot_tl.y : plot_br.y);
  1273. plot.YAxis[0].SetMax(plot.YAxis[0].IsInverted() ? plot_br.y : plot_tl.y);
  1274. double xar = plot.XAxis.GetAspect();
  1275. double yar = plot.YAxis[0].GetAspect();
  1276. if (!ImAlmostEqual(xar,yar) && !plot.YAxis[0].IsInputLocked())
  1277. plot.XAxis.SetAspect(yar);
  1278. equal_dragged = true;
  1279. }
  1280. if (!plot.XAxis.IsInputLocked() && plot.XAxis.Dragging && !equal_dragged) {
  1281. ImPlotPoint plot_tl = PixelsToPlot(plot.PlotRect.Min - IO.MouseDelta, 0);
  1282. ImPlotPoint plot_br = PixelsToPlot(plot.PlotRect.Max - IO.MouseDelta, 0);
  1283. plot.XAxis.SetMin(plot.XAxis.IsInverted() ? plot_br.x : plot_tl.x);
  1284. plot.XAxis.SetMax(plot.XAxis.IsInverted() ? plot_tl.x : plot_br.x);
  1285. if (axis_equal)
  1286. plot.YAxis[0].SetAspect(plot.XAxis.GetAspect());
  1287. }
  1288. for (int i = 0; i < IMPLOT_Y_AXES; i++) {
  1289. if (!plot.YAxis[i].IsInputLocked() && plot.YAxis[i].Dragging && !(i == 0 && equal_dragged)) {
  1290. ImPlotPoint plot_tl = PixelsToPlot(plot.PlotRect.Min - IO.MouseDelta, i);
  1291. ImPlotPoint plot_br = PixelsToPlot(plot.PlotRect.Max - IO.MouseDelta, i);
  1292. plot.YAxis[i].SetMin(plot.YAxis[i].IsInverted() ? plot_tl.y : plot_br.y);
  1293. plot.YAxis[i].SetMax(plot.YAxis[i].IsInverted() ? plot_br.y : plot_tl.y);
  1294. if (i == 0 && axis_equal)
  1295. plot.XAxis.SetAspect(plot.YAxis[0].GetAspect());
  1296. }
  1297. }
  1298. // Set the mouse cursor based on which axes are moving.
  1299. int direction = 0;
  1300. if (!plot.XAxis.IsInputLocked() && plot.XAxis.Dragging) {
  1301. direction |= (1 << 1);
  1302. }
  1303. for (int i = 0; i < IMPLOT_Y_AXES; i++) {
  1304. if (!plot.YAxis[i].Present) { continue; }
  1305. if (!plot.YAxis[i].IsInputLocked() && plot.YAxis[i].Dragging) {
  1306. direction |= (1 << 2);
  1307. break;
  1308. }
  1309. }
  1310. if (IO.MouseDragMaxDistanceSqr[0] > 5) {
  1311. if (direction == 0)
  1312. ImGui::SetMouseCursor(ImGuiMouseCursor_NotAllowed);
  1313. else if (direction == (1 << 1))
  1314. ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeEW);
  1315. else if (direction == (1 << 2))
  1316. ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeNS);
  1317. else
  1318. ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeAll);
  1319. }
  1320. }
  1321. // start drag
  1322. if (!drag_in_progress && plot.FrameHovered && IO.MouseClicked[gp.InputMap.PanButton] && ImHasFlag(IO.KeyMods, gp.InputMap.PanMod) && !plot.Selecting && !plot.Items.Legend.Hovered && !hov_query && !plot.DraggingQuery) {
  1323. if (plot.XAxis.AllHovered) {
  1324. plot.XAxis.Dragging = true;
  1325. }
  1326. for (int i = 0; i < IMPLOT_Y_AXES; i++) {
  1327. if (plot.YAxis[i].AllHovered) {
  1328. plot.YAxis[i].Dragging = true;
  1329. }
  1330. }
  1331. }
  1332. // SCROLL INPUT -----------------------------------------------------------
  1333. if (plot.FrameHovered && (plot.XAxis.AllHovered || any_hov_y_axis_region) && IO.MouseWheel != 0) {
  1334. UpdateTransformCache();
  1335. float zoom_rate = IMPLOT_ZOOM_RATE;
  1336. if (IO.MouseWheel > 0)
  1337. zoom_rate = (-zoom_rate) / (1.0f + (2.0f * zoom_rate));
  1338. float tx = ImRemap(IO.MousePos.x, plot.PlotRect.Min.x, plot.PlotRect.Max.x, 0.0f, 1.0f);
  1339. float ty = ImRemap(IO.MousePos.y, plot.PlotRect.Min.y, plot.PlotRect.Max.y, 0.0f, 1.0f);
  1340. bool equal_zoomed = false;
  1341. // special case for axis equal and both x and y0 hovered
  1342. if (axis_equal && plot.XAxis.AllHovered && !plot.XAxis.IsInputLocked() && plot.YAxis[0].AllHovered && !plot.YAxis[0].IsInputLocked()) {
  1343. const ImPlotPoint& plot_tl = PixelsToPlot(plot.PlotRect.Min - plot.PlotRect.GetSize() * ImVec2(tx * zoom_rate, ty * zoom_rate), 0);
  1344. const ImPlotPoint& plot_br = PixelsToPlot(plot.PlotRect.Max + plot.PlotRect.GetSize() * ImVec2((1 - tx) * zoom_rate, (1 - ty) * zoom_rate), 0);
  1345. plot.XAxis.SetMin(plot.XAxis.IsInverted() ? plot_br.x : plot_tl.x);
  1346. plot.XAxis.SetMax(plot.XAxis.IsInverted() ? plot_tl.x : plot_br.x);
  1347. plot.YAxis[0].SetMin(plot.YAxis[0].IsInverted() ? plot_tl.y : plot_br.y);
  1348. plot.YAxis[0].SetMax(plot.YAxis[0].IsInverted() ? plot_br.y : plot_tl.y);
  1349. double xar = plot.XAxis.GetAspect();
  1350. double yar = plot.YAxis[0].GetAspect();
  1351. if (!ImAlmostEqual(xar,yar) && !plot.YAxis[0].IsInputLocked())
  1352. plot.XAxis.SetAspect(yar);
  1353. equal_zoomed = true;
  1354. }
  1355. if (plot.XAxis.AllHovered && !plot.XAxis.IsInputLocked() && !equal_zoomed) {
  1356. const ImPlotPoint& plot_tl = PixelsToPlot(plot.PlotRect.Min - plot.PlotRect.GetSize() * ImVec2(tx * zoom_rate, ty * zoom_rate), 0);
  1357. const ImPlotPoint& plot_br = PixelsToPlot(plot.PlotRect.Max + plot.PlotRect.GetSize() * ImVec2((1 - tx) * zoom_rate, (1 - ty) * zoom_rate), 0);
  1358. plot.XAxis.SetMin(plot.XAxis.IsInverted() ? plot_br.x : plot_tl.x);
  1359. plot.XAxis.SetMax(plot.XAxis.IsInverted() ? plot_tl.x : plot_br.x);
  1360. if (axis_equal)
  1361. plot.YAxis[0].SetAspect(plot.XAxis.GetAspect());
  1362. }
  1363. for (int i = 0; i < IMPLOT_Y_AXES; i++) {
  1364. if (plot.YAxis[i].AllHovered && !plot.YAxis[i].IsInputLocked() && !(i == 0 && equal_zoomed)) {
  1365. const ImPlotPoint& plot_tl = PixelsToPlot(plot.PlotRect.Min - plot.PlotRect.GetSize() * ImVec2(tx * zoom_rate, ty * zoom_rate), i);
  1366. const ImPlotPoint& plot_br = PixelsToPlot(plot.PlotRect.Max + plot.PlotRect.GetSize() * ImVec2((1 - tx) * zoom_rate, (1 - ty) * zoom_rate), i);
  1367. plot.YAxis[i].SetMin(plot.YAxis[i].IsInverted() ? plot_tl.y : plot_br.y);
  1368. plot.YAxis[i].SetMax(plot.YAxis[i].IsInverted() ? plot_br.y : plot_tl.y);
  1369. if (i == 0 && axis_equal)
  1370. plot.XAxis.SetAspect(plot.YAxis[0].GetAspect());
  1371. }
  1372. }
  1373. }
  1374. // BOX-SELECTION AND QUERY ------------------------------------------------
  1375. // begin selection
  1376. if (!ImHasFlag(plot.Flags, ImPlotFlags_NoBoxSelect) && plot.PlotHovered && IO.MouseClicked[gp.InputMap.BoxSelectButton] && ImHasFlag(IO.KeyMods, gp.InputMap.BoxSelectMod)) {
  1377. plot.Selecting = true;
  1378. plot.SelectStart = IO.MousePos;
  1379. plot.SelectRect = ImRect(0,0,0,0);
  1380. }
  1381. // update selection
  1382. if (plot.Selecting) {
  1383. UpdateTransformCache();
  1384. const ImVec2 d = plot.SelectStart - IO.MousePos;
  1385. const bool x_can_change = !ImHasFlag(IO.KeyMods,gp.InputMap.HorizontalMod) && ImFabs(d.x) > 2;
  1386. const bool y_can_change = !ImHasFlag(IO.KeyMods,gp.InputMap.VerticalMod) && ImFabs(d.y) > 2;
  1387. // confirm
  1388. if (IO.MouseReleased[gp.InputMap.BoxSelectButton] || !IO.MouseDown[gp.InputMap.BoxSelectButton]) {
  1389. if (!plot.XAxis.IsInputLocked() && x_can_change) {
  1390. ImPlotPoint p1 = PixelsToPlot(plot.SelectStart);
  1391. ImPlotPoint p2 = PixelsToPlot(IO.MousePos);
  1392. plot.XAxis.SetMin(ImMin(p1.x, p2.x));
  1393. plot.XAxis.SetMax(ImMax(p1.x, p2.x));
  1394. }
  1395. for (int i = 0; i < IMPLOT_Y_AXES; i++) {
  1396. if (!plot.YAxis[i].IsInputLocked() && y_can_change) {
  1397. ImPlotPoint p1 = PixelsToPlot(plot.SelectStart, i);
  1398. ImPlotPoint p2 = PixelsToPlot(IO.MousePos, i);
  1399. plot.YAxis[i].SetMin(ImMin(p1.y, p2.y));
  1400. plot.YAxis[i].SetMax(ImMax(p1.y, p2.y));
  1401. }
  1402. }
  1403. if (x_can_change || y_can_change || (ImHasFlag(IO.KeyMods,gp.InputMap.HorizontalMod) && ImHasFlag(IO.KeyMods,gp.InputMap.VerticalMod)))
  1404. plot.ContextLocked = gp.InputMap.BoxSelectButton == gp.InputMap.ContextMenuButton;
  1405. plot.Selected = plot.Selecting = false;
  1406. }
  1407. // cancel
  1408. else if (IO.MouseClicked[gp.InputMap.BoxSelectCancelButton] || IO.MouseDown[gp.InputMap.BoxSelectCancelButton]) {
  1409. plot.Selected = plot.Selecting = false;
  1410. plot.ContextLocked = gp.InputMap.BoxSelectButton == gp.InputMap.ContextMenuButton;
  1411. }
  1412. else if (ImLengthSqr(d) > 4) {
  1413. // bad selection
  1414. if (plot.IsInputLocked()) {
  1415. ImGui::SetMouseCursor(ImGuiMouseCursor_NotAllowed);
  1416. plot.ContextLocked = gp.InputMap.BoxSelectButton == gp.InputMap.ContextMenuButton;
  1417. plot.Selected = false;
  1418. }
  1419. else {
  1420. // TODO: Handle only min or max locked cases
  1421. plot.SelectRect.Min.x = ImHasFlag(IO.KeyMods, gp.InputMap.HorizontalMod) || plot.XAxis.IsInputLocked() ? plot.PlotRect.Min.x : ImMin(plot.SelectStart.x, IO.MousePos.x);
  1422. plot.SelectRect.Max.x = ImHasFlag(IO.KeyMods, gp.InputMap.HorizontalMod) || plot.XAxis.IsInputLocked() ? plot.PlotRect.Max.x : ImMax(plot.SelectStart.x, IO.MousePos.x);
  1423. plot.SelectRect.Min.y = ImHasFlag(IO.KeyMods, gp.InputMap.VerticalMod) || plot.AllYInputLocked() ? plot.PlotRect.Min.y : ImMin(plot.SelectStart.y, IO.MousePos.y);
  1424. plot.SelectRect.Max.y = ImHasFlag(IO.KeyMods, gp.InputMap.VerticalMod) || plot.AllYInputLocked() ? plot.PlotRect.Max.y : ImMax(plot.SelectStart.y, IO.MousePos.y);
  1425. plot.SelectRect.Min -= plot.PlotRect.Min;
  1426. plot.SelectRect.Max -= plot.PlotRect.Min;
  1427. plot.Selected = true;
  1428. }
  1429. }
  1430. else {
  1431. plot.Selected = false;
  1432. }
  1433. }
  1434. // begin query
  1435. if (ImHasFlag(plot.Flags, ImPlotFlags_Query) && plot.PlotHovered && IO.MouseClicked[gp.InputMap.QueryButton] && ImHasFlag(IO.KeyMods, gp.InputMap.QueryMod)) {
  1436. plot.Querying = true;
  1437. plot.QueryStart = IO.MousePos;
  1438. plot.QueryRect = ImRect(0,0,0,0);
  1439. }
  1440. // update query
  1441. if (plot.Querying) {
  1442. UpdateTransformCache();
  1443. // confirm
  1444. if (IO.MouseReleased[gp.InputMap.QueryButton] || IO.MouseReleased[gp.InputMap.BoxSelectButton]) {
  1445. plot.Querying = false;
  1446. if (plot.QueryRect.GetWidth() > 2 && plot.QueryRect.GetHeight() > 2) {
  1447. plot.Queried = true;
  1448. plot.ContextLocked = gp.InputMap.BoxSelectButton == gp.InputMap.ContextMenuButton;
  1449. }
  1450. else
  1451. plot.Queried = false;
  1452. }
  1453. else {
  1454. plot.QueryRect.Min.x = ImHasFlag(IO.KeyMods, gp.InputMap.HorizontalMod) ? plot.PlotRect.Min.x : ImMin(plot.QueryStart.x, IO.MousePos.x);
  1455. plot.QueryRect.Max.x = ImHasFlag(IO.KeyMods, gp.InputMap.HorizontalMod) ? plot.PlotRect.Max.x : ImMax(plot.QueryStart.x, IO.MousePos.x);
  1456. plot.QueryRect.Min.y = ImHasFlag(IO.KeyMods, gp.InputMap.VerticalMod) ? plot.PlotRect.Min.y : ImMin(plot.QueryStart.y, IO.MousePos.y);
  1457. plot.QueryRect.Max.y = ImHasFlag(IO.KeyMods, gp.InputMap.VerticalMod) ? plot.PlotRect.Max.y : ImMax(plot.QueryStart.y, IO.MousePos.y);
  1458. plot.QueryRect.Min -= plot.PlotRect.Min;
  1459. plot.QueryRect.Max -= plot.PlotRect.Min;
  1460. plot.Queried = plot.QueryRect.GetWidth() > 2 && plot.QueryRect.GetHeight() > 2;
  1461. }
  1462. }
  1463. // switch select to query
  1464. if (ImHasFlag(plot.Flags, ImPlotFlags_Query) && plot.Selecting && ImHasFlag(IO.KeyMods,gp.InputMap.QueryToggleMod)) {
  1465. plot.Selecting = plot.Selected = false;
  1466. plot.Querying = plot.Queried = true;
  1467. plot.QueryStart = plot.SelectStart;
  1468. plot.QueryRect = plot.SelectRect;
  1469. }
  1470. // switch query to select
  1471. if (!ImHasFlag(plot.Flags, ImPlotFlags_NoBoxSelect) && plot.Querying && !ImHasFlag(IO.KeyMods, gp.InputMap.QueryToggleMod) && !IO.MouseDown[gp.InputMap.QueryButton]) {
  1472. plot.Selecting = plot.Selected = true;
  1473. plot.Querying = plot.Queried = false;
  1474. plot.SelectStart = plot.QueryStart;
  1475. plot.SelectRect = plot.QueryRect;
  1476. }
  1477. // FIT -----------------------------------------------------------
  1478. // fit from double click
  1479. if ( IO.MouseDoubleClicked[gp.InputMap.FitButton] && plot.FrameHovered && (plot.XAxis.AllHovered || any_hov_y_axis_region) && !plot.Items.Legend.Hovered && !hov_query ) {
  1480. gp.FitThisFrame = true;
  1481. gp.FitX = plot.XAxis.AllHovered;
  1482. for (int i = 0; i < IMPLOT_Y_AXES; i++)
  1483. gp.FitY[i] = plot.YAxis[i].AllHovered;
  1484. }
  1485. // fit from FitNextPlotAxes or auto fit
  1486. if (gp.NextPlotData.FitX || ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_AutoFit)) {
  1487. gp.FitThisFrame = true;
  1488. gp.FitX = true;
  1489. }
  1490. for (int i = 0; i < IMPLOT_Y_AXES; ++i) {
  1491. if (gp.NextPlotData.FitY[i] || ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_AutoFit)) {
  1492. gp.FitThisFrame = true;
  1493. gp.FitY[i] = true;
  1494. }
  1495. }
  1496. // FOCUS ------------------------------------------------------------------
  1497. // focus window
  1498. if ((IO.MouseClicked[0] || IO.MouseClicked[1] || IO.MouseClicked[2]) && plot.FrameHovered)
  1499. ImGui::FocusWindow(ImGui::GetCurrentWindow());
  1500. }
  1501. //-----------------------------------------------------------------------------
  1502. // Context Menu
  1503. //-----------------------------------------------------------------------------
  1504. template <typename F>
  1505. bool DragFloat(const char*, F*, float, F, F) {
  1506. return false;
  1507. }
  1508. template <>
  1509. bool DragFloat<double>(const char* label, double* v, float v_speed, double v_min, double v_max) {
  1510. return ImGui::DragScalar(label, ImGuiDataType_Double, v, v_speed, &v_min, &v_max, "%.3f", 1);
  1511. }
  1512. template <>
  1513. bool DragFloat<float>(const char* label, float* v, float v_speed, float v_min, float v_max) {
  1514. return ImGui::DragScalar(label, ImGuiDataType_Float, v, v_speed, &v_min, &v_max, "%.3f", 1);
  1515. }
  1516. inline void BeginDisabledControls(bool cond) {
  1517. if (cond) {
  1518. ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
  1519. ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * 0.25f);
  1520. }
  1521. }
  1522. inline void EndDisabledControls(bool cond) {
  1523. if (cond) {
  1524. ImGui::PopItemFlag();
  1525. ImGui::PopStyleVar();
  1526. }
  1527. }
  1528. void ShowAxisContextMenu(ImPlotAxis& axis, ImPlotAxis* equal_axis, bool time_allowed) {
  1529. ImGui::PushItemWidth(75);
  1530. bool always_locked = axis.IsRangeLocked() || axis.IsAutoFitting();
  1531. bool label = !ImHasFlag(axis.Flags, ImPlotAxisFlags_NoLabel);
  1532. bool grid = !ImHasFlag(axis.Flags, ImPlotAxisFlags_NoGridLines);
  1533. bool ticks = !ImHasFlag(axis.Flags, ImPlotAxisFlags_NoTickMarks);
  1534. bool labels = !ImHasFlag(axis.Flags, ImPlotAxisFlags_NoTickLabels);
  1535. double drag_speed = (axis.Range.Size() <= DBL_EPSILON) ? DBL_EPSILON * 1.0e+13 : 0.01 * axis.Range.Size(); // recover from almost equal axis limits.
  1536. if (axis.IsTime()) {
  1537. ImPlotTime tmin = ImPlotTime::FromDouble(axis.Range.Min);
  1538. ImPlotTime tmax = ImPlotTime::FromDouble(axis.Range.Max);
  1539. BeginDisabledControls(always_locked);
  1540. ImGui::CheckboxFlags("##LockMin", (unsigned int*)&axis.Flags, ImPlotAxisFlags_LockMin);
  1541. EndDisabledControls(always_locked);
  1542. ImGui::SameLine();
  1543. BeginDisabledControls(axis.IsLockedMin() || always_locked);
  1544. if (ImGui::BeginMenu("Min Time")) {
  1545. if (ShowTimePicker("mintime", &tmin)) {
  1546. if (tmin >= tmax)
  1547. tmax = AddTime(tmin, ImPlotTimeUnit_S, 1);
  1548. axis.SetRange(tmin.ToDouble(),tmax.ToDouble());
  1549. }
  1550. ImGui::Separator();
  1551. if (ShowDatePicker("mindate",&axis.PickerLevel,&axis.PickerTimeMin,&tmin,&tmax)) {
  1552. tmin = CombineDateTime(axis.PickerTimeMin, tmin);
  1553. if (tmin >= tmax)
  1554. tmax = AddTime(tmin, ImPlotTimeUnit_S, 1);
  1555. axis.SetRange(tmin.ToDouble(), tmax.ToDouble());
  1556. }
  1557. ImGui::EndMenu();
  1558. }
  1559. EndDisabledControls(axis.IsLockedMin() || always_locked);
  1560. BeginDisabledControls(always_locked);
  1561. ImGui::CheckboxFlags("##LockMax", (unsigned int*)&axis.Flags, ImPlotAxisFlags_LockMax);
  1562. EndDisabledControls(always_locked);
  1563. ImGui::SameLine();
  1564. BeginDisabledControls(axis.IsLockedMax() || always_locked);
  1565. if (ImGui::BeginMenu("Max Time")) {
  1566. if (ShowTimePicker("maxtime", &tmax)) {
  1567. if (tmax <= tmin)
  1568. tmin = AddTime(tmax, ImPlotTimeUnit_S, -1);
  1569. axis.SetRange(tmin.ToDouble(),tmax.ToDouble());
  1570. }
  1571. ImGui::Separator();
  1572. if (ShowDatePicker("maxdate",&axis.PickerLevel,&axis.PickerTimeMax,&tmin,&tmax)) {
  1573. tmax = CombineDateTime(axis.PickerTimeMax, tmax);
  1574. if (tmax <= tmin)
  1575. tmin = AddTime(tmax, ImPlotTimeUnit_S, -1);
  1576. axis.SetRange(tmin.ToDouble(), tmax.ToDouble());
  1577. }
  1578. ImGui::EndMenu();
  1579. }
  1580. EndDisabledControls(axis.IsLockedMax() || always_locked);
  1581. }
  1582. else {
  1583. BeginDisabledControls(always_locked);
  1584. ImGui::CheckboxFlags("##LockMin", (unsigned int*)&axis.Flags, ImPlotAxisFlags_LockMin);
  1585. EndDisabledControls(always_locked);
  1586. ImGui::SameLine();
  1587. BeginDisabledControls(axis.IsLockedMin() || always_locked);
  1588. double temp_min = axis.Range.Min;
  1589. if (DragFloat("Min", &temp_min, (float)drag_speed, -HUGE_VAL, axis.Range.Max - DBL_EPSILON)) {
  1590. axis.SetMin(temp_min,true);
  1591. if (equal_axis != NULL)
  1592. equal_axis->SetAspect(axis.GetAspect());
  1593. }
  1594. EndDisabledControls(axis.IsLockedMin() || always_locked);
  1595. BeginDisabledControls(always_locked);
  1596. ImGui::CheckboxFlags("##LockMax", (unsigned int*)&axis.Flags, ImPlotAxisFlags_LockMax);
  1597. EndDisabledControls(always_locked);
  1598. ImGui::SameLine();
  1599. BeginDisabledControls(axis.IsLockedMax() || always_locked);
  1600. double temp_max = axis.Range.Max;
  1601. if (DragFloat("Max", &temp_max, (float)drag_speed, axis.Range.Min + DBL_EPSILON, HUGE_VAL)) {
  1602. axis.SetMax(temp_max,true);
  1603. if (equal_axis != NULL)
  1604. equal_axis->SetAspect(axis.GetAspect());
  1605. }
  1606. EndDisabledControls(axis.IsLockedMax() || always_locked);
  1607. }
  1608. ImGui::Separator();
  1609. ImGui::CheckboxFlags("Auto-Fit",(unsigned int*)&axis.Flags, ImPlotAxisFlags_AutoFit);
  1610. ImGui::CheckboxFlags("Invert",(unsigned int*)&axis.Flags, ImPlotAxisFlags_Invert);
  1611. BeginDisabledControls(axis.IsTime() && time_allowed);
  1612. ImGui::CheckboxFlags("Log Scale",(unsigned int*)&axis.Flags, ImPlotAxisFlags_LogScale);
  1613. EndDisabledControls(axis.IsTime() && time_allowed);
  1614. if (time_allowed) {
  1615. BeginDisabledControls(axis.IsLog());
  1616. ImGui::CheckboxFlags("Time",(unsigned int*)&axis.Flags, ImPlotAxisFlags_Time);
  1617. EndDisabledControls(axis.IsLog());
  1618. }
  1619. ImGui::Separator();
  1620. if (ImGui::Checkbox("Label", &label))
  1621. ImFlipFlag(axis.Flags, ImPlotAxisFlags_NoLabel);
  1622. if (ImGui::Checkbox("Grid Lines", &grid))
  1623. ImFlipFlag(axis.Flags, ImPlotAxisFlags_NoGridLines);
  1624. if (ImGui::Checkbox("Tick Marks", &ticks))
  1625. ImFlipFlag(axis.Flags, ImPlotAxisFlags_NoTickMarks);
  1626. if (ImGui::Checkbox("Tick Labels", &labels))
  1627. ImFlipFlag(axis.Flags, ImPlotAxisFlags_NoTickLabels);
  1628. }
  1629. bool ShowLegendContextMenu(ImPlotLegendData& legend, bool visible) {
  1630. const float s = ImGui::GetFrameHeight();
  1631. bool ret = false;
  1632. if (ImGui::Checkbox("Show",&visible))
  1633. ret = true;
  1634. if (legend.CanGoInside)
  1635. ImGui::Checkbox("Outside", &legend.Outside);
  1636. if (ImGui::RadioButton("H", legend.Orientation == ImPlotOrientation_Horizontal))
  1637. legend.Orientation = ImPlotOrientation_Horizontal;
  1638. ImGui::SameLine();
  1639. if (ImGui::RadioButton("V", legend.Orientation == ImPlotOrientation_Vertical))
  1640. legend.Orientation = ImPlotOrientation_Vertical;
  1641. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(2,2));
  1642. if (ImGui::Button("NW",ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_NorthWest; } ImGui::SameLine();
  1643. if (ImGui::Button("N", ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_North; } ImGui::SameLine();
  1644. if (ImGui::Button("NE",ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_NorthEast; }
  1645. if (ImGui::Button("W", ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_West; } ImGui::SameLine();
  1646. if (ImGui::InvisibleButton("C", ImVec2(1.5f*s,s))) { } ImGui::SameLine();
  1647. if (ImGui::Button("E", ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_East; }
  1648. if (ImGui::Button("SW",ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_SouthWest; } ImGui::SameLine();
  1649. if (ImGui::Button("S", ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_South; } ImGui::SameLine();
  1650. if (ImGui::Button("SE",ImVec2(1.5f*s,s))) { legend.Location = ImPlotLocation_SouthEast; }
  1651. ImGui::PopStyleVar();
  1652. return ret;
  1653. }
  1654. void ShowSubplotsContextMenu(ImPlotSubplot& subplot) {
  1655. if ((ImGui::BeginMenu("Linking"))) {
  1656. if (ImGui::MenuItem("Link Rows",NULL,ImHasFlag(subplot.Flags, ImPlotSubplotFlags_LinkRows)))
  1657. ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_LinkRows);
  1658. if (ImGui::MenuItem("Link Cols",NULL,ImHasFlag(subplot.Flags, ImPlotSubplotFlags_LinkCols)))
  1659. ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_LinkCols);
  1660. if (ImGui::MenuItem("Link All X",NULL,ImHasFlag(subplot.Flags, ImPlotSubplotFlags_LinkAllX)))
  1661. ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_LinkAllX);
  1662. if (ImGui::MenuItem("Link All Y",NULL,ImHasFlag(subplot.Flags, ImPlotSubplotFlags_LinkAllY)))
  1663. ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_LinkAllY);
  1664. ImGui::EndMenu();
  1665. }
  1666. // if (ImHasFlag(subplot.Flags, ImPlotSubplotFlags_ShareItems)) {
  1667. // if ((ImGui::BeginMenu("Legend"))) {
  1668. // if (ShowLegendContextMenu(subplot.Items.Legend, !ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoLegend)))
  1669. // ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_NoLegend);
  1670. // ImGui::EndMenu();
  1671. // }
  1672. // }
  1673. if ((ImGui::BeginMenu("Settings"))) {
  1674. if (ImGui::MenuItem("Title",NULL,!ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoTitle)))
  1675. ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_NoTitle);
  1676. if (ImGui::MenuItem("Resizable",NULL,!ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoResize)))
  1677. ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_NoResize);
  1678. if (ImGui::MenuItem("Align",NULL,!ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoAlign)))
  1679. ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_NoAlign);
  1680. if (ImGui::MenuItem("Share Items",NULL,ImHasFlag(subplot.Flags, ImPlotSubplotFlags_ShareItems)))
  1681. ImFlipFlag(subplot.Flags, ImPlotSubplotFlags_ShareItems);
  1682. ImGui::EndMenu();
  1683. }
  1684. }
  1685. void ShowPlotContextMenu(ImPlotPlot& plot) {
  1686. const bool owns_legend = GImPlot->CurrentItems == &plot.Items;
  1687. const bool equal = ImHasFlag(plot.Flags, ImPlotFlags_Equal);
  1688. if (ImGui::BeginMenu("X-Axis")) {
  1689. ImGui::PushID("X");
  1690. ShowAxisContextMenu(plot.XAxis, equal ? &plot.YAxis[0] : NULL, true);
  1691. ImGui::PopID();
  1692. ImGui::EndMenu();
  1693. }
  1694. for (int i = 0; i < IMPLOT_Y_AXES; i++) {
  1695. if (i == 1 && !ImHasFlag(plot.Flags, ImPlotFlags_YAxis2)) {
  1696. continue;
  1697. }
  1698. if (i == 2 && !ImHasFlag(plot.Flags, ImPlotFlags_YAxis3)) {
  1699. continue;
  1700. }
  1701. char buf[10] = {};
  1702. if (i == 0) {
  1703. snprintf(buf, sizeof(buf) - 1, "Y-Axis");
  1704. } else {
  1705. snprintf(buf, sizeof(buf) - 1, "Y-Axis %d", i + 1);
  1706. }
  1707. if (ImGui::BeginMenu(buf)) {
  1708. ImGui::PushID(i);
  1709. ShowAxisContextMenu(plot.YAxis[i], (equal && i == 0) ? &plot.XAxis : NULL, false);
  1710. ImGui::PopID();
  1711. ImGui::EndMenu();
  1712. }
  1713. }
  1714. ImGui::Separator();
  1715. if ((ImGui::BeginMenu("Legend"))) {
  1716. if (owns_legend) {
  1717. if (ShowLegendContextMenu(plot.Items.Legend, !ImHasFlag(plot.Flags, ImPlotFlags_NoLegend)))
  1718. ImFlipFlag(plot.Flags, ImPlotFlags_NoLegend);
  1719. }
  1720. else if (GImPlot->CurrentSubplot != NULL) {
  1721. if (ShowLegendContextMenu(GImPlot->CurrentSubplot->Items.Legend, !ImHasFlag(GImPlot->CurrentSubplot->Flags, ImPlotSubplotFlags_NoLegend)))
  1722. ImFlipFlag(GImPlot->CurrentSubplot->Flags, ImPlotSubplotFlags_NoLegend);
  1723. }
  1724. ImGui::EndMenu();
  1725. }
  1726. if ((ImGui::BeginMenu("Settings"))) {
  1727. if (ImGui::MenuItem("Anti-Aliased Lines",NULL,ImHasFlag(plot.Flags, ImPlotFlags_AntiAliased)))
  1728. ImFlipFlag(plot.Flags, ImPlotFlags_AntiAliased);
  1729. if (ImGui::MenuItem("Equal", NULL, ImHasFlag(plot.Flags, ImPlotFlags_Equal)))
  1730. ImFlipFlag(plot.Flags, ImPlotFlags_Equal);
  1731. if (ImGui::MenuItem("Box Select",NULL,!ImHasFlag(plot.Flags, ImPlotFlags_NoBoxSelect)))
  1732. ImFlipFlag(plot.Flags, ImPlotFlags_NoBoxSelect);
  1733. if (ImGui::MenuItem("Query",NULL,ImHasFlag(plot.Flags, ImPlotFlags_Query)))
  1734. ImFlipFlag(plot.Flags, ImPlotFlags_Query);
  1735. if (ImGui::MenuItem("Title",NULL,!ImHasFlag(plot.Flags, ImPlotFlags_NoTitle)))
  1736. ImFlipFlag(plot.Flags, ImPlotFlags_NoTitle);
  1737. if (ImGui::MenuItem("Mouse Position",NULL,!ImHasFlag(plot.Flags, ImPlotFlags_NoMousePos)))
  1738. ImFlipFlag(plot.Flags, ImPlotFlags_NoMousePos);
  1739. if (ImGui::MenuItem("Crosshairs",NULL,ImHasFlag(plot.Flags, ImPlotFlags_Crosshairs)))
  1740. ImFlipFlag(plot.Flags, ImPlotFlags_Crosshairs);
  1741. ImGui::EndMenu();
  1742. }
  1743. if (GImPlot->CurrentSubplot != NULL && !ImHasFlag(GImPlot->CurrentPlot->Flags, ImPlotSubplotFlags_NoMenus)) {
  1744. ImGui::Separator();
  1745. if ((ImGui::BeginMenu("Subplots"))) {
  1746. ShowSubplotsContextMenu(*GImPlot->CurrentSubplot);
  1747. ImGui::EndMenu();
  1748. }
  1749. }
  1750. }
  1751. //-----------------------------------------------------------------------------
  1752. // BeginPlot()
  1753. //-----------------------------------------------------------------------------
  1754. bool BeginPlot(const char* title, const char* x_label, const char* y1_label, const ImVec2& size,
  1755. ImPlotFlags flags, ImPlotAxisFlags x_flags, ImPlotAxisFlags y1_flags, ImPlotAxisFlags y2_flags, ImPlotAxisFlags y3_flags,
  1756. const char* y2_label, const char* y3_label)
  1757. {
  1758. IM_ASSERT_USER_ERROR(GImPlot != NULL, "No current context. Did you call ImPlot::CreateContext() or ImPlot::SetCurrentContext()?");
  1759. ImPlotContext& gp = *GImPlot;
  1760. IM_ASSERT_USER_ERROR(gp.CurrentPlot == NULL, "Mismatched BeginPlot()/EndPlot()!");
  1761. IM_ASSERT_USER_ERROR(!(ImHasFlag(x_flags, ImPlotAxisFlags_Time) && ImHasFlag(x_flags, ImPlotAxisFlags_LogScale)), "ImPlotAxisFlags_Time and ImPlotAxisFlags_LogScale cannot be enabled at the same time!");
  1762. IM_ASSERT_USER_ERROR(!ImHasFlag(y1_flags, ImPlotAxisFlags_Time), "Y axes cannot display time formatted labels!");
  1763. // SUBPLOT ID --------------------------------------------------------------
  1764. if (gp.CurrentSubplot != NULL)
  1765. ImGui::PushID(gp.CurrentSubplot->CurrentIdx);
  1766. // FRONT MATTER -----------------------------------------------------------
  1767. ImGuiContext &G = *GImGui;
  1768. ImGuiWindow * Window = G.CurrentWindow;
  1769. if (Window->SkipItems && !gp.CurrentSubplot) {
  1770. ResetCtxForNextPlot(GImPlot);
  1771. return false;
  1772. }
  1773. const ImGuiID ID = Window->GetID(title);
  1774. const ImGuiStyle &Style = G.Style;
  1775. const ImGuiIO & IO = ImGui::GetIO();
  1776. bool just_created = gp.Plots.GetByKey(ID) == NULL;
  1777. gp.CurrentPlot = gp.Plots.GetOrAddByKey(ID);
  1778. gp.CurrentPlot->ID = ID;
  1779. gp.CurrentPlot->Items.ID = ID;
  1780. ImPlotPlot &plot = *gp.CurrentPlot;
  1781. plot.CurrentYAxis = 0;
  1782. if (just_created) {
  1783. plot.Flags = flags;
  1784. plot.XAxis.Flags = x_flags;
  1785. plot.YAxis[0].Flags = y1_flags;
  1786. plot.YAxis[1].Flags = y2_flags;
  1787. plot.YAxis[2].Flags = y3_flags;
  1788. }
  1789. else {
  1790. // TODO: Check which individual flags changed, and only reset those!
  1791. // There's probably an easy bit mask trick I'm not aware of.
  1792. if (flags != plot.PreviousFlags)
  1793. plot.Flags = flags;
  1794. if (x_flags != plot.XAxis.PreviousFlags)
  1795. plot.XAxis.Flags = x_flags;
  1796. if (y1_flags != plot.YAxis[0].PreviousFlags)
  1797. plot.YAxis[0].Flags = y1_flags;
  1798. if (y2_flags != plot.YAxis[1].PreviousFlags)
  1799. plot.YAxis[1].Flags = y2_flags;
  1800. if (y3_flags != plot.YAxis[2].PreviousFlags)
  1801. plot.YAxis[2].Flags = y3_flags;
  1802. }
  1803. plot.PreviousFlags = flags;
  1804. plot.XAxis.PreviousFlags = x_flags;
  1805. plot.YAxis[0].PreviousFlags = y1_flags;
  1806. plot.YAxis[1].PreviousFlags = y2_flags;
  1807. plot.YAxis[2].PreviousFlags = y3_flags;
  1808. // capture scroll with a child region
  1809. if (!ImHasFlag(plot.Flags, ImPlotFlags_NoChild)) {
  1810. ImVec2 child_size;
  1811. if (gp.CurrentSubplot != NULL)
  1812. child_size = gp.CurrentSubplot->CellSize;
  1813. else
  1814. child_size = ImVec2(size.x == 0 ? gp.Style.PlotDefaultSize.x : size.x, size.y == 0 ? gp.Style.PlotDefaultSize.y : size.y);
  1815. ImGui::BeginChild(title, child_size, false, ImGuiWindowFlags_NoScrollbar);
  1816. Window = ImGui::GetCurrentWindow();
  1817. Window->ScrollMax.y = 1.0f;
  1818. gp.ChildWindowMade = true;
  1819. }
  1820. else {
  1821. gp.ChildWindowMade = false;
  1822. }
  1823. ImDrawList &DrawList = *Window->DrawList;
  1824. // NextPlotData -----------------------------------------------------------
  1825. // linked axes
  1826. plot.XAxis.LinkedMin = gp.NextPlotData.LinkedXmin;
  1827. plot.XAxis.LinkedMax = gp.NextPlotData.LinkedXmax;
  1828. PullLinkedAxis(plot.XAxis);
  1829. for (int i = 0; i < IMPLOT_Y_AXES; ++i) {
  1830. plot.YAxis[i].LinkedMin = gp.NextPlotData.LinkedYmin[i];
  1831. plot.YAxis[i].LinkedMax = gp.NextPlotData.LinkedYmax[i];
  1832. PullLinkedAxis(plot.YAxis[i]);
  1833. }
  1834. if (gp.NextPlotData.HasXRange) {
  1835. if (!plot.Initialized || gp.NextPlotData.XRangeCond == ImGuiCond_Always)
  1836. plot.XAxis.SetRange(gp.NextPlotData.XRange);
  1837. }
  1838. for (int i = 0; i < IMPLOT_Y_AXES; i++) {
  1839. if (gp.NextPlotData.HasYRange[i]) {
  1840. if (!plot.Initialized || gp.NextPlotData.YRangeCond[i] == ImGuiCond_Always)
  1841. plot.YAxis[i].SetRange(gp.NextPlotData.YRange[i]);
  1842. }
  1843. }
  1844. // Initialization ------------------------------------------------------------
  1845. if (!plot.Initialized) {
  1846. if (!ImHasFlag(plot.XAxis.Flags,ImPlotAxisFlags_NoInitialFit) && !gp.NextPlotData.HasXRange && !gp.NextPlotData.LinkedXmin && !gp.NextPlotData.LinkedXmax)
  1847. gp.FitThisFrame = gp.FitX = true;
  1848. for (int i = 0; i < IMPLOT_Y_AXES; ++i) {
  1849. if (!ImHasFlag(plot.YAxis[i].Flags,ImPlotAxisFlags_NoInitialFit) && !gp.NextPlotData.HasYRange[i] && !gp.NextPlotData.LinkedYmin[i] && !gp.NextPlotData.LinkedYmax[i])
  1850. gp.FitThisFrame = gp.FitY[i] = true;
  1851. }
  1852. }
  1853. // AXIS STATES ------------------------------------------------------------
  1854. plot.XAxis.HasRange = gp.NextPlotData.HasXRange; plot.XAxis.RangeCond = gp.NextPlotData.XRangeCond; plot.XAxis.Present = true;
  1855. plot.YAxis[0].HasRange = gp.NextPlotData.HasYRange[0]; plot.YAxis[0].RangeCond = gp.NextPlotData.YRangeCond[0]; plot.YAxis[0].Present = true;
  1856. plot.YAxis[1].HasRange = gp.NextPlotData.HasYRange[1]; plot.YAxis[1].RangeCond = gp.NextPlotData.YRangeCond[1]; plot.YAxis[1].Present = ImHasFlag(plot.Flags, ImPlotFlags_YAxis2);
  1857. plot.YAxis[2].HasRange = gp.NextPlotData.HasYRange[2]; plot.YAxis[2].RangeCond = gp.NextPlotData.YRangeCond[2]; plot.YAxis[2].Present = ImHasFlag(plot.Flags, ImPlotFlags_YAxis3);
  1858. for (int i = 0; i < IMPLOT_Y_AXES; ++i) {
  1859. if (!ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_LogScale) && !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_LogScale))
  1860. gp.Scales[i] = ImPlotScale_LinLin;
  1861. else if (ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_LogScale) && !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_LogScale))
  1862. gp.Scales[i] = ImPlotScale_LogLin;
  1863. else if (!ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_LogScale) && ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_LogScale))
  1864. gp.Scales[i] = ImPlotScale_LinLog;
  1865. else if (ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_LogScale) && ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_LogScale))
  1866. gp.Scales[i] = ImPlotScale_LogLog;
  1867. }
  1868. // constraints
  1869. plot.XAxis.Constrain();
  1870. for (int i = 0; i < IMPLOT_Y_AXES; ++i)
  1871. plot.YAxis[i].Constrain();
  1872. // AXIS COLORS -----------------------------------------------------------------
  1873. UpdateAxisColors(ImPlotCol_XAxis, &plot.XAxis);
  1874. UpdateAxisColors(ImPlotCol_YAxis, &plot.YAxis[0]);
  1875. UpdateAxisColors(ImPlotCol_YAxis2, &plot.YAxis[1]);
  1876. UpdateAxisColors(ImPlotCol_YAxis3, &plot.YAxis[2]);
  1877. // SIZING, BB, PADDING, HOVER -----------------------------------------------------------
  1878. // frame size
  1879. ImVec2 frame_size;
  1880. if (gp.CurrentSubplot != NULL)
  1881. frame_size = gp.CurrentSubplot->CellSize;
  1882. else
  1883. frame_size = ImGui::CalcItemSize(size, gp.Style.PlotDefaultSize.x, gp.Style.PlotDefaultSize.y);
  1884. if (frame_size.x < gp.Style.PlotMinSize.x && (size.x < 0.0f || gp.CurrentSubplot != NULL))
  1885. frame_size.x = gp.Style.PlotMinSize.x;
  1886. if (frame_size.y < gp.Style.PlotMinSize.y && (size.y < 0.0f || gp.CurrentSubplot != NULL))
  1887. frame_size.y = gp.Style.PlotMinSize.y;
  1888. plot.FrameRect = ImRect(Window->DC.CursorPos, Window->DC.CursorPos + frame_size);
  1889. ImGui::ItemSize(plot.FrameRect);
  1890. if (!ImGui::ItemAdd(plot.FrameRect, ID, &plot.FrameRect) && !gp.CurrentSubplot) {
  1891. ResetCtxForNextPlot(GImPlot);
  1892. return false;
  1893. }
  1894. // NB: ImGuiButtonFlags_AllowItemOverlap and SetItemAllowOverlap() required for DragLine and DragPoint
  1895. ImGui::ButtonBehavior(plot.FrameRect,plot.ID,&plot.FrameHovered,&plot.FrameHeld,ImGuiButtonFlags_AllowItemOverlap);
  1896. ImGui::SetItemAllowOverlap();
  1897. // canvas/axes bb
  1898. plot.CanvasRect = ImRect(plot.FrameRect.Min + gp.Style.PlotPadding, plot.FrameRect.Max - gp.Style.PlotPadding);
  1899. plot.AxesRect = plot.FrameRect;
  1900. // outside legend adjustments
  1901. if (!ImHasFlag(plot.Flags, ImPlotFlags_NoLegend) && plot.Items.GetLegendCount() > 0 && plot.Items.Legend.Outside) {
  1902. const ImVec2 legend_size = CalcLegendSize(plot.Items, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, plot.Items.Legend.Orientation);
  1903. const bool west = ImHasFlag(plot.Items.Legend.Location, ImPlotLocation_West) && !ImHasFlag(plot.Items.Legend.Location, ImPlotLocation_East);
  1904. const bool east = ImHasFlag(plot.Items.Legend.Location, ImPlotLocation_East) && !ImHasFlag(plot.Items.Legend.Location, ImPlotLocation_West);
  1905. const bool north = ImHasFlag(plot.Items.Legend.Location, ImPlotLocation_North) && !ImHasFlag(plot.Items.Legend.Location, ImPlotLocation_South);
  1906. const bool south = ImHasFlag(plot.Items.Legend.Location, ImPlotLocation_South) && !ImHasFlag(plot.Items.Legend.Location, ImPlotLocation_North);
  1907. const bool horz = plot.Items.Legend.Orientation == ImPlotOrientation_Horizontal;
  1908. if ((west && !horz) || (west && horz && !north && !south)) {
  1909. plot.CanvasRect.Min.x += (legend_size.x + gp.Style.LegendPadding.x);
  1910. plot.AxesRect.Min.x += (legend_size.x + gp.Style.PlotPadding.x);
  1911. }
  1912. if ((east && !horz) || (east && horz && !north && !south)) {
  1913. plot.CanvasRect.Max.x -= (legend_size.x + gp.Style.LegendPadding.x);
  1914. plot.AxesRect.Max.x -= (legend_size.x + gp.Style.PlotPadding.x);
  1915. }
  1916. if ((north && horz) || (north && !horz && !west && !east)) {
  1917. plot.CanvasRect.Min.y += (legend_size.y + gp.Style.LegendPadding.y);
  1918. plot.AxesRect.Min.y += (legend_size.y + gp.Style.PlotPadding.y);
  1919. }
  1920. if ((south && horz) || (south && !horz && !west && !east)) {
  1921. plot.CanvasRect.Max.y -= (legend_size.y + gp.Style.LegendPadding.y);
  1922. plot.AxesRect.Max.y -= (legend_size.y + gp.Style.PlotPadding.y);
  1923. }
  1924. }
  1925. gp.RenderX = (!ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_NoGridLines) ||
  1926. !ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_NoTickMarks) ||
  1927. !ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_NoTickLabels));
  1928. for (int i = 0; i < IMPLOT_Y_AXES; i++) {
  1929. gp.RenderY[i] = plot.YAxis[i].Present &&
  1930. (!ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_NoGridLines) ||
  1931. !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_NoTickMarks) ||
  1932. !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_NoTickLabels));
  1933. }
  1934. // plot bb
  1935. // (1) calc top/bot padding and plot height
  1936. ImVec2 title_size(0.0f, 0.0f);
  1937. const float txt_height = ImGui::GetTextLineHeight();
  1938. if (!ImHasFlag(plot.Flags, ImPlotFlags_NoTitle)){
  1939. title_size = ImGui::CalcTextSize(title, NULL, true);
  1940. }
  1941. const bool show_x_label = x_label && !ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_NoLabel);
  1942. float pad_top = title_size.x > 0.0f ? title_size.y + gp.Style.LabelPadding.y : 0;
  1943. float pad_bot = (plot.XAxis.IsLabeled() ? ImMax(txt_height, gp.XTicks.MaxHeight) + gp.Style.LabelPadding.y + (plot.XAxis.IsTime() ? txt_height + gp.Style.LabelPadding.y : 0) : 0)
  1944. + (show_x_label ? txt_height + gp.Style.LabelPadding.y : 0);
  1945. // (1*) align plots group
  1946. if (gp.CurrentAlignmentH)
  1947. gp.CurrentAlignmentH->Update(pad_top,pad_bot);
  1948. const float plot_height = plot.CanvasRect.GetHeight() - pad_top - pad_bot;
  1949. // (2) get y tick labels (needed for left/right pad)
  1950. for (int i = 0; i < IMPLOT_Y_AXES; i++) {
  1951. if (gp.RenderY[i] && gp.NextPlotData.ShowDefaultTicksY[i]) {
  1952. if (ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_LogScale))
  1953. AddTicksLogarithmic(plot.YAxis[i].Range, plot_height, ImPlotOrientation_Vertical, gp.YTicks[i], GetFormatY(i));
  1954. else
  1955. AddTicksDefault(plot.YAxis[i].Range, plot_height, ImPlotOrientation_Vertical, gp.YTicks[i], GetFormatY(i));
  1956. }
  1957. }
  1958. // (3) calc left/right pad
  1959. const bool show_y1_label = y1_label && !ImHasFlag(plot.YAxis[0].Flags, ImPlotAxisFlags_NoLabel);
  1960. const bool show_y2_label = y2_label && !ImHasFlag(plot.YAxis[1].Flags, ImPlotAxisFlags_NoLabel);
  1961. const bool show_y3_label = y3_label && !ImHasFlag(plot.YAxis[2].Flags, ImPlotAxisFlags_NoLabel);
  1962. float pad_left = (show_y1_label ? txt_height + gp.Style.LabelPadding.x : 0)
  1963. + (plot.YAxis[0].IsLabeled() ? gp.YTicks[0].MaxWidth + gp.Style.LabelPadding.x : 0);
  1964. float pad_right = ((plot.YAxis[1].Present && plot.YAxis[1].IsLabeled()) ? gp.YTicks[1].MaxWidth + gp.Style.LabelPadding.x : 0)
  1965. + ((plot.YAxis[1].Present && show_y2_label) ? txt_height + gp.Style.LabelPadding.x : 0)
  1966. + ((plot.YAxis[1].Present && plot.YAxis[2].Present) ? gp.Style.LabelPadding.x + gp.Style.MinorTickLen.y : 0)
  1967. + ((plot.YAxis[2].Present && plot.YAxis[2].IsLabeled()) ? gp.YTicks[2].MaxWidth + gp.Style.LabelPadding.x : 0)
  1968. + ((plot.YAxis[2].Present && show_y3_label) ? txt_height + gp.Style.LabelPadding.x : 0);
  1969. // (3*) align plots group
  1970. if (gp.CurrentAlignmentV)
  1971. gp.CurrentAlignmentV->Update(pad_left,pad_right);
  1972. const float plot_width = plot.CanvasRect.GetWidth() - pad_left - pad_right;
  1973. // (4) get x ticks
  1974. if (gp.RenderX && gp.NextPlotData.ShowDefaultTicksX) {
  1975. if (plot.XAxis.IsTime())
  1976. AddTicksTime(plot.XAxis.Range, plot_width, gp.XTicks);
  1977. else if (ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_LogScale))
  1978. AddTicksLogarithmic(plot.XAxis.Range, plot_width, ImPlotOrientation_Horizontal, gp.XTicks, GetFormatX());
  1979. else
  1980. AddTicksDefault(plot.XAxis.Range, plot_width, ImPlotOrientation_Horizontal, gp.XTicks, GetFormatX());
  1981. }
  1982. // (5) calc plot bb
  1983. plot.PlotRect = ImRect(plot.CanvasRect.Min + ImVec2(pad_left, pad_top), plot.CanvasRect.Max - ImVec2(pad_right, pad_bot));
  1984. plot.PlotHovered = plot.FrameHovered && plot.PlotRect.Contains(IO.MousePos);
  1985. // x axis region bb and hover
  1986. plot.XAxis.HoverRect = ImRect(plot.PlotRect.GetBL(), ImVec2(plot.PlotRect.Max.x, plot.AxesRect.Max.y));
  1987. plot.XAxis.ExtHovered = plot.XAxis.HoverRect.Contains(IO.MousePos);
  1988. plot.XAxis.AllHovered = plot.XAxis.ExtHovered || plot.PlotHovered;
  1989. // axis label reference
  1990. gp.YAxisReference[0] = plot.PlotRect.Min.x;
  1991. gp.YAxisReference[1] = plot.PlotRect.Max.x;
  1992. gp.YAxisReference[2] = !plot.YAxis[1].Present ? plot.PlotRect.Max.x : gp.YAxisReference[1]
  1993. + (plot.YAxis[1].IsLabeled() ? gp.Style.LabelPadding.x + gp.YTicks[1].MaxWidth : 0)
  1994. + (show_y2_label ? txt_height + gp.Style.LabelPadding.x : 0)
  1995. + gp.Style.LabelPadding.x + gp.Style.MinorTickLen.y;
  1996. // y axis regions bb and hover
  1997. plot.YAxis[0].HoverRect = ImRect(ImVec2(plot.AxesRect.Min.x, plot.PlotRect.Min.y), ImVec2(plot.PlotRect.Min.x, plot.PlotRect.Max.y));
  1998. plot.YAxis[1].HoverRect = plot.YAxis[2].Present
  1999. ? ImRect(plot.PlotRect.GetTR(), ImVec2(gp.YAxisReference[2], plot.PlotRect.Max.y))
  2000. : ImRect(plot.PlotRect.GetTR(), ImVec2(plot.AxesRect.Max.x, plot.PlotRect.Max.y));
  2001. plot.YAxis[2].HoverRect = ImRect(ImVec2(gp.YAxisReference[2], plot.PlotRect.Min.y), ImVec2(plot.AxesRect.Max.x, plot.PlotRect.Max.y));
  2002. for (int i = 0; i < IMPLOT_Y_AXES; ++i) {
  2003. plot.YAxis[i].ExtHovered = plot.YAxis[i].Present && plot.YAxis[i].HoverRect.Contains(IO.MousePos);
  2004. plot.YAxis[i].AllHovered = plot.YAxis[i].ExtHovered || plot.PlotHovered;
  2005. }
  2006. // AXIS ASPECT RATIOS
  2007. plot.XAxis.Pixels = plot.PlotRect.GetWidth();
  2008. for (int i = 0; i < IMPLOT_Y_AXES; ++i)
  2009. plot.YAxis[i].Pixels = plot.PlotRect.GetHeight();
  2010. // Equal axis constraint. Must happen after we set Pixels
  2011. // constrain equal axes for primary x and y if not approximately equal
  2012. // constrains x to y since x pixel size depends on y labels width, and causes feedback loops in opposite case
  2013. if (ImHasFlag(plot.Flags, ImPlotFlags_Equal)) {
  2014. double xar = plot.XAxis.GetAspect();
  2015. double yar = plot.YAxis[0].GetAspect();
  2016. // edge case: user has set x range this frame, so fit y to x so that we honor their request for x range
  2017. // NB: because of feedback across several frames, the user's x request may not be perfectly honored
  2018. if (gp.NextPlotData.HasXRange) {
  2019. plot.YAxis[0].SetAspect(xar);
  2020. }
  2021. else {
  2022. if (!ImAlmostEqual(xar,yar) && !plot.YAxis[0].IsInputLocked())
  2023. plot.XAxis.SetAspect(yar);
  2024. }
  2025. }
  2026. // INPUT ------------------------------------------------------------------
  2027. HandlePlotInput(plot);
  2028. UpdateTransformCache();
  2029. // set mouse position
  2030. for (int i = 0; i < IMPLOT_Y_AXES; i++) {
  2031. gp.MousePos[i] = PixelsToPlot(IO.MousePos, i);
  2032. }
  2033. // RENDER -----------------------------------------------------------------
  2034. // render frame
  2035. ImGui::RenderFrame(plot.FrameRect.Min, plot.FrameRect.Max, GetStyleColorU32(ImPlotCol_FrameBg), true, Style.FrameRounding);
  2036. // grid bg
  2037. DrawList.AddRectFilled(plot.PlotRect.Min, plot.PlotRect.Max, GetStyleColorU32(ImPlotCol_PlotBg));
  2038. // transform ticks (TODO: Move this into ImPlotTickCollection)
  2039. if (gp.RenderX) {
  2040. for (int t = 0; t < gp.XTicks.Size; t++) {
  2041. ImPlotTick *xt = &gp.XTicks.Ticks[t];
  2042. xt->PixelPos = IM_ROUND(PlotToPixels(xt->PlotPos, 0, 0).x);
  2043. }
  2044. }
  2045. for (int i = 0; i < IMPLOT_Y_AXES; i++) {
  2046. if (gp.RenderY[i]) {
  2047. for (int t = 0; t < gp.YTicks[i].Size; t++) {
  2048. ImPlotTick *yt = &gp.YTicks[i].Ticks[t];
  2049. yt->PixelPos = IM_ROUND(PlotToPixels(0, yt->PlotPos, i).y);
  2050. }
  2051. }
  2052. }
  2053. // render grid (background)
  2054. PushPlotClipRect(gp.Style.PlotBorderSize == 0 ? 1.0f : 0.0f);
  2055. if (!ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_NoGridLines) && !ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_Foreground))
  2056. RenderGridLinesX(DrawList, gp.XTicks, plot.PlotRect, plot.XAxis.ColorMaj, plot.XAxis.ColorMin, gp.Style.MajorGridSize.x, gp.Style.MinorGridSize.x);
  2057. for (int i = 0; i < IMPLOT_Y_AXES; i++) {
  2058. if (plot.YAxis[i].Present && !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_NoGridLines) && !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_Foreground))
  2059. RenderGridLinesY(DrawList, gp.YTicks[i], plot.PlotRect, plot.YAxis[i].ColorMaj, plot.YAxis[i].ColorMin, gp.Style.MajorGridSize.y, gp.Style.MinorGridSize.y);
  2060. }
  2061. PopPlotClipRect();
  2062. // render title
  2063. if (title_size.x > 0.0f && !ImHasFlag(plot.Flags, ImPlotFlags_NoTitle)) {
  2064. ImU32 col = GetStyleColorU32(ImPlotCol_TitleText);
  2065. AddTextCentered(&DrawList,ImVec2(plot.PlotRect.GetCenter().x, plot.CanvasRect.Min.y),col,title);
  2066. }
  2067. // render axis labels
  2068. if (show_x_label) {
  2069. const ImVec2 xLabel_size = ImGui::CalcTextSize(x_label);
  2070. const ImVec2 xLabel_pos(plot.PlotRect.GetCenter().x - xLabel_size.x * 0.5f, plot.CanvasRect.Max.y - txt_height);
  2071. DrawList.AddText(xLabel_pos, plot.XAxis.ColorTxt, x_label);
  2072. }
  2073. if (show_y1_label) {
  2074. const ImVec2 yLabel_size = CalcTextSizeVertical(y1_label);
  2075. const ImVec2 yLabel_pos(plot.CanvasRect.Min.x, plot.PlotRect.GetCenter().y + yLabel_size.y * 0.5f);
  2076. AddTextVertical(&DrawList, yLabel_pos, plot.YAxis[0].ColorTxt, y1_label);
  2077. }
  2078. const char* y_labels[] = {y2_label, y3_label};
  2079. for (int i = 1; i < IMPLOT_Y_AXES; i++) {
  2080. const char* current_label = y_labels[i-1];
  2081. if (plot.YAxis[i].Present && current_label && !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_NoLabel)) {
  2082. const ImVec2 yLabel_size = CalcTextSizeVertical(current_label);
  2083. float label_offset = (plot.YAxis[i].IsLabeled() ? gp.YTicks[i].MaxWidth + gp.Style.LabelPadding.x : 0.0f) + gp.Style.LabelPadding.x;
  2084. const ImVec2 yLabel_pos(gp.YAxisReference[i] + label_offset, plot.PlotRect.GetCenter().y + yLabel_size.y * 0.5f);
  2085. AddTextVertical(&DrawList, yLabel_pos, plot.YAxis[i].ColorTxt, current_label);
  2086. }
  2087. }
  2088. // render tick labels
  2089. ImGui::PushClipRect(plot.FrameRect.Min, plot.FrameRect.Max, true);
  2090. if (!ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_NoTickLabels)) {
  2091. for (int t = 0; t < gp.XTicks.Size; t++) {
  2092. ImPlotTick *xt = &gp.XTicks.Ticks[t];
  2093. if (xt->ShowLabel && xt->PixelPos >= plot.PlotRect.Min.x - 1 && xt->PixelPos <= plot.PlotRect.Max.x + 1)
  2094. DrawList.AddText(ImVec2(xt->PixelPos - xt->LabelSize.x * 0.5f, plot.PlotRect.Max.y + gp.Style.LabelPadding.y + xt->Level * (txt_height + gp.Style.LabelPadding.y)),
  2095. plot.XAxis.ColorTxt, gp.XTicks.GetText(t));
  2096. }
  2097. }
  2098. for (int i = 0; i < IMPLOT_Y_AXES; i++) {
  2099. if (plot.YAxis[i].Present && !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_NoTickLabels)) {
  2100. for (int t = 0; t < gp.YTicks[i].Size; t++) {
  2101. const float x_start = gp.YAxisReference[i] + (i == 0 ? (-gp.Style.LabelPadding.x - gp.YTicks[i].Ticks[t].LabelSize.x) : gp.Style.LabelPadding.x);
  2102. ImPlotTick *yt = &gp.YTicks[i].Ticks[t];
  2103. if (yt->ShowLabel && yt->PixelPos >= plot.PlotRect.Min.y - 1 && yt->PixelPos <= plot.PlotRect.Max.y + 1) {
  2104. ImVec2 start(x_start, yt->PixelPos - 0.5f * yt->LabelSize.y);
  2105. DrawList.AddText(start, plot.YAxis[i].ColorTxt, gp.YTicks[i].GetText(t));
  2106. }
  2107. }
  2108. }
  2109. }
  2110. ImGui::PopClipRect();
  2111. // clear legend (TODO: put elsewhere)
  2112. plot.Items.Legend.Reset();
  2113. // setup items (or dont)
  2114. if (gp.CurrentItems == NULL)
  2115. gp.CurrentItems = &plot.Items;
  2116. // push ID to see item hashes
  2117. ImGui::PushOverrideID(gp.CurrentItems->ID);
  2118. return true;
  2119. }
  2120. //-----------------------------------------------------------------------------
  2121. // EndPlot()
  2122. //-----------------------------------------------------------------------------
  2123. void EndPlot() {
  2124. IM_ASSERT_USER_ERROR(GImPlot != NULL, "No current context. Did you call ImPlot::CreateContext() or ImPlot::SetCurrentContext()?");
  2125. ImPlotContext& gp = *GImPlot;
  2126. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "Mismatched BeginPlot()/EndPlot()!");
  2127. ImGuiContext &G = *GImGui;
  2128. ImPlotPlot &plot = *gp.CurrentPlot;
  2129. ImGuiWindow * Window = G.CurrentWindow;
  2130. ImDrawList & DrawList = *Window->DrawList;
  2131. const ImGuiIO & IO = ImGui::GetIO();
  2132. // AXIS STATES ------------------------------------------------------------
  2133. const bool any_y_dragging = plot.YAxis[0].Dragging || plot.YAxis[1].Dragging || plot.YAxis[2].Dragging;
  2134. // FINAL RENDER -----------------------------------------------------------
  2135. // render grid (foreground)
  2136. PushPlotClipRect(gp.Style.PlotBorderSize == 0 ? 1.0f : 0.0f);
  2137. if (!ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_NoGridLines) && ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_Foreground))
  2138. RenderGridLinesX(DrawList, gp.XTicks, plot.PlotRect, plot.XAxis.ColorMaj, plot.XAxis.ColorMaj, gp.Style.MajorGridSize.x, gp.Style.MinorGridSize.x);
  2139. for (int i = 0; i < IMPLOT_Y_AXES; i++) {
  2140. if (plot.YAxis[i].Present && !ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_NoGridLines) && ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_Foreground))
  2141. RenderGridLinesY(DrawList, gp.YTicks[i], plot.PlotRect, plot.YAxis[i].ColorMaj, plot.YAxis[i].ColorMin, gp.Style.MajorGridSize.y, gp.Style.MinorGridSize.y);
  2142. }
  2143. PopPlotClipRect();
  2144. // render x-ticks
  2145. PushPlotClipRect();
  2146. if (!ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_NoTickMarks)) {
  2147. for (int t = 0; t < gp.XTicks.Size; t++) {
  2148. ImPlotTick *xt = &gp.XTicks.Ticks[t];
  2149. if (xt->Level == 0)
  2150. DrawList.AddLine(ImVec2(xt->PixelPos, plot.PlotRect.Max.y),
  2151. ImVec2(xt->PixelPos, plot.PlotRect.Max.y - (xt->Major ? gp.Style.MajorTickLen.x : gp.Style.MinorTickLen.x)),
  2152. plot.XAxis.ColorMaj,
  2153. xt->Major ? gp.Style.MajorTickSize.x : gp.Style.MinorTickSize.x);
  2154. }
  2155. }
  2156. PopPlotClipRect();
  2157. // render y-ticks
  2158. ImGui::PushClipRect(plot.PlotRect.Min, ImVec2(plot.FrameRect.Max.x, plot.PlotRect.Max.y), true);
  2159. int axis_count = 0;
  2160. for (int i = 0; i < IMPLOT_Y_AXES; i++) {
  2161. if (!plot.YAxis[i].Present) { continue; }
  2162. axis_count++;
  2163. float x_start = gp.YAxisReference[i];
  2164. if (!ImHasFlag(plot.YAxis[i].Flags, ImPlotAxisFlags_NoTickMarks)) {
  2165. float direction = (i == 0) ? 1.0f : -1.0f;
  2166. bool no_major = axis_count >= 3;
  2167. for (int t = 0; t < gp.YTicks[i].Size; t++) {
  2168. ImPlotTick *yt = &gp.YTicks[i].Ticks[t];
  2169. ImVec2 start = ImVec2(x_start, yt->PixelPos);
  2170. DrawList.AddLine(start,
  2171. start + ImVec2(direction * ((!no_major && yt->Major) ? gp.Style.MajorTickLen.y : gp.Style.MinorTickLen.y), 0),
  2172. plot.YAxis[i].ColorMaj,
  2173. (!no_major && yt->Major) ? gp.Style.MajorTickSize.y : gp.Style.MinorTickSize.y);
  2174. }
  2175. }
  2176. if (axis_count >= 3) {
  2177. // Draw a bar next to the ticks to act as a visual separator.
  2178. DrawList.AddLine(ImVec2(x_start, plot.PlotRect.Min.y), ImVec2(x_start, plot.PlotRect.Max.y), GetStyleColorU32(ImPlotCol_YAxisGrid3), 1);
  2179. }
  2180. }
  2181. ImGui::PopClipRect();
  2182. // render annotations
  2183. PushPlotClipRect();
  2184. for (int i = 0; i < gp.Annotations.Size; ++i) {
  2185. const char* txt = gp.Annotations.GetText(i);
  2186. ImPlotAnnotation& an = gp.Annotations.Annotations[i];
  2187. const ImVec2 txt_size = ImGui::CalcTextSize(txt);
  2188. const ImVec2 size = txt_size + gp.Style.AnnotationPadding * 2;
  2189. ImVec2 pos = an.Pos;
  2190. if (an.Offset.x == 0)
  2191. pos.x -= size.x / 2;
  2192. else if (an.Offset.x > 0)
  2193. pos.x += an.Offset.x;
  2194. else
  2195. pos.x -= size.x - an.Offset.x;
  2196. if (an.Offset.y == 0)
  2197. pos.y -= size.y / 2;
  2198. else if (an.Offset.y > 0)
  2199. pos.y += an.Offset.y;
  2200. else
  2201. pos.y -= size.y - an.Offset.y;
  2202. if (an.Clamp)
  2203. pos = ClampLabelPos(pos, size, plot.PlotRect.Min, plot.PlotRect.Max);
  2204. ImRect rect(pos,pos+size);
  2205. if (an.Offset.x != 0 || an.Offset.y != 0) {
  2206. ImVec2 corners[4] = {rect.GetTL(), rect.GetTR(), rect.GetBR(), rect.GetBL()};
  2207. int min_corner = 0;
  2208. float min_len = FLT_MAX;
  2209. for (int c = 0; c < 4; ++c) {
  2210. float len = ImLengthSqr(an.Pos - corners[c]);
  2211. if (len < min_len) {
  2212. min_corner = c;
  2213. min_len = len;
  2214. }
  2215. }
  2216. DrawList.AddLine(an.Pos, corners[min_corner], an.ColorBg);
  2217. }
  2218. DrawList.AddRectFilled(rect.Min, rect.Max, an.ColorBg);
  2219. DrawList.AddText(pos + gp.Style.AnnotationPadding, an.ColorFg, txt);
  2220. }
  2221. // render selection
  2222. if (plot.Selected)
  2223. RenderSelectionRect(DrawList, plot.SelectRect.Min + plot.PlotRect.Min, plot.SelectRect.Max + plot.PlotRect.Min, GetStyleColorVec4(ImPlotCol_Selection));
  2224. // render query
  2225. if (plot.Queried)
  2226. RenderSelectionRect(DrawList, plot.QueryRect.Min + plot.PlotRect.Min, plot.QueryRect.Max + plot.PlotRect.Min, GetStyleColorVec4(ImPlotCol_Query));
  2227. // render crosshairs
  2228. if (ImHasFlag(plot.Flags, ImPlotFlags_Crosshairs) && plot.PlotHovered && !(plot.XAxis.Dragging || any_y_dragging) && !plot.Selecting && !plot.Querying && !plot.Items.Legend.Hovered) {
  2229. ImGui::SetMouseCursor(ImGuiMouseCursor_None);
  2230. ImVec2 xy = IO.MousePos;
  2231. ImVec2 h1(plot.PlotRect.Min.x, xy.y);
  2232. ImVec2 h2(xy.x - 5, xy.y);
  2233. ImVec2 h3(xy.x + 5, xy.y);
  2234. ImVec2 h4(plot.PlotRect.Max.x, xy.y);
  2235. ImVec2 v1(xy.x, plot.PlotRect.Min.y);
  2236. ImVec2 v2(xy.x, xy.y - 5);
  2237. ImVec2 v3(xy.x, xy.y + 5);
  2238. ImVec2 v4(xy.x, plot.PlotRect.Max.y);
  2239. ImU32 col = GetStyleColorU32(ImPlotCol_Crosshairs);
  2240. DrawList.AddLine(h1, h2, col);
  2241. DrawList.AddLine(h3, h4, col);
  2242. DrawList.AddLine(v1, v2, col);
  2243. DrawList.AddLine(v3, v4, col);
  2244. }
  2245. // render mouse pos
  2246. if (!ImHasFlag(plot.Flags, ImPlotFlags_NoMousePos) && plot.PlotHovered) {
  2247. char buffer[128] = {};
  2248. ImBufferWriter writer(buffer, sizeof(buffer));
  2249. // x
  2250. if (ImHasFlag(plot.XAxis.Flags, ImPlotAxisFlags_Time)) {
  2251. ImPlotTimeUnit unit = GetUnitForRange(plot.XAxis.Range.Size() / (plot.PlotRect.GetWidth() / 100));
  2252. const int written = FormatDateTime(ImPlotTime::FromDouble(gp.MousePos[0].x), &writer.Buffer[writer.Pos], writer.Size - writer.Pos - 1, GetDateTimeFmt(TimeFormatMouseCursor, unit));
  2253. if (written > 0)
  2254. writer.Pos += ImMin(written, writer.Size - writer.Pos - 1);
  2255. }
  2256. else {
  2257. writer.Write(GetFormatX(), RoundAxisValue(plot.XAxis, gp.XTicks, gp.MousePos[0].x));
  2258. }
  2259. // y1
  2260. writer.Write(", ");
  2261. writer.Write(GetFormatY(0), RoundAxisValue(plot.YAxis[0], gp.YTicks[0], gp.MousePos[0].y));
  2262. // y2
  2263. if (ImHasFlag(plot.Flags, ImPlotFlags_YAxis2)) {
  2264. writer.Write(", (");
  2265. writer.Write(GetFormatY(1), RoundAxisValue(plot.YAxis[1], gp.YTicks[1], gp.MousePos[1].y));
  2266. writer.Write(")");
  2267. }
  2268. // y3
  2269. if (ImHasFlag(plot.Flags, ImPlotFlags_YAxis3)) {
  2270. writer.Write(", (");
  2271. writer.Write(GetFormatY(2), RoundAxisValue(plot.YAxis[2], gp.YTicks[2], gp.MousePos[2].y));
  2272. writer.Write(")");
  2273. }
  2274. const ImVec2 size = ImGui::CalcTextSize(buffer);
  2275. const ImVec2 pos = GetLocationPos(plot.PlotRect, size, plot.MousePosLocation, gp.Style.MousePosPadding);
  2276. DrawList.AddText(pos, GetStyleColorU32(ImPlotCol_InlayText), buffer);
  2277. }
  2278. PopPlotClipRect();
  2279. // reset legend hovers
  2280. plot.Items.Legend.Hovered = false;
  2281. for (int i = 0; i < plot.Items.GetItemCount(); ++i)
  2282. plot.Items.GetItemByIndex(i)->LegendHovered = false;
  2283. // render legend
  2284. if (!ImHasFlag(plot.Flags, ImPlotFlags_NoLegend) && plot.Items.GetLegendCount() > 0) {
  2285. const ImVec2 legend_size = CalcLegendSize(plot.Items, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, plot.Items.Legend.Orientation);
  2286. const ImVec2 legend_pos = GetLocationPos(plot.Items.Legend.Outside ? plot.FrameRect : plot.PlotRect,
  2287. legend_size,
  2288. plot.Items.Legend.Location,
  2289. plot.Items.Legend.Outside ? gp.Style.PlotPadding : gp.Style.LegendPadding);
  2290. plot.Items.Legend.Rect = ImRect(legend_pos, legend_pos + legend_size);
  2291. // test hover
  2292. plot.Items.Legend.Hovered = plot.FrameHovered && plot.Items.Legend.Rect.Contains(IO.MousePos);
  2293. if (plot.Items.Legend.Outside)
  2294. ImGui::PushClipRect(plot.FrameRect.Min, plot.FrameRect.Max, true);
  2295. else
  2296. PushPlotClipRect();
  2297. ImU32 col_bg = GetStyleColorU32(ImPlotCol_LegendBg);
  2298. ImU32 col_bd = GetStyleColorU32(ImPlotCol_LegendBorder);
  2299. DrawList.AddRectFilled(plot.Items.Legend.Rect.Min, plot.Items.Legend.Rect.Max, col_bg);
  2300. DrawList.AddRect(plot.Items.Legend.Rect.Min, plot.Items.Legend.Rect.Max, col_bd);
  2301. bool legend_contextable = ShowLegendEntries(plot.Items, plot.Items.Legend.Rect, plot.Items.Legend.Hovered, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, plot.Items.Legend.Orientation, DrawList);
  2302. // main ctx menu
  2303. if (legend_contextable && !ImHasFlag(plot.Flags, ImPlotFlags_NoMenus) && IO.MouseReleased[gp.InputMap.ContextMenuButton] && !plot.ContextLocked)
  2304. ImGui::OpenPopup("##LegendContext");
  2305. ImGui::PopClipRect();
  2306. if (ImGui::BeginPopup("##LegendContext")) {
  2307. ImGui::Text("Legend"); ImGui::Separator();
  2308. if (ShowLegendContextMenu(plot.Items.Legend, !ImHasFlag(plot.Flags, ImPlotFlags_NoLegend)))
  2309. ImFlipFlag(plot.Flags, ImPlotFlags_NoLegend);
  2310. ImGui::EndPopup();
  2311. }
  2312. }
  2313. else {
  2314. plot.Items.Legend.Rect = ImRect();
  2315. }
  2316. if (plot.Items.Legend.FlipSideNextFrame) {
  2317. plot.Items.Legend.Outside = !plot.Items.Legend.Outside;
  2318. plot.Items.Legend.FlipSideNextFrame = false;
  2319. }
  2320. // render border
  2321. if (gp.Style.PlotBorderSize > 0)
  2322. DrawList.AddRect(plot.PlotRect.Min, plot.PlotRect.Max, GetStyleColorU32(ImPlotCol_PlotBorder), 0, ImDrawFlags_RoundCornersAll, gp.Style.PlotBorderSize);
  2323. // FIT DATA --------------------------------------------------------------
  2324. const bool axis_equal = ImHasFlag(plot.Flags, ImPlotFlags_Equal);
  2325. if (gp.FitThisFrame) {
  2326. if (gp.FitX) {
  2327. const double ext_size = gp.ExtentsX.Size() * 0.5;
  2328. gp.ExtentsX.Min -= ext_size * gp.Style.FitPadding.x;
  2329. gp.ExtentsX.Max += ext_size * gp.Style.FitPadding.x;
  2330. if (!plot.XAxis.IsLockedMin() && !ImNanOrInf(gp.ExtentsX.Min))
  2331. plot.XAxis.Range.Min = (gp.ExtentsX.Min);
  2332. if (!plot.XAxis.IsLockedMax() && !ImNanOrInf(gp.ExtentsX.Max))
  2333. plot.XAxis.Range.Max = (gp.ExtentsX.Max);
  2334. if (ImAlmostEqual(plot.XAxis.Range.Max, plot.XAxis.Range.Min)) {
  2335. plot.XAxis.Range.Max += 0.5;
  2336. plot.XAxis.Range.Min -= 0.5;
  2337. }
  2338. plot.XAxis.Constrain();
  2339. if (axis_equal && !gp.FitY[0])
  2340. plot.YAxis[0].SetAspect(plot.XAxis.GetAspect());
  2341. }
  2342. for (int i = 0; i < IMPLOT_Y_AXES; i++) {
  2343. if (gp.FitY[i]) {
  2344. const double ext_size = gp.ExtentsY[i].Size() * 0.5;
  2345. gp.ExtentsY[i].Min -= ext_size * gp.Style.FitPadding.y;
  2346. gp.ExtentsY[i].Max += ext_size * gp.Style.FitPadding.y;
  2347. if (!plot.YAxis[i].IsLockedMin() && !ImNanOrInf(gp.ExtentsY[i].Min))
  2348. plot.YAxis[i].Range.Min = (gp.ExtentsY[i].Min);
  2349. if (!plot.YAxis[i].IsLockedMax() && !ImNanOrInf(gp.ExtentsY[i].Max))
  2350. plot.YAxis[i].Range.Max = (gp.ExtentsY[i].Max);
  2351. if (ImAlmostEqual(plot.YAxis[i].Range.Max, plot.YAxis[i].Range.Min)) {
  2352. plot.YAxis[i].Range.Max += 0.5;
  2353. plot.YAxis[i].Range.Min -= 0.5;
  2354. }
  2355. plot.YAxis[i].Constrain();
  2356. if (i == 0 && axis_equal && !gp.FitX)
  2357. plot.XAxis.SetAspect(plot.YAxis[0].GetAspect());
  2358. }
  2359. }
  2360. if (axis_equal && gp.FitX && gp.FitY[0]) {
  2361. double aspect = ImMax(plot.XAxis.GetAspect(), plot.YAxis[0].GetAspect());
  2362. plot.XAxis.SetAspect(aspect);
  2363. plot.YAxis[0].SetAspect(aspect);
  2364. }
  2365. }
  2366. // CONTEXT MENUS -----------------------------------------------------------
  2367. ImGui::PushOverrideID(plot.ID);
  2368. // main ctx menu
  2369. if (!ImHasFlag(plot.Flags, ImPlotFlags_NoMenus) && plot.PlotHovered && IO.MouseReleased[gp.InputMap.ContextMenuButton] && !plot.Items.Legend.Hovered && !plot.ContextLocked)
  2370. ImGui::OpenPopup("##PlotContext");
  2371. if (ImGui::BeginPopup("##PlotContext")) {
  2372. ShowPlotContextMenu(plot);
  2373. ImGui::EndPopup();
  2374. }
  2375. // x-axis ctx menu
  2376. if (!ImHasFlag(plot.Flags, ImPlotFlags_NoMenus) && plot.FrameHovered && plot.XAxis.ExtHovered && IO.MouseReleased[gp.InputMap.ContextMenuButton] && !plot.Items.Legend.Hovered && !plot.ContextLocked)
  2377. ImGui::OpenPopup("##XContext");
  2378. if (ImGui::BeginPopup("##XContext")) {
  2379. ImGui::Text("X-Axis"); ImGui::Separator();
  2380. ShowAxisContextMenu(plot.XAxis, ImHasFlag(plot.Flags, ImPlotFlags_Equal) ? &plot.YAxis[0] : NULL, true);
  2381. ImGui::EndPopup();
  2382. }
  2383. // y-axes ctx menus
  2384. for (int i = 0; i < IMPLOT_Y_AXES; ++i) {
  2385. ImGui::PushID(i);
  2386. if (!ImHasFlag(plot.Flags, ImPlotFlags_NoMenus) && plot.FrameHovered && plot.YAxis[i].ExtHovered && IO.MouseReleased[gp.InputMap.ContextMenuButton] && !plot.Items.Legend.Hovered && !plot.ContextLocked)
  2387. ImGui::OpenPopup("##YContext");
  2388. if (ImGui::BeginPopup("##YContext")) {
  2389. if (i == 0) {
  2390. ImGui::Text("Y-Axis"); ImGui::Separator();
  2391. }
  2392. else {
  2393. ImGui::Text("Y-Axis %d", i + 1); ImGui::Separator();
  2394. }
  2395. ShowAxisContextMenu(plot.YAxis[i], (i == 0 && ImHasFlag(plot.Flags, ImPlotFlags_Equal)) ? &plot.XAxis : NULL, false);
  2396. ImGui::EndPopup();
  2397. }
  2398. ImGui::PopID();
  2399. }
  2400. ImGui::PopID();
  2401. // LINKED AXES ------------------------------------------------------------
  2402. PushLinkedAxis(plot.XAxis);
  2403. for (int i = 0; i < IMPLOT_Y_AXES; ++i)
  2404. PushLinkedAxis(plot.YAxis[i]);
  2405. // CLEANUP ----------------------------------------------------------------
  2406. // resset context locked flag
  2407. if (plot.ContextLocked && IO.MouseReleased[gp.InputMap.BoxSelectButton])
  2408. plot.ContextLocked = false;
  2409. // remove items
  2410. if (gp.CurrentItems == &plot.Items)
  2411. gp.CurrentItems = NULL;
  2412. // reset the plot items for the next frame
  2413. for (int i = 0; i < plot.Items.GetItemCount(); ++i) {
  2414. plot.Items.GetItemByIndex(i)->SeenThisFrame = false;
  2415. }
  2416. // mark the plot as initialized, i.e. having made it through one frame completely
  2417. plot.Initialized = true;
  2418. // Pop ImGui::PushID at the end of BeginPlot
  2419. ImGui::PopID();
  2420. // Reset context for next plot
  2421. ResetCtxForNextPlot(GImPlot);
  2422. // setup next subplot
  2423. if (gp.CurrentSubplot != NULL) {
  2424. ImGui::PopID();
  2425. SubplotNextCell();
  2426. }
  2427. }
  2428. //-----------------------------------------------------------------------------
  2429. // BEGIN/END SUBPLOT
  2430. //-----------------------------------------------------------------------------
  2431. static const float SUBPLOT_BORDER_SIZE = 1.0f;
  2432. static const float SUBPLOT_SPLITTER_HALF_THICKNESS = 4.0f;
  2433. static const float SUBPLOT_SPLITTER_FEEDBACK_TIMER = 0.06f;
  2434. void SubplotSetCell(int row, int col) {
  2435. ImPlotContext& gp = *GImPlot;
  2436. ImPlotSubplot& subplot = *gp.CurrentSubplot;
  2437. if (row >= subplot.Rows || col >= subplot.Cols)
  2438. return;
  2439. float xoff = 0;
  2440. float yoff = 0;
  2441. for (int c = 0; c < col; ++c)
  2442. xoff += subplot.ColRatios[c];
  2443. for (int r = 0; r < row; ++r)
  2444. yoff += subplot.RowRatios[r];
  2445. const ImVec2 grid_size = subplot.GridRect.GetSize();
  2446. ImVec2 cpos = subplot.GridRect.Min + ImVec2(xoff*grid_size.x,yoff*grid_size.y);
  2447. cpos.x = IM_ROUND(cpos.x);
  2448. cpos.y = IM_ROUND(cpos.y);
  2449. ImGui::GetCurrentWindow()->DC.CursorPos = cpos;
  2450. // set cell size
  2451. subplot.CellSize.x = IM_ROUND(subplot.GridRect.GetWidth() * subplot.ColRatios[col]);
  2452. subplot.CellSize.y = IM_ROUND(subplot.GridRect.GetHeight() * subplot.RowRatios[row]);
  2453. // setup links
  2454. const bool lx = ImHasFlag(subplot.Flags, ImPlotSubplotFlags_LinkAllX);
  2455. const bool ly = ImHasFlag(subplot.Flags, ImPlotSubplotFlags_LinkAllY);
  2456. const bool lr = ImHasFlag(subplot.Flags, ImPlotSubplotFlags_LinkRows);
  2457. const bool lc = ImHasFlag(subplot.Flags, ImPlotSubplotFlags_LinkCols);
  2458. LinkNextPlotLimits(lx ? &subplot.ColLinkData[0].Min : lc ? &subplot.ColLinkData[col].Min : NULL,
  2459. lx ? &subplot.ColLinkData[0].Max : lc ? &subplot.ColLinkData[col].Max : NULL,
  2460. ly ? &subplot.RowLinkData[0].Min : lr ? &subplot.RowLinkData[row].Min : NULL,
  2461. ly ? &subplot.RowLinkData[0].Max : lr ? &subplot.RowLinkData[row].Max : NULL);
  2462. // setup alignment
  2463. if (!ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoAlign)) {
  2464. gp.CurrentAlignmentH = &subplot.RowAlignmentData[row];
  2465. gp.CurrentAlignmentV = &subplot.ColAlignmentData[col];
  2466. }
  2467. // set idx
  2468. if (ImHasFlag(subplot.Flags, ImPlotSubplotFlags_ColMajor))
  2469. subplot.CurrentIdx = col * subplot.Rows + row;
  2470. else
  2471. subplot.CurrentIdx = row * subplot.Cols + col;
  2472. }
  2473. void SubplotSetCell(int idx) {
  2474. ImPlotContext& gp = *GImPlot;
  2475. ImPlotSubplot& subplot = *gp.CurrentSubplot;
  2476. if (idx >= subplot.Rows * subplot.Cols)
  2477. return;
  2478. int row = 0, col = 0;
  2479. if (ImHasFlag(subplot.Flags, ImPlotSubplotFlags_ColMajor)) {
  2480. row = idx % subplot.Rows;
  2481. col = idx / subplot.Rows;
  2482. }
  2483. else {
  2484. row = idx / subplot.Cols;
  2485. col = idx % subplot.Cols;
  2486. }
  2487. return SubplotSetCell(row, col);
  2488. }
  2489. void SubplotNextCell() {
  2490. ImPlotContext& gp = *GImPlot;
  2491. ImPlotSubplot& subplot = *gp.CurrentSubplot;
  2492. SubplotSetCell(++subplot.CurrentIdx);
  2493. }
  2494. bool BeginSubplots(const char* title, int rows, int cols, const ImVec2& size, ImPlotSubplotFlags flags, float* row_sizes, float* col_sizes) {
  2495. IM_ASSERT_USER_ERROR(rows > 0 && cols > 0, "Invalid sizing arguments!");
  2496. IM_ASSERT_USER_ERROR(GImPlot != NULL, "No current context. Did you call ImPlot::CreateContext() or ImPlot::SetCurrentContext()?");
  2497. IM_ASSERT_USER_ERROR(GImPlot->CurrentSubplot == NULL, "Mismatched BeginSubplots()/EndSubplots()!");
  2498. ImPlotContext& gp = *GImPlot;
  2499. ImGuiContext &G = *GImGui;
  2500. ImGuiWindow * Window = G.CurrentWindow;
  2501. if (Window->SkipItems)
  2502. return false;
  2503. const ImGuiID ID = Window->GetID(title);
  2504. bool just_created = gp.Subplots.GetByKey(ID) == NULL;
  2505. gp.CurrentSubplot = gp.Subplots.GetOrAddByKey(ID);
  2506. ImPlotSubplot& subplot = *gp.CurrentSubplot;
  2507. subplot.ID = ID;
  2508. subplot.Items.ID = ID;
  2509. // push ID
  2510. ImGui::PushID(ID);
  2511. if (just_created)
  2512. subplot.Flags = flags;
  2513. else if (flags != subplot.PreviousFlags)
  2514. subplot.Flags = flags;
  2515. subplot.PreviousFlags = flags;
  2516. // check for change in rows and cols
  2517. if (subplot.Rows != rows || subplot.Cols != cols) {
  2518. subplot.RowAlignmentData.resize(rows);
  2519. subplot.RowLinkData.resize(rows);
  2520. subplot.RowRatios.resize(rows);
  2521. for (int r = 0; r < rows; ++r) {
  2522. subplot.RowAlignmentData[r].Reset();
  2523. subplot.RowLinkData[r] = ImPlotRange(0,1);
  2524. subplot.RowRatios[r] = 1.0f / rows;
  2525. }
  2526. subplot.ColAlignmentData.resize(cols);
  2527. subplot.ColLinkData.resize(cols);
  2528. subplot.ColRatios.resize(cols);
  2529. for (int c = 0; c < cols; ++c) {
  2530. subplot.ColAlignmentData[c].Reset();
  2531. subplot.ColLinkData[c] = ImPlotRange(0,1);
  2532. subplot.ColRatios[c] = 1.0f / cols;
  2533. }
  2534. }
  2535. // check incoming size requests
  2536. float row_sum = 0, col_sum = 0;
  2537. if (row_sizes != NULL) {
  2538. row_sum = ImSum(row_sizes, rows);
  2539. for (int r = 0; r < rows; ++r)
  2540. subplot.RowRatios[r] = row_sizes[r] / row_sum;
  2541. }
  2542. if (col_sizes != NULL) {
  2543. col_sum = ImSum(col_sizes, cols);
  2544. for (int c = 0; c < cols; ++c)
  2545. subplot.ColRatios[c] = col_sizes[c] / col_sum;
  2546. }
  2547. subplot.Rows = rows;
  2548. subplot.Cols = cols;
  2549. // calc plot frame sizes
  2550. ImVec2 title_size(0.0f, 0.0f);
  2551. const float txt_height = ImGui::GetTextLineHeight();
  2552. if (!ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoTitle))
  2553. title_size = ImGui::CalcTextSize(title, NULL, true);
  2554. const float pad_top = title_size.x > 0.0f ? title_size.y + gp.Style.LabelPadding.y : 0;
  2555. const ImVec2 half_pad = gp.Style.PlotPadding/2;
  2556. const ImVec2 frame_size = ImGui::CalcItemSize(size, gp.Style.PlotDefaultSize.x, gp.Style.PlotDefaultSize.y);
  2557. subplot.FrameRect = ImRect(Window->DC.CursorPos, Window->DC.CursorPos + frame_size);
  2558. subplot.GridRect.Min = subplot.FrameRect.Min + half_pad + ImVec2(0,pad_top);
  2559. subplot.GridRect.Max = subplot.FrameRect.Max - half_pad;
  2560. subplot.FrameHovered = subplot.FrameRect.Contains(ImGui::GetMousePos()) && ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows);
  2561. // outside legend adjustments
  2562. const bool share_items = ImHasFlag(subplot.Flags, ImPlotSubplotFlags_ShareItems);
  2563. if (share_items)
  2564. gp.CurrentItems = &subplot.Items;
  2565. if (share_items && !ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoLegend) && subplot.Items.GetLegendCount() > 0) {
  2566. const ImVec2 legend_size = CalcLegendSize(subplot.Items, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, subplot.Items.Legend.Orientation);
  2567. const bool west = ImHasFlag(subplot.Items.Legend.Location, ImPlotLocation_West) && !ImHasFlag(subplot.Items.Legend.Location, ImPlotLocation_East);
  2568. const bool east = ImHasFlag(subplot.Items.Legend.Location, ImPlotLocation_East) && !ImHasFlag(subplot.Items.Legend.Location, ImPlotLocation_West);
  2569. const bool north = ImHasFlag(subplot.Items.Legend.Location, ImPlotLocation_North) && !ImHasFlag(subplot.Items.Legend.Location, ImPlotLocation_South);
  2570. const bool south = ImHasFlag(subplot.Items.Legend.Location, ImPlotLocation_South) && !ImHasFlag(subplot.Items.Legend.Location, ImPlotLocation_North);
  2571. const bool horz = subplot.Items.Legend.Orientation == ImPlotOrientation_Horizontal;
  2572. if ((west && !horz) || (west && horz && !north && !south))
  2573. subplot.GridRect.Min.x += (legend_size.x + gp.Style.LegendPadding.x);
  2574. if ((east && !horz) || (east && horz && !north && !south))
  2575. subplot.GridRect.Max.x -= (legend_size.x + gp.Style.LegendPadding.x);
  2576. if ((north && horz) || (north && !horz && !west && !east))
  2577. subplot.GridRect.Min.y += (legend_size.y + gp.Style.LegendPadding.y);
  2578. if ((south && horz) || (south && !horz && !west && !east))
  2579. subplot.GridRect.Max.y -= (legend_size.y + gp.Style.LegendPadding.y);
  2580. }
  2581. // render single background frame
  2582. ImGui::RenderFrame(subplot.FrameRect.Min, subplot.FrameRect.Max, GetStyleColorU32(ImPlotCol_FrameBg), true, ImGui::GetStyle().FrameRounding);
  2583. // render title
  2584. if (title_size.x > 0.0f && !ImHasFlag(subplot.Flags, ImPlotFlags_NoTitle)) {
  2585. const ImU32 col = GetStyleColorU32(ImPlotCol_TitleText);
  2586. AddTextCentered(ImGui::GetWindowDrawList(),ImVec2(subplot.GridRect.GetCenter().x, subplot.GridRect.Min.y - pad_top + half_pad.y),col,title);
  2587. }
  2588. // render splitters
  2589. if (!ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoResize)) {
  2590. ImDrawList& DrawList = *ImGui::GetWindowDrawList();
  2591. const ImU32 nrm_col = ImGui::ColorConvertFloat4ToU32(GImGui->Style.Colors[ImGuiCol_Separator]);
  2592. const ImU32 hov_col = ImGui::ColorConvertFloat4ToU32(GImGui->Style.Colors[ImGuiCol_SeparatorHovered]);
  2593. const ImU32 act_col = ImGui::ColorConvertFloat4ToU32(GImGui->Style.Colors[ImGuiCol_SeparatorActive]);
  2594. float xpos = subplot.GridRect.Min.x;
  2595. float ypos = subplot.GridRect.Min.y;
  2596. const ImVec2 mouse = ImGui::GetIO().MousePos;
  2597. int separator = 1;
  2598. // bool pass = false;
  2599. for (int r = 0; r < subplot.Rows-1; ++r) {
  2600. ypos += subplot.RowRatios[r] * subplot.GridRect.GetHeight();
  2601. const ImGuiID sep_id = subplot.ID + separator;
  2602. ImGui::KeepAliveID(sep_id);
  2603. const ImRect sep_bb = ImRect(subplot.GridRect.Min.x, ypos-SUBPLOT_SPLITTER_HALF_THICKNESS, subplot.GridRect.Max.x, ypos+SUBPLOT_SPLITTER_HALF_THICKNESS);
  2604. bool sep_hov = false, sep_hld = false;
  2605. const bool sep_clk = ImGui::ButtonBehavior(sep_bb, sep_id, &sep_hov, &sep_hld, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_AllowItemOverlap | ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnDoubleClick);
  2606. if ((sep_hov && G.HoveredIdTimer > SUBPLOT_SPLITTER_FEEDBACK_TIMER) || sep_hld) {
  2607. if (sep_clk && ImGui::IsMouseDoubleClicked(0)) {
  2608. float p = (subplot.RowRatios[r] + subplot.RowRatios[r+1])/2;
  2609. subplot.RowRatios[r] = subplot.RowRatios[r+1] = p;
  2610. }
  2611. if (sep_clk) {
  2612. subplot.TempSizes[0] = subplot.RowRatios[r];
  2613. subplot.TempSizes[1] = subplot.RowRatios[r+1];
  2614. }
  2615. if (sep_hld) {
  2616. float dp = ImGui::GetMouseDragDelta(0).y / subplot.GridRect.GetHeight();
  2617. if (subplot.TempSizes[0] + dp > 0.1f && subplot.TempSizes[1] - dp > 0.1f) {
  2618. subplot.RowRatios[r] = subplot.TempSizes[0] + dp;
  2619. subplot.RowRatios[r+1] = subplot.TempSizes[1] - dp;
  2620. }
  2621. }
  2622. DrawList.AddLine(ImVec2(IM_ROUND(subplot.GridRect.Min.x),IM_ROUND(ypos)),
  2623. ImVec2(IM_ROUND(subplot.GridRect.Max.x),IM_ROUND(ypos)),
  2624. sep_hld ? act_col : hov_col, SUBPLOT_BORDER_SIZE);
  2625. ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeNS);
  2626. }
  2627. separator++;
  2628. }
  2629. for (int c = 0; c < subplot.Cols-1; ++c) {
  2630. xpos += subplot.ColRatios[c] * subplot.GridRect.GetWidth();
  2631. const ImGuiID sep_id = subplot.ID + separator;
  2632. ImGui::KeepAliveID(sep_id);
  2633. const ImRect sep_bb = ImRect(xpos-SUBPLOT_SPLITTER_HALF_THICKNESS, subplot.GridRect.Min.y, xpos+SUBPLOT_SPLITTER_HALF_THICKNESS, subplot.GridRect.Max.y);
  2634. bool sep_hov = false, sep_hld = false;
  2635. const bool sep_clk = ImGui::ButtonBehavior(sep_bb, sep_id, &sep_hov, &sep_hld, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_AllowItemOverlap | ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnDoubleClick);
  2636. if ((sep_hov && G.HoveredIdTimer > SUBPLOT_SPLITTER_FEEDBACK_TIMER) || sep_hld) {
  2637. if (sep_clk && ImGui::IsMouseDoubleClicked(0)) {
  2638. float p = (subplot.ColRatios[c] + subplot.ColRatios[c+1])/2;
  2639. subplot.ColRatios[c] = subplot.ColRatios[c+1] = p;
  2640. }
  2641. if (sep_clk) {
  2642. subplot.TempSizes[0] = subplot.ColRatios[c];
  2643. subplot.TempSizes[1] = subplot.ColRatios[c+1];
  2644. }
  2645. if (sep_hld) {
  2646. float dp = ImGui::GetMouseDragDelta(0).x / subplot.GridRect.GetWidth();
  2647. if (subplot.TempSizes[0] + dp > 0.1f && subplot.TempSizes[1] - dp > 0.1f) {
  2648. subplot.ColRatios[c] = subplot.TempSizes[0] + dp;
  2649. subplot.ColRatios[c+1] = subplot.TempSizes[1] - dp;
  2650. }
  2651. }
  2652. DrawList.AddLine(ImVec2(IM_ROUND(xpos),IM_ROUND(subplot.GridRect.Min.y)),
  2653. ImVec2(IM_ROUND(xpos),IM_ROUND(subplot.GridRect.Max.y)),
  2654. sep_hld ? act_col : hov_col, SUBPLOT_BORDER_SIZE);
  2655. ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeEW);
  2656. }
  2657. separator++;
  2658. }
  2659. }
  2660. // set outgoing sizes
  2661. if (row_sizes != NULL) {
  2662. for (int r = 0; r < rows; ++r)
  2663. row_sizes[r] = subplot.RowRatios[r] * row_sum;
  2664. }
  2665. if (col_sizes != NULL) {
  2666. for (int c = 0; c < cols; ++c)
  2667. col_sizes[c] = subplot.ColRatios[c] * col_sum;
  2668. }
  2669. // push styling
  2670. PushStyleColor(ImPlotCol_FrameBg, IM_COL32_BLACK_TRANS);
  2671. PushStyleVar(ImPlotStyleVar_PlotPadding, half_pad);
  2672. PushStyleVar(ImPlotStyleVar_PlotMinSize, ImVec2(0,0));
  2673. ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize,0);
  2674. // set initial cursor pos
  2675. Window->DC.CursorPos = subplot.GridRect.Min;
  2676. // begin alignrmnts
  2677. for (int r = 0; r < subplot.Rows; ++r)
  2678. subplot.RowAlignmentData[r].Begin();
  2679. for (int c = 0; c < subplot.Cols; ++c)
  2680. subplot.ColAlignmentData[c].Begin();
  2681. // clear legend data
  2682. subplot.Items.Legend.Reset();
  2683. // Setup first subplot
  2684. SubplotSetCell(0,0);
  2685. return true;
  2686. }
  2687. void EndSubplots() {
  2688. IM_ASSERT_USER_ERROR(GImPlot != NULL, "No current context. Did you call ImPlot::CreateContext() or ImPlot::SetCurrentContext()?");
  2689. IM_ASSERT_USER_ERROR(GImPlot->CurrentSubplot != NULL, "Mismatched BeginSubplots()/EndSubplots()!");
  2690. ImPlotContext& gp = *GImPlot;
  2691. ImPlotSubplot& subplot = *GImPlot->CurrentSubplot;
  2692. // set alignments
  2693. for (int r = 0; r < subplot.Rows; ++r)
  2694. subplot.RowAlignmentData[r].End();
  2695. for (int c = 0; c < subplot.Cols; ++c)
  2696. subplot.ColAlignmentData[c].End();
  2697. // pop styling
  2698. PopStyleColor();
  2699. PopStyleVar();
  2700. PopStyleVar();
  2701. ImGui::PopStyleVar();
  2702. // legend
  2703. subplot.Items.Legend.Hovered = false;
  2704. for (int i = 0; i < subplot.Items.GetItemCount(); ++i)
  2705. subplot.Items.GetItemByIndex(i)->LegendHovered = false;
  2706. // render legend
  2707. const bool share_items = ImHasFlag(subplot.Flags, ImPlotSubplotFlags_ShareItems);
  2708. ImDrawList& DrawList = *ImGui::GetWindowDrawList();
  2709. if (share_items && !ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoLegend) && subplot.Items.GetLegendCount() > 0) {
  2710. const ImVec2 legend_size = CalcLegendSize(subplot.Items, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, subplot.Items.Legend.Orientation);
  2711. const ImVec2 legend_pos = GetLocationPos(subplot.FrameRect, legend_size, subplot.Items.Legend.Location, gp.Style.PlotPadding);
  2712. subplot.Items.Legend.Rect = ImRect(legend_pos, legend_pos + legend_size);
  2713. subplot.Items.Legend.Hovered = subplot.FrameHovered && subplot.Items.Legend.Rect.Contains(ImGui::GetIO().MousePos);
  2714. ImGui::PushClipRect(subplot.FrameRect.Min, subplot.FrameRect.Max, true);
  2715. ImU32 col_bg = GetStyleColorU32(ImPlotCol_LegendBg);
  2716. ImU32 col_bd = GetStyleColorU32(ImPlotCol_LegendBorder);
  2717. DrawList.AddRectFilled(subplot.Items.Legend.Rect.Min, subplot.Items.Legend.Rect.Max, col_bg);
  2718. DrawList.AddRect(subplot.Items.Legend.Rect.Min, subplot.Items.Legend.Rect.Max, col_bd);
  2719. bool legend_contextable =ShowLegendEntries(subplot.Items, subplot.Items.Legend.Rect, subplot.Items.Legend.Hovered, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, subplot.Items.Legend.Orientation, DrawList);
  2720. if (legend_contextable && !ImHasFlag(subplot.Flags, ImPlotSubplotFlags_NoMenus) && ImGui::GetIO().MouseReleased[gp.InputMap.ContextMenuButton])
  2721. ImGui::OpenPopup("##LegendContext");
  2722. ImGui::PopClipRect();
  2723. if (ImGui::BeginPopup("##LegendContext")) {
  2724. ImGui::Text("Legend"); ImGui::Separator();
  2725. if (ShowLegendContextMenu(subplot.Items.Legend, !ImHasFlag(subplot.Flags, ImPlotFlags_NoLegend)))
  2726. ImFlipFlag(subplot.Flags, ImPlotFlags_NoLegend);
  2727. ImGui::EndPopup();
  2728. }
  2729. }
  2730. else {
  2731. subplot.Items.Legend.Rect = ImRect();
  2732. }
  2733. // remove items
  2734. if (gp.CurrentItems == &subplot.Items)
  2735. gp.CurrentItems = NULL;
  2736. // reset the plot items for the next frame (TODO: put this elswhere)
  2737. for (int i = 0; i < subplot.Items.GetItemCount(); ++i) {
  2738. subplot.Items.GetItemByIndex(i)->SeenThisFrame = false;
  2739. }
  2740. // pop id
  2741. ImGui::PopID();
  2742. // set DC back correctly
  2743. GImGui->CurrentWindow->DC.CursorPos = subplot.FrameRect.Min;
  2744. ImGui::Dummy(subplot.FrameRect.GetSize());
  2745. ResetCtxForNextSubplot(GImPlot);
  2746. }
  2747. //-----------------------------------------------------------------------------
  2748. // MISC API
  2749. //-----------------------------------------------------------------------------
  2750. bool BeginAlignedPlots(const char* group_id, ImPlotOrientation orientation) {
  2751. IM_ASSERT_USER_ERROR(GImPlot != NULL, "No current context. Did you call ImPlot::CreateContext() or ImPlot::SetCurrentContext()?");
  2752. IM_ASSERT_USER_ERROR(GImPlot->CurrentAlignmentH == NULL && GImPlot->CurrentAlignmentV == NULL, "Mismatched BeginAlignedPlots()/EndAlignedPlots()!");
  2753. ImPlotContext& gp = *GImPlot;
  2754. ImGuiContext &G = *GImGui;
  2755. ImGuiWindow * Window = G.CurrentWindow;
  2756. if (Window->SkipItems)
  2757. return false;
  2758. const ImGuiID ID = Window->GetID(group_id);
  2759. ImPlotAlignmentData* alignment = gp.AlignmentData.GetOrAddByKey(ID);
  2760. if (orientation == ImPlotOrientation_Horizontal)
  2761. gp.CurrentAlignmentH = alignment;
  2762. if (orientation == ImPlotOrientation_Vertical)
  2763. gp.CurrentAlignmentV = alignment;
  2764. if (alignment->Orientation != orientation)
  2765. alignment->Reset();
  2766. alignment->Orientation = orientation;
  2767. alignment->Begin();
  2768. return true;
  2769. }
  2770. void EndAlignedPlots() {
  2771. IM_ASSERT_USER_ERROR(GImPlot != NULL, "No current context. Did you call ImPlot::CreateContext() or ImPlot::SetCurrentContext()?");
  2772. IM_ASSERT_USER_ERROR(GImPlot->CurrentAlignmentH != NULL || GImPlot->CurrentAlignmentV != NULL, "Mismatched BeginAlignedPlots()/EndAlignedPlots()!");
  2773. ImPlotContext& gp = *GImPlot;
  2774. ImPlotAlignmentData* alignment = gp.CurrentAlignmentH != NULL ? gp.CurrentAlignmentH : (gp.CurrentAlignmentV != NULL ? gp.CurrentAlignmentV : NULL);
  2775. if (alignment)
  2776. alignment->End();
  2777. ResetCtxForNextAlignedPlots(GImPlot);
  2778. }
  2779. ImPlotInputMap& GetInputMap() {
  2780. return GImPlot->InputMap;
  2781. }
  2782. void SetNextPlotLimits(double x_min, double x_max, double y_min, double y_max, ImGuiCond cond) {
  2783. IM_ASSERT_USER_ERROR(GImPlot->CurrentPlot == NULL, "SetNextPlotLimits() needs to be called before BeginPlot()!");
  2784. SetNextPlotLimitsX(x_min, x_max, cond);
  2785. SetNextPlotLimitsY(y_min, y_max, cond);
  2786. }
  2787. void SetNextPlotLimitsX(double x_min, double x_max, ImGuiCond cond) {
  2788. ImPlotContext& gp = *GImPlot;
  2789. IM_ASSERT_USER_ERROR(gp.CurrentPlot == NULL, "SetNextPlotLSetNextPlotLimitsXimitsY() needs to be called before BeginPlot()!");
  2790. IM_ASSERT(cond == 0 || ImIsPowerOfTwo(cond)); // Make sure the user doesn't attempt to combine multiple condition flags.
  2791. gp.NextPlotData.HasXRange = true;
  2792. gp.NextPlotData.XRangeCond = cond;
  2793. gp.NextPlotData.XRange.Min = x_min;
  2794. gp.NextPlotData.XRange.Max = x_max;
  2795. }
  2796. void SetNextPlotLimitsY(double y_min, double y_max, ImGuiCond cond, ImPlotYAxis y_axis) {
  2797. ImPlotContext& gp = *GImPlot;
  2798. IM_ASSERT_USER_ERROR(gp.CurrentPlot == NULL, "SetNextPlotLimitsY() needs to be called before BeginPlot()!");
  2799. IM_ASSERT_USER_ERROR(y_axis >= 0 && y_axis < IMPLOT_Y_AXES, "y_axis needs to be between 0 and IMPLOT_Y_AXES");
  2800. IM_ASSERT(cond == 0 || ImIsPowerOfTwo(cond)); // Make sure the user doesn't attempt to combine multiple condition flags.
  2801. gp.NextPlotData.HasYRange[y_axis] = true;
  2802. gp.NextPlotData.YRangeCond[y_axis] = cond;
  2803. gp.NextPlotData.YRange[y_axis].Min = y_min;
  2804. gp.NextPlotData.YRange[y_axis].Max = y_max;
  2805. }
  2806. void LinkNextPlotLimits(double* xmin, double* xmax, double* ymin, double* ymax, double* ymin2, double* ymax2, double* ymin3, double* ymax3) {
  2807. ImPlotContext& gp = *GImPlot;
  2808. gp.NextPlotData.LinkedXmin = xmin;
  2809. gp.NextPlotData.LinkedXmax = xmax;
  2810. gp.NextPlotData.LinkedYmin[0] = ymin;
  2811. gp.NextPlotData.LinkedYmax[0] = ymax;
  2812. gp.NextPlotData.LinkedYmin[1] = ymin2;
  2813. gp.NextPlotData.LinkedYmax[1] = ymax2;
  2814. gp.NextPlotData.LinkedYmin[2] = ymin3;
  2815. gp.NextPlotData.LinkedYmax[2] = ymax3;
  2816. }
  2817. void FitNextPlotAxes(bool x, bool y, bool y2, bool y3) {
  2818. ImPlotContext& gp = *GImPlot;
  2819. IM_ASSERT_USER_ERROR(gp.CurrentPlot == NULL, "FitNextPlotAxes() needs to be called before BeginPlot()!");
  2820. gp.NextPlotData.FitX = x;
  2821. gp.NextPlotData.FitY[0] = y;
  2822. gp.NextPlotData.FitY[1] = y2;
  2823. gp.NextPlotData.FitY[2] = y3;
  2824. }
  2825. void SetNextPlotTicksX(const double* values, int n_ticks, const char* const labels[], bool show_default) {
  2826. ImPlotContext& gp = *GImPlot;
  2827. IM_ASSERT_USER_ERROR(gp.CurrentPlot == NULL, "SetNextPlotTicksX() needs to be called before BeginPlot()!");
  2828. gp.NextPlotData.ShowDefaultTicksX = show_default;
  2829. AddTicksCustom(values, labels, n_ticks, gp.XTicks, GetFormatX());
  2830. }
  2831. void SetNextPlotTicksX(double x_min, double x_max, int n_ticks, const char* const labels[], bool show_default) {
  2832. IM_ASSERT_USER_ERROR(n_ticks > 1, "The number of ticks must be greater than 1");
  2833. static ImVector<double> buffer;
  2834. FillRange(buffer, n_ticks, x_min, x_max);
  2835. SetNextPlotTicksX(&buffer[0], n_ticks, labels, show_default);
  2836. }
  2837. void SetNextPlotTicksY(const double* values, int n_ticks, const char* const labels[], bool show_default, ImPlotYAxis y_axis) {
  2838. ImPlotContext& gp = *GImPlot;
  2839. IM_ASSERT_USER_ERROR(gp.CurrentPlot == NULL, "SetNextPlotTicksY() needs to be called before BeginPlot()!");
  2840. IM_ASSERT_USER_ERROR(y_axis >= 0 && y_axis < IMPLOT_Y_AXES, "y_axis needs to be between 0 and IMPLOT_Y_AXES");
  2841. gp.NextPlotData.ShowDefaultTicksY[y_axis] = show_default;
  2842. AddTicksCustom(values, labels, n_ticks, gp.YTicks[y_axis], GetFormatY(y_axis));
  2843. }
  2844. void SetNextPlotTicksY(double y_min, double y_max, int n_ticks, const char* const labels[], bool show_default, ImPlotYAxis y_axis) {
  2845. IM_ASSERT_USER_ERROR(n_ticks > 1, "The number of ticks must be greater than 1");
  2846. static ImVector<double> buffer;
  2847. FillRange(buffer, n_ticks, y_min, y_max);
  2848. SetNextPlotTicksY(&buffer[0], n_ticks, labels, show_default,y_axis);
  2849. }
  2850. void SetNextPlotFormatX(const char* fmt){
  2851. ImPlotContext& gp = *GImPlot;
  2852. IM_ASSERT_USER_ERROR(gp.CurrentPlot == NULL, "SetNextPlotFormatX() needs to be called before BeginPlot()!");
  2853. gp.NextPlotData.HasFmtX = true;
  2854. ImStrncpy(gp.NextPlotData.FmtX, fmt, 16);
  2855. }
  2856. void SetNextPlotFormatY(const char* fmt, ImPlotYAxis y_axis) {
  2857. ImPlotContext& gp = *GImPlot;
  2858. IM_ASSERT_USER_ERROR(gp.CurrentPlot == NULL, "SetNextPlotFormatY() needs to be called before BeginPlot()!");
  2859. IM_ASSERT_USER_ERROR(y_axis >= 0 && y_axis < IMPLOT_Y_AXES, "y_axis needs to be between 0 and IMPLOT_Y_AXES");
  2860. gp.NextPlotData.HasFmtY[y_axis] = true;
  2861. ImStrncpy(gp.NextPlotData.FmtY[y_axis], fmt, 16);
  2862. }
  2863. void SetPlotYAxis(ImPlotYAxis y_axis) {
  2864. ImPlotContext& gp = *GImPlot;
  2865. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "SetPlotYAxis() needs to be called between BeginPlot() and EndPlot()!");
  2866. IM_ASSERT_USER_ERROR(y_axis >= 0 && y_axis < IMPLOT_Y_AXES, "y_axis needs to be between 0 and IMPLOT_Y_AXES");
  2867. gp.CurrentPlot->CurrentYAxis = y_axis;
  2868. }
  2869. ImVec2 GetPlotPos() {
  2870. ImPlotContext& gp = *GImPlot;
  2871. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "GetPlotPos() needs to be called between BeginPlot() and EndPlot()!");
  2872. return gp.CurrentPlot->PlotRect.Min;
  2873. }
  2874. ImVec2 GetPlotSize() {
  2875. ImPlotContext& gp = *GImPlot;
  2876. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "GetPlotSize() needs to be called between BeginPlot() and EndPlot()!");
  2877. return gp.CurrentPlot->PlotRect.GetSize();
  2878. }
  2879. ImDrawList* GetPlotDrawList() {
  2880. return ImGui::GetWindowDrawList();
  2881. }
  2882. void PushPlotClipRect(float expand) {
  2883. ImPlotContext& gp = *GImPlot;
  2884. ImRect rect = gp.CurrentPlot->PlotRect;
  2885. rect.Expand(expand);
  2886. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "PushPlotClipRect() needs to be called between BeginPlot() and EndPlot()!");
  2887. ImGui::PushClipRect(rect.Min, rect.Max, true);
  2888. }
  2889. void PopPlotClipRect() {
  2890. ImGui::PopClipRect();
  2891. }
  2892. bool IsPlotHovered() {
  2893. ImPlotContext& gp = *GImPlot;
  2894. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "IsPlotHovered() needs to be called between BeginPlot() and EndPlot()!");
  2895. return gp.CurrentPlot->PlotHovered;
  2896. }
  2897. bool IsPlotXAxisHovered() {
  2898. ImPlotContext& gp = *GImPlot;
  2899. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "IsPlotXAxisHovered() needs to be called between BeginPlot() and EndPlot()!");
  2900. return gp.CurrentPlot->XAxis.ExtHovered;
  2901. }
  2902. bool IsPlotYAxisHovered(ImPlotYAxis y_axis_in) {
  2903. ImPlotContext& gp = *GImPlot;
  2904. IM_ASSERT_USER_ERROR(y_axis_in >= -1 && y_axis_in < IMPLOT_Y_AXES, "y_axis needs to between -1 and IMPLOT_Y_AXES");
  2905. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "IsPlotYAxisHovered() needs to be called between BeginPlot() and EndPlot()!");
  2906. const ImPlotYAxis y_axis = y_axis_in >= 0 ? y_axis_in : gp.CurrentPlot->CurrentYAxis;
  2907. return gp.CurrentPlot->YAxis[y_axis].ExtHovered;
  2908. }
  2909. ImPlotPoint GetPlotMousePos(ImPlotYAxis y_axis_in) {
  2910. ImPlotContext& gp = *GImPlot;
  2911. IM_ASSERT_USER_ERROR(y_axis_in >= -1 && y_axis_in < IMPLOT_Y_AXES, "y_axis needs to between -1 and IMPLOT_Y_AXES");
  2912. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "GetPlotMousePos() needs to be called between BeginPlot() and EndPlot()!");
  2913. const ImPlotYAxis y_axis = y_axis_in >= 0 ? y_axis_in : gp.CurrentPlot->CurrentYAxis;
  2914. return gp.MousePos[y_axis];
  2915. }
  2916. ImPlotLimits GetPlotLimits(ImPlotYAxis y_axis_in) {
  2917. ImPlotContext& gp = *GImPlot;
  2918. IM_ASSERT_USER_ERROR(y_axis_in >= -1 && y_axis_in < IMPLOT_Y_AXES, "y_axis needs to between -1 and IMPLOT_Y_AXES");
  2919. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "GetPlotLimits() needs to be called between BeginPlot() and EndPlot()!");
  2920. const ImPlotYAxis y_axis = y_axis_in >= 0 ? y_axis_in : gp.CurrentPlot->CurrentYAxis;
  2921. ImPlotPlot& plot = *gp.CurrentPlot;
  2922. ImPlotLimits limits;
  2923. limits.X = plot.XAxis.Range;
  2924. limits.Y = plot.YAxis[y_axis].Range;
  2925. return limits;
  2926. }
  2927. bool IsPlotSelected() {
  2928. ImPlotContext& gp = *GImPlot;
  2929. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "IsPlotSelected() needs to be called between BeginPlot() and EndPlot()!");
  2930. return gp.CurrentPlot->Selected;
  2931. }
  2932. ImPlotLimits GetPlotSelection(ImPlotYAxis y_axis) {
  2933. ImPlotContext& gp = *GImPlot;
  2934. IM_ASSERT_USER_ERROR(y_axis >= -1 && y_axis < IMPLOT_Y_AXES, "y_axis needs to between -1 and IMPLOT_Y_AXES");
  2935. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "GetPlotSelection() needs to be called between BeginPlot() and EndPlot()!");
  2936. ImPlotPlot& plot = *gp.CurrentPlot;
  2937. y_axis = y_axis >= 0 ? y_axis : gp.CurrentPlot->CurrentYAxis;
  2938. if (!plot.Selected)
  2939. return ImPlotLimits(0,0,0,0);
  2940. UpdateTransformCache();
  2941. ImPlotPoint p1 = PixelsToPlot(plot.SelectRect.Min + plot.PlotRect.Min, y_axis);
  2942. ImPlotPoint p2 = PixelsToPlot(plot.SelectRect.Max + plot.PlotRect.Min, y_axis);
  2943. ImPlotLimits result;
  2944. result.X.Min = ImMin(p1.x, p2.x);
  2945. result.X.Max = ImMax(p1.x, p2.x);
  2946. result.Y.Min = ImMin(p1.y, p2.y);
  2947. result.Y.Max = ImMax(p1.y, p2.y);
  2948. return result;
  2949. }
  2950. bool IsPlotQueried() {
  2951. ImPlotContext& gp = *GImPlot;
  2952. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "IsPlotQueried() needs to be called between BeginPlot() and EndPlot()!");
  2953. return gp.CurrentPlot->Queried;
  2954. }
  2955. ImPlotLimits GetPlotQuery(ImPlotYAxis y_axis) {
  2956. ImPlotContext& gp = *GImPlot;
  2957. IM_ASSERT_USER_ERROR(y_axis >= -1 && y_axis < IMPLOT_Y_AXES, "y_axis needs to between -1 and IMPLOT_Y_AXES");
  2958. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "GetPlotQuery() needs to be called between BeginPlot() and EndPlot()!");
  2959. ImPlotPlot& plot = *gp.CurrentPlot;
  2960. y_axis = y_axis >= 0 ? y_axis : gp.CurrentPlot->CurrentYAxis;
  2961. if (!plot.Queried)
  2962. return ImPlotLimits(0,0,0,0);
  2963. UpdateTransformCache();
  2964. ImPlotPoint p1 = PixelsToPlot(plot.QueryRect.Min + plot.PlotRect.Min, y_axis);
  2965. ImPlotPoint p2 = PixelsToPlot(plot.QueryRect.Max + plot.PlotRect.Min, y_axis);
  2966. ImPlotLimits result;
  2967. result.X.Min = ImMin(p1.x, p2.x);
  2968. result.X.Max = ImMax(p1.x, p2.x);
  2969. result.Y.Min = ImMin(p1.y, p2.y);
  2970. result.Y.Max = ImMax(p1.y, p2.y);
  2971. return result;
  2972. }
  2973. void SetPlotQuery(const ImPlotLimits& query, ImPlotYAxis y_axis) {
  2974. ImPlotContext& gp = *GImPlot;
  2975. IM_ASSERT_USER_ERROR(y_axis >= -1 && y_axis < IMPLOT_Y_AXES, "y_axis needs to between -1 and IMPLOT_Y_AXES");
  2976. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "GetPlotQuery() needs to be called between BeginPlot() and EndPlot()!");
  2977. ImPlotPlot& plot = *gp.CurrentPlot;
  2978. y_axis = y_axis >= 0 ? y_axis : gp.CurrentPlot->CurrentYAxis;
  2979. UpdateTransformCache();
  2980. ImVec2 p1 = PlotToPixels(query.Min(),y_axis);
  2981. ImVec2 p2 = PlotToPixels(query.Max(),y_axis);
  2982. plot.Queried = true;
  2983. plot.Querying = false;
  2984. plot.QueryRect = ImRect(ImMin(p1,p2)-plot.PlotRect.Min, ImMax(p1,p2)-plot.PlotRect.Min);
  2985. }
  2986. void AnnotateEx(double x, double y, bool clamp, const ImVec4& col, const ImVec2& off, const char* fmt, va_list args) {
  2987. ImPlotContext& gp = *GImPlot;
  2988. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "Annotate() needs to be called between BeginPlot() and EndPlot()!");
  2989. ImVec2 pos = PlotToPixels(x,y);
  2990. ImU32 bg = ImGui::GetColorU32(col);
  2991. ImU32 fg = col.w == 0 ? GetStyleColorU32(ImPlotCol_InlayText) : CalcTextColor(col);
  2992. gp.Annotations.AppendV(pos, off, bg, fg, clamp, fmt, args);
  2993. }
  2994. void AnnotateV(double x, double y, const ImVec2& offset, const char* fmt, va_list args) {
  2995. AnnotateEx(x,y,false,ImVec4(0,0,0,0),offset,fmt,args);
  2996. }
  2997. void Annotate(double x, double y, const ImVec2& offset, const char* fmt, ...) {
  2998. va_list args;
  2999. va_start(args, fmt);
  3000. AnnotateV(x,y,offset,fmt,args);
  3001. va_end(args);
  3002. }
  3003. void AnnotateV(double x, double y, const ImVec2& offset, const ImVec4& col, const char* fmt, va_list args) {
  3004. AnnotateEx(x,y,false,col,offset,fmt,args);
  3005. }
  3006. void Annotate(double x, double y, const ImVec2& offset, const ImVec4& col, const char* fmt, ...) {
  3007. va_list args;
  3008. va_start(args, fmt);
  3009. AnnotateV(x,y,offset,col,fmt,args);
  3010. va_end(args);
  3011. }
  3012. void AnnotateClampedV(double x, double y, const ImVec2& offset, const char* fmt, va_list args) {
  3013. AnnotateEx(x,y,true,ImVec4(0,0,0,0),offset,fmt,args);
  3014. }
  3015. void AnnotateClamped(double x, double y, const ImVec2& offset, const char* fmt, ...) {
  3016. va_list args;
  3017. va_start(args, fmt);
  3018. AnnotateClampedV(x,y,offset,fmt,args);
  3019. va_end(args);
  3020. }
  3021. void AnnotateClampedV(double x, double y, const ImVec2& offset, const ImVec4& col, const char* fmt, va_list args) {
  3022. AnnotateEx(x,y,true,col,offset,fmt,args);
  3023. }
  3024. void AnnotateClamped(double x, double y, const ImVec2& offset, const ImVec4& col, const char* fmt, ...) {
  3025. va_list args;
  3026. va_start(args, fmt);
  3027. AnnotateClampedV(x,y,offset,col,fmt,args);
  3028. va_end(args);
  3029. }
  3030. bool DragLineX(const char* id, double* value, bool show_label, const ImVec4& col, float thickness) {
  3031. ImPlotContext& gp = *GImPlot;
  3032. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "DragLineX() needs to be called between BeginPlot() and EndPlot()!");
  3033. const float grab_size = ImMax(5.0f, thickness);
  3034. float yt = gp.CurrentPlot->PlotRect.Min.y;
  3035. float yb = gp.CurrentPlot->PlotRect.Max.y;
  3036. float x = IM_ROUND(PlotToPixels(*value,0).x);
  3037. const bool outside = x < (gp.CurrentPlot->PlotRect.Min.x - grab_size / 2) || x > (gp.CurrentPlot->PlotRect.Max.x + grab_size / 2);
  3038. if (outside)
  3039. return false;
  3040. float len = gp.Style.MajorTickLen.x;
  3041. ImVec4 color = IsColorAuto(col) ? ImGui::GetStyleColorVec4(ImGuiCol_Text) : col;
  3042. ImU32 col32 = ImGui::ColorConvertFloat4ToU32(color);
  3043. ImDrawList& DrawList = *GetPlotDrawList();
  3044. PushPlotClipRect();
  3045. DrawList.AddLine(ImVec2(x,yt), ImVec2(x,yb), col32, thickness);
  3046. DrawList.AddLine(ImVec2(x,yt), ImVec2(x,yt+len), col32, 3*thickness);
  3047. DrawList.AddLine(ImVec2(x,yb), ImVec2(x,yb-len), col32, 3*thickness);
  3048. PopPlotClipRect();
  3049. if (gp.CurrentPlot->Selecting || gp.CurrentPlot->Querying)
  3050. return false;
  3051. ImVec2 old_cursor_pos = ImGui::GetCursorScreenPos();
  3052. ImVec2 new_cursor_pos = ImVec2(x - grab_size / 2.0f, yt);
  3053. ImGui::GetCurrentWindow()->DC.CursorPos = new_cursor_pos;
  3054. ImGui::InvisibleButton(id, ImVec2(grab_size, yb-yt));
  3055. ImGui::GetCurrentWindow()->DC.CursorPos = old_cursor_pos;
  3056. if (ImGui::IsItemHovered() || ImGui::IsItemActive()) {
  3057. gp.CurrentPlot->PlotHovered = false;
  3058. ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeEW);
  3059. if (show_label) {
  3060. char buff[32];
  3061. LabelAxisValue(gp.CurrentPlot->XAxis, gp.XTicks, *value, buff, 32);
  3062. gp.Annotations.Append(ImVec2(x,yb),ImVec2(0,0),col32,CalcTextColor(color),true,"%s = %s", id, buff);
  3063. }
  3064. }
  3065. bool dragging = false;
  3066. if (ImGui::IsItemActive() && ImGui::IsMouseDragging(0)) {
  3067. *value = ImPlot::GetPlotMousePos().x;
  3068. *value = ImClamp(*value, gp.CurrentPlot->XAxis.Range.Min, gp.CurrentPlot->XAxis.Range.Max);
  3069. dragging = true;
  3070. }
  3071. return dragging;
  3072. }
  3073. bool DragLineY(const char* id, double* value, bool show_label, const ImVec4& col, float thickness) {
  3074. ImPlotContext& gp = *GImPlot;
  3075. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "DragLineY() needs to be called between BeginPlot() and EndPlot()!");
  3076. const float grab_size = ImMax(5.0f, thickness);
  3077. float xl = gp.CurrentPlot->PlotRect.Min.x;
  3078. float xr = gp.CurrentPlot->PlotRect.Max.x;
  3079. float y = IM_ROUND(PlotToPixels(0, *value).y);
  3080. const bool outside = y < (gp.CurrentPlot->PlotRect.Min.y - grab_size / 2) || y > (gp.CurrentPlot->PlotRect.Max.y + grab_size / 2);
  3081. if (outside)
  3082. return false;
  3083. float len = gp.Style.MajorTickLen.y;
  3084. ImVec4 color = IsColorAuto(col) ? ImGui::GetStyleColorVec4(ImGuiCol_Text) : col;
  3085. ImU32 col32 = ImGui::ColorConvertFloat4ToU32(color);
  3086. ImDrawList& DrawList = *GetPlotDrawList();
  3087. PushPlotClipRect();
  3088. DrawList.AddLine(ImVec2(xl,y), ImVec2(xr,y), col32, thickness);
  3089. DrawList.AddLine(ImVec2(xl,y), ImVec2(xl+len,y), col32, 3*thickness);
  3090. DrawList.AddLine(ImVec2(xr,y), ImVec2(xr-len,y), col32, 3*thickness);
  3091. PopPlotClipRect();
  3092. if (gp.CurrentPlot->Selecting || gp.CurrentPlot->Querying)
  3093. return false;
  3094. ImVec2 old_cursor_pos = ImGui::GetCursorScreenPos();
  3095. ImVec2 new_cursor_pos = ImVec2(xl, y - grab_size / 2.0f);
  3096. ImGui::SetItemAllowOverlap();
  3097. ImGui::GetCurrentWindow()->DC.CursorPos = new_cursor_pos;
  3098. ImGui::InvisibleButton(id, ImVec2(xr - xl, grab_size));
  3099. ImGui::GetCurrentWindow()->DC.CursorPos = old_cursor_pos;
  3100. int yax = GetCurrentYAxis();
  3101. if (ImGui::IsItemHovered() || ImGui::IsItemActive()) {
  3102. gp.CurrentPlot->PlotHovered = false;
  3103. ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeNS);
  3104. if (show_label) {
  3105. char buff[32];
  3106. LabelAxisValue(gp.CurrentPlot->YAxis[yax], gp.YTicks[yax], *value, buff, 32);
  3107. gp.Annotations.Append(ImVec2(yax == 0 ? xl : xr,y), ImVec2(0,0), col32, CalcTextColor(color), true, "%s = %s", id, buff);
  3108. }
  3109. }
  3110. bool dragging = false;
  3111. if (ImGui::IsItemActive() && ImGui::IsMouseDragging(0)) {
  3112. *value = ImPlot::GetPlotMousePos().y;
  3113. *value = ImClamp(*value, gp.CurrentPlot->YAxis[yax].Range.Min, gp.CurrentPlot->YAxis[yax].Range.Max);
  3114. dragging = true;
  3115. }
  3116. return dragging;
  3117. }
  3118. bool DragPoint(const char* id, double* x, double* y, bool show_label, const ImVec4& col, float radius) {
  3119. ImPlotContext& gp = *GImPlot;
  3120. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "DragPoint() needs to be called between BeginPlot() and EndPlot()!");
  3121. const float grab_size = ImMax(5.0f, 2*radius);
  3122. const bool outside = !GetPlotLimits().Contains(*x,*y);
  3123. if (outside)
  3124. return false;
  3125. const ImVec4 color = IsColorAuto(col) ? ImGui::GetStyleColorVec4(ImGuiCol_Text) : col;
  3126. const ImU32 col32 = ImGui::ColorConvertFloat4ToU32(color);
  3127. ImDrawList& DrawList = *GetPlotDrawList();
  3128. const ImVec2 pos = PlotToPixels(*x,*y);
  3129. int yax = GetCurrentYAxis();
  3130. ImVec2 old_cursor_pos = ImGui::GetCursorScreenPos();
  3131. ImVec2 new_cursor_pos = ImVec2(pos - ImVec2(grab_size,grab_size)*0.5f);
  3132. ImGui::GetCurrentWindow()->DC.CursorPos = new_cursor_pos;
  3133. ImGui::InvisibleButton(id, ImVec2(grab_size, grab_size));
  3134. ImGui::GetCurrentWindow()->DC.CursorPos = old_cursor_pos;
  3135. PushPlotClipRect();
  3136. if (ImGui::IsItemHovered() || ImGui::IsItemActive()) {
  3137. DrawList.AddCircleFilled(pos, 1.5f*radius, (col32));
  3138. gp.CurrentPlot->PlotHovered = false;
  3139. if (show_label) {
  3140. ImVec2 label_pos = pos + ImVec2(16 * GImGui->Style.MouseCursorScale, 8 * GImGui->Style.MouseCursorScale);
  3141. char buff1[32];
  3142. char buff2[32];
  3143. LabelAxisValue(gp.CurrentPlot->XAxis, gp.XTicks, *x, buff1, 32);
  3144. LabelAxisValue(gp.CurrentPlot->YAxis[yax], gp.YTicks[yax], *y, buff2, 32);
  3145. gp.Annotations.Append(label_pos, ImVec2(0.0001f,0.00001f), col32, CalcTextColor(color), true, "%s = %s,%s", id, buff1, buff2);
  3146. }
  3147. }
  3148. else {
  3149. DrawList.AddCircleFilled(pos, radius, col32);
  3150. }
  3151. PopPlotClipRect();
  3152. bool dragging = false;
  3153. if (ImGui::IsItemActive() && ImGui::IsMouseDragging(0)) {
  3154. *x = ImPlot::GetPlotMousePos().x;
  3155. *y = ImPlot::GetPlotMousePos().y;
  3156. *x = ImClamp(*x, gp.CurrentPlot->XAxis.Range.Min, gp.CurrentPlot->XAxis.Range.Max);
  3157. *y = ImClamp(*y, gp.CurrentPlot->YAxis[yax].Range.Min, gp.CurrentPlot->YAxis[yax].Range.Max);
  3158. dragging = true;
  3159. }
  3160. return dragging;
  3161. }
  3162. //-----------------------------------------------------------------------------
  3163. #define IMPLOT_ID_PLT 10030910
  3164. #define IMPLOT_ID_LEG 10030911
  3165. #define IMPLOT_ID_XAX 10030912
  3166. #define IMPLOT_ID_YAX 10030913
  3167. #define IMPLOT_ID_ITM 10030914
  3168. bool BeginDragDropTargetEx(int id, const ImRect& rect) {
  3169. ImGuiContext& G = *GImGui;
  3170. const ImGuiID ID = G.CurrentWindow->GetID(id);
  3171. if (ImGui::ItemAdd(rect, ID, &rect) &&
  3172. ImGui::BeginDragDropTarget())
  3173. return true;
  3174. return false;
  3175. }
  3176. bool BeginDragDropTarget() {
  3177. return BeginDragDropTargetEx(IMPLOT_ID_PLT, GImPlot->CurrentPlot->PlotRect);
  3178. }
  3179. bool BeginDragDropTargetX() {
  3180. return BeginDragDropTargetEx(IMPLOT_ID_XAX, GImPlot->CurrentPlot->XAxis.HoverRect);
  3181. }
  3182. bool BeginDragDropTargetY(ImPlotYAxis axis) {
  3183. return BeginDragDropTargetEx(IMPLOT_ID_YAX + axis, GImPlot->CurrentPlot->YAxis[axis].HoverRect);
  3184. }
  3185. bool BeginDragDropTargetLegend() {
  3186. return BeginDragDropTargetEx(IMPLOT_ID_LEG, GImPlot->CurrentItems->Legend.Rect);
  3187. }
  3188. void EndDragDropTarget() {
  3189. ImGui::EndDragDropTarget();
  3190. }
  3191. bool BeginDragDropSourceEx(ImGuiID source_id, bool is_hovered, ImGuiDragDropFlags flags, ImGuiKeyModFlags key_mods) {
  3192. ImGuiContext& g = *GImGui;
  3193. ImGuiWindow* window = g.CurrentWindow;
  3194. ImGuiMouseButton mouse_button = ImGuiMouseButton_Left;
  3195. if (g.IO.MouseDown[mouse_button] == false) {
  3196. if (g.ActiveId == source_id)
  3197. ImGui::ClearActiveID();
  3198. return false;
  3199. }
  3200. if (is_hovered && g.IO.MouseClicked[mouse_button] && g.IO.KeyMods == key_mods) {
  3201. ImGui::SetActiveID(source_id, window);
  3202. ImGui::FocusWindow(window);
  3203. }
  3204. if (g.ActiveId != source_id) {
  3205. return false;
  3206. }
  3207. g.ActiveIdAllowOverlap = is_hovered;
  3208. g.ActiveIdUsingNavDirMask = ~(ImU32)0;
  3209. g.ActiveIdUsingNavInputMask = ~(ImU32)0;
  3210. g.ActiveIdUsingKeyInputMask = ~(ImU64)0;
  3211. if (ImGui::IsMouseDragging(mouse_button)) {
  3212. if (!g.DragDropActive) {
  3213. ImGui::ClearDragDrop();
  3214. ImGuiPayload& payload = g.DragDropPayload;
  3215. payload.SourceId = source_id;
  3216. payload.SourceParentId = 0;
  3217. g.DragDropActive = true;
  3218. g.DragDropSourceFlags = 0;
  3219. g.DragDropMouseButton = mouse_button;
  3220. }
  3221. g.DragDropSourceFrameCount = g.FrameCount;
  3222. g.DragDropWithinSource = true;
  3223. if (!(flags & ImGuiDragDropFlags_SourceNoPreviewTooltip)) {
  3224. ImGui::BeginTooltip();
  3225. if (g.DragDropAcceptIdPrev && (g.DragDropAcceptFlags & ImGuiDragDropFlags_AcceptNoPreviewTooltip)) {
  3226. ImGuiWindow* tooltip_window = g.CurrentWindow;
  3227. tooltip_window->SkipItems = true;
  3228. tooltip_window->HiddenFramesCanSkipItems = 1;
  3229. }
  3230. }
  3231. return true;
  3232. }
  3233. return false;
  3234. }
  3235. bool BeginDragDropSource(ImGuiKeyModFlags key_mods, ImGuiDragDropFlags flags) {
  3236. if (ImGui::GetIO().KeyMods == key_mods) {
  3237. GImPlot->CurrentPlot->XAxis.Dragging = false;
  3238. for (int i = 0; i < IMPLOT_Y_AXES; ++i)
  3239. GImPlot->CurrentPlot->YAxis[i].Dragging = false;
  3240. }
  3241. const ImGuiID ID = GImGui->CurrentWindow->GetID(IMPLOT_ID_PLT);
  3242. ImRect rect = GImPlot->CurrentPlot->PlotRect;
  3243. return ImGui::ItemAdd(rect, ID, &rect) && BeginDragDropSourceEx(ID, GImPlot->CurrentPlot->PlotHovered, flags, key_mods);
  3244. }
  3245. bool BeginDragDropSourceX(ImGuiKeyModFlags key_mods, ImGuiDragDropFlags flags) {
  3246. if (ImGui::GetIO().KeyMods == key_mods)
  3247. GImPlot->CurrentPlot->XAxis.Dragging = false;
  3248. const ImGuiID ID = GImGui->CurrentWindow->GetID(IMPLOT_ID_XAX);
  3249. ImRect rect = GImPlot->CurrentPlot->XAxis.HoverRect;
  3250. return ImGui::ItemAdd(rect, ID, &rect) && BeginDragDropSourceEx(ID, GImPlot->CurrentPlot->XAxis.ExtHovered, flags, key_mods);
  3251. }
  3252. bool BeginDragDropSourceY(ImPlotYAxis axis, ImGuiKeyModFlags key_mods, ImGuiDragDropFlags flags) {
  3253. if (ImGui::GetIO().KeyMods == key_mods)
  3254. GImPlot->CurrentPlot->YAxis[axis].Dragging = false;
  3255. const ImGuiID ID = GImGui->CurrentWindow->GetID(IMPLOT_ID_YAX + axis);
  3256. ImRect rect = GImPlot->CurrentPlot->YAxis[axis].HoverRect;
  3257. return ImGui::ItemAdd(rect, ID, &rect) && BeginDragDropSourceEx(ID, GImPlot->CurrentPlot->YAxis[axis].ExtHovered, flags, key_mods);
  3258. }
  3259. bool BeginDragDropSourceItem(const char* label_id, ImGuiDragDropFlags flags) {
  3260. ImPlotContext& gp = *GImPlot;
  3261. IM_ASSERT_USER_ERROR(gp.CurrentItems != NULL, "BeginDragDropSourceItem() needs to be called within an itemized context!");
  3262. ImGuiID item_id = ImGui::GetIDWithSeed(label_id, NULL, gp.CurrentItems->ID);
  3263. ImPlotItem* item = gp.CurrentItems->GetItem(item_id);
  3264. bool is_hovered = item && item->LegendHovered;
  3265. ImGuiID temp_id = ImGui::GetIDWithSeed("dnd",NULL,item->ID); // total hack
  3266. return BeginDragDropSourceEx(temp_id, is_hovered, flags, ImGuiKeyModFlags_None);
  3267. }
  3268. void EndDragDropSource() {
  3269. ImGui::EndDragDropSource();
  3270. }
  3271. void ItemIcon(const ImVec4& col) {
  3272. ItemIcon(ImGui::ColorConvertFloat4ToU32(col));
  3273. }
  3274. void ItemIcon(ImU32 col) {
  3275. const float txt_size = ImGui::GetTextLineHeight();
  3276. ImVec2 size(txt_size-4,txt_size);
  3277. ImGuiWindow* window = ImGui::GetCurrentWindow();
  3278. ImVec2 pos = window->DC.CursorPos;
  3279. ImGui::GetWindowDrawList()->AddRectFilled(pos + ImVec2(0,2), pos + size - ImVec2(0,2), col);
  3280. ImGui::Dummy(size);
  3281. }
  3282. void ColormapIcon(ImPlotColormap cmap) {
  3283. ImPlotContext& gp = *GImPlot;
  3284. const float txt_size = ImGui::GetTextLineHeight();
  3285. ImVec2 size(txt_size-4,txt_size);
  3286. ImGuiWindow* window = ImGui::GetCurrentWindow();
  3287. ImVec2 pos = window->DC.CursorPos;
  3288. ImRect rect(pos+ImVec2(0,2),pos+size-ImVec2(0,2));
  3289. ImDrawList& DrawList = *ImGui::GetWindowDrawList();
  3290. RenderColorBar(gp.ColormapData.GetKeys(cmap),gp.ColormapData.GetKeyCount(cmap),DrawList,rect,false,false,!gp.ColormapData.IsQual(cmap));
  3291. ImGui::Dummy(size);
  3292. }
  3293. //-----------------------------------------------------------------------------
  3294. void SetLegendLocation(ImPlotLocation location, ImPlotOrientation orientation, bool outside) {
  3295. ImPlotContext& gp = *GImPlot;
  3296. IM_ASSERT_USER_ERROR(gp.CurrentItems != NULL, "SetLegendLocation() needs to be called within an itemized context!");
  3297. gp.CurrentItems->Legend.Location = location;
  3298. gp.CurrentItems->Legend.Orientation = orientation;
  3299. if (gp.CurrentItems->Legend.Outside != outside)
  3300. gp.CurrentItems->Legend.FlipSideNextFrame = true;
  3301. }
  3302. void SetMousePosLocation(ImPlotLocation location) {
  3303. ImPlotContext& gp = *GImPlot;
  3304. IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "SetMousePosLocation() needs to be called between BeginPlot() and EndPlot()!");
  3305. gp.CurrentPlot->MousePosLocation = location;
  3306. }
  3307. bool IsLegendEntryHovered(const char* label_id) {
  3308. ImPlotContext& gp = *GImPlot;
  3309. IM_ASSERT_USER_ERROR(gp.CurrentItems != NULL, "IsPlotItemHighlight() needs to be called within an itemized context!");
  3310. ImGuiID id = ImGui::GetIDWithSeed(label_id, NULL, gp.CurrentItems->ID);
  3311. ImPlotItem* item = gp.CurrentItems->GetItem(id);
  3312. return item && item->LegendHovered;
  3313. }
  3314. bool BeginLegendPopup(const char* label_id, ImGuiMouseButton mouse_button) {
  3315. ImPlotContext& gp = *GImPlot;
  3316. IM_ASSERT_USER_ERROR(gp.CurrentItems != NULL, "BeginLegendPopup() needs to be called within an itemized context!");
  3317. ImGuiWindow* window = GImGui->CurrentWindow;
  3318. if (window->SkipItems)
  3319. return false;
  3320. ImGuiID id = ImGui::GetIDWithSeed(label_id, NULL, gp.CurrentItems->ID);
  3321. if (ImGui::IsMouseReleased(mouse_button)) {
  3322. ImPlotItem* item = gp.CurrentItems->GetItem(id);
  3323. if (item && item->LegendHovered)
  3324. ImGui::OpenPopupEx(id);
  3325. }
  3326. return ImGui::BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings);
  3327. }
  3328. void EndLegendPopup() {
  3329. ImGui::EndPopup();
  3330. }
  3331. void ShowAltLegend(const char* title_id, ImPlotOrientation orientation, const ImVec2 size, bool interactable) {
  3332. ImPlotContext& gp = *GImPlot;
  3333. ImGuiContext &G = *GImGui;
  3334. ImGuiWindow * Window = G.CurrentWindow;
  3335. if (Window->SkipItems)
  3336. return;
  3337. ImDrawList &DrawList = *Window->DrawList;
  3338. ImPlotPlot* plot = GetPlot(title_id);
  3339. ImVec2 legend_size;
  3340. ImVec2 default_size = gp.Style.LegendPadding * 2;
  3341. if (plot != NULL) {
  3342. legend_size = CalcLegendSize(plot->Items, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, orientation);
  3343. default_size = legend_size + gp.Style.LegendPadding * 2;
  3344. }
  3345. ImVec2 frame_size = ImGui::CalcItemSize(size, default_size.x, default_size.y);
  3346. ImRect bb_frame = ImRect(Window->DC.CursorPos, Window->DC.CursorPos + frame_size);
  3347. ImGui::ItemSize(bb_frame);
  3348. if (!ImGui::ItemAdd(bb_frame, 0, &bb_frame))
  3349. return;
  3350. ImGui::RenderFrame(bb_frame.Min, bb_frame.Max, GetStyleColorU32(ImPlotCol_FrameBg), true, G.Style.FrameRounding);
  3351. DrawList.PushClipRect(bb_frame.Min, bb_frame.Max, true);
  3352. if (plot != NULL) {
  3353. const ImVec2 legend_pos = GetLocationPos(bb_frame, legend_size, 0, gp.Style.LegendPadding);
  3354. const ImRect legend_bb(legend_pos, legend_pos + legend_size);
  3355. interactable = interactable && bb_frame.Contains(ImGui::GetIO().MousePos);
  3356. // render legend box
  3357. ImU32 col_bg = GetStyleColorU32(ImPlotCol_LegendBg);
  3358. ImU32 col_bd = GetStyleColorU32(ImPlotCol_LegendBorder);
  3359. DrawList.AddRectFilled(legend_bb.Min, legend_bb.Max, col_bg);
  3360. DrawList.AddRect(legend_bb.Min, legend_bb.Max, col_bd);
  3361. // render entries
  3362. ShowLegendEntries(plot->Items, legend_bb, interactable, gp.Style.LegendInnerPadding, gp.Style.LegendSpacing, orientation, DrawList);
  3363. }
  3364. DrawList.PopClipRect();
  3365. }
  3366. //-----------------------------------------------------------------------------
  3367. // STYLING
  3368. //-----------------------------------------------------------------------------
  3369. ImPlotStyle& GetStyle() {
  3370. ImPlotContext& gp = *GImPlot;
  3371. return gp.Style;
  3372. }
  3373. void PushStyleColor(ImPlotCol idx, ImU32 col) {
  3374. ImPlotContext& gp = *GImPlot;
  3375. ImGuiColorMod backup;
  3376. backup.Col = idx;
  3377. backup.BackupValue = gp.Style.Colors[idx];
  3378. gp.ColorModifiers.push_back(backup);
  3379. gp.Style.Colors[idx] = ImGui::ColorConvertU32ToFloat4(col);
  3380. }
  3381. void PushStyleColor(ImPlotCol idx, const ImVec4& col) {
  3382. ImPlotContext& gp = *GImPlot;
  3383. ImGuiColorMod backup;
  3384. backup.Col = idx;
  3385. backup.BackupValue = gp.Style.Colors[idx];
  3386. gp.ColorModifiers.push_back(backup);
  3387. gp.Style.Colors[idx] = col;
  3388. }
  3389. void PopStyleColor(int count) {
  3390. ImPlotContext& gp = *GImPlot;
  3391. IM_ASSERT_USER_ERROR(count <= gp.ColorModifiers.Size, "You can't pop more modifiers than have been pushed!");
  3392. while (count > 0)
  3393. {
  3394. ImGuiColorMod& backup = gp.ColorModifiers.back();
  3395. gp.Style.Colors[backup.Col] = backup.BackupValue;
  3396. gp.ColorModifiers.pop_back();
  3397. count--;
  3398. }
  3399. }
  3400. void PushStyleVar(ImPlotStyleVar idx, float val) {
  3401. ImPlotContext& gp = *GImPlot;
  3402. const ImPlotStyleVarInfo* var_info = GetPlotStyleVarInfo(idx);
  3403. if (var_info->Type == ImGuiDataType_Float && var_info->Count == 1) {
  3404. float* pvar = (float*)var_info->GetVarPtr(&gp.Style);
  3405. gp.StyleModifiers.push_back(ImGuiStyleMod(idx, *pvar));
  3406. *pvar = val;
  3407. return;
  3408. }
  3409. IM_ASSERT(0 && "Called PushStyleVar() float variant but variable is not a float!");
  3410. }
  3411. void PushStyleVar(ImPlotStyleVar idx, int val) {
  3412. ImPlotContext& gp = *GImPlot;
  3413. const ImPlotStyleVarInfo* var_info = GetPlotStyleVarInfo(idx);
  3414. if (var_info->Type == ImGuiDataType_S32 && var_info->Count == 1) {
  3415. int* pvar = (int*)var_info->GetVarPtr(&gp.Style);
  3416. gp.StyleModifiers.push_back(ImGuiStyleMod(idx, *pvar));
  3417. *pvar = val;
  3418. return;
  3419. }
  3420. else if (var_info->Type == ImGuiDataType_Float && var_info->Count == 1) {
  3421. float* pvar = (float*)var_info->GetVarPtr(&gp.Style);
  3422. gp.StyleModifiers.push_back(ImGuiStyleMod(idx, *pvar));
  3423. *pvar = (float)val;
  3424. return;
  3425. }
  3426. IM_ASSERT(0 && "Called PushStyleVar() int variant but variable is not a int!");
  3427. }
  3428. void PushStyleVar(ImGuiStyleVar idx, const ImVec2& val)
  3429. {
  3430. ImPlotContext& gp = *GImPlot;
  3431. const ImPlotStyleVarInfo* var_info = GetPlotStyleVarInfo(idx);
  3432. if (var_info->Type == ImGuiDataType_Float && var_info->Count == 2)
  3433. {
  3434. ImVec2* pvar = (ImVec2*)var_info->GetVarPtr(&gp.Style);
  3435. gp.StyleModifiers.push_back(ImGuiStyleMod(idx, *pvar));
  3436. *pvar = val;
  3437. return;
  3438. }
  3439. IM_ASSERT(0 && "Called PushStyleVar() ImVec2 variant but variable is not a ImVec2!");
  3440. }
  3441. void PopStyleVar(int count) {
  3442. ImPlotContext& gp = *GImPlot;
  3443. IM_ASSERT_USER_ERROR(count <= gp.StyleModifiers.Size, "You can't pop more modifiers than have been pushed!");
  3444. while (count > 0) {
  3445. ImGuiStyleMod& backup = gp.StyleModifiers.back();
  3446. const ImPlotStyleVarInfo* info = GetPlotStyleVarInfo(backup.VarIdx);
  3447. void* data = info->GetVarPtr(&gp.Style);
  3448. if (info->Type == ImGuiDataType_Float && info->Count == 1) {
  3449. ((float*)data)[0] = backup.BackupFloat[0];
  3450. }
  3451. else if (info->Type == ImGuiDataType_Float && info->Count == 2) {
  3452. ((float*)data)[0] = backup.BackupFloat[0];
  3453. ((float*)data)[1] = backup.BackupFloat[1];
  3454. }
  3455. else if (info->Type == ImGuiDataType_S32 && info->Count == 1) {
  3456. ((int*)data)[0] = backup.BackupInt[0];
  3457. }
  3458. gp.StyleModifiers.pop_back();
  3459. count--;
  3460. }
  3461. }
  3462. //------------------------------------------------------------------------------
  3463. // COLORMAPS
  3464. //------------------------------------------------------------------------------
  3465. ImPlotColormap AddColormap(const char* name, const ImVec4* colormap, int size, bool qual) {
  3466. ImPlotContext& gp = *GImPlot;
  3467. IM_ASSERT_USER_ERROR(size > 1, "The colormap size must be greater than 1!");
  3468. IM_ASSERT_USER_ERROR(gp.ColormapData.GetIndex(name) == -1, "The colormap name has already been used!");
  3469. ImVector<ImU32> buffer;
  3470. buffer.resize(size);
  3471. for (int i = 0; i < size; ++i)
  3472. buffer[i] = ImGui::ColorConvertFloat4ToU32(colormap[i]);
  3473. return gp.ColormapData.Append(name, buffer.Data, size, qual);
  3474. }
  3475. ImPlotColormap AddColormap(const char* name, const ImU32* colormap, int size, bool qual) {
  3476. ImPlotContext& gp = *GImPlot;
  3477. IM_ASSERT_USER_ERROR(size > 1, "The colormap size must be greater than 1!");
  3478. IM_ASSERT_USER_ERROR(gp.ColormapData.GetIndex(name) == -1, "The colormap name has already be used!");
  3479. return gp.ColormapData.Append(name, colormap, size, qual);
  3480. }
  3481. int GetColormapCount() {
  3482. ImPlotContext& gp = *GImPlot;
  3483. return gp.ColormapData.Count;
  3484. }
  3485. const char* GetColormapName(ImPlotColormap colormap) {
  3486. ImPlotContext& gp = *GImPlot;
  3487. return gp.ColormapData.GetName(colormap);
  3488. }
  3489. ImPlotColormap GetColormapIndex(const char* name) {
  3490. ImPlotContext& gp = *GImPlot;
  3491. return gp.ColormapData.GetIndex(name);
  3492. }
  3493. void PushColormap(ImPlotColormap colormap) {
  3494. ImPlotContext& gp = *GImPlot;
  3495. IM_ASSERT_USER_ERROR(colormap >= 0 && colormap < gp.ColormapData.Count, "The colormap index is invalid!");
  3496. gp.ColormapModifiers.push_back(gp.Style.Colormap);
  3497. gp.Style.Colormap = colormap;
  3498. }
  3499. void PushColormap(const char* name) {
  3500. ImPlotContext& gp = *GImPlot;
  3501. ImPlotColormap idx = gp.ColormapData.GetIndex(name);
  3502. IM_ASSERT_USER_ERROR(idx != -1, "The colormap name is invalid!");
  3503. PushColormap(idx);
  3504. }
  3505. void PopColormap(int count) {
  3506. ImPlotContext& gp = *GImPlot;
  3507. IM_ASSERT_USER_ERROR(count <= gp.ColormapModifiers.Size, "You can't pop more modifiers than have been pushed!");
  3508. while (count > 0) {
  3509. const ImPlotColormap& backup = gp.ColormapModifiers.back();
  3510. gp.Style.Colormap = backup;
  3511. gp.ColormapModifiers.pop_back();
  3512. count--;
  3513. }
  3514. }
  3515. ImU32 NextColormapColorU32() {
  3516. ImPlotContext& gp = *GImPlot;
  3517. IM_ASSERT_USER_ERROR(gp.CurrentItems != NULL, "NextColormapColor() needs to be called between BeginPlot() and EndPlot()!");
  3518. int idx = gp.CurrentItems->ColormapIdx % gp.ColormapData.GetKeyCount(gp.Style.Colormap);
  3519. ImU32 col = gp.ColormapData.GetKeyColor(gp.Style.Colormap, idx);
  3520. gp.CurrentItems->ColormapIdx++;
  3521. return col;
  3522. }
  3523. ImVec4 NextColormapColor() {
  3524. return ImGui::ColorConvertU32ToFloat4(NextColormapColorU32());
  3525. }
  3526. int GetColormapSize(ImPlotColormap cmap) {
  3527. ImPlotContext& gp = *GImPlot;
  3528. cmap = cmap == IMPLOT_AUTO ? gp.Style.Colormap : cmap;
  3529. IM_ASSERT_USER_ERROR(cmap >= 0 && cmap < gp.ColormapData.Count, "Invalid colormap index!");
  3530. return gp.ColormapData.GetKeyCount(cmap);
  3531. }
  3532. ImU32 GetColormapColorU32(int idx, ImPlotColormap cmap) {
  3533. ImPlotContext& gp = *GImPlot;
  3534. cmap = cmap == IMPLOT_AUTO ? gp.Style.Colormap : cmap;
  3535. IM_ASSERT_USER_ERROR(cmap >= 0 && cmap < gp.ColormapData.Count, "Invalid colormap index!");
  3536. idx = idx % gp.ColormapData.GetKeyCount(cmap);
  3537. return gp.ColormapData.GetKeyColor(cmap, idx);
  3538. }
  3539. ImVec4 GetColormapColor(int idx, ImPlotColormap cmap) {
  3540. return ImGui::ColorConvertU32ToFloat4(GetColormapColorU32(idx,cmap));
  3541. }
  3542. ImU32 SampleColormapU32(float t, ImPlotColormap cmap) {
  3543. ImPlotContext& gp = *GImPlot;
  3544. cmap = cmap == IMPLOT_AUTO ? gp.Style.Colormap : cmap;
  3545. IM_ASSERT_USER_ERROR(cmap >= 0 && cmap < gp.ColormapData.Count, "Invalid colormap index!");
  3546. return gp.ColormapData.LerpTable(cmap, t);
  3547. }
  3548. ImVec4 SampleColormap(float t, ImPlotColormap cmap) {
  3549. return ImGui::ColorConvertU32ToFloat4(SampleColormapU32(t,cmap));
  3550. }
  3551. void RenderColorBar(const ImU32* colors, int size, ImDrawList& DrawList, const ImRect& bounds, bool vert, bool reversed, bool continuous) {
  3552. const int n = continuous ? size - 1 : size;
  3553. ImU32 col1, col2;
  3554. if (vert) {
  3555. const float step = bounds.GetHeight() / n;
  3556. ImRect rect(bounds.Min.x, bounds.Min.y, bounds.Max.x, bounds.Min.y + step);
  3557. for (int i = 0; i < n; ++i) {
  3558. if (reversed) {
  3559. col1 = colors[size-i-1];
  3560. col2 = continuous ? colors[size-i-2] : col1;
  3561. }
  3562. else {
  3563. col1 = colors[i];
  3564. col2 = continuous ? colors[i+1] : col1;
  3565. }
  3566. DrawList.AddRectFilledMultiColor(rect.Min, rect.Max, col1, col1, col2, col2);
  3567. rect.TranslateY(step);
  3568. }
  3569. }
  3570. else {
  3571. const float step = bounds.GetWidth() / n;
  3572. ImRect rect(bounds.Min.x, bounds.Min.y, bounds.Min.x + step, bounds.Max.y);
  3573. for (int i = 0; i < n; ++i) {
  3574. if (reversed) {
  3575. col1 = colors[size-i-1];
  3576. col2 = continuous ? colors[size-i-2] : col1;
  3577. }
  3578. else {
  3579. col1 = colors[i];
  3580. col2 = continuous ? colors[i+1] : col1;
  3581. }
  3582. DrawList.AddRectFilledMultiColor(rect.Min, rect.Max, col1, col2, col2, col1);
  3583. rect.TranslateX(step);
  3584. }
  3585. }
  3586. }
  3587. void ColormapScale(const char* label, double scale_min, double scale_max, const ImVec2& size, ImPlotColormap cmap, const char* fmt) {
  3588. ImGuiContext &G = *GImGui;
  3589. ImGuiWindow * Window = G.CurrentWindow;
  3590. if (Window->SkipItems)
  3591. return;
  3592. const ImGuiID ID = Window->GetID(label);
  3593. ImVec2 label_size(0,0);
  3594. label_size = ImGui::CalcTextSize(label,NULL,true);
  3595. ImPlotContext& gp = *GImPlot;
  3596. cmap = cmap == IMPLOT_AUTO ? gp.Style.Colormap : cmap;
  3597. IM_ASSERT_USER_ERROR(cmap >= 0 && cmap < gp.ColormapData.Count, "Invalid colormap index!");
  3598. ImVec2 frame_size = ImGui::CalcItemSize(size, 0, gp.Style.PlotDefaultSize.y);
  3599. if (frame_size.y < gp.Style.PlotMinSize.y && size.y < 0.0f)
  3600. frame_size.y = gp.Style.PlotMinSize.y;
  3601. ImPlotRange range(scale_min,scale_max);
  3602. gp.CTicks.Reset();
  3603. AddTicksDefault(range, frame_size.y, ImPlotOrientation_Vertical, gp.CTicks, fmt);
  3604. const float txt_off = gp.Style.LabelPadding.x;
  3605. const float pad_right = txt_off + gp.CTicks.MaxWidth + (label_size.x > 0 ? txt_off + label_size.y : 0);
  3606. float bar_w = 20;
  3607. if (frame_size.x == 0)
  3608. frame_size.x = bar_w + pad_right + 2 * gp.Style.PlotPadding.x;
  3609. else {
  3610. bar_w = frame_size.x - (pad_right + 2 * gp.Style.PlotPadding.x);
  3611. if (bar_w < gp.Style.MajorTickLen.y)
  3612. bar_w = gp.Style.MajorTickLen.y;
  3613. }
  3614. ImDrawList &DrawList = *Window->DrawList;
  3615. ImRect bb_frame = ImRect(Window->DC.CursorPos, Window->DC.CursorPos + frame_size);
  3616. ImGui::ItemSize(bb_frame);
  3617. if (!ImGui::ItemAdd(bb_frame, ID, &bb_frame))
  3618. return;
  3619. ImGui::RenderFrame(bb_frame.Min, bb_frame.Max, GetStyleColorU32(ImPlotCol_FrameBg), true, G.Style.FrameRounding);
  3620. ImRect bb_grad(bb_frame.Min + gp.Style.PlotPadding, bb_frame.Min + ImVec2(bar_w + gp.Style.PlotPadding.x, frame_size.y - gp.Style.PlotPadding.y));
  3621. ImGui::PushClipRect(bb_frame.Min, bb_frame.Max, true);
  3622. RenderColorBar(gp.ColormapData.GetKeys(cmap), gp.ColormapData.GetKeyCount(cmap), DrawList, bb_grad, true, true, !gp.ColormapData.IsQual(cmap));
  3623. const ImU32 col_tick = GetStyleColorU32(ImPlotCol_YAxis);
  3624. const ImU32 col_text = ImGui::GetColorU32(ImGuiCol_Text);
  3625. for (int i = 0; i < gp.CTicks.Size; ++i) {
  3626. const float ypos = ImRemap((float)gp.CTicks.Ticks[i].PlotPos, (float)range.Max, (float)range.Min, bb_grad.Min.y, bb_grad.Max.y);
  3627. const float tick_width = gp.CTicks.Ticks[i].Major ? gp.Style.MajorTickLen.y : gp.Style.MinorTickLen.y;
  3628. const float tick_thick = gp.CTicks.Ticks[i].Major ? gp.Style.MajorTickSize.y : gp.Style.MinorTickSize.y;
  3629. if (ypos < bb_grad.Max.y - 2 && ypos > bb_grad.Min.y + 2)
  3630. DrawList.AddLine(ImVec2(bb_grad.Max.x-1, ypos), ImVec2(bb_grad.Max.x - tick_width, ypos), col_tick, tick_thick);
  3631. DrawList.AddText(ImVec2(bb_grad.Max.x-1, ypos) + ImVec2(txt_off, -gp.CTicks.Ticks[i].LabelSize.y * 0.5f), col_text, gp.CTicks.GetText(i));
  3632. }
  3633. if (label_size.x > 0) {
  3634. ImVec2 label_pos(bb_grad.Max.x - 1 + 2*txt_off + gp.CTicks.MaxWidth, bb_grad.GetCenter().y + label_size.x*0.5f );
  3635. const char* label_end = ImGui::FindRenderedTextEnd(label);
  3636. AddTextVertical(&DrawList,label_pos,col_text,label,label_end);
  3637. }
  3638. DrawList.AddRect(bb_grad.Min, bb_grad.Max, GetStyleColorU32(ImPlotCol_PlotBorder));
  3639. ImGui::PopClipRect();
  3640. }
  3641. bool ColormapSlider(const char* label, float* t, ImVec4* out, const char* format, ImPlotColormap cmap) {
  3642. *t = ImClamp(*t,0.0f,1.0f);
  3643. ImGuiContext &G = *GImGui;
  3644. ImGuiWindow * Window = G.CurrentWindow;
  3645. if (Window->SkipItems)
  3646. return false;
  3647. ImPlotContext& gp = *GImPlot;
  3648. cmap = cmap == IMPLOT_AUTO ? gp.Style.Colormap : cmap;
  3649. IM_ASSERT_USER_ERROR(cmap >= 0 && cmap < gp.ColormapData.Count, "Invalid colormap index!");
  3650. const ImU32* keys = GImPlot->ColormapData.GetKeys(cmap);
  3651. const int count = GImPlot->ColormapData.GetKeyCount(cmap);
  3652. const bool qual = GImPlot->ColormapData.IsQual(cmap);
  3653. const ImVec2 pos = ImGui::GetCurrentWindow()->DC.CursorPos;
  3654. const float w = ImGui::CalcItemWidth();
  3655. const float h = ImGui::GetFrameHeight();
  3656. const ImRect rect = ImRect(pos.x,pos.y,pos.x+w,pos.y+h);
  3657. RenderColorBar(keys,count,*ImGui::GetWindowDrawList(),rect,false,false,!qual);
  3658. const ImU32 grab = CalcTextColor(GImPlot->ColormapData.LerpTable(cmap,*t));
  3659. // const ImU32 text = CalcTextColor(GImPlot->ColormapData.LerpTable(cmap,0.5f));
  3660. ImGui::PushStyleColor(ImGuiCol_FrameBg,IM_COL32_BLACK_TRANS);
  3661. ImGui::PushStyleColor(ImGuiCol_FrameBgActive,IM_COL32_BLACK_TRANS);
  3662. ImGui::PushStyleColor(ImGuiCol_FrameBgHovered,ImVec4(1,1,1,0.1f));
  3663. ImGui::PushStyleColor(ImGuiCol_SliderGrab,grab);
  3664. ImGui::PushStyleColor(ImGuiCol_SliderGrabActive, grab);
  3665. ImGui::PushStyleVar(ImGuiStyleVar_GrabMinSize,2);
  3666. ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding,0);
  3667. const bool changed = ImGui::SliderFloat(label,t,0,1,format);
  3668. ImGui::PopStyleColor(5);
  3669. ImGui::PopStyleVar(2);
  3670. if (out != NULL)
  3671. *out = ImGui::ColorConvertU32ToFloat4(GImPlot->ColormapData.LerpTable(cmap,*t));
  3672. return changed;
  3673. }
  3674. bool ColormapButton(const char* label, const ImVec2& size_arg, ImPlotColormap cmap) {
  3675. ImGuiContext &G = *GImGui;
  3676. const ImGuiStyle& style = G.Style;
  3677. ImGuiWindow * Window = G.CurrentWindow;
  3678. if (Window->SkipItems)
  3679. return false;
  3680. ImPlotContext& gp = *GImPlot;
  3681. cmap = cmap == IMPLOT_AUTO ? gp.Style.Colormap : cmap;
  3682. IM_ASSERT_USER_ERROR(cmap >= 0 && cmap < gp.ColormapData.Count, "Invalid colormap index!");
  3683. const ImU32* keys = GImPlot->ColormapData.GetKeys(cmap);
  3684. const int count = GImPlot->ColormapData.GetKeyCount(cmap);
  3685. const bool qual = GImPlot->ColormapData.IsQual(cmap);
  3686. const ImVec2 pos = ImGui::GetCurrentWindow()->DC.CursorPos;
  3687. const ImVec2 label_size = ImGui::CalcTextSize(label, NULL, true);
  3688. ImVec2 size = ImGui::CalcItemSize(size_arg, label_size.x + style.FramePadding.x * 2.0f, label_size.y + style.FramePadding.y * 2.0f);
  3689. const ImRect rect = ImRect(pos.x,pos.y,pos.x+size.x,pos.y+size.y);
  3690. RenderColorBar(keys,count,*ImGui::GetWindowDrawList(),rect,false,false,!qual);
  3691. const ImU32 text = CalcTextColor(GImPlot->ColormapData.LerpTable(cmap,G.Style.ButtonTextAlign.x));
  3692. ImGui::PushStyleColor(ImGuiCol_Button,IM_COL32_BLACK_TRANS);
  3693. ImGui::PushStyleColor(ImGuiCol_ButtonHovered,ImVec4(1,1,1,0.1f));
  3694. ImGui::PushStyleColor(ImGuiCol_ButtonActive,ImVec4(1,1,1,0.2f));
  3695. ImGui::PushStyleColor(ImGuiCol_Text,text);
  3696. ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding,0);
  3697. const bool pressed = ImGui::Button(label,size);
  3698. ImGui::PopStyleColor(4);
  3699. ImGui::PopStyleVar(1);
  3700. return pressed;
  3701. }
  3702. //-----------------------------------------------------------------------------
  3703. // Style Editor etc.
  3704. //-----------------------------------------------------------------------------
  3705. static void HelpMarker(const char* desc) {
  3706. ImGui::TextDisabled("(?)");
  3707. if (ImGui::IsItemHovered()) {
  3708. ImGui::BeginTooltip();
  3709. ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
  3710. ImGui::TextUnformatted(desc);
  3711. ImGui::PopTextWrapPos();
  3712. ImGui::EndTooltip();
  3713. }
  3714. }
  3715. bool ShowStyleSelector(const char* label)
  3716. {
  3717. static int style_idx = -1;
  3718. if (ImGui::Combo(label, &style_idx, "Auto\0Classic\0Dark\0Light\0"))
  3719. {
  3720. switch (style_idx)
  3721. {
  3722. case 0: StyleColorsAuto(); break;
  3723. case 1: StyleColorsClassic(); break;
  3724. case 2: StyleColorsDark(); break;
  3725. case 3: StyleColorsLight(); break;
  3726. }
  3727. return true;
  3728. }
  3729. return false;
  3730. }
  3731. bool ShowColormapSelector(const char* label) {
  3732. ImPlotContext& gp = *GImPlot;
  3733. bool set = false;
  3734. if (ImGui::BeginCombo(label, gp.ColormapData.GetName(gp.Style.Colormap))) {
  3735. for (int i = 0; i < gp.ColormapData.Count; ++i) {
  3736. const char* name = gp.ColormapData.GetName(i);
  3737. if (ImGui::Selectable(name, gp.Style.Colormap == i)) {
  3738. gp.Style.Colormap = i;
  3739. ImPlot::BustItemCache();
  3740. set = true;
  3741. }
  3742. }
  3743. ImGui::EndCombo();
  3744. }
  3745. return set;
  3746. }
  3747. void ShowStyleEditor(ImPlotStyle* ref) {
  3748. ImPlotContext& gp = *GImPlot;
  3749. ImPlotStyle& style = GetStyle();
  3750. static ImPlotStyle ref_saved_style;
  3751. // Default to using internal storage as reference
  3752. static bool init = true;
  3753. if (init && ref == NULL)
  3754. ref_saved_style = style;
  3755. init = false;
  3756. if (ref == NULL)
  3757. ref = &ref_saved_style;
  3758. if (ImPlot::ShowStyleSelector("Colors##Selector"))
  3759. ref_saved_style = style;
  3760. // Save/Revert button
  3761. if (ImGui::Button("Save Ref"))
  3762. *ref = ref_saved_style = style;
  3763. ImGui::SameLine();
  3764. if (ImGui::Button("Revert Ref"))
  3765. style = *ref;
  3766. ImGui::SameLine();
  3767. HelpMarker("Save/Revert in local non-persistent storage. Default Colors definition are not affected. "
  3768. "Use \"Export\" below to save them somewhere.");
  3769. if (ImGui::BeginTabBar("##StyleEditor")) {
  3770. if (ImGui::BeginTabItem("Variables")) {
  3771. ImGui::Text("Item Styling");
  3772. ImGui::SliderFloat("LineWeight", &style.LineWeight, 0.0f, 5.0f, "%.1f");
  3773. ImGui::SliderFloat("MarkerSize", &style.MarkerSize, 2.0f, 10.0f, "%.1f");
  3774. ImGui::SliderFloat("MarkerWeight", &style.MarkerWeight, 0.0f, 5.0f, "%.1f");
  3775. ImGui::SliderFloat("FillAlpha", &style.FillAlpha, 0.0f, 1.0f, "%.2f");
  3776. ImGui::SliderFloat("ErrorBarSize", &style.ErrorBarSize, 0.0f, 10.0f, "%.1f");
  3777. ImGui::SliderFloat("ErrorBarWeight", &style.ErrorBarWeight, 0.0f, 5.0f, "%.1f");
  3778. ImGui::SliderFloat("DigitalBitHeight", &style.DigitalBitHeight, 0.0f, 20.0f, "%.1f");
  3779. ImGui::SliderFloat("DigitalBitGap", &style.DigitalBitGap, 0.0f, 20.0f, "%.1f");
  3780. float indent = ImGui::CalcItemWidth() - ImGui::GetFrameHeight();
  3781. ImGui::Indent(ImGui::CalcItemWidth() - ImGui::GetFrameHeight());
  3782. ImGui::Checkbox("AntiAliasedLines", &style.AntiAliasedLines);
  3783. ImGui::Unindent(indent);
  3784. ImGui::Text("Plot Styling");
  3785. ImGui::SliderFloat("PlotBorderSize", &style.PlotBorderSize, 0.0f, 2.0f, "%.0f");
  3786. ImGui::SliderFloat("MinorAlpha", &style.MinorAlpha, 0.0f, 1.0f, "%.2f");
  3787. ImGui::SliderFloat2("MajorTickLen", (float*)&style.MajorTickLen, 0.0f, 20.0f, "%.0f");
  3788. ImGui::SliderFloat2("MinorTickLen", (float*)&style.MinorTickLen, 0.0f, 20.0f, "%.0f");
  3789. ImGui::SliderFloat2("MajorTickSize", (float*)&style.MajorTickSize, 0.0f, 2.0f, "%.1f");
  3790. ImGui::SliderFloat2("MinorTickSize", (float*)&style.MinorTickSize, 0.0f, 2.0f, "%.1f");
  3791. ImGui::SliderFloat2("MajorGridSize", (float*)&style.MajorGridSize, 0.0f, 2.0f, "%.1f");
  3792. ImGui::SliderFloat2("MinorGridSize", (float*)&style.MinorGridSize, 0.0f, 2.0f, "%.1f");
  3793. ImGui::SliderFloat2("PlotDefaultSize", (float*)&style.PlotDefaultSize, 0.0f, 1000, "%.0f");
  3794. ImGui::SliderFloat2("PlotMinSize", (float*)&style.PlotMinSize, 0.0f, 300, "%.0f");
  3795. ImGui::Text("Plot Padding");
  3796. ImGui::SliderFloat2("PlotPadding", (float*)&style.PlotPadding, 0.0f, 20.0f, "%.0f");
  3797. ImGui::SliderFloat2("LabelPadding", (float*)&style.LabelPadding, 0.0f, 20.0f, "%.0f");
  3798. ImGui::SliderFloat2("LegendPadding", (float*)&style.LegendPadding, 0.0f, 20.0f, "%.0f");
  3799. ImGui::SliderFloat2("LegendInnerPadding", (float*)&style.LegendInnerPadding, 0.0f, 10.0f, "%.0f");
  3800. ImGui::SliderFloat2("LegendSpacing", (float*)&style.LegendSpacing, 0.0f, 5.0f, "%.0f");
  3801. ImGui::SliderFloat2("MousePosPadding", (float*)&style.MousePosPadding, 0.0f, 20.0f, "%.0f");
  3802. ImGui::SliderFloat2("AnnotationPadding", (float*)&style.AnnotationPadding, 0.0f, 5.0f, "%.0f");
  3803. ImGui::SliderFloat2("FitPadding", (float*)&style.FitPadding, 0, 0.2f, "%.2f");
  3804. ImGui::EndTabItem();
  3805. }
  3806. if (ImGui::BeginTabItem("Colors")) {
  3807. static int output_dest = 0;
  3808. static bool output_only_modified = false;
  3809. if (ImGui::Button("Export", ImVec2(75,0))) {
  3810. if (output_dest == 0)
  3811. ImGui::LogToClipboard();
  3812. else
  3813. ImGui::LogToTTY();
  3814. ImGui::LogText("ImVec4* colors = ImPlot::GetStyle().Colors;\n");
  3815. for (int i = 0; i < ImPlotCol_COUNT; i++) {
  3816. const ImVec4& col = style.Colors[i];
  3817. const char* name = ImPlot::GetStyleColorName(i);
  3818. if (!output_only_modified || memcmp(&col, &ref->Colors[i], sizeof(ImVec4)) != 0) {
  3819. if (IsColorAuto(i))
  3820. ImGui::LogText("colors[ImPlotCol_%s]%*s= IMPLOT_AUTO_COL;\n",name,14 - (int)strlen(name), "");
  3821. else
  3822. ImGui::LogText("colors[ImPlotCol_%s]%*s= ImVec4(%.2ff, %.2ff, %.2ff, %.2ff);\n",
  3823. name, 14 - (int)strlen(name), "", col.x, col.y, col.z, col.w);
  3824. }
  3825. }
  3826. ImGui::LogFinish();
  3827. }
  3828. ImGui::SameLine(); ImGui::SetNextItemWidth(120); ImGui::Combo("##output_type", &output_dest, "To Clipboard\0To TTY\0");
  3829. ImGui::SameLine(); ImGui::Checkbox("Only Modified Colors", &output_only_modified);
  3830. static ImGuiTextFilter filter;
  3831. filter.Draw("Filter colors", ImGui::GetFontSize() * 16);
  3832. static ImGuiColorEditFlags alpha_flags = ImGuiColorEditFlags_AlphaPreviewHalf;
  3833. if (ImGui::RadioButton("Opaque", alpha_flags == ImGuiColorEditFlags_None)) { alpha_flags = ImGuiColorEditFlags_None; } ImGui::SameLine();
  3834. if (ImGui::RadioButton("Alpha", alpha_flags == ImGuiColorEditFlags_AlphaPreview)) { alpha_flags = ImGuiColorEditFlags_AlphaPreview; } ImGui::SameLine();
  3835. if (ImGui::RadioButton("Both", alpha_flags == ImGuiColorEditFlags_AlphaPreviewHalf)) { alpha_flags = ImGuiColorEditFlags_AlphaPreviewHalf; } ImGui::SameLine();
  3836. HelpMarker(
  3837. "In the color list:\n"
  3838. "Left-click on colored square to open color picker,\n"
  3839. "Right-click to open edit options menu.");
  3840. ImGui::Separator();
  3841. ImGui::PushItemWidth(-160);
  3842. for (int i = 0; i < ImPlotCol_COUNT; i++) {
  3843. const char* name = ImPlot::GetStyleColorName(i);
  3844. if (!filter.PassFilter(name))
  3845. continue;
  3846. ImGui::PushID(i);
  3847. ImVec4 temp = GetStyleColorVec4(i);
  3848. const bool is_auto = IsColorAuto(i);
  3849. if (!is_auto)
  3850. ImGui::PushStyleVar(ImGuiStyleVar_Alpha, 0.25f);
  3851. if (ImGui::Button("Auto")) {
  3852. if (is_auto)
  3853. style.Colors[i] = temp;
  3854. else
  3855. style.Colors[i] = IMPLOT_AUTO_COL;
  3856. BustItemCache();
  3857. }
  3858. if (!is_auto)
  3859. ImGui::PopStyleVar();
  3860. ImGui::SameLine();
  3861. if (ImGui::ColorEdit4(name, &temp.x, ImGuiColorEditFlags_NoInputs | alpha_flags)) {
  3862. style.Colors[i] = temp;
  3863. BustItemCache();
  3864. }
  3865. if (memcmp(&style.Colors[i], &ref->Colors[i], sizeof(ImVec4)) != 0) {
  3866. ImGui::SameLine(175); if (ImGui::Button("Save")) { ref->Colors[i] = style.Colors[i]; }
  3867. ImGui::SameLine(); if (ImGui::Button("Revert")) {
  3868. style.Colors[i] = ref->Colors[i];
  3869. BustItemCache();
  3870. }
  3871. }
  3872. ImGui::PopID();
  3873. }
  3874. ImGui::PopItemWidth();
  3875. ImGui::Separator();
  3876. ImGui::Text("Colors that are set to Auto (i.e. IMPLOT_AUTO_COL) will\n"
  3877. "be automatically deduced from your ImGui style or the\n"
  3878. "current ImPlot Colormap. If you want to style individual\n"
  3879. "plot items, use Push/PopStyleColor around its function.");
  3880. ImGui::EndTabItem();
  3881. }
  3882. if (ImGui::BeginTabItem("Colormaps")) {
  3883. static int output_dest = 0;
  3884. if (ImGui::Button("Export", ImVec2(75,0))) {
  3885. if (output_dest == 0)
  3886. ImGui::LogToClipboard();
  3887. else
  3888. ImGui::LogToTTY();
  3889. int size = GetColormapSize();
  3890. const char* name = GetColormapName(gp.Style.Colormap);
  3891. ImGui::LogText("static const ImU32 %s_Data[%d] = {\n", name, size);
  3892. for (int i = 0; i < size; ++i) {
  3893. ImU32 col = GetColormapColorU32(i,gp.Style.Colormap);
  3894. ImGui::LogText(" %u%s\n", col, i == size - 1 ? "" : ",");
  3895. }
  3896. ImGui::LogText("};\nImPlotColormap %s = ImPlot::AddColormap(\"%s\", %s_Data, %d);", name, name, name, size);
  3897. ImGui::LogFinish();
  3898. }
  3899. ImGui::SameLine(); ImGui::SetNextItemWidth(120); ImGui::Combo("##output_type", &output_dest, "To Clipboard\0To TTY\0");
  3900. ImGui::SameLine();
  3901. static bool edit = false;
  3902. ImGui::Checkbox("Edit Mode",&edit);
  3903. // built-in/added
  3904. ImGui::Separator();
  3905. for (int i = 0; i < gp.ColormapData.Count; ++i) {
  3906. ImGui::PushID(i);
  3907. int size = gp.ColormapData.GetKeyCount(i);
  3908. bool selected = i == gp.Style.Colormap;
  3909. const char* name = GetColormapName(i);
  3910. if (!selected)
  3911. ImGui::PushStyleVar(ImGuiStyleVar_Alpha, 0.25f);
  3912. if (ImGui::Button(name, ImVec2(100,0))) {
  3913. gp.Style.Colormap = i;
  3914. BustItemCache();
  3915. }
  3916. if (!selected)
  3917. ImGui::PopStyleVar();
  3918. ImGui::SameLine();
  3919. ImGui::BeginGroup();
  3920. if (edit) {
  3921. for (int c = 0; c < size; ++c) {
  3922. ImGui::PushID(c);
  3923. ImVec4 col4 = ImGui::ColorConvertU32ToFloat4(gp.ColormapData.GetKeyColor(i,c));
  3924. if (ImGui::ColorEdit4("",&col4.x,ImGuiColorEditFlags_NoInputs)) {
  3925. ImU32 col32 = ImGui::ColorConvertFloat4ToU32(col4);
  3926. gp.ColormapData.SetKeyColor(i,c,col32);
  3927. BustItemCache();
  3928. }
  3929. if ((c + 1) % 12 != 0 && c != size -1)
  3930. ImGui::SameLine();
  3931. ImGui::PopID();
  3932. }
  3933. }
  3934. else {
  3935. if (ImPlot::ColormapButton("##",ImVec2(-1,0),i))
  3936. edit = true;
  3937. }
  3938. ImGui::EndGroup();
  3939. ImGui::PopID();
  3940. }
  3941. static ImVector<ImVec4> custom;
  3942. if (custom.Size == 0) {
  3943. custom.push_back(ImVec4(1,0,0,1));
  3944. custom.push_back(ImVec4(0,1,0,1));
  3945. custom.push_back(ImVec4(0,0,1,1));
  3946. }
  3947. ImGui::Separator();
  3948. ImGui::BeginGroup();
  3949. static char name[16] = "MyColormap";
  3950. if (ImGui::Button("+", ImVec2((100 - ImGui::GetStyle().ItemSpacing.x)/2,0)))
  3951. custom.push_back(ImVec4(0,0,0,1));
  3952. ImGui::SameLine();
  3953. if (ImGui::Button("-", ImVec2((100 - ImGui::GetStyle().ItemSpacing.x)/2,0)) && custom.Size > 2)
  3954. custom.pop_back();
  3955. ImGui::SetNextItemWidth(100);
  3956. ImGui::InputText("##Name",name,16,ImGuiInputTextFlags_CharsNoBlank);
  3957. static bool qual = true;
  3958. ImGui::Checkbox("Qualitative",&qual);
  3959. if (ImGui::Button("Add", ImVec2(100, 0)) && gp.ColormapData.GetIndex(name)==-1)
  3960. AddColormap(name,custom.Data,custom.Size,qual);
  3961. ImGui::EndGroup();
  3962. ImGui::SameLine();
  3963. ImGui::BeginGroup();
  3964. for (int c = 0; c < custom.Size; ++c) {
  3965. ImGui::PushID(c);
  3966. if (ImGui::ColorEdit4("##Col1", &custom[c].x, ImGuiColorEditFlags_NoInputs)) {
  3967. }
  3968. if ((c + 1) % 12 != 0)
  3969. ImGui::SameLine();
  3970. ImGui::PopID();
  3971. }
  3972. ImGui::EndGroup();
  3973. ImGui::EndTabItem();
  3974. }
  3975. ImGui::EndTabBar();
  3976. }
  3977. }
  3978. void ShowUserGuide() {
  3979. ImGui::BulletText("Left-click drag within the plot area to pan X and Y axes.");
  3980. ImGui::Indent();
  3981. ImGui::BulletText("Left-click drag on axis labels to pan an individual axis.");
  3982. ImGui::Unindent();
  3983. ImGui::BulletText("Scroll in the plot area to zoom both X any Y axes.");
  3984. ImGui::Indent();
  3985. ImGui::BulletText("Scroll on axis labels to zoom an individual axis.");
  3986. ImGui::Unindent();
  3987. ImGui::BulletText("Right-click drag to box select data.");
  3988. ImGui::Indent();
  3989. ImGui::BulletText("Hold Alt to expand box selection horizontally.");
  3990. ImGui::BulletText("Hold Shift to expand box selection vertically.");
  3991. ImGui::BulletText("Left-click while box selecting to cancel the selection.");
  3992. ImGui::Unindent();
  3993. ImGui::BulletText("Double left-click to fit all visible data.");
  3994. ImGui::Indent();
  3995. ImGui::BulletText("Double left-click axis labels to fit the individual axis.");
  3996. ImGui::Unindent();
  3997. ImGui::BulletText("Right-click open the full plot context menu.");
  3998. ImGui::Indent();
  3999. ImGui::BulletText("Right-click axis labels to open an individual axis context menu.");
  4000. ImGui::Unindent();
  4001. ImGui::BulletText("Click legend label icons to show/hide plot items.");
  4002. }
  4003. void ShowAxisMetrics(ImPlotAxis* axis) {
  4004. ImGui::Bullet(); ImGui::Text("Flags: %d", axis->Flags);
  4005. ImGui::Bullet(); ImGui::Text("Range: [%f,%f]",axis->Range.Min, axis->Range.Max);
  4006. ImGui::Bullet(); ImGui::Text("Pixels: %f", axis->Pixels);
  4007. ImGui::Bullet(); ImGui::Text("Aspect: %f", axis->GetAspect());
  4008. ImGui::Bullet(); ImGui::Text("Dragging: %s", axis->Dragging ? "true" : "false");
  4009. ImGui::Bullet(); ImGui::Text("ExtHovered: %s", axis->ExtHovered ? "true" : "false");
  4010. ImGui::Bullet(); ImGui::Text("AllHovered: %s", axis->AllHovered ? "true" : "false");
  4011. ImGui::Bullet(); ImGui::Text("Present: %s", axis->Present ? "true" : "false");
  4012. ImGui::Bullet(); ImGui::Text("HasRange: %s", axis->HasRange ? "true" : "false");
  4013. ImGui::Bullet(); ImGui::Text("LinkedMin: %p", (void*)axis->LinkedMin);
  4014. ImGui::Bullet(); ImGui::Text("LinkedMax: %p", (void*)axis->LinkedMax);
  4015. }
  4016. void ShowMetricsWindow(bool* p_popen) {
  4017. static bool show_plot_rects = false;
  4018. static bool show_axes_rects = false;
  4019. static bool show_canvas_rects = false;
  4020. static bool show_frame_rects = false;
  4021. static bool show_subplot_frame_rects = false;
  4022. static bool show_subplot_grid_rects = false;
  4023. ImDrawList& fg = *ImGui::GetForegroundDrawList();
  4024. ImPlotContext& gp = *GImPlot;
  4025. // ImGuiContext& g = *GImGui;
  4026. ImGuiIO& io = ImGui::GetIO();
  4027. ImGui::Begin("ImPlot Metrics", p_popen);
  4028. ImGui::Text("ImPlot " IMPLOT_VERSION);
  4029. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
  4030. ImGui::Separator();
  4031. if (ImGui::TreeNode("Tools")) {
  4032. if (ImGui::Button("Bust Plot Cache"))
  4033. BustPlotCache();
  4034. ImGui::SameLine();
  4035. if (ImGui::Button("Bust Item Cache"))
  4036. BustItemCache();
  4037. ImGui::Checkbox("Show Frame Rects", &show_frame_rects);
  4038. ImGui::Checkbox("Show Canvas Rects",&show_canvas_rects);
  4039. ImGui::Checkbox("Show Plot Rects", &show_plot_rects);
  4040. ImGui::Checkbox("Show Axes Rects", &show_axes_rects);
  4041. ImGui::Checkbox("Show Subplot Frame Rects", &show_subplot_frame_rects);
  4042. ImGui::Checkbox("Show Subplot Grid Rects", &show_subplot_grid_rects);
  4043. ImGui::TreePop();
  4044. }
  4045. const int n_plots = gp.Plots.GetBufSize();
  4046. const int n_subplots = gp.Subplots.GetBufSize();
  4047. // render rects
  4048. for (int p = 0; p < n_plots; ++p) {
  4049. ImPlotPlot* plot = gp.Plots.GetByIndex(p);
  4050. if (show_frame_rects)
  4051. fg.AddRect(plot->FrameRect.Min, plot->FrameRect.Max, IM_COL32(255,0,255,255));
  4052. if (show_canvas_rects)
  4053. fg.AddRect(plot->CanvasRect.Min, plot->CanvasRect.Max, IM_COL32(0,255,255,255));
  4054. if (show_plot_rects)
  4055. fg.AddRect(plot->PlotRect.Min, plot->PlotRect.Max, IM_COL32(255,255,0,255));
  4056. if (show_axes_rects) {
  4057. fg.AddRect(plot->XAxis.HoverRect.Min, plot->XAxis.HoverRect.Max, IM_COL32(0,255,0,255));
  4058. fg.AddRect(plot->YAxis[0].HoverRect.Min, plot->YAxis[0].HoverRect.Max, IM_COL32(0,255,0,255));
  4059. if (ImHasFlag(plot->Flags, ImPlotFlags_YAxis2))
  4060. fg.AddRect(plot->YAxis[1].HoverRect.Min, plot->YAxis[1].HoverRect.Max, IM_COL32(0,255,0,255));
  4061. if (ImHasFlag(plot->Flags, ImPlotFlags_YAxis3))
  4062. fg.AddRect(plot->YAxis[2].HoverRect.Min, plot->YAxis[2].HoverRect.Max, IM_COL32(0,255,0,255));
  4063. }
  4064. }
  4065. for (int p = 0; p < n_subplots; ++p) {
  4066. ImPlotSubplot* subplot = gp.Subplots.GetByIndex(p);
  4067. if (show_subplot_frame_rects)
  4068. fg.AddRect(subplot->FrameRect.Min, subplot->FrameRect.Max, IM_COL32(255,0,0,255));
  4069. if (show_subplot_grid_rects)
  4070. fg.AddRect(subplot->GridRect.Min, subplot->GridRect.Max, IM_COL32(0,0,255,255));
  4071. }
  4072. if (ImGui::TreeNode("Plots","Plots (%d)", n_plots)) {
  4073. for (int p = 0; p < n_plots; ++p) {
  4074. // plot
  4075. ImPlotPlot* plot = gp.Plots.GetByIndex(p);
  4076. ImGui::PushID(p);
  4077. if (ImGui::TreeNode("Plot", "Plot [ID=0x%08X]", plot->ID)) {
  4078. int n_items = plot->Items.GetItemCount();
  4079. if (ImGui::TreeNode("Items", "Items (%d)", n_items)) {
  4080. for (int i = 0; i < n_items; ++i) {
  4081. ImPlotItem* item = plot->Items.GetItemByIndex(i);
  4082. ImGui::PushID(i);
  4083. if (ImGui::TreeNode("Item", "Item [ID=0x%08X]", item->ID)) {
  4084. ImGui::Bullet(); ImGui::Checkbox("Show", &item->Show);
  4085. ImGui::Bullet();
  4086. ImVec4 temp = ImGui::ColorConvertU32ToFloat4(item->Color);
  4087. if (ImGui::ColorEdit4("Color",&temp.x, ImGuiColorEditFlags_NoInputs))
  4088. item->Color = ImGui::ColorConvertFloat4ToU32(temp);
  4089. ImGui::Bullet(); ImGui::Text("NameOffset: %d",item->NameOffset);
  4090. ImGui::Bullet(); ImGui::Text("Name: %s", item->NameOffset != -1 ? plot->Items.Legend.Labels.Buf.Data + item->NameOffset : "N/A");
  4091. ImGui::Bullet(); ImGui::Text("Hovered: %s",item->LegendHovered ? "true" : "false");
  4092. ImGui::TreePop();
  4093. }
  4094. ImGui::PopID();
  4095. }
  4096. ImGui::TreePop();
  4097. }
  4098. if (ImGui::TreeNode("X-Axis")) {
  4099. ShowAxisMetrics(&plot->XAxis);
  4100. ImGui::TreePop();
  4101. }
  4102. if (ImGui::TreeNode("Y-Axis")) {
  4103. ShowAxisMetrics(&plot->YAxis[0]);
  4104. ImGui::TreePop();
  4105. }
  4106. if (ImHasFlag(plot->Flags, ImPlotFlags_YAxis2) && ImGui::TreeNode("Y-Axis 2")) {
  4107. ShowAxisMetrics(&plot->YAxis[1]);
  4108. ImGui::TreePop();
  4109. }
  4110. if (ImHasFlag(plot->Flags, ImPlotFlags_YAxis3) && ImGui::TreeNode("Y-Axis 3")) {
  4111. ShowAxisMetrics(&plot->YAxis[2]);
  4112. ImGui::TreePop();
  4113. }
  4114. ImGui::Bullet(); ImGui::Text("Flags: 0x%08X", plot->Flags);
  4115. ImGui::Bullet(); ImGui::Text("Initialized: %s", plot->Initialized ? "true" : "false");
  4116. ImGui::Bullet(); ImGui::Text("Selecting: %s", plot->Selecting ? "true" : "false");
  4117. ImGui::Bullet(); ImGui::Text("Selected: %s", plot->Selected ? "true" : "false");
  4118. ImGui::Bullet(); ImGui::Text("Querying: %s", plot->Querying ? "true" : "false");
  4119. ImGui::Bullet(); ImGui::Text("Queried: %s", plot->Queried ? "true" : "false");
  4120. ImGui::Bullet(); ImGui::Text("FrameHovered: %s", plot->FrameHovered ? "true" : "false");
  4121. ImGui::Bullet(); ImGui::Text("PlotHovered: %s", plot->PlotHovered ? "true" : "false");
  4122. ImGui::Bullet(); ImGui::Text("LegendHovered: %s", plot->Items.Legend.Hovered ? "true" : "false");
  4123. ImGui::TreePop();
  4124. }
  4125. ImGui::PopID();
  4126. }
  4127. ImGui::TreePop();
  4128. }
  4129. if (ImGui::TreeNode("Subplots","Subplots (%d)", n_subplots)) {
  4130. for (int p = 0; p < n_subplots; ++p) {
  4131. // plot
  4132. ImPlotSubplot* plot = gp.Subplots.GetByIndex(p);
  4133. ImGui::PushID(p);
  4134. if (ImGui::TreeNode("Subplot", "Subplot [ID=0x%08X]", plot->ID)) {
  4135. int n_items = plot->Items.GetItemCount();
  4136. if (ImGui::TreeNode("Items", "Items (%d)", n_items)) {
  4137. for (int i = 0; i < n_items; ++i) {
  4138. ImPlotItem* item = plot->Items.GetItemByIndex(i);
  4139. ImGui::PushID(i);
  4140. if (ImGui::TreeNode("Item", "Item [ID=0x%08X]", item->ID)) {
  4141. ImGui::Bullet(); ImGui::Checkbox("Show", &item->Show);
  4142. ImGui::Bullet();
  4143. ImVec4 temp = ImGui::ColorConvertU32ToFloat4(item->Color);
  4144. if (ImGui::ColorEdit4("Color",&temp.x, ImGuiColorEditFlags_NoInputs))
  4145. item->Color = ImGui::ColorConvertFloat4ToU32(temp);
  4146. ImGui::Bullet(); ImGui::Text("NameOffset: %d",item->NameOffset);
  4147. ImGui::Bullet(); ImGui::Text("Name: %s", item->NameOffset != -1 ? plot->Items.Legend.Labels.Buf.Data + item->NameOffset : "N/A");
  4148. ImGui::Bullet(); ImGui::Text("Hovered: %s",item->LegendHovered ? "true" : "false");
  4149. ImGui::TreePop();
  4150. }
  4151. ImGui::PopID();
  4152. }
  4153. ImGui::TreePop();
  4154. }
  4155. ImGui::Bullet(); ImGui::Text("Flags: 0x%08X", plot->Flags);
  4156. ImGui::Bullet(); ImGui::Text("FrameHovered: %s", plot->FrameHovered ? "true" : "false");
  4157. ImGui::Bullet(); ImGui::Text("LegendHovered: %s", plot->Items.Legend.Hovered ? "true" : "false");
  4158. ImGui::TreePop();
  4159. }
  4160. ImGui::PopID();
  4161. }
  4162. ImGui::TreePop();
  4163. }
  4164. if (ImGui::TreeNode("Colormaps")) {
  4165. ImGui::BulletText("Colormaps: %d", gp.ColormapData.Count);
  4166. ImGui::BulletText("Memory: %d bytes", gp.ColormapData.Tables.Size * 4);
  4167. if (ImGui::TreeNode("Data")) {
  4168. for (int m = 0; m < gp.ColormapData.Count; ++m) {
  4169. if (ImGui::TreeNode(gp.ColormapData.GetName(m))) {
  4170. int count = gp.ColormapData.GetKeyCount(m);
  4171. int size = gp.ColormapData.GetTableSize(m);
  4172. bool qual = gp.ColormapData.IsQual(m);
  4173. ImGui::BulletText("Qualitative: %s", qual ? "true" : "false");
  4174. ImGui::BulletText("Key Count: %d", count);
  4175. ImGui::BulletText("Table Size: %d", size);
  4176. ImGui::Indent();
  4177. static float t = 0.5;
  4178. ImVec4 samp;
  4179. float wid = 32 * 10 - ImGui::GetFrameHeight() - ImGui::GetStyle().ItemSpacing.x;
  4180. ImGui::SetNextItemWidth(wid);
  4181. ImPlot::ColormapSlider("##Sample",&t,&samp,"%.3f",m);
  4182. ImGui::SameLine();
  4183. ImGui::ColorButton("Sampler",samp);
  4184. ImGui::PushStyleColor(ImGuiCol_FrameBg, ImVec4(0,0,0,0));
  4185. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0,0));
  4186. for (int c = 0; c < size; ++c) {
  4187. ImVec4 col = ImGui::ColorConvertU32ToFloat4(gp.ColormapData.GetTableColor(m,c));
  4188. ImGui::PushID(m*1000+c);
  4189. ImGui::ColorButton("",col,0,ImVec2(10,10));
  4190. ImGui::PopID();
  4191. if ((c + 1) % 32 != 0 && c != size - 1)
  4192. ImGui::SameLine();
  4193. }
  4194. ImGui::PopStyleVar();
  4195. ImGui::PopStyleColor();
  4196. ImGui::Unindent();
  4197. ImGui::TreePop();
  4198. }
  4199. }
  4200. ImGui::TreePop();
  4201. }
  4202. ImGui::TreePop();
  4203. }
  4204. ImGui::End();
  4205. }
  4206. bool ShowDatePicker(const char* id, int* level, ImPlotTime* t, const ImPlotTime* t1, const ImPlotTime* t2) {
  4207. ImGui::PushID(id);
  4208. ImGui::BeginGroup();
  4209. ImGuiStyle& style = ImGui::GetStyle();
  4210. ImVec4 col_txt = style.Colors[ImGuiCol_Text];
  4211. ImVec4 col_dis = style.Colors[ImGuiCol_TextDisabled];
  4212. ImVec4 col_btn = style.Colors[ImGuiCol_Button];
  4213. ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0,0,0,0));
  4214. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0,0));
  4215. const float ht = ImGui::GetFrameHeight();
  4216. ImVec2 cell_size(ht*1.25f,ht);
  4217. char buff[32];
  4218. bool clk = false;
  4219. tm& Tm = GImPlot->Tm;
  4220. const int min_yr = 1970;
  4221. const int max_yr = 2999;
  4222. // t1 parts
  4223. int t1_mo = 0; int t1_md = 0; int t1_yr = 0;
  4224. if (t1 != NULL) {
  4225. GetTime(*t1,&Tm);
  4226. t1_mo = Tm.tm_mon;
  4227. t1_md = Tm.tm_mday;
  4228. t1_yr = Tm.tm_year + 1900;
  4229. }
  4230. // t2 parts
  4231. int t2_mo = 0; int t2_md = 0; int t2_yr = 0;
  4232. if (t2 != NULL) {
  4233. GetTime(*t2,&Tm);
  4234. t2_mo = Tm.tm_mon;
  4235. t2_md = Tm.tm_mday;
  4236. t2_yr = Tm.tm_year + 1900;
  4237. }
  4238. // day widget
  4239. if (*level == 0) {
  4240. *t = FloorTime(*t, ImPlotTimeUnit_Day);
  4241. GetTime(*t, &Tm);
  4242. const int this_year = Tm.tm_year + 1900;
  4243. const int last_year = this_year - 1;
  4244. const int next_year = this_year + 1;
  4245. const int this_mon = Tm.tm_mon;
  4246. const int last_mon = this_mon == 0 ? 11 : this_mon - 1;
  4247. const int next_mon = this_mon == 11 ? 0 : this_mon + 1;
  4248. const int days_this_mo = GetDaysInMonth(this_year, this_mon);
  4249. const int days_last_mo = GetDaysInMonth(this_mon == 0 ? last_year : this_year, last_mon);
  4250. ImPlotTime t_first_mo = FloorTime(*t,ImPlotTimeUnit_Mo);
  4251. GetTime(t_first_mo,&Tm);
  4252. const int first_wd = Tm.tm_wday;
  4253. // month year
  4254. snprintf(buff, 32, "%s %d", MONTH_NAMES[this_mon], this_year);
  4255. if (ImGui::Button(buff))
  4256. *level = 1;
  4257. ImGui::SameLine(5*cell_size.x);
  4258. BeginDisabledControls(this_year <= min_yr && this_mon == 0);
  4259. if (ImGui::ArrowButtonEx("##Up",ImGuiDir_Up,cell_size))
  4260. *t = AddTime(*t, ImPlotTimeUnit_Mo, -1);
  4261. EndDisabledControls(this_year <= min_yr && this_mon == 0);
  4262. ImGui::SameLine();
  4263. BeginDisabledControls(this_year >= max_yr && this_mon == 11);
  4264. if (ImGui::ArrowButtonEx("##Down",ImGuiDir_Down,cell_size))
  4265. *t = AddTime(*t, ImPlotTimeUnit_Mo, 1);
  4266. EndDisabledControls(this_year >= max_yr && this_mon == 11);
  4267. // render weekday abbreviations
  4268. ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
  4269. for (int i = 0; i < 7; ++i) {
  4270. ImGui::Button(WD_ABRVS[i],cell_size);
  4271. if (i != 6) { ImGui::SameLine(); }
  4272. }
  4273. ImGui::PopItemFlag();
  4274. // 0 = last mo, 1 = this mo, 2 = next mo
  4275. int mo = first_wd > 0 ? 0 : 1;
  4276. int day = mo == 1 ? 1 : days_last_mo - first_wd + 1;
  4277. for (int i = 0; i < 6; ++i) {
  4278. for (int j = 0; j < 7; ++j) {
  4279. if (mo == 0 && day > days_last_mo) {
  4280. mo = 1; day = 1;
  4281. }
  4282. else if (mo == 1 && day > days_this_mo) {
  4283. mo = 2; day = 1;
  4284. }
  4285. const int now_yr = (mo == 0 && this_mon == 0) ? last_year : ((mo == 2 && this_mon == 11) ? next_year : this_year);
  4286. const int now_mo = mo == 0 ? last_mon : (mo == 1 ? this_mon : next_mon);
  4287. const int now_md = day;
  4288. const bool off_mo = mo == 0 || mo == 2;
  4289. const bool t1_or_t2 = (t1 != NULL && t1_mo == now_mo && t1_yr == now_yr && t1_md == now_md) ||
  4290. (t2 != NULL && t2_mo == now_mo && t2_yr == now_yr && t2_md == now_md);
  4291. if (off_mo)
  4292. ImGui::PushStyleColor(ImGuiCol_Text, col_dis);
  4293. if (t1_or_t2) {
  4294. ImGui::PushStyleColor(ImGuiCol_Button, col_btn);
  4295. ImGui::PushStyleColor(ImGuiCol_Text, col_txt);
  4296. }
  4297. ImGui::PushID(i*7+j);
  4298. snprintf(buff,32,"%d",day);
  4299. if (now_yr == min_yr-1 || now_yr == max_yr+1) {
  4300. ImGui::Dummy(cell_size);
  4301. }
  4302. else if (ImGui::Button(buff,cell_size) && !clk) {
  4303. *t = MakeTime(now_yr, now_mo, now_md);
  4304. clk = true;
  4305. }
  4306. ImGui::PopID();
  4307. if (t1_or_t2)
  4308. ImGui::PopStyleColor(2);
  4309. if (off_mo)
  4310. ImGui::PopStyleColor();
  4311. if (j != 6)
  4312. ImGui::SameLine();
  4313. day++;
  4314. }
  4315. }
  4316. }
  4317. // month widget
  4318. else if (*level == 1) {
  4319. *t = FloorTime(*t, ImPlotTimeUnit_Mo);
  4320. GetTime(*t, &Tm);
  4321. int this_yr = Tm.tm_year + 1900;
  4322. snprintf(buff, 32, "%d", this_yr);
  4323. if (ImGui::Button(buff))
  4324. *level = 2;
  4325. BeginDisabledControls(this_yr <= min_yr);
  4326. ImGui::SameLine(5*cell_size.x);
  4327. if (ImGui::ArrowButtonEx("##Up",ImGuiDir_Up,cell_size))
  4328. *t = AddTime(*t, ImPlotTimeUnit_Yr, -1);
  4329. EndDisabledControls(this_yr <= min_yr);
  4330. ImGui::SameLine();
  4331. BeginDisabledControls(this_yr >= max_yr);
  4332. if (ImGui::ArrowButtonEx("##Down",ImGuiDir_Down,cell_size))
  4333. *t = AddTime(*t, ImPlotTimeUnit_Yr, 1);
  4334. EndDisabledControls(this_yr >= max_yr);
  4335. // ImGui::Dummy(cell_size);
  4336. cell_size.x *= 7.0f/4.0f;
  4337. cell_size.y *= 7.0f/3.0f;
  4338. int mo = 0;
  4339. for (int i = 0; i < 3; ++i) {
  4340. for (int j = 0; j < 4; ++j) {
  4341. const bool t1_or_t2 = (t1 != NULL && t1_yr == this_yr && t1_mo == mo) ||
  4342. (t2 != NULL && t2_yr == this_yr && t2_mo == mo);
  4343. if (t1_or_t2)
  4344. ImGui::PushStyleColor(ImGuiCol_Button, col_btn);
  4345. if (ImGui::Button(MONTH_ABRVS[mo],cell_size) && !clk) {
  4346. *t = MakeTime(this_yr, mo);
  4347. *level = 0;
  4348. }
  4349. if (t1_or_t2)
  4350. ImGui::PopStyleColor();
  4351. if (j != 3)
  4352. ImGui::SameLine();
  4353. mo++;
  4354. }
  4355. }
  4356. }
  4357. else if (*level == 2) {
  4358. *t = FloorTime(*t, ImPlotTimeUnit_Yr);
  4359. int this_yr = GetYear(*t);
  4360. int yr = this_yr - this_yr % 20;
  4361. ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
  4362. snprintf(buff,32,"%d-%d",yr,yr+19);
  4363. ImGui::Button(buff);
  4364. ImGui::PopItemFlag();
  4365. ImGui::SameLine(5*cell_size.x);
  4366. BeginDisabledControls(yr <= min_yr);
  4367. if (ImGui::ArrowButtonEx("##Up",ImGuiDir_Up,cell_size))
  4368. *t = MakeTime(yr-20);
  4369. EndDisabledControls(yr <= min_yr);
  4370. ImGui::SameLine();
  4371. BeginDisabledControls(yr + 20 >= max_yr);
  4372. if (ImGui::ArrowButtonEx("##Down",ImGuiDir_Down,cell_size))
  4373. *t = MakeTime(yr+20);
  4374. EndDisabledControls(yr+ 20 >= max_yr);
  4375. // ImGui::Dummy(cell_size);
  4376. cell_size.x *= 7.0f/4.0f;
  4377. cell_size.y *= 7.0f/5.0f;
  4378. for (int i = 0; i < 5; ++i) {
  4379. for (int j = 0; j < 4; ++j) {
  4380. const bool t1_or_t2 = (t1 != NULL && t1_yr == yr) || (t2 != NULL && t2_yr == yr);
  4381. if (t1_or_t2)
  4382. ImGui::PushStyleColor(ImGuiCol_Button, col_btn);
  4383. snprintf(buff,32,"%d",yr);
  4384. if (yr<1970||yr>3000) {
  4385. ImGui::Dummy(cell_size);
  4386. }
  4387. else if (ImGui::Button(buff,cell_size)) {
  4388. *t = MakeTime(yr);
  4389. *level = 1;
  4390. }
  4391. if (t1_or_t2)
  4392. ImGui::PopStyleColor();
  4393. if (j != 3)
  4394. ImGui::SameLine();
  4395. yr++;
  4396. }
  4397. }
  4398. }
  4399. ImGui::PopStyleVar();
  4400. ImGui::PopStyleColor();
  4401. ImGui::EndGroup();
  4402. ImGui::PopID();
  4403. return clk;
  4404. }
  4405. bool ShowTimePicker(const char* id, ImPlotTime* t) {
  4406. ImGui::PushID(id);
  4407. tm& Tm = GImPlot->Tm;
  4408. GetTime(*t,&Tm);
  4409. static const char* nums[] = { "00","01","02","03","04","05","06","07","08","09",
  4410. "10","11","12","13","14","15","16","17","18","19",
  4411. "20","21","22","23","24","25","26","27","28","29",
  4412. "30","31","32","33","34","35","36","37","38","39",
  4413. "40","41","42","43","44","45","46","47","48","49",
  4414. "50","51","52","53","54","55","56","57","58","59"};
  4415. static const char* am_pm[] = {"am","pm"};
  4416. bool hour24 = GImPlot->Style.Use24HourClock;
  4417. int hr = hour24 ? Tm.tm_hour : ((Tm.tm_hour == 0 || Tm.tm_hour == 12) ? 12 : Tm.tm_hour % 12);
  4418. int min = Tm.tm_min;
  4419. int sec = Tm.tm_sec;
  4420. int ap = Tm.tm_hour < 12 ? 0 : 1;
  4421. bool changed = false;
  4422. ImVec2 spacing = ImGui::GetStyle().ItemSpacing;
  4423. spacing.x = 0;
  4424. float width = ImGui::CalcTextSize("888").x;
  4425. float height = ImGui::GetFrameHeight();
  4426. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, spacing);
  4427. ImGui::PushStyleVar(ImGuiStyleVar_ScrollbarSize,2.0f);
  4428. ImGui::PushStyleColor(ImGuiCol_FrameBg, ImVec4(0,0,0,0));
  4429. ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0,0,0,0));
  4430. ImGui::PushStyleColor(ImGuiCol_FrameBgHovered, ImGui::GetStyleColorVec4(ImGuiCol_ButtonHovered));
  4431. ImGui::SetNextItemWidth(width);
  4432. if (ImGui::BeginCombo("##hr",nums[hr],ImGuiComboFlags_NoArrowButton)) {
  4433. const int ia = hour24 ? 0 : 1;
  4434. const int ib = hour24 ? 24 : 13;
  4435. for (int i = ia; i < ib; ++i) {
  4436. if (ImGui::Selectable(nums[i],i==hr)) {
  4437. hr = i;
  4438. changed = true;
  4439. }
  4440. }
  4441. ImGui::EndCombo();
  4442. }
  4443. ImGui::SameLine();
  4444. ImGui::Text(":");
  4445. ImGui::SameLine();
  4446. ImGui::SetNextItemWidth(width);
  4447. if (ImGui::BeginCombo("##min",nums[min],ImGuiComboFlags_NoArrowButton)) {
  4448. for (int i = 0; i < 60; ++i) {
  4449. if (ImGui::Selectable(nums[i],i==min)) {
  4450. min = i;
  4451. changed = true;
  4452. }
  4453. }
  4454. ImGui::EndCombo();
  4455. }
  4456. ImGui::SameLine();
  4457. ImGui::Text(":");
  4458. ImGui::SameLine();
  4459. ImGui::SetNextItemWidth(width);
  4460. if (ImGui::BeginCombo("##sec",nums[sec],ImGuiComboFlags_NoArrowButton)) {
  4461. for (int i = 0; i < 60; ++i) {
  4462. if (ImGui::Selectable(nums[i],i==sec)) {
  4463. sec = i;
  4464. changed = true;
  4465. }
  4466. }
  4467. ImGui::EndCombo();
  4468. }
  4469. if (!hour24) {
  4470. ImGui::SameLine();
  4471. if (ImGui::Button(am_pm[ap],ImVec2(height,height))) {
  4472. ap = 1 - ap;
  4473. changed = true;
  4474. }
  4475. }
  4476. ImGui::PopStyleColor(3);
  4477. ImGui::PopStyleVar(2);
  4478. ImGui::PopID();
  4479. if (changed) {
  4480. if (!hour24)
  4481. hr = hr % 12 + ap * 12;
  4482. Tm.tm_hour = hr;
  4483. Tm.tm_min = min;
  4484. Tm.tm_sec = sec;
  4485. *t = MkTime(&Tm);
  4486. }
  4487. return changed;
  4488. }
  4489. void StyleColorsAuto(ImPlotStyle* dst) {
  4490. ImPlotStyle* style = dst ? dst : &ImPlot::GetStyle();
  4491. ImVec4* colors = style->Colors;
  4492. style->MinorAlpha = 0.25f;
  4493. colors[ImPlotCol_Line] = IMPLOT_AUTO_COL;
  4494. colors[ImPlotCol_Fill] = IMPLOT_AUTO_COL;
  4495. colors[ImPlotCol_MarkerOutline] = IMPLOT_AUTO_COL;
  4496. colors[ImPlotCol_MarkerFill] = IMPLOT_AUTO_COL;
  4497. colors[ImPlotCol_ErrorBar] = IMPLOT_AUTO_COL;
  4498. colors[ImPlotCol_FrameBg] = IMPLOT_AUTO_COL;
  4499. colors[ImPlotCol_PlotBg] = IMPLOT_AUTO_COL;
  4500. colors[ImPlotCol_PlotBorder] = IMPLOT_AUTO_COL;
  4501. colors[ImPlotCol_LegendBg] = IMPLOT_AUTO_COL;
  4502. colors[ImPlotCol_LegendBorder] = IMPLOT_AUTO_COL;
  4503. colors[ImPlotCol_LegendText] = IMPLOT_AUTO_COL;
  4504. colors[ImPlotCol_TitleText] = IMPLOT_AUTO_COL;
  4505. colors[ImPlotCol_InlayText] = IMPLOT_AUTO_COL;
  4506. colors[ImPlotCol_PlotBorder] = IMPLOT_AUTO_COL;
  4507. colors[ImPlotCol_XAxis] = IMPLOT_AUTO_COL;
  4508. colors[ImPlotCol_XAxisGrid] = IMPLOT_AUTO_COL;
  4509. colors[ImPlotCol_YAxis] = IMPLOT_AUTO_COL;
  4510. colors[ImPlotCol_YAxisGrid] = IMPLOT_AUTO_COL;
  4511. colors[ImPlotCol_YAxis2] = IMPLOT_AUTO_COL;
  4512. colors[ImPlotCol_YAxisGrid2] = IMPLOT_AUTO_COL;
  4513. colors[ImPlotCol_YAxis3] = IMPLOT_AUTO_COL;
  4514. colors[ImPlotCol_YAxisGrid3] = IMPLOT_AUTO_COL;
  4515. colors[ImPlotCol_Selection] = IMPLOT_AUTO_COL;
  4516. colors[ImPlotCol_Query] = IMPLOT_AUTO_COL;
  4517. colors[ImPlotCol_Crosshairs] = IMPLOT_AUTO_COL;
  4518. }
  4519. void StyleColorsClassic(ImPlotStyle* dst) {
  4520. ImPlotStyle* style = dst ? dst : &ImPlot::GetStyle();
  4521. ImVec4* colors = style->Colors;
  4522. style->MinorAlpha = 0.5f;
  4523. colors[ImPlotCol_Line] = IMPLOT_AUTO_COL;
  4524. colors[ImPlotCol_Fill] = IMPLOT_AUTO_COL;
  4525. colors[ImPlotCol_MarkerOutline] = IMPLOT_AUTO_COL;
  4526. colors[ImPlotCol_MarkerFill] = IMPLOT_AUTO_COL;
  4527. colors[ImPlotCol_ErrorBar] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f);
  4528. colors[ImPlotCol_FrameBg] = ImVec4(0.43f, 0.43f, 0.43f, 0.39f);
  4529. colors[ImPlotCol_PlotBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.35f);
  4530. colors[ImPlotCol_PlotBorder] = ImVec4(0.50f, 0.50f, 0.50f, 0.50f);
  4531. colors[ImPlotCol_LegendBg] = ImVec4(0.11f, 0.11f, 0.14f, 0.92f);
  4532. colors[ImPlotCol_LegendBorder] = ImVec4(0.50f, 0.50f, 0.50f, 0.50f);
  4533. colors[ImPlotCol_LegendText] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f);
  4534. colors[ImPlotCol_TitleText] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f);
  4535. colors[ImPlotCol_InlayText] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f);
  4536. colors[ImPlotCol_XAxis] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f);
  4537. colors[ImPlotCol_XAxisGrid] = ImVec4(0.90f, 0.90f, 0.90f, 0.25f);
  4538. colors[ImPlotCol_YAxis] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f);
  4539. colors[ImPlotCol_YAxisGrid] = ImVec4(0.90f, 0.90f, 0.90f, 0.25f);
  4540. colors[ImPlotCol_YAxis2] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f);
  4541. colors[ImPlotCol_YAxisGrid2] = ImVec4(0.90f, 0.90f, 0.90f, 0.25f);
  4542. colors[ImPlotCol_YAxis3] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f);
  4543. colors[ImPlotCol_YAxisGrid3] = ImVec4(0.90f, 0.90f, 0.90f, 0.25f);
  4544. colors[ImPlotCol_Selection] = ImVec4(0.97f, 0.97f, 0.39f, 1.00f);
  4545. colors[ImPlotCol_Query] = ImVec4(0.00f, 1.00f, 0.59f, 1.00f);
  4546. colors[ImPlotCol_Crosshairs] = ImVec4(0.50f, 0.50f, 0.50f, 0.75f);
  4547. }
  4548. void StyleColorsDark(ImPlotStyle* dst) {
  4549. ImPlotStyle* style = dst ? dst : &ImPlot::GetStyle();
  4550. ImVec4* colors = style->Colors;
  4551. style->MinorAlpha = 0.25f;
  4552. colors[ImPlotCol_Line] = IMPLOT_AUTO_COL;
  4553. colors[ImPlotCol_Fill] = IMPLOT_AUTO_COL;
  4554. colors[ImPlotCol_MarkerOutline] = IMPLOT_AUTO_COL;
  4555. colors[ImPlotCol_MarkerFill] = IMPLOT_AUTO_COL;
  4556. colors[ImPlotCol_ErrorBar] = IMPLOT_AUTO_COL;
  4557. colors[ImPlotCol_FrameBg] = ImVec4(1.00f, 1.00f, 1.00f, 0.07f);
  4558. colors[ImPlotCol_PlotBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.50f);
  4559. colors[ImPlotCol_PlotBorder] = ImVec4(0.43f, 0.43f, 0.50f, 0.50f);
  4560. colors[ImPlotCol_LegendBg] = ImVec4(0.08f, 0.08f, 0.08f, 0.94f);
  4561. colors[ImPlotCol_LegendBorder] = ImVec4(0.43f, 0.43f, 0.50f, 0.50f);
  4562. colors[ImPlotCol_LegendText] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  4563. colors[ImPlotCol_TitleText] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  4564. colors[ImPlotCol_InlayText] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  4565. colors[ImPlotCol_XAxis] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  4566. colors[ImPlotCol_XAxisGrid] = ImVec4(1.00f, 1.00f, 1.00f, 0.25f);
  4567. colors[ImPlotCol_YAxis] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  4568. colors[ImPlotCol_YAxisGrid] = ImVec4(1.00f, 1.00f, 1.00f, 0.25f);
  4569. colors[ImPlotCol_YAxis2] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  4570. colors[ImPlotCol_YAxisGrid2] = ImVec4(1.00f, 1.00f, 1.00f, 0.25f);
  4571. colors[ImPlotCol_YAxis3] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  4572. colors[ImPlotCol_YAxisGrid3] = ImVec4(1.00f, 1.00f, 1.00f, 0.25f);
  4573. colors[ImPlotCol_Selection] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f);
  4574. colors[ImPlotCol_Query] = ImVec4(0.00f, 1.00f, 0.44f, 1.00f);
  4575. colors[ImPlotCol_Crosshairs] = ImVec4(1.00f, 1.00f, 1.00f, 0.50f);
  4576. }
  4577. void StyleColorsLight(ImPlotStyle* dst) {
  4578. ImPlotStyle* style = dst ? dst : &ImPlot::GetStyle();
  4579. ImVec4* colors = style->Colors;
  4580. style->MinorAlpha = 1.0f;
  4581. colors[ImPlotCol_Line] = IMPLOT_AUTO_COL;
  4582. colors[ImPlotCol_Fill] = IMPLOT_AUTO_COL;
  4583. colors[ImPlotCol_MarkerOutline] = IMPLOT_AUTO_COL;
  4584. colors[ImPlotCol_MarkerFill] = IMPLOT_AUTO_COL;
  4585. colors[ImPlotCol_ErrorBar] = IMPLOT_AUTO_COL;
  4586. colors[ImPlotCol_FrameBg] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  4587. colors[ImPlotCol_PlotBg] = ImVec4(0.42f, 0.57f, 1.00f, 0.13f);
  4588. colors[ImPlotCol_PlotBorder] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
  4589. colors[ImPlotCol_LegendBg] = ImVec4(1.00f, 1.00f, 1.00f, 0.98f);
  4590. colors[ImPlotCol_LegendBorder] = ImVec4(0.82f, 0.82f, 0.82f, 0.80f);
  4591. colors[ImPlotCol_LegendText] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  4592. colors[ImPlotCol_TitleText] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  4593. colors[ImPlotCol_InlayText] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  4594. colors[ImPlotCol_XAxis] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  4595. colors[ImPlotCol_XAxisGrid] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  4596. colors[ImPlotCol_YAxis] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  4597. colors[ImPlotCol_YAxisGrid] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  4598. colors[ImPlotCol_YAxis2] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  4599. colors[ImPlotCol_YAxisGrid2] = ImVec4(0.00f, 0.00f, 0.00f, 0.50f);
  4600. colors[ImPlotCol_YAxis3] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  4601. colors[ImPlotCol_YAxisGrid3] = ImVec4(0.00f, 0.00f, 0.00f, 0.50f);
  4602. colors[ImPlotCol_Selection] = ImVec4(0.82f, 0.64f, 0.03f, 1.00f);
  4603. colors[ImPlotCol_Query] = ImVec4(0.00f, 0.84f, 0.37f, 1.00f);
  4604. colors[ImPlotCol_Crosshairs] = ImVec4(0.00f, 0.00f, 0.00f, 0.50f);
  4605. }
  4606. } // namespace ImPlot