historic-stock

This commit is contained in:
张 元山 2025-05-23 11:21:02 +08:00
parent 17753d48b3
commit 77fe06694c
2 changed files with 199 additions and 14 deletions

View File

@ -1210,6 +1210,9 @@ let defaultTableData = [
"volume": "561"
}
]
defaultTableData.map(item=>item.close='')
defaultTableData.map(item => {
item.close = ''
item.adjClose = ''
})
export default defaultTableData;

View File

@ -1,9 +1,13 @@
<template>
<div class="historic-data-container">
<img src="@/assets/image/historic-stock.png" alt="1" />
<div class="historic-data-container" style="margin-bottom: 100px">
<img
src="@/assets/image/historic-stock.png"
alt="1"
style="max-width: 100%; margin: 0 auto"
/>
<div class="header">
<h1 class="title">Historical Data</h1>
<div class="header mt-[20px]">
<div class="title">Historical Data</div>
<div class="filter-container">
<n-dropdown
trigger="click"
@ -32,20 +36,58 @@
<n-data-table
:columns="columns"
:data="state.tableData"
:data="paginatedData"
:bordered="false"
:single-line="false"
/>
<div class="pagination-container">
<n-button class="page-btn prev-btn" @click="handlePrevPage">
<n-icon><chevron-back-outline /></n-icon> Previous
</n-button>
<div class="page-info">
Page {{ state.currentPage }} of {{ totalPages }}
</div>
<div class="right-controls">
<n-dropdown
trigger="click"
:options="pageSizeOptions"
@select="handlePageSizeChange"
>
<n-button class="rows-dropdown">
{{ state.pageSize }} Rows <n-icon><chevron-down-outline /></n-icon>
</n-button>
</n-dropdown>
<n-button class="page-btn next-btn" @click="handleNextPage">
Next <n-icon><chevron-forward-outline /></n-icon>
</n-button>
</div>
</div>
<div class="back-to-top-link">
<a href="#" @click.prevent="scrollToTop"
>Back to Top <n-icon><arrow-up-outline /></n-icon
></a>
</div>
</div>
</template>
<script setup>
import { NDataTable, NButton, NDropdown, NIcon } from "naive-ui";
import { reactive, onMounted, h } from "vue";
import { reactive, onMounted, h, computed } from "vue";
import axios from "axios";
import { ChevronDownOutline } from "@vicons/ionicons5";
import {
ChevronDownOutline,
ChevronBackOutline,
ChevronForwardOutline,
ArrowUpOutline,
} from "@vicons/ionicons5";
import defaultTableData from "../data";
console.log("defaultTableData", defaultTableData);
//
const periodOptions = [
{ label: "Daily", key: "Daily" },
@ -64,10 +106,32 @@ const durationOptions = [
{ label: "10 Years", key: "10 Years" },
];
//
const pageSizeOptions = [
{ label: "50", key: 50 },
{ label: "100", key: 100 },
{ label: "500", key: 500 },
{ label: "1000", key: 1000 },
];
const state = reactive({
selectedPeriod: "Daily",
selectedDuration: "3 Months",
tableData: [],
currentPage: 1,
pageSize: 50,
});
//
const totalPages = computed(() => {
return Math.ceil(state.tableData.length / state.pageSize);
});
//
const paginatedData = computed(() => {
const start = (state.currentPage - 1) * state.pageSize;
const end = start + state.pageSize;
return state.tableData.slice(start, end);
});
//
@ -130,24 +194,75 @@ const handleDurationChange = (key) => {
getPageData();
};
//
const handlePrevPage = () => {
if (state.currentPage === 1) {
return;
}
state.currentPage--;
};
const handleNextPage = () => {
if (state.currentPage >= totalPages.value) {
return;
}
state.currentPage++;
};
const handlePageSizeChange = (size) => {
state.pageSize = size;
state.currentPage = 1; //
};
//
const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: "smooth",
});
};
onMounted(() => {
getPageDefaultData();
});
const getPageDefaultData = async () => {
try {
// state.selectedPeriodstate.selectedDurationAPI
let url =
"https://stockanalysis.com/api/symbol/a/OTC-MINM/history?type=chart";
const res = await axios.get(url);
let originalData = res.data.data;
//
// "Nov 26, 2024"
let calcApiData = originalData.map((item) => [
new Date(item[0]).toISOString().split("T")[0], // YYYY-MM-DD
new Date(item[0]).toLocaleDateString("en-US", {
month: "short",
day: "numeric",
year: "numeric",
}),
item[1],
]);
console.log("接口数据", calcApiData);
state.tableData = defaultTableData;
// 使APIdefaultTableDatacloseadjClose
const updatedTableData = defaultTableData.map((tableItem) => {
// API
const matchedApiData = calcApiData.find(
(apiItem) => apiItem[0] === tableItem.date
);
if (matchedApiData) {
// closeadjClose
return {
...tableItem,
close: matchedApiData[1].toFixed(2),
adjClose: matchedApiData[1].toFixed(2),
};
}
return tableItem;
});
state.tableData = updatedTableData;
} catch (error) {
console.error("获取数据失败", error);
}
@ -162,7 +277,8 @@ const getPageData = async () => {
<style scoped lang="scss">
.historic-data-container {
padding: 20px;
width: 100%;
max-width: 1200px;
margin: 0 auto;
.header {
display: flex;
@ -171,7 +287,7 @@ const getPageData = async () => {
margin-bottom: 20px;
.title {
font-size: 24px;
font-size: 40px;
font-weight: bold;
margin: 0;
}
@ -182,6 +298,72 @@ const getPageData = async () => {
}
}
.pagination-container {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 20px;
padding: 10px 16px;
border-radius: 4px;
background-color: #ffffff;
.page-btn {
display: flex;
align-items: center;
gap: 5px;
padding: 6px 12px;
font-size: 20px;
&.prev-btn {
margin-right: auto;
}
&.next-btn {
margin-left: 10px;
}
&:disabled {
opacity: 0.5;
cursor: not-allowed;
}
}
.page-info {
font-size: 16px;
color: #374151;
margin: 0 10px;
}
.right-controls {
display: flex;
align-items: center;
.rows-dropdown {
font-size: 16px;
}
}
}
.back-to-top-link {
display: flex;
justify-content: center;
margin-top: 16px;
a {
display: flex;
align-items: center;
gap: 5px;
color: #2563eb;
font-size: 20px;
font-weight: bold;
text-decoration: none;
&:hover {
text-decoration: underline;
}
}
}
:deep(.n-data-table) {
.n-data-table-td {
padding: 12px 8px;