-
Notifications
You must be signed in to change notification settings - Fork 236
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
Changes from 3 commits
d98f6d9
3afe593
5935c75
953e672
6a437fd
d5f3d5f
34a2036
569054a
d04a819
ed82c73
9b7dee7
de5fb75
36ffb89
871acb9
fdbf768
91f2365
eacc059
2be4abb
fd56c95
1ff0a37
f18f262
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
window.addEventListener('resize', callback, true); | ||
}; | ||
|
||
const removeOnResize = callback => { | ||
window.removeEventListener('resize', callback, true); | ||
}; | ||
|
||
const createChart = (root, selection) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. initChart |
||
const config = defaultsDeep( | ||
customConfiguration || {}, | ||
defaultConfiguration(d3) | ||
|
@@ -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') | ||
|
@@ -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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move these two lines in |
||
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; | ||
|
There was a problem hiding this comment.
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: