const allData = { jual: [ { x: new Date('2025-06-21'), y: 1946855 }, { x: new Date('2025-06-22'), y: 1946855 }, { x: new Date('2025-06-23'), y: 1946855 }, { x: new Date('2025-06-24'), y: 1936830 }, { x: new Date('2025-06-25'), y: 1936830 }, { x: new Date('2025-06-26'), y: 1928810 }, { x: new Date('2025-06-27'), y: 1911768 }, { x: new Date('2025-06-28'), y: 1888710 }, { x: new Date('2025-06-29'), y: 1888710 }, { x: new Date('2025-06-30'), y: 1884700 }, { x: new Date('2025-07-01'), y: 1884700 } ], beli: [ { x: new Date('2025-06-21'), y: 1786000 }, { x: new Date('2025-06-22'), y: 1786000 }, { x: new Date('2025-06-23'), y: 1786000 }, { x: new Date('2025-06-24'), y: 1776000 }, { x: new Date('2025-06-25'), y: 1776000 }, { x: new Date('2025-06-26'), y: 1768000 }, { x: new Date('2025-06-27'), y: 1751000 }, { x: new Date('2025-06-28'), y: 1728000 }, { x: new Date('2025-06-29'), y: 1728000 }, { x: new Date('2025-06-30'), y: 1724000 }, { x: new Date('2025-07-01'), y: 1740000 } ] }; const chartOptions = { chart: { type: 'area', height: '300', zoom: { enabled: false }, toolbar: { show: false }, offsetX: 0 }, colors: ['#008FFB', '#DFBD69'], dataLabels: { enabled: false }, stroke: { curve: 'smooth', width: 2 }, series: [], xaxis: { type: 'datetime', labels: { datetimeUTC: false }, min: undefined, max: undefined }, yaxis: { tickAmount: 4, labels: { style: { fontSize: '12px' }, offsetX: -15, formatter: function (value) { const text = value.toLocaleString('id-ID'); return text.length > 5 ? text.slice(0, 5) + '…' : text; } } }, grid: { padding: { left: -10, right: 0 } }, tooltip: { x: { format: 'dd MMM yyyy' }, y: { formatter: value => new Intl.NumberFormat('id-ID', { style: 'currency', currency: 'IDR', maximumFractionDigits: 0 }).format(value) } }, fill: { type: 'gradient', gradient: { shadeIntensity: 1, opacityFrom: 0.4, opacityTo: 0.05, stops: [0, 90, 100] } }, legend: { position: 'top', horizontalAlign: 'right' } }; const chart = new ApexCharts(document.querySelector("#chart"), chartOptions); chart.render(); function padIfTooFew(data, cutoff, now) { if (data.length === 1) { return [ { x: cutoff.getTime(), y: null }, ...data, { x: now.getTime(), y: null } ]; } return data; } function filterData(months) { const now = new Date(); let cutoff; if (months === 'all') { cutoff = new Date('2000-01-01'); } else { cutoff = new Date(); cutoff.setMonth(cutoff.getMonth() - parseInt(months)); } let filteredJual = allData.jual.filter(d => d.x >= cutoff.getTime()); let filteredBeli = allData.beli.filter(d => d.x >= cutoff.getTime()); filteredJual = padIfTooFew(filteredJual, cutoff, now); filteredBeli = padIfTooFew(filteredBeli, cutoff, now); chart.updateSeries([ { name: 'Jual', data: filteredJual }, { name: 'Beli', data: filteredBeli } ]); chart.updateOptions({ xaxis: { min: cutoff.getTime(), max: now.getTime() } }); } document.querySelectorAll(".filter-btn").forEach(btn => { btn.addEventListener("click", function () { document.querySelectorAll(".filter-btn").forEach(b => b.classList.remove("active")); this.classList.add("active"); const months = this.getAttribute("data-months"); if (months) { filterData(months); } }); }); // Initial load (1 Bulan) filterData('1');