Skip to content

Commit 890eeb7

Browse files
282764407@qq.com282764407@qq.com
authored andcommitted
1、优化鸿蒙平台的README说明,添加示例图
2、新增基于状态管理V2的组件
1 parent 7362bd7 commit 890eeb7

37 files changed

Lines changed: 557 additions & 586 deletions

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,41 @@ UCharts 是一个基于 TypeScript 实现的高性能、模块化、可扩展的
2929
- **玫瑰图 (rose)**
3030
- **更多类型持续开发中...**
3131

32+
## 图表示例
33+
34+
以下为部分图表类型的鸿蒙平台实际渲染效果:
35+
36+
- 柱状图
37+
38+
![柱状图](./docs/image/column.png) ![柱状图](./docs/image/column3.png)
39+
40+
- 区域图
41+
42+
![区域图](./docs/image/area1.png) ![区域图](./docs/image/area2.png)
43+
44+
- 山峰图
45+
46+
![山峰图](./docs/image/mount1.png) ![山峰图](./docs/image/mount2.png)
47+
48+
- 散点图
49+
50+
![散点图](./docs/image/scatter.png)
51+
52+
- 气泡图
53+
54+
![气泡图](./docs/image/bubble.png)
55+
56+
- 饼图
57+
58+
![饼图](./docs/image/piepng.png)
59+
60+
- 玫瑰图
61+
62+
![玫瑰图](./docs/image/rose.png)
63+
64+
(更多类型和样式可参考 docs 目录)
65+
66+
3267
## 下载安装
3368

3469
鸿蒙平台请查看适配目录的README.md文件,其他平台可通过[Github下载](https://github.com/junbin-yang/uCharts-v3/releases)

adapters/platform/harmony/README.md

Lines changed: 17 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
# UCharts - 跨平台可视化图表库
1+
# UCharts for Harmony - 鸿蒙可视化图表库
22

33
## 项目简介
44

5-
UCharts 是一个基于 TypeScript 实现的高性能、模块化、可扩展的跨平台图表库。底层渲染逻辑全部采用 TypeScript 实现,上层通过适配层(adapters)适配到不同平台,包括鸿蒙(HarmonyOS)、微信小程序、uniapp 等,真正实现"一套核心,多端复用"
5+
UCharts 是一款高性能、易用的图表库,现已适配 HarmonyOS 平台。支持多种常用图表类型,满足鸿蒙应用的数据可视化需求
66

77
## 特性
88

9-
- 🚀 **跨平台**:支持鸿蒙、微信小程序、uniapp 等主流平台
109
- 🧩 **模块化设计**:底层渲染与平台适配解耦,易于扩展和维护
1110
- 🛡️ **TypeScript 全面支持**:类型安全,开发体验优秀
1211
- 🎨 **丰富图表类型**:柱状图、条状图、折线图、区域图、山峰图等
@@ -50,7 +49,7 @@ struct Index {
5049
private opts: Partial<ChartOptions> = {
5150
type: "column",
5251
categories: ["2018","2019","2020","2021","2022","2023"],
53-
series: series: [
52+
series: [
5453
{
5554
name: "目标值",
5655
data: [35,36,31,33,13,34]
@@ -82,102 +81,35 @@ struct Index {
8281
UCharts({ controller: this.chart, onReady: () => {
8382
this.chart.updateData(this.opts)
8483
}})
84+
/*
85+
* 或者初始化时传入默认配置
86+
* @State chart: UChartsController = new UChartsController(this.opts);
87+
* UCharts({ controller: this.chart })
88+
* */
8589
}
8690
.height('100%')
8791
.width('100%')
8892
}
8993
}
9094
```
9195

92-
> 具体平台的 context 获取方式请参考各自适配层文档。
96+
## API 文档
9397

94-
## 目录结构
98+
详见[文档](https://github.com/junbin-yang/uCharts-v3/tree/master/docs)目录。
9599

96-
```
97-
├── core/ # 图表核心能力(平台无关)
98-
│ ├── types/ # 类型定义
99-
│ ├── utils/ # 工具函数
100-
│ ├── chart/ # 各类图表渲染器
101-
│ ├── event/ # 事件系统
102-
│ ├── animation/ # 动画系统
103-
│ └── factory.ts # 图表工厂
104-
├── adapters/ # 平台适配层
105-
│ ├── harmony/ # 鸿蒙适配(**当前仓库目录位置**)
106-
│ ├── h5/ # 原生H5适配
107-
│ ├── wechat/ # 微信小程序适配
108-
│ └── uniapp/ # uniapp适配
109-
├── interface/ # 对外统一接口
110-
│ ├── CanvasContext.ts # 跨平台统一 canvas context 接口定义
111-
├── examples/ # 示例代码
112-
├── docs/ # 文档
113-
└── README.md
114-
```
115-
116-
## 跨平台适配层说明
117-
118-
- 每个平台在 `adapters/` 下有独立目录,负责将平台 API 适配为统一的底层渲染接口。
119-
- 适配层需实现统一的适配接口,并暴露标准的 context、事件等能力,可参考h5实现。
120-
- 新增平台时,仅需在 `adapters/` 下新增目录并实现适配接口,无需修改 core 层代码。
121-
- **CanvasContext 统一接口**
122-
- `ChartOptions.context` 字段要求传入的 canvas context 必须兼容 `interface/CanvasContext` 类型。
123-
- 各平台适配层需将平台原生 canvas context 封装/适配为该接口,保证 core 层渲染逻辑的统一调用。
124-
- 具体接口定义见 `interface/CanvasContext.ts`,如需适配新平台,请实现该接口。
125-
126-
## 开发指南
127-
128-
### 新增图表类型
129-
130-
-`core/chart/` 下创建新的渲染器类,**需继承 `BaseRenderer`**,实现通用渲染逻辑。
131-
-`core/factory.ts` 中注册新图表类型。
132-
-`core/types/extra.ts``core/types/series.ts` 中添加扩展配置类型。
133-
- 更新文档和示例。
134-
135-
### 新增平台适配层
136-
137-
1.`adapters/` 下新建平台目录(如 `myplatform/`)。
138-
2. 实现统一适配接口,封装平台 canvas、事件等能力。
139-
3. 在平台入口文件(如 `index.ts`)中暴露适配能力。
140-
4. 如有需要暴露给用户层的适配层类型,在 `interface/` 下新建对应平台目录并导出。
141-
5. 更新文档说明。
142-
143-
## 配置选项
144-
145-
详见 `core/types/` 目录下类型定义,支持丰富的通用与扩展配置。
146-
147-
## 类型与接口统一导出
148-
149-
为方便用户开发,所有类型和接口均已在 `UCharts/interface` 统一导出。无论是图表配置类型、CanvasContext 适配类型,还是平台相关类型,均可通过如下方式导入:
100+
## 适配说明
150101

151-
```ts
152-
import type { ChartOptions, CanvasContext } from 'UCharts/interface';
153-
```
154-
155-
如需使用平台专属类型,可从 `UCharts/interface/harmony``UCharts/interface/wechat` 等路径导入。例如:
156-
157-
```ts
158-
import type { HarmonyCanvasContext } from 'UCharts/interface/harmony';
159-
import type { WechatCanvasContext } from 'UCharts/interface/wechat';
160-
```
161-
162-
普通用户只需用 `UCharts/interface` 统一类型即可,平台专属类型仅在需要平台能力扩展时使用。
163-
164-
## 性能优化
165-
166-
- **底层渲染优化**:TypeScript 实现,便于多端编译优化
167-
- **动画与事件分离**:动画、事件系统独立,提升流畅度
168-
- **按需加载**:仅加载所需图表类型和适配层
169-
170-
## 兼容性
171-
172-
- **鸿蒙系统**:支持 HarmonyOS 5.0 及以上
173-
- **浏览器**:支持浏览器等H5运行环境
174-
- **微信小程序**:支持主流小程序平台(适配中...)
175-
- **uniapp**:支持主流 uniapp 运行环境(适配中...)
102+
- 本库专为 HarmonyOS 平台适配,充分利用鸿蒙 Canvas 绘图能力。
103+
- 如需自定义扩展,可参考 adapters/platform/harmony 目录下的适配代码。
176104

177105
## 链接
178106
- [Github](https://github.com/junbin-yang/uCharts-v3)
179107
- [Gitee](https://gitee.com/uCharts/uCharts-v3)
180108

109+
## 问题反馈
110+
111+
如在使用过程中遇到问题,欢迎通过 GitHub Issues 反馈,或加入官方交流群获取支持。
112+
181113
## 许可证
182114

183115
本项目采用 **Apache License 2.0** 开源协议。

adapters/platform/harmony/entry/src/main/ets/pages/ColumnChart.ets

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { IBestButton, IBestDialogUtil, IBestNavBar } from "@ibestservices/ibest-
22
import RouterUtil from "./common/utils"
33
import ComponentShowContainer from "./ComponentShowContainer"
44
import { LengthMetrics } from '@ohos.arkui.node'
5-
import { ChartOptions, Series, UCharts, UChartsController } from '@ibestservices/ucharts'
5+
import { Series, UCharts, UChartsController, UChartsControllerV2, UChartsV2 } from '@ibestservices/ucharts'
66
import { chartDemoType } from "./common/type"
77
import { CollapseAndExpand } from "./CollapseAndExpand"
88
import { GetColumnDemoCodeStr } from "./common/desc"
@@ -20,6 +20,32 @@ export struct ColumnChart {
2020
this.icon = Param.icon as Resource
2121
}
2222

23+
@State easyChart:UChartsController = new UChartsController({
24+
type: "column",
25+
categories: ["2018","2019","2020","2021"],
26+
series: [
27+
{
28+
name: "目标",
29+
data: [35,36,31,33]
30+
},
31+
{
32+
name: "完成",
33+
data: [18,27,21,24]
34+
}
35+
],
36+
padding: [15,15,0,5],
37+
xAxis: { disableGrid: true },
38+
yAxis: { data: [{ min: 0 }] },
39+
extra: {
40+
column: {
41+
type: "group",
42+
width: 30,
43+
activeBgColor: "#000000",
44+
activeBgOpacity: 0.08
45+
}
46+
}
47+
})
48+
2349
build() {
2450
NavDestination() {
2551
IBestNavBar({
@@ -31,6 +57,17 @@ export struct ColumnChart {
3157
}
3258
})
3359
List() {
60+
ListItem() {
61+
ComponentShowContainer({title: "简单用法"}) {
62+
Flex({
63+
wrap: FlexWrap.Wrap,
64+
space: { main: LengthMetrics.vp(12), cross: LengthMetrics.vp(12) }
65+
}) {
66+
UCharts({controller: this.easyChart}).height(260)
67+
CollapseAndExpand({ value: GetColumnDemoCodeStr(-1) })
68+
}
69+
}
70+
}
3471
ForEach(this.demoData, (item: chartDemoType, index: number)=>{
3572
ListItem() {
3673
ComponentShowContainer({title: item.title}) {
@@ -277,4 +314,73 @@ const SetColumnChartDemo = () => {
277314
ctl: new UChartsController()
278315
},
279316
] as chartDemoType[]
317+
}
318+
319+
320+
@ComponentV2
321+
export struct ColumnChartV2 {
322+
@Local title: string = ""
323+
@Local icon: Resource = $r("app.media.column")
324+
325+
aboutToAppear(): void {
326+
const Param: Record<string, object> = RouterUtil.navPathStack.getParamByName("column_v2")[0] as Record<string, object>
327+
const title_param: Record<string, number> = Param.name as Record<string, number>
328+
this.title = getContext().resourceManager.getStringSync(title_param.id)
329+
this.icon = Param.icon as Resource
330+
}
331+
332+
@Local easyChart:UChartsControllerV2 = new UChartsControllerV2({
333+
type: "column",
334+
categories: ["2018","2019","2020","2021"],
335+
series: [
336+
{
337+
name: "目标",
338+
data: [35,36,31,33]
339+
},
340+
{
341+
name: "完成",
342+
data: [18,27,21,24]
343+
}
344+
],
345+
padding: [15,15,0,5],
346+
xAxis: { disableGrid: true },
347+
yAxis: { data: [{ min: 0 }] },
348+
extra: {
349+
column: {
350+
type: "group",
351+
width: 30,
352+
activeBgColor: "#000000",
353+
activeBgOpacity: 0.08
354+
}
355+
}
356+
})
357+
358+
build() {
359+
NavDestination() {
360+
IBestNavBar({
361+
title: this.title,
362+
titleIcon: this.icon,
363+
isShowStatusBar: true,
364+
onLeftClick: () => {
365+
RouterUtil.pop()
366+
}
367+
})
368+
List() {
369+
ListItem() {
370+
ComponentShowContainer({title: ""}) {
371+
Flex({
372+
wrap: FlexWrap.Wrap,
373+
space: { main: LengthMetrics.vp(12), cross: LengthMetrics.vp(12) }
374+
}) {
375+
UChartsV2({controller: this.easyChart}).height(260)
376+
CollapseAndExpand({ value: GetColumnDemoCodeStr(-2) })
377+
}
378+
}
379+
}
380+
}
381+
.layoutWeight(1)
382+
.padding({ left: 12, right: 12 })
383+
}.hideTitleBar(true)
384+
.backgroundColor("#f7f8fa")
385+
}
280386
}

adapters/platform/harmony/entry/src/main/ets/pages/Index.ets

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { bundleManager } from '@kit.AbilityKit';
22
import { router, window } from '@kit.ArkUI';
33
import { IBestCell, IBestCellGroup, IBestNoticeBar } from '@ibestservices/ibest-ui';
4-
import { ColumnChart } from './ColumnChart';
4+
import { ColumnChart, ColumnChartV2 } from './ColumnChart';
55
import RouterUtil from './common/utils';
66
import { chartListType, Tab } from './common/type';
77
import { MountChart } from './MountChart';
@@ -72,6 +72,7 @@ struct Index {
7272
}
7373

7474
readonly chartList: chartListType [] = [
75+
{ key: "column_v2", name: $r("app.string.charts_stateV2"), icon: $r("app.media.column") },
7576
{ key: "column", name: $r("app.string.charts_column"), icon: $r("app.media.column") },
7677
{ key: "mount", name: $r("app.string.charts_mount"), icon: $r("app.media.mount") },
7778
{ key: "bar", name: $r("app.string.charts_bar"), icon: $r("app.media.bar") },
@@ -217,7 +218,9 @@ struct Index {
217218

218219
@Builder
219220
PageMap(name: string) {
220-
if(name === 'column') {
221+
if(name === 'column_v2') {
222+
ColumnChartV2()
223+
} else if(name === 'column') {
221224
ColumnChart()
222225
} else if(name === 'mount') {
223226
MountChart()

0 commit comments

Comments
 (0)