Skip to content

Commit

Permalink
Feature: custom crosshair labels formatter (#305)
Browse files Browse the repository at this point in the history
* fix: denormalize value before invert.

* feat: add custom formatter.

---------

Co-authored-by: dan12411 <dan12411@fugle.tw>
  • Loading branch information
dan12411 and dan12411 authored Jul 26, 2024
1 parent adfe987 commit 44fbd01
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
26 changes: 21 additions & 5 deletions lib/src/guide/interaction/crosshair.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class CrosshairGuide {
this.labelStyles,
this.labelBackgroundStyles,
this.showLabel,
this.formatter,
this.followPointer,
this.layer,
this.mark,
Expand Down Expand Up @@ -62,6 +63,11 @@ class CrosshairGuide {
/// If null, a default `[false, false]` is set.
List<bool>? showLabel;

/// Convert the value to a [String] on the chart.
///
/// If null, a default [Scale.format, Scale.format] is used.
List<String? Function(dynamic)?>? formatter;

/// Whether the position for each dimension follows the pointer or stick to selected
/// points.
///
Expand Down Expand Up @@ -127,6 +133,7 @@ class CrosshairRenderOp extends Render {
final labelBackgroundStyles =
params['labelBackgroundStyles'] as List<PaintStyle?>;
final showLabel = params['showLabel'] as List<bool>;
final formatter = params['formatter'] as List<String? Function(dynamic)?>;
final followPointer = params['followPointer'] as List<bool>;
final scales = params['scales'] as Map<String, ScaleConv>;
final size = params['size'] as Size;
Expand Down Expand Up @@ -210,7 +217,10 @@ class CrosshairRenderOp extends Render {
if (showLabel[0] && !canvasCross.dx.isNaN && labelStyleX != null) {
final fieldX = coord.transposed ? fields[1] : fields[0];
final scaleX = scales[fieldX];
final text = scaleX?.format(scaleX.invert(cross.dx)) ?? '';
final denormalize = scaleX?.denormalize(cross.dx) ?? -1;
final invert = scaleX?.invert(denormalize);
final text =
formatter[0]?.call(invert) ?? scaleX?.format(invert) ?? '';
final rect = _getLabelBlock(text: text, style: labelStyleX);

double posX = canvasCrossX;
Expand Down Expand Up @@ -256,7 +266,10 @@ class CrosshairRenderOp extends Render {
if (showLabel[1] && !canvasCross.dy.isNaN && labelStyleY != null) {
final fieldY = coord.transposed ? fields[0] : fields[1];
final scaleY = scales[fieldY];
final text = scaleY?.format(scaleY.invert(cross.dy)) ?? '';
final denormalize = scaleY?.denormalize(cross.dy) ?? -1;
final invert = scaleY?.invert(denormalize);
final text =
formatter[1]?.call(invert) ?? scaleY?.format(invert) ?? '';
final rect = _getLabelBlock(text: text, style: labelStyleY);

double posY = canvasCrossY;
Expand Down Expand Up @@ -297,7 +310,8 @@ class CrosshairRenderOp extends Render {
if (showLabel[0] && labelStyleX != null) {
final fieldX = coord.transposed ? fields[2] : fields[0];
final scaleX = scales[fieldX];
final text = scaleX?.format(tuple[fieldX]) ?? '';
final value = tuple[fieldX];
final text = formatter[0]?.call(value) ?? scaleX?.format(value) ?? '';
final diagonal = _getLabelDiagonal(text: text, style: labelStyleX);

final label = LabelElement(
Expand Down Expand Up @@ -330,8 +344,10 @@ class CrosshairRenderOp extends Render {
if (showLabel[1] && labelStyleY != null) {
final fieldY = coord.transposed ? fields[0] : fields[2];
final scaleY = scales[fieldY];
final value = scaleY?.invert(abstractRadius);
final text = scaleY?.format(value) ?? '';
final denormalize = scaleY?.denormalize(abstractRadius) ?? 0;
final invert = scaleY?.invert(denormalize);
final text =
formatter[0]?.call(invert) ?? scaleY?.format(value) ?? '';
final rect = _getLabelBlock(text: text, style: labelStyleY);

final label = LabelElement(
Expand Down
1 change: 1 addition & 0 deletions lib/src/parse/parse.dart
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ void parse<D>(
PaintStyle(fillColor: const Color(0xff000000)),
],
'showLabel': showLabel,
'formatter': crosshairSpec.formatter ?? [null, null],
'followPointer': crosshairSpec.followPointer ?? [false, false],
'scales': scales,
'size': size,
Expand Down

0 comments on commit 44fbd01

Please sign in to comment.