-
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 12 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 |
---|---|---|
|
@@ -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: | ||
|
||
* **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)** redraws chart using given configuration and `d3.scaleTime` scale | ||
* **destroy** execute this function before to removing the chart from DOM. It prevents some memory leaks due to event listeners. | ||
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. s/execute this function before to removing the chart from DOM/should be executed whenever you remove the chart from DOM/ 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. 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: | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -326,3 +326,20 @@ 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. | ||
|
||
### numberDisplayedTicks | ||
|
||
\_Default: | ||
|
||
```js | ||
const chart = eventDrops({ | ||
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. That's not the default value. Display default value only here. 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. Done |
||
numberDisplayedTicks: { | ||
small: 3, | ||
medium: 5, | ||
large: 7, | ||
extra: 12, | ||
}, | ||
}); | ||
|
||
This parameter configures the number of ticks display in the header | ||
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. I don't understand this sentence. I propose:
And add a list of breakpoint resolutions to know what |
||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,14 @@ | ||
import { timeFormat } from 'd3-time-format'; | ||
import breakpoints from './breakpoints'; | ||
|
||
export const getBreakpointLabel = point => | ||
Object.keys(breakpoints).reduce((accumulator, label) => { | ||
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. Why not just using a |
||
if (!accumulator.length && point <= breakpoints[label]) { | ||
return label; | ||
} | ||
|
||
return accumulator; | ||
}, ''); | ||
|
||
export const tickFormat = (date, formats, d3) => { | ||
if (d3.timeSecond(date) < date) { | ||
|
@@ -33,7 +43,12 @@ export const tickFormat = (date, formats, d3) => { | |
}; | ||
|
||
export default (d3, config, xScale) => { | ||
const { label: { width: labelWidth }, axis: { formats }, locale } = config; | ||
const { | ||
label: { width: labelWidth }, | ||
axis: { formats }, | ||
locale, | ||
numberDisplayedTicks, | ||
} = config; | ||
d3.timeFormatDefaultLocale(locale); | ||
return selection => { | ||
const axis = selection.selectAll('.axis').data(d => d); | ||
|
@@ -44,6 +59,11 @@ export default (d3, config, xScale) => { | |
.axisTop(xScale) | ||
.tickFormat(d => tickFormat(d, formats, d3)); | ||
|
||
const breakpoint = getBreakpointLabel(window.innerWidth) || 'extra'; | ||
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. Can't we do it in the |
||
if (numberDisplayedTicks) { | ||
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. Do we need an |
||
axisTop.ticks(numberDisplayedTicks[breakpoint]); | ||
} | ||
|
||
axis | ||
.enter() | ||
.filter((_, i) => !i) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import defaultLocale from 'd3-time-format/locale/en-US.json'; | ||
|
||
import axis, { tickFormat } from './axis'; | ||
import axis, { tickFormat, getBreakpointLabel } from './axis'; | ||
|
||
const defaultConfig = { | ||
label: { | ||
|
@@ -120,8 +120,37 @@ describe('Axis', () => { | |
expect(timeFormatDefaultLocaleSpy).toHaveBeenCalledWith(defaultLocale); | ||
}); | ||
|
||
it('should display tick using configuration locale', () => { | ||
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. Not sure why the locale intervenes here. :) |
||
const data = [[{ id: 'foo' }]]; | ||
const selection = d3.select('svg').data(data); | ||
|
||
let config = { | ||
...defaultConfig, | ||
numberDisplayedTicks: { extra: 9 }, | ||
}; | ||
|
||
axis(d3, config, defaultScale)(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. Use a |
||
expect(document.querySelectorAll('.tick').length).toBe(9); | ||
|
||
config = { | ||
...defaultConfig, | ||
numberDisplayedTicks: { extra: 5 }, | ||
}; | ||
axis(d3, config, defaultScale)(selection); | ||
expect(document.querySelectorAll('.tick').length).toBe(5); | ||
}); | ||
|
||
afterEach(() => { | ||
document.body.innerHTML = ''; | ||
jest.restoreAllMocks(); | ||
}); | ||
}); | ||
|
||
describe('Breakpoint Label', () => { | ||
it('should return breakpoint label correctly depending of current point', () => { | ||
expect(getBreakpointLabel(400)).toBe('small'); | ||
expect(getBreakpointLabel(600)).toBe('medium'); | ||
expect(getBreakpointLabel(800)).toBe('large'); | ||
expect(getBreakpointLabel(1000)).toBe('extra'); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default { | ||
small: 576, | ||
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. Shouldn't it be in configuration? |
||
medium: 768, | ||
large: 992, | ||
extra: 1200, | ||
}; |
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.
s/methods/members/