What seems to be the problem:
I have a chart that I need to re render upon data changes made in the db. I am able to reload new data in and re render it but it doesn’t display on the screen. When I click inspect element in safari, it begins to display in the browser, but without clicking inspect element, my card remains empty with no chart. If anyone can assist or provide me with a better solution, it would be greatly appreciated.
Steps to Reproduce:
Master File:
@yield('widget_scripts')
Livewire component
<div class="card-body"><!--begin::Chart--><div id="kt_charts_widget_1_chart"></div><!--end::Chart--></div>
@section('widget_scripts')
<script type="text/javascript">
"use strict";
// Class definition
var KTWidgets = function() {
// Private properties
// Charts widgets
var _initChartsWidget1 = function() {
var element = document.getElementById("kt_charts_widget_1_chart");
if (!element) {
return;
}
var options = {
series: [{
name: 'Amount Correct',
data: {!! json_encode($correct) !!}
}],
chart: {
type: 'bar',
height: 450,
toolbar: {
show: true
}
},
plotOptions: {
bar: {
horizontal: false,
columnWidth: ['30%'],
endingShape: 'rounded'
},
},
legend: {
show: false
},
dataLabels: {
enabled: false
},
stroke: {
show: true,
width: 2,
colors: ['transparent']
},
xaxis: {
categories: {!! json_encode($questions) !!},
axisBorder: {
show: false,
},
axisTicks: {
show: false
},
labels: {
style: {
colors: KTApp.getSettings()['colors']['gray']['gray-500'],
fontSize: '12px',
fontFamily: KTApp.getSettings()['font-family']
}
}
},
yaxis: {
labels: {
style: {
colors: KTApp.getSettings()['colors']['gray']['gray-500'],
fontSize: '12px',
fontFamily: KTApp.getSettings()['font-family']
}
}
},
fill: {
opacity: 1
},
states: {
normal: {
filter: {
type: 'none',
value: 0
}
},
hover: {
filter: {
type: 'none',
value: 0
}
},
active: {
allowMultipleDataPointsSelection: false,
filter: {
type: 'none',
value: 0
}
}
},
tooltip: {
style: {
fontSize: '12px',
fontFamily: KTApp.getSettings()['font-family'],
},
y: {
formatter: function(val) {
return val
}
}
},
colors: [KTApp.getSettings()['colors']['theme']['base']['success'], KTApp.getSettings()['colors']['gray']['gray-300']],
grid: {
borderColor: KTApp.getSettings()['colors']['gray']['gray-200'],
strokeDashArray: 4,
yaxis: {
lines: {
show: true
}
}
}
};
var chart = new ApexCharts(element, options);
chart.render();
}
// Public methods
return {
init: function() {
// Charts Widgets
_initChartsWidget1();
}
}
}();
// Webpack support
if (typeof module !== 'undefined') {
module.exports = KTWidgets;
}
jQuery(document).ready(function() {
KTWidgets.init();
});
</script>
<script>
window.addEventListener('update_scripts', event => {
"use strict";
// Class definition
var KTWidgets = function() {
// Private properties
// Charts widgets
var _initChartsWidget1 = function() {
var element = document.getElementById("kt_charts_widget_1_chart");
if (!element) {
return;
}
var options = {
series: [{
name: 'Amount Correct',
data: event.detail.correct
}],
chart: {
type: 'bar',
height: 450,
toolbar: {
show: true
}
},
plotOptions: {
bar: {
horizontal: false,
columnWidth: ['30%'],
endingShape: 'rounded'
},
},
legend: {
show: false
},
dataLabels: {
enabled: false
},
stroke: {
show: true,
width: 2,
colors: ['transparent']
},
xaxis: {
categories: {!! json_encode($questions) !!},
axisBorder: {
show: false,
},
axisTicks: {
show: false
},
labels: {
style: {
colors: KTApp.getSettings()['colors']['gray']['gray-500'],
fontSize: '12px',
fontFamily: KTApp.getSettings()['font-family']
}
}
},
yaxis: {
labels: {
style: {
colors: KTApp.getSettings()['colors']['gray']['gray-500'],
fontSize: '12px',
fontFamily: KTApp.getSettings()['font-family']
}
}
},
fill: {
opacity: 1
},
states: {
normal: {
filter: {
type: 'none',
value: 0
}
},
hover: {
filter: {
type: 'none',
value: 0
}
},
active: {
allowMultipleDataPointsSelection: false,
filter: {
type: 'none',
value: 0
}
}
},
tooltip: {
style: {
fontSize: '12px',
fontFamily: KTApp.getSettings()['font-family'],
},
y: {
formatter: function(val) {
return val
}
}
},
colors: [KTApp.getSettings()['colors']['theme']['base']['success'], KTApp.getSettings()['colors']['gray']['gray-300']],
grid: {
borderColor: KTApp.getSettings()['colors']['gray']['gray-200'],
strokeDashArray: 4,
yaxis: {
lines: {
show: true
}
}
}
};
var chart = new ApexCharts(element, options);
chart.render();
}
// Public methods
return {
init: function() {
// Charts Widgets
_initChartsWidget1();
}
}
}();
// Webpack support
if (typeof module !== 'undefined') {
module.exports = KTWidgets;
}
jQuery(document).ready(function() {
KTWidgets.init();
});
});
</script>
@endsection
Livewire Controller
$this->dispatchBrowserEvent('update_scripts', ['correct' => $this->correct]);
Are you using the latest version of Livewire:
Yes
Do you have any screenshots or code examples: