new Vue({
el: "#app",
data: {
todos: [
{ text: "Learn JavaScript", done: false },
{ text: "Learn Vue", done: false },
{ text: "Play around in JSFiddle", done: true },
{ text: "Build something awesome", done: true }
],
todoHovered: "hover a circle"
},
mounted() {
const svg = d3.select(this.$refs.chart)
.append("svg")
.attr("width", 400)
.attr("height", 100);
const circles = svg.selectAll("circle")
.data(this.todos).enter()
.append("circle")
.attr("cx", 10)
.attr("cy", (d,i) => 10 + i * 15)
.attr("r", 5)
.style("fill", d => d.done ? "green" : "red");
circles.on("mouseover", this.showMessage);
circles.on("mouseout", (e) => d3.select(e.currentTarget).attr("r", 5));
},
methods: {
showMessage(e, d) {
d3.select(e.currentTarget).attr("r", 8);
this.todoHovered = `${d.text} is ${d.done ? 'done' : 'not done'}`;
}
}
})
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
<script src="https://d3js.org/d3.v6.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<div id="app">
<div ref="chart">
</div>
<p>
Message: {{ todoHovered }}
</p>
</div>
showMessage(d) {...}
变成showMessage(e, d) {...}
。 - thibautgshowMessage
方法内)。我添加了另一个事件,在鼠标移出时将其设置回5。 - thibautg