score:0
Accepted answer
I have just made a few changes to the mousemove
event.
var margin = {
top: 50,
right: 50,
bottom: 50,
left: 50
},
width = window.innerWidth - margin.left - margin.right,
height = window.innerHeight - margin.top - margin.bottom;
//labels
var labels = ['Mon', 'Tue', 'Thur', 'Frid'];
var yvals = [12, 11, 0, 18];
// X scale
var xScale = d3.scalePoint()
.domain(labels) // input
.range([0, width - 1]); // output
// Y scale
var yScale = d3.scaleLinear()
.domain([0, 20])
.range([height, 0]);
var line = d3.line()
.x(function(d, i) {
return xScale(labels[i]);
})
.y(function(d) {
return yScale(d.y);
})
.curve(d3.curveMonotoneX)
var dataset = d3.range(yvals.length).map(function(d, i) {
return {
"y": yvals[i]
}
})
var tip = d3.select('body').append("div")
.attr("class", "tip");
// SVGs
var svg = d3.select("body").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 + ")");
svg.append("rect")
.attr("width", "100%")
.attr("height", "100%")
.attr("fill", "white");
svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// x axis call
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
//.call(d3.axisBottom(xScale));
.call(d3.axisBottom(xScale));
// y axis call
svg.append("g")
.attr("class", "y axis")
.call(d3.axisLeft(yScale));
svg.append("path")
.datum(dataset)
.attr("class", "line")
.attr("d", line);
// 12. Appends a circle for each datapoint
svg.selectAll(".dot")
.data(dataset)
.enter().append("circle") // Uses the enter().append() method
.attr("class", "dot") // Assign a class for styling
.attr("cx", function(d, i) {
return xScale(labels[i])
})
.attr("cy", function(d, i) {
return yScale(yvals[i])
})
.attr("r", 3)
.on("mouseover", function() {
tip.style("display", null);
})
.on("mouseout", function() {
tip.style("display", "none");
})
.on("mousemove", function(d) {
return tip
.style("left", d3.event.pageX + "px")
.style("top", d3.event.pageY + 10 + "px")
.style("visibility", "visible")
.html(function() {
return '<div style="border:1px solid #ccc;">' +
'<p style="font-weight:bold;">' + d.y + '</p>' +
'</div>';
})
})
svg.append("text")
.attr("class", "title")
.attr("x", width / 2)
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.text("Testing");
.line {
fill: none;
stroke: orange;
stroke-width: 1;
}
.dot {
fill: brown;
stroke: #fff;
}
.tip {
position: absolute;
border: 1px solid steelblue;
visibility: hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.0.0/d3.min.js"></script>
Here is the working jsFiddle
Hope it helps :)
Source: stackoverflow.com
Related Query
- Tooltip not working nor displaying on D3 scatter plot
- D3 scatter plot points not displaying correctly
- Y Axis not displaying for scatter plot with d3 v4
- My d3 Scatter plot is not displaying anything at all
- D3 - Positioning tooltip on SVG element not working
- d3.js Tooltip positioning not working
- Using Bootstrap Tooltip with SVG Object - Not Displaying Despite specifying Container
- Tooltip to display in two separate lines not working in Firefox
- Display tooltip close to the mouse pointer using d3.js in a scatter plot
- Add tooltip to d3 scatter plot
- Tooltips not working on the initial plot but start working once the plot changes
- mouse over the center text in the doughnut chart is not displaying the tooltip
- D3 multi-line plot is displaying 2 lines not 3
- D3 tooltip not displaying in Firefox
- DC.JS Scatter Plot not filtering
- d3 transition on tooltip not working
- D3- positioning of a tooltip using d3.mouse is not working
- Tooltip in d3/topojson choropleth map not working
- D3 Scatter Plot Not Implementing Zoom Interaction Properly
- Browser tooltip not working in d3.js bar chart
- nvd3 scatter chart circle size not working
- D3.js Tooltip not displaying on mouseover (or at all)
- Tooltip is not displaying despite data being present
- Tooltip not displaying on top of the element using d3.js on Mouseover
- x position of tooltip in d3 stacked bar chart not working
- D3 Arc Diagram: Tooltip on Circle Mouseover Not Displaying
- jquery tipsy tooltip not working with d3.js circles
- Not able add zoom/pan on scatter plot
- d3.js plot not displaying within R Shiny plot area
- Scattered plot zooming not working correctly in d3.js
More Query from same tag
- How to clear the canvas without interrupting animations?
- Creating a BarChart with scala.js
- D3 Javascript -- Example SVG is declared in HTML cannot embed with HTML Tags
- How do I get a tooltip on hovering over an SVG circle
- Hierarchical edge bundling 2 way - link colour
- d3 axis orientation: center
- Text being covered by paths in d3 pie chart
- Tweening text in d3.js - count up
- d3: How to get stacked graph to zoom?
- Click event in not working on D3 US Map, and mouseover event is working on particular states only
- D3: order of selections: style and text
- How to remove spaces between bars in grouped bar charts in d3
- d3.select(#elementId) doesn't find a element
- D3js v5 grouped bar chart data x.domain labels and data separated
- How to move a node along a vector?
- D3 hide overflow of the chart area, when Y axis has a min and max
- Visualizing dataset into d3 contour by converting obervable to notebook to plain JS
- How to have smooth (horizontal) transition on a c3js dynamic chart
- Aggregating Categorical Variables w/ Dimple.js
- Dimple.js scatter plot not drawing correctly in Firefox v. 38.0.5
- Highlighting lines on a random walk plot mpld3
- d3.select do not append
- D3 Pie chart not updating label on center
- Wrong number of lines/ticks on d3 bar chart
- Changes to forceSimulation disable drag
- d3: us states in topojson format filesize is way too large
- D3js Updating Histogram elements not working (General Update Pattern)
- Custom multi-scale time formats in d3
- D3 force simulation in Angular
- Original Data Display on chart after filtering (DC.js)