feat(stock):优化股票迷你图刷新逻辑

- 在 Stock 组件中添加 lastPrice 属性,传递当前价格给 StockSparkLine 组件
- 在 StockSparkLine 组件中接收 lastPrice 属性,并使用它来更新 K 线图数据
- 优化 StockSparkLine 组件的渲染逻辑,使用 onMounted 和 watchEffect
This commit is contained in:
ArvinLovegood 2025-07-08 10:50:04 +08:00
parent f0314187e5
commit 3ba18e8ef2
2 changed files with 19 additions and 11 deletions

View File

@ -1815,7 +1815,7 @@ function searchStockReport(stockCode) {
</n-text> </n-text>
</n-gi> </n-gi>
<n-gi :span="6"> <n-gi :span="6">
<stock-spark-line :open-price="result['昨日收盘价']" :stock-code="result['股票代码']" :stock-name="result['股票名称']" ></stock-spark-line> <stock-spark-line :last-price="result['当前价格']" :open-price="result['昨日收盘价']" :stock-code="result['股票代码']" :stock-name="result['股票名称']" ></stock-spark-line>
</n-gi> </n-gi>
</n-grid> </n-grid>
<n-grid :cols="2" :y-gap="4" :x-gap="4"> <n-grid :cols="2" :y-gap="4" :x-gap="4">

View File

@ -1,8 +1,8 @@
<script setup> <script setup>
import {onMounted,onBeforeMount, ref} from "vue"; import {onMounted, onBeforeMount, ref, watchEffect} from "vue";
import * as echarts from 'echarts'; import * as echarts from 'echarts';
import {GetStockMinutePriceLineData} from "../../wailsjs/go/main/App"; // 使 import {GetStockMinutePriceLineData} from "../../wailsjs/go/main/App"; // 使
const {stockCode,stockName,openPrice,darkTheme} = defineProps({ const {stockCode,stockName,lastPrice,openPrice,darkTheme} = defineProps({
stockCode: { stockCode: {
type: String, type: String,
default: "" default: ""
@ -11,6 +11,10 @@ const {stockCode,stockName,openPrice,darkTheme} = defineProps({
type: String, type: String,
default: "" default: ""
}, },
lastPrice: {
type: Number,
default: 0
},
openPrice: { openPrice: {
type: Number, type: Number,
default: 0 default: 0
@ -24,6 +28,7 @@ const {stockCode,stockName,openPrice,darkTheme} = defineProps({
const chartRef=ref(); const chartRef=ref();
function setChartData(chart) { function setChartData(chart) {
//console.log("setChartData")
GetStockMinutePriceLineData(stockCode, stockName).then(result => { GetStockMinutePriceLineData(stockCode, stockName).then(result => {
//console.log("GetStockMinutePriceLineData",result) //console.log("GetStockMinutePriceLineData",result)
const priceData = result.priceData const priceData = result.priceData
@ -31,7 +36,6 @@ function setChartData(chart) {
let price = [] let price = []
let min = 0 let min = 0
let max = 0 let max = 0
let lastPrice = priceData[priceData.length - 1].price
for (let i = 0; i < priceData.length; i++) { for (let i = 0; i < priceData.length; i++) {
category.push(priceData[i].time) category.push(priceData[i].time)
price.push(priceData[i].price) price.push(priceData[i].price)
@ -112,16 +116,20 @@ function setChartData(chart) {
chart.setOption(option); chart.setOption(option);
}) })
} }
const chart =ref( null)
onMounted(() => { onMounted(() => {
chart.value = echarts.init( document.getElementById('sparkLine'+stockCode));
const chart = echarts.init( document.getElementById('sparkLine'+stockCode)); setChartData(chart.value);
setChartData(chart);
// setInterval(() => {
// setChartData(chart);
// }, 1000 * 5 );
}) })
watchEffect(() => {
//console.log(stockName,'lastPrice:', lastPrice)
setChartData(chart.value);
})
</script> </script>
<template> <template>
<div style="height: 20px;width: 100%" :id="'sparkLine'+stockCode"> <div style="height: 20px;width: 100%" :id="'sparkLine'+stockCode">