SVG Dashboard

by luca
14 comments

PHP and XML

As you learned before, SVG file is a text file in XML format: we can change it easily using PHP SimpleXML library.

Our PHP code will:

  • receive as a parameter (GET, using a URL like ?parameter=value) the actual value to be shown;
  • load the SVG template;
  • change the rectangle’s height property and text value;
  • change their color depending on the value (blue if less than 15, red if more than 25 otherwise green);
  • send the resulting SVG to user’s browser.

Get actual value from the associative array  $_GET:

$value = $_GET["value"];

Load the template file from the same folder of our PHP script: you can get folder’s real path from the __DIR__ constant:

$root = __DIR__;
$svg_template = simplexml_load_file("$root/template.svg");

Get XML nodes with an XPath query which looks for the ids we previously set in Inkscape:

$result = $svg_template->xpath("//*[@id='temp_level']");
if(count($result) == 1) $temp_level = $result[0];
else exit("Unable to find a node with temp_level ID");
 
$result = $svg_template->xpath("//*[@id='temp_value']");
if(count($result) == 1) $temp_value = $result[0];
else exit("Unable to find a node with temp_value ID");

Change the rectangle’s height and text value (as you remember we need to multiply value with 5 to have the connect height because of in our drawing each degree corresponds to 5 units):

$temp_level['height'] = $value * 5;
$temp_value[0] = $value;

Change style property to change the color (blue = #0000ff…):

if($value < 15) {
  $temp_level['style']="fill:#0000ff;stroke:none";
  $temp_value['style'] = "font-size:72px;fill:#0000ff";
} elseif($value < 25) {
  $temp_level['style']="fill:#00ff00;stroke:none";
  $temp_value['style'] = "font-size:72px;fill:#00ff00";
} else {
  $temp_level['style']="fill:#ff0000;stroke:none";
  $temp_value['style'] = "font-size:72px;fill:#ff0000";
}

At last notificate to the browser the correct MIME Type and send the changed XML:

header("Content-type: image/svg+xml");
echo $svg_template->asXML();

In the next page we’re going to learn how to receive data from Pachube using PHP…

Related Posts

14 comments

How to add flair to a webpage that shows your project data - Hack a Day Friday January 27th, 2012 - 07:00 PM

[…] temperature display may not knock your socks off, but it’s a simple demonstration of how you can used vector graphics as a web readout for data (translated). [Luca] wrote this four page tutorial to help others, he makes it look really easy, […]

Reply
How to add flair to a webpage that shows your project data » Geko Geek Friday January 27th, 2012 - 08:30 PM

[…] temperature display may not knock your socks off, but it’s a simple demonstration of how you can used vector graphics as a web readout for data (translated). [Luca] wrote this four page tutorial to help others, he makes it look really easy, […]

Reply
How to add flair to a webpage that shows your project data | ro-Stire Friday January 27th, 2012 - 10:05 PM

[…] temperature display may not knock your socks off, but it’s a simple demonstration of how you can used vector graphics as a web readout for data (translated). [Luca] wrote this four page tutorial to help others, he makes it look really easy, […]

Reply
How to add flair to a webpage that shows your project data | My Blog Saturday January 28th, 2012 - 12:36 AM

[…] heat arrangement might not hit your hosiery off, though it’s a elementary proof of how we can used matrix graphics as a web readout for data (translated). [Luca] wrote this 4 page educational to assistance others, he creates it demeanour […]

Reply
How to add flair to a webpage that shows your project data « Gadgets « The Depot of Talk Saturday January 28th, 2012 - 05:01 AM

[…] temperature display may not knock your socks off, but it’s a simple demonstration of how you can used vector graphics as a web readout for data (translated). [Luca] wrote this four page tutorial to help others, he makes it look really easy, […]

Reply
How to add flair to a webpage that shows your project data « « SNAP-DIYSNAP-DIY Saturday January 28th, 2012 - 09:16 AM

[…] temperature display may not knock your socks off, but it’s a simple demonstration of how you can used vector graphics as a web readout for data (translated). [Luca] wrote this four page tutorial to help others, he makes it look really easy, […]

Reply
How to add flair to a webpage that shows your project data | CisforComputers Sunday January 29th, 2012 - 06:01 AM

[…] temperature display may not knock your socks off, but it’s a simple demonstration of how you can used vector graphics as a web readout for data (translated). [Luca] wrote this four page tutorial to help others, he makes it look really easy, […]

Reply
Tutorial : changer dynamiquement un SVG Sunday January 29th, 2012 - 09:23 AM

[…] XML est facilement modifiable par les langage de scripting web. Tous les détails en anglais et en italien sont sur le blog de lucadentella, les code sources des exemples SVG et PHP sont sur GITHub et enfin […]

Reply
Lucio Saturday June 30th, 2012 - 04:51 PM

Gentile Luca Dentella,
ho trovato interessantissimo il suo tutorial che riguarda il dashboard svg. Le chiederei gentilmente, se possibile, di fornirmi un esempio per visualizzare in forma grafica sul browser i valore di tensione letti sugli ingressi analogici dell’arduino uno in forma vu meter lineare (tipo il suo termometro) oppure classico a lancetta.
Ringraziando anticipatamente, Le porgo distinti saluti.
lucio.ra@libero.it

Reply
A MacLeod Monday May 6th, 2013 - 10:24 PM

Thanks for taking the time to document this, I used it as a basis for my own arduino project (a web-connected heating oil level monitor.)

Getting a nice SVG-based level indication turned out to be easier than I had feared… strangely, getting it to send warning emails was trickier!

http://s230.photobucket.com/user/macleodaj/media/Junk/oil_level_gauge.jpg.html

Reply
luca Tuesday May 7th, 2013 - 08:19 AM

Hi! Thanks for your feedback and great SVG web interface!

Reply
lucadentella.it – Yún – Utilizzare PHP Thursday December 5th, 2013 - 10:33 AM

[…] Dashboard in SVG […]

Reply
Yún – How to use PHP | LinkSprite Learning Center Monday June 27th, 2016 - 04:16 AM

[…] you have already read about some of my projects (dashboard, web charts…), you’ve certainly noticed that I’d like to use the PHP language for developing […]

Reply
Mosesliete Monday April 16th, 2018 - 11:05 PM

Wonderful site, how do u get all this information?I’ve read a few posts on your site and I like your writing style. Thanks a million, keep up the great work.

Reply

Leave a Comment

twenty − 12 =