27.10.2015 Views

AJAX and PHP

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

// called when mouse hovers over chart node to display its coordinates<br />

function createPointInfo(x, y, whereX, whereY)<br />

{<br />

// make sure you don't display more coordinates at the same time<br />

if (currentNodeInfo) removePointInfo();<br />

// create text node<br />

currentNodeInfo = documentSVG.createElementNS(svgNS, "text");<br />

currentNodeInfo.appendChild(documentSVG.createTextNode("("+x+","+y+")"));<br />

// set coordinates<br />

currentNodeInfo.setAttribute("x", whereX.toFixed(1));<br />

currentNodeInfo.setAttribute("y", whereY - 10);<br />

// add the node to the group<br />

chartGroup.appendChild(currentNodeInfo);<br />

}<br />

// removes the text node that displays chart node coordinates<br />

function removePointInfo()<br />

{<br />

chartGroup.removeChild(currentNodeInfo);<br />

currentNodeInfo = null;<br />

}<br />

// draws a new point on the graph<br />

function addPoint(X, Y)<br />

{<br />

// save these values for future reference<br />

lastX = X;<br />

lastY = Y;<br />

// start over (reload page) after the last value was generated<br />

if (X == xt2)<br />

window.location.reload(false);<br />

// calculate the coordinates of the new node<br />

coordX = (X - xt1) * (width / (xt2 - xt1));<br />

coordY = height - (Y - yt1) * (height / (yt2 - yt1));<br />

// draw the node on the chart as a blue filled circle<br />

var circle = documentSVG.createElementNS(svgNS, "circle");<br />

circle.setAttribute("cx", coordX); // X position<br />

circle.setAttribute("cy", coordY); // Y position<br />

circle.setAttribute("r", 3); // radius<br />

circle.setAttribute("fill", "blue"); // color<br />

circle.setAttribute("onmouseover",<br />

"createPointInfo(" + X + "," +<br />

Y + "," + coordX + "," + coordY + ")");<br />

circle.setAttribute("onmouseout", "removePointInfo()");<br />

chartGroup.appendChild(circle);<br />

// add a new line to the new node on the graph<br />

current = dataPath.getAttribute("d"); // current path definition<br />

// update path definition<br />

if (!current || current == "")<br />

dataPath.setAttribute("d", " M " + coordX + " " + coordY);<br />

else<br />

dataPath.setAttribute("d", current + " L " + coordX + " " + coordY);<br />

}<br />

// initiates asynchronous request to retrieve new chart data<br />

function updateChart()<br />

{<br />

// builds the query string<br />

param = "?lastX=" + lastX + ((lastY != -1) ? "&lastY=" + lastY : "");<br />

// make the request through either <strong>AJAX</strong><br />

if (window.getURL)<br />

// Supported by Adobe's SVG Viewer <strong>and</strong> Apache Batik<br />

getURL("svg_chart.php" + param, h<strong>and</strong>leResults);<br />

else<br />

198<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!