score:1
Accepted answer
Create a
Add a
Append a
React to the
An updated jsfiddle is available here. Besides the line chart part, I did the following steps:
Create a scales
object to get one scale per chart:
var scales = {};
fields.forEach(function(f){
var currentData = test.filter(function(d){return d.name===f;})[0].data;
scales[f]= d3.scale.linear()
.domain([0, d3.max(currentData, function(d) {return d.value})])
.range([lineHeight, 0])
.nice();
});
Add a mousemove
to the dispatch:
var dispatch = d3.dispatch('hideTooltip', 'iTooltip', 'mousemove');
Append a text to each chart to display the tooltip:
alphaWrap.append('text')
.classed('tooltip', true)
.style('text-anchor', 'end')
.style('fill', function(d){return colorScale(d.name)});
Append a rect
on each chart to subscribe to the mousemove
event.
When the mouse moves, I get its position, compute the corresponding date, and dispatch it:
alphaWrap.append('rect')
.attr('x', 0)
.attr('y', 0)
.attr('width', xScale.range()[1])
.attr('height', lineHeight)
.style('pointer-events', 'all')
.style('fill', 'transparent')
.on('mousemove', function(d,i){
var mouse=d3.mouse(this);
var date = xScale.invert(mouse[0]);
dispatch.mousemove(date);
});
Append a line over all 3 charts:
d3.select('#chart-container')
.append('line')
.classed('tooltip', 'true')
.style('stroke', 'darkgray')
.attr('x1',90)
.attr('x2',90)
.attr('y1',0)
.attr('y2',fields.length*plotHeight);
React to the dispatch
mousemove
event
I created a bisector
to find the index of the closest date in the array of data for the chart.
Thanks the date and, the bisector, the line can be moved appropriately, and the value of the tooltip can be displayed
var bisector = d3.bisector(function(d){return d.date}).right;
dispatch.on('mousemove', function(date){
d3.select('line.tooltip')
.attr('transform', 'translate('+xScale(date)+',0)')
alphaWrap.select('text.tooltip')
.attr('transform', function(d,i){
var value = bisector(d.data, date);
return 'translate('+xScale(date)+','+scales[d.name](d.data[value].value)+')';
})
.text(function(d,i){
var value = bisector(d.data, date);
return d3.format('2.2f')(d.data[value].value);
});
});
Source: stackoverflow.com
Related Query
- Change bar chart to line chart in D3
- D3js - change Vertical bar chart to Horizontal bar chart
- D3.js combining bar and line chart
- nvd3.js - unable to change color of line in line chart
- How to add a line on x-axis on a horizontal bar chart in d3
- dc-js line chart showing extra filled line at average change
- D3: How can I change data in an existing bar chart
- How to change the viewfinder (focus chart) of a NVD3 line chart programmatically?
- NVD3 Stacked Bar Chart Plus Line Overlapped
- d3 change bar chart color according to height
- How to line up x axis on stacked bar chart with a line overlay in d3.js
- How to change color of line in line chart using dimple.js?
- d3 bar chart with fixed bar width and fixed spacing to align in center axis line
- C3.js change label position of Line Chart Y axis to center up of Y axis
- How to create a % difference arrow line with value in a bar chart using D3.js
- Adding hard coded fixed line to d3 bar chart
- d3.js stacked bar chart sort / change order of individual values within a stack
- D3.js: Dynamically change the bar chart
- NVD3 Line Plus Bar With Focus Chart only displaying half width of first and last bar
- NVD3 Line Plus Bar Chart timeline
- Why does the NVD3.js line plus bar chart example render two charts instead of one?
- Add vertical line to d3js bar chart
- Angular-NVD3 Stacked Bar Chart With Line
- how i can put more lines in Line Plus Bar Chart using nvd3.js?
- Average line on a bar chart
- Why does my bar chart using scaleBand not line up with my tick marks?
- How to change color of a data point in line chart according to a condition?
- dimple js straight line y-axis over bar chart
- How to change pattern of one bar of a bar chart in d3.js?
- How can I make the legend's similar to the stacked bar chart (and change the legend's label) in D3 v6?
More Query from same tag
- Skipping overlapping labels on x-axis for a barchart in dc.js
- nvd3 pie outerRadius
- d3 sunburst hardcoded data
- ds3 drawing json data from mysql
- Three.js ugly rendering effect
- D3 line .defined() with different array
- Explain d3js projection fundamentals
- show text with diagonal line in d3 tree graph
- How can I solve Error: Invalid value for <text> attribute transform="translate(NaN,NaN)" on arc.centroid(d)?
- convert a flat json file to tree structure in javascript
- SVG manipulate gradiant fill color
- Coordinating highlighting between two tables
- Node Transition does not occur with D3.js V4 and Angular
- how to access data from nested json array D3
- How to call d3.json() outside of d3
- d3 draw animated line graph basic example
- Dynamically changing bins and height of a d3.js histogram
- Using two selectAll on one axis
- Adding some content to a <text> tag through JS
- Making DOM Reflow/Refresh
- D3 - drawing a line between two object not intersecting other objects
- Getting the centroid of each polygon from a GeoJson multipolygon in d3.js
- d3 csv parser on progress value
- d3.js - tooltips not following mouse position
- d3.js error after installing c3.js in angular2
- How do I import d3 (v4) into typescript in an ASP.NET MVC Application?
- Conditionally build group in d3
- previously logged d3 object doesn't show its __data__ property after removing __data__ with datum(num)
- D3 Issue while Creating a Table
- How can I make rects from JSON?