score:0
Try this
- Set pan enabled to false, set zoom enabled and drag to true.
- Set scales.xAxes[0].type to 'time'.
- Set scales.xAxes[0].time.min and max to first and end of data label array to limit the time scaling.
- Also, set pan range min and max to first and end of data label array. This will limit the pan movement.
For example:
// if your data
myChart.data.labels = [
moment(data[0].timestamp),
moment(data[1].timestamp),
moment(data[2].timestamp),
moment(data[3].timestamp)
]
// then set
pan: {
enabled: false,
mode: "x",
speed: 10,
threshold: 10,
rangeMin: {
x: new Date(data[0].timestamp).getTime() - offset
},
rangeMax: {
x: new Date(data[3].timestamp).getTime() + offset
}
},
zoom: {
enabled: true,
drag: true,
mode: "x",
sensitivity: 0.3,
rangeMin: {
x: 3600000 - offset // let say the zoom range min is 1 hour
},
rangeMax: {
x: ((new Date(data[3].timestamp).getTime()) - (new Date(data[0].timestamp).getTime())) + offset
}
},
scales: {
xAxes: [{
type: "time",
time: {
min: new Date(data[0].timestamp).getTime(),
max: new Date(data[3].timestamp).getTime()
}
}]
}
- note: in my case timestamp is in ISO string. And labels must be in moment format.
note: you can also add an offset in min and max. set offset to 0 if not used.
- lastly to switch between pan and zoom, you need to do this:
// since myChart.update() is not working. You need to do this.
var myChartOptions = myChart.options; // need to store in variable.
if (Pan) {
myChartOptions.pan.enabled = true;
myChartOptions.zoom.enabled = false;
myChartOptions.zoom.drag = false;
myChart.options = myChartOptions; // update myChart.options.
}
else if (Zoom) {
myChartOptions.pan.enabled = false;
myChartOptions.zoom.enabled = true;
myChartOptions.zoom.drag = true;
myChart.options = myChartOptions; // update myChart.options.
}
score:5
You have to give same data type you used in your scale.
For example:
if you are using Date object for one of the axes which you want to pan or zoom this may be helpful for you:
import { Chart } from 'chart.js';
import 'chartjs-plugin-zoom';
...
let date_list=["2019-08-09","2019-08-10","2019-08-11","2019-08-12","2019-08-13","2019-08-14"]
let value_list=[1000,2000,3000,2500,3000,5000]
let start_date = new Date(date_list[0]);
let end_date = new Date(date_list[date_list.length-1]);
let range_min = new Date(date_list[0]); //start date
range_min.setDate(range_min.getDate()-10);
let range_max = new Date(date_list[date_list.length-1]); //end date
range_max.setDate(range_max.getDate()+10);
this.chart = new Chart(this.my_chart.nativeElement, {
type: 'bar',
data: {
labels: date_list,
datasets: [
{
data: value_list,
}
]
},
options: {
responsive: true,
scales:{
yAxes: [{
type: 'linear',
ticks: {
beginAtZero: true,
}
},
],
xAxes: [{
distribution: 'linear',
type: "time",
time: {
min: start_date.toDateString(),
max: end_date.toDateString(),
unit: "day",
stepSize: "1",
},
}],
},
pan: {
enabled: true,
mode: 'x',
rangeMin: {
x: range_min,
},
rangeMax: {
x: range_max,
},
},
zoom: {
enabled: true,
mode: 'x',
threshold: 10,
rangeMin: {
x: range_min,
},
rangeMax: {
x: range_max,
},
},
}
});
Also don't forget to install dependencies.
Source: stackoverflow.com
Related Query
- How to set time scale zoom in ChartJS?
- How to set a time scale to a ChartJS chart from JSON?
- chartjs : how to set custom scale in bar chart
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- How to set the chartjs bar graph scale to the highest value in the result data
- How to take a set of Date objects and display the frequency based on time of day using ChartJS
- How to zoom Y axis on time series or X-Y chartjs graph
- How to set up an initial Zoom value for a chart (ChartJS + ChartJS Zoom plugin)?
- How to set ChartJS Y axis title?
- How to format x-axis time scale values in Chart.js v2
- How to prevent first/last bars from being cut off in a chart with time scale
- how to plot multiple time series in chartjs where each time series has different times
- How to make integer scale in Chartjs
- Category scale on Y-axis and time on x-axis in bubble chart in Chartjs
- How to set a full length background color for each bar in chartjs bar
- ChartJS -- How do I change scale color when I have to scales?
- How to dynamically set ChartJs line chart width based on dataset size?
- How to add ChartJS code in Html2Pdf to view image
- ChartJS How to set max labels for X axis?
- How to define chart.js time scale date formats
- How to set ChartJS x-axis title
- how to set color for each item in tooltip with ChartJS
- chartjs - how to set the order in which the different charts are displayed
- How to show scale labels and set min/max for yAxes?
- How can I set the yAxis scale for the chart?
- How to set max smaller than min in ChartJS
- Chartjs 2.0 alpha, how to have a static scale for y-axis
- How to set a borderWidth on ChartJS - AngularJS?
- How to set vertical lines for new day on x-axis in ChartJS v3.x
- How to set the xAxes min and max values of time cartesian chart in Chart.js
More Query from same tag
- Gantt Chart Timeline Chart js 3
- How to reduce the number of data points displayed in react-chart-js2 (data decimation)
- Is it possible to set up 0 as a minimum number for bar chart? react-chart.js-2
- Why are the default Chart.js legend boxes transparent rectangles?
- How to access or get value of specific graph on chart plot by click event?
- Annotation problem with ng2-charts in Angular 11
- chartjs time cartesian axis adapter and date library setup
- Error: Cannot read property 'props' of undefined, Calling parent Method in Child
- Chart.js / HTML Canvas - move line
- Django 1.11 - child template not inheriting from parent
- Using ChartJs with MVC 5 Razor
- ChartJs days of the week from numbers
- Chart not displaying on page load
- Javascript generate random color with equivalent highlight
- Image-Chart ChartJs Ticks Callback not Working?
- Socket.io is not sending data to frontend as intended
- Grouping y-axis labels on multiple lines on a ChartJS with an extra dimension
- How to get values inside a chart without clicking
- pie chart not rendered properly in flask app
- How can i do so that my chartjs updates everytime data is inserted or by time interval?
- how to only show zero grid axes at center and hide all other gridlines in chart js
- ChartJS: Grouped Stacked Bar Chart not Grouping
- Chartkick cumulative chart by date never descending
- Datas put one on each other on chart.js
- Chartjs in format Line is full points
- Pie chart is not getting rendered in ChartJS
- Chart JS V3 Tooltip styling location changed?
- Chart.js bar chart: show tooltip on label hover
- how to add percentage value to legend field in pie chart using chart.js
- Add percentage to label badge - doughnut chart.js