ChartJs latest version not working

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chart</title>
</head>
<header>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/4.6.1/css/bootstrap.min.css" integrity="sha512-T584yQ/tdRR5QwOpfvDfVQUidzfgc2339Lc8uBDtcp/wYu80d7jwBgAxbyMh0a9YM9F8N3tdErpFI8iaGx6x5g==" crossorigin="anonymous" referrerpolicy="no-referrer" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/4.6.1/js/bootstrap.min.js" integrity="sha512-UR25UO94eTnCVwjbXozyeVd6ZqpaAE9naiEUBK/A+QDbfSTQFhPGj5lOR6d8tsgbBk84Ggb5A3EkjsOgPRPcKA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.min.js" integrity="sha512-QSkVNOCYLtj73J4hbmVoOV6KVZuMluZlioC+trLpewV8qMjsWqlIQvkn1KGX2StWvPMdWGBqim1xlC8krl1EKQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-datalabels/2.0.0/chartjs-plugin-datalabels.min.js" integrity="sha512-R/QOHLpV1Ggq22vfDAWYOaMd5RopHrJNMxi8/lJu8Oihwi4Ho4BRFeiMiCefn9rasajKjnx9/fTQ/xkWnkDACg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</header>
<body>
    <div class="container">
        <div class="row">
            <div class="h-50 d-inline-block col-sm-12 ">
                <h3>Income Trend</h3>
                <canvas id="myChart" width="400px" height="400px"></canvas>
            </div>
        </div>
    <div > 
<script>
const ctx = document.getElementById('myChart').getContext('2d');
Chart.register(ChartDataLabels);
const labels = ["Feb-21","Mar-21","Apr-21","May-21","Jun-21","Jul-21"];
const data = {
    labels: labels,
    datasets: [{
        label: 'Income Trend',
        data: [400, 300, 350, 400, 300, 500],
        datalabels: {
            align:"top",
            backgroundColor: '#0075ff',
            borderRadius: 25,
            color: 'white',
            font: {
                weight: 'bold'
            },
            formatter: function(value, context) {
                return '£ ' + Math.round(value);
            },
            padding: 15,
            offset: 20,
            display: function(context) {
                return context.dataIndex > 4 ||context.dataIndex < 1; // display labels for first last
            }
        },
        borderColor: '#0075ff',
        tension: 0.5,
        pointBorderWidth: 0,
        pointBackgroundColor: 'black',
        pointRadius:10
    }]
};
const myChart = new Chart(ctx, {
    type: 'line',
    data,
    options: {
        scales: {
            x:{
                grid:{
                    display:false,
                    drawBorder: false
                }
            },
            y: {
                ticks: {
                    display: false,
                },
                grace: '5%',
                grid:{
                    drawBorder: false
                }
            }
        },
        plugins: {
            legend: {
                display: false
            }
        },
        layout: {
            padding: {
                left: 50,
                right:50
            }
        },
    },
});
</script>

</body>
</html>

Hi @dinu_checkboard,

We can’t help with raw HTML/Javascript implementations, but if you’re looking to render this same chart with QuickChart then make sure you set version=3 or version=3.7.1 in your request. See documentation here

Thank you @ian .
I added the parameter version=3 then its worked except the datalabel.
Why the datalabel not worked?

Your config is constructed incorrectly. datalabels belongs inside options.plugins. See datalabels documentation or this datalabels example.

Thanks @ian … how we can add function in json like below?
formatter: function(value, context) {
return '£ ’ + Math.round(value);
},
display: function(context) {
return context.dataIndex > 4 ||context.dataIndex < 1; // display labels for first last
}

I need to view the first and last labels only and append pound with the labels. but the functions not working.
any suggestion @ian

You’ll have to send the entire config as a string, rather than just the function. Have a look at Using Javascript Functions :slight_smile:

If you prefer, this process is simplified by the use of quickchart-js to generate configs.