【问题标题】:Convert object created in HTML to React将 HTML 中创建的对象转换为 React
【发布时间】: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


【解决方案1】:

您不需要特殊的 D3 反应库,来自 npm (https://www.npmjs.com/package/d3) 的普通 D3 就可以正常工作。如果你真的想要 CDN,here 你可以找到你想要的版本的 CDN

然而,在您的代码中,您将 #legend1 作为元素选择器传递,但没有选择具有该 ID 的元素。

【讨论】:

  • 感谢您的建议。这最终工作:useEffect(() =&gt; { var colorScale1 = d3.scaleSequential(d3.interpolateYlGnBu).domain([-4, 16]); var svg = continuous("#legend1", colorScale1); }, []); 然后&lt;div id="legend1"&gt; &lt;svg /&gt; &lt;/div&gt;
  • 我不知道你是否使用了钩子!但是,是的,这也是我会做的(并且做过很多次)!很高兴我能帮上忙
猜你喜欢
  • 2017-01-11
  • 2018-03-26
  • 2021-07-23
  • 1970-01-01
  • 2016-09-17
  • 1970-01-01
  • 1970-01-01
  • 2022-12-17
  • 1970-01-01
相关资源
最近更新 更多