Skip to content

Commit

Permalink
Update the dart documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
imaNNeo committed Jan 13, 2025
1 parent 66b5800 commit 5b160bd
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 3 deletions.
27 changes: 26 additions & 1 deletion lib/src/chart/bar_chart/bar_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ class BarChartData extends AxisChartData with EquatableMixin {
/// Handles touch behaviors and responses.
final BarTouchData barTouchData;

/// Holds data for showing error indicators on the spots in this line.
/// Holds data for showing error (threshold) indicators on the spots in
/// the different [BarChartGroupData.barRods]
final FlErrorIndicatorData<BarChartSpotErrorRangeCallbackInput>
errorIndicatorData;

Expand Down Expand Up @@ -356,6 +357,13 @@ class BarChartRodData with EquatableMixin {
/// [BarChart] renders rods vertically from [fromY] to [toY].
final double toY;

/// If the data has error range/threshold, it will be rendered
/// with this error range. So you can provide the
/// [FlErrorRange.lowerBy] and [FlErrorRange.upperBy] that is relative to
/// the [toY] property.
///
/// If you want to customize the visual representation of the error range,
/// you can use [BarChartData.errorIndicatorData] to customize the error range
final FlErrorRange? toYErrorRange;

/// If provided, this [BarChartRodData] draws with this [color]
Expand Down Expand Up @@ -950,6 +958,16 @@ class BarTouchedSpot extends TouchedSpot with EquatableMixin {
];
}

/// It is the input of the [GetSpotRangeErrorPainter] callback in
/// the [BarChartData.errorIndicatorData]
///
/// As you see, we have some properties that are related to each individual
/// rod (the object we show the error range on top of it).
/// For example,
/// [group] is the group that the rod belongs to,
/// [groupIndex] is the index of the group,
/// [rod] is the rod that the error range belongs to,
/// [barRodIndex] is the index of the rod in the group.
class BarChartSpotErrorRangeCallbackInput
extends FlSpotErrorRangeCallbackInput {
BarChartSpotErrorRangeCallbackInput({
Expand All @@ -959,9 +977,16 @@ class BarChartSpotErrorRangeCallbackInput
required this.barRodIndex,
});

// The group that the rod belongs to
final BarChartGroupData group;

// The index of the group that the rod belongs to
final int groupIndex;

// The rod that the error range belongs to
final BarChartRodData rod;

// The index of the rod in the group
final int barRodIndex;

@override
Expand Down
96 changes: 95 additions & 1 deletion lib/src/chart/base/axis_chart/axis_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -571,21 +571,34 @@ class FlSpot {
x.hashCode ^ y.hashCode ^ xError.hashCode ^ yError.hashCode;
}

/// Represents a range of values that can be used to show error bars/threshold
///
/// [lowerBy] and [upperBy] are the values that will be added and subtracted
/// from the main value. It means that they should be non-negative.
/// Also it means that they are relative to the main value.
class FlErrorRange with EquatableMixin {
const FlErrorRange({
required this.lowerBy,
required this.upperBy,
}) : assert(lowerBy >= 0, 'lowerBy must be non-negative'),
assert(upperBy >= 0, 'upperBy must be non-negative');

/// Creates a symmetric error range.
/// It sets [lowerBy] and [upperBy] to the same [value].
const FlErrorRange.symmetric(double value)
: lowerBy = value,
upperBy = value,
assert(value >= 0, 'value must be non-negative');

/// determines the lower bound of the error range, it will be subtracted from
/// the main value. So it is non-negative and it is relative to the main value
final double lowerBy;

/// determines the lower bound of the error range, it will be added to
/// the main value. So it is non-negative and it is relative to the main value
final double upperBy;

/// Lerps a [FlErrorRange] based on [t] value
static FlErrorRange? lerp(FlErrorRange? a, FlErrorRange? b, double t) {
if (a != null && b != null) {
return FlErrorRange(
Expand Down Expand Up @@ -1685,6 +1698,25 @@ class FlDotCrossPainter extends FlDotPainter {
];
}

/// Holds the information about the error range of a spot
///
/// We support horizontal and vertical error range/indicator for our axis based
/// charts such as [LineChart], [BarChart] and [PieChart]
///
/// For example, in [LineChart] you can add [FlSpot.xError] and [FlSpot.yError]
/// in your data points, so we can draw error indicators for them.
/// And it works relative to the point that you are setting the error range
///
/// For [BarChart], you can set the [BarChartRodData.toYErrorRange] to have
/// vertical error range for each bar. (relative to [BarChartRodData.toY] value)
///
/// [show] is tru by default, it means that we show
/// the error indicator lines (if you provide them in [FlSpot]s)
///
/// [painter] is a callback that allows you to return a
/// [FlSpotErrorRangePainter] per each data point which is responsible for
/// drawing the error indicator. You can use the default [FlSimpleErrorPainter]
/// or create your own by extending our abstract [FlSpotErrorRangePainter]
class FlErrorIndicatorData<T extends FlSpotErrorRangeCallbackInput>
with EquatableMixin {
const FlErrorIndicatorData({
Expand Down Expand Up @@ -1718,20 +1750,49 @@ class FlErrorIndicatorData<T extends FlSpotErrorRangeCallbackInput>
}

/// A callback that allows you to return a [FlSpotErrorRangePainter] based on
/// the provided specific data point (for example [FlSpot] in line chart)
/// the provided specific data point (for example [FlSpot] in [LineChart])
///
/// So [input] is different based on the chart type,
/// for example in [LineChart] it will be [LineChartSpotErrorRangeCallbackInput]
typedef GetSpotRangeErrorPainter<T extends FlSpotErrorRangeCallbackInput>
= FlSpotErrorRangePainter Function(
T input,
);

/// The default [GetSpotRangeErrorPainter] for [FlErrorIndicatorData],
/// it draws a simple and typical error indicator using [FlSimpleErrorPainter]
FlSpotErrorRangePainter _defaultGetSpotRangeErrorPainter(
FlSpotErrorRangeCallbackInput input,
) =>
FlSimpleErrorPainter();

/// The abstract painter that is responsible for drawing the error range of
/// a point in our axis based charts such as [LineChart] and [BarChart]
///
/// It has a [draw] method that you should override to draw the error range
/// as you like
///
/// The default implementation is [FlSpotErrorRangePainter]. It is a simple and
/// common error indicator painter.
///
/// You can see how does it look in the [example app](https://app.flchart.dev/)
abstract class FlSpotErrorRangePainter with EquatableMixin {
const FlSpotErrorRangePainter();

/// Draws the error range of a point in our axis based charts
///
/// [canvas] is the canvas that you should draw on it
/// [offsetInCanvas] is the absolute position/offset of the point in
/// the canvas that you can use it as your center point
/// [origin] is the relative point point that you should draw
/// the error range on it (it is based on the chart values)
/// [errorRelativeRect] is the relative rect that you should draw the error,
/// it is absolute and you can shift it with [offsetInCanvas] to draw your
/// shape inside it.
/// [axisChartData] is the axis chart data that you can use it to get more
/// information about the chart
///
/// You can take a look at our default implementation [FlSimpleErrorPainter]
void draw(
Canvas canvas,
Offset offsetInCanvas,
Expand All @@ -1741,6 +1802,19 @@ abstract class FlSpotErrorRangePainter with EquatableMixin {
);
}

/// The default implementation of [FlSpotErrorRangePainter]
///
/// It draws a simple and common error indicator for the error range of a point
/// in our axis based charts such as [LineChart] and [BarChart]
///
/// You can see how does it look in the [example app](https://app.flchart.dev/)
///
/// You can customize the lines using [lineColor], [lineWidth], [capLength],
///
/// You can customize the text using [showErrorTexts], [errorTextStyle]
/// and [errorTextDirection]
///
/// You can customize the alignment of the error lines using [crossAlignment]
class FlSimpleErrorPainter extends FlSpotErrorRangePainter with EquatableMixin {
FlSimpleErrorPainter({
this.lineColor = Colors.white,
Expand All @@ -1764,12 +1838,26 @@ class FlSimpleErrorPainter extends FlSpotErrorRangePainter with EquatableMixin {
);
}

/// The color of the error lines
final Color lineColor;

/// The thickness of the error lines
final double lineWidth;

/// The length of the cap of the error lines
final double capLength;

/// The alignment of the error lines,
/// it should be between -1 (start) and 1 (end)
final double crossAlignment;

/// Determines showing the error texts or not
final bool showErrorTexts;

/// The style of the error texts
final TextStyle errorTextStyle;

/// The direction of the error texts
final TextDirection errorTextDirection;

late final Paint _linePaint;
Expand Down Expand Up @@ -1965,4 +2053,10 @@ class FlSimpleErrorPainter extends FlSpotErrorRangePainter with EquatableMixin {
];
}

/// The abstract class that is used as the input of
/// the [GetSpotRangeErrorPainter] callback.
///
/// So as you know, we have this feature in our axis-based charts and each chart
/// has its own input type, for example in [LineChart]
/// it is [LineChartSpotErrorRangeCallbackInput] (which contains the [FlSpot])
abstract class FlSpotErrorRangeCallbackInput with EquatableMixin {}
6 changes: 6 additions & 0 deletions lib/src/chart/line_chart/line_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,12 @@ class LineTouchResponse extends BaseTouchResponse {
);
}

/// It is the input of the [GetSpotRangeErrorPainter] callback in
/// the [LineChartData.errorIndicatorData]
///
/// So it contains the information about the spot, and the bar that the spot
/// is in. The callback should return a [FlSpotErrorRangePainter] that will draw
/// the error bars
class LineChartSpotErrorRangeCallbackInput
extends FlSpotErrorRangeCallbackInput {
LineChartSpotErrorRangeCallbackInput({
Expand Down
8 changes: 7 additions & 1 deletion lib/src/chart/scatter_chart/scatter_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class ScatterChartData extends AxisChartData with EquatableMixin {

final ScatterLabelSettings scatterLabelSettings;

/// Holds data for showing error indicators on the spots in this line.
/// Holds data for showing error indicators on the [scatterSpots]
final FlErrorIndicatorData<ScatterChartSpotErrorRangeCallbackInput>
errorIndicatorData;

Expand Down Expand Up @@ -761,6 +761,12 @@ class ScatterLabelSettings with EquatableMixin {
];
}

/// It is the input of the [GetSpotRangeErrorPainter] callback in
/// the [ScatterChartData.errorIndicatorData]
///
/// It contains the [spot] and [spotIndex] that the error range
/// should be drawn for.
/// It works based on the [ScatterSpot.xError] and [ScatterSpot.yError] values.
class ScatterChartSpotErrorRangeCallbackInput
extends FlSpotErrorRangeCallbackInput {
ScatterChartSpotErrorRangeCallbackInput({
Expand Down
1 change: 1 addition & 0 deletions lib/src/utils/canvas_wrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class CanvasWrapper {
painter.draw(canvas, spot, offset);
}

/// Paints a error indicator using the [painter]
void drawErrorIndicator(

Check warning on line 123 in lib/src/utils/canvas_wrapper.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/utils/canvas_wrapper.dart#L123

Added line #L123 was not covered by tests
FlSpotErrorRangePainter painter,
FlSpot origin,
Expand Down

0 comments on commit 5b160bd

Please sign in to comment.