Skip to content

Commit

Permalink
enhance LinePointHighlighter to support callbacking the highlight poi…
Browse files Browse the repository at this point in the history
…nts's info like position, datum and its corresponding seriers id
  • Loading branch information
ZhaosongRen committed Mar 28, 2024
1 parent 85bd43b commit 3440ecc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -116,6 +117,14 @@ class LinePointHighlighter<D> implements ChartBehavior<D> {
// data.
final _currentKeys = <String>[];

// series ids, only highlight the data from these series
//
final List<String>? _seriesIds;

// callback the caller with the selected point
//
final void Function(List<DatumDetails<D>>? selectedDetails)? onHighLightDatumUpdated;

LinePointHighlighter(
{SelectionModelType? selectionModelType,
double? defaultRadiusPx,
Expand All @@ -124,6 +133,8 @@ class LinePointHighlighter<D> implements ChartBehavior<D> {
LinePointHighlighterFollowLineType? showVerticalFollowLine,
List<int>? dashPattern,
bool? drawFollowLinesAcrossChart,
List<String>? seriesIds,
this.onHighLightDatumUpdated,
SymbolRenderer? symbolRenderer})
: selectionModelType = selectionModelType ?? SelectionModelType.info,
defaultRadiusPx = defaultRadiusPx ?? 4.0,
Expand All @@ -134,6 +145,7 @@ class LinePointHighlighter<D> implements ChartBehavior<D> {
LinePointHighlighterFollowLineType.nearest,
dashPattern = dashPattern ?? [1, 3],
drawFollowLinesAcrossChart = drawFollowLinesAcrossChart ?? true,
_seriesIds = seriesIds,
symbolRenderer = symbolRenderer ?? CircleSymbolRenderer() {
_lifecycleListener =
LifecycleListener<D>(onAxisConfigured: _updateViewData);
Expand Down Expand Up @@ -181,8 +193,9 @@ class LinePointHighlighter<D> implements ChartBehavior<D> {
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
Expand Down Expand Up @@ -272,6 +285,8 @@ class LinePointHighlighter<D> implements ChartBehavior<D> {

_seriesPointMap = newSeriesMap;
_view.seriesPointMap = _seriesPointMap;

onHighLightDatumUpdated?.call(allSelectedDatumDetails);
}

@override
Expand Down
7 changes: 7 additions & 0 deletions community_charts_common/lib/src/chart/line/line_renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -926,6 +927,12 @@ class LineRenderer<D> extends BaseCartesianRenderer<D> {
// 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
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.
///
Expand All @@ -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<D> extends ChartBehavior<D> {
class LinePointHighlighter<D, T extends Object> extends ChartBehavior<D> {
final desiredGestures = new Set<GestureType>();

final common.SelectionModelType? selectionModelType;
Expand Down Expand Up @@ -71,6 +80,9 @@ class LinePointHighlighter<D> extends ChartBehavior<D> {
/// Renderer used to draw the highlighted points.
final common.SymbolRenderer? symbolRenderer;

final List<String>? seriesIds;
final void Function(List<HighlighterPointInfo<T>>? selectedDetails)? onHighLightDatumUpdated;

LinePointHighlighter(
{this.selectionModelType,
this.defaultRadiusPx,
Expand All @@ -79,6 +91,8 @@ class LinePointHighlighter<D> extends ChartBehavior<D> {
this.showVerticalFollowLine,
this.dashPattern,
this.drawFollowLinesAcrossChart,
this.seriesIds,
this.onHighLightDatumUpdated,
this.symbolRenderer});

@override
Expand All @@ -92,6 +106,8 @@ class LinePointHighlighter<D> extends ChartBehavior<D> {
dashPattern: dashPattern,
drawFollowLinesAcrossChart: drawFollowLinesAcrossChart,
symbolRenderer: symbolRenderer,
seriesIds: seriesIds,
onHighLightDatumUpdated: (selectedDetails) => onHighLightDatumUpdated?.call(selectedDetails?.map((e) => HighlighterPointInfo<T>(seriesId: e.series?.id ?? '', point: e.chartPosition, datum: e.datum,)).toList()),
);

@override
Expand All @@ -108,6 +124,7 @@ class LinePointHighlighter<D> extends ChartBehavior<D> {
showHorizontalFollowLine == o.showHorizontalFollowLine &&
showVerticalFollowLine == o.showVerticalFollowLine &&
selectionModelType == o.selectionModelType &&
seriesIds == o.seriesIds &&
new ListEquality().equals(dashPattern, o.dashPattern) &&
drawFollowLinesAcrossChart == o.drawFollowLinesAcrossChart;
}
Expand All @@ -122,6 +139,7 @@ class LinePointHighlighter<D> extends ChartBehavior<D> {
showVerticalFollowLine,
dashPattern,
drawFollowLinesAcrossChart,
seriesIds,
);
}
}
2 changes: 1 addition & 1 deletion community_charts_flutter/lib/src/line_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ class LineChart extends CartesianChart<num> {
void addDefaultInteractions(List<ChartBehavior> behaviors) {
super.addDefaultInteractions(behaviors);

behaviors.add(new LinePointHighlighter<num>());
behaviors.add(new LinePointHighlighter<num, Object>());
}
}
2 changes: 1 addition & 1 deletion community_charts_flutter/lib/src/time_series_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ class TimeSeriesChart extends CartesianChart<DateTime> {
void addDefaultInteractions(List<ChartBehavior> behaviors) {
super.addDefaultInteractions(behaviors);

behaviors.add(new LinePointHighlighter<DateTime>());
behaviors.add(new LinePointHighlighter<DateTime, Object>());
}
}

0 comments on commit 3440ecc

Please sign in to comment.