2019-10-02 19:21:39 +02:00
|
|
|
/*
|
|
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
2021-02-04 03:12:39 +01:00
|
|
|
* or more contributor license agreements. Licensed under the Elastic License
|
|
|
|
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
|
|
* 2.0.
|
2019-10-02 19:21:39 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { EuiIconType } from '@elastic/eui/src/components/icon/icon';
|
2020-11-17 10:19:13 +01:00
|
|
|
import { FramePublicAPI, DatasourcePublicAPI } from '../types';
|
2021-02-03 12:03:55 +01:00
|
|
|
import { SeriesType, visualizationTypes, XYLayerConfig, YConfig, ValidLayer } from './types';
|
2019-10-02 19:21:39 +02:00
|
|
|
|
|
|
|
export function isHorizontalSeries(seriesType: SeriesType) {
|
2020-09-15 11:24:02 +02:00
|
|
|
return (
|
|
|
|
seriesType === 'bar_horizontal' ||
|
|
|
|
seriesType === 'bar_horizontal_stacked' ||
|
|
|
|
seriesType === 'bar_horizontal_percentage_stacked'
|
|
|
|
);
|
2019-10-02 19:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isHorizontalChart(layers: Array<{ seriesType: SeriesType }>) {
|
2020-05-22 09:08:58 +02:00
|
|
|
return layers.every((l) => isHorizontalSeries(l.seriesType));
|
2019-10-02 19:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getIconForSeries(type: SeriesType): EuiIconType {
|
2020-05-22 09:08:58 +02:00
|
|
|
const definition = visualizationTypes.find((t) => t.id === type);
|
2019-10-02 19:21:39 +02:00
|
|
|
|
|
|
|
if (!definition) {
|
|
|
|
throw new Error(`Unknown series type ${type}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (definition.icon as EuiIconType) || 'empty';
|
|
|
|
}
|
2020-07-03 13:22:47 +02:00
|
|
|
|
2021-02-03 12:03:55 +01:00
|
|
|
export const getSeriesColor = (layer: XYLayerConfig, accessor: string) => {
|
2020-07-03 13:22:47 +02:00
|
|
|
if (layer.splitAccessor) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
layer?.yConfig?.find((yConfig: YConfig) => yConfig.forAccessor === accessor)?.color || null
|
|
|
|
);
|
|
|
|
};
|
2020-11-06 16:34:30 +01:00
|
|
|
|
2021-02-03 12:03:55 +01:00
|
|
|
export const getColumnToLabelMap = (layer: XYLayerConfig, datasource: DatasourcePublicAPI) => {
|
2020-11-17 10:19:13 +01:00
|
|
|
const columnToLabel: Record<string, string> = {};
|
|
|
|
|
|
|
|
layer.accessors.concat(layer.splitAccessor ? [layer.splitAccessor] : []).forEach((accessor) => {
|
|
|
|
const operation = datasource.getOperationForColumnId(accessor);
|
|
|
|
if (operation?.label) {
|
|
|
|
columnToLabel[accessor] = operation.label;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return columnToLabel;
|
|
|
|
};
|
|
|
|
|
2020-11-06 16:34:30 +01:00
|
|
|
export function hasHistogramSeries(
|
|
|
|
layers: ValidLayer[] = [],
|
|
|
|
datasourceLayers?: FramePublicAPI['datasourceLayers']
|
|
|
|
) {
|
|
|
|
if (!datasourceLayers) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const validLayers = layers.filter(({ accessors }) => accessors.length);
|
|
|
|
|
|
|
|
return validLayers.some(({ layerId, xAccessor }: ValidLayer) => {
|
|
|
|
const xAxisOperation = datasourceLayers[layerId].getOperationForColumnId(xAccessor);
|
|
|
|
return (
|
|
|
|
xAxisOperation &&
|
|
|
|
xAxisOperation.isBucketed &&
|
|
|
|
xAxisOperation.scale &&
|
|
|
|
xAxisOperation.scale !== 'ordinal'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|