Commit a7e485a0 authored by Masayuki Tanaka's avatar Masayuki Tanaka

Add destory() as destructor and fix duplicated chart generation - #6

parent bb469770
...@@ -903,7 +903,6 @@ ...@@ -903,7 +903,6 @@
var firstDate = null, lastDate = null, orgXDomain; var firstDate = null, lastDate = null, orgXDomain;
function init(data) { function init(data) {
var targets = c3.data.targets = convertDataToTargets(data);
var grid, xgridLine; var grid, xgridLine;
var i; var i;
...@@ -911,8 +910,13 @@ ...@@ -911,8 +910,13 @@
if (selectChart.empty()) { if (selectChart.empty()) {
window.alert('No bind element found. Check the selector specified by "bindto" and existance of that element. Default "bindto" is "#chart".'); window.alert('No bind element found. Check the selector specified by "bindto" and existance of that element. Default "bindto" is "#chart".');
return; return;
} else {
selectChart.html("");
} }
c3.data.x = undefined;
c3.data.targets = convertDataToTargets(data);
// TODO: set names if names not specified // TODO: set names if names not specified
// Init sizes and scales // Init sizes and scales
...@@ -1326,10 +1330,10 @@ ...@@ -1326,10 +1330,10 @@
/*-- Legend Region --*/ /*-- Legend Region --*/
if (__legend_show) { updateLegend(targets); } if (__legend_show) { updateLegend(c3.data.targets); }
// Set targets // Set targets
updateTargets(targets); updateTargets(c3.data.targets);
// Draw with targets // Draw with targets
redraw({withTransition: false, withUpdateXDomain: true}); redraw({withTransition: false, withUpdateXDomain: true});
...@@ -1338,18 +1342,23 @@ ...@@ -1338,18 +1342,23 @@
if (__tooltip_init_show) { if (__tooltip_init_show) {
if (isTimeSeries && typeof __tooltip_init_x === 'string') { if (isTimeSeries && typeof __tooltip_init_x === 'string') {
__tooltip_init_x = parseDate(__tooltip_init_x); __tooltip_init_x = parseDate(__tooltip_init_x);
for (i = 0; i < targets[0].values.length; i++) { for (i = 0; i < c3.data.targets[0].values.length; i++) {
if ((targets[0].values[i].x - __tooltip_init_x) === 0) { break; } if ((c3.data.targets[0].values[i].x - __tooltip_init_x) === 0) { break; }
} }
__tooltip_init_x = i; __tooltip_init_x = i;
} }
tooltip.html(__tooltip_contents(targets.map(function (d) { tooltip.html(__tooltip_contents(c3.data.targets.map(function (d) {
return addName(d.values[__tooltip_init_x]); return addName(d.values[__tooltip_init_x]);
}))); })));
tooltip.style("top", __tooltip_init_position.top) tooltip.style("top", __tooltip_init_position.top)
.style("left", __tooltip_init_position.left) .style("left", __tooltip_init_position.left)
.style("visibility", "visible"); .style("visibility", "visible");
} }
// Bind resize event
if (window.onresize == null) {
window.onresize = resize;
}
} }
function redraw(options) { function redraw(options) {
...@@ -2009,6 +2018,13 @@ ...@@ -2009,6 +2018,13 @@
return targets.length > 0 ? targets[0] : undefined; return targets.length > 0 ? targets[0] : undefined;
}; };
c3.destroy = function () {
c3.data.targets = undefined;
c3.data.x = undefined;
selectChart.html("");
window.onresize = null;
};
/*-- Load data and init chart with defined functions --*/ /*-- Load data and init chart with defined functions --*/
if ('url' in config.data) { if ('url' in config.data) {
...@@ -2024,9 +2040,6 @@ ...@@ -2024,9 +2040,6 @@
throw Error('url or rows or columns is required.'); throw Error('url or rows or columns is required.');
} }
// Bind resize event
window.onresize = resize;
return c3; return c3;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment