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

fix: 双轴图 theme 主题设置 #2451

Merged
merged 1 commit into from
Mar 24, 2021
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
66 changes: 66 additions & 0 deletions __tests__/unit/plots/dual-axes/theme-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { DualAxes, G2 } from '../../../../src';
import { PV_DATA, UV_DATA } from '../../../data/pv-uv';
import { createDiv } from '../../../utils/dom';

G2.registerTheme('my-theme', {
components: {
axis: {
left: {
line: {
style: {
stroke: '#5B8FF9',
},
},
},
right: {
line: {
style: {
stroke: '#5B8FF9',
},
},
},
},
legend: {
top: {
itemName: {
style: {
fill: '#5B8FF9',
},
},
},
},
},
});

describe('DualAxes theme', () => {
it('theme', () => {
const yField = ['pv', 'uv'];
const dualAxes = new DualAxes(createDiv(), {
width: 400,
height: 500,
data: [PV_DATA, UV_DATA],
xField: 'date',
yField,
theme: 'my-theme',
});

dualAxes.render();

// test legend
expect(dualAxes.chart.getTheme().components.legend.top.itemName.style.fill).toBe('#5B8FF9');

const legend = dualAxes.chart.getComponents().find((i) => i.type === 'legend');
expect(legend.component.cfg.itemName.style.fill).toBe('#5B8FF9');

const [leftView, rightView] = dualAxes.chart.views;
const leftViewAxis = leftView.getComponents().find((i) => i.type === 'axis' && i.direction === 'left');
expect(leftView.getTheme().components.axis.left.line.style.stroke).toBe('#5B8FF9');
expect(leftViewAxis.component.cfg.line.style.stroke).toBe('#5B8FF9');

const rightViewAxis = rightView.getComponents().find((i) => i.type === 'axis' && i.direction === 'right');
expect(rightView.getTheme().components.axis.right.line.style.stroke).toBe('#5B8FF9');
expect(rightViewAxis.component.cfg.line.style.stroke).toBe('#5B8FF9');

dualAxes.destroy();
});
});
16 changes: 15 additions & 1 deletion src/plots/dual-axes/adaptor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { each, findIndex, get, find, isObject, every, isEqual, isBoolean } from '@antv/util';
import { Scale, Types, Event } from '@antv/g2';
import {
theme,
theme as commonTheme,
animation as commonAnimation,
scale,
interaction as commonInteraction,
Expand Down Expand Up @@ -282,6 +282,20 @@ export function annotation(params: Params<DualAxesOptions>): Params<DualAxesOpti
return params;
}

export function theme(params: Params<DualAxesOptions>): Params<DualAxesOptions> {
const { chart } = params;

/*
* 双轴图中,部分组件是绘制在子 view 层(例如 axis,line),部分组件是绘制在 chart (例如 legend)
* 为 chart 和 子 view 均注册 theme,使其自行遵循 G2 theme geometry > view > chart 进行渲染。
*/
commonTheme(deepAssign({}, params, { chart: findViewById(chart, LEFT_AXES_VIEW) }));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里有一个小小的隐患就是,view 会不会不存在

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个只要 g2 能正常创建出来,就会存在,反过来说,如果 view 没创建成功,整个 adaptor 里都得做层判断,感觉只在这个函数里判断意义不是很大:)

commonTheme(deepAssign({}, params, { chart: findViewById(chart, RIGHT_AXES_VIEW) }));
commonTheme(params);
visiky marked this conversation as resolved.
Show resolved Hide resolved

return params;
}

export function animation(params: Params<DualAxesOptions>): Params<DualAxesOptions> {
const { chart } = params;

Expand Down