Skip to content

Commit

Permalink
Reuse root element ShadowRoot if possible.
Browse files Browse the repository at this point in the history
fixes #13
  • Loading branch information
huww98 committed Nov 12, 2020
1 parent 927809b commit c6a11f6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ const chart = new TimeChart(el, {

* `chart.dispose()`: Dispose all the resources used by this chart instance.
Note: We use shadow root to protect the chart from unintended style conflict. However, there is no easy way to remove the shadow root after dispose.
But you can reuse the same HTML element to create another TimeChart. [Example](https://huww98.github.io/TimeChart/demo/reset.html)

* `chart.onResize()`: Calculate size after layout changes.
This method is automatically called when window size changed.
Expand Down
45 changes: 45 additions & 0 deletions demo/reset.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>TimeChart Demo (Reset)</title>
</head>
<body>
<p>
Demostrate how to dispose a chart, and reuse the same HTML element to create another TimeChart.
</p>
<div id="chart" style="width: 100%; height: 640px;"></div>

<script src="https://d3js.org/d3-array.v2.min.js"></script>
<script src="https://d3js.org/d3-color.v2.min.js"></script>
<script src="https://d3js.org/d3-format.v2.min.js"></script>
<script src="https://d3js.org/d3-interpolate.v2.min.js"></script>
<script src="https://d3js.org/d3-time.v2.min.js"></script>
<script src="https://d3js.org/d3-time-format.v3.min.js"></script>
<script src="https://d3js.org/d3-scale.v3.min.js"></script>
<script src="https://d3js.org/d3-selection.v2.min.js"></script>
<script src="https://d3js.org/d3-axis.v2.min.js"></script>

<script src="../dist/timechart.min.js"></script>
<script>
const el = document.getElementById('chart');
function createChart() {
const data = [];
for (let x = 0; x < 100; x++) {
data.push({x, y: Math.random()});
}
const chart = new TimeChart(el, {
series: [{ data, name: 'Random' }],
});

setTimeout(() => {
chart.dispose();
createChart()
}, 200)
}
createChart()
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default class TimeChart {
};

this.model = new RenderModel(renderOptions);
const shadowRoot = el.attachShadow({ mode: 'open' });
const shadowRoot = el.shadowRoot ?? el.attachShadow({ mode: 'open' });
const style = document.createElement('style');
style.innerText = `
:host {
Expand Down

0 comments on commit c6a11f6

Please sign in to comment.