如何将 Vega 图表集成到 Stencil JS 组件中?

huangapple go评论77阅读模式
英文:

How to integrate vega chart to stencil js component?

问题

我正在尝试使用vega图表创建一个stencil js组件,根据vega手册,我安装了3个包:

npm install vega
npm install vega-lite
npm install vega-embed

并尝试导入

import { Vega } from 'vega'

但出现以下错误信息:

模块'“vega”'没有导出成员'Vega'

有人成功将vega图表集成到stencil js中吗?

提前感谢您!

英文:

I am trying to create a stencil js component using vega chart, according to vega manual i installed 3 packages:

npm install vega
npm install vega-lite
npm install vega-embed

and trying to import

import { Vega } from 'vega'

and get this.

>Module '"vega"' has no exported member 'Vega'

Did anyone manage to integrate vega chart into stencil js?

Thank you in advance!

答案1

得分: 2

根据使用文档

> 在使用捆绑工具时使用 Vega。 如果您使用像 rollup.js 这样的捆绑工具,您可以将 Vega 作为模块导入。
>
> import * as vega from "vega";

请注意,如果您在启用了 Shadow DOM 的组件中使用它,您可能需要将 Vega 作为 container 配置的一部分传递给一个 DOM 节点,而不是一个 CSS 选择器:

class MyComponent {
  container: HTMLDivElement;

  componentDidLoad() {
    new vega.View(vega.parse(spec), {
      renderer:  'canvas',  // 渲染器(canvas 或 svg)
      container: this.container,   // 父 DOM 容器
    });

    // ...
  }

  render() {
    return <div ref={el => this.container = el}></div>
  }
}
英文:

According to the usage docs:

> Using Vega with a bundler. If you use Vega with a bundler like rollup.js, you can import Vega as a module.
>
> import * as vega from &quot;vega&quot;;

Note that if you're using it in a component with Shadow DOM enabled you will probably need to pass Vega, in the container configuration, a DOM node instead of a CSS selector:

class MyComponent {
  container: HTMLDivElement;

  componentDidLoad() {
    new vega.View(vega.parse(spec), {
      renderer:  &#39;canvas&#39;,  // renderer (canvas or svg)
      container: this.container,   // parent DOM container
    });

    // ...
  }

  render() {
    return &lt;div ref={el =&gt; this.container = el}&gt;&lt;/div&gt;
  }
}

huangapple
  • 本文由 发表于 2023年1月6日 03:44:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75023601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定