Skip to content

Commit

Permalink
Feature: add label and labelBackground on crosshair (#303)
Browse files Browse the repository at this point in the history
* feat: add label & labelBackground on crosshair (RectCoordConv).

* feat: add label & labelBackground on crosshair (PolarCoordConv).

* fix: remove unnecessary 1px spacing.

* fix: typo for label.

* fix: pin crosshair on region boundary.

* fix: deprecate flutter format sub-command in favor of dart format.

* chore: dart format --set-exit-if-changed .

---------

Co-authored-by: dan12411 <[email protected]>
  • Loading branch information
dan12411 and dan12411 authored Jul 18, 2024
1 parent 336f953 commit 616e0e9
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
run: flutter pub get

- name: Run formatter
run: flutter format --set-exit-if-changed .
run: dart format --set-exit-if-changed .

- name: Run analyzer
run: flutter analyze .
Expand Down
15 changes: 10 additions & 5 deletions example/lib/pages/animation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ class AnimationPageState extends State<AnimationPage> {
],
marks: [
IntervalMark(
transition: Transition(duration: const Duration(seconds: 1)),
transition:
Transition(duration: const Duration(seconds: 1)),
entrance: {MarkEntrance.y},
label: LabelEncode(
encoder: (tuple) => Label(tuple['sold'].toString())),
Expand Down Expand Up @@ -259,7 +260,8 @@ class AnimationPageState extends State<AnimationPage> {
CircleShape(),
SquareShape(),
]),
transition: Transition(duration: const Duration(seconds: 1)),
transition:
Transition(duration: const Duration(seconds: 1)),
)
],
axes: [
Expand Down Expand Up @@ -329,7 +331,8 @@ class AnimationPageState extends State<AnimationPage> {
color: ColorEncode(
variable: 'genre', values: Defaults.colors10),
modifiers: [StackModifier()],
transition: Transition(duration: const Duration(seconds: 2)),
transition:
Transition(duration: const Duration(seconds: 2)),
entrance: {MarkEntrance.y},
)
],
Expand Down Expand Up @@ -382,7 +385,8 @@ class AnimationPageState extends State<AnimationPage> {
Defaults.colors10.first.withAlpha(80),
Defaults.colors10.first.withAlpha(10),
])),
transition: Transition(duration: const Duration(seconds: 2)),
transition:
Transition(duration: const Duration(seconds: 2)),
entrance: {
MarkEntrance.x,
MarkEntrance.y,
Expand All @@ -392,7 +396,8 @@ class AnimationPageState extends State<AnimationPage> {
LineMark(
shape: ShapeEncode(value: BasicLineShape(smooth: true)),
size: SizeEncode(value: 0.5),
transition: Transition(duration: const Duration(seconds: 2)),
transition:
Transition(duration: const Duration(seconds: 2)),
entrance: {
MarkEntrance.x,
MarkEntrance.y,
Expand Down
7 changes: 6 additions & 1 deletion lib/graphic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,12 @@ export 'src/shape/util/style.dart' show getPaintStyle;

export 'src/graffiti/transition.dart' show Transition;
export 'src/graffiti/element/element.dart'
show MarkElement, ElementStyle, PaintStyle, PrimitiveElement, getBlockPaintPoint;
show
MarkElement,
ElementStyle,
PaintStyle,
PrimitiveElement,
getBlockPaintPoint;
export 'src/graffiti/element/arc.dart' show ArcElement;
export 'src/graffiti/element/circle.dart' show CircleElement;
export 'src/graffiti/element/group.dart' show GroupElement;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/graffiti/element/label.dart
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class LabelElement extends BlockElement<LabelStyle> {
Rect getBlock() {
const horizontalSpacing = 1;
final left = paintPoint.dx - horizontalSpacing;
final top = paintPoint.dy - 1;
final top = paintPoint.dy;
final width = _textSize.width + 2 * horizontalSpacing;
final height = _textSize.height;

Expand Down
12 changes: 8 additions & 4 deletions lib/src/guide/axis/axis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ typedef GridMapper = PaintStyle? Function(String? text, int index, int total);
/// Gets an axis labelBackground style form an axis value text.
///
/// [index] and [total] is current and total count of all ticks respectively.
typedef LabelBackgroundMapper = PaintStyle? Function(String? text, int index, int total);
typedef LabelBackgroundMapper = PaintStyle? Function(
String? text, int index, int total);

/// The specification of an axis.
///
Expand All @@ -80,7 +81,8 @@ class AxisGuide<V> {
}) : assert(isSingle([tickLine, tickLineMapper], allowNone: true)),
assert(isSingle([label, labelMapper], allowNone: true)),
assert(isSingle([grid, gridMapper], allowNone: true)),
assert(isSingle([labelBackground, labelBackgroundMapper], allowNone: true));
assert(isSingle([labelBackground, labelBackgroundMapper],
allowNone: true));

/// The dimension where this axis lies.
///
Expand Down Expand Up @@ -214,7 +216,8 @@ class TickInfo {
bool get haveLabel => label != null && text != null && text!.isNotEmpty;

/// Whether this tick has a labelBackground to render.
bool get haveLabelBackground => labelBackground != null && text != null && text!.isNotEmpty;
bool get haveLabelBackground =>
labelBackground != null && text != null && text!.isNotEmpty;
}

/// The operator to create tick informations.
Expand All @@ -234,7 +237,8 @@ class TickInfoOp extends Operator<List<TickInfo>> {
final grid = params['grid'] as PaintStyle?;
final gridMapper = params['gridMapper'] as GridMapper?;
final labelBackground = params['labelBackground'] as PaintStyle?;
final labelBackgroundMapper = params['labelBackgroundMapper'] as LabelBackgroundMapper?;
final labelBackgroundMapper =
params['labelBackgroundMapper'] as LabelBackgroundMapper?;

final scale = scales[variable]!;

Expand Down
6 changes: 3 additions & 3 deletions lib/src/guide/axis/horizontal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ List<MarkElement>? renderHorizontalAxis(
style: tick.tickLine!.style));
}
if (tick.haveLabel) {
final babel = LabelElement(
final label = LabelElement(
text: tick.text!,
anchor: Offset(x, y),
defaultAlign: flip ? Alignment.topCenter : Alignment.bottomCenter,
style: tick.label!);

if (tick.haveLabelBackground) {
rst.add(RectElement(
rect: babel.getBlock(),
rect: label.getBlock(),
style: tick.labelBackground!,
));
}

rst.add(babel);
rst.add(label);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/src/guide/axis/radial.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,20 @@ List<MarkElement>? renderRadialAxis(
final labelAlign = radialLabelAlign(featureOffset) * flipSign;
for (var index in labelAnchors.keys) {
final tick = ticks[index];
final babel = LabelElement(
final label = LabelElement(
text: tick.text!,
anchor: labelAnchors[index]!,
defaultAlign: labelAlign,
style: tick.label!);

if (tick.haveLabelBackground) {
rst.add(RectElement(
rect: babel.getBlock(),
rect: label.getBlock(),
style: tick.labelBackground!,
));
}

rst.add(babel);
rst.add(label);
}

return rst.isEmpty ? null : rst;
Expand Down
6 changes: 3 additions & 3 deletions lib/src/guide/axis/vertical.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ List<MarkElement>? renderVerticalAxis(
style: tick.tickLine!.style));
}
if (tick.haveLabel) {
final babel = LabelElement(
final label = LabelElement(
text: tick.text!,
anchor: Offset(x, y),
defaultAlign: flip ? Alignment.centerRight : Alignment.centerLeft,
style: tick.label!);

if (tick.haveLabelBackground) {
rst.add(RectElement(
rect: babel.getBlock(),
rect: label.getBlock(),
style: tick.labelBackground!,
));
}

rst.add(babel);
rst.add(label);
}
}
}
Expand Down
Loading

0 comments on commit 616e0e9

Please sign in to comment.