98_SVG.pm 91 KB

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