Skip to content

Commit

Permalink
raster identity fill shorthand
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jan 11, 2023
1 parent 3ec2db6 commit 44e1c0f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1564,7 +1564,7 @@ Returns a new link with the given *data* and *options*.
[Source](./src/marks/raster.js) · [Examples](https://observablehq.com/@observablehq/plot-raster) · Renders a raster image from spatial samples. If data is provided, it represents discrete samples in abstract coordinates *x* and *y*; the *fill* and *fillOpacity* channels specify further abstract values (_e.g._, height in a topographic map) to be [spatially interpolated](#spatial-interpolation) to produce an image.
```js
Plot.raster(volcano.values, {width: volcano.width, height: volcano.height, fill: Plot.identity})
Plot.raster(volcano.values, {width: volcano.width, height: volcano.height})
```
The *fill* and *fillOpacity* channels may alternatively be specified as continuous functions *f*(*x*, *y*) to be evaluated at each pixel centroid of the raster grid (without interpolation).
Expand Down
2 changes: 1 addition & 1 deletion src/marks/contour.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function contourGeometry({thresholds, interval, ...options}) {
}

export function contour() {
return new Contour(...maybeTuples(...arguments));
return new Contour(...maybeTuples("value", ...arguments));
}

function finiteExtent(VV) {
Expand Down
22 changes: 14 additions & 8 deletions src/marks/raster.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {blurImage, Delaunay, randomLcg, rgb} from "d3";
import {valueObject} from "../channel.js";
import {create} from "../context.js";
import {map, first, second, third, isTuples, isNumeric, isTemporal, take} from "../options.js";
import {map, first, second, third, isTuples, isNumeric, isTemporal, take, identity} from "../options.js";
import {Mark} from "../plot.js";
import {applyAttr, applyDirectStyles, applyIndirectStyles, applyTransform, impliedString} from "../style.js";
import {initializer} from "../transforms/basic.js";
Expand Down Expand Up @@ -165,23 +165,29 @@ export class Raster extends AbstractRaster {
}
}

export function maybeTuples(data, options) {
if (arguments.length < 2) (options = data), (data = null);
let {x, y, fill, ...rest} = options;
// Because we implicit x and y when fill is a function of (x, y), and when
export function maybeTuples(k, data, options) {
if (arguments.length < 3) (options = data), (data = null);
let {x, y, [k]: z, ...rest} = options;
// Because we use implicit x and y when z is a function of (x, y), and when
// data is a dense grid, we must further disambiguate by testing whether data
// contains [x, y, z?] tuples. Hence you can’t use this shorthand with a
// transform that lazily generates tuples, but that seems reasonable since
// this is just for convenience anyway.
if (x === undefined && y === undefined && isTuples(data)) {
(x = first), (y = second);
if (fill === undefined) fill = third;
if (z === undefined) z = third;
}
return [data, {...rest, x, y, fill}];
return [data, {...rest, x, y, [k]: z}];
}

export function raster() {
return new Raster(...maybeTuples(...arguments));
const [data, options] = maybeTuples("fill", ...arguments);
return new Raster(
data,
data == null || options.fill !== undefined || options.fillOpacity !== undefined
? options
: {...options, fill: identity}
);
}

// See rasterBounds; this version is called during render.
Expand Down
Loading

0 comments on commit 44e1c0f

Please sign in to comment.