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

Commit

Permalink
feat: add getDomain to ChannelEncoder
Browse files Browse the repository at this point in the history
  • Loading branch information
kristw committed Jun 5, 2019
1 parent 0042f46 commit e2ea8c8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export default abstract class AbstractEncoder<
// Only work for nominal channels now
// TODO: Add support for numerical scale
if (channelEncoder.definition.type === 'nominal') {
const domain = Array.from(new Set(data.map(channelEncoder.get)));
const domain = channelEncoder.getDomain(data) as string[];

return domain.map((value: ChannelInput) => ({
field,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { extent as d3Extent } from 'd3-array';
import { Value } from 'vega-lite/build/src/channeldef';
import { extractFormatFromChannelDef } from './parsers/extractFormat';
import extractScale, { ScaleAgent } from './parsers/extractScale';
import extractGetter from './parsers/extractGetter';
import { ChannelOptions, ChannelType, ChannelInput } from './types/Channel';
import { PlainObject } from './types/Data';
import { PlainObject, Dataset } from './types/Data';
import {
ChannelDef,
isScaleFieldDef,
Expand Down Expand Up @@ -95,6 +96,31 @@ export default class ChannelEncoder<Def extends ChannelDef<Output>, Output exten
: (value as T);
}

getDomain(data: Dataset) {
if (isTypedFieldDef(this.definition)) {
const { type } = this.definition;
if (type === 'nominal' || type === 'ordinal') {
return Array.from(new Set(data.map(d => this.get(d)))) as string[];
} else if (type === 'quantitative') {
const extent = d3Extent(data, d => this.get<number>(d));
if (typeof extent[0] === 'undefined') {
return [0, 1];
}

return extent as [number, number];
} else if (type === 'temporal') {
const extent = d3Extent(data, d => this.get<number | Date>(d));
if (typeof extent[0] === 'undefined') {
return [0, 1];
}

return extent as [number, number] | [Date, Date];
}
}

return [];
}

getTitle() {
if (isFieldDef(this.definition)) {
return this.definition.title || this.definition.field;
Expand Down

0 comments on commit e2ea8c8

Please sign in to comment.