javascript - 在 React js 中使用 D3Funnel 时,TypeError : this. querySelectorAll 不是函数

标签 javascript reactjs typescript

我正在使用 d3funnel在我的 react 应用程序(基于 typescript )中,我不断收到错误 TypeError: this.querySelectorAll is not a function .而且,我不明白为什么会这样。这是我的示例代码:

import * as React from 'react'
import * as D3Funnel from 'd3-funnel'
import * as d3 from "d3";

const FunnelChart: React.FC = () => {

    const Ref = React.useRef(null)
    var data = [
        ['Applicants', 267 , '#1e4684', '#1e4684'],
        ['Interviews', 134,  '#1e4684'],
        ['Assessments', 48,  '#1e4684'],
        ['Hired',26,  '#1e4684']
    ];

    var options = {
        width : 200,
        height : 400,
        bottomWidth : 1/2,
        bottomPinch : 0,      // How many sections to pinch
        isCurved : true,     // Whether the funnel is curved
        curveHeight : 10,     // The curvature amount
        fillType : "gradient",   // Either "solid" or "gradient"
        isInverted : false,   // Whether the funnel is inverted
        hoverEffects : true,  // Whether the funnel has effects on hover
        fontSize : '18px'
    };

    
    React.useEffect(() => {
        var funnel = new D3Funnel( data, options );
        funnel.draw (d3.select(Ref.current));
    }, [])

        
        

    return (
        <>
            <div ref = {Ref}>
            </div>
        </>
    )
   } `
我真的很感激任何帮助。

编辑:这是错误:
react-dom.development.min.js:1 Uncaught TypeError: this.querySelectorAll is not a function at Array.__webpack_exports__.default (d3-funnel.js?f3d7:2417) at Selection.eval [as selectAll] (d3-funnel.js?f3d7:2395) at D3Funnel.destroy (d3-funnel.js?f3d7:194) at D3Funnel.draw (d3-funnel.js?f3d7:217) at eval (index.tsx?21e5:57) at Sg (react-dom.development.min.js:1) at Eg (react-dom.development.min.js:1) at HTMLUnknownElement.e (react-dom.development.min.js:1) at at g (react-dom.development.min.js:1) 

最佳答案

您需要通过ref.currentD3Funnel同时初始化它和 data & option 作为 draw 函数的参数。

  • 这是solution

  • import React, { useRef, useEffect } from "react";
    import D3Funnel from "d3-funnel";
    
    export default function App() {
      const chartRef = useRef(null);
      var data = [
        { label: "Teal", value: 12000, backgroundColor: "#008080" },
        { label: "Byzantium", value: 4000, backgroundColor: "#702963" },
        { label: "Persimmon", value: 2500, backgroundColor: "#ff634d" },
        { label: "Azure", value: 1500, backgroundColor: "#007fff" }
      ];
    
      var options = {
        width: 200,
        height: 400,
        bottomWidth: 1 / 2,
        bottomPinch: 2, // How many sections to pinch
        isCurved: true, // Whether the funnel is curved
        curveHeight: 10, // The curvature amount
        fillType: "gradient", // Either "solid" or "gradient"
        isInverted: true, // Whether the funnel is inverted
        hoverEffects: true, // Whether the funnel has effects on hover
        fontSize: "18px",
        label: {
          format: "{l}\n{f}"
        }
      };
    
      useEffect(() => {
        const chart = new D3Funnel(chartRef.current);
        console.log(chart);
        chart.draw(data, options);
      }, []);
      return <div className="App" ref={chartRef}></div>;
    }
    

    关于javascript - 在 React js 中使用 D3Funnel 时,TypeError : this. querySelectorAll 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64927048/

    相关文章:

    javascript - 从给定的数组输入创建数组列表

    javascript - 通过多个元素的单个引用来 react : How to replace . querySelectorAll

    javascript - 从字符串数组分配回调事件 (PIXI.js)

    javascript - 无法将 remote.require() 与 TypeScript 一起使用

    angular - 类型 'Observable<DocumentData[]>' 不可分配

    javascript - 如何使 svg 路径一个接一个地淡入/淡出?

    javascript - Highcharts错误13,但渲染Div存在

    javascript - 在窗口调整大小时刷新或触发 JavaScript

    javascript - React Native - 可按下 - 无法运行示例代码

    reactjs - React Redux - 为什么我不能轻松地从 this.props 设置初始值?