_resolveDomain()

in src/utils/resolveXYScales.js [234:356]


    _resolveDomain(props, Component, xScaleType, yScaleType) {
      let { xDomain, yDomain } = props;
      const { includeXZero, includeYZero } = props;
      const xDataType = dataTypeFromScaleType(xScaleType);
      const yDataType = dataTypeFromScaleType(yScaleType);

      const isXDone = () => isValidDomain(xDomain, xDataType);
      const isYDone = () => isValidDomain(yDomain, yDataType);
      const isDone = () => isXDone() && isYDone();

      // short-circuit if all domains provided
      if (isDone()) return { xDomain, yDomain };

      // if Component provides a custom static getScaleType method
      // use it to determine remaining scale types
      if (isFunction(Component.getDomain)) {
        const {
          xDomain: componentXDomain,
          yDomain: componentYDomain,
        } = Component.getDomain({ ...props, xScaleType, yScaleType });

        if (
          !isXDone() &&
          componentXDomain &&
          !isValidDomain(componentXDomain, xDataType)
        )
          console.warn(
            `Component.getDomain returned an invalid domain for data type '${xDataType}': ${componentXDomain} - ignoring`,
          );
        if (!isXDone() && isValidDomain(componentXDomain, xDataType))
          xDomain = componentXDomain;

        if (
          !isYDone() &&
          componentYDomain &&
          !isValidDomain(componentYDomain, yDataType)
        )
          console.warn(
            `Component.getDomain returned an invalid domain for data type '${yDataType}': ${componentYDomain} - ignoring`,
          );
        if (!isYDone() && isValidDomain(componentYDomain, yDataType))
          yDomain = componentYDomain;
      }

      // if Component has data or datasets props,
      // use the default domainFromDatasets function to determine a domain from them
      if (
        !isDone() &&
        (Array.isArray(props.data) || Array.isArray(props.datasets))
      ) {
        const datasets = Array.isArray(props.datasets)
          ? props.datasets
          : [props.data];
        if (!isXDone()) {
          xDomain = domainFromDatasets(
            datasets,
            makeAccessor2(props.x),
            xDataType,
          );
        }
        if (!isYDone()) {
          yDomain = domainFromDatasets(
            datasets,
            makeAccessor2(props.y),
            yDataType,
          );
        }
      }

      // if Component has children,
      // recurse through descendants to resolve their domains the same way,
      // and combine them into a single domain, if there are multiple
      if (!isDone() && React.Children.count(props.children)) {
        const childrenDomains = mapOverChildren(
          props.children,
          this._resolveDomain.bind(this),
          xScaleType,
          yScaleType,
        );

        if (!isXDone()) {
          const childXDomains = compact(
            childrenDomains.map(childDomains => childDomains.xDomain),
          );
          xDomain = combineDomains(childXDomains, xDataType);
        }
        if (!isYDone()) {
          const childYDomains = compact(
            childrenDomains.map(childDomains => childDomains.yDomain),
          );
          yDomain = combineDomains(childYDomains, yDataType);
        }
      }

      if (isDone()) {
        if (includeXZero && !inRange(0, ...xDomain)) {
          // If both are negative set max of domain to 0
          if (xDomain[0] < 0 && xDomain[1] < 0) {
            xDomain[1] = 0;
          } else {
            xDomain[0] = 0;
          }
        }

        if (includeYZero && !inRange(0, ...yDomain)) {
          // If both are negative set max of domain to 0
          if (yDomain[0] < 0 && yDomain[1] < 0) {
            yDomain[1] = 0;
          } else {
            yDomain[0] = 0;
          }
        }
      }

      // TODO handle resolveXYScales not calculating the domain
      // Because this is recursive on its children it will log this warn for children missing domain
      // even though it is later inferred by parent later during the recursion
      // if (!isDone()) {
      //   console.warn(`resolveXYScales was unable to resolve both domains. xDomain: ${xDomain}, yDomain: ${yDomain}`);
      // }

      return { xDomain, yDomain };
    }