pChart.class.php 138 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613
  1. <?php
  2. /*
  3. pChart - a PHP class to build charts!
  4. Copyright (C) 2008 Jean-Damien POGOLOTTI
  5. Version 1.27d last updated on 09/30/08
  6. http://pchart.sourceforge.net
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 1,2,3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. Class initialisation :
  18. pChart($XSize,$YSize)
  19. Draw methods :
  20. drawBackground($R,$G,$B)
  21. drawRectangle($X1,$Y1,$X2,$Y2,$R,$G,$B)
  22. drawFilledRectangle($X1,$Y1,$X2,$Y2,$R,$G,$B,$DrawBorder=TRUE,$Alpha=100)
  23. drawRoundedRectangle($X1,$Y1,$X2,$Y2,$Radius,$R,$G,$B)
  24. drawFilledRoundedRectangle($X1,$Y1,$X2,$Y2,$Radius,$R,$G,$B)
  25. drawCircle($Xc,$Yc,$Height,$R,$G,$B,$Width=0)
  26. drawFilledCircle($Xc,$Yc,$Height,$R,$G,$B,$Width=0)
  27. drawEllipse($Xc,$Yc,$Height,$Width,$R,$G,$B)
  28. drawFilledEllipse($Xc,$Yc,$Height,$Width,$R,$G,$B)
  29. drawLine($X1,$Y1,$X2,$Y2,$R,$G,$B,$GraphFunction=FALSE)
  30. drawDottedLine($X1,$Y1,$X2,$Y2,$DotSize,$R,$G,$B)
  31. drawAlphaPixel($X,$Y,$Alpha,$R,$G,$B)
  32. drawFromPNG($FileName,$X,$Y,$Alpha=100)
  33. drawFromGIF($FileName,$X,$Y,$Alpha=100)
  34. drawFromJPG($FileName,$X,$Y,$Alpha=100)
  35. Graph setup methods :
  36. addBorder($Width=3,$R=0,$G=0,$B=0)
  37. clearScale()
  38. clearShadow()
  39. createColorGradientPalette($R1,$G1,$B1,$R2,$G2,$B2,$Shades)
  40. drawGraphArea($R,$G,$B,$Stripe=FALSE)
  41. drawScale($Data,$DataDescription,$ScaleMode,$R,$G,$B,$DrawTicks=TRUE,$Angle=0,$Decimals=1,$WithMargin=FALSE,$SkipLabels=1,$RightScale=FALSE)
  42. drawRightScale($Data,$DataDescription,$ScaleMode,$R,$G,$B,$DrawTicks=TRUE,$Angle=0,$Decimals=1,$WithMargin=FALSE,$SkipLabels=1)
  43. drawXYScale($Data,$DataDescription,$YSerieName,$XSerieName,$R,$G,$B,$WithMargin=0,$Angle=0,$Decimals=1)
  44. drawGrid($LineWidth,$Mosaic=TRUE,$R=220,$G=220,$B=220,$Alpha=100)
  45. drawLegend($XPos,$YPos,$DataDescription,$R,$G,$B,$Rs=-1,$Gs=-1,$Bs=-1,$Rt=0,$Gt=0,$Bt=0,$Border=FALSE)
  46. drawPieLegend($XPos,$YPos,$Data,$DataDescription,$R,$G,$B)
  47. drawTitle($XPos,$YPos,$Value,$R,$G,$B,$XPos2=-1,$YPos2=-1,$Shadow=FALSE)
  48. drawTreshold($Value,$R,$G,$B,$ShowLabel=FALSE,$ShowOnRight=FALSE,$TickWidth=4,$FreeText=NULL)
  49. drawArea($Data,$Serie1,$Serie2,$R,$G,$B,$Alpha = 50)
  50. drawRadarAxis($Data,$DataDescription,$Mosaic=TRUE,$BorderOffset=10,$A_R=60,$A_G=60,$A_B=60,$S_R=200,$S_G=200,$S_B=200,$MaxValue=-1)
  51. drawGraphAreaGradient($R,$G,$B,$Decay,$Target=TARGET_GRAPHAREA)
  52. drawTextBox($X1,$Y1,$X2,$Y2,$Text,$Angle=0,$R=255,$G=255,$B=255,$Align=ALIGN_LEFT,$Shadow=TRUE,$BgR=-1,$BgG=-1,$BgB=-1,$Alpha=100)
  53. getLegendBoxSize($DataDescription)
  54. loadColorPalette($FileName,$Delimiter=",")
  55. reportWarnings($Interface="CLI")
  56. setGraphArea($X1,$Y1,$X2,$Y2)
  57. setLabel($Data,$DataDescription,$SerieName,$ValueName,$Caption,$R=210,$G=210,$B=210)
  58. setColorPalette($ID,$R,$G,$B)
  59. setCurrency($Currency)
  60. setDateFormat($Format)
  61. setFontProperties($FontName,$FontSize)
  62. setLineStyle($Width=1,$DotSize=0)
  63. setFixedScale($VMin,$VMax,$Divisions=5,$VXMin=0,$VXMin=0,$XDivisions=5)
  64. setShadowProperties($XDistance=1,$YDistance=1,$R=60,$G=60,$B=60,$Alpha)
  65. writeValues($Data,$DataDescription,$Series)
  66. Graphs methods :
  67. drawPlotGraph($Data,$DataDescription,$BigRadius=5,$SmallRadius=2,$R2=-1,$G2=-1,$B2=-1,$Shadow=FALSE)
  68. drawXYPlotGraph($Data,$DataDescription,$YSerieName,$XSerieName,$PaletteID=0,$BigRadius=5,$SmallRadius=2,$R2=-1,$G2=-1,$B2=-1)
  69. drawLineGraph($Data,$DataDescription,$SerieName="")
  70. drawXYGraph($Data,$DataDescription,$YSerieName,$XSerieName,$PaletteID=0)
  71. drawFilledLineGraph($Data,$DataDescription,$Alpha=100,$AroundZero=FALSE)
  72. drawCubicCurve($Data,$DataDescription,$Accuracy=.1,$SerieName="")
  73. drawFilledCubicCurve($Data,$DataDescription,$Accuracy=.1,$Alpha=100,$AroundZero=FALSE)
  74. drawOverlayBarGraph($Data,$DataDescription,$Alpha=50)
  75. drawBarGraph($Data,$DataDescription,$Shadow=FALSE)
  76. drawStackedBarGraph($Data,$DataDescription,$Alpha=50,$Contiguous=FALSE)
  77. drawLimitsGraph($Data,$DataDescription,$R=0,$G=0,$B=0)
  78. drawRadar($Data,$DataDescription,$BorderOffset=10,$MaxValue=-1)
  79. drawFilledRadar($Data,$DataDescription,$Alpha=50,$BorderOffset=10,$MaxValue=-1)
  80. drawBasicPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$R=255,$G=255,$B=255,$Decimals=0)
  81. drawFlatPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$SpliceDistance=0,$Decimals = 0)
  82. drawFlatPieGraphWithShadow($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$SpliceDistance=0,$Decimals = 0)
  83. drawPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$EnhanceColors=TRUE,$Skew=60,$SpliceHeight=20,$SpliceDistance=0,$Decimals=0)
  84. Other methods :
  85. setImageMap($Mode=TRUE,$GraphID="MyGraph")
  86. getImageMap($MapName,$Flush=TRUE)
  87. Render($FileName)
  88. Stroke()
  89. */
  90. /* Declare some script wide constants */
  91. define("SCALE_NORMAL",1);
  92. define("SCALE_ADDALL",2);
  93. define("SCALE_START0",3);
  94. define("SCALE_ADDALLSTART0",4);
  95. define("PIE_PERCENTAGE", 1);
  96. define("PIE_LABELS",2);
  97. define("PIE_NOLABEL",3);
  98. define("PIE_PERCENTAGE_LABEL", 4);
  99. define("TARGET_GRAPHAREA",1);
  100. define("TARGET_BACKGROUND",2);
  101. define("ALIGN_TOP_LEFT",1);
  102. define("ALIGN_TOP_CENTER",2);
  103. define("ALIGN_TOP_RIGHT",3);
  104. define("ALIGN_LEFT",4);
  105. define("ALIGN_CENTER",5);
  106. define("ALIGN_RIGHT",6);
  107. define("ALIGN_BOTTOM_LEFT",7);
  108. define("ALIGN_BOTTOM_CENTER",8);
  109. define("ALIGN_BOTTOM_RIGHT",9);
  110. /* pChart class definition */
  111. class pChart
  112. {
  113. /* Palettes definition */
  114. var $Palette = array("0"=>array("R"=>188,"G"=>224,"B"=>46),
  115. "1"=>array("R"=>224,"G"=>100,"B"=>46),
  116. "2"=>array("R"=>224,"G"=>214,"B"=>46),
  117. "3"=>array("R"=>46,"G"=>151,"B"=>224),
  118. "4"=>array("R"=>176,"G"=>46,"B"=>224),
  119. "5"=>array("R"=>224,"G"=>46,"B"=>117),
  120. "6"=>array("R"=>92,"G"=>224,"B"=>46),
  121. "7"=>array("R"=>224,"G"=>176,"B"=>46));
  122. /* Some static vars used in the class */
  123. var $XSize = NULL;
  124. var $YSize = NULL;
  125. var $Picture = NULL;
  126. var $ImageMap = NULL;
  127. /* Error management */
  128. var $ErrorReporting = FALSE;
  129. var $ErrorInterface = "CLI";
  130. var $Errors = NULL;
  131. var $ErrorFontName = "Fonts/pf_arma_five.ttf";
  132. var $ErrorFontSize = 6;
  133. /* vars related to the graphing area */
  134. var $GArea_X1 = NULL;
  135. var $GArea_Y1 = NULL;
  136. var $GArea_X2 = NULL;
  137. var $GArea_Y2 = NULL;
  138. var $GAreaXOffset = NULL;
  139. var $VMax = NULL;
  140. var $VMin = NULL;
  141. var $VXMax = NULL;
  142. var $VXMin = NULL;
  143. var $Divisions = NULL;
  144. var $XDivisions = NULL;
  145. var $DivisionHeight = NULL;
  146. var $XDivisionHeight = NULL;
  147. var $DivisionCount = NULL;
  148. var $XDivisionCount = NULL;
  149. var $DivisionRatio = NULL;
  150. var $XDivisionRatio = NULL;
  151. var $DivisionWidth = NULL;
  152. var $DataCount = NULL;
  153. var $Currency = "\$";
  154. /* Text format related vars */
  155. var $FontName = NULL;
  156. var $FontSize = NULL;
  157. var $DateFormat = "d/m/Y";
  158. /* Lines format related vars */
  159. var $LineWidth = 1;
  160. var $LineDotSize = 0;
  161. /* Layer related vars */
  162. var $Layers = NULL;
  163. /* Set antialias quality : 0 is maximum, 100 minimum*/
  164. var $AntialiasQuality = 0;
  165. /* Shadow settings */
  166. var $ShadowActive = FALSE;
  167. var $ShadowXDistance = 1;
  168. var $ShadowYDistance = 1;
  169. var $ShadowRColor = 60;
  170. var $ShadowGColor = 60;
  171. var $ShadowBColor = 60;
  172. var $ShadowAlpha = 50;
  173. var $ShadowBlur = 0;
  174. /* Image Map settings */
  175. var $BuildMap = FALSE;
  176. var $MapFunction = NULL;
  177. var $tmpFolder = "tmp/";
  178. var $MapID = NULL;
  179. /* This function create the background picture */
  180. function pChart($XSize,$YSize)
  181. {
  182. $this->XSize = $XSize;
  183. $this->YSize = $YSize;
  184. $this->Picture = imagecreatetruecolor($XSize,$YSize);
  185. $C_White = $this->AllocateColor($this->Picture,255,255,255);
  186. imagefilledrectangle($this->Picture,0,0,$XSize,$YSize,$C_White);
  187. imagecolortransparent($this->Picture,$C_White);
  188. $this->setFontProperties("tahoma.ttf",8);
  189. }
  190. /* Set if warnings should be reported */
  191. function reportWarnings($Interface="CLI")
  192. {
  193. $this->ErrorReporting = TRUE;
  194. $this->ErrorInterface = $Interface;
  195. }
  196. /* Set the font properties */
  197. function setFontProperties($FontName,$FontSize)
  198. {
  199. $this->FontName = $FontName;
  200. $this->FontSize = $FontSize;
  201. }
  202. /* Set the shadow properties */
  203. function setShadowProperties($XDistance=1,$YDistance=1,$R=60,$G=60,$B=60,$Alpha=50,$Blur=0)
  204. {
  205. $this->ShadowActive = TRUE;
  206. $this->ShadowXDistance = $XDistance;
  207. $this->ShadowYDistance = $YDistance;
  208. $this->ShadowRColor = $R;
  209. $this->ShadowGColor = $G;
  210. $this->ShadowBColor = $B;
  211. $this->ShadowAlpha = $Alpha;
  212. $this->ShadowBlur = $Blur;
  213. }
  214. /* Remove shadow option */
  215. function clearShadow()
  216. {
  217. $this->ShadowActive = FALSE;
  218. }
  219. /* Set Palette color */
  220. function setColorPalette($ID,$R,$G,$B)
  221. {
  222. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  223. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  224. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  225. $this->Palette[$ID]["R"] = $R;
  226. $this->Palette[$ID]["G"] = $G;
  227. $this->Palette[$ID]["B"] = $B;
  228. }
  229. /* Create a color palette shading from one color to another */
  230. function createColorGradientPalette($R1,$G1,$B1,$R2,$G2,$B2,$Shades)
  231. {
  232. $RFactor = ($R2-$R1)/$Shades;
  233. $GFactor = ($G2-$G1)/$Shades;
  234. $BFactor = ($B2-$B1)/$Shades;
  235. for($i=0;$i<=$Shades-1;$i++)
  236. {
  237. $this->Palette[$i]["R"] = $R1+$RFactor*$i;
  238. $this->Palette[$i]["G"] = $G1+$GFactor*$i;
  239. $this->Palette[$i]["B"] = $B1+$BFactor*$i;
  240. }
  241. }
  242. /* Load Color Palette from file */
  243. function loadColorPalette($FileName,$Delimiter=",")
  244. {
  245. $handle = @fopen($FileName,"r");
  246. $ColorID = 0;
  247. if ($handle)
  248. {
  249. while (!feof($handle))
  250. {
  251. $buffer = fgets($handle, 4096);
  252. $buffer = str_replace(chr(10),"",$buffer);
  253. $buffer = str_replace(chr(13),"",$buffer);
  254. $Values = split($Delimiter,$buffer);
  255. if ( count($Values) == 3 )
  256. {
  257. $this->Palette[$ColorID]["R"] = $Values[0];
  258. $this->Palette[$ColorID]["G"] = $Values[1];
  259. $this->Palette[$ColorID]["B"] = $Values[2];
  260. $ColorID++;
  261. }
  262. }
  263. }
  264. }
  265. /* Set line style */
  266. function setLineStyle($Width=1,$DotSize=0)
  267. {
  268. $this->LineWidth = $Width;
  269. $this->LineDotSize = $DotSize;
  270. }
  271. /* Set currency symbol */
  272. function setCurrency($Currency)
  273. {
  274. $this->Currency = $Currency;
  275. }
  276. /* Set the graph area location */
  277. function setGraphArea($X1,$Y1,$X2,$Y2)
  278. {
  279. $this->GArea_X1 = $X1;
  280. $this->GArea_Y1 = $Y1;
  281. $this->GArea_X2 = $X2;
  282. $this->GArea_Y2 = $Y2;
  283. }
  284. /* Prepare the graph area */
  285. function drawGraphArea($R,$G,$B,$Stripe=FALSE)
  286. {
  287. $this->drawFilledRectangle($this->GArea_X1,$this->GArea_Y1,$this->GArea_X2,$this->GArea_Y2,$R,$G,$B,FALSE);
  288. $this->drawRectangle($this->GArea_X1,$this->GArea_Y1,$this->GArea_X2,$this->GArea_Y2,$R-40,$G-40,$B-40);
  289. if ( $Stripe )
  290. {
  291. $R2 = $R-15; if ( $R2 < 0 ) { $R2 = 0; }
  292. $G2 = $R-15; if ( $G2 < 0 ) { $G2 = 0; }
  293. $B2 = $R-15; if ( $B2 < 0 ) { $B2 = 0; }
  294. $LineColor =$this->AllocateColor($this->Picture,$R2,$G2,$B2);
  295. $SkewWidth = $this->GArea_Y2-$this->GArea_Y1-1;
  296. for($i=$this->GArea_X1-$SkewWidth;$i<=$this->GArea_X2;$i=$i+4)
  297. {
  298. $X1 = $i; $Y1 = $this->GArea_Y2;
  299. $X2 = $i+$SkewWidth; $Y2 = $this->GArea_Y1;
  300. if ( $X1 < $this->GArea_X1 )
  301. { $X1 = $this->GArea_X1; $Y1 = $this->GArea_Y1 + $X2 - $this->GArea_X1 + 1; }
  302. if ( $X2 >= $this->GArea_X2 )
  303. { $Y2 = $this->GArea_Y1 + $X2 - $this->GArea_X2 +1; $X2 = $this->GArea_X2 - 1; }
  304. // * Fixed in 1.27 * { $X2 = $this->GArea_X2 - 1; $Y2 = $this->GArea_Y2 - ($this->GArea_X2 - $X1); }
  305. imageline($this->Picture,$X1,$Y1,$X2,$Y2+1,$LineColor);
  306. }
  307. }
  308. }
  309. /* Allow you to clear the scale : used if drawing multiple charts */
  310. function clearScale()
  311. {
  312. $this->VMin = NULL;
  313. $this->VMax = NULL;
  314. $this->VXMin = NULL;
  315. $this->VXMax = NULL;
  316. $this->Divisions = NULL;
  317. $this->XDivisions = NULL; }
  318. /* Allow you to fix the scale, use this to bypass the automatic scaling */
  319. function setFixedScale($VMin,$VMax,$Divisions=5,$VXMin=0,$VXMax=0,$XDivisions=5)
  320. {
  321. $this->VMin = $VMin;
  322. $this->VMax = $VMax;
  323. $this->Divisions = $Divisions;
  324. if ( !$VXMin == 0 )
  325. {
  326. $this->VXMin = $VXMin;
  327. $this->VXMax = $VXMax;
  328. $this->XDivisions = $XDivisions;
  329. }
  330. }
  331. /* Wrapper to the drawScale() function allowing a second scale to be drawn */
  332. function drawRightScale($Data,$DataDescription,$ScaleMode,$R,$G,$B,$DrawTicks=TRUE,$Angle=0,$Decimals=1,$WithMargin=FALSE,$SkipLabels=1)
  333. {
  334. $this->drawScale($Data,$DataDescription,$ScaleMode,$R,$G,$B,$DrawTicks,$Angle,$Decimals,$WithMargin,$SkipLabels,TRUE);
  335. }
  336. /* Compute and draw the scale */
  337. function drawScale($Data,$DataDescription,$ScaleMode,$R,$G,$B,$DrawTicks=TRUE,$Angle=0,$Decimals=1,$WithMargin=FALSE,$SkipLabels=1,$RightScale=FALSE)
  338. {
  339. /* Validate the Data and DataDescription array */
  340. $this->validateData("drawScale",$Data);
  341. $C_TextColor =$this->AllocateColor($this->Picture,$R,$G,$B);
  342. $this->drawLine($this->GArea_X1,$this->GArea_Y1,$this->GArea_X1,$this->GArea_Y2,$R,$G,$B);
  343. $this->drawLine($this->GArea_X1,$this->GArea_Y2,$this->GArea_X2,$this->GArea_Y2,$R,$G,$B);
  344. if ( $this->VMin == NULL && $this->VMax == NULL)
  345. {
  346. if (isset($DataDescription["Values"][0]))
  347. {
  348. $this->VMin = $Data[0][$DataDescription["Values"][0]];
  349. $this->VMax = $Data[0][$DataDescription["Values"][0]];
  350. }
  351. else { $this->VMin = 2147483647; $this->VMax = -2147483647; }
  352. /* Compute Min and Max values */
  353. if ( $ScaleMode == SCALE_NORMAL || $ScaleMode == SCALE_START0 )
  354. {
  355. if ( $ScaleMode == SCALE_START0 ) { $this->VMin = 0; }
  356. foreach ( $Data as $Key => $Values )
  357. {
  358. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  359. {
  360. if (isset($Data[$Key][$ColName]))
  361. {
  362. $Value = $Data[$Key][$ColName];
  363. if ( is_numeric($Value) )
  364. {
  365. if ( $this->VMax < $Value) { $this->VMax = $Value; }
  366. if ( $this->VMin > $Value) { $this->VMin = $Value; }
  367. }
  368. }
  369. }
  370. }
  371. }
  372. elseif ( $ScaleMode == SCALE_ADDALL || $ScaleMode == SCALE_ADDALLSTART0 ) /* Experimental */
  373. {
  374. if ( $ScaleMode == SCALE_ADDALLSTART0 ) { $this->VMin = 0; }
  375. foreach ( $Data as $Key => $Values )
  376. {
  377. $Sum = 0;
  378. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  379. {
  380. if (isset($Data[$Key][$ColName]))
  381. {
  382. $Value = $Data[$Key][$ColName];
  383. if ( is_numeric($Value) )
  384. $Sum += $Value;
  385. }
  386. }
  387. if ( $this->VMax < $Sum) { $this->VMax = $Sum; }
  388. if ( $this->VMin > $Sum) { $this->VMin = $Sum; }
  389. }
  390. }
  391. if ( $this->VMax > preg_replace('/\.[0-9]+/','',$this->VMax) )
  392. $this->VMax = preg_replace('/\.[0-9]+/','',$this->VMax)+1;
  393. /* If all values are the same */
  394. if ( $this->VMax == $this->VMin )
  395. {
  396. if ( $this->VMax >= 0 ) { $this->VMax++; }
  397. else { $this->VMin--; }
  398. }
  399. $DataRange = $this->VMax - $this->VMin;
  400. if ( $DataRange == 0 ) { $DataRange = .1; }
  401. /* Compute automatic scaling */
  402. $ScaleOk = FALSE; $Factor = 1;
  403. $MinDivHeight = 25; $MaxDivs = ($this->GArea_Y2 - $this->GArea_Y1) / $MinDivHeight;
  404. if ( $this->VMin == 0 && $this->VMax == 0 )
  405. { $this->VMin = 0; $this->VMax = 2; $Scale = 1; $Divisions = 2;}
  406. elseif ($MaxDivs > 1)
  407. {
  408. while(!$ScaleOk)
  409. {
  410. $Scale1 = ( $this->VMax - $this->VMin ) / $Factor;
  411. $Scale2 = ( $this->VMax - $this->VMin ) / $Factor / 2;
  412. $Scale4 = ( $this->VMax - $this->VMin ) / $Factor / 4;
  413. if ( $Scale1 > 1 && $Scale1 <= $MaxDivs && !$ScaleOk) { $ScaleOk = TRUE; $Divisions = floor($Scale1); $Scale = 1;}
  414. if ( $Scale2 > 1 && $Scale2 <= $MaxDivs && !$ScaleOk) { $ScaleOk = TRUE; $Divisions = floor($Scale2); $Scale = 2;}
  415. if (!$ScaleOk)
  416. {
  417. if ( $Scale2 > 1 ) { $Factor = $Factor * 10; }
  418. if ( $Scale2 < 1 ) { $Factor = $Factor / 10; }
  419. }
  420. }
  421. if ( floor($this->VMax / $Scale / $Factor) != $this->VMax / $Scale / $Factor)
  422. {
  423. $GridID = floor ( $this->VMax / $Scale / $Factor) + 1;
  424. $this->VMax = $GridID * $Scale * $Factor;
  425. $Divisions++;
  426. }
  427. if ( floor($this->VMin / $Scale / $Factor) != $this->VMin / $Scale / $Factor)
  428. {
  429. $GridID = floor( $this->VMin / $Scale / $Factor);
  430. $this->VMin = $GridID * $Scale * $Factor;
  431. $Divisions++;
  432. }
  433. }
  434. else /* Can occurs for small graphs */
  435. $Scale = 1;
  436. if ( !isset($Divisions) )
  437. $Divisions = 2;
  438. if ($Scale == 1 && $Divisions%2 == 1)
  439. $Divisions--;
  440. }
  441. else
  442. $Divisions = $this->Divisions;
  443. $this->DivisionCount = $Divisions;
  444. $DataRange = $this->VMax - $this->VMin;
  445. if ( $DataRange == 0 ) { $DataRange = .1; }
  446. $this->DivisionHeight = ( $this->GArea_Y2 - $this->GArea_Y1 ) / $Divisions;
  447. $this->DivisionRatio = ( $this->GArea_Y2 - $this->GArea_Y1 ) / $DataRange;
  448. $this->GAreaXOffset = 0;
  449. if ( count($Data) > 1 )
  450. {
  451. if ( $WithMargin == FALSE )
  452. $this->DivisionWidth = ( $this->GArea_X2 - $this->GArea_X1 ) / (count($Data)-1);
  453. else
  454. {
  455. $this->DivisionWidth = ( $this->GArea_X2 - $this->GArea_X1 ) / (count($Data));
  456. $this->GAreaXOffset = $this->DivisionWidth / 2;
  457. }
  458. }
  459. else
  460. {
  461. $this->DivisionWidth = $this->GArea_X2 - $this->GArea_X1;
  462. $this->GAreaXOffset = $this->DivisionWidth / 2;
  463. }
  464. $this->DataCount = count($Data);
  465. if ( $DrawTicks == FALSE )
  466. return(0);
  467. $YPos = $this->GArea_Y2; $XMin = NULL;
  468. for($i=1;$i<=$Divisions+1;$i++)
  469. {
  470. if ( $RightScale )
  471. $this->drawLine($this->GArea_X2,$YPos,$this->GArea_X2+5,$YPos,$R,$G,$B);
  472. else
  473. $this->drawLine($this->GArea_X1,$YPos,$this->GArea_X1-5,$YPos,$R,$G,$B);
  474. $Value = $this->VMin + ($i-1) * (( $this->VMax - $this->VMin ) / $Divisions);
  475. $Value = round($Value * pow(10,$Decimals)) / pow(10,$Decimals);
  476. if ( $DataDescription["Format"]["Y"] == "number" )
  477. $Value = $Value.$DataDescription["Unit"]["Y"];
  478. if ( $DataDescription["Format"]["Y"] == "time" )
  479. $Value = $this->ToTime($Value);
  480. if ( $DataDescription["Format"]["Y"] == "date" )
  481. $Value = $this->ToDate($Value);
  482. if ( $DataDescription["Format"]["Y"] == "metric" )
  483. $Value = $this->ToMetric($Value);
  484. if ( $DataDescription["Format"]["Y"] == "currency" )
  485. $Value = $this->ToCurrency($Value);
  486. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value);
  487. $TextWidth = $Position[2]-$Position[0];
  488. if ( $RightScale )
  489. {
  490. imagettftext($this->Picture,$this->FontSize,0,$this->GArea_X2+10,$YPos+($this->FontSize/2),$C_TextColor,$this->FontName,$Value);
  491. if ( $XMin < $this->GArea_X2+15+$TextWidth || $XMin == NULL ) { $XMin = $this->GArea_X2+15+$TextWidth; }
  492. }
  493. else
  494. {
  495. imagettftext($this->Picture,$this->FontSize,0,$this->GArea_X1-10-$TextWidth,$YPos+($this->FontSize/2),$C_TextColor,$this->FontName,$Value);
  496. if ( $XMin > $this->GArea_X1-10-$TextWidth || $XMin == NULL ) { $XMin = $this->GArea_X1-10-$TextWidth; }
  497. }
  498. $YPos = $YPos - $this->DivisionHeight;
  499. }
  500. /* Write the Y Axis caption if set */
  501. if ( isset($DataDescription["Axis"]["Y"]) )
  502. {
  503. $Position = imageftbbox($this->FontSize,90,$this->FontName,$DataDescription["Axis"]["Y"]);
  504. $TextHeight = abs($Position[1])+abs($Position[3]);
  505. $TextTop = (($this->GArea_Y2 - $this->GArea_Y1) / 2) + $this->GArea_Y1 + ($TextHeight/2);
  506. if ( $RightScale )
  507. imagettftext($this->Picture,$this->FontSize,90,$XMin+$this->FontSize,$TextTop,$C_TextColor,$this->FontName,$DataDescription["Axis"]["Y"]);
  508. else
  509. imagettftext($this->Picture,$this->FontSize,90,$XMin-$this->FontSize,$TextTop,$C_TextColor,$this->FontName,$DataDescription["Axis"]["Y"]);
  510. }
  511. /* Horizontal Axis */
  512. $XPos = $this->GArea_X1 + $this->GAreaXOffset;
  513. $ID = 1; $YMax = NULL;
  514. $maxTextHeight = 0;
  515. foreach ( $Data as $Key => $Values )
  516. {
  517. if ( $ID % $SkipLabels == 0 )
  518. {
  519. $this->drawLine(floor($XPos),$this->GArea_Y2,floor($XPos),$this->GArea_Y2+5,$R,$G,$B);
  520. $Value = $Data[$Key][$DataDescription["Position"]];
  521. if ( $DataDescription["Format"]["X"] == "number" )
  522. $Value = $Value.$DataDescription["Unit"]["X"];
  523. if ( $DataDescription["Format"]["X"] == "time" )
  524. $Value = $this->ToTime($Value);
  525. if ( $DataDescription["Format"]["X"] == "date" )
  526. $Value = $this->ToDate($Value);
  527. if ( $DataDescription["Format"]["X"] == "metric" )
  528. $Value = $this->ToMetric($Value);
  529. if ( $DataDescription["Format"]["X"] == "currency" )
  530. $Value = $this->ToCurrency($Value);
  531. $Position = imageftbbox($this->FontSize,$Angle,$this->FontName,$Value);
  532. $TextWidth = abs($Position[2])+abs($Position[0]);
  533. $TextHeight = abs($Position[1])+abs($Position[3]);
  534. // Save max text height
  535. if ($maxTextHeight < $TextHeight) {
  536. $maxTextHeight = $TextHeight;
  537. }
  538. if ( $Angle == 0 )
  539. {
  540. $YPos = $this->GArea_Y2+18;
  541. imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)-floor($TextWidth/2),$YPos,$C_TextColor,$this->FontName,$Value);
  542. }
  543. else
  544. {
  545. $YPos = $this->GArea_Y2+10;
  546. // If rotation is top down, then add TextHeight;
  547. $YPos = (sin($Angle) < 0) ? $YPos + $TextHeight : $YPos;
  548. if ( $Angle <= 90 )
  549. imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)-$TextWidth+5,$YPos,$C_TextColor,$this->FontName,$Value);
  550. else
  551. imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)+$TextWidth+5,$YPos,$C_TextColor,$this->FontName,$Value);
  552. }
  553. if ( $YMax < $YPos || $YMax == NULL ) { $YMax = $YPos; }
  554. }
  555. $XPos = $XPos + $this->DivisionWidth;
  556. $ID++;
  557. }
  558. /* Write the X Axis caption if set */
  559. if ( isset($DataDescription["Axis"]["X"]) )
  560. {
  561. $Position = imageftbbox($this->FontSize,90,$this->FontName,$DataDescription["Axis"]["X"]);
  562. $TextWidth = abs($Position[2])+abs($Position[0]);
  563. $TextLeft = (($this->GArea_X2 - $this->GArea_X1) / 2) + $this->GArea_X1 + ($TextWidth/2);
  564. imagettftext($this->Picture,$this->FontSize,0,$TextLeft,$YMax+$this->FontSize+5+$maxTextHeight,$C_TextColor,$this->FontName,$DataDescription["Axis"]["X"]);
  565. }
  566. }
  567. /* Compute and draw the scale for X/Y charts */
  568. function drawXYScale($Data,$DataDescription,$YSerieName,$XSerieName,$R,$G,$B,$WithMargin=0,$Angle=0,$Decimals=1)
  569. {
  570. /* Validate the Data and DataDescription array */
  571. $this->validateData("drawScale",$Data);
  572. $C_TextColor =$this->AllocateColor($this->Picture,$R,$G,$B);
  573. $this->drawLine($this->GArea_X1,$this->GArea_Y1,$this->GArea_X1,$this->GArea_Y2,$R,$G,$B);
  574. $this->drawLine($this->GArea_X1,$this->GArea_Y2,$this->GArea_X2,$this->GArea_Y2,$R,$G,$B);
  575. /* Process Y scale */
  576. if ( $this->VMin == NULL && $this->VMax == NULL)
  577. {
  578. $this->VMin = $Data[0][$YSerieName];
  579. $this->VMax = $Data[0][$YSerieName];
  580. foreach ( $Data as $Key => $Values )
  581. {
  582. if (isset($Data[$Key][$YSerieName]))
  583. {
  584. $Value = $Data[$Key][$YSerieName];
  585. if ( $this->VMax < $Value) { $this->VMax = $Value; }
  586. if ( $this->VMin > $Value) { $this->VMin = $Value; }
  587. }
  588. }
  589. if ( $this->VMax > preg_replace('/\.[0-9]+/','',$this->VMax) )
  590. $this->VMax = preg_replace('/\.[0-9]+/','',$this->VMax)+1;
  591. $DataRange = $this->VMax - $this->VMin;
  592. if ( $DataRange == 0 ) { $DataRange = .1; }
  593. /* Compute automatic scaling */
  594. $ScaleOk = FALSE; $Factor = 1;
  595. $MinDivHeight = 25; $MaxDivs = ($this->GArea_Y2 - $this->GArea_Y1) / $MinDivHeight;
  596. if ( $this->VMin == 0 && $this->VMax == 0 )
  597. { $this->VMin = 0; $this->VMax = 2; $Scale = 1; $Divisions = 2;}
  598. elseif ($MaxDivs > 1)
  599. {
  600. while(!$ScaleOk)
  601. {
  602. $Scale1 = ( $this->VMax - $this->VMin ) / $Factor;
  603. $Scale2 = ( $this->VMax - $this->VMin ) / $Factor / 2;
  604. $Scale4 = ( $this->VMax - $this->VMin ) / $Factor / 4;
  605. if ( $Scale1 > 1 && $Scale1 <= $MaxDivs && !$ScaleOk) { $ScaleOk = TRUE; $Divisions = floor($Scale1); $Scale = 1;}
  606. if ( $Scale2 > 1 && $Scale2 <= $MaxDivs && !$ScaleOk) { $ScaleOk = TRUE; $Divisions = floor($Scale2); $Scale = 2;}
  607. if (!$ScaleOk)
  608. {
  609. if ( $Scale2 > 1 ) { $Factor = $Factor * 10; }
  610. if ( $Scale2 < 1 ) { $Factor = $Factor / 10; }
  611. }
  612. }
  613. if ( floor($this->VMax / $Scale / $Factor) != $this->VMax / $Scale / $Factor)
  614. {
  615. $GridID = floor ( $this->VMax / $Scale / $Factor) + 1;
  616. $this->VMax = $GridID * $Scale * $Factor;
  617. $Divisions++;
  618. }
  619. if ( floor($this->VMin / $Scale / $Factor) != $this->VMin / $Scale / $Factor)
  620. {
  621. $GridID = floor( $this->VMin / $Scale / $Factor);
  622. $this->VMin = $GridID * $Scale * $Factor;
  623. $Divisions++;
  624. }
  625. }
  626. else /* Can occurs for small graphs */
  627. $Scale = 1;
  628. if ( !isset($Divisions) )
  629. $Divisions = 2;
  630. if ( $this->isRealInt(($this->VMax-$this->VMin)/($Divisions-1)))
  631. $Divisions--;
  632. elseif ( $this->isRealInt(($this->VMax-$this->VMin)/($Divisions+1)))
  633. $Divisions++;
  634. }
  635. else
  636. $Divisions = $this->Divisions;
  637. $this->DivisionCount = $Divisions;
  638. $DataRange = $this->VMax - $this->VMin;
  639. if ( $DataRange == 0 ) { $DataRange = .1; }
  640. $this->DivisionHeight = ( $this->GArea_Y2 - $this->GArea_Y1 ) / $Divisions;
  641. $this->DivisionRatio = ( $this->GArea_Y2 - $this->GArea_Y1 ) / $DataRange;
  642. $YPos = $this->GArea_Y2; $XMin = NULL;
  643. for($i=1;$i<=$Divisions+1;$i++)
  644. {
  645. $this->drawLine($this->GArea_X1,$YPos,$this->GArea_X1-5,$YPos,$R,$G,$B);
  646. $Value = $this->VMin + ($i-1) * (( $this->VMax - $this->VMin ) / $Divisions);
  647. $Value = round($Value * pow(10,$Decimals)) / pow(10,$Decimals);
  648. if ( $DataDescription["Format"]["Y"] == "number" )
  649. $Value = $Value.$DataDescription["Unit"]["Y"];
  650. if ( $DataDescription["Format"]["Y"] == "time" )
  651. $Value = $this->ToTime($Value);
  652. if ( $DataDescription["Format"]["Y"] == "date" )
  653. $Value = $this->ToDate($Value);
  654. if ( $DataDescription["Format"]["Y"] == "metric" )
  655. $Value = $this->ToMetric($Value);
  656. if ( $DataDescription["Format"]["Y"] == "currency" )
  657. $Value = $this->ToCurrency($Value);
  658. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value);
  659. $TextWidth = $Position[2]-$Position[0];
  660. imagettftext($this->Picture,$this->FontSize,0,$this->GArea_X1-10-$TextWidth,$YPos+($this->FontSize/2),$C_TextColor,$this->FontName,$Value);
  661. if ( $XMin > $this->GArea_X1-10-$TextWidth || $XMin == NULL ) { $XMin = $this->GArea_X1-10-$TextWidth; }
  662. $YPos = $YPos - $this->DivisionHeight;
  663. }
  664. /* Process X scale */
  665. if ( $this->VXMin == NULL && $this->VXMax == NULL)
  666. {
  667. $this->VXMin = $Data[0][$XSerieName];
  668. $this->VXMax = $Data[0][$XSerieName];
  669. foreach ( $Data as $Key => $Values )
  670. {
  671. if (isset($Data[$Key][$XSerieName]))
  672. {
  673. $Value = $Data[$Key][$XSerieName];
  674. if ( $this->VXMax < $Value) { $this->VXMax = $Value; }
  675. if ( $this->VXMin > $Value) { $this->VXMin = $Value; }
  676. }
  677. }
  678. if ( $this->VXMax > preg_replace('/\.[0-9]+/','',$this->VXMax) )
  679. $this->VXMax = preg_replace('/\.[0-9]+/','',$this->VXMax)+1;
  680. $DataRange = $this->VMax - $this->VMin;
  681. if ( $DataRange == 0 ) { $DataRange = .1; }
  682. /* Compute automatic scaling */
  683. $ScaleOk = FALSE; $Factor = 1;
  684. $MinDivWidth = 25; $MaxDivs = ($this->GArea_X2 - $this->GArea_X1) / $MinDivWidth;
  685. if ( $this->VXMin == 0 && $this->VXMax == 0 )
  686. { $this->VXMin = 0; $this->VXMax = 2; $Scale = 1; $XDivisions = 2;}
  687. elseif ($MaxDivs > 1)
  688. {
  689. while(!$ScaleOk)
  690. {
  691. $Scale1 = ( $this->VXMax - $this->VXMin ) / $Factor;
  692. $Scale2 = ( $this->VXMax - $this->VXMin ) / $Factor / 2;
  693. $Scale4 = ( $this->VXMax - $this->VXMin ) / $Factor / 4;
  694. if ( $Scale1 > 1 && $Scale1 <= $MaxDivs && !$ScaleOk) { $ScaleOk = TRUE; $XDivisions = floor($Scale1); $Scale = 1;}
  695. if ( $Scale2 > 1 && $Scale2 <= $MaxDivs && !$ScaleOk) { $ScaleOk = TRUE; $XDivisions = floor($Scale2); $Scale = 2;}
  696. if (!$ScaleOk)
  697. {
  698. if ( $Scale2 > 1 ) { $Factor = $Factor * 10; }
  699. if ( $Scale2 < 1 ) { $Factor = $Factor / 10; }
  700. }
  701. }
  702. if ( floor($this->VXMax / $Scale / $Factor) != $this->VXMax / $Scale / $Factor)
  703. {
  704. $GridID = floor ( $this->VXMax / $Scale / $Factor) + 1;
  705. $this->VXMax = $GridID * $Scale * $Factor;
  706. $XDivisions++;
  707. }
  708. if ( floor($this->VXMin / $Scale / $Factor) != $this->VXMin / $Scale / $Factor)
  709. {
  710. $GridID = floor( $this->VXMin / $Scale / $Factor);
  711. $this->VXMin = $GridID * $Scale * $Factor;
  712. $XDivisions++;
  713. }
  714. }
  715. else /* Can occurs for small graphs */
  716. $Scale = 1;
  717. if ( !isset($XDivisions) )
  718. $XDivisions = 2;
  719. if ( $this->isRealInt(($this->VXMax-$this->VXMin)/($XDivisions-1)))
  720. $XDivisions--;
  721. elseif ( $this->isRealInt(($this->VXMax-$this->VXMin)/($XDivisions+1)))
  722. $XDivisions++;
  723. }
  724. else
  725. $XDivisions = $this->XDivisions;
  726. $this->XDivisionCount = $Divisions;
  727. $this->DataCount = $Divisions + 2;
  728. $XDataRange = $this->VXMax - $this->VXMin;
  729. if ( $XDataRange == 0 ) { $XDataRange = .1; }
  730. $this->DivisionWidth = ( $this->GArea_X2 - $this->GArea_X1 ) / $XDivisions;
  731. $this->XDivisionRatio = ( $this->GArea_X2 - $this->GArea_X1 ) / $XDataRange;
  732. $XPos = $this->GArea_X1; $YMax = NULL;
  733. for($i=1;$i<=$XDivisions+1;$i++)
  734. {
  735. $this->drawLine($XPos,$this->GArea_Y2,$XPos,$this->GArea_Y2+5,$R,$G,$B);
  736. $Value = $this->VXMin + ($i-1) * (( $this->VXMax - $this->VXMin ) / $XDivisions);
  737. $Value = round($Value * pow(10,$Decimals)) / pow(10,$Decimals);
  738. if ( $DataDescription["Format"]["Y"] == "number" )
  739. $Value = $Value.$DataDescription["Unit"]["Y"];
  740. if ( $DataDescription["Format"]["Y"] == "time" )
  741. $Value = $this->ToTime($Value);
  742. if ( $DataDescription["Format"]["Y"] == "date" )
  743. $Value = $this->ToDate($Value);
  744. if ( $DataDescription["Format"]["Y"] == "metric" )
  745. $Value = $this->ToMetric($Value);
  746. if ( $DataDescription["Format"]["Y"] == "currency" )
  747. $Value = $this->ToCurrency($Value);
  748. $Position = imageftbbox($this->FontSize,$Angle,$this->FontName,$Value);
  749. $TextWidth = abs($Position[2])+abs($Position[0]);
  750. $TextHeight = abs($Position[1])+abs($Position[3]);
  751. if ( $Angle == 0 )
  752. {
  753. $YPos = $this->GArea_Y2+18;
  754. imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)-floor($TextWidth/2),$YPos,$C_TextColor,$this->FontName,$Value);
  755. }
  756. else
  757. {
  758. $YPos = $this->GArea_Y2+10+$TextHeight;
  759. if ( $Angle <= 90 )
  760. imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)-$TextWidth+5,$YPos,$C_TextColor,$this->FontName,$Value);
  761. else
  762. imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)+$TextWidth+5,$YPos,$C_TextColor,$this->FontName,$Value);
  763. }
  764. if ( $YMax < $YPos || $YMax == NULL ) { $YMax = $YPos; }
  765. $XPos = $XPos + $this->DivisionWidth;
  766. }
  767. /* Write the Y Axis caption if set */
  768. if ( isset($DataDescription["Axis"]["Y"]) )
  769. {
  770. $Position = imageftbbox($this->FontSize,90,$this->FontName,$DataDescription["Axis"]["Y"]);
  771. $TextHeight = abs($Position[1])+abs($Position[3]);
  772. $TextTop = (($this->GArea_Y2 - $this->GArea_Y1) / 2) + $this->GArea_Y1 + ($TextHeight/2);
  773. imagettftext($this->Picture,$this->FontSize,90,$XMin-$this->FontSize,$TextTop,$C_TextColor,$this->FontName,$DataDescription["Axis"]["Y"]);
  774. }
  775. /* Write the X Axis caption if set */
  776. if ( isset($DataDescription["Axis"]["X"]) )
  777. {
  778. $Position = imageftbbox($this->FontSize,90,$this->FontName,$DataDescription["Axis"]["X"]);
  779. $TextWidth = abs($Position[2])+abs($Position[0]);
  780. $TextLeft = (($this->GArea_X2 - $this->GArea_X1) / 2) + $this->GArea_X1 + ($TextWidth/2);
  781. imagettftext($this->Picture,$this->FontSize,0,$TextLeft,$YMax+$this->FontSize+5,$C_TextColor,$this->FontName,$DataDescription["Axis"]["X"]);
  782. }
  783. }
  784. /* Compute and draw the scale */
  785. function drawGrid($LineWidth,$Mosaic=TRUE,$R=220,$G=220,$B=220,$Alpha=100)
  786. {
  787. /* Draw mosaic */
  788. if ( $Mosaic )
  789. {
  790. $LayerWidth = $this->GArea_X2-$this->GArea_X1;
  791. $LayerHeight = $this->GArea_Y2-$this->GArea_Y1;
  792. $this->Layers[0] = imagecreatetruecolor($LayerWidth,$LayerHeight);
  793. $C_White =$this->AllocateColor($this->Layers[0],255,255,255);
  794. imagefilledrectangle($this->Layers[0],0,0,$LayerWidth,$LayerHeight,$C_White);
  795. imagecolortransparent($this->Layers[0],$C_White);
  796. $C_Rectangle =$this->AllocateColor($this->Layers[0],250,250,250);
  797. $YPos = $LayerHeight; //$this->GArea_Y2-1;
  798. $LastY = $YPos;
  799. for($i=0;$i<=$this->DivisionCount;$i++)
  800. {
  801. $LastY = $YPos;
  802. $YPos = $YPos - $this->DivisionHeight;
  803. if ( $YPos <= 0 ) { $YPos = 1; }
  804. if ( $i % 2 == 0 )
  805. {
  806. imagefilledrectangle($this->Layers[0],1,$YPos,$LayerWidth-1,$LastY,$C_Rectangle);
  807. }
  808. }
  809. imagecopymerge($this->Picture,$this->Layers[0],$this->GArea_X1,$this->GArea_Y1,0,0,$LayerWidth,$LayerHeight,$Alpha);
  810. imagedestroy($this->Layers[0]);
  811. }
  812. /* Horizontal lines */
  813. $YPos = $this->GArea_Y2 - $this->DivisionHeight;
  814. for($i=1;$i<=$this->DivisionCount;$i++)
  815. {
  816. if ( $YPos > $this->GArea_Y1 && $YPos < $this->GArea_Y2 )
  817. $this->drawDottedLine($this->GArea_X1,$YPos,$this->GArea_X2,$YPos,$LineWidth,$R,$G,$B);
  818. $YPos = $YPos - $this->DivisionHeight;
  819. }
  820. /* Vertical lines */
  821. if ( $this->GAreaXOffset == 0 )
  822. { $XPos = $this->GArea_X1 + $this->DivisionWidth + $this->GAreaXOffset; $ColCount = $this->DataCount-2; }
  823. else
  824. { $XPos = $this->GArea_X1 + $this->GAreaXOffset; $ColCount = floor( ($this->GArea_X2 - $this->GArea_X1) / $this->DivisionWidth ); }
  825. for($i=1;$i<=$ColCount;$i++)
  826. {
  827. if ( $XPos > $this->GArea_X1 && $XPos < $this->GArea_X2 )
  828. $this->drawDottedLine(floor($XPos),$this->GArea_Y1,floor($XPos),$this->GArea_Y2,$LineWidth,$R,$G,$B);
  829. $XPos = $XPos + $this->DivisionWidth;
  830. }
  831. }
  832. /* retrieve the legends size */
  833. function getLegendBoxSize($DataDescription)
  834. {
  835. if ( !isset($DataDescription["Description"]) )
  836. return(-1);
  837. /* <-10->[8]<-4->Text<-10-> */
  838. $MaxWidth = 0; $MaxHeight = 8;
  839. foreach($DataDescription["Description"] as $Key => $Value)
  840. {
  841. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value);
  842. $TextWidth = $Position[2]-$Position[0];
  843. $TextHeight = $Position[1]-$Position[7];
  844. if ( $TextWidth > $MaxWidth) { $MaxWidth = $TextWidth; }
  845. $MaxHeight = $MaxHeight + $TextHeight + 4;
  846. }
  847. $MaxHeight = $MaxHeight - 3;
  848. $MaxWidth = $MaxWidth + 32;
  849. return(array($MaxWidth,$MaxHeight));
  850. }
  851. /* Draw the data legends */
  852. function drawLegend($XPos,$YPos,$DataDescription,$R,$G,$B,$Rs=-1,$Gs=-1,$Bs=-1,$Rt=0,$Gt=0,$Bt=0,$Border=TRUE)
  853. {
  854. /* Validate the Data and DataDescription array */
  855. $this->validateDataDescription("drawLegend",$DataDescription);
  856. if ( !isset($DataDescription["Description"]) )
  857. return(-1);
  858. $C_TextColor =$this->AllocateColor($this->Picture,$Rt,$Gt,$Bt);
  859. /* <-10->[8]<-4->Text<-10-> */
  860. $MaxWidth = 0; $MaxHeight = 8;
  861. foreach($DataDescription["Description"] as $Key => $Value)
  862. {
  863. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value);
  864. $TextWidth = $Position[2]-$Position[0];
  865. $TextHeight = $Position[1]-$Position[7];
  866. if ( $TextWidth > $MaxWidth) { $MaxWidth = $TextWidth; }
  867. $MaxHeight = $MaxHeight + $TextHeight + 4;
  868. }
  869. $MaxHeight = $MaxHeight - 5;
  870. $MaxWidth = $MaxWidth + 32;
  871. if ( $Rs == -1 || $Gs == -1 || $Bs == -1 )
  872. { $Rs = $R-30; $Gs = $G-30; $Bs = $B-30; }
  873. if ( $Border )
  874. {
  875. $this->drawFilledRoundedRectangle($XPos+1,$YPos+1,$XPos+$MaxWidth+1,$YPos+$MaxHeight+1,5,$Rs,$Gs,$Bs);
  876. $this->drawFilledRoundedRectangle($XPos,$YPos,$XPos+$MaxWidth,$YPos+$MaxHeight,5,$R,$G,$B);
  877. }
  878. $YOffset = 4 + $this->FontSize; $ID = 0;
  879. foreach($DataDescription["Description"] as $Key => $Value)
  880. {
  881. $this->drawFilledRoundedRectangle($XPos+10,$YPos+$YOffset-4,$XPos+14,$YPos+$YOffset-4,2,$this->Palette[$ID]["R"],$this->Palette[$ID]["G"],$this->Palette[$ID]["B"]);
  882. imagettftext($this->Picture,$this->FontSize,0,$XPos+22,$YPos+$YOffset,$C_TextColor,$this->FontName,$Value);
  883. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value);
  884. $TextHeight = $Position[1]-$Position[7];
  885. $YOffset = $YOffset + $TextHeight + 4;
  886. $ID++;
  887. }
  888. }
  889. /* Draw the data legends */
  890. function drawPieLegend($XPos,$YPos,$Data,$DataDescription,$R,$G,$B)
  891. {
  892. /* Validate the Data and DataDescription array */
  893. $this->validateDataDescription("drawPieLegend",$DataDescription,FALSE);
  894. $this->validateData("drawPieLegend",$Data);
  895. if ( !isset($DataDescription["Position"]) )
  896. return(-1);
  897. $C_TextColor =$this->AllocateColor($this->Picture,0,0,0);
  898. /* <-10->[8]<-4->Text<-10-> */
  899. $MaxWidth = 0; $MaxHeight = 8;
  900. foreach($Data as $Key => $Value)
  901. {
  902. $Value2 = $Value[$DataDescription["Position"]];
  903. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value2);
  904. $TextWidth = $Position[2]-$Position[0];
  905. $TextHeight = $Position[1]-$Position[7];
  906. if ( $TextWidth > $MaxWidth) { $MaxWidth = $TextWidth; }
  907. $MaxHeight = $MaxHeight + $TextHeight + 4;
  908. }
  909. $MaxHeight = $MaxHeight - 3;
  910. $MaxWidth = $MaxWidth + 32;
  911. $this->drawFilledRoundedRectangle($XPos+1,$YPos+1,$XPos+$MaxWidth+1,$YPos+$MaxHeight+1,5,$R-30,$G-30,$B-30);
  912. $this->drawFilledRoundedRectangle($XPos,$YPos,$XPos+$MaxWidth,$YPos+$MaxHeight,5,$R,$G,$B);
  913. $YOffset = 4 + $this->FontSize; $ID = 0;
  914. foreach($Data as $Key => $Value)
  915. {
  916. $Value2 = $Value[$DataDescription["Position"]];
  917. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value2);
  918. $TextHeight = $Position[1]-$Position[7];
  919. $this->drawFilledRectangle($XPos+10,$YPos+$YOffset-6,$XPos+14,$YPos+$YOffset-2,$this->Palette[$ID]["R"],$this->Palette[$ID]["G"],$this->Palette[$ID]["B"]);
  920. imagettftext($this->Picture,$this->FontSize,0,$XPos+22,$YPos+$YOffset,$C_TextColor,$this->FontName,$Value2);
  921. $YOffset = $YOffset + $TextHeight + 4;
  922. $ID++;
  923. }
  924. }
  925. /* Draw the graph title */
  926. function drawTitle($XPos,$YPos,$Value,$R,$G,$B,$XPos2=-1,$YPos2=-1,$Shadow=FALSE)
  927. {
  928. $C_TextColor = $this->AllocateColor($this->Picture,$R,$G,$B);
  929. if ( $XPos2 != -1 )
  930. {
  931. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value);
  932. $TextWidth = $Position[2]-$Position[0];
  933. $XPos = floor(( $XPos2 - $XPos - $TextWidth ) / 2 ) + $XPos;
  934. }
  935. if ( $YPos2 != -1 )
  936. {
  937. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value);
  938. $TextHeight = $Position[5]-$Position[3];
  939. $YPos = floor(( $YPos2 - $YPos - $TextHeight ) / 2 ) + $YPos;
  940. }
  941. if ( $Shadow )
  942. {
  943. $C_ShadowColor = $this->AllocateColor($this->Picture,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor);
  944. imagettftext($this->Picture,$this->FontSize,0,$XPos+$this->ShadowXDistance,$YPos+$this->ShadowYDistance,$C_ShadowColor,$this->FontName,$Value);
  945. }
  946. imagettftext($this->Picture,$this->FontSize,0,$XPos,$YPos,$C_TextColor,$this->FontName,$Value);
  947. }
  948. /* Draw a text box with text align & alpha properties */
  949. function drawTextBox($X1,$Y1,$X2,$Y2,$Text,$Angle=0,$R=255,$G=255,$B=255,$Align=ALIGN_LEFT,$Shadow=TRUE,$BgR=-1,$BgG=-1,$BgB=-1,$Alpha=100)
  950. {
  951. $Position = imageftbbox($this->FontSize,$Angle,$this->FontName,$Text);
  952. $TextWidth = $Position[2]-$Position[0];
  953. $TextHeight = $Position[5]-$Position[3];
  954. $AreaWidth = $X2 - $X1;
  955. $AreaHeight = $Y2 - $Y1;
  956. if ( $BgR != -1 && $BgG != -1 && $BgB != -1 )
  957. $this->drawFilledRectangle($X1,$Y1,$X2,$Y2,$BgR,$BgG,$BgB,FALSE,$Alpha);
  958. if ( $Align == ALIGN_TOP_LEFT ) { $X = $X1+1; $Y = $Y1+$this->FontSize+1; }
  959. if ( $Align == ALIGN_TOP_CENTER ) { $X = $X1+($AreaWidth/2)-($TextWidth/2); $Y = $Y1+$this->FontSize+1; }
  960. if ( $Align == ALIGN_TOP_RIGHT ) { $X = $X2-$TextWidth-1; $Y = $Y1+$this->FontSize+1; }
  961. if ( $Align == ALIGN_LEFT ) { $X = $X1+1; $Y = $Y1+($AreaHeight/2)-($TextHeight/2); }
  962. if ( $Align == ALIGN_CENTER ) { $X = $X1+($AreaWidth/2)-($TextWidth/2); $Y = $Y1+($AreaHeight/2)-($TextHeight/2); }
  963. if ( $Align == ALIGN_RIGHT ) { $X = $X2-$TextWidth-1; $Y = $Y1+($AreaHeight/2)-($TextHeight/2); }
  964. if ( $Align == ALIGN_BOTTOM_LEFT ) { $X = $X1+1; $Y = $Y2-1; }
  965. if ( $Align == ALIGN_BOTTOM_CENTER ) { $X = $X1+($AreaWidth/2)-($TextWidth/2); $Y = $Y2-1; }
  966. if ( $Align == ALIGN_BOTTOM_RIGHT ) { $X = $X2-$TextWidth-1; $Y = $Y2-1; }
  967. $C_TextColor =$this->AllocateColor($this->Picture,$R,$G,$B);
  968. $C_ShadowColor =$this->AllocateColor($this->Picture,0,0,0);
  969. if ( $Shadow )
  970. imagettftext($this->Picture,$this->FontSize,$Angle,$X+1,$Y+1,$C_ShadowColor,$this->FontName,$Text);
  971. imagettftext($this->Picture,$this->FontSize,$Angle,$X,$Y,$C_TextColor,$this->FontName,$Text);
  972. }
  973. /* Compute and draw the scale */
  974. function drawTreshold($Value,$R,$G,$B,$ShowLabel=FALSE,$ShowOnRight=FALSE,$TickWidth=4,$FreeText=NULL)
  975. {
  976. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  977. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  978. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  979. $C_TextColor =$this->AllocateColor($this->Picture,$R,$G,$B);
  980. $Y = $this->GArea_Y2 - ($Value - $this->VMin) * $this->DivisionRatio;
  981. if ( $Y <= $this->GArea_Y1 || $Y >= $this->GArea_Y2 )
  982. return(-1);
  983. if ( $TickWidth == 0 )
  984. $this->drawLine($this->GArea_X1,$Y,$this->GArea_X2,$Y,$R,$G,$B);
  985. else
  986. $this->drawDottedLine($this->GArea_X1,$Y,$this->GArea_X2,$Y,$TickWidth,$R,$G,$B);
  987. if ( $ShowLabel )
  988. {
  989. if ( $FreeText == NULL )
  990. { $Label = $Value; } else { $Label = $FreeText; }
  991. if ( $ShowOnRight )
  992. imagettftext($this->Picture,$this->FontSize,0,$this->GArea_X2+2,$Y+($this->FontSize/2),$C_TextColor,$this->FontName,$Label);
  993. else
  994. imagettftext($this->Picture,$this->FontSize,0,$this->GArea_X1+2,$Y-($this->FontSize/2),$C_TextColor,$this->FontName,$Label);
  995. }
  996. }
  997. /* This function put a label on a specific point */
  998. function setLabel($Data,$DataDescription,$SerieName,$ValueName,$Caption,$R=210,$G=210,$B=210)
  999. {
  1000. /* Validate the Data and DataDescription array */
  1001. $this->validateDataDescription("setLabel",$DataDescription);
  1002. $this->validateData("setLabel",$Data);
  1003. $ShadowFactor = 100;
  1004. $C_Label =$this->AllocateColor($this->Picture,$R,$G,$B);
  1005. $C_Shadow =$this->AllocateColor($this->Picture,$R-$ShadowFactor,$G-$ShadowFactor,$B-$ShadowFactor);
  1006. $C_TextColor =$this->AllocateColor($this->Picture,0,0,0);
  1007. $Cp = 0; $Found = FALSE;
  1008. foreach ( $Data as $Key => $Value )
  1009. {
  1010. if ( $Data[$Key][$DataDescription["Position"]] == $ValueName )
  1011. { $NumericalValue = $Data[$Key][$SerieName]; $Found = TRUE; }
  1012. if ( !$Found )
  1013. $Cp++;
  1014. }
  1015. $XPos = $this->GArea_X1 + $this->GAreaXOffset + ( $this->DivisionWidth * $Cp ) + 2;
  1016. $YPos = $this->GArea_Y2 - ($NumericalValue - $this->VMin) * $this->DivisionRatio;
  1017. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Caption);
  1018. $TextHeight = $Position[3] - $Position[5];
  1019. $TextWidth = $Position[2]-$Position[0] + 2;
  1020. $TextOffset = floor($TextHeight/2);
  1021. // Shadow
  1022. $Poly = array($XPos+1,$YPos+1,$XPos + 9,$YPos - $TextOffset,$XPos + 8,$YPos + $TextOffset + 2);
  1023. imagefilledpolygon($this->Picture,$Poly,3,$C_Shadow);
  1024. $this->drawLine($XPos,$YPos+1,$XPos + 9,$YPos - $TextOffset - .2,$R-$ShadowFactor,$G-$ShadowFactor,$B-$ShadowFactor);
  1025. $this->drawLine($XPos,$YPos+1,$XPos + 9,$YPos + $TextOffset + 2.2,$R-$ShadowFactor,$G-$ShadowFactor,$B-$ShadowFactor);
  1026. $this->drawFilledRectangle($XPos + 9,$YPos - $TextOffset-.2,$XPos + 13 + $TextWidth,$YPos + $TextOffset + 2.2,$R-$ShadowFactor,$G-$ShadowFactor,$B-$ShadowFactor);
  1027. // Label background
  1028. $Poly = array($XPos,$YPos,$XPos + 8,$YPos - $TextOffset - 1,$XPos + 8,$YPos + $TextOffset + 1);
  1029. imagefilledpolygon($this->Picture,$Poly,3,$C_Label);
  1030. $this->drawLine($XPos-1,$YPos,$XPos + 8,$YPos - $TextOffset - 1.2,$R,$G,$B);
  1031. $this->drawLine($XPos-1,$YPos,$XPos + 8,$YPos + $TextOffset + 1.2,$R,$G,$B);
  1032. $this->drawFilledRectangle($XPos + 8,$YPos - $TextOffset - 1.2,$XPos + 12 + $TextWidth,$YPos + $TextOffset + 1.2,$R,$G,$B);
  1033. imagettftext($this->Picture,$this->FontSize,0,$XPos + 10,$YPos + $TextOffset,$C_TextColor,$this->FontName,$Caption);
  1034. }
  1035. /* This function draw a plot graph */
  1036. function drawPlotGraph($Data,$DataDescription,$BigRadius=5,$SmallRadius=2,$R2=-1,$G2=-1,$B2=-1,$Shadow=FALSE)
  1037. {
  1038. /* Validate the Data and DataDescription array */
  1039. $this->validateDataDescription("drawPlotGraph",$DataDescription);
  1040. $this->validateData("drawPlotGraph",$Data);
  1041. $GraphID = 0;
  1042. $Ro = $R2; $Go = $G2; $Bo = $B2;
  1043. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1044. {
  1045. $ID = 0;
  1046. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1047. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  1048. $R = $this->Palette[$ColorID]["R"];
  1049. $G = $this->Palette[$ColorID]["G"];
  1050. $B = $this->Palette[$ColorID]["B"];
  1051. $R2 = $Ro; $G2 = $Go; $B2 = $Bo;
  1052. if ( isset($DataDescription["Symbol"][$ColName]) )
  1053. {
  1054. $Is_Alpha = ((ord ( file_get_contents ($DataDescription["Symbol"][$ColName], false, null, 25, 1)) & 6) & 4) == 4;
  1055. $Infos = getimagesize($DataDescription["Symbol"][$ColName]);
  1056. $ImageWidth = $Infos[0];
  1057. $ImageHeight = $Infos[1];
  1058. $Symbol = imagecreatefromgif($DataDescription["Symbol"][$ColName]);
  1059. }
  1060. $XPos = $this->GArea_X1 + $this->GAreaXOffset;
  1061. $Hsize = round($BigRadius/2);
  1062. $R3 = -1; $G3 = -1; $B3 = -1;
  1063. foreach ( $Data as $Key => $Values )
  1064. {
  1065. $Value = $Data[$Key][$ColName];
  1066. $YPos = $this->GArea_Y2 - (($Value-$this->VMin) * $this->DivisionRatio);
  1067. /* Save point into the image map if option activated */
  1068. if ( $this->BuildMap )
  1069. $this->addToImageMap($XPos-$Hsize,$YPos-$Hsize,$XPos+1+$Hsize,$YPos+$Hsize+1,$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"Plot");
  1070. if ( is_numeric($Value) )
  1071. {
  1072. if ( !isset($DataDescription["Symbol"][$ColName]) )
  1073. {
  1074. if ( $Shadow )
  1075. {
  1076. if ( $R3 !=-1 && $G3 !=-1 && $B3 !=-1 )
  1077. $this->drawFilledCircle($XPos+2,$YPos+2,$BigRadius,$R3,$G3,$B3);
  1078. else
  1079. {
  1080. $R3 = $this->Palette[$ColorID]["R"]-20; if ( $R3 < 0 ) { $R3 = 0; }
  1081. $G3 = $this->Palette[$ColorID]["G"]-20; if ( $G3 < 0 ) { $G3 = 0; }
  1082. $B3 = $this->Palette[$ColorID]["B"]-20; if ( $B3 < 0 ) { $B3 = 0; }
  1083. $this->drawFilledCircle($XPos+2,$YPos+2,$BigRadius,$R3,$G3,$B3);
  1084. }
  1085. }
  1086. $this->drawFilledCircle($XPos+1,$YPos+1,$BigRadius,$R,$G,$B);
  1087. if ( $SmallRadius != 0 )
  1088. {
  1089. if ( $R2 !=-1 && $G2 !=-1 && $B2 !=-1 )
  1090. $this->drawFilledCircle($XPos+1,$YPos+1,$SmallRadius,$R2,$G2,$B2);
  1091. else
  1092. {
  1093. $R2 = $this->Palette[$ColorID]["R"]-15; if ( $R2 < 0 ) { $R2 = 0; }
  1094. $G2 = $this->Palette[$ColorID]["G"]-15; if ( $G2 < 0 ) { $G2 = 0; }
  1095. $B2 = $this->Palette[$ColorID]["B"]-15; if ( $B2 < 0 ) { $B2 = 0; }
  1096. $this->drawFilledCircle($XPos+1,$YPos+1,$SmallRadius,$R2,$G2,$B2);
  1097. }
  1098. }
  1099. }
  1100. else
  1101. {
  1102. imagecopymerge($this->Picture,$Symbol,$XPos+1-$ImageWidth/2,$YPos+1-$ImageHeight/2,0,0,$ImageWidth,$ImageHeight,100);
  1103. }
  1104. }
  1105. $XPos = $XPos + $this->DivisionWidth;
  1106. }
  1107. $GraphID++;
  1108. }
  1109. }
  1110. /* This function draw a plot graph in an X/Y space */
  1111. function drawXYPlotGraph($Data,$DataDescription,$YSerieName,$XSerieName,$PaletteID=0,$BigRadius=5,$SmallRadius=2,$R2=-1,$G2=-1,$B2=-1,$Shadow=TRUE)
  1112. {
  1113. $R = $this->Palette[$PaletteID]["R"];
  1114. $G = $this->Palette[$PaletteID]["G"];
  1115. $B = $this->Palette[$PaletteID]["B"];
  1116. $R3 = -1; $G3 = -1; $B3 = -1;
  1117. $YLast = -1; $XLast = -1;
  1118. foreach ( $Data as $Key => $Values )
  1119. {
  1120. if ( isset($Data[$Key][$YSerieName]) && isset($Data[$Key][$XSerieName]) )
  1121. {
  1122. $X = $Data[$Key][$XSerieName];
  1123. $Y = $Data[$Key][$YSerieName];
  1124. $Y = $this->GArea_Y2 - (($Y-$this->VMin) * $this->DivisionRatio);
  1125. $X = $this->GArea_X1 + (($X-$this->VXMin) * $this->XDivisionRatio);
  1126. if ( $Shadow )
  1127. {
  1128. if ( $R3 !=-1 && $G3 !=-1 && $B3 !=-1 )
  1129. $this->drawFilledCircle($X+2,$Y+2,$BigRadius,$R3,$G3,$B3);
  1130. else
  1131. {
  1132. $R3 = $this->Palette[$PaletteID]["R"]-20; if ( $R < 0 ) { $R = 0; }
  1133. $G3 = $this->Palette[$PaletteID]["G"]-20; if ( $G < 0 ) { $G = 0; }
  1134. $B3 = $this->Palette[$PaletteID]["B"]-20; if ( $B < 0 ) { $B = 0; }
  1135. $this->drawFilledCircle($X+2,$Y+2,$BigRadius,$R3,$G3,$B3);
  1136. }
  1137. }
  1138. $this->drawFilledCircle($X+1,$Y+1,$BigRadius,$R,$G,$B);
  1139. if ( $R2 !=-1 && $G2 !=-1 && $B2 !=-1 )
  1140. $this->drawFilledCircle($X+1,$Y+1,$SmallRadius,$R2,$G2,$B2);
  1141. else
  1142. {
  1143. $R2 = $this->Palette[$PaletteID]["R"]+20; if ( $R > 255 ) { $R = 255; }
  1144. $G2 = $this->Palette[$PaletteID]["G"]+20; if ( $G > 255 ) { $G = 255; }
  1145. $B2 = $this->Palette[$PaletteID]["B"]+20; if ( $B > 255 ) { $B = 255; }
  1146. $this->drawFilledCircle($X+1,$Y+1,$SmallRadius,$R2,$G2,$B2);
  1147. }
  1148. }
  1149. }
  1150. }
  1151. /* This function draw an area between two series */
  1152. function drawArea($Data,$Serie1,$Serie2,$R,$G,$B,$Alpha = 50)
  1153. {
  1154. /* Validate the Data and DataDescription array */
  1155. $this->validateData("drawArea",$Data);
  1156. $LayerWidth = $this->GArea_X2-$this->GArea_X1;
  1157. $LayerHeight = $this->GArea_Y2-$this->GArea_Y1;
  1158. $this->Layers[0] = imagecreatetruecolor($LayerWidth,$LayerHeight);
  1159. $C_White =$this->AllocateColor($this->Layers[0],255,255,255);
  1160. imagefilledrectangle($this->Layers[0],0,0,$LayerWidth,$LayerHeight,$C_White);
  1161. imagecolortransparent($this->Layers[0],$C_White);
  1162. $C_Graph =$this->AllocateColor($this->Layers[0],$R,$G,$B);
  1163. $XPos = $this->GAreaXOffset;
  1164. $LastXPos = -1;
  1165. foreach ( $Data as $Key => $Values )
  1166. {
  1167. $Value1 = $Data[$Key][$Serie1];
  1168. $Value2 = $Data[$Key][$Serie2];
  1169. $YPos1 = $LayerHeight - (($Value1-$this->VMin) * $this->DivisionRatio);
  1170. $YPos2 = $LayerHeight - (($Value2-$this->VMin) * $this->DivisionRatio);
  1171. if ( $LastXPos != -1 )
  1172. {
  1173. $Points = "";
  1174. $Points[] = $LastXPos; $Points[] = $LastYPos1;
  1175. $Points[] = $LastXPos; $Points[] = $LastYPos2;
  1176. $Points[] = $XPos; $Points[] = $YPos2;
  1177. $Points[] = $XPos; $Points[] = $YPos1;
  1178. imagefilledpolygon($this->Layers[0],$Points,4,$C_Graph);
  1179. }
  1180. $LastYPos1 = $YPos1;
  1181. $LastYPos2 = $YPos2;
  1182. $LastXPos = $XPos;
  1183. $XPos = $XPos + $this->DivisionWidth;
  1184. }
  1185. imagecopymerge($this->Picture,$this->Layers[0],$this->GArea_X1,$this->GArea_Y1,0,0,$LayerWidth,$LayerHeight,$Alpha);
  1186. imagedestroy($this->Layers[0]);
  1187. }
  1188. /* This function write the values of the specified series */
  1189. function writeValues($Data,$DataDescription,$Series)
  1190. {
  1191. /* Validate the Data and DataDescription array */
  1192. $this->validateDataDescription("writeValues",$DataDescription);
  1193. $this->validateData("writeValues",$Data);
  1194. if ( !is_array($Series) ) { $Series = array($Series); }
  1195. foreach($Series as $Key => $Serie)
  1196. {
  1197. $ID = 0;
  1198. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1199. { if ( $keyI == $Serie ) { $ColorID = $ID; }; $ID++; }
  1200. $XPos = $this->GArea_X1 + $this->GAreaXOffset;
  1201. $XLast = -1;
  1202. foreach ( $Data as $Key => $Values )
  1203. {
  1204. if ( isset($Data[$Key][$Serie]) && is_numeric($Data[$Key][$Serie]))
  1205. {
  1206. $Value = $Data[$Key][$Serie];
  1207. $YPos = $this->GArea_Y2 - (($Value-$this->VMin) * $this->DivisionRatio);
  1208. $Positions = imagettfbbox($this->FontSize,0,$this->FontName,$Value);
  1209. $Width = $Positions[2] - $Positions[6]; $XOffset = $XPos - ($Width/2);
  1210. $Height = $Positions[3] - $Positions[7]; $YOffset = $YPos - 4;
  1211. $C_TextColor =$this->AllocateColor($this->Picture,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]);
  1212. imagettftext($this->Picture,$this->FontSize,0,$XOffset,$YOffset,$C_TextColor,$this->FontName,$Value);
  1213. }
  1214. $XPos = $XPos + $this->DivisionWidth;
  1215. }
  1216. }
  1217. }
  1218. /* This function draw a line graph */
  1219. function drawLineGraph($Data,$DataDescription,$SerieName="")
  1220. {
  1221. /* Validate the Data and DataDescription array */
  1222. $this->validateDataDescription("drawLineGraph",$DataDescription);
  1223. $this->validateData("drawLineGraph",$Data);
  1224. $GraphID = 0;
  1225. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1226. {
  1227. $ID = 0;
  1228. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1229. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  1230. if ( $SerieName == "" || $SerieName == $ColName )
  1231. {
  1232. $XPos = $this->GArea_X1 + $this->GAreaXOffset;
  1233. $XLast = -1;
  1234. foreach ( $Data as $Key => $Values )
  1235. {
  1236. if ( isset($Data[$Key][$ColName]))
  1237. {
  1238. $Value = $Data[$Key][$ColName];
  1239. $YPos = $this->GArea_Y2 - (($Value-$this->VMin) * $this->DivisionRatio);
  1240. /* Save point into the image map if option activated */
  1241. if ( $this->BuildMap )
  1242. $this->addToImageMap($XPos-3,$YPos-3,$XPos+3,$YPos+3,$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"Line");
  1243. if (!is_numeric($Value)) { $XLast = -1; }
  1244. if ( $XLast != -1 )
  1245. $this->drawLine($XLast,$YLast,$XPos,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE);
  1246. $XLast = $XPos;
  1247. $YLast = $YPos;
  1248. if (!is_numeric($Value)) { $XLast = -1; }
  1249. }
  1250. $XPos = $XPos + $this->DivisionWidth;
  1251. }
  1252. $GraphID++;
  1253. }
  1254. }
  1255. }
  1256. /* This function draw a line graph */
  1257. function drawXYGraph($Data,$DataDescription,$YSerieName,$XSerieName,$PaletteID=0)
  1258. {
  1259. $YLast = -1; $XLast = -1;
  1260. foreach ( $Data as $Key => $Values )
  1261. {
  1262. if ( isset($Data[$Key][$YSerieName]) && isset($Data[$Key][$XSerieName]) )
  1263. {
  1264. $X = $Data[$Key][$XSerieName];
  1265. $Y = $Data[$Key][$YSerieName];
  1266. $Y = $this->GArea_Y2 - (($Y-$this->VMin) * $this->DivisionRatio);
  1267. $X = $this->GArea_X1 + (($X-$this->VXMin) * $this->XDivisionRatio);
  1268. if ($XLast != -1 && $YLast != -1)
  1269. {
  1270. $this->drawLine($XLast,$YLast,$X,$Y,$this->Palette[$PaletteID]["R"],$this->Palette[$PaletteID]["G"],$this->Palette[$PaletteID]["B"],TRUE);
  1271. }
  1272. $XLast = $X;
  1273. $YLast = $Y;
  1274. }
  1275. }
  1276. }
  1277. /* This function draw a cubic curve */
  1278. function drawCubicCurve($Data,$DataDescription,$Accuracy=.1,$SerieName="")
  1279. {
  1280. /* Validate the Data and DataDescription array */
  1281. $this->validateDataDescription("drawCubicCurve",$DataDescription);
  1282. $this->validateData("drawCubicCurve",$Data);
  1283. $GraphID = 0;
  1284. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1285. {
  1286. if ( $SerieName == "" || $SerieName == $ColName )
  1287. {
  1288. $XIn = ""; $Yin = ""; $Yt = ""; $U = "";
  1289. $XIn[0] = 0; $YIn[0] = 0;
  1290. $ID = 0;
  1291. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1292. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  1293. $Index = 1;
  1294. $XLast = -1; $Missing = "";
  1295. foreach ( $Data as $Key => $Values )
  1296. {
  1297. if ( isset($Data[$Key][$ColName]) )
  1298. {
  1299. $Value = $Data[$Key][$ColName];
  1300. $XIn[$Index] = $Index;
  1301. $YIn[$Index] = $Value;
  1302. if ( !is_numeric($Value) ) { $Missing[$Index] = TRUE; }
  1303. $Index++;
  1304. }
  1305. }
  1306. $Index--;
  1307. $Yt[0] = 0;
  1308. $Yt[1] = 0;
  1309. $U[1] = 0;
  1310. for($i=2;$i<=$Index-1;$i++)
  1311. {
  1312. $Sig = ($XIn[$i] - $XIn[$i-1]) / ($XIn[$i+1] - $XIn[$i-1]);
  1313. $p = $Sig * $Yt[$i-1] + 2;
  1314. $Yt[$i] = ($Sig - 1) / $p;
  1315. $U[$i] = ($YIn[$i+1] - $YIn[$i]) / ($XIn[$i+1] - $XIn[$i]) - ($YIn[$i] - $YIn[$i-1]) / ($XIn[$i] - $XIn[$i-1]);
  1316. $U[$i] = (6 * $U[$i] / ($XIn[$i+1] - $XIn[$i-1]) - $Sig * $U[$i-1]) / $p;
  1317. }
  1318. $qn = 0;
  1319. $un = 0;
  1320. $Yt[$Index] = ($un - $qn * $U[$Index-1]) / ($qn * $Yt[$Index-1] + 1);
  1321. for($k=$Index-1;$k>=1;$k--)
  1322. $Yt[$k] = $Yt[$k] * $Yt[$k+1] + $U[$k];
  1323. $XPos = $this->GArea_X1 + $this->GAreaXOffset;
  1324. for($X=1;$X<=$Index;$X=$X+$Accuracy)
  1325. {
  1326. $klo = 1;
  1327. $khi = $Index;
  1328. $k = $khi - $klo;
  1329. while($k > 1)
  1330. {
  1331. $k = $khi - $klo;
  1332. If ( $XIn[$k] >= $X )
  1333. $khi = $k;
  1334. else
  1335. $klo = $k;
  1336. }
  1337. $klo = $khi - 1;
  1338. $h = $XIn[$khi] - $XIn[$klo];
  1339. $a = ($XIn[$khi] - $X) / $h;
  1340. $b = ($X - $XIn[$klo]) / $h;
  1341. $Value = $a * $YIn[$klo] + $b * $YIn[$khi] + (($a*$a*$a - $a) * $Yt[$klo] + ($b*$b*$b - $b) * $Yt[$khi]) * ($h*$h) / 6;
  1342. $YPos = $this->GArea_Y2 - (($Value-$this->VMin) * $this->DivisionRatio);
  1343. if ( $XLast != -1 && !isset($Missing[floor($X)]) && !isset($Missing[floor($X+1)]) )
  1344. $this->drawLine($XLast,$YLast,$XPos,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE);
  1345. $XLast = $XPos;
  1346. $YLast = $YPos;
  1347. $XPos = $XPos + $this->DivisionWidth * $Accuracy;
  1348. }
  1349. // Add potentialy missing values
  1350. $XPos = $XPos - $this->DivisionWidth * $Accuracy;
  1351. if ( $XPos < ($this->GArea_X2 - $this->GAreaXOffset) )
  1352. {
  1353. $YPos = $this->GArea_Y2 - (($YIn[$Index]-$this->VMin) * $this->DivisionRatio);
  1354. $this->drawLine($XLast,$YLast,$this->GArea_X2-$this->GAreaXOffset,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE);
  1355. }
  1356. $GraphID++;
  1357. }
  1358. }
  1359. }
  1360. /* This function draw a filled cubic curve */
  1361. function drawFilledCubicCurve($Data,$DataDescription,$Accuracy=.1,$Alpha=100,$AroundZero=FALSE)
  1362. {
  1363. /* Validate the Data and DataDescription array */
  1364. $this->validateDataDescription("drawFilledCubicCurve",$DataDescription);
  1365. $this->validateData("drawFilledCubicCurve",$Data);
  1366. $LayerWidth = $this->GArea_X2-$this->GArea_X1;
  1367. $LayerHeight = $this->GArea_Y2-$this->GArea_Y1;
  1368. $YZero = $LayerHeight - ((0-$this->VMin) * $this->DivisionRatio);
  1369. if ( $YZero > $LayerHeight ) { $YZero = $LayerHeight; }
  1370. $GraphID = 0;
  1371. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1372. {
  1373. $XIn = ""; $Yin = ""; $Yt = ""; $U = "";
  1374. $XIn[0] = 0; $YIn[0] = 0;
  1375. $ID = 0;
  1376. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1377. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  1378. $Index = 1;
  1379. $XLast = -1; $Missing = "";
  1380. foreach ( $Data as $Key => $Values )
  1381. {
  1382. $Value = $Data[$Key][$ColName];
  1383. $XIn[$Index] = $Index;
  1384. $YIn[$Index] = $Value;
  1385. if ( !is_numeric($Value) ) { $Missing[$Index] = TRUE; }
  1386. $Index++;
  1387. }
  1388. $Index--;
  1389. $Yt[0] = 0;
  1390. $Yt[1] = 0;
  1391. $U[1] = 0;
  1392. for($i=2;$i<=$Index-1;$i++)
  1393. {
  1394. $Sig = ($XIn[$i] - $XIn[$i-1]) / ($XIn[$i+1] - $XIn[$i-1]);
  1395. $p = $Sig * $Yt[$i-1] + 2;
  1396. $Yt[$i] = ($Sig - 1) / $p;
  1397. $U[$i] = ($YIn[$i+1] - $YIn[$i]) / ($XIn[$i+1] - $XIn[$i]) - ($YIn[$i] - $YIn[$i-1]) / ($XIn[$i] - $XIn[$i-1]);
  1398. $U[$i] = (6 * $U[$i] / ($XIn[$i+1] - $XIn[$i-1]) - $Sig * $U[$i-1]) / $p;
  1399. }
  1400. $qn = 0;
  1401. $un = 0;
  1402. $Yt[$Index] = ($un - $qn * $U[$Index-1]) / ($qn * $Yt[$Index-1] + 1);
  1403. for($k=$Index-1;$k>=1;$k--)
  1404. $Yt[$k] = $Yt[$k] * $Yt[$k+1] + $U[$k];
  1405. $Points = "";
  1406. $Points[] = $this->GAreaXOffset;
  1407. $Points[] = $LayerHeight;
  1408. $this->Layers[0] = imagecreatetruecolor($LayerWidth,$LayerHeight);
  1409. $C_White =$this->AllocateColor($this->Layers[0],255,255,255);
  1410. imagefilledrectangle($this->Layers[0],0,0,$LayerWidth,$LayerHeight,$C_White);
  1411. imagecolortransparent($this->Layers[0],$C_White);
  1412. $YLast = NULL;
  1413. $XPos = $this->GAreaXOffset; $PointsCount = 2;
  1414. for($X=1;$X<=$Index;$X=$X+$Accuracy)
  1415. {
  1416. $klo = 1;
  1417. $khi = $Index;
  1418. $k = $khi - $klo;
  1419. while($k > 1)
  1420. {
  1421. $k = $khi - $klo;
  1422. If ( $XIn[$k] >= $X )
  1423. $khi = $k;
  1424. else
  1425. $klo = $k;
  1426. }
  1427. $klo = $khi - 1;
  1428. $h = $XIn[$khi] - $XIn[$klo];
  1429. $a = ($XIn[$khi] - $X) / $h;
  1430. $b = ($X - $XIn[$klo]) / $h;
  1431. $Value = $a * $YIn[$klo] + $b * $YIn[$khi] + (($a*$a*$a - $a) * $Yt[$klo] + ($b*$b*$b - $b) * $Yt[$khi]) * ($h*$h) / 6;
  1432. $YPos = $LayerHeight - (($Value-$this->VMin) * $this->DivisionRatio);
  1433. if ( $YLast != NULL && $AroundZero && !isset($Missing[floor($X)]) && !isset($Missing[floor($X+1)]))
  1434. {
  1435. $aPoints = "";
  1436. $aPoints[] = $XLast;
  1437. $aPoints[] = $YLast;
  1438. $aPoints[] = $XPos;
  1439. $aPoints[] = $YPos;
  1440. $aPoints[] = $XPos;
  1441. $aPoints[] = $YZero;
  1442. $aPoints[] = $XLast;
  1443. $aPoints[] = $YZero;
  1444. $C_Graph =$this->AllocateColor($this->Layers[0],$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]);
  1445. imagefilledpolygon($this->Layers[0],$aPoints,4,$C_Graph);
  1446. }
  1447. if ( !isset($Missing[floor($X)]) || $YLast == NULL )
  1448. {
  1449. $PointsCount++;
  1450. $Points[] = $XPos;
  1451. $Points[] = $YPos;
  1452. }
  1453. else
  1454. {
  1455. $PointsCount++; $Points[] = $XLast; $Points[] = $LayerHeight;
  1456. }
  1457. $YLast = $YPos; $XLast = $XPos;
  1458. $XPos = $XPos + $this->DivisionWidth * $Accuracy;
  1459. }
  1460. // Add potentialy missing values
  1461. $XPos = $XPos - $this->DivisionWidth * $Accuracy;
  1462. if ( $XPos < ($LayerWidth-$this->GAreaXOffset) )
  1463. {
  1464. $YPos = $LayerHeight - (($YIn[$Index]-$this->VMin) * $this->DivisionRatio);
  1465. if ( $YLast != NULL && $AroundZero )
  1466. {
  1467. $aPoints = "";
  1468. $aPoints[] = $XLast;
  1469. $aPoints[] = $YLast;
  1470. $aPoints[] = $LayerWidth-$this->GAreaXOffset;
  1471. $aPoints[] = $YPos;
  1472. $aPoints[] = $LayerWidth-$this->GAreaXOffset;
  1473. $aPoints[] = $YZero;
  1474. $aPoints[] = $XLast;
  1475. $aPoints[] = $YZero;
  1476. $C_Graph =$this->AllocateColor($this->Layers[0],$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]);
  1477. imagefilledpolygon($this->Layers[0],$aPoints,4,$C_Graph);
  1478. }
  1479. if ( $YIn[$klo] != "" && $YIn[$khi] != "" || $YLast == NULL )
  1480. {
  1481. $PointsCount++;
  1482. $Points[] = $LayerWidth-$this->GAreaXOffset;
  1483. $Points[] = $YPos;
  1484. }
  1485. }
  1486. $Points[] = $LayerWidth-$this->GAreaXOffset;
  1487. $Points[] = $LayerHeight;
  1488. if ( !$AroundZero )
  1489. {
  1490. $C_Graph =$this->AllocateColor($this->Layers[0],$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]);
  1491. imagefilledpolygon($this->Layers[0],$Points,$PointsCount,$C_Graph);
  1492. }
  1493. imagecopymerge($this->Picture,$this->Layers[0],$this->GArea_X1,$this->GArea_Y1,0,0,$LayerWidth,$LayerHeight,$Alpha);
  1494. imagedestroy($this->Layers[0]);
  1495. $this->drawCubicCurve($Data,$DataDescription,$Accuracy,$ColName);
  1496. $GraphID++;
  1497. }
  1498. }
  1499. /* This function draw a filled line graph */
  1500. function drawFilledLineGraph($Data,$DataDescription,$Alpha=100,$AroundZero=FALSE)
  1501. {
  1502. $Empty = -2147483647;
  1503. /* Validate the Data and DataDescription array */
  1504. $this->validateDataDescription("drawFilledLineGraph",$DataDescription);
  1505. $this->validateData("drawFilledLineGraph",$Data);
  1506. $LayerWidth = $this->GArea_X2-$this->GArea_X1;
  1507. $LayerHeight = $this->GArea_Y2-$this->GArea_Y1;
  1508. $GraphID = 0;
  1509. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1510. {
  1511. $ID = 0;
  1512. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1513. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  1514. $aPoints = "";
  1515. $aPoints[] = $this->GAreaXOffset;
  1516. $aPoints[] = $LayerHeight;
  1517. $this->Layers[0] = imagecreatetruecolor($LayerWidth,$LayerHeight);
  1518. $C_White = $this->AllocateColor($this->Layers[0],255,255,255);
  1519. imagefilledrectangle($this->Layers[0],0,0,$LayerWidth,$LayerHeight,$C_White);
  1520. imagecolortransparent($this->Layers[0],$C_White);
  1521. $XPos = $this->GAreaXOffset;
  1522. $XLast = -1; $PointsCount = 2;
  1523. $YZero = $LayerHeight - ((0-$this->VMin) * $this->DivisionRatio);
  1524. if ( $YZero > $LayerHeight ) { $YZero = $LayerHeight; }
  1525. $YLast = $Empty;
  1526. foreach ( $Data as $Key => $Values )
  1527. {
  1528. $Value = $Data[$Key][$ColName];
  1529. $YPos = $LayerHeight - (($Value-$this->VMin) * $this->DivisionRatio);
  1530. /* Save point into the image map if option activated */
  1531. if ( $this->BuildMap )
  1532. $this->addToImageMap($XPos-3,$YPos-3,$XPos+3,$YPos+3,$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"FLine");
  1533. if ( !is_numeric($Value) )
  1534. {
  1535. $PointsCount++;
  1536. $aPoints[] = $XLast;
  1537. $aPoints[] = $LayerHeight;
  1538. $YLast = $Empty;
  1539. }
  1540. else
  1541. {
  1542. $PointsCount++;
  1543. if ( $YLast <> $Empty )
  1544. { $aPoints[] = $XPos; $aPoints[] = $YPos; }
  1545. else
  1546. { $PointsCount++; $aPoints[] = $XPos; $aPoints[] = $LayerHeight; $aPoints[] = $XPos; $aPoints[] = $YPos; }
  1547. if ($YLast <> $Empty && $AroundZero)
  1548. {
  1549. $Points = "";
  1550. $Points[] = $XLast; $Points[] = $YLast;
  1551. $Points[] = $XPos;
  1552. $Points[] = $YPos;
  1553. $Points[] = $XPos;
  1554. $Points[] = $YZero;
  1555. $Points[] = $XLast;
  1556. $Points[] = $YZero;
  1557. $C_Graph = $this->AllocateColor($this->Layers[0],$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]);
  1558. imagefilledpolygon($this->Layers[0],$Points,4,$C_Graph);
  1559. }
  1560. $YLast = $YPos;
  1561. }
  1562. $XLast = $XPos;
  1563. $XPos = $XPos + $this->DivisionWidth;
  1564. }
  1565. $aPoints[] = $LayerWidth - $this->GAreaXOffset;
  1566. $aPoints[] = $LayerHeight;
  1567. if ( $AroundZero == FALSE )
  1568. {
  1569. $C_Graph = $this->AllocateColor($this->Layers[0],$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]);
  1570. imagefilledpolygon($this->Layers[0],$aPoints,$PointsCount,$C_Graph);
  1571. }
  1572. imagecopymerge($this->Picture,$this->Layers[0],$this->GArea_X1,$this->GArea_Y1,0,0,$LayerWidth,$LayerHeight,$Alpha);
  1573. imagedestroy($this->Layers[0]);
  1574. $GraphID++;
  1575. $this->drawLineGraph($Data,$DataDescription,$ColName);
  1576. }
  1577. }
  1578. /* This function draw a bar graph */
  1579. function drawOverlayBarGraph($Data,$DataDescription,$Alpha=50)
  1580. {
  1581. /* Validate the Data and DataDescription array */
  1582. $this->validateDataDescription("drawOverlayBarGraph",$DataDescription);
  1583. $this->validateData("drawOverlayBarGraph",$Data);
  1584. $LayerWidth = $this->GArea_X2-$this->GArea_X1;
  1585. $LayerHeight = $this->GArea_Y2-$this->GArea_Y1;
  1586. $GraphID = 0;
  1587. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1588. {
  1589. $ID = 0;
  1590. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1591. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  1592. $this->Layers[$GraphID] = imagecreatetruecolor($LayerWidth,$LayerHeight);
  1593. $C_White = $this->AllocateColor($this->Layers[$GraphID],255,255,255);
  1594. $C_Graph = $this->AllocateColor($this->Layers[$GraphID],$this->Palette[$GraphID]["R"],$this->Palette[$GraphID]["G"],$this->Palette[$GraphID]["B"]);
  1595. imagefilledrectangle($this->Layers[$GraphID],0,0,$LayerWidth,$LayerHeight,$C_White);
  1596. imagecolortransparent($this->Layers[$GraphID],$C_White);
  1597. $XWidth = $this->DivisionWidth / 4;
  1598. $XPos = $this->GAreaXOffset;
  1599. $YZero = $LayerHeight - ((0-$this->VMin) * $this->DivisionRatio);
  1600. $XLast = -1; $PointsCount = 2;
  1601. foreach ( $Data as $Key => $Values )
  1602. {
  1603. if ( isset($Data[$Key][$ColName]) )
  1604. {
  1605. $Value = $Data[$Key][$ColName];
  1606. if ( is_numeric($Value) )
  1607. {
  1608. $YPos = $LayerHeight - (($Value-$this->VMin) * $this->DivisionRatio);
  1609. imagefilledrectangle($this->Layers[$GraphID],$XPos-$XWidth,$YPos,$XPos+$XWidth,$YZero,$C_Graph);
  1610. $X1 = floor($XPos - $XWidth + $this->GArea_X1); $Y1 = floor($YPos+$this->GArea_Y1) + .2;
  1611. $X2 = floor($XPos + $XWidth + $this->GArea_X1); $Y2 = $this->GArea_Y2 - ((0-$this->VMin) * $this->DivisionRatio);
  1612. if ( $X1 <= $this->GArea_X1 ) { $X1 = $this->GArea_X1 + 1; }
  1613. if ( $X2 >= $this->GArea_X2 ) { $X2 = $this->GArea_X2 - 1; }
  1614. /* Save point into the image map if option activated */
  1615. if ( $this->BuildMap )
  1616. $this->addToImageMap($X1,min($Y1,$Y2),$X2,max($Y1,$Y2),$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"oBar");
  1617. $this->drawLine($X1,$Y1,$X2,$Y1,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE);
  1618. }
  1619. }
  1620. $XPos = $XPos + $this->DivisionWidth;
  1621. }
  1622. $GraphID++;
  1623. }
  1624. for($i=0;$i<=($GraphID-1);$i++)
  1625. {
  1626. imagecopymerge($this->Picture,$this->Layers[$i],$this->GArea_X1,$this->GArea_Y1,0,0,$LayerWidth,$LayerHeight,$Alpha);
  1627. imagedestroy($this->Layers[$i]);
  1628. }
  1629. }
  1630. /* This function draw a bar graph */
  1631. function drawHorizontalBarGraph($Data,$DataDescription,$Shadow=FALSE,$Alpha=100)
  1632. {
  1633. /* Validate the Data and DataDescription array */
  1634. $this->validateDataDescription("drawHorizontalBarGraph",$DataDescription);
  1635. $this->validateData("drawHorizontalBarGraph",$Data);
  1636. $GraphID = 0;
  1637. $Series = count($DataDescription["Values"]);
  1638. $SeriesWidth = $this->DivisionWidth / ($Series+1);
  1639. $SerieXOffset = $this->DivisionWidth / 2 - $SeriesWidth / 2;
  1640. $YZero = $this->GArea_Y2 - ((0-$this->VMin) * $this->DivisionRatio);
  1641. if ( $YZero > $this->GArea_Y2 ) { $YZero = $this->GArea_Y2; }
  1642. $SerieID = 0;
  1643. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1644. {
  1645. $ID = 0;
  1646. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1647. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  1648. $XPos = $this->GArea_X1 + $this->GAreaXOffset - $SerieXOffset + $SeriesWidth * $SerieID;
  1649. $XLast = -1;
  1650. foreach ( $Data as $Key => $Values )
  1651. {
  1652. if ( isset($Data[$Key][$ColName]))
  1653. {
  1654. if ( is_numeric($Data[$Key][$ColName]) )
  1655. {
  1656. $Value = $Data[$Key][$ColName];
  1657. $YPos = $this->GArea_Y2 - (($Value-$this->VMin) * $this->DivisionRatio);
  1658. /* Save point into the image map if option activated */
  1659. if ( $this->BuildMap )
  1660. {
  1661. $this->addToImageMap($XPos+1,min($YZero,$YPos),$XPos+$SeriesWidth-1,max($YZero,$YPos),$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"Bar");
  1662. }
  1663. if ( $Shadow && $Alpha == 100 )
  1664. $X1 = $YZero;
  1665. $Y1 = $XPos+1;
  1666. $X2 = $YPos;
  1667. $Y2 = $XPos+$SeriesWidth-1;
  1668. $this->drawRectangle($XPos+1,$YZero,$XPos+$SeriesWidth-1,$YPos,25,25,25,TRUE,$Alpha);
  1669. $this->drawFilledRectangle($XPos+1,$YZero,$XPos+$SeriesWidth-1,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE,$Alpha);
  1670. //$this->drawRectangle($X1,$Y1,$X2,$Y2,$YPos,25,25,25,TRUE,$Alpha);
  1671. //$this->drawFilledRectangle($X1,$Y1,$X2,$Y2,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE,$Alpha);
  1672. }
  1673. }
  1674. $XPos = $XPos + $this->DivisionWidth;
  1675. }
  1676. $SerieID++;
  1677. }
  1678. }
  1679. /* This function draw a bar graph */
  1680. function drawBarGraph($Data,$DataDescription,$Shadow=FALSE,$Alpha=100)
  1681. {
  1682. /* Validate the Data and DataDescription array */
  1683. $this->validateDataDescription("drawBarGraph",$DataDescription);
  1684. $this->validateData("drawBarGraph",$Data);
  1685. $GraphID = 0;
  1686. $Series = count($DataDescription["Values"]);
  1687. $SeriesWidth = $this->DivisionWidth / ($Series+1);
  1688. $SerieXOffset = $this->DivisionWidth / 2 - $SeriesWidth / 2;
  1689. $YZero = $this->GArea_Y2 - ((0-$this->VMin) * $this->DivisionRatio);
  1690. if ( $YZero > $this->GArea_Y2 ) { $YZero = $this->GArea_Y2; }
  1691. $SerieID = 0;
  1692. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1693. {
  1694. $ID = 0;
  1695. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1696. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  1697. $XPos = $this->GArea_X1 + $this->GAreaXOffset - $SerieXOffset + $SeriesWidth * $SerieID;
  1698. $XLast = -1;
  1699. foreach ( $Data as $Key => $Values )
  1700. {
  1701. if ( isset($Data[$Key][$ColName]))
  1702. {
  1703. if ( is_numeric($Data[$Key][$ColName]) )
  1704. {
  1705. $Value = $Data[$Key][$ColName];
  1706. $ValueWidth = strlen("$Value") * 6;
  1707. $YPos = $this->GArea_Y2 - (($Value-$this->VMin) * $this->DivisionRatio);
  1708. /* Save point into the image map if option activated */
  1709. if ( $this->BuildMap )
  1710. {
  1711. $this->addToImageMap($XPos+1,min($YZero,$YPos),$XPos+$SeriesWidth-1,max($YZero,$YPos),$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"Bar");
  1712. }
  1713. if ( $Shadow && $Alpha == 100 )
  1714. $this->drawRectangle($XPos+1,$YZero,$XPos+$SeriesWidth-1,$YPos,25,25,25,TRUE,$Alpha);
  1715. $this->drawFilledRectangle($XPos+1,$YZero,$XPos+$SeriesWidth-1,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE,$Alpha);
  1716. imagettftext($this->Picture, $this->FontSize, 0, $XPos + ($SeriesWidth / 2) - ($ValueWidth / 2), $YPos - 2, $ColorID, $this->FontName, $Value);
  1717. }
  1718. }
  1719. $XPos = $XPos + $this->DivisionWidth;
  1720. }
  1721. $SerieID++;
  1722. }
  1723. }
  1724. /* This function draw a stacked bar graph */
  1725. function drawStackedBarGraph($Data,$DataDescription,$Alpha=50,$Contiguous=FALSE)
  1726. {
  1727. /* Validate the Data and DataDescription array */
  1728. $this->validateDataDescription("drawBarGraph",$DataDescription);
  1729. $this->validateData("drawBarGraph",$Data);
  1730. $GraphID = 0;
  1731. $Series = count($DataDescription["Values"]);
  1732. if ( $Contiguous )
  1733. $SeriesWidth = $this->DivisionWidth;
  1734. else
  1735. $SeriesWidth = $this->DivisionWidth * .8;
  1736. $YZero = $this->GArea_Y2 - ((0-$this->VMin) * $this->DivisionRatio);
  1737. if ( $YZero > $this->GArea_Y2 ) { $YZero = $this->GArea_Y2; }
  1738. $SerieID = 0; $LastValue = "";
  1739. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1740. {
  1741. $ID = 0;
  1742. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1743. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  1744. $XPos = $this->GArea_X1 + $this->GAreaXOffset - $SeriesWidth / 2;
  1745. $XLast = -1;
  1746. foreach ( $Data as $Key => $Values )
  1747. {
  1748. if ( isset($Data[$Key][$ColName]))
  1749. {
  1750. if ( is_numeric($Data[$Key][$ColName]) )
  1751. {
  1752. $Value = $Data[$Key][$ColName];
  1753. if ( isset($LastValue[$Key]) )
  1754. {
  1755. $YPos = $this->GArea_Y2 - ((($Value+$LastValue[$Key])-$this->VMin) * $this->DivisionRatio);
  1756. $YBottom = $this->GArea_Y2 - (($LastValue[$Key]-$this->VMin) * $this->DivisionRatio);
  1757. $LastValue[$Key] += $Value;
  1758. }
  1759. else
  1760. {
  1761. $YPos = $this->GArea_Y2 - (($Value-$this->VMin) * $this->DivisionRatio);
  1762. $YBottom = $YZero;
  1763. $LastValue[$Key] = $Value;
  1764. }
  1765. /* Save point into the image map if option activated */
  1766. if ( $this->BuildMap )
  1767. $this->addToImageMap($XPos+1,min($YBottom,$YPos),$XPos+$SeriesWidth-1,max($YBottom,$YPos),$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"sBar");
  1768. $this->drawFilledRectangle($XPos+1,$YBottom,$XPos+$SeriesWidth-1,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE,$Alpha);
  1769. }
  1770. }
  1771. $XPos = $XPos + $this->DivisionWidth;
  1772. }
  1773. $SerieID++;
  1774. }
  1775. }
  1776. /* This function draw a limits bar graphs */
  1777. function drawLimitsGraph($Data,$DataDescription,$R=0,$G=0,$B=0)
  1778. {
  1779. /* Validate the Data and DataDescription array */
  1780. $this->validateDataDescription("drawLimitsGraph",$DataDescription);
  1781. $this->validateData("drawLimitsGraph",$Data);
  1782. $XWidth = $this->DivisionWidth / 4;
  1783. $XPos = $this->GArea_X1 + $this->GAreaXOffset;
  1784. foreach ( $Data as $Key => $Values )
  1785. {
  1786. $Min = $Data[$Key][$DataDescription["Values"][0]];
  1787. $Max = $Data[$Key][$DataDescription["Values"][0]];
  1788. $GraphID = 0; $MaxID = 0; $MinID = 0;
  1789. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1790. {
  1791. if ( isset($Data[$Key][$ColName]) )
  1792. {
  1793. if ( $Data[$Key][$ColName] > $Max && is_numeric($Data[$Key][$ColName]))
  1794. { $Max = $Data[$Key][$ColName]; $MaxID = $GraphID; }
  1795. }
  1796. if ( isset($Data[$Key][$ColName]) && is_numeric($Data[$Key][$ColName]))
  1797. {
  1798. if ( $Data[$Key][$ColName] < $Min )
  1799. { $Min = $Data[$Key][$ColName]; $MinID = $GraphID; }
  1800. $GraphID++;
  1801. }
  1802. }
  1803. $YPos = $this->GArea_Y2 - (($Max-$this->VMin) * $this->DivisionRatio);
  1804. $X1 = floor($XPos - $XWidth); $Y1 = floor($YPos) - .2;
  1805. $X2 = floor($XPos + $XWidth);
  1806. if ( $X1 <= $this->GArea_X1 ) { $X1 = $this->GArea_X1 + 1; }
  1807. if ( $X2 >= $this->GArea_X2 ) { $X2 = $this->GArea_X2 - 1; }
  1808. $YPos = $this->GArea_Y2 - (($Min-$this->VMin) * $this->DivisionRatio);
  1809. $Y2 = floor($YPos) + .2;
  1810. $this->drawLine(floor($XPos)-.2,$Y1+1,floor($XPos)-.2,$Y2-1,$R,$G,$B,TRUE);
  1811. $this->drawLine(floor($XPos)+.2,$Y1+1,floor($XPos)+.2,$Y2-1,$R,$G,$B,TRUE);
  1812. $this->drawLine($X1,$Y1,$X2,$Y1,$this->Palette[$MaxID]["R"],$this->Palette[$MaxID]["G"],$this->Palette[$MaxID]["B"],FALSE);
  1813. $this->drawLine($X1,$Y2,$X2,$Y2,$this->Palette[$MinID]["R"],$this->Palette[$MinID]["G"],$this->Palette[$MinID]["B"],FALSE);
  1814. $XPos = $XPos + $this->DivisionWidth;
  1815. }
  1816. }
  1817. /* This function draw radar axis centered on the graph area */
  1818. function drawRadarAxis($Data,$DataDescription,$Mosaic=TRUE,$BorderOffset=10,$A_R=60,$A_G=60,$A_B=60,$S_R=200,$S_G=200,$S_B=200,$MaxValue=-1)
  1819. {
  1820. /* Validate the Data and DataDescription array */
  1821. $this->validateDataDescription("drawRadarAxis",$DataDescription);
  1822. $this->validateData("drawRadarAxis",$Data);
  1823. $C_TextColor = $this->AllocateColor($this->Picture,$A_R,$A_G,$A_B);
  1824. /* Draw radar axis */
  1825. $Points = count($Data);
  1826. $Radius = ( $this->GArea_Y2 - $this->GArea_Y1 ) / 2 - $BorderOffset;
  1827. $XCenter = ( $this->GArea_X2 - $this->GArea_X1 ) / 2 + $this->GArea_X1;
  1828. $YCenter = ( $this->GArea_Y2 - $this->GArea_Y1 ) / 2 + $this->GArea_Y1;
  1829. /* Search for the max value */
  1830. if ( $MaxValue == -1 )
  1831. {
  1832. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1833. {
  1834. foreach ( $Data as $Key => $Values )
  1835. {
  1836. if ( isset($Data[$Key][$ColName]))
  1837. if ( $Data[$Key][$ColName] > $MaxValue ) { $MaxValue = $Data[$Key][$ColName]; }
  1838. }
  1839. }
  1840. }
  1841. /* Draw the mosaic */
  1842. if ( $Mosaic )
  1843. {
  1844. $RadiusScale = $Radius / $MaxValue;
  1845. for ( $t=1; $t<=$MaxValue-1; $t++)
  1846. {
  1847. $TRadius = $RadiusScale * $t;
  1848. $LastX1 = -1;
  1849. for ( $i=0; $i<=$Points; $i++)
  1850. {
  1851. $Angle = -90 + $i * 360/$Points;
  1852. $X1 = cos($Angle * 3.1418 / 180 ) * $TRadius + $XCenter;
  1853. $Y1 = sin($Angle * 3.1418 / 180 ) * $TRadius + $YCenter;
  1854. $X2 = cos($Angle * 3.1418 / 180 ) * ($TRadius+$RadiusScale) + $XCenter;
  1855. $Y2 = sin($Angle * 3.1418 / 180 ) * ($TRadius+$RadiusScale) + $YCenter;
  1856. if ( $t % 2 == 1 && $LastX1 != -1)
  1857. {
  1858. $Plots = "";
  1859. $Plots[] = $X1; $Plots[] = $Y1;
  1860. $Plots[] = $X2; $Plots[] = $Y2;
  1861. $Plots[] = $LastX2; $Plots[] = $LastY2;
  1862. $Plots[] = $LastX1; $Plots[] = $LastY1;
  1863. $C_Graph = $this->AllocateColor($this->Picture,250,250,250);
  1864. imagefilledpolygon($this->Picture,$Plots,(count($Plots)+1)/2,$C_Graph);
  1865. }
  1866. $LastX1 = $X1; $LastY1= $Y1;
  1867. $LastX2 = $X2; $LastY2= $Y2;
  1868. }
  1869. }
  1870. }
  1871. /* Draw the spider web */
  1872. for ( $t=1; $t<=$MaxValue; $t++)
  1873. {
  1874. $TRadius = ( $Radius / $MaxValue ) * $t;
  1875. $LastX = -1;
  1876. for ( $i=0; $i<=$Points; $i++)
  1877. {
  1878. $Angle = -90 + $i * 360/$Points;
  1879. $X = cos($Angle * 3.1418 / 180 ) * $TRadius + $XCenter;
  1880. $Y = sin($Angle * 3.1418 / 180 ) * $TRadius + $YCenter;
  1881. if ( $LastX != -1 )
  1882. $this->drawDottedLine($LastX,$LastY,$X,$Y,4,$S_R,$S_G,$S_B);
  1883. $LastX = $X; $LastY= $Y;
  1884. }
  1885. }
  1886. /* Draw the axis */
  1887. for ( $i=0; $i<=$Points; $i++)
  1888. {
  1889. $Angle = -90 + $i * 360/$Points;
  1890. $X = cos($Angle * 3.1418 / 180 ) * $Radius + $XCenter;
  1891. $Y = sin($Angle * 3.1418 / 180 ) * $Radius + $YCenter;
  1892. $this->drawLine($XCenter,$YCenter,$X,$Y,$A_R,$A_G,$A_B);
  1893. $XOffset = 0; $YOffset = 0;
  1894. if (isset($Data[$i][$DataDescription["Position"]]))
  1895. {
  1896. $Label = $Data[$i][$DataDescription["Position"]];
  1897. $Positions = imagettfbbox($this->FontSize,0,$this->FontName,$Label);
  1898. $Width = $Positions[2] - $Positions[6];
  1899. $Height = $Positions[3] - $Positions[7];
  1900. if ( $Angle >= 0 && $Angle <= 90 )
  1901. $YOffset = $Height;
  1902. if ( $Angle > 90 && $Angle <= 180 )
  1903. { $YOffset = $Height; $XOffset = -$Width; }
  1904. if ( $Angle > 180 && $Angle <= 270 )
  1905. { $XOffset = -$Width; }
  1906. imagettftext($this->Picture,$this->FontSize,0,$X+$XOffset,$Y+$YOffset,$C_TextColor,$this->FontName,$Label);
  1907. }
  1908. }
  1909. /* Write the values */
  1910. for ( $t=1; $t<=$MaxValue; $t++)
  1911. {
  1912. $TRadius = ( $Radius / $MaxValue ) * $t;
  1913. $Angle = -90 + 360 / $Points;
  1914. $X1 = $XCenter;
  1915. $Y1 = $YCenter - $TRadius;
  1916. $X2 = cos($Angle * 3.1418 / 180 ) * $TRadius + $XCenter;
  1917. $Y2 = sin($Angle * 3.1418 / 180 ) * $TRadius + $YCenter;
  1918. $XPos = floor(($X2-$X1)/2) + $X1;
  1919. $YPos = floor(($Y2-$Y1)/2) + $Y1;
  1920. $Positions = imagettfbbox($this->FontSize,0,$this->FontName,$t);
  1921. $X = $XPos - ( $X+$Positions[2] - $X+$Positions[6] ) / 2;
  1922. $Y = $YPos + $this->FontSize;
  1923. $this->drawFilledRoundedRectangle($X+$Positions[6]-2,$Y+$Positions[7]-1,$X+$Positions[2]+4,$Y+$Positions[3]+1,2,240,240,240);
  1924. $this->drawRoundedRectangle($X+$Positions[6]-2,$Y+$Positions[7]-1,$X+$Positions[2]+4,$Y+$Positions[3]+1,2,220,220,220);
  1925. imagettftext($this->Picture,$this->FontSize,0,$X,$Y,$C_TextColor,$this->FontName,$t);
  1926. }
  1927. }
  1928. /* This function draw a radar graph centered on the graph area */
  1929. function drawRadar($Data,$DataDescription,$BorderOffset=10,$MaxValue=-1)
  1930. {
  1931. /* Validate the Data and DataDescription array */
  1932. $this->validateDataDescription("drawRadar",$DataDescription);
  1933. $this->validateData("drawRadar",$Data);
  1934. $Points = count($Data);
  1935. $Radius = ( $this->GArea_Y2 - $this->GArea_Y1 ) / 2 - $BorderOffset;
  1936. $XCenter = ( $this->GArea_X2 - $this->GArea_X1 ) / 2 + $this->GArea_X1;
  1937. $YCenter = ( $this->GArea_Y2 - $this->GArea_Y1 ) / 2 + $this->GArea_Y1;
  1938. /* Search for the max value */
  1939. if ( $MaxValue == -1 )
  1940. {
  1941. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1942. {
  1943. foreach ( $Data as $Key => $Values )
  1944. {
  1945. if ( isset($Data[$Key][$ColName]))
  1946. if ( $Data[$Key][$ColName] > $MaxValue ) { $MaxValue = $Data[$Key][$ColName]; }
  1947. }
  1948. }
  1949. }
  1950. $GraphID = 0;
  1951. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1952. {
  1953. $ID = 0;
  1954. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1955. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  1956. $Angle = -90;
  1957. $XLast = -1;
  1958. foreach ( $Data as $Key => $Values )
  1959. {
  1960. if ( isset($Data[$Key][$ColName]))
  1961. {
  1962. $Value = $Data[$Key][$ColName];
  1963. $Strength = ( $Radius / $MaxValue ) * $Value;
  1964. $XPos = cos($Angle * 3.1418 / 180 ) * $Strength + $XCenter;
  1965. $YPos = sin($Angle * 3.1418 / 180 ) * $Strength + $YCenter;
  1966. if ( $XLast != -1 )
  1967. $this->drawLine($XLast,$YLast,$XPos,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]);
  1968. if ( $XLast == -1 )
  1969. { $FirstX = $XPos; $FirstY = $YPos; }
  1970. $Angle = $Angle + (360/$Points);
  1971. $XLast = $XPos;
  1972. $YLast = $YPos;
  1973. }
  1974. }
  1975. $this->drawLine($XPos,$YPos,$FirstX,$FirstY,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]);
  1976. $GraphID++;
  1977. }
  1978. }
  1979. /* This function draw a radar graph centered on the graph area */
  1980. function drawFilledRadar($Data,$DataDescription,$Alpha=50,$BorderOffset=10,$MaxValue=-1)
  1981. {
  1982. /* Validate the Data and DataDescription array */
  1983. $this->validateDataDescription("drawFilledRadar",$DataDescription);
  1984. $this->validateData("drawFilledRadar",$Data);
  1985. $Points = count($Data);
  1986. $LayerWidth = $this->GArea_X2-$this->GArea_X1;
  1987. $LayerHeight = $this->GArea_Y2-$this->GArea_Y1;
  1988. $Radius = ( $this->GArea_Y2 - $this->GArea_Y1 ) / 2 - $BorderOffset;
  1989. $XCenter = ( $this->GArea_X2 - $this->GArea_X1 ) / 2;
  1990. $YCenter = ( $this->GArea_Y2 - $this->GArea_Y1 ) / 2;
  1991. /* Search for the max value */
  1992. if ( $MaxValue == -1 )
  1993. {
  1994. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1995. {
  1996. foreach ( $Data as $Key => $Values )
  1997. {
  1998. if ( isset($Data[$Key][$ColName]))
  1999. if ( $Data[$Key][$ColName] > $MaxValue && is_numeric($Data[$Key][$ColName])) { $MaxValue = $Data[$Key][$ColName]; }
  2000. }
  2001. }
  2002. }
  2003. $GraphID = 0;
  2004. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  2005. {
  2006. $ID = 0;
  2007. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  2008. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  2009. $Angle = -90;
  2010. $XLast = -1;
  2011. $Plots = "";
  2012. foreach ( $Data as $Key => $Values )
  2013. {
  2014. if ( isset($Data[$Key][$ColName]))
  2015. {
  2016. $Value = $Data[$Key][$ColName];
  2017. if ( !is_numeric($Value) ) { $Value = 0; }
  2018. $Strength = ( $Radius / $MaxValue ) * $Value;
  2019. $XPos = cos($Angle * 3.1418 / 180 ) * $Strength + $XCenter;
  2020. $YPos = sin($Angle * 3.1418 / 180 ) * $Strength + $YCenter;
  2021. $Plots[] = $XPos;
  2022. $Plots[] = $YPos;
  2023. $Angle = $Angle + (360/$Points);
  2024. $XLast = $XPos;
  2025. $YLast = $YPos;
  2026. }
  2027. }
  2028. if (isset($Plots[0]))
  2029. {
  2030. $Plots[] = $Plots[0];
  2031. $Plots[] = $Plots[1];
  2032. $this->Layers[0] = imagecreatetruecolor($LayerWidth,$LayerHeight);
  2033. $C_White = $this->AllocateColor($this->Layers[0],255,255,255);
  2034. imagefilledrectangle($this->Layers[0],0,0,$LayerWidth,$LayerHeight,$C_White);
  2035. imagecolortransparent($this->Layers[0],$C_White);
  2036. $C_Graph = $this->AllocateColor($this->Layers[0],$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]);
  2037. imagefilledpolygon($this->Layers[0],$Plots,(count($Plots)+1)/2,$C_Graph);
  2038. imagecopymerge($this->Picture,$this->Layers[0],$this->GArea_X1,$this->GArea_Y1,0,0,$LayerWidth,$LayerHeight,$Alpha);
  2039. imagedestroy($this->Layers[0]);
  2040. for($i=0;$i<=count($Plots)-4;$i=$i+2)
  2041. $this->drawLine($Plots[$i]+$this->GArea_X1,$Plots[$i+1]+$this->GArea_Y1,$Plots[$i+2]+$this->GArea_X1,$Plots[$i+3]+$this->GArea_Y1,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]);
  2042. }
  2043. $GraphID++;
  2044. }
  2045. }
  2046. /* This function draw a flat pie chart */
  2047. function drawBasicPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$R=255,$G=255,$B=255,$Decimals=0)
  2048. {
  2049. /* Validate the Data and DataDescription array */
  2050. $this->validateDataDescription("drawBasicPieGraph",$DataDescription,FALSE);
  2051. $this->validateData("drawBasicPieGraph",$Data);
  2052. /* Determine pie sum */
  2053. $Series = 0; $PieSum = 0;
  2054. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  2055. {
  2056. if ( $ColName != $DataDescription["Position"] )
  2057. {
  2058. $Series++;
  2059. foreach ( $Data as $Key => $Values )
  2060. {
  2061. if ( isset($Data[$Key][$ColName]))
  2062. $PieSum = $PieSum + $Data[$Key][$ColName]; $iValues[] = $Data[$Key][$ColName]; $iLabels[] = $Data[$Key][$DataDescription["Position"]];
  2063. }
  2064. }
  2065. }
  2066. /* Validate serie */
  2067. if ( $Series != 1 )
  2068. RaiseFatal("Pie chart can only accept one serie of data.");
  2069. $SpliceRatio = 360 / $PieSum;
  2070. $SplicePercent = 100 / $PieSum;
  2071. /* Calculate all polygons */
  2072. $Angle = 0; $TopPlots = "";
  2073. foreach($iValues as $Key => $Value)
  2074. {
  2075. $TopPlots[$Key][] = $XPos;
  2076. $TopPlots[$Key][] = $YPos;
  2077. /* Process labels position & size */
  2078. $Caption = "";
  2079. if ( !($DrawLabels == PIE_NOLABEL) )
  2080. {
  2081. $TAngle = $Angle+($Value*$SpliceRatio/2);
  2082. if ($DrawLabels == PIE_PERCENTAGE)
  2083. $Caption = (round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%";
  2084. elseif ($DrawLabels == PIE_LABELS)
  2085. $Caption = $iLabels[$Key];
  2086. elseif ($DrawLabels == PIE_PERCENTAGE_LABEL)
  2087. $Caption = $iLabels[$Key]."\r\n".(round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%";
  2088. elseif ($DrawLabels == PIE_PERCENTAGE_LABEL)
  2089. $Caption = $iLabels[$Key]."\r\n".(round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%";
  2090. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Caption);
  2091. $TextWidth = $Position[2]-$Position[0];
  2092. $TextHeight = abs($Position[1])+abs($Position[3]);
  2093. $TX = cos(($TAngle) * 3.1418 / 180 ) * ($Radius+10) + $XPos;
  2094. if ( $TAngle > 0 && $TAngle < 180 )
  2095. $TY = sin(($TAngle) * 3.1418 / 180 ) * ($Radius+10) + $YPos + 4;
  2096. else
  2097. $TY = sin(($TAngle) * 3.1418 / 180 ) * ($Radius+4) + $YPos - ($TextHeight/2);
  2098. if ( $TAngle > 90 && $TAngle < 270 )
  2099. $TX = $TX - $TextWidth;
  2100. $C_TextColor = $this->AllocateColor($this->Picture,70,70,70);
  2101. imagettftext($this->Picture,$this->FontSize,0,$TX,$TY,$C_TextColor,$this->FontName,$Caption);
  2102. }
  2103. /* Process pie slices */
  2104. for($iAngle=$Angle;$iAngle<=$Angle+$Value*$SpliceRatio;$iAngle=$iAngle+.5)
  2105. {
  2106. $TopX = cos($iAngle * 3.1418 / 180 ) * $Radius + $XPos;
  2107. $TopY = sin($iAngle * 3.1418 / 180 ) * $Radius + $YPos;
  2108. $TopPlots[$Key][] = $TopX;
  2109. $TopPlots[$Key][] = $TopY;
  2110. }
  2111. $TopPlots[$Key][] = $XPos;
  2112. $TopPlots[$Key][] = $YPos;
  2113. $Angle = $iAngle;
  2114. }
  2115. $PolyPlots = $TopPlots;
  2116. /* Set array values type to float --- PHP Bug with imagefilledpolygon casting to integer */
  2117. foreach ($TopPlots as $Key => $Value)
  2118. { foreach ($TopPlots[$Key] as $Key2 => $Value2) { settype($TopPlots[$Key][$Key2],"float"); } }
  2119. /* Draw Top polygons */
  2120. foreach ($PolyPlots as $Key => $Value)
  2121. {
  2122. $C_GraphLo = $this->AllocateColor($this->Picture,$this->Palette[$Key]["R"],$this->Palette[$Key]["G"],$this->Palette[$Key]["B"]);
  2123. imagefilledpolygon($this->Picture,$PolyPlots[$Key],(count($PolyPlots[$Key])+1)/2,$C_GraphLo);
  2124. }
  2125. $this->drawCircle($XPos-.5,$YPos-.5,$Radius,$R,$G,$B);
  2126. $this->drawCircle($XPos-.5,$YPos-.5,$Radius+.5,$R,$G,$B);
  2127. /* Draw Top polygons */
  2128. foreach ($TopPlots as $Key => $Value)
  2129. {
  2130. for($j=0;$j<=count($TopPlots[$Key])-4;$j=$j+2)
  2131. $this->drawLine($TopPlots[$Key][$j],$TopPlots[$Key][$j+1],$TopPlots[$Key][$j+2],$TopPlots[$Key][$j+3],$R,$G,$B);
  2132. }
  2133. }
  2134. function drawFlatPieGraphWithShadow($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$SpliceDistance=0,$Decimals=0)
  2135. {
  2136. $this->drawFlatPieGraph($Data,$DataDescription,$XPos+$this->ShadowXDistance,$YPos+$this->ShadowYDistance,$Radius,PIE_NOLABEL,$SpliceDistance,$Decimals,TRUE);
  2137. $this->drawFlatPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius,$DrawLabels,$SpliceDistance,$Decimals,FALSE);
  2138. }
  2139. /* This function draw a flat pie chart */
  2140. function drawFlatPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$SpliceDistance=0,$Decimals=0,$AllBlack=FALSE)
  2141. {
  2142. /* Validate the Data and DataDescription array */
  2143. $this->validateDataDescription("drawFlatPieGraph",$DataDescription,FALSE);
  2144. $this->validateData("drawFlatPieGraph",$Data);
  2145. $ShadowStatus = $this->ShadowActive ; $this->ShadowActive = FALSE;
  2146. /* Determine pie sum */
  2147. $Series = 0; $PieSum = 0;
  2148. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  2149. {
  2150. if ( $ColName != $DataDescription["Position"] )
  2151. {
  2152. $Series++;
  2153. foreach ( $Data as $Key => $Values )
  2154. {
  2155. if ( isset($Data[$Key][$ColName]))
  2156. $PieSum = $PieSum + $Data[$Key][$ColName]; $iValues[] = $Data[$Key][$ColName]; $iLabels[] = $Data[$Key][$DataDescription["Position"]];
  2157. }
  2158. }
  2159. }
  2160. /* Validate serie */
  2161. if ( $Series != 1 )
  2162. {
  2163. RaiseFatal("Pie chart can only accept one serie of data.");
  2164. return(0);
  2165. }
  2166. $SpliceRatio = 360 / $PieSum;
  2167. $SplicePercent = 100 / $PieSum;
  2168. /* Calculate all polygons */
  2169. $Angle = 0; $TopPlots = "";
  2170. foreach($iValues as $Key => $Value)
  2171. {
  2172. $XOffset = cos(($Angle+($Value/2*$SpliceRatio)) * 3.1418 / 180 ) * $SpliceDistance;
  2173. $YOffset = sin(($Angle+($Value/2*$SpliceRatio)) * 3.1418 / 180 ) * $SpliceDistance;
  2174. $TopPlots[$Key][] = round($XPos + $XOffset);
  2175. $TopPlots[$Key][] = round($YPos + $YOffset);
  2176. if ( $AllBlack )
  2177. { $Rc = $this->ShadowRColor; $Gc = $this->ShadowGColor; $Bc = $this->ShadowBColor; }
  2178. else
  2179. { $Rc = $this->Palette[$Key]["R"]; $Gc = $this->Palette[$Key]["G"]; $Bc = $this->Palette[$Key]["B"]; }
  2180. $XLineLast = ""; $YLineLast = "";
  2181. /* Process labels position & size */
  2182. $Caption = "";
  2183. if ( !($DrawLabels == PIE_NOLABEL) )
  2184. {
  2185. $TAngle = $Angle+($Value*$SpliceRatio/2);
  2186. if ($DrawLabels == PIE_PERCENTAGE)
  2187. $Caption = (round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%";
  2188. elseif ($DrawLabels == PIE_LABELS)
  2189. $Caption = $iLabels[$Key];
  2190. elseif ($DrawLabels == PIE_PERCENTAGE_LABEL)
  2191. $Caption = $iLabels[$Key]."\r\n".(round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%";
  2192. elseif ($DrawLabels == PIE_PERCENTAGE_LABEL)
  2193. $Caption = $iLabels[$Key]."\r\n".(round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%";
  2194. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Caption);
  2195. $TextWidth = $Position[2]-$Position[0];
  2196. $TextHeight = abs($Position[1])+abs($Position[3]);
  2197. $TX = cos(($TAngle) * 3.1418 / 180 ) * ($Radius+10+$SpliceDistance) + $XPos;
  2198. if ( $TAngle > 0 && $TAngle < 180 )
  2199. $TY = sin(($TAngle) * 3.1418 / 180 ) * ($Radius+10+$SpliceDistance) + $YPos + 4;
  2200. else
  2201. $TY = sin(($TAngle) * 3.1418 / 180 ) * ($Radius+$SpliceDistance+4) + $YPos - ($TextHeight/2);
  2202. if ( $TAngle > 90 && $TAngle < 270 )
  2203. $TX = $TX - $TextWidth;
  2204. $C_TextColor = $this->AllocateColor($this->Picture,70,70,70);
  2205. imagettftext($this->Picture,$this->FontSize,0,$TX,$TY,$C_TextColor,$this->FontName,$Caption);
  2206. }
  2207. /* Process pie slices */
  2208. if ( !$AllBlack )
  2209. $LineColor = $this->AllocateColor($this->Picture,$Rc,$Gc,$Bc);
  2210. else
  2211. $LineColor = $this->AllocateColor($this->Picture,$Rc,$Gc,$Bc);
  2212. $XLineLast = ""; $YLineLast = "";
  2213. for($iAngle=$Angle;$iAngle<=$Angle+$Value*$SpliceRatio;$iAngle=$iAngle+.5)
  2214. {
  2215. $PosX = cos($iAngle * 3.1418 / 180 ) * $Radius + $XPos + $XOffset;
  2216. $PosY = sin($iAngle * 3.1418 / 180 ) * $Radius + $YPos + $YOffset;
  2217. $TopPlots[$Key][] = round($PosX); $TopPlots[$Key][] = round($PosY);
  2218. if ( $iAngle == $Angle || $iAngle == $Angle+$Value*$SpliceRatio || $iAngle +.5 > $Angle+$Value*$SpliceRatio)
  2219. $this->drawLine($XPos+$XOffset,$YPos+$YOffset,$PosX,$PosY,$Rc,$Gc,$Bc);
  2220. if ( $XLineLast != "" )
  2221. $this->drawLine($XLineLast,$YLineLast,$PosX,$PosY,$Rc,$Gc,$Bc);
  2222. $XLineLast = $PosX; $YLineLast = $PosY;
  2223. }
  2224. $TopPlots[$Key][] = round($XPos + $XOffset); $TopPlots[$Key][] = round($YPos + $YOffset);
  2225. $Angle = $iAngle;
  2226. }
  2227. $PolyPlots = $TopPlots;
  2228. /* Draw Top polygons */
  2229. foreach ($PolyPlots as $Key => $Value)
  2230. {
  2231. if ( !$AllBlack )
  2232. $C_GraphLo = $this->AllocateColor($this->Picture,$this->Palette[$Key]["R"],$this->Palette[$Key]["G"],$this->Palette[$Key]["B"]);
  2233. else
  2234. $C_GraphLo = $this->AllocateColor($this->Picture,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor);
  2235. imagefilledpolygon($this->Picture,$PolyPlots[$Key],(count($PolyPlots[$Key])+1)/2,$C_GraphLo);
  2236. }
  2237. $this->ShadowActive = $ShadowStatus;
  2238. }
  2239. /* This function draw a pseudo-3D pie chart */
  2240. function drawPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$EnhanceColors=TRUE,$Skew=60,$SpliceHeight=20,$SpliceDistance=0,$Decimals=0)
  2241. {
  2242. /* Validate the Data and DataDescription array */
  2243. $this->validateDataDescription("drawPieGraph",$DataDescription,FALSE);
  2244. $this->validateData("drawPieGraph",$Data);
  2245. /* Determine pie sum */
  2246. $Series = 0; $PieSum = 0; $rPieSum = 0;
  2247. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  2248. {
  2249. if ( $ColName != $DataDescription["Position"] )
  2250. {
  2251. $Series++;
  2252. foreach ( $Data as $Key => $Values )
  2253. if ( isset($Data[$Key][$ColName]))
  2254. {
  2255. if ( $Data[$Key][$ColName] == 0 )
  2256. { $iValues[] = 0; $rValues[] = 0; $iLabels[] = $Data[$Key][$DataDescription["Position"]]; }
  2257. // Removed : $PieSum++; $rValues[] = 1;
  2258. else
  2259. { $PieSum += $Data[$Key][$ColName]; $iValues[] = $Data[$Key][$ColName]; $iLabels[] = $Data[$Key][$DataDescription["Position"]]; $rValues[] = $Data[$Key][$ColName]; $rPieSum += $Data[$Key][$ColName];}
  2260. }
  2261. }
  2262. }
  2263. /* Validate serie */
  2264. if ( $Series != 1 )
  2265. RaiseFatal("Pie chart can only accept one serie of data.");
  2266. $SpliceDistanceRatio = $SpliceDistance;
  2267. $SkewHeight = ($Radius * $Skew) / 100;
  2268. $SpliceRatio = (360 - $SpliceDistanceRatio * count($iValues) ) / $PieSum;
  2269. $SplicePercent = 100 / $PieSum;
  2270. $rSplicePercent = 100 / $rPieSum;
  2271. /* Calculate all polygons */
  2272. $Angle = 0; $CDev = 5;
  2273. $TopPlots = ""; $BotPlots = "";
  2274. $aTopPlots = ""; $aBotPlots = "";
  2275. foreach($iValues as $Key => $Value)
  2276. {
  2277. $XCenterPos = cos(($Angle-$CDev+($Value*$SpliceRatio+$SpliceDistanceRatio)/2) * 3.1418 / 180 ) * $SpliceDistance + $XPos;
  2278. $YCenterPos = sin(($Angle-$CDev+($Value*$SpliceRatio+$SpliceDistanceRatio)/2) * 3.1418 / 180 ) * $SpliceDistance + $YPos;
  2279. $XCenterPos2 = cos(($Angle+$CDev+($Value*$SpliceRatio+$SpliceDistanceRatio)/2) * 3.1418 / 180 ) * $SpliceDistance + $XPos;
  2280. $YCenterPos2 = sin(($Angle+$CDev+($Value*$SpliceRatio+$SpliceDistanceRatio)/2) * 3.1418 / 180 ) * $SpliceDistance + $YPos;
  2281. $TopPlots[$Key][] = round($XCenterPos); $BotPlots[$Key][] = round($XCenterPos);
  2282. $TopPlots[$Key][] = round($YCenterPos); $BotPlots[$Key][] = round($YCenterPos + $SpliceHeight);
  2283. $aTopPlots[$Key][] = $XCenterPos; $aBotPlots[$Key][] = $XCenterPos;
  2284. $aTopPlots[$Key][] = $YCenterPos; $aBotPlots[$Key][] = $YCenterPos + $SpliceHeight;
  2285. /* Process labels position & size */
  2286. $Caption = "";
  2287. if ( !($DrawLabels == PIE_NOLABEL) )
  2288. {
  2289. $TAngle = $Angle+($Value*$SpliceRatio/2);
  2290. if ($DrawLabels == PIE_PERCENTAGE)
  2291. $Caption = (round($rValues[$Key] * pow(10,$Decimals) * $rSplicePercent)/pow(10,$Decimals))."%";
  2292. elseif ($DrawLabels == PIE_LABELS)
  2293. $Caption = $iLabels[$Key];
  2294. elseif ($DrawLabels == PIE_PERCENTAGE_LABEL)
  2295. $Caption = $iLabels[$Key]."\r\n".(round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%";
  2296. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Caption);
  2297. $TextWidth = $Position[2]-$Position[0];
  2298. $TextHeight = abs($Position[1])+abs($Position[3]);
  2299. $TX = cos(($TAngle) * 3.1418 / 180 ) * ($Radius + 10)+ $XPos;
  2300. if ( $TAngle > 0 && $TAngle < 180 )
  2301. $TY = sin(($TAngle) * 3.1418 / 180 ) * ($SkewHeight + 10) + $YPos + $SpliceHeight + 4;
  2302. else
  2303. $TY = sin(($TAngle) * 3.1418 / 180 ) * ($SkewHeight + 4) + $YPos - ($TextHeight/2);
  2304. if ( $TAngle > 90 && $TAngle < 270 )
  2305. $TX = $TX - $TextWidth;
  2306. $C_TextColor = $this->AllocateColor($this->Picture,70,70,70);
  2307. imagettftext($this->Picture,$this->FontSize,0,$TX,$TY,$C_TextColor,$this->FontName,$Caption);
  2308. }
  2309. /* Process pie slices */
  2310. for($iAngle=$Angle;$iAngle<=$Angle+$Value*$SpliceRatio;$iAngle=$iAngle+.5)
  2311. {
  2312. $TopX = cos($iAngle * 3.1418 / 180 ) * $Radius + $XPos;
  2313. $TopY = sin($iAngle * 3.1418 / 180 ) * $SkewHeight + $YPos;
  2314. $TopPlots[$Key][] = round($TopX); $BotPlots[$Key][] = round($TopX);
  2315. $TopPlots[$Key][] = round($TopY); $BotPlots[$Key][] = round($TopY + $SpliceHeight);
  2316. $aTopPlots[$Key][] = $TopX; $aBotPlots[$Key][] = $TopX;
  2317. $aTopPlots[$Key][] = $TopY; $aBotPlots[$Key][] = $TopY + $SpliceHeight;
  2318. }
  2319. $TopPlots[$Key][] = round($XCenterPos2); $BotPlots[$Key][] = round($XCenterPos2);
  2320. $TopPlots[$Key][] = round($YCenterPos2); $BotPlots[$Key][] = round($YCenterPos2 + $SpliceHeight);
  2321. $aTopPlots[$Key][] = $XCenterPos2; $aBotPlots[$Key][] = $XCenterPos2;
  2322. $aTopPlots[$Key][] = $YCenterPos2; $aBotPlots[$Key][] = $YCenterPos2 + $SpliceHeight;
  2323. $Angle = $iAngle + $SpliceDistanceRatio;
  2324. }
  2325. /* Draw Bottom polygons */
  2326. foreach($iValues as $Key => $Value)
  2327. {
  2328. $C_GraphLo = $this->AllocateColor($this->Picture,$this->Palette[$Key]["R"],$this->Palette[$Key]["G"],$this->Palette[$Key]["B"],-20);
  2329. imagefilledpolygon($this->Picture,$BotPlots[$Key],(count($BotPlots[$Key])+1)/2,$C_GraphLo);
  2330. if ( $EnhanceColors ) { $En = -10; } else { $En = 0; }
  2331. for($j=0;$j<=count($aBotPlots[$Key])-4;$j=$j+2)
  2332. $this->drawLine($aBotPlots[$Key][$j],$aBotPlots[$Key][$j+1],$aBotPlots[$Key][$j+2],$aBotPlots[$Key][$j+3],$this->Palette[$Key]["R"]+$En,$this->Palette[$Key]["G"]+$En,$this->Palette[$Key]["B"]+$En);
  2333. }
  2334. /* Draw pie layers */
  2335. if ( $EnhanceColors ) { $ColorRatio = 30 / $SpliceHeight; } else { $ColorRatio = 25 / $SpliceHeight; }
  2336. for($i=$SpliceHeight-1;$i>=1;$i--)
  2337. {
  2338. foreach($iValues as $Key => $Value)
  2339. {
  2340. $C_GraphLo = $this->AllocateColor($this->Picture,$this->Palette[$Key]["R"],$this->Palette[$Key]["G"],$this->Palette[$Key]["B"],-10);
  2341. $Plots = ""; $Plot = 0;
  2342. foreach($TopPlots[$Key] as $Key2 => $Value2)
  2343. {
  2344. $Plot++;
  2345. if ( $Plot % 2 == 1 )
  2346. $Plots[] = $Value2;
  2347. else
  2348. $Plots[] = $Value2+$i;
  2349. }
  2350. imagefilledpolygon($this->Picture,$Plots,(count($Plots)+1)/2,$C_GraphLo);
  2351. $Index = count($Plots);
  2352. if ($EnhanceColors ) {$ColorFactor = -20 + ($SpliceHeight - $i) * $ColorRatio; } else { $ColorFactor = 0; }
  2353. $this->drawAntialiasPixel($Plots[0],$Plots[1],$this->Palette[$Key]["R"]+$ColorFactor,$this->Palette[$Key]["G"]+$ColorFactor,$this->Palette[$Key]["B"]+$ColorFactor);
  2354. $this->drawAntialiasPixel($Plots[2],$Plots[3],$this->Palette[$Key]["R"]+$ColorFactor,$this->Palette[$Key]["G"]+$ColorFactor,$this->Palette[$Key]["B"]+$ColorFactor);
  2355. $this->drawAntialiasPixel($Plots[$Index-4],$Plots[$Index-3],$this->Palette[$Key]["R"]+$ColorFactor,$this->Palette[$Key]["G"]+$ColorFactor,$this->Palette[$Key]["B"]+$ColorFactor);
  2356. }
  2357. }
  2358. /* Draw Top polygons */
  2359. for($Key=count($iValues)-1;$Key>=0;$Key--)
  2360. {
  2361. $C_GraphLo = $this->AllocateColor($this->Picture,$this->Palette[$Key]["R"],$this->Palette[$Key]["G"],$this->Palette[$Key]["B"]);
  2362. imagefilledpolygon($this->Picture,$TopPlots[$Key],(count($TopPlots[$Key])+1)/2,$C_GraphLo);
  2363. if ( $EnhanceColors ) { $En = 10; } else { $En = 0; }
  2364. for($j=0;$j<=count($aTopPlots[$Key])-4;$j=$j+2)
  2365. $this->drawLine($aTopPlots[$Key][$j],$aTopPlots[$Key][$j+1],$aTopPlots[$Key][$j+2],$aTopPlots[$Key][$j+3],$this->Palette[$Key]["R"]+$En,$this->Palette[$Key]["G"]+$En,$this->Palette[$Key]["B"]+$En);
  2366. }
  2367. }
  2368. /* This function can be used to set the background color */
  2369. function drawBackground($R,$G,$B)
  2370. {
  2371. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2372. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2373. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2374. $C_Background = $this->AllocateColor($this->Picture,$R,$G,$B);
  2375. imagefilledrectangle($this->Picture,0,0,$this->XSize,$this->YSize,$C_Background);
  2376. }
  2377. /* This function can be used to set the background color */
  2378. function drawGraphAreaGradient($R,$G,$B,$Decay,$Target=TARGET_GRAPHAREA)
  2379. {
  2380. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2381. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2382. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2383. if ( $Target == TARGET_GRAPHAREA ) { $X1 = $this->GArea_X1+1; $X2 = $this->GArea_X2-1; $Y1 = $this->GArea_Y1+1; $Y2 = $this->GArea_Y2; }
  2384. if ( $Target == TARGET_BACKGROUND ) { $X1 = 0; $X2 = $this->XSize; $Y1 = 0; $Y2 = $this->YSize; }
  2385. /* Positive gradient */
  2386. if ( $Decay > 0 )
  2387. {
  2388. $YStep = ($Y2 - $Y1 - 2) / $Decay;
  2389. for($i=0;$i<=$Decay;$i++)
  2390. {
  2391. $R-=1;$G-=1;$B-=1;
  2392. $Yi1 = $Y1 + ( $i * $YStep );
  2393. $Yi2 = ceil( $Yi1 + ( $i * $YStep ) + $YStep );
  2394. if ( $Yi2 >= $Yi2 ) { $Yi2 = $Y2-1; }
  2395. $C_Background = $this->AllocateColor($this->Picture,$R,$G,$B);
  2396. imagefilledrectangle($this->Picture,$X1,$Yi1,$X2,$Yi2,$C_Background);
  2397. }
  2398. }
  2399. /* Negative gradient */
  2400. if ( $Decay < 0 )
  2401. {
  2402. $YStep = ($Y2 - $Y1 - 2) / -$Decay;
  2403. $Yi1 = $Y1; $Yi2 = $Y1+$YStep;
  2404. for($i=-$Decay;$i>=0;$i--)
  2405. {
  2406. $R+=1;$G+=1;$B+=1;
  2407. $C_Background = $this->AllocateColor($this->Picture,$R,$G,$B);
  2408. imagefilledrectangle($this->Picture,$X1,$Yi1,$X2,$Yi2,$C_Background);
  2409. $Yi1+= $YStep;
  2410. $Yi2+= $YStep;
  2411. if ( $Yi2 >= $Yi2 ) { $Yi2 = $Y2-1; }
  2412. }
  2413. }
  2414. }
  2415. /* This function create a rectangle with antialias */
  2416. function drawRectangle($X1,$Y1,$X2,$Y2,$R,$G,$B)
  2417. {
  2418. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2419. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2420. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2421. $C_Rectangle = $this->AllocateColor($this->Picture,$R,$G,$B);
  2422. $X1=$X1-.2;$Y1=$Y1-.2;
  2423. $X2=$X2+.2;$Y2=$Y2+.2;
  2424. $this->drawLine($X1,$Y1,$X2,$Y1,$R,$G,$B);
  2425. $this->drawLine($X2,$Y1,$X2,$Y2,$R,$G,$B);
  2426. $this->drawLine($X2,$Y2,$X1,$Y2,$R,$G,$B);
  2427. $this->drawLine($X1,$Y2,$X1,$Y1,$R,$G,$B);
  2428. }
  2429. /* This function create a filled rectangle with antialias */
  2430. function drawFilledRectangle($X1,$Y1,$X2,$Y2,$R,$G,$B,$DrawBorder=TRUE,$Alpha=100,$NoFallBack=FALSE)
  2431. {
  2432. if ( $X2 < $X1 ) { list($X1, $X2) = array($X2, $X1); }
  2433. if ( $Y2 < $Y1 ) { list($Y1, $Y2) = array($Y2, $Y1); }
  2434. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2435. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2436. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2437. if ( $Alpha == 100 )
  2438. {
  2439. /* Process shadows */
  2440. if ( $this->ShadowActive && !$NoFallBack )
  2441. {
  2442. $this->drawFilledRectangle($X1+$this->ShadowXDistance,$Y1+$this->ShadowYDistance,$X2+$this->ShadowXDistance,$Y2+$this->ShadowYDistance,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor,FALSE,$this->ShadowAlpha,TRUE);
  2443. if ( $this->ShadowBlur != 0 )
  2444. {
  2445. $AlphaDecay = ($this->ShadowAlpha / $this->ShadowBlur);
  2446. for($i=1; $i<=$this->ShadowBlur; $i++)
  2447. $this->drawFilledRectangle($X1+$this->ShadowXDistance-$i/2,$Y1+$this->ShadowYDistance-$i/2,$X2+$this->ShadowXDistance-$i/2,$Y2+$this->ShadowYDistance-$i/2,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor,FALSE,$this->ShadowAlpha-$AlphaDecay*$i,TRUE);
  2448. for($i=1; $i<=$this->ShadowBlur; $i++)
  2449. $this->drawFilledRectangle($X1+$this->ShadowXDistance+$i/2,$Y1+$this->ShadowYDistance+$i/2,$X2+$this->ShadowXDistance+$i/2,$Y2+$this->ShadowYDistance+$i/2,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor,FALSE,$this->ShadowAlpha-$AlphaDecay*$i,TRUE);
  2450. }
  2451. }
  2452. $C_Rectangle = $this->AllocateColor($this->Picture,$R,$G,$B);
  2453. imagefilledrectangle($this->Picture,round($X1),round($Y1),round($X2),round($Y2),$C_Rectangle);
  2454. }
  2455. else
  2456. {
  2457. $LayerWidth = abs($X2-$X1)+2;
  2458. $LayerHeight = abs($Y2-$Y1)+2;
  2459. $this->Layers[0] = imagecreatetruecolor($LayerWidth,$LayerHeight);
  2460. $C_White = $this->AllocateColor($this->Layers[0],255,255,255);
  2461. imagefilledrectangle($this->Layers[0],0,0,$LayerWidth,$LayerHeight,$C_White);
  2462. imagecolortransparent($this->Layers[0],$C_White);
  2463. $C_Rectangle = $this->AllocateColor($this->Layers[0],$R,$G,$B);
  2464. imagefilledrectangle($this->Layers[0],round(1),round(1),round($LayerWidth-1),round($LayerHeight-1),$C_Rectangle);
  2465. imagecopymerge($this->Picture,$this->Layers[0],round(min($X1,$X2)-1),round(min($Y1,$Y2)-1),0,0,$LayerWidth,$LayerHeight,$Alpha);
  2466. imagedestroy($this->Layers[0]);
  2467. }
  2468. if ( $DrawBorder )
  2469. {
  2470. $ShadowSettings = $this->ShadowActive; $this->ShadowActive = FALSE;
  2471. $this->drawRectangle($X1,$Y1,$X2,$Y2,$R,$G,$B);
  2472. $this->ShadowActive = $ShadowSettings;
  2473. }
  2474. }
  2475. /* This function create a rectangle with rounded corners and antialias */
  2476. function drawRoundedRectangle($X1,$Y1,$X2,$Y2,$Radius,$R,$G,$B)
  2477. {
  2478. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2479. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2480. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2481. $C_Rectangle = $this->AllocateColor($this->Picture,$R,$G,$B);
  2482. $Step = 90 / ((3.1418 * $Radius)/2);
  2483. for($i=0;$i<=90;$i=$i+$Step)
  2484. {
  2485. $X = cos(($i+180)*3.1418/180) * $Radius + $X1 + $Radius;
  2486. $Y = sin(($i+180)*3.1418/180) * $Radius + $Y1 + $Radius;
  2487. $this->drawAntialiasPixel($X,$Y,$R,$G,$B);
  2488. $X = cos(($i-90)*3.1418/180) * $Radius + $X2 - $Radius;
  2489. $Y = sin(($i-90)*3.1418/180) * $Radius + $Y1 + $Radius;
  2490. $this->drawAntialiasPixel($X,$Y,$R,$G,$B);
  2491. $X = cos(($i)*3.1418/180) * $Radius + $X2 - $Radius;
  2492. $Y = sin(($i)*3.1418/180) * $Radius + $Y2 - $Radius;
  2493. $this->drawAntialiasPixel($X,$Y,$R,$G,$B);
  2494. $X = cos(($i+90)*3.1418/180) * $Radius + $X1 + $Radius;
  2495. $Y = sin(($i+90)*3.1418/180) * $Radius + $Y2 - $Radius;
  2496. $this->drawAntialiasPixel($X,$Y,$R,$G,$B);
  2497. }
  2498. $X1=$X1-.2;$Y1=$Y1-.2;
  2499. $X2=$X2+.2;$Y2=$Y2+.2;
  2500. $this->drawLine($X1+$Radius,$Y1,$X2-$Radius,$Y1,$R,$G,$B);
  2501. $this->drawLine($X2,$Y1+$Radius,$X2,$Y2-$Radius,$R,$G,$B);
  2502. $this->drawLine($X2-$Radius,$Y2,$X1+$Radius,$Y2,$R,$G,$B);
  2503. $this->drawLine($X1,$Y2-$Radius,$X1,$Y1+$Radius,$R,$G,$B);
  2504. }
  2505. /* This function create a filled rectangle with rounded corners and antialias */
  2506. function drawFilledRoundedRectangle($X1,$Y1,$X2,$Y2,$Radius,$R,$G,$B)
  2507. {
  2508. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2509. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2510. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2511. $C_Rectangle = $this->AllocateColor($this->Picture,$R,$G,$B);
  2512. $Step = 90 / ((3.1418 * $Radius)/2);
  2513. for($i=0;$i<=90;$i=$i+$Step)
  2514. {
  2515. $Xi1 = cos(($i+180)*3.1418/180) * $Radius + $X1 + $Radius;
  2516. $Yi1 = sin(($i+180)*3.1418/180) * $Radius + $Y1 + $Radius;
  2517. $Xi2 = cos(($i-90)*3.1418/180) * $Radius + $X2 - $Radius;
  2518. $Yi2 = sin(($i-90)*3.1418/180) * $Radius + $Y1 + $Radius;
  2519. $Xi3 = cos(($i)*3.1418/180) * $Radius + $X2 - $Radius;
  2520. $Yi3 = sin(($i)*3.1418/180) * $Radius + $Y2 - $Radius;
  2521. $Xi4 = cos(($i+90)*3.1418/180) * $Radius + $X1 + $Radius;
  2522. $Yi4 = sin(($i+90)*3.1418/180) * $Radius + $Y2 - $Radius;
  2523. imageline($this->Picture,$Xi1,$Yi1,$X1+$Radius,$Yi1,$C_Rectangle);
  2524. imageline($this->Picture,$X2-$Radius,$Yi2,$Xi2,$Yi2,$C_Rectangle);
  2525. imageline($this->Picture,$X2-$Radius,$Yi3,$Xi3,$Yi3,$C_Rectangle);
  2526. imageline($this->Picture,$Xi4,$Yi4,$X1+$Radius,$Yi4,$C_Rectangle);
  2527. $this->drawAntialiasPixel($Xi1,$Yi1,$R,$G,$B);
  2528. $this->drawAntialiasPixel($Xi2,$Yi2,$R,$G,$B);
  2529. $this->drawAntialiasPixel($Xi3,$Yi3,$R,$G,$B);
  2530. $this->drawAntialiasPixel($Xi4,$Yi4,$R,$G,$B);
  2531. }
  2532. imagefilledrectangle($this->Picture,$X1,$Y1+$Radius,$X2,$Y2-$Radius,$C_Rectangle);
  2533. imagefilledrectangle($this->Picture,$X1+$Radius,$Y1,$X2-$Radius,$Y2,$C_Rectangle);
  2534. $X1=$X1-.2;$Y1=$Y1-.2;
  2535. $X2=$X2+.2;$Y2=$Y2+.2;
  2536. $this->drawLine($X1+$Radius,$Y1,$X2-$Radius,$Y1,$R,$G,$B);
  2537. $this->drawLine($X2,$Y1+$Radius,$X2,$Y2-$Radius,$R,$G,$B);
  2538. $this->drawLine($X2-$Radius,$Y2,$X1+$Radius,$Y2,$R,$G,$B);
  2539. $this->drawLine($X1,$Y2-$Radius,$X1,$Y1+$Radius,$R,$G,$B);
  2540. }
  2541. /* This function create a circle with antialias */
  2542. function drawCircle($Xc,$Yc,$Height,$R,$G,$B,$Width=0)
  2543. {
  2544. if ( $Width == 0 ) { $Width = $Height; }
  2545. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2546. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2547. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2548. $C_Circle = $this->AllocateColor($this->Picture,$R,$G,$B);
  2549. $Step = 360 / (2 * 3.1418 * max($Width,$Height));
  2550. for($i=0;$i<=360;$i=$i+$Step)
  2551. {
  2552. $X = cos($i*3.1418/180) * $Height + $Xc;
  2553. $Y = sin($i*3.1418/180) * $Width + $Yc;
  2554. $this->drawAntialiasPixel($X,$Y,$R,$G,$B);
  2555. }
  2556. }
  2557. /* This function create a filled circle/ellipse with antialias */
  2558. function drawFilledCircle($Xc,$Yc,$Height,$R,$G,$B,$Width=0)
  2559. {
  2560. if ( $Width == 0 ) { $Width = $Height; }
  2561. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2562. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2563. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2564. $C_Circle = $this->AllocateColor($this->Picture,$R,$G,$B);
  2565. $Step = 360 / (2 * 3.1418 * max($Width,$Height));
  2566. for($i=90;$i<=270;$i=$i+$Step)
  2567. {
  2568. $X1 = cos($i*3.1418/180) * $Height + $Xc;
  2569. $Y1 = sin($i*3.1418/180) * $Width + $Yc;
  2570. $X2 = cos((180-$i)*3.1418/180) * $Height + $Xc;
  2571. $Y2 = sin((180-$i)*3.1418/180) * $Width + $Yc;
  2572. $this->drawAntialiasPixel($X1-1,$Y1-1,$R,$G,$B);
  2573. $this->drawAntialiasPixel($X2-1,$Y2-1,$R,$G,$B);
  2574. if ( ($Y1-1) > $Yc - max($Width,$Height) )
  2575. imageline($this->Picture,$X1,$Y1-1,$X2-1,$Y2-1,$C_Circle);
  2576. }
  2577. }
  2578. /* This function will draw a filled ellipse */
  2579. function drawEllipse($Xc,$Yc,$Height,$Width,$R,$G,$B)
  2580. { $this->drawCircle($Xc,$Yc,$Height,$R,$G,$B,$Width); }
  2581. /* This function will draw an ellipse */
  2582. function drawFilledEllipse($Xc,$Yc,$Height,$Width,$R,$G,$B)
  2583. { $this->drawFilledCircle($Xc,$Yc,$Height,$R,$G,$B,$Width); }
  2584. /* This function create a line with antialias */
  2585. function drawLine($X1,$Y1,$X2,$Y2,$R,$G,$B,$GraphFunction=FALSE)
  2586. {
  2587. if ( $this->LineDotSize > 1 ) { $this->drawDottedLine($X1,$Y1,$X2,$Y2,$this->LineDotSize,$R,$G,$B,$GraphFunction); return(0); }
  2588. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2589. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2590. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2591. $Distance = sqrt(($X2-$X1)*($X2-$X1)+($Y2-$Y1)*($Y2-$Y1));
  2592. if ( $Distance == 0 )
  2593. return(-1);
  2594. $XStep = ($X2-$X1) / $Distance;
  2595. $YStep = ($Y2-$Y1) / $Distance;
  2596. for($i=0;$i<=$Distance;$i++)
  2597. {
  2598. $X = $i * $XStep + $X1;
  2599. $Y = $i * $YStep + $Y1;
  2600. if ( ($X >= $this->GArea_X1 && $X <= $this->GArea_X2 && $Y >= $this->GArea_Y1 && $Y <= $this->GArea_Y2) || !$GraphFunction )
  2601. {
  2602. if ( $this->LineWidth == 1 )
  2603. $this->drawAntialiasPixel($X,$Y,$R,$G,$B);
  2604. else
  2605. {
  2606. $StartOffset = -($this->LineWidth/2); $EndOffset = ($this->LineWidth/2);
  2607. for($j=$StartOffset;$j<=$EndOffset;$j++)
  2608. $this->drawAntialiasPixel($X+$j,$Y+$j,$R,$G,$B);
  2609. }
  2610. }
  2611. }
  2612. }
  2613. /* This function create a line with antialias */
  2614. function drawDottedLine($X1,$Y1,$X2,$Y2,$DotSize,$R,$G,$B,$GraphFunction=FALSE)
  2615. {
  2616. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2617. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2618. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2619. $Distance = sqrt(($X2-$X1)*($X2-$X1)+($Y2-$Y1)*($Y2-$Y1));
  2620. $XStep = ($X2-$X1) / $Distance;
  2621. $YStep = ($Y2-$Y1) / $Distance;
  2622. $DotIndex = 0;
  2623. for($i=0;$i<=$Distance;$i++)
  2624. {
  2625. $X = $i * $XStep + $X1;
  2626. $Y = $i * $YStep + $Y1;
  2627. if ( $DotIndex <= $DotSize)
  2628. {
  2629. if ( ($X >= $this->GArea_X1 && $X <= $this->GArea_X2 && $Y >= $this->GArea_Y1 && $Y <= $this->GArea_Y2) || !$GraphFunction )
  2630. {
  2631. if ( $this->LineWidth == 1 )
  2632. $this->drawAntialiasPixel($X,$Y,$R,$G,$B);
  2633. else
  2634. {
  2635. $StartOffset = -($this->LineWidth/2); $EndOffset = ($this->LineWidth/2);
  2636. for($j=$StartOffset;$j<=$EndOffset;$j++)
  2637. $this->drawAntialiasPixel($X+$j,$Y+$j,$R,$G,$B);
  2638. }
  2639. }
  2640. }
  2641. $DotIndex++;
  2642. if ( $DotIndex == $DotSize * 2 )
  2643. $DotIndex = 0;
  2644. }
  2645. }
  2646. /* Load a PNG file and draw it over the chart */
  2647. function drawFromPNG($FileName,$X,$Y,$Alpha=100)
  2648. { $this->drawFromPicture(1,$FileName,$X,$Y,$Alpha); }
  2649. /* Load a GIF file and draw it over the chart */
  2650. function drawFromGIF($FileName,$X,$Y,$Alpha=100)
  2651. { $this->drawFromPicture(2,$FileName,$X,$Y,$Alpha); }
  2652. /* Load a JPEG file and draw it over the chart */
  2653. function drawFromJPG($FileName,$X,$Y,$Alpha=100)
  2654. { $this->drawFromPicture(3,$FileName,$X,$Y,$Alpha); }
  2655. /* Generic loader function for external pictures */
  2656. function drawFromPicture($PicType,$FileName,$X,$Y,$Alpha=100)
  2657. {
  2658. if ( file_exists($FileName))
  2659. {
  2660. $Infos = getimagesize($FileName);
  2661. $Width = $Infos[0];
  2662. $Height = $Infos[1];
  2663. if ( $PicType == 1 ) { $Raster = imagecreatefrompng($FileName); }
  2664. if ( $PicType == 2 ) { $Raster = imagecreatefromgif($FileName); }
  2665. if ( $PicType == 3 ) { $Raster = imagecreatefromjpeg($FileName); }
  2666. imagecopymerge($this->Picture,$Raster,$X,$Y,0,0,$Width,$Height,$Alpha);
  2667. imagedestroy($Raster);
  2668. }
  2669. }
  2670. /* Draw an alpha pixel */
  2671. function drawAlphaPixel($X,$Y,$Alpha,$R,$G,$B)
  2672. {
  2673. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2674. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2675. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2676. if ( $X < 0 || $Y < 0 || $X >= $this->XSize || $Y >= $this->YSize )
  2677. return(-1);
  2678. $RGB2 = imagecolorat($this->Picture, $X, $Y);
  2679. $R2 = ($RGB2 >> 16) & 0xFF;
  2680. $G2 = ($RGB2 >> 8) & 0xFF;
  2681. $B2 = $RGB2 & 0xFF;
  2682. $iAlpha = (100 - $Alpha)/100;
  2683. $Alpha = $Alpha / 100;
  2684. $Ra = floor($R*$Alpha+$R2*$iAlpha);
  2685. $Ga = floor($G*$Alpha+$G2*$iAlpha);
  2686. $Ba = floor($B*$Alpha+$B2*$iAlpha);
  2687. $C_Aliased = $this->AllocateColor($this->Picture,$Ra,$Ga,$Ba);
  2688. imagesetpixel($this->Picture,$X,$Y,$C_Aliased);
  2689. }
  2690. /* Color helper */
  2691. function AllocateColor($Picture,$R,$G,$B,$Factor=0)
  2692. {
  2693. $R = $R + $Factor;
  2694. $G = $G + $Factor;
  2695. $B = $B + $Factor;
  2696. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2697. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2698. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2699. return(imagecolorallocate($Picture,$R,$G,$B));
  2700. }
  2701. /* Add a border to the picture */
  2702. function addBorder($Size=3,$R=0,$G=0,$B=0)
  2703. {
  2704. $Width = $this->XSize+2*$Size;
  2705. $Height = $this->YSize+2*$Size;
  2706. $Resampled = imagecreatetruecolor($Width,$Height);
  2707. $C_Background = $this->AllocateColor($Resampled,$R,$G,$B);
  2708. imagefilledrectangle($Resampled,0,0,$Width,$Height,$C_Background);
  2709. imagecopy($Resampled,$this->Picture,$Size,$Size,0,0,$this->XSize,$this->YSize);
  2710. imagedestroy($this->Picture);
  2711. $this->XSize = $Width;
  2712. $this->YSize = $Height;
  2713. $this->Picture = imagecreatetruecolor($this->XSize,$this->YSize);
  2714. $C_White = $this->AllocateColor($this->Picture,255,255,255);
  2715. imagefilledrectangle($this->Picture,0,0,$this->XSize,$this->YSize,$C_White);
  2716. imagecolortransparent($this->Picture,$C_White);
  2717. imagecopy($this->Picture,$Resampled,0,0,0,0,$this->XSize,$this->YSize);
  2718. }
  2719. /* Render the current picture to a file */
  2720. function Render($FileName)
  2721. {
  2722. if ( $this->ErrorReporting )
  2723. $this->printErrors($this->ErrorInterface);
  2724. /* Save image map if requested */
  2725. if ( $this->BuildMap )
  2726. $this->SaveImageMap();
  2727. imagepng($this->Picture,$FileName);
  2728. }
  2729. /* Render the current picture to STDOUT */
  2730. function Stroke()
  2731. {
  2732. if ( $this->ErrorReporting )
  2733. $this->printErrors("GD");
  2734. /* Save image map if requested */
  2735. if ( $this->BuildMap )
  2736. $this->SaveImageMap();
  2737. header('Content-type: image/png');
  2738. imagepng($this->Picture);
  2739. }
  2740. /* Private functions for internal processing */
  2741. function drawAntialiasPixel($X,$Y,$R,$G,$B,$Alpha=100,$NoFallBack=FALSE)
  2742. {
  2743. /* Process shadows */
  2744. if ( $this->ShadowActive && !$NoFallBack )
  2745. {
  2746. $this->drawAntialiasPixel($X+$this->ShadowXDistance,$Y+$this->ShadowYDistance,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor,$this->ShadowAlpha,TRUE);
  2747. if ( $this->ShadowBlur != 0 )
  2748. {
  2749. $AlphaDecay = ($this->ShadowAlpha / $this->ShadowBlur);
  2750. for($i=1; $i<=$this->ShadowBlur; $i++)
  2751. $this->drawAntialiasPixel($X+$this->ShadowXDistance-$i/2,$Y+$this->ShadowYDistance-$i/2,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor,$this->ShadowAlpha-$AlphaDecay*$i,TRUE);
  2752. for($i=1; $i<=$this->ShadowBlur; $i++)
  2753. $this->drawAntialiasPixel($X+$this->ShadowXDistance+$i/2,$Y+$this->ShadowYDistance+$i/2,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor,$this->ShadowAlpha-$AlphaDecay*$i,TRUE);
  2754. }
  2755. }
  2756. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2757. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2758. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2759. $Plot = "";
  2760. $Xi = floor($X);
  2761. $Yi = floor($Y);
  2762. if ( $Xi == $X && $Yi == $Y)
  2763. {
  2764. if ( $Alpha == 100 )
  2765. {
  2766. $C_Aliased = $this->AllocateColor($this->Picture,$R,$G,$B);
  2767. imagesetpixel($this->Picture,$X,$Y,$C_Aliased);
  2768. }
  2769. else
  2770. $this->drawAlphaPixel($X,$Y,$Alpha,$R,$G,$B);
  2771. }
  2772. else
  2773. {
  2774. $Alpha1 = (((1 - ($X - floor($X))) * (1 - ($Y - floor($Y))) * 100) / 100) * $Alpha;
  2775. if ( $Alpha1 > $this->AntialiasQuality ) { $this->drawAlphaPixel($Xi,$Yi,$Alpha1,$R,$G,$B); }
  2776. $Alpha2 = ((($X - floor($X)) * (1 - ($Y - floor($Y))) * 100) / 100) * $Alpha;
  2777. if ( $Alpha2 > $this->AntialiasQuality ) { $this->drawAlphaPixel($Xi+1,$Yi,$Alpha2,$R,$G,$B); }
  2778. $Alpha3 = (((1 - ($X - floor($X))) * ($Y - floor($Y)) * 100) / 100) * $Alpha;
  2779. if ( $Alpha3 > $this->AntialiasQuality ) { $this->drawAlphaPixel($Xi,$Yi+1,$Alpha3,$R,$G,$B); }
  2780. $Alpha4 = ((($X - floor($X)) * ($Y - floor($Y)) * 100) / 100) * $Alpha;
  2781. if ( $Alpha4 > $this->AntialiasQuality ) { $this->drawAlphaPixel($Xi+1,$Yi+1,$Alpha4,$R,$G,$B); }
  2782. }
  2783. }
  2784. /* Validate data contained in the description array */
  2785. function validateDataDescription($FunctionName,&$DataDescription,$DescriptionRequired=TRUE)
  2786. {
  2787. if (!isset($DataDescription["Position"]))
  2788. {
  2789. $this->Errors[] = "[Warning] ".$FunctionName." - Y Labels are not set.";
  2790. $DataDescription["Position"] = "Name";
  2791. }
  2792. if ( $DescriptionRequired )
  2793. {
  2794. if (!isset($DataDescription["Description"]))
  2795. {
  2796. $this->Errors[] = "[Warning] ".$FunctionName." - Series descriptions are not set.";
  2797. foreach($DataDescription["Values"] as $key => $Value)
  2798. {
  2799. $DataDescription["Description"][$Value] = $Value;
  2800. }
  2801. }
  2802. if (count($DataDescription["Description"]) < count($DataDescription["Values"]))
  2803. {
  2804. $this->Errors[] = "[Warning] ".$FunctionName." - Some series descriptions are not set.";
  2805. foreach($DataDescription["Values"] as $key => $Value)
  2806. {
  2807. if ( !isset($DataDescription["Description"][$Value]))
  2808. $DataDescription["Description"][$Value] = $Value;
  2809. }
  2810. }
  2811. }
  2812. }
  2813. /* Validate data contained in the data array */
  2814. function validateData($FunctionName,&$Data)
  2815. {
  2816. $DataSummary = array();
  2817. foreach($Data as $key => $Values)
  2818. {
  2819. foreach($Values as $key2 => $Value)
  2820. {
  2821. if (!isset($DataSummary[$key2]))
  2822. $DataSummary[$key2] = 1;
  2823. else
  2824. $DataSummary[$key2]++;
  2825. }
  2826. }
  2827. if ( max($DataSummary) == 0 )
  2828. $this->Errors[] = "[Warning] ".$FunctionName." - No data set.";
  2829. foreach($DataSummary as $key => $Value)
  2830. {
  2831. if ($Value < max($DataSummary))
  2832. {
  2833. $this->Errors[] = "[Warning] ".$FunctionName." - Missing data in serie ".$key.".";
  2834. }
  2835. }
  2836. }
  2837. /* Print all error messages on the CLI or graphically */
  2838. function printErrors($Mode="CLI")
  2839. {
  2840. if (count($this->Errors) == 0)
  2841. return(0);
  2842. if ( $Mode == "CLI" )
  2843. {
  2844. foreach($this->Errors as $key => $Value)
  2845. echo $Value."\r\n";
  2846. }
  2847. elseif ( $Mode == "GD" )
  2848. {
  2849. $this->setLineStyle($Width=1);
  2850. $MaxWidth = 0;
  2851. foreach($this->Errors as $key => $Value)
  2852. {
  2853. $Position = imageftbbox($this->ErrorFontSize,0,$this->ErrorFontName,$Value);
  2854. $TextWidth = $Position[2]-$Position[0];
  2855. if ( $TextWidth > $MaxWidth ) { $MaxWidth = $TextWidth; }
  2856. }
  2857. $this->drawFilledRoundedRectangle($this->XSize-($MaxWidth+20),$this->YSize-(20+(($this->ErrorFontSize+4)*count($this->Errors))),$this->XSize-10,$this->YSize-10,6,233,185,185);
  2858. $this->drawRoundedRectangle($this->XSize-($MaxWidth+20),$this->YSize-(20+(($this->ErrorFontSize+4)*count($this->Errors))),$this->XSize-10,$this->YSize-10,6,193,145,145);
  2859. $C_TextColor = $this->AllocateColor($this->Picture,133,85,85);
  2860. $YPos = $this->YSize - (18 + (count($this->Errors)-1) * ($this->ErrorFontSize + 4));
  2861. foreach($this->Errors as $key => $Value)
  2862. {
  2863. imagettftext($this->Picture,$this->ErrorFontSize,0,$this->XSize-($MaxWidth+15),$YPos,$C_TextColor,$this->ErrorFontName,$Value);
  2864. $YPos = $YPos + ($this->ErrorFontSize + 4);
  2865. }
  2866. }
  2867. }
  2868. /* Activate the image map creation process */
  2869. function setImageMap($Mode=TRUE,$GraphID="MyGraph")
  2870. {
  2871. $this->BuildMap = $Mode;
  2872. $this->MapID = $GraphID;
  2873. }
  2874. /* Add a box into the image map */
  2875. function addToImageMap($X1,$Y1,$X2,$Y2,$SerieName,$Value,$CallerFunction)
  2876. {
  2877. if ( $this->MapFunction == NULL || $this->MapFunction == $CallerFunction )
  2878. {
  2879. $this->ImageMap[] = round($X1).",".round($Y1).",".round($X2).",".round($Y2).",".$SerieName.",".$Value;
  2880. $this->MapFunction = $CallerFunction;
  2881. }
  2882. }
  2883. /* Load and cleanup the image map from disk */
  2884. function getImageMap($MapName,$Flush=TRUE)
  2885. {
  2886. /* Strip HTML query strings */
  2887. $Values = $this->tmpFolder.$MapName;
  2888. $Value = split("\?",$Values);
  2889. $FileName = $Value[0];
  2890. if ( file_exists($FileName) )
  2891. {
  2892. $Handle = fopen($FileName, "r");
  2893. $MapContent = fread($Handle, filesize($FileName));
  2894. fclose($Handle);
  2895. echo $MapContent;
  2896. if ( $Flush )
  2897. unlink($FileName);
  2898. exit();
  2899. }
  2900. else
  2901. {
  2902. header("HTTP/1.0 404 Not Found");
  2903. exit();
  2904. }
  2905. }
  2906. /* Save the image map to the disk */
  2907. function SaveImageMap()
  2908. {
  2909. if ( !$this->BuildMap ) { return(-1); }
  2910. if ( $this->ImageMap == NULL )
  2911. {
  2912. $this->Errors[] = "[Warning] SaveImageMap - Image map is empty.";
  2913. return(-1);
  2914. }
  2915. $Handle = fopen($this->tmpFolder.$this->MapID, 'w');
  2916. if ( !$Handle )
  2917. {
  2918. $this->Errors[] = "[Warning] SaveImageMap - Cannot save the image map.";
  2919. return(-1);
  2920. }
  2921. else
  2922. {
  2923. foreach($this->ImageMap as $Key => $Value)
  2924. fwrite($Handle, htmlentities($Value)."\r");
  2925. }
  2926. fclose ($Handle);
  2927. }
  2928. /* Convert seconds to a time format string */
  2929. function ToTime($Value)
  2930. {
  2931. $Hour = floor($Value/3600);
  2932. $Minute = floor(($Value - $Hour*3600)/60);
  2933. $Second = floor($Value - $Hour*3600 - $Minute*60);
  2934. if (strlen($Hour) == 1 ) { $Hour = "0".$Hour; }
  2935. if (strlen($Minute) == 1 ) { $Minute = "0".$Minute; }
  2936. if (strlen($Second) == 1 ) { $Second = "0".$Second; }
  2937. return($Hour.":".$Minute.":".$Second);
  2938. }
  2939. /* Convert to metric system */
  2940. function ToMetric($Value)
  2941. {
  2942. $Go = floor($Value/1000000000);
  2943. $Mo = floor(($Value - $Go*1000000000)/1000000);
  2944. $Ko = floor(($Value - $Go*1000000000 - $Mo*1000000)/1000);
  2945. $o = floor($Value - $Go*1000000000 - $Mo*1000000 - $Ko*1000);
  2946. if ($Go != 0) { return($Go.".".$Mo."g"); }
  2947. if ($Mo != 0) { return($Mo.".".$ko."m"); }
  2948. if ($Ko != 0) { return($Ko.".".$o)."k"; }
  2949. return($o);
  2950. }
  2951. /* Convert to curency */
  2952. function ToCurrency($Value)
  2953. {
  2954. $Go = floor($Value/1000000000);
  2955. $Mo = floor(($Value - $Go*1000000000)/1000000);
  2956. $Ko = floor(($Value - $Go*1000000000 - $Mo*1000000)/1000);
  2957. $o = floor($Value - $Go*1000000000 - $Mo*1000000 - $Ko*1000);
  2958. if ( strlen($o) == 1 ) { $o = "00".$o; }
  2959. if ( strlen($o) == 2 ) { $o = "0".$o; }
  2960. $ResultString = $o;
  2961. if ( $Ko != 0 ) { $ResultString = $Ko.".".$ResultString; }
  2962. if ( $Mo != 0 ) { $ResultString = $Mo.".".$ResultString; }
  2963. if ( $Go != 0 ) { $ResultString = $Go.".".$ResultString; }
  2964. $ResultString = $this->Currency.$ResultString;
  2965. return($ResultString);
  2966. }
  2967. /* Set date format for axis labels */
  2968. function setDateFormat($Format)
  2969. {
  2970. $this->DateFormat = $Format;
  2971. }
  2972. /* Convert TS to a date format string */
  2973. function ToDate($Value)
  2974. {
  2975. return(date($this->DateFormat,$Value));
  2976. }
  2977. /* Check if a number is a full integer (for scaling) */
  2978. function isRealInt($Value)
  2979. {
  2980. if ($Value == floor($Value))
  2981. return(TRUE);
  2982. return(FALSE);
  2983. }
  2984. /**
  2985. * Return a new pChart object fixing its height by legend rotations
  2986. * Must be set fonts before call this method
  2987. * @param array $data
  2988. * @param array $dataDescription
  2989. * @param float $angle
  2990. * @return pChart
  2991. */
  2992. function fixHeightByRotation ($data, $dataDescription, $angle)
  2993. {
  2994. $maxTextHeight = 0;
  2995. foreach ($data as $key => $values) {
  2996. // Get legend value
  2997. $value = $data[$key][$dataDescription["Position"]];
  2998. if ($dataDescription["Format"]["X"] == "number") {
  2999. $value = $value.$dataDescription["Unit"]["X"];
  3000. } elseif ($dataDescription["Format"]["X"] == "time") {
  3001. $value = $this->ToTime($value);
  3002. } elseif ($dataDescription["Format"]["X"] == "date") {
  3003. $value = $this->ToDate($value);
  3004. } elseif ($dataDescription["Format"]["X"] == "metric") {
  3005. $value = $this->ToMetric($value);
  3006. } elseif ($dataDescription["Format"]["X"] == "currency") {
  3007. $value = $this->ToCurrency($value);
  3008. }
  3009. // Get positions array from label generated
  3010. $Position = imageftbbox($this->FontSize, $angle, $this->FontName, $value);
  3011. $TextHeight = abs($Position[1]) + abs($Position[3]);
  3012. // Save max height
  3013. if ($maxTextHeight < $TextHeight) {
  3014. $maxTextHeight = $TextHeight;
  3015. }
  3016. }
  3017. $this->YSize += $maxTextHeight;
  3018. $pChart = new pChart($this->XSize, $this->YSize);
  3019. // set font of the axes
  3020. $pChart->setFontProperties($this->FontName, $this->FontSize);
  3021. return $pChart;
  3022. }
  3023. }
  3024. function RaiseFatal($Message)
  3025. {
  3026. echo "[FATAL] ".$Message."\r\n";
  3027. exit();
  3028. }
  3029. ?>