Skip to content

Commit c158c80

Browse files
committed
fix(legend): use custom legend type throughout vega-lite compile functions
1 parent b3f0753 commit c158c80

File tree

5 files changed

+26
-11
lines changed

5 files changed

+26
-11
lines changed

src/compile/layer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Legend as VgLegend, NewSignal, SignalRef, Title as VgTitle} from 'vega';
1+
import {NewSignal, SignalRef, Title as VgTitle} from 'vega';
22
import {array} from 'vega-util';
33
import {Config} from '../config';
44
import * as log from '../log';
@@ -13,6 +13,8 @@ import {parseLayerLayoutSize} from './layoutsize/parse';
1313
import {assembleLegends} from './legend/assemble';
1414
import {Model} from './model';
1515
import {assembleLayerSelectionMarks} from './selection/assemble';
16+
import {FullVgLegend as VgLegend} from './legend/component';
17+
1618
import {UnitModel} from './unit';
1719

1820
export class LayerModel extends Model {

src/compile/legend/assemble.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import {Legend as VgLegend, LegendEncode} from 'vega';
1+
import {LegendEncode} from 'vega';
22
import {Config} from '../../config';
33
import {LEGEND_SCALE_CHANNELS} from '../../legend';
44
import {keys, replaceAll, stringify, vals} from '../../util';
55
import {isSignalRef, VgEncodeChannel, VgValueRef} from '../../vega.schema';
66
import {Model} from '../model';
7-
import {LegendComponent} from './component';
7+
import {FullVgLegend as VgLegend, LegendComponent} from './component';
88
import {mergeLegendComponent} from './parse';
99

1010
function setLegendEncode(

src/compile/legend/component.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1-
import {Legend as VgLegend} from 'vega';
1+
import {Legend as VgLegend, LegendType} from 'vega';
22
import {NonPositionScaleChannel} from '../../channel';
33
import {COMMON_LEGEND_PROPERTY_INDEX, LegendInternal} from '../../legend';
44
import {Flag, keys} from '../../util';
55
import {Split} from '../split';
66

7-
export type LegendComponentProps = VgLegend & {
7+
/** Update LegendType to support a third value (discrete), which is needed for discrete gradient legends
8+
* https://github.com/vega/vega/blob/main/packages/vega-parser/src/parsers/legend.js */
9+
export type FullLegendType = LegendType | 'discrete';
10+
11+
/** Update LegendType to support a third value (discrete), which is needed for discrete gradient legends
12+
* https://github.com/vega/vega/blob/main/packages/vega-parser/src/parsers/legend.js */
13+
export type FullVgLegend = Omit<VgLegend, 'type'> & {
14+
/**
15+
* 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.
16+
* 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.
17+
* TODO: Remove FullLegendType override after base type in VgLegend is updated */
18+
type?: FullLegendType;
19+
};
20+
21+
export type LegendComponentProps = FullVgLegend & {
822
labelExpr?: string;
923
selections?: string[];
1024
disable?: boolean;
@@ -29,7 +43,7 @@ const LEGEND_COMPONENT_PROPERTY_INDEX: Flag<keyof LegendComponentProps> = {
2943

3044
export const LEGEND_COMPONENT_PROPERTIES = keys(LEGEND_COMPONENT_PROPERTY_INDEX);
3145

32-
export class LegendComponent extends Split<LegendComponentProps> {}
46+
export class LegendComponent extends Split<LegendComponentProps> { }
3347

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

src/compile/legend/encode.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {ColorValueRef, EncodeEntry, Gradient, LegendEncode, LegendType, SignalRef, SymbolEncodeEntry} from 'vega';
1+
import {ColorValueRef, EncodeEntry, Gradient, LegendEncode, SignalRef, SymbolEncodeEntry} from 'vega';
22
import {array, isArray, stringValue} from 'vega-util';
33
import {COLOR, NonPositionScaleChannel, OPACITY} from '../../channel';
44
import {
@@ -19,14 +19,14 @@ import {formatCustomType, isCustomFormatType} from '../format';
1919
import * as mixins from '../mark/encode';
2020
import {STORE} from '../selection';
2121
import {UnitModel} from '../unit';
22-
import {LegendComponent} from './component';
22+
import {LegendComponent, FullLegendType} from './component';
2323

2424
export interface LegendEncodeParams {
2525
fieldOrDatumDef: TypedFieldDef<string> | DatumDef;
2626
model: UnitModel;
2727
channel: NonPositionScaleChannel;
2828
legendCmpt: LegendComponent;
29-
legendType: LegendType;
29+
legendType: FullLegendType;
3030
}
3131

3232
export const legendEncodeRules: {

src/compile/model.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
AnchorValue,
33
Axis as VgAxis,
4-
Legend as VgLegend,
54
NewSignal,
65
Projection as VgProjection,
76
Signal,
@@ -56,7 +55,7 @@ import {
5655
LayoutSizeType
5756
} from './layoutsize/component';
5857
import {assembleLegends} from './legend/assemble';
59-
import {LegendComponentIndex} from './legend/component';
58+
import {LegendComponentIndex, FullVgLegend as VgLegend} from './legend/component';
6059
import {parseLegend} from './legend/parse';
6160
import {assembleProjections} from './projection/assemble';
6261
import {ProjectionComponent} from './projection/component';

0 commit comments

Comments
 (0)