XML technologies / XSLT / XSLT Cookbook / XML converted into SVG: conversion

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

“Pictures can be found everywhere. What matters is noticing and arranging things.”
Elliott Erwitt

Introduction

SVG (Scalable Vector Graphics) is an XML-encoded vector graphics format that may revolutionize graphical content delivery on the Internet. A compelling reason to encode graphics as XML is that transformations can render data graphically. Thus XSLT, without built-in graphics, can produce complex images by letting a browser’s SVG engine do most of the work.

Some SVG familiarity is assumed, but this introduction presents frequently used basic techniques.

One of the first things to know is the graphics system’s coordinate system orientation. After algebra, trigonometry and calculus, technical readers usually find Cartesian coordinates natural: X increases rightward, Y upward. SVG instead reverses Y, placing origin 0,0 at the top left and increasing Y downward. Coordinates do not matter for many applications, but Cartesian orientation is more intuitive for data displays. SVG transforms coordinate systems through translation, rotation and scaling of individual shapes or entire groups. This transformation gives Cartesian coordinates:

<svg:g transform="translate(0,{$height}) scale(1,-1)">
  <!-- Alle Inhalte in kartesischen Koordinaten -->
</svg:g>

Here, $height is the height of the whole SVG graphic or the maximum Y coordinate in the group.

When plotting data, translate and scale coordinates to the data range, so data values can be used directly as coordinates:

<svg:g transform="scale(1,{$height div $max})">
  <!-- Alle Inhalte in kartesischen Koordinaten -->
</svg:g>

Here Y is scaled according to $height, the SVG height, and $max, the maximum plotted value.

Cartesian conversion and scaling simplify plotting, but text becomes upside down and distorted. Apply a compensating transformation to text:

<svg:text x="{$someXPos}" y="{$someYPos}" transform="translate({$someXPos},{$someYPos}) scale(1,{-$max div $height}) translate({-$someXPos},{-$someYPos})">Some Text</svg:text>

These transformations make me dizzy, but sometimes are the easiest route to the desired result.

  

  

 

 

 

back to top