Skip to content

Commit

Permalink
handle Arrow in a few more places
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Aug 5, 2024
1 parent bd22e0b commit d0d0b70
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 45 deletions.
7 changes: 6 additions & 1 deletion src/interactions/pointer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {pointer as pointof} from "d3";
import {composeRender} from "../mark.js";
import {isArray} from "../options.js";
import {applyFrameAnchor} from "../style.js";

const states = new WeakMap();
Expand Down Expand Up @@ -126,7 +127,11 @@ function pointerK(kx, ky, {x, y, px, py, maxRadius = 40, channels, render, ...op

// Dispatch the value. When simultaneously exiting this facet and
// entering a new one, prioritize the entering facet.
if (!(i == null && facetState?.size > 1)) context.dispatchValue(i == null ? null : data[i]);
if (!(i == null && facetState?.size > 1)) {
const value = i == null ? null : isArray(data) ? data[i] : data.get(i);
context.dispatchValue(value);
}

return r;
}

Expand Down
4 changes: 2 additions & 2 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {timeInterval, utcInterval} from "./time.js";
export const TypedArray = Object.getPrototypeOf(Uint8Array);
const objectToString = Object.prototype.toString;

function isArray(value) {
export function isArray(value) {
return value instanceof Array || value instanceof TypedArray;
}

Expand Down Expand Up @@ -608,7 +608,7 @@ export function maybeClip(clip) {
}

// https://github.com/observablehq/stdlib/blob/746ca2e69135df6178e4f3a17244def35d8d6b20/src/arrow.js#L4C1-L17C1
export function isArrowTable(value) {
function isArrowTable(value) {
return (
value &&
typeof value.getChild === "function" &&
Expand Down
7 changes: 5 additions & 2 deletions src/transforms/basic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {randomLcg} from "d3";
import {ascendingDefined, descendingDefined} from "../defined.js";
import {dataify, isDomainSort, isOptions, maybeValue, valueof} from "../options.js";
import {isArray, isDomainSort, isOptions} from "../options.js";
import {dataify, maybeValue, valueof} from "../options.js";

export function basic({filter: f1, sort: s1, reverse: r1, transform: t1, initializer: i1, ...options} = {}, transform) {
// If both t1 and t2 are defined, returns a composite transform that first
Expand Down Expand Up @@ -101,7 +102,9 @@ function sortTransform(value) {

function sortData(compare) {
return (data, facets) => {
const compareData = (i, j) => compare(data[i], data[j]);
const compareData = isArray(data)
? (i, j) => compare(data[i], data[j])
: (i, j) => compare(data.get(i), data.get(j));
return {data, facets: facets.map((I) => I.slice().sort(compareData))};
};
}
Expand Down
42 changes: 6 additions & 36 deletions src/transforms/group.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,9 @@
import {
InternSet,
deviation,
group as grouper,
max,
maxIndex,
mean,
median,
min,
minIndex,
mode,
rollup,
sort,
sum,
variance
} from "d3";
import {InternSet, group as grouper, rollup, sort} from "d3";
import {deviation, max, maxIndex, mean, median, min, minIndex, mode, sum, variance} from "d3";
import {ascendingDefined} from "../defined.js";
import {
arrayify,
column,
identity,
isObject,
isTemporal,
labelof,
maybeApplyInterval,
maybeColorChannel,
maybeColumn,
maybeInput,
maybeTuple,
percentile,
range,
second,
take,
valueof
} from "../options.js";
import {maybeApplyInterval, maybeColorChannel, maybeColumn, maybeInput, maybeTuple} from "../options.js";
import {isArray, isObject, isTemporal} from "../options.js";
import {column, identity, labelof, percentile, range, second, take, valueof} from "../options.js";
import {basic} from "./basic.js";

// Group on {z, fill, stroke}.
Expand Down Expand Up @@ -445,8 +416,7 @@ export function find(test) {
if (typeof test !== "function") throw new Error(`invalid test function: ${test}`);
return {
reduceIndex(I, V, {data}) {
data = arrayify(data);
return V[I.find((i) => test(data[i], i, data))];
return V[I.find(isArray(data) ? (i) => test(data[i], i, data) : (i) => test(data.get(i), i, data))];
}
};
}
7 changes: 3 additions & 4 deletions src/transforms/stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {InternMap, cumsum, greatest, group, groupSort, max, min, rollup, sum} fr
import {ascendingDefined, descendingDefined} from "../defined.js";
import {withTip} from "../mark.js";
import {maybeApplyInterval, maybeColumn, maybeZ, maybeZero} from "../options.js";
import {arrayify, column, field, lengthof, mid, one, range, valueof} from "../options.js";
import {column, field, isArray, lengthof, mid, one, range, valueof} from "../options.js";
import {basic} from "./basic.js";
import {exclusiveFacets} from "./exclusiveFacets.js";

Expand Down Expand Up @@ -252,7 +252,7 @@ function maybeOrder(order, offset, ky) {
return orderAccessor(field(order));
}
if (typeof order === "function") return (order.length === 1 ? orderAccessor : orderComparator)(order);
if (Array.isArray(order)) return orderGiven(order);
if (isArray(order)) return orderGiven(order);
throw new Error(`invalid order: ${order}`);
}

Expand Down Expand Up @@ -328,8 +328,7 @@ function orderAccessor(f) {

function orderComparator(f) {
return (data) => {
data = arrayify(data);
return (i, j) => f(data[i], data[j]);
return isArray(data) ? (i, j) => f(data[i], data[j]) : (i, j) => f(data.get(i), data.get(j));
};
}

Expand Down

0 comments on commit d0d0b70

Please sign in to comment.