function chart() {
let btnPanel = undefined;
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("style", "border: 1px solid #eee")
.attr("xmlns", "http://www.w3.org/2000/svg");
svg.call(xAxis);
svg.call(yAxis);
const brush = d3
.brush()
.extent([
[0, 0],
[width, height]
])
.on("end", brushedRectBrushStartBrush);
svg.append("g").attr("class", "brush").call(brush);
let tooltip;
const barArea = svg
.selectAll(".g-area")
.data([0])
.join("g")
.selectAll("path")
.data([data["attn_dist"].map((d, i) => [d, i])])
.join("path")
.attr("d", (d) => {
return area(d);
})
.style("stroke-width", 4)
.style("fill", "none")
const bar = svg
.selectAll(".g-bar")
.data([0])
.join("g")
.selectAll("path")
.data([data["attn_dist"].map((d, i) => [d, i])])
.join("path")
.attr("d", (d) => {
return lineBuilder(d);
})
.style("stroke", (d) => color(d[4]))
.style("stroke-width", 2)
.style("fill", "transparent");
const dots = svg
.append("g")
.selectAll("circle")
.data(data["attn_dist"].map((d, i) => [d, i]))
.join("circle")
.attr("class", "dot")
.attr("r", 2)
.attr("fill", "black")
.attr("stroke", "white")
.attr("stroke-width", 1)
.attr("cx", function (d) {
return xScale(d[1]) + xScale.bandwidth() / 2;
})
.attr("cy", function (d) {
return yScale(d[0]);
})
.on("mouseover", function (d) {
if (xDomainsKey[d[1]] == undefined) console.log(d)
if (!tooltip) tooltip = createToolTip(svg);
tooltip.display(`${xDomainsKey[d[1]]}:${d[0]}`, [d3.event.pageX - margin.left, d3.event.pageY - margin.top - margin.bottom - 30]);
})
.on("mouseout", function (d) {
tooltip.hide()
});
function brushedRectBrushStartBrush() {
console.log(d3.event.type, d3.event.sourceEvent.type);
const extent = d3.event.selection;
console.log("check extent", extent);
if (extent) {
let selected_dots = [];
dots.classed("selected", function (d) {
let res = isBrushed(
extent,
xScale(d[1]) + xScale.bandwidth() / 2,
yScale(d[0])
);
if (res) selected_dots.push(d);
console.log(res)
return res;
});
highlightSelected(selected_dots, extent);
} else {
dots.classed("selected", false);
const bar = svg.selectAll(".g-tmp").remove();
if (btnPanel) btnPanel.forEach((btn) => btn.hide());
}
}
function highlightSelected(selectedItems, extent) {
console.log(selectedItems)
const bar = svg
.selectAll(".g-tmp")
.data([0])
.join("g")
.attr("class", "g-tmp")
.selectAll("path")
.data([selectedItems])
.join("path")
.attr("d", (d) => {
return lineBuilder(d);
})
.style("stroke", (d) => "red")
.style("stroke-width", 2)
.style("fill", "transparent");
if (!btnPanel) {
btnPanel = [addIcon1([80, 10], svg), addIcon2([80, 10], svg)];
}
btnPanel.forEach((btn) => btn.display(extent[0]));
}
function isBrushed(brush_coords, cx, cy) {
const x0 = brush_coords[0][0];
const x1 = brush_coords[1][0];
const y0 = brush_coords[0][1];
const y1 = brush_coords[1][1];
return x0 <= cx && cx <= x1 && y0 <= cy && cy <= y1;
}
return svg.node();
}