Skip to content

Commit

Permalink
[Lens] Adds fill opacity slider
Browse files Browse the repository at this point in the history
  • Loading branch information
stratoula committed May 24, 2021
1 parent 210395d commit 0965a6e
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function PointOptions({ chart, setChart }: PointOptionsParams) {
/>
<EuiSpacer size="m" />
<EuiFormRow
label={i18n.translate('isTypeXy.controls.pointSeries.series.dotsRadius', {
label={i18n.translate('visTypeXy.controls.pointSeries.series.dotsRadius', {
defaultMessage: 'Dots size',
})}
fullWidth
Expand Down

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

7 changes: 7 additions & 0 deletions x-pack/plugins/lens/public/xy_visualization/expression.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ export const xyChart: ExpressionFunctionDefinition<
defaultMessage: 'Define how curve type is rendered for a line chart',
}),
},
fillOpacity: {
types: ['number'],
help: i18n.translate('xpack.lens.xyChart.fillOpacity.help', {
defaultMessage: 'Define the area chart fill opacity',
}),
},
hideEndzones: {
types: ['boolean'],
default: false,
Expand Down Expand Up @@ -812,6 +818,7 @@ export function XYChart({
visible: !xAccessor,
radius: 5,
},
...(args.fillOpacity && { area: { opacity: args.fillOpacity } }),
},
lineSeriesStyle: {
point: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export const buildExpression = (
],
fittingFunction: [state.fittingFunction || 'None'],
curveType: [state.curveType || 'LINEAR'],
fillOpacity: [state.fillOpacity || 0.3],
yLeftExtent: [
{
type: 'expression',
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/lens/public/xy_visualization/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ export interface XYArgs {
tickLabelsVisibilitySettings?: AxesSettingsConfig & { type: 'lens_xy_tickLabelsConfig' };
gridlinesVisibilitySettings?: AxesSettingsConfig & { type: 'lens_xy_gridlinesConfig' };
curveType?: XYCurveType;
fillOpacity?: number;
hideEndzones?: boolean;
}

Expand All @@ -485,6 +486,7 @@ export interface XYState {
tickLabelsVisibilitySettings?: AxesSettingsConfig;
gridlinesVisibilitySettings?: AxesSettingsConfig;
curveType?: XYCurveType;
fillOpacity?: number;
hideEndzones?: boolean;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* 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.
*/

import React from 'react';
import { mountWithIntl as mount, shallowWithIntl as shallow } from '@kbn/test/jest';
import { EuiRange } from '@elastic/eui';
import { FillOpacityOption } from './fill_opacity_option';

describe('Line curve option', () => {
it('should show currently selected opacity value', () => {
const component = shallow(<FillOpacityOption onChange={jest.fn()} value={0.3} />);

expect(component.find(EuiRange).prop('value')).toEqual(0.3);
});

it('should show fill opacity option when enabled', () => {
const component = mount(
<FillOpacityOption onChange={jest.fn()} value={0.3} isFillOpacityEnabled={true} />
);

expect(component.exists('[data-test-subj="lnsFillOpacity"]')).toEqual(true);
});

it('should hide curve option when disabled', () => {
const component = mount(
<FillOpacityOption onChange={jest.fn()} value={1} isFillOpacityEnabled={false} />
);

expect(component.exists('[data-test-subj="lnsFillOpacity"]')).toEqual(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* 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.
*/

import React from 'react';
import { i18n } from '@kbn/i18n';
import { EuiFormRow, EuiSpacer, EuiRange } from '@elastic/eui';

export interface FillOpacityOptionProps {
/**
* Currently selected value
*/
value: number;
/**
* Callback on display option change
*/
onChange: (value: number) => void;
isFillOpacityEnabled?: boolean;
}

export const FillOpacityOption: React.FC<FillOpacityOptionProps> = ({
onChange,
value,
isFillOpacityEnabled = true,
}) => {
return isFillOpacityEnabled ? (
<>
<EuiFormRow
display="columnCompressedSwitch"
label={i18n.translate('xpack.lens.xyChart.fillOpacityLabel', {
defaultMessage: 'Fill opacity',
})}
>
<EuiRange
data-test-subj="lnsFillOpacity"
value={value}
min={0.1}
max={1}
step={0.1}
showInput
compressed
onChange={(e) => {
onChange(Number(e.currentTarget.value));
}}
/>
</EuiFormRow>
<EuiSpacer />
</>
) : null;
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { State } from '../types';
import { VisualOptionsPopover } from './visual_options_popover';
import { ToolbarPopover } from '../../shared_components';
import { MissingValuesOptions } from './missing_values_option';
import { FillOpacityOption } from './fill_opacity_option';

describe('Visual options popover', () => {
let frame: FramePublicAPI;
Expand Down Expand Up @@ -74,6 +75,22 @@ describe('Visual options popover', () => {
expect(component.find(MissingValuesOptions).prop('isFittingEnabled')).toEqual(false);
});

it('should not disable the fill opacity for percentage area charts', () => {
const state = testState();
const component = shallow(
<VisualOptionsPopover
datasourceLayers={frame.datasourceLayers}
setState={jest.fn()}
state={{
...state,
layers: [{ ...state.layers[0], seriesType: 'area_percentage_stacked' }],
}}
/>
);

expect(component.find(FillOpacityOption).prop('isFillOpacityEnabled')).toEqual(true);
});

it('should not disable the visual options for percentage area charts', () => {
const state = testState();
const component = shallow(
Expand Down Expand Up @@ -128,6 +145,40 @@ describe('Visual options popover', () => {
expect(component.find(MissingValuesOptions).prop('isFittingEnabled')).toEqual(false);
});

it('should hide the fill opacity option for bar series', () => {
const state = testState();
const component = shallow(
<VisualOptionsPopover
datasourceLayers={frame.datasourceLayers}
setState={jest.fn()}
state={{
...state,
layers: [{ ...state.layers[0], seriesType: 'bar_horizontal' }],
fittingFunction: 'Carry',
}}
/>
);

expect(component.find(FillOpacityOption).prop('isFillOpacityEnabled')).toEqual(false);
});

it('should hide the fill opacity option for line series', () => {
const state = testState();
const component = shallow(
<VisualOptionsPopover
datasourceLayers={frame.datasourceLayers}
setState={jest.fn()}
state={{
...state,
layers: [{ ...state.layers[0], seriesType: 'line' }],
fittingFunction: 'Carry',
}}
/>
);

expect(component.find(FillOpacityOption).prop('isFillOpacityEnabled')).toEqual(false);
});

it('should show the popover and display field enabled for bar and horizontal_bar series', () => {
const state = testState();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { i18n } from '@kbn/i18n';
import { ToolbarPopover } from '../../shared_components';
import { MissingValuesOptions } from './missing_values_option';
import { LineCurveOption } from './line_curve_option';
import { FillOpacityOption } from './fill_opacity_option';
import { XYState } from '../types';
import { hasHistogramSeries } from '../state_helpers';
import { ValidLayer } from '../types';
Expand Down Expand Up @@ -61,6 +62,10 @@ export const VisualOptionsPopover: React.FC<VisualOptionsPopoverProps> = ({
['bar', 'bar_horizontal'].includes(seriesType)
);

const hasAreaSeries = state?.layers.some(({ seriesType }) =>
['area_stacked', 'area', 'area_percentage_stacked'].includes(seriesType)
);

const isHistogramSeries = Boolean(
hasHistogramSeries(state?.layers as ValidLayer[], datasourceLayers)
);
Expand Down Expand Up @@ -110,6 +115,17 @@ export const VisualOptionsPopover: React.FC<VisualOptionsPopoverProps> = ({
setState({ ...state, fittingFunction: newVal });
}}
/>

<FillOpacityOption
isFillOpacityEnabled={hasAreaSeries}
value={state?.fillOpacity ?? 0.3}
onChange={(newValue) => {
setState({
...state,
fillOpacity: newValue,
});
}}
/>
</ToolbarPopover>
</TooltipWrapper>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ function buildSuggestion({
valueLabels: currentState?.valueLabels || 'hide',
fittingFunction: currentState?.fittingFunction || 'None',
curveType: currentState?.curveType,
fillOpacity: currentState?.fillOpacity,
xTitle: currentState?.xTitle,
yTitle: currentState?.yTitle,
yRightTitle: currentState?.yRightTitle,
Expand Down

0 comments on commit 0965a6e

Please sign in to comment.