score:1
For context, this is from some code that implements chart brushing:
function brushed() {
if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; // ignore brush-by-zoom
var s = d3.event.selection || x2.range();
x.domain(s.map(x2.invert, x2));
focus.select(".area").attr("d", area);
focus.select(".axis--x").call(xAxis);
svg.select(".zoom").call(zoom.transform, d3.zoomIdentity
.scale(width / (s[1] - s[0]))
.translate(-s[0], 0));
}
We already have x
and x2
initialised as two time scales:
var x = d3.scaleTime().range([0, width]),
x2 = d3.scaleTime().range([0, width])
and s
is initialised as
var s = d3.event.selection || x2.range();
(where the d3.event
is a brushing event)
The line
x.domain(s.map(x2.invert, x2));
sets the x
scale domain by running x2.invert
on each item in array s
with x2
as the this
value. In practice, this means you are running
x.domain( x2.invert( s[0] ), x2.invert( s[1] ) );
as there are only two values in s
and the this
context does not affect the invert
function. In terms of the visualisation, this is setting the time span covered by the large chart by converting the pixel values of the edges of the selection box in the bottom chart into dates on the large chart.
To briefly summarise the whole function:
function brushed() {
if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; // ignore brush-by-zoom
// get the edges of the selection box or use the maximum values (in x2.range)
var s = d3.event.selection || x2.range();
// convert those pixel values into dates, and set the x scale domain to those values
x.domain(s.map(x2.invert, x2));
// redraw the top graph contents to show only the area within the x domain
focus.select(".area").attr("d", area);
// redraw the top graph's x axis with the updated x scale domain
focus.select(".axis--x").call(xAxis);
// zoom the overlay of the top graph to reflect the new x domain
// so that any zoom operations will scale correctly
svg.select(".zoom").call(zoom.transform, d3.zoomIdentity
.scale(width / (s[1] - s[0]))
.translate(-s[0], 0));
}
Source: stackoverflow.com
Related Query
- D3 v4 Brush and Zoom on the same element (without mouse events conflicting)
- d3 zoom and brush working at the same time
- D3 Brush and Zoom error
- D3 v4 - Brush and Zoom - rescale y axis
- D3 Canvas brush and zoom
- In my d3 chart, Zoom selection disappears when I change the brush and click on not selected range
- Brush and Zoom d3
- d3 using zoom and brush on a bar chart
- D3: Unit Bar Chart with Brush and Zoom (transpose)
- d3 v3 - sync brush zoom and scroll zoom
- Achieving animated zoom with d3 and Leaflet
- d3 zoom and pan upgrade to version 4
- brush on rotated lines using d3 to create zoom effect
- Mouse events and brush in D3
- d3.js click and apply zoom and pan to distribute points located inside a targeted division to the triggered subdivisions
- Unusual d3 zoom in reusable class - this and bind
- D3 click coordinates after pan and zoom
- D3 - How to get correct scale and translate origin after manual zoom and pan to country path
- d3 zoom difference between Chrome and Firefox
- How to use mouse click and drag to zoom in D3
- D3 Zoom with Scrollbars used as panning, Scrollbar width and height adjusts to zoom scale
- d3.js pan and zoom jumps when using mouse after programatic zoom
- Constrain zoom and pan in d3
- how do i add a zoom in and zoom out button in a graph on d3
- javascript d3.js: initialize brush with brush.extent and stop data from spilling over margin
- Restricting minimum and maximum width of brush box
- D3.js - Line Graph: Area path goes over x and y axis on zoom
- Accurate pan and zoom to svg node
- D3 Map Pan and Zoom Performance on Chrome
- D3 Collapsible Tree only drag without zoom and jump and scroll
More Query from same tag
- D3 v4 drag/drop tree node into new parents children
- d3.js: Transition callback is not called for enter()
- Update bar chart and removing labels
- Tick marks on both sides of axis on d3 scale
- adding nodes to tree force layout - almost working
- Hide the transformation of the graph in the beginning
- D3.js transitions between charts
- Is it possible to get the y-axis interval in dimple.js?
- SVG path transition in D3 (v5) not working properly
- Making a D3 Pie v4 Example - Animate On initial render
- Error: <g> attribute transform: Expected number, "translate(undefined,undefiā¦"
- Redraw D3 Pie Chart with new data when clicking button or enter search field
- Displaying number of children in collapse tree
- Can't get d3js to load properly/display
- D3 show tooltip on mouse over with a timeout
- d3.js center image in bubble chart
- Force simulation of bubbles and texts going unbounded and inconsistent with XY axis
- Add a SVG object on a timescale axis in D3?
- D3 Line chart - display value label on Y axis tick and a not scale
- how to scale a specific Date value in d3 js
- C3js - How to set category label on axis marker
- How can I get the bounding box values of all the text elements in a selection?
- Can't access data in D3 within on click event?
- Rect in in d3js svg disappears after routing to another page in AngularJS
- How to load txt files into D3.JS
- Extract array from one json variable
- How to load chart data from URL for reactd3?
- d3 bar chart is upside down
- d3.js axes for time intervals (ages) spanning days, months, years
- How to specify height and width of a d3 tree dynamically to allow it to take up all the space it needs