const allData = { jual: [], beli: [ { x: new Date('2025-07-17'), y: 1700000 }, { x: new Date('2025-07-17'), y: 1686000 }, { x: new Date('2025-07-18'), y: 1686000 }, { x: new Date('2025-07-19'), y: 1686000 }, { x: new Date('2025-07-21'), y: 1697000 }, { x: new Date('2025-07-22'), y: 1697000 }, { x: new Date('2025-07-23'), y: 1712000 }, { x: new Date('2025-07-24'), y: 1730000 }, { x: new Date('2025-07-25'), y: 1698000 }, { x: new Date('2025-07-26'), y: 1698000 }, { x: new Date('2025-07-28'), y: 1703000 }, { x: new Date('2025-07-29'), y: 1691000 }, { x: new Date('2025-07-30'), y: 1682000 }, { x: new Date('2025-07-31'), y: 1703000 }, { x: new Date('2025-08-01'), y: 1680000 }, { x: new Date('2025-08-02'), y: 1696000 }, { x: new Date('2025-08-04'), y: 1719000 }, { x: new Date('2025-08-05'), y: 1699000 }, { x: new Date('2025-08-06'), y: 1614000 }, { x: new Date('2025-08-07'), y: 1716000 }, { x: new Date('2025-08-08'), y: 1706000 }, { x: new Date('2025-08-09'), y: 1709000 }, { x: new Date('2025-08-11'), y: 1706000 }, { x: new Date('2025-08-11'), y: 1706000 }, { x: new Date('2025-08-12'), y: 1691000 }, { x: new Date('2025-08-13'), y: 1702000 }, { x: new Date('2025-08-14'), y: 1699000 }, { x: new Date('2025-08-15'), y: 1705000 }, { x: new Date('2025-08-16'), y: 1676000 }, { x: new Date('2025-08-18'), y: 1683000 } ] }; const bulanSingkat = ["Jan","Feb","Mar","Apr","Mei","Jun","Jul","Agu","Sep","Okt","Nov","Des"]; 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, formatter: function (value) { const date = new Date(value); return `${date.getDate()} ${bulanSingkat[date.getMonth()]}`; } }, min: undefined, max: undefined }, yaxis: { tickAmount: 8, labels: { style: { fontSize: '12px' }, offsetX: -15, formatter: function (value) { return new Intl.NumberFormat('id-ID', { maximumFractionDigits: 0 }).format(value); } } }, grid: { padding: { left: -10, right: 0 } }, tooltip: { x: { formatter: function (value) { const date = new Date(value); // Tooltip tetap pakai lengkap: 16 Juli 2025 const bulanIndo = ["Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember"]; return `${date.getDate()} ${bulanIndo[date.getMonth()]} ${date.getFullYear()}`; } }, 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); window.addEventListener('load', () => chart.render().then(() => filterData('1'))); 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 = months === 'all' ? new Date('2000-01-01') : new Date(new Date().setMonth(new Date().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: 'Harga Jual', data: filteredJual }, { name: 'Harga Beli', data: filteredBeli } ]); chart.updateOptions({ xaxis: { labels: { formatter: function (value) { const date = new Date(value); return `${date.getDate()} ${bulanSingkat[date.getMonth()]}`; } } }, }); } 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');