remove conditional rendering cases from color legend (#1952)

* Revert "Remove Continuous vars with 1 value from histogram, add to info drawer (#1927)"

This reverts commit 242546371b.

* remove conditional rendering cases

* ignore pointer events

Co-authored-by: Madison Dunitz <madison.dunitz@chanzuckerberg.com>
This commit is contained in:
Severiano Badajoz
2020-10-28 15:35:28 -07:00
committed by GitHub
co-authored by Madison Dunitz
parent 924b518492
commit 727af83152
+25 -58
View File
@@ -11,20 +11,20 @@ import {
// create continuous color legend // create continuous color legend
// http://bl.ocks.org/syntagmatic/e8ccca52559796be775553b467593a9f // http://bl.ocks.org/syntagmatic/e8ccca52559796be775553b467593a9f
const continuous = (selectorId, colorscale, colorAccessor) => { const continuous = (selectorId, colorScale, colorAccessor) => {
const legendheight = 200; const legendHeight = 200;
const legendwidth = 80; const legendWidth = 80;
const margin = { top: 10, right: 60, bottom: 10, left: 2 }; const margin = { top: 10, right: 60, bottom: 10, left: 2 };
const canvas = d3 const canvas = d3
.select(selectorId) .select(selectorId)
.style("height", `${legendheight}px`) .style("height", `${legendHeight}px`)
.style("width", `${legendwidth}px`) .style("width", `${legendWidth}px`)
.append("canvas") .append("canvas")
.attr("height", legendheight - margin.top - margin.bottom) .attr("height", legendHeight - margin.top - margin.bottom)
.attr("width", 1) .attr("width", 1)
.style("height", `${legendheight - margin.top - margin.bottom}px`) .style("height", `${legendHeight - margin.top - margin.bottom}px`)
.style("width", `${legendwidth - margin.left - margin.right}px`) .style("width", `${legendWidth - margin.left - margin.right}px`)
.style("position", "absolute") .style("position", "absolute")
.style("top", `${margin.top + 1}px`) .style("top", `${margin.top + 1}px`)
.style("left", `${margin.left + 1}px`) .style("left", `${margin.left + 1}px`)
@@ -37,18 +37,18 @@ const continuous = (selectorId, colorscale, colorAccessor) => {
const ctx = canvas.getContext("2d"); const ctx = canvas.getContext("2d");
const legendscale = d3 const legendScale = d3
.scaleLinear() .scaleLinear()
.range([1, legendheight - margin.top - margin.bottom]) .range([1, legendHeight - margin.top - margin.bottom])
.domain([ .domain([
colorscale.domain()[1], colorScale.domain()[1],
colorscale.domain()[0], colorScale.domain()[0],
]); /* we flip this to make viridis colors dark if high in the color scale */ ]); /* we flip this to make viridis colors dark if high in the color scale */
// image data hackery based on http://bl.ocks.org/mbostock/048d21cf747371b11884f75ad896e5a5 // image data hackery based on http://bl.ocks.org/mbostock/048d21cf747371b11884f75ad896e5a5
const image = ctx.createImageData(1, legendheight); const image = ctx.createImageData(1, legendHeight);
d3.range(legendheight).forEach((i) => { d3.range(legendHeight).forEach((i) => {
const c = d3.rgb(colorscale(legendscale.invert(i))); const c = d3.rgb(colorScale(legendScale.invert(i)));
image.data[4 * i] = c.r; image.data[4 * i] = c.r;
image.data[4 * i + 1] = c.g; image.data[4 * i + 1] = c.g;
image.data[4 * i + 2] = c.b; image.data[4 * i + 2] = c.b;
@@ -66,20 +66,20 @@ const continuous = (selectorId, colorscale, colorAccessor) => {
}); });
*/ */
const legendaxis = d3 const legendAxis = d3
.axisRight(legendscale) .axisRight(legendScale)
.ticks(6) .ticks(6)
.tickFormat( .tickFormat(
d3.format( d3.format(
legendscale.domain().some((n) => Math.abs(n) >= 10000) ? ".0e" : "," legendScale.domain().some((n) => Math.abs(n) >= 10000) ? ".0e" : ","
) )
); );
const svg = d3 const svg = d3
.select(selectorId) .select(selectorId)
.append("svg") .append("svg")
.attr("height", `${legendheight}px`) .attr("height", `${legendHeight}px`)
.attr("width", `${legendwidth}px`) .attr("width", `${legendWidth}px`)
.style("position", "absolute") .style("position", "absolute")
.style("left", "0px") .style("left", "0px")
.style("top", "0px"); .style("top", "0px");
@@ -89,16 +89,16 @@ const continuous = (selectorId, colorscale, colorAccessor) => {
.attr("class", "axis") .attr("class", "axis")
.attr( .attr(
"transform", "transform",
`translate(${legendwidth - margin.left - margin.right + 3},${margin.top})` `translate(${legendWidth - margin.left - margin.right + 3},${margin.top})`
) )
.call(legendaxis); .call(legendAxis);
// text label for the y axis // text label for the y axis
svg svg
.append("text") .append("text")
.attr("transform", "rotate(-90)") .attr("transform", "rotate(-90)")
.attr("y", 2) .attr("y", 2)
.attr("x", 0 - legendheight / 2) .attr("x", 0 - legendHeight / 2)
.attr("dy", "1em") .attr("dy", "1em")
.style("text-anchor", "middle") .style("text-anchor", "middle")
.style("fill", "white") .style("fill", "white")
@@ -110,24 +110,7 @@ const continuous = (selectorId, colorscale, colorAccessor) => {
colors: state.colors, colors: state.colors,
})) }))
class ContinuousLegend extends React.Component { class ContinuousLegend extends React.Component {
constructor(props) { async componentDidUpdate(prevProps) {
super(props);
this.ref = null;
this.state = {
colorAccessor: null,
colorScale: null,
};
}
componentDidMount() {
this.updateState(null);
}
componentDidUpdate(prevProps) {
this.updateState(prevProps);
}
async updateState(prevProps) {
const { annoMatrix, colors } = this.props; const { annoMatrix, colors } = this.props;
if (!colors || !annoMatrix) return; if (!colors || !annoMatrix) return;
@@ -161,35 +144,19 @@ class ContinuousLegend extends React.Component {
); );
} }
} }
this.setState({
colorAccessor,
colorScale: colorTable.scale,
});
} }
} }
render() { render() {
const { colorAccessor, colorScale } = this.state;
if (
colorScale?.domain &&
colorScale.domain()[1] === colorScale.domain()[0]
) {
/* it's a single value, not a distribution, min max are the same */
return null;
}
return ( return (
<div <div
id="continuous_legend" id="continuous_legend"
ref={(ref) => {
this.ref = ref;
}}
style={{ style={{
display: colorAccessor ? "inherit" : "none",
position: "absolute", position: "absolute",
left: 8, left: 8,
top: 35, top: 35,
zIndex: 1, zIndex: 1,
pointerEvents: "none",
}} }}
/> />
); );