From 7937a902c7dc20615be0f0cd8896f3528d3e5302 Mon Sep 17 00:00:00 2001 From: satori Date: Sat, 7 Dec 2024 06:53:37 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=B9=B6=E7=BB=98=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.js | 92 ++++++++++++++++++++++------------------------------ package.json | 1 + server.js | 5 +++ 3 files changed, 44 insertions(+), 54 deletions(-) diff --git a/main.js b/main.js index ce809bb..afbae56 100644 --- a/main.js +++ b/main.js @@ -1,64 +1,49 @@ import Chart from 'chart.js/auto' +import { createElement, div, table, thead, tbody, tr, th, td } from '@laniakeasupercluster/widgets' const rest = await fetch('http://localhost:2000/api').then(res => res.json()) -const table = document.createElement('table') +document.body.appendChild(div.childs([ + div.text('航班信息'), + table.childs([ + thead.childs([ + tr.childs([ + th.text('ID'), + th.text('航班号'), + th.text('航空公司'), + th.text('起飞机场'), + th.text('到达机场'), + th.text('数据') + ]) + ]), + tbody.childs( + rest.map(row => tr.childs([ + td.text(row.flight_id), + td.text(row.flight_number), + td.text(row.airline), + td.text(row.origin), + td.text(row.destination), + td.text(row.shuju) + ])) + ) + ]) +])) -// 添加表头 -const thead = document.createElement('thead') -const tr = document.createElement('tr') -for (let key in rest[0]) { - const th = document.createElement('th') - th.textContent = key - tr.appendChild(th) -} -thead.appendChild(tr) -table.appendChild(thead) - -// 添加数据 -for (let row of rest) { - const tr = document.createElement('tr') - for (let key in row) { - const td = document.createElement('td') - td.textContent = row[key] - tr.appendChild(td) - } - table.appendChild(tr) -} - -// 写入页面 -document.body.appendChild(table) - - -// 生成随机数据 -function generateRandomData(num, minPrice, maxPrice) { - const data = []; - const today = new Date(); - for (let i = 0; i < num; i++) { - const randomDays = Math.floor(Math.random() * 30); // 随机天数 - const newDate = new Date(today.getTime() + randomDays * 24 * 60 * 60 * 1000); // 增加随机天数 - const price = Math.floor(Math.random() * (maxPrice - minPrice + 1)) + minPrice; // 随机价格 - data.push({ - date: newDate.toISOString().split('T')[0], // 格式化为 YYYY-MM-DD - price: price - }); - } - return data; -} - -// 生成 10 条随机数据 -const numEntries = 10; -const data = generateRandomData(numEntries, 100, 500); +const metrics = await fetch('http://localhost:2000/api/metrics').then(res => res.json()) // 创建图表 -const ctx = document.createElement('canvas'); -document.body.appendChild(ctx); -new Chart(ctx, { +const context = createElement({ + width: 800, + height: 200, +}, 'canvas') + +document.body.appendChild(context) +new Chart(context, { type: 'line', data: { - labels: data.map(row => row.date), + labels: metrics.map(row => new Date(row.date).toLocaleDateString()), // 显示日期 datasets: [{ - label: '航班价格', - data: data.map(row => row.price), + label: '航班数量', + data: metrics.map(row => row.count), // 使用 count 作为数据 backgroundColor: 'rgba(255, 99, 132, 0.2)', // 曲线下方填充颜色(透明度0.2) borderColor: 'rgba(255, 99, 132, 1)', // 曲线颜色 borderWidth: 2, // 曲线宽度 @@ -73,5 +58,4 @@ new Chart(ctx, { } } } -}); - +}) \ No newline at end of file diff --git a/package.json b/package.json index b9aa5fb..65c5c42 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "author": "", "license": "ISC", "dependencies": { + "@laniakeasupercluster/widgets": "^1.1.5", "chart.js": "^4.4.7", "cors": "^2.8.5", "express": "^4.21.2", diff --git a/server.js b/server.js index 278b6ba..4c9283e 100644 --- a/server.js +++ b/server.js @@ -26,6 +26,11 @@ app.get('/api', async (req, res) => { res.json(rows) }) +app.get('/api/metrics', async (req, res) => { + const [rows] = await db.query('SELECT date, COUNT(*) AS count FROM flight_price_history GROUP BY date') + res.json(rows) +}) + // 启动应用 app.listen(2000, () => { console.log('服务已启动成功 http://localhost:2000/api')