score:1
The translate value is aligning the graph so that x(d0)
is the leftmost x value visible in the plot area. This ensures the visible portion of the plot area extends from d0
through d1
(the visible subdomain). If our full domain for the x scale has a minimum of 0, then x(0)
will be shifted left (negative shift) x(d0)
pixels.
I'll use a snippet to demonstrate:
var svg = d3.select("svg"),
margin = {top: 10, right: 50, bottom: 70, left: 200},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
// Scale for Data:
var x = d3.scaleLinear()
.range([0, width])
.domain([0,20]);
// Scale for Zoom:
var xZoom = d3.scaleLinear()
.range([0,width])
.domain([0,width]);
var xAxis = d3.axisBottom(x).ticks(5);
var xZoomAxis = d3.axisBottom(xZoom);
var zoom = d3.zoom()
.scaleExtent([1, 32])
.translateExtent([[0, 0], [width, height]])
.extent([[0, 0], [width, height]])
.on("zoom", zoomed);
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// plot area
g.append("rect")
.attr("width",width)
.attr("height",height)
.attr("fill","url(#stripes)");
g.append("text")
.attr("x",width/2)
.attr("y",height/2)
.style("text-anchor","middle")
.text("plot area");
g.append("line")
.attr("y1",0)
.attr("y2",height)
.attr("stroke-width",1)
.attr("stroke","black");
// zoomed plot area:
var rect = g.append("rect")
.attr("width",width)
.attr("height",height)
.attr("fill","lightgrey")
.attr("opacity",0.4);
// Axis for plot:
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Axis for zoom:
g.append("g")
.attr("class", "axis axis-zoom-x")
.attr("transform", "translate(0,"+(height+30)+")")
.call(xZoomAxis);
var text = g.append("text")
.attr("y", height+60)
.attr("text-anchor","middle")
.text("zoom units")
.attr("x",width/2);
// Gratuitous intro zoom:
var d1 = 18;
var d0 = 8;
svg.call(zoom).transition()
.duration(2000)
.call(zoom.transform, d3.zoomIdentity
.scale(width / (x(d1) - x(d0)))
.translate(-x(d0), 0));
function zoomed() {
var t = d3.event.transform, xt = t.rescaleX(x);
xZoom.range([xt(0),xt(20)]);
g.select(".axis--x").call(xAxis.scale(xt));
g.select(".axis-zoom-x").call(xZoomAxis.scale(xZoom));
rect.attr("x", xt(0));
rect.attr("width", xt(20) - xt(0));
text.attr("x", xt(10));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg width="400" height="180">
<defs>
<pattern id="stripes" patternUnits="userSpaceOnUse" width="8" height="8" patternTransform="rotate(45 0 0)">
<rect width="3" height="8" fill="orange"></rect>
</pattern>
</defs>
</svg>
Snippet Explanation:
- Plot area: orange stripes
- Full scaled extent of data: grey box.
- Left hand side of plot area is x=0 (pixels) for the parent
g
that holds everything.
As we zoom in the bounds of our data exceeds the plot area. We want to show a specific subdomain of our data. We achieve part of that with the scale (as you correctly deduce) but the other portion is with the translate: we push values less than the lowest value of our x subdomain to the left. By pushing the entire graph left by an amount equal to x(d0)
, x(d0)
appears as the leftmost coordinate of the plot area.
Source: stackoverflow.com
Related Query
- What Transformation values to calculate for scale and translate if you want to zoom
- When a d3.behavior.zoom event is triggered programatically, how do you set inital values for translate and scale?
- D3: Create a continuous color scale with many strings/inputs for the range and dynamically changing values of the domain
- d3 v6 pointer function not adjusting for scale and translate
- What do the nodes, groups, and values mean in the JSON for a d3 force-directed graph?
- If condition never met and hence my bars in a simple bar chart are always blue, while I want them to be red for values below a certain number
- D3 transform scale and translate
- How to change speed of translate and scale when zooming in and out in D3 (using zoom.on & d3.event.translate, d3.event.zoom)?
- what scale to use for representing years only on x axis in d3 js
- Special donut chart with different rings/arcs for positive and negative values
- How can I plot positive and negative values for both axes in a scatter plot with d3?
- How to calculate in-degree, out-degree and weigted degree for force directed graph (d3.js)?
- D3 - How to get correct scale and translate origin after manual zoom and pan to country path
- d3 using a different X scale and Y scale domain for each selection
- D3.js: Set fitExtent or fitSize (or center and scale programmatically) for map with multiple geoJSON files
- custom d3 linear scale that returns null for null values instead of 0
- reversing order of d3.zoom scale and translate
- What is the proper way to both rotate and translate text in d3?
- D3.js: calculate x-axis time scale for bar graph?
- How do you translate HTML elements along with SVG elements when zooming and panning with D3
- Number formatting functions: What algorithm is used for rounding values ending in 5?
- Multiple maps with d3.js: change values of scale and center
- NVD3.js line graphs fail to scale y axis to max and min values
- Create a padding for the first and last data points using a time scale
- Using d3js to calculate midpoints and curve path for quadratic SVG connectors
- How do I add bubbles on each of the y-axis values that I have and how do I add mouseover function for each of them?
- How to scale and translate together?
- rounded values for color in d3 scale
- Simultaneously extracting list of dimensions and creating scale for each in d3.js - Expand on process?
- What kind of projection to use for D3 and topojson maps of Mexican cities?
More Query from same tag
- modifying the d3 force-directed graph example
- merge multiple arrays side by side on a given key
- How to display multiple values on mouseover on map in d3?
- Resizable bubble chart
- Data refresh issue with D3 burnup chart
- Is it possible to combine data from JSON object AND from columns? [C3JS]
- How to change the opacity of a node?
- Update: Changing the color of Negative Predetermined Value Using Javascript/Jquery
- Alternative to eval() when dealing with d3 animation queues
- d3 bubble chart with fisheye
- Drawing Connecting Lines ("Great Arcs") on a D3 Symbol Map
- D3js: How to stretch object vertically?
- d3 js line graph takes two different data formats. how do I differentiate those two?
- Add names of the states to a map in d3.js
- Simplest D3.js example not working
- Highlight tree path in d3.js
- Rickshaw only show gridlines on some points
- How to move D3 graph to initial zoom and at 0,0 position - RESET Graph?
- D3 - how to include č,š,ž letters in D3
- D3 JS - Group Containing a SVG Rectangle Won't Translate Smoothly on Drag
- Mouseover is not working in d3.js by attaching it to circles
- Type definition of object in d3.js version 5?
- D3 - data format required for cal-heatmap calendar heatmap?
- D3 map, click and get id from json
- Parsing header row with dates when importing csv data
- Prevent svg groups from overlapping (D3 js v4)
- How to create a javascript function using Scala.js?
- How to match a position in an array to a scale in d3
- Getting array length with d3
- d3 on click on circle pause and resume transition of marker along line