Skip to content

Commit

Permalink
Move logic inside chart_option component
Browse files Browse the repository at this point in the history
  • Loading branch information
VladLasitsa committed Apr 6, 2021
1 parent 0904e11 commit bc7e3be
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 38 deletions.

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

Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
*/

import React from 'react';
import { shallow } from 'enzyme';
import { shallow, mount } from 'enzyme';

import { ChartOptions, ChartOptionsParams } from './chart_options';
import { SeriesParam, ChartMode } from '../../../../types';
import { SeriesParam, ChartMode, AxisMode } from '../../../../types';
import { LineOptions } from './line_options';
import { valueAxis, seriesParam } from './mocks';
import { ChartType } from '../../../../../common';
Expand Down Expand Up @@ -71,4 +71,14 @@ describe('ChartOptions component', () => {

expect(setParamByIndex).toBeCalledWith('seriesParams', 0, paramName, ChartMode.Normal);
});

it('should set "stacked" mode and disabled control if the referenced axis is "percentage"', () => {
defaultProps.valueAxes[0].scale.mode = AxisMode.Percentage;
defaultProps.chart.mode = ChartMode.Normal;
const paramName = 'mode';
const comp = mount(<ChartOptions {...defaultProps} />);

expect(setParamByIndex).toBeCalledWith('seriesParams', 0, paramName, ChartMode.Stacked);
expect(comp.find({ paramName }).prop('disabled')).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
* Side Public License, v 1.
*/

import React, { useMemo, useCallback } from 'react';
import React, { useMemo, useCallback, useEffect, useState } from 'react';

import { i18n } from '@kbn/i18n';
import { EuiFlexGroup, EuiFlexItem, EuiSpacer } from '@elastic/eui';

import { SelectOption } from '../../../../../../vis_default_editor/public';

import { SeriesParam, ValueAxis } from '../../../../types';
import { SeriesParam, ValueAxis, ChartMode, AxisMode } from '../../../../types';
import { LineOptions } from './line_options';
import { SetParamByIndex, ChangeValueAxis } from '.';
import { ChartType } from '../../../../../common';
Expand All @@ -38,6 +38,7 @@ function ChartOptions({
changeValueAxis,
setParamByIndex,
}: ChartOptionsParams) {
const [disabledMode, setDisabledMode] = useState<boolean>(false);
const setChart: SetChart = useCallback(
(paramName, value) => {
setParamByIndex('seriesParams', index, paramName, value);
Expand Down Expand Up @@ -68,6 +69,20 @@ function ChartOptions({
[valueAxes]
);

useEffect(() => {
const valueAxisToMetric = valueAxes.find((valueAxis) => valueAxis.id === chart.valueAxis);
if (valueAxisToMetric) {
if (valueAxisToMetric.scale.mode === AxisMode.Percentage) {
setDisabledMode(true);
if (chart.mode !== ChartMode.Stacked) {
setChart('mode', ChartMode.Stacked);
}
} else if (disabledMode) {
setDisabledMode(false);
}
}
}, [valueAxes, chart, disabledMode, setChart, setDisabledMode]);

return (
<>
<SelectOption
Expand Down Expand Up @@ -104,7 +119,7 @@ function ChartOptions({
})}
options={collections.chartModes}
paramName="mode"
disabled={chart.disabledMode}
disabled={disabledMode}
value={chart.mode}
setValue={setChart}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,6 @@ describe('MetricsAxisOptions component', () => {
const updatedSeries = [{ ...chart, data: { id: agg.id, label: agg.makeLabel() } }];
expect(setValue).toHaveBeenCalledWith(SERIES_PARAMS, updatedSeries);
});

it('should set "stacked" mode for metric if the referenced axis is "percentage"', () => {
defaultProps.stateParams.valueAxes[0].scale.mode = AxisMode.Percentage;
defaultProps.stateParams.seriesParams[0].mode = ChartMode.Normal;
mount(<MetricsAxisOptions {...defaultProps} />);

const updatedSeries = [{ ...chart, disabledMode: true, mode: ChartMode.Stacked }];
expect(setValue).toHaveBeenCalledWith(SERIES_PARAMS, updatedSeries);
});
});

describe('updateAxisTitle', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ import { EuiSpacer } from '@elastic/eui';

import { IAggConfig } from '../../../../../../data/public';

import {
VisParams,
ValueAxis,
SeriesParam,
CategoryAxis,
ChartMode,
AxisMode,
} from '../../../../types';
import { VisParams, ValueAxis, SeriesParam, CategoryAxis } from '../../../../types';
import { ValidationVisOptionsProps } from '../../common';
import { SeriesPanel } from './series_panel';
import { CategoryAxisPanel } from './category_axis_panel';
Expand Down Expand Up @@ -310,18 +303,6 @@ function MetricsAxisOptions(props: ValidationVisOptionsProps<VisParams>) {
);
}

const valueAxisToMetric = stateParams.valueAxes.find(
(valueAxis) => valueAxis.id === series.valueAxis
);
if (valueAxisToMetric) {
if (valueAxisToMetric.scale.mode === AxisMode.Percentage) {
series.mode = ChartMode.Stacked;
series.disabledMode = true;
} else if (series.disabledMode) {
series.disabledMode = false;
}
}

return series;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ const seriesParam: SeriesParam = {
show: true,
type: ChartType.Histogram,
mode: ChartMode.Stacked,
disabledMode: false,
data: {
label: 'Count',
id: '1',
Expand Down
1 change: 0 additions & 1 deletion src/plugins/vis_type_xy/public/types/param.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export interface SeriesParam {
interpolate?: InterpolationMode;
lineWidth?: number;
mode: ChartMode;
disabledMode: boolean;
show: boolean;
showCircles: boolean;
type: ChartType;
Expand Down

0 comments on commit bc7e3be

Please sign in to comment.