Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/core/series/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ export interface RenderShapesArgs {
dispatcher?: Dispatch<object>;
}

export interface ValidateSeriesArgs<T = ChartSeries> {
/** The series being validated. */
series: T;
/** All series in the chart. Needed only by collection-level checks (e.g. treemap uniqueness); other types ignore it. */
allSeries: ChartSeries[];
xAxis?: ChartXAxis;
yAxis?: ChartYAxis[];
}

export interface SeriesPlugin<T extends ChartSeries = ChartSeries> {
/** Unique series type identifier (e.g. `'line'`, `'bar-x'`). Used for plugin lookup and CSS class generation. */
type: T['type'];
Expand All @@ -67,6 +76,11 @@ export interface SeriesPlugin<T extends ChartSeries = ChartSeries> {
useClipPath?: boolean;
/** Transforms raw chart series config into prepared series objects used throughout the render pipeline. */
prepareSeries(args: PrepareSeriesArgs): PreparedSeries[] | Promise<PreparedSeries[]>;
/**
* Validates type-specific series config. Called once per series by `validateData`.
* Should throw a `ChartError` on invalid input. Omit for types that need no validation.
*/
validate?(args: ValidateSeriesArgs<T>): void;
/** Computes shape data (geometry, labels, markers) from prepared series. Called once per render cycle. */
prepareShapeData(
args: PrepareShapeDataArgs,
Expand Down
8 changes: 8 additions & 0 deletions src/core/series/seriesRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ export function getSeriesPlugin(type: string): SeriesPlugin {
}
return plugin;
}

export function hasSeriesPlugin(type: string): boolean {
return registry.has(type);
}

export function getRegisteredSeriesTypes(): string[] {
return [...registry.keys()];
}
1 change: 1 addition & 0 deletions src/core/validation/__tests__/validation.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {validateData} from '../';
import type {ChartError} from '../../../libs';
import {CHART_ERROR_CODE} from '../../../libs';
import '../../../plugins';
import type {ChartData} from '../../types';
import {PIE_SERIES, XY_SERIES} from '../__mocks__';

Expand Down
298 changes: 298 additions & 0 deletions src/core/validation/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,298 @@
import get from 'lodash/get';

import {CHART_ERROR_CODE, ChartError} from '../../libs';
import {DEFAULT_AXIS_TYPE} from '../constants';
import {i18n} from '../i18n';
import type {
AreaSeries,
BarXSeries,
BarYSeries,
ChartXAxis,
ChartYAxis,
LineSeries,
ScatterSeries,
} from '../types';

export type XYSeries = ScatterSeries | BarXSeries | BarYSeries | LineSeries | AreaSeries;
Comment thread
korvin89 marked this conversation as resolved.
Outdated

export function validateXYSeries(args: {
series: XYSeries;
xAxis?: ChartXAxis;
yAxis?: ChartYAxis[];
}) {
const {series, xAxis, yAxis = []} = args;

const yAxisIndex = get(series, 'yAxis', 0);
const seriesYAxis = yAxis[yAxisIndex];
if (yAxisIndex !== 0 && typeof seriesYAxis === 'undefined') {
throw new ChartError({
code: CHART_ERROR_CODE.INVALID_DATA,
message: i18n('error', 'label_invalid-y-axis-index', {
index: yAxisIndex,
}),
});
}

const xType = get(xAxis, 'type', DEFAULT_AXIS_TYPE);
const yType = get(seriesYAxis, 'type', DEFAULT_AXIS_TYPE);

series.data.forEach(({x, y}) => {
switch (xType) {
case 'category': {
if (typeof x !== 'string' && typeof x !== 'number' && x !== null) {
throw new ChartError({
code: CHART_ERROR_CODE.INVALID_DATA,
message: i18n('error', 'label_invalid-axis-category-data-point', {
key: 'x',
seriesName: series.name,
}),
});
}

break;
}
case 'datetime': {
if (typeof x !== 'number') {
throw new ChartError({
code: CHART_ERROR_CODE.INVALID_DATA,
message: i18n('error', 'label_invalid-axis-datetime-data-point', {
key: 'x',
seriesName: series.name,
}),
});
}

break;
}
case 'linear': {
if (typeof x !== 'number' && x !== null) {
throw new ChartError({
code: CHART_ERROR_CODE.INVALID_DATA,
message: i18n('error', 'label_invalid-axis-linear-data-point', {
key: 'x',
seriesName: series.name,
}),
});
}
}
}
switch (yType) {
case 'category': {
if (typeof y !== 'string' && typeof y !== 'number' && y !== null) {
throw new ChartError({
code: CHART_ERROR_CODE.INVALID_DATA,
message: i18n('error', 'label_invalid-axis-category-data-point', {
key: 'y',
seriesName: series.name,
}),
});
}

break;
}
case 'datetime': {
if (typeof y !== 'number') {
throw new ChartError({
code: CHART_ERROR_CODE.INVALID_DATA,
message: i18n('error', 'label_invalid-axis-datetime-data-point', {
key: 'y',
seriesName: series.name,
}),
});
}

break;
}
case 'linear': {
if (typeof y !== 'number' && y !== null) {
throw new ChartError({
code: CHART_ERROR_CODE.INVALID_DATA,
message: i18n('error', 'label_invalid-axis-linear-data-point', {
key: 'y',
seriesName: series.name,
}),
});
}
}
}
});
}

export function validateAxisPlotValues(args: {
series: XYSeries;
xAxis?: ChartXAxis;
yAxis?: ChartYAxis[];
}) {
const {series, xAxis, yAxis = []} = args;

const yAxisIndex = get(series, 'yAxis', 0);
const seriesYAxis = yAxis[yAxisIndex];
if (yAxisIndex !== 0 && typeof seriesYAxis === 'undefined') {
throw new ChartError({
code: CHART_ERROR_CODE.INVALID_DATA,
message: i18n('error', 'label_invalid-y-axis-index', {
index: yAxisIndex,
}),
});
}

const xPlotBands = get(xAxis, 'plotBands', []);
const yPlotBands = get(yAxis, 'plotBands', []);

if (!xPlotBands.length && !yPlotBands.length) {
return;
}

const xType = get(xAxis, 'type', DEFAULT_AXIS_TYPE);
const yType = get(seriesYAxis, 'type', DEFAULT_AXIS_TYPE);

xPlotBands.forEach(({from = 0, to = 0}) => {
const fromNotEqualTo = typeof to !== typeof from;

if (fromNotEqualTo) {
throw new ChartError({
code: CHART_ERROR_CODE.INVALID_OPTION_TYPE,
message: i18n('error', 'label_axis-plot-band-options-not-equal', {
axis: 'x',
option: 'from',
}),
});
}

switch (xType) {
case 'category': {
const invalidFrom = typeof from !== 'string' && typeof from !== 'number';
const invalidTo = typeof to !== 'string' && typeof to !== 'number';
if (invalidFrom) {
throw new ChartError({
code: CHART_ERROR_CODE.INVALID_OPTION_TYPE,
message: i18n('error', 'label_invalid-axis-plot-band-option', {
axis: 'x',
option: 'from',
}),
});
}

if (invalidTo) {
throw new ChartError({
code: CHART_ERROR_CODE.INVALID_OPTION_TYPE,
message: i18n('error', 'label_invalid-axis-plot-band-option', {
axis: 'x',
option: 'to',
}),
});
}

break;
}
case 'linear':
case 'datetime': {
const invalidFrom = typeof from !== 'number';
const invalidTo = typeof to !== 'number';
if (invalidFrom) {
throw new ChartError({
code: CHART_ERROR_CODE.INVALID_OPTION_TYPE,
message: i18n('error', 'label_invalid-axis-plot-band-option', {
axis: 'x',
option: 'from',
}),
});
}

if (invalidTo) {
throw new ChartError({
code: CHART_ERROR_CODE.INVALID_OPTION_TYPE,
message: i18n('error', 'label_invalid-axis-plot-band-option', {
axis: 'x',
option: 'to',
}),
});
}

break;
}
}
});

yPlotBands.forEach(({from = 0, to = 0}) => {
const fromNotEqualTo = typeof to !== typeof from;

if (fromNotEqualTo) {
throw new ChartError({
code: CHART_ERROR_CODE.INVALID_OPTION_TYPE,
message: i18n('error', 'label_axis-plot-band-options-not-equal', {
axis: 'x',
option: 'from',
}),
});
}

switch (yType) {
case 'category': {
const invalidFrom = typeof from !== 'string' && typeof from !== 'number';
const invalidTo = typeof to !== 'string' && typeof to !== 'number';
if (invalidFrom) {
throw new ChartError({
code: CHART_ERROR_CODE.INVALID_OPTION_TYPE,
message: i18n('error', 'label_invalid-axis-plot-band-option', {
axis: 'y',
option: 'from',
}),
});
}

if (invalidTo) {
throw new ChartError({
code: CHART_ERROR_CODE.INVALID_OPTION_TYPE,
message: i18n('error', 'label_invalid-axis-plot-band-option', {
axis: 'y',
option: 'to',
}),
});
}

break;
}
case 'linear':
case 'datetime': {
const invalidFrom = typeof from !== 'number';
const invalidTo = typeof to !== 'number';
if (invalidFrom) {
throw new ChartError({
code: CHART_ERROR_CODE.INVALID_OPTION_TYPE,
message: i18n('error', 'label_invalid-axis-plot-band-option', {
axis: 'y',
option: 'from',
}),
});
}

if (invalidTo) {
throw new ChartError({
code: CHART_ERROR_CODE.INVALID_OPTION_TYPE,
message: i18n('error', 'label_invalid-axis-plot-band-option', {
axis: 'y',
option: 'to',
}),
});
}

break;
}
}
});
}

export function validateStacking({series}: {series: AreaSeries | BarXSeries | BarYSeries}) {
const availableStackingValues = ['normal', 'percent'];

if (series.stacking && !availableStackingValues.includes(series.stacking)) {
throw new ChartError({
code: CHART_ERROR_CODE.INVALID_DATA,
message: i18n('error', 'label_invalid-series-property', {
key: 'stacking',
values: availableStackingValues,
}),
});
}
}
Loading
Loading