reactjs - 用 typescript 对 js 作出 react

标签 reactjs plotly

我将 react-plotly.js 与 typescript 一起使用,我从中得到以下错误:

No overload matches this call.
  Overload 1 of 2, '(props: PlotParams | Readonly<PlotParams>): Plot', gave the following error.
    Type '({ type: string; mode: string; x: number[]; y: number[]; marker: { color: string; }; } | { type: string; x: number[]; y: number[]; mode?: undefined; marker?: undefined; })[]' is not assignable to type 'Data[]'.
      Type '{ type: string; mode: string; x: number[]; y: number[]; marker: { color: string; }; } | { type: string; x: number[]; y: number[]; mode?: undefined; marker?: undefined; }' is not assignable to type 'Data'.
        Type '{ type: string; mode: string; x: number[]; y: number[]; marker: { color: string; }; }' is not assignable to type 'Data'.
          Type '{ type: string; mode: string; x: number[]; y: number[]; marker: { color: string; }; }' is not assignable to type 'Partial<BoxPlotData>'.
            Types of property 'type' are incompatible.
              Type 'string' is not assignable to type '"box" | undefined'.
  Overload 2 of 2, '(props: PlotParams, context: any): Plot', gave the following error.
    Type '({ type: string; mode: string; x: number[]; y: number[]; marker: { color: string; }; } | { type: string; x: number[]; y: number[]; mode?: undefined; marker?: undefined; })[]' is not assignable to type 'Data[]'.  TS2769

    20 | };
    21 | 
  > 22 | const Dashboard = () => <div><Plot data={data} layout={layout} /></div>;
       |                                    ^
    23 | 
    24 | export default Dashboard;
    25 |

我用它发送到以下数据,但它似乎不起作用。

const data = [{
    type: 'scatter',
    mode: 'lines+points',
    x: [1, 2, 3],
    y: [2, 6, 3],
    marker: { color: 'red' },
}, {
    type: 'bar',
    x: [1, 2, 3],
    y: [2, 5, 3],
}];

const layout = {
    width: 640,
    height: 480,
    title: 'A Fancy Plot',
};

const Dashboard = () => <div><Plot data={data} layout={layout} /></div>;

这与我发送的数据有关吗? 我安装了 react-plotly 的类型,因此出现了这个错误。

最佳答案

我在尝试将 react-plotly.js 与 TypeScript 一起使用时遇到了类似的问题。

这里你传递的是“lines+points”,这不是一个有效的类型,但即使你传递了一个正确的类型,仍然面临这个问题。

我就是这样解决的!

需要的包:

npm i plotly.js react-plotly.js

开发依赖:

npm i --save-dev @types/plotly.js @types/react-plotly.js

Chart.tsx 文件示例:

import React from "react";
import Plotly from "plotly.js";
import createPlotlyComponent from "react-plotly.js/factory";
const Plot = createPlotlyComponent(Plotly);

export default function LineChart() {
  const data = [
    {
      x: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
      y: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
      mode: "lines",
    },
  ];
  const layout = { title: "Chart Title" };

  return <Plot data={data} layout={layout} />;
}

关于reactjs - 用 typescript 对 js 作出 react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68518749/

相关文章:

javascript - 如何添加样式或类名并从 Web 组件中删除其他元素?

reactjs - 如何将 axios 中的数组参数传递到 Spring Controller ?

pandas - 放在眼镜编辑器上时,短划线图失去交互性

python - plotly + python : How to plot arrows in 3D?

r - plotly 3d surface - 将立方体更改为矩形空间

javascript - 如何从回调函数访问状态

javascript - React,将 prop 作为函数参数传递

python - 使用 plotly 在 3d 散点图中绘制 3d 框

r - 更改在 plotly 中创建的雷达图中特定环的颜色

html - 使用 React 将多个 html 元素存储在变量中