UNPKG

@mui/x-charts

Version:

The community edition of the charts components (MUI X).

66 lines (65 loc) 2.83 kB
import _typeof from "@babel/runtime/helpers/esm/typeof"; import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; import * as React from 'react'; import { isBandScale } from '../internals/isBandScale'; export function getTickNumber(params) { var tickMaxStep = params.tickMaxStep, tickMinStep = params.tickMinStep, tickNumber = params.tickNumber, range = params.range, domain = params.domain; var maxTicks = tickMinStep === undefined ? 999 : Math.floor(Math.abs(domain[1] - domain[0]) / tickMinStep); var minTicks = tickMaxStep === undefined ? 2 : Math.ceil(Math.abs(domain[1] - domain[0]) / tickMaxStep); var defaultizedTickNumber = tickNumber != null ? tickNumber : Math.floor(Math.abs(range[1] - range[0]) / 50); return Math.min(maxTicks, Math.max(minTicks, defaultizedTickNumber)); } function useTicks(options) { var scale = options.scale, tickNumber = options.tickNumber, valueFormatter = options.valueFormatter, tickInterval = options.tickInterval; return React.useMemo(function () { // band scale if (isBandScale(scale)) { var domain = scale.domain(); if (scale.bandwidth() > 0) { // scale type = 'band' return [].concat(_toConsumableArray(domain.map(function (value) { var _valueFormatter; return { value: value, formattedValue: (_valueFormatter = valueFormatter == null ? void 0 : valueFormatter(value)) != null ? _valueFormatter : "".concat(value), offset: scale(value) - (scale.step() - scale.bandwidth()) / 2, labelOffset: scale.step() / 2 }; })), [{ formattedValue: undefined, offset: scale.range()[1], labelOffset: 0 }]); } // scale type = 'point' var filteredDomain = typeof tickInterval === 'function' && domain.filter(tickInterval) || _typeof(tickInterval) === 'object' && tickInterval || domain; return filteredDomain.map(function (value) { var _valueFormatter2; return { value: value, formattedValue: (_valueFormatter2 = valueFormatter == null ? void 0 : valueFormatter(value)) != null ? _valueFormatter2 : "".concat(value), offset: scale(value), labelOffset: 0 }; }); } var ticks = _typeof(tickInterval) === 'object' ? tickInterval : scale.ticks(tickNumber); return ticks.map(function (value) { var _valueFormatter3; return { value: value, formattedValue: (_valueFormatter3 = valueFormatter == null ? void 0 : valueFormatter(value)) != null ? _valueFormatter3 : scale.tickFormat(tickNumber)(value), offset: scale(value), labelOffset: 0 }; }); }, [tickNumber, scale, valueFormatter, tickInterval]); } export default useTicks;