简体   繁体   English

无法在反应中嵌入 vega-lite 可视化

[英]Unable to embed vega-lite visualization in react

I am trying to use vega-lite in react.我正在尝试在反应中使用 vega-lite。 I tried below (check on codesandbox ) and its working:我在下面尝试过(检查codesandbox )及其工作:

Attempt 1 - data hardcoded in spec (working)尝试 1 - 在规范中硬编码的数据(工作)


radialChart.js径向图表.js

import { createClassFromSpec } from "react-vega";

export const RadialChart = createClassFromSpec({
  spec: {
    $schema: "https://vega.github.io/schema/vega-lite/v5.json",
    data: {
      values: [
        {
          "i-type": "A",
          count: 3,
          color: "rgb(121, 199, 227)"
        },
        {
          "i-type": "B",
          count: 20,
          color: "rgb(26, 49, 119)"
        },
        { "i-type": "C", count: 24, color: "rgb(18, 147, 154)" }
      ]
    },
    description: "A simple pie chart with labels.",
    encoding: {
      theta: { field: "count", type: "quantitative", stack: true },
      color: { field: "color", type: "nominal", legend: null, scale: null }
    },
    layer: [
      {
        mark: { type: "arc", outerRadius: 80 }
      }
    ]
  }
});

index.js index.js

import { RadialChart } from "./radialChart.js";

import "./styles.css";
function App() {
  return (
    <div className="App">
      <h1>react-vega-lite test</h1>
      <RadialChart />
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

It renders:它呈现:

在此处输入图像描述

Attempt 2 - dynamic data as suggested by docs (not work,g)尝试 2 - 文档建议的动态数据(不起作用,g)


However, as you can see in attempt 1, data is hardcoded in radialChart.js.但是,正如您在尝试 1 中看到的那样,数据在radialChart.js 中是硬编码的。 But I wanted to dynamically pass the data.但我想动态传递数据。 So, I checked the docs and found this .所以,我检查了文档并找到了这个 It suggests following changes:它建议进行以下更改:

radialChart.js径向图表.js

...
"data": [{ "name": "table" }],
...

radialData.js径向数据.js

const data = [ ... whole data array goes here ... ];

export default data;

index.js index.js

import radialData from "./radialData";
//...
<RadialChart data={{ table: radialData }} />

However its not working.但是它不起作用。 It gives following error:它给出以下错误:

Error: Unrecognized data set: table
    at error (https://d78mhq.csb.app/node_modules/vega-util/build/vega-util.module.js:517:9)
    at dataref (https://d78mhq.csb.app/node_modules/vega-view/build/vega-view.module.js:75:31)
    at View.change (https://d78mhq.csb.app/node_modules/vega-view/build/vega-view.module.js:86:19)
    at updateSingleDatasetInView (https://d78mhq.csb.app/node_modules/react-vega/esm/utils/updateSingleDatasetInView.js:13:12)
    at eval (https://d78mhq.csb.app/node_modules/react-vega/esm/utils/updateMultipleDatasetsInView.js:9:43)
    at Array.forEach (<anonymous>)
    at updateMultipleDatasetsInView (https://d78mhq.csb.app/node_modules/react-vega/esm/utils/updateMultipleDatasetsInView.js:8:21)
    at eval (https://d78mhq.csb.app/node_modules/react-vega/esm/Vega.js:66:50)
    at eval (https://d78mhq.csb.app/node_modules/react-vega/esm/VegaEmbed.js:52:13)

and a warnings:和警告:

WARN Infinite extent for field "count_end": [Infinity, -Infinity] 
WARN Infinite extent for field "count_end": [Infinity, -Infinity] 

Here is the codesandbox. 是代码框。 Here is the line in github where it is breaking which corresponds to line 86 in this bundled file. 是 github 中中断的行,对应于此捆绑文件中的第 86 行。 Whats exactly going wrong here?这里到底出了什么问题?

Update更新


Even passing data directly to RadialChart doesn't work ( sandbox link ):即使将数据直接传递给RadialChart也不起作用( 沙箱链接):

<RadialChart
    data={{
      values: [
        {
          "i-type": "A",
          count: 3,
          color: "rgb(121, 199, 227)"
        },
        {
          "i-type": "B",
          count: 20,
          color: "rgb(26, 49, 119)"
        },
        { "i-type": "C", count: 24, color: "rgb(18, 147, 154)" }
      ]
    }}
  />

I guess the doc specifies it incorrectly, it should be have been:我猜文档指定它不正确,它应该是:

"data": { "name": "table" },

instead of代替

"data": [{ "name": "table" }],

in radialChart.js.在radialChart.js 中。

Changing this fixes the issue as you can see in the corresponding sandbox, the visualization is rendered.正如您在相应的沙箱中看到的那样,更改此设置可以解决问题,并呈现可视化。

Now that I have resolved it myself, I will be happy if someone tells me how one can come up with this from the source code of react-vega.现在我已经自己解决了,如果有人告诉我如何从 react-vega 的源代码中提出这个问题,我会很高兴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM