UNPKG

@data-ui/xy-chart

Version:

A package of charts with standard x- and y- axes. https://williaster.github.io/data-ui

42 lines (35 loc) 1.9 kB
function _extends() { _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]; } } } return target; }; return _extends.apply(this, arguments); } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } import { scaleLinear, scaleTime, scaleUtc, scaleBand, scaleOrdinal } from '@vx/scale'; import { extent } from 'd3-array'; export var scaleTypeToScale = { time: scaleTime, timeUtc: scaleUtc, linear: scaleLinear, band: scaleBand, ordinal: scaleOrdinal }; export default function getScaleForAccessor(_ref) { var allData = _ref.allData, minAccessor = _ref.minAccessor, maxAccessor = _ref.maxAccessor, type = _ref.type, _ref$includeZero = _ref.includeZero, includeZero = _ref$includeZero === void 0 ? true : _ref$includeZero, range = _ref.range, rest = _objectWithoutPropertiesLoose(_ref, ["allData", "minAccessor", "maxAccessor", "type", "includeZero", "range"]); var domain; if (type === 'band' || type === 'ordinal') { domain = allData.map(minAccessor); } if (type === 'linear' || type === 'time' || type === 'timeUtc') { var _extent = extent([].concat(extent(allData, minAccessor), extent(allData, maxAccessor))), min = _extent[0], max = _extent[1]; domain = [type === 'linear' && includeZero ? Math.min(0, min) : min, type === 'linear' && includeZero ? Math.max(0, max) : max]; } return scaleTypeToScale[type](_extends({ domain: domain, range: range }, rest)); }