Skip to content
This repository has been archived by the owner on Jun 25, 2020. It is now read-only.

Commit

Permalink
feat: add types back
Browse files Browse the repository at this point in the history
  • Loading branch information
kristw committed Mar 14, 2019
1 parent 054376a commit 9c3c907
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/superset-ui-preset-chart-xy/src/encodeable/types/Axis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
interface Axis {
title: string;
tickCount: number;
format: string;
}

export type XAxis = Axis & {
orient: 'top' | 'bottom';
labelAngle: number;
labelOverlap: string;
};

export interface WithXAxis {
axis?: XAxis;
}

export type YAxis = Axis & {
orient: 'left' | 'right';
};

export interface WithYAxis {
axis?: YAxis;
}

export interface WithAxis {
axis?: XAxis | YAxis;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// eslint-disable-next-line import/prefer-default-export
export interface ChannelOptions {
namespace?: string;
legend?: boolean;
}
12 changes: 12 additions & 0 deletions packages/superset-ui-preset-chart-xy/src/encodeable/types/Data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export type PlainObject<Key extends string = string, Value extends any = any> = {
[key in Key]: Value
};

export type Dataset<T extends string = string> = {
keys: T[];
values: Partial<PlainObject<T>>[];
};

export function verifyDataset<T extends string>(input: Dataset<T>): Dataset<T> {
return input;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// eslint-disable no-unused-vars
import { ValueDef, Value } from 'vega-lite/build/src/fielddef';
import { Type } from 'vega-lite/build/src/type';
import { TimeFormatter } from '@superset-ui/time-format';
import { NumberFormatter } from '@superset-ui/number-format';
import { WithScale } from './Scale';
import { WithXAxis, WithYAxis, WithAxis } from './Axis';
import { WithLegend } from './Legend';

export type Formatter = NumberFormatter | TimeFormatter | ((d: any) => string);

// ValueDef is { value: xxx }

// FieldDef is { field: 'fieldName', type: ... }

export interface FieldDef {
field: string;
format?: string;
}

export interface TypedFieldDef extends FieldDef {
type: Type;
}

export type TextFieldDef = FieldDef;

// PropFieldDef is { field: 'fieldName', scale: xxx }

type ScaleFieldDef<V extends Value = Value> = TypedFieldDef & WithScale<V>;

export type MarkPropFieldDef<V extends Value = Value> = ScaleFieldDef<V> & WithLegend;

// PositionFieldDef is { field: 'fieldName', scale: xxx, axis: xxx }

type PositionFieldDefBase<V extends Value = Value> = ScaleFieldDef<V>;

export type XFieldDef<V extends Value = Value> = PositionFieldDefBase<V> & WithXAxis;

export type YFieldDef<V extends Value = Value> = PositionFieldDefBase<V> & WithYAxis;

export type PositionFieldDef<V extends Value = Value> = ScaleFieldDef<V> & WithAxis;

export type MarkPropChannelDef<V extends Value = Value> = MarkPropFieldDef<V> | ValueDef<V>;

export type TextChannelDef = TextFieldDef | ValueDef<string>;

export type ChannelDef<V extends Value = Value> =
| XFieldDef<V>
| YFieldDef<V>
| MarkPropFieldDef<V>
| TextFieldDef
| ValueDef<V>;

export function isValueDef<V extends Value>(channelDef: ChannelDef<V>): channelDef is ValueDef<V> {
return channelDef && 'value' in channelDef && !!channelDef.value;
}

export function isFieldDef<V extends Value>(channelDef: ChannelDef<V>): channelDef is FieldDef {
return channelDef && 'field' in channelDef && !!channelDef.field;
}

export function isTypedFieldDef<V extends Value>(
channelDef: ChannelDef<V>,
): channelDef is TypedFieldDef {
return isFieldDef(channelDef) && 'type' in channelDef && !!channelDef.type;
}

export function isScaleFieldDef<V extends Value>(
channelDef: ChannelDef<V>,
): channelDef is ScaleFieldDef {
return channelDef && ('scale' in channelDef || 'sort' in channelDef);
}

export function isMarkPropFieldDef<V extends Value>(
channelDef: ChannelDef<V>,
): channelDef is MarkPropFieldDef {
return channelDef && 'legend' in channelDef;
}

export function isPositionFieldDef<V extends Value>(
channelDef: ChannelDef<V>,
): channelDef is PositionFieldDef<V> {
return channelDef && ('axis' in channelDef || 'stack' in channelDef || 'impute' in channelDef);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type Legend = boolean | null;

export interface WithLegend {
legend?: Legend;
}
15 changes: 15 additions & 0 deletions packages/superset-ui-preset-chart-xy/src/encodeable/types/Scale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ScaleType } from 'vega-lite/build/src/scale';
import { Value } from 'vega-lite/build/src/fielddef';

export { ScaleType, Value };

export interface Scale<V extends Value = Value> {
type?: ScaleType;
domain?: any[];
range?: V[];
scheme?: string;
}

export interface WithScale<V extends Value = Value> {
scale?: Scale<V>;
}
13 changes: 13 additions & 0 deletions packages/superset-ui-preset-chart-xy/src/encodeable/types/Spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface BaseOptions {
namespace?: string;
}

export interface PartialSpec<Encoding, Options extends BaseOptions = BaseOptions> {
encoding: Partial<Encoding>;
options?: Options;
}

export interface FullSpec<Encoding, Options extends BaseOptions = BaseOptions> {
encoding: Encoding;
options?: Options;
}

0 comments on commit 9c3c907

Please sign in to comment.