Skip to content

Commit

Permalink
fix: Fix Contour density encoding channels.
Browse files Browse the repository at this point in the history
  • Loading branch information
jheer committed Mar 20, 2024
1 parent 928d8aa commit d63ee38
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/plot/src/marks/ContourMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ export class ContourMark extends Grid2DMark {

// generate contours
this.data = kde.flatMap(cell => tz.map(t => {
// annotate contour geojson with cell groupby fields
// d3-contour already adds a threshold "value" property
const { density, ...groupby } = cell;
return Object.assign(
transform(contour.contour(cell.density, t), x, y),
{ ...cell, density: t }
transform(contour.contour(density, t), x, y),
{ ...groupby }
);
}));

Expand All @@ -70,8 +73,12 @@ export class ContourMark extends Grid2DMark {
options[channel] = channelOption(c);
}
}
if (densityMap.fill) options.fill = 'density';
if (densityMap.stroke) options.stroke = 'density';
// d3-contour adds a threshold "value" property
// here we ensure requested density values are encoded
for (const channel in densityMap) {
if (!densityMap[channel]) continue;
options[channel] = channelOption({ channel, as: 'value' });
}
return [{ type, data, options }];
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/plot/src/marks/Mark.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { toDataArray } from './util/to-data-array.js';
import { Transform } from '../symbols.js';

const isColorChannel = channel => channel === 'stroke' || channel === 'fill';
const isOpacityChannel = channel => /opacity$/i.test(channel);
const isSymbolChannel = channel => channel === 'symbol';
const isFieldObject = (channel, field) => {
return channel !== 'sort' && channel !== 'tip'
Expand Down Expand Up @@ -185,6 +186,7 @@ export function channelOption(c) {
// https://github.com/observablehq/plot/issues/1593
return Object.hasOwn(c, 'value') ? c.value
: isColorChannel(c.channel) ? { value: c.as, scale: 'color' }
: isOpacityChannel(c.channel) ? { value: c.as, scale: 'opacity' }
: c.as;
}

Expand Down

0 comments on commit d63ee38

Please sign in to comment.