Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement clipData feature in scatter chart. #898

Merged
merged 4 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## newVersion
* **BUGFIX** Fix `clipData` implementation in ScatterChart, #897.
* **BUGFIX** Fix PieChart changing sections issue (we have disabled semantics for pieChart badgeWidgets), #861.
* **BUGFIX** Fix LineChart width smaller width or height lower than 40, #869, #857.
* **BUGFIX** Allow to show title when axis diff is zero.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/chart/scatter_chart/scatter_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import 'dart:ui';

import 'package:equatable/equatable.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:fl_chart/src/extensions/color_extension.dart';
imaNNeo marked this conversation as resolved.
Show resolved Hide resolved
import 'package:fl_chart/src/utils/lerp.dart';
import 'package:flutter/material.dart';
import 'package:fl_chart/src/extensions/color_extension.dart';

import 'scatter_chart_helper.dart';

Expand Down
47 changes: 47 additions & 0 deletions lib/src/chart/scatter_chart/scatter_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,49 @@ class ScatterChartPainter extends AxisChartPainter<ScatterChartData> {
final data = holder.data;
final viewSize = canvasWrapper.size;
final chartUsableSize = getChartUsableDrawSize(viewSize, holder);
final clip = data.clipData;
final border = data.borderData.show ? data.borderData.border : null;

if (data.clipData.any) {
canvasWrapper.saveLayer(
Rect.fromLTRB(
0,
0,
canvasWrapper.size.width,
canvasWrapper.size.height,
),
Paint(),
);

var left = 0.0;
var top = 0.0;
var right = viewSize.width;
var bottom = viewSize.height;

if (clip.left) {
final borderWidth = border?.left.width ?? 0;
left = getLeftOffsetDrawSize(holder) + (borderWidth / 2);
}
if (clip.top) {
final borderWidth = border?.top.width ?? 0;
top = getTopOffsetDrawSize(holder) + (borderWidth / 2);
}
if (clip.right) {
final borderWidth = border?.right.width ?? 0;
right = getLeftOffsetDrawSize(holder) +
chartUsableSize.width -
(borderWidth / 2);
}
if (clip.bottom) {
final borderWidth = border?.bottom.width ?? 0;
bottom = getTopOffsetDrawSize(holder) +
chartUsableSize.height -
(borderWidth / 2);
}

canvasWrapper.clipRect(Rect.fromLTRB(left, top, right, bottom));
}

for (final scatterSpot in data.scatterSpots) {
if (!scatterSpot.show) {
continue;
Expand All @@ -242,6 +285,10 @@ class ScatterChartPainter extends AxisChartPainter<ScatterChartData> {
_spotsPaint,
);
}

if (data.clipData.any) {
canvasWrapper.restore();
imaNNeo marked this conversation as resolved.
Show resolved Hide resolved
}
}

@visibleForTesting
Expand Down
8 changes: 7 additions & 1 deletion test/chart/scatter_chart/scatter_chart_painter_test.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import 'dart:math' as math;

import 'package:fl_chart/fl_chart.dart';
import 'package:fl_chart/src/chart/base/base_chart/base_chart_painter.dart';
import 'package:fl_chart/src/chart/scatter_chart/scatter_chart_painter.dart';
import 'package:fl_chart/src/utils/canvas_wrapper.dart';
import 'package:fl_chart/src/utils/utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:fl_chart/src/chart/base/base_chart/base_chart_painter.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';

import '../data_pool.dart';
import 'scatter_chart_painter_test.mocks.dart';

Expand Down Expand Up @@ -423,6 +424,7 @@ void main() {
ScatterSpot(7, 5, radius: 6),
],
titlesData: FlTitlesData(show: false),
clipData: FlClipData.all(),
);

final ScatterChartPainter scatterChartPainter = ScatterChartPainter();
Expand All @@ -441,6 +443,8 @@ void main() {
.called(1);
verify(_mockCanvasWrapper.drawCircle(const Offset(70, 50), 6, any))
.called(1);

verify(_mockCanvasWrapper.clipRect(any)).called(1);
});

test('test 2', () {
Expand All @@ -458,6 +462,7 @@ void main() {
ScatterSpot(7, 5, show: false),
],
titlesData: FlTitlesData(show: false),
clipData: FlClipData.none(),
);

final ScatterChartPainter scatterChartPainter = ScatterChartPainter();
Expand All @@ -471,6 +476,7 @@ void main() {
);

verifyNever(_mockCanvasWrapper.drawCircle(any, any, any));
verifyNever(_mockCanvasWrapper.clipRect(any));
});
});

Expand Down