# @google-web-components/google-chart 中文文档

规范地址：https://npmcn.com/package/%40google-web-components/google-chart/

版本：5\.1\.0
npm 元数据更新时间：2024-05-06T08:06:07.464Z
中文译文更新时间：2026-11-16T20:13:54.968Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40google-web-components/google-chart
原文对照：https://npmcn.com/package/%40google-web-components/google-chart/?lang=original
项目主页：https://googlewebcomponents.github.io/google-chart
源码仓库：https://github.com/GoogleWebComponents/google-chart

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：89.8 kB，11 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：2 个
- 首次发布：2018-12-19T20:54:30.559Z
- 最新版本发布：2024-05-06T08:06:07.271Z
- 近 30 天下载量：28426（2026-10-17 至 2026-11-15，npm 官方下载统计）
- 已发布版本数：25（近 12 个月 0 个）

## 中文简介

将 Google 图表封入网络组件

## 安装

```sh
npm install @google-web-components/google-chart
```

也可以使用：`pnpm add @google-web-components/google-chart`、`yarn add @google-web-components/google-chart`、`bun add @google-web-components/google-chart`

## README

# 谷歌图

[谷歌图表 API](https://developers.google.com/chart/) 网络组件.

见: [文档](https://www.webcomponents.org/element/@google-web-components/google-chart)

[![Published on NPM](https://img.shields.io/npm/v/@google-web-components/google-chart.svg)](https://www.npmjs.com/package/@google-web-components/google-chart) [![Published on webcomponents.org](https://img.shields.io/badge/webcomponents.org-published-blue.svg)](https://www.webcomponents.org/element/@google-web-components/google-chart)

## 使用量

### 安装

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

### 在 HTML 文件内

```html
<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>
```

### 在一个LitElement中

```typescript
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>
    `;
  }
}
```

### 在多聚体3元素中

```javascript
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);
```

### 更多使用实例

查看演示中的实例或尝试此实况 [联合来文:本](https://jsbin.com/zitotejimi/edit?html,output)。 。 。 .

## 从 3.x 恢复

该组件已迁移到 LitElement , 现在使用 TypeScript 。 这种迁移带来了两个突破性的变化.

### 删除的多聚体特性 `selection-changed` 事件

特定多聚体 `selection-changed` 用于双向绑定的事件已被删除. 以往有两起观测图表选择变化的事件: `google-chart-select` 和多聚体产生的 `selection-changed`。 。 。 . 为了与其他事件保持一致(例如:. `google-chart-ready`),我们只保留 `google-chart-select`。 。 。 .

使用此特性的多聚体组件必须更新,以明确命名选择事件([细节](https://polymer-library.polymer-project.org/3.0/docs/devguide/data-binding#two-way-native)) (中文(简体) ). 在下文的例子中,注意到增加了: `::google-chart-select`。 。 。 .

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

使用 `selection-changed` 事件必须以类似方式更新:

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

### 删除 `google-chart-loader` 组件

其功能可以从 `loader.js` 模块 :

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

或者您可以选择使用 `google.visualization.ChartWrapper` 直接( e)[实例](https://developers.google.com/chart/interactive/docs/reference#chartwrapper-class)) (中文(简体) ).

## 捐款

本地测试和演示操作指令 :

### 安装

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

### 本地运行演示

```sh
npm start
```

浏览器会自动打开 .

### 运行测试

```sh
npm test
```

## 常见问题

### @google-web-components/google-chart 是什么？

将 Google 图表封入网络组件

### 如何安装 @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 和提交记录判断。

