Skip to content

Commit

Permalink
addColumn action; ordinal scale on numeric field
Browse files Browse the repository at this point in the history
  • Loading branch information
lixun910 committed Feb 1, 2024
1 parent 166ad0e commit 398905e
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/actions/src/action-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export const ActionTypes = {
SORT_TABLE_COLUMN: `${ACTION_PREFIX}SORT_TABLE_COLUMN`,
PIN_TABLE_COLUMN: `${ACTION_PREFIX}PIN_TABLE_COLUMN`,
COPY_TABLE_COLUMN: `${ACTION_PREFIX}COPY_TABLE_COLUMN`,
ADD_TABLE_COLUMN: `${ACTION_PREFIX}ADD_TABLE_COLUMN`,
SET_COLUMN_DISPLAY_FORMAT: `${ACTION_PREFIX}SET_COLUMN_DISPLAY_FORMAT`,
NEXT_FILE_BATCH: `${ACTION_PREFIX}NEXT_FILE_BATCH`,
PROCESS_FILE_CONTENT: `${ACTION_PREFIX}PROCESS_FILE_CONTENT`,
Expand Down
29 changes: 28 additions & 1 deletion src/actions/src/vis-state-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
Filter,
ParsedConfig,
ParsedLayer,
EffectPropsPartial
EffectPropsPartial,
Field
} from '@kepler.gl/types';
// TODO - import LoaderObject type from @loaders.gl/core when supported
// TODO - import LoadOptions type from @loaders.gl/core when supported
Expand Down Expand Up @@ -720,6 +721,32 @@ export function copyTableColumn(
};
}

export type AddTableColumnUpdaterAction = {
dataId: string;
field: Field;
values: unknown[];
};
/**
* Add new column to dataset, for table display
* @param dataId
* @param field
* @param values
* @returns action
* @public
*/
export function addTableColumn(
dataId: string,
field: Field,
values: unknown[]
): Merge<AddTableColumnUpdaterAction, {type: typeof ActionTypes.ADD_TABLE_COLUMN}> {
return {
type: ActionTypes.ADD_TABLE_COLUMN,
dataId,
field,
values
};
}

export type SetColumnDisplayFormatUpdaterAction = {
dataId: string;
formats: {
Expand Down
2 changes: 1 addition & 1 deletion src/constants/src/default-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ export const AGGREGATION_TYPE_OPTIONS: {id: string; label: string}[] = Object.en
}));

export const linearFieldScaleFunctions = {
[CHANNEL_SCALES.color]: [SCALE_TYPES.quantize, SCALE_TYPES.quantile, SCALE_TYPES.custom],
[CHANNEL_SCALES.color]: [SCALE_TYPES.quantize, SCALE_TYPES.quantile, SCALE_TYPES.custom, SCALE_TYPES.ordinal],
[CHANNEL_SCALES.radius]: [SCALE_TYPES.sqrt],
[CHANNEL_SCALES.size]: [SCALE_TYPES.linear, SCALE_TYPES.sqrt, SCALE_TYPES.log]
};
Expand Down
2 changes: 1 addition & 1 deletion src/layers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {default as GridLayer} from './grid-layer/grid-layer';
export {pointToPolygonGeo} from './grid-layer/grid-utils';
import {default as HexagonLayer} from './hexagon-layer/hexagon-layer';
import {default as GeojsonLayer} from './geojson-layer/geojson-layer';
export {defaultElevation, defaultLineWidth, defaultRadius} from './geojson-layer/geojson-layer';
export {default as GeojsonLayer, defaultElevation, defaultLineWidth, defaultRadius} from './geojson-layer/geojson-layer';
import {default as ClusterLayer} from './cluster-layer/cluster-layer';
import {default as IconLayer} from './icon-layer/icon-layer';
import {default as HeatmapLayer} from './heatmap-layer/heatmap-layer';
Expand Down
36 changes: 36 additions & 0 deletions src/reducers/src/vis-state-updaters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2774,6 +2774,42 @@ export function copyTableColumnUpdater(
return state;
}

/**
* Add new field to table with values, if field already exists, update the column values
* @memberof visStateUpdaters
* @public
*/
export function addTableColumnUpdater(
state: VisState,
{dataId, field, values}: VisStateActions.AddTableColumnUpdaterAction
): VisState {
const dataset = state.datasets[dataId];
if (!dataset) {
return state;
}
const dataContainer = dataset.dataContainer;
let newDataset = dataset;

// check if field already exists
const fieldIdx = dataset.fields.findIndex(f => f.name === field.name);
if (fieldIdx >= 0) {
// update the column values
if (dataContainer && dataContainer.updateColumn) {
dataContainer.updateColumn(fieldIdx, values);
}
newDataset = copyTableAndUpdate(dataset);
} else {
// add new column with values
const newFields = [...dataset.fields, field];
if (dataContainer && dataContainer.addColumn) {
dataContainer.addColumn(values, field);
}
newDataset = copyTableAndUpdate(dataset, { fields: newFields });
}

return pick_('datasets')(merge_({[dataId]: newDataset}))(state);
}

/**
* Set display format from columns from user selection
* @memberof visStateUpdaters
Expand Down
2 changes: 2 additions & 0 deletions src/reducers/src/vis-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ const actionHandler = {

[ActionTypes.COPY_TABLE_COLUMN]: visStateUpdaters.copyTableColumnUpdater,

[ActionTypes.ADD_TABLE_COLUMN]: visStateUpdaters.addTableColumnUpdater,

[ActionTypes.SET_COLUMN_DISPLAY_FORMAT]: visStateUpdaters.setColumnDisplayFormatUpdater,

[ActionTypes.NEXT_FILE_BATCH]: visStateUpdaters.nextFileBatchUpdater,
Expand Down
11 changes: 11 additions & 0 deletions src/utils/src/arrow-data-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ export class ArrowDataContainer implements DataContainerInterface {
// this._colData = this._cols.map(c => c.toArray());
}

addColumn(values: unknown[], field: Field) {
const newColumn = arrow.vectorFromArray(values);
this._cols.push(newColumn);
this._numColumns++;
this._fields.push(field);
}

updateColumn(columnIndex: number, values: unknown[]) {
this._cols[columnIndex] = arrow.vectorFromArray(values);
}

numChunks(): number {
return this._numChunks;
}
Expand Down
10 changes: 10 additions & 0 deletions src/utils/src/data-container-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ export interface DataContainerInterface {
*/
update?(updateData: any[]): void;

/**
* Adds a new column to the data container.
*/
addColumn?(columnValues: unknown[], field: Field): void;

/**
* Update the values of a column
*/
updateColumn?(columnIndex: number, columnValues: unknown[]): void;

/**
* Returns the number of rows in the data container.
* @returns Number of rows in the data container.
Expand Down

0 comments on commit 398905e

Please sign in to comment.