Skip to content

Commit

Permalink
Migrate scatter_chart_data
Browse files Browse the repository at this point in the history
  • Loading branch information
imaNNeo committed Mar 3, 2021
1 parent e55ed8c commit 2de1285
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 123 deletions.
196 changes: 73 additions & 123 deletions lib/src/chart/scatter_chart/scatter_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'package:fl_chart/src/utils/lerp.dart';
import 'package:flutter/animation.dart';
import 'package:flutter/material.dart';

import 'scatter_chart_helper.dart';

/// [ScatterChart] needs this class to render itself.
///
/// It holds data needed to draw a scatter chart,
Expand Down Expand Up @@ -37,19 +39,19 @@ class ScatterChartData extends AxisChartData with EquatableMixin {
///
/// [clipData] forces the [LineChart] to draw lines inside the chart bounding box.
ScatterChartData({
List<ScatterSpot> scatterSpots,
FlTitlesData titlesData,
ScatterTouchData scatterTouchData,
List<int> showingTooltipIndicators,
FlGridData gridData,
FlBorderData borderData,
FlAxisTitleData axisTitleData,
double minX,
double maxX,
double minY,
double maxY,
FlClipData clipData,
Color backgroundColor,
List<ScatterSpot>? scatterSpots,
FlTitlesData? titlesData,
ScatterTouchData? scatterTouchData,
List<int>? showingTooltipIndicators,
FlGridData? gridData,
FlBorderData? borderData,
FlAxisTitleData? axisTitleData,
double? minX,
double? maxX,
double? minY,
double? maxY,
FlClipData? clipData,
Color? backgroundColor,
}) : scatterSpots = scatterSpots ?? const [],
titlesData = titlesData ?? FlTitlesData(),
scatterTouchData = scatterTouchData ?? ScatterTouchData(),
Expand All @@ -61,69 +63,18 @@ class ScatterChartData extends AxisChartData with EquatableMixin {
axisTitleData: axisTitleData ?? FlAxisTitleData(),
clipData: clipData ?? FlClipData.none(),
backgroundColor: backgroundColor,
) {
initSuperMinMaxValues(minX, maxX, minY, maxY);
}
minX: minX ?? ScatterChartHelper.calculateMaxAxisValues(scatterSpots ?? const []).minX,
maxX: maxX ?? ScatterChartHelper.calculateMaxAxisValues(scatterSpots ?? const []).maxX,
minY: minY ?? ScatterChartHelper.calculateMaxAxisValues(scatterSpots ?? const []).minY,
maxY: maxY ?? ScatterChartHelper.calculateMaxAxisValues(scatterSpots ?? const []).maxY,
);

/// fills [minX], [maxX], [minY], [maxY] if they are null,
/// based on the provided [scatterSpots].
void initSuperMinMaxValues(
double minX,
double maxX,
double minY,
double maxY,
) {
if (scatterSpots.isNotEmpty) {
final canModifyMinX = minX == null;
if (canModifyMinX) {
minX = scatterSpots[0].x;
}

final canModifyMaxX = maxX == null;
if (canModifyMaxX) {
maxX = scatterSpots[0].x;
}

final canModifyMinY = minY == null;
if (canModifyMinY) {
minY = scatterSpots[0].y;
}

final canModifyMaxY = maxY == null;
if (canModifyMaxY) {
maxY = scatterSpots[0].y;
}

for (var j = 0; j < scatterSpots.length; j++) {
final spot = scatterSpots[j];
if (canModifyMaxX && spot.x > maxX) {
maxX = spot.x;
}

if (canModifyMinX && spot.x < minX) {
minX = spot.x;
}

if (canModifyMaxY && spot.y > maxY) {
maxY = spot.y;
}

if (canModifyMinY && spot.y < minY) {
minY = spot.y;
}
}
}

super.minX = minX ?? 0;
super.maxX = maxX ?? 1;
super.minY = minY ?? 0;
super.maxY = maxY ?? 1;
}

/// Lerps a [ScatterChartData] based on [t] value, check [Tween.lerp].
@override
ScatterChartData lerp(BaseChartData a, BaseChartData b, double t) {
if (a is ScatterChartData && b is ScatterChartData && t != null) {
if (a is ScatterChartData && b is ScatterChartData) {
return ScatterChartData(
scatterSpots: lerpScatterSpotList(a.scatterSpots, b.scatterSpots, t),
titlesData: FlTitlesData.lerp(a.titlesData, b.titlesData, t),
Expand All @@ -148,19 +99,19 @@ class ScatterChartData extends AxisChartData with EquatableMixin {
/// Copies current [ScatterChartData] to a new [ScatterChartData],
/// and replaces provided values.
ScatterChartData copyWith({
List<ScatterSpot> scatterSpots,
FlTitlesData titlesData,
ScatterTouchData scatterTouchData,
List<int> showingTooltipIndicators,
FlGridData gridData,
FlBorderData borderData,
FlAxisTitleData axisTitleData,
double minX,
double maxX,
double minY,
double maxY,
FlClipData clipData,
Color backgroundColor,
List<ScatterSpot>? scatterSpots,
FlTitlesData? titlesData,
ScatterTouchData? scatterTouchData,
List<int>? showingTooltipIndicators,
FlGridData? gridData,
FlBorderData? borderData,
FlAxisTitleData? axisTitleData,
double? minX,
double? maxX,
double? minY,
double? maxY,
FlClipData? clipData,
Color? backgroundColor,
}) {
return ScatterChartData(
scatterSpots: scatterSpots ?? this.scatterSpots,
Expand All @@ -181,7 +132,7 @@ class ScatterChartData extends AxisChartData with EquatableMixin {

/// Used for equality check, see [EquatableMixin].
@override
List<Object> get props => [
List<Object?> get props => [
scatterSpots,
titlesData,
scatterTouchData,
Expand Down Expand Up @@ -217,21 +168,21 @@ class ScatterSpot extends FlSpot with EquatableMixin {
ScatterSpot(
double x,
double y, {
bool show,
double radius,
Color color,
bool? show,
double? radius,
Color? color,
}) : show = show ?? true,
radius = radius ?? 6,
color = color ?? Colors.primaries[((x * y) % Colors.primaries.length).toInt()],
super(x, y);

@override
ScatterSpot copyWith({
double x,
double y,
bool show,
double radius,
Color color,
double? x,
double? y,
bool? show,
double? radius,
Color? color,
}) {
return ScatterSpot(
x ?? this.x,
Expand All @@ -245,8 +196,8 @@ class ScatterSpot extends FlSpot with EquatableMixin {
/// Lerps a [ScatterSpot] based on [t] value, check [Tween.lerp].
static ScatterSpot lerp(ScatterSpot a, ScatterSpot b, double t) {
return ScatterSpot(
lerpDouble(a.x, b.x, t),
lerpDouble(a.y, b.y, t),
lerpDouble(a.x, b.x, t)!,
lerpDouble(a.y, b.y, t)!,
show: b.show,
radius: lerpDouble(a.radius, b.radius, t),
color: Color.lerp(a.color, b.color, t),
Expand All @@ -255,7 +206,7 @@ class ScatterSpot extends FlSpot with EquatableMixin {

/// Used for equality check, see [EquatableMixin].
@override
List<Object> get props => [
List<Object?> get props => [
x,
y,
show,
Expand All @@ -281,7 +232,7 @@ class ScatterTouchData extends FlTouchData with EquatableMixin {
final bool handleBuiltInTouches;

/// you can implement it to receive touches callback
final Function(ScatterTouchResponse) touchCallback;
final Function(ScatterTouchResponse)? touchCallback;

/// You can disable or enable the touch system using [enabled] flag,
/// if [handleBuiltInTouches] is true, [ScatterChart] shows a tooltip popup on top of the spots if
Expand All @@ -294,11 +245,11 @@ class ScatterTouchData extends FlTouchData with EquatableMixin {
/// It gives you a [ScatterTouchResponse] that contains some
/// useful information about happened touch.
ScatterTouchData({
bool enabled,
ScatterTouchTooltipData touchTooltipData,
double touchSpotThreshold,
bool handleBuiltInTouches,
Function(ScatterTouchResponse) touchCallback,
bool? enabled,
ScatterTouchTooltipData? touchTooltipData,
double? touchSpotThreshold,
bool? handleBuiltInTouches,
Function(ScatterTouchResponse)? touchCallback,
}) : touchTooltipData = touchTooltipData ?? ScatterTouchTooltipData(),
touchSpotThreshold = touchSpotThreshold ?? 10,
handleBuiltInTouches = handleBuiltInTouches ?? true,
Expand All @@ -308,11 +259,11 @@ class ScatterTouchData extends FlTouchData with EquatableMixin {
/// Copies current [ScatterTouchData] to a new [ScatterTouchData],
/// and replaces provided values.
ScatterTouchData copyWith({
bool enabled,
LineTouchTooltipData touchTooltipData,
double touchSpotThreshold,
bool handleBuiltInTouches,
Function(ScatterTouchResponse) touchCallback,
bool? enabled,
ScatterTouchTooltipData? touchTooltipData,
double? touchSpotThreshold,
bool? handleBuiltInTouches,
Function(ScatterTouchResponse)? touchCallback,
}) {
return ScatterTouchData(
enabled: enabled ?? this.enabled,
Expand All @@ -325,7 +276,7 @@ class ScatterTouchData extends FlTouchData with EquatableMixin {

/// Used for equality check, see [EquatableMixin].
@override
List<Object> get props => [
List<Object?> get props => [
enabled,
touchTooltipData,
touchSpotThreshold,
Expand Down Expand Up @@ -358,7 +309,7 @@ class ScatterTouchResponse extends BaseTouchResponse with EquatableMixin {

/// Used for equality check, see [EquatableMixin].
@override
List<Object> get props => [
List<Object?> get props => [
touchInput,
touchedSpot,
touchedSpotIndex,
Expand Down Expand Up @@ -401,13 +352,13 @@ class ScatterTouchTooltipData with EquatableMixin {
/// you can set [fitInsideHorizontally] true to force it to shift inside the chart horizontally,
/// also you can set [fitInsideVertically] true to force it to shift inside the chart vertically.
ScatterTouchTooltipData({
Color tooltipBgColor,
double tooltipRoundedRadius,
EdgeInsets tooltipPadding,
double maxContentWidth,
GetScatterTooltipItems getTooltipItems,
bool fitInsideHorizontally,
bool fitInsideVertically,
Color? tooltipBgColor,
double? tooltipRoundedRadius,
EdgeInsets? tooltipPadding,
double? maxContentWidth,
GetScatterTooltipItems? getTooltipItems,
bool? fitInsideHorizontally,
bool? fitInsideVertically,
}) : tooltipBgColor = tooltipBgColor ?? Colors.white,
tooltipRoundedRadius = tooltipRoundedRadius ?? 4,
tooltipPadding = tooltipPadding ?? const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
Expand All @@ -419,7 +370,7 @@ class ScatterTouchTooltipData with EquatableMixin {

/// Used for equality check, see [EquatableMixin].
@override
List<Object> get props => [
List<Object?> get props => [
tooltipBgColor,
tooltipRoundedRadius,
tooltipPadding,
Expand All @@ -440,9 +391,6 @@ typedef GetScatterTooltipItems = ScatterTooltipItem Function(ScatterSpot touched

/// Default implementation for [ScatterTouchTooltipData.getTooltipItems].
ScatterTooltipItem defaultScatterTooltipItem(ScatterSpot touchedSpot) {
if (touchedSpot == null) {
return null;
}
final textStyle = TextStyle(
color: touchedSpot.color,
fontWeight: FontWeight.bold,
Expand Down Expand Up @@ -475,7 +423,7 @@ class ScatterTooltipItem with EquatableMixin {

/// Used for equality check, see [EquatableMixin].
@override
List<Object> get props => [
List<Object?> get props => [
text,
textStyle,
bottomMargin,
Expand All @@ -484,12 +432,14 @@ class ScatterTooltipItem with EquatableMixin {

/// It lerps a [ScatterChartData] to another [ScatterChartData] (handles animation for updating values)
class ScatterChartDataTween extends Tween<ScatterChartData> {
ScatterChartDataTween({ScatterChartData begin, ScatterChartData end})
: super(begin: begin, end: end);
ScatterChartDataTween({
required ScatterChartData begin,
required ScatterChartData end
}) : super(begin: begin, end: end);

/// Lerps a [ScatterChartData] based on [t] value, check [Tween.lerp].
@override
ScatterChartData lerp(double t) {
return begin.lerp(begin, end, t);
return begin!.lerp(begin!, end!, t);
}
}
Loading

0 comments on commit 2de1285

Please sign in to comment.