docs/getting-started/integration.md
Chart.js can be integrated with plain JavaScript or with different module loaders. The examples below show how to load Chart.js in different systems.
If you're using a front-end framework (e.g., React, Angular, or Vue), please see available integrations.
<script src="path/to/chartjs/dist/chart.umd.min.js"></script>
<script>
const myChart = new Chart(ctx, {...});
</script>
Chart.js is tree-shakeable, so it is necessary to import and register the controllers, elements, scales and plugins you are going to use.
If you don't care about the bundle size, you can use the auto package ensuring all features are available:
import Chart from 'chart.js/auto';
When optimizing the bundle, you need to import and register the components that are needed in your application.
The options are categorized into controllers, elements, plugins, scales. You can pick and choose many of these, e.g. if you are not going to use tooltips, don't import and register the Tooltip plugin. But each type of chart has its own bare-minimum requirements (typically the type's controller, element(s) used by that controller and scale(s)):
BarControllerBarElementCategoryScale (x), LinearScale (y)BubbleControllerPointElementLinearScale (x/y)DoughnutControllerArcElementLineControllerLineElementPointElementCategoryScale (x), LinearScale (y)PieControllerArcElementPolarAreaControllerArcElementRadialLinearScale (r)RadarControllerLineElementPointElementRadialLinearScale (r)ScatterControllerPointElementLinearScale (x/y)Available plugins:
DecimationFiller - used to fill area described by LineElement, see Area chartsLegendSubTitleTitleTooltipAvailable scales:
Cartesian scales (x/y)
Radial scales (r)
If you want to use the helper functions, you will need to import these separately from the helpers package and use them as stand-alone functions.
Example of Converting Events to Data Values using bundlers.
import Chart from 'chart.js/auto';
import { getRelativePosition } from 'chart.js/helpers';
const chart = new Chart(ctx, {
type: 'line',
data: data,
options: {
onClick: (e) => {
const canvasPosition = getRelativePosition(e, chart);
// Substitute the appropriate scale IDs
const dataX = chart.scales.x.getValueForPixel(canvasPosition.x);
const dataY = chart.scales.y.getValueForPixel(canvasPosition.y);
}
}
});
Because Chart.js is an ESM library, in CommonJS modules you should use a dynamic import:
const { Chart } = await import('chart.js');
Important: RequireJS can load only AMD modules, so be sure to require one of the UMD builds instead (i.e. dist/chart.umd.min.js).
require(['path/to/chartjs/dist/chart.umd.min.js'], function(Chart){
const myChart = new Chart(ctx, {...});
});
:::tip Note
In order to use the time scale, you need to make sure one of the available date adapters and corresponding date library are fully loaded after requiring Chart.js. For this you can use nested requires:
require(['chartjs'], function(Chart) {
require(['moment'], function() {
require(['chartjs-adapter-moment'], function() {
new Chart(ctx, {...});
});
});
});
:::