@@ -21,13 +21,15 @@ var graphTooltip = d3.select("body").append("div")
2121 . style ( "position" , "absolute" )
2222 . style ( "z-index" , "10" )
2323 . style ( "visibility" , "hidden" )
24- . text ( "a simple tooltip " ) ;
24+ . text ( "" ) ;
2525
2626// Show "text" at location (x, y)
2727function showGraphTooltip ( text , x , y ) {
28+ var left = x ;
29+ var top = y ;
2830 graphTooltip . style ( "visibility" , "visible" )
29- . style ( "top" , x + "px" )
30- . style ( "left" , y + "px" )
31+ . style ( "top" , top + "px" )
32+ . style ( "left" , left + "px" )
3133 . text ( text ) ;
3234}
3335
@@ -52,7 +54,14 @@ function drawTimeline(id, data, minX, maxX, minY, maxY, unitY) {
5254 var y = d3 . scale . linear ( ) . domain ( [ minY , maxY ] ) . range ( [ height , 0 ] ) ;
5355
5456 var timeFormat = d3 . time . format ( "%H:%M:%S" )
55- var xAxis = d3 . svg . axis ( ) . scale ( x ) . orient ( "bottom" ) . tickFormat ( timeFormat ) ;
57+ var xAxis = d3 . svg . axis ( ) . scale ( x ) . orient ( "bottom" ) . ticks ( 10 )
58+ . tickFormat ( function ( d ) {
59+ if ( d . getTime ( ) == minX || d . getTime ( ) == maxX ) {
60+ return timeFormat ( d ) ;
61+ } else {
62+ return "x" ;
63+ }
64+ } ) ;
5665 var yAxis = d3 . svg . axis ( ) . scale ( y ) . orient ( "left" ) . ticks ( 5 ) ;
5766
5867 var line = d3 . svg . line ( )
@@ -77,6 +86,7 @@ function drawTimeline(id, data, minX, maxX, minY, maxY, unitY) {
7786 . attr ( "class" , "y axis" )
7887 . call ( yAxis )
7988 . append ( "text" )
89+ . attr ( "transform" , "translate(0," + ( - 3 ) + ")" )
8090 . text ( unitY ) ;
8191
8292 svg . append ( "path" )
@@ -97,7 +107,7 @@ function drawTimeline(id, data, minX, maxX, minY, maxY, unitY) {
97107 . attr ( "r" , function ( d ) { return 3 ; } )
98108 . on ( 'mouseover' , function ( d ) {
99109 var tip = d . y + " " + unitY + " at " + timeFormat ( new Date ( d . x ) ) ;
100- showGraphTooltip ( tip , event . pageY - 25 , event . pageX ) ;
110+ showGraphTooltip ( tip , d3 . event . pageX + 5 , d3 . event . pageY - 25 ) ;
101111 // show the point
102112 d3 . select ( this )
103113 . attr ( "stroke" , "steelblue" )
@@ -126,7 +136,8 @@ function drawDistribution(id, values, minY, maxY, unitY) {
126136 var width = 300 - margin . left - margin . right ;
127137 var height = 150 - margin . top - margin . bottom ;
128138
129- var binCount = values . length > 100 ? 100 : values . length ;
139+ //var binCount = values.length > 100 ? 100 : values.length;
140+ var binCount = 10 ;
130141 var formatBinValue = d3 . format ( ",.2f" ) ;
131142
132143 var y = d3 . scale . linear ( ) . domain ( [ minY , maxY ] ) . range ( [ height , 0 ] ) ;
@@ -139,10 +150,6 @@ function drawDistribution(id, values, minY, maxY, unitY) {
139150 var xAxis = d3 . svg . axis ( ) . scale ( x ) . orient ( "bottom" ) . ticks ( 5 ) ;
140151 var yAxis = d3 . svg . axis ( ) . scale ( y ) . orient ( "left" ) . ticks ( 5 ) ;
141152
142- var line = d3 . svg . line ( )
143- . x ( function ( d ) { return x ( d . y ) ; } )
144- . y ( function ( d ) { return y ( d . x ) ; } ) ;
145-
146153 var svg = d3 . select ( id ) . append ( "svg" )
147154 . attr ( "width" , width + margin . left + margin . right )
148155 . attr ( "height" , height + margin . top + margin . bottom )
@@ -158,37 +165,35 @@ function drawDistribution(id, values, minY, maxY, unitY) {
158165 . attr ( "class" , "y axis" )
159166 . call ( yAxis )
160167 . append ( "text" )
168+ . attr ( "transform" , "translate(0," + ( - 3 ) + ")" )
161169 . text ( unitY ) ;
162170
163- svg . append ( "path" )
164- . datum ( data )
165- . attr ( "class" , "line" )
166- . attr ( "d" , line ) ;
167-
168- // Add points to the line. However, we make it invisible at first. But when the user moves mouse
169- // over a point, it will be displayed with its detail.
170- svg . selectAll ( ".point" )
171+ var bar = svg . selectAll ( ".bar" )
171172 . data ( data )
172- . enter ( ) . append ( "circle" )
173- . attr ( "stroke" , "white" ) // white and opacity = 0 make it invisible
174- . attr ( "fill" , "white" )
175- . attr ( "opacity" , "0" )
176- . attr ( "cx" , function ( d ) { return x ( d . y ) ; } )
177- . attr ( "cy" , function ( d ) { return y ( d . x ) ; } )
178- . attr ( "r" , function ( d ) { return 3 ; } )
179- . on ( 'mouseover' , function ( d ) {
180- var tip = "[" + formatBinValue ( d . x ) + ", " + formatBinValue ( d . x + d . dx ) + "): " + d . y ;
181- showGraphTooltip ( tip , event . pageY , event . pageX + 10 ) ;
182- d3 . select ( this )
183- . attr ( "stroke" , "steelblue" )
184- . attr ( "fill" , "steelblue" )
185- . attr ( "opacity" , "1" ) ;
186- } )
187- . on ( 'mouseout' , function ( ) {
188- hideGraphTooltip ( ) ;
189- d3 . select ( this )
190- . attr ( "stroke" , "white" )
191- . attr ( "fill" , "white" )
192- . attr ( "opacity" , "0" ) ;
193- } ) ;
173+ . enter ( ) . append ( "g" )
174+ . attr ( "transform" , function ( d ) { return "translate(0," + ( y ( d . x ) - height + y ( d . dx ) ) + ")" ; } )
175+ . attr ( "class" , "bar" ) . append ( "rect" )
176+ . attr ( "width" , function ( d ) { return x ( d . y ) ; } )
177+ . attr ( "height" , function ( d ) { return height - y ( d . dx ) ; } )
178+ . on ( 'mouseover' , function ( d ) {
179+ var tip = "[" + formatBinValue ( d . x ) + ", " + formatBinValue ( d . x + d . dx ) + "): " + d . y ;
180+
181+ // Calculate the location for tip
182+ var scrollTop = document . documentElement . scrollTop || document . body . scrollTop ;
183+ var scrollLeft = document . documentElement . scrollLeft || document . body . scrollLeft ;
184+ var target = d3 . event . target ;
185+ var matrix = target . getScreenCTM ( ) ;
186+ var targetBBox = target . getBBox ( ) ;
187+ var point = svg . node ( ) . ownerSVGElement . createSVGPoint ( ) ;
188+ point . x = targetBBox . x ;
189+ point . y = targetBBox . y ;
190+ var bbox = point . matrixTransform ( matrix ) ;
191+ var tipX = bbox . x + scrollLeft + x ( d . y ) + 2 ;
192+ var tipY = bbox . y + scrollTop - 6 ;
193+
194+ showGraphTooltip ( tip , tipX , tipY ) ;
195+ } )
196+ . on ( 'mouseout' , function ( ) {
197+ hideGraphTooltip ( ) ;
198+ } ) ;
194199}
0 commit comments