examples/adjustments for scatter plot

master
Matt Huntington 7 years ago
parent 609f86c505
commit 9972ccdd46

@ -1021,7 +1021,22 @@ Now if we zoom out, the graph should look something like this:
## Update axes when zooming/panning ## Update axes when zooming/panning
Now when we zoom, the points move in/out. When we pan, they move vertically/horizontally. Unfortunately, the axes don't update accordingly. Let's first add ids to the `<g>` elements that contain them Now when we zoom, the points move in/out. When we pan, they move vertically/horizontally. Unfortunately, the axes don't update accordingly. Let's first add ids to the `<g>` elements that contain them. Find the following code:
```javascript
var bottomAxis = d3.axisBottom(xScale);
d3.select('svg')
.append('g')
.call(bottomAxis)
.attr('transform', 'translate(0,'+HEIGHT+')');
var leftAxis = d3.axisLeft(yScale);
d3.select('svg')
.append('g')
.call(leftAxis);
```
Add `.attr('id', 'x-axis')` after the first `.append('g')` and `.attr('id', 'y-axis')` after the second `.append('g')`:
```javascript ```javascript
d3.select('svg') d3.select('svg')
@ -1037,19 +1052,42 @@ d3.select('svg')
.call(leftAxis); .call(leftAxis);
``` ```
Now let's use those ids when we zoom: Now let's use those ids to adjust the axes when we zoom. Find this code:
```javascript ```javascript
var zoomCallback = function(){ var zoomCallback = function(){
d3.select('#points').attr("transform", d3.event.transform); d3.select('#points').attr("transform", d3.event.transform);
d3.select('#x-axis').call(bottomAxis.scale(d3.event.transform.rescaleX(xScale))); //tell the bottom axis to adjust its values based on how much we zoomed
d3.select('#y-axis').call(leftAxis.scale(d3.event.transform.rescaleY(yScale))); //tell the left axis to adjust its values based on how much we zoomed
} }
``` ```
Add the following at the end of it:
```javascript
d3.select('#x-axis')
.call(bottomAxis.scale(d3.event.transform.rescaleX(xScale)));
d3.select('#y-axis')
.call(leftAxis.scale(d3.event.transform.rescaleY(yScale)));
```
Your zoomCallback should now look like this:
```javascript
var zoomCallback = function(){
d3.select('#points').attr("transform", d3.event.transform);
d3.select('#x-axis')
.call(bottomAxis.scale(d3.event.transform.rescaleX(xScale)));
d3.select('#y-axis')
.call(leftAxis.scale(d3.event.transform.rescaleY(yScale)));
}
```
Two things to note about the code above:
- `bottomAxis.scale()` tells the axis to redraw itself - `bottomAxis.scale()` tells the axis to redraw itself
- `d3.event.transform.rescaleX(xScale)` returns a value indicating how the bottom axis should rescale - `d3.event.transform.rescaleX(xScale)` returns a value indicating how the bottom axis should rescale
Now when you zoom out, the axes should redraw themselves:
![](https://i.imgur.com/AI3KoG9.png) ![](https://i.imgur.com/AI3KoG9.png)
## Update click points after a transform ## Update click points after a transform

@ -91,16 +91,18 @@ var render = function(){
} }
render(); render();
var bottomAxis = d3.axisBottom(xScale); //pass the appropriate scale in as a parameter var bottomAxis = d3.axisBottom(xScale);
d3.select('svg') d3.select('svg')
.append('g') //put everything inside a group .append('g')
.call(bottomAxis) //generate the axis within the group .attr('id', 'x-axis') //add an id
.call(bottomAxis)
.attr('transform', 'translate(0,'+HEIGHT+')'); .attr('transform', 'translate(0,'+HEIGHT+')');
var leftAxis = d3.axisLeft(yScale); var leftAxis = d3.axisLeft(yScale);
d3.select('svg') d3.select('svg')
.append('g') .append('g')
.call(leftAxis); //no need to transform, since it's placed correctly initially .attr('id', 'y-axis') //add an id
.call(leftAxis);
var createTable = function(){ var createTable = function(){
d3.select('tbody').html(''); //clear out all rows from the table d3.select('tbody').html(''); //clear out all rows from the table
@ -133,7 +135,12 @@ d3.select('svg').on('click', function(){
var zoomCallback = function(){ var zoomCallback = function(){
d3.select('#points').attr("transform", d3.event.transform); d3.select('#points').attr("transform", d3.event.transform);
d3.select('#x-axis')
.call(bottomAxis.scale(d3.event.transform.rescaleX(xScale)));
d3.select('#y-axis')
.call(leftAxis.scale(d3.event.transform.rescaleY(yScale)));
} }
var zoom = d3.zoom() var zoom = d3.zoom()
.on('zoom', zoomCallback); .on('zoom', zoomCallback);
d3.select('svg').call(zoom); d3.select('svg').call(zoom);

Loading…
Cancel
Save