繁体   English   中英

如何在 vuejs 中使用 D3 缩放图表?

[英]How can i zoom a chart with D3 in vuejs?

我正在尝试使用 d3 放大我的图表。

我有我的方法:

  this.chart = d3.select("#chart")
    .append("g")
    .attr("transform", `translate(${margin.left}, ${margin.top})`);

  this.x = d3.scaleLinear()
    .range([0, width - margin.left - margin.right]);

  this.y = d3.scaleLinear()
    .range([height - margin.top - margin.bottom, 0]);

  this.xAxis = this.chart.append("g")
    .attr(
      "transform",
      `translate(0, ${height - margin.top - margin.bottom})`
  );

在我的模板中:

<div id="app">
  <svg id="chart" viewBox="0 0 500 500"></svg>
</div>

所以基本上,我试过这个:

testZoom() {
  this.chart.attr("transform", "translate(" + d3.event.translate + ")" + " scale(" + d3.event.scale + ")")
},
this.chart = d3.select("#chart")
  .call(d3.behavior.zoom().on("zoom", this.testZoom))
  .append("g")
  .attr("transform", `translate(${margin.left}, ${margin.top})`);

还有很多其他的东西,但我每次都有这个错误

[Vue warn]: Error in mounted hook: "TypeError: d3__WEBPACK_IMPORTED_MODULE_0__.behavior is undefined"

我想知道是否有人知道为什么这不起作用,以及我能做些什么来解决我的问题。

要修复错误,我们可以这样做

this.chart = d3.select("#chart")
   .call(d3.zoom().on("zoom", this.test2))
   .append("g")
   .attr("transform", `translate(${margin.left}, ${margin.top})`)

test2(d) {
  this.chart.attr("transform", d.transform)
},

问题是因为我使用的是 D3 V6。 使用 V5,d3.event.translate 将起作用(Michael Rovinsky 的回应)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM