Skip to content

Commit

Permalink
fix(#2264): 修复 canvas 样式导致的 resize 问题, 修复容器层级
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcc authored and simaQ committed Apr 3, 2020
1 parent e6c8fd9 commit 1020085
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/chart/chart.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { debounce, each, isString } from '@antv/util';
import { debounce, each, isString, } from '@antv/util';

import { ChartCfg } from '../interface';

import { GROUP_Z_INDEX } from '../constant';

import { getEngine } from '../engine';
import { createDom, getChartSize, removeDom } from '../util/dom';
import { createDom, getChartSize, removeDom, modifyCSS } from '../util/dom';
import View from './view';

/**
Expand Down Expand Up @@ -58,7 +58,7 @@ export default class Chart extends View {
const G = getEngine(renderer);

const canvas = new G.Canvas({
container: ele,
container: wrapperElement,
pixelRatio,
localRefresh,
...size,
Expand Down Expand Up @@ -89,6 +89,7 @@ export default class Chart extends View {
this.wrapperElement = wrapperElement;

// 自适应大小
this.updateCanvasStyle();
this.bindAutoFit();
this.initDefaultInteractions(defaultInteractions);
}
Expand Down Expand Up @@ -150,6 +151,13 @@ export default class Chart extends View {
this.changeSize(width, height);
}

private updateCanvasStyle() {
modifyCSS(this.canvas.get('el'), {
display: 'inline-block',
verticalAlign: 'middle',
});
}

private bindAutoFit() {
if (this.autoFit) {
window.addEventListener('resize', this.onResize);
Expand Down
2 changes: 1 addition & 1 deletion src/util/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ export function removeDom(dom: HTMLElement) {
}

/** @ignore */
export { createDom } from '@antv/dom-util';
export { createDom, modifyCSS } from '@antv/dom-util';
32 changes: 32 additions & 0 deletions tests/bugs/2264-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Chart } from '../../src';
import { createDiv } from '../util/dom';

describe('#2264', () => {
const div = createDiv();
div.style.height = '300px';
const data = [
{ year: '1991', value: 3 },
{ year: '1992', value: 4 },
{ year: '1993', value: 5 },
];
const chart = new Chart({
container: div,
width: 400,
height: 100,
autoFit: true,
});

chart.data(data);
chart.interval().position('year*value');

chart.render();

it('canvas height === container size', async () => {
expect(getComputedStyle(chart.canvas.get('el')).height).toBe(getComputedStyle(div.querySelector('div')).height);
});

it('g2 div 层级结构', () => {
expect(chart.canvas.get('el').parentNode).toBe(div.querySelector('div'));
expect(chart.canvas.get('el').parentNode.parentNode).toBe(div);
})
});

0 comments on commit 1020085

Please sign in to comment.