admin管理员组文章数量:1432359
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 Answer
Reset to default 1I 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();
}
};}
本文标签:
版权声明:本文标题:chart.js - AlpineJs 3 and Charts.js (4.4.6) gives Uncaught RangeError: Maximum call stack size exceeded when calling Chart.updat 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745594833a2665413.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
chart
(the chart instance) as a property of the state object? Why not declarechart
as a local variable inchartComponent
function instead? See fiddle – kikon Commented Nov 19, 2024 at 9:52Object.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