score:4
It might be worth reviewing the ideas behind d3 & data binding to the dom.
Here:
.on("mouseover", function(d) {
var xPosition = 100;
var yPosition = 100;
d3.select("#tooltip")
.data(pie(data)) // re-bind tooltip with entire data set
.style("left", xPosition + "px")
.style("top", yPosition + "px")
.select("#NumberStudents")
.text(function(d) { return d.value });
//Show the tooltip
d3.select("#tooltip").classed("hidden", false);
})
The original data
is an array. When you use .data
, .enter
and .each
, not only is a segment of the pie chart created, but each segment also has a single data point bound to it.
So, when you tell the segment to react to a mouseover event, the d
argument of the callback function will be set by d3 to the bound data point for that segment.
In contrast, in your code above, after selecting the #tooltip element, there's a call to .data(pie(data))
- which will bind the entire data set to the tooltip each time you mouseover - and is the cause of the unexpected behavior you observe.
Instead, use the single data item (d within the function here) and its value to set the text of the tooltip:
.on("mouseover", function(d) {
// d = the single data point used
// to create the segment here
var xPosition = 100;
var yPosition = 100;
d3.select("#tooltip")
.style("left", xPosition + "px")
.style("top", yPosition + "px")
.select("#NumberStudents")
.text(d.value);
d3.select("#tooltip").classed("hidden", false);
})
To access the other values (separate from the one used to create the pie slices), check the docs for pie layout. The original datum will be in the .data attribute of d. So, for example, assuming your tooltip has a #SchoolType
<span>
then you can set the text from the Type
attribute like:
.on("mouseover", function(d) {
var xPosition = 100;
var yPosition = 100;
d3.select("#tooltip")
.style("left", xPosition + "px")
.style("top", yPosition + "px")
.select("#NumberStudents")
.text(d.value)
//d.data will hold the original data map
d3.select("#tooltip #SchoolType").text(d.data["Type"]);
d3.select("#tooltip").classed("hidden", false);
})
Source: stackoverflow.com
Related Query
- How can a tooltip in D3 fetch data dynamically?
- How can I dynamically resize an SVG rect based on text width?
- Using d3.js, how can I display data faster on my chart?
- How can I get "call" to execute for each data element in D3?
- Using Javascript D3 library, how can I determine mouse position in data set of an area element on mousemove event?
- D3: How to dynamically refresh a graph by changing the data file source?
- How can I connect two parent node into one child node and how to make a tooltip for each node in the tree pragmatically? in D3 js (SVG)
- How can I add a click event to show a tooltip in an NVD3 Pie Chart?
- D3: How can I change data in an existing bar chart
- How can I style a tooltip for an NVD3 Chart?
- How can we create an onclick event for a d3 data map?
- How to map data such that I can use it to generate pie chart?
- Can D3.js fetch in data using "whitespace" separated values?
- In D3, how can I create multiple elements for each data element based on value in data?
- how to dynamically place lat, lon data onto a topoJSON map
- How can I efficiently convert data from string to int within a d3 method chain?
- how can i group JSON data into the weeks according to calender?
- How can I make a line between 2 elements dynamically (without knowing their coordinates)?
- How can I identify scatter plot data points that are included in a D3 brush?
- How to create a new div dynamically everytime a element in data is introduced
- how to fetch data from postgresql using d3.js
- How can I dynamically change the position and the size of direction arrows on a directed d3.js force layout?
- How to fetch data from json file to draw a dynamic graph by using d3.js
- How can I apply a time scale and show my data in intervals of "n" years?
- How can I append string to y-axis data with tick and d3.format?
- How can I dynamically update text labels in d3?
- How can I handle data filtering on a button click on D3.js?
- How can I use D3 tree data in the VEGA Api?
- How can I distinguish if a request came from a Fetch API call, in a Node.js / Express / D3.js app?
- How to add the data in the tooltip after the dataset switching
More Query from same tag
- R2D3's QUnit tests fail out of the box - what am I doing wrong?
- D3 line graph show positive and negative numbers
- How do I scale in d3.js
- Move the "Stacked/Grouped" selector on NVD3.js multiBarChart.js
- Wrong result in dimplejs scatterplot
- Why would a string of methods need to be separated?
- create fluid (scaling) concentric arcs using css, canvas, or svg
- d3 shrinking radar chart shape using attrTween
- Crosshair / x value tooltip for linear scale
- Tree diagram - prevent labels overlapping nodes
- D3js force layout update nodes and links
- Force a d3 line chart to ignore null values
- d3: how to focus separate tooltips for stacked area chart?
- Object Oriented d3
- Filter by Point in Polygon and Return County ID/Name
- Set the entry of a node in force layout
- How do I update my multiple line chart data in D3?
- Multiple lines for each point
- D3: Resize bar chart according to window width
- D3.js: Multiple line charts mouseover with same x (time) showing different y (price)
- Select existing circle node in d3 and append childs
- d3.js scatter plot - zoom/drag boundaries, zoom buttons, reset zoom, calculate median
- D3- positioning of a tooltip using d3.mouse is not working
- Dis Alignment of bar chart in D3.js
- D3 remove comma delimiters for thousands
- D3 X-Axis Doesn't Appear At Bottom
- d3.js how to dynamically add nodes to a tree #2
- D3 Adding hyperlinks to objects?
- d3 force map renders the links incorrectly after re-render
- Graph ~ axis alignment issue