diff --git a/example/lib/charts/scrollable_chart_screen.dart b/example/lib/charts/scrollable_chart_screen.dart index 9ddeed6..278b58b 100644 --- a/example/lib/charts/scrollable_chart_screen.dart +++ b/example/lib/charts/scrollable_chart_screen.dart @@ -81,13 +81,19 @@ class _ScrollableChartScreenState extends State { }, ), behaviour: ChartBehaviour( - scrollSettings: - _isScrollable ? ScrollSettings() : ScrollSettings.none(), + scrollSettings: _isScrollable ? ScrollSettings() : ScrollSettings.none(), onItemClicked: (item) { + print('Clciked'); setState(() { _selected = item.itemIndex; }); }, + onItemHoverEnter: (_) { + print('Hover Enter'); + }, + onItemHoverExit: (_) { + print('Hover Enter'); + }, ), backgroundDecorations: [ HorizontalAxisDecoration( diff --git a/lib/chart.dart b/lib/chart.dart index 9dc873d..307245a 100644 --- a/lib/chart.dart +++ b/lib/chart.dart @@ -7,6 +7,7 @@ import 'dart:ui'; import 'package:collection/collection.dart'; import 'package:equatable/equatable.dart'; import 'package:flutter/foundation.dart'; +import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; diff --git a/lib/chart/model/theme/chart_behaviour.dart b/lib/chart/model/theme/chart_behaviour.dart index e6f9499..9dd8e62 100644 --- a/lib/chart/model/theme/chart_behaviour.dart +++ b/lib/chart/model/theme/chart_behaviour.dart @@ -11,9 +11,16 @@ class ChartBehaviour { const ChartBehaviour({ this.scrollSettings = const ScrollSettings.none(), this.onItemClicked, + this.onItemHoverEnter, + this.onItemHoverExit, }); - ChartBehaviour._lerp(this.scrollSettings, this.onItemClicked); + ChartBehaviour._lerp( + this.scrollSettings, + this.onItemClicked, + this.onItemHoverEnter, + this.onItemHoverExit, + ); final ScrollSettings scrollSettings; @@ -24,11 +31,21 @@ class ChartBehaviour { /// Return true if chart is currently scrollable bool get isScrollable => scrollSettings._isScrollable > 0.5; + /// Return index of item clicked. Since graph can be multi value, user + /// will have to handle clicked index to show data they want to show + final ValueChanged? onItemHoverEnter; + + /// Return index of item clicked. Since graph can be multi value, user + /// will have to handle clicked index to show data they want to show + final ValueChanged? onItemHoverExit; + /// Animate Behaviour from one state to other static ChartBehaviour lerp(ChartBehaviour a, ChartBehaviour b, double t) { return ChartBehaviour._lerp( ScrollSettings.lerp(a.scrollSettings, b.scrollSettings, t), t > 0.5 ? b.onItemClicked : a.onItemClicked, + t > 0.5 ? b.onItemHoverEnter : a.onItemHoverEnter, + t > 0.5 ? b.onItemHoverExit : a.onItemHoverExit, ); } } diff --git a/lib/chart/render/geometry/child_item_renderer.dart b/lib/chart/render/geometry/child_item_renderer.dart index 8a80eb7..9bf7b9e 100644 --- a/lib/chart/render/geometry/child_item_renderer.dart +++ b/lib/chart/render/geometry/child_item_renderer.dart @@ -144,6 +144,16 @@ class _RenderChildChartItem extends RenderShiftedBox { _state.behaviour.onItemClicked ?.call(ItemBuilderData(item, itemIndex, listIndex)); } + + if (event is PointerHoverEvent) { + _state.behaviour.onItemHoverEnter + ?.call(ItemBuilderData(item, itemIndex, listIndex)); + } + + if (event is PointerHoverEvent) { + _state.behaviour.onItemHoverExit + ?.call(ItemBuilderData(item, itemIndex, listIndex)); + } } @override diff --git a/lib/chart/render/geometry/leaf_item_renderer.dart b/lib/chart/render/geometry/leaf_item_renderer.dart index 5b70ce4..6e3280a 100644 --- a/lib/chart/render/geometry/leaf_item_renderer.dart +++ b/lib/chart/render/geometry/leaf_item_renderer.dart @@ -164,6 +164,16 @@ class _RenderLeafChartItem extends RenderBox { _state.behaviour.onItemClicked ?.call(ItemBuilderData(item, itemIndex, listIndex)); } + + if (event is PointerHoverEvent) { + _state.behaviour.onItemHoverEnter + ?.call(ItemBuilderData(item, itemIndex, listIndex)); + } + + if (event is PointerHoverEvent) { + _state.behaviour.onItemHoverExit + ?.call(ItemBuilderData(item, itemIndex, listIndex)); + } } @override