Skip to content

Commit

Permalink
fix: <rect> attribute width bug
Browse files Browse the repository at this point in the history
Hidden elements have zero width, subtracting offset would make the width negative.
These negative values would cascade down to component creation
  • Loading branch information
scmmishra committed Jun 30, 2020
1 parent 136b914 commit d3eabea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/js/charts/BaseChart.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import SvgTip from '../objects/SvgTip';
import { $, isElementInViewport, getElementContentWidth } from '../utils/dom';
import { $, isElementInViewport, getElementContentWidth, isHidden } from '../utils/dom';
import { makeSVGContainer, makeSVGDefs, makeSVGGroup, makeText } from '../utils/draw';
import { BASE_MEASURES, getExtraHeight, getExtraWidth, getTopOffset, getLeftOffset,
INIT_CHART_UPDATE_TIMEOUT, CHART_POST_ANIMATE_TIMEOUT, DEFAULT_COLORS} from '../utils/constants';
Expand Down Expand Up @@ -134,6 +134,10 @@ export default class BaseChart {
bindTooltip() {}

draw(onlyWidthChange=false, init=false) {
if (onlyWidthChange && isHidden(this.parent)) {
// Don't update anything if the chart is hidden
return;
}
this.updateWidth();

this.calc(onlyWidthChange);
Expand Down
7 changes: 7 additions & 0 deletions src/js/utils/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ export function getOffset(element) {
};
}

// https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetParent
// an element's offsetParent property will return null whenever it, or any of its parents,
// is hidden via the display style property.
export function isHidden(el) {
return (el.offsetParent === null);
}

export function isElementInViewport(el) {
// Although straightforward: https://stackoverflow.com/a/7557433/6495043
var rect = el.getBoundingClientRect();
Expand Down

0 comments on commit d3eabea

Please sign in to comment.