list_rrd.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Default max of 3 lines are drawn for memory. Colors need to be update to work better
  2. colors = ['rgba(255,52,120,0.5)', 'rgba(255,52,0,0.5)', 'rgba(255,255,120,0.5)'];
  3. // Other markups are working see https://www.chartjs.org/docs/latest/
  4. // todo make charts responsive
  5. (function () {
  6. document.querySelectorAll('canvas').forEach(async (el) => {
  7. const response = await fetch('/list/rrd/ajax.php', {
  8. method: 'POST',
  9. headers: {
  10. 'Content-Type': 'application/json',
  11. },
  12. body: {
  13. service: el.getAttribute('id'),
  14. period: el.getAttribute('period'),
  15. },
  16. });
  17. const rrdData = await response.clone().json();
  18. // data is stored as start, end time and step between each step
  19. const labels = [];
  20. for (i = rrdData.meta.start; i < rrdData.meta.end; i = i + rrdData.meta.step) {
  21. labels.push(new Date(i * 1000).toLocaleString());
  22. }
  23. // rrdData.data stores data as i[x,y] useless for chartjs split in separate datasets
  24. const datasets = [];
  25. for (i = 0; i < rrdData.meta.legend.length; i++) {
  26. const data = [];
  27. for (b of rrdData.data) {
  28. data.push(b[i]);
  29. }
  30. dataset = { label: rrdData.meta.legend[i], data: data, borderColor: colors[i] };
  31. datasets.push(dataset);
  32. }
  33. // draw chart
  34. const ctx = document.getElementById(rrdData.service).getContext('2d');
  35. new Chart(ctx, {
  36. type: 'line',
  37. data: { labels, datasets },
  38. options: {
  39. scales: {
  40. y: { beginAtZero: true },
  41. },
  42. },
  43. });
  44. });
  45. })();