-
Hello, Platform:I am coding in observable Notebook using observable plot. I would like Observablehq community help for an issue I am facing regarding classes Issue:Facing an issue where the classes added to an element on plot is not displaying the styles as per the requirement defined in classes. Description of Issue:As shown in the video below,
I hope this explains my issue. I have copied and built up on Fill's code which selects an element as per the pointer. Multiple line chart with hover. Please correct me if I missed any logic, I tried chatGPT and did not solve this issue, the solution it provided is, to use Note:_However, if I add the classes to a HTML observable Notebook cell with style tags then it works, nonetheless, i would like to add the style element to svg owner element. Code:The code responsible for my issue
The code that works well when I use direct styling
Observable Notebook: Observable Notebook Link Please see the issue is action:Screen.Recording.2025-03-27.223606.mp4Conclusion:I would like your help for the issue facing regarding the classed true added to an element and thank you in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
hello, I finally managed to resolve the issue. Reason:The issue was caused by using a randomly generated ${id} in the style selector. Since the direct parents of the targetElements did not have this generated ID, the target elements did not apply the styles, even though .is-target was added with .classed(true). Solution:For the styles to take effect, the parent element must have the corresponding class. Instead of using a randomly generated ${id}, I used the existing class of the element. This ensures that the target elements correctly inherit and apply the styles. Working code // Select the existing <style> element inside the SVG
let styleElement = d3.select(svg).select("style");
const svgClass = d3.select(svg).attr("class");
// Get existing styles
let existingStyles = styleElement.html();
// Define the new CSS rule
const newStyleRule = `
.${svgClass} .is-target {
${Object.entries(targetElementStyles)
.map(([key, value]) => `${key}: ${value};`)
.join(" ")}
}`;
// Prevent duplicate styles
if (!existingStyles.includes("is-target".trim())) {
styleElement.html(existingStyles + newStyleRule);
} Now, the target elements correctly apply the styles as expected. Thank you. |
Beta Was this translation helpful? Give feedback.
hello,
I finally managed to resolve the issue.
Reason:
The issue was caused by using a randomly generated ${id} in the style selector. Since the direct parents of the targetElements did not have this generated ID, the target elements did not apply the styles, even though .is-target was added with .classed(true).
Solution:
For the styles to take effect, the parent element must have the corresponding class. Instead of using a randomly generated ${id}, I used the existing class of the element. This ensures that the target elements correctly inherit and apply the styles.
Working code