@awsui/components-react
Version:
AWS UI is a collection of [React](https://reactjs.org/) components that help create intuitive, responsive, and accessible user experiences for web applications. It is developed by Amazon Web Services (AWS). This work is available under the terms of the [A
95 lines (94 loc) • 2.87 kB
JavaScript
import { scaleLinear, scaleLog, scaleTime, scaleBand, scalePow } from 'd3-scale';
function isNumericDomain(domain) {
return domain.length > 0 && typeof domain[0] === 'number';
}
function isDateDomain(domain) {
return domain.length > 0 && domain[0] instanceof Date;
}
function isStringDomain(domain) {
return domain.length > 0 && typeof domain[0] === 'string';
}
function createNumericScale(type, domain) {
var scale;
switch (type) {
case 'log':
scale = scaleLog();
break;
case 'pow':
scale = scalePow();
break;
default:
scale = scaleLinear();
}
if (isNumericDomain(domain)) {
scale.domain(domain);
}
return scale;
}
function createTimeScale(domain) {
var scale = scaleTime();
if (isDateDomain(domain)) {
scale.domain(domain);
}
return scale;
}
function createBandScale(domain) {
var scale = scaleBand().padding(0.1);
if (isStringDomain(domain)) {
scale.domain(domain);
}
return scale;
}
export function createScale(type, domain, range) {
switch (type) {
case 'linear':
case 'log':
case 'pow':
return { type: 'numeric', scale: createNumericScale(type, domain).range(range) };
case 'time':
return { type: 'time', scale: createTimeScale(domain).range(range) };
case 'categorical':
return { type: 'categorical', scale: createBandScale(domain).range(range) };
}
}
var ChartScale = (function () {
function ChartScale(scaleType, domain, range) {
this.scaleType = scaleType;
this.domain = domain;
this.range = range;
this.scale = createScale(this.scaleType, this.domain, this.range);
this.d3Scale = this.scale.scale;
}
ChartScale.prototype.cloneScale = function (newScaleType) {
return new ChartScale(newScaleType || this.scaleType, this.domain, this.range);
};
ChartScale.prototype.isNumeric = function () {
return this.scale.type === 'numeric';
};
ChartScale.prototype.isTime = function () {
return this.scale.type === 'time';
};
ChartScale.prototype.isCategorical = function () {
return this.scale.type === 'categorical';
};
ChartScale.prototype.numericD3Scale = function () {
if (this.scale.type === 'numeric') {
return this.scale.scale;
}
return null;
};
ChartScale.prototype.timeD3Scale = function () {
if (this.scale.type === 'time') {
return this.scale.scale;
}
return null;
};
ChartScale.prototype.categoricalD3Scale = function () {
if (this.scale.type === 'categorical') {
return this.scale.scale;
}
return null;
};
return ChartScale;
}());
export { ChartScale };