我在StackOverflow上找到的其他类似答案都是旧的,不再适用于最新版本.

正在调用我的zoom 函数.transform参数包含:

transform parameter

我假设我将需要获取transform.transform中的信息,并将其应用于SVG的transform属性.this变量包含对SVG的引用.

我不确定的是到底该怎么做……?

    const width = 1000
    const height = 400

    const node_data = Array.from({ length: 5 }, () => ({
      group: Math.floor(Math.random() * 3),
    }))

    const edge_data = Array.from({ length: 10 }, () => ({
      source: Math.floor(Math.random() * 5),
      target: Math.floor(Math.random() * 5),
      value: Math.floor(Math.random() * 10) + 1,
    }))

    const links = edge_data.map((d) => ({ ...d }))
    const nodes = node_data.map((d, index) => ({ id: index, ...d }))
    const color = d3.scaleOrdinal(d3.schemeCategory10)

    //
    //
    //

    function zoomed(transform) {
      // console.log(`🚀 ~ zoomed ~ this:`, this)
      // console.log(`🚀 ~ zoomed ~ transform:`, transform)
    }

    const svg = d3.select('#chart').call(d3.zoom().on('zoom', zoomed))

    const simulation = d3
      .forceSimulation(nodes)
      .force(
        'link',
        d3
          .forceLink(links)
          .id((d) => d.id)
          .distance((d) => 100)
      )
      .force('charge', d3.forceManyBody())
      .force('center', d3.forceCenter(width / 2, height / 2))
      .on('tick', ticked)

    const link = svg
      .append('g')
      .attr('stroke', '#999')
      .attr('stroke-opacity', 0.6)
      .selectAll()
      .data(links)
      .join('line')
      .attr('stroke-width', (d) => Math.sqrt(d.value))

    const node = svg
      .append('g')
      .attr('stroke', '#fff')
      .attr('stroke-width', 1.5)
      .selectAll()
      .data(nodes)
      .join('circle')
      .attr('r', 16)
      .attr('fill', (d) => color(d.group))

    node.append('title').text((d) => `hello ${d.id}`)

    function ticked() {
      link
        .attr('x1', (d) => d.source.x)
        .attr('y1', (d) => d.source.y)
        .attr('x2', (d) => d.target.x)
        .attr('y2', (d) => d.target.y)

      node.attr('cx', (d) => d.x).attr('cy', (d) => d.y)
    }

    node.call(d3.drag().on('start', dragstarted).on('drag', dragged).on('end', dragended))

    function dragstarted(event) {
      if (!event.active) simulation.alphaTarget(0.3).restart()
      event.subject.fx = event.subject.x
      event.subject.fy = event.subject.y
    }

    function dragged(event) {
      event.subject.fx = event.x
      event.subject.fy = event.y
    }

    function dragended(event) {
      if (!event.active) simulation.alphaTarget(0)
      event.subject.fx = null
      event.subject.fy = null
    }
    .graph {
      width: 1000px;
      height: 400px;
    }
    <script src="https://d3js.org/d3.v7.min.js" charset="utf-8"></script>
  <svg ref="chart" id="chart" class="graph"></svg>

推荐答案

最直截了当的实施是:

function zoomed(e) {
  zoomG.attr('transform', e.transform); //<-- apply zoom
}

const svg = d3.select('#chart').call(d3.zoom().on('zoom', zoomed));  //<-- add zoom behavior
const zoomG = svg.append('g'); //<-- wrap drawing in g to apply zoom on

const width = 1000
    const height = 400

    const node_data = Array.from({ length: 5 }, () => ({
      group: Math.floor(Math.random() * 3),
    }))

    const edge_data = Array.from({ length: 10 }, () => ({
      source: Math.floor(Math.random() * 5),
      target: Math.floor(Math.random() * 5),
      value: Math.floor(Math.random() * 10) + 1,
    }))

    const links = edge_data.map((d) => ({ ...d }))
    const nodes = node_data.map((d, index) => ({ id: index, ...d }))
    const color = d3.scaleOrdinal(d3.schemeCategory10)

    //
    //
    //

    function zoomed(e) {
      zoomG.attr('transform', e.transform); //<-- apply zoome
    }

    const svg = d3.select('#chart').call(d3.zoom().on('zoom', zoomed));  //<-- add zoom behavior
    const zoomG = svg.append('g'); //<-- wrap drawing in g to apply zoom on

    const simulation = d3
      .forceSimulation(nodes)
      .force(
        'link',
        d3
          .forceLink(links)
          .id((d) => d.id)
          .distance((d) => 100)
      )
      .force('charge', d3.forceManyBody())
      .force('center', d3.forceCenter(width / 2, height / 2))
      .on('tick', ticked)

    const link = zoomG
      .append('g')
      .attr('stroke', '#999')
      .attr('stroke-opacity', 0.6)
      .selectAll()
      .data(links)
      .join('line')
      .attr('stroke-width', (d) => Math.sqrt(d.value))

    const node = zoomG
      .append('g')
      .attr('stroke', '#fff')
      .attr('stroke-width', 1.5)
      .selectAll()
      .data(nodes)
      .join('circle')
      .attr('r', 16)
      .attr('fill', (d) => color(d.group))

    node.append('title').text((d) => `hello ${d.id}`)

    function ticked() {
      link
        .attr('x1', (d) => d.source.x)
        .attr('y1', (d) => d.source.y)
        .attr('x2', (d) => d.target.x)
        .attr('y2', (d) => d.target.y)

      node.attr('cx', (d) => d.x).attr('cy', (d) => d.y)
    }

    node.call(d3.drag().on('start', dragstarted).on('drag', dragged).on('end', dragended))

    function dragstarted(event) {
      if (!event.active) simulation.alphaTarget(0.3).restart()
      event.subject.fx = event.subject.x
      event.subject.fy = event.subject.y
    }

    function dragged(event) {
      event.subject.fx = event.x
      event.subject.fy = event.y
    }

    function dragended(event) {
      if (!event.active) simulation.alphaTarget(0)
      event.subject.fx = null
      event.subject.fy = null
    }
.graph {
      width: 1000px;
      height: 400px;
    }
<script src="https://d3js.org/d3.v7.min.js" charset="utf-8"></script>
  <svg ref="chart" id="chart" class="graph"></svg>

Javascript相关问答推荐

无法将nPM simplex-noise包导入在JS项目中工作

获取表格的左滚动位置

togglePopover()不打开但不关闭原生HTML popover'

将2D数组转换为图形

如何修复(或忽略)HTML模板中的TypeScript错误?'

处理时间和字符串时MySQL表中显示的日期无效

如何在输入元素中附加一个属性为checkbox?

Ember.js 5.4更新会话存储时如何更新组件变量

S文本内容和值不必要的不同

如何修复我的数据表,以使stateSave正常工作?

类构造函数不能在没有用With Router包装的情况下调用

使用Nuxt Apollo在Piniastore 中获取产品细节

为什么客户端没有收到来自服务器的响应消息?

DOM不自动更新,尽管运行倒计时TS,JS

有效路径同时显示有效路径组件和不存在的路径组件

未捕获的不变违规:即使在使用DndProvider之后也应使用拖放上下文

我如何才能获得价值观察家&对象&S的价值?

Refine.dev从不同的表取多条记录

不允许在对象文本中注释掉的属性

相对于具有选定类的不同SVG组放置自定义工具提示