viewof focus = {
if (colorView === "Heatwaves") {
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, focusHeight])
.style("display", "block");
const x = d3
.scaleTime()
.domain(dateExtent)
.range([margin.left, width - margin.right]);
const brush = d3
.brushX()
.extent([
[margin.left, 0.5],
[width - margin.right, focusHeight - margin.bottom + 0.5]
])
.on("brush", brushed)
.on("end", brushended);
const defaultSelection = [
x(d3.utcYear.offset(x.domain()[1], -1)),
x.range()[1]
];
svg.append("g").call(xAxis, x, focusHeight).attr("class", "axisWhite");
let previousS0, previousS1;
const chartData = stack(monthlyGrouped);
const groups = svg
.append("g")
.selectAll("g")
.data(chartData)
.join("g")
// rects in the same layer will all have the same color, so we can put it on the group
// we can use the key on the layer's array to set the color
.style("fill", (d, i) => {
return colors.get(d.key);
});
// .attr("d", area(x, y.copy().range([focusHeight - margin.bottom, 4])));
groups
.selectAll("rect")
// Now we place the rects, which are the children of the layer array
.data((d) => d)
.join("rect")
.attr("x", (d) => {
// console.log(d.data.date);
return x1(new Date(d.data.date).toISOString().substring(0, 10));
})
.attr("y", (d) => y(d[1]))
.attr("height", (d) => y(d[0]) - y(d[1]))
.attr("width", x1.bandwidth());
const gb = svg.append("g").call(brush);
gb.call(brush.move, defaultSelection);
function brushed({ selection }) {
if (selection) {
// console.log("fire1");
var s = selection || x.range();
svg.property("value", selection.map(x.invert, x).map(d3.utcMonth.ceil));
svg.dispatch("input");
if (((s[1] - s[0]) / width) * 120 > 30) {
gb.call(brush.move, [previousS0, previousS1]);
return;
}
previousS0 = s[0];
previousS1 = s[1];
}
}
function brushended({ selection }) {
if (!selection) {
gb.call(brush.move, [defaultSelection]);
}
}
return svg.node();
/////
} else {
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, focusHeight])
.style("display", "block");
const x = d3
.scaleTime()
.domain(dateExtent)
.range([margin.left, width - margin.right]);
const brush = d3
.brushX()
.extent([
[margin.left, 0.5],
[width - margin.right, focusHeight - margin.bottom + 0.5]
])
.on("brush", brushed)
.on("end", brushended);
const defaultSelection = [
x(d3.utcYear.offset(x.domain()[1], -1)),
x.range()[1]
];
svg.append("g").call(xAxis, x, focusHeight).attr("class", "axisWhite");
let previousS0, previousS1;
// svg.append("path")
const bars = svg
.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("x", (d) => x1(d.date.toISOString().substring(0, 10)))
.attr("y", (d) => {
return y(1);
})
.attr("width", (d) => x1.bandwidth())
.attr("height", (d) => y(0) - y(1))
// .attr("fill", (d) => sstaColors(d.value));
.attr("fill", (d) => (d.value === -99 ? "#ddd" : sstaColors(d.value)));
// .attr("d", area(x, y.copy().range([focusHeight - margin.bottom, 4])));
const gb = svg.append("g").call(brush);
gb.call(brush.move, defaultSelection);
function brushed({ selection }) {
// mutable debug1 = selection;
if (selection) {
// console.log("fire1");
var s = selection || x.range();
svg.property("value", selection.map(x.invert, x).map(d3.utcMonth.ceil));
svg.dispatch("input");
if (
((s[1] - s[0]) / width) * 120 > 30 ||
((s[1] - s[0]) / width) * 120 < 2
) {
gb.call(brush.move, [previousS0, previousS1]);
return;
}
previousS0 = s[0];
previousS1 = s[1];
}
}
function brushended({ selection }) {
if (!selection) {
mutable debug1 = defaultSelection;
gb.call(brush.move, [previousS0, previousS1]); // changed from defaultSelection so that a single click doesn't mess things up
}
}
return svg.node();
}
}