// Plot US GDP Scatterplot over time var margin = {top: 20, right: 20, bottom: 30, left: 40}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; /* * value accessor - returns the value to encode for a given data object. * scale - maps value to a visual display encoding, such as a pixel position. * map function - maps from data value to display value * axis - sets up axis */ // setup x var xValue5 = function(d) { return d.Year;}, // data -> value xScale5 = d3.scale.linear().range([0, width]), // value -> display xMap5 = function(d) { return xScale5(xValue5(d));}, // data -> display xAxis5 = d3.svg.axis().scale(xScale5).orient("bottom").tickFormat(d3.format("")); // setup y var yValue5 = function(d) { return d.GDPUSDelta;}, // data -> value yScale5 = d3.scale.linear().range([height, 0]), // value -> display yMap5 = function(d) { return yScale5(yValue5(d));}, // data -> display yAxis5 = d3.svg.axis().scale(yScale5).orient("left"); // setup fill color //var cValue4 = function(d) { if ( d.Country == "UnitedStates") { return "#35586C";} // else { return "#8E2323";} // }; // add the graph canvas to the body of the webpage var svg5 = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); // add the tooltip area to the webpage var tooltip = d3.select("body").append("div") .attr("class", "tooltip") .style("opacity", 0); // load data d3.csv("RawCountryData.csv", function(error, data) { // change string (from CSV) into number format data.forEach(function(d) { d.Year = +d.Year; d.GDPUSDelta = +d.GDPUSDelta; // console.log(d); }); // don't want dots overlapping axis, so add in buffer to data domain xScale5.domain([d3.min(data, xValue5)-1, d3.max(data, xValue5)+1]); yScale5.domain([d3.min(data, yValue5)-1, d3.max(data, yValue5)+1]); // x-axis svg5.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis5) .append("text") .attr("class", "label") .attr("x", width) .attr("y", -6) .style("text-anchor", "end") .text("Year"); // y-axis svg5.append("g") .attr("class", "y axis") .call(yAxis5) .append("text") .attr("class", "label") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("GDP % Chg Delta"); //Add Title svg5.append("text") .attr("x", (width / 2)) .attr("y", 0 - (margin.top / 6)) .attr("text-anchor", "middle") .style("font-size", "16px") .style("text-decoration", "underline") .text("UK GDP % Chg Relative to US"); // draw dots svg5.selectAll(".dot") .data(data) .enter().append("circle") .filter(function(d) {return (d.Country == "UK")}) .attr("class", "dot") .attr("r", 5) .attr("cx", xMap5) .attr("cy", yMap5) .style("fill", "#8E2323") .on("mouseover", function(d) { tooltip.transition() .duration(200) .style("opacity", .9); tooltip.html(d["Country"] + "
(" + xValue5(d) + ", " + yValue5(d) + ")") .style("left", (d3.event.pageX + 5) + "px") .style("top", (d3.event.pageY - 12) + "px"); }) .on("mouseout", function(d) { tooltip.transition() .duration(500) .style("opacity", 0); }); });