| [a762898] | 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.d3ScaleToRechartsScale = d3ScaleToRechartsScale;
|
|---|
| 7 | exports.rechartsScaleFactory = rechartsScaleFactory;
|
|---|
| 8 | var d3Scales = _interopRequireWildcard(require("victory-vendor/d3-scale"));
|
|---|
| 9 | var _DataUtils = require("../DataUtils");
|
|---|
| 10 | function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
|
|---|
| 11 | /**
|
|---|
| 12 | * This is internal representation of scale used in Recharts.
|
|---|
| 13 | * Users will provide CustomScaleDefinition or a string, which we will parse into RechartsScale.
|
|---|
| 14 | * Most importantly, RechartsScale is fully immutable - there are no setters that mutate the scale in place.
|
|---|
| 15 | * This is important for React integration - if the scale changes, we want to trigger re-renders.
|
|---|
| 16 | * Mutating the scale in place would not trigger re-renders, leading to stale UI.
|
|---|
| 17 | */
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * Position within a band for banded scales.
|
|---|
| 21 | * In scales that are not banded, this parameter is ignored.
|
|---|
| 22 | *
|
|---|
| 23 | * @inline
|
|---|
| 24 | */
|
|---|
| 25 |
|
|---|
| 26 | function getD3ScaleFromType(realScaleType) {
|
|---|
| 27 | if (realScaleType in d3Scales) {
|
|---|
| 28 | // @ts-expect-error we should do better type verification here
|
|---|
| 29 | return d3Scales[realScaleType]();
|
|---|
| 30 | }
|
|---|
| 31 | var name = "scale".concat((0, _DataUtils.upperFirst)(realScaleType));
|
|---|
| 32 | if (name in d3Scales) {
|
|---|
| 33 | // @ts-expect-error we should do better type verification here
|
|---|
| 34 | return d3Scales[name]();
|
|---|
| 35 | }
|
|---|
| 36 | return undefined;
|
|---|
| 37 | }
|
|---|
| 38 | function d3ScaleToRechartsScale(d3Scale) {
|
|---|
| 39 | var ticksFn = d3Scale.ticks;
|
|---|
| 40 | var bandwidthFn = d3Scale.bandwidth;
|
|---|
| 41 | var d3Range = d3Scale.range();
|
|---|
| 42 | var range = [Math.min(...d3Range), Math.max(...d3Range)];
|
|---|
| 43 | return {
|
|---|
| 44 | domain: () => d3Scale.domain(),
|
|---|
| 45 | range: function (_range) {
|
|---|
| 46 | function range() {
|
|---|
| 47 | return _range.apply(this, arguments);
|
|---|
| 48 | }
|
|---|
| 49 | range.toString = function () {
|
|---|
| 50 | return _range.toString();
|
|---|
| 51 | };
|
|---|
| 52 | return range;
|
|---|
| 53 | }(() => range),
|
|---|
| 54 | rangeMin: () => range[0],
|
|---|
| 55 | rangeMax: () => range[1],
|
|---|
| 56 | isInRange(value) {
|
|---|
| 57 | var first = range[0];
|
|---|
| 58 | var last = range[1];
|
|---|
| 59 | return first <= last ? value >= first && value <= last : value >= last && value <= first;
|
|---|
| 60 | },
|
|---|
| 61 | bandwidth: bandwidthFn ? () => bandwidthFn.call(d3Scale) : undefined,
|
|---|
| 62 | ticks: ticksFn ? count => ticksFn.call(d3Scale, count) : undefined,
|
|---|
| 63 | map: (input, options) => {
|
|---|
| 64 | var baseValue = d3Scale(input);
|
|---|
| 65 | if (baseValue == null) {
|
|---|
| 66 | return undefined;
|
|---|
| 67 | }
|
|---|
| 68 | if (d3Scale.bandwidth && options !== null && options !== void 0 && options.position) {
|
|---|
| 69 | var bandWidth = d3Scale.bandwidth();
|
|---|
| 70 | switch (options.position) {
|
|---|
| 71 | case 'middle':
|
|---|
| 72 | baseValue += bandWidth / 2;
|
|---|
| 73 | break;
|
|---|
| 74 | case 'end':
|
|---|
| 75 | baseValue += bandWidth;
|
|---|
| 76 | break;
|
|---|
| 77 | default:
|
|---|
| 78 | // 'start' requires no adjustment
|
|---|
| 79 | break;
|
|---|
| 80 | }
|
|---|
| 81 | }
|
|---|
| 82 | return baseValue;
|
|---|
| 83 | }
|
|---|
| 84 | };
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | /**
|
|---|
| 88 | * Converts external scale definition into internal RechartsScale definition.
|
|---|
| 89 | * @param scale custom function scale - if you have the string, use `combineRealScaleType` first
|
|---|
| 90 | * @param axisDomain
|
|---|
| 91 | * @param axisRange
|
|---|
| 92 | */
|
|---|
| 93 |
|
|---|
| 94 | function rechartsScaleFactory(scale, axisDomain, axisRange) {
|
|---|
| 95 | if (typeof scale === 'function') {
|
|---|
| 96 | return d3ScaleToRechartsScale(scale.copy().domain(axisDomain).range(axisRange));
|
|---|
| 97 | }
|
|---|
| 98 | if (scale == null) {
|
|---|
| 99 | return undefined;
|
|---|
| 100 | }
|
|---|
| 101 | var d3ScaleFunction = getD3ScaleFromType(scale);
|
|---|
| 102 | if (d3ScaleFunction == null) {
|
|---|
| 103 | return undefined;
|
|---|
| 104 | }
|
|---|
| 105 | d3ScaleFunction.domain(axisDomain).range(axisRange);
|
|---|
| 106 | return d3ScaleToRechartsScale(d3ScaleFunction);
|
|---|
| 107 | } |
|---|