diff --git a/community_charts_common/lib/src/chart/common/behavior/line_point_highlighter.dart b/community_charts_common/lib/src/chart/common/behavior/line_point_highlighter.dart index 24d8d76..f9a6047 100644 --- a/community_charts_common/lib/src/chart/common/behavior/line_point_highlighter.dart +++ b/community_charts_common/lib/src/chart/common/behavior/line_point_highlighter.dart @@ -36,6 +36,7 @@ import '../../layout/layout_view.dart' ViewMeasuredSizes; import '../base_chart.dart' show BaseChart, LifecycleListener; import '../chart_canvas.dart' show ChartCanvas, getAnimatedColor; +import '../datum_details.dart'; import '../processed_series.dart' show ImmutableSeries; import '../selection_model/selection_model.dart' show SelectionModel, SelectionModelType; @@ -116,6 +117,14 @@ class LinePointHighlighter implements ChartBehavior { // data. final _currentKeys = []; + // series ids, only highlight the data from these series + // + final List? _seriesIds; + + // callback the caller with the selected point + // + final void Function(List>? selectedDetails)? onHighLightDatumUpdated; + LinePointHighlighter( {SelectionModelType? selectionModelType, double? defaultRadiusPx, @@ -124,6 +133,8 @@ class LinePointHighlighter implements ChartBehavior { LinePointHighlighterFollowLineType? showVerticalFollowLine, List? dashPattern, bool? drawFollowLinesAcrossChart, + List? seriesIds, + this.onHighLightDatumUpdated, SymbolRenderer? symbolRenderer}) : selectionModelType = selectionModelType ?? SelectionModelType.info, defaultRadiusPx = defaultRadiusPx ?? 4.0, @@ -134,6 +145,7 @@ class LinePointHighlighter implements ChartBehavior { LinePointHighlighterFollowLineType.nearest, dashPattern = dashPattern ?? [1, 3], drawFollowLinesAcrossChart = drawFollowLinesAcrossChart ?? true, + _seriesIds = seriesIds, symbolRenderer = symbolRenderer ?? CircleSymbolRenderer() { _lifecycleListener = LifecycleListener(onAxisConfigured: _updateViewData); @@ -181,8 +193,9 @@ class LinePointHighlighter implements ChartBehavior { void _updateViewData() { _currentKeys.clear(); - final selectedDatumDetails = - _chart.getSelectedDatumDetails(selectionModelType); + final allSelectedDatumDetails = _chart.getSelectedDatumDetails(selectionModelType); + final selectedDatumDetails = allSelectedDatumDetails + .where((element) => _seriesIds == null || _seriesIds!.contains(element.series?.id)); // Create a new map each time to ensure that we have it sorted in the // selection model order. This preserves the "nearestDetail" ordering, so @@ -272,6 +285,8 @@ class LinePointHighlighter implements ChartBehavior { _seriesPointMap = newSeriesMap; _view.seriesPointMap = _seriesPointMap; + + onHighLightDatumUpdated?.call(allSelectedDatumDetails); } @override diff --git a/community_charts_common/lib/src/chart/line/line_renderer.dart b/community_charts_common/lib/src/chart/line/line_renderer.dart index 475bd59..6ff55d4 100644 --- a/community_charts_common/lib/src/chart/line/line_renderer.dart +++ b/community_charts_common/lib/src/chart/line/line_renderer.dart @@ -20,6 +20,7 @@ import 'package:collection/collection.dart' show IterableExtension; import 'package:meta/meta.dart' show visibleForTesting; import '../../common/color.dart' show Color; +import '../../common/graphics_factory.dart'; import '../../common/math.dart'; import '../../data/series.dart' show AttributeKey, AccessorFn; import '../cartesian/axis/axis.dart' @@ -926,6 +927,12 @@ class LineRenderer extends BaseCartesianRenderer { // creation time, since chart onInit is called after the chart is created. _chart = chart; } + + @override + void set graphicsFactory(GraphicsFactory? graphicsFactory) { + super.graphicsFactory = graphicsFactory; + _pointRenderer.graphicsFactory = graphicsFactory; + } @override void paint(ChartCanvas canvas, double animationPercent) { diff --git a/community_charts_flutter/lib/src/behaviors/line_point_highlighter.dart b/community_charts_flutter/lib/src/behaviors/line_point_highlighter.dart index 365096a..1cc5c6f 100644 --- a/community_charts_flutter/lib/src/behaviors/line_point_highlighter.dart +++ b/community_charts_flutter/lib/src/behaviors/line_point_highlighter.dart @@ -20,11 +20,20 @@ import 'package:community_charts_common/community_charts_common.dart' as common LinePointHighlighter, LinePointHighlighterFollowLineType, SelectionModelType, + NullablePoint, SymbolRenderer; import 'package:meta/meta.dart' show immutable; import 'chart_behavior.dart' show ChartBehavior, GestureType; +class HighlighterPointInfo { + final common.NullablePoint? point; + final T? datum; + final String seriesId; + + HighlighterPointInfo({this.point, this.datum, required this.seriesId,}); +} + /// Chart behavior that monitors the specified [SelectionModel] and darkens the /// color for selected data. /// @@ -33,7 +42,7 @@ import 'chart_behavior.dart' show ChartBehavior, GestureType; /// It is used in combination with SelectNearest to update the selection model /// and expand selection out to the domain value. @immutable -class LinePointHighlighter extends ChartBehavior { +class LinePointHighlighter extends ChartBehavior { final desiredGestures = new Set(); final common.SelectionModelType? selectionModelType; @@ -71,6 +80,9 @@ class LinePointHighlighter extends ChartBehavior { /// Renderer used to draw the highlighted points. final common.SymbolRenderer? symbolRenderer; + final List? seriesIds; + final void Function(List>? selectedDetails)? onHighLightDatumUpdated; + LinePointHighlighter( {this.selectionModelType, this.defaultRadiusPx, @@ -79,6 +91,8 @@ class LinePointHighlighter extends ChartBehavior { this.showVerticalFollowLine, this.dashPattern, this.drawFollowLinesAcrossChart, + this.seriesIds, + this.onHighLightDatumUpdated, this.symbolRenderer}); @override @@ -92,6 +106,8 @@ class LinePointHighlighter extends ChartBehavior { dashPattern: dashPattern, drawFollowLinesAcrossChart: drawFollowLinesAcrossChart, symbolRenderer: symbolRenderer, + seriesIds: seriesIds, + onHighLightDatumUpdated: (selectedDetails) => onHighLightDatumUpdated?.call(selectedDetails?.map((e) => HighlighterPointInfo(seriesId: e.series?.id ?? '', point: e.chartPosition, datum: e.datum,)).toList()), ); @override @@ -108,6 +124,7 @@ class LinePointHighlighter extends ChartBehavior { showHorizontalFollowLine == o.showHorizontalFollowLine && showVerticalFollowLine == o.showVerticalFollowLine && selectionModelType == o.selectionModelType && + seriesIds == o.seriesIds && new ListEquality().equals(dashPattern, o.dashPattern) && drawFollowLinesAcrossChart == o.drawFollowLinesAcrossChart; } @@ -122,6 +139,7 @@ class LinePointHighlighter extends ChartBehavior { showVerticalFollowLine, dashPattern, drawFollowLinesAcrossChart, + seriesIds, ); } } diff --git a/community_charts_flutter/lib/src/line_chart.dart b/community_charts_flutter/lib/src/line_chart.dart index 22af7f1..77f2b97 100644 --- a/community_charts_flutter/lib/src/line_chart.dart +++ b/community_charts_flutter/lib/src/line_chart.dart @@ -85,6 +85,6 @@ class LineChart extends CartesianChart { void addDefaultInteractions(List behaviors) { super.addDefaultInteractions(behaviors); - behaviors.add(new LinePointHighlighter()); + behaviors.add(new LinePointHighlighter()); } } diff --git a/community_charts_flutter/lib/src/time_series_chart.dart b/community_charts_flutter/lib/src/time_series_chart.dart index dd8c531..6ccaa31 100644 --- a/community_charts_flutter/lib/src/time_series_chart.dart +++ b/community_charts_flutter/lib/src/time_series_chart.dart @@ -92,6 +92,6 @@ class TimeSeriesChart extends CartesianChart { void addDefaultInteractions(List behaviors) { super.addDefaultInteractions(behaviors); - behaviors.add(new LinePointHighlighter()); + behaviors.add(new LinePointHighlighter()); } }