From fc78120946b819077f337a592ff7111c6d10485c Mon Sep 17 00:00:00 2001 From: QuocDoBV <111261098+QuocDoBV@users.noreply.github.com> Date: Fri, 6 Dec 2024 15:15:41 +0700 Subject: [PATCH 1/3] Set limit range for zoom, scroll, and adjust operations Set limit range for zoom, scroll, and adjust operations - Added a method `setLimitRange` to define minimum and maximum range boundaries for dynamic axis operations. - Enforced these limits in `zoomIn`, `zoomOut`, `scrollUp`, `scrollDown`, and `adjustRange` to ensure axis values stay within the specified range. - Updated logic in these methods to respect `minRange` and `maxRange`. - Added checks to prevent invalid configurations for `minRange` and `maxRange`. This change does not affect manual range updates via `setRange` to provide flexibility for custom settings. --- .../eclipse/swtchart/internal/axis/Axis.java | 64 ++++++++++++++++++- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/org.eclipse.swtchart/src/org/eclipse/swtchart/internal/axis/Axis.java b/org.eclipse.swtchart/src/org/eclipse/swtchart/internal/axis/Axis.java index c08218b8..6a10cc63 100644 --- a/org.eclipse.swtchart/src/org/eclipse/swtchart/internal/axis/Axis.java +++ b/org.eclipse.swtchart/src/org/eclipse/swtchart/internal/axis/Axis.java @@ -94,7 +94,16 @@ public class Axis implements IAxis { private int height; /** draw the horizontal|vertical axis line */ private boolean drawAxisLine; - // + /** + * The minimum value allowed for the axis. Initialized to the smallest possible value for a double. + */ + private double minRange = -Double.MAX_VALUE; + /** + * The maximum value allowed for the axis. Initialized to the largest possible value for a double. + */ + private double maxRange = Double.MAX_VALUE; + // The padding ratio to be zoomed when reached limit + private static final double PADDING_RATIO = 0.01; /** the list of dispose listeners */ private List listeners; @@ -383,6 +392,9 @@ public void adjustRange(boolean update) { minimum -= margin; maximum += margin; } + // Clamp the values to minRange and maxRange + minimum = Math.max(minRange, minimum); + maximum = Math.min(maxRange, maximum); setRange(new Range(minimum, maximum), update); } } @@ -396,6 +408,9 @@ public void zoomIn() { @Override public void zoomIn(double coordinate) { + if(coordinate < minRange || coordinate > maxRange) { + return; + } double lower = min; double upper = max; if(isValidCategoryAxis()) { @@ -419,6 +434,12 @@ public void zoomIn(double coordinate) { lower = min + 2 * ZOOM_RATIO * (coordinate - min); upper = max + 2 * ZOOM_RATIO * (coordinate - max); } + if(lower < minRange) { + lower = minRange - PADDING_RATIO * (upper - lower); + } + if(upper > maxRange) { + upper = maxRange + PADDING_RATIO * (upper - lower); + } setRange(new Range(lower, upper)); } @@ -431,6 +452,9 @@ public void zoomOut() { @Override public void zoomOut(double coordinate) { + if(coordinate < minRange || coordinate > maxRange) { + return; + } double lower = min; double upper = max; if(isValidCategoryAxis()) { @@ -452,6 +476,12 @@ public void zoomOut(double coordinate) { lower = (min - 2 * ZOOM_RATIO * coordinate) / (1 - 2 * ZOOM_RATIO); upper = (max - 2 * ZOOM_RATIO * coordinate) / (1 - 2 * ZOOM_RATIO); } + if(lower < minRange) { + lower = minRange - PADDING_RATIO * (upper - lower); + } + if(upper > maxRange) { + upper = maxRange + PADDING_RATIO * (upper - lower); + } setRange(new Range(lower, upper)); } @@ -474,6 +504,10 @@ public void scrollUp() { lower = min + (max - min) * SCROLL_RATIO; upper = max + (max - min) * SCROLL_RATIO; } + if(upper > maxRange) { + upper = maxRange + PADDING_RATIO * (upper - lower); + lower = upper - (max - min); + } setRange(new Range(lower, upper)); } @@ -496,6 +530,10 @@ public void scrollDown() { lower = min - (max - min) * SCROLL_RATIO; upper = max - (max - min) * SCROLL_RATIO; } + if(lower < minRange) { + lower = minRange - PADDING_RATIO * (upper - lower); + upper = lower + max - min; + } setRange(new Range(lower, upper)); } @@ -821,4 +859,26 @@ public void updatePositionMarker(MouseEvent e) { tick.getAxisPositionMarker().update(e.x, e.y); } -} \ No newline at end of file + + /** + * Sets the minimum and maximum limits for the axis range. These limits + * are enforced during dynamic operations such as {@code adjustRange}, + * {@code scroll}, and {@code zoom}. However, they do not restrict + * manual range settings through the {@link #setRange(Range, boolean)} method. + * + * @param minRange + * the minimum value allowed for the axis range. + * @param maxRange + * the maximum value allowed for the axis range. + * @throws IllegalArgumentException + * if {@code minRange} is greater than or equal to {@code maxRange}. + */ + public void setLimitRange(double minRange, double maxRange) { + + if(minRange >= maxRange) { + throw new IllegalArgumentException("minRange must be less than maxRange."); + } + this.minRange = minRange; + this.maxRange = maxRange; + } +} From edc94664a568f04c5dadd7006fd58d7550f393ca Mon Sep 17 00:00:00 2001 From: QuocDoBV <111261098+QuocDoBV@users.noreply.github.com> Date: Mon, 9 Dec 2024 14:30:10 +0700 Subject: [PATCH 2/3] Update copyright headers to include 2023, 2024 Updated the copyright headers in the modified files to include the year range 2023, 2024 --- .../src/org/eclipse/swtchart/internal/axis/Axis.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/org.eclipse.swtchart/src/org/eclipse/swtchart/internal/axis/Axis.java b/org.eclipse.swtchart/src/org/eclipse/swtchart/internal/axis/Axis.java index 6a10cc63..2a9f6b55 100644 --- a/org.eclipse.swtchart/src/org/eclipse/swtchart/internal/axis/Axis.java +++ b/org.eclipse.swtchart/src/org/eclipse/swtchart/internal/axis/Axis.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2023 SWTChart project. + * Copyright (c) 2023, 2024 SWTChart project. * * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 From 37f910fb44c39bfecc985526a1849843012a980b Mon Sep 17 00:00:00 2001 From: QuocDoBV <111261098+QuocDoBV@users.noreply.github.com> Date: Wed, 11 Dec 2024 10:12:59 +0700 Subject: [PATCH 3/3] correct the copyright header --- .../src/org/eclipse/swtchart/internal/axis/Axis.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/org.eclipse.swtchart/src/org/eclipse/swtchart/internal/axis/Axis.java b/org.eclipse.swtchart/src/org/eclipse/swtchart/internal/axis/Axis.java index 2a9f6b55..3321043a 100644 --- a/org.eclipse.swtchart/src/org/eclipse/swtchart/internal/axis/Axis.java +++ b/org.eclipse.swtchart/src/org/eclipse/swtchart/internal/axis/Axis.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2023, 2024 SWTChart project. + * Copyright (c) 2008, 2024 SWTChart project. * * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0