Skip to content

Commit

Permalink
feat: allow signals in scalenice (#3887)
Browse files Browse the repository at this point in the history
* feat: allow signals in scale `nice`
  • Loading branch information
lsh committed Mar 5, 2024
1 parent b23a841 commit c62a1be
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/docs/scales.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ All quantitative scales support color-valued ranges, defined either as an array
| bins | [Bins](#bins) | {% include tag ver="5.0" %} Bin boundaries over the scale domain, such as those computed by Vega's [`bin` transform](../transforms/bin). If provided, axes and legends will use the bin boundaries to inform the choice of axis tick marks and legend labels. This property can be either an explicit array of bin boundary values or a specification object, see the [scale bins reference](#bins) for more.|
| clamp | {% include type t="Boolean" %} | A boolean indicating if output values should be clamped to the _range_ (default `false`). If clamping is disabled and the scale is passed a value outside the _domain_, the scale may return a value outside the _range_ through extrapolation. If clamping is enabled, the output value of the scale is always within the scale's range.|
| padding | {% include type t="Number" %} | Expands the scale domain to accommodate the specified number of pixels on each of the scale range. The scale range must represent pixels for this parameter to function as intended. Padding adjustment is performed _prior_ to all other adjustments, including the effects of the _zero_, _nice_, _domainMin_, and _domainMax_ properties.|
| nice | {% include type t="Boolean|Number" %} | Extends the domain so that it starts and ends on nice round values (default `false`). This method typically modifies the scale's domain, and may only extend the bounds to the nearest round value. Nicing is useful if the domain is computed from data and may be irregular. For example, for a domain of [0.201479…, 0.996679…], a nice domain might be [0.2, 1.0]. Domain values set via _domainMin_ and _domainMax_ (but **not** _domainRaw_) are subject to nicing. Using a number value for this parameter (representing a desired tick count) allows greater control over the step size used to extend the bounds, guaranteeing that the returned ticks will exactly cover the domain.|
| nice | {% include type t="Boolean|Number|Signal" %} | Extends the domain so that it starts and ends on nice round values (default `false`). This method typically modifies the scale's domain, and may only extend the bounds to the nearest round value. Nicing is useful if the domain is computed from data and may be irregular. For example, for a domain of [0.201479…, 0.996679…], a nice domain might be [0.2, 1.0]. Domain values set via _domainMin_ and _domainMax_ (but **not** _domainRaw_) are subject to nicing. Using a number value for this parameter (representing a desired tick count) allows greater control over the step size used to extend the bounds, guaranteeing that the returned ticks will exactly cover the domain.|
| zero | {% include type t="Boolean" %} | Boolean flag indicating if the scale domain should include zero. The default value is `true` for `linear`, `sqrt` and `pow`, and `false` otherwise.|

### <a name="linear"></a>Linear Scales
Expand Down
8 changes: 5 additions & 3 deletions packages/vega-parser/src/parsers/scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function parseScale(spec, scope) {
}

if (spec.nice != null) {
params.nice = parseScaleNice(spec.nice);
params.nice = parseScaleNice(spec.nice, scope);
}

if (spec.bins != null) {
Expand Down Expand Up @@ -213,8 +213,10 @@ function parseScaleBins(v, scope) {

// -- SCALE NICE -----

function parseScaleNice(nice) {
return isObject(nice)
function parseScaleNice(nice, scope) {
return nice.signal
? scope.signalRef(nice.signal)
: isObject(nice)
? {
interval: parseLiteral(nice.interval),
step: parseLiteral(nice.step)
Expand Down
8 changes: 5 additions & 3 deletions packages/vega-parser/test/scale-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ tape('Parser parses Vega specs with scales', t => {
'signals': [
{'name': 'yfield', 'value': 'y'},
{'name': 'sortop', 'value': 'median'},
{'name': 'order', 'value': 'ascending'}
{'name': 'order', 'value': 'ascending'},
{'name': 'niceCount', 'value': '3'}
],
'data': [
{
Expand All @@ -24,6 +25,7 @@ tape('Parser parses Vega specs with scales', t => {
'name': 'xscale',
'type': 'band',
'range': [0, {'signal': 'width'}],
'nice': {'signal': 'niceCount'},
'domain': {
'data': 'table',
'field': 'x',
Expand Down Expand Up @@ -54,11 +56,11 @@ tape('Parser parses Vega specs with scales', t => {

const dfs = parse(spec);

t.equal(dfs.operators.length, 28);
t.equal(dfs.operators.length, 29);
t.deepEqual(dfs.operators.map(o => o.type),
['operator', 'operator', 'operator', 'operator', 'operator',
'operator', 'operator', 'operator', 'operator', 'operator',
'collect', 'encode', 'sieve',
'operator', 'collect', 'encode', 'sieve',
'scale', 'scale', 'scale',
'collect', 'sieve',
'field', 'aggregate', 'collect', 'compare', 'values',
Expand Down

0 comments on commit c62a1be

Please sign in to comment.