Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@nivo/scales

Package Overview
Dependencies
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nivo/scales - npm Package Compare versions

Comparing version 0.34.0-1 to 0.47.0

LICENSE.md

563

cjs/nivo-scales.js

@@ -7,284 +7,385 @@ 'use strict';

var React = require('react');
var React__default = _interopDefault(React);
var compose = _interopDefault(require('recompose/compose'));
var withPropsOnChange = _interopDefault(require('recompose/withPropsOnChange'));
var setDisplayName = _interopDefault(require('recompose/setDisplayName'));
var pure = _interopDefault(require('recompose/pure'));
var d3Scale = require('d3-scale');
var PropTypes = _interopDefault(require('prop-types'));
var getMin = _interopDefault(require('lodash/min'));
var getMax = _interopDefault(require('lodash/max'));
var d3Scale = require('d3-scale');
var d3TimeFormat = require('d3-time-format');
var uniq = _interopDefault(require('lodash/uniq'));
var uniqBy = _interopDefault(require('lodash/uniqBy'));
var sortBy = _interopDefault(require('lodash/sortBy'));
var last = _interopDefault(require('lodash/last'));
var isDate = _interopDefault(require('lodash/isDate'));
/*
* 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.
*/
var LinearScalePropType = PropTypes.shape({
type: PropTypes.oneOf(['linear']),
data: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.object)).isRequired,
property: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
var linearScale = function linearScale(_ref, xy, width, height) {
var axis = _ref.axis,
_ref$min = _ref.min,
min = _ref$min === undefined ? 0 : _ref$min,
_ref$max = _ref.max,
max = _ref$max === undefined ? 'auto' : _ref$max,
_ref$stacked = _ref.stacked,
stacked = _ref$stacked === undefined ? false : _ref$stacked;
var values = xy[axis];
var size = axis === 'x' ? width : height;
var minValue = min;
if (min === 'auto') {
minValue = stacked === true ? values.minStacked : values.min;
}
var maxValue = max;
if (max === 'auto') {
maxValue = stacked === true ? values.maxStacked : values.max;
}
var scale = d3Scale.scaleLinear().rangeRound(axis === 'x' ? [0, size] : [size, 0]).domain([minValue, maxValue]);
scale.type = 'linear';
scale.stacked = stacked;
return scale;
};
var linearScalePropTypes = {
type: PropTypes.oneOf(['linear']).isRequired,
min: PropTypes.oneOfType([PropTypes.oneOf(['auto']), PropTypes.number]),
max: PropTypes.oneOfType([PropTypes.oneOf(['auto']), PropTypes.number]),
range: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])).isRequired,
stacked: PropTypes.bool
});
};
var PointScalePropType = PropTypes.shape({
type: PropTypes.oneOf(['point']),
data: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.object)).isRequired,
property: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
domain: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])),
range: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])).isRequired
});
var pointScale = function pointScale(_ref, xy, width, height) {
var axis = _ref.axis;
var TimeScalePropType = PropTypes.shape({
type: PropTypes.oneOf(['time']),
data: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.object)).isRequired,
property: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
min: PropTypes.oneOfType([PropTypes.oneOf(['auto']), PropTypes.number]),
max: PropTypes.oneOfType([PropTypes.oneOf(['auto']), PropTypes.number]),
range: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])).isRequired
});
var values = xy[axis];
var size = axis === 'x' ? width : height;
/*
* 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.
*/
/**
* Compute a linear scale from config and data set.
*
* @param {Array.<Array>} data
* @param {'auto'|number} min
* @param {'auto'|number} max
* @param {string|number} property
* @param {Array.<number>} range
* @param {boolean} stacked
*
* @return {Object}
*/
var computeLinearScale = function computeLinearScale(_ref) {
var data = _ref.data,
var scale = d3Scale.scalePoint().range([0, size]).domain(values.all);
scale.type = 'point';
return scale;
};
var pointScalePropTypes = {
type: PropTypes.oneOf(['point']).isRequired
};
var _precisionCutOffsByTy;
var TIME_PRECISION_MILLISECOND = 'millisecond';
var TIME_PRECISION_SECOND = 'second';
var TIME_PRECISION_MINUTE = 'minute';
var TIME_PRECISION_HOUR = 'hour';
var TIME_PRECISION_DAY = 'day';
var TIME_PRECISION_MONTH = 'month';
var TIME_PRECISION_YEAR = 'year';
var timePrecisions = [TIME_PRECISION_MILLISECOND, TIME_PRECISION_SECOND, TIME_PRECISION_MINUTE, TIME_PRECISION_HOUR, TIME_PRECISION_DAY, TIME_PRECISION_MONTH, TIME_PRECISION_YEAR];
var precisionCutOffs = [function (date) {
return date.setMilliseconds(0);
}, function (date) {
return date.setSeconds(0);
}, function (date) {
return date.setMinutes(0);
}, function (date) {
return date.setHours(0);
}, function (date) {
return date.setDate(1);
}, function (date) {
return date.setMonth(0);
}];
var precisionCutOffsByType = (_precisionCutOffsByTy = {}, _precisionCutOffsByTy[TIME_PRECISION_MILLISECOND] = [], _precisionCutOffsByTy[TIME_PRECISION_SECOND] = precisionCutOffs.slice(0, 1), _precisionCutOffsByTy[TIME_PRECISION_MINUTE] = precisionCutOffs.slice(0, 2), _precisionCutOffsByTy[TIME_PRECISION_HOUR] = precisionCutOffs.slice(0, 3), _precisionCutOffsByTy[TIME_PRECISION_DAY] = precisionCutOffs.slice(0, 4), _precisionCutOffsByTy[TIME_PRECISION_MONTH] = precisionCutOffs.slice(0, 5), _precisionCutOffsByTy[TIME_PRECISION_YEAR] = precisionCutOffs.slice(0, 6), _precisionCutOffsByTy);
var createPrecisionMethod = function createPrecisionMethod(precision) {
return function (date) {
precisionCutOffsByType[precision].forEach(function (cutOff) {
cutOff(date);
});
return date;
};
};
var createDateNormalizer = function createDateNormalizer(_ref) {
var _ref$format = _ref.format,
format = _ref$format === undefined ? 'native' : _ref$format,
_ref$precision = _ref.precision,
precision = _ref$precision === undefined ? 'millisecond' : _ref$precision;
var precisionFn = createPrecisionMethod(precision);
if (format === 'native') return function (v) {
return precisionFn(v);
};
var parseTime = d3TimeFormat.timeParse(format);
return function (v) {
return precisionFn(parseTime(v));
};
};
var timeScale = function timeScale(_ref, xy, width, height) {
var axis = _ref.axis,
_ref$format = _ref.format,
format = _ref$format === undefined ? 'native' : _ref$format,
_ref$precision = _ref.precision,
precision = _ref$precision === undefined ? TIME_PRECISION_MILLISECOND : _ref$precision,
_ref$min = _ref.min,
min = _ref$min === undefined ? 'auto' : _ref$min,
_ref$max = _ref.max,
max = _ref$max === undefined ? 'auto' : _ref$max,
property = _ref.property,
range = _ref.range,
_ref$stacked = _ref.stacked,
stacked = _ref$stacked === undefined ? false : _ref$stacked;
max = _ref$max === undefined ? 'auto' : _ref$max;
var allValues = data.reduce(function (agg, serie) {
return [].concat(agg, serie.map(function (d) {
return d[property];
}));
}, []);
var values = xy[axis];
var size = axis === 'x' ? width : height;
var domainMin = min === 'auto' ? getMin(allValues) : min;
var normalize = createDateNormalizer({ format: format, precision: precision });
if (stacked === true) {
allValues = data.reduce(function (agg, serie) {
return serie.map(function (d, i) {
var previousValue = agg[i];
var value = d[property];
var minValue = min;
if (min === 'auto') {
minValue = values.min;
} else if (format !== 'native') {
minValue = normalize(values.min);
}
// if a value is null, we discard the whole slice's stack
if (previousValue === null || value === null) return null;
if (previousValue === undefined) return value;
return previousValue + value;
});
}, []).filter(function (d) {
return d !== null;
});
var maxValue = max;
if (max === 'auto') {
maxValue = values.max;
} else if (format !== 'native') {
maxValue = normalize(values.max);
}
var domainMax = max === 'auto' ? getMax(allValues) : max;
return d3Scale.scaleLinear().domain([domainMin, domainMax]).range(range);
var scale = d3Scale.scaleTime().domain([minValue, maxValue]).range([0, size]);
scale.type = 'time';
return scale;
};
/*
* 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.
*/
var computePointScale = function computePointScale(_ref) {
var data = _ref.data,
_domain = _ref.domain,
range = _ref.range,
property = _ref.property,
_ref$checkConsistency = _ref.checkConsistency,
checkConsistency = _ref$checkConsistency === undefined ? false : _ref$checkConsistency;
var timeScalePropTypes = {
type: PropTypes.oneOf(['time']).isRequired,
format: PropTypes.string,
precision: PropTypes.oneOf(timePrecisions)
};
if (checkConsistency === true) {
var uniqLengths = uniq(data.map(function (_ref2) {
var data = _ref2.data;
return 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(' '));
}
var _extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
var domain = _domain !== undefined ? _domain : data[0].map(function (d) {
return d[property];
});
return target;
};
return d3Scale.scalePoint().range(range).domain(domain);
var getOtherAxis = function getOtherAxis(axis) {
return axis === 'x' ? 'y' : 'x';
};
/*
* 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.
*/
/**
* 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}
*/
var enhanceScale = function enhanceScale(scale) {
var enhancedScale = function enhancedScale(dateStr) {
return scale(new Date(dateStr));
};
var compareValues = function compareValues(a, b) {
return a === b;
};
var compareDateValues = function compareDateValues(a, b) {
return a.getTime() === b.getTime();
};
// @todo: fix copy()
Object.keys(scale).forEach(function (key) {
enhancedScale[key] = scale[key].bind(scale);
var computeXYScalesForSeries = function computeXYScalesForSeries(_series, xScaleSpec, yScaleSpec, width, height) {
var series = _series.map(function (serie) {
return _extends({}, serie, {
data: serie.data.map(function (d) {
return { data: _extends({}, d) };
})
});
});
return enhancedScale;
var xy = generateSeriesXY(series, xScaleSpec, yScaleSpec);
if (xScaleSpec.stacked === true) {
stackX(yScaleSpec.type, xy, series);
}
if (yScaleSpec.stacked === true) {
stackY(xScaleSpec.type, xy, series);
}
var xScale = computeScale(_extends({}, xScaleSpec, { axis: 'x' }), xy, width, height);
var yScale = computeScale(_extends({}, yScaleSpec, { axis: 'y' }), xy, width, height);
series.forEach(function (serie) {
serie.data.forEach(function (d) {
d.position = {
x: xScale.stacked === true ? d.data.xStacked === null ? null : xScale(d.data.xStacked) : d.data.x === null ? null : xScale(d.data.x),
y: yScale.stacked === true ? d.data.yStacked === null ? null : yScale(d.data.yStacked) : d.data.y === null ? null : yScale(d.data.y)
};
});
});
return _extends({}, xy, {
series: series,
xScale: xScale,
yScale: yScale
});
};
var computeScale = function computeScale(spec, xy, width, height) {
if (spec.type === 'linear') return linearScale(spec, xy, width, height);else if (spec.type === 'point') return pointScale(spec, xy, width, height);else if (spec.type === 'time') return timeScale(spec, xy, width, height);
};
var generateSeriesXY = function generateSeriesXY(series, xScaleSpec, yScaleSpec) {
return {
x: generateSeriesAxis(series, 'x', xScaleSpec),
y: generateSeriesAxis(series, 'y', yScaleSpec)
};
};
/**
* 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}
* Normalize data according to scale type, (time => Date, linear => Number)
* compute sorted unique values and min/max.
*/
var computeTimeScale = function computeTimeScale(_ref) {
var data = _ref.data,
_ref$min = _ref.min,
min = _ref$min === undefined ? 'auto' : _ref$min,
_ref$max = _ref.max,
max = _ref$max === undefined ? 'auto' : _ref$max,
property = _ref.property,
range = _ref.range;
var generateSeriesAxis = function generateSeriesAxis(series, axis, scaleSpec) {
if (scaleSpec.type === 'linear') {
series.forEach(function (serie) {
serie.data.forEach(function (d) {
d.data[axis] = d.data[axis] === null ? null : parseFloat(d.data[axis]);
});
});
} else if (scaleSpec.type === 'time' && scaleSpec.format !== 'native') {
var parseTime = createDateNormalizer(scaleSpec);
series.forEach(function (serie) {
serie.data.forEach(function (d) {
d.data[axis] = d.data[axis] === null ? null : parseTime(d.data[axis]);
});
});
}
var domainMin = min !== 'auto' ? new Date(min) : min;
var domainMax = max !== 'auto' ? new Date(max) : max;
var all = [];
series.forEach(function (serie) {
serie.data.forEach(function (d) {
all.push(d.data[axis]);
});
});
if (min === 'auto' || max === 'auto') {
var allDates = uniq(data.reduce(function (agg, serie) {
return [].concat(agg, serie.map(function (d) {
return d[property];
}));
}, [])).map(function (dateStr) {
return new Date(dateStr);
}).sort(function (a, b) {
var min = void 0,
max = void 0;
if (scaleSpec.type === 'linear') {
all = uniq(all);
all = sortBy(all, function (v) {
return v;
});
min = Math.min.apply(Math, all);
max = Math.max.apply(Math, all);
} else if (scaleSpec.type === 'time') {
all = uniqBy(all, function (v) {
return v.getTime();
});
all = all.slice(0).sort(function (a, b) {
return b - a;
}).reverse();
if (min === 'auto') domainMin = allDates[0];
if (max === 'auto') domainMax = allDates[allDates.length - 1];
min = all[0];
max = last(all);
} else {
all = uniq(all);
min = all[0];
max = last(all);
}
var scale = d3Scale.scaleTime().domain([domainMin, domainMax]).range(range);
return enhanceScale(scale);
return { all: all, min: min, max: max };
};
/*
* 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.
*/
var scaleByType = {
linear: computeLinearScale,
point: computePointScale,
time: computeTimeScale
};
var stackAxis = function stackAxis(axis, otherType, xy, series) {
var otherAxis = getOtherAxis(axis);
var scalesFromConfig = function scalesFromConfig(scaleConfigs) {
var computedScales = {};
scaleConfigs.forEach(function (scaleConfig) {
computedScales[scaleConfig.id] = scaleByType[scaleConfig.type](scaleConfig);
var all = [];
xy[otherAxis].all.forEach(function (v) {
var compare = isDate(v) ? compareDateValues : compareValues;
var stack = [];
series.forEach(function (serie) {
var datum = serie.data.find(function (d) {
return compare(d.data[otherAxis], v);
});
var value = null;
var stackValue = null;
if (datum !== undefined) {
value = datum.data[axis];
if (value !== null) {
var head = last(stack);
if (head === undefined) {
stackValue = value;
} else if (head !== null) {
stackValue = head + value;
}
}
datum.data[axis + 'Stacked'] = stackValue;
}
stack.push(stackValue);
all.push(stackValue);
});
});
all = all.filter(function (v) {
return v !== null;
});
return computedScales;
xy[axis].minStacked = Math.min.apply(Math, all);
xy[axis].maxStacked = Math.max.apply(Math, all);
};
/*
* 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.
*/
var Scales = function Scales(_ref) {
var computedScales = _ref.computedScales,
children = _ref.children;
return React__default.createElement(
React.Fragment,
null,
children(computedScales)
);
var stackX = function stackX(xy, otherType, series) {
return stackAxis('x', xy, otherType, series);
};
var stackY = function stackY(xy, otherType, series) {
return stackAxis('y', xy, otherType, series);
};
Scales.propTypes = {
children: PropTypes.func.isRequired,
computedScales: PropTypes.object.isRequired,
scales: PropTypes.arrayOf(PropTypes.oneOfType([LinearScalePropType, PointScalePropType, TimeScalePropType])).isRequired
var computeAxisSlices = function computeAxisSlices(axis, data) {
var otherAxis = getOtherAxis(axis);
return data[otherAxis].all.map(function (v) {
var _slice;
var slice = (_slice = {
id: v
}, _slice[otherAxis] = data[otherAxis + 'Scale'](v), _slice.data = [], _slice);
var compare = isDate(v) ? compareDateValues : compareValues;
data.series.forEach(function (serie) {
var datum = serie.data.find(function (d) {
return compare(d.data[otherAxis], v);
});
if (datum !== undefined) {
slice.data.push(_extends({}, datum, {
serie: serie
}));
}
});
slice.data.reverse();
return slice;
});
};
var enhance = compose(setDisplayName('Scales'), withPropsOnChange(['scales'], function (_ref2) {
var scales = _ref2.scales;
return {
computedScales: scalesFromConfig(scales)
};
}), pure);
var computeXSlices = function computeXSlices(data) {
return computeAxisSlices('x', data);
};
var computeYSlices = function computeYSlices(data) {
return computeAxisSlices('y', data);
};
var Scales$1 = enhance(Scales);
var scalePropType = PropTypes.oneOfType([PropTypes.shape(linearScalePropTypes), PropTypes.shape(pointScalePropTypes), PropTypes.shape(timeScalePropTypes)]);
/*
* 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.
*/
exports.Scales = Scales$1;
exports.computeLinearScale = computeLinearScale;
exports.computePointScale = computePointScale;
exports.scaleByType = scaleByType;
exports.scalesFromConfig = scalesFromConfig;
exports.scalePropType = scalePropType;
exports.getOtherAxis = getOtherAxis;
exports.compareValues = compareValues;
exports.compareDateValues = compareDateValues;
exports.computeXYScalesForSeries = computeXYScalesForSeries;
exports.computeScale = computeScale;
exports.generateSeriesXY = generateSeriesXY;
exports.generateSeriesAxis = generateSeriesAxis;
exports.stackAxis = stackAxis;
exports.stackX = stackX;
exports.stackY = stackY;
exports.computeAxisSlices = computeAxisSlices;
exports.computeXSlices = computeXSlices;
exports.computeYSlices = computeYSlices;
exports.linearScale = linearScale;
exports.linearScalePropTypes = linearScalePropTypes;
exports.pointScale = pointScale;
exports.pointScalePropTypes = pointScalePropTypes;
exports.timeScale = timeScale;
exports.timeScalePropTypes = timeScalePropTypes;
{
"name": "@nivo/scales",
"version": "0.34.0-1",
"version": "0.47.0",
"license": "MIT",

@@ -9,25 +9,12 @@ "main": "./index.js",

"index.js",
"index.d.ts",
"cjs/"
],
"dependencies": {
"d3-scale": "^1.0.6",
"recompose": "^0.26.0"
"d3-scale": "^2.1.2",
"d3-time-format": "^2.1.3",
"lodash": "^4.17.4"
},
"devDependencies": {
"@nivo/babel-preset": "0.34.0-1",
"@nivo/generators": "0.34.0-1",
"babel-cli": "^6.26.0",
"babel-eslint": "^8.0.3",
"babel-jest": "^20.0.3",
"cross-env": "^5.0.5",
"eslint": "^4.12.1",
"eslint-plugin-react": "^7.5.1",
"jest": "^21.0.1",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-test-renderer": "^16.2.0"
},
"peerDependencies": {
"prop-types": "^15.5.10",
"react": ">= 16.2.0 < 17.0.0"
"prop-types": "^15.5.10"
},

@@ -37,5 +24,3 @@ "publishConfig": {

},
"scripts": {
"lint": "eslint src stories tests"
}
"gitHead": "8c41577a3b413d4b110622c3fdea7f977ca1452b"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc