27.10.2015 Views

AJAX and PHP

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>AJAX</strong> Real-Time Charting with SVG<br />

<br />

<br />

<br />

<br />

<br />

0.0<br />

...<br />

... more text nodes here that draw horizontal <strong>and</strong> vertical unit numbers<br />

...<br />

<br />

<br />

<br />

<br />

<br />

...<br />

... more circle nodes here that draw filled blue circles for chart nodes<br />

...<br />

<br />

<br />

Have a closer look at this code snippet to identify all the chart elements. The SVG format supports<br />

the notion of element groups, which are elements grouped under a element. In temp.svg we<br />

have two groups: the first group contains all the charts' elements, translating them by (50, 50)<br />

pixels, while the second element group is a child of the first group, <strong>and</strong> it contains the chart's<br />

axis lines <strong>and</strong> numbers.<br />

SVG knows how to h<strong>and</strong>le many element types, which can also be animated (yes, SVG is very<br />

powerful). In our example, we make use of some of the very basic ones: path (to draw the axis<br />

lines <strong>and</strong> chart lines), text (to draw the axis numbers, <strong>and</strong> to dynamically display chart node<br />

coordinates when the mouse cursor hovers over them—this latter feature isn't included in the code<br />

snippet), <strong>and</strong> circle (to draw the blue dots on the chart that represent the chart nodes).<br />

You can find documentation for these elements at:<br />

• http://www.w3schools.com/svg/svg_path.asp<br />

• http://www.w3schools.com/svg/svg_circle.asp<br />

• http://www.w3schools.com/svg/svg_text.asp<br />

The paths are described by a path definition. The complete code for the path element that draws<br />

the chart lines you can see in Figure 7.1 looks like this:<br />

<br />

<br />

A detail that was stripped from the code snippet was the mouseover <strong>and</strong> mouseout events of the<br />

chart node circles. In our code, the mouseover event (which fires when you move the mouse<br />

pointer over a node) will call a JavaScript function that displays a text above the node specifying<br />

its coordinates. The mouseout event makes that text disappear. You can see this feature in action<br />

in Figure 7.2, which displays the SVG chart application in action.<br />

192<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!