Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lens] Add ability to set colors for y-axis series #70311

Merged
merged 20 commits into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion x-pack/plugins/lens/public/xy_visualization/state_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { EuiIconType } from '@elastic/eui/src/components/icon/icon';
import { SeriesType, visualizationTypes } from './types';
import { SeriesType, visualizationTypes, LayerConfig, YConfig } from './types';

export function isHorizontalSeries(seriesType: SeriesType) {
return seriesType === 'bar_horizontal' || seriesType === 'bar_horizontal_stacked';
Expand All @@ -24,3 +24,12 @@ export function getIconForSeries(type: SeriesType): EuiIconType {

return (definition.icon as EuiIconType) || 'empty';
}

export const getSeriesColor = (layer: LayerConfig, accessor: string) => {
if (layer.splitAccessor) {
return null;
}
return (
layer?.yConfig?.find((yConfig: YConfig) => yConfig.forAccessor === accessor)?.color || null
);
};
3 changes: 2 additions & 1 deletion x-pack/plugins/lens/public/xy_visualization/to_expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ export const buildExpression = (
function: 'lens_xy_yConfig',
arguments: {
forAccessor: [yConfig.forAccessor],
axisMode: [yConfig.axisMode],
axisMode: yConfig.axisMode ? [yConfig.axisMode] : [],
color: yConfig.color ? [yConfig.color] : [],
},
},
],
Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugins/lens/public/xy_visualization/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ export const yAxisConfig: ExpressionFunctionDefinition<
options: ['auto', 'left', 'right'],
help: 'The axis mode of the metric',
},
color: {
types: ['string'],
help: 'The color of the series',
},
},
fn: function fn(input: unknown, args: YConfig) {
return {
Expand Down Expand Up @@ -195,6 +199,7 @@ export type YAxisMode = 'auto' | 'left' | 'right';
export interface YConfig {
forAccessor: string;
axisMode?: YAxisMode;
color?: string;
}

export interface LayerConfig {
Expand Down
204 changes: 145 additions & 59 deletions x-pack/plugins/lens/public/xy_visualization/xy_config_panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import React, { useState } from 'react';
import { i18n } from '@kbn/i18n';
import { EuiButtonGroup, EuiFormRow, htmlIdGenerator } from '@elastic/eui';
import {
EuiButtonGroup,
EuiFormRow,
htmlIdGenerator,
EuiForm,
EuiColorPicker,
EuiColorPickerProps,
EuiToolTip,
} from '@elastic/eui';
import { State, SeriesType, visualizationTypes, YAxisMode } from './types';
import { VisualizationDimensionEditorProps, VisualizationLayerWidgetProps } from '../types';
import { isHorizontalChart, isHorizontalSeries } from './state_helpers';
import { isHorizontalChart, isHorizontalSeries, getSeriesColor } from './state_helpers';
import { trackUiEvent } from '../lens_ui_telemetry';

type UnwrapArray<T> = T extends Array<infer P> ? P : T;
Expand Down Expand Up @@ -70,70 +78,148 @@ export function LayerContextMenu(props: VisualizationLayerWidgetProps<State>) {

const idPrefix = htmlIdGenerator()();

export function DimensionEditor({
state,
setState,
layerId,
accessor,
}: VisualizationDimensionEditorProps<State>) {
export function DimensionEditor(props: VisualizationDimensionEditorProps<State>) {
const { state, setState, layerId, accessor } = props;
const index = state.layers.findIndex((l) => l.layerId === layerId);
const layer = state.layers[index];
const axisMode =
(layer.yConfig &&
layer.yConfig?.find((yAxisConfig) => yAxisConfig.forAccessor === accessor)?.axisMode) ||
'auto';

return (
<EuiFormRow
display="columnCompressed"
label={i18n.translate('xpack.lens.xyChart.axisSide.label', {
defaultMessage: 'Axis side',
})}
>
<EuiButtonGroup
legend={i18n.translate('xpack.lens.xyChart.axisSide.label', {
<EuiForm>
<EuiFormRow
display="columnCompressed"
mbondyra marked this conversation as resolved.
Show resolved Hide resolved
fullWidth
label={i18n.translate('xpack.lens.xyChart.seriesColor.label', {
defaultMessage: 'Series color',
})}
>
<ColorPicker {...props} />
</EuiFormRow>
<EuiFormRow
display="columnCompressed"
fullWidth
label={i18n.translate('xpack.lens.xyChart.axisSide.label', {
defaultMessage: 'Axis side',
})}
name="axisSide"
buttonSize="compressed"
className="eui-displayInlineBlock"
options={[
{
id: `${idPrefix}auto`,
label: i18n.translate('xpack.lens.xyChart.axisSide.auto', {
defaultMessage: 'Auto',
}),
},
{
id: `${idPrefix}left`,
label: i18n.translate('xpack.lens.xyChart.axisSide.left', {
defaultMessage: 'Left',
}),
},
{
id: `${idPrefix}right`,
label: i18n.translate('xpack.lens.xyChart.axisSide.right', {
defaultMessage: 'Right',
}),
},
]}
idSelected={`${idPrefix}${axisMode}`}
onChange={(id) => {
const newMode = id.replace(idPrefix, '') as YAxisMode;
const newYAxisConfigs = [...(layer.yConfig || [])];
const existingIndex = newYAxisConfigs.findIndex(
(yAxisConfig) => yAxisConfig.forAccessor === accessor
);
if (existingIndex !== -1) {
newYAxisConfigs[existingIndex].axisMode = newMode;
} else {
newYAxisConfigs.push({
forAccessor: accessor,
axisMode: newMode,
});
}
setState(updateLayer(state, { ...layer, yConfig: newYAxisConfigs }, index));
}}
/>
</EuiFormRow>
>
<EuiButtonGroup
legend={i18n.translate('xpack.lens.xyChart.axisSide.label', {
defaultMessage: 'Axis side',
})}
name="axisSide"
buttonSize="compressed"
mbondyra marked this conversation as resolved.
Show resolved Hide resolved
className="eui-displayInlineBlock"
options={[
{
id: `${idPrefix}auto`,
label: i18n.translate('xpack.lens.xyChart.axisSide.auto', {
defaultMessage: 'Auto',
}),
},
{
id: `${idPrefix}left`,
label: i18n.translate('xpack.lens.xyChart.axisSide.left', {
defaultMessage: 'Left',
}),
},
{
id: `${idPrefix}right`,
label: i18n.translate('xpack.lens.xyChart.axisSide.right', {
defaultMessage: 'Right',
}),
},
]}
idSelected={`${idPrefix}${axisMode}`}
onChange={(id) => {
const newMode = id.replace(idPrefix, '') as YAxisMode;
const newYAxisConfigs = [...(layer.yConfig || [])];
const existingIndex = newYAxisConfigs.findIndex(
(yAxisConfig) => yAxisConfig.forAccessor === accessor
);
if (existingIndex !== -1) {
newYAxisConfigs[existingIndex].axisMode = newMode;
} else {
newYAxisConfigs.push({
forAccessor: accessor,
axisMode: newMode,
});
}
setState(updateLayer(state, { ...layer, yConfig: newYAxisConfigs }, index));
}}
/>
</EuiFormRow>
</EuiForm>
);
}

const tooltipContent = {
auto: i18n.translate('xpack.lens.configPanel.color.tooltip.auto', {
defaultMessage: 'Lens automatically picks colors for you unless you specify a custom color.',
}),
custom: i18n.translate('xpack.lens.configPanel.color.tooltip.custom', {
defaultMessage: 'Clear the custom color to return to “Auto” mode.',
}),
disabled: i18n.translate('xpack.lens.configPanel.color.tooltip.disabled', {
defaultMessage:
'Individual series cannot be custom colored when the layer includes a “Split by.“',
mbondyra marked this conversation as resolved.
Show resolved Hide resolved
}),
};

const ColorPicker = ({
state,
setState,
layerId,
accessor,
}: VisualizationDimensionEditorProps<State>) => {
const index = state.layers.findIndex((l) => l.layerId === layerId);
const layer = state.layers[index];
const disabled = !!layer.splitAccessor;

const [color, setColor] = useState(getSeriesColor(layer, accessor));

const handleColor: EuiColorPickerProps['onChange'] = (text, output) => {
setColor(text);

if (output.isValid || text === '') {
const newYConfigs = [...(layer.yConfig || [])];
const existingIndex = newYConfigs.findIndex((yConfig) => yConfig.forAccessor === accessor);
if (existingIndex !== -1) {
if (text === '') {
delete newYConfigs[existingIndex].color;
} else {
newYConfigs[existingIndex].color = output.hex;
}
} else {
newYConfigs.push({
forAccessor: accessor,
color: output.hex,
});
}
setState(updateLayer(state, { ...layer, yConfig: newYConfigs }, index));
mbondyra marked this conversation as resolved.
Show resolved Hide resolved
}
};

return (
<EuiToolTip
position="top"
content={
disabled ? tooltipContent.disabled : color ? tooltipContent.custom : tooltipContent.auto
}
delay="long"
mbondyra marked this conversation as resolved.
Show resolved Hide resolved
anchorClassName="eui-displayBlock"
>
<EuiColorPicker
compressed
onChange={handleColor}
color={disabled ? '' : color}
disabled={disabled}
aria-label={i18n.translate('xpack.lens.xyChart.seriesColor.label', {
defaultMessage: 'Series color',
})}
/>
</EuiToolTip>
);
};
Loading