d3 scatterplot example using the Breakfast Cereal Dataset from http://idvbook.com and based on the scatterplot example at /mbostock/3887118
forked from weiglemc's block: D3 Scatterplot Example
xxxxxxxxxx
<html>
<meta charset="utf-8">
<!-- Example based on https://bl.ocks.org/mbostock/3887118 -->
<!-- Tooltip example from https://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html -->
<style>
body {
font: 16px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #fff;
shape-rendering: crispEdges;
}
.dot {
stroke: #000;
}
.tooltip {
position: absolute;
padding: 5px 5px;
pointer-events: none;
background-color: white;
border: 1px solid black;
}
form {
position: absolute;
right: 10px;
top: 720px;
}
label {
font-size: 20px;
text-transform: uppercase;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
<!-- Technology radio buttons -->
<form>
<label><input type="radio" name="mode" value="Onshore" checked> Onshore Wind</label>
<label><input type="radio" name="mode" value="Offshore"> Offshore Wind</label>
<label><input type="radio" name="mode" value="SolarPV"> Solar PV</label>
</form>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 150},
width = 960 - margin.left - margin.right,
height = 700 - margin.top - margin.bottom;
var nest = d3.nest()
.key(function (d) { return d.Technology })
.key(function (d) { return d.Region })
.key(function (d) { return d.Period });
/*
* 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 xValue = function(d) { return d.x;}, // data -> value
xScale = d3.scale.linear().range([0, width]), // value -> display
xMap = function(d) { return xScale(xValue(d));}, // data -> display
xAxis = d3.svg.axis().scale(xScale).orient("bottom");
// setup y
var yValue = function(d) { return d.Investor_Type; }, // data -> value
yScale = d3.scale.ordinal()
// .domain(["Utilities","Developers","FIs","Large Consumers","Small Consumers","Government"])
.rangeRoundBands([0,height+100],1), // value -> display
yMap = function(d) { return yScale(yValue(d)); }, // data -> display
yAxis = d3.svg.axis().scale(yScale).orient("left");
// setup fill color
var cValue = function(d) { return d.Score;},
color = d3.scale.pow().exponent(2).domain([1,5]).range(["#fff111","blue"]);
// add the graph canvas to the body of the webpage
var svg = 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", .5);
// load data
d3.csv("160208.csv", function(error, data) {
yScale.domain(data.map(function(d) { return d.Investor_Type; }));
// change string (from CSV) into number format
data.forEach(function(d) {
d.Value = +d.Value;
d.Score = +d.Score;
d.y = +d.y;
d.x = +d.x;
// console.log(d);
});
// don't want dots overlapping axis, so add in buffer to data domain
xScale.domain([0, 29]);
// x-axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("");
// y-axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("y", 5)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Investor Class");
// draw dots
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", function(d) {return d.Value/500;})
.attr("cx", xMap)
.attr("cy", yMap)
.style("fill", function(d) { return color(cValue(d));})
.on("mouseover", function(d) {
tooltip.transition()
.duration(200)
.style("opacity", 1);
tooltip.html("Technology: " + d["Technology"] + "<br/>" + "Region: " + d["Region"]
+ "<br/>" + "Investor: " + d["Investor_Type"] + "<br/>" + "Capacity: " + d.Value + "MW" + "<br/>" + "Market Rating: " + d.Score)
.style("left", (d3.event.pageX + 5) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d) {
tooltip.transition()
.duration(500)
.style("opacity", 0);
});
// draw legend
var legend = svg.selectAll(".legend")
.data(color.domain())
.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("width", 18)
.attr("height", 18)
.style("fill", color);
// draw legend text
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d;})
d3.selectAll("input").on("change", change);
var timeout = setTimeout(function() {
d3.select("input[value=\"Onshore\"]").property("checked", true).each(change);
}, 2000);
function change() {
clearTimeout(timeout);
if (this.value === "Onshore") transitionOffshore();
else if (this.value === "Offshore") transitionSolar();
else transitionOnshore();
}
function transitionOffshore() {
var t = svg.transition().duration(750),
g = t.selectAll(".group").attr("transform", function(d) { return "translate(0," + y0(d.key) + ")"; });
g.selectAll("circle").attr("x", function(d) { return y1(d.value); });
g.select(".dot").attr("x", function(d) { return y1(d.values[0].value / 2); })
}
function transitionSolar() {
var t = svg.transition().duration(750),
g = t.selectAll(".group").attr("transform", "translate(0," + y0(y0.domain()[0]) + ")");
g.selectAll("circle").attr("y", function(d) { return y1(d.value + d.valueOffset); });
g.select(".dot").attr("y", function(d) { return y1(d.values[0].value / 2 + d.values[0].valueOffset); })
}
function transitionOnshore(){
var t = svg.transition().duration(750),
g = t.selectAll(".dot").attr("r", function(d) { return d.Value/400 });
}
});
</script>
</body>
</html>
https://d3js.org/d3.v3.min.js