Skip to content
This repository was archived by the owner on Nov 2, 2021. It is now read-only.

Commit c2c7afb

Browse files
committed
fix height resize problem (issue#13)
1 parent 1775726 commit c2c7afb

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

src/AbstractChart.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class AbstractChart {
2727
};
2828

2929
this.container = select(selector);
30+
// Enforce line-height = 0 to fix issue with height resizing
31+
// https://github.com/twitter/d3kit/issues/13
32+
this.container.style('line-height', 0);
3033

3134
const customEvents = this.constructor.getCustomEventNames();
3235
this.setupDispatcher(customEvents);

src/AbstractChart.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ describe('AbstractChart', () => {
2828
return ['custom1', 'custom2'];
2929
}
3030
}
31-
const chart = new Chart();
32-
expect(chart.getCustomEventNames()).to.deep.equal(['custom1', 'custom2']);
31+
const chart2 = new Chart();
32+
expect(chart2.getCustomEventNames()).to.deep.equal(['custom1', 'custom2']);
3333
});
3434
});
3535

src/SvgChart.spec.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ import { select } from 'd3-selection';
22
import SvgChart from './SvgChart.js';
33

44
describe('SvgChart', () => {
5-
let element, $element, $svg, chart;
5+
let element, $element, chart;
66

77
beforeEach(() => {
88
element = document.body.appendChild(document.createElement('div'));
99
chart = new SvgChart(element, null);
1010
$element = select(element);
11-
$svg = $element.select('svg');
1211
});
1312

1413
describe('new SvgChart(element, options)', () => {
@@ -68,39 +67,50 @@ describe('SvgChart', () => {
6867

6968
describe('.width(width)', () => {
7069
it('should return <svg> width when called without argument', () => {
71-
const w = $svg.attr('width');
70+
const w = chart.svg.attr('width');
7271
expect(chart.width()).to.equal(+w);
7372
});
7473
it('should set <svg> width when called with Number as the first argument', () => {
7574
chart
7675
.width(300)
7776
.updateDimensionNow();
78-
expect(+$svg.attr('width')).to.equal(300);
77+
expect(+chart.svg.attr('width')).to.equal(300);
7978
});
8079
});
8180

8281
describe('.height(height)', () => {
8382
it('should return <svg> height when called without argument', () => {
84-
const w = $svg.attr('height');
83+
const w = chart.svg.attr('height');
8584
expect(chart.height()).to.equal(+w);
8685
});
8786
it('should set <svg> height when called with Number as the first argument', () => {
8887
chart
8988
.height(300)
9089
.updateDimensionNow();
91-
expect(+$svg.attr('height')).to.equal(300);
90+
expect(+chart.svg.attr('height')).to.equal(300);
91+
});
92+
it('should override line-height and set <svg> height correctly', () => {
93+
const element2 = document.body.appendChild(document.createElement('div'));
94+
element2.style.lineHeight = 1;
95+
const chart2 = new SvgChart(element2);
96+
chart2
97+
.height(300)
98+
.updateDimensionNow();
99+
expect(+chart2.svg.attr('height')).to.equal(300);
100+
expect(+chart2.container.node().clientHeight).to.equal(300);
101+
expect(+element2.style.lineHeight).to.equal(0);
92102
});
93103
});
94104

95105
describe('.dimension(dimension)', () => {
96106
it('should return an array [width, height] when called without argument', () => {
97-
const dim = [+$svg.attr('width'), +$svg.attr('height')];
107+
const dim = [+chart.svg.attr('width'), +chart.svg.attr('height')];
98108
expect(chart.dimension()).to.deep.equal(dim);
99109
});
100110
it('should set width and height of the <svg> when called with an array [width, height] as the first argument', done => {
101111
chart.dimension([118, 118]);
102112
setTimeout(() => {
103-
expect([+$svg.attr('width'), +$svg.attr('height')]).to.deep.equal([118, 118]);
113+
expect([+chart.svg.attr('width'), +chart.svg.attr('height')]).to.deep.equal([118, 118]);
104114
done();
105115
}, 0);
106116
});

0 commit comments

Comments
 (0)