grafana/public/app/features/dimensions/scale.ts
Jack Westbrook d87bf30e9e
Build: Introduce ESM and Treeshaking to NPM package builds (#51517)
* Revert "Chore: Bump terser to fix security vulnerability (#53052)"

This reverts commit 7ae74d2a18.

* feat: use tsc and rollup directly with esbuild and publishConfig, files props

* refactor(grafana-data): fix isolatedModules re-export type error

* refactor(grafana-data): import paths from src not package name

* refactor(rollup): fix dts output.file

* chore(grafana-schema): delete dashboard_experimental.gen.ts - cannot work with isolatedModules

* refactor(grafana-e2e-selectors): fix export types isolatedModules error

* refactor(grafana-runtime): fix isolatedModules re-export type error

* refactor(grafana-ui): fix isolatedModules re-export type error

* feat(grafana-ui): use named imports for treeshaking

* refactor(grafana-ui): use named imports for treeshaking

* feat: react and react-dom as peerDeps for packages

* feat(grafana-ui): emotion packages as peerDeps

* feat(grafana-e2e): use tsc, rollup, esbuild for bundling

* chore(packages): clean up redundant dependencies

* chore(toolkit): deprecate unused package:build task

* chore(schema): put back dashboard_experimental and exclude to prevent isolatedModules error

* docs(packages): update readme

* chore(storybook): disable isolatedModules for builds

* chore: relax peerDeps for emotion and react

* revert(grafana-ui): put @emotion dependencies back

* refactor: replace relative package imports with package name

* build(packages): set emitDeclaration false for typecheck scripts to work

* test(publicdashboarddatasource): move test next to implementation. try to appease the betterer gods

* chore(storybook): override ts-node config for storybook compilation

* refactor(grafana-data): use ternary so babel doesnt complain about expecting flow types

* chore(toolkit): prefer files and publishConfig package.json props over copying

* build(npm): remove --contents dist arg from publishing commands

* chore(packages): introduce sideEffects prop to package.json to hint package can be treeshaken

* chore(packages): remove redundant index.js files

* feat(packages): set publishConfig.access to public

* feat(packages): use yarn berry and npm for packaging and publishing

* refactor(packages): simplify rollup configs

* chore(schema): add comment explaining need to exclude dashboard_experimental

* revert(toolkit): put back clean to prevent cli failures

* ci(packages): run packages:pack before a canary publish

* chore(gitignore): add npm-artifacts directory to ignore list

* test(publicdashboarddatasource): fix module mocking

* chore(packages): delete package.tgz when running clean

* chore(grafana-data): move dependencies from devDeps to prevent build resolution errors
2022-08-03 15:47:09 +02:00

135 lines
3.3 KiB
TypeScript

import { DataFrame, Field } from '@grafana/data';
import { getMinMaxAndDelta } from '@grafana/data/src/field/scale';
import { ScaleDimensionConfig, DimensionSupplier, ScaleDimensionOptions } from './types';
import { findField, getLastNotNullFieldValue } from './utils';
import { ScaleDimensionMode } from '.';
//---------------------------------------------------------
// Scale dimension
//---------------------------------------------------------
export function getScaledDimension(
frame: DataFrame | undefined,
config: ScaleDimensionConfig
): DimensionSupplier<number> {
return getScaledDimensionForField(findField(frame, config?.field), config);
}
export function getScaledDimensionForField(
field: Field | undefined,
config: ScaleDimensionConfig,
mode?: ScaleDimensionMode
): DimensionSupplier<number> {
if (!field) {
const v = config.fixed ?? 0;
return {
isAssumed: Boolean(config.field?.length) || !config.fixed,
fixed: v,
value: () => v,
get: () => v,
};
}
const info = getMinMaxAndDelta(field);
const delta = config.max - config.min;
const values = field.values;
if (values.length < 1 || delta <= 0 || info.delta <= 0) {
return {
fixed: config.min,
value: () => config.min,
get: () => config.min,
};
}
let scaled = (percent: number) => config.min + percent * delta;
if (mode === ScaleDimensionMode.Quadratic) {
const maxArea = Math.PI * (config.max / 2) ** 2;
const minArea = Math.PI * (config.min / 2) ** 2;
const deltaArea = maxArea - minArea;
// quadratic scaling (px area)
scaled = (percent: number) => {
let area = minArea + deltaArea * percent;
return Math.sqrt(area / Math.PI) * 2;
};
}
const get = (i: number) => {
const value = field.values.get(i);
let percent = 0;
if (value !== -Infinity) {
percent = (value - info.min!) / info.delta;
}
if (percent > 1) {
percent = 1;
} else if (percent < 0) {
percent = 0;
}
return scaled(percent);
};
return {
get,
value: () => get(getLastNotNullFieldValue(field)),
field,
};
}
// This will mutate options
export function validateScaleOptions(options?: ScaleDimensionOptions): ScaleDimensionOptions {
if (!options) {
options = { min: 0, max: 1 };
}
if (options.min == null) {
options.min = 0;
}
if (options.max == null) {
options.max = 1;
}
return options;
}
/** Mutates and will return a valid version */
export function validateScaleConfig(copy: ScaleDimensionConfig, options: ScaleDimensionOptions): ScaleDimensionConfig {
let { min, max } = validateScaleOptions(options);
if (!copy) {
copy = {} as any;
}
if (copy.max == null) {
copy.max = max;
}
if (copy.min == null) {
copy.min = min;
}
// Make sure the order is right
if (copy.min > copy.max) {
const tmp = copy.max;
copy.max = copy.min;
copy.min = tmp;
}
// Validate range
if (copy.min < min) {
copy.min = min;
}
if (copy.max > max) {
copy.max = max;
}
if (copy.fixed == null) {
copy.fixed = copy.min + (copy.max - copy.min) / 2.0;
}
// Make sure the field value is within the absolute range
if (!copy.field) {
if (copy.fixed > max) {
copy.fixed = max;
} else if (copy.fixed < min) {
copy.fixed = min;
}
}
return copy;
}