You need to enable JavaScript to run this app. 如何在Vue.js中展示来自FastAPI接口的多数据集垂直柱状图?
优惠活动
大模型
产品
解决方案
定价
更多

如何在Vue.js中展示来自FastAPI接口的多数据集垂直柱状图?

Vue.js对接FastAPI展示股票数据对比

以下是实现实际股票数据与预测股票数据并排展示的具体方案,包含多种展示形式供你选择:

一、前端项目准备(基于Vue3 + Vite)

如果还没有Vue项目,先通过Vite快速创建:

npm create vite@latest stock-dashboard -- --template vue
cd stock-dashboard
npm install

二、编写对比展示组件

在src/components/StockComparison.vue中创建组件,实现数据获取与多种并排展示方式:

<template>
  <div class="stock-container">
    <h2>股票实际值 vs 预测值对比</h2>

    <!-- 方式1:独立表格并排展示 -->
    <div class="table-group">
      <div class="table-wrapper">
        <h3>实际股票数据</h3>
        <table>
          <thead>
            <tr>
              <th>日期</th>
              <th>实际值</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="item in stockData" :key="item.date">
              <td>{{ item.date }}</td>
              <td>{{ item.actual_stock }}</td>
            </tr>
          </tbody>
        </table>
      </div>

      <div class="table-wrapper">
        <h3>预测股票数据</h3>
        <table>
          <thead>
            <tr>
              <th>日期</th>
              <th>预测值</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="item in stockData" :key="item.date">
              <td>{{ item.date }}</td>
              <td>{{ item.predicted_stock }}</td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>

    <!-- 方式2:合并表格对比(更直观) -->
    <div class="combined-table">
      <h3>实际vs预测 合并对比</h3>
      <table>
        <thead>
          <tr>
            <th>日期</th>
            <th>实际股票值</th>
            <th>预测股票值</th>
            <th>差值</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="item in stockData" :key="item.date">
            <td>{{ item.date }}</td>
            <td>{{ item.actual_stock }}</td>
            <td>{{ item.predicted_stock }}</td>
            <td :class="{ positive: item.actual_stock > item.predicted_stock, negative: item.actual_stock < item.predicted_stock }">
              {{ (item.actual_stock - item.predicted_stock).toFixed(2) }}
            </td>
          </tr>
        </tbody>
      </table>
    </div>

    <!-- 方式3:折线图对比(可选) -->
    <div class="chart-container" v-if="stockData.length">
      <h3>股票走势对比图</h3>
      <div id="stockChart" style="width: 100%; height: 400px;"></div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
// 如需折线图,先安装echarts:npm install echarts
// import * as echarts from 'echarts'

const stockData = ref([])

// 从FastAPI拉取数据
const fetchStockData = async () => {
  try {
    // 替换为你的FastAPI服务器地址,默认是http://localhost:8000/data
    const res = await fetch('http://localhost:8000/data')
    if (!res.ok) throw new Error('数据请求失败')
    stockData.value = await res.json()
    // 渲染折线图(可选)
    // renderChart()
  } catch (err) {
    console.error('获取数据出错:', err)
  }
}

// 折线图渲染逻辑(可选)
// const renderChart = () => {
//   const chartDom = document.getElementById('stockChart')
//   const chart = echarts.init(chartDom)
//   const option = {
//     xAxis: { type: 'category', data: stockData.value.map(i => i.date) },
//     yAxis: { type: 'value' },
//     series: [
//       { name: '实际值', type: 'line', data: stockData.value.map(i => i.actual_stock), lineStyle: { color: '#e63946' } },
//       { name: '预测值', type: 'line', data: stockData.value.map(i => i.predicted_stock), lineStyle: { color: '#457b9d', type: 'dashed' } }
//     ]
//   }
//   chart.setOption(option)
// }

onMounted(() => {
  fetchStockData()
})
</script>

<style scoped>
.stock-container {
  max-width: 1200px;
  margin: 2rem auto;
  padding: 0 1rem;
}

.table-group {
  display: flex;
  gap: 2rem;
  margin-bottom: 2rem;
  flex-wrap: wrap;
}

.table-wrapper {
  flex: 1;
  min-width: 300px;
}

table {
  width: 100%;
  border-collapse: collapse;
  border: 1px solid #e5e7eb;
}

th, td {
  padding: 0.8rem;
  text-align: left;
  border-bottom: 1px solid #e5e7eb;
}

th {
  background-color: #f3f4f6;
}

.combined-table table {
  margin-top: 1rem;
}

.positive { color: #16a34a; font-weight: 600; }
.negative { color: #dc2626; font-weight: 600; }

.chart-container { margin-top: 2rem; }
</style>

三、在主页面引入组件

修改src/App.vue,引入并使用刚才创建的组件:

<template>
  <StockComparison />
</template>

<script setup>
import StockComparison from './components/StockComparison.vue'
</script>

四、运行项目

  1. 启动FastAPI后端:确保后端代码保存为main.py,执行命令
uvicorn main:app --reload
  1. 启动Vue前端:
npm run dev

访问Vue项目的默认地址http://localhost:5173即可看到数据对比展示。

注意事项

  • 确认FastAPI的CORS配置中allow_origins包含Vue项目的地址(你的后端已经配置了http://localhost:5173,无需修改)
  • 如果使用折线图,需要先安装echarts依赖:npm install echarts,然后取消组件中相关代码的注释
  • 若FastAPI部署在其他服务器,替换fetch中的地址为实际服务器IP+端口

内容的提问来源于stack exchange,提问作者Péter Szilvási

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 21:40:53