(function($) { const canvas = document.getElementById('ntPriceChart'); if (!canvas) return; const product_id = canvas.dataset.productId; $.post(ntPriceHistory.ajaxurl, { action: 'nt_update_price_history', product_id: product_id }, function(response) { if (!response.success) { console.error('Chyba v odpovědi serveru:', response); return; } let data = response.data; if (!data || data.length === 0) { console.warn('Žádná data k zobrazení:', data); return; } // Kontrola formátu dat console.log('Přijatá data:', data); let dataPoints = data.map(point => { let date = new Date(point.x); if (isNaN(date)) { if (point.x.match(/^\d{2}\.\d{2}\.\d{4}$/)) { const [day, month, year] = point.x.split('.'); date = new Date(`${year}-${month}-${day}`); } } return { x: date.toISOString().split('T')[0], y: point.y }; }); const prices = dataPoints.map(p => p.y); const minPrice = Math.min(...prices); const maxPrice = Math.max(...prices); // Handle single data point case if (dataPoints.length === 1) { const singlePrice = dataPoints[0].y; const today = new Date(); const thirtyDaysAgo = new Date(today.getTime() - 30 * 24 * 60 * 60 * 1000); const fifteenDaysAgo = new Date(today.getTime() - 15 * 24 * 60 * 60 * 1000); dataPoints = [ { x: thirtyDaysAgo.toISOString().split('T')[0], y: singlePrice }, { x: fifteenDaysAgo.toISOString().split('T')[0], y: singlePrice }, { x: today.toISOString().split('T')[0], y: singlePrice } ]; } const ctx = canvas.getContext('2d'); new Chart(ctx, { type: 'line', data: { datasets: [{ label: 'Historie ceny (30 dní)', data: dataPoints, borderColor: '#0094DE', backgroundColor: function(context) { const gradient = context.chart.ctx.createLinearGradient(0, 0, 0, 300); gradient.addColorStop(0, 'rgba(0,148,222,0.3)'); gradient.addColorStop(1, 'rgba(0,148,222,0)'); return gradient; }, fill: true, tension: dataPoints.length === 1 ? 0 : 0.3, pointRadius: dataPoints.length === 1 ? [0, 4, 0] : 4, pointHoverRadius: dataPoints.length === 1 ? [0, 6, 0] : 6, pointBackgroundColor: dataPoints.map(p => { if (p.y === minPrice) return 'green'; if (p.y === maxPrice) return 'red'; return '#0094DE'; }) }] }, options: { parsing: { xAxisKey: 'x', yAxisKey: 'y' }, plugins: { tooltip: { enabled: true, backgroundColor: '#0094DE', titleColor: '#fff', bodyColor: '#fff', callbacks: { title: function(context) { const index = context[0].dataIndex; const originalDate = dataPoints[index].x; // Použijeme původní datum z dataPoints console.log('Tooltip original date:', originalDate); // Ladění const date = new Date(originalDate); if (isNaN(date)) { console.error('Neplatné datum v tooltipu:', originalDate); return 'Datum: Neplatné'; } const day = String(date.getDate()).padStart(2, '0'); const month = String(date.getMonth() + 1).padStart(2, '0'); const year = date.getFullYear(); return `Datum: ${day}.${month}.${year}`; }, label: function(context) { return 'Cena: ' + context.formattedValue + ' Kč'; } } }, legend: { display: false } }, scales: { x: { display: true, title: { display: true, text: 'Datum' }, ticks: { autoSkip: true, maxTicksLimit: 10 }, grid: { color: 'rgba(0,0,0,0.05)' }, type: 'time', time: { unit: 'day', displayFormats: { day: 'dd.MM.yyyy' } }, min: new Date(new Date().getTime() - 30 * 24 * 60 * 60 * 1000).toISOString().split('T')[0], max: new Date().toISOString().split('T')[0] }, y: { display: true, title: { display: true, text: 'Cena (Kč)' }, beginAtZero: false, grid: { color: 'rgba(0,0,0,0.05)' } } } } }); }); })(jQuery);