From 52ab79c24e7e7bb7fe9b314d3440abed30a5025e Mon Sep 17 00:00:00 2001 From: Grace Guo Date: Wed, 4 Dec 2019 16:01:00 -0800 Subject: [PATCH] fix: JS exception: Cannot read property "dispatch" of undefined (#287) --- plugins/superset-ui-plugins/.gitignore | 1 + .../src/NVD3Vis.js | 82 ++++++++++--------- 2 files changed, 45 insertions(+), 38 deletions(-) diff --git a/plugins/superset-ui-plugins/.gitignore b/plugins/superset-ui-plugins/.gitignore index 168f2efa81..bd5deaaad2 100644 --- a/plugins/superset-ui-plugins/.gitignore +++ b/plugins/superset-ui-plugins/.gitignore @@ -14,6 +14,7 @@ *.log *.map *.min.js +.*.swp babel.config.js build/ diff --git a/plugins/superset-ui-plugins/packages/superset-ui-legacy-preset-chart-nvd3/src/NVD3Vis.js b/plugins/superset-ui-plugins/packages/superset-ui-legacy-preset-chart-nvd3/src/NVD3Vis.js index 0dc8f167ec..90df0c150c 100644 --- a/plugins/superset-ui-plugins/packages/superset-ui-legacy-preset-chart-nvd3/src/NVD3Vis.js +++ b/plugins/superset-ui-plugins/packages/superset-ui-legacy-preset-chart-nvd3/src/NVD3Vis.js @@ -454,14 +454,16 @@ function nvd3Vis(element, props) { } if (canShowBrush && onBrushEnd !== NOOP) { - chart.focus.dispatch.on('brush', event => { - const timeRange = stringifyTimeRange(event.extent); - if (timeRange) { - event.brush.on('brushend', () => { - onBrushEnd(timeRange); - }); - } - }); + if (chart.focus) { + chart.focus.dispatch.on('brush', event => { + const timeRange = stringifyTimeRange(event.extent); + if (timeRange) { + event.brush.on('brushend', () => { + onBrushEnd(timeRange); + }); + } + }); + } } if (chart.xAxis && chart.xAxis.staggerLabels) { @@ -970,22 +972,24 @@ function nvd3Vis(element, props) { } // update annotation positions on brush event - chart.focus.dispatch.on('onBrush.event-annotation', () => { - annotations - .selectAll('line') - .data(records) - .attr({ - x1: d => xScale(new Date(d[e.timeColumn])), - y1: 0, - x2: d => xScale(new Date(d[e.timeColumn])), - y2: annotationHeight, - opacity: d => { - const x = xScale(new Date(d[e.timeColumn])); - - return x > 0 && x < chartWidth ? 1 : 0; - }, - }); - }); + if (chart.focus) { + chart.focus.dispatch.on('onBrush.event-annotation', () => { + annotations + .selectAll('line') + .data(records) + .attr({ + x1: d => xScale(new Date(d[e.timeColumn])), + y1: 0, + x2: d => xScale(new Date(d[e.timeColumn])), + y2: annotationHeight, + opacity: d => { + const x = xScale(new Date(d[e.timeColumn])); + + return x > 0 && x < chartWidth ? 1 : 0; + }, + }); + }); + } }); // Interval annotations @@ -1059,20 +1063,22 @@ function nvd3Vis(element, props) { } // update annotation positions on brush event - chart.focus.dispatch.on('onBrush.interval-annotation', () => { - annotations - .selectAll('rect') - .data(records) - .attr({ - x: d => xScale(new Date(d[e.timeColumn])), - width: d => { - const x1 = xScale(new Date(d[e.timeColumn])); - const x2 = xScale(new Date(d[e.intervalEndColumn])); - - return x2 - x1; - }, - }); - }); + if (chart.focus) { + chart.focus.dispatch.on('onBrush.interval-annotation', () => { + annotations + .selectAll('rect') + .data(records) + .attr({ + x: d => xScale(new Date(d[e.timeColumn])), + width: d => { + const x1 = xScale(new Date(d[e.timeColumn])); + const x2 = xScale(new Date(d[e.intervalEndColumn])); + + return x2 - x1; + }, + }); + }); + } }); }