最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

chart.js - AlpineJs 3 and Charts.js (4.4.6) gives Uncaught RangeError: Maximum call stack size exceeded when calling Chart.updat

programmeradmin1浏览0评论

I'm tring to update my chart with new data. But whenever I call update functions I get error: Maximum call stack size exceeded.

    <div x-data="chartComponent()" class="max-w-xl mx-auto">
        <canvas id="myChart"></canvas>
        <button
            @click="updateChart"
            class="mt-4 px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600"
        >
            Update Chart
        </button>
    </div>

    <script>
        function chartComponent() {
            return {
                chart: null,
                data: {
                    labels: ['January', 'February', 'March', 'April', 'May'],
                    datasets: [{
                        label: 'Sales',
                        data: [10, 20, 30, 40, 50],
                        backgroundColor: 'rgba(75, 192, 192, 0.2)',
                        borderColor: 'rgba(75, 192, 192, 1)',
                        borderWidth: 1
                    }]
                },
                init() {
                    const ctx = document.getElementById('myChart').getContext('2d');
                    this.chart = new Chart(ctx, {
                        type: 'bar',
                        data: this.data,
                        options: {
                            responsive: true,
                            plugins: {
                                legend: {
                                    position: 'top',
                                },
                                tooltip: {
                                    callbacks: {
                                        label: function (context) {
                                            return `${context.label}: ${context.raw}`;
                                        }
                                    }
                                }
                            }
                        }
                    });
                },
                updateChart() {
                    this.chart.data.datasets[0].data = [50, 40, 30, 20, 10];
                    this.chart.update();
                }
            };
        }
    </script>

I found that Alpine js works well Charts.js not higher then 2.9.4. Starting from v3 it gives such error.

I'm tring to update my chart with new data. But whenever I call update functions I get error: Maximum call stack size exceeded.

    <div x-data="chartComponent()" class="max-w-xl mx-auto">
        <canvas id="myChart"></canvas>
        <button
            @click="updateChart"
            class="mt-4 px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600"
        >
            Update Chart
        </button>
    </div>

    <script>
        function chartComponent() {
            return {
                chart: null,
                data: {
                    labels: ['January', 'February', 'March', 'April', 'May'],
                    datasets: [{
                        label: 'Sales',
                        data: [10, 20, 30, 40, 50],
                        backgroundColor: 'rgba(75, 192, 192, 0.2)',
                        borderColor: 'rgba(75, 192, 192, 1)',
                        borderWidth: 1
                    }]
                },
                init() {
                    const ctx = document.getElementById('myChart').getContext('2d');
                    this.chart = new Chart(ctx, {
                        type: 'bar',
                        data: this.data,
                        options: {
                            responsive: true,
                            plugins: {
                                legend: {
                                    position: 'top',
                                },
                                tooltip: {
                                    callbacks: {
                                        label: function (context) {
                                            return `${context.label}: ${context.raw}`;
                                        }
                                    }
                                }
                            }
                        }
                    });
                },
                updateChart() {
                    this.chart.data.datasets[0].data = [50, 40, 30, 20, 10];
                    this.chart.update();
                }
            };
        }
    </script>

I found that Alpine js works well Charts.js not higher then 2.9.4. Starting from v3 it gives such error.

Share Improve this question asked Nov 18, 2024 at 20:54 zhovtyjzhovtyj 611 silver badge3 bronze badges 2
  • 1 Why do you need to have chart (the chart instance) as a property of the state object? Why not declare chart as a local variable in chartComponent function instead? See fiddle – kikon Commented Nov 19, 2024 at 9:52
  • The chart instance is known to not be working (well) with reactivity - there are multiple posts here on this with other frameworks, search for chart.js and reactivity; see this good one or even this with the funny (but probably working) solution of applying Object.seal. The chart instance is a hugely complex object, with circular references and even if you haven't had infinite recursion, it would've been a huge strain to your app to have it observable. – kikon Commented Nov 19, 2024 at 9:52
Add a comment  | 

1 Answer 1

Reset to default 1

I have found an temporary solution that wil simply first destroy the chart and then reinitialize it and somehow preventing the error.

    function chartComponent() {
        return {
            chart: null,
            data: {
                labels: ['January', 'February', 'March', 'April', 'May'],
                datasets: [{
                    label: 'Sales',
                    data: [10, 20, 30, 40, 50],
                    backgroundColor: 'rgba(75, 192, 192, 0.2)',
                    borderColor: 'rgba(75, 192, 192, 1)',
                    borderWidth: 1
                }]
            },
            init() {
                const ctx = document.getElementById('myChart').getContext('2d');
                this.chart = new Chart(ctx, {
                    type: 'bar',
                    data: this.data,
                    options: {
                       responsive: true,
                    },
                    plugins: {
                        legend: {
                            position: 'top',
                        },
                        tooltip: {
                            callbacks: {
                                label: function (context) {
                                    return `${context.label}: ${context.raw}`;
                                }
                            }
                        }
                    }
                });
            },
            updateChart() {
                this.data.datasets[0].data = [50, 40, 30, 20, 10];
                this.chart.destroy();
                this.init();
            }
        };
    }
    

Prevent that people can stress test the update button on this solution because it then will break. It breaks if you try to update before the whole chart is loaded.

or

You will make chart a local variable so it wont cause crashes (this is provided by user @kikon)

function chartComponent() {
let chart = null;
return {
    data: {
        labels: ['January', 'February', 'March', 'April', 'May'],
        datasets: [{
            label: 'Sales',
            data: [10, 20, 30, 40, 50],
            backgroundColor: 'rgba(75, 192, 192, 0.2)',
            borderColor: 'rgba(75, 192, 192, 1)',
            borderWidth: 1
        }]
    },
    init() {
        const ctx = document.getElementById('myChart').getContext('2d');
        chart = new Chart(ctx, {
            type: 'bar',
            data: this.data,
            options: {
                responsive: true,
                plugins: {
                    legend: {
                        position: 'top',
                    },
                    tooltip: {
                        callbacks: {
                            label: function (context) {
                                return `${context.label}: ${context.raw}`;
                            }
                        }
                    }
                }
            }
        });
    },
    updateChart() {
        chart.data.datasets[0].data = [50, 40, 30, 20, 10];
        chart.update();
    }
};}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论