Skip to content

Commit eb31eb3

Browse files
authored
sampler requires a function (#1228)
* sampler requires a function * fix test * no sampler for constant fill/fillOpacity
1 parent eec122d commit eb31eb3

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

src/marks/contour.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class Contour extends AbstractRaster {
5050
// by the sampler initializer, and hence is not passed to super to avoid
5151
// computing it before there’s data.
5252
if (data == null) {
53-
if (typeof value !== "function") throw new Error("invalid contour value");
53+
if (value == null) throw new Error("missing contour value");
5454
options = sampler("value", {value, ...options});
5555
value = null;
5656
}

src/marks/raster.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {blurImage, Delaunay, randomLcg, rgb} from "d3";
22
import {valueObject} from "../channel.js";
33
import {create} from "../context.js";
44
import {map, first, second, third, isTuples, isNumeric, isTemporal, take, identity} from "../options.js";
5+
import {maybeColorChannel, maybeNumberChannel} from "../options.js";
56
import {Mark} from "../plot.js";
67
import {applyAttr, applyDirectStyles, applyIndirectStyles, applyTransform, impliedString} from "../style.js";
78
import {initializer} from "../transforms/basic.js";
@@ -84,7 +85,12 @@ export class AbstractRaster extends Mark {
8485
export class Raster extends AbstractRaster {
8586
constructor(data, options = {}) {
8687
const {imageRendering} = options;
87-
super(data, undefined, data == null ? sampler("fill", sampler("fillOpacity", options)) : options, defaults);
88+
if (data == null) {
89+
const {fill, fillOpacity} = options;
90+
if (maybeNumberChannel(fillOpacity)[0] !== undefined) options = sampler("fillOpacity", options);
91+
if (maybeColorChannel(fill)[0] !== undefined) options = sampler("fill", options);
92+
}
93+
super(data, undefined, options, defaults);
8894
this.imageRendering = impliedString(imageRendering, "auto");
8995
}
9096
// Ignore the color scale, so the fill channel is returned unscaled.
@@ -217,7 +223,7 @@ export function rasterBounds({x1, y1, x2, y2}, scales, dimensions, context) {
217223
// generating a channel of the same name.
218224
export function sampler(name, options = {}) {
219225
const {[name]: value} = options;
220-
if (typeof value !== "function") return options;
226+
if (typeof value !== "function") throw new Error(`invalid ${name}: not a function`);
221227
return initializer({...options, [name]: undefined}, function (data, facets, channels, scales, dimensions, context) {
222228
const {x, y} = scales;
223229
// TODO Allow projections, if invertible.

test/plots/raster-vapor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export async function rasterVapor() {
1313
color: {scheme: "blues"},
1414
x: {transform: (x) => x - 180},
1515
y: {transform: (y) => 90 - y},
16-
marks: [Plot.raster({fill: await vapor(), width: 360, height: 180})]
16+
marks: [Plot.raster(await vapor(), {width: 360, height: 180})]
1717
});
1818
}
1919

0 commit comments

Comments
 (0)