d3-cross-highlighting
How to implement cross-highlighting between D3.js visualizations and HTML Data Tables. Use this skill whenever the user wants to connect a chart (like a bubble chart or scatter plot) with an HTML table, so clicking or hovering one updates the other.
- License 许可
- MIT
- 最近更新
- 2026-10-02
- Stars
- ★ 93
- Forks
- 8
01 前言 Front Matter
- name
- d3-cross-highlighting
- description
- How to implement cross-highlighting between D3.js visualizations and HTML Data Tables. Use this skill whenever the user wants to connect a chart (like a bubble chart or scatter plot) with an HTML table, so clicking or hovering one updates the other.
03 SKILL.md 正文
d3-cross-highlighting
This skill explains how to synchronize state between a D3.js chart and an HTML <table/> to enable cross-highlighting (e.g., clicking a bubble highlights the table row, clicking a table row highlights the bubble).
Architecture
- Unique Identifiers: Ensure every data row has a unique identifier (like an ID or a ticker symbol).
- State Management: Keep track of the currently selected identifier.
- Render/Update Logic: Write a function that updates the visual state of both the SVG chart and the HTML table based on the selected identifier.
- Event Listeners: Attach click/hover listeners to both SVG shapes and HTML table rows that trigger the update logic.
Example Implementation
Setup Data and Table
// Data must have a unique ID, e.g., 'ticker'
const data = [
{ ticker: "AAPL", name: "Apple", value: 100 },
{ ticker: "MSFT", name: "Microsoft", value: 95 }
];
let selectedTicker = null; // Global state for selection
// Render HTML table rows
const tableBody = d3.select("#data-table tbody");
const rows = tableBody.selectAll("tr")
.data(data, d => d.ticker)
.join("tr")
.attr("id", d => `row-${d.ticker}`) // Optional, useful for scrolling
.on("click", (event, d) => {
// Toggle selection
selectedTicker = selectedTicker === d.ticker ? null : d.ticker;
updateSelection();
});
rows.selectAll("td")
.data(d => [d.ticker, d.name, d.value])
.join("td")
.text(d => d);
Setup SVG Chart
// Render bubbles
const bubbles = svg.selectAll("circle")
.data(data, d => d.ticker)
.join("circle")
.attr("class", "chart-bubble")
// ... other attributes like r, cx, cy ...
.on("click", (event, d) => {
// Toggle selection
selectedTicker = selectedTicker === d.ticker ? null : d.ticker;
updateSelection();
});
Update Function
This function applies visual changes to both the table and the chart based on the selectedTicker.
function updateSelection() {
// Update Table Rows
rows.classed("highlighted-row", d => d.ticker === selectedTicker);
// Optionally, scroll the table to show the highlighted row
if (selectedTicker) {
const rowElement = document.getElementById(`row-${selectedTicker}`);
if (rowElement) {
rowElement.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
}
// Update SVG Bubbles
bubbles
.classed("highlighted-bubble", d => d.ticker === selectedTicker)
.attr("stroke", d => d.ticker === selectedTicker ? "black" : "none")
.attr("stroke-width", d => d.ticker === selectedTicker ? 3 : 0)
.style("opacity", d => (selectedTicker === null || selectedTicker === d.ticker) ? 1 : 0.3);
}
CSS Styling
Define classes to visually emphasize the selected state.
/* Table highlighting */
.highlighted-row {
background-color: #ffffcc;
font-weight: bold;
}
/* Bubble highlighting */
.highlighted-bubble {
/* Typically handled via SVG attributes or stroke, but can also use CSS */
}
/* Base styles for transition */
.chart-bubble {
transition: opacity 0.2s, stroke 0.2s;
cursor: pointer;
}
tr {
cursor: pointer;
}
Considerations
- Hover vs. Click: If implementing hover logic, track a
hoveredTickervariable and apply CSS classes similarly in anupdateHoverSelection()function, attaching it to"mouseenter"and"mouseleave"events. - Performance: For very large datasets, using
.classed()on D3 selections is efficient, as it batches DOM updates.
04 同仓库更多 Skills
object_counter
Count occurrences of an object in the image using computer vision algorithm.
travel-planning-workflow
Invoke this skill when performing travel planning. It defines a structured workflow for generating itineraries. Following this workflow is essential to ensure the final plan satisfies user requirements.
bundled-dataset-search-tool
invoke this skill when you need to perform database search for travel planning. This skill provides some useful pre-packaged tools to look up accommodations, attractions, cities, driving distance, flights, and restaurants from the bundled dataset.
glm-output
Output-processing guidance for GLM tasks. Especially useful after glm-basics and glm-calibration when you need verifier-matching metrics from output.nc and a final /root/metrics.json.
glm-calibration
Calibration guidance for GLM tasks. Often most effective after glm-basics has clarified the setup; glm-output is the companion skill for exact final metric computation.
glm-basics
A good starting point for GLM calibration tasks. Use to inspect glm3.nml, confirm how GLM runs, and identify the relevant files before moving on to calibration and output evaluation.