Example of a scatter plot with brush using d3.js v4.
Tutorial at http://www.rajvansia.com/scatterplotbrush-d3-v4.html
Adapted from https://bl.ocks.org/mbostock/34f08d5e11952a80609169b7917d4172
xxxxxxxxxx
<!-- adapted from mbostock v4 area brush https://bl.ocks.org/mbostock/34f08d5e11952a80609169b7917d4172-->
<meta charset="utf-8">
<body>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
// Commented version of
// https://bl.ocks.org/rajvansia/ce6903fad978d20773c41ee34bf6735c
// Variables
// Height - focus
// Height2 - context
var margin = {top: 20, right: 20, bottom: 110, left: 50},
margin2 = {top: 430, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom,
height2 = 500 - margin2.top - margin2.bottom;
// Date Parser takes in Date string and returns JS Data Object
var parseDate = d3.timeParse("%b %Y");
// Chart Scales
// x, y -> Focus
// x2, y2 -> Context
var x = d3.scaleTime().range([0, width]),
x2 = d3.scaleTime().range([0, width]),
y = d3.scaleLinear().range([height, 0]),
y2 = d3.scaleLinear().range([height2, 0]);
// Chart Axis
var xAxis = d3.axisBottom(x),
xAxis2 = d3.axisBottom(x2),
yAxis = d3.axisLeft(y);
// Define Brush, extent, and what function to run on "brush"
var brush = d3.brushX()
.extent([[0, 0], [width, height2]])
.on("brush", brushed);
// Define inner drawing space with D3 Marging Convention
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
// Define clippath
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
// Define focus "g" and where it will live
var focus = svg.append("g")
.attr("class", "focus")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Define context "g" and where it will live
var context = svg.append("g")
.attr("class", "context")
.attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");
// CSV AJAX call to get data
// Run type function to pre-process ingested data
// Call-back function to process data once parsed and pre-processed
d3.csv("sp500.csv", type, function(error, data) {
if (error) throw error;
// Update scale domains, now that we have the data
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.price; })+200]);
x2.domain(x.domain());
y2.domain(y.domain());
// Append scatter plot to Focus chart area
// Make sure to clip the path
// Do a data join
var dots = focus.append("g");
dots.attr("clip-path", "url(#clip)");
dots.selectAll("dot")
.data(data)
.enter().append("circle")
.attr('class', 'dot')
.attr("r",5)
.style("opacity", .5)
.attr("cx", function(d) { return x(d.date); })
.attr("cy", function(d) { return y(d.price); })
// Create the Focus X Axis
focus.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Create the Focus Y Axis
focus.append("g")
.attr("class", "axis axis--y")
.call(yAxis);
// Create the Focus Y Axis Text Label
focus.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left)
.attr("x",0 - (height / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.text("Price");
// Create the Context X Axis Text Label
svg.append("text")
.attr("transform",
"translate(" + ((width + margin.right + margin.left)/2) + " ," +
(height + margin.top + margin.bottom) + ")")
.style("text-anchor", "middle")
.text("Date");
// append scatter plot to brush chart area
var dots = context.append("g");
// Attach the clip-path to the dots in the Focus chart
dots.attr("clip-path", "url(#clip)");
// Create the dots through the data join in the context chart
dots.selectAll("dot")
.data(data)
.enter().append("circle")
.attr('class', 'dotContext')
.attr("r",3)
.style("opacity", .5)
.attr("cx", function(d) { return x2(d.date); })
.attr("cy", function(d) { return y2(d.price); })
// Create the Context X Axis
context.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height2 + ")")
.call(xAxis2);
// ** Note that this example doesn't show a Context Y Axis
// Add the brush functionality to the context graph
// Initialize the initial brush to cover the full context graph rage
context.append("g")
.attr("class", "brush")
.call(brush)
.call(brush.move, x.range());
});
//create brush function redraw scatterplot with selection
function brushed() {
var selection = d3.event.selection;
x.domain(selection.map(x2.invert, x2));
focus.selectAll(".dot")
.attr("cx", function(d) { return x(d.date); })
.attr("cy", function(d) { return y(d.price); });
focus.select(".axis--x").call(xAxis);
}
// Function to process the data when it gets ingested by the AJAX call
function type(d) {
d.date = parseDate(d.date);
d.price = +d.price;
return d;
}
</script>
https://d3js.org/d3.v4.min.js