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

feat(types): improve generic types in specs, and spec prop types #1421

Merged
merged 21 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@
"@elastic/datemath": "^5.0.3",
"@elastic/eui": "^41.3.0",
"@mdx-js/loader": "^1.6.6",
"@microsoft/api-documenter": "^7.12.7",
"@microsoft/api-extractor": "^7.18.9",
"@microsoft/api-documenter": "^7.13.63",
"@microsoft/api-extractor": "^7.18.16",
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/commit-analyzer": "^8.0.1",
"@semantic-release/exec": "^5.0.0",
Expand Down
257 changes: 145 additions & 112 deletions packages/charts/api/charts.api.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ export type ShapeViewModel = {
};

const commonDefaults = {
specType: SpecType.Series,
subtype: GoalSubtype.Goal,
base: 0,
actual: 50,
ticks: [0, 25, 50, 75, 100],
Expand All @@ -85,6 +83,8 @@ export const defaultGoalSpec = {
/** @internal */
export const nullGoalViewModel = {
...commonDefaults,
specType: SpecType.Series,
subtype: GoalSubtype.Goal,
bands: [],
ticks: [],
labelMajor: '',
Expand Down
48 changes: 17 additions & 31 deletions packages/charts/src/chart_types/goal_chart/specs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* Side Public License, v 1.
*/

import React from 'react';
import { ComponentProps } from 'react';

import { ChartType } from '../..';
import { Color } from '../../../common/colors';
import { Spec } from '../../../specs';
import { SpecType } from '../../../specs/constants';
import { getConnect, specComponentFactory } from '../../../state/spec_factory';
import { specComponentFactory } from '../../../state/spec_factory';
import { LabelAccessor, RecursivePartial } from '../../../utils/common';
import { Config } from '../layout/types/config_types';
import { defaultGoalSpec } from '../layout/types/viewmodel_types';
Expand All @@ -36,11 +36,6 @@ export type BandFillColorAccessor = (input: BandFillColorAccessorInput) => Color
/** @alpha */
export type GoalLabelAccessor = LabelAccessor<BandFillColorAccessorInput>;

const defaultProps = {
chartType: ChartType.Goal,
...defaultGoalSpec,
};

/** @alpha */
export interface GoalSpec extends Spec {
specType: typeof SpecType.Series;
Expand All @@ -67,28 +62,19 @@ export interface GoalSpec extends Spec {
bandLabels: string[];
}

type SpecRequiredProps = Pick<GoalSpec, 'id' | 'actual'>;
type SpecOptionalProps = Partial<Omit<GoalSpec, 'chartType' | 'specType' | 'id' | 'data'>>;

/** @alpha */
export const Goal: React.FunctionComponent<SpecRequiredProps & SpecOptionalProps> = getConnect()(
specComponentFactory<
GoalSpec,
| 'chartType'
| 'subtype'
| 'base'
| 'target'
| 'actual'
| 'bands'
| 'bandLabels'
| 'ticks'
| 'bandFillColor'
| 'tickValueFormatter'
| 'labelMajor'
| 'labelMinor'
| 'centralMajor'
| 'centralMinor'
| 'angleStart'
| 'angleEnd'
>(defaultProps),
/**
* Add Goal spec to chart
* @alpha
*/
export const Goal = specComponentFactory<GoalSpec>()(
{
specType: SpecType.Series,
chartType: ChartType.Goal,
},
{
...defaultGoalSpec,
},
);

/** @public */
export type GoalProps = ComponentProps<typeof Goal>;
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { ScaleType } from '../../../../scales/constants';
import { LinearScale, OrdinalScale, RasterTimeScale, SettingsSpec } from '../../../../specs';
import { withTextMeasure } from '../../../../utils/bbox/canvas_text_bbox_calculator';
import { addIntervalToTime } from '../../../../utils/chrono/elasticsearch';
import { clamp } from '../../../../utils/common';
import { clamp, Datum } from '../../../../utils/common';
import { Dimensions } from '../../../../utils/dimensions';
import { Logger } from '../../../../utils/logger';
import { Theme } from '../../../../utils/themes/theme';
Expand All @@ -36,6 +36,7 @@ import {
ShapeViewModel,
TextBox,
} from '../types/viewmodel_types';
import { BaseDatum } from './../../../xy_chart/utils/specs';

/** @public */
export interface HeatmapCellDatum {
Expand Down Expand Up @@ -71,9 +72,9 @@ function estimatedNonOverlappingTickCount(
}

/** @internal */
export function shapeViewModel(
export function shapeViewModel<D extends BaseDatum = Datum>(
textMeasure: TextMeasure,
spec: HeatmapSpec,
spec: HeatmapSpec<D>,
config: Config,
settingsSpec: SettingsSpec,
chartDimensions: Dimensions,
Expand Down
87 changes: 47 additions & 40 deletions packages/charts/src/chart_types/heatmap/specs/heatmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,22 @@
* Side Public License, v 1.
*/

import React from 'react';
import { ComponentProps, useRef } from 'react';

import { ChartType } from '../..';
import { Color } from '../../../common/colors';
import { Predicate } from '../../../common/predicate';
import { ScaleType } from '../../../scales/constants';
import { Spec } from '../../../specs';
import { BaseDatum, Spec } from '../../../specs';
import { SpecType } from '../../../specs/constants';
import { getConnect, specComponentFactory } from '../../../state/spec_factory';
import { buildSFProps, SFProps, useSpecFactory } from '../../../state/spec_factory';
import { Accessor, AccessorFn } from '../../../utils/accessor';
import { ESCalendarInterval, ESFixedInterval } from '../../../utils/chrono/elasticsearch';
import { Datum, RecursivePartial } from '../../../utils/common';
import { Datum, RecursivePartial, stripUndefined } from '../../../utils/common';
import { config } from '../layout/config/config';
import { Config } from '../layout/types/config_types';
import { X_SCALE_DEFAULT } from './scale_defaults';

const defaultProps = {
chartType: ChartType.Heatmap,
specType: SpecType.Series,
data: [],
xAccessor: ({ x }: { x: string | number }) => x,
yAccessor: ({ y }: { y: string | number }) => y,
xScale: { type: X_SCALE_DEFAULT.type },
valueAccessor: ({ value }: { value: string | number }) => value,
valueFormatter: (value: number) => `${value}`,
xSortPredicate: Predicate.AlphaAsc,
ySortPredicate: Predicate.AlphaAsc,
config,
};

/** @public */
export type HeatmapScaleType =
| typeof ScaleType.Linear
Expand Down Expand Up @@ -80,14 +66,14 @@ export interface OrdinalScale {
}

/** @alpha */
export interface HeatmapSpec extends Spec {
export interface HeatmapSpec<D extends BaseDatum = Datum> extends Spec {
specType: typeof SpecType.Series;
chartType: typeof ChartType.Heatmap;
data: Datum[];
data: D[];
colorScale: HeatmapBandsColorScale;
xAccessor: Accessor | AccessorFn;
yAccessor: Accessor | AccessorFn;
valueAccessor: Accessor | AccessorFn;
xAccessor: Accessor<D> | AccessorFn<D>;
yAccessor: Accessor<D> | AccessorFn<D>;
valueAccessor: Accessor<never> | AccessorFn;
valueFormatter: (value: number) => string;
xSortPredicate: Predicate;
ySortPredicate: Predicate;
Expand All @@ -97,20 +83,41 @@ export interface HeatmapSpec extends Spec {
name?: string;
}

/** @alpha */
export const Heatmap: React.FunctionComponent<
Pick<HeatmapSpec, 'id' | 'data' | 'colorScale'> & Partial<Omit<HeatmapSpec, 'chartType' | 'specType' | 'id' | 'data'>>
> = getConnect()(
specComponentFactory<
HeatmapSpec,
| 'xAccessor'
| 'yAccessor'
| 'valueAccessor'
| 'data'
| 'ySortPredicate'
| 'xSortPredicate'
| 'valueFormatter'
| 'config'
| 'xScale'
>(defaultProps),
);
/**
* Adds heatmap spec to chart specs
* @alpha
*/
export const Heatmap = function <D extends BaseDatum = Datum>(
props: SFProps<
HeatmapSpec<D>,
keyof typeof buildProps.current['overrides'],
keyof typeof buildProps.current['defaults'],
keyof typeof buildProps.current['optionals'],
keyof typeof buildProps.current['requires']
>,
) {
const buildProps = useRef(
// @ts-ignore - excessively deep types
buildSFProps<HeatmapSpec<D>>()(
{
chartType: ChartType.Heatmap,
specType: SpecType.Series,
},
{
data: [],
valueAccessor: ({ value }) => value,
xScale: { type: X_SCALE_DEFAULT.type },
valueFormatter: (value) => `${value}`,
xSortPredicate: Predicate.AlphaAsc,
ySortPredicate: Predicate.AlphaAsc,
config,
},
),
);
const { defaults, overrides } = buildProps.current;
useSpecFactory<HeatmapSpec<D>>({ ...defaults, ...stripUndefined(props), ...overrides });
return null;
};

/** @public */
export type HeatmapProps = ComponentProps<typeof Heatmap>;
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const getHeatmapTableSelector = createCustomCachedSelector(
const resultData = data.reduce<HeatmapTable>(
(acc, curr, index) => {
const x = getAccessorValue(curr, xAccessor);

const y = getAccessorValue(curr, yAccessor);
const value = getAccessorValue(curr, valueAccessor);

Expand Down
Loading