Skip to content

sort hex bins by radius (descending) #808

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

Closed
wants to merge 2 commits into from
Closed
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
16 changes: 8 additions & 8 deletions src/transforms/hexbin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export function hexbin(outputs = {fill: "count"}, options = {}) {

// TODO group by (implicit) z
// TODO filter e.g. to show empty hexbins?
// TODO data output with sort and reverse?
// TODO disallow x, x1, x2, y, y1, y2 reducers?
function hexbinn(outputs, {radius = 10, ...options}) {
radius = +radius;
Expand Down Expand Up @@ -44,14 +43,15 @@ function hexbinn(outputs, {radius = 10, ...options}) {
}
binFacets.push(binFacet);
}
return {
facets: binFacets,
channels: {
x: {value: BX},
y: {value: BY},
...Object.fromEntries(outputs.map(({name, output}) => [name, {scale: true, radius: name === "r" ? radius : undefined, value: output.transform()}]))
}
const channels = {
x: {value: BX},
y: {value: BY},
...Object.fromEntries(outputs.map(({name, output}) => [name, {scale: true, radius: name === "r" ? radius : undefined, value: output.transform()}]))
};
if ("r" in channels) {
binFacets.forEach(index => index.sort((i, j) => channels.r.value[j] - channels.r.value[i]));
}
return {facets: binFacets, channels};
}
};
}
164 changes: 82 additions & 82 deletions test/output/hexbin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
521 changes: 521 additions & 0 deletions test/output/hexbinR.html

Large diffs are not rendered by default.

236 changes: 236 additions & 0 deletions test/output/hexbinSymbol.html

Large diffs are not rendered by default.

195 changes: 195 additions & 0 deletions test/output/hexbinText.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions test/plots/hexbin-r.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as Plot from "@observablehq/plot";
import * as d3 from "d3";

export default async function() {
const penguins = await d3.csv("data/penguins.csv", d3.autoType);
return Plot.plot({
width: 820,
height: 320,
color: {scheme: "reds", nice: true, tickFormat: d => 100 * d, label: "Proportion of each facet (%)", legend: true},
facet: {
data: penguins,
x: "sex",
marginRight: 80
},
marks: [
Plot.frame(),
Plot.dot(penguins, Plot.hexbin({title: "proportion-facet", r: "count", fill: "proportion-facet"}, {x: "culmen_depth_mm", y: "culmen_length_mm", strokeWidth: 1}))
]
});
}
18 changes: 18 additions & 0 deletions test/plots/hexbin-symbol.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as Plot from "@observablehq/plot";
import * as d3 from "d3";

export default async function() {
const penguins = await d3.csv("data/penguins.csv", d3.autoType);
return Plot.plot({
grid: true,
marks: [
Plot.dot(penguins, Plot.hexbin({r: "count", symbol: "mode"}, {
radius: 20,
symbol: "sex",
x: "culmen_depth_mm",
y: "culmen_length_mm"
}))
],
symbol: {legend: true}
});
}
21 changes: 21 additions & 0 deletions test/plots/hexbin-text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as Plot from "@observablehq/plot";
import * as d3 from "d3";

export default async function() {
const penguins = await d3.csv("data/penguins.csv", d3.autoType);
return Plot.plot({
width: 820,
height: 320,
facet: {
data: penguins,
x: "sex",
marginRight: 80
},
inset: 14,
marks: [
Plot.frame(),
Plot.dot(penguins, Plot.hexbin({fillOpacity: "count"}, {x: "culmen_depth_mm", y: "culmen_length_mm", fill: "brown", stroke: "black", strokeWidth: 0.5})),
Plot.text(penguins, Plot.hexbin({text: "count"}, {x: "culmen_depth_mm", y: "culmen_length_mm"}))
]
});
}
3 changes: 3 additions & 0 deletions test/plots/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export {default as googleTrendsRidgeline} from "./google-trends-ridgeline.js";
export {default as gridChoropleth} from "./grid-choropleth.js";
export {default as hadcrutWarmingStripes} from "./hadcrut-warming-stripes.js";
export {default as hexbin} from "./hexbin.js";
export {default as hexbinR} from "./hexbin-r.js";
export {default as hexbinSymbol} from "./hexbin-symbol.js";
export {default as hexbinText} from "./hexbin-text.js";
export {default as highCardinalityOrdinal} from "./high-cardinality-ordinal.js";
export {default as identityScale} from "./identity-scale.js";
export {default as industryUnemployment} from "./industry-unemployment.js";
Expand Down