var margin10 = {top: 20, right: 20, bottom: 30, left: 40}, width10 = 960 - margin.left - margin.right, height10 = 500 - margin.top - margin.bottom; var x10 = d3.scale.ordinal() .rangeRoundBands([0, width10], .1); var y10 = d3.scale.linear() .range([height10, 0], 0.1); var xAxis10 = d3.svg.axis() .scale(x10) .orient("bottom"); var yAxis10 = d3.svg.axis() .scale(y10) .orient("left") .tickSize(0) .tickPadding(10); var cValue10 = function(d) { if ( d.SubClass == "Americas") { return "#35586C";} else { if ( d.SubClass == "Europe") { return "#8E2323";} else { if ( d.SubClass == "EasternEurope") { return "#5C5C5C";} else { if ( d.SubClass == "Asia") { return "#FF8600";} else { if ( d.SubClass == "SouthAsia") { return "#397D02";} else { if ( d.SubClass == "Oceania") { return "#388E8E";} else { return "#5E2D79";} } } } } } }; var tooltip = d3.select("body").append("div") .attr("class", "tooltip") .style("opacity", 0); var svg10 = d3.select("body").append("svg") .attr("width", width10 + margin10.left + margin10.right) .attr("height", height10 + margin10.top + margin10.bottom) .append("g") .attr("transform", "translate(" + margin10.left + "," + margin10.top + ")"); d3.csv("Correlations2.csv", type, function(error, data) { x10.domain(data.map(function(d) { if (d.Type == "Country") {return d.Class;} })); //y9.domain(d3.extent(data, function(d) { return d.Corr; })).nice(); y10.domain([-.8,.8]); svg10.selectAll(".bar") .data(data) .enter().append("rect") .filter(function(d) {return (d.Type == "Country")}) // .attr("class", function(d) { return "bar bar--" + (d.Corr < 0 ? "negative" : "positive"); }) .style("fill", cValue10) .attr("x", function(d) { return x10(d.Class); }) .attr("y", function(d) { return y10(Math.max(0, d.Corr)); }) .attr("width", x10.rangeBand()) .attr("height", function(d) { return Math.abs(y10(d.Corr) - y10(0)); }) .on("mouseover", function(d) { tooltip.transition() .duration(200) .style("opacity", .9); tooltip.html(d["Class"] + "
(" + d.Corr + ")") .style("left", (d3.event.pageX -5) + "px") .style("top", (d3.event.pageY + 50) + "px"); }) .on("mouseout", function(d) { tooltip.transition() .duration(500) .style("opacity", 0); }); svg10.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height10 + ")") .call(xAxis10) .append("text") .attr("class", "label") .attr("x", width10) .attr("y", -10) .style("text-anchor", "end") .text("Region"); //.text("Region"); svg10.append("g") .attr("class", "y axis") .attr("transform", "translate(" + 0 + ",0)") .call(yAxis10) .append("text") .attr("class", "label") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("Corr FX Rate and GDP Change");; //Add Title svg10.append("text") .attr("x", (width10 / 2)) .attr("y", 0 - (margin10.top / 6)) .attr("text-anchor", "middle") .style("font-size", "16px") .style("text-decoration", "underline") .text("Country Correlation"); var regions = ["Americas","Europe", "EasternEurope","Asia","SouthAsia","Oceania", "Africa"] // draw legend var legend = svg10.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", width10 - 18) .attr("y", 12) .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", width10 - 24) .attr("y", 20) .attr("dy", ".35em") .style("text-anchor", "end") .text(function(d) { return d;}) }); function type(d) { d.value = +d.Corr; return d; }