From 09f407f553eb8bca90f6da79cf60c742296488c7 Mon Sep 17 00:00:00 2001 From: Alanna Scott Date: Wed, 12 Apr 2017 13:24:07 -0700 Subject: [PATCH] add tooltips to big number vis (#2599) --- superset/assets/visualizations/big_number.css | 9 ++++ superset/assets/visualizations/big_number.js | 42 ++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/superset/assets/visualizations/big_number.css b/superset/assets/visualizations/big_number.css index e490395914b30..3e4734ece04a2 100644 --- a/superset/assets/visualizations/big_number.css +++ b/superset/assets/visualizations/big_number.css @@ -28,3 +28,12 @@ stroke: black; stroke-width: 1; } +.line-tooltip { + position: absolute; + text-align: left; + padding: 10px; + background: #ffffff; + border: 1px solid #ccc; + border-radius: 2px; + pointer-events: none; + } diff --git a/superset/assets/visualizations/big_number.js b/superset/assets/visualizations/big_number.js index af943a2facdb5..9089443d4aace 100644 --- a/superset/assets/visualizations/big_number.js +++ b/superset/assets/visualizations/big_number.js @@ -55,7 +55,7 @@ function bigNumberVis(slice, payload) { .y(function (d) { return scaleY(d[1]); }) - .interpolate('basis'); + .interpolate('cardinal'); let y = height / 2; let g = svg.append('g'); @@ -146,6 +146,46 @@ function bigNumberVis(slice, payload) { g.selectAll('text') .style('font-size', '10px'); + // Define the div for the tooltip + const tooltipEl = + d3.select('body') + .append('div') + .attr('class', 'line-tooltip') + .attr('width', 200) + .attr('height', 200) + .style('opacity', 0); + + const renderTooltip = (d) => { + const date = formatDate(d[0]); + const value = f(d[1]); + return ` +
+ ${date} + ${value} +
+ `; + }; + + // Add the scatterplot and trigger the mouse events for the tooltips + svg + .selectAll('dot') + .data(data) + .enter() + .append('circle') + .attr('r', 10) + .attr('cx', d => scaleX(d[0])) + .attr('cy', d => scaleY(d[1])) + .attr('fill-opacity', '0') + .on('mouseover', (d) => { + tooltipEl.html(renderTooltip(d)) + .style('left', (d3.event.pageX) + 'px') + .style('top', (d3.event.pageY) + 'px'); + tooltipEl.transition().duration(200).style('opacity', 0.9); + }) + .on('mouseout', () => { + tooltipEl.transition().duration(500).style('opacity', 0); + }); + div.on('mouseover', function () { const el = d3.select(this); el.selectAll('path')