{"id":13409212,"url":"https://github.com/ecomfe/echarts-for-weixin","last_synced_at":"2025-05-14T07:10:24.939Z","repository":{"id":37832343,"uuid":"122909676","full_name":"ecomfe/echarts-for-weixin","owner":"ecomfe","description":"基于 Apache ECharts 的微信小程序图表库","archived":false,"fork":false,"pushed_at":"2024-06-09T03:51:09.000Z","size":1817,"stargazers_count":7283,"open_issues_count":506,"forks_count":1594,"subscribers_count":165,"default_branch":"master","last_synced_at":"2025-04-30T10:05:19.755Z","etag":null,"topics":["chart","echarts","visualization","weixin"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ecomfe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-26T03:49:10.000Z","updated_at":"2025-04-30T09:06:48.000Z","dependencies_parsed_at":"2022-08-08T22:01:38.246Z","dependency_job_id":"4acacedb-611e-4f13-ad97-8c58b6c82396","html_url":"https://github.com/ecomfe/echarts-for-weixin","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecomfe%2Fecharts-for-weixin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecomfe%2Fecharts-for-weixin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecomfe%2Fecharts-for-weixin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecomfe%2Fecharts-for-weixin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ecomfe","download_url":"https://codeload.github.com/ecomfe/echarts-for-weixin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254092796,"owners_count":22013291,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chart","echarts","visualization","weixin"],"created_at":"2024-07-30T20:00:58.897Z","updated_at":"2025-05-14T07:10:19.928Z","avatar_url":"https://github.com/ecomfe.png","language":"JavaScript","funding_links":[],"categories":["Libraries","JavaScript","Code","小程序","前端开发框架及项目","Repository","Uncategorized","Projects List"],"sub_categories":["Charts Libraries","library/repositories","其他_文本生成、文本对话","Data Visualization","Uncategorized"],"readme":"# 在微信小程序中使用 Apache ECharts\n\n本项目是基于 [Apache ECharts](https://github.com/apache/echarts) 的微信小程序图表库，以及使用的示例。\n\n开发者可以通过熟悉的 ECharts 配置方式，快速开发图表，满足各种可视化需求。\n\n## 体验示例小程序\n\n在微信中扫描下面的二维码即可体验 ECharts Demo：\n\n![ECharts Demo](img/weixin-app.jpg)\n\n## 下载\n\n为了兼容小程序 Canvas，我们提供了一个小程序的组件，用这种方式可以方便地使用 ECharts。\n\n首先，下载本项目。\n\n其中，`ec-canvas` 是我们提供的组件，其他文件是如何使用该组件的示例。\n\n`ec-canvas` 目录下有一个 `echarts.js`，默认我们会在每次 `echarts-for-weixin` 项目发版的时候替换成最新版的 ECharts。如有必要，可以自行从 ECharts 项目中下载[最新发布版](https://github.com/apache/echarts/releases)，或者从[官网自定义构建](https://echarts.apache.org/zh/builder.html)以减小文件大小。\n\n## 引入组件\n\n微信小程序的项目创建可以参见[微信公众平台官方文档](https://developers.weixin.qq.com/miniprogram/dev/framework/quickstart/)。\n\n在创建项目之后，可以将下载的 [ecomfe/echarts-for-weixin](https://github.com/ecomfe/echarts-for-weixin) 项目完全替换新建的项目，然后将修改代码；或者仅拷贝 `ec-canvas` 目录到新建的项目下，然后做相应的调整。\n\n如果采用完全替换的方式，需要将 `project.config.json` 中的 `appid` 替换成在公众平台申请的项目 id。`pages` 目录下的每个文件夹是一个页面，可以根据情况删除不需要的页面，并且在 `app.json` 中删除对应页面。\n\n如果仅拷贝 `ec-canvas` 目录，则可以参考 `pages/bar` 目录下的几个文件的写法。下面，我们具体地说明。\n\n\n## 创建图表\n\n首先，在 `pages/bar` 目录下新建以下几个文件：`index.js`、 `index.json`、 `index.wxml`、 `index.wxss`。并且在 `app.json` 的 `pages` 中增加 `'pages/bar/index'`。\n\n`index.json` 配置如下：\n\n```json\n{\n  \"usingComponents\": {\n    \"ec-canvas\": \"../../ec-canvas/ec-canvas\"\n  }\n}\n```\n\n这一配置的作用是，允许我们在 `pages/bar/index.wxml` 中使用 `\u003cec-canvas\u003e` 组件。注意路径的相对位置要写对，如果目录结构和本例相同，就应该像上面这样配置。\n\n`index.wxml` 中，我们创建了一个 `\u003cec-canvas\u003e` 组件，内容如下：\n\n```xml\n\u003cview class=\"container\"\u003e\n  \u003cec-canvas id=\"mychart-dom-bar\" canvas-id=\"mychart-bar\" ec=\"{{ ec }}\"\u003e\u003c/ec-canvas\u003e\n\u003c/view\u003e\n```\n\n\u003e 注意此处的 `.container`，新建小程序项目后，其中 `app.wxss` 中默认自动生成的此 class 与本 demo 中的可能不一致，导致图表不能正常显示，只显示空白。请注意参考 `app.wxss` 修改样式，保证图表的初始化的时候是有宽度和高度的。\n\n其中 `ec` 是一个我们在 `index.js` 中定义的对象，它使得图表能够在页面加载后被初始化并设置。`index.js` 的结构如下：\n\n```js\nfunction initChart(canvas, width, height, dpr) {\n  const chart = echarts.init(canvas, null, {\n    width: width,\n    height: height,\n    devicePixelRatio: dpr // 像素比\n  });\n  canvas.setChart(chart);\n\n  var option = {\n    ...\n  };\n  chart.setOption(option);\n  return chart;\n}\n\nPage({\n  data: {\n    ec: {\n      onInit: initChart\n    }\n  }\n});\n```\n\n这对于所有 ECharts 图表都是通用的，用户只需要修改上面 `option` 的内容，即可改变图表。`option` 的使用方法参见 [ECharts 配置项文档](https://echarts.apache.org/zh/option.html)。对于不熟悉 ECharts 的用户，可以参见 [快速上手 ECharts](https://echarts.apache.org/handbook/zh/get-started/) 教程。\n\n完整的例子请参见 [ecomfe/echarts-for-weixin](https://github.com/ecomfe/echarts-for-weixin) 项目。\n\n## FAQ\n### 如何获取图表实例？\n\n`echarts.init` 返回的即为图表实例，可以参考 [pages/bar/index.js](/blob/master/pages/bar/index.js) 的写法。\n\n### 如何延迟加载图表？\n\n参见 `pages/lazyLoad` 的例子，可以在获取数据后再初始化数据。\n\n### 如何在一个页面中加载多个图表？\n\n参见 `pages/multiCharts` 的例子。\n\n### 如何使用 Tooltip？\n\n目前，本项目已支持 ECharts Tooltip，但是由于 ECharts 相关功能尚未发版，因此需要使用当前本项目中 `ec-canvas/echarts.js`，这个文件包含了可以在微信中使用 Tooltip 的相关代码。目前在 ECharts 官网下载的 `echarts.js` 还不能直接替换使用，等 ECharts 正式发版后即可。\n\n具体使用方法和 ECharts 相同，例子参见 `pages/line/index.js`。\n\n目前一个已知的 bug 是，有些图表的 tooltip 会显示 `\u003cbr/\u003e` 而非换行符。这一问题将在之后修复，暂时碰到这一问题时，开发者可以通过在 formatter 中使用 `\\n` 作为换行。\n\n### 如何保存为图片？\n\n参见 `pages/saveCanvas` 的例子。\n\n### 文件太大怎么办？\n\n本项目默认提供的 ECharts 文件是最新版本的包含所有组件文件。可以下载不同版本的 [ECharts](https://github.com/apache/echarts/blob/master/dist/) 进行替换。建议调试时使用未压缩版本，发布时使用压缩版本，否则文件会太大无法发布。\n\n发布时，如果对文件大小要求更高，可以在 [ECharts 在线定制](https://echarts.apache.org/zh/builder.html)网页下载仅包含必要组件的包，并且选择压缩。\n\n下载的文件放在 `ec-canvas/echarts.js`，**注意一定需要重命名为 `echarts.js`**。\n\n此外，还可考虑使用微信小程序的[分包策略](https://developers.weixin.qq.com/miniprogram/dev/framework/subpackages/independent.html)\n\n## 微信版本要求\n\n### Canvas 2d 版本要求\n\n最新版的 ECharts 微信小程序支持微信 Canvas 2d，当用户的基础库版本 \u003e= 2.9.0 且没有设置 `force-use-old-canvas=\"true\"` 的情况下，使用新的 Canvas 2d（默认）。\n\n使用新的 Canvas 2d 可以提升渲染性能，解决非同层渲染问题，强烈建议开启\n\n如果仍需使用旧版 Canvas，使用方法如下：\n\n```html\n\u003cec-canvas id=\"xxx\" canvas-id=\"xxx\" ec=\"{{ ec }}\" force-use-old-canvas=\"true\"\u003e\u003c/ec-canvas\u003e\n```\n\n### 最低版本要求\n\n支持微信版本 \u003e= 6.6.3，对应基础库版本 \u003e= 1.9.91。尽可能使用更高版本的基础库版本。\n\n调试的时候，需要在微信开发者工具中，将“详情”下的“调试基础库”设为 1.9.91 及以上版本。\n\n发布前，需要在 [https://mp.weixin.qq.com](https://mp.weixin.qq.com) 的“设置”页面，将“基础库最低版本设置”设为 1.9.91。当用户微信版本过低的时候，会提示用户更新。\n\n\n## 暂不支持的功能\n\nECharts 中的绝大部分功能都支持小程序版本，因此这里仅说明不支持的功能，以及存在的问题。\n\n以下功能尚不支持，如果有相关需求请在 [issue](https://github.com/ecomfe/echarts-for-weixin/issues) 中向我们反馈，对于反馈人数多的需求将优先支持：\n\n- 多个 zlevel 分层\n\n此外，目前还有一些 bug 尚未修复，部分需要小程序团队配合上线支持，但不影响基本的使用。已知的 bug 包括：\n\n- 安卓平台：transform 的问题（会影响关系图边两端的标记位置、旭日图文字位置等）\n- iOS 平台：半透明略有变深的问题\n- iOS 平台：渐变色出现在定义区域之外的地方\n\n如有其它问题，也欢迎在 [issue](https://github.com/ecomfe/echarts-for-weixin/issues) 中向我们反馈，谢谢！\n\n## Notice\n\nThe Apache Software Foundation [Apache ECharts, ECharts](https://echarts.apache.org/), Apache, the Apache feather, and the Apache ECharts project logo are either registered trademarks or trademarks of the [Apache Software Foundation](https://www.apache.org/).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fecomfe%2Fecharts-for-weixin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fecomfe%2Fecharts-for-weixin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fecomfe%2Fecharts-for-weixin/lists"}