How to import the js file locally and why does the chartzoom register failed at second time?

Hi,

I have this code using chartjs and chartjs-plugin-zoom.

I met two problems:

  1. I can not load the hammerjs, chartjs and plugin-zoom js by local folder, I already downloaded three js files and put them at the node-red/lib folder, and using the script below, it did not work as I expected. I checked other topics regarding offline cdn access, something need to be done in the settings.js, but I did not find the settings.js in node-red folder, only package.json,flows.json file like below:

    <script type="text/javascript" src="lib/hammer.min.js"></script>
    <script type="text/javascript" src="lib/chart.umd.min.js"></script>
    <script type="text/javascript" src="lib/chartjs-plugin-zoom.min.js"></script>
    
  2. Every time I refresh the front end web page, the code line Chart.register(ChartZoom) will occur the error showed ChartZoom is not defined. However, if I do not put Chart.register(ChartZoom) in the mounted(), the chart can not be zoomed;

<template>
  <div class="chart-container">
    <v-btn
    @click="resetZoom" 
    id="resetZoomBtn" 
    class="reset-btn">Reset</v-btn>
    <canvas 
    id="myChart" 
    class="chart-canvas"></canvas>
  </div>
</template>

<style scoped>
    .chart-container {
        width: 100%;
        height: 100%;
        position: relative;
        padding-top: 80px;
    }

    .chart-canvas {
        width: 100% !important;
        height: 100% !important;
    }

    .reset-btn {
        position: absolute;
        top: 10px;
        right: 10px;
        z-index: 10;
    }
</style>
<!-- 
<script type="text/javascript" src="lib/hammer.min.js"></script>
<script type="text/javascript" src="lib/chart.umd.min.js"></script>
<script type="text/javascript" src="lib/chartjs-plugin-zoom.min.js"></script>
-->
<script src="https://cdn.jsdelivr.net/npm/hammerjs@2.0.8"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.2"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-zoom@2.0.1"></script>

<script>
export default {
    data() {
        return {
            myChart: null
        }
    },
    mounted() {
        try {
        Chart.register(ChartZoom);
        } catch(e) {
            console.warn('ChartZoom register failed!', e);
        }
        
        const ctx = document.getElementById('myChart').getContext('2d');
        this.myChart = new Chart(ctx, {
            type: 'line',
            data: {
                datasets: [{
                    label: '',
                    data: [{ x: 1, y: 5 },
                    { x: 2, y: 10 },
                    { x: 3.5, y: 6 },
                    { x: 4, y: 2 },
                    { x: 3, y: 8 }],
                    borderColor: 'rgb(127, 147, 164)',
                    showLine: true,
                    pointRadius: 0,
                    tension: 0.1
                }]
            },
            options: {
                
                responsive: true,
                interaction: { mode: 'nearest', intersect: true },
                scales: {
                    x: { type: 'linear', title: { display: true, text: 'Position[mm]' } },
                    y: { type: 'linear', title: { display: true, text: 'Force[N]' } }
                },
                plugins: {
                    legend:{display:false},
                    zoom: {
                        pan: { enabled: true, mode: 'xy' },
                        zoom: { wheel: { enabled: true }, pinch: { enabled: true }, mode: 'xy' }
                    }
                }
            }
        });
    },
    watch: {
            msg: function () 
            {
                if(this.msg.topic===("POS/FORCE"))
                {
                    this.updateCurve(this.msg.payload);
                }
            },
            deep:true
        },
    methods: {
        updateCurve: function (payload) 
        {
            if (Array.isArray(payload)) {
                this.myChart.data.datasets[0].data = payload;
                this.myChart.resetZoom();
                this.myChart.update();
                
            }
        },
        resetZoom() {
            if (this.myChart) {
                this.myChart.resetZoom();
            }
        }
    },
    unmounted() {
        if (this.myChart) {
            this.myChart.destroy();
        }
    }
}
</script>

You have to "serve" the files. You can do this using http-in ~ http-response nodes or adding httpStatic settings

The documentation details how you should wait for the external lib to be loaded: Template ui-template | Node-RED Dashboard 2.0

To clarify the reason that you have to serve them, that code is running in the browser. The browser may be running on a machine the other side of the world and so cannot directly access the folder on the node-red server.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.