使用 ES6 在 D3 可重用图表中添加缩放功能 类
Adding a zoom function in D3 reusable charts using ES6 Classes
我正在将可重复使用的 D3 v5 图表切换为使用 ES6 classes,并且在实现像缩放函数这样更新变量的函数时遇到了问题。到目前为止,我有一张工作地图:
class myMap{
constructor(args = {}){
this.data = args.data;
this.topo = args.topo;
this.element =document.querySelector(args.element);
this.width =args.width || this.element.offsetWidth;
this.height = args.height || this.width / 2;
this.setup();
}
setup(){
this.projection = d3.geoMercator()
.translate([(this.width/2), (this.height/2)])
.scale( this.width / 2 / Math.PI);
this.path = d3.geoPath().projection(this.projection);
// zoom fuction inserted here
this.element.innerHTML ='';
this.svg =d3.select(this.element).append("svg")
.attr("width", this.width)
.attr("height", this.height)
//.call(zoom)
.append("g");
this.plot = this.svg.append("g");
d3.json(this.topo).then( world => {
var topo = topojson.feature(world, world.objects.countries).features;
this.draw(topo);
});
}
draw(topo){
var country = this.plot.selectAll(".country")
.data(topo);
country.enter().insert("path")
.attr("class", "country")
.attr("d", this.path)
.attr('id', 'countries')
.attr("fill", #cde)
.attr("class", "feature");
}
//move(){} goes here
}
使用以下方式调用:
const chart = new myMap({
element: '#map',
data: DATA_IN_JSON,
topo:"../../../LINK_to_topojsonfile"});
在使用函数时,我通过使用变量并调用移动函数来添加缩放,并在 SVG 上附加了 .call(zoom)
:
var zoom = d3.zoom()
.scaleExtent([1, 9])
.on("zoom", move);
function move() {
g.style("stroke-width", 1.5 / d3.event.transform.k + "px");
g.attr("transform", d3.event.transform);
}
使用 classes 我尝试在 class 的 setup()
部分声明缩放并调用移动表单 .on("zoom", this.move)
并附加一个 call
函数到上面注释中标记的 SVG。但是在引用 this.plot
时,我在移动函数中得到了 Uncaught TypeError: Cannot read property 'style' of undefined at SVGSVGElement.move
const zoom = d3.zoom()
.scaleExtent([1, 9])
.on("zoom", this.move);
move() {
this.plot
.style("stroke-width", 1.5 / d3.event.transform.k + "px");
this.plot
.attr("transform", d3.event.transform);
}
你需要绑定this
const zoom = d3.zoom()
.scaleExtent([1, 9])
.on("zoom", this.move.bind(this));
console.logthis
里面move()
我正在将可重复使用的 D3 v5 图表切换为使用 ES6 classes,并且在实现像缩放函数这样更新变量的函数时遇到了问题。到目前为止,我有一张工作地图:
class myMap{
constructor(args = {}){
this.data = args.data;
this.topo = args.topo;
this.element =document.querySelector(args.element);
this.width =args.width || this.element.offsetWidth;
this.height = args.height || this.width / 2;
this.setup();
}
setup(){
this.projection = d3.geoMercator()
.translate([(this.width/2), (this.height/2)])
.scale( this.width / 2 / Math.PI);
this.path = d3.geoPath().projection(this.projection);
// zoom fuction inserted here
this.element.innerHTML ='';
this.svg =d3.select(this.element).append("svg")
.attr("width", this.width)
.attr("height", this.height)
//.call(zoom)
.append("g");
this.plot = this.svg.append("g");
d3.json(this.topo).then( world => {
var topo = topojson.feature(world, world.objects.countries).features;
this.draw(topo);
});
}
draw(topo){
var country = this.plot.selectAll(".country")
.data(topo);
country.enter().insert("path")
.attr("class", "country")
.attr("d", this.path)
.attr('id', 'countries')
.attr("fill", #cde)
.attr("class", "feature");
}
//move(){} goes here
}
使用以下方式调用:
const chart = new myMap({
element: '#map',
data: DATA_IN_JSON,
topo:"../../../LINK_to_topojsonfile"});
在使用函数时,我通过使用变量并调用移动函数来添加缩放,并在 SVG 上附加了 .call(zoom)
:
var zoom = d3.zoom()
.scaleExtent([1, 9])
.on("zoom", move);
function move() {
g.style("stroke-width", 1.5 / d3.event.transform.k + "px");
g.attr("transform", d3.event.transform);
}
使用 classes 我尝试在 class 的 setup()
部分声明缩放并调用移动表单 .on("zoom", this.move)
并附加一个 call
函数到上面注释中标记的 SVG。但是在引用 this.plot
Uncaught TypeError: Cannot read property 'style' of undefined at SVGSVGElement.move
const zoom = d3.zoom()
.scaleExtent([1, 9])
.on("zoom", this.move);
move() {
this.plot
.style("stroke-width", 1.5 / d3.event.transform.k + "px");
this.plot
.attr("transform", d3.event.transform);
}
你需要绑定this
const zoom = d3.zoom()
.scaleExtent([1, 9])
.on("zoom", this.move.bind(this));
console.logthis
里面move()