Integration

    1. const myChart = new Chart(ctx, {...});

    Chart.js 3 is tree-shakeable, so it is necessary to import and register the controllers, elements, scales and plugins you are going to use.

    For all available imports see the example below.

    1. import { Chart, registerables } from 'chart.js';
    2. Chart.register(...registerables);

    And finally there is an separate path to do just the above for you, in one line:

    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.

    1. import Chart from 'chart.js/auto';
    2. const chart = new Chart(ctx, {
    3. type: 'line',
    4. options: {
    5. onClick: (e) => {
    6. const canvasPosition = getRelativePosition(e, chart);
    7. // Substitute the appropriate scale IDs
    8. const dataX = chart.scales.x.getValueForPixel(canvasPosition.x);
    9. }
    10. });

    Important: RequireJS can not load CommonJS module as is (opens new window), so be sure to require one of the UMD builds instead (i.e. dist/chart.js, dist/chart.min.js, etc.).

    Note: in order to use the time scale, you need to make sure and corresponding date library are fully loaded after requiring Chart.js. For this you can use nested requires:

    1. require(['chartjs'], function(Chart) {
    2. require(['moment'], function() {
    3. require(['chartjs-adapter-moment'], function() {
    4. new Chart(ctx, {...});
    5. });
    6. });