Formatting
D3 comes with functions for formatting data. It can handle all types of values from numeric to time.
We'll cover the following...
Let’s update the tooltip with information about the dot the reader is hovering over. We will be working on the callback function we have written for the mouseenter
event. In our function, we are provided an argument called datum
. There are three things we need to grab. They are the date, humidity, and temperature. These properties are available in this argument.
Rendering the data
Let’s load these properties in the appropriate location in the tooltip. After moving the tooltip, we will use the tooltip
selection to select the respective locations.
.on('mouseenter', function(e, datum) {d3.select(this).attr('fill', '#120078').attr('r', 8)tooltip.style('display', 'block').style('top', yScale(yAccessor(datum)) - 25 + "px").style('left', xScale(xAccessor(datum)) + "px")tooltip.select(".metric-humidity span").text(xAccessor(datum))tooltip.select(".metric-temp span").text(yAccessor(datum))tooltip.select(".metric-date").text(datum.currently.time)})
For the humidity, we are using the .metric-humidity span
selector. The <span>
element is where we will insert the humidity. We are changing the inner text with the text()
function. We have an accessor function to help us access the correct property in our object. We can use the xAccessor
function with the datum
argument.
Next, we are updating the tooltip with the temperature. The selector for this part of the ...