-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add FlDotLineSegmentPainter for Enhanced Chart Customization #1544
Open
JaredEzz
wants to merge
7
commits into
imaNNeo:main
Choose a base branch
from
JaredEzz:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e736269
add FlDotLineSegmentPainter
JaredEzz 3ffb17f
add FlDotLineSegmentPainter
JaredEzz 250cdbf
Merge remote-tracking branch 'origin/main'
JaredEzz 76072c9
Merge branch 'imaNNeo:main' into main
JaredEzz 898b29a
Merge branch 'main' into main
JaredEzz 5c33e55
run dart format
JaredEzz 704486e
Merge branch 'main' into main
JaredEzz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1636,3 +1636,101 @@ class FlDotCrossPainter extends FlDotPainter { | |
width, | ||
]; | ||
} | ||
|
||
/// This class is an implementation of a [FlDotPainter] that draws | ||
/// a line segment with a value label above it. | ||
class FlDotLineSegmentPainter extends FlDotPainter { | ||
/// Constructs a [FlDotLineSegmentPainter]. | ||
FlDotLineSegmentPainter({ | ||
required this.width, | ||
required this.height, | ||
required this.value, | ||
required this.textColor, | ||
this.color = Colors.blue, | ||
this.showText = true, | ||
}); | ||
|
||
/// The color of the line segment. | ||
final Color color; | ||
|
||
/// The width of the line segment. | ||
final double width; | ||
|
||
/// The height of the line segment. | ||
final double height; | ||
|
||
/// The numerical value to display above the line segment. | ||
final double value; | ||
|
||
/// The color of the text displaying the value. | ||
final Color textColor; | ||
|
||
/// Whether to show the text label. | ||
final bool showText; | ||
|
||
/// Draws the line segment and the value label on the canvas. | ||
@override | ||
void draw(Canvas canvas, FlSpot spot, Offset offsetInCanvas) { | ||
// Draw the line segment | ||
final paint = Paint() | ||
..color = color | ||
..style = PaintingStyle.stroke | ||
..strokeWidth = width; | ||
canvas.drawLine( | ||
Offset(offsetInCanvas.dx, offsetInCanvas.dy - height / 2), | ||
Offset(offsetInCanvas.dx, offsetInCanvas.dy + height / 2), | ||
paint, | ||
); | ||
|
||
// Draw the value label | ||
if (showText) { | ||
final textSpan = TextSpan( | ||
text: value.toStringAsFixed(1), | ||
style: TextStyle(color: textColor), | ||
); | ||
final textPainter = TextPainter( | ||
text: textSpan, | ||
textDirection: TextDirection.ltr, | ||
)..layout(); | ||
textPainter.paint( | ||
canvas, | ||
Offset( | ||
offsetInCanvas.dx - textPainter.width / 2, | ||
offsetInCanvas.dy - height / 2 - textPainter.height - 5, | ||
), | ||
); | ||
} | ||
} | ||
|
||
/// Returns the size of the painter area. | ||
@override | ||
Size getSize(FlSpot spot) { | ||
return Size(width, height); | ||
} | ||
|
||
/// Properties used for the equality check. | ||
@override | ||
List<Object?> get props => [color, width, height, value, textColor]; | ||
|
||
/// Interpolates between two [FlDotLineSegmentPainter]s. | ||
@override | ||
FlDotPainter lerp(FlDotPainter a, FlDotPainter b, double t) { | ||
if (a is FlDotLineSegmentPainter && b is FlDotLineSegmentPainter) { | ||
return FlDotLineSegmentPainter( | ||
width: lerpDouble(a.width, b.width, t)!, | ||
height: lerpDouble(a.height, b.height, t)!, | ||
color: Color.lerp(a.color, b.color, t)!, | ||
value: lerpDouble(a.value, b.value, t)!, | ||
textColor: Color.lerp(a.textColor, b.textColor, t)!, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And also here |
||
); | ||
} else { | ||
throw Exception( | ||
'Cannot interpolate between different types of FlDotPainters', | ||
); | ||
} | ||
} | ||
|
||
/// The main color of the dot painter. | ||
@override | ||
Color get mainColor => color; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, it seems you forgot to add
showText
here