Skip to content

improve bin type coercion #790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2022
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
5 changes: 3 additions & 2 deletions src/transforms/bin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {bin as binner, extent, thresholdFreedmanDiaconis, thresholdScott, thresholdSturges, utcTickInterval} from "d3";
import {valueof, range, identity, maybeLazyChannel, maybeTuple, maybeColorChannel, maybeValue, mid, labelof, isTemporal} from "../options.js";
import {coerceDate} from "../scales.js";
import {coerceDate, coerceNumber} from "../scales.js";
import {basic} from "./basic.js";
import {hasOutput, maybeEvaluator, maybeGroup, maybeOutput, maybeOutputs, maybeReduce, maybeSort, maybeSubgroup, reduceCount, reduceIdentity} from "./group.js";
import {maybeInsetX, maybeInsetY} from "./inset.js";
Expand Down Expand Up @@ -181,7 +181,7 @@ function maybeBin(options) {
if (options == null) return;
const {value, cumulative, domain = extent, thresholds} = options;
const bin = data => {
let V = valueof(data, value);
let V = valueof(data, value, Array); // d3.bin prefers Array input
const bin = binner().value(i => V[i]);
if (isTemporal(V) || isTimeThresholds(thresholds)) {
V = V.map(coerceDate);
Expand All @@ -197,6 +197,7 @@ function maybeBin(options) {
}
bin.thresholds(t).domain([min, max]);
} else {
V = V.map(coerceNumber);
let d = domain;
let t = thresholds;
if (isInterval(t)) {
Expand Down
105 changes: 105 additions & 0 deletions test/output/binStrings.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 86 additions & 0 deletions test/output/binTimestamps.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
105 changes: 105 additions & 0 deletions test/output/stringBins.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions test/plots/bin-strings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as Plot from "@observablehq/plot";

export default async function() {
return Plot.rectY(["9.6", "9.6", "14.8", "14.8", "7.2"], Plot.binX()).plot();
}
7 changes: 7 additions & 0 deletions test/plots/bin-timestamps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as Plot from "@observablehq/plot";
import * as d3 from "d3";

export default async function() {
const timestamps = Float64Array.of(1609459200000, 1609545600000, 1609632000000, 1609718400000, 1609804800000, 1609891200000, 1609977600000);
return Plot.rectY(timestamps, Plot.binX({y: "count"}, {interval: d3.utcDay})).plot();
}
2 changes: 2 additions & 0 deletions test/plots/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export {default as athletesWeightCumulative} from "./athletes-weight-cumulative.
export {default as availability} from "./availability.js";
export {default as ballotStatusRace} from "./ballot-status-race.js";
export {default as beckerBarley} from "./becker-barley.js";
export {default as binStrings} from "./bin-strings.js";
export {default as binTimestamps} from "./bin-timestamps.js";
export {default as boxplot} from "./boxplot.js";
export {default as caltrain} from "./caltrain.js";
export {default as caltrainDirection} from "./caltrain-direction.js";
Expand Down