const allData = { jual: [], beli: [ { x: new Date('2025-06-30'), y: 16618 }, { x: new Date('2025-07-07'), y: 16863 }, { x: new Date('2025-07-07'), y: 16863 }, { x: new Date('2025-07-08'), y: 17077 }, { x: new Date('2025-07-09'), y: 17052 }, { x: new Date('2025-07-10'), y: 17048 }, { x: new Date('2025-07-11'), y: 17093 }, { x: new Date('2025-07-14'), y: 17418 }, { x: new Date('2025-07-15'), y: 17437 }, { x: new Date('2025-07-16'), y: 17466 }, { x: new Date('2025-07-17'), y: 17411 }, { x: new Date('2025-07-18'), y: 17434 }, { x: new Date('2025-07-19'), y: 17434 }, { x: new Date('2025-07-22'), y: 17605 }, { x: new Date('2025-07-23'), y: 17683 }, { x: new Date('2025-07-24'), y: 17437 }, { x: new Date('2025-07-25'), y: 17438 }, { x: new Date('2025-07-28'), y: 17295 }, { x: new Date('2025-07-29'), y: 17274 }, { x: new Date('2025-07-30'), y: 17356 }, { x: new Date('2025-07-31'), y: 17230 }, { x: new Date('2025-08-01'), y: 17404 }, { x: new Date('2025-08-04'), y: 17378 }, { x: new Date('2025-08-05'), y: 17412 }, { x: new Date('2025-08-06'), y: 17521 }, { x: new Date('2025-08-07'), y: 17441 }, { x: new Date('2025-08-08'), y: 17519 }, { x: new Date('2025-08-11'), y: 17561 }, { x: new Date('2025-08-12'), y: 17472 }, { x: new Date('2025-08-13'), y: 17375 }, { x: new Date('2025-08-14'), y: 17387 }, { x: new Date('2025-08-15'), y: 17316 }, { x: new Date('2025-08-16'), y: 17310 }, { x: new Date('2025-08-18'), y: 17402 }, { x: new Date('2025-08-19'), y: 17353 }, { x: new Date('2025-08-20'), y: 17221 }, { x: new Date('2025-08-21'), y: 17339 }, { x: new Date('2025-08-22'), y: 17404 }, { x: new Date('2025-08-25'), y: 17454 }, { x: new Date('2025-08-26'), y: 17493 }, { x: new Date('2025-08-27'), y: 17598 }, { x: new Date('2025-08-28'), y: 17583 }, { x: new Date('2025-08-29'), y: 17617 }, { x: new Date('2025-09-01'), y: 17809 }, { x: new Date('2025-09-02'), y: 18156 }, { x: new Date('2025-09-03'), y: 18691 }, { x: new Date('2025-09-04'), y: 18499 }, { x: new Date('2025-09-05'), y: 18499 }, { x: new Date('2025-09-06'), y: 18530 }, { x: new Date('2025-09-08'), y: 18724 }, { x: new Date('2025-09-09'), y: 18836 }, { x: new Date('2025-09-10'), y: 18801 }, { x: new Date('2025-09-11'), y: 18880 }, { x: new Date('2025-09-12'), y: 18796 }, { x: new Date('2025-09-16'), y: 19073 }, { x: new Date('2025-09-17'), y: 19006 }, { x: new Date('2025-09-18'), y: 18940 }, { x: new Date('2025-09-19'), y: 18956 }, { x: new Date('2025-09-22'), y: 19235 }, { x: new Date('2025-09-24'), y: 19544 }, { x: new Date('2025-09-25'), y: 19622 }, { x: new Date('2025-09-26'), y: 19749 }, { x: new Date('2025-09-29'), y: 19827 }, { x: new Date('2025-09-30'), y: 19941 }, { x: new Date('2025-10-01'), y: 20133 }, { x: new Date('2025-10-02'), y: 20003 }, { x: new Date('2025-10-03'), y: 20016 }, { x: new Date('2025-10-06'), y: 20555 }, { x: new Date('2025-10-07'), y: 21076 }, { x: new Date('2025-10-08'), y: 21441 }, { x: new Date('2025-10-09'), y: 21613 }, { x: new Date('2025-10-13'), y: 21516 }, { x: new Date('2025-10-14'), y: 22135 }, { x: new Date('2025-10-15'), y: 22260 }, { x: new Date('2025-10-16'), y: 22404 }, { x: new Date('2025-10-17'), y: 23062 }, { x: new Date('2025-10-20'), y: 22483 }, { x: new Date('2025-10-21'), y: 23179 }, { x: new Date('2025-10-22'), y: 21639 }, { x: new Date('2025-10-23'), y: 21843 }, { x: new Date('2025-10-24'), y: 22181 }, { x: new Date('2025-10-27'), y: 21911 }, { x: new Date('2025-10-28'), y: 21395 }, { x: new Date('2025-10-29'), y: 21210 }, { x: new Date('2025-10-30'), y: 21409 }, { x: new Date('2025-11-03'), y: 21860 }, { x: new Date('2025-11-04'), y: 21708 }, { x: new Date('2025-11-05'), y: 21310 }, { x: new Date('2025-11-06'), y: 21513 }, { x: new Date('2025-11-07'), y: 21488 }, { x: new Date('2025-11-10'), y: 21721 }, { x: new Date('2025-11-11'), y: 22362 }, { x: new Date('2025-11-12'), y: 22462 }, { x: new Date('2025-11-13'), y: 22919 }, { x: new Date('2025-11-14'), y: 22785 }, { x: new Date('2025-11-17'), y: 22225 }, { x: new Date('2025-11-18'), y: 22033 }, { x: new Date('2025-11-19'), y: 22297 }, { x: new Date('2025-11-20'), y: 22701 }, { x: new Date('2025-11-21'), y: 22586 }, { x: new Date('2025-11-25'), y: 22849 }, { x: new Date('2025-11-26'), y: 22876 }, { x: new Date('2025-11-27'), y: 22886 }, { x: new Date('2025-11-28'), y: 23012 }, { x: new Date('2025-12-01'), y: 23192 }, { x: new Date('2025-12-02'), y: 23120 }, { x: new Date('2025-12-03'), y: 23139 }, { x: new Date('2025-12-04'), y: 22998 }, { x: new Date('2025-12-05'), y: 23003 }, { x: new Date('2025-12-08'), y: 23000 }, { x: new Date('2025-12-09'), y: 23020 }, { x: new Date('2025-12-10'), y: 23238 }, { x: new Date('2025-12-11'), y: 23252 }, { x: new Date('2025-12-12'), y: 23437 }, { x: new Date('2025-12-15'), y: 23614 }, { x: new Date('2025-12-16'), y: 23484 }, { x: new Date('2025-12-17'), y: 23660 }, { x: new Date('2025-12-18'), y: 23769 }, { x: new Date('2025-12-19'), y: 23745 }, { x: new Date('2025-12-22'), y: 24360 }, { x: new Date('2025-12-23'), y: 24669 }, { x: new Date('2025-12-24'), y: 24823 }, { x: new Date('2025-12-25'), y: 24571 }, { x: new Date('2025-12-28'), y: 24777 }, { x: new Date('2025-12-29'), y: 24724 }, { x: new Date('2025-12-30'), y: 24130 }, { x: new Date('2026-01-05'), y: 24376 }, { x: new Date('2026-01-06'), y: 24566 }, { x: new Date('2026-01-07'), y: 24763 }, { x: new Date('2026-01-08'), y: 24579 }, { x: new Date('2026-01-09'), y: 24689 }, { x: new Date('2026-01-13'), y: 25685 }, { x: new Date('2026-01-14'), y: 25931 }, { x: new Date('2026-01-15'), y: 25751 }, { x: new Date('2026-01-16'), y: 25709 }, { x: new Date('2026-01-19'), y: 25911 }, { x: new Date('2026-01-20'), y: 26065 }, { x: new Date('2026-01-21'), y: 27041 }, { x: new Date('2026-01-22'), y: 26783 }, { x: new Date('2026-01-23'), y: 27703 }, { x: new Date('2026-01-26'), y: 27516 }, { x: new Date('2026-01-27'), y: 27543 }, { x: new Date('2026-01-28'), y: 28226 }, { x: new Date('2026-01-29'), y: 30002 }, { x: new Date('2026-01-30'), y: 27994 }, { x: new Date('2026-02-02'), y: 26465 }, { x: new Date('2026-02-03'), y: 26428 }, { x: new Date('2026-02-04'), y: 27948 }, { x: new Date('2026-02-05'), y: 27749 }, { x: new Date('2026-02-06'), y: 26692 }, { x: new Date('2026-02-09'), y: 27792 }, { x: new Date('2026-02-10'), y: 27725 }, { x: new Date('2026-02-12'), y: 27308 }, { x: new Date('2026-02-13'), y: 26832 }, { x: new Date('2026-02-16'), y: 26971 }, { x: new Date('2026-02-17'), y: 26835 }, { x: new Date('2026-02-18'), y: 26652 }, { x: new Date('2026-02-19'), y: 27111 }, { x: new Date('2026-02-20'), y: 27253 }, { x: new Date('2026-02-24'), y: 28431 }, { x: new Date('2026-02-25'), y: 28376 }, { x: new Date('2026-02-26'), y: 28478 }, { x: new Date('2026-02-27'), y: 28377 }, { x: new Date('2026-03-02'), y: 29508 }, { x: new Date('2026-03-03'), y: 29695 }, { x: new Date('2026-03-04'), y: 28651 }, { x: new Date('2026-03-05'), y: 28567 }, { x: new Date('2026-03-06'), y: 28473 }, { x: new Date('2026-03-09'), y: 28112 }, { x: new Date('2026-03-09'), y: 28383 }, { x: new Date('2026-03-10'), y: 28692 }, { x: new Date('2026-03-11'), y: 28985 }, { x: new Date('2026-03-12'), y: 28874 }, { x: new Date('2026-03-13'), y: 28523 }, { x: new Date('2026-03-16'), y: 28157 }, { x: new Date('2026-03-17'), y: 28197 }, { x: new Date('2026-03-18'), y: 27943 }, { x: new Date('2026-03-19'), y: 27219 }, { x: new Date('2026-03-23'), y: 24841 }, { x: new Date('2026-03-24'), y: 24451 }, { x: new Date('2026-03-25'), y: 25512 }, { x: new Date('2026-03-26'), y: 25359 }, { x: new Date('2026-03-27'), y: 24920 }, { x: new Date('2026-03-30'), y: 25193 }, { x: new Date('2026-03-31'), y: 25871 }, { x: new Date('2026-04-01'), y: 26288 }, { x: new Date('2026-04-02'), y: 26708 }, { x: new Date('2026-04-03'), y: 26499 }, { x: new Date('2026-04-06'), y: 26110 }, { x: new Date('2026-04-07'), y: 26188 }, { x: new Date('2026-04-08'), y: 26891 }, { x: new Date('2026-04-09'), y: 26331 }, { x: new Date('2026-04-10'), y: 26607 }, { x: new Date('2026-04-10'), y: 26607 }, { x: new Date('2026-04-13'), y: 26471 }, { x: new Date('2026-04-14'), y: 26691 }, { x: new Date('2026-04-14'), y: 26691 }, { x: new Date('2026-04-15'), y: 26965 }, { x: new Date('2026-04-16'), y: 26961 }, { x: new Date('2026-04-16'), y: 26961 }, { x: new Date('2026-04-17'), y: 26897 }, { x: new Date('2026-04-20'), y: 26776 }, { x: new Date('2026-04-21'), y: 26943 }, { x: new Date('2026-04-22'), y: 26623 }, { x: new Date('2026-04-23'), y: 26633 }, { x: new Date('2026-04-24'), y: 26403 }, { x: new Date('2026-04-27'), y: 26452 }, { x: new Date('2026-04-28'), y: 26299 }, { x: new Date('2026-04-30'), y: 25660 }, { x: new Date('2026-05-01'), y: 25551 }, { x: new Date('2026-05-07'), y: 25824 }, { x: new Date('2026-05-08'), y: 25948 }, { x: new Date('2026-05-08'), y: 26003 }, { x: new Date('2026-05-11'), y: 25846 }, { x: new Date('2026-05-12'), y: 26310 }, { x: new Date('2026-05-13'), y: 26183 }, { x: new Date('2026-05-14'), y: 26076 }, { x: new Date('2026-05-15'), y: 25789 } ] }; 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.getMonth() + 1}月${date.getDate()}日`; } }, min: undefined, max: undefined }, yaxis: { tickAmount: 8, labels: { style: { fontSize: '12px' }, offsetX: -15, formatter: function (value) { const text = value.toLocaleString('ja-JP'); return text.length > 5 ? text.slice(0, 6) + '' : text; } } }, grid: { padding: { left: -10, right: 0 } }, tooltip: { x: { formatter: function (value) { const date = new Date(value); return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`; } }, y: { formatter: value => new Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY', 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(() => { // Panggil filterData hanya setelah render selesai 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; 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: '買値', data: filteredJual }, { name: '売値', data: filteredBeli } ]); const minX = cutoff.getTime(); const maxX = now.getTime(); chart.updateOptions({ xaxis: { type: 'datetime', min: minX, max: maxX, labels: { datetimeUTC: false, formatter: function (value) { const date = new Date(value); if (isNaN(date)) return value; return `${date.getMonth() + 1}月${date.getDate()}日`; } } } }); } 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');