Skip to content

Commit

Permalink
[Metrics UI] Converting legend key to optional (elastic#83495) (elast…
Browse files Browse the repository at this point in the history
…ic#83649)

* [Metrics UI] Converting legend key to optional

* Adding check and default to legend component
  • Loading branch information
simianhacker authored Nov 18, 2020
1 parent 95772c1 commit 7c1ac30
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { PageContent } from '../../../../components/page';
import { useSnapshot } from '../hooks/use_snaphot';
import { useWaffleTimeContext } from '../hooks/use_waffle_time';
import { useWaffleFiltersContext } from '../hooks/use_waffle_filters';
import { useWaffleOptionsContext } from '../hooks/use_waffle_options';
import { DEFAULT_LEGEND, useWaffleOptionsContext } from '../hooks/use_waffle_options';
import { useSourceContext } from '../../../../containers/source';
import { InfraFormatterType } from '../../../../lib/lib';
import { euiStyled } from '../../../../../../observability/public';
Expand Down Expand Up @@ -61,10 +61,14 @@ export const Layout = () => {
false
);

const legendPalette = legend?.palette ?? DEFAULT_LEGEND.palette;
const legendSteps = legend?.steps ?? DEFAULT_LEGEND.steps;
const legendReverseColors = legend?.reverseColors ?? DEFAULT_LEGEND.reverseColors;

const options = {
formatter: InfraFormatterType.percent,
formatTemplate: '{{value}}',
legend: createLegend(legend.palette, legend.steps, legend.reverseColors),
legend: createLegend(legendPalette, legendSteps, legendReverseColors),
metric,
sort,
fields: source?.configuration?.fields,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import {
import { GradientLegend } from './gradient_legend';
import { LegendControls } from './legend_controls';
import { StepLegend } from './steps_legend';
import { useWaffleOptionsContext, WaffleLegendOptions } from '../../hooks/use_waffle_options';
import {
DEFAULT_LEGEND,
useWaffleOptionsContext,
WaffleLegendOptions,
} from '../../hooks/use_waffle_options';
import { SteppedGradientLegend } from './stepped_gradient_legend';
interface Props {
legend: InfraWaffleMapLegend;
Expand Down Expand Up @@ -52,7 +56,7 @@ export const Legend: React.FC<Props> = ({ dataBounds, legend, bounds, formatter
return (
<LegendContainer>
<LegendControls
options={legendOptions}
options={legendOptions != null ? legendOptions : DEFAULT_LEGEND}
dataBounds={dataBounds}
bounds={bounds}
autoBounds={autoBounds}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ import {
import { useUrlState } from '../../../../utils/use_url_state';
import { InventoryItemType, ItemTypeRT } from '../../../../../common/inventory_models/types';

export const DEFAULT_LEGEND: WaffleLegendOptions = {
palette: 'cool',
steps: 10,
reverseColors: false,
};

export const DEFAULT_WAFFLE_OPTIONS_STATE: WaffleOptionsState = {
metric: { type: 'cpu' },
groupBy: [],
Expand All @@ -34,11 +40,7 @@ export const DEFAULT_WAFFLE_OPTIONS_STATE: WaffleOptionsState = {
accountId: '',
region: '',
customMetrics: [],
legend: {
palette: 'cool',
steps: 10,
reverseColors: false,
},
legend: DEFAULT_LEGEND,
source: 'default',
sort: { by: 'name', direction: 'desc' },
};
Expand Down Expand Up @@ -183,10 +185,9 @@ export const WaffleOptionsStateRT = rt.intersection([
accountId: rt.string,
region: rt.string,
customMetrics: rt.array(SnapshotCustomMetricInputRT),
legend: WaffleLegendOptionsRT,
sort: WaffleSortOptionRT,
}),
rt.partial({ source: rt.string }),
rt.partial({ source: rt.string, legend: WaffleLegendOptionsRT }),
]);

export type WaffleSortOption = rt.TypeOf<typeof WaffleSortOptionRT>;
Expand Down

0 comments on commit 7c1ac30

Please sign in to comment.