reactjs - 如何将 d3 与 reactJS / nextJS 一起使用?
问题描述
我想按照教程添加一个 d3 图表,但什么也没有发生。我实际上不确定 useEffect() 是否处于良好的“时机”,是否应该使用 componentDidMount,或者它是否不是添加元素的好方法......似乎我在这里遗漏了一些东西!
import React from 'react';
import * as d3 from "d3";
import { useEffect } from 'react';
function drawChart() {
const data = [12, 5, 6, 6, 9, 10];
const h = 100;
const w = 100;
const svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h)
.style("margin-left", 100);
svg.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("x", (d, i) => i * 70)
.attr("y", (d, i) => h - 10 * d)
.attr("width", 65)
.attr("height", (d, i) => d * 10)
.attr("fill", "green")
}
const chart: React.FunctionComponent = () => {
useEffect(() => {
drawChart();
}, []);
return (
<div>
</div>
);
};
export default chart;
解决方案
在这个示例中,可能是错误的来源是 d3 将 SVG 附加到正文中,这完全在 React DOM 之外。
更好的方法是在 JSX 中添加 SVG,并使用引用(钩子中的 useRef)告诉 D3 必须在哪里呈现图表:
import * as React from "react";
import * as d3 from "d3";
function drawChart(svgRef: React.RefObject<SVGSVGElement>) {
const data = [12, 5, 6, 6, 9, 10];
const h = 120;
const w = 250;
const svg = d3.select(svgRef.current);
svg
.attr("width", w)
.attr("height", h)
.style("margin-top", 50)
.style("margin-left", 50);
svg
.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("x", (d, i) => i * 40)
.attr("y", (d, i) => h - 10 * d)
.attr("width", 20)
.attr("height", (d, i) => d * 10)
.attr("fill", "steelblue");
}
const Chart: React.FunctionComponent = () => {
const svg = React.useRef<SVGSVGElement>(null);
React.useEffect(() => {
drawChart(svg);
}, [svg]);
return (
<div id="chart">
<svg ref={svg} />
</div>
);
};
export default Chart;
推荐阅读
- django - 无法解析剩余部分:来自“菜单 [cat_id]”的“[cat_id]”
- version-control - 有没有办法在 Perforce 中进行代码检查?
- nuxt.js - 尽管使用了 Nuxt 根页面(父),但不会为子路由呈现
在父母 - python - enumerate(iterable) 和 zip(itertools.count(), iterable) 的区别
- python - 如果不逐行遍历数据框,这需要很长时间,我如何检查多行是否都满足条件?
- c++ - 内核驱动程序:注册回调失败(状态=C0000022)
- python - imap SEARCH 如何在 Microsoft Exchange 上使用 python imaplib 工作?
- html - 折叠具有可变行高的多列 HTML 布局
- python - MATPLOTLIB 3D 曲面图,每个列表包含 3 个不同大小的列表
- python - 在 Python 中计算数据表中的平均值