Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ import { EMPTY_LEGEND_ITEM_EXTRA_VALUES } from '../../../../common/legend';
import type { SeriesKey } from '../../../../common/series_id';
import { ScaleType } from '../../../../scales/constants';
import { createCustomCachedSelector } from '../../../../state/create_selector';
import { getLegendConfigSelector } from '../../../../state/selectors/get_legend_config_selector';
import { getLegendItemExtraValues } from '../../tooltip/tooltip';

/** @internal */
export const getLegendItemExtraValuesSelector = createCustomCachedSelector(
[getTooltipInfoAndGeomsSelector, getComputedScalesSelector],
({ tooltip: { values } }, { xScale: { type } }): Map<SeriesKey, LegendItemExtraValues> =>
[getTooltipInfoAndGeomsSelector, getComputedScalesSelector, getLegendConfigSelector],
({ tooltip: { values } }, { xScale: { type } }, { legendValues }): Map<SeriesKey, LegendItemExtraValues> =>
// See https://github.com/elastic/elastic-charts/issues/2050
type === ScaleType.Ordinal ? EMPTY_LEGEND_ITEM_EXTRA_VALUES : getLegendItemExtraValues(values),
type === ScaleType.Ordinal || legendValues.length === 0
? EMPTY_LEGEND_ITEM_EXTRA_VALUES
: getLegendItemExtraValues(values),
);
31 changes: 16 additions & 15 deletions packages/charts/src/state/chart_selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ export interface LegendItemLabel {
depth: number;
}

/** @internal */
export const EMPTY_LEGEND_ITEM_LIST: LegendItemLabel[] = [];

/**
* A set of chart-type-dependant functions that are required by all chart types
* @internal
Expand Down Expand Up @@ -148,10 +145,19 @@ export interface ChartSelectors {
canDisplayChartTitles(globalState: GlobalChartState): boolean;
}

/** @internal */
export type ChartSelectorsFactory = () => ChartSelectors;
type ChartSelectorsFactory = () => ChartSelectors;

const EMPTY_LEGEND_ITEM_LIST: LegendItemLabel[] = [];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const EMPTY_LEGEND_ITEM_LIST: LegendItemLabel[] = [];
const EMPTY_LEGEND_ITEM_LIST: LegendItemLabel[] = Object.freeze([]);

Do you think it's too much to use Object.freeze on all these? It's unlikely they would change based on our aversion to mutations but still you never know. I don't think freeze affects runtime performance in any meaningful way.

Up to you...

const EMPTY_TOOLTIP = Object.freeze({ header: null, values: [] });
const EMPTY_DIMENSION = Object.freeze({ top: 0, left: 0, width: 0, height: 0 });
const EMPTY_SM_DOMAINS: SmallMultiplesSeriesDomains = Object.freeze({ smVDomain: [], smHDomain: [] });
const EMPTY_OBJ = Object.freeze({});
const EMPTY_TOOLTIP_VISIBILITY: TooltipVisibility = Object.freeze({
visible: false,
isExternal: false,
displayOnly: false,
isPinnable: false,
});

type CallbackCreator = () => (state: GlobalChartState) => void;

Expand All @@ -173,20 +179,15 @@ export const createChartSelectorsFactory =
getLegendItemsLabels: () => EMPTY_LEGEND_ITEM_LIST,
getLegendExtraValues: () => EMPTY_LEGEND_ITEM_EXTRA_VALUES,
getPointerCursor: () => DEFAULT_CSS_CURSOR,
isTooltipVisible: () => ({
visible: false,
isExternal: false,
displayOnly: false,
isPinnable: false,
}),
isTooltipVisible: () => EMPTY_TOOLTIP_VISIBILITY,
getTooltipInfo: () => EMPTY_TOOLTIP,
getTooltipAnchor: () => null,
getProjectionContainerArea: () => ({ top: 0, left: 0, width: 0, height: 0 }),
getMainProjectionArea: () => ({ top: 0, left: 0, width: 0, height: 0 }),
getProjectionContainerArea: () => EMPTY_DIMENSION,
getMainProjectionArea: () => EMPTY_DIMENSION,
getBrushArea: () => null,
getDebugState: () => ({}),
getDebugState: () => EMPTY_OBJ,
getChartTypeDescription: () => '',
getSmallMultiplesDomains: () => ({ smVDomain: [], smHDomain: [] }),
getSmallMultiplesDomains: () => EMPTY_SM_DOMAINS,
canDisplayChartTitles: () => true,
...overrides,
eventCallbacks: (state: GlobalChartState) => {
Expand Down