Skip to content

Add 'onXOffsetChanged' event #25

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/src/interactive_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:math';

import 'package:flutter/gestures.dart';
import 'package:flutter/widgets.dart';
import 'package:interactive_chart/src/x_axis_offset_details.dart';
import 'package:intl/intl.dart' as intl;

import 'candle_data.dart';
Expand Down Expand Up @@ -60,6 +61,11 @@ class InteractiveChart extends StatefulWidget {
/// This provides the width of a candlestick at the current zoom level.
final ValueChanged<double>? onCandleResize;

/// Optional event fired when the user moves the chart along the X axis.
///
/// Provides X-axis offset details.
final ValueChanged<XAxisOffsetDetails>? onXOffsetChanged;

const InteractiveChart({
Key? key,
required this.candles,
Expand All @@ -70,6 +76,7 @@ class InteractiveChart extends StatefulWidget {
this.overlayInfo,
this.onTap,
this.onCandleResize,
this.onXOffsetChanged,
}) : this.style = style ?? const ChartStyle(),
assert(candles.length >= 3,
"InteractiveChart requires 3 or more CandleData"),
Expand Down Expand Up @@ -252,7 +259,8 @@ class _InteractiveChartState extends State<InteractiveChart> {
final zoomAdjustment = (currCount - prevCount) * candleWidth;
final focalPointFactor = focalPoint.dx / w;
startOffset -= zoomAdjustment * focalPointFactor;
startOffset = startOffset.clamp(0, _getMaxStartOffset(w, candleWidth));
final maxStartOffset = _getMaxStartOffset(w, candleWidth);
startOffset = startOffset.clamp(0, maxStartOffset);
// Fire candle width resize event
if (candleWidth != _candleWidth) {
widget.onCandleResize?.call(candleWidth);
Expand All @@ -262,6 +270,13 @@ class _InteractiveChartState extends State<InteractiveChart> {
_candleWidth = candleWidth;
_startOffset = startOffset;
});

if (_prevStartOffset != startOffset) {
widget.onXOffsetChanged?.call(XAxisOffsetDetails(
offset: startOffset,
maxOffset: maxStartOffset,
));
}
}

_handleResize(double w) {
Expand Down
25 changes: 25 additions & 0 deletions lib/src/x_axis_offset_details.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class XAxisOffsetDetails {
XAxisOffsetDetails({
required this.offset,
required this.maxOffset,
});

final double offset;
final double maxOffset;

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is XAxisOffsetDetails &&
runtimeType == other.runtimeType &&
offset == other.offset &&
maxOffset == other.maxOffset;

@override
int get hashCode => offset.hashCode ^ maxOffset.hashCode;

@override
String toString() {
return 'XAxisOffsetDetails{offset: $offset, maxOffset: $maxOffset}';
}
}