I’m attempting to use amCharts in my Twig file, but I’m facing issues with parsing the code. Regardless of what I try, the charts don’t seem to work. However, when I place my code outside of the JavaScript loop, I can see that the data is being retrieved correctly.
But I couldn’t do this between tags. No results are returned.
<div id="kt_charts_widget_15_chart" class="min-h-auto ps-4 pe-6 mb-3 h-350px"></div>
<script>
var KTChartsWidget15 = {
init: function () {
var e = am5.Root.new("kt_charts_widget_15_chart");
if (e) {
var t,
a = function () {
t = am5.Root.new(e);
t.setThemes([am5themes_Animated.new(t)]);
var salesData = [
{% for data in getLastDays %}
{
country: {{ data.sales_date }},
visits: {{ data.total_sales }},
icon: "https://www.amcharts.com/wp-content/uploads/flags/united-states.svg",
columnSettings: { fill: am5.color(KTUtil.getCssVariableValue("--bs-primary")) },
},
{% endfor %}
];
var l = t.container.children.push(am5xy.XYChart.new(t, { panX: !1, panY: !1, layout: t.verticalLayout }));
var r = l.xAxes.push(
am5xy.CategoryAxis.new(t, {
categoryField: "country",
renderer: am5xy.AxisRendererX.new(t, { minGridDistance: 30 }),
bullet: function (e, t, a) {
return am5xy.AxisBullet.new(e, {
location: 0.5,
sprite: am5.Picture.new(e, {
width: 24,
height: 24,
centerY: am5.p50,
centerX: am5.p50,
src: a.dataContext.icon,
}),
});
},
})
);
r.get("renderer").labels.template.setAll({ paddingTop: 20, fontWeight: "400", fontSize: 10, fill: am5.color(KTUtil.getCssVariableValue("--bs-gray-500")) });
r.get("renderer").grid.template.setAll({ disabled: !0, strokeOpacity: 0 });
r.data.setAll(salesData);
var o = l.yAxes.push(am5xy.ValueAxis.new(t, { renderer: am5xy.AxisRendererY.new(t, {}) }));
o.get("renderer").grid.template.setAll({ stroke: am5.color(KTUtil.getCssVariableValue("--bs-gray-300")), strokeWidth: 1, strokeOpacity: 1, strokeDasharray: [3] });
o.get("renderer").labels.template.setAll({ fontWeight: "400", fontSize: 10, fill: am5.color(KTUtil.getCssVariableValue("--bs-gray-500")) });
var i = l.series.push(am5xy.ColumnSeries.new(t, { xAxis: r, yAxis: o, valueYField: "visits", categoryXField: "country" }));
i.columns.template.setAll({ tooltipText: "{categoryX}: {valueY}", tooltipY: 0, strokeOpacity: 0, templateField: "columnSettings" });
i.columns.template.setAll({ strokeOpacity: 0, cornerRadiusBR: 0, cornerRadiusTR: 6, cornerRadiusBL: 0, cornerRadiusTL: 6 });
i.data.setAll(salesData);
i.appear();
l.appear(1000, 100);
};
am5.ready(function () {
a();
});
}
},
};
"undefined" != typeof module && (module.exports = KTChartsWidget15);
KTUtil.onDOMContentLoaded(function () {
KTChartsWidget15.init();
});
</script>