Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: custom theme #5750

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions __tests__/unit/api/theme.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Light, Dark, Classic, ClassicDark, Academy } from '../../../src';

describe('theme', () => {
it('should export all the themes', () => {
expect(Light).toBeDefined();
expect(Dark).toBeDefined();
expect(Classic).toBeDefined();
expect(ClassicDark).toBeDefined();
expect(Academy).toBeDefined();
});
});
45 changes: 34 additions & 11 deletions site/docs/manual/core/theme.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,45 @@ chart.theme({ type: 'light', ...theme });
})();
```

如果希望自定义所有的主题样式,可以新增一个主题注册,然后使用。
如果希望自定义所有的主题样式,可以新增一个主题、覆盖默认主题、注册,然后使用。

```js
import { register } from '@antv/g2';
```js | ob
(() => {
// 定义主题
function CustomTheme() {
const light = G2.Light();
return { ...light, color: 'red' };
}

const theme = {};
// 注册主题
G2.register('theme.custom', CustomTheme);

register('theme.custom', theme);
const chart = new G2.Chart();

// Spec 形式
const options = {
theme: { type: 'custom' },
};
chart.options({
type: 'interval',
data: {
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/fb9db6b7-23a5-4c23-bbef-c54a55fee580.csv',
},
encode: { x: 'letter', y: 'frequency' },
axis: { y: { labelFormatter: '.0%' } },
theme: { type: 'custom' }, // 使用主题
});

// API 形式
chart.theme({ type: 'custom' });
chart.render();

return chart.getContainer();
})();
```

包含的默认主题有:

- `G2.Light`
- `G2.Dark`
- `G2.Classic`
- `G2.ClassicDark`
- `G2.Academy`

完整的主题配置可以参考 [light](https://github.com/antvis/G2/blob/v5/src/theme/light.ts) 主题。
2 changes: 2 additions & 0 deletions src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ export { ChartEvent } from './utils/event';
export type { G2Context } from './runtime';

export * from './spec';

export { Light, Dark, Academy, Classic, ClassicDark } from './theme';
Loading