chart_linear.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>ISP Speed Chart</title>
  5. <script src="chartjs/Chart.min.js"></script>
  6. <script src="chartjs/utils.js"></script>
  7. <style>
  8. canvas {
  9. -moz-user-select: none;
  10. -webkit-user-select: none;
  11. -ms-user-select: none;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div style="width:75%">
  17. <canvas id="canvas"></canvas>
  18. </div>
  19. <script>
  20. var color = Chart.helpers.color;
  21. async function mainfunc() {
  22. var color = Chart.helpers.color;
  23. console.log(color)
  24. var datareq = await fetch('backend/results-api.php');
  25. console.log(datareq)
  26. var dataraw = await datareq.text();
  27. console.log(dataraw)
  28. try {
  29. dataraw = JSON.parse(dataraw);
  30. } catch {
  31. alert("Can't get data");
  32. return 0;
  33. }
  34. data_sorted = {};
  35. for (const data of dataraw["data"]) {
  36. let data_isp = data["isp"].split(" ").length > 1 ? data["isp"].split(" ")[0] + " " + data["isp"].split(" ")[1] : data["isp"];
  37. if (data_isp in data_sorted) {
  38. data_sorted[data_isp] = data_sorted[data_isp].concat([data])
  39. } else {
  40. data_sorted[data_isp] = [data]
  41. }
  42. }
  43. let datasets = []
  44. for (const isp of Object.keys(data_sorted)) {
  45. let ispdata_chart = {
  46. label: isp,
  47. data: []
  48. }
  49. for (const ispdata of data_sorted[isp]) {
  50. let [t_h, t_m, t_s] = ispdata["created"].split(" ")[1].split(":");
  51. ispdata_chart["data"] = ispdata_chart["data"].concat([{
  52. x: t_h * 1.0 + t_m / 60 + t_s / 3600,
  53. y: ispdata["dspeed"],
  54. label: ispdata
  55. }]);
  56. }
  57. datasets = datasets.concat([ispdata_chart])
  58. }
  59. datasets = datasets.sort((a, b) => a["data"].length < b["data"].length ? 1 : -1);
  60. for (const [i, data] of datasets.entries()) {
  61. let pcolor = "gray"
  62. if (i === 0) {
  63. pcolor = "red";
  64. } else if (i === 1) {
  65. pcolor = "green"
  66. } else if (i === 2) {
  67. pcolor = "blue"
  68. }
  69. datasets[i]["borderColor"] = window.chartColors[pcolor];
  70. datasets[i]["backgroundColor"] = pcolor !== "gray" ? color(window.chartColors[pcolor]).alpha(0.9).rgbString() : color(window.chartColors[pcolor]).alpha(0.1).rgbString()
  71. }
  72. console.log(datasets);
  73. scatterChartData = {
  74. datasets: datasets
  75. }
  76. var ctx = document.getElementById('canvas').getContext('2d');
  77. window.myScatter = Chart.Scatter(ctx, {
  78. data: scatterChartData,
  79. options: {
  80. title: {
  81. display: true,
  82. text: 'ISP Speed Chart'
  83. },
  84. scales: {
  85. yAxes: [{
  86. type: 'linear',
  87. position: 'bottom',
  88. ticks: {
  89. userCallback: function(tick) {
  90. return tick.toString() + 'Mbps';
  91. },
  92. },
  93. scaleLabel: {
  94. labelString: 'Speed',
  95. display: true,
  96. }
  97. }],
  98. xAxes: [{
  99. type: 'linear',
  100. ticks: {
  101. userCallback: function(tick) {
  102. return tick.toString() + ':00';
  103. }
  104. },
  105. scaleLabel: {
  106. labelString: 'Time',
  107. display: true
  108. }
  109. }]
  110. }
  111. }
  112. });
  113. }
  114. window.onload = function() {
  115. mainfunc();
  116. };
  117. </script>
  118. </body>
  119. </html>