Skip to content

Commit

Permalink
fix(legend): return unique legend type for discrete gradient legends …
Browse files Browse the repository at this point in the history
…to avoid dropping gradientLength
  • Loading branch information
hydrosquall committed Jul 31, 2024
1 parent 8bad9d2 commit b3f0753
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/compile/legend/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ export function parseLegendForChannel(model: UnitModel, channel: NonPositionScal

for (const property of LEGEND_COMPONENT_PROPERTIES) {
if (
// Continuous and discrete gradients ignore symbol properties
(legendType === 'gradient' && property.startsWith('symbol')) ||
(legendType === 'discrete' && property.startsWith('symbol')) ||
// Symbols ignore gradient properties
(legendType === 'symbol' && property.startsWith('gradient'))
) {
continue;
Expand Down
20 changes: 13 additions & 7 deletions src/compile/legend/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import {Config} from '../../config';
import {Encoding} from '../../encoding';
import {Legend, LegendConfig, LegendInternal} from '../../legend';
import {Mark, MarkDef} from '../../mark';
import {isContinuousToContinuous, ScaleType} from '../../scale';
import {isContinuousToContinuous, isContinuousToDiscrete, ScaleType} from '../../scale';
import {TimeUnit} from '../../timeunit';
import {contains, getFirstDefined} from '../../util';
import {isSignalRef} from '../../vega.schema';
import {guideFormat, guideFormatType} from '../format';
import {Model} from '../model';
import {UnitModel} from '../unit';
import {NonPositionScaleChannel} from './../../channel';
import {LegendComponentProps} from './component';
import {LegendComponentProps, FullLegendType} from './component';
import {getFirstConditionValue} from './encode';

export interface LegendRuleParams {
Expand All @@ -28,7 +28,7 @@ export interface LegendRuleParams {
config: Config<SignalRef>;
scaleType: ScaleType;
orient: LegendOrient;
legendType: LegendType;
legendType: FullLegendType;
direction: Orientation;
}

Expand Down Expand Up @@ -132,7 +132,7 @@ export function getLegendType(params: {
channel: NonPositionScaleChannel;
timeUnit?: TimeUnit;
scaleType: ScaleType;
}): LegendType {
}): FullLegendType {
const {legend} = params;

return getFirstDefined(legend.type, defaultType(params));
Expand All @@ -146,7 +146,9 @@ export function defaultType({
channel: NonPositionScaleChannel;
timeUnit?: TimeUnit;
scaleType: ScaleType;
}): LegendType {
// TODO: Update this function and 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
}): FullLegendType {
// Following the logic in https://github.com/vega/vega-parser/blob/master/src/parsers/legend.js

if (isColorChannel(channel)) {
Expand All @@ -157,6 +159,10 @@ export function defaultType({
if (isContinuousToContinuous(scaleType)) {
return 'gradient';
}

if (isContinuousToDiscrete(scaleType)) {
return 'discrete';
}
}
return 'symbol';
}
Expand All @@ -169,7 +175,7 @@ export function getDirection({
}: {
orient: LegendOrient;
legendConfig: LegendConfig<SignalRef>;
legendType: LegendType;
legendType: FullLegendType;
legend: Legend<SignalRef>;
}): Orientation {
return (
Expand All @@ -179,7 +185,7 @@ export function getDirection({
);
}

export function defaultDirection(orient: LegendOrient, legendType: LegendType): 'horizontal' | undefined {
export function defaultDirection(orient: LegendOrient, legendType: FullLegendType): 'horizontal' | undefined {
switch (orient) {
case 'top':
case 'bottom':
Expand Down

0 comments on commit b3f0753

Please sign in to comment.