HEX
Server: Apache/2.4.37 (CentOS Stream) OpenSSL/1.1.1k
System: Linux ysnet.com.tw 4.18.0-553.5.1.el8.x86_64 #1 SMP Tue May 21 05:46:01 UTC 2024 x86_64
User: test (521)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: //var/www/test/mrtg.html
<h2>Bridge-LAN 即時流量</h2>
<div id="loading">資料載入中...</div>
<canvas id="trafficChart" width="600" height="300" style="display:none;"></canvas>

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
  fetch('/get_traffic.php')  // <<✅ 注意這裡是絕對路徑,不要寫相對路徑
    .then(res => res.json())
    .then(data => {
      if (data.error) {
        document.getElementById('loading').innerText = data.error;
        return;
      }

      const rx = (data.rx / 1024).toFixed(2); // kbps
      const tx = (data.tx / 1024).toFixed(2);

      document.getElementById('loading').style.display = 'none';
      document.getElementById('trafficChart').style.display = 'block';

      new Chart(document.getElementById('trafficChart').getContext('2d'), {
        type: 'bar',
        data: {
          labels: ['下載 RX', '上傳 TX'],
          datasets: [{
            label: '目前速率 (kbps)',
            data: [rx, tx],
            backgroundColor: ['#4bc0c0', '#ff9f40']
          }]
        },
        options: {
          plugins: { legend: { display: false }},
          scales: {
            y: {
              beginAtZero: true,
              title: { display: true, text: 'kbps' }
            }
          }
        }
      });
    })
    .catch(err => {
      document.getElementById('loading').innerText = '資料取得失敗';
      console.error(err);
    });
</script>