我正在try 使用d3js和reaction js创建一个Voronoi图.我在实现所需代码后遇到的问题是,Voronoi图的宽度超过了我为SVG指定的宽度.

以下是我实现的代码:

import React, { useState, useEffect, useRef, useCallback } from "react";
import * as d3 from "d3";
import "../App.css";




 const mySVGRef = useRef(null);
  const width = 600;
  const height = 500;
    // Create a Voronoi generator
    const voronoi = d3.Delaunay.from(data).voronoi();

const svg = d3.select(mySVGRef.current);
svg.attr("width", width).attr("height", height);
const xScale = d3
  .scaleLinear() // scale is used for linear
  .domain([0, d3.max(data, (d) => d[0])])
  .range([0, width]);
// Draw the Voronoi cells
svg
  .selectAll("path")
  .data(data)
  .enter()
  .append("path")
  .attr("d", (d, i) => voronoi.renderCell(i))
  .attr("fill", "none")
  .attr("stroke", "black");

// Draw the data points
svg
  .selectAll("circle")
  .data(data)
  .enter()
  .append("circle")
  .attr("cx", (d) => xScale(d[0]))
  //same must be done for the y scale
  .attr("cy", (d) => d[1])
  .attr("r", 3)
  .attr("fill", "red");
//draw the x and y axis

var x_axis = d3
  .axisBottom()
  .scale(xScale);
svg
  .append("g") // 
  .attr("transform", "translate(0, " + height + ")")
  .call(x_axis);


 

推荐答案

voronoi方法带有一个可选的bounds参数:

渲染时,图表将被剪裁到指定的边界= [xmin,ymin,xmax,ymax].如果未指定边界,则默认为 [0,0,960,500].观看《走向无限》,然后再回来观看互动节目 Voronoi细胞剪裁的解释.

您可以在此处更新代码:

const voronoi = d3.Delaunay.from(data).voronoi(); // <-- no bounds

对此:

const voronoi = d3.Delaunay.from(data).voronoi([1, 1, width-1, height-1]);

请参见下面的内容:

// put your React items back later
//import React, { useState, useEffect, useRef, useCallback } from "react";
//import * as d3 from "d3";
//import "../App.css";
// const mySVGRef = useRef(null);
const mySVGRef = { current: "#ref" }

const width = 600;
const height = 500;

// fake data
const data = Array.from({length: 200}, () => [Math.random() * width, Math.random() * height]) 

// Create a Voronoi generator
//const voronoi = d3.Delaunay.from(data).voronoi(); <-- no bounds
// with bounds
const voronoi = d3.Delaunay.from(data).voronoi([1, 1, width-1, height-1]);

const svg = d3.select(mySVGRef.current);
svg.attr("width", width).attr("height", height);
const xScale = d3
  .scaleLinear() // scale is used for linear
  .domain([0, d3.max(data, (d) => d[0])])
  .range([0, width]);
// Draw the Voronoi cells
svg
  .selectAll("path")
  .data(data)
  .enter()
  .append("path")
  .attr("d", (d, i) => voronoi.renderCell(i))
  .attr("fill", "none")
  .attr("stroke", "black");

// Draw the data points
svg
  .selectAll("circle")
  .data(data)
  .enter()
  .append("circle")
  .attr("cx", (d) => xScale(d[0]))
  //same must be done for the y scale
  .attr("cy", (d) => d[1])
  .attr("r", 3)
  .attr("fill", "red");
//draw the x and y axis

var x_axis = d3
  .axisBottom()
  .scale(xScale);
svg
  .append("g") // 
  .attr("transform", "translate(0, " + height + ")")
  .call(x_axis);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.8.0/d3.min.js"></script>
<svg id="ref"></svg>

Javascript相关问答推荐

Angular:ng-contract未显示

在JavaScript中,如何将请求的回调函数的结果合并到运行的代码中?

foreach循环中的Typescript字符串索引

JavaScript文本区域阻止KeyDown/KeyUp事件本身上的Alt GR +键组合

从PWA中的内部存储读取文件

如何将连续的十六进制字符串拆分为以空间分隔的十六进制块,每个十六进制块包含32个二元组?

django无法解析余数:[0] from carray[0]'

配置WebAssembly/Emscripten本地生成问题

如何将Map字符串,布尔值转换为{key:string;value:bo布尔值;}[]?<>

并不是所有的iframe都被更换

如何强制Sphinx中的自定义js/css文件始终加载更改而不缓存?

从页面到应用程序(NextJS):REST.STATUS不是一个函数

如何将数组用作复合函数参数?

VSCode中出现随机行

为什么JPG图像不能在VITE中导入以进行react ?

在FAQ Accodion应用程序中使用React useState时出现问题

AddEventListner,按键事件不工作

在表单集中保存更改时删除';禁用';

使用API调用的VUE 3键盘输入同步问题

JQuery使用选项填充HTMLSELECT并设置默认结果,默认结果显示为空