@@ -422,7 +422,7 @@ module.exports = __webpack_require__(10);
422
422
Object . defineProperty ( exports , "__esModule" , {
423
423
value : true
424
424
} ) ;
425
- exports . SparklinesText = exports . SparklinesNormalBand = exports . SparklinesReferenceLine = exports . SparklinesSpots = exports . SparklinesBars = exports . SparklinesCurve = exports . SparklinesLine = exports . Sparklines = undefined ;
425
+ exports . SparklinesInteractiveLayer = exports . SparklinesText = exports . SparklinesNormalBand = exports . SparklinesReferenceLine = exports . SparklinesSpots = exports . SparklinesBars = exports . SparklinesCurve = exports . SparklinesLine = exports . Sparklines = undefined ;
426
426
427
427
var _createClass = function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ( ) ;
428
428
@@ -501,8 +501,7 @@ var Sparklines = function (_PureComponent) {
501
501
margin = _props . margin ,
502
502
style = _props . style ,
503
503
max = _props . max ,
504
- min = _props . min ,
505
- interactive = _props . interactive ;
504
+ min = _props . min ;
506
505
507
506
508
507
if ( data . length === 0 ) return null ;
@@ -518,8 +517,7 @@ var Sparklines = function (_PureComponent) {
518
517
svgOpts ,
519
518
_react2 . default . Children . map ( this . props . children , function ( child ) {
520
519
return _react2 . default . cloneElement ( child , { data : data , points : points , width : width , height : height , margin : margin } ) ;
521
- } ) ,
522
- interactive ? _react2 . default . createElement ( _SparklinesInteractiveLayer2 . default , { width : width , height : height , points : points } ) : null
520
+ } )
523
521
) ;
524
522
}
525
523
} ] ) ;
@@ -547,8 +545,7 @@ Sparklines.defaultProps = {
547
545
height : 60 ,
548
546
//Scale the graphic content of the given element non-uniformly if necessary such that the element's bounding box exactly matches the viewport rectangle.
549
547
preserveAspectRatio : 'none' , //https://www.w3.org/TR/SVG/coords.html#PreserveAspectRatioAttribute
550
- margin : 2 ,
551
- interactive : false
548
+ margin : 2
552
549
} ;
553
550
exports . Sparklines = Sparklines ;
554
551
exports . SparklinesLine = _SparklinesLine2 . default ;
@@ -558,6 +555,7 @@ exports.SparklinesSpots = _SparklinesSpots2.default;
558
555
exports . SparklinesReferenceLine = _SparklinesReferenceLine2 . default ;
559
556
exports . SparklinesNormalBand = _SparklinesNormalBand2 . default ;
560
557
exports . SparklinesText = _SparklinesText2 . default ;
558
+ exports . SparklinesInteractiveLayer = _SparklinesInteractiveLayer2 . default ;
561
559
562
560
/***/ } ) ,
563
561
/* 11 */
@@ -2219,6 +2217,8 @@ Object.defineProperty(exports, "__esModule", {
2219
2217
value : true
2220
2218
} ) ;
2221
2219
2220
+ var _extends = Object . assign || function ( target ) { for ( var i = 1 ; i < arguments . length ; i ++ ) { var source = arguments [ i ] ; for ( var key in source ) { if ( Object . prototype . hasOwnProperty . call ( source , key ) ) { target [ key ] = source [ key ] ; } } } return target ; } ;
2221
+
2222
2222
var _createClass = function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ( ) ;
2223
2223
2224
2224
var _propTypes = __webpack_require__ ( 0 ) ;
@@ -2237,22 +2237,71 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen
2237
2237
2238
2238
function _inherits ( subClass , superClass ) { if ( typeof superClass !== "function" && superClass !== null ) { throw new TypeError ( "Super expression must either be null or a function, not " + typeof superClass ) ; } subClass . prototype = Object . create ( superClass && superClass . prototype , { constructor : { value : subClass , enumerable : false , writable : true , configurable : true } } ) ; if ( superClass ) Object . setPrototypeOf ? Object . setPrototypeOf ( subClass , superClass ) : subClass . __proto__ = superClass ; }
2239
2239
2240
- var SparklinesInteractiveLayer = function ( _PureComponent ) {
2241
- _inherits ( SparklinesInteractiveLayer , _PureComponent ) ;
2240
+ var Spot = function ( _PureComponent ) {
2241
+ _inherits ( Spot , _PureComponent ) ;
2242
+
2243
+ function Spot ( ) {
2244
+ _classCallCheck ( this , Spot ) ;
2245
+
2246
+ return _possibleConstructorReturn ( this , ( Spot . __proto__ || Object . getPrototypeOf ( Spot ) ) . apply ( this , arguments ) ) ;
2247
+ }
2248
+
2249
+ _createClass ( Spot , [ {
2250
+ key : 'render' ,
2251
+ value : function render ( ) {
2252
+ return _react2 . default . createElement ( 'circle' , { cx : this . props . cx , cy : this . props . cy , r : 2 } ) ;
2253
+ }
2254
+ } ] ) ;
2255
+
2256
+ return Spot ;
2257
+ } ( _react . PureComponent ) ;
2258
+
2259
+ var Cursor = function ( _PureComponent2 ) {
2260
+ _inherits ( Cursor , _PureComponent2 ) ;
2261
+
2262
+ function Cursor ( ) {
2263
+ _classCallCheck ( this , Cursor ) ;
2264
+
2265
+ return _possibleConstructorReturn ( this , ( Cursor . __proto__ || Object . getPrototypeOf ( Cursor ) ) . apply ( this , arguments ) ) ;
2266
+ }
2267
+
2268
+ _createClass ( Cursor , [ {
2269
+ key : 'render' ,
2270
+ value : function render ( ) {
2271
+ return _react2 . default . createElement ( 'line' , { x1 : this . props . x1 , x2 : this . props . x2 , y1 : 0 , y2 : this . props . height , style : { strokeWidth : 1 , stroke : "red" } } ) ;
2272
+ }
2273
+ } ] ) ;
2274
+
2275
+ return Cursor ;
2276
+ } ( _react . PureComponent ) ;
2277
+
2278
+ var offscreen = - 1000 ;
2279
+
2280
+ var SparklinesInteractiveLayer = function ( _PureComponent3 ) {
2281
+ _inherits ( SparklinesInteractiveLayer , _PureComponent3 ) ;
2242
2282
2243
2283
function SparklinesInteractiveLayer ( props ) {
2244
2284
_classCallCheck ( this , SparklinesInteractiveLayer ) ;
2245
2285
2246
- var _this = _possibleConstructorReturn ( this , ( SparklinesInteractiveLayer . __proto__ || Object . getPrototypeOf ( SparklinesInteractiveLayer ) ) . call ( this , props ) ) ;
2286
+ var _this3 = _possibleConstructorReturn ( this , ( SparklinesInteractiveLayer . __proto__ || Object . getPrototypeOf ( SparklinesInteractiveLayer ) ) . call ( this , props ) ) ;
2247
2287
2248
- _this . onMouseMove = function ( datapoints ) {
2288
+ _this3 . onMouseMove = function ( datapoints , width ) {
2249
2289
var lastItemIndex = datapoints [ datapoints . length - 1 ] ;
2250
2290
return function ( event ) {
2251
- var mouseX = event . nativeEvent . offsetX ;
2252
- console . log ( 'movementX: ' , event . nativeEvent . movementX ) ;
2291
+ if ( _this3 . state . isActive ) {
2292
+ return ;
2293
+ }
2253
2294
2295
+ var mouseX = Math . floor ( event . nativeEvent . offsetX / ( _this3 . rectWidth / width ) ) ;
2296
+
2297
+ var pointIndex = 0 ;
2254
2298
var nextDataPoint = datapoints . find ( function ( entry ) {
2255
- return entry . x >= mouseX ;
2299
+ var match = entry . x >= mouseX ;
2300
+ if ( match && ! pointIndex ) {
2301
+ return match ;
2302
+ }
2303
+ pointIndex = pointIndex + 1 ;
2304
+ return match ;
2256
2305
} ) ;
2257
2306
2258
2307
if ( ! nextDataPoint ) {
@@ -2275,36 +2324,68 @@ var SparklinesInteractiveLayer = function (_PureComponent) {
2275
2324
var x = currentDataPoint . x ;
2276
2325
var y = currentDataPoint . y ;
2277
2326
2278
- console . log ( 'x:' , x , 'y: ' , y ) ;
2327
+ _this3 . setState ( { cx : x , cy : y } ) ;
2328
+ _this3 . props . onMouseMove ( currentDataPoint , Math . max ( 0 , pointIndex - 1 ) , event . nativeEvent . offsetX , event . nativeEvent . offsetY ) ;
2329
+ } ;
2330
+ } ;
2279
2331
2280
- // spot.setAttribute("cx", x);
2281
- // spot.setAttribute("cy", y);
2332
+ _this3 . onMouseLeave = function ( ) {
2333
+ if ( _this3 . state . isActive ) {
2334
+ return ;
2335
+ }
2282
2336
2283
- // cursor.setAttribute("x1", x);
2284
- // cursor.setAttribute("x2", x);
2285
- } ;
2337
+ _this3 . setState ( { cx : offscreen , cy : offscreen } ) ;
2338
+ _this3 . props . onMouseLeave ( ) ;
2339
+ } ;
2340
+
2341
+ _this3 . onClick = function ( ) {
2342
+ _this3 . setState ( function ( prevState ) {
2343
+ return { isActive : ! prevState . isActive } ;
2344
+ } ) ;
2286
2345
} ;
2287
2346
2288
- return _this ;
2347
+ _this3 . state = {
2348
+ cx : offscreen ,
2349
+ cy : offscreen ,
2350
+ isActive : false
2351
+ } ;
2352
+ return _this3 ;
2289
2353
}
2290
2354
2291
2355
_createClass ( SparklinesInteractiveLayer , [ {
2356
+ key : 'componentDidMount' ,
2357
+ value : function componentDidMount ( ) {
2358
+ this . rectWidth = this . rect . getBoundingClientRect ( ) . width ;
2359
+ }
2360
+ } , {
2292
2361
key : 'render' ,
2293
2362
value : function render ( ) {
2363
+ var _this4 = this ;
2364
+
2294
2365
var _props = this . props ,
2295
2366
height = _props . height ,
2296
2367
width = _props . width ,
2297
- points = _props . points ;
2368
+ points = _props . points ,
2369
+ style = _props . style ;
2370
+ var _state = this . state ,
2371
+ cx = _state . cx ,
2372
+ cy = _state . cy ;
2298
2373
2299
- console . log ( 'points :' , points ) ;
2300
2374
return _react2 . default . createElement (
2301
- 'span' ,
2375
+ _react2 . default . Fragment ,
2302
2376
null ,
2377
+ _react2 . default . createElement ( Spot , { cx : cx , cy : cy } ) ,
2378
+ _react2 . default . createElement ( Cursor , { x1 : cx , x2 : cx , height : height } ) ,
2303
2379
_react2 . default . createElement ( 'rect' , {
2380
+ ref : function ref ( _ref ) {
2381
+ _this4 . rect = _ref ;
2382
+ } ,
2304
2383
height : height ,
2305
2384
width : width ,
2306
- style : { fill : "transparent" , stroke : "transparent" } ,
2307
- onMouseMove : this . onMouseMove ( points )
2385
+ style : _extends ( { fill : 'transparent' , stroke : 'transparent' } , style ) ,
2386
+ onMouseMove : this . onMouseMove ( points , width ) ,
2387
+ onMouseLeave : this . onMouseLeave ,
2388
+ onClick : this . onClick
2308
2389
} )
2309
2390
) ;
2310
2391
}
@@ -2313,6 +2394,19 @@ var SparklinesInteractiveLayer = function (_PureComponent) {
2313
2394
return SparklinesInteractiveLayer ;
2314
2395
} ( _react . PureComponent ) ;
2315
2396
2397
+ SparklinesInteractiveLayer . propTypes = {
2398
+ points : _propTypes2 . default . arrayOf ( _propTypes2 . default . object ) ,
2399
+ height : _propTypes2 . default . number ,
2400
+ width : _propTypes2 . default . number ,
2401
+ onMouseMove : _propTypes2 . default . func ,
2402
+ onMouseLeave : _propTypes2 . default . func ,
2403
+ onClick : _propTypes2 . default . func
2404
+ } ;
2405
+ SparklinesInteractiveLayer . defaultProps = {
2406
+ onMouseMove : function onMouseMove ( ) { } ,
2407
+ onMouseLeave : function onMouseLeave ( ) { } ,
2408
+ onClick : function onClick ( ) { }
2409
+ } ;
2316
2410
exports . default = SparklinesInteractiveLayer ;
2317
2411
2318
2412
/***/ } )
0 commit comments