@@ -422,7 +422,7 @@ module.exports = __webpack_require__(10);
422422Object . defineProperty ( exports , "__esModule" , {
423423 value : true
424424} ) ;
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 ;
426426
427427var _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 ; } ; } ( ) ;
428428
@@ -501,8 +501,7 @@ var Sparklines = function (_PureComponent) {
501501 margin = _props . margin ,
502502 style = _props . style ,
503503 max = _props . max ,
504- min = _props . min ,
505- interactive = _props . interactive ;
504+ min = _props . min ;
506505
507506
508507 if ( data . length === 0 ) return null ;
@@ -518,8 +517,7 @@ var Sparklines = function (_PureComponent) {
518517 svgOpts ,
519518 _react2 . default . Children . map ( this . props . children , function ( child ) {
520519 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+ } )
523521 ) ;
524522 }
525523 } ] ) ;
@@ -547,8 +545,7 @@ Sparklines.defaultProps = {
547545 height : 60 ,
548546 //Scale the graphic content of the given element non-uniformly if necessary such that the element's bounding box exactly matches the viewport rectangle.
549547 preserveAspectRatio : 'none' , //https://www.w3.org/TR/SVG/coords.html#PreserveAspectRatioAttribute
550- margin : 2 ,
551- interactive : false
548+ margin : 2
552549} ;
553550exports . Sparklines = Sparklines ;
554551exports . SparklinesLine = _SparklinesLine2 . default ;
@@ -558,6 +555,7 @@ exports.SparklinesSpots = _SparklinesSpots2.default;
558555exports . SparklinesReferenceLine = _SparklinesReferenceLine2 . default ;
559556exports . SparklinesNormalBand = _SparklinesNormalBand2 . default ;
560557exports . SparklinesText = _SparklinesText2 . default ;
558+ exports . SparklinesInteractiveLayer = _SparklinesInteractiveLayer2 . default ;
561559
562560/***/ } ) ,
563561/* 11 */
@@ -2219,6 +2217,8 @@ Object.defineProperty(exports, "__esModule", {
22192217 value : true
22202218} ) ;
22212219
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+
22222222var _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 ; } ; } ( ) ;
22232223
22242224var _propTypes = __webpack_require__ ( 0 ) ;
@@ -2237,22 +2237,71 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen
22372237
22382238function _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 ; }
22392239
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 ) ;
22422282
22432283 function SparklinesInteractiveLayer ( props ) {
22442284 _classCallCheck ( this , SparklinesInteractiveLayer ) ;
22452285
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 ) ) ;
22472287
2248- _this . onMouseMove = function ( datapoints ) {
2288+ _this3 . onMouseMove = function ( datapoints , width ) {
22492289 var lastItemIndex = datapoints [ datapoints . length - 1 ] ;
22502290 return function ( event ) {
2251- var mouseX = event . nativeEvent . offsetX ;
2252- console . log ( 'movementX: ' , event . nativeEvent . movementX ) ;
2291+ if ( _this3 . state . isActive ) {
2292+ return ;
2293+ }
22532294
2295+ var mouseX = Math . floor ( event . nativeEvent . offsetX / ( _this3 . rectWidth / width ) ) ;
2296+
2297+ var pointIndex = 0 ;
22542298 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 ;
22562305 } ) ;
22572306
22582307 if ( ! nextDataPoint ) {
@@ -2275,36 +2324,68 @@ var SparklinesInteractiveLayer = function (_PureComponent) {
22752324 var x = currentDataPoint . x ;
22762325 var y = currentDataPoint . y ;
22772326
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+ } ;
22792331
2280- // spot.setAttribute("cx", x);
2281- // spot.setAttribute("cy", y);
2332+ _this3 . onMouseLeave = function ( ) {
2333+ if ( _this3 . state . isActive ) {
2334+ return ;
2335+ }
22822336
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+ } ) ;
22862345 } ;
22872346
2288- return _this ;
2347+ _this3 . state = {
2348+ cx : offscreen ,
2349+ cy : offscreen ,
2350+ isActive : false
2351+ } ;
2352+ return _this3 ;
22892353 }
22902354
22912355 _createClass ( SparklinesInteractiveLayer , [ {
2356+ key : 'componentDidMount' ,
2357+ value : function componentDidMount ( ) {
2358+ this . rectWidth = this . rect . getBoundingClientRect ( ) . width ;
2359+ }
2360+ } , {
22922361 key : 'render' ,
22932362 value : function render ( ) {
2363+ var _this4 = this ;
2364+
22942365 var _props = this . props ,
22952366 height = _props . height ,
22962367 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 ;
22982373
2299- console . log ( 'points :' , points ) ;
23002374 return _react2 . default . createElement (
2301- 'span' ,
2375+ _react2 . default . Fragment ,
23022376 null ,
2377+ _react2 . default . createElement ( Spot , { cx : cx , cy : cy } ) ,
2378+ _react2 . default . createElement ( Cursor , { x1 : cx , x2 : cx , height : height } ) ,
23032379 _react2 . default . createElement ( 'rect' , {
2380+ ref : function ref ( _ref ) {
2381+ _this4 . rect = _ref ;
2382+ } ,
23042383 height : height ,
23052384 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
23082389 } )
23092390 ) ;
23102391 }
@@ -2313,6 +2394,19 @@ var SparklinesInteractiveLayer = function (_PureComponent) {
23132394 return SparklinesInteractiveLayer ;
23142395} ( _react . PureComponent ) ;
23152396
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+ } ;
23162410exports . default = SparklinesInteractiveLayer ;
23172411
23182412/***/ } )
0 commit comments