@google-web-components/google-chart

中文文档
5.1.0发布于 2024/05/06Apache-2.0TypeScriptES Module近 30 天下载 2.8万

Encapsulates Google Charts into a web component

原始使用文档
文档目录13 节
  1. Usage
  2. Installation
  3. In HTML file
  4. In a LitElement
  5. In a Polymer 3 element
  6. More usage examples
  7. Uprading from 3.x
  8. Removed Polymer-specific selection-changed event
  9. Removed google-chart-loader component
  10. Contributing
  11. Installation
  12. Running the demo locally
  13. Running the tests

google-chart

Google Charts API web components.

See: Documentation

Published on NPM Published on webcomponents.org

Usage

Installation

npm i @google-web-components/google-chart

In HTML file

<html>
  <head>
    <script type="module">
      import '@google-web-components/google-chart';
    </script>
  </head>
  <body>
    <google-chart data='[["Month", "Days"], ["Jan", 31]]'></google-chart>
  </body>
</html>

In a LitElement

import {LitElement, html} from 'lit';
import {customElement} from 'lit/decorators.js';
import '@google-web-components/google-chart';

@customElement('new-element')
export class NewElement extends LitElement {
  render() {
    return html`
      <google-chart data='[["Month", "Days"], ["Jan", 31]]'></google-chart>
    `;
  }
}

In a Polymer 3 element

import {PolymerElement, html} from '@polymer/polymer';
import '@google-web-components/google-chart';

class NewElement extends PolymerElement {
  static get template() {
    return html`
      <google-chart data='[["Month", "Days"], ["Jan", 31]]'></google-chart>
    `;
  }
}
customElements.define('new-element', NewElement);

More usage examples

See examples in the demo or try this live JS bin.

Uprading from 3.x

The component has been migrated to LitElement and uses TypeScript now. This migration introduced two breaking changes.

Removed Polymer-specific selection-changed event

The Polymer-specific selection-changed event commonly used for 2-way bindings has been removed. There were previously two events for observing chart selection changes: google-chart-select and the Polymer-generated selection-changed. For consistency with other events (e.g. google-chart-ready), we keep only google-chart-select.

Polymer components using this feature must be updated to explicitly name the selection event (details). In the example below, note the addition of ::google-chart-select.

- <google-chart selection="{{chartSelection}}"></google-chart>
+ <google-chart selection="{{chartSelection::google-chart-select}}"></google-chart>

LitElement components using the selection-changed event must be updated in a similar fashion:

- <google-chart .selection="${chartSelection}" @selection-changed="${reactToChartSelection}"></google-chart>
+ <google-chart .selection="${chartSelection}" @google-chart-select="${reactToChartSelection}"></google-chart>

Removed google-chart-loader component

Its functionality can be imported from the loader.js module:

import {dataTable, load} from '@google-web-components/google-chart/loader.js';

or you may instead choose to use google.visualization.ChartWrapper directly (example).

Contributing

Instructions for running the tests and demo locally:

Installation

git clone https://github.com/GoogleWebComponents/google-chart.git
cd google-chart
npm install

Running the demo locally

npm start

The browser will open automatically.

Running the tests

npm test

版本与发布记录

@google-web-components/google-chart 共发布 25 个版本,首次发布于 2018/12/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.1.0latest2024/05/062 年前
5.0.52023/09/063 年前
5.0.42023/07/203 年前
5.0.32023/02/113 年前
5.0.22022/03/254 年前
5.0.12021/12/174 年前

关于 @google-web-components/google-chart 的常见问题

@google-web-components/google-chart 是什么?

Encapsulates Google Charts into a web component

如何安装 @google-web-components/google-chart?

在项目目录运行 npm install @google-web-components/google-chart,或使用 pnpm add / yarn add / bun add。

@google-web-components/google-chart 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@google-web-components/google-chart 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@google-web-components/google-chart 可以商用吗?

@google-web-components/google-chart 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@google-web-components/google-chart 的下载量有多少?

根据 npm 官方下载统计,@google-web-components/google-chart 在 2026/10/17 – 2026/11/15 这 30 天内被下载 28,426 次,日均约 948 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@google-web-components/google-chart 还在维护吗?

最新版本 5.1.0 发布于 2024/05/06(2 年前);至今共发布 25 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/12/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。