Skip to content

Commit

Permalink
Fix tests and analyze problems
Browse files Browse the repository at this point in the history
  • Loading branch information
imaNNeo committed Mar 23, 2022
1 parent 644f2eb commit b368b82
Show file tree
Hide file tree
Showing 22 changed files with 322 additions and 233 deletions.
1 change: 0 additions & 1 deletion example/lib/app_utils.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:math' as math;

class AppUtils {

static final AppUtils _singleton = AppUtils._internal();

factory AppUtils() {
Expand Down
2 changes: 1 addition & 1 deletion example/lib/bar_chart/samples/bar_chart_sample4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class BarChartSample4State extends State<BarChartSample4> {
final Color normal = const Color(0xff64caad);
final Color light = const Color(0xff73e8c9);

Widget bottomTitles(double value,TitleMeta meta) {
Widget bottomTitles(double value, TitleMeta meta) {
const style = TextStyle(color: Color(0xff939393), fontSize: 10);
String text;
switch (value.toInt()) {
Expand Down
1 change: 0 additions & 1 deletion example/lib/bar_chart/samples/bar_chart_sample5.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ class BarChartSample5State extends State<BarChartSample5> {
break;
default:
return Container();
break;
}
return Center(child: Text(text, style: style));
}
Expand Down
7 changes: 3 additions & 4 deletions example/lib/line_chart/samples/line_chart_sample6.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,9 @@ class LineChartSample6 extends StatelessWidget {
),
rightTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: true,
getTitles: rightTitleWidgets,
reservedSize: 30
),
showTitles: true,
getTitles: rightTitleWidgets,
reservedSize: 30),
),
bottomTitles: AxisTitles(
sideTitles: SideTitles(showTitles: false),
Expand Down
20 changes: 8 additions & 12 deletions lib/src/chart/base/axis_chart/axis_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:fl_chart/fl_chart.dart';
import 'package:fl_chart/src/chart/base/axis_chart/axis_chart_painter.dart';
import 'package:fl_chart/src/chart/base/base_chart/base_chart_data.dart';
import 'package:fl_chart/src/utils/lerp.dart';
import 'package:fl_chart/src/utils/utils.dart';
import 'package:flutter/material.dart';

/// This is the base class for axis base charts data
Expand Down Expand Up @@ -83,7 +82,6 @@ abstract class AxisChartData extends BaseChartData with EquatableMixin {

/// Holds data for showing label values on axis numbers
class SideTitles with EquatableMixin {

/// Determines showing or hiding this side titles
final bool showTitles;

Expand Down Expand Up @@ -156,16 +154,15 @@ class SideTitles with EquatableMixin {
/// Used for equality check, see [EquatableMixin].
@override
List<Object?> get props => [
showTitles,
getTitles,
reservedSize,
interval,
];
showTitles,
getTitles,
reservedSize,
interval,
];
}

/// Holds data for showing each side titles (left, top, right, bottom)
class AxisTitles with EquatableMixin {

/// Determines the size of [axisName]
final double axisNameSize;

Expand Down Expand Up @@ -201,8 +198,7 @@ class AxisTitles with EquatableMixin {
static AxisTitles lerp(AxisTitles a, AxisTitles b, double t) {
return AxisTitles(
axisName: b.axisName,
axisNameSize:
lerpDouble(a.axisNameSize, b.axisNameSize, t),
axisNameSize: lerpDouble(a.axisNameSize, b.axisNameSize, t),
sideTitles: SideTitles.lerp(a.sideTitles, b.sideTitles, t),
);
}
Expand All @@ -211,12 +207,12 @@ class AxisTitles with EquatableMixin {
/// and replaces provided values.
AxisTitles copyWith({
Widget? axisName,
double? axisNameReservedSize,
double? axisNameSize,
SideTitles? sideTitles,
}) {
return AxisTitles(
axisName: axisName ?? this.axisName,
axisNameSize: axisNameReservedSize ?? this.axisNameSize,
axisNameSize: axisNameSize ?? this.axisNameSize,
sideTitles: sideTitles ?? this.sideTitles,
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/chart/base/axis_chart/axis_chart_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class AxisChartHelper {
final lastPosition = initialValue + (count * interval);
final lastPositionOverlapsWithMax = lastPosition == max;
final end =
!maxIncluded && lastPositionOverlapsWithMax ? max - interval : max;
!maxIncluded && lastPositionOverlapsWithMax ? max - interval : max;

List<double> results = [];
final epsilon = interval / 100000;
Expand Down
18 changes: 3 additions & 15 deletions lib/src/chart/base/axis_chart/side_titles/side_titles_flex.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'dart:math' as math;

/// Inspired from [Flex]
class SideTitlesFlex extends MultiChildRenderObjectWidget {
SideTitlesFlex({
Key? key,
Expand Down Expand Up @@ -86,8 +87,6 @@ class AxisSideTitlesRenderFlex extends RenderBox
}
}

double _overflow = 0;

@override
void setupParentData(RenderBox child) {
if (child.parentData is! FlexParentData) {
Expand Down Expand Up @@ -264,7 +263,6 @@ class AxisSideTitlesRenderFlex extends RenderBox
constraints: constraints,
);

final double allocatedSize = sizes.allocatedSize;
double actualSize = sizes.mainSize;
double crossSize = sizes.crossSize;

Expand All @@ -281,18 +279,8 @@ class AxisSideTitlesRenderFlex extends RenderBox
crossSize = size.width;
break;
}
final double actualSizeDelta = actualSize - allocatedSize;
_overflow = math.max(0.0, -actualSizeDelta);
final double remainingSpace = math.max(0.0, actualSizeDelta);
late final double leadingSpace;
late final double betweenSpace;

// spaceBetween
leadingSpace = 0.0;
betweenSpace = childCount > 1 ? remainingSpace / (childCount - 1) : 0.0;

// Position elements
double childMainPosition = leadingSpace;
RenderBox? child = firstChild;
int counter = 0;
while (child != null) {
Expand All @@ -303,8 +291,8 @@ class AxisSideTitlesRenderFlex extends RenderBox

// Stretch
childCrossPosition = 0.0;
final childMainPosition = metaData.axisPixelLocation -
(_getMainSize(child.size) / 2);
final childMainPosition =
metaData.axisPixelLocation - (_getMainSize(child.size) / 2);
switch (_direction) {
case Axis.horizontal:
childParentData.offset =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ class SideTitlesWidget extends StatelessWidget {
final interval = sideTitles.interval ??
Utils().getEfficientInterval(
axisViewSize,
axisMax - axisMin,);
axisMax - axisMin,
);
if (isHorizontal && axisChartData is BarChartData) {
final barChartData = axisChartData as BarChartData;
final xLocations = barChartData.calculateGroupsX(axisViewSize);
Expand All @@ -147,7 +148,8 @@ class SideTitlesWidget extends StatelessWidget {
max: axisMax,
baseLine: axisBaseLine,
interval: interval,
).map((axisValue) {
)
.map((axisValue) {
var portion = (axisValue - axisMin) / (axisMax - axisMin);
if (isVertical) {
portion = 1 - portion;
Expand Down
31 changes: 11 additions & 20 deletions lib/src/chart/base/base_chart/base_chart_data.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// coverage:ignore-file
import 'dart:core';
import 'dart:core';

import 'package:equatable/equatable.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:fl_chart/src/utils/utils.dart';
import 'package:flutter/material.dart';

import 'base_chart_painter.dart';
Expand All @@ -25,16 +23,14 @@ abstract class BaseChartData with EquatableMixin {
BaseChartData({
FlBorderData? borderData,
required FlTouchData touchData,
})
: borderData = borderData ?? FlBorderData(),
}) : borderData = borderData ?? FlBorderData(),
touchData = touchData;

BaseChartData lerp(BaseChartData a, BaseChartData b, double t);

/// Used for equality check, see [EquatableMixin].
@override
List<Object?> get props =>
[
List<Object?> get props => [
borderData,
touchData,
];
Expand All @@ -50,8 +46,7 @@ class FlBorderData with EquatableMixin {
FlBorderData({
bool? show,
Border? border,
})
: show = show ?? true,
}) : show = show ?? true,
border = border ??
Border.all(
color: Colors.black,
Expand Down Expand Up @@ -81,8 +76,7 @@ class FlBorderData with EquatableMixin {

/// Used for equality check, see [EquatableMixin].
@override
List<Object?> get props =>
[
List<Object?> get props => [
show,
border,
];
Expand All @@ -108,17 +102,17 @@ abstract class FlTouchData<R extends BaseTouchResponse> with EquatableMixin {
final MouseCursorResolver<R>? mouseCursorResolver;

/// You can disable or enable the touch system using [enabled] flag,
FlTouchData(bool enabled,
BaseTouchCallback<R>? touchCallback,
MouseCursorResolver<R>? mouseCursorResolver,)
: enabled = enabled,
FlTouchData(
bool enabled,
BaseTouchCallback<R>? touchCallback,
MouseCursorResolver<R>? mouseCursorResolver,
) : enabled = enabled,
touchCallback = touchCallback,
mouseCursorResolver = mouseCursorResolver;

/// Used for equality check, see [EquatableMixin].
@override
List<Object?> get props =>
[
List<Object?> get props => [
enabled,
touchCallback,
mouseCursorResolver,
Expand Down Expand Up @@ -186,10 +180,7 @@ class TitleMeta {
final SideTitles sideTitles;
final String formattedValue;

TitleMeta(this.min,
this.max,
this.appliedInterval,
this.sideTitles,
TitleMeta(this.min, this.max, this.appliedInterval, this.sideTitles,
this.formattedValue);
}

Expand Down
3 changes: 2 additions & 1 deletion lib/src/chart/scatter_chart/scatter_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class _ScatterChartState extends AnimatedWidgetBaseState<ScatterChart> {
return AxisChartScaffoldWidget(
data: showingData,
chart: ScatterChartLeaf(
data: _withTouchedIndicators(_scatterChartDataTween!.evaluate(animation)),
data:
_withTouchedIndicators(_scatterChartDataTween!.evaluate(animation)),
targetData: _withTouchedIndicators(showingData),
),
);
Expand Down
11 changes: 6 additions & 5 deletions lib/src/chart/scatter_chart/scatter_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,11 @@ class ScatterChartData extends AxisChartData with EquatableMixin {
@override
List<Object?> get props => [
scatterSpots,
titlesData,
scatterTouchData,
showingTooltipIndicators,
gridData,
touchData,
borderData,
clipData,
backgroundColor,
titlesData,
rangeAnnotations,
minX,
maxX,
baselineX,
Expand All @@ -173,6 +170,10 @@ class ScatterChartData extends AxisChartData with EquatableMixin {
baselineY,
rangeAnnotations,
scatterLabelSettings,
clipData,
backgroundColor,
borderData,
touchData,
];
}

Expand Down
12 changes: 6 additions & 6 deletions lib/src/extensions/bar_chart_data_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:fl_chart/src/chart/bar_chart/bar_chart_data.dart';

extension BarChartDataExtension on BarChartData {
List<double> calculateGroupsX(double viewWidth) {
assert(barGroups.isNotEmpty);
final groupsX = List.filled(barGroups.length, 0.0, growable: false);
switch (alignment) {
case BarChartAlignment.start:
Expand All @@ -23,15 +24,14 @@ extension BarChartDataExtension on BarChartData {

case BarChartAlignment.center:
var sumWidth =
barGroups.map((group) => group.width).reduce((a, b) => a + b);
barGroups.map((group) => group.width).reduce((a, b) => a + b);
sumWidth += groupsSpace * (barGroups.length - 1);
final horizontalMargin = (viewWidth - sumWidth) / 2;

var tempX = 0.0;
for (var i = 0; i < barGroups.length; i++) {
final group = barGroups[i];
groupsX[i] =
horizontalMargin + tempX + group.width / 2;
groupsX[i] = horizontalMargin + tempX + group.width / 2;

final groupSpace = i == barGroups.length - 1 ? 0 : groupsSpace;
tempX += group.width + groupSpace;
Expand All @@ -40,7 +40,7 @@ extension BarChartDataExtension on BarChartData {

case BarChartAlignment.spaceBetween:
final sumWidth =
barGroups.map((group) => group.width).reduce((a, b) => a + b);
barGroups.map((group) => group.width).reduce((a, b) => a + b);
final spaceAvailable = viewWidth - sumWidth;
final eachSpace = spaceAvailable / (barGroups.length - 1);

Expand All @@ -57,7 +57,7 @@ extension BarChartDataExtension on BarChartData {

case BarChartAlignment.spaceAround:
final sumWidth =
barGroups.map((group) => group.width).reduce((a, b) => a + b);
barGroups.map((group) => group.width).reduce((a, b) => a + b);
final spaceAvailable = viewWidth - sumWidth;
final eachSpace = spaceAvailable / (barGroups.length * 2);

Expand All @@ -73,7 +73,7 @@ extension BarChartDataExtension on BarChartData {

case BarChartAlignment.spaceEvenly:
final sumWidth =
barGroups.map((group) => group.width).reduce((a, b) => a + b);
barGroups.map((group) => group.width).reduce((a, b) => a + b);
final spaceAvailable = viewWidth - sumWidth;
final eachSpace = spaceAvailable / (barGroups.length + 1);

Expand Down
22 changes: 11 additions & 11 deletions lib/src/extensions/edge_insets_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import 'package:flutter/widgets.dart';

extension EdgeInsetsExtension on EdgeInsets {
EdgeInsets get onlyTopBottom => EdgeInsets.only(
left: 0,
top: top,
right: 0,
bottom: bottom,
);
left: 0,
top: top,
right: 0,
bottom: bottom,
);

EdgeInsets get onlyLeftRight => EdgeInsets.only(
left: left,
top: 0,
right: right,
bottom: 0,
);
}
left: left,
top: 0,
right: right,
bottom: 0,
);
}
Loading

0 comments on commit b368b82

Please sign in to comment.