98_SVG.pm 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884
  1. ##############################################
  2. # $Id: 98_SVG.pm 16293 2018-02-28 21:33:57Z rudolfkoenig $
  3. package main;
  4. use strict;
  5. use warnings;
  6. use POSIX;
  7. use HttpUtils;
  8. #use Devel::Size qw(size total_size);
  9. # This block is only needed when SVG is loaded bevore FHEMWEB
  10. sub FW_pO(@);
  11. use vars qw($FW_ME); # webname (default is fhem), needed by 97_GROUP
  12. use vars qw($FW_RET); # Returned data (html)
  13. use vars qw($FW_RETTYPE); # image/png or the like
  14. use vars qw($FW_cssdir); # css directory
  15. use vars qw($FW_detail); # currently selected device for detail view
  16. use vars qw($FW_dir); # base directory for web server
  17. use vars qw($FW_gplotdir);# gplot directory for web server: the first
  18. use vars qw($FW_plotmode);# Global plot mode (WEB attribute), used by SVG
  19. use vars qw($FW_plotsize);# Global plot size (WEB attribute), used by SVG
  20. use vars qw($FW_room); # currently selected room
  21. use vars qw($FW_subdir); # Sub-path in URL, used by FLOORPLAN/weblink
  22. use vars qw($FW_wname); # Web instance
  23. use vars qw(%FW_hiddenroom); # hash of hidden rooms, used by weblink
  24. use vars qw(%FW_pos); # scroll position
  25. use vars qw(%FW_webArgs); # all arguments specified in the GET
  26. use vars qw($FW_formmethod);
  27. use vars qw($FW_userAgent);
  28. use vars qw($FW_hiddenroom);
  29. use vars qw($FW_CSRF);
  30. my $SVG_RET; # Returned data (SVG)
  31. sub SVG_calcOffsets($$);
  32. sub SVG_doround($$$);
  33. sub SVG_fmtTime($$);
  34. sub SVG_pO($);
  35. sub SVG_readgplotfile($$$);
  36. sub SVG_render($$$$$$$$$$);
  37. sub SVG_showLog($);
  38. sub SVG_substcfg($$$$$$);
  39. sub SVG_time_align($$);
  40. sub SVG_time_to_sec($);
  41. sub SVG_openFile($$$);
  42. sub SVG_doShowLog($$$$;$);
  43. sub SVG_getData($$$$$);
  44. sub SVG_sel($$$;$$);
  45. sub SVG_getControlPoints($);
  46. sub SVG_calcControlPoints($$$$$$);
  47. my %SVG_devs; # hash of from/to entries per device
  48. #####################################
  49. sub
  50. SVG_Initialize($)
  51. {
  52. my ($hash) = @_;
  53. $hash->{DefFn} = "SVG_Define";
  54. no warnings 'qw';
  55. my @attrList = qw(
  56. captionLeft:1,0"
  57. captionPos:right,left,auto
  58. endPlotNow
  59. endPlotToday
  60. fixedoffset
  61. fixedrange
  62. label
  63. nrAxis
  64. plotWeekStartDay:0,1,2,3,4,5,6
  65. plotfunction
  66. plotmode
  67. plotsize
  68. plotReplace:textField-long
  69. startDate
  70. title
  71. );
  72. use warnings 'qw';
  73. $hash->{AttrList} = join(" ", @attrList);
  74. $hash->{SetFn} = "SVG_Set";
  75. $hash->{AttrFn} = "SVG_AttrFn";
  76. $hash->{RenameFn} = "SVG_Rename";
  77. $hash->{FW_summaryFn} = "SVG_FwFn";
  78. $hash->{FW_detailFn} = "SVG_FwFn";
  79. $hash->{FW_atPageEnd} = 1;
  80. $data{FWEXT}{"/SVG_WriteGplot"}{CONTENTFUNC} = "SVG_WriteGplot";
  81. $data{FWEXT}{"/SVG_showLog"}{FUNC} = "SVG_showLog";
  82. $data{FWEXT}{"/SVG_showLog"}{FORKABLE} = 1;
  83. }
  84. #####################################
  85. sub
  86. SVG_Define($$)
  87. {
  88. my ($hash, $def) = @_;
  89. my ($name, $type, $arg) = split("[ \t]+", $def, 3);
  90. if(!$arg ||
  91. !($arg =~ m/^(.*):(.*):(.*)$/ || $arg =~ m/^(.*):(.*)$/)) {
  92. return "Usage: define <name> SVG <logdevice>:<gnuplot-file>:<logfile>";
  93. }
  94. $hash->{LOGDEVICE} = $1;
  95. $hash->{GPLOTFILE} = $2;
  96. $hash->{LOGFILE} = ($3 ? $3 : "CURRENT");
  97. $hash->{STATE} = "initialized";
  98. $hash->{LOGDEVICE} =~ s/^fileplot //; # Autocreate bug.
  99. notifyRegexpChanged($hash, "global");
  100. return undef;
  101. }
  102. ##################
  103. sub
  104. SVG_Set($@)
  105. {
  106. my ($hash, @a) = @_;
  107. my $me = $hash->{NAME};
  108. return "no set argument specified" if(int(@a) < 2);
  109. my $cmd = $a[1];
  110. return "Unknown argument $cmd, choose one of copyGplotFile:noArg"
  111. if($cmd ne "copyGplotFile");
  112. my $srcName = "$FW_gplotdir/$hash->{GPLOTFILE}.gplot";
  113. my ($og,$od) = ($hash->{GPLOTFILE}, $hash->{DEF});
  114. $hash->{GPLOTFILE} = $hash->{NAME};
  115. my $dstName = "$FW_gplotdir/$hash->{GPLOTFILE}.gplot";
  116. return "this is already a unique gplot file" if($srcName eq $dstName);
  117. $hash->{DEF} = $hash->{LOGDEVICE} . ":".
  118. $hash->{GPLOTFILE} . ":".
  119. $hash->{LOGFILE};
  120. my ($err,@rows) = FileRead($srcName);
  121. return $err if($err);
  122. $err = FileWrite($dstName, @rows);
  123. if($err) {
  124. $hash->{DEF} = $od; $hash->{GPLOTFILE} = $og;
  125. } else {
  126. addStructChange("modify", $me, "$me $hash->{DEF}")
  127. }
  128. return $err;
  129. }
  130. sub
  131. SVG_AttrFn(@)
  132. {
  133. my ($cmd,$name,$aName,$aVal) = @_;
  134. if($aName eq "captionLeft" && $cmd eq "set") {
  135. my $dir = (!defined($aVal) || $aVal) ? "left" : "right";
  136. AnalyzeCommand(undef, "attr $name captionPos $dir");
  137. return "attr $name captionLeft converted to attr $name captionPos $dir";
  138. }
  139. return undef;
  140. }
  141. sub
  142. SVG_Attr($$$$)
  143. {
  144. my ($parent, $dev, $attr, $default) = @_;
  145. my $val = AttrVal($dev, $attr, undef);
  146. return $val if(defined($val));
  147. return AttrVal($parent, $attr, $default);
  148. }
  149. sub
  150. SVG_Rename($$)
  151. {
  152. my ($new, $old) = @_;
  153. my $hash = $defs{$new};
  154. return if($hash->{GPLOTFILE} ne $old);
  155. SVG_Set($hash, $new, "copyGplotFile"); # Forum #59786
  156. }
  157. sub
  158. jsSVG_getAttrs($;$)
  159. {
  160. my ($d , $flt) = @_;
  161. return join("&#01;", map { #00 arrives as 65533 in JS
  162. my $v=$attr{$d}{$_};
  163. $v =~ s/'/&#39;/g;
  164. "$_=$v";
  165. } grep { $flt ? $flt->{$_} : 1 } keys %{$attr{$d}});
  166. }
  167. sub
  168. SVG_getplotsize($)
  169. {
  170. my ($d) = @_;
  171. return $FW_webArgs{plotsize} ?
  172. $FW_webArgs{plotsize} : AttrVal($d,"plotsize",$FW_plotsize);
  173. }
  174. sub
  175. SVG_isEmbed($)
  176. {
  177. return (AttrVal($FW_wname, "plotEmbed", 1));
  178. # $FW_userAgent !~ m/(iPhone|iPad|iPod).*OS (8|9)/));
  179. }
  180. sub
  181. SVG_log10($)
  182. {
  183. my ($n) = @_;
  184. return 0.0000000001 if( $n <= 0 );
  185. return log(1+$n)/log(10);
  186. }
  187. ##################
  188. sub
  189. SVG_FwFn($$$$)
  190. {
  191. my ($FW_wname, $d, $room, $pageHash) = @_; # pageHash is set for summaryFn.
  192. my $hash = $defs{$d};
  193. my $ret = "";
  194. if(!$pageHash || !$pageHash->{jsLoaded}) {
  195. $ret .= "<script type='text/javascript' src='$FW_ME/pgm2/svg.js'></script>";
  196. $pageHash->{jsLoaded} = 1 if($pageHash);
  197. }
  198. # plots navigation buttons
  199. my $pm = AttrVal($d,"plotmode",$FW_plotmode);
  200. if((!$pageHash || !$pageHash->{buttons}) &&
  201. AttrVal($d, "fixedrange", "x") !~ m/^[ 0-9:-]*$/) {
  202. $ret .= '<div class="SVGlabel" data-name="svgZoomControl">';
  203. $ret .= SVG_zoomLink("zoom=-1", "Zoom-in", "zoom in");
  204. $ret .= SVG_zoomLink("zoom=1", "Zoom-out","zoom out");
  205. $ret .= SVG_zoomLink("off=-1", "Prev", "prev");
  206. $ret .= SVG_zoomLink("off=1", "Next", "next");
  207. $ret .= '</div>';
  208. $pageHash->{buttons} = 1 if($pageHash);
  209. $ret .= "<br>";
  210. }
  211. if($pm eq "jsSVG") {
  212. my @d=split(":",$defs{$d}{DEF});
  213. my ($err, @svgplotfile) = FileRead("$FW_gplotdir/$d[1].gplot");
  214. ($err, @svgplotfile) = FileRead("$FW_gplotdir/template.gplot") if($err);
  215. my $gplot = join("&#01;", @svgplotfile);
  216. $gplot =~ s/'/&#39;/g;
  217. my %webattrflt = ( endPlotNow=>1, endPlotToday=>1, plotmode=>1,
  218. plotsize=>1, nrAxis=>1, stylesheetPrefix=>1 );
  219. if(!$pageHash || !$pageHash->{jssvgLoaded}) {
  220. $ret .=
  221. "<script type='text/javascript' src='$FW_ME/pgm2/jsSVG.js'></script>";
  222. $pageHash->{jssvgLoaded} = 1 if($pageHash);
  223. }
  224. SVG_calcOffsets($d[0], $d);
  225. $ret .= "<div id='jsSVG_$d' class='jsSVG' ".
  226. "data-webAttr='".jsSVG_getAttrs($FW_wname, \%webattrflt)."' ".
  227. "data-svgAttr='".jsSVG_getAttrs($d)."' ".
  228. "data-svgName='".$d."' ".
  229. "data-from='".$SVG_devs{$d[0]}{from}."' ".
  230. "data-to='" .$SVG_devs{$d[0]}{to} ."' ".
  231. "data-gplotFile='$gplot' source='$d[0]'>".
  232. "</div>";
  233. $ret .= (SVG_PEdit($FW_wname,$d,$room,$pageHash) . "<br>")
  234. if(!$pageHash);
  235. return $ret;
  236. }
  237. my $arg="$FW_ME/SVG_showLog?dev=$d".
  238. "&logdev=$hash->{LOGDEVICE}".
  239. "&gplotfile=$hash->{GPLOTFILE}".
  240. "&logfile=$hash->{LOGFILE}".
  241. "&pos=" . join(";", map {"$_=$FW_pos{$_}"} keys %FW_pos);
  242. if($pm eq "SVG") {
  243. $ret .= "<div class=\"SVGplot SVG_$d\">";
  244. if(SVG_isEmbed($FW_wname)) {
  245. my ($w, $h) = split(",", SVG_getplotsize($d));
  246. $ret .= "<embed src=\"$arg\" type=\"image/svg+xml\" " .
  247. "width=\"$w\" height=\"$h\" name=\"$d\"/>\n";
  248. } else {
  249. my $oret=$FW_RET; $FW_RET="";
  250. my ($type, $data) = SVG_doShowLog($d, $hash->{LOGDEVICE},
  251. $hash->{GPLOTFILE}, $hash->{LOGFILE}, 1);
  252. $FW_RET=$oret;
  253. $ret .= $data;
  254. }
  255. $ret .= "</div>";
  256. } elsif($pm eq "gnuplot-scroll") {
  257. $ret .= "<img src=\"$arg\"/>";
  258. } elsif($pm eq "gnuplot-scroll-svg") {
  259. $ret .= "<object type=\"image/svg+xml\" ".
  260. "data=\"$arg\">Your browser does not support SVG.</object>";
  261. }
  262. if(!$pageHash) {
  263. if($FW_plotmode eq "SVG") {
  264. $ret .= SVG_PEdit($FW_wname,$d,$room,$pageHash) . "<br>";
  265. }
  266. } else {
  267. if(!AttrVal($d, "group", "") && !$FW_subdir) {
  268. my $alias = AttrVal($d, "alias", $d);
  269. my $clAdd = "\" data-name=\"$d";
  270. $clAdd .= "\" style=\"display:none;" if($FW_hiddenroom{detail});
  271. $ret .= FW_pH("detail=$d", $alias, 0, "SVGlabel SVG_$d $clAdd", 1, 0);
  272. $ret .= "<br>";
  273. }
  274. }
  275. return $ret;
  276. }
  277. sub
  278. SVG_cb($$$)
  279. {
  280. my ($v,$t,$c) = @_;
  281. $c = ($c ? " checked" : "");
  282. return "$t&nbsp;<input type=\"checkbox\" name=\"$v\" value=\"$v\"$c>";
  283. }
  284. sub
  285. SVG_txt($$$$)
  286. {
  287. my ($v,$t,$c,$sz) = @_;
  288. $c = "" if(!defined($c));
  289. $c =~ s/&/\&amp;/g;
  290. $c =~ s/"/\&quot;/g;
  291. return "$t&nbsp;<input type=\"text\" name=\"$v\" size=\"$sz\" ".
  292. "value=\"$c\"/>";
  293. }
  294. sub
  295. SVG_sel($$$;$$)
  296. {
  297. my ($v,$l,$c,$fnData,$class) = @_;
  298. my @al = split(",",$l);
  299. $c =~ s/\\x3a/:/g if($c);
  300. return FW_select(undef,$v,\@al,$c, $class?$class:"set", $fnData);
  301. }
  302. ############################
  303. # gnuplot file "editor"
  304. sub
  305. SVG_PEdit($$$$)
  306. {
  307. my ($FW_wname,$d,$room,$pageHash) = @_;
  308. my $pe = AttrVal($FW_wname, "ploteditor", "always");
  309. return "" if( $pe eq 'never' );
  310. my $gpf = $defs{$d}{GPLOTFILE};
  311. my $gpfEsc = $gpf;
  312. $gpfEsc =~ s,\.,\\\\.,g;
  313. my $link = "$FW_ME?cmd=style edit $gpf.gplot".
  314. (configDBUsed() ? " configDB" : "").$FW_CSRF;
  315. my $gp = "$FW_gplotdir/$gpf.gplot";
  316. my $pm = AttrVal($d,"plotmode",$FW_plotmode);
  317. my ($err, $cfg, $plot, $srcDesc) = SVG_readgplotfile($d, $gp, $pm);
  318. my %conf = SVG_digestConf($cfg, $plot);
  319. my $ret = "<br>";
  320. my $pestyle = "";
  321. if( $pe eq 'onClick' ) {
  322. my $pgm = "Javascript:" .
  323. "s=document.getElementById('pedit').style;".
  324. "s.display = s.display=='none' ? 'block' : 'none';".
  325. "s=document.getElementById('pdisp').style;".
  326. "s.display = s.display=='none' ? 'block' : 'none';";
  327. $ret .= "<a id=\"pdisp\" style=\"cursor:pointer\" onClick=\"$pgm\">Show Plot Editor</a>";
  328. $pestyle = 'style="display:none"';
  329. }
  330. $ret .= "<form $pestyle id=\"pedit\" method=\"$FW_formmethod\" autocomplete=\"off\" ".
  331. "action=\"$FW_ME/SVG_WriteGplot\">";
  332. $ret .= FW_hidden("detail", $d); # go to detail after save
  333. if(defined($FW_pos{zoom}) && defined($FW_pos{off})) { # for showData
  334. $ret .= FW_hidden("pos", "zoom=$FW_pos{zoom};off=$FW_pos{off}");
  335. }
  336. $ret .= "<div class='makeTable wide'><span>Plot Editor</span>";
  337. $ret .= "<table class=\"block wide plotEditor\">";
  338. $ret .= "<tr class=\"even\">";
  339. $ret .= "<td>Plot title</td>";
  340. $ret .= "<td>".SVG_txt("title", "", $conf{title}, 32)."</td>";
  341. $ret .= "</tr>";
  342. $ret .= "<tr class=\"odd\">";
  343. $ret .= "<td>Y-Axis label</td>";
  344. $conf{ylabel} =~ s/"//g if($conf{ylabel});
  345. $ret .= "<td>".SVG_txt("ylabel", "left", $conf{ylabel}, 16)."</td>";
  346. $conf{y2label} =~ s/"//g if($conf{y2label});
  347. $ret .= "<td>".SVG_txt("y2label","right", $conf{y2label}, 16)."</td>";
  348. $ret .= "</tr>";
  349. $ret .= "<tr class=\"even\">";
  350. $ret .= "<td>Grid aligned</td>";
  351. $ret .= "<td>".SVG_cb("gridy", "left", $conf{hasygrid})."</td>";
  352. $ret .= "<td>".SVG_cb("gridy2","right",$conf{hasy2grid})."</td>";
  353. $ret .= "</tr>";
  354. $ret .= "<tr class=\"odd\">";
  355. $ret .= "<td>Range as [min:max]</td>";
  356. $ret .= "<td>".SVG_txt("yrange", "left", $conf{yrange}, 16);
  357. $ret .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".
  358. SVG_cb("yscale", "log", $conf{yscale})."</td>";
  359. $ret .= "<td>".SVG_txt("y2range", "right", $conf{y2range}, 16);
  360. $ret .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".
  361. SVG_cb("y2scale", "log", $conf{y2scale})."</td>";
  362. $ret .= "</tr>";
  363. if( $conf{xrange} ) {
  364. $ret .= "<tr class=\"odd\"><td/><td>";
  365. $ret .= SVG_txt("xrange", "x&nbsp;&nbsp;&nbsp;",$conf{xrange},16)."</td>";
  366. $ret .= "<td/></tr>";
  367. }
  368. $ret .= "<tr class=\"even\">";
  369. $ret .= "<td>Tics as (\"Txt\" val, ...)</td>";
  370. $ret .= "<td>".SVG_txt("ytics", "left", $conf{ytics}, 16)."</td>";
  371. $ret .= "<td>".SVG_txt("y2tics","right", $conf{y2tics}, 16)."</td>";
  372. $ret .= "</tr>";
  373. if( $conf{xtics} ) {
  374. $ret .= "<tr class=\"even\"><td/><td>";
  375. $ret .= SVG_txt("xtics", "x&nbsp;&nbsp;&nbsp;", $conf{xtics}, 16)."</td>";
  376. $ret .= "<td/></tr>";
  377. }
  378. my ($desc, $cnt) = ("Spec", 0);
  379. my (@srcHtml, @paramHtml, @exampleHtml, @revIdx);
  380. my @srcNames = grep { $modules{$defs{$_}{TYPE}}{SVG_sampleDataFn} }
  381. sort keys %defs;
  382. foreach my $src (@{$srcDesc->{order}}) {
  383. my $lmax = $srcDesc->{src}{$src}{idx}+1;
  384. my $fn = $modules{$defs{$src}{TYPE}}{SVG_sampleDataFn};
  385. my @argArr = split(" ", $srcDesc->{src}{$src}{arg});
  386. if($fn) {
  387. no strict "refs";
  388. my ($ldesc, $paramHtml, $example) =
  389. &{$fn}($src, \@argArr, $lmax,\%conf, $FW_wname);
  390. use strict "refs";
  391. $desc = $ldesc;
  392. push @paramHtml, @{$paramHtml} if($paramHtml);
  393. map { push @exampleHtml, $example } (0..$lmax-1);
  394. } else {
  395. push @paramHtml, map { SVG_txt("par_${_}_0","",$_,20) } @argArr;
  396. map { push @exampleHtml, "" } (0..$lmax-1);
  397. }
  398. push @srcHtml,
  399. map { FW_select(undef,"src_$_",\@srcNames,$src,"svgSrc");} (0..$lmax-1);
  400. map { push @revIdx,$srcDesc->{rev}{$cnt}{$_}; } (0..$lmax-1);
  401. $cnt++;
  402. }
  403. # Last, empty line
  404. push @revIdx,int(@revIdx);
  405. push @srcHtml, FW_select(undef,"src_".int(@srcHtml),\@srcNames,"","svgSrc");
  406. push @paramHtml, (@paramHtml==0 ?
  407. SVG_txt("par_0_0", "", "parameter", 10) : $paramHtml[0]);
  408. push @exampleHtml, "Set the label and 'Write .gplot file' first in order to ".
  409. "get example data and correct parameter choice";
  410. my @lineStyles;
  411. if(SVG_openFile($FW_cssdir,
  412. AttrVal($FW_wname,"stylesheetPrefix",""), "svg_style.css")) {
  413. map { push(@lineStyles,$1) if($_ =~ m/^\.SVGplot.(l[^{ ]*)/) } <FH>; # }
  414. close(FH);
  415. }
  416. $ret .= "<tr class=\"odd\"><td>Diagram label, Source</td>";
  417. $ret .= "<td>$desc</td>";
  418. $ret .=" <td>Y-Axis,Plot-Type,Style,Width</td></tr>";
  419. my ($r, $example, @output) = (0, "");
  420. my $max = int(@srcHtml);
  421. for($r=0; $r < $max; $r++) {
  422. my $idx = $revIdx[$r];
  423. $example .= "<div class='ex ex_$idx' style='display:".($idx?"none":"block").
  424. "'>$exampleHtml[$r]</div>";
  425. my $o = "<tr row='$idx' class=\"".(($r&1)?"odd":"even")."\"><td>";
  426. $o .= SVG_txt("title_$idx", "", !$conf{lTitle}[$idx]&&$idx<($max-1) ?
  427. "notitle" : $conf{lTitle}[$idx], 12);
  428. my $sh = $srcHtml[$r]; $sh =~ s/src_\d+/src_$idx/g;
  429. $o .= $sh;
  430. $o .= "</td><td>";
  431. my $ph = $paramHtml[$r]; $ph =~ s/par_\d+_/par_${idx}_/g;
  432. $o .= $ph;
  433. $o .= "</td><td>";
  434. my $v = $conf{lAxis}[$idx];
  435. my $sel = ($v && $v eq "x1y1") ? "left" : "right";
  436. $o .= SVG_sel("axes_${idx}", "left,right,left log,right log", $sel );
  437. $o .= SVG_sel("type_${idx}",
  438. "lines,points,steps,fsteps,histeps,bars,ibars,".
  439. "cubic,quadratic,quadraticSmooth",
  440. $conf{lType}[$idx]);
  441. my $ls = $conf{lStyle}[$idx];
  442. if($ls) {
  443. $ls =~ s/class=.* //g;
  444. $ls =~ s/"//g;
  445. }
  446. $o .= SVG_sel("style_$idx", join(",", @lineStyles), $ls);
  447. my $lw = $conf{lWidth}[$idx];
  448. if($lw) {
  449. $lw =~ s/.*stroke-width://g;
  450. $lw =~ s/"//g;
  451. }
  452. $o .= SVG_sel("width_$idx", "0.2,0.5,1,1.5,2,3,4", ($lw ? $lw : 1));
  453. $o .= "</td></tr>";
  454. $output[$idx] = $o;
  455. }
  456. $ret .= join("", @output);
  457. $ret .= "<tr class=\"".(($r++&1)?"odd":"even")."\"><td colspan=\"3\">";
  458. $ret .= "Example lines for input:<br>$example</td></tr>";
  459. my %gpf;
  460. map {
  461. $gpf{$defs{$_}{GPLOTFILE}}{$_} = 1 if($defs{$_}{TYPE} eq "SVG");
  462. } sort keys %defs;
  463. if(int(keys %{$gpf{$defs{$d}{GPLOTFILE}}}) > 1) {
  464. $ret .= "<tr class='".(($r++&1)?"odd":"even")."'><td colspan='3'>".
  465. "<b>Note:</b>".
  466. "The .gplot file ($defs{$d}{GPLOTFILE}) is used by multiple SVG ".
  467. "devices (".join(",", sort keys %{$gpf{$defs{$d}{GPLOTFILE}}})."), ".
  468. "writing probably will corrupt the other SVGs. ".
  469. "Remedy: execute set $d copyGplotFile".
  470. "</td></tr>";
  471. }
  472. $ret .= "<tr class=\"".(($r++&1)?"odd":"even")."\"><td colspan=\"3\">";
  473. $ret .= FW_submit("submit", "Write .gplot file")."&nbsp;".
  474. FW_submit("showFileLogData", "Show preprocessed input").
  475. "</td></tr>";
  476. $ret .= "</table></div></form>";
  477. my $sl = "$FW_ME/SVG_WriteGplot?detail=$d&showFileLogData=1";
  478. if(defined($FW_pos{zoom}) && defined($FW_pos{off})) {
  479. $sl .= "&pos=zoom=$FW_pos{zoom};off=$FW_pos{off}";
  480. }
  481. $ret .= <<'EOF';
  482. <script type="text/javascript">
  483. var sel = "table.plotEditor tr[row] ";
  484. $(sel+"input,"+sel+"select").focus(function(){
  485. var row = $(this).closest("tr").attr("row");
  486. $("table.plotEditor div.ex").css("display","none");
  487. $("table.plotEditor div.ex_"+row).css("display","block");
  488. });
  489. $("table.plotEditor input[name=title_0]").focus();
  490. $("table.plotEditor input[name=showFileLogData]").click(function(e){
  491. e.preventDefault();
  492. EOF
  493. $ret .=
  494. "FW_cmd('$sl', function(arg){" .<<"EOF";
  495. FW_okDialog(arg);
  496. });
  497. });
  498. setTimeout(function(){
  499. \$("table.internals div[informid=$gpfEsc-GPLOTFILE]")
  500. .html("<a href='$link'>$gpf</a>");
  501. }, 10);
  502. </script>
  503. EOF
  504. return $ret;
  505. }
  506. ##################
  507. # Generate the zoom and scroll images with links if appropriate
  508. sub
  509. SVG_zoomLink($$$)
  510. {
  511. my ($cmd, $img, $alt) = @_;
  512. my $prf;
  513. $cmd =~ m/^(.*);([^;]*)$/;
  514. if($2) {
  515. ($prf, $cmd) = ($1, $2);
  516. $prf =~ s/&pos=.*//;
  517. }
  518. my ($d,$off) = split("=", $cmd, 2);
  519. my $val = $FW_pos{$d};
  520. $cmd = ($FW_detail ? "detail=$FW_detail":
  521. ($prf ? $prf : "room=$FW_room")) . "&pos=";
  522. if($d eq "zoom") {
  523. my $n = 0;
  524. my @FW_zoom = ("hour","qday","day","week","month","year");
  525. my %FW_zoom = map { $_, $n++ } @FW_zoom;
  526. $val = "day" if(!$val);
  527. $val = $FW_zoom{$val};
  528. return "" if(!defined($val) || $val+$off < 0 || $val+$off >= int(@FW_zoom));
  529. $val = $FW_zoom[$val+$off];
  530. return "" if(!$val);
  531. # Approximation of the next offset.
  532. my $w_off = $FW_pos{off};
  533. $w_off = 0 if(!$w_off);
  534. if ($val eq "hour") {
  535. $w_off = $w_off*6;
  536. } elsif($val eq "qday") {
  537. $w_off = ($off < 0) ? $w_off*4 : int($w_off/6);
  538. } elsif($val eq "day") {
  539. $w_off = ($off < 0) ? $w_off*7 : int($w_off/4);
  540. } elsif($val eq "week") {
  541. $w_off = ($off < 0) ? $w_off*4 : int($w_off/7);
  542. } elsif($val eq "month") {
  543. $w_off = ($off < 0) ? $w_off*12: int($w_off/4);
  544. } elsif($val eq "year") {
  545. $w_off = int($w_off/12);
  546. }
  547. $cmd .= "zoom=$val;off=$w_off";
  548. } else {
  549. return "" if((!$val && $off > 0) || ($val && $val+$off > 0)); # no future
  550. $off=($val ? $val+$off : $off);
  551. my $zoom=$FW_pos{zoom};
  552. $zoom = 0 if(!$zoom);
  553. $cmd .= "zoom=$zoom;off=$off";
  554. }
  555. return "&nbsp;&nbsp;".FW_pHPlain("$cmd", FW_makeImage($img, $alt));
  556. }
  557. # Debugging: show the data received from GET
  558. sub
  559. SVG_showData()
  560. {
  561. my $wl = $FW_webArgs{detail};
  562. my $hash = $defs{$wl};
  563. my ($d, $gplotfile, $file) = split(":", $hash->{DEF});
  564. $gplotfile = "$FW_gplotdir/$gplotfile.gplot";
  565. my $pm = AttrVal($d,"plotmode",$FW_plotmode);
  566. my ($err, $cfg, $plot, $srcDesc) = SVG_readgplotfile($wl, $gplotfile, $pm);
  567. if($err) {
  568. $FW_RET=$err;
  569. return 1;
  570. }
  571. SVG_calcOffsets($d, $wl);
  572. $FW_RET = SVG_getData($wl,$SVG_devs{$d}{from}, $SVG_devs{$d}{to}, $srcDesc,1);
  573. $FW_RET =~ s/\n/<br>/gs;
  574. return 1;
  575. }
  576. sub
  577. SVG_WriteGplot($)
  578. {
  579. my ($arg) = @_;
  580. FW_digestCgi($arg);
  581. return if($FW_hiddenroom{detail});
  582. return SVG_showData() if($FW_webArgs{showFileLogData});
  583. if(!defined($FW_webArgs{par_0_0})) {
  584. $FW_RET .=
  585. '<div id="errmsg">'.
  586. "missing data in logfile: won't write incomplete .gplot definition".
  587. '</div>';
  588. return 0;
  589. }
  590. my $maxLines = 0;
  591. foreach my $i (keys %FW_webArgs) {
  592. next if($i !~ m/^title_(.*)$/);
  593. $maxLines = $1 if($1 > $maxLines);
  594. }
  595. my @rows;
  596. push @rows, "# Created by FHEM/98_SVG.pm, ".TimeNow();
  597. push @rows, "set terminal png transparent size <SIZE> crop";
  598. push @rows, "set output '<OUT>.png'";
  599. push @rows, "set xdata time";
  600. push @rows, "set timefmt \"%Y-%m-%d_%H:%M:%S\"";
  601. push @rows, "set xlabel \" \"";
  602. push @rows, "set title '$FW_webArgs{title}'";
  603. push @rows, "set xtics ".$FW_webArgs{xtics} if($FW_webArgs{xtics});
  604. push @rows, "set ytics ".$FW_webArgs{ytics};
  605. push @rows, "set y2tics ".$FW_webArgs{y2tics};
  606. push @rows, "set grid".($FW_webArgs{gridy} ? " ytics" :"").
  607. ($FW_webArgs{gridy2} ? " y2tics":"")."";
  608. push @rows, "set ylabel \"$FW_webArgs{ylabel}\"";
  609. push @rows, "set y2label \"$FW_webArgs{y2label}\"";
  610. push @rows, "set xrange $FW_webArgs{xrange}" if($FW_webArgs{xrange});
  611. push @rows, "set yrange $FW_webArgs{yrange}" if($FW_webArgs{yrange});
  612. push @rows, "set y2range $FW_webArgs{y2range}" if($FW_webArgs{y2range});
  613. push @rows, "set yscale log" if($FW_webArgs{yscale});
  614. push @rows, "set y2scale log" if($FW_webArgs{y2scale});
  615. push @rows, "";
  616. my @plot;
  617. for(my $i=0; $i <= $maxLines; $i++) {
  618. next if(!$FW_webArgs{"title_$i"});
  619. my $prf = "par_${i}_";
  620. my @v = map {$FW_webArgs{"$prf$_"}}
  621. grep {defined($FW_webArgs{"$prf$_"})} (0..9);
  622. my $r = @v > 1 ?
  623. join(":", map { $v[$_] =~ s/:/\\x3a/g if($_<$#v); $v[$_] } 0..$#v) :
  624. $v[0];
  625. my $src = $FW_webArgs{"src_$i"};
  626. push @rows, "#$src $r";
  627. push @plot, "\"<IN>\" using 1:2 axes ".
  628. ($FW_webArgs{"axes_$i"} eq "right" ? "x1y2" : "x1y1").
  629. ($FW_webArgs{"title_$i"} eq "notitle" ? " notitle" :
  630. " title '".$FW_webArgs{"title_$i"} ."'").
  631. " ls " .$FW_webArgs{"style_$i"} .
  632. " lw " .$FW_webArgs{"width_$i"} .
  633. " with " .$FW_webArgs{"type_$i"};
  634. }
  635. push @rows, "";
  636. for(my $i=0; $i < @plot; $i++) {
  637. my $r = $plot[$i];
  638. $r = "plot $r" if($i == 0);
  639. $r = " $r" if($i > 0);
  640. $r = "$r,\\" if($i+1 < @plot);
  641. push @rows, $r;
  642. }
  643. my $hash = $defs{$FW_webArgs{detail}};
  644. my $err = FileWrite("$FW_gplotdir/$hash->{GPLOTFILE}.gplot", @rows);
  645. $FW_RET .= "<div id='errmsg'>SVG_WriteGplot: $err</div>" if($err);
  646. return 0;
  647. }
  648. #######################################################
  649. # srcDesc:
  650. # - {all} : space separated plot arguments, in the file order, without devname
  651. # - {order}: unique name of the devs (FileLog,etc) in the .gplot order
  652. # - {src}{X}: hash (X is an order element), consisting of
  653. # {arg}: plot arguments for one dev, space separated
  654. # {idx}: number of lines requested from the same source
  655. # {num}: number of this src in the order array
  656. # - {rev}{orderIdx}{localIdx} = N: reverse lookup of the plot argument index,
  657. # using {src}{X}{num} as orderIdx and {src}{X}{idx} as localIdx
  658. sub
  659. SVG_readgplotfile($$$)
  660. {
  661. my ($wl, $gplot_pgm, $plotmode) = @_;
  662. ############################
  663. # Read in the template gnuplot file. Digest the #FileLog lines. Replace
  664. # the plot directive with our own, as we offer a file for each line
  665. my (%srcDesc, @data, $plot);
  666. my $ld = $defs{$wl}{LOGDEVICE}
  667. if($defs{$wl} && $defs{$wl}{LOGDEVICE});
  668. my $ldType = $defs{$defs{$wl}{LOGDEVICE}}{TYPE}
  669. if($ld && $defs{$ld});
  670. if(!$ldType && $defs{$wl}) {
  671. $ldType = $defs{$wl}{TYPE};
  672. $ld = $wl;
  673. }
  674. my ($err1, $err2, @svgplotfile);
  675. ($err1, @svgplotfile) = FileRead($gplot_pgm);
  676. ($err2, @svgplotfile) = FileRead("$FW_gplotdir/template.gplot") if($err1);
  677. return ($err1, undef) if($err2);
  678. my ($plotfnCnt, $srcNum) = (0,0);
  679. my @empty;
  680. $srcDesc{all} = "";
  681. $srcDesc{order} = \@empty;
  682. my $specval = AttrVal($wl, "plotfunction", undef);
  683. my $plotReplace = AttrVal($wl, "plotReplace", undef);
  684. my $pr;
  685. (undef, $pr) = parseParams($plotReplace,"\\s"," ") if($plotReplace);
  686. my $prSubst = sub($)
  687. {
  688. return "%$_[0]%" if(!$pr);
  689. my $v = $pr->{$_[0]};
  690. return "%$_[0]%" if(!$v);
  691. if($v =~ m/^{.*}$/) {
  692. $cmdFromAnalyze = $v;
  693. return eval $v;
  694. } else {
  695. return $v;
  696. }
  697. };
  698. foreach my $l (@svgplotfile) {
  699. $l = "$l\n" unless $l =~ m/\n$/;
  700. map { $l =~ s/%($_)%/&$prSubst($1)/ge } keys %$pr if($plotReplace);
  701. my ($src, $plotfn) = (undef, undef);
  702. if($l =~ m/^#([^ ]*) (.*)$/) {
  703. if($1 eq $ldType) {
  704. $src = $ld; $plotfn = $2;
  705. } elsif($1 && $defs{$1}) {
  706. $src = $1; $plotfn = $2;
  707. }
  708. } elsif($l =~ "^plot" || $plot) {
  709. $plot .= $l;
  710. } else {
  711. push(@data, $l);
  712. }
  713. if($plotfn) {
  714. Log 3, "$wl: space is not allowed in $ldType definition: $plotfn"
  715. if($plotfn =~ m/\s/);
  716. if ($specval) {
  717. my @spec = split(" ",$specval);
  718. my $spec_count=1;
  719. foreach (@spec) {
  720. $plotfn =~ s/<SPEC$spec_count>/$_/g;
  721. $spec_count++;
  722. }
  723. }
  724. my $p = $srcDesc{src}{$src};
  725. if(!$p) {
  726. $p = { arg => $plotfn, idx=>0, num=>$srcNum++ };
  727. $srcDesc{src}{$src} = $p;
  728. push(@{$srcDesc{order}}, $src);
  729. } else {
  730. $p->{arg} .= " $plotfn";
  731. $p->{idx}++;
  732. }
  733. $srcDesc{rev}{$p->{num}}{$p->{idx}} = $plotfnCnt++;
  734. $srcDesc{all} .= " $plotfn";
  735. }
  736. }
  737. return (undef, \@data, $plot, \%srcDesc);
  738. }
  739. sub
  740. SVG_substcfg($$$$$$)
  741. {
  742. my ($splitret, $wl, $cfg, $plot, $file, $tmpfile) = @_;
  743. # interpret title and label as a perl command and make
  744. # to all internal values e.g. $value.
  745. my $ldt = $defs{$defs{$wl}{LOGDEVICE}}{TYPE}
  746. if($defs{$wl} && $defs{$wl}{LOGDEVICE});
  747. $ldt = "" if(!defined($ldt));
  748. if($file eq "CURRENT" && $ldt eq "FileLog") {
  749. $file = $defs{$defs{$wl}{LOGDEVICE}}{currentlogfile};
  750. $file =~ s+.*/++;
  751. }
  752. my $fileesc = $file;
  753. $fileesc =~ s/\\/\\\\/g; # For Windows, by MarkusRR
  754. my $title = AttrVal($wl, "title", "\"$fileesc\"");
  755. $title = AnalyzeCommand(undef, "{ $title }");
  756. my $label = AttrVal($wl, "label", undef);
  757. my @g_label;
  758. if ($label) {
  759. @g_label = split("::",$label);
  760. foreach (@g_label) {
  761. $_ = AnalyzeCommand(undef, "{ $_ }");
  762. }
  763. }
  764. my $gplot_script = join("", @{$cfg});
  765. $gplot_script .= $plot if(!$splitret);
  766. my $plotReplace = AttrVal($wl, "plotReplace", undef);
  767. if($plotReplace) {
  768. my ($list, $pr) = parseParams($plotReplace, "\\s"," ");
  769. for my $k (keys %$pr) {
  770. if($pr->{$k} =~ m/^{.*}$/) {
  771. $cmdFromAnalyze = $pr->{$k};
  772. $pr->{$k} = eval $cmdFromAnalyze;
  773. }
  774. $gplot_script =~ s/<$k>/$pr->{$k}/g;
  775. }
  776. }
  777. $gplot_script =~ s/<OUT>/$tmpfile/g;
  778. $gplot_script =~ s/<IN>/$file/g;
  779. my $ps = SVG_getplotsize($wl);
  780. $gplot_script =~ s/<SIZE>/$ps/g;
  781. $gplot_script =~ s/<TL>/$title/g;
  782. my $g_count=1;
  783. if ($label) {
  784. foreach (@g_label) {
  785. $gplot_script =~ s/<L$g_count>/$_/g;
  786. $plot =~ s/<L$g_count>/$_/g;
  787. $g_count++;
  788. }
  789. }
  790. $plot =~ s/\r//g; # For our windows friends...
  791. $gplot_script =~ s/\r//g;
  792. if($splitret == 1) {
  793. my @ret = split("\n", $gplot_script);
  794. return (\@ret, $plot);
  795. } else {
  796. return $gplot_script;
  797. }
  798. }
  799. sub
  800. SVG_tspec(@)
  801. {
  802. return sprintf("%04d-%02d-%02d_%02d:%02d:%02d",
  803. $_[5]+1900,$_[4]+1,$_[3],$_[2],$_[1],$_[0]);
  804. }
  805. ##################
  806. # Calculate either the number of scrollable SVGs (for $d = undef) or
  807. # for the device the valid from and to dates for the given zoom and offset
  808. sub
  809. SVG_calcOffsets($$)
  810. {
  811. my ($d,$wl) = @_;
  812. my $pm = AttrVal($wl,"plotmode",$FW_plotmode);
  813. my ($fr, $fo);
  814. my $frx; #fixedrange with offset
  815. if($defs{$wl}) {
  816. $fr = AttrVal($wl, "fixedrange", undef);
  817. if($fr) {
  818. if($fr =~ "^(hour|qday|day|week|month|year)" ||
  819. $fr =~ m/^\d+days$/ ) { #fixedrange with offset
  820. $frx=$fr; #fixedrange with offset
  821. } else {
  822. my @range = split(" ", $fr);
  823. my @t = localtime;
  824. $SVG_devs{$d}{from} = ResolveDateWildcards($range[0], @t);
  825. $SVG_devs{$d}{to} = ResolveDateWildcards($range[1], @t);
  826. return;
  827. }
  828. }
  829. $fo = AttrVal( $wl, "fixedoffset", undef);
  830. }
  831. my $off = 0;
  832. $off += $FW_pos{off} if($FW_pos{off});
  833. $off = $fo if(defined($fo) && $fo =~ m/^[+-]?\d+$/);
  834. my $now;
  835. my $st = AttrVal($wl, "startDate", undef);
  836. if($st) {
  837. $now = mktime(0,0,12,$3,$2-1,$1-1900,0,0,-1)
  838. if($st =~ m/(\d\d\d\d)-(\d\d)-(\d\d)/);
  839. }
  840. $now = time() if(!$now);
  841. my $zoom = $FW_pos{zoom};
  842. $zoom = "day" if(!$zoom);
  843. $zoom = $fr if(defined($fr));
  844. $zoom = $frx if ($frx); #fixedrange with offset
  845. my @zrange = split(" ", $zoom); #fixedrange with offset
  846. if(defined($zrange[1])) { $off += $zrange[1]; $zoom=$zrange[0]; } #fixedrange with offset
  847. my $endPlotNow = (SVG_Attr($FW_wname, $wl, "endPlotNow", undef) && !$st);
  848. if($zoom eq "hour") {
  849. if($endPlotNow) {
  850. my $t = int(($now + $off*3600 - 3600)/300.0)*300 + 300;
  851. my @l = localtime($t);
  852. $SVG_devs{$d}{from} = SVG_tspec(@l);
  853. @l = localtime($t+3599);
  854. $SVG_devs{$d}{to} = SVG_tspec(@l);
  855. } else {
  856. my $t = int($now/3600)*3600 + $off*3600;
  857. my @l = localtime($t);
  858. $SVG_devs{$d}{from} = SVG_tspec(@l);
  859. @l = localtime($t+3600-1);
  860. $SVG_devs{$d}{to} = SVG_tspec(@l);
  861. }
  862. } elsif($zoom eq "qday") {
  863. if($endPlotNow) {
  864. my $t = int($now/300)*300 + ($off-1)*21600;
  865. my @l = localtime($t);
  866. $SVG_devs{$d}{from} = SVG_tspec( 0,$l[1],$l[2],$l[3],$l[4],$l[5]);
  867. @l = localtime($t+21600-1);
  868. $SVG_devs{$d}{to} = SVG_tspec(59,$l[1],$l[2],$l[3],$l[4],$l[5]);
  869. } else {
  870. my $t = int($now/3600)*3600 + $off*21600;
  871. my @l = localtime($t);
  872. $l[2] = int($l[2]/6)*6;
  873. $SVG_devs{$d}{from} = SVG_tspec( 0, 0,$l[2],$l[3],$l[4],$l[5]);
  874. $l[2] += 5;
  875. $SVG_devs{$d}{to} = SVG_tspec(59,59,$l[2],$l[3],$l[4],$l[5]);
  876. }
  877. } elsif($zoom =~ m/^(\d+)?day/) {
  878. my $nDays = $1 ? ($1-1) : 0;
  879. if($endPlotNow) {
  880. my $t = int($now/300)*300 + ($off-$nDays-1)*86400;
  881. my @l = localtime($t);
  882. $SVG_devs{$d}{from} = SVG_tspec(0,$l[1],$l[2],$l[3],$l[4],$l[5]);
  883. @l = localtime($t+(1+$nDays)*86400-1);
  884. $SVG_devs{$d}{to} = SVG_tspec(59,$l[1],$l[2],$l[3],$l[4],$l[5]);
  885. } else {
  886. my $t = $now + ($off-$nDays)*86400;
  887. my @l = localtime($t);
  888. $SVG_devs{$d}{from} = SVG_tspec( 0, 0, 0,$l[3],$l[4],$l[5]);
  889. @l = localtime($t+$nDays*86400);
  890. $SVG_devs{$d}{to} = SVG_tspec(59,59,23,$l[3],$l[4],$l[5]);
  891. }
  892. } elsif($zoom eq "week") {
  893. my @l = localtime($now);
  894. my $start = (SVG_Attr($FW_wname, $wl, "endPlotToday", undef) ?
  895. 6 : $l[6] - SVG_Attr($FW_wname, $wl, "plotWeekStartDay", 0));
  896. $start += 7 if($start < 0);
  897. my $t = $now - ($start*86400) + ($off*86400)*7;
  898. @l = localtime($t);
  899. $SVG_devs{$d}{from} = SVG_tspec( 0, 0, 0,$l[3],$l[4],$l[5]);
  900. @l = localtime($t+6*86400);
  901. $SVG_devs{$d}{to} = SVG_tspec(59,59,23,$l[3],$l[4],$l[5]);
  902. } elsif($zoom eq "month") {
  903. my ($sd,$ed,$sm,$em,$sy,$ey);
  904. my @l = localtime($now);
  905. while($off < -12) { # Correct the year
  906. $off += 12; $l[5]--;
  907. }
  908. $l[4] += $off;
  909. $l[4] += 12, $l[5]-- if($l[4] < 0);
  910. my @me = (31,28,31,30,31,30,31,31,30,31,30,31);
  911. if(SVG_Attr($FW_wname, $wl, "endPlotToday", undef)) {
  912. $sy = $ey = $l[5];
  913. $sm = $l[4]-1; $em = $l[4];
  914. $sm += 12, $sy-- if($sm < 0);
  915. $sd = $l[3]+1; $ed = $l[3];
  916. $sd = $me[$sm] if($sd > $me[$sm]);
  917. } else {
  918. $sy = $ey = $l[5];
  919. $sm = $em = $l[4];
  920. $sd = 1; $ed = $me[$l[4]];
  921. $ed++ if($l[4]==1 && !(($sy+1900)%4)); # leap year
  922. }
  923. $SVG_devs{$d}{from} = SVG_tspec( 0, 0, 0,$sd,$sm,$sy);
  924. $SVG_devs{$d}{to} = SVG_tspec(59,59,23,$ed,$em,$ey);
  925. } elsif($zoom eq "year") {
  926. my @l = localtime($now);
  927. $l[5] += ($off-1);
  928. if(SVG_Attr($FW_wname, $wl, "endPlotToday", undef)) {
  929. $SVG_devs{$d}{from} = SVG_tspec( 0, 0, 0,$l[3],$l[4],$l[5]);
  930. $l[5]++; # today, 23:59
  931. $SVG_devs{$d}{to} = SVG_tspec(59,59,23,$l[3],$l[4],$l[5]);
  932. } elsif(SVG_Attr($FW_wname, $wl, "endPlotNow", undef)) {
  933. $SVG_devs{$d}{from} = SVG_tspec(0, $l[0], @l);
  934. $SVG_devs{$d}{from} = SVG_tspec(@l);
  935. $l[5]++; # now
  936. $SVG_devs{$d}{to} = SVG_tspec(@l);
  937. } else {
  938. $l[5]++;
  939. $SVG_devs{$d}{from} = SVG_tspec( 0, 0, 0, 1, 0,$l[5]); #Jan01 00:00:00
  940. $SVG_devs{$d}{to} = SVG_tspec(59,59,23,31,11,$l[5]); #Dec31 23:59:59
  941. }
  942. }
  943. }
  944. ######################
  945. # Generate an image from the log via gnuplot or SVG
  946. sub
  947. SVG_showLog($)
  948. {
  949. return SVG_doShowLog($FW_webArgs{dev},
  950. $FW_webArgs{logdev},
  951. $FW_webArgs{gplotfile},
  952. $FW_webArgs{logfile});
  953. }
  954. sub
  955. SVG_doShowLog($$$$;$)
  956. {
  957. my ($wl, $d, $type, $file, $noHeader) = @_;
  958. my $pm = AttrVal($wl,"plotmode",$FW_plotmode);
  959. my $gplot_pgm = "$FW_gplotdir/$type.gplot";
  960. my ($err, $cfg, $plot, $srcDesc) = SVG_readgplotfile($wl, $gplot_pgm, $pm);
  961. if($err || !$defs{$d}) {
  962. my $msg = ($defs{$d} ? "Cannot read $gplot_pgm" : "No Logdevice $d");
  963. Log3 $FW_wname, 1, $msg;
  964. if($pm =~ m/SVG/) { # FW_fatal for SVG:
  965. $FW_RETTYPE = "image/svg+xml";
  966. FW_pO '<svg xmlns="http://www.w3.org/2000/svg">';
  967. FW_pO '<text x="20" y="20">'.$msg.'</text>';
  968. FW_pO '</svg>';
  969. return ($FW_RETTYPE, $FW_RET);
  970. } else {
  971. return ($FW_RETTYPE, $msg);
  972. }
  973. }
  974. SVG_calcOffsets($d,$wl);
  975. my ($f,$t)=($SVG_devs{$d}{from}, $SVG_devs{$d}{to});
  976. $f = 0 if(!$f); # From the beginning of time...
  977. $t = 9 if(!$t); # till the end
  978. if($pm =~ m/gnuplot/) {
  979. my $tmpfile = "/tmp/file.$$";
  980. my $errfile = "/tmp/gnuplot.err";
  981. my $da = SVG_getData($wl, $f, $t, $srcDesc, 0); # substcfg needs it(!)
  982. my $tmpstring = "";
  983. open(FH, ">$tmpfile");
  984. for(my $dIdx=0; $dIdx<@{$da}; $dIdx++) {
  985. if (${$da->[$dIdx]}) {
  986. $tmpstring = ${$da->[$dIdx]};
  987. $tmpstring =~ s/#.*/\n/g;
  988. } else {
  989. $tmpstring = "$f 0\n\n";
  990. }
  991. print FH "$tmpstring";
  992. }
  993. close(FH);
  994. # put in the filename of the temporary data file into the plot file string
  995. my $i = 0;
  996. $plot =~ s/\".*?using 1:[^ ]+ /"\"$tmpfile\" i " . $i++ . " using 1:2 "/gse;
  997. $plot = "set xrange [\"$f\":\"$t\"]\n\n$plot" if($SVG_devs{$d}{from});
  998. my $gplot_script = SVG_substcfg(0, $wl, $cfg, $plot, $file, $tmpfile);
  999. $gplot_script =~ s/<TMPFILE>/$tmpfile/g;
  1000. $plot =~ s/ls \w+//g;
  1001. open(FH, "|gnuplot >> $errfile 2>&1");# feed it to gnuplot
  1002. print FH $gplot_script;
  1003. close(FH);
  1004. unlink($tmpfile);
  1005. my $ext;
  1006. if($pm eq "gnuplot-scroll") {
  1007. $FW_RETTYPE = "image/png";
  1008. $ext = "png";
  1009. }
  1010. else {
  1011. $FW_RETTYPE = "image/svg+xml";
  1012. $ext = "svg";
  1013. }
  1014. open(FH, "$tmpfile.$ext"); # read in the result and send it
  1015. binmode (FH); # necessary for Windows
  1016. FW_pO join("", <FH>);
  1017. close(FH);
  1018. unlink("$tmpfile.$ext");
  1019. } elsif($pm eq "SVG") {
  1020. my ($f,$t)=($SVG_devs{$d}{from}, $SVG_devs{$d}{to});
  1021. $f = 0 if(!$f); # From the beginning of time...
  1022. $t = 9 if(!$t); # till the end
  1023. Log3 $FW_wname, 5, "plotcommand: get $d $file INT $f $t ".$srcDesc->{all};
  1024. $FW_RETTYPE = "image/svg+xml";
  1025. (my $cachedate = TimeNow()) =~ s/ /_/g;
  1026. my $SVGcache = (AttrVal($FW_wname, "SVGcache", undef) && $t lt $cachedate);
  1027. my $cDir = "$FW_dir/SVGcache";
  1028. my $cFile = "$wl-$f-$t.svg";
  1029. $cFile =~ s/:/-/g; # For Windows / #11053
  1030. my $cPath = "$cDir/$cFile";
  1031. if($SVGcache && open(CFH, $cPath)) {
  1032. FW_pO join("", <CFH>);
  1033. close(CFH);
  1034. } else {
  1035. my $da = SVG_getData($wl, $f, $t, $srcDesc, 0); # substcfg needs it(!)
  1036. ($cfg, $plot) = SVG_substcfg(1, $wl, $cfg, $plot, $file, "<OuT>");
  1037. my $ret = SVG_render($wl, $f, $t, $cfg, $da,
  1038. $plot, $FW_wname, $FW_cssdir, $srcDesc, $noHeader);
  1039. $internal_data = "";
  1040. FW_pO $ret;
  1041. if($SVGcache) {
  1042. mkdir($cDir) if(! -d $cDir);
  1043. if(open(CFH, ">$cPath")) {
  1044. print CFH $ret;
  1045. close(CFH);
  1046. }
  1047. }
  1048. }
  1049. }
  1050. return ($FW_RETTYPE, $FW_RET);
  1051. }
  1052. sub
  1053. SVG_getData($$$$$)
  1054. {
  1055. my ($d, $f,$t,$srcDesc,$showData) = @_;
  1056. my (@da, $ret, @vals);
  1057. my @keys = ("min","mindate","max","maxdate","currval","currdate",
  1058. "firstval","firstdate","avg","cnt","lastraw","sum");
  1059. foreach my $src (@{$srcDesc->{order}}) {
  1060. my $s = $srcDesc->{src}{$src};
  1061. my $fname = ($src eq $defs{$d}{LOGDEVICE} ? $defs{$d}{LOGFILE} : "CURRENT");
  1062. my $cmd = "get $src $fname INT $f $t ".$s->{arg};
  1063. FW_fC($cmd, 1);
  1064. if($showData) {
  1065. $ret .= "\n$cmd\n\n";
  1066. $ret .= $$internal_data if(ref $internal_data eq "SCALAR");
  1067. } else {
  1068. push(@da, $internal_data);
  1069. for(my $i = 0; $i<=$s->{idx}; $i++) {
  1070. my %h;
  1071. foreach my $k (@keys) {
  1072. $h{$k} = $data{$k.($i+1)};
  1073. }
  1074. push @vals, \%h;
  1075. }
  1076. }
  1077. }
  1078. # Reorder the $data{maxX} stuff
  1079. my ($min, $max) = (999999, -999999);
  1080. my $no = int(keys %{$srcDesc->{rev}});
  1081. for(my $oi = 0; $oi < $no; $oi++) {
  1082. my $nl = int(keys %{$srcDesc->{rev}{$oi}});
  1083. for(my $li = 0; $li < $nl; $li++) {
  1084. my $r = $srcDesc->{rev}{$oi}{$li}+1;
  1085. my $val = shift @vals;
  1086. foreach my $k (@keys) {
  1087. $min = $val->{$k} if($k eq "min" && defined($val->{$k}) &&
  1088. $val->{$k} =~ m/[-+]?\d*\.?\d+/ && $val->{$k} < $min);
  1089. $max = $val->{$k} if($k eq "max" && defined($val->{$k}) &&
  1090. $val->{$k} =~ m/[-+]?\d*\.?\d+/ && $val->{$k} > $max);
  1091. $data{"$k$r"} = $val->{$k};
  1092. }
  1093. }
  1094. }
  1095. $data{maxAll} = $max;
  1096. $data{minAll} = $min;
  1097. return $ret if($showData);
  1098. return \@da;
  1099. }
  1100. ######################
  1101. # Convert the configuration to a "readable" form -> array to hash
  1102. sub
  1103. SVG_digestConf($$)
  1104. {
  1105. my ($confp,$plot) = @_;
  1106. my %conf;
  1107. map { chomp; my @a=split(" ",$_, 3);
  1108. if($a[0] && $a[0] eq "set") { $conf{lc($a[1])} = $a[2]; }
  1109. } @{$confp};
  1110. $conf{title} = "" if(!defined($conf{title}));
  1111. $conf{title} =~ s/'//g;
  1112. ######################
  1113. # Digest grid
  1114. my $t = ($conf{grid} ? $conf{grid} : "");
  1115. #$conf{hasxgrid} = ( $t =~ /.*xtics.*/ ? 1 : 0); # Unused
  1116. $conf{hasygrid} = ( $t =~ /.*ytics.*/ ? 1 : 0);
  1117. $conf{hasy2grid}= ( $t =~ /.*y2tics.*/ ? 1 : 0);
  1118. # Digest axes/title/etc from $plot (gnuplot) and draw the line-titles
  1119. my (@lAxis,@lTitle,@lType,@lStyle,@lWidth);
  1120. my ($i, $pTemp);
  1121. $pTemp = $plot; $i = 0; $pTemp =~ s/ axes (\w+)/$lAxis[$i++]=$1/gse;
  1122. $pTemp = $plot; $i = 0;
  1123. $pTemp =~ s/title( '([^']*)')?/$lTitle[$i++]=(defined($2)?$2:"")/gse;
  1124. $pTemp = $plot; $i = 0; $pTemp =~ s/ with (\w+)/$lType[$i++]=$1/gse;
  1125. $pTemp = $plot; $i = 0; $pTemp =~ s/ ls (\w+)/$lStyle[$i++]=$1/gse;
  1126. $pTemp = $plot; $i = 0; $pTemp =~ s/ lw ([\w.]+)/$lWidth[$i++]=$1/gse;
  1127. for my $i (0..int(@lType)-1) { # lAxis is optional
  1128. $lAxis[$i] = "x1y2" if(!$lAxis[$i]);
  1129. $lStyle[$i] = "class=\"SVGplot ".
  1130. (defined($lStyle[$i]) ? $lStyle[$i] : "l$i")."\"";
  1131. $lWidth[$i] = (defined($lWidth[$i]) ?
  1132. "style=\"stroke-width:$lWidth[$i]\"" :"");
  1133. }
  1134. $conf{lAxis} = \@lAxis;
  1135. $conf{lTitle} = \@lTitle;
  1136. $conf{lType} = \@lType;
  1137. $conf{lStyle} = \@lStyle;
  1138. $conf{lWidth} = \@lWidth;
  1139. return %conf;
  1140. }
  1141. sub
  1142. SVG_openFile($$$)
  1143. {
  1144. my ($dir, $prf, $fName) = @_;
  1145. my $baseStyle = $prf;
  1146. $baseStyle =~ s/(touchpad|smallscreen)//;
  1147. if(open(FH, "$dir/${baseStyle}$fName") || # Forum #32530
  1148. open(FH, "$dir/$fName")) {
  1149. return 1;
  1150. }
  1151. return 0;
  1152. }
  1153. #####################################
  1154. sub
  1155. SVG_getSteps($$$)
  1156. {
  1157. my ($range,$min,$max) = @_;
  1158. my $dh = $max - $min;
  1159. my ($step, $mi, $ma) = (1, 1, 1);
  1160. my @limit = (0.001, 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50,
  1161. 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000,
  1162. 200000, 500000, 1000000, 2000000);
  1163. for my $li (0..$#limit-1) {
  1164. my $l = $limit[$li];
  1165. next if($dh > $l*10);
  1166. $ma = $range ? $max : SVG_doround($max, $l, 1);
  1167. $mi = $range ? $min : SVG_doround($min, $l, 0);
  1168. if(($ma-$mi)/$l >= 7) { # If more then 7 steps, then choose next
  1169. $l = $limit[$li+1];
  1170. $ma = $range ? $max : SVG_doround($max, $l, 1);
  1171. $mi = $range ? $min : SVG_doround($min, $l, 0);
  1172. }
  1173. $step = $l;
  1174. last;
  1175. }
  1176. if($step==0.001 && $max==$min) { # Don't want 0.001 range for nil
  1177. $step = 1;
  1178. $ma = $mi + $step;
  1179. }
  1180. return ($step, $mi, $ma);
  1181. }
  1182. sub
  1183. SVG_render($$$$$$$$$$)
  1184. {
  1185. my $name = shift; # e.g. wl_8
  1186. my $from = shift; # e.g. 2008-01-01
  1187. my $to = shift; # e.g. 2009-01-01
  1188. my $confp = shift; # lines from the .gplot file, w/o FileLog and plot
  1189. my $da = shift; # data pointer array
  1190. my $plot = shift; # Plot lines from the .gplot file
  1191. my $parent_name = shift; # e.g. FHEMWEB instance name
  1192. my $parent_dir = shift; # FW_dir
  1193. my $srcDesc = shift; # #FileLog lines, as array pointer
  1194. my $noHeader = shift;
  1195. $SVG_RET="";
  1196. my $SVG_ss = AttrVal($parent_name, "smallscreen", 0);
  1197. my ($nr_left_axis,$nr_right_axis,$use_left_axis,$use_right_axis) =
  1198. split(",", SVG_Attr($parent_name, $name,"nrAxis","1,1"));
  1199. $use_left_axis = $nr_left_axis if( !defined($use_left_axis) );
  1200. $use_right_axis = $nr_right_axis if( !defined($use_right_axis) );
  1201. my $th = 16; # "Font" height
  1202. my $axis_width = ($SVG_ss ? 2 : 3)*$th;
  1203. my ($x, $y) = ($axis_width*$nr_left_axis, 1.2*$th); # Rect offset
  1204. ######################
  1205. # Convert the configuration to a "readable" form -> array to hash
  1206. my %conf = SVG_digestConf($confp, $plot);
  1207. my $ps = "800,400";
  1208. $ps = $1 if($conf{terminal} =~ m/.*size[ ]*([^ ]*)/);
  1209. my ($ow,$oh) = split(",", $ps); # Original width
  1210. my $w = $ow-$nr_left_axis*$axis_width-$nr_right_axis*$axis_width;
  1211. my $h = $oh-2*$y; # Rect size
  1212. my @filter;
  1213. foreach my $src (keys %{$srcDesc->{src}}) {
  1214. my $f = CallFn($src, "SVG_regexpFn", $src, $srcDesc->{src}{$src}{arg});
  1215. push(@filter, $f) if($f);
  1216. }
  1217. my $filter = join("|", @filter);
  1218. $filter =~ s/"/./g;
  1219. $filter = AttrVal($parent_name, "longpollSVG", 0) ? "flog=\"$filter\"" : "";
  1220. my %dataIdx; # Build a reverse Index for the dataSource
  1221. ######################
  1222. # SVG Header
  1223. my $svghdr = 'version="1.1" xmlns="http://www.w3.org/2000/svg" '.
  1224. 'xmlns:xlink="http://www.w3.org/1999/xlink" '.
  1225. "id='SVGPLOT_$name' $filter data-origin='FHEM'";
  1226. if(!$noHeader) {
  1227. SVG_pO '<?xml version="1.0" encoding="UTF-8"?>';
  1228. SVG_pO '<!DOCTYPE svg>';
  1229. SVG_pO "<svg $svghdr width=\"${ow}px\" height=\"${oh}px\">";
  1230. } else {
  1231. SVG_pO "<svg $svghdr style='width:${ow}px; height:${oh}px;'>";
  1232. }
  1233. my $prf = AttrVal($parent_name, "stylesheetPrefix", "");
  1234. SVG_pO "<style type=\"text/css\"><![CDATA[";
  1235. if(SVG_openFile($parent_dir, $prf, "svg_style.css")) {
  1236. SVG_pO join("", <FH>);
  1237. close(FH);
  1238. } else {
  1239. Log3 $name, 0, "Can't open $parent_dir/svg_style.css"
  1240. }
  1241. SVG_pO "]]></style>";
  1242. ######################
  1243. # gradient definitions
  1244. if(SVG_openFile($parent_dir, $prf, "svg_defs.svg")) {
  1245. SVG_pO join("", <FH>);
  1246. close(FH);
  1247. } else {
  1248. Log3 $name, 0, "Can't open $parent_dir/svg_defs.svg"
  1249. }
  1250. ######################
  1251. # Rectangle
  1252. SVG_pO "<rect x=\"$x\" y=\"$y\" width =\"$w\" height =\"$h\" rx=\"8\" ry=\"8\" ".
  1253. "fill=\"none\" class=\"border\"/>";
  1254. my ($off1,$off2) = ($x+$w/2, 3*$y/4);
  1255. my $title = ($conf{title} ? $conf{title} : " ");
  1256. $title =~ s/</&lt;/g;
  1257. $title =~ s/>/&gt;/g;
  1258. SVG_pO "<text id=\"svg_title\" x=\"$off1\" y=\"$off2\" " .
  1259. "class=\"title\" text-anchor=\"middle\">$title</text>";
  1260. ######################
  1261. # Left label = ylabel and right label = y2label
  1262. if(!$SVG_ss) {
  1263. for my $idx (1..$use_left_axis) {
  1264. my $name = "y".($idx)."label";
  1265. $name = "ylabel" if( $idx == 1 );
  1266. my $t = ($conf{$name} ? $conf{$name} : "");
  1267. $t =~ s/"//g;
  1268. ($off1,$off2) = ($x-($idx)*$axis_width+3*$th/4, $oh/2);
  1269. SVG_pO "<text x=\"$off1\" y=\"$off2\" text-anchor=\"middle\" " .
  1270. "class=\"ylabel\" transform=\"rotate(270,$off1,$off2)\">$t</text>";
  1271. }
  1272. for my $idx ($use_left_axis+1..$use_left_axis+$use_right_axis) {
  1273. my $name = "y".($idx)."label";
  1274. $name = "ylabel" if( $idx == 1 );
  1275. my $t = ($conf{$name} ? $conf{$name} : "");
  1276. $t =~ s/"//g;
  1277. ($off1,$off2) = ($x+$w+($idx-$use_left_axis)*$axis_width-$th/4, $oh/2);
  1278. SVG_pO "<text x=\"$off1\" y=\"$off2\" text-anchor=\"middle\" " .
  1279. "class=\"y2label\" transform=\"rotate(270,$off1,$off2)\">$t</text>";
  1280. }
  1281. }
  1282. ######################
  1283. ($off1,$off2) = ($ow-$nr_right_axis*$axis_width-$th, $y+$th);
  1284. my ($xmin, $xmax, $xtics)= (99999999, -99999999, "");
  1285. if(defined($conf{xrange})) {
  1286. my $idx= 1;
  1287. while(defined($data{"xmin$idx"})) {
  1288. $xmin= $data{"xmin$idx"} if($data{"xmin$idx"}< $xmin);
  1289. $xmax= $data{"xmax$idx"} if($data{"xmax$idx"}> $xmax);
  1290. $idx++;
  1291. }
  1292. #main::Debug "xmin= $xmin xmax=$xmax";
  1293. $conf{xrange} = AnalyzeCommand(undef, $1) if($conf{xrange} =~ /^({.*})$/);
  1294. if($conf{xrange} =~ /\[(.*):(.*)\]/) {
  1295. $xmin = $1 if($1 ne "");
  1296. $xmax = $2 if($2 ne "");
  1297. }
  1298. }
  1299. $xtics = defined($conf{xtics}) ? $conf{xtics} : "";
  1300. ######################
  1301. # Loop over the input, digest dates, calculate min/max values
  1302. my ($fromsec, $tosec);
  1303. $fromsec = SVG_time_to_sec($from) if($from ne "0"); # 0 is special
  1304. $tosec = SVG_time_to_sec($to) if($to ne "9"); # 9 is special
  1305. my $tmul;
  1306. $tmul = $w/($tosec-$fromsec) if($tosec && $fromsec && $tosec != $fromsec);
  1307. my ($min, $max, $idx) = (99999999, -99999999, 0);
  1308. my (%hmin, %hmax, @hdx, @hdy);
  1309. my ($dxp, $dyp) = (\(), \());
  1310. my ($d, $v, $ld, $lv) = ("","","","");
  1311. for(my $dIdx=0; $dIdx<@{$da}; $dIdx++) {
  1312. my $lIdx = 0;
  1313. $idx = $srcDesc->{rev}{$dIdx}{$lIdx};
  1314. my $dp = $da->[$dIdx];
  1315. next if(ref $dp ne "SCALAR"); # Avoid Crash, Forum #34523
  1316. my ($dpl,$dpoff,$l) = (length($$dp), 0, "");
  1317. while($dpoff < $dpl) { # using split instead is memory hog
  1318. my $ndpoff = index($$dp, "\n", $dpoff);
  1319. if($ndpoff == -1) {
  1320. $l = substr($$dp, $dpoff);
  1321. } else {
  1322. $l = substr($$dp, $dpoff, $ndpoff-$dpoff);
  1323. }
  1324. $dpoff = $ndpoff+1;
  1325. if($l =~ m/^#/) {
  1326. my $a = $conf{lAxis}[$idx];
  1327. if(defined($a)) {
  1328. $hmin{$a} = $min if(!defined($hmin{$a}) || $hmin{$a} > $min);
  1329. $hmax{$a} = $max if(!defined($hmax{$a}) || $hmax{$a} < $max);
  1330. }
  1331. ($min, $max) = (99999999, -99999999);
  1332. $hdx[$idx] = $dxp; $hdy[$idx] = $dyp;
  1333. ($dxp, $dyp) = (\(), \());
  1334. $lIdx++;
  1335. $idx = $srcDesc->{rev}{$dIdx}{$lIdx};
  1336. last if(!$idx);
  1337. } elsif( $l =~ /^;/ ) { #allow ;special lines
  1338. if( $l =~ m/^;p (\S+)\s(\S+)/ ) {# point
  1339. my $xmul = $w/($xmax-$xmin) if($xmax-$xmin > 0 );
  1340. my $x1;
  1341. if( $conf{xrange} ) {
  1342. $x1 = int(($1-$xmin)*$xmul);
  1343. } else {
  1344. $x1 = $x1;
  1345. }
  1346. my $y1 = $2;
  1347. push @{$dxp}, $x1;
  1348. push @{$dyp}, $y1;
  1349. $min = $y1 if($min > $y1);
  1350. $max = $y1 if($max < $y1);
  1351. } elsif( $conf{lType}[$idx] eq "lines" ) {
  1352. push @{$dxp}, undef;
  1353. push @{$dyp}, $l;
  1354. }
  1355. } else {
  1356. ($d, $v) = split(" ", $l);
  1357. $d = ($tmul ? int((SVG_time_to_sec($d)-$fromsec)*$tmul) : $d);
  1358. $d = 0 if($tmul && $d < 0); # Forum #40358
  1359. if($ld ne $d || $lv ne $v) { # Saves a lot on year zoomlevel
  1360. $ld = $d; $lv = $v;
  1361. push @{$dxp}, $d;
  1362. push @{$dyp}, $v;
  1363. $min = $v if($min > $v);
  1364. $max = $v if($max < $v);
  1365. }
  1366. }
  1367. last if($ndpoff == -1);
  1368. }
  1369. }
  1370. $dxp = $hdx[0];
  1371. if(($dxp && int(@{$dxp}) < 2 && !$tosec) || # not enough data and no range...
  1372. (!$tmul && !$dxp)) {
  1373. SVG_pO "</svg>";
  1374. return $SVG_RET;
  1375. }
  1376. if(!$tmul) { # recompute the x data if no range sepcified
  1377. $fromsec = SVG_time_to_sec($dxp->[0]) if(!$fromsec);
  1378. $tosec = SVG_time_to_sec($dxp->[int(@{$dxp})-1]) if(!$tosec);
  1379. $tmul = $w/($tosec-$fromsec) if($tosec != $fromsec);
  1380. for my $i (0..@hdx-1) {
  1381. $dxp = $hdx[$i];
  1382. for my $i (0..@{$dxp}-1) {
  1383. $dxp->[$i] = int((SVG_time_to_sec($dxp->[$i])-$fromsec)*$tmul);
  1384. }
  1385. }
  1386. }
  1387. ######################
  1388. # Compute & draw vertical tics, grid and labels
  1389. my $ddur = ($tosec-$fromsec)/86400;
  1390. my ($first_tag, $tag, $step, $tstep, $aligntext, $aligntics);
  1391. if($ddur <= 0.1) {
  1392. $first_tag=". 2 1"; $tag=": 3 4"; $step = 300; $tstep = 60;
  1393. } elsif($ddur <= 0.5) {
  1394. $first_tag=". 2 1"; $tag=": 3 4"; $step = 3600; $tstep = 900;
  1395. } elsif($ddur <= 1.1) { # +0.1 -> DST
  1396. $first_tag=". 2 1"; $tag=": 3 4"; $step = 4*3600; $tstep = 3600;
  1397. } elsif ($ddur <= 7.1) {
  1398. $first_tag=". 6"; $tag=". 2 1"; $step = 24*3600; $tstep = 6*3600;
  1399. } elsif ($ddur <= 31.1) {
  1400. $first_tag=". 6"; $tag=". 2 1"; $step = 7*24*3600; $tstep = 24*3600;
  1401. $aligntext = 1;
  1402. } else {
  1403. $first_tag=". 6"; $tag=". 1"; $step = 28*24*3600; $tstep = 28*24*3600;
  1404. $aligntext = 2; $aligntics = 2;
  1405. }
  1406. my $barwidth = $tstep;
  1407. ######################
  1408. # First the tics
  1409. $off2 = $y+4;
  1410. my ($off3, $off4) = ($y+$h-4, $y+$h);
  1411. my $initoffset = $tstep;
  1412. if( $conf{xrange} ) { # user defined range
  1413. if( !$xtics || $xtics ne "()" ) { # auto tics
  1414. my $xmul = $w/($xmax-$xmin);
  1415. my ($step,$mi,$ma) = SVG_getSteps( $conf{xrange}, $xmin, $xmax );
  1416. $step /= 5 if( $step > 50 );
  1417. $step /= 2 if( $step > 10 );
  1418. for(my $i = $mi; $i <= $ma; $i += $step) {
  1419. $off1 = int($x+($i-$xmin)*$xmul);
  1420. SVG_pO "<polyline class='SVGplot' points='$off1,$y $off1,$off2'/>";
  1421. SVG_pO "<polyline class='SVGplot' points='$off1,$off3 $off1,$off4'/>";
  1422. }
  1423. }
  1424. } else { # times
  1425. $initoffset = int(($tstep/2)/86400)*86400 if($aligntics);
  1426. for(my $i = $fromsec+$initoffset; $i < $tosec; $i += $tstep) {
  1427. $i = SVG_time_align($i,$aligntics);
  1428. $off1 = int($x+($i-$fromsec)*$tmul);
  1429. SVG_pO "<polyline class='SVGplot' points='$off1,$y $off1,$off2'/>";
  1430. SVG_pO "<polyline class='SVGplot' points='$off1,$off3 $off1,$off4'/>";
  1431. }
  1432. }
  1433. ######################
  1434. # then the text and the grid
  1435. $off1 = $x;
  1436. $off2 = $y+$h+$th;
  1437. my $t = SVG_fmtTime($first_tag, $fromsec);
  1438. SVG_pO "<text x=\"0\" y=\"$off2\" class=\"ylabel\">$t</text>"
  1439. if(!$conf{xrange});
  1440. $initoffset = $step;
  1441. if(SVG_Attr($parent_name,$name,"endPlotNow",undef) && $ddur>1.1 && $ddur<6.9){
  1442. my $now = time();
  1443. $initoffset -= ($now+fhemTzOffset($now))%86400; # Forum #25768
  1444. }
  1445. if( $conf{xrange} ) { # user defined range
  1446. my $xmul = $w/($xmax-$xmin);
  1447. if( $xtics ) { #user tics and grid
  1448. my $tic = $xtics;
  1449. $tic =~ s/^\((.*)\)$/$1/; # Strip ()
  1450. foreach my $onetic (split(",", $tic)) {
  1451. $onetic =~ s/^ *(.*) *$/$1/;
  1452. my ($tlabel, $tvalue) = split(" ", $onetic);
  1453. $tlabel =~ s/^"(.*)"$/$1/;
  1454. $tvalue = 0 if( !$tvalue );
  1455. $off1 = int($x+($tvalue-$xmin)*$xmul);
  1456. $t = $tvalue;
  1457. SVG_pO "<text x=\"$off1\" y=\"$off2\" class=\"ylabel\" " .
  1458. "text-anchor=\"middle\">$t</text>";
  1459. SVG_pO " <polyline points=\"$off1,$y $off1,$off4\" class=\"hgrid\"/>";
  1460. }
  1461. } else { # auto grid
  1462. my ($step,$mi,$ma) = SVG_getSteps( $conf{xrange}, $xmin, $xmax );
  1463. for(my $i = $mi; $i <= $ma; $i += $step) {
  1464. $off1 = int($x+($i-$xmin)*$xmul);
  1465. $t = $i;
  1466. SVG_pO "<text x=\"$off1\" y=\"$off2\" class=\"ylabel\" " .
  1467. "text-anchor=\"middle\">$t</text>";
  1468. SVG_pO " <polyline points=\"$off1,$y $off1,$off4\" class=\"hgrid\"/>"
  1469. if( $i != $mi && $i != $ma );
  1470. }
  1471. }
  1472. } else { # times
  1473. $initoffset = int(($step/2)/86400)*86400 if($aligntext);
  1474. for(my $i = $fromsec+$initoffset; $i < $tosec; $i += $step) {
  1475. $i = SVG_time_align($i,$aligntext);
  1476. $off1 = int($x+($i-$fromsec)*$tmul);
  1477. $t = SVG_fmtTime($tag, $i);
  1478. SVG_pO "<text x=\"$off1\" y=\"$off2\" class=\"ylabel\" " .
  1479. "text-anchor=\"middle\">$t</text>";
  1480. SVG_pO " <polyline points=\"$off1,$y $off1,$off4\" class=\"hgrid\"/>";
  1481. }
  1482. }
  1483. ######################
  1484. # Left and right axis tics / text / grid
  1485. #-- just in case we have only one data line, but want to draw both axes
  1486. $hmin{x1y1}=$hmin{x1y2}, $hmax{x1y1}=$hmax{x1y2} if(!defined($hmin{x1y1}));
  1487. $hmin{x1y2}=$hmin{x1y1}, $hmax{x1y2}=$hmax{x1y1} if(!defined($hmin{x1y2}));
  1488. my (%hstep,%htics);
  1489. #-- yrange handling for axes x1y1..x1y8
  1490. for my $idx (0..7) {
  1491. my $a = "x1y".($idx+1);
  1492. next if( !defined($hmax{$a}) || !defined($hmin{$a}) );
  1493. my $yra="y".($idx+1)."range";
  1494. $yra="yrange" if ($yra eq "y1range");
  1495. #-- yrange is specified in plotfile
  1496. if($conf{$yra}) {
  1497. $conf{$yra} = AnalyzeCommand(undef, $1)
  1498. if($conf{$yra} =~ /^({.*})$/);
  1499. if($conf{$yra} =~ /\[(.*):(.*)\]/) {
  1500. $hmin{$a} = $1 if($1 ne "");
  1501. $hmax{$a} = $2 if($2 ne "");
  1502. }
  1503. }
  1504. #-- tics handling
  1505. my $yt="y".($idx+1)."tics";
  1506. $yt="ytics" if ($yt eq"y1tics");
  1507. $htics{$a} = defined($conf{$yt}) ? $conf{$yt} : "";
  1508. #-- Round values, compute a nice step
  1509. ($hstep{$a}, $hmin{$a}, $hmax{$a}) =
  1510. SVG_getSteps($conf{$yra},$hmin{$a},$hmax{$a});
  1511. #Log3 $name, 2, "Axis $a has interval [$hmin{$a},$hmax{$a}],
  1512. # step $hstep{$a}, tics $htics{$a}\n";
  1513. }
  1514. #-- run through all axes for drawing (each only once !)
  1515. foreach my $a (sort keys %hmin) {
  1516. next if(!defined($hmin{$a})); # Bogus case
  1517. #-- safeguarding against pathological data
  1518. if( !$hstep{$a} ){
  1519. $hmax{$a} = $hmin{$a}+1;
  1520. $hstep{$a} = 1;
  1521. }
  1522. #-- Draw the y-axis values and grid
  1523. my $dh = $hmax{$a} - $hmin{$a};
  1524. my $hmul = $dh>0 ? $h/$dh : $h;
  1525. my $axis = 1;
  1526. $axis = $1 if( $a =~ m/x\d+y(\d+)/ );
  1527. my $scale = "y".($axis)."scale"; $scale = "yscale" if( $axis == 1 );
  1528. my $log = ""; $log = $conf{$scale} if( $conf{$scale} );
  1529. my $f_log = int($hmax{$a}) ? (SVG_log10($hmax{$a}) / $hmax{$a}) : 1;
  1530. # offsets
  1531. my ($align,$display,$cll);
  1532. if( $axis <= $use_left_axis ) {
  1533. $off1 = $x - ($axis-1)*$axis_width-4-$th*0.3;
  1534. $off3 = $x - ($axis-1)*$axis_width-4;
  1535. $off4 = $off3+5;
  1536. $align = " text-anchor=\"end\"";
  1537. $display = "";
  1538. $cll = "";
  1539. } elsif( $axis <= $use_left_axis+$use_right_axis ) {
  1540. $off1 = $x+4+$w+($axis-1-$use_left_axis)*$axis_width+$th*0.3;
  1541. $off3 = $x+4+$w+($axis-1-$use_left_axis)*$axis_width-5;
  1542. $off4 = $off3+5;
  1543. $align = "";
  1544. $display = "";
  1545. $cll = "";
  1546. } else {
  1547. $off1 = $x-$th*0.3+30;
  1548. $off3 = $x+30;
  1549. $off4 = $off3+5;
  1550. $align = " text-anchor=\"end\"";
  1551. $display = " display=\"none\" id=\"hline_$axis\"";
  1552. $cll = " class=\"SVGplot l$axis\"";
  1553. }
  1554. #-- grouping
  1555. SVG_pO "<g$display>";
  1556. my $yp = $y + $h;
  1557. #-- axis if not left or right axis
  1558. SVG_pO "<polyline points=\"$off3,$y $off3,$yp\" $cll/>"
  1559. if($a ne "x1y1" && $a ne "x1y2");
  1560. #-- tics handling
  1561. my $tic = $htics{$a};
  1562. #-- tics as in the config-file
  1563. if($tic && $tic !~ m/mirror/) {
  1564. $tic =~ s/^\((.*)\)$/$1/; # Strip ()
  1565. for(my $decimal = 0;
  1566. $decimal < ($log eq 'log'?SVG_log10($hmax{$a}):1);
  1567. $decimal++ ) {
  1568. foreach my $onetic (split(",", $tic)) {
  1569. $onetic =~ s/^ *(.*) *$/$1/;
  1570. my ($tlabel, $tvalue) = split(" ", $onetic);
  1571. $tlabel =~ s/^"(.*)"$/$1/;
  1572. $tvalue = 0 if( !$tvalue );
  1573. $tvalue /= 10 ** $decimal;
  1574. $tlabel = $tvalue if( !$tlabel );
  1575. $off2 = int($y+($hmax{$a}-$tvalue)*$hmul);
  1576. $off2 = int($y+($hmax{$a}-SVG_log10($tvalue)/$f_log)*$hmul)
  1577. if( $log eq 'log' );
  1578. #-- tics
  1579. SVG_pO "<polyline points=\"$off3,$off2 $off4,$off2\" $cll/>";
  1580. #--grids
  1581. my $off6 = $x+$w;
  1582. if( ($a eq "x1y1") && $conf{hasygrid} ) {
  1583. SVG_pO "<polyline points=\"$x,$off2 $off6,$off2\" class=\"vgrid\"/>"
  1584. if($tvalue > $hmin{$a} && $tvalue < $hmax{$a});
  1585. }elsif( ($a eq "x1y2") && $conf{hasy2grid} ) {
  1586. SVG_pO " <polyline points=\"$x,$off2 $off6,$off2\" class=\"vgrid\"/>"
  1587. if($tvalue > $hmin{$a} && $tvalue < $hmax{$a});
  1588. }
  1589. $off2 += $th/4;
  1590. #-- text
  1591. SVG_pO
  1592. "<text x=\"$off1\" y=\"$off2\" class=\"ylabel\"$align>$tlabel</text>";
  1593. }
  1594. }
  1595. #-- tics automatically
  1596. } elsif( $hstep{$a}>0 ) {
  1597. for(my $decimal = 0;
  1598. $decimal < ($log eq 'log'?SVG_log10($hmax{$a}):1);
  1599. $decimal++ ) {
  1600. for(my $i = $hmin{$a}; $i <= $hmax{$a}; $i += $hstep{$a}) {
  1601. my $i = $i / 10 ** $decimal;
  1602. $off2 = int($y+($hmax{$a}-$i)*$hmul);
  1603. $off2 = int($y+($hmax{$a}-SVG_log10($i)/$f_log)*$hmul)
  1604. if( $log eq 'log' );
  1605. #-- tics
  1606. SVG_pO " <polyline points=\"$off3,$off2 $off4,$off2\" $cll/>";
  1607. #--grids
  1608. my $off6 = $x+$w;
  1609. if( ($a eq "x1y1") && $conf{hasygrid} ) {
  1610. my $off6 = $x+$w;
  1611. SVG_pO " <polyline points=\"$x,$off2 $off6,$off2\" class=\"vgrid\"/>"
  1612. if($i > $hmin{$a} && $i < $hmax{$a});
  1613. }elsif( ($a eq "x1y2") && $conf{hasy2grid} ) {
  1614. SVG_pO " <polyline points=\"$x,$off2 $off6,$off2\" class=\"vgrid\"/>"
  1615. if($i > $hmin{$a} && $i < $hmax{$a});
  1616. }
  1617. $off2 += $th/4;
  1618. #-- text
  1619. my $txt = sprintf("%g", $i);
  1620. SVG_pO
  1621. "<text x=\"$off1\" y=\"$off2\" class=\"ylabel\"$align>$txt</text>";
  1622. }
  1623. }
  1624. }
  1625. SVG_pO "</g>";
  1626. }
  1627. ######################
  1628. # Second loop over the data: draw the measured points
  1629. for(my $idx=$#hdx; $idx >= 0; $idx--) {
  1630. my $a = $conf{lAxis}[$idx];
  1631. SVG_pO "<!-- Warning: No axis for data item $idx defined -->"
  1632. if(!defined($a));
  1633. next if(!defined($a));
  1634. my $axis = 1; $axis = $1 if( $a =~ m/x\d+y(\d+)/ );
  1635. my $scale = "y".($axis)."scale"; $scale = "yscale" if( $axis == 1 );
  1636. my $log = ""; $log = $conf{$scale} if( $conf{$scale} );
  1637. $min = $hmin{$a};
  1638. $hmax{$a} += 1 if($min == $hmax{$a}); # Else division by 0 in the next line
  1639. my $xmul;
  1640. $xmul = $w/($xmax-$xmin) if( $conf{xrange} );
  1641. my $hmul = $h/($hmax{$a}-$min);
  1642. my $ret = "";
  1643. my ($dxp, $dyp) = ($hdx[$idx], $hdy[$idx]);
  1644. SVG_pO "<!-- Warning: No data item $idx defined -->" if(!defined($dxp));
  1645. next if(!defined($dxp));
  1646. my $f_log = int($hmax{$a}) ? (SVG_log10($hmax{$a}) / $hmax{$a}) : 1;
  1647. if( $log eq 'log' ) {
  1648. foreach my $i (1..int(@{$dxp})-1) {
  1649. $dyp->[$i] = SVG_log10($dyp->[$i]) / $f_log;
  1650. }
  1651. }
  1652. my $yh = $y+$h;
  1653. #-- Title attributes
  1654. my $tl = $conf{lTitle}[$idx] ? $conf{lTitle}[$idx] : "";
  1655. #my $dec = int(log($hmul*3)/log(10)); # perl can be compiled without log() !
  1656. my $dec = length(sprintf("%d",$hmul*3))-1;
  1657. $dec = 0 if($dec < 0);
  1658. my $attributes = "id=\"line_$idx\" decimals=\"$dec\" ".
  1659. "x_min=\"$x\" ".
  1660. ($conf{xrange}?"x_off=\"$xmin\" ":"x_off=\"$fromsec\" ").
  1661. ($conf{xrange}?"x_mul=\"$xmul\" ":"t_mul=\"$tmul\" ").
  1662. "y_h=\"$yh\" y_min=\"$min\" y_mul=\"$hmul\" title=\"$tl\" ".
  1663. ($log eq 'log'?"log_scale=\"$f_log\" ":"").
  1664. "onclick=\"parent.svg_click(evt)\" $conf{lWidth}[$idx]";
  1665. my $lStyle = $conf{lStyle}[$idx];
  1666. my $isFill = ($conf{lStyle}[$idx] =~ m/fill/);
  1667. my $doClose = $isFill;
  1668. my ($lx, $ly) = (-1,-1);
  1669. my $lType = $conf{lType}[$idx];
  1670. if($lType eq "points" ) {
  1671. foreach my $i (0..int(@{$dxp})-1) {
  1672. my ($x1, $y1) = (int($x+$dxp->[$i]),
  1673. int($y+$h-($dyp->[$i]-$min)*$hmul));
  1674. next if($x1 == $lx && $y1 == $ly);
  1675. $ly = $x1; $ly = $y1;
  1676. $ret = sprintf(" %d,%d %d,%d %d,%d %d,%d %d,%d",
  1677. $x1-3,$y1, $x1,$y1-3, $x1+3,$y1, $x1,$y1+3, $x1-3,$y1);
  1678. SVG_pO "<polyline $attributes $lStyle points=\"$ret\"/>";
  1679. }
  1680. } elsif($lType eq "steps" || $lType eq "fsteps" ) {
  1681. $ret .= sprintf(" %d,%d", $x+$dxp->[0], $y+$h) if($isFill && @{$dxp});
  1682. if(@{$dxp} == 1) {
  1683. my $y1 = $y+$h-($dyp->[0]-$min)*$hmul;
  1684. $ret .= sprintf(" %d,%d %d,%d %d,%d %d,%d",
  1685. $x,$y+$h, $x,$y1, $x+$w,$y1, $x+$w,$y+$h);
  1686. } else {
  1687. foreach my $i (1..int(@{$dxp})-1) {
  1688. my ($x1, $y1) = ($x+$dxp->[$i-1], $y+$h-($dyp->[$i-1]-$min)*$hmul);
  1689. my ($x2, $y2) = ($x+$dxp->[$i], $y+$h-($dyp->[$i] -$min)*$hmul);
  1690. next if(int($x2) == $lx && int($y1) == $ly);
  1691. $lx = int($x2); $ly = int($y2);
  1692. if($lType eq "steps") {
  1693. $ret .= sprintf(" %d,%d %d,%d %d,%d", $x1,$y1, $x2,$y1, $x2,$y2);
  1694. } else {
  1695. $ret .= sprintf(" %d,%d %d,%d %d,%d", $x1,$y1, $x1,$y2, $x2,$y2);
  1696. }
  1697. }
  1698. }
  1699. $ret .= sprintf(" %d,%d", $lx, $y+$h) if($isFill && $lx > -1);
  1700. SVG_pO "<polyline $attributes $lStyle points=\"$ret\"/>";
  1701. } elsif($lType eq "histeps" ) {
  1702. $ret .= sprintf(" %d,%d", $x+$dxp->[0], $y+$h) if($isFill && @{$dxp});
  1703. if(@{$dxp} == 1) {
  1704. my $y1 = $y+$h-($dyp->[0]-$min)*$hmul;
  1705. $ret .= sprintf(" %d,%d %d,%d %d,%d %d,%d",
  1706. $x,$y+$h, $x,$y1, $x+$w,$y1, $x+$w,$y+$h);
  1707. } else {
  1708. foreach my $i (1..int(@{$dxp})-1) {
  1709. my ($x1, $y1) = ($x+$dxp->[$i-1], $y+$h-($dyp->[$i-1]-$min)*$hmul);
  1710. my ($x2, $y2) = ($x+$dxp->[$i], $y+$h-($dyp->[$i] -$min)*$hmul);
  1711. next if(int($x2) == $lx && int($y1) == $ly);
  1712. $lx = int($x2); $ly = int($y2);
  1713. $ret .= sprintf(" %d,%d %d,%d %d,%d %d,%d",
  1714. $x1,$y1, ($x1+$x2)/2,$y1, ($x1+$x2)/2,$y2, $x2,$y2);
  1715. }
  1716. }
  1717. $ret .= sprintf(" %d,%d", $lx, $y+$h) if($isFill && $lx > -1);
  1718. SVG_pO "<polyline $attributes $lStyle points=\"$ret\"/>";
  1719. } elsif( $lType eq "bars" ) {
  1720. my $bw = $barwidth*$tmul;
  1721. # bars are all of equal width (see far above !),
  1722. # position rounded to integer multiples of bar width
  1723. foreach my $i (0..int(@{$dxp})-1) {
  1724. my ($x1, $y1) = ( $x + $dxp->[$i] - $bw,
  1725. $y +$h-($dyp->[$i]-$min)*$hmul);
  1726. my $curBw = $bw;
  1727. if($x1 < $x) {
  1728. $curBw -= $x - $x1;
  1729. $x1 = $x;
  1730. }
  1731. my ($x2, $y2) = ($curBw, ($dyp->[$i]-$min)*$hmul);
  1732. SVG_pO "<rect $attributes $lStyle x=\"$x1\" y=\"$y1\" ".
  1733. "width=\"$x2\" height=\"$y2\"/>";
  1734. }
  1735. } elsif( $lType eq "ibars" ) { # Forum #35268
  1736. if(@{$dxp} == 1) {
  1737. my $y1 = $y+$h-($dyp->[0]-$min)*$hmul;
  1738. $ret .= sprintf(" %d,%d %d,%d %d,%d %d,%d",
  1739. $x,$y+$h, $x,$y1, $x+$w,$y1, $x+$w,$y+$h);
  1740. } else {
  1741. # interconnected bars (ibars):
  1742. # these bars will connect all datapoints. so the width of the bars
  1743. # might vary depending on the distance between data points
  1744. foreach my $i (1..int(@{$dxp})-1) {
  1745. my $x1 = $x + $dxp->[$i-1];
  1746. my $y1 = $y +$h-($dyp->[$i]-$min)*$hmul;
  1747. my $x2 = $x + $dxp->[$i]; # used to calculate bar width later
  1748. my $height = ($dyp->[$i]-$min)*$hmul;
  1749. my $bw = $x2 - $x1;
  1750. SVG_pO "<rect $attributes $lStyle x=\"$x1\" y=\"$y1\" ".
  1751. "width=\"$bw\" height=\"$height\"/>";
  1752. }
  1753. }
  1754. } else { # lines and everything else
  1755. my ($ymin, $ymax) = (99999999, -99999999);
  1756. my %lt =(cubic=>"C",quadratic=>"Q",quadraticSmooth=>"T");
  1757. my ($x1, $y1);
  1758. my $lt = ($lt{$lType} ? $lt{$lType} : "L"); # defaults to line
  1759. my $maxIdx = int(@{$dxp})-1;
  1760. foreach my $i (0..$maxIdx) {
  1761. if( !defined($dxp->[$i]) ) { # specials
  1762. if( $dyp->[$i] =~ m/^;$/ ) { # new line segment after newline
  1763. my @tvals = split("[ ,]", $ret);
  1764. if (@tvals > 2) {
  1765. if ($tvals[0] ne "M") { # just points, no M/L
  1766. $ret = sprintf("M %d,%d $lt $ret", $tvals[1],$tvals[2]);
  1767. }
  1768. }
  1769. SVG_pO "<path $attributes $lStyle d=\"$ret\"/>";
  1770. $ret = "";
  1771. } elsif( $dyp->[$i] =~ m/^;c (.*)/ ) {# close polyline ?
  1772. $doClose = $1;
  1773. } elsif( $dyp->[$i] =~ m/^;ls (\w+)?/ ) {# line style
  1774. if( $1 ) {
  1775. $lStyle = "class='SVGplot $1'";
  1776. } else {
  1777. $lStyle = $conf{lStyle}[$idx];
  1778. }
  1779. # marker with optional text
  1780. } elsif( $dyp->[$i] =~ m/^;m (\S+)\s(\S+)(\s(\S+)\s(.*))?/ ) {
  1781. if( defined($xmin) ) {
  1782. $x1 = int($x+($1-$xmin)*$xmul);
  1783. } else {
  1784. $x1 = ($tmul ? int((SVG_time_to_sec($1)-$fromsec)*$tmul) : $x);
  1785. }
  1786. $y1 = int($y+$h-($2-$min)*$hmul);
  1787. my $ret = sprintf("%d,%d %d,%d %d,%d %d,%d %d,%d",
  1788. $x1-3,$y1, $x1,$y1-3, $x1+3,$y1, $x1,$y1+3, $x1-3,$y1);
  1789. SVG_pO "<polyline $attributes $lStyle points=\"$ret\"/>";
  1790. SVG_pO "<text x=\"$x1\" y=\"$y1\" $lStyle text-anchor=\"$4\">$5".
  1791. "</text>" if( $3 );
  1792. } elsif( $dyp->[$i] =~ m/^;t (\S+)\s(\S+)\s(\S+)\s(.*)/ ) {# text
  1793. if( defined($xmin) ) {
  1794. $x1 = int($x+($1-$xmin)*$xmul);
  1795. } else {
  1796. $x1 = ($tmul ? int((SVG_time_to_sec($1)-$fromsec)*$tmul) : $x);
  1797. }
  1798. $y1 = int($y+$h-($2-$min)*$hmul);
  1799. SVG_pO "<text x=\"$x1\" y=\"$y1\" $lStyle text-anchor=\"$3\">$4".
  1800. "</text>";
  1801. } else {
  1802. Log3 $name, 2, "unknown special $dyp->[$i]"
  1803. }
  1804. next;
  1805. }
  1806. ($x1, $y1) = (int($x+$dxp->[$i]),
  1807. int($y+$h-($dyp->[$i]-$min)*$hmul));
  1808. next if($x1 == $lx && $y1 == $ly);
  1809. # calc ymin/ymax for points with the same x coordinates
  1810. if($x1 == $lx && $i < $maxIdx) {
  1811. $ymin = $y1 if($y1 < $ymin);
  1812. $ymax = $y1 if($y1 > $ymax);
  1813. $ly = $y1;
  1814. next;
  1815. }
  1816. if($i == 0) {
  1817. if($doClose) {
  1818. $ret .= sprintf("M %d,%d L %d,%d $lt", $x1,$y+$h, $x1,$y1);
  1819. } else {
  1820. $ret .= sprintf("M %d,%d $lt", $x1,$y1);
  1821. }
  1822. $lx = $x1; $ly = $y1;
  1823. next;
  1824. }
  1825. # plot ymin/ymax range for points with the same x coordinates
  1826. if( $ymin != 99999999 ) {
  1827. $ret .= sprintf(" %d,%d", $lx, $ymin);
  1828. $ret .= sprintf(" %d,%d", $lx, $ymax);
  1829. $ret .= sprintf(" %d,%d", $lx, $ly);
  1830. ($ymin, $ymax) = (99999999, -99999999);
  1831. }
  1832. $ret .= sprintf(" %d,%d", $x1, $y1) if ($lt ne "T");
  1833. $ret .= sprintf(" %.1f,%.1f", (($lx+$x1)/2.0), (($ly+$y1)/2.0))
  1834. if (($lt eq "T") && ($lx > -1));
  1835. $lx = $x1; $ly = $y1;
  1836. }
  1837. #-- calculate control points for interpolation
  1838. $ret = SVG_getControlPoints($ret) if (($lt eq "C") || ($lt eq "Q"));
  1839. #-- insert last point for filled line
  1840. $ret .= sprintf(" %.1f,%.1f", $x1, $y1) if(($lt eq "T") && defined($x1));
  1841. $ret .= sprintf(" L %d,%d Z", $x1, $y+$h) if($doClose && defined($x1));
  1842. if($ret =~ m/^ (\d+),(\d+)/) { # just points, no M/L
  1843. $ret = sprintf("M %d,%d $lt ", $1, $2).$ret;
  1844. }
  1845. $ret = "" if($maxIdx == 0);
  1846. SVG_pO "<path $attributes $lStyle d=\"$ret\"/>";
  1847. }
  1848. }
  1849. ######################
  1850. # Plot caption (title) at the end, should be draw on top of the lines
  1851. my $caption_pos = SVG_Attr($parent_name, $name, "captionPos", 'right');
  1852. my( $li,$ri ) = (0,0);
  1853. for my $i (0..int(@{$conf{lTitle}})-1) {
  1854. my $caption_anchor = "end";
  1855. if( $caption_pos eq 'auto' ) {
  1856. my $a = $conf{lAxis}[$i];
  1857. my $axis = 1; $axis = $1 if( $a && $a =~ m/x\d+y(\d+)/ );
  1858. if( $axis <= $use_left_axis ) {
  1859. $caption_anchor = "beginning";
  1860. } else {
  1861. $caption_anchor = "end";
  1862. }
  1863. } elsif( $caption_pos eq 'left' ) {
  1864. $caption_anchor = "beginning";
  1865. }
  1866. my $txtoff1 = $nr_left_axis*$axis_width + $w - $th/2;
  1867. $txtoff1 = $nr_left_axis*$axis_width+$th/2
  1868. if($caption_anchor eq 'beginning');
  1869. my $j = $i+1;
  1870. my $t = $conf{lTitle}[$i];
  1871. next if( !$t );
  1872. my $txtoff2;
  1873. if( $caption_anchor eq 'beginning' ) {
  1874. $txtoff2 = $y + 3 + $th/1.3 + $th * $li;
  1875. ++$li;
  1876. } else {
  1877. $txtoff2 = $y + 3 + $th/1.3 + $th * $ri;
  1878. ++$ri;
  1879. }
  1880. my $desc = "";
  1881. if(defined($data{"min$j"}) && $data{"min$j"} ne "undef" &&
  1882. defined($data{"currval$j"}) && $data{"currval$j"} ne "undef") {
  1883. $desc = sprintf("%s: Min:%g Max:%g Last:%g",
  1884. $t, $data{"min$j"}, $data{"max$j"}, $data{"currval$j"});
  1885. }
  1886. my $style = $conf{lStyle}[$i];
  1887. $style =~ s/class="/class="legend /;
  1888. SVG_pO "<text line_id=\"line_$i\" x=\"$txtoff1\" y=\"$txtoff2\" ".
  1889. "text-anchor=\"$caption_anchor\" $style>$t<title>$desc</title></text>";
  1890. $txtoff2 += $th;
  1891. }
  1892. my $fnName = SVG_isEmbed($FW_wname) ? "parent.window.svg_init" : "svg_init";
  1893. SVG_pO "<script type='text/javascript'>if(typeof $fnName == 'function') ".
  1894. "$fnName('SVGPLOT_$name')</script>";
  1895. SVG_pO "</svg>";
  1896. return $SVG_RET;
  1897. }
  1898. ######################
  1899. # Derives control points for interpolation of bezier curves for SVG "path"
  1900. sub
  1901. SVG_getControlPoints($)
  1902. {
  1903. my ($ret) = @_;
  1904. my (@xa, @ya);
  1905. my (@vals) = split("[ ,]", $ret);
  1906. my (@xcp1, @xcp2, @ycp1, @ycp2);
  1907. my $header = "";
  1908. foreach my $i (0..int(@vals)-1) {
  1909. $header .= $vals[$i] . ($i == 1 ? "," : " ");
  1910. if ($vals[$i] eq "C" || $vals[$i] eq "Q") {
  1911. my $lt = $vals[$i];
  1912. $i++;
  1913. my $ii = 0;
  1914. while (defined($vals[$i])) {
  1915. ($xa[$ii], $ya[$ii]) = ($vals[$i], $vals[$i+1]);
  1916. $i += 2;
  1917. $ii++;
  1918. }
  1919. return(1) if (@xa < 2);
  1920. SVG_calcControlPoints(\@xcp1, \@xcp2, \@ycp1, \@ycp2, \@xa, \@ya);
  1921. $ret = $header;
  1922. foreach my $i (1..int(@xa)-1) {
  1923. $ret .= sprintf(" %d,%d,%d,%d,%d,%d", $xcp1[$i-1], $ycp1[$i-1], $xcp2[$i-1], $ycp2[$i-1], $xa[$i], $ya[$i]) if ($lt eq "C");
  1924. $ret .= sprintf(" %d,%d,%d,%d", $xcp2[$i-1], $ycp2[$i-1], $xa[$i], $ya[$i]) if ($lt eq "Q");
  1925. }
  1926. }
  1927. }
  1928. return($ret);
  1929. }
  1930. ######################
  1931. # Calculate control points for interpolation of bezier curves for SVG "path"
  1932. sub
  1933. SVG_calcControlPoints($$$$$$)
  1934. {
  1935. my ($px1, $px2, $py1, $py2, $inputx, $inputy) = @_;
  1936. my $n = @{$inputx};
  1937. # Loop over all Points in Input arrays
  1938. for (my $i=0; $i<$n-1; $i++) {
  1939. my (@lxp, @lyp);
  1940. # Loop over 4 Points around actual Point to calculate
  1941. my $iloc = 0;
  1942. for (my $ii=$i-1; $ii<=$i+2; $ii++) {
  1943. my $icorr = $ii;
  1944. $icorr = 0 if ($icorr < 0);
  1945. $icorr = $n-1 if ($icorr > $n-1);
  1946. $lxp[$iloc] = $inputx->[$icorr];
  1947. $lyp[$iloc] = $inputy->[$icorr];
  1948. $iloc++;
  1949. }
  1950. # Calulcation of first control Point using first 3 Points around actual Point
  1951. my $m1x = ($lxp[0]+$lxp[1])/2.0;
  1952. my $m1y = ($lyp[0]+$lyp[1])/2.0;
  1953. my $m2x = ($lxp[1]+$lxp[2])/2.0;
  1954. my $m2y = ($lyp[1]+$lyp[2])/2.0;
  1955. my $l1 = sqrt(($lxp[0]-$lxp[1])*($lxp[0]-$lxp[1])+($lyp[0]-$lyp[1])*($lyp[0]-$lyp[1]));
  1956. my $l2 = sqrt(($lxp[1]-$lxp[2])*($lxp[1]-$lxp[2])+($lyp[1]-$lyp[2])*($lyp[1]-$lyp[2]));
  1957. my $dxm = ($m1x - $m2x);
  1958. my $dym = ($m1y - $m2y);
  1959. my $k = 0;
  1960. $k = $l2/($l1+$l2) if (($l1+$l2) != 0);
  1961. my $tx = $lxp[1] - ($m2x + $dxm*$k);
  1962. my $ty = $lyp[1] - ($m2y + $dym*$k);
  1963. $px1->[$i] = $m2x + $tx;
  1964. $py1->[$i] = $m2y + $ty;
  1965. # Calulcation of second control Point using last 3 Points around actual Point
  1966. $m1x = ($lxp[1]+$lxp[2])/2.0;
  1967. $m1y = ($lyp[1]+$lyp[2])/2.0;
  1968. $m2x = ($lxp[2]+$lxp[3])/2.0;
  1969. $m2y = ($lyp[2]+$lyp[3])/2.0;
  1970. $l1 = sqrt(($lxp[1]-$lxp[2])*($lxp[1]-$lxp[2])+($lyp[1]-$lyp[2])*($lyp[1]-$lyp[2]));
  1971. $l2 = sqrt(($lxp[2]-$lxp[3])*($lxp[2]-$lxp[3])+($lyp[2]-$lyp[3])*($lyp[2]-$lyp[3]));
  1972. $dxm = ($m1x - $m2x);
  1973. $dym = ($m1y - $m2y);
  1974. $k=0;
  1975. $k = $l2/($l1+$l2) if (($l1+$l2) != 0);
  1976. $tx = $lxp[2] - ($m2x + $dxm*$k);
  1977. $ty = $lyp[2] - ($m2y + $dym*$k);
  1978. $px2->[$i] = $m1x + $tx;
  1979. $py2->[$i] = $m1y + $ty;
  1980. }
  1981. return (1);
  1982. }
  1983. sub
  1984. SVG_fmtTime($$)
  1985. {
  1986. my ($sepfmt, $sec) = @_;
  1987. my @tarr = split("[ :]+", localtime($sec));
  1988. my ($sep, $fmt) = split(" ", $sepfmt, 2);
  1989. my $ret = "";
  1990. for my $f (split(" ", $fmt)) {
  1991. $ret .= $sep if($ret);
  1992. $ret .= $tarr[$f];
  1993. }
  1994. return $ret;
  1995. }
  1996. sub
  1997. SVG_time_align($$)
  1998. {
  1999. my ($v,$align) = @_;
  2000. return $v if(!$align);
  2001. if($align == 1) { # Look for the beginning of the week
  2002. for(;;) {
  2003. my @a = localtime($v);
  2004. return $v if($a[6] == 0);
  2005. $v += 86400;
  2006. }
  2007. }
  2008. if($align == 2) { # Look for the beginning of the month
  2009. for(;;) {
  2010. my @a = localtime($v);
  2011. return $v if($a[3] == 1);
  2012. $v += 86400;
  2013. }
  2014. }
  2015. }
  2016. sub
  2017. SVG_doround($$$)
  2018. {
  2019. my ($v, $step, $isup) = @_;
  2020. $step = 1 if(!$step); # Avoid division by zero
  2021. my $d = $v/$step;
  2022. my $dr = int($d);
  2023. return $v if($d == $dr);
  2024. if($v >= 0) {
  2025. return int($v/$step)*$step+($isup ? $step : 0);
  2026. } else {
  2027. return int($v/$step)*$step+($isup ? 0 : -$step);
  2028. }
  2029. }
  2030. ##################
  2031. # print (append) to output
  2032. sub
  2033. SVG_pO($)
  2034. {
  2035. my $arg = shift;
  2036. return if(!defined($arg));
  2037. $SVG_RET .= $arg;
  2038. $SVG_RET .= "\n";
  2039. }
  2040. ##################
  2041. # this is a helper function which creates a PNG image from a given plot
  2042. sub
  2043. plotAsPng(@)
  2044. {
  2045. my (@plotName) = @_;
  2046. my (@webs, $mimetype, $svgdata, $rsvg, $pngImg);
  2047. @webs=devspec2array("TYPE=FHEMWEB");
  2048. foreach(@webs) {
  2049. if(!InternalVal($_,'TEMPORARY',undef)) {
  2050. $FW_wname=InternalVal($_,'NAME','');
  2051. last;
  2052. }
  2053. }
  2054. #Debug "FW_wname= $FW_wname, plotName= $plotName[0]";
  2055. $FW_RET = undef;
  2056. $FW_webArgs{dev} = $plotName[0];
  2057. $FW_webArgs{logdev} = InternalVal($plotName[0], "LOGDEVICE", "");
  2058. $FW_webArgs{gplotfile} = InternalVal($plotName[0], "GPLOTFILE", "");
  2059. $FW_webArgs{logfile} = InternalVal($plotName[0], "LOGFILE", "CURRENT");
  2060. $FW_pos{zoom} = $plotName[1] if $plotName[1];
  2061. $FW_pos{off} = $plotName[2] if $plotName[2];
  2062. ($mimetype, $svgdata) = SVG_showLog("unused");
  2063. #Debug "MIME type= $mimetype";
  2064. #Debug "SVG= $svgdata";
  2065. my ($w, $h) = split(",", AttrVal($plotName[0],"plotsize","800,160"));
  2066. $svgdata =~ s/<\/svg>/<polyline opacity="0" points="0,0 $w,$h"\/><\/svg>/;
  2067. $svgdata =~ s/\.SVGplot\./\./g;
  2068. eval {
  2069. require Image::LibRSVG;
  2070. $rsvg = new Image::LibRSVG();
  2071. $rsvg->loadImageFromString($svgdata);
  2072. $pngImg = $rsvg->getImageBitmap();
  2073. };
  2074. Log3 $FW_wname, 1,
  2075. "plotAsPng(): Cannot create plot as png image for \"" .
  2076. join(" ", @plotName) . "\": $@"
  2077. if($@ or !defined($pngImg) or ($pngImg eq ""));
  2078. return $pngImg if $pngImg;
  2079. return;
  2080. }
  2081. ##################
  2082. 1;
  2083. ##################
  2084. =pod
  2085. =item helper
  2086. =item summary draw an SVG-Plot based on FileLog or DbLog data
  2087. =item summary_DE malt ein SVG-Plot aus FileLog oder DbLog Daten
  2088. =begin html
  2089. <a name="SVG"></a>
  2090. <h3>SVG</h3>
  2091. <ul>
  2092. <a name="SVGlinkdefine"></a>
  2093. <b>Define</b>
  2094. <ul>
  2095. <code>define &lt;name&gt; SVG
  2096. &lt;logDevice&gt;:&lt;gplotfile&gt;:&lt;logfile&gt;</code>
  2097. <br><br>
  2098. This is the Plotting/Charting device of FHEMWEB
  2099. Examples:
  2100. <ul>
  2101. <code>define MyPlot SVG inlog:temp4hum4:CURRENT</code><br>
  2102. </ul>
  2103. <br>
  2104. Notes:
  2105. <ul>
  2106. <li>Normally you won't define an SVG device manually, as
  2107. FHEMWEB makes it easy for you, just plot a logfile (see <a
  2108. href="#logtype">logtype</a>) and click on "Create SVG instance".
  2109. Specifying CURRENT as a logfilename will always access the current
  2110. logfile, even if its name changes regularly.</li>
  2111. <li>For historic reasons this module uses a Gnuplot file description
  2112. to store different attributes. Some special commands (beginning with
  2113. #FileLog or #DbLog) are used additionally, and not all gnuplot
  2114. attribtues are implemented.</li>
  2115. </ul>
  2116. </ul>
  2117. <a name="SVGset"></a>
  2118. <b>Set</b>
  2119. <ul>
  2120. <li>copyGplotFile<br>
  2121. Copy the currently specified gplot file to a new file, which is named
  2122. after the SVG device, existing files will be overwritten.
  2123. This operation is needed in order to use the plot editor (see below)
  2124. without affecting other SVG instances using the same gplot file.
  2125. Creating the SVG instance from the FileLog detail menu will also
  2126. create a unique gplot file, in this case this operation is not needed.
  2127. </li>
  2128. </ul><br>
  2129. <a name="SVGget"></a>
  2130. <b>Get</b> <ul>N/A</ul><br>
  2131. <a name="SVGattr"></a>
  2132. <b>Attributes</b>
  2133. <ul>
  2134. <li><a href="#endPlotNow">endPlotNow</a></li><br>
  2135. <li><a href="#endPlotToday">endPlotToday</a></li><br>
  2136. <a name="captionLeft"></a>
  2137. <li>captionLeft<br>
  2138. Show the legend on the left side (deprecated, will be autoconverted to
  2139. captionPos)
  2140. </li><br>
  2141. <a name="captionPos"></a>
  2142. <li>captionPos<br>
  2143. right - Show the legend on the right side (default)<br>
  2144. left - Show the legend on the left side<br>
  2145. auto - Show the legend labels on the left or on the right side depending
  2146. on the axis it belongs to<br>
  2147. </li><br>
  2148. <a name="fixedrange"></a>
  2149. <li>fixedrange [offset]<br>
  2150. Contains two time specs in the form YYYY-MM-DD separated by a space.
  2151. In plotmode gnuplot-scroll(-svg) or SVG the given time-range will be
  2152. used, and no scrolling for this SVG will be possible. Needed e.g. for
  2153. looking at last-years data without scrolling.<br><br> If the value is
  2154. one of hour, day, &lt;N&gt;days, week, month, year then set the zoom
  2155. level for this SVG independently of the user specified zoom-level. This
  2156. is useful for pages with multiple plots: one of the plots is best
  2157. viewed in with the default (day) zoom, the other one with a week
  2158. zoom.<br>
  2159. If given, the optional integer parameter offset refers to a different
  2160. period (e.g. last year: fixedrange year -1, 2 days ago: fixedrange day
  2161. -2).
  2162. </li><br>
  2163. <a name="fixedoffset"></a>
  2164. <li>fixedoffset &lt;nDays&gt;<br>
  2165. Set an fixed offset (in days) for the plot.
  2166. </li><br>
  2167. <a name="label"></a>
  2168. <li>label<br>
  2169. Double-Colon separated list of values. The values will be used to replace
  2170. &lt;L#&gt; type of strings in the .gplot file, with # beginning at 1
  2171. (&lt;L1&gt;, &lt;L2&gt;, etc.). Each value will be evaluated as a perl
  2172. expression, so you have access e.g. to the Value functions.<br><br>
  2173. If the plotmode is gnuplot-scroll(-svg) or SVG, you can also use the min,
  2174. max, mindate, maxdate, avg, cnt, sum, firstval, firstdate, currval (last
  2175. value) and currdate (last date) values of the individual curves, by
  2176. accessing the corresponding values from the data hash, see the example
  2177. below:<br>
  2178. <ul>
  2179. <li>Fixed text for the right and left axis:<br>
  2180. <ul>
  2181. <li>Fhem config:<br>
  2182. attr wl_1 label "Temperature"::"Humidity"</li>
  2183. <li>.gplot file entry:<br>
  2184. set ylabel &lt;L1&gt;<br>
  2185. set y2label &lt;L2&gt;</li>
  2186. </ul></li>
  2187. <li>Title with maximum and current values of the 1st curve (FileLog)
  2188. <ul>
  2189. <li>Fhem config:<br>
  2190. attr wl_1 label "Max $data{max1}, Current $data{currval1}"</li>
  2191. <li>.gplot file entry:<br>
  2192. set title &lt;L1&gt;<br></li>
  2193. </ul></li>
  2194. </ul>
  2195. The value minAll and maxAll (representing the minimum/maximum over all
  2196. values) is also available from the data hash.
  2197. <br>Deprecated, see plotReplace.
  2198. </li><br>
  2199. <li><a href="#nrAxis">nrAxis</a></li><br>
  2200. <a name="plotfunction"></a>
  2201. <li>plotfunction<br>
  2202. Space value separated list of values. The value will be used to replace
  2203. &lt;SPEC#&gt; type of strings in the .gplot file, with # beginning at 1
  2204. (&lt;SPEC1&gt;, &lt;SPEC2&gt;, etc.) in the #FileLog or #DbLog directive.
  2205. With this attribute you can use the same .gplot file for multiple devices
  2206. with the same logdevice.
  2207. <ul><b>Example:</b><br>
  2208. <li>#FileLog &lt;SPEC1&gt;<br>
  2209. with: attr &lt;SVGdevice&gt; plotfunction "4:IR\x3a:0:"<br>
  2210. instead of<br>
  2211. #FileLog 4:IR\x3a:0:
  2212. </li>
  2213. <li>#DbLog &lt;SPEC1&gt;<br>
  2214. with: attr &lt;SVGdevice&gt; plotfunction
  2215. "Garage_Raumtemp:temperature::"<br> instead of<br>
  2216. #DbLog Garage_Raumtemp:temperature::
  2217. </li>
  2218. </ul>
  2219. Deprecated, see plotReplace.
  2220. </li><br>
  2221. <li><a href="#plotmode">plotmode</a></li><br>
  2222. <a name="plotReplace"></a>
  2223. <li>plotReplace<br>
  2224. space separated list of key=value pairs. value may contain spaces if
  2225. enclosed in "" or {}. value will be evaluated as a perl expression, if it
  2226. is enclosed in {}.
  2227. <br>
  2228. In the .gplot file &lt;key&gt; is replaced with the corresponding value,
  2229. the evaluation of {} takes place <i>after</i> the input file is
  2230. processed, so $data{min1} etc can be used.
  2231. <br>
  2232. %key% will be repaced <i>before</i> the input file is processed, this
  2233. expression can be used to replace parameters for the input processing.
  2234. </li><br>
  2235. <li><a href="#plotsize">plotsize</a></li><br>
  2236. <li><a href="#plotWeekStartDay">plotWeekStartDay</a></li><br>
  2237. <a name="startDate"></a>
  2238. <li>startDate<br>
  2239. Set the start date for the plot. Used for demo installations.
  2240. </li><br>
  2241. <a name="title"></a>
  2242. <li>title<br>
  2243. A special form of label (see above), which replaces the string &lt;TL&gt;
  2244. in the .gplot file. It defaults to the filename of the logfile.
  2245. <br>Deprecated, see plotReplace.
  2246. </li><br>
  2247. </ul>
  2248. <br>
  2249. <a name="plotEditor"></a>
  2250. <b>Plot-Editor</b>
  2251. <br>
  2252. This editor is visible on the detail screen of the SVG instance.
  2253. Most features are obvious here, up to some exceptions:
  2254. <ul>
  2255. <li>if you want to omit the title for a Diagram label, enter notitle in the
  2256. input field.</li>
  2257. <li>if you want to specify a fixed value (not taken from a column) if a
  2258. string found (e.g. 1 if the FS20 switch is on and 0 if it is off), then
  2259. you have to specify the Tics first, and write the .gplot file, before you
  2260. can select this value from the dropdown.<br>
  2261. Example:
  2262. <ul>
  2263. Enter in the Tics field: ("On" 1, "Off" 0)<br>
  2264. Write .gplot file<br>
  2265. Select "1" from the column dropdown (note the double quote!) for the
  2266. regexp switch.on, and "0" for the regexp switch.off.<br>
  2267. Write .gplot file again<br>
  2268. </ul></li>
  2269. <li>If the range is of the form {...}, then it will be evaluated with perl.
  2270. The result is a string, and must have the form [min:max]
  2271. </li>
  2272. </ul>
  2273. The visibility of the ploteditor can be configured with the FHEMWEB attribute
  2274. <a href="#ploteditor">ploteditor</a>.
  2275. <br>
  2276. </ul>
  2277. =end html
  2278. =begin html_DE
  2279. <a name="SVG"></a>
  2280. <h3>SVG</h3>
  2281. <ul>
  2282. <a name="SVGlinkdefine"></a>
  2283. <b>Define</b>
  2284. <ul>
  2285. <code>define &lt;name&gt; SVG &lt;logDevice&gt;:&lt;gplotfile&gt;:&lt;logfile&gt;</code>
  2286. <br><br>
  2287. Dies ist das Zeichenmodul von FHEMWEB, mit dem Vektorgrafiken (SVG) erzeugt
  2288. werden. <br><br>
  2289. Beispiel:
  2290. <ul>
  2291. <code>define MyPlot SVG inlog:temp4hum4:CURRENT</code><br>
  2292. </ul>
  2293. <br>
  2294. Hinweise:
  2295. <ul>
  2296. <li>Normalerweise m&uuml;ssen SVG-Ger&auml;te nicht manuell erzeugt
  2297. werden, da FHEMWEB es f&uuml;r den Nutzer einfach macht: man muss in
  2298. der Detailansicht eines FileLogs wechseln und auf "Create SVG instance"
  2299. klicken.</li>
  2300. <li>CURRENT als &lt;logfile&gt; wird immer das aktuelle Logfile
  2301. benutzen, selbst dann, wenn der Name des Logfiles sich
  2302. regelm&auml;&szlig;ig &auml;ndert. </li>
  2303. <li>Aus historischen Gr&uuml;nden ben&ouml;tigt jede SVG-Instanz eine
  2304. sog. .gplot Datei, die auch als Input f&uuml;r das gnuplot Programm
  2305. verwendet werden kann. Einige besondere Zeilen (welche mit #FileLog
  2306. oder #DbLog beginnen) werden zus&auml;tzlich benutzt, diese werden von
  2307. gnuplot als Kommentar betrachtet. Auf der anderen Seite implementiert
  2308. dieses Modul nicht alle gnuplot-Attribute.</li>
  2309. </ul>
  2310. </ul>
  2311. <br>
  2312. <a name="SVGset"></a>
  2313. <b>Set</b>
  2314. <ul>
  2315. <li>copyGplotFile<br>
  2316. Kopiert die aktuell ausgew&auml;hlte .gplot Datei in eine neue Datei, die
  2317. den Namen der SVG Instanz tr&auml;gt; bereits bestehende Dateien mit
  2318. gleichem Namen werden &uuml;berschrieben. Diese Vorgehensweise ist
  2319. notwendig, wenn man den Ploteditor benutzt. Erzeugt man aus der
  2320. Detailansicht des FileLogs die SVG Instanz, wird eine eindeutige
  2321. .gplot-Datei erzeugt. In diesem Fall ist dieses Befehl nicht
  2322. erforderlich.</li>
  2323. </ul><br>
  2324. <a name="SVGget"></a>
  2325. <b>Get</b> <ul>N/A</ul><br>
  2326. <a name="SVGattr"></a>
  2327. <b>Attribute</b>
  2328. <ul>
  2329. <a name="captionLeft"></a>
  2330. <li>captionLeft<br>
  2331. Anzeigen der Legende auf der linken Seite. &Uuml;berholt, wird
  2332. automatisch nach captionPos konvertiert.
  2333. </li><br>
  2334. <a name="captionPos"></a>
  2335. <li>captionPos<br>
  2336. right - Anzeigen der Legende auf der rechten Seite (default)<br>
  2337. left - Anzeigen der Legende auf der linken Seite<br>
  2338. auto - Anzeigen der Labels der Legende auf der linken oder rechten Seite
  2339. je nach Achsenzugeh&ouml;rigkeit<br>
  2340. </li><br>
  2341. <li><a href="#endPlotNow">endPlotNow</a></li><br>
  2342. <li><a href="#endPlotToday">endPlotToday</a></li><br>
  2343. <a name="fixedoffset"></a>
  2344. <li>fixedoffset &lt;nTage&gt;<br>
  2345. Verschiebt den Plot-Offset um einen festen Wert (in Tagen).
  2346. </li><br>
  2347. <a name="fixedrange"></a>
  2348. <li>fixedrange [offset]<br>
  2349. Erste Alternative:<br>
  2350. Enth&auml;lt zwei Zeit-Spezifikationen in der Schreibweise YYYY-MM-DD,
  2351. getrennt durch ein Leerzeichen. scrollen der Zeitachse ist nicht
  2352. m&ouml;glich, es wird z.B. verwendet, um sich die Daten verschiedener
  2353. Jahre auf eine Seite anzusehen.<br><br>
  2354. Zweite Alternative:<br>
  2355. Wenn der Wert entweder hour, day, &lt;N&gt;days, week, month oder year
  2356. lautet, kann der Zoom-Level f&uuml;r dieses SVG unabh&auml;ngig vom
  2357. User-spezifischen Zoom eingestellt werden. Diese Einstellung ist
  2358. n&uuml;tzlich f&uuml;r mehrere Plots auf einer Seite: Eine Grafik ist mit
  2359. dem Standard-Zoom am aussagekr&auml;ftigsten, die anderen mit einem Zoom
  2360. &uuml;ber eine Woche. Der optionale ganzzahlige Parameter [offset] setzt
  2361. ein anderes Zeitintervall (z.B. letztes Jahr: <code>fixedrange year
  2362. -1</code>, vorgestern:<code> fixedrange day -2</code>).
  2363. </li><br>
  2364. <a name="label"></a>
  2365. <li>label<br>
  2366. Eine Liste, bei der die einzelnen Werte mit einem zweifachen Doppelpunkt
  2367. voneinander getrennt werden. Diese Liste wird verwendet um die &lt;L#&gt;
  2368. Zeichenfolgen in der .gplot-Datei zu ersetzen. Dabei steht das # f&uuml;r
  2369. eine laufende Ziffer beginnend mit 1 (&lt;L1&gt;, &lt;L2&gt;, usw.).
  2370. Jeder Wert wird als Perl-Ausdruck bewertet, deshalb hat man Zugriff z.B.
  2371. auf die hinterlegten Funktionen. <br><br>
  2372. Egal, ob es sich bei der Plotart um gnuplot-scroll(-svg) oder SVG
  2373. handelt, es k&ouml;nnen ebenfalls die Werte der individuellen Kurve
  2374. f&uuml;r min, max, mindate, maxdate, avg, cnt, sum, currval (letzter
  2375. Wert) und currdate (letztes Datum) durch Zugriff der entsprechenden Werte
  2376. &uuml;ber das data Hash verwendet werden. Siehe untenstehendes
  2377. Beispiel:<br>
  2378. <ul>
  2379. <li>Beschriftunng der rechten und linken y-Achse:<br>
  2380. <ul>
  2381. <li>Fhem config:<br>
  2382. <code>attr wl_1 label "Temperature"::"Humidity"</code></li>
  2383. <li>Eintrag in der .gplot-Datei:<br>
  2384. <code>set ylabel &lt;L1&gt;<br>
  2385. set y2label &lt;L2&gt;</code></li>
  2386. </ul>
  2387. </li>
  2388. <li>&Uuml;berschrift aus Maximum und dem letzten Wert der ersten
  2389. Kurve(FileLog)
  2390. <ul>
  2391. <li>Fhem config:<br>
  2392. <code>attr wl_1 label "Max $data{max1}, Current
  2393. $data{currval1}"</code></li>
  2394. <li>Eintrag in der .gplot-Datei:<br>
  2395. <code>set title &lt;L1&gt;</code><br></li>
  2396. </ul>
  2397. </li>
  2398. </ul>
  2399. Die Werte minAll und maxAll (die das Minimum/Maximum aller Werte
  2400. repr&auml;sentieren) sind ebenfals im data hash vorhanden.
  2401. <br>&Uuml;berholt, wird durch das plotReplace Attribut abgel&ouml;st.
  2402. </li><br>
  2403. <li><a href="#nrAxis">nrAxis</a></li><br>
  2404. <a name="plotfunction"></a>
  2405. <li>plotfunction<br>
  2406. Eine Liste, deren Werte durch Leerzeichen voneinander getrennt sind.
  2407. Diese Liste wird verwendet um die &lt;SPEC#&gt; Zeichenfolgen in der
  2408. .gplot-Datei zu ersetzen. Dabei steht das # f&uuml;r eine laufende Ziffer
  2409. beginnend mit 1 (&lt;SPEC1&gt;, &lt;SPEC2&gt;, usw.) in der #FileLog oder
  2410. #DBLog Anweisung. Mit diesem Attrbute ist es m&ouml;glich eine
  2411. .gplot-Datei f&uuml;r mehrere Ger&auml;te mit einem einzigen logdevice zu
  2412. verwenden. <br><br>
  2413. <ul><b>Beispiel:</b><br>
  2414. <li>#FileLog &lt;SPEC1&gt;<br>
  2415. mit:<br>
  2416. <code>attr &lt;SVGdevice&gt; plotfunction "4:IR\x3a:0:"</code><br>
  2417. anstelle von:<br>
  2418. <code>#FileLog 4:IR\x3a:0:</code>
  2419. </li>
  2420. <li>#DbLog &lt;SPEC1&gt;<br>
  2421. mit:<br>
  2422. <code>attr &lt;SVGdevice&gt; plotfunction
  2423. "Garage_Raumtemp:temperature::"</code><br>
  2424. anstelle von:<br>
  2425. <code>#DbLog Garage_Raumtemp:temperature::</code>
  2426. </li>
  2427. </ul>
  2428. &Uuml;berholt, wird durch das plotReplace Attribut abgel&ouml;st.
  2429. </li><br>
  2430. <li><a href="#plotmode">plotmode</a></li><br>
  2431. <a name="plotReplace"></a>
  2432. <li>plotReplace<br>
  2433. Leerzeichen getrennte Liste von Name=Wert Paaren. Wert kann Leerzeichen
  2434. enthalten, falls es in "" oder {} eingeschlossen ist. Wert wird als
  2435. perl-Ausdruck ausgewertet, falls es in {} eingeschlossen ist.
  2436. <br>
  2437. In der .gplot Datei werden &lt;Name&gt; Zeichenketten durch den
  2438. zugehoerigen Wert ersetzt, die Auswertung von {} Ausdr&uuml;cken erfolgt
  2439. <i>nach</i> dem die Daten ausgewertet wurden, d.h. man kann hier
  2440. $data{min1},etc verwenden.
  2441. <br>
  2442. Bei %Name% erfolgt die Ersetzung <i>vor</i> der Datenauswertung, das kann
  2443. man verwenden, um Parameter f&uuml;r die Auswertung zu ersetzen.
  2444. </li><br>
  2445. <li><a href="#plotsize">plotsize</a></li><br>
  2446. <li><a href="#plotWeekStartDay">plotWeekStartDay</a></li><br>
  2447. <a name="startDate"></a>
  2448. <li>startDate<br>
  2449. Setzt das Startdatum f&uuml;r den Plot. Wird f&uuml;r Demo-Installationen
  2450. verwendet.
  2451. </li><br>
  2452. <a name="title"></a>
  2453. <li>title<br>
  2454. Eine besondere Form der &Uuml;berschrift (siehe oben), bei der die
  2455. Zeichenfolge &lt;TL&gt; in der .gplot-Datei ersetzt wird.
  2456. Standardm&auml;&szlig;ig wird als &lt;TL&gt; der Dateiname des Logfiles
  2457. eingesetzt.
  2458. <br>&Uuml;berholt, wird durch das plotReplace Attribut abgel&ouml;st.
  2459. </li><br>
  2460. </ul>
  2461. <br>
  2462. <a name="plotEditor"></a>
  2463. <b>Plot-Editor</b>
  2464. <br>
  2465. Dieser Editor ist in der Detailansicht der SVG-Instanz zu sehen. Die
  2466. meisten Features sind hier einleuchtend und bekannt, es gibt aber auch
  2467. einige Ausnahmen:
  2468. <ul>
  2469. <li>wenn f&uuml;r ein Diagramm die &Uuml;berschrift unterdr&uuml;ckt werden
  2470. soll, muss im Eingabefeld <code>notitle</code> eingetragen werden.
  2471. </li>
  2472. <li>wenn ein fester Wert (nicht aus einer Wertespalte) definiert werden
  2473. soll, f&uuml;r den Fall, das eine Zeichenfoge gefunden wurde (z.B. 1
  2474. f&uuml;r eine FS20 Schalter, der AN ist und 0 f&uuml;r den AUS-Zustand),
  2475. muss zuerst das Tics-Feld gef&uuml;llt, und die .gplot-Datei
  2476. gespeichert werden, bevor der Wert &uuml;ber die Dropdownliste erreichbar
  2477. ist.
  2478. <ul><b>Beispiel:</b><br>
  2479. Eingabe im Tics-Feld: ("On" 1, "Off" 0)<br>
  2480. .gplot-Datei speichern<br>
  2481. "1" als Regexp switch.on und "0" f&uuml;r den Regexp switch.off vom
  2482. Spalten-Dropdown ausw&auml;hlen (auf die G&auml;nsef&uuml;&szlig;chen
  2483. achten!).<br>
  2484. .gplot-Datei erneut speichern<br>
  2485. </ul>
  2486. </li>
  2487. <li>Falls Range der Form {...} entspricht, dann wird sie als Perl -
  2488. Expression ausgewertet. Das Ergebnis muss in der Form [min:max] sein.
  2489. </li>
  2490. </ul>
  2491. Die sichtbarkeit des Plot-Editors kann mit dem FHEMWEB Attribut <a
  2492. href="#ploteditor">ploteditor</a> konfiguriert werden.
  2493. <br>
  2494. </ul>
  2495. =end html_DE
  2496. =cut