Created
December 12, 2014 19:33
-
-
Save canoedf/1c5741997ea5465f4b6e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var postdata; //used to get the data wanted from d3.xhr() below | |
function testResults (form) { | |
var TestVar1 = form.value1.value; | |
var TestVar2 = form.value2.value; | |
var TestVar3 = form.value3.value; | |
// var TestVar4 = form.value4.value; | |
postdata = "data1=" + TestVar1 + "&data2=" + TestVar2 + "&data3=" + TestVar3; | |
// Set the dimensions of the canvas/graph - made it smaller for testing | |
var margin = {top: 15, right: 20, bottom: 50, left: 50}, | |
//width = 900 - margin.left - margin.right, | |
//height = 400 - margin.top - margin.bottom; | |
width = 300 - margin.left - margin.right, | |
height = 135 - margin.top - margin.bottom; | |
// Parse the date / time | |
var parseDate = d3.time.format("%Y-%m-%d %H:%M:%S").parse; | |
// Set the ranges | |
var x = d3.time.scale().range([0, width]); | |
var y = d3.scale.linear().range([height, 0]); | |
// Define the axes | |
var xAxis = d3.svg.axis().scale(x).orient("bottom").ticks(5); | |
var yAxis = d3.svg.axis().scale(y).orient("left").ticks(5); | |
// Define the line | |
var valueline = d3.svg.line().x(function(d) { return x(d.tm); }).y(function(d) { return y(d.metric); }); | |
// Adds the svg canvas | |
var svg = d3.select("#the_SVG_ID").remove(); | |
svg = d3.select("body") | |
.append("svg") | |
.attr("id","the_SVG_ID") //give the svg a name so we can remove it before updating | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", | |
"translate(" + margin.left + "," + margin.top + ")"); | |
// Get the data - set header and pass parameters - receive and parse for display | |
d3.xhr("testA.php") | |
.header("Content-type", "application/x-www-form-urlencoded") | |
.post(postdata,function(error, rawData){ | |
var raw = rawData.responseText.replace(/(^\d+)/,""); | |
//alert(raw); //diagnostic - remove remark to see the clean data returned from database | |
var data = JSON.parse(raw); | |
data.forEach(function(d) {d.tm = parseDate(d.tm);d.metric = +d.metric;}); | |
// Scale the range of the data | |
x.domain(d3.extent(data, function(d) { return d.tm; })); | |
y.domain([0, d3.max(data, function(d) { return d.metric; })]); | |
// Add the valueline path. | |
svg.append("path").attr("class", "line").attr("d", valueline(data)); | |
// Add the X Axis | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis) | |
.selectAll("text") | |
.style("text-anchor", "end") | |
.attr("dx", "-.75em") | |
.attr("dy", ".12em") | |
.attr("transform", function(d) {return "rotate(-90)" }); | |
// Add the Y Axis | |
svg.append("g").attr("class", "y axis").call(yAxis); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment