score:2
Accepted answer
I assume you're using chartjs-plugin-zoom for this.
Digging around in the chart
object passed to the onPanComplete
function reveals that the scales
property is updated to achieve the zoom'n'pan functionality. Knowing this, it's fairly trivial to retrieve the first and last tick objects which contain the label
property you're looking for.
Below is a snippet that demonstrates accessing the ticks via onPanComplete
and outputting them to the page:
let ft = document.getElementById("ft"),
lt = document.getElementById("lt");
new Chart(document.getElementById("chart"), {
type: "line",
data: {
labels: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'],
datasets: [{
data: [0, 1, 2, 4, 8, 16, 32, 64, 128, 256]
}]
},
options: {
maintainAspectRatio: false,
scales: {
xAxes: [{
ticks: {
min: 'c',
max: 'h'
}
}]
},
plugins: {
zoom: {
pan: {
enabled: true,
mode: 'x',
onPanComplete: function({
chart
}) {
let ticks = chart.scales["x-axis-0"]._ticks;
ft.innerText = JSON.stringify(ticks[0]);
lt.innerText = JSON.stringify(ticks[ticks.length - 1]);
}
}
}
}
}
});
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.1"></script>
<script src="https://cdn.jsdelivr.net/npm/hammerjs@2.0.8"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-zoom@0.7.4"></script>
<canvas id="chart" style="max-height:125px"></canvas>
<p>
First tick: <span id="ft"></span><br>Last tick: <span id="lt"></span>
</p>
Source: stackoverflow.com
Related Query
- ChartJS - Finding the minimum and maximum labels that are inside a pan (When zooming)
- ChartJs - Pie Chart - how to remove labels that are on the pie chart
- How to hide the labels of graphs that are not toggled in ChartJS
- ChartJs - Labelling minimum and maximum value of the Y Axis
- Setting Common labels and background color common for all the charts in ChartJs
- Is there any way to change the font color and size of labels in Chartjs 3.0.0
- Why are the chartjs tooltip labels always showing the first x-axis label?
- ChartJS : Hook before values are given to doughnut (absolute value in chart and real values in the tooltips)
- When I added a funnel chart to chartjs all the charts are load compressed until resize page
- Adjusting the font size and family of extra fillText labels in ChartJS
- JavaScript ignore commas inside ChartJS data field pulled from csv that data has commas and is enclosed by double quotes
- Chart.js, increase space between bars and its labels when increasing the charts width
- Chartjs + DataLabelPlugin: Suddenly data labels are shown in every chart even without the plugin?
- Chart.js displaying each letter of label and data as a single value on chart. The data and labels provided are arrays
- Chartjs doc examples are lightning fast but same code is slow when reproducing
- Ticks for the minimum and maximum values in a Chart.js v2 axis
- When I try to include the chart.js and angular chartjs files, I get: ReferenceError: require is not defined
- Destroying the Div and its contents inside the jquery dialog when close button is clicked
- Truncating canvas labels in ChartJS while keeping the full label value in the tooltips
- chartjs datalabels change font and color of text displaying inside pie chart
- ChartJS and jsPDF - why the background is black?
- Using Chart.js - The X axis labels are not all showing
- TypeError: Cannot read property 'defaults' of undefined when using the react wrapper of chartjs
- How to change the color of legend in chartjs and be able to add one more legend?
- How to change the cursor to a pointer when I hover over a bar in a ChartJS bar chart?
- zoom and pan on charts in angular
- How to remove gridlines and grid labels in Chartjs Radar?
- Zoom and Pan in react-chartjs-2
- ChartJS Line chart cut off at the top and bottom
- Chart.js How to synchronize pan and zoom in multiple chats
More Query from same tag
- Line Chart: background of RangeSlider
- Offset Overlay Line on chart.js Graph
- Chart Value of the bar
- Chart.js - Vertical crosshair (vertical annotation that moves with mouse) in line graph
- Chart.js legend alignment left side
- Canvas static height Chartjs
- Relative bar chart overlay on line chart in chart.js
- How do I make my Type:Time to work with my Line Chart?
- Chart.js: How to handle very large and very small values in radar chart?
- Chart JS: All Values Displayed on x-axis After Callback Conversion
- How to Add X axis Padding in chart js Line Graph
- Chart JS V3 Tooltip styling location changed?
- Cannot visualise chartJS data in ASP .NET Core
- How to draw a chart like below using chart js
- On hover bar is overlapped by label chartjs
- Create multiple dynamic charts
- Contrast chart.js datalabels colors with the background
- Chat.js, Codeigniter and MySQL. Something wrong on recover data
- cant resize range of Y axis of chart in HTML/PHP
- chartjs plugin datalabels does not show value on charts
- Chart.js Y-Axis data not rendering
- How do we can use chart.js in angular JS?
- How to use chart.js in blazor?
- Chart,js Pie Chart can the gap between a pie chart and the legend be adjusted
- Chart.js horizontal bar grid line colors
- Chart only prints out first value using jquery ajax php
- generate basic pie chart using chartjs
- Chart.js not able to re-set data
- Chartjs plugin label in Angular app moving unexpectedly at hover
- Why isn't my X axes on my bar chart starting from zero?