score:5
To have functional Pan and Zoom on your ng2-charts, this is what you need to do:
- Use
npm
to install the dependencies:hammerjs
andchartjs-plugin-zoom
- Import the dependencies to your module.
- Add the
pan
andzoom
configurations in theplugin
section of the chart configuration. - Horizontal zoom and pan only works with scatter charts
It is easier said than done so, before adding zoom and pan, be sure that you have a working ng2-chart in your project. Otherwise you never know what's going wrong.
1. Install the required plugins
Execute the following commands in the root folder of your project:
npm install hammerjs
npm install charts-plugin-zoom
2. Import the plugins into your module
After installing the plugins, you can check that these two folders exist:
node_modules/hammerjs
node_modules/chartjs-plugin-zoom
You need to include them in your module. For example, you can add two imports in app.module.ts
:
import 'hammerjs';
import 'chartjs-plugin-zoom';
@NgModule({
declarations: [
AppComponent,
...
]
}
As you can see, no need to add anything in the declarations
or any of the other sections of the @NgModule
annotation.
3. Add the zoom options in chart configuration To setup a chart with ng2-chart you had to create a template with something like the following snippet:
<canvas baseChart
chartType="bar"
[datasets]="dataSets"
[labels]="labels"
[options]="options"
legend="true">
</canvas>
Add, in the corresponding class, a plugin
element with zoom
section. Mind the zoom
nested twice:
import { ChartDataSets, ChartOptions } from 'chart.js';
...
...
export class YourChartComponent {
...
public options: ChartOptions = {
legend: {
...
},
scales: {
...
},
plugins: {
zoom: {
pan: {
enabled: true,
mode: 'xy'
},
zoom: {
enabled: true,
mode: 'xy'
}
}
}
};
...
...
}
4. Horizontal pan only works with scatter charts
Typically, the example above will only pan and zoom vertically.
Some links
Credits to where I got the information:
- Official hammerjr web site Train here your gestures. At first they're not exactly intuitive if you have a touchpad.
- Official readme.md of charts-plugin-zoom shows the complete syntax for the
zoom
configuration - Can ng2-charts work with Chart.js zoom plugin? A discussion on how to use the zoom plugin.
Source: stackoverflow.com
Related Query
- zoom and pan on charts in angular
- How to zoom charts in chart.js using angular 7
- Zoom and Pan in react-chartjs-2
- Update charts in chartjs and angular
- Chart.js How to synchronize pan and zoom in multiple chats
- pan on chart.js also zoom on line charts
- Chart changes different on zoom when using multiple charts with chart.js and flask
- The dataset for bar graph is not set properly using ng2 charts and ng5-slider in Angular
- Angular 8 and ng2 charts - Updating labels and data
- chart.js charts wouldn't show or hide with Angular directives ng-show and ng-hide
- How to save Chart JS charts as image without black background using blobs and filesaver?
- Chart.js v2 charts do not display inside angular 2 (but Chart.js v1 works perfectly)
- Problem for display a chart with Chart.js and Angular
- Add zoom event handler to charts for chartjs with chartjs-plugin-zoom
- Create a rounded bar graph with Angular and chartJS
- Angular 5 and chart.js ts error
- Chart.js is not rendered until zoom in in angular 8
- How to create dynamic charts with Django and Chart.js?
- Make y-axis sticky when having horizontal scroll on chartJS and Angular
- ChartJS line chart drag and zoom
- How do I keep chart.js charts in one JS file, and not get errors when the ID from the JS file don't exist on one specific html page?
- How to achieve the best possible performance with mutable data and real-time charts in React?
- Angular 2 Chart.js and NG2-Charts Not Binding Data Objects After Updates To Project
- ERROR TypeError: "this.canvas is undefined" | Problem with creating a chart with angular and chart.js
- ChartJS - Finding the minimum and maximum labels that are inside a pan (When zooming)
- How do I correctly include Moment.js and Chart.js with RequireJS when I want to create charts with time scales?
- Setting Common labels and background color common for all the charts in ChartJs
- Remove additional white space on left and right side of Angular Chart
- How can I implement Polar area charts with Chart.js and Canvas
- Chart.js creating multiple Charts in Angular in same Component
More Query from same tag
- Skip labels when associated value is null in chartJS
- Vue.js: How to retrieve data from API for vue chart.js
- Using CSS variables (color) with chart.js - "var(--primaryColor)" not working
- vue-chartjs with chartkick vertical bar chart
- Horizontal overlapping of bars, not whole graph
- automatic Y-axis scale: max is always as 120% of highest value in ChartJS
- How to run Chart.js samples using source code
- How to hide the tick marks of the axis in react-chartjs-2
- How to update only new data in Chart.JS?
- Configuring locale with bundled (CDN) chartjs-adapter-date-fns.bundle.min.js?
- In Chart.js multibar graph, is there a way to highlight a single category?
- PrimeNG/Chart.js - max Number of labels to show
- How to set y axis to the max dataset value in Chartjs?
- Line Chart using Chart js with time data
- Not able to remove legent from Radar Chart in chart.js even using legent: {display : false}
- Vertical stacked bar chart with chart.js
- How can I implement Polar area charts with Chart.js and Canvas
- polymer, using attribute data binding in the constructor functions
- Updating chart.js based on selections from dropdown
- ChartJs Angular directive - chart[chartType] is not a function
- How do I update the Chart.js automatically every 2 seconds?
- Chartjs v2.5 - only show min and max on scale, absolute positioning on scale
- ChartJS: Is it possible to omit last grid line to save space
- chartjs display data in one bar line with 3 different data sets in 3 different colors
- ChartJS: How to center monthly bars against a daily line chart?
- ChartJS 3.x How to place tick label inside graph?
- How can I set different colours for each bar in angular-chart.js v1.0.0?
- Chart.js dataset controller 'null' when chart drawn
- I can't change the legend position in Laravel Charts & ChartJS
- Changing color of specific chartjs point