mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-10-09 23:20:55 +08:00
Procedurally resize brush (#650)
* add on brush end event * brush snap move * resize brush
This commit is contained in:
@@ -124,6 +124,63 @@ class HistogramBrush extends React.Component {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onBrushEnd(selection, x) {
|
||||||
|
return () => {
|
||||||
|
const { dispatch, field, isObs, isUserDefined, isDiffExp } = this.props;
|
||||||
|
const { brushX } = this.state;
|
||||||
|
const minAllowedBrushSize = 10;
|
||||||
|
const smallAmountToAvoidInfiniteLoop = 0.1;
|
||||||
|
|
||||||
|
if (d3.event.selection) {
|
||||||
|
let _range;
|
||||||
|
|
||||||
|
if (
|
||||||
|
d3.event.selection[1] - d3.event.selection[0] >
|
||||||
|
minAllowedBrushSize
|
||||||
|
) {
|
||||||
|
_range = [x(d3.event.selection[0]), x(d3.event.selection[1])];
|
||||||
|
} else {
|
||||||
|
/* the user selected range is too small and will be hidden #587, so take control of it procedurally */
|
||||||
|
/* https://stackoverflow.com/questions/12354729/d3-js-limit-size-of-brush */
|
||||||
|
|
||||||
|
const procedurallyResizedBrushWidth =
|
||||||
|
d3.event.selection[0] +
|
||||||
|
minAllowedBrushSize +
|
||||||
|
smallAmountToAvoidInfiniteLoop; //
|
||||||
|
|
||||||
|
_range = [x(d3.event.selection[0]), x(procedurallyResizedBrushWidth)];
|
||||||
|
|
||||||
|
d3.event.target.move(brushX, [
|
||||||
|
d3.event.selection[0],
|
||||||
|
procedurallyResizedBrushWidth
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
dispatch({
|
||||||
|
type: "continuous metadata histogram brush",
|
||||||
|
selection: field,
|
||||||
|
continuousNamespace: {
|
||||||
|
isObs,
|
||||||
|
isUserDefined,
|
||||||
|
isDiffExp
|
||||||
|
},
|
||||||
|
range: _range
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
dispatch({
|
||||||
|
type: "continuous metadata histogram brush",
|
||||||
|
selection: field,
|
||||||
|
continuousNamespace: {
|
||||||
|
isObs,
|
||||||
|
isUserDefined,
|
||||||
|
isDiffExp
|
||||||
|
},
|
||||||
|
range: null
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
drawHistogram(svgRef) {
|
drawHistogram(svgRef) {
|
||||||
const { obsAnnotations, field, ranges } = this.props;
|
const { obsAnnotations, field, ranges } = this.props;
|
||||||
const histogramCache = this.calcHistogramCache(
|
const histogramCache = this.calcHistogramCache(
|
||||||
@@ -224,7 +281,8 @@ class HistogramBrush extends React.Component {
|
|||||||
.attr("height", d => y(0) - y(d.length / numValues));
|
.attr("height", d => y(0) - y(d.length / numValues));
|
||||||
|
|
||||||
/* BRUSH */
|
/* BRUSH */
|
||||||
d3.select(svgRef)
|
const brushX = d3
|
||||||
|
.select(svgRef)
|
||||||
.append("g")
|
.append("g")
|
||||||
.attr("class", "brush")
|
.attr("class", "brush")
|
||||||
.attr("data-testid", `${svgRef.id}-brush`)
|
.attr("data-testid", `${svgRef.id}-brush`)
|
||||||
@@ -232,7 +290,7 @@ class HistogramBrush extends React.Component {
|
|||||||
d3
|
d3
|
||||||
.brushX()
|
.brushX()
|
||||||
.on("brush", this.onBrush(field, x.invert).bind(this))
|
.on("brush", this.onBrush(field, x.invert).bind(this))
|
||||||
.on("end", this.onBrush(field, x.invert).bind(this))
|
.on("end", this.onBrushEnd(field, x.invert).bind(this))
|
||||||
);
|
);
|
||||||
|
|
||||||
/* AXIS */
|
/* AXIS */
|
||||||
@@ -253,6 +311,8 @@ class HistogramBrush extends React.Component {
|
|||||||
d3.select(svgRef)
|
d3.select(svgRef)
|
||||||
.selectAll(".axis--x line")
|
.selectAll(".axis--x line")
|
||||||
.style("stroke", "rgb(230,230,230)");
|
.style("stroke", "rgb(230,230,230)");
|
||||||
|
|
||||||
|
this.setState({ brushX });
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
|||||||
Reference in New Issue
Block a user