heatmap_generator = type_i => {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, total_width, height.top]);
var brush = d3.brush()
.extent([[margin.left, margin.top], [width.right - margin.right, height.top - margin.bottom]])
.on("brush end", brushend);
const plot1 = svg.append("g");
const plot2 = svg.append("g")
.attr("transform", `translate(${width.left}, 0)`);
plot_hist(type_i, plot1, x_hist);
plot_heatmap(type_i, plot2 , x_year_torque, y_city);
plot2.append("g")
.attr("id", "brush")
.call(brush);
function brushend({selection}) {
d3.selectAll(".signal_plot")
.attr("d", d => line(x_year_signal, y_signal[type_i])(d[1]))
.style("opacity", 1)
.style("stroke", "steelblue");
d3.selectAll(".map_plot")
.style("opacity", 0.15);
if(selection){
var x_range = [selection[0][0], selection[1][0]].map(x_year_torque.invert).sort((a, b) => a - b);
if (x_range[1] > 34) x_range[1] = 34;
var y_invert = [selection[0][1], selection[1][1]].map(y_city.invert).sort((a, b) => a - b);
x_year_signal.domain(x_range);
y_signal[type_i].domain(
d3.extent(
torque
.filter(d => d.type == type[type_i])
.filter(d => (y_invert[0] - 50 <= d.city) && (y_invert[1] + 50 >= d.city))
.filter(d => (x_range[0] - 4 <= d.year) && (x_range[1] + 4 >= d.year))
.map(d => d.signal)
)
);
d3.select(".signal_x_axis").call(d3.axisBottom(x_year_signal).tickFormat(d => d + 1986));
d3.select(".signal_y_axis").call(d3.axisLeft(y_signal[type_i]));
d3.selectAll(".signal_plot")
.attr("d", d => {
console.log(d);
return line(x_year_signal, y_signal[type_i])(d[1]);
});
d3.selectAll(".signal_plot")
.style("opacity", d => {
var is_between = (d[0] >= y_invert[0]) && (d[0] <= y_invert[1]);
return is_between ? 1 : 0.1;
})
.style("stroke", d => {
var is_between = (d[0] >= y_invert[0]) && (d[0] <= y_invert[1]);
return is_between ?"steelblue" : "#dddddd"
});
var cities_selected = cities_cd_sorted.slice(parseInt(y_invert[0]), parseInt(y_invert[1])).forEach(d => {
d3.select("#city_" + d.cd_geocmu)
.style("opacity", 1);
});
} else {
x_year_signal.domain(d3.extent(torque, d => d.year));
y_signal[type_i].domain(d3.extent(torque.filter(d => d.type == type[type_i]).map(d => d.signal)));
d3.select(".signal_x_axis").call(d3.axisBottom(x_year_signal).tickFormat(d => d + 1986));
d3.select(".signal_y_axis").call(d3.axisLeft(y_signal[type_i]));
}
}
return svg.node();
}