Skip to content

Commit 6efcd0c

Browse files
authored
Merge pull request #4322 from VisActor/doc/theme-usage
docs: add theme guid
2 parents b9513e4 + 5928eef commit 6efcd0c

File tree

5 files changed

+1321
-2
lines changed

5 files changed

+1321
-2
lines changed

docs/assets/api/en/API/vchart.md

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,9 @@ getCurrentThemeName: () => string;
763763

764764
### setCurrentTheme
765765

766-
**asynchronous method**, set the current Theme.
766+
**Asynchronous method**, set the current Theme.
767+
768+
**Note: If the `theme` property is configured in the chart spec, the `spec.theme` has higher priority.**
767769

768770
```ts
769771
/**
@@ -774,6 +776,66 @@ getCurrentThemeName: () => string;
774776
setCurrentTheme: (name: string) => Promise<IVChart>;
775777
```
776778

779+
Usage example:
780+
781+
```javascript livedemo
782+
// Define custom theme
783+
const customTheme = {
784+
colorScheme: {
785+
default: [
786+
'#FF6B6B',
787+
'#4ECDC4',
788+
'#45B7D1',
789+
'#FFA07A',
790+
'#98D8C8',
791+
'#F7DC6F',
792+
'#BB8FCE',
793+
'#85C1E2'
794+
]
795+
},
796+
series: {
797+
bar: {
798+
barMaxWidth: 20,
799+
label: {
800+
visible: true,
801+
position: 'top'
802+
}
803+
}
804+
}
805+
};
806+
807+
// Register theme
808+
VChart.ThemeManager.registerTheme('myCustomTheme', customTheme);
809+
810+
const spec = {
811+
type: 'bar',
812+
data: [
813+
{
814+
id: 'barData',
815+
values: [
816+
{ type: 'A', value: 120 },
817+
{ type: 'B', value: 200 },
818+
{ type: 'C', value: 150 },
819+
{ type: 'D', value: 80 }
820+
]
821+
}
822+
],
823+
xField: 'type',
824+
yField: 'value'
825+
};
826+
827+
const vchart = new VChart(spec, { dom: CONTAINER_ID });
828+
vchart.renderSync();
829+
830+
// Set theme through instance method
831+
vchart.setCurrentTheme('myCustomTheme');
832+
```
833+
834+
**Note**:
835+
- Before using the `setCurrentTheme` method, you need to register the theme first through `VChart.ThemeManager.registerTheme()`
836+
- This method is asynchronous and returns a Promise, you can use `await` or `.then()` to handle it
837+
- If the `theme` property is configured in the chart spec, the theme in the spec has higher priority
838+
777839
### setTooltipHandler
778840

779841
When the configuration item cannot meet the display needs of the tooltip, we also provide the ability to customize the tooltip. You can configure `TooltipHandler` To override the default tooltip presentation logic.

docs/assets/api/zh/API/vchart.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,66 @@ getCurrentThemeName: () => string;
787787
setCurrentTheme: (name: string) => Promise<IVChart>;
788788
```
789789

790+
使用示例:
791+
792+
```javascript livedemo
793+
// 定义自定义主题
794+
const customTheme = {
795+
colorScheme: {
796+
default: [
797+
'#FF6B6B',
798+
'#4ECDC4',
799+
'#45B7D1',
800+
'#FFA07A',
801+
'#98D8C8',
802+
'#F7DC6F',
803+
'#BB8FCE',
804+
'#85C1E2'
805+
]
806+
},
807+
series: {
808+
bar: {
809+
barMaxWidth: 20,
810+
label: {
811+
visible: true,
812+
position: 'top'
813+
}
814+
}
815+
}
816+
};
817+
818+
// 注册主题
819+
VChart.ThemeManager.registerTheme('myCustomTheme', customTheme);
820+
821+
const spec = {
822+
type: 'bar',
823+
data: [
824+
{
825+
id: 'barData',
826+
values: [
827+
{ type: 'A', value: 120 },
828+
{ type: 'B', value: 200 },
829+
{ type: 'C', value: 150 },
830+
{ type: 'D', value: 80 }
831+
]
832+
}
833+
],
834+
xField: 'type',
835+
yField: 'value'
836+
};
837+
838+
const vchart = new VChart(spec, { dom: CONTAINER_ID });
839+
vchart.renderSync();
840+
841+
// 通过实例方法设置主题
842+
vchart.setCurrentTheme('myCustomTheme');
843+
```
844+
845+
**注意**
846+
- 使用 `setCurrentTheme` 方法前,需要先通过 `VChart.ThemeManager.registerTheme()` 注册主题
847+
- 该方法为异步方法,返回 Promise,可以使用 `await``.then()` 处理
848+
- 如果图表 spec 中配置了 `theme` 属性,则 spec 中的主题优先级更高
849+
790850
### setTooltipHandler
791851

792852
当配置项无法满足 tooltip 的展示需求时,我们还提供了自定义 tooltip 的能力。可以通过配置 `TooltipHandler` 来覆盖默认 tooltip 展示逻辑。

0 commit comments

Comments
 (0)