Skip to content

Commit

Permalink
Migrate line_chart_painter.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
imaNNeo committed Mar 3, 2021
1 parent 507557f commit e4959cb
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 110 deletions.
6 changes: 3 additions & 3 deletions lib/src/chart/line_chart/line_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ Color _defaultGetDotColor(FlSpot _, double xPercentage, LineChartBarData bar) {
} else if (bar.colors.length == 1) {
return bar.colors[0];
} else {
return lerpGradient(bar.colors, bar.getColorStops(), xPercentage / 100);
return lerpGradient(bar.colors, bar.getSafeColorStops(), xPercentage / 100);
}
}

Expand All @@ -716,7 +716,7 @@ Color _defaultGetDotStrokeColor(FlSpot spot, double xPercentage, LineChartBarDat
} else if (bar.colors.length == 1) {
color = bar.colors[0];
} else {
color = lerpGradient(bar.colors, bar.getColorStops(), xPercentage / 100);
color = lerpGradient(bar.colors, bar.getSafeColorStops(), xPercentage / 100);
}
return color.darken();
}
Expand Down Expand Up @@ -1502,7 +1502,7 @@ class LineTouchTooltipData with EquatableMixin {
/// then you should and pass your custom [LineTooltipItem] list
/// (length should be equal to the [touchedSpots.length]),
/// to show inside the tooltip popup.
typedef GetLineTooltipItems = List<LineTooltipItem> Function(List<LineBarSpot> touchedSpots);
typedef GetLineTooltipItems = List<LineTooltipItem?> Function(List<LineBarSpot> touchedSpots);

/// Default implementation for [LineTouchTooltipData.getTooltipItems].
List<LineTooltipItem> defaultLineTooltipItem(List<LineBarSpot> touchedSpots) {
Expand Down
92 changes: 69 additions & 23 deletions lib/src/chart/line_chart/line_chart_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,6 @@ class LineChartHelper {

}

extension LineChartDataExtension on LineChartBarData {

/// Returns colorStops
///
/// if [colorStops] provided, returns it directly,
/// Otherwise we calculate it using colors list
List<double> getColorStops() {
var stops = <double>[];
if (colorStops == null || colorStops!.length != colors.length) {
/// provided colorStops is invalid and we calculate it here
colors.asMap().forEach((index, color) {
final percent = 1.0 / colors.length;
stops.add(percent * index);
});
} else {
stops = colorStops!;
}
return stops;
}

}


/// Holds minX, maxX, minY, and maxY for use in [LineChartData]
class LineChartMinMaxAxisValues with EquatableMixin {
final double minX;
Expand Down Expand Up @@ -126,4 +103,73 @@ class LineChartMinMaxAxisValues with EquatableMixin {
readFromCache: readFromCache ?? this.readFromCache,
);
}
}

/// Extensions on [LineChartBarData]
extension LineChartDataExtension on LineChartBarData {

/// Returns colorStops
///
/// if [colorStops] provided, returns it directly,
/// Otherwise we calculate it using colors list
List<double> getSafeColorStops() {
var stops = <double>[];
if (colorStops == null || colorStops!.length != colors.length) {
/// provided colorStops is invalid and we calculate it here
colors.asMap().forEach((index, color) {
final percent = 1.0 / colors.length;
stops.add(percent * index);
});
} else {
stops = colorStops!;
}
return stops;
}

}

/// Extensions on [BarAreaData]
extension BarAreaDataExtension on BarAreaData {

/// Returns colorStops
///
/// if [colorStops] provided, returns it directly,
/// Otherwise we calculate it using colors list
List<double> getSafeColorStops() {
var stops = <double>[];
if (gradientColorStops == null || gradientColorStops!.length != colors.length) {
/// provided colorStops is invalid and we calculate it here
colors.asMap().forEach((index, color) {
final percent = 1.0 / colors.length;
stops.add(percent * index);
});
} else {
stops = gradientColorStops!;
}
return stops;
}

}

/// Extensions on [BetweenBarsData]
extension BetweenBarsDataExtension on BetweenBarsData {

/// Returns colorStops
///
/// if [colorStops] provided, returns it directly,
/// Otherwise we calculate it using colors list
List<double> getSafeColorStops() {
var stops = <double>[];
if (gradientColorStops == null || gradientColorStops!.length != colors.length) {
/// provided colorStops is invalid and we calculate it here
colors.asMap().forEach((index, color) {
final percent = 1.0 / colors.length;
stops.add(percent * index);
});
} else {
stops = gradientColorStops!;
}
return stops;
}

}
Loading

0 comments on commit e4959cb

Please sign in to comment.