-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scales): add support for time scale
Raphaël Benitte
committed
Dec 16, 2017
1 parent
22c186a
commit 28e8ebf
Showing
9 changed files
with
163 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* This file is part of the nivo project. | ||
* | ||
* Copyright 2016-present, Raphaël Benitte. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
import uniq from 'lodash/uniq' | ||
import { scalePoint } from 'd3-scale' | ||
|
||
export const computePointScale = ({ | ||
data, | ||
domain: _domain, | ||
range, | ||
property, | ||
checkConsistency = false, | ||
}) => { | ||
if (checkConsistency === true) { | ||
const uniqLengths = uniq(data.map(({ data }) => data.length)) | ||
if (uniqLengths.length > 1) { | ||
throw new Error( | ||
[ | ||
`Found inconsistent data for '${property}',`, | ||
`expecting all series to have same length`, | ||
`but found: ${uniqLengths.join(', ')}`, | ||
].join(' ') | ||
) | ||
} | ||
} | ||
|
||
const domain = _domain !== undefined ? _domain : data[0].map(d => d[property]) | ||
|
||
return scalePoint() | ||
.range(range) | ||
.domain(domain) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* This file is part of the nivo project. | ||
* | ||
* Copyright 2016-present, Raphaël Benitte. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
import { computeLinearScale } from './linearScale' | ||
import { computePointScale } from './pointScale' | ||
import { computeTimeScale } from './timeScale' | ||
|
||
export const scaleByType = { | ||
linear: computeLinearScale, | ||
point: computePointScale, | ||
time: computeTimeScale, | ||
} | ||
|
||
export const scalesFromConfig = scaleConfigs => { | ||
const computedScales = {} | ||
scaleConfigs.forEach(scaleConfig => { | ||
computedScales[scaleConfig.id] = scaleByType[scaleConfig.type](scaleConfig) | ||
}) | ||
|
||
return computedScales | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* This file is part of the nivo project. | ||
* | ||
* Copyright 2016-present, Raphaël Benitte. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
import uniq from 'lodash/uniq' | ||
import { scaleTime } from 'd3-scale' | ||
|
||
/** | ||
* Add auto date conversion to d3 `timeScale`, | ||
* it helps to be able to pas raw data without having | ||
* to deal with Date objects and pre-process the data. | ||
* | ||
* @param {Object} scale | ||
* | ||
* @return {Object} | ||
*/ | ||
const enhanceScale = scale => { | ||
const enhancedScale = function(dateStr) { | ||
return scale(new Date(dateStr)) | ||
} | ||
|
||
// @todo: fix copy() | ||
Object.keys(scale).forEach(key => { | ||
enhancedScale[key] = scale[key].bind(scale) | ||
}) | ||
|
||
return enhancedScale | ||
} | ||
|
||
/** | ||
* Compute a time scale from config and data set. | ||
* | ||
* @param {Array.<Array>} data | ||
* @param {'auto'|string} min | ||
* @param {'auto'|string} max | ||
* @param {string|number} property | ||
* @param {Array.<number>} range | ||
* | ||
* @return {Object} | ||
*/ | ||
export const computeTimeScale = ({ data, min = 'auto', max = 'auto', property, range }) => { | ||
let domainMin = min !== 'auto' ? new Date(min) : min | ||
let domainMax = max !== 'auto' ? new Date(max) : max | ||
|
||
if (min === 'auto' || max === 'auto') { | ||
const allDates = uniq( | ||
data.reduce((agg, serie) => [...agg, ...serie.map(d => d[property])], []) | ||
) | ||
.map(dateStr => new Date(dateStr)) | ||
.sort((a, b) => b - a) | ||
.reverse() | ||
|
||
if (min === 'auto') domainMin = allDates[0] | ||
if (max === 'auto') domainMax = allDates[allDates.length - 1] | ||
} | ||
|
||
const scale = scaleTime() | ||
.domain([domainMin, domainMax]) | ||
.range(range) | ||
|
||
return enhanceScale(scale) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters