@@ -21,13 +21,15 @@ var graphTooltip = d3.select("body").append("div")
21
21
. style ( "position" , "absolute" )
22
22
. style ( "z-index" , "10" )
23
23
. style ( "visibility" , "hidden" )
24
- . text ( "a simple tooltip " ) ;
24
+ . text ( "" ) ;
25
25
26
26
// Show "text" at location (x, y)
27
27
function showGraphTooltip ( text , x , y ) {
28
+ var left = x ;
29
+ var top = y ;
28
30
graphTooltip . style ( "visibility" , "visible" )
29
- . style ( "top" , x + "px" )
30
- . style ( "left" , y + "px" )
31
+ . style ( "top" , top + "px" )
32
+ . style ( "left" , left + "px" )
31
33
. text ( text ) ;
32
34
}
33
35
@@ -52,7 +54,14 @@ function drawTimeline(id, data, minX, maxX, minY, maxY, unitY) {
52
54
var y = d3 . scale . linear ( ) . domain ( [ minY , maxY ] ) . range ( [ height , 0 ] ) ;
53
55
54
56
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
+ } ) ;
56
65
var yAxis = d3 . svg . axis ( ) . scale ( y ) . orient ( "left" ) . ticks ( 5 ) ;
57
66
58
67
var line = d3 . svg . line ( )
@@ -77,6 +86,7 @@ function drawTimeline(id, data, minX, maxX, minY, maxY, unitY) {
77
86
. attr ( "class" , "y axis" )
78
87
. call ( yAxis )
79
88
. append ( "text" )
89
+ . attr ( "transform" , "translate(0," + ( - 3 ) + ")" )
80
90
. text ( unitY ) ;
81
91
82
92
svg . append ( "path" )
@@ -97,7 +107,7 @@ function drawTimeline(id, data, minX, maxX, minY, maxY, unitY) {
97
107
. attr ( "r" , function ( d ) { return 3 ; } )
98
108
. on ( 'mouseover' , function ( d ) {
99
109
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 ) ;
101
111
// show the point
102
112
d3 . select ( this )
103
113
. attr ( "stroke" , "steelblue" )
@@ -126,7 +136,8 @@ function drawDistribution(id, values, minY, maxY, unitY) {
126
136
var width = 300 - margin . left - margin . right ;
127
137
var height = 150 - margin . top - margin . bottom ;
128
138
129
- var binCount = values . length > 100 ? 100 : values . length ;
139
+ //var binCount = values.length > 100 ? 100 : values.length;
140
+ var binCount = 10 ;
130
141
var formatBinValue = d3 . format ( ",.2f" ) ;
131
142
132
143
var y = d3 . scale . linear ( ) . domain ( [ minY , maxY ] ) . range ( [ height , 0 ] ) ;
@@ -139,10 +150,6 @@ function drawDistribution(id, values, minY, maxY, unitY) {
139
150
var xAxis = d3 . svg . axis ( ) . scale ( x ) . orient ( "bottom" ) . ticks ( 5 ) ;
140
151
var yAxis = d3 . svg . axis ( ) . scale ( y ) . orient ( "left" ) . ticks ( 5 ) ;
141
152
142
- var line = d3 . svg . line ( )
143
- . x ( function ( d ) { return x ( d . y ) ; } )
144
- . y ( function ( d ) { return y ( d . x ) ; } ) ;
145
-
146
153
var svg = d3 . select ( id ) . append ( "svg" )
147
154
. attr ( "width" , width + margin . left + margin . right )
148
155
. attr ( "height" , height + margin . top + margin . bottom )
@@ -158,37 +165,35 @@ function drawDistribution(id, values, minY, maxY, unitY) {
158
165
. attr ( "class" , "y axis" )
159
166
. call ( yAxis )
160
167
. append ( "text" )
168
+ . attr ( "transform" , "translate(0," + ( - 3 ) + ")" )
161
169
. text ( unitY ) ;
162
170
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" )
171
172
. 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
+ } ) ;
194
199
}
0 commit comments