Skip to content
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

Chartjs-plugin-zoom drag issue #807

Open
bjlee3075 opened this issue Mar 6, 2024 · 0 comments · May be fixed by #811
Open

Chartjs-plugin-zoom drag issue #807

bjlee3075 opened this issue Mar 6, 2024 · 0 comments · May be fixed by #811

Comments

@bjlee3075
Copy link

Hi
I am drawing a graph using chart.js. Below is my code.
zoom works fine. However, there is a little problem.
If the mouse leaves the chart while dragging, it will not zoom in properly.
Like the first picture, the starting point of the drag is 2023, and the end of the drag is outside the chart, not inside the chart.
The starting point of drag is 2023, but as shown in the second picture, 2011 became the starting point.
Is there any way to prevent this?

Thanks,

2
1

		zoom: {
			pan: {
				enabled: false,
				mode: 'x',
			},
			zoom: {
				wheel: {
					enabled: false,
				},
				pinch: {
					enabled: false,
				},
				drag: {
					enabled: true,
					mode: 'x',
					borderColor: 'rgba(0, 0, 0, 0.5)', 
					borderWidth: 1, 
					backgroundColor: 'rgba(0, 0, 0, 0.3)',
				},
				mode: 'x',
				
				onZoomComplete: function (context) {
					const chart = context.chart;
					const xScale = chart.scales.x;
					const displayedRange = xScale.max - xScale.min;
					const oneYear = 1000 * 60 * 60 * 24 * 365;
					isLongRange = displayedRange >= oneYear * 10;

					chart.options.scales.x.time.unit = 'month';

					if (displayedRange < oneYear * 3) {
						chart.options.scales.x.ticks.stepSize = 1;
					} else if (displayedRange < oneYear * 6) {
						chart.options.scales.x.ticks.stepSize = 2;
					} else if (displayedRange < oneYear * 10) {
						chart.options.scales.x.ticks.stepSize = 6;
					} else {
						chart.options.scales.x.time.unit = 'year';
						delete chart.options.scales.x.ticks.stepSize; 
					}

					chart.update();
				},
			},
			limits: { 
				x: {
					min: new Date(originalXMin).getTime(), 
					max: new Date(originalXMax).getTime() 
				}
			},
		},
@gopal-panigrahi gopal-panigrahi linked a pull request Mar 17, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant