@neo4j-ndl/react-charts
Version:
React implementation of charts from Neo4j Design System
59 lines • 2.36 kB
JavaScript
/**
*
* 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 capitalizeFirstLetter = (str) => {
if (!str) {
return str;
}
return str.charAt(0).toUpperCase() + str.substring(1);
};
const enUSNumberFormatter = new Intl.NumberFormat('en-US');
/**
* Formats finite number values with en-US separators and returns all other
* values unchanged.
*/
const formatNumberEnUS = (value) => {
if (typeof value === 'number') {
return Number.isFinite(value) ? enUSNumberFormatter.format(value) : value;
}
return value;
};
/**
* 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;
};
export { capitalizeFirstLetter, extractValueFromTooltipSeries, formatNumberEnUS, };
//# sourceMappingURL=format-utils.js.map