Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFR] Responsive chart #249

Merged
merged 21 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ _Default: Infinity_

This parameter configures the maximum zoom level available. Set it to a lower value to prevent your users from zooming in too deeply.

### removeOnResize
### destroy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


_Default: () => {}_

Function to be executed when you want to remove [`resize`](https://developer.mozilla.org/en-US/docs/Web/Events/resize) event.
Function to be executed when you want to destroy the chart. By default, it remove `resize` event and execute the callback.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Function to be executed when you want to destroy the chart. By default, it remove resize event and execute the callback./Execute this function before to removing the chart from DOM. It prevents some memory leaks due to event listeners./

29 changes: 13 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,10 @@ import { withinRange } from './withinRange';

// do not export anything else here to keep window.eventDrops as a function
export default ({ d3 = window.d3, ...customConfiguration }) => {
const onResize = callback => {
window.addEventListener('resize', callback, true);
};

const removeOnResize = callback => {
window.removeEventListener('resize', callback, true);
};
const initChart = selection => {
const root = selection.selectAll('svg').data(selection.data());
root.exit().remove();

const createChart = (root, selection) => {
const config = defaultsDeep(
customConfiguration || {},
defaultConfiguration(d3)
Expand Down Expand Up @@ -78,23 +73,25 @@ export default ({ d3 = window.d3, ...customConfiguration }) => {
};

const chart = selection => {
const root = selection.selectAll('svg').data(selection.data());
root.exit().remove();

createChart(root, selection);
initChart(selection);

const updateChart = () => {
selection.selectAll('svg').remove();
createChart(root, selection);
initChart(selection);
};

onResize(updateChart);
chart._removeOnResize = () => removeOnResize(updateChart);
window.addEventListener('resize', updateChart, true);

chart._removeOnResize = () =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need a function for this single line? :)

window.removeEventListener('resize', updateChart, true);
};

chart.scale = () => chart._scale;
chart.filteredData = () => chart._filteredData;
chart.removeOnResize = () => chart._removeOnResize();
chart.destroy = callback => {
chart._removeOnResize();
callback();
};

const draw = (config, scale) => selection => {
const { drop: { date: dropDate } } = config;
Expand Down