@apptane/react-ui-charts
Version:
Chart components in Apptane React UI framework
72 lines (57 loc) • 2.42 kB
JavaScript
import { binarySearch } from "@apptane/react-ui-core";
import PropTypes from "prop-types";
/**
* One of D3 chromatic color schemes.
* See: https://github.com/d3/d3-scale-chromatic
*/
export const PropTypeDatum = {
id: PropTypes.string,
label: PropTypes.string,
color: PropTypes.string,
data: PropTypes.any
};
export const PropTypeDomainValue = PropTypes.oneOfType([PropTypes.number, PropTypes.string, PropTypes.object]);
export const PropTypeDomainType = PropTypes.oneOf(["time", "numeric", "ordinal", "none"]);
export const PropTypeColorScheme = PropTypes.oneOf(["BrBG", "PRGn", "PiYG", "PuOr", "RdBu", "RdGy", "RdYlBu", "RdYlGn", "BuGn", "BuPu", "GnBu", "OrRd", "PuBuGn", "PuBu", "PuRd", "RdPu", "YlGnBu", "YlGn", "YlOrBr", "YlOrRd", "spectral", "blues", "greens", "greys", "oranges", "purples", "reds", "turbo", "viridis", "inferno", "magma", "plasma", "cividis", "warm", "cool", "cubehelix", "rainbow", "sinebow", "red", "orange", "yellow", "green", "cyan", "teal", "blue", "indigo", "purple"]);
/**
* Represents domain of values, possibly comparable.
*/
export class Domain {
constructor(values, comparer, ordinal) {
this.values = void 0;
this.__comparer = void 0;
this.__ordinal = void 0;
this.isEqual = (a, b) => !this.__ordinal && this.__comparer != null ? this.__comparer(a, b) === 0 : a === b;
if (comparer != null) {
values.sort(comparer);
}
this.values = values;
this.__comparer = comparer;
this.__ordinal = ordinal == true;
}
/**
* Tests two domain values for equality.
*/
/**
* @public
* Locates the closest value in the domain to the specified value.
* Returns both the value and its index in the domain.
*/
findNearest(v) {
if (v == null) {
return [undefined, undefined];
} // exact match only
if (this.__ordinal || this.__comparer == null) {
const index = this.values.indexOf(v);
return index < 0 ? [undefined, undefined] : [v, index];
} // binary search
let index = binarySearch(this.values, v, this.__comparer);
if (index >= 0) {
return [this.values[index], index];
} // since we only require domain to be comparable, and not necessarily
// measurable, we always default to the leftmost entry unless first
index = ~index;
return index === 0 ? [this.values[0], 0] : [this.values[index - 1], index - 1];
}
}
//# sourceMappingURL=Types.js.map