// 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 xValue8 = function(d) { return d.GDPUSDelta;}, // data -> value xScale8 = d3.scale.linear().range([0, width]), // value -> display xMap8 = function(d) { return xScale8(xValue8(d));}, // data -> display xAxis8 = d3.svg.axis().scale(xScale8).orient("bottom"); // setup y var yValue8 = function(d) { return d.ExChg;}, // data -> value yScale8 = d3.scale.linear().range([height, 0]), // value -> display yMap8 = function(d) { return yScale8(yValue8(d));}, // data -> display yAxis8 = d3.svg.axis().scale(yScale8).orient("left"); // setup fill color var cValue8 = function(d) { if ( d.Region == "Americas") { return "#35586C";} else { if ( d.Region == "Europe") { return "#8E2323";} else { if ( d.Region == "EasternEurope") { return "#5C5C5C";} else { if ( d.Region == "Asia") { return "#FF8600";} else { if ( d.Region == "SouthAsia") { return "#397D02";} else { if ( d.Region == "Oceania") { return "#388E8E";} else { return "#5E2D79";} } } } } } }; // add the graph canvas to the body of the webpage var svg8 = 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.ExChg = +d.ExChg; d.GDPUSDelta = +d.GDPUSDelta; // console.log(d); }); // don't want dots overlapping axis, so add in buffer to data domain xScale8.domain([d3.min(data, xValue8)-1, d3.max(data, xValue8)+1]); yScale8.domain([d3.min(data, yValue8)-1, d3.max(data, yValue8)+1]); // x-axis svg8.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis8) .append("text") .attr("class", "label") .attr("x", width) .attr("y", -6) .style("text-anchor", "end") .text("GDP Change Relative to US"); // y-axis svg8.append("g") .attr("class", "y axis") .call(yAxis8) .append("text") .attr("class", "label") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("USD FX Rate % Chg"); //Add Title svg8.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("FX Rate % Chg to GDP % Change"); // draw dots svg8.selectAll(".dot") .data(data) .enter().append("circle") .filter(function(d) {return (d.Country != "UnitedStates")}) .attr("class", "dot") .attr("r", 5) .attr("cx", xMap8) .attr("cy", yMap8) .style("fill", cValue8) .on("mouseover", function(d) { tooltip.transition() .duration(200) .style("opacity", .9); tooltip.html(d["Country"] + "
(" + xValue8(d) + ", " + yValue8(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); }); var regions = ["Americas","Europe", "EasternEurope","Asia","SouthAsia","Oceania", "Africa"] // draw legend var legend = svg8.selectAll(".legend") .data(regions) .enter().append("g") .attr("class", "legend") .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); // draw legend colored rectangles legend.append("rect") .attr("x", width - 18) .attr("y", 40) .attr("width", 18) .attr("height", 18) .style("fill", function(d) { if ( d == "Americas") { return "#35586C";} else { if ( d == "Europe") { return "#8E2323";} else { if ( d == "EasternEurope") { return "#5C5C5C";} else { if ( d == "Asia") { return "#FF8600";} else { if ( d == "SouthAsia") { return "#397D02";} else { if ( d == "Oceania") { return "#388E8E";} else { return "#5E2D79";} } } } } } } ); // draw legend text legend.append("text") .attr("x", width - 24) .attr("y", 49) .attr("dy", ".35em") .style("text-anchor", "end") .text(function(d) { return d;}) });