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();
}
};}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745594834a4635047.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