// set the dimensions and margins of the graph
var margin = {top: 10, right: 30, bottom: 30, left: 60},
width = 460 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// append the SVG object to the body of the page
var SVG = d3.select("#dataviz_axisZoom")
.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 + ")");
//Read the data
d3.csv("https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/iris.csv", function(data) {
// Add X axis
var x = d3.scaleLinear()
.domain([4, 8])
.range([ 0, width ]);
var xAxis = SVG.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// Add Y axis
var y = d3.scaleLinear()
.domain([0, 9])
.range([ height, 0]);
var yAxis = SVG.append("g")
.call(d3.axisLeft(y));
// Add a clipPath: everything out of this area won't be drawn.
var clip = SVG.append("defs").append("SVG:clipPath")
.attr("id", "clip")
.append("SVG:rect")
.attr("width", width )
.attr("height", height )
.attr("x", 0)
.attr("y", 0);
// Create the scatter variable: where both the circles and the brush take place
var scatter = SVG.append('g')
.attr("clip-path", "url(#clip)")
// Add circles
scatter
.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("cx", function (d) { return x(d.Sepal_Length); } )
.attr("cy", function (d) { return y(d.Petal_Length); } )
.attr("r", 8)
.style("fill", "#61a3a9")
.style("opacity", 0.5)
// Set the zoom and Pan features: how much you can zoom, on which part, and what to do when there is a zoom
var zoom = d3.zoom()
.scaleExtent([.5, 20]) // This control how much you can unzoom (x0.5) and zoom (x20)
.extent([[0, 0], [width, height]])
.on("zoom", updateChart);
// This add an invisible rect on top of the chart area. This rect can recover pointer events: necessary to understand when the user zoom
SVG.append("rect")
.attr("width", width)
.attr("height", height)
.style("fill", "none")
.style("pointer-events", "all")
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
.call(zoom);
// now the user can zoom and it will trigger the function called updateChart
// A function that updates the chart when the user zoom and thus new boundaries are available
function updateChart() {
// recover the new scale
var newX = d3.event.transform.rescaleX(x);
var newY = d3.event.transform.rescaleY(y);
// update axes with these new boundaries
xAxis.call(d3.axisBottom(newX))
yAxis.call(d3.axisLeft(newY))
// update circle position
scatter
.selectAll("circle")
.attr('cx', function(d) {return newX(d.Sepal_Length)})
.attr('cy', function(d) {return newY(d.Petal_Length)});
}
})
我试着使用这个代码,但我不断得到错误说d3。event.transform函数(在updateChat()函数中)不再起作用,并且在新版本的d3js中被弃用。如何在最新版本的d3中编写此代码。js.我试图弄清楚如何使用d3js中的缩放功能来改变轴的比例以及内容,而不仅仅是像d3js缩放功能封面上的大多数在线指南那样的内容。
1条答案
按热度按时间hlswsv351#
向
updateChart
函数添加一个参数,并使用该参数代替d3.event.transform
。我将参数命名为zoomEvent
,所以结果是:数据似乎没有显示在您提供的代码中,但这里是工作轴缩放的完整代码。你可以在https://codepen.io/pen/上试试。