@neo4j-ndl/react-charts
Version:
React implementation of charts from Neo4j Design System
67 lines • 2.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractValueFromTooltipSeries = exports.useThrottle = exports.capitalizeFirstLetter = void 0;
/**
*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const react_1 = require("react");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const useThrottle = (callback, limit) => {
const lastCallRef = (0, react_1.useRef)(0);
const throttledCallback = (0, react_1.useCallback)(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(...args) => {
const now = Date.now();
if (now - lastCallRef.current >= limit) {
lastCallRef.current = now;
callback(...args);
}
}, [callback, limit]);
return throttledCallback;
};
exports.useThrottle = useThrottle;
const capitalizeFirstLetter = (str) => {
if (!str)
return str;
return str.charAt(0).toUpperCase() + str.substring(1);
};
exports.capitalizeFirstLetter = capitalizeFirstLetter;
/**
* Extract the tooltip value from the series depending on the axisDim and encode properties
* If the value is a single value, return it as it is
* If the encode property does not exist fallback to 1
*/
const extractValueFromTooltipSeries = (value, encode,
// We currently only support x and y axes
axisDim = 'x') => {
var _a, _b;
if (!Array.isArray(value)) {
return value;
}
// The axisDim is the axis that the tooltip was triggered on,
// if it was triggered on the x axis the value should be read from the y axis and vice versa
const encodeValueToRead = axisDim === 'x' ? encode === null || encode === void 0 ? void 0 : encode.y : (_a = encode === null || encode === void 0 ? void 0 : encode.x) !== null && _a !== void 0 ? _a : 1;
const seriesValueIdx = Array.isArray(encodeValueToRead)
? encodeValueToRead[0]
: encodeValueToRead;
return (_b = value[seriesValueIdx]) !== null && _b !== void 0 ? _b : value;
};
exports.extractValueFromTooltipSeries = extractValueFromTooltipSeries;
//# sourceMappingURL=utils.js.map