tianhe-2prog
Goto Top

Feldwert aus einer externen XML Datei mit PHP in ein SVG Diagramm übertragen

Guten Tag,

ich möchte aus einer XML Datei ein bestimmtes Feld auslesen und nur einen Wert
in ein SVG Diagramm übertragen.

Ich habe mal Testweise die XML Datei von http://www.pegelonline.wsv.de/svgz/pegelstaende_neu.xml
genommen.

Hier mal mein PHP Code dazu Wert '223' wird ausgegeben:

<?php
$out = '';  
function _PRS($str) {
   return(iconv("UTF-8","ISO-8859-1//TRANSLIT",$str));  
}
try {
   $xref = simplexml_load_file('http://www.pegelonline.wsv.de/svgz/pegelstaende_neu.xml');  
   foreach ($xref->table->gewaesser as $value) {
      if (_PRS($value->name) == 'ELBE') {  
         foreach ($value->item as $items) {
            if (_PRS($items->pegelname) == 'DRESDEN') {  

                               $out .= ""._PRS($items->messwert)."";  

                        

               break;
            }
         }
         break;      
      }
   }
   if ($out == '') {  
     $out = "No Data";  
   }
} catch (Exception $e) {
   $out = "No Data";  
}
echo($out);


?> 



Nun möchte ich diesen wert in dieses SVG Balkendiagramm übertragen:


<svg width="650" height="500">  
  <g id="axis" transform="translate(0 500) scale(1 -1)">  
    <line id="axis-y" x1="30" y1="20" x2="30" y2="450" style="fill:none;stroke:rgb(0,0,0);stroke-width:2"/>  
    <line id="axis-x" x1="30" y1="20" x2="460" y2="20" style="fill:none;stroke:rgb(0,0,0);stroke-width:2"/>  
  </g>
  <g id="bars" transform="translate(30 479) scale(1 -430)">  
    <rect x="30" y="0" width="50" height=$out .= ""._PRS($items->messwert).""; style="fill:rgb(255,0,0);stroke:rgb(0,0,0);stroke-width:0"/>  
    <rect x="100" y="0" width="50" height="HIER SOLL DIE 223 EINGEFÜGT WERDEN" style="fill:rgb(0,255,0);stroke:rgb(0,0,0);stroke-width:0"/>  
    <rect x="170" y="0" width="50" height="0.75" style="fill:rgb(255,255,0);stroke:rgb(0,0,0);stroke-width:0"/>  
    <rect x="240" y="0" width="50" height="0.9" style="fill:rgb(0,255,255);stroke:rgb(0,0,0);stroke-width:0"/>  
    <rect x="310" y="0" width="50" height="1" style="fill:rgb(0,0,255);stroke:rgb(0,0,0);stroke-width:0"/>  
  </g>
  <g id="scale" transform="translate(29 60)">  
    <text id="scale1" x="0px" y="320px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">0.25</text>  
    <text id="scale2" x="0px" y="215px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">0.50</text>  
    <text id="scale3" x="0px" y="107.5px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">0.75</text>  
    <text id="scale4" x="0px" y="0px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">1.00</text>  
  </g>
  <g id="key">  
    <rect id="key1" x="430" y="80" width="25" height="15" style="fill:rgb(255,0,0);stroke:rgb(0,0,0);stroke-width:1"/>  
    <rect id="key2" x="430" y="100" width="25" height="15" style="fill:rgb(0,255,0);stroke:rgb(0,0,0);stroke-width:1"/>  
    <rect id="key3" x="430" y="120" width="25" height="15" style="fill:rgb(255,255,0);stroke:rgb(0,0,0);stroke-width:1"/>  
    <rect id="key5" x="430" y="140" width="25" height="15" style="fill:rgb(0,255,255);stroke:rgb(0,0,0);stroke-width:1"/>  
    <rect id="key4" x="430" y="160" width="25" height="15" style="fill:rgb(0,0,255);stroke:rgb(0,0,0);stroke-width:1"/>  
    <text id="key1-text" x="465px" y="92px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">schlüssel1</text>  
    <text id="key2-text" x="465px" y="112px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">schlüssel2</text>  
    <text id="key3-text" x="465px" y="132px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">schlüssel3</text>  
    <text id="key4-text" x="465px" y="152px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">schlüssel4</text>  
    <text id="key5-text" x="465px" y="172px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">schlüssel5</text>  
  </g>
  <g id="title">  
    <text x="325px" y="20px" style="text-anchor:middle;fill:rgb(0,0,0);font-size:24;font-family:Arial">Überschrift</text>  
  </g>
</svg>

Wie könnte ich das umsetzen?

Vielen Dank

Content-Key: 356619

Url: https://administrator.de/contentid/356619

Printed on: April 18, 2024 at 10:04 o'clock

Mitglied: 134464
134464 Nov 30, 2017 updated at 14:54:47 (UTC)
Goto Top
HIER SOLL DIE 223 EINGEFÜGT WERDEN
Und warum fügst du dann dort nicht die Variable $out ein??
Member: Tianhe-2prog
Tianhe-2prog Nov 30, 2017 at 15:36:39 (UTC)
Goto Top
Hi,

danke für die Antwort. Klappt leider nicht. Wenn ich den Wert 225 händisch eintrage, ist der grüne Balken ganz oben. Wenn ich dafür
$out eintrage, sieht man den grünen Balken gar nicht. Mit und ohne Anführungszeichen. Das ist scheinbar so noch nicht richtig -.-

         foreach ($value->item as $items) {
            if (_PRS($items->pegelname) == 'DRESDEN') {  

                               $out .= ""._PRS($items->messwert)."";  

                        

               break;
            }
         }
         break;      
      }
   }
   if ($out == '') {  
     $out = "No Data";  
   }
} catch (Exception $e) {
   $out = "No Data";  
}
echo($out);


?> 


<svg width="650" height="500">  
  <g id="axis" transform="translate(0 500) scale(1 -1)">  
    <line id="axis-y" x1="30" y1="20" x2="30" y2="450" style="fill:none;stroke:rgb(0,0,0);stroke-width:2"/>  
    <line id="axis-x" x1="30" y1="20" x2="460" y2="20" style="fill:none;stroke:rgb(0,0,0);stroke-width:2"/>  
  </g>
  <g id="bars" transform="translate(30 479) scale(1 -430)">  
    <rect x="100" y="0" width="50" height="$out"  style="fill:rgb(0,255,0);stroke:rgb(0,0,0);stroke-width:0"/>  
    <rect x="170" y="0" width="50" height="0.75" style="fill:rgb(255,255,0);stroke:rgb(0,0,0);stroke-width:0"/>  
    <rect x="240" y="0" width="50" height="0.9" style="fill:rgb(0,255,255);stroke:rgb(0,0,0);stroke-width:0"/>  
    <rect x="310" y="0" width="50" height="1" style="fill:rgb(0,0,255);stroke:rgb(0,0,0);stroke-width:0"/>  
  </g>
  <g id="scale" transform="translate(29 60)">  
    <text id="scale1" x="0px" y="320px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">0.25</text>  
    <text id="scale2" x="0px" y="215px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">0.50</text>  
    <text id="scale3" x="0px" y="107.5px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">0.75</text>  
    <text id="scale4" x="0px" y="0px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">1.00</text>  
  </g>
  <g id="key">  
    <rect id="key1" x="430" y="80" width="25" height="15" style="fill:rgb(255,0,0);stroke:rgb(0,0,0);stroke-width:1"/>  
    <rect id="key2" x="430" y="100" width="25" height="15" style="fill:rgb(0,255,0);stroke:rgb(0,0,0);stroke-width:1"/>  
    <rect id="key3" x="430" y="120" width="25" height="15" style="fill:rgb(255,255,0);stroke:rgb(0,0,0);stroke-width:1"/>  
    <rect id="key5" x="430" y="140" width="25" height="15" style="fill:rgb(0,255,255);stroke:rgb(0,0,0);stroke-width:1"/>  
    <rect id="key4" x="430" y="160" width="25" height="15" style="fill:rgb(0,0,255);stroke:rgb(0,0,0);stroke-width:1"/>  
    <text id="key1-text" x="465px" y="92px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">schlüssel1</text>  
    <text id="key2-text" x="465px" y="112px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">schlüssel2</text>  
    <text id="key3-text" x="465px" y="132px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">schlüssel3</text>  
    <text id="key4-text" x="465px" y="152px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">schlüssel4</text>  
    <text id="key5-text" x="465px" y="172px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">schlüssel5</text>  
  </g>
  <g id="title">  
    <text x="325px" y="20px" style="text-anchor:middle;fill:rgb(0,0,0);font-size:24;font-family:Arial">Überschrift</text>  
  </g>

$out
</svg>
diangramm
Mitglied: 134464
Solution 134464 Nov 30, 2017 updated at 15:48:40 (UTC)
Goto Top
Naja das muss natürlich so aussehen:
height="<?php echo $out;?>"  
Da du ja nicht mehr im PHP Kontext sondern im HTML-Kontext bist face-wink, deswegen musst du dort erst wieder rein wechseln.
Member: Tianhe-2prog
Tianhe-2prog Nov 30, 2017 at 16:03:33 (UTC)
Goto Top
Hey Specht,

TOP! Danke. Genau das hat mir gefehlt! Vielen DANK!!!