Skip to content

Commit

Permalink
feat: add custom formatter.
Browse files Browse the repository at this point in the history
  • Loading branch information
dan12411 committed Jul 25, 2024
1 parent efa1ab1 commit 6ea8173
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
23 changes: 18 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 @@ -211,7 +218,9 @@ class CrosshairRenderOp extends Render {
final fieldX = coord.transposed ? fields[1] : fields[0];
final scaleX = scales[fieldX];
final denormalize = scaleX?.denormalize(cross.dx) ?? -1;
final text = scaleX?.format(scaleX.invert(denormalize)) ?? '';
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 @@ -258,7 +267,9 @@ class CrosshairRenderOp extends Render {
final fieldY = coord.transposed ? fields[0] : fields[1];
final scaleY = scales[fieldY];
final denormalize = scaleY?.denormalize(cross.dy) ?? -1;
final text = scaleY?.format(scaleY.invert(denormalize)) ?? '';
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 @@ -299,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 @@ -333,8 +345,9 @@ class CrosshairRenderOp extends Render {
final fieldY = coord.transposed ? fields[0] : fields[2];
final scaleY = scales[fieldY];
final denormalize = scaleY?.denormalize(abstractRadius) ?? 0;
final value = scaleY?.invert(denormalize);
final text = scaleY?.format(value) ?? '';
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 6ea8173

Please sign in to comment.