Skip to content

Commit 462c2d6

Browse files
committed
fix(core): Use a minimum size for all charts, and use resizeObserver rather than requestAnimationFra
1 parent 6899e35 commit 462c2d6

File tree

4 files changed

+38
-28
lines changed

4 files changed

+38
-28
lines changed

Diff for: packages/core/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
],
2828
"author": "IBM",
2929
"license": "Apache-2.0",
30+
"dependencies": {
31+
"resize-observer-polyfill": "1.5.0"
32+
},
3033
"peerDependencies": {
3134
"d3": ">=4.11.0 <=5.7.0"
3235
},

Diff for: packages/core/src/base-axis-chart.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ export class BaseAxisChart extends BaseChart {
165165
width: (container.node().clientWidth - marginsToExclude) * ratio
166166
};
167167

168-
return computedChartSize;
168+
return {
169+
height: Math.max(computedChartSize.height, Configuration.charts.axisCharts.minHeight),
170+
width: Math.max(computedChartSize.width, Configuration.charts.axisCharts.minWidth)
171+
};
169172
}
170173

171174
resizeChart() {

Diff for: packages/core/src/base-chart.ts

+25-25
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ import {
88
import { scaleOrdinal } from "d3-scale";
99
import { transition, Transition } from "d3-transition";
1010

11+
// Internal Imports
1112
import * as Configuration from "./configuration";
1213
import { Tools } from "./tools";
1314
import PatternsService from "./services/patterns";
1415

16+
// Misc
17+
import ResizeObserver from "resize-observer-polyfill";
18+
1519
export class BaseChart {
1620
static chartCount = 1;
1721

@@ -210,8 +214,6 @@ export class BaseChart {
210214

211215
// TODO - Refactor
212216
getChartSize(container = this.container) {
213-
const noAxis = this.options.type === "pie" || this.options.type === "donut";
214-
215217
let ratio, marginForLegendTop;
216218
if (container.node().clientWidth > Configuration.charts.widthBreak) {
217219
ratio = Configuration.charts.magicRatio;
@@ -222,24 +224,20 @@ export class BaseChart {
222224
}
223225

224226
// Store computed actual size, to be considered for change if chart does not support axis
225-
const marginsToExclude = noAxis ? 0 : (Configuration.charts.margin.left + Configuration.charts.margin.right);
227+
const marginsToExclude = 0;
226228
const computedChartSize = {
227229
height: container.node().clientHeight - marginForLegendTop,
228230
width: (container.node().clientWidth - marginsToExclude) * ratio
229231
};
230232

231233
// If chart is of type pie or donut, width and height should equal to the min of the width and height computed
232-
if (noAxis) {
233-
let maxSizePossible = Math.min(computedChartSize.height, computedChartSize.width);
234-
maxSizePossible = Math.max(maxSizePossible, Configuration.pie.minWidth);
235-
236-
return {
237-
height: maxSizePossible,
238-
width: maxSizePossible
239-
};
240-
}
234+
let maxSizePossible = Math.min(computedChartSize.height, computedChartSize.width);
235+
maxSizePossible = Math.max(maxSizePossible, Configuration.charts.minWidth);
241236

242-
return computedChartSize;
237+
return {
238+
height: maxSizePossible,
239+
width: maxSizePossible
240+
};
243241
}
244242

245243
/*
@@ -299,21 +297,23 @@ export class BaseChart {
299297
resizeWhenContainerChange() {
300298
let containerWidth = this.holder.clientWidth;
301299
let containerHeight = this.holder.clientHeight;
302-
const frame = () => {
303-
if (Math.abs(containerWidth - this.holder.clientWidth) > 1
304-
|| Math.abs(containerHeight - this.holder.clientHeight) > 1) {
305-
containerWidth = this.holder.clientWidth;
306-
containerHeight = this.holder.clientHeight;
307300

308-
selectAll(".legend-tooltip").style("display", "none");
301+
const resizeObserver = new ResizeObserver((entries, observer) => {
302+
for (const entry of entries) {
303+
if (Math.abs(containerWidth - this.holder.clientWidth) > 1
304+
|| Math.abs(containerHeight - this.holder.clientHeight) > 1) {
305+
containerWidth = this.holder.clientWidth;
306+
containerHeight = this.holder.clientHeight;
309307

310-
this.hideTooltip();
311-
this.resizeChart();
312-
}
308+
selectAll(".legend-tooltip").style("display", "none");
313309

314-
requestAnimationFrame(frame);
315-
};
316-
requestAnimationFrame(frame);
310+
this.hideTooltip();
311+
this.resizeChart();
312+
}
313+
}
314+
});
315+
316+
resizeObserver.observe(this.holder);
317317
}
318318

319319
setClickableLegend() {

Diff for: packages/core/src/configuration.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,15 @@ export const charts = {
8888
width: 20,
8989
height: 20
9090
},
91+
minWidth: 150,
9192
widthBreak: 600,
9293
marginForLegendTop: 40,
9394
magicRatio: 0.7,
94-
magicMoreForY2Axis: 70
95+
magicMoreForY2Axis: 70,
96+
axisCharts: {
97+
minWidth: 100,
98+
minHeight: 200
99+
}
95100
};
96101

97102
export const scales = {
@@ -176,7 +181,6 @@ export const lines = {
176181
};
177182

178183
export const pie = {
179-
minWidth: 100,
180184
maxWidth: 516.6,
181185
mouseover: {
182186
strokeWidth: 6,

0 commit comments

Comments
 (0)