11import angular from 'angular'
22import d3 from 'd3'
3+ import React from 'react' // eslint-disable-line no-unused-vars
4+ import ReactDOM from 'react-dom'
5+ import Tooltip from 'appirio-tech-react-components/components/Tooltip/Tooltip.jsx'
36
47( function ( ) {
58 'use strict'
@@ -59,7 +62,7 @@ import d3 from 'd3'
5962 ]
6063
6164 var measurements = {
62- w : 600 ,
65+ w : 900 ,
6366 h : 400 ,
6467 padding : {
6568 top : 20 ,
@@ -176,7 +179,24 @@ import d3 from 'd3'
176179 . attr ( 'fill' , function ( d ) {
177180 return ratingToColor ( $scope . colors , d . start )
178181 } )
179-
182+
183+ var mousemoveInterval = null
184+
185+ /* render react tooltip component */
186+ ReactDOM . unmountComponentAtNode ( document . getElementById ( 'chart-tooltip' ) )
187+ ReactDOM . render ( < Tooltip popMethod = 'click' >
188+ < div className = 'tooltip-target' > </ div >
189+ < div className = 'tooltip-body' >
190+ < div className = 'tooltip-rating' > </ div >
191+ < div className = 'tooltip-challenge' >
192+ < div className = 'challenge-name' > </ div >
193+ < div className = 'challenge-date' > </ div >
194+ </ div >
195+ </ div >
196+ </ Tooltip >
197+ , document . getElementById ( 'chart-tooltip' ) )
198+
199+ $scope . isFocused = false
180200 svg . selectAll ( 'rect.hover' )
181201 . data ( ranges )
182202 . enter ( )
@@ -187,24 +207,81 @@ import d3 from 'd3'
187207 return xScale ( i )
188208 } )
189209 . attr ( 'y' , function ( d ) {
190- return padding . top
210+ return yScale ( d . number )
191211 } )
192212 . attr ( 'width' , xScale . rangeBand ( ) )
193213 . attr ( 'height' , function ( d ) {
194- return totalH - padding . bottom - padding . top
214+ return totalH - padding . bottom - yScale ( d . number )
195215 } )
196216 . on ( 'mouseover' , function ( d ) {
217+ $scope . isFocused = true
197218 $scope . highlightedRating = d . start
198219 $scope . displayCoders = true
199220 $scope . numCoders = d . number
221+
222+ /* update tooltip location on mouseover, feature currently not inbuilt in react tooltip component */
223+ d3 . select ( '#chart-tooltip' )
224+ . style ( 'left' , ( d3 . event . pageX - 4 ) + 'px' )
225+ . style ( 'top' , ( d3 . event . pageY - 4 ) + 'px' )
226+
227+ $ ( '#chart-tooltip' ) . addClass ( 'distribution' )
228+ d3 . select ( '#chart-tooltip .tooltip-container' )
229+ . style ( 'left' , '20px !important' )
230+ . style ( 'top' , '-20px !important' )
231+
232+ d3 . select ( '#chart-tooltip .tooltip-container .tooltip-pointer' )
233+ . style ( 'left' , '-5.5px !important' )
234+ . style ( 'bottom' , '25px !important' )
235+
236+ d3 . select ( '#chart-tooltip .challenge-name' ) . text ( $scope . numCoders + ' Coders' )
237+ d3 . select ( '#chart-tooltip .challenge-date' ) . text ( 'Rating Range: ' + $scope . highlightedRating + '-' + ( $scope . highlightedRating + 99 ) )
238+ d3 . select ( '#chart-tooltip .tooltip-rating' ) . text ( $scope . numCoders )
239+ d3 . select ( '#chart-tooltip .tooltip-rating' ) . style ( 'background' , ratingToColor ( $scope . colors , $scope . highlightedRating ) )
200240 $scope . $digest ( )
201241 } )
242+ . on ( 'mousemove' , function ( d ) {
243+
244+ /* update tooltip on mousemove, using interval of 50ms to improve performance */
245+ window . clearTimeout ( mousemoveInterval )
246+ var left = ( d3 . event . pageX - 4 )
247+ var top = ( d3 . event . pageY - 4 )
248+
249+ mousemoveInterval = window . setTimeout ( function ( ) {
250+ d3 . select ( '#chart-tooltip' )
251+ . style ( 'left' , left + 'px' )
252+ . style ( 'top' , top + 'px' )
253+
254+ d3 . select ( '#chart-tooltip .tooltip-container' )
255+ . style ( 'left' , '20px !important' )
256+ . style ( 'top' , '-20px !important' )
257+
258+ d3 . select ( '#chart-tooltip .tooltip-container .tooltip-pointer' )
259+ . style ( 'left' , '-5.5px !important' )
260+ . style ( 'bottom' , '25px !important' )
261+ } , 50 )
262+
263+ } )
202264 . on ( 'mouseout' , function ( d ) {
203265 $scope . displayCoders = false
204266 $scope . highlightedRating = false
267+ $scope . isFocused = false
205268 $scope . $digest ( )
206269 } )
207-
270+
271+ /* hide tooltip when clicked anywhere outside */
272+ d3 . select ( 'body' ) . on ( 'click' , function ( ) {
273+ if ( ( d3 . event . target . classList [ 0 ] != 'tooltip-target' ) && ! $ ( '#chart-tooltip .tooltip-container' ) . hasClass ( 'tooltip-hide' ) &&
274+ ! isInArray ( d3 . event . target . classList [ 0 ] , [ 'tooltip-content-container' , 'tooltip-container' , 'tooltip-body' , 'Tooltip' ] ) &&
275+ ( d3 . event . target . tagName . toLowerCase ( ) != 'circle' ) && ! ( d3 . event . target . tagName . toLowerCase ( ) == 'rect' && d3 . event . target . classList [ 0 ] == 'hover' ) ) {
276+ $ ( '#chart-tooltip .tooltip-target' ) . trigger ( 'click' )
277+ $ ( '#chart-tooltip' ) . removeClass ( 'distribution' )
278+ }
279+ } )
280+
281+ function isInArray ( value , array ) {
282+ return array . indexOf ( value ) > - 1
283+ }
284+
208285 svg . selectAll ( 'line.xaxis' )
209286 . data ( ranges )
210287 . enter ( )
0 commit comments