From 8ef424bf6a69c951e5b4f3efafc77670842ab607 Mon Sep 17 00:00:00 2001 From: Victor Tang Date: Tue, 3 Oct 2017 20:40:07 -0700 Subject: [PATCH] fix destroy function to prevent null error during pie animation --- src/api.chart.js | 18 +++++++++++++++++- src/arc.js | 2 +- src/interaction.js | 4 ++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/api.chart.js b/src/api.chart.js index a06381a..8bf9b53 100644 --- a/src/api.chart.js +++ b/src/api.chart.js @@ -14,6 +14,9 @@ c3_chart_fn.flush = function () { c3_chart_fn.destroy = function () { var $$ = this.internal; + var defaultConfig = $$.getDefaultConfig(); + //subsequent checks to config should check this instead of !config + defaultConfig.destroyed = true; window.clearInterval($$.intervalForObserveInserted); @@ -37,7 +40,20 @@ c3_chart_fn.destroy = function () { // MEMO: this is needed because the reference of some elements will not be released, then memory leak will happen. Object.keys($$).forEach(function (key) { - $$[key] = null; + if(key === "config") + { + $$[key] = defaultConfig; + } + else if(key === "data") + { + $$[key] = { + targets: [] + }; + } + else + { + $$[key] = null; + } }); return null; diff --git a/src/arc.js b/src/arc.js index 7b5c16e..e004dac 100644 --- a/src/arc.js +++ b/src/arc.js @@ -31,7 +31,7 @@ c3_chart_internal_fn.updateAngle = function (d) { found = false, index = 0, gMin, gMax, gTic, gValue; - if (!config) { + if (config.destroyed) { // chart is destroyed return null; } diff --git a/src/interaction.js b/src/interaction.js index a51f6de..a780351 100644 --- a/src/interaction.js +++ b/src/interaction.js @@ -133,7 +133,7 @@ c3_chart_internal_fn.generateEventRectsForSingleX = function (eventRectEnter) { }) .on('mouseout', function (d) { var index = d.index; - if (!$$.config) { return; } // chart is destroyed + if ($$.config.destroyed) { return; } // chart is destroyed if ($$.hasArcType()) { return; } $$.hideXGridFocus(); $$.hideTooltip(); @@ -245,7 +245,7 @@ c3_chart_internal_fn.generateEventRectsForMultipleXs = function (eventRectEnter) .attr('height', $$.height) .attr('class', CLASS.eventRect) .on('mouseout', function () { - if (!$$.config) { return; } // chart is destroyed + if ($$.config.destroyed) { return; } // chart is destroyed if ($$.hasArcType()) { return; } mouseout(); })