jilo-web/app/helpers/graph.php

113 lines
3.4 KiB
PHP
Raw Normal View History

2024-10-09 12:56:51 +00:00
<div style="position: relative; width: 800px; height: 400px;">
2024-10-10 08:31:05 +00:00
<div id="current-period-<?= $data['graph_name'] ?>" style="text-align: center; position: absolute; top: 0; left: 0; right: 0; z-index: 10; font-size: 14px; background-color: rgba(255, 255, 255, 0.7);"></div>
<canvas id="graph_<?= $data['graph_name'] ?>" style="margin-top: 20px; margin-bottom: 50px;"></canvas>
2024-10-09 12:56:51 +00:00
</div>
<script>
2024-10-10 08:31:05 +00:00
var ctx = document.getElementById('graph_<?= $data['graph_name'] ?>').getContext('2d');
var timeRangeName = '';
2024-10-09 12:56:51 +00:00
2025-01-14 13:37:20 +00:00
// Prepare datasets
var datasets = [];
<?php foreach ($data['datasets'] as $dataset): ?>
var chartData = <?php echo json_encode($dataset['data']); ?>;
datasets.push({
label: '<?= $dataset['label'] ?>',
data: chartData.map(function(item) {
return {
x: item.date,
y: item.value
};
}),
borderColor: '<?= $dataset['color'] ?>',
borderWidth: 1,
fill: false
});
<?php endforeach; ?>
2024-10-09 12:56:51 +00:00
2024-10-10 08:31:05 +00:00
var graph_<?= $data['graph_name'] ?> = new Chart(ctx, {
2024-10-09 12:56:51 +00:00
type: 'line',
data: {
2025-01-14 13:37:20 +00:00
datasets: datasets
2024-10-09 12:56:51 +00:00
},
options: {
layout: {
padding: {
top: 30
}
},
scales: {
x: {
type: 'time',
time: {
unit: 'day'
}
},
y: {
beginAtZero: true
}
},
plugins: {
zoom: {
pan: {
enabled: true,
mode: 'x'
},
zoom: {
2024-10-10 08:31:05 +00:00
mode: 'x',
drag: {
enabled: true, // Enable drag to select range
borderColor: 'rgba(255, 99, 132, 0.3)',
borderWidth: 1
},
onZoom: function({ chart }) {
propagateZoom(chart); // Propagate the zoom to all graphs
2024-10-12 14:02:07 +00:00
setActive(document.getElementById('custom_range'));
2024-10-10 08:31:05 +00:00
}
2024-10-09 12:56:51 +00:00
}
},
legend: {
position: 'bottom',
labels: {
boxWidth: 20,
2024-10-10 08:31:05 +00:00
padding: 10
}
},
title: {
display: true,
text: '<?= $data['graph_title'] ?>',
font: {
size: 16,
weight: 'bold'
},
padding: {
bottom: 10
2024-10-09 12:56:51 +00:00
}
}
}
}
});
2025-01-14 13:37:20 +00:00
// Store graph instance and title for later reference
2024-10-10 08:31:05 +00:00
graphs.push({
graph: graph_<?= $data['graph_name'] ?>,
2025-01-14 13:37:20 +00:00
label: '<?= $data['graph_title'] ?>'
2024-10-10 08:31:05 +00:00
});
2024-10-09 12:56:51 +00:00
2025-01-14 13:37:20 +00:00
// Function to update the period label
function updatePeriodLabel(chart, label) {
var startDate = new Date(chart.scales.x.min);
var endDate = new Date(chart.scales.x.max);
var periodLabel = document.getElementById('current-period-<?= $data['graph_name'] ?>');
if (timeRangeName) {
periodLabel.textContent = label + ' (' + timeRangeName + ')';
2024-10-10 08:31:05 +00:00
} else {
2025-01-14 13:37:20 +00:00
periodLabel.textContent = label + ' (from ' + startDate.toLocaleDateString() + ' to ' + endDate.toLocaleDateString() + ')';
2024-10-10 08:31:05 +00:00
}
2024-10-09 12:56:51 +00:00
}
2025-01-14 13:37:20 +00:00
// Initial label update
updatePeriodLabel(graph_<?= $data['graph_name'] ?>, '<?= $data['graph_title'] ?>');
2024-10-09 12:56:51 +00:00
</script>