【发布时间】:2021-09-22 23:52:08
【问题描述】:
我正在尝试将在 HTML 中创建的对象转换为 React。这是对象:
它是用这个 HTML 创建的:
<!doctype html>
<meta charset="utf-8">
<style>
body {
margin: 30px;
}
</style>
<body>
<div id="legend1" style="display: inline-block"></div>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
<script>
var colorScale1 = d3.scaleSequential(d3.interpolateYlGnBu)
.domain([-4, 16]);
continuous("#legend1", colorScale1);
function continuous(selector_id, colorscale) {
var legendheight = 200,
legendwidth = 80,
margin = {top: 10, right: 60, bottom: 10, left: 2};
var canvas = d3.select(selector_id)
.style("height", legendheight + "px")
.style("width", legendwidth + "px")
.style("position", "relative")
.append("canvas")
.attr("height", legendheight - margin.top - margin.bottom)
.attr("width", 1)
.style("height", (legendheight - margin.top - margin.bottom) + "px")
.style("width", (legendwidth - margin.left - margin.right) + "px")
.style("border", "1px solid #000")
.style("position", "absolute")
.style("top", (margin.top) + "px")
.style("left", (margin.left) + "px")
.node();
var ctx = canvas.getContext("2d");
var legendscale = d3.scaleLinear()
.range([1, legendheight - margin.top - margin.bottom])
.domain(colorscale.domain());
var image = ctx.createImageData(1, legendheight);
d3.range(legendheight).forEach(function(i) {
var c = d3.rgb(colorscale(legendscale.invert(i)));
image.data[4*i] = c.r;
image.data[4*i + 1] = c.g;
image.data[4*i + 2] = c.b;
image.data[4*i + 3] = 255;
});
ctx.putImageData(image, 0, 0);
var legendaxis = d3.axisRight()
.scale(legendscale)
.tickSize(6)
.ticks(8);
var svg = d3.select(selector_id)
.append("svg")
.attr("height", (legendheight) + "px")
.attr("width", (legendwidth) + "px")
.style("position", "absolute")
.style("left", "0px")
.style("top", "0px")
svg
.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + (legendwidth - margin.left - margin.right + 3) + "," + (margin.top) + ")")
.call(legendaxis);
};
</script>
这是我目前将 HTML 转换为 React 的尝试:
import React from "react";
import {Helmet} from "react-helmet"
import d3 from "react-d3-library";
// --------------------------------------
function App() {
function continuous(selector_id, colorscale) {
var legendheight = 200,
legendwidth = 80,
margin = { top: 10, right: 60, bottom: 10, left: 2 };
var canvas = d3
.select(selector_id)
.style("height", legendheight + "px")
.style("width", legendwidth + "px")
.style("position", "relative")
.append("canvas")
.attr("height", legendheight - margin.top - margin.bottom)
.attr("width", 1)
.style("height", legendheight - margin.top - margin.bottom + "px")
.style("width", legendwidth - margin.left - margin.right + "px")
.style("border", "1px solid #000")
.style("position", "absolute")
.style("top", margin.top + "px")
.style("left", margin.left + "px")
.node();
var ctx = canvas.getContext("2d");
var legendscale = d3
.scaleLinear()
.range([1, legendheight - margin.top - margin.bottom])
.domain(colorscale.domain());
var image = ctx.createImageData(1, legendheight);
d3.range(legendheight).forEach(function (i) {
var c = d3.rgb(colorscale(legendscale.invert(i)));
image.data[4 * i] = c.r;
image.data[4 * i + 1] = c.g;
image.data[4 * i + 2] = c.b;
image.data[4 * i + 3] = 255;
});
ctx.putImageData(image, 0, 0);
var legendaxis = d3.axisRight().scale(legendscale).tickSize(6).ticks(8);
var svg = d3
.select(selector_id)
.append("svg")
.attr("height", legendheight + "px")
.attr("width", legendwidth + "px")
.style("position", "absolute")
.style("left", "0px")
.style("top", "0px");
svg
.append("g")
.attr("class", "axis")
.attr(
"transform",
"translate(" +
(legendwidth - margin.left - margin.right + 3) +
"," +
margin.top +
")"
)
.call(legendaxis);
}
var colorScale1 = d3.scaleSequential(d3.interpolateYlGnBu).domain([-4, 16]);
var Result = continuous("#legend1", colorScale1);
return (
<>
<Helmet>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
</Helmet>
<Result/>
</>
);
}
export default App;
但是,我收到以下错误:
TypeError: react_d3_library__WEBPACK_IMPORTED_MODULE_6___default.a.scaleSequential is not a function
看来我可能错误地导入了d3 和d3-scale-chromatic。是否有其他方法可以从 CDN 访问 d3?据我所知,d3-scale-chromatic 没有 React 模块。
【问题讨论】:
标签: javascript reactjs react-native d3.js charts