我是使用angular和D3js的初学者,我用它绘制了一张地图,并且希望将路径svg上的每次点击都路由到另一个页面。
我尝试这样,但是
map.component.ts
constructor(private router: Router) { }
我构建路由器。
ngOnInit() {
const width = document.getElementById("container").offsetWidth * 0.95,
height = 800,
legendCellSize = 20,
colors = ['#d4eac7', '#c6e3b5', '#b7dda2', '#a9d68f', '#9bcf7d', '#8cc86a', '#7ec157', '#77be4e', '#70ba45', '#65a83e', '#599537', '#4e8230', '#437029', '#385d22', '#2d4a1c', '#223815'];
const path = d3.geoPath()
.pointRadius(2);
const projection = d3.geoConicConformal() // Lambert-93
.center([-19.76,37.63]) // Center
.scale(2600)
.translate([width / 2, height / 2]);
path.projection(projection);
const svg = d3.select('#map').append("svg")
.attr("id", "svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", "0 0 " + width + " " + height)
.attr("preserveAspectRatio", "xMinYMid")
.attr("class", "svgun")
.style("display","block")
//.style("margin","auto")
.style("background","#222b1d")
.style("color","#bebebe");
// Append the group that will contain our paths
const deps = svg.append("g");
const geojson = departmentJSON;
const csv = infoJSON;
var features = deps
.selectAll("path")
.data(geojson.features)
.enter()
.append("path")
.attr('id', function(d) {return "d" + d.properties.ID_GEOFLA;})
.attr("d", path)
.attr("class","country");
const min = d3.min(csv, function(e) { return +e.ex; }),
max = d3.max(csv, function(e) { return +e.ex; });
此处是路由到另一页的功能
function goToPage(pageName: number){
//console.log("let's go"); //it works
this.router.navigate(['/auth']); // ERROR TypeError: Cannot read property 'router' of undefined
}
var quantile = d3.scaleQuantile().domain([min, max])
.range(colors);
var tooltip = addTooltip();
var legend = addLegend(min, max);
csv.forEach(function(e,i) {
var countryPath = d3.select("#d" + e.ID_GEOFLA);
countryPath
.attr("scorecolor", quantile(+e.ex))
.style("fill", function(d) { return quantile(+e.ex); })
.on("click", function(d){
//alert('let's gooooooo '); //it works
goToPage(5); //ERROR TypeError: Cannot read property 'router' of undefined
//this.router.navigate(['not-found']); // ERROR TypeError: Cannot read property 'navigate' of undefined
})
});
}
}
map.html
<div id="container" class="container">
<div id="map"></div>
</div>
我尝试在单击时调用toGoPage(5)函数,但出现此错误
// ERROR TypeError: Cannot read property 'router' of undefined
如果我尝试在点击时进行路由,则会出现另一个错误:
// ERROR TypeError: Cannot read property 'router' of undefined