I am using d3 v7 in a ReactJS 17.0.2 project. I'd like to integrate the code shown here: https://www.d3-graph-gallery.com/graph/bubble_tooltip.html I receive the data from the function props, but if I pass them directly to svg.append().data() I get the axis shown ok, but the bubbles do not show. The code I use to create the circles is this:
function D3BubbleChart({ input }) {
const ref = useD3(
(svg) => {
/* some margin, tooltip and axis stuff here */
...
/* then I try to create the circles like this */
svg
.append("g")
.selectAll("dot")
.data(mydata)
.append("circle")
.attr("class", "bubbles")
.attr("cx", (d) => x(d.average_value))
.attr("cy", (d) => y(d.average_price))
.attr("r", (d) => z(d.pieces))
.style("fill", "red")
.on("mouseover", showTooltip)
.on("mousemove", moveTooltip)
.on("mouseleave", hideTooltip)
Everything else (axis, tooltips...) seem to be built OK, but the nodes are empty. The Svg element in the HTML page looks like this:
<svg style="height: 500px; width: 100%; margin-right: 0px; margin-left: 0px;">
<g id="plot-area">
<svg width="500" height="420">
<g transform="translate(50,10)">
<g transform="translate(0, 380)" fill="none" font-size="10" font-family="sans-serif" text-anchor="middle">...</g>
<g class="tick" opacity="1" transform="translate(36.33333333333333,0)">...</g>
...
</svg><div class="tooltip" style="opacity: 0; background-color: black; border-radius: 5px; padding: 10px; color: white;"></div></g>
<g class="x-axis"></g>
<g class="y-axis"></g>
</svg>
I copied only the relevant elements to show that the g elements are empty. Am I maybe using d3 v6 code for d3 v7? I was not able to find documentation specific to bubble charts for d3 v7. I checked data and it contains data, but I also tried to hardcode scalar values for cx, cy and r with the same result. I feel that the problem might be related to the creation of the circles.
I finally managed to get it working. I post here the code for others trying to do the same thing.
const ref = useD3(
(svg) => {
// set the dimensions and margins of the graph
const margin = { top: 10, right: 20, bottom: 30, left: 50 },
width = 1080 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// append the svg object to the body of the page
svg = d3
.select("#plot-area")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
// Add X axis
const x = d3.scaleLog().domain([1, 2000]).range([0, width]);
svg
.append("g")
.attr("transform", `translate(0, ${height})`)
.call(d3.axisBottom(x));
// Add Y axis
const y = d3.scaleLinear().domain([0, 4]).range([height, 0]).nice();
svg.append("g").call(d3.axisLeft(y));
// Add a scale for bubble size
const z = d3.scaleLog().domain([1, 1000]).range([1, 100]);
// -1- Create a tooltip div that is hidden by default:
const tooltip = d3
.select("#plot-area")
.append("div")
.style("opacity", 1)
.style("position", "absolute")
.attr("class", "tooltip")
.style("background-color", "black")
.style("border-radius", "5px")
.style("padding", "10px")
.style("color", "white");
// -2- Create 3 functions to show / update (when mouse move but stay on same circle) / hide the tooltip
const showTooltip = function (event, d) {
tooltip.transition().duration(200);
tooltip
.style("opacity", 1)
.html("Collection: " + d.name + " " + d.tokens + " tokens")
.style("left", event.x / 2 + "px")
.style("top", event.y / 2 + 30 + "px");
};
const moveTooltip = function (event, d) {
tooltip
.style("left", event.x / 2 + "px")
.style("top", event.y / 2 + 30 + "px");
};
const hideTooltip = function (event, d) {
tooltip.transition().duration(200).style("opacity", 0);
};
// Add dots
svg
.append("g")
.selectAll("dot")
.data(input)
.join("circle")
.attr("class", "bubbles")
.attr("cx", (d) => x(d.average_value))
.attr("cy", (d) => y(d.average_price))
.attr("r", (d) => z(d.pieces))
.style("fill", "red")
// -3- Trigger the functions
.on("mouseover", function () {
return tooltip.style("visibility", "visible");
})
.on("mousemove", function (event) {
return tooltip
.style("top", event.pageY + "px")
.style("left", event.pageX + "px");
})
.on("mouseout", function () {
return tooltip.style("visibility", "hidden");
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.