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 2 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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ You can either use D3 as a specific import (specifying it in first argument of `

In addition to this configuration object, it also exposes some public methods allowing you to customize your application based on filtered data:
Copy link
Contributor

Choose a reason for hiding this comment

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

s/methods/members/


* **scale()** provides the horizontal scale, allowing you to retrieve bounding dates thanks to `.scale().domain()`,
* **filteredData()** returns an object with both `data` and `fullData` keys containing respectively bounds filtered data and full dataset.
* **draw(config, scale)** redraw chart using given configuration and `d3.scaleTime` scale
* **scale()** provides the horizontal scale, allowing you to retrieve bounding dates thanks to `.scale().domain()`,
* **filteredData()** returns an object with both `data` and `fullData` keys containing respectively bounds filtered data and full dataset.
* **draw(config, scale)** redraw chart using given configuration and `d3.scaleTime` scale
Copy link
Contributor

Choose a reason for hiding this comment

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

s/redraw/redraws/

* **destroy** execute this function before to removing the chart from DOM. It prevents some memory leaks due to event listeners.
Copy link
Contributor

Choose a reason for hiding this comment

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

s/execute this function before to removing the chart from DOM/should be executed whenever you remove the chart from DOM/

Copy link
Contributor

Choose a reason for hiding this comment

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

s/destroy/destroy()/


Hence, if you want to display number of displayed data and time bounds as in the [demo](https://marmelab.com/EventDrops/), you can use the following code:

Expand Down
6 changes: 0 additions & 6 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,6 @@ _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.

### destroy

_Default: () => {}_

Function to be executed when you want to destroy the chart. By default, it remove `resize` event and execute the callback.

### numberDisplayedTicks

\_Default:
Expand Down
19 changes: 7 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { withinRange } from './withinRange';
// do not export anything else here to keep window.eventDrops as a function
export default ({ d3 = window.d3, ...customConfiguration }) => {
const initChart = selection => {
selection.selectAll('svg').remove();

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

Expand Down Expand Up @@ -73,23 +75,16 @@ export default ({ d3 = window.d3, ...customConfiguration }) => {
};

const chart = selection => {
initChart(selection);

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

window.addEventListener('resize', updateChart, true);
chart._initialize = () => initChart(selection);
chart._initialize();

chart._removeOnResize = () =>
window.removeEventListener('resize', updateChart, true);
global.addEventListener('resize', chart._initialize, true);
};

chart.scale = () => chart._scale;
chart.filteredData = () => chart._filteredData;
chart.destroy = callback => {
chart._removeOnResize();
chart.destroy = (callback = () => {}) => {
global.removeEventListener('resize', chart._initialize, true);
callback();
};

Expand Down