98_SVG.pm 87 KB

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