vue.js - Vue 应用程序中的 d3 - 如何在地图上应用缩放
问题描述
我想将此示例实现为 Vue.js 应用程序:
https://blockbuilder.org/adkdev/fe15a54ad3748c72e059475e3f43d462
我的方法是这样的:
<template>
<svg>
<path
v-for="(item, index) in features"
:key="index"
:stroke-width="countyBorderWidth"
:class="{ active: isActive(index) }"
:d="geoPath(item)"
v-on:click="clickedCountry(index, item.id)"
>
</svg>
</template>
<script>
projection() {
const p = d3.geoNaturalEarth1();
p.fitSize([this.width, this.height], this.features);
return p;
},
geoPath() {
return d3.geoPath().projection(this.projection);
},
zoom(item) {
// TODO
}
</script>
然后让zoom()
方法进行缩放。
我的问题是缩放部分,因为在示例中它被调用g
:
// Zoom
g.transition()
.duration(750)
.attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')scale(' + k + ')translate(' + -x + ',' + -y + ')');
您如何将这个示例转换为 Vue 应用程序,具体来说,您将如何实现地图的缩放?
谢谢你的帮助!
解决方案
你很好地识别了问题。缩放是在组( g
) 上进行的。
既然你没有,<g>
你可以像这样添加它:
<template>
<svg>
<g>
<path
v-for="(item, index) in features"
:key="index"
:stroke-width="countyBorderWidth"
:class="{ active: isActive(index) }"
:d="geoPath(item)"
v-on:click="clickedCountry(index, item.id)"
>
</g>
</svg>
</template>
然后在方法中选择它zoom
:
zoom(item) {
d3.select('g').transition()
.duration(750)
.attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')scale(' + k + ')translate(' + -x + ',' + -y + ')');
如果您想<g>
省略并继续您拥有的内容,请<svg>
直接选择:
zoom(item) {
d3.select('svg').transition()
.duration(750)
.attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')scale(' + k + ')translate(' + -x + ',' + -y + ')');
}
或者,您可以在组件的一部分中做所有事情,只<script>
留下部分:<svg>
<template>
<template>
<svg></svg>
</template>
然后您指定样式和属性,并像click
使用 D3.js一样管理事件。那将是链接示例中的代码。您需要重新组织它并将其分组为几个方法。
推荐阅读
- kubernetes - 如何从春季启动到 Kubernetes 发出“坏”但不是“致命”的健康检查信号?
- python-3.7 - “元组”对象不可调用?
- javascript - 试图在反应中添加多张图片,但它只显示一张图片
- azure - 即使我使用了切换按钮并获得了用户和管理员的同意,也需要管理员同意警告
- python - TensorFlow - tensorflow.python.framework.errors_impl.FailedPreconditionError
- r - R quantmod:拉取数据时出现连接错误
- ssh - 将 Visual Studio Code (VSCode) 连接到 VirtualBox VM
- javascript - 拖拽逐渐变慢
- mysql - 通讯链路故障,Spring Boot + MySql +Docker + Hibernate
- entity-framework-6 - 在包含 EF6 中加载引用属性的引用属性