Skip to content

Commit

Permalink
fix(legend): use custom legend type throughout vega-lite compile func…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
hydrosquall committed Jul 31, 2024
1 parent b3f0753 commit c158c80
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/compile/layer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Legend as VgLegend, NewSignal, SignalRef, Title as VgTitle} from 'vega';
import {NewSignal, SignalRef, Title as VgTitle} from 'vega';
import {array} from 'vega-util';
import {Config} from '../config';
import * as log from '../log';
Expand All @@ -13,6 +13,8 @@ import {parseLayerLayoutSize} from './layoutsize/parse';
import {assembleLegends} from './legend/assemble';
import {Model} from './model';
import {assembleLayerSelectionMarks} from './selection/assemble';
import {FullVgLegend as VgLegend} from './legend/component';

import {UnitModel} from './unit';

export class LayerModel extends Model {
Expand Down
4 changes: 2 additions & 2 deletions src/compile/legend/assemble.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {Legend as VgLegend, LegendEncode} from 'vega';
import {LegendEncode} from 'vega';
import {Config} from '../../config';
import {LEGEND_SCALE_CHANNELS} from '../../legend';
import {keys, replaceAll, stringify, vals} from '../../util';
import {isSignalRef, VgEncodeChannel, VgValueRef} from '../../vega.schema';
import {Model} from '../model';
import {LegendComponent} from './component';
import {FullVgLegend as VgLegend, LegendComponent} from './component';
import {mergeLegendComponent} from './parse';

function setLegendEncode(
Expand Down
20 changes: 17 additions & 3 deletions src/compile/legend/component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import {Legend as VgLegend} from 'vega';
import {Legend as VgLegend, LegendType} from 'vega';
import {NonPositionScaleChannel} from '../../channel';
import {COMMON_LEGEND_PROPERTY_INDEX, LegendInternal} from '../../legend';
import {Flag, keys} from '../../util';
import {Split} from '../split';

export type LegendComponentProps = VgLegend & {
/** Update LegendType to support a third value (discrete), which is needed for discrete gradient legends
* https://github.com/vega/vega/blob/main/packages/vega-parser/src/parsers/legend.js */
export type FullLegendType = LegendType | 'discrete';

/** Update LegendType to support a third value (discrete), which is needed for discrete gradient legends
* https://github.com/vega/vega/blob/main/packages/vega-parser/src/parsers/legend.js */
export type FullVgLegend = Omit<VgLegend, 'type'> & {
/**
* The type of legend to include. One of `"symbol"` for discrete symbol legends, `"gradient"` for a continuous color gradient, or `"discrete"` for a discrete color gradient.
* If gradient is used only the fill, stroke, and length scale parameters are considered. If unspecified, the type will be inferred based on the scale parameters used and their backing scale types.
* TODO: Remove FullLegendType override after base type in VgLegend is updated */
type?: FullLegendType;
};

export type LegendComponentProps = FullVgLegend & {
labelExpr?: string;
selections?: string[];
disable?: boolean;
Expand All @@ -29,7 +43,7 @@ const LEGEND_COMPONENT_PROPERTY_INDEX: Flag<keyof LegendComponentProps> = {

export const LEGEND_COMPONENT_PROPERTIES = keys(LEGEND_COMPONENT_PROPERTY_INDEX);

export class LegendComponent extends Split<LegendComponentProps> {}
export class LegendComponent extends Split<LegendComponentProps> { }

Check warning on line 46 in src/compile/legend/component.ts

View workflow job for this annotation

GitHub Actions / Runtime, Linting, and Coverage

Delete `·`

export type LegendComponentIndex = Partial<Record<NonPositionScaleChannel, LegendComponent>>;

Expand Down
6 changes: 3 additions & 3 deletions src/compile/legend/encode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ColorValueRef, EncodeEntry, Gradient, LegendEncode, LegendType, SignalRef, SymbolEncodeEntry} from 'vega';
import {ColorValueRef, EncodeEntry, Gradient, LegendEncode, SignalRef, SymbolEncodeEntry} from 'vega';
import {array, isArray, stringValue} from 'vega-util';
import {COLOR, NonPositionScaleChannel, OPACITY} from '../../channel';
import {
Expand All @@ -19,14 +19,14 @@ import {formatCustomType, isCustomFormatType} from '../format';
import * as mixins from '../mark/encode';
import {STORE} from '../selection';
import {UnitModel} from '../unit';
import {LegendComponent} from './component';
import {LegendComponent, FullLegendType} from './component';

export interface LegendEncodeParams {
fieldOrDatumDef: TypedFieldDef<string> | DatumDef;
model: UnitModel;
channel: NonPositionScaleChannel;
legendCmpt: LegendComponent;
legendType: LegendType;
legendType: FullLegendType;
}

export const legendEncodeRules: {
Expand Down
3 changes: 1 addition & 2 deletions src/compile/model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
AnchorValue,
Axis as VgAxis,
Legend as VgLegend,
NewSignal,
Projection as VgProjection,
Signal,
Expand Down Expand Up @@ -56,7 +55,7 @@ import {
LayoutSizeType
} from './layoutsize/component';
import {assembleLegends} from './legend/assemble';
import {LegendComponentIndex} from './legend/component';
import {LegendComponentIndex, FullVgLegend as VgLegend} from './legend/component';
import {parseLegend} from './legend/parse';
import {assembleProjections} from './projection/assemble';
import {ProjectionComponent} from './projection/component';
Expand Down

0 comments on commit c158c80

Please sign in to comment.