-
Couldn't load subscription status.
- Fork 21
feat: Add adaptive interval feature #396
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
Draft
behnam-deriv
wants to merge
8
commits into
deriv-com:master
Choose a base branch
from
behnam-deriv:add-auto-interval-feature
base: master
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.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bb032fb
feate: add AutoIntervalWrapper
behnam-deriv 3f1fcd7
merge master into add-auto-interval-feature
behnam-deriv 0925836
chore: extract AnimatedSwitcher as a widget
behnam-deriv 0ea0992
fix: keep chart state when auto interval toggles on and off
behnam-deriv ff4f0c3
chore: adjust default auto interval ranges
behnam-deriv 7b60712
chore: add adaptive interval toggle and zoom in/out buttons in exampl…
behnam-deriv 84f87fb
chore: update docs
behnam-deriv 37652b4
chore: dart format
behnam-deriv 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| # Adaptive Interval (Auto-Interval) Feature | ||
|
|
||
| ## Overview | ||
|
|
||
| The **adaptive interval** (also called **auto-interval**) feature automatically adjusts the chart's time granularity (interval) based on the current zoom level. This ensures that the chart remains readable and usable at all zoom levels, without requiring the user to manually select the most appropriate interval. | ||
|
|
||
| When enabled, the chart will suggest and can switch to the most suitable granularity as the user zooms in or out, based on a configurable mapping between zoom levels and time intervals. | ||
|
|
||
| --- | ||
|
|
||
| ## Key Components | ||
|
|
||
| - **`AutoIntervalWrapper`**: A widget that observes zoom level changes and manages the logic for suggesting granularity changes. | ||
| - **`ZoomLevelObserver`**: An interface for components that need to react to zoom level changes. | ||
| - **`AutoIntervalZoomRange`**: A configuration class that maps each granularity (in milliseconds) to its optimal pixel range on the chart. | ||
| - **`ChartAxisConfig`**: Holds the configuration for enabling/disabling auto-interval and its zoom ranges. | ||
|
|
||
| --- | ||
|
|
||
| ## How It Works | ||
|
|
||
| 1. **Zoom Level Observation**: The chart's x-axis model notifies the `AutoIntervalWrapper` of zoom level changes (in milliseconds per pixel). | ||
| 2. **Optimal Granularity Calculation**: The wrapper calculates the optimal granularity for the current zoom level using the configured `AutoIntervalZoomRange` list. | ||
| 3. **Granularity Suggestion**: If a different granularity is optimal and hasn't already been suggested, the wrapper triggers the `onGranularityChangeRequested` callback. | ||
| 4. **Granularity Update**: The parent widget or chart controller can then update the chart's granularity, which will fetch and display data at the new interval. | ||
|
|
||
| --- | ||
|
|
||
| ## Configuration | ||
|
|
||
| - **Enable/Disable**: Set `autoIntervalEnabled` in `ChartAxisConfig` to `true` or `false`. | ||
| - **Customize Ranges**: Modify `autoIntervalZoomRanges` in `ChartAxisConfig` to change which granularities are used at which zoom levels. | ||
| - **Default Ranges**: The default configuration covers common trading timeframes (1m, 2m, 5m, etc.), each with a pixel range for when it should be used. | ||
|
|
||
| Example: | ||
| ```dart | ||
| ChartAxisConfig( | ||
| autoIntervalEnabled: true, | ||
| autoIntervalZoomRanges: [ | ||
| AutoIntervalZoomRange( | ||
| granularity: 60000, // 1 minute | ||
| minPixelsPerInterval: 12, | ||
| maxPixelsPerInterval: 24, | ||
| ), | ||
| // ... more ranges ... | ||
| ], | ||
| ) | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Usage Example | ||
|
|
||
| Wrap your chart widget with `AutoIntervalWrapper`: | ||
|
|
||
| ```dart | ||
| AutoIntervalWrapper( | ||
| enabled: true, | ||
| granularity: currentGranularity, // in milliseconds | ||
| zoomRanges: autoIntervalRanges, // List<AutoIntervalZoomRange> | ||
| onGranularityChangeRequested: (newGranularity) { | ||
| // Update chart granularity and fetch new data | ||
| }, | ||
| child: ... // your chart widget | ||
| ) | ||
| ``` | ||
|
|
||
| - The `onGranularityChangeRequested` callback is called when the wrapper suggests a new optimal granularity. | ||
| - You are responsible for updating the chart's granularity and fetching new data as needed. | ||
|
|
||
| --- | ||
|
|
||
| ## Internal Logic | ||
|
|
||
| - The wrapper listens for zoom level changes (ms per pixel) via the `ZoomLevelObserver` interface. | ||
| - For each zoom event, it calculates the number of pixels each interval (candle) would occupy at the current zoom. | ||
| - It checks all configured `AutoIntervalZoomRange` entries to find which range the current zoom fits into, and selects the one closest to its optimal pixel width. | ||
| - If the optimal granularity is different from the current one and hasn't already been suggested, it triggers the callback. | ||
|
|
||
| --- | ||
|
|
||
| ## Example Integration | ||
|
|
||
| ```dart | ||
| ChartAxisConfig config = ChartAxisConfig( | ||
| autoIntervalEnabled: true, | ||
| ); | ||
|
|
||
| Chart( | ||
| chartAxisConfig: config, | ||
| granularity: granularity, | ||
| onGranularityChangeRequested: (int suggestedGranularity) { | ||
| // Convert ms to seconds for API call if needed | ||
| final int seconds = suggestedGranularity ~/ 1000; | ||
| if (seconds != granularity) { | ||
| // Update granularity and fetch new data | ||
| setState(() => granularity = seconds); | ||
| fetchDataWithGranularity(seconds); | ||
| } | ||
| }, | ||
| // ... other chart params ... | ||
| ) | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Notes | ||
|
|
||
| - The adaptive interval feature is especially useful for financial charts where users frequently zoom in and out to analyze data at different timeframes. | ||
| - You can fully customize the mapping between zoom levels and granularities to suit your application's needs. | ||
| - The feature is opt-in and can be toggled at runtime. | ||
|
|
||
| --- | ||
|
|
||
| ## Related Classes and Files | ||
| - `lib/src/deriv_chart/chart/auto_interval/auto_interval_wrapper.dart` | ||
| - `lib/src/deriv_chart/chart/auto_interval/zoom_level_observer.dart` | ||
| - `lib/src/models/chart_axis_config.dart` | ||
| - `lib/src/deriv_chart/chart/x_axis/x_axis_model.dart` | ||
| - Example usage: `example/lib/main.dart` |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
question: scrollToLastTick is skipped when adaptive interval is enabled, which may affect user experience.
If auto-scrolling to the latest tick is still desired with adaptive interval enabled, consider decoupling these behaviors.