@adaptabletools/adaptable
Version:
Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
138 lines (137 loc) • 5.08 kB
JavaScript
import { getVariableColor } from '../StyleHelper';
import { clamp } from '../../Extensions/NumberExtensions';
import { STYLED_COLUMN_PREVIEW_SAMPLE_COUNT, ZERO_CENTRED_PREVIEW_SCALE_MAX, ZERO_CENTRED_PREVIEW_SCALE_MIN, } from './GradientStyleHelper';
import { buildBarStyleCellTextLabels, joinBarStyleCellTextLabels } from './BarStylesHelper';
const evenlySpacedValues = (min, max, count) => {
if (count <= 1) {
return [min];
}
const step = (max - min) / (count - 1);
return Array.from({ length: count }, (_, i) => min + step * i);
};
const toFraction = (value, min, max) => {
if (max === min) {
return 0;
}
return clamp((value - min) / (max - min), 0, 1);
};
const resolveOrigin = (percentBarStyle, cellValue, min, max) => {
const origin = percentBarStyle.Origin ?? 'Auto';
if (typeof origin === 'number') {
return origin;
}
if (origin === 'Zero') {
return 0;
}
if (origin === 'Min') {
return min;
}
if (min < 0 || max < 0 || cellValue < 0) {
return 0;
}
return min;
};
const columnComparisonUsesCentredAxis = (origin) => {
const o = origin ?? 'Auto';
if (typeof o === 'number') {
return false;
}
return o === 'Zero' || o === 'Auto';
};
const resolveRangeEndpoint = (endpoint, scaleMin, scaleMax) => {
if (endpoint === 'Col-Min') {
return scaleMin;
}
if (endpoint === 'Col-Max') {
return scaleMax;
}
if (typeof endpoint === 'number') {
return endpoint;
}
return scaleMin;
};
const getPreviewBarColor = (value, pb, scaleMin, scaleMax) => {
if (pb.ColumnComparison) {
return pb.ColumnComparison.Color;
}
for (const range of pb.CellRanges ?? []) {
const rMin = resolveRangeEndpoint(range.Min, scaleMin, scaleMax);
const rMax = resolveRangeEndpoint(range.Max, scaleMin, scaleMax);
if (value >= Math.min(rMin, rMax) && value <= Math.max(rMin, rMax)) {
return range.Color;
}
}
return pb.CellRanges?.[0]?.Color;
};
export const hasPercentBarRangesConfigured = (pb) => !!(pb?.CellRanges?.length || pb?.ColumnComparison);
export const getPercentBarPreviewScale = (pb) => {
const origin = pb.Origin ?? 'Auto';
if (origin === 'Zero' || origin === 'Auto') {
return { min: ZERO_CENTRED_PREVIEW_SCALE_MIN, max: ZERO_CENTRED_PREVIEW_SCALE_MAX };
}
return { min: 0, max: 100 };
};
export const getPercentBarPreviewSampleValues = (pb) => {
const { min, max } = getPercentBarPreviewScale(pb);
return evenlySpacedValues(min, max, STYLED_COLUMN_PREVIEW_SAMPLE_COUNT);
};
export const getPercentBarPreviewGeometry = (numericValue, pb, min, max) => {
let percentageValue = 0;
let barLeftPercent = 0;
let barWidthPercent = 0;
if (pb.ColumnComparison) {
const absMax = Math.abs(max);
percentageValue = absMax === 0 ? 0 : (numericValue / absMax) * 100;
const magnitude = Math.min(100, Math.abs(percentageValue));
const origin = pb.Origin ?? 'Auto';
const isCentred = columnComparisonUsesCentredAxis(origin);
if (isCentred) {
const halfWidth = magnitude / 2;
if (percentageValue >= 0) {
barLeftPercent = 50;
barWidthPercent = halfWidth;
}
else {
barLeftPercent = 50 - halfWidth;
barWidthPercent = halfWidth;
}
}
else {
barLeftPercent = 0;
barWidthPercent = magnitude;
}
}
else if (max === min) {
percentageValue = 0;
barLeftPercent = 0;
barWidthPercent = 0;
}
else {
percentageValue = ((clamp(numericValue, min, max) - min) / (max - min)) * 100;
const origin = resolveOrigin(pb, numericValue, min, max);
const valueFrac = toFraction(numericValue, min, max);
const originFrac = toFraction(origin, min, max);
const start = Math.min(valueFrac, originFrac);
const end = Math.max(valueFrac, originFrac);
barLeftPercent = start * 100;
barWidthPercent = (end - start) * 100;
}
return {
barLeftPercent,
barWidthPercent,
percentageValue,
barColor: getPreviewBarColor(numericValue, pb, min, max),
};
};
export const formatPercentBarPreviewCellText = (numericValue, percentageValue, pb) => {
const labels = buildBarStyleCellTextLabels(pb.CellTextProperties, Number.isInteger(numericValue) ? String(numericValue) : numericValue.toFixed(1), `${percentageValue.toFixed(0)}%`);
return joinBarStyleCellTextLabels(labels);
};
export const getPercentBarPreviewTrackColor = (pb) => {
if (pb.ColumnComparison) {
return pb.BackColor ?? undefined;
}
return pb.BackColor ?? undefined;
};
export const getPercentBarPreviewResolvedBarColor = (color) => (color ? getVariableColor(color) : undefined);
export const hasPercentBarStylePreview = (styledColumn) => hasPercentBarRangesConfigured(styledColumn.PercentBarStyle);