Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
juliansteenbakker committed May 15, 2024
2 parents 171d742 + 353028e commit 706bc8b
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 11 deletions.
1 change: 1 addition & 0 deletions community_charts_common/lib/community_charts_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ export 'src/chart/scatter_plot/point_renderer_config.dart'
show PointRendererConfig;
export 'src/chart/scatter_plot/point_renderer_decorator.dart'
show PointRendererDecorator;
export 'src/chart/scatter_plot/point_label_decorator.dart';
export 'src/chart/scatter_plot/scatter_plot_chart.dart' show ScatterPlotChart;
export 'src/chart/scatter_plot/symbol_annotation_renderer.dart'
show SymbolAnnotationRenderer;
Expand Down
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
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 @@ -76,7 +77,7 @@ class LineRenderer<D> extends BaseCartesianRenderer<D> {

LineRenderer._internal({required String rendererId, required this.config})
: _pointRenderer = PointRenderer<D>(
config: PointRendererConfig<D>(radiusPx: config.radiusPx)),
config: PointRendererConfig<D>(radiusPx: config.radiusPx, symbolRenderer: config.symbolRenderer, pointRendererDecorators: config.pointRendererDecorators,)),
super(
rendererId: rendererId,
layoutPaintOrder: config.layoutPaintOrder,
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 @@ -17,6 +17,7 @@ import '../../common/symbol_renderer.dart';
import '../common/series_renderer_config.dart'
show RendererAttributes, SeriesRendererConfig;
import '../layout/layout_view.dart' show LayoutViewConfig, LayoutViewPaintOrder;
import '../scatter_plot/point_renderer_decorator.dart';
import 'line_renderer.dart' show LineRenderer;

/// Configuration for a line renderer.
Expand Down Expand Up @@ -73,6 +74,8 @@ class LineRendererConfig<D> extends LayoutViewConfig
/// Whether lines should have round end caps, or square if false.
final bool roundEndCaps;

final List<PointRendererDecorator<D>> pointRendererDecorators;

LineRendererConfig(
{this.customRendererId,
this.radiusPx = 3.5,
Expand All @@ -85,6 +88,7 @@ class LineRendererConfig<D> extends LayoutViewConfig
this.layoutPaintOrder = LayoutViewPaintOrder.line,
this.areaOpacity = 0.1,
this.roundEndCaps = false,
this.pointRendererDecorators = const [],
SymbolRenderer? symbolRenderer})
: symbolRenderer = symbolRenderer ?? LineSymbolRenderer();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2018 the Charts project authors. Please see the AUTHORS file
// for details.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import 'dart:math' show Rectangle;

import '../../common/color.dart';
import '../../common/graphics_factory.dart';
import '../../common/text_style.dart';
import '../cartesian/axis/spec/axis_spec.dart';
import '../common/chart_canvas.dart';
import 'point_renderer.dart';
import 'point_renderer_decorator.dart';


class PointLabelSpec {
final String label;
final bool selected;
PointLabelSpec({required this.label, this.selected = false,});
}

typedef LabelCallback = PointLabelSpec Function(Object? datum);

class PointLabelDecorator<D> extends PointRendererDecorator<D> {
final int horizontalPadding;
final int verticalPadding;

final TextStyleSpec? labelStyleSpec;
final TextStyleSpec? selectedLabelStyleSpec;
final LabelCallback labelCallback;
TextStyle? _labelStyle;
TextStyle? _selectedLabelStyle;


PointLabelDecorator({
this.labelStyleSpec,
this.selectedLabelStyleSpec,
required this.labelCallback,
this.horizontalPadding = 0,
this.verticalPadding = 0
});

@override
bool get renderAbove => true;

@override
void decorate(PointRendererElement<D> pointElement, ChartCanvas canvas,
GraphicsFactory graphicsFactory,
{required Rectangle drawBounds,
required double animationPercent,
bool rtl = false}) {
// Only decorate the bars when animation is at 100%.
if (animationPercent != 1.0) {
return;
}


if (_labelStyle == null) {
_initLabelStyle(graphicsFactory);
}

final labelInfo = labelCallback(pointElement.point?.datum);
final labelElement = graphicsFactory.createTextElement(labelInfo.label)
..textStyle = labelInfo.selected ? _selectedLabelStyle : _labelStyle;
final point = pointElement.point;
if (point == null || point.x == null || point.y == null) return;

final labelX = point.x!.toInt() - horizontalPadding;
final labelY = point.y!.toInt() - verticalPadding;

canvas.drawText(labelElement, labelX, labelY);

}

void _initLabelStyle(GraphicsFactory graphicsFactory) {
_labelStyle = _textStyleFromSpec(graphicsFactory, labelStyleSpec);
_selectedLabelStyle =
selectedLabelStyleSpec != null ? _textStyleFromSpec(graphicsFactory, selectedLabelStyleSpec) : _labelStyle;
}

TextStyle? _textStyleFromSpec(GraphicsFactory graphicsFactory, TextStyleSpec? spec) {
return graphicsFactory.createTextPaint()
..color = spec?.color ?? const Color(r: 0, g: 0, b: 0)
..fontFamily = spec?.fontFamily
..fontSize = spec?.fontSize ?? 12
..fontWeight = spec?.fontWeight ?? '400';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,31 @@ import 'package:flutter/material.dart';
class ScatterPlotComboLineChart extends StatelessWidget {
final List<charts.Series<dynamic, num>> seriesList;
final bool animate;

ScatterPlotComboLineChart(this.seriesList, {this.animate = false});
List<LinearSales> selectedDomains = [];
late charts.PointRendererDecorator<num> pointCustomDecorator;

ScatterPlotComboLineChart(this.seriesList, {this.animate = false}): pointCustomDecorator = charts.PointLabelDecorator(
horizontalPadding: 6,
verticalPadding: 20,
selectedLabelStyleSpec: charts.TextStyleSpec(
color: charts.Color.fromHex(code: '#FF9560'),
fontWeight: '500',
fontSize: 14,
),
labelCallback: (domain) {
if (domain is LinearSales?) {
// final selected = selectedDomains.contains(domain);
return charts.PointLabelSpec(
label: domain?.sales.toString() ?? '',
selected: false,
);
}
return charts.PointLabelSpec(
label: '',
selected: false,
);
});


/// Creates a [ScatterPlotChart] with sample data and no transition.
factory ScatterPlotComboLineChart.withSampleData() {
Expand Down Expand Up @@ -108,6 +131,8 @@ class ScatterPlotComboLineChart extends StatelessWidget {
}
// EXCLUDE_FROM_GALLERY_DOCS_END



@override
Widget build(BuildContext context) {
return new charts.ScatterPlotChart(seriesList,
Expand All @@ -117,7 +142,11 @@ class ScatterPlotComboLineChart extends StatelessWidget {
//
// This is the default configuration, but is shown here for
// illustration.
defaultRenderer: new charts.PointRendererConfig(),
defaultRenderer: new charts.PointRendererConfig(
pointRendererDecorators: [
pointCustomDecorator,
]
),
// Custom renderer configuration for the line series.
customSeriesRenderers: [
new charts.LineRendererConfig(
Expand All @@ -127,7 +156,7 @@ class ScatterPlotComboLineChart extends StatelessWidget {
//
// By default, series drawn by the point renderer are painted on
// top of those drawn by a line renderer.
layoutPaintOrder: charts.LayoutViewPaintOrder.point + 1)
layoutPaintOrder: charts.LayoutViewPaintOrder.point + 1),
]);
}

Expand Down
2 changes: 2 additions & 0 deletions community_charts_flutter/lib/community_charts_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ export 'package:community_charts_common/community_charts_common.dart'
PointRenderer,
PointRendererConfig,
PointRendererDecorator,
PointLabelSpec,
PointLabelDecorator,
PointRendererElement,
PointSymbolRenderer,
RangeAnnotationAxisType,
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,
);
}
}
Loading

0 comments on commit 706bc8b

Please sign in to comment.