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 3 commits
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
6 changes: 6 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,9 @@ This parameter configures the minimum zoom level available. Set it to a not-null
_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
Copy link
Contributor

Choose a reason for hiding this comment

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

I would remove this configuration parameter. Instead, I would expose a destroy method taking a callback as argument. Something like:

destroy: callback => {
    global.removeEventListener('resize', resizeListener);
    callback();
}


_Default: () => {}_

Function to be executed when you want to remove [`resize`](https://developer.mozilla.org/en-US/docs/Web/Events/resize) event.
30 changes: 25 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ import { withinRange } from './withinRange';

// do not export anything else here to keep window.eventDrops as a function
export default ({ d3 = window.d3, ...customConfiguration }) => {
const chart = selection => {
const onResize = callback => {
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for these functions. Just keep a reference to the event listener to be able to remove it in the destroy method.

window.addEventListener('resize', callback, true);
};

const removeOnResize = callback => {
window.removeEventListener('resize', callback, true);
};

const createChart = (root, selection) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

initChart

const config = defaultsDeep(
customConfiguration || {},
defaultConfiguration(d3)
Expand Down Expand Up @@ -41,10 +49,6 @@ export default ({ d3 = window.d3, ...customConfiguration }) => {

chart._scale = xScale;

const root = selection.selectAll('svg').data(selection.data());

root.exit().remove();

const svg = root
.enter()
.append('svg')
Expand Down Expand Up @@ -73,8 +77,24 @@ export default ({ d3 = window.d3, ...customConfiguration }) => {
.call(draw(config, xScale));
};

const chart = selection => {
const root = selection.selectAll('svg').data(selection.data());
Copy link
Contributor

Choose a reason for hiding this comment

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

Move these two lines in createChart

root.exit().remove();

createChart(root, selection);

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

onResize(updateChart);
chart._removeOnResize = () => removeOnResize(updateChart);
};

chart.scale = () => chart._scale;
chart.filteredData = () => chart._filteredData;
chart.removeOnResize = () => chart._removeOnResize();

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