-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use expression to generalize zeroOrMinOrMax only when there is a…
… scale
- Loading branch information
Showing
5 changed files
with
93 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,80 @@ | ||
import {NonPositionScaleChannel, PolarPositionScaleChannel, PositionScaleChannel} from '../../../channel'; | ||
import {Config} from '../../../config'; | ||
import {VgValueRef} from '../../../vega.schema'; | ||
import {ScaleComponent} from '../../scale/component'; | ||
|
||
export function zeroOrMinOrMax({ | ||
scaleName, | ||
scale, | ||
mode, | ||
mainChannel | ||
mainChannel, | ||
config | ||
}: { | ||
scaleName: string; | ||
scale: ScaleComponent; | ||
mode: 'min' | 'zeroOrMin' | {zeroOrMax: {widthSignal: string; heightSignal: string}}; | ||
mainChannel: PositionScaleChannel | PolarPositionScaleChannel | NonPositionScaleChannel; | ||
config: Config; | ||
}): VgValueRef { | ||
if (mode !== 'min' && scaleName && scale.domainDefinitelyIncludesZero()) { | ||
return { | ||
scale: scaleName, | ||
value: 0 | ||
}; | ||
} | ||
const domain = `domain('${scaleName}')`; | ||
const min = `${domain}[0]`; | ||
const max = `peek(${domain})`; // peek = the last item of the array | ||
|
||
if (mode === 'zeroOrMin' || mode === 'min') { | ||
switch (mainChannel) { | ||
case 'radius': | ||
case 'theta': | ||
case 'x': | ||
return {value: 0}; | ||
case 'y': | ||
return {field: {group: 'height'}}; | ||
default: | ||
return {signal: `scale('${scaleName}', domain('${scaleName}')[0])`}; // encode the scale domain min | ||
} | ||
} else { | ||
// zeroOrMax | ||
switch (mainChannel) { | ||
case 'radius': { | ||
const {widthSignal, heightSignal} = mode.zeroOrMax; | ||
// max of radius is min(width, height) / 2 | ||
if (scale && scaleName) { | ||
// If there is a scale (and hence its name) | ||
const domainHasZero = scale.domainHasZero(); | ||
if (mode === 'min') { | ||
return {signal: `scale('${scaleName}', ${min})`}; // encode the scale domain min | ||
} else { | ||
// zeroOrMin or zeroOrMax mode | ||
if (domainHasZero === 'definitely') { | ||
return { | ||
signal: `min(${widthSignal},${heightSignal})/2` | ||
scale: scaleName, | ||
value: 0 | ||
}; | ||
} else if (domainHasZero === 'maybe') { | ||
if (mode === 'zeroOrMin') { | ||
return {signal: `scale('${scaleName}', inrange(0, ${domain}) ? 0 : ${min})`}; // encode the scale domain min | ||
} else { | ||
return {signal: `scale('${scaleName}', inrange(0, ${domain}) ? 0 : ${max})`}; // encode the scale domain max | ||
} | ||
} | ||
} | ||
} | ||
|
||
const isMin = mode === 'zeroOrMin' || mode === 'min'; | ||
switch (mainChannel) { | ||
case 'radius': { | ||
if (mode === 'min' || mode === 'zeroOrMin') { | ||
return {value: 0}; // min value | ||
} | ||
case 'theta': | ||
return {signal: '2*PI'}; | ||
case 'x': | ||
return {field: {group: 'width'}}; | ||
case 'y': | ||
return {value: 0}; | ||
default: | ||
return {signal: `scale('${scaleName}', domain('${scaleName}')[1])`}; // encode the scale domain max | ||
const {widthSignal, heightSignal} = mode.zeroOrMax; | ||
// max of radius is min(width, height) / 2 | ||
return { | ||
signal: `min(${widthSignal},${heightSignal})/2` | ||
}; | ||
} | ||
case 'theta': | ||
case 'angle': | ||
return isMin ? {value: 0} : {signal: '2*PI'}; | ||
case 'x': | ||
return isMin ? {value: 0} : {field: {group: 'width'}}; | ||
case 'y': | ||
return isMin ? {field: {group: 'height'}} : {value: 0}; | ||
case 'color': | ||
case 'fill': | ||
case 'stroke': | ||
return {value: '#aaa'}; | ||
case 'opacity': | ||
case 'fillOpacity': | ||
case 'strokeOpacity': | ||
return {value: config.scale[isMin ? 'minOpacity' : 'maxOpacity']}; | ||
case 'strokeWidth': | ||
return {value: config.scale[isMin ? 'minStrokeWidth' : 'maxStrokeWidth']}; | ||
case 'size': | ||
return {value: config.scale[isMin ? 'minSize' : 'maxSize']}; | ||
case 'strokeDash': | ||
case 'shape': | ||
return {value: null}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters