Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {axisFx, axisFy, axisX, axisY, gridFx, gridFy, gridX, gridY} from "./mark
import {frame} from "./marks/frame.js";
import {tip} from "./marks/tip.js";
import {arrayify, isColor, isIterable, isNone, isScaleOptions, map, yes, maybeIntervalTransform} from "./options.js";
import {createProjection, getGeometryChannels} from "./projection.js";
import {createProjection, getGeometryChannels, hasProjection} from "./projection.js";
import {createScales, createScaleFunctions, autoScaleRange, exposeScales} from "./scales.js";
import {innerDimensions, outerDimensions} from "./scales.js";
import {position, registry as scaleRegistry} from "./scales/index.js";
Expand Down Expand Up @@ -48,8 +48,8 @@ export function plot(options = {}) {

// Compute a Map from scale name to an array of associated channels.
const channelsByScale = new Map();
if (topFacetState) addScaleChannels(channelsByScale, [topFacetState]);
addScaleChannels(channelsByScale, facetStateByMark);
if (topFacetState) addScaleChannels(channelsByScale, [topFacetState], options);
addScaleChannels(channelsByScale, facetStateByMark, options);

// Add implicit axis marks. Because this happens after faceting (because it
// depends on whether faceting is present), we must initialize the facet state
Expand Down Expand Up @@ -139,7 +139,7 @@ export function plot(options = {}) {
}

// Initalize the scales and dimensions.
const scaleDescriptors = createScales(addScaleChannels(channelsByScale, stateByMark), options);
const scaleDescriptors = createScales(addScaleChannels(channelsByScale, stateByMark, options), options);
const scales = createScaleFunctions(scaleDescriptors);
const dimensions = createDimensions(scaleDescriptors, marks, options);

Expand Down Expand Up @@ -217,8 +217,8 @@ export function plot(options = {}) {
// reinitialization. Preserve existing scale labels, if any.
if (newByScale.size) {
const newChannelsByScale = new Map();
addScaleChannels(newChannelsByScale, stateByMark, (key) => newByScale.has(key));
addScaleChannels(channelsByScale, stateByMark, (key) => newByScale.has(key));
addScaleChannels(newChannelsByScale, stateByMark, options, (key) => newByScale.has(key));
addScaleChannels(channelsByScale, stateByMark, options, (key) => newByScale.has(key));
const newScaleDescriptors = inheritScaleLabels(createScales(newChannelsByScale, options), scaleDescriptors);
const newScales = createScaleFunctions(newScaleDescriptors);
Object.assign(scaleDescriptors, newScaleDescriptors);
Expand Down Expand Up @@ -410,17 +410,21 @@ function inferChannelScales(channels) {
}
}

function addScaleChannels(channelsByScale, stateByMark, filter = yes) {
function addScaleChannels(channelsByScale, stateByMark, options, filter = yes) {
for (const {channels} of stateByMark.values()) {
for (const name in channels) {
const channel = channels[name];
const {scale} = channel;
if (scale != null && filter(scale)) {
// Geo marks affect the default x and y domains if there is no
// projection. Skip this (as an optimization) when a projection is
// specified, or when the domains for x and y are specified.
if (scale === "projection") {
// TODO only do this if there’s no projection
const [x, y] = getGeometryChannels(channel);
addScaleChannel(channelsByScale, "x", x);
addScaleChannel(channelsByScale, "y", y);
if (!hasProjection(options)) {
const [x, y] = getGeometryChannels(channel);
if (options.x?.domain === undefined) addScaleChannel(channelsByScale, "x", x);
if (options.y?.domain === undefined) addScaleChannel(channelsByScale, "y", y);
}
} else {
addScaleChannel(channelsByScale, scale, channel);
}
Expand Down
13 changes: 12 additions & 1 deletion src/projection.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,24 @@ export function project(cx, cy, values, projection) {
}
}

// Returns true if a projection was specified. This should match the logic of
// createProjection above, and is called before we construct the projection.
// (Though note that we ignore the edge case where the projection initializer
// may return null.)
export function hasProjection({projection} = {}) {
if (projection == null) return false;
if (typeof projection.stream === "function") return true;
if (isObject(projection)) projection = projection.type;
return projection != null;
}

// When a named projection is specified, we can use its natural aspect ratio to
// determine a good value for the projection’s height based on the desired
// width. When we don’t have a way to know, the golden ratio is our best guess.
// Due to a circular dependency (we need to know the height before we can
// construct the projection), we have to test the raw projection option rather
// than the materialized projection; therefore we must be extremely careful that
// the logic of this function exactly matches Projection above!
// the logic of this function exactly matches createProjection above!
export function projectionAspectRatio(projection) {
if (typeof projection?.stream === "function") return defaultAspectRatio;
if (isObject(projection)) projection = projection.type;
Expand Down
70 changes: 12 additions & 58 deletions test/output/projectionHeightGeometry.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 90 additions & 0 deletions test/output/projectionHeightGeometryNull.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 62 additions & 0 deletions test/output/projectionNull.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/plots/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ export * from "./projection-height-equal-earth.js";
export * from "./projection-height-geometry.js";
export * from "./projection-height-mercator.js";
export * from "./projection-height-orthographic.js";
export * from "./projection-null.js";
export * from "./random-bins-xy.js";
export * from "./random-bins.js";
export * from "./random-quantile.js";
Expand Down
21 changes: 19 additions & 2 deletions test/plots/projection-height-geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,29 @@ export async function projectionHeightGeometry() {
coordinates: Array.from({length: 201}, (_, i) => {
const angle = (i / 100) * Math.PI;
const r = (i % 2) + 5;
return [340 + 30 * r * Math.cos(angle), 205 + 30 * r * Math.sin(angle)];
return [300 + 30 * r * Math.cos(angle), 185 + 30 * r * Math.sin(angle)];
})
} as const;
return Plot.plot({
facet: {data: [0, 1], y: [0, 1]},
projection: null,
projection: "identity",
marks: [Plot.geo(shape), Plot.frame({stroke: "red", strokeDasharray: 4})]
});
}

export async function projectionHeightGeometryNull() {
const shape = {
type: "LineString",
coordinates: Array.from({length: 201}, (_, i) => {
const angle = (i / 100) * Math.PI;
const r = (i % 2) + 5;
return [300 + 30 * r * Math.cos(angle), 185 + 30 * r * Math.sin(angle)];
})
} as const;
return Plot.plot({
aspectRatio: true,
width: 400,
facet: {data: [0, 1], y: [0, 1]},
marks: [Plot.geo(shape), Plot.frame({stroke: "red", strokeDasharray: 4})]
});
}
12 changes: 12 additions & 0 deletions test/plots/projection-null.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as Plot from "@observablehq/plot";
import * as d3 from "d3";
import {feature} from "topojson-client";

export async function projectionNull() {
const world = await d3.json<any>("data/countries-110m.json");
const land = feature(world, world.objects.land);
return Plot.plot({
projection: null,
marks: [Plot.geo(land), Plot.graticule()]
});
}