You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2377 lines
66 KiB

4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
  1. <!-- @format -->
  2. <template>
  3. <!-- 容器 -->
  4. <view class="container">
  5. <!-- 标题 -->
  6. <view class="title-container">
  7. <view class="title">
  8. <image class="back-homepage-btn" src="/static/marketSituation-image/back.png" mode="返回按钮" @click="backToHomepage()"> </image>
  9. <view class="mid-title">
  10. <view class="arrow-left"> </view>
  11. <view class="stock-id">
  12. <view class="stock-name"> {{ stockInformation.stockName }} </view>
  13. <view class="stock-code"> {{ stockInformation.stockCode }} </view>
  14. </view>
  15. <view class="arrow-right"> </view>
  16. </view>
  17. <image class="search" src="/static/marketSituation-image/search.png" mode="搜索" @click="search()"> </image>
  18. <view class="more" @click="getMore()">···</view>
  19. </view>
  20. </view>
  21. <view class="body">
  22. <!-- 股票信息栏 -->
  23. <view class="stock-information" @click="openStockDetail()">
  24. <view v-if="isStockDetail" class="stock-detail-container" @click.stop @click="closeStockDetail()">
  25. <view class="stock-detail" @click.stop>
  26. <view class="first-column">
  27. <view class="first-column-data" v-for="item in firstColumData" :key="item">
  28. <view class="stock-detail-title">
  29. {{ item.title }}
  30. </view>
  31. <view class="stock-detail-value">
  32. {{ item.value }}
  33. </view>
  34. </view>
  35. </view>
  36. <view class="second-column">
  37. <view class="second-column-data" v-for="item in secondColumnData" :key="item">
  38. <view class="stock-detail-title">
  39. {{ item.title }}
  40. </view>
  41. <view class="stock-detail-value">
  42. {{ item.value }}
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. </view>
  48. <view class="stock-current-data">
  49. <view class="stock-current-price" :class="confirmStockColor(stockInformation.currentPrice, stockInformation.lastDayStockClosePrice)">
  50. {{ Number(stockInformation.currentPrice).toFixed(2) }}
  51. </view>
  52. <view class="stock-current-other">
  53. <view class="stock-current-value" :class="confirmStockColor(stockInformation.currentValue)">
  54. {{ Number(stockInformation.currentValue).toFixed(2) }}
  55. </view>
  56. <view class="stock-current-ratio" :class="confirmStockColor(stockInformation.currentRatio)"> {{ Number(stockInformation.currentRatio).toFixed(2) }}% </view>
  57. </view>
  58. </view>
  59. <view class="stock-other-data">
  60. <view class="first-line">
  61. <view class="high-price">
  62. <view class="value" :class="confirmStockColor(stockInformation.highPrice, stockInformation.lastDayStockClosePrice)">
  63. {{ Number(stockInformation.highPrice).toFixed(2) }}
  64. </view>
  65. </view>
  66. <view class="volume">
  67. <view class="value">
  68. {{ utils.formatStockNumber(stockInformation.volume, 2) }}
  69. </view>
  70. </view>
  71. <view class="volume-ratio">
  72. 量比
  73. <view class="value">
  74. {{ Number(stockInformation.volumeRatio).toFixed(2) }}
  75. </view>
  76. </view>
  77. </view>
  78. <view class="second-line">
  79. <view class="low-price">
  80. <view class="value" :class="confirmStockColor(stockInformation.lowPrice, stockInformation.lastDayStockClosePrice)">
  81. {{ Number(stockInformation.lowPrice).toFixed(2) }}
  82. </view>
  83. </view>
  84. <view class="amount">
  85. <view class="value">
  86. {{ utils.formatStockNumber(stockInformation.amount, 2) }}
  87. </view>
  88. </view>
  89. <view class="market-earn">
  90. 市盈
  91. <view class="value">
  92. {{ Number(stockInformation.marketEarn).toFixed(2) }}
  93. </view>
  94. </view>
  95. </view>
  96. <view class="third-line">
  97. <view class="open-price">
  98. <view class="value" :class="confirmStockColor(stockInformation.openPrice, stockInformation.lastDayStockClosePrice)">
  99. {{ Number(stockInformation.openPrice).toFixed(2) }}
  100. </view>
  101. </view>
  102. <view class="turnover-ratio">
  103. <view class="value"> {{ Number(stockInformation.turnoverRatio).toFixed(2) }}% </view>
  104. </view>
  105. <view class="market-value">
  106. 市值
  107. <view class="value">
  108. {{ utils.formatStockNumber(stockInformation.marketValue, 2) }}
  109. </view>
  110. </view>
  111. </view>
  112. </view>
  113. </view>
  114. <!-- 股票图表 -->
  115. <view class="stock-chart">
  116. <view class="stock-kline-tab">
  117. <!-- 1:分时 2:日K 3:周K 4:月K -->
  118. <view class="tab-time" :class="{ 'tab-selected': klineTab == 1 }" @click="selectKlineTab(1)"> 分时 </view>
  119. <view class="tab-day" :class="{ 'tab-selected': klineTab == 2 }" @click="selectKlineTab(2)"> 日K </view>
  120. <view class="tab-week" :class="{ 'tab-selected': klineTab == 3 }" @click="selectKlineTab(3)"> 周K </view>
  121. <view class="tab-month" :class="{ 'tab-selected': klineTab == 4 }" @click="selectKlineTab(4)"> 月K </view>
  122. <view class="tab-more" :class="{ 'tab-selected': klineTab != 1 && klineTab != 2 && klineTab != 3 && klineTab != 4 }" @click="isMoreTabs ? closeMoreTabs() : openMoreTabs()">
  123. <view class="more-no-choose" v-if="klineTab == 1 || klineTab == 2 || klineTab == 3 || klineTab == 4"> 更多 </view>
  124. <view class="more-choose" v-else>
  125. {{ chooseTabName(klineTab) }}
  126. </view>
  127. <view :class="[isMoreTabs ? 'arrow-down' : 'arrow-up']"> </view>
  128. </view>
  129. <view class="tab-setting">
  130. <image class="tab-setting-img" src="/static/marketSituation-image/marketCondition-image/setting2.png" mode="设置"></image>
  131. </view>
  132. </view>
  133. <view v-if="isMoreTabs" class="moreTabsContainer">
  134. <view v-for="item in moreTabsData" :key="item" class="moreTabItem" :class="{ 'tab-selected': klineTab == item.value }" @click="selectMoreTab(item.value)">
  135. {{ item.title }}
  136. </view>
  137. </view>
  138. <view class="stock-kline">
  139. <view v-if="klineTab === 1 || klineTab === 2 || klineTab === 3 || klineTab === 4 || klineTab === 5 || klineTab === 6 || klineTab === 7 || klineTab === 8 || klineTab === 9" class="time-chart-container" style="position: relative">
  140. <!-- 主图Canvas -->
  141. <canvas
  142. canvas-id="stockChart"
  143. class="stock-chart"
  144. :width="canvasWidth"
  145. :height="canvasHeight"
  146. :style="{
  147. width: canvasWidth + 'px',
  148. height: canvasHeight + 'px',
  149. pointerEvents: 'none',
  150. }"
  151. will-read-frequently="true"
  152. ></canvas>
  153. <!-- 动态数据Canvas层 -->
  154. <canvas
  155. canvas-id="dynamicCanvas"
  156. id="dynamicCanvas"
  157. :width="canvasWidth"
  158. :height="canvasHeight"
  159. :style="{
  160. width: canvasWidth + 'px',
  161. height: canvasHeight + 'px',
  162. pointerEvents: 'none',
  163. position: 'absolute',
  164. top: '0',
  165. left: '0',
  166. }"
  167. will-read-frequently="true"
  168. ></canvas>
  169. <!-- 十字准心Canvas层 -->
  170. <canvas
  171. canvas-id="crosshairCanvas"
  172. id="crosshairCanvas"
  173. :width="canvasWidth"
  174. :height="canvasHeight"
  175. :style="{
  176. width: canvasWidth + 'px',
  177. height: canvasHeight + 'px',
  178. position: 'absolute',
  179. top: '0',
  180. left: '0',
  181. }"
  182. will-read-frequently="true"
  183. @touchstart="touchStart"
  184. @touchmove="touchMove"
  185. @touchend="touchEnd"
  186. ></canvas>
  187. </view>
  188. <!-- K线图区域 -->
  189. <view class="test" v-else-if="klineTab === 10">
  190. <button @click="startTcp()">接收消息</button>
  191. <button @click="sendStopTimeData()">停止消息</button>
  192. <button @click="sendTcpMessage('real_time')">实时行情推送</button>
  193. <button @click="sendTcpMessage('init_real_time')">初始化获取行情历史数据</button>
  194. <button @click="sendTcpMessage('stop_real_time')">停止实时推送</button>
  195. </view>
  196. <view v-else class="kline-chart-container">
  197. <text>K线图开发中...</text>
  198. </view>
  199. </view>
  200. </view>
  201. </view>
  202. <view class="bottomTool">
  203. <view class="index">
  204. <image class="icon" src="/static/marketSituation-image/marketCondition-image/index.png" mode="指标仓库图标"> </image>
  205. 指标仓库
  206. </view>
  207. <view class="function">
  208. <image class="icon" src="/static/marketSituation-image/marketCondition-image/function.png" mode="功能图标"> </image>
  209. 功能
  210. </view>
  211. <view class="favorites">
  212. <image class="icon" src="/static/marketSituation-image/marketCondition-image/favorites.png" mode="加自选图标"></image>
  213. 加自选
  214. </view>
  215. </view>
  216. </view>
  217. </template>
  218. <script setup>
  219. import { ref, reactive, computed, onMounted, watch, nextTick, onUnmounted, getCurrentInstance } from "vue";
  220. import { onLoad } from "@dcloudio/uni-app";
  221. const instance = getCurrentInstance();
  222. import { prevClosePrice, timeData as testTimeData, klineData as testKlineData } from "@/common/stockTimeInformation.js";
  223. import { throttle } from "@/common/util.js";
  224. import { HCharts } from "@/common/canvasMethod.js";
  225. import tcpConnection, { TCPConnection, TCP_CONFIG } from "@/api/tcpConnection.js";
  226. // TCP相关响应式变量
  227. const tcpConnected = ref(false);
  228. const connectionListener = ref(null);
  229. const messageListener = ref(null);
  230. // 股票信息栏变量
  231. const stockInformation = ref({
  232. stockName: "----", //股票名称
  233. stockCode: "------", //股票代码
  234. lastDayStockClosePrice: 0.0, //前一日收盘价
  235. currentPrice: 0.0, //当前股价
  236. currentValue: 0.0, //涨跌额度
  237. currentRatio: 0.0, //涨跌幅度
  238. highPrice: 0.0, //最高价
  239. lowPrice: 0.0, //最低价
  240. openPrice: 0.0, //开盘价
  241. closePrice: 0.0, //收盘价
  242. volume: 0.0, //成交量
  243. volumeRatio: 0.0, //成交量比
  244. amount: 0.0, //成交额
  245. marketEarn: 0.0, //市盈
  246. turnoverRatio: 0.0, //换手率
  247. marketValue: 0.0, //市值
  248. });
  249. // 是否展开股票信息细节栏的判断变量
  250. const isStockDetail = ref(false);
  251. // 股票信息细节内容变量
  252. const firstColumData = [
  253. {
  254. title: "振幅",
  255. value: 0,
  256. },
  257. {
  258. title: "现手",
  259. value: 0,
  260. },
  261. {
  262. title: "内盘",
  263. value: 0,
  264. },
  265. {
  266. title: "外盘",
  267. value: 0,
  268. },
  269. {
  270. title: "流通股",
  271. value: 0,
  272. },
  273. {
  274. title: "每股净资产",
  275. value: 0,
  276. },
  277. {
  278. title: "净资产收益率",
  279. value: 0,
  280. },
  281. {
  282. title: "总股本",
  283. value: 0,
  284. },
  285. {
  286. title: "总资本",
  287. value: 0,
  288. },
  289. {
  290. title: "总市值",
  291. value: 0,
  292. },
  293. {
  294. title: "一个月最高",
  295. value: 0,
  296. },
  297. {
  298. title: "一个月最低",
  299. value: 0,
  300. },
  301. ];
  302. const secondColumnData = [
  303. {
  304. title: "均价",
  305. value: 0,
  306. },
  307. {
  308. title: "昨收",
  309. value: 0,
  310. },
  311. {
  312. title: "委比",
  313. value: 0,
  314. },
  315. {
  316. title: "委买",
  317. value: 0,
  318. },
  319. {
  320. title: "委卖",
  321. value: 0,
  322. },
  323. {
  324. title: "市盈利(静)",
  325. value: 0,
  326. },
  327. {
  328. title: "市盈利(动)",
  329. value: 0,
  330. },
  331. {
  332. title: "市净率",
  333. value: 0,
  334. },
  335. {
  336. title: "涨停价",
  337. value: 0,
  338. },
  339. {
  340. title: "跌停价",
  341. value: 0,
  342. },
  343. {
  344. title: "一年最高",
  345. value: 0,
  346. },
  347. {
  348. title: "一年最低",
  349. value: 0,
  350. },
  351. ];
  352. // 是否展开更多Tab的判断变量
  353. const isMoreTabs = ref(false);
  354. const moreTabsData = ref([
  355. {
  356. title: "1分",
  357. value: 5,
  358. },
  359. {
  360. title: "5分",
  361. value: 6,
  362. },
  363. {
  364. title: "15分",
  365. value: 7,
  366. },
  367. {
  368. title: "30分",
  369. value: 8,
  370. },
  371. {
  372. title: "60分",
  373. value: 9,
  374. },
  375. {
  376. title: "季K",
  377. value: 10,
  378. },
  379. {
  380. title: "年K",
  381. value: 11,
  382. },
  383. ]);
  384. // 股票当前选中的K线类型Tab
  385. // 1:分时 2:日K 3:周K 4:月K
  386. const klineTab = ref(1);
  387. const startTcp = () => {
  388. try {
  389. removeTcpListeners();
  390. disconnectTcp();
  391. initTcpListeners();
  392. connectTcp();
  393. } catch (error) {
  394. console.error("建立连接并设置监听:", error);
  395. uni.showToast({
  396. title: "建立连接并设置监听",
  397. icon: "none",
  398. duration: 1500,
  399. });
  400. }
  401. };
  402. const sendStopTimeData = () => {
  403. disconnectTcp();
  404. removeTcpListeners();
  405. };
  406. // 确定股票数据的颜色方法
  407. const confirmStockColor = (price, lastDayStockClosePrice) => {
  408. if (typeof lastDayStockClosePrice === "undefined") {
  409. if (price == 0) {
  410. return "price-none";
  411. } else if (price > 0) {
  412. return "price-up";
  413. } else {
  414. return "price-down";
  415. }
  416. } else {
  417. if (price == lastDayStockClosePrice) {
  418. return "price-none";
  419. } else if (price > lastDayStockClosePrice) {
  420. return "price-up";
  421. } else {
  422. return "price-down";
  423. }
  424. }
  425. };
  426. // 股票K线类型方法
  427. const selectKlineTab = (tabId) => {
  428. klineTab.value = tabId;
  429. if (klineTab.value) {
  430. sendTcpMessage("stop_real_time");
  431. }
  432. switch (klineTab.value) {
  433. case 1:
  434. sendTcpMessage("init_real_time");
  435. break;
  436. case 2:
  437. sendTcpMessage("daily_data");
  438. break;
  439. case 3:
  440. sendTcpMessage("weekly_data");
  441. break;
  442. case 4:
  443. sendTcpMessage("monthly_data");
  444. break;
  445. case 5:
  446. sendTcpMessage("daily_one_minutes_data");
  447. break;
  448. case 6:
  449. sendTcpMessage("daily_five_minutes_data");
  450. break;
  451. case 7:
  452. sendTcpMessage("daily_fifteen_minutes_data");
  453. break;
  454. case 8:
  455. sendTcpMessage("daily_thirty_minutes_data");
  456. break;
  457. case 9:
  458. sendTcpMessage("daily_sixty_minutes_data");
  459. break;
  460. case 10:
  461. uni.showToast({
  462. title: "暂无季K数据",
  463. icon: "none",
  464. duration: 2000,
  465. });
  466. break;
  467. case 11:
  468. uni.showToast({
  469. title: "暂无年K数据",
  470. icon: "none",
  471. duration: 2000,
  472. });
  473. break;
  474. default:
  475. break;
  476. }
  477. initCanvas();
  478. // startAddDataTimer();
  479. };
  480. // 返回按钮
  481. const backToHomepage = () => {
  482. const pages = getCurrentPages();
  483. if (pages.length > 1) {
  484. uni.navigateBack();
  485. } else {
  486. // 如果没有上一页,跳转到首页
  487. uni.reLaunch({
  488. url: "/pages/home/home",
  489. });
  490. }
  491. };
  492. const openStockDetail = () => {
  493. isStockDetail.value = true;
  494. };
  495. const closeStockDetail = () => {
  496. isStockDetail.value = false;
  497. };
  498. const openMoreTabs = () => {
  499. isMoreTabs.value = true;
  500. };
  501. const closeMoreTabs = () => {
  502. isMoreTabs.value = false;
  503. };
  504. const selectMoreTab = (value) => {
  505. selectKlineTab(value);
  506. };
  507. const chooseTabName = () => {
  508. if (klineTab.value === 5) {
  509. return "1分";
  510. } else if (klineTab.value === 6) {
  511. return "5分";
  512. } else if (klineTab.value === 7) {
  513. return "15分";
  514. } else if (klineTab.value === 8) {
  515. return "30分";
  516. } else if (klineTab.value === 9) {
  517. return "60分";
  518. } else if (klineTab.value === 10) {
  519. return "季K";
  520. } else if (klineTab.value === 11) {
  521. return "年K";
  522. }
  523. };
  524. // 画布对象
  525. const canvasWidth = ref(100);
  526. const canvasHeight = ref(100);
  527. const CANVAS_BACKGROUND_COLOR = "#fff";
  528. const TEXT_COLOR1 = "#000";
  529. const TEXT_COLOR2 = "red";
  530. const LINE_COLOR = "#e5e5e5";
  531. const timeChartObject = ref({
  532. min: 0,
  533. max: 0,
  534. });
  535. const klineChartObject = ref({
  536. min: 0,
  537. max: 0,
  538. });
  539. const chartRange = ref();
  540. // 开盘时间
  541. const openTime = "09:30";
  542. // 收盘时间
  543. const closeTime = "15:00";
  544. const ctx = ref(null);
  545. const dynamicCtx = ref(null); // 动态Canvas上下文
  546. const crosshairCtx = ref(null); // 十字准心Canvas上下文
  547. const pixelRatio = ref(1);
  548. // 触屏对象
  549. const touchState = reactive({
  550. startX: 0,
  551. startY: 0,
  552. isMoving: false,
  553. moveDistance: 0,
  554. moveThreshold: 10, //移动阈值(用于判断是否在点击)
  555. scale: 1, // 当前缩放比例
  556. minScale: 0.3, // 最小缩放比例
  557. maxScale: 5, // 最大缩放比例
  558. baseVisibleCount: 40, // 基准可见K线数量
  559. offset: 0, // 数据偏移量
  560. isZooming: false, // 是否正在缩放
  561. initialDistance: 0, // 初始两指距离
  562. initialScale: 1, // 初始缩放比例
  563. });
  564. // 计算当前可见的K线数量
  565. const visibleCount = computed(() => {
  566. return Math.floor(touchState.baseVisibleCount / touchState.scale);
  567. });
  568. // 计算当前显示的数据范围
  569. const visibleDataRange = computed(() => {
  570. const start = Math.max(0, klineData.value.length - visibleCount.value - touchState.offset);
  571. const end = Math.min(klineData.value.length, start + visibleCount.value);
  572. return {
  573. start,
  574. end,
  575. };
  576. });
  577. // 获取当前可见的数据
  578. const visibleKlineData = computed(() => {
  579. const { start, end } = visibleDataRange.value;
  580. return klineData.value.slice(start, end);
  581. });
  582. // 计算两点之间的距离
  583. const getDistance = (touch1, touch2) => {
  584. const dx = touch1.x - touch2.x;
  585. const dy = touch1.y - touch2.y;
  586. return Math.sqrt(dx * dx + dy * dy);
  587. };
  588. // 十字准线相关状态
  589. const crosshair = reactive({
  590. show: false,
  591. x: 0,
  592. y: 0,
  593. currentData: null,
  594. snapToData: true,
  595. });
  596. // 绘制网格和坐标轴
  597. const grid = [
  598. {
  599. top: 20,
  600. bottom: canvasHeight.value * 0.5,
  601. left: 5,
  602. right: 5,
  603. lineColor: LINE_COLOR,
  604. lineWidth: 1,
  605. horizontalLineNum: 5,
  606. verticalLineNum: 5,
  607. },
  608. {
  609. top: canvasHeight.value * 0.5 + 20,
  610. bottom: 20,
  611. left: 5,
  612. right: 5,
  613. lineColor: LINE_COLOR,
  614. lineWidth: 1,
  615. horizontalLineNum: 3,
  616. verticalLineNum: 5,
  617. },
  618. ];
  619. // 绘制网格和坐标轴
  620. const dayGrid = [
  621. {
  622. top: 20,
  623. bottom: canvasHeight.value * 0.5,
  624. left: 5,
  625. right: 5,
  626. lineColor: LINE_COLOR,
  627. lineWidth: 1,
  628. horizontalLineNum: 5,
  629. verticalLineNum: 2,
  630. },
  631. {
  632. top: canvasHeight.value * 0.5 + 20,
  633. bottom: 20,
  634. left: 5,
  635. right: 5,
  636. lineColor: LINE_COLOR,
  637. lineWidth: 1,
  638. horizontalLineNum: 3,
  639. verticalLineNum: 2,
  640. },
  641. {
  642. top: canvasHeight.value * 0.5 + 20,
  643. bottom: 20,
  644. left: 5,
  645. right: 5,
  646. lineColor: LINE_COLOR,
  647. lineWidth: 1,
  648. horizontalLineNum: 3,
  649. verticalLineNum: 2,
  650. },
  651. ];
  652. // 工具函数
  653. const utils = {
  654. // 格式化价格
  655. formatPrice(price) {
  656. return price.toFixed(2);
  657. },
  658. // 计算数据范围
  659. calculateDataRange(data, key) {
  660. if (!data || data.length === 0) {
  661. return {
  662. min: 0,
  663. max: 0,
  664. };
  665. }
  666. const values = data.map((item) => item[key]);
  667. return {
  668. min: Math.min(...values),
  669. max: Math.max(...values),
  670. };
  671. },
  672. // 计算标签
  673. calculateLabel(data, type = 2, preClosePrice = 0, key, num) {
  674. let label = [];
  675. if (key === "price") {
  676. // 分时价格区间
  677. if (type == 1) {
  678. const priceRange = utils.calculateDataRange(data, "price");
  679. const theMost = Math.max(priceRange.max - preClosePrice, preClosePrice - priceRange.min);
  680. const mid = (num - 1) / 2;
  681. // 计算分时价格标签
  682. label[mid] = {
  683. value: utils.formatPrice(preClosePrice),
  684. ratio: utils.formatPrice(0) + "%",
  685. };
  686. for (let i = 0; i < mid; i++) {
  687. label[i] = {
  688. value: utils.formatPrice(preClosePrice + (theMost * (mid - i)) / mid),
  689. ratio: utils.formatPrice((100 * (theMost * (mid - i))) / mid / preClosePrice) + "%",
  690. };
  691. label[num - 1 - i] = {
  692. value: utils.formatPrice(preClosePrice - (theMost * (mid - i)) / mid),
  693. ratio: utils.formatPrice((-1 * 100 * (theMost * (mid - i))) / mid / preClosePrice) + "%",
  694. };
  695. }
  696. chartRange.value.push({
  697. max: preClosePrice + theMost,
  698. min: preClosePrice - theMost,
  699. });
  700. // timeChartObject.value.max = preClosePrice + theMost;
  701. // timeChartObject.value.min = preClosePrice - theMost;
  702. return label;
  703. } else {
  704. const highPriceRange = utils.calculateDataRange(data, "high");
  705. const lowPriceRange = utils.calculateDataRange(data, "low");
  706. const priceRange = {
  707. max: highPriceRange.max * 1.01,
  708. min: lowPriceRange.min * 0.99,
  709. };
  710. const priceDiff = priceRange.max - priceRange.min;
  711. for (let i = 0; i < num; ++i) {
  712. label[i] = {
  713. value: utils.formatPrice(priceRange.max - (i * priceDiff) / (num - 1)),
  714. };
  715. }
  716. chartRange.value.push(priceRange);
  717. // klineChartObject.value.max = highPriceRange.max * 1.01;
  718. // klineChartObject.value.min = lowPriceRange.min * 0.99;
  719. return label;
  720. }
  721. } else if (key === "volume") {
  722. const volumeRange = utils.calculateDataRange(data, "volume");
  723. chartRange.value.push({
  724. max: volumeRange.max,
  725. min: 0,
  726. });
  727. label[0] = {
  728. value: utils.formatPrice(volumeRange.max),
  729. };
  730. label[1] = {
  731. value: utils.formatPrice(0),
  732. };
  733. return label;
  734. }
  735. return null;
  736. },
  737. // 线性插值
  738. lerp(start, end, factor) {
  739. return start + (end - start) * factor;
  740. },
  741. // 根据X坐标找到最近的数据点
  742. findNearestDataPoint(x, pointLen, data, grid, offset) {
  743. if (!data.length) return null;
  744. const width = canvasWidth.value;
  745. const height = canvasHeight.value;
  746. // 计算每个数据点的X坐标间隔
  747. // 倒推 const x=5+(index*(width-10)/pointLen) 已知x求index
  748. const xStep = width - grid[0].left - grid[0].right;
  749. // 计算触摸点对应的数据索引
  750. const touchX = (x - grid[0].left - offset) * pointLen;
  751. let nearestIndex = Math.round(touchX / xStep);
  752. let dataX;
  753. // 确保索引在有效范围内
  754. if (nearestIndex >= 0 && nearestIndex <= data.length - 1) {
  755. dataX = offset + grid[0].left + (nearestIndex * (width - grid[0].left - grid[0].right)) / pointLen;
  756. } else {
  757. dataX = x;
  758. }
  759. nearestIndex = Math.max(0, Math.min(nearestIndex, data.length - 1));
  760. return {
  761. ...data[nearestIndex],
  762. index: nearestIndex,
  763. x: dataX,
  764. };
  765. },
  766. // 根据Y坐标计算价格
  767. calculatePriceFromY(y, data, grid) {
  768. if (!data.length) return 0;
  769. const width = canvasWidth.value;
  770. const height = canvasHeight.value;
  771. // 上下边距1
  772. const topPadding1 = 20;
  773. const bottomPadding1 = height * 0.4;
  774. // 上下边距2
  775. const topPadding2 = height - bottomPadding1 + 40;
  776. const bottomPadding2 = 5;
  777. // 左右边距
  778. const verticalPadding = 5;
  779. let chartY;
  780. let price;
  781. for (let i = 0; i < grid.length; i++) {
  782. if (y >= grid[i].top && y <= height - grid[i].bottom) {
  783. const chartDiff = chartRange.value[i].max - chartRange.value[i].min;
  784. chartY = y - grid[i].top;
  785. price = chartRange.value[i].max - (chartY / (height - grid[i].bottom - grid[i].top)) * chartDiff;
  786. break;
  787. }
  788. }
  789. // if (y >= topPadding1 && y <= height - bottomPadding1) {
  790. // const priceDiff = priceRange.max - priceRange.min;
  791. // chartY = y - topPadding1;
  792. // price = priceRange.max - (chartY / (height - topPadding1 - bottomPadding1)) * priceDiff;
  793. // } else if (y >= topPadding2 && y <= height - bottomPadding2) {
  794. // const volumeRange = utils.calculateDataRange(data, "volume");
  795. // const volumeDiff = volumeRange.max - 0;
  796. // chartY = y - topPadding2;
  797. // price = volumeRange.max - (chartY / (height - topPadding2 - bottomPadding2)) * volumeDiff;
  798. // }
  799. return price;
  800. },
  801. // 股市数值格式化方法
  802. formatStockNumber(value, decimalPlaces = 2) {
  803. const num = Number(value);
  804. if (isNaN(num)) return "0";
  805. const absNum = Math.abs(num);
  806. const sign = num < 0 ? "-" : "";
  807. if (absNum >= 1000000000000) {
  808. // 万亿级别
  809. return sign + (absNum / 1000000000000).toFixed(decimalPlaces) + "万亿";
  810. } else if (absNum >= 100000000) {
  811. // 亿级别
  812. return sign + (absNum / 100000000).toFixed(decimalPlaces) + "亿";
  813. } else if (absNum >= 10000) {
  814. // 万级别
  815. return sign + (absNum / 10000).toFixed(decimalPlaces) + "万";
  816. } else {
  817. // 小于万的直接显示
  818. return sign + absNum.toFixed(decimalPlaces);
  819. }
  820. },
  821. };
  822. let text = [
  823. [
  824. {
  825. name: "领先",
  826. value: "暂无数据",
  827. color: "red",
  828. },
  829. {
  830. name: "价",
  831. value: utils.formatPrice(stockInformation.value.currentPrice),
  832. color: "black",
  833. },
  834. ],
  835. [
  836. {
  837. name: "量",
  838. value: utils.formatStockNumber(stockInformation.value.volume),
  839. color: "green",
  840. },
  841. {
  842. name: "额",
  843. value: "暂无数据",
  844. color: "black",
  845. },
  846. ],
  847. ];
  848. // 示例数据
  849. const timeData = ref([]);
  850. const klineData = ref([]);
  851. const initCanvas = async () => {
  852. try {
  853. crosshair.show = false;
  854. grid[0].bottom = canvasHeight.value * 0.4;
  855. grid[1].top = canvasHeight.value * 0.6 + 30;
  856. dayGrid[0].top = 20;
  857. dayGrid[0].bottom = canvasHeight.value * 0.6;
  858. dayGrid[1].top = canvasHeight.value * 0.4 + 30;
  859. dayGrid[1].bottom = canvasHeight.value * 0.3;
  860. dayGrid[2].top = canvasHeight.value * 0.7 + 20;
  861. dayGrid[2].bottom = 20;
  862. // 等待DOM更新
  863. await nextTick();
  864. ctx.value = uni.createCanvasContext("stockChart", instance.proxy);
  865. // 初始化动态数据Canvas上下文
  866. dynamicCtx.value = uni.createCanvasContext("dynamicCanvas", instance.proxy);
  867. // 初始化十字准心Canvas上下文
  868. crosshairCtx.value = uni.createCanvasContext("crosshairCanvas", instance.proxy);
  869. if (ctx.value) {
  870. // 设置Canvas的内部绘图区域尺寸
  871. ctx.value.canvas = {
  872. width: canvasWidth.value,
  873. height: canvasHeight.value,
  874. };
  875. } else {
  876. console.warn("Canvas上下文未初始化,跳过绘制");
  877. }
  878. // 确保Canvas上下文知道正确的尺寸
  879. if (dynamicCtx.value) {
  880. // 设置Canvas的内部绘图区域尺寸
  881. dynamicCtx.value.canvas = {
  882. width: canvasWidth.value,
  883. height: canvasHeight.value,
  884. };
  885. } else {
  886. console.warn("动态Canvas上下文未初始化,跳过绘制");
  887. }
  888. if (crosshairCtx.value) {
  889. // 设置Canvas的内部绘图区域尺寸
  890. crosshairCtx.value.canvas = {
  891. width: canvasWidth.value,
  892. height: canvasHeight.value,
  893. };
  894. } else {
  895. console.warn("十字准心Canvas上下文未初始化,跳过绘制");
  896. }
  897. // 等待DOM更新
  898. await nextTick();
  899. console.log("ctx", ctx.value);
  900. drawChart();
  901. } catch (error) {
  902. console.error("初始化Canvas失败:", error);
  903. }
  904. };
  905. // 绘制图表主函数-设置基础数据
  906. const drawChart = () => {
  907. if (!ctx.value || !dynamicCtx.value || !crosshairCtx.value) {
  908. console.warn("Canvas上下文未初始化,跳过绘制");
  909. return;
  910. }
  911. const data = klineTab.value == 1 ? timeData.value : klineData.value;
  912. chartRange.value = [];
  913. // 清除画布
  914. // HCharts.setCanvasColor(ctx.value, width, height, CANVAS_BACKGROUND_COLOR);
  915. // HCharts.setCanvasColor(dynamicCtx.value, width, height, CANVAS_BACKGROUND_COLOR);
  916. // HCharts.setCanvasColor(crosshairCtx.value, width, height, CANVAS_BACKGROUND_COLOR);
  917. // 根据当前标签绘制对应图表
  918. if (klineTab.value == 1) {
  919. console.log("stockInfomaton.lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  920. // 设置标签样式
  921. const label = [
  922. {
  923. text: utils.calculateLabel(data, 1, stockInformation.value.lastDayStockClosePrice, "price", grid[0].horizontalLineNum),
  924. color: [TEXT_COLOR1, TEXT_COLOR1, TEXT_COLOR1, TEXT_COLOR1, TEXT_COLOR2],
  925. fontSize: 12,
  926. lineStyle: ["solid", "solid", "solid", "solid", "solid"],
  927. onlyTwo: false,
  928. },
  929. {
  930. text: utils.calculateLabel(data, 2, stockInformation.value.lastDayStockClosePrice, "volume", grid[1].horizontalLineNum),
  931. color: [TEXT_COLOR1, TEXT_COLOR1],
  932. lineStyle: ["solid", "solid", "solid"],
  933. fontSize: 12,
  934. onlyTwo: true,
  935. },
  936. ];
  937. // 把label加进grid中
  938. grid[0].label = label[0];
  939. grid[1].label = label[1];
  940. drawTimeChart();
  941. } else {
  942. // 设置标签样式
  943. const label = [
  944. {
  945. text: utils.calculateLabel(data, 2, stockInformation.value.lastDayStockClosePrice, "price", grid[0].horizontalLineNum),
  946. color: [TEXT_COLOR1, TEXT_COLOR1, TEXT_COLOR1, TEXT_COLOR1, TEXT_COLOR2],
  947. lineStyle: ["solid", "dash", "dash", "dash", "solid"],
  948. fontSize: 12,
  949. onlyTwo: false,
  950. },
  951. {
  952. text: utils.calculateLabel(data, 2, stockInformation.value.lastDayStockClosePrice, "volume", grid[1].horizontalLineNum),
  953. color: [TEXT_COLOR1, TEXT_COLOR1],
  954. lineStyle: ["solid", "dash", "solid"],
  955. fontSize: 12,
  956. onlyTwo: true,
  957. },
  958. {
  959. text: utils.calculateLabel(data, 2, stockInformation.value.lastDayStockClosePrice, "volume", grid[1].horizontalLineNum),
  960. color: [TEXT_COLOR1, TEXT_COLOR1],
  961. lineStyle: ["solid", "dash", "solid"],
  962. fontSize: 12,
  963. onlyTwo: true,
  964. },
  965. ];
  966. // 把label加进grid中
  967. dayGrid[0].label = label[0];
  968. dayGrid[1].label = label[1];
  969. dayGrid[2].label = label[2];
  970. // HCharts.drawGrid(ctx.value, canvasWidth.value, canvasHeight.value, dayGrid, data[0].date, data[data.length - 1].date);
  971. drawKLineChart();
  972. }
  973. // crosshairCtx.value.draw();
  974. };
  975. const throttledDrawChart = throttle(drawChart, 50);
  976. // // 绘制分时图
  977. const drawTimeChart = () => {
  978. drawCtxChart(grid);
  979. drawDynamicCtxChart();
  980. };
  981. // // 绘制K线图
  982. const drawKLineChart = () => {
  983. drawCtxChart(dayGrid);
  984. drawDynamicCtxChart();
  985. };
  986. const drawCtxChart = (paramGrid) => {
  987. // 检查Canvas上下文是否已初始化
  988. if (!ctx.value) {
  989. console.warn("Canvas上下文未初始化,跳过绘制");
  990. return;
  991. }
  992. // 绘制网格
  993. HCharts.drawGrid(ctx.value, canvasWidth.value, canvasHeight.value, paramGrid, openTime, closeTime);
  994. //执行绘制
  995. ctx.value.draw();
  996. };
  997. const drawDynamicCtxChart = () => {
  998. if (klineTab.value == 1) {
  999. //绘制价格曲线
  1000. HCharts.drawPriceLine(dynamicCtx.value, canvasWidth.value, canvasHeight.value, timeData.value, grid, chartRange.value[0]);
  1001. //绘制成交量
  1002. HCharts.drawVolume(
  1003. dynamicCtx.value,
  1004. canvasWidth.value,
  1005. canvasHeight.value,
  1006. timeData.value,
  1007. 2,
  1008. 240,
  1009. grid,
  1010. {
  1011. max: utils.calculateDataRange(timeData.value, "volume").max,
  1012. min: 0,
  1013. },
  1014. 0
  1015. );
  1016. HCharts.drawAxisLabels(dynamicCtx.value, canvasWidth.value, canvasHeight.value, grid);
  1017. HCharts.drawTopPriceDisplay(crosshairCtx.value, grid, text);
  1018. } else {
  1019. drawKLine(dynamicCtx.value);
  1020. //绘制成交量
  1021. HCharts.drawVolume(
  1022. dynamicCtx.value,
  1023. canvasWidth.value,
  1024. canvasHeight.value,
  1025. klineData.value,
  1026. 2,
  1027. klineData.value.length,
  1028. dayGrid,
  1029. {
  1030. max: utils.calculateDataRange(klineData.value, "volume").max,
  1031. min: 0,
  1032. },
  1033. touchState.offset
  1034. );
  1035. //绘制成交量
  1036. HCharts.drawVolume(
  1037. dynamicCtx.value,
  1038. canvasWidth.value,
  1039. canvasHeight.value,
  1040. klineData.value,
  1041. 3,
  1042. klineData.value.length,
  1043. dayGrid,
  1044. {
  1045. max: utils.calculateDataRange(klineData.value, "volume").max,
  1046. min: 0,
  1047. },
  1048. touchState.offset
  1049. );
  1050. HCharts.drawAxisLabels(dynamicCtx.value, canvasWidth.value, canvasHeight.value, dayGrid);
  1051. // drawMovingAverage();
  1052. HCharts.drawTopPriceDisplay(crosshairCtx.value, dayGrid, []);
  1053. }
  1054. //执行绘制
  1055. dynamicCtx.value.draw(false);
  1056. crosshairCtx.value.draw();
  1057. };
  1058. const throttledDrawDynamicCtxChart = throttle(drawDynamicCtxChart, 50);
  1059. // const throttledDrawKLineChart = throttle(drawKLineChart, 10);
  1060. // 绘制K线
  1061. const drawKLine = (ctx) => {
  1062. const data = klineData.value;
  1063. if (!data.length) return;
  1064. const width = canvasWidth.value;
  1065. const height = canvasHeight.value;
  1066. // 计算价格范围
  1067. const priceRange = chartRange.value[0];
  1068. const priceDiff = priceRange.max - priceRange.min;
  1069. const areaWidth = (width - dayGrid[0].left - dayGrid[0].right) / data.length;
  1070. const candleWidth = areaWidth * 0.6;
  1071. data.forEach((item, index) => {
  1072. const x = touchState.offset + dayGrid[0].left + (index * (width - dayGrid[0].left - dayGrid[0].right)) / data.length;
  1073. // 计算坐标
  1074. const highY = dayGrid[0].top + (height - dayGrid[0].top - dayGrid[0].bottom) * (1 - (item.high - priceRange.min) / priceDiff);
  1075. const lowY = dayGrid[0].top + (height - dayGrid[0].top - dayGrid[0].bottom) * (1 - (item.low - priceRange.min) / priceDiff);
  1076. const openY = dayGrid[0].top + (height - dayGrid[0].top - dayGrid[0].bottom) * (1 - (item.open - priceRange.min) / priceDiff);
  1077. const closeY = dayGrid[0].top + (height - dayGrid[0].top - dayGrid[0].bottom) * (1 - (item.close - priceRange.min) / priceDiff);
  1078. // 判断涨跌
  1079. const isRise = item.close >= item.open;
  1080. const color = isRise ? "green" : "red";
  1081. // 绘制上下影线
  1082. ctx.setStrokeStyle(color);
  1083. ctx.setLineWidth(1);
  1084. ctx.beginPath();
  1085. ctx.moveTo(x, highY);
  1086. ctx.lineTo(x, lowY);
  1087. ctx.stroke();
  1088. // 绘制K线实体
  1089. const entityTop = isRise ? closeY : openY;
  1090. const entityBottom = isRise ? openY : closeY;
  1091. const entityHeight = Math.max(Math.abs(entityBottom - entityTop), 1);
  1092. ctx.setFillStyle(color);
  1093. ctx.fillRect(x - candleWidth / 2, entityTop, candleWidth, entityHeight);
  1094. });
  1095. };
  1096. // 绘制移动平均线
  1097. const drawMovingAverage = () => {
  1098. const data = klineData.value;
  1099. if (!data.length) return;
  1100. const ma5 = calculateMA(data, 5);
  1101. const ma10 = calculateMA(data, 10);
  1102. drawMALine(ma5, "#fadb14", "MA5"); // 黄色
  1103. drawMALine(ma10, "#eb2f96", "MA10"); // 粉色
  1104. };
  1105. // 计算移动平均线
  1106. const calculateMA = (data, period) => {
  1107. const result = [];
  1108. for (let i = period - 1; i < data.length; i++) {
  1109. let sum = 0;
  1110. for (let j = 0; j < period; j++) {
  1111. sum += data[i - j].close;
  1112. }
  1113. result.push({
  1114. index: i,
  1115. value: sum / period,
  1116. });
  1117. }
  1118. return result;
  1119. };
  1120. // 绘制均线
  1121. const drawMALine = (maData, color, label) => {
  1122. if (!maData.length) return;
  1123. const width = canvasWidth.value;
  1124. const height = canvasHeight.value;
  1125. const data = klineData.value;
  1126. // 计算价格范围
  1127. const highs = data.map((item) => item.high);
  1128. const lows = data.map((item) => item.low);
  1129. const priceRange = chartRange.value[0];
  1130. const priceDiff = priceRange.max - priceRange.min;
  1131. ctx.value.setStrokeStyle(color);
  1132. ctx.value.setLineWidth(1.5);
  1133. ctx.value.beginPath();
  1134. maData.forEach((point, idx) => {
  1135. const x = 40 + (point.index * (width - 60)) / (data.length - 1);
  1136. const y = 20 + (height - 60) * (1 - (point.value - priceRange.min) / priceDiff);
  1137. if (idx === 0) {
  1138. ctx.value.moveTo(x, y);
  1139. } else {
  1140. ctx.value.lineTo(x, y);
  1141. }
  1142. });
  1143. ctx.value.stroke();
  1144. // 绘制图例
  1145. if (maData.length > 0) {
  1146. const lastPoint = maData[maData.length - 1];
  1147. const x = 40 + (lastPoint.index * (width - 60)) / (data.length - 1);
  1148. const y = 20 + (height - 60) * (1 - (lastPoint.value - priceRange.min) / priceDiff);
  1149. HCharts.drawText(ctx.value`${label}: ${utils.formatPrice(lastPoint.value)}`, x + 10, y - 5, 12, color);
  1150. }
  1151. };
  1152. // 触摸事件处理
  1153. const touchStart = (e) => {
  1154. e.preventDefault(); // 阻止页面滚动等默认行为
  1155. if (typeof e.touches[1] === "undefined") {
  1156. touchState.startX = e.touches[0].x;
  1157. touchState.startY = e.touches[0].y;
  1158. touchState.isMoving = false;
  1159. } else if (typeof e.touches[1] !== "undefined" && !crosshair.show) {
  1160. // touchState.startX1 = e.touches[0].x;
  1161. // touchState.startY1 = e.touches[0].y;
  1162. // touchState.startX2 = e.touches[1].x;
  1163. // touchState.startY2 = e.touches[1].y;
  1164. // touchState.isMoving = false;
  1165. }
  1166. };
  1167. const touchMove = (e) => {
  1168. touchState.isMoving = true;
  1169. // 计算移动距离
  1170. const currentX = e.touches[0].x;
  1171. const currentY = e.touches[0].y;
  1172. const deltaX = currentX - touchState.startX;
  1173. const deltaY = currentY - touchState.startY;
  1174. touchState.moveDistance = Math.max(touchState.moveDistance, Math.sqrt(deltaX * deltaX + deltaY * deltaY));
  1175. if (crosshair.show) {
  1176. if (isInChartArea(currentX, currentY, klineTab.value === 1 ? grid : dayGrid)) {
  1177. throttledUpdateCrosshair(currentX, currentY);
  1178. } else {
  1179. // 如果移出图表区域,隐藏十字准线
  1180. // crosshair.show = false;
  1181. if (klineTab.value === 1) {
  1182. HCharts.drawCrosshair(crosshairCtx.value, canvasWidth.value, canvasHeight.value, grid, crosshair, text);
  1183. } else {
  1184. HCharts.drawCrosshair(crosshairCtx.value, canvasWidth.value, canvasHeight.value, dayGrid, crosshair, []);
  1185. }
  1186. }
  1187. } else {
  1188. return;
  1189. if (klineTab.value === 2) {
  1190. // if(currentY)
  1191. if (currentX < touchState.startX) {
  1192. touchState.offset += currentX - touchState.startX;
  1193. throttledDrawDynamicCtxChart();
  1194. touchState.startX = currentX;
  1195. } else {
  1196. touchState.offset += currentX - touchState.startX;
  1197. throttledDrawDynamicCtxChart();
  1198. touchState.startX = currentX;
  1199. }
  1200. if (canvasWidth.value + touchState.offset < 30) {
  1201. touchState.offset = 0;
  1202. }
  1203. }
  1204. }
  1205. };
  1206. const touchEnd = (e) => {
  1207. // 触摸结束,隐藏十字准线
  1208. if (touchState.moveDistance < touchState.moveThreshold) {
  1209. // 移动距离小于阈值,认为是点击事件
  1210. crosshair.show = !crosshair.show;
  1211. }
  1212. if (crosshair.show) {
  1213. const currentX = e.changedTouches[0].x;
  1214. const currentY = e.changedTouches[0].y;
  1215. if (isInChartArea(currentX, currentY, klineTab.value === 1 ? grid : dayGrid)) {
  1216. throttledUpdateCrosshair(currentX, currentY);
  1217. }
  1218. }
  1219. if (klineTab.value === 1) {
  1220. HCharts.drawCrosshair(crosshairCtx.value, canvasWidth.value, canvasHeight.value, grid, crosshair, text);
  1221. } else {
  1222. HCharts.drawCrosshair(crosshairCtx.value, canvasWidth.value, canvasHeight.value, dayGrid, crosshair, []);
  1223. }
  1224. // 重置移动距离
  1225. touchState.moveDistance = 0;
  1226. };
  1227. // 检查坐标是否在图表区域内
  1228. const isInChartArea = (x, y, grid) => {
  1229. const width = canvasWidth.value;
  1230. const height = canvasHeight.value;
  1231. if (x < grid[0].left || x > width - grid[0].right) return false;
  1232. for (let i = 0; i < grid.length; i++) {
  1233. if (y >= grid[i].top && y <= height - grid[i].bottom) return true;
  1234. }
  1235. return false;
  1236. };
  1237. // 更新十字准线位置和数据
  1238. const updateCrosshair = (x, y) => {
  1239. if (!crosshair.show) return;
  1240. // 更新Y坐标以匹配实际价格
  1241. const data = klineTab.value === 1 ? timeData.value : klineData.value;
  1242. const width = canvasWidth.value;
  1243. const height = canvasHeight.value;
  1244. crosshair.x = x;
  1245. crosshair.y = y;
  1246. const nearestData = utils.findNearestDataPoint(x, klineTab.value === 1 ? 240 : data.length, data, klineTab.value === 1 ? grid : dayGrid, klineTab.value === 1 ? 0 : touchState.offset);
  1247. crosshair.x = nearestData.x;
  1248. stockInformation.value.currentPrice = nearestData.price || nearestData.close;
  1249. stockInformation.value.volume = nearestData.volume;
  1250. // 涨跌额度
  1251. if (klineTab.value == 1) {
  1252. stockInformation.value.currentValue = stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice;
  1253. stockInformation.value.currentRatio = ((stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice) / stockInformation.value.lastDayStockClosePrice) * 100;
  1254. } else {
  1255. stockInformation.value.openPrice = nearestData.open;
  1256. stockInformation.value.highPrice = nearestData.high;
  1257. stockInformation.value.lowPrice = nearestData.low;
  1258. if (nearestData.index != 0) {
  1259. stockInformation.value.lastDayStockClosePrice = data[nearestData.index - 1].close;
  1260. stockInformation.value.currentValue = stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice;
  1261. stockInformation.value.currentRatio = ((stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice) / stockInformation.value.lastDayStockClosePrice) * 100;
  1262. }
  1263. }
  1264. if (klineTab.value === 1 && crosshair.snapToData) {
  1265. // 吸附到最近的数据点
  1266. if (nearestData) {
  1267. crosshair.currentData = nearestData;
  1268. const priceDiff = chartRange.value[0].max - chartRange.value[0].min;
  1269. crosshair.y = grid[0].top + (height - grid[0].bottom - grid[0].top) * (1 - (nearestData.price - chartRange.value[0].min) / priceDiff);
  1270. }
  1271. } else {
  1272. // 自由移动模式
  1273. const currentPrice = utils.calculatePriceFromY(y, data, klineTab.value === 1 ? grid : dayGrid);
  1274. crosshair.currentData = {
  1275. ...nearestData,
  1276. price: currentPrice,
  1277. };
  1278. }
  1279. if (crosshair?.currentData?.volume) {
  1280. text[0][1].value = crosshair.currentData.price;
  1281. text[1][0].value = crosshair.currentData.volume;
  1282. }
  1283. // 只重绘十字准心,不重绘整个图表
  1284. if (klineTab.value === 1) {
  1285. HCharts.drawCrosshair(crosshairCtx.value, canvasWidth.value, canvasHeight.value, grid, crosshair, text);
  1286. } else {
  1287. HCharts.drawCrosshair(crosshairCtx.value, canvasWidth.value, canvasHeight.value, dayGrid, crosshair, []);
  1288. }
  1289. };
  1290. const throttledUpdateCrosshair = throttle(updateCrosshair, 10);
  1291. // 缩放控制函数
  1292. const zoomIn = () => {
  1293. touchState.scale = Math.min(touchState.maxScale, touchState.scale * 1.2);
  1294. drawChart();
  1295. };
  1296. const zoomOut = () => {
  1297. touchState.scale = Math.max(touchState.minScale, touchState.scale / 1.2);
  1298. drawChart();
  1299. };
  1300. // 监听数据变化
  1301. // watch([timeData, klineData], () => {
  1302. // console.log("数据变化");
  1303. // drawChart();
  1304. // });
  1305. watch(klineTab, () => {
  1306. console.log("标签页变化");
  1307. drawChart();
  1308. });
  1309. // 初始化TCP监听器
  1310. const initTcpListeners = () => {
  1311. // 创建连接状态监听器并保存引用
  1312. connectionListener.value = (status, result) => {
  1313. tcpConnected.value = status === "connected";
  1314. console.log("TCP连接状态变化:", status, tcpConnected.value);
  1315. // 显示连接状态提示
  1316. uni.showToast({
  1317. title: status === "connected" ? "TCP连接成功" : "TCP连接断开",
  1318. icon: status === "connected" ? "success" : "none",
  1319. duration: 2000,
  1320. });
  1321. if (status === "connected") {
  1322. if (klineTab.value == 1) {
  1323. sendTcpMessage("init_real_time");
  1324. }
  1325. }
  1326. };
  1327. // 创建消息监听器并保存引用
  1328. messageListener.value = (type, message, parsedArray) => {
  1329. const messageObj = {
  1330. type: type,
  1331. content: message,
  1332. parsedArray: parsedArray,
  1333. timestamp: new Date().toLocaleTimeString(),
  1334. direction: "received",
  1335. };
  1336. // 解析股票数据
  1337. parseStockData(message);
  1338. };
  1339. // 注册监听器
  1340. tcpConnection.onConnectionChange(connectionListener.value);
  1341. tcpConnection.onMessage(messageListener.value);
  1342. };
  1343. // 连接TCP服务器
  1344. const connectTcp = () => {
  1345. console.log("开始连接TCP服务器...");
  1346. tcpConnection.connect();
  1347. };
  1348. // 断开TCP连接
  1349. const disconnectTcp = () => {
  1350. console.log("断开TCP连接...");
  1351. tcpConnection.disconnect();
  1352. tcpConnected.value = false;
  1353. };
  1354. // 发送TCP消息
  1355. const sendTcpMessage = (command) => {
  1356. let messageData;
  1357. switch (command) {
  1358. // 实时行情推送
  1359. case "real_time":
  1360. messageData = {
  1361. command: "real_time",
  1362. stock_code: "SH.000001",
  1363. };
  1364. break;
  1365. // 初始化获取行情历史数据
  1366. case "init_real_time":
  1367. messageData = {
  1368. command: "init_real_time",
  1369. stock_code: "SH.000001",
  1370. };
  1371. break;
  1372. case "stop_real_time":
  1373. messageData = {
  1374. command: "stop_real_time",
  1375. };
  1376. break;
  1377. // 股票列表
  1378. case "stock_list":
  1379. messageData = {
  1380. command: "stock_list",
  1381. };
  1382. break;
  1383. // 日线数据
  1384. case "daily_data":
  1385. messageData = {
  1386. command: "daily_data",
  1387. stock_code: "GBPAUD.FXCM",
  1388. start_date: "20251001",
  1389. end_date: "20251023",
  1390. };
  1391. break;
  1392. // 周线数据
  1393. case "weekly_data":
  1394. messageData = {
  1395. command: "weekly_data",
  1396. stock_code: "000001.SZ",
  1397. start_date: "2024912",
  1398. end_date: "20251029",
  1399. };
  1400. break;
  1401. // 周线数据
  1402. case "monthly_data":
  1403. messageData = {
  1404. command: "monthly_data",
  1405. stock_code: "000001.SZ",
  1406. start_date: "2024912",
  1407. end_date: "20251029",
  1408. };
  1409. break;
  1410. // 1分钟线数据
  1411. case "daily_one_minutes_data":
  1412. messageData = {
  1413. command: "daily_one_minutes_data",
  1414. stock_code: "000001.SZ",
  1415. };
  1416. break;
  1417. // 5分钟线数据
  1418. case "daily_five_minutes_data":
  1419. messageData = {
  1420. command: "daily_five_minutes_data",
  1421. stock_code: "000001.SZ",
  1422. };
  1423. break;
  1424. // 15分钟线数据
  1425. case "daily_fifteen_minutes_data":
  1426. messageData = {
  1427. command: "daily_fifteen_minutes_data",
  1428. stock_code: "000001.SZ",
  1429. };
  1430. break;
  1431. // 30分钟线数据
  1432. case "daily_thirty_minutes_data":
  1433. messageData = {
  1434. command: "daily_thirty_minutes_data",
  1435. stock_code: "000001.SZ",
  1436. };
  1437. break;
  1438. // 60分钟线数据
  1439. case "daily_sixty_minutes_data":
  1440. messageData = {
  1441. command: "daily_sixty_minutes_data",
  1442. stock_code: "000001.SZ",
  1443. };
  1444. break;
  1445. case "batch_real_time":
  1446. messageData = {
  1447. command: "batch_real_time",
  1448. stock_codes: ["SH.000001", "SH.000002", "SH.000003", "SH.000004", "SH.000005"],
  1449. };
  1450. break;
  1451. case "help":
  1452. messageData = {
  1453. command: "help",
  1454. };
  1455. break;
  1456. }
  1457. if (!messageData) {
  1458. uni.showToast({
  1459. title: "命令不存在",
  1460. icon: "none",
  1461. duration: 1000,
  1462. });
  1463. return;
  1464. } else {
  1465. try {
  1466. // 发送消息
  1467. const success = tcpConnection.send(messageData);
  1468. if (success) {
  1469. console.log("home发送TCP消息:", messageData);
  1470. uni.showToast({
  1471. title: "消息发送成功",
  1472. icon: "success",
  1473. duration: 1000,
  1474. });
  1475. }
  1476. } catch (error) {
  1477. console.error("发送TCP消息时出错:", error);
  1478. uni.showToast({
  1479. title: "消息发送失败",
  1480. icon: "none",
  1481. duration: 1000,
  1482. });
  1483. }
  1484. }
  1485. };
  1486. // 获取TCP连接状态
  1487. const getTcpStatus = () => {
  1488. const status = tcpConnection.getConnectionStatus();
  1489. uni.showModal({
  1490. title: "TCP连接状态",
  1491. content: `当前状态: ${status ? "已连接" : "未连接"}`,
  1492. showCancel: false,
  1493. });
  1494. };
  1495. let isMorePacket = {
  1496. init_real_time: false,
  1497. daily_data: false,
  1498. weekly_data: false,
  1499. monthly_data: false,
  1500. daily_one_minutes_data: false,
  1501. daily_five_minutes_data: false,
  1502. daily_fifteen_minutes_data: false,
  1503. daily_thirty_minutes_data: false,
  1504. daily_sixty_minutes_data: false,
  1505. };
  1506. let receivedMessage;
  1507. const findJsonPacket = (message, command) => {
  1508. let jsonStartIndex = 0;
  1509. let jsonEndIndex = message.indexOf(command);
  1510. let jsonStartCount = 0;
  1511. let jsonEndCount = 0;
  1512. for (let i = 0; i < message.length - 1; ++i) {
  1513. if (message[i] == "{") {
  1514. jsonStartCount++;
  1515. if (jsonStartCount == 2) {
  1516. jsonStartIndex = i;
  1517. break;
  1518. }
  1519. }
  1520. }
  1521. for (let i = message.indexOf(command); i >= 0; --i) {
  1522. if (message[i] == "}" || i == jsonStartIndex) {
  1523. jsonEndCount++;
  1524. if (jsonEndCount == 1) {
  1525. jsonEndIndex = i;
  1526. break;
  1527. }
  1528. }
  1529. }
  1530. // 检查JSON字符串是否有效
  1531. if (jsonStartIndex >= jsonEndIndex) {
  1532. return { error: true };
  1533. }
  1534. return { json: JSON.parse(message.substring(jsonStartIndex, jsonEndIndex + 1)) };
  1535. };
  1536. // 解析TCP股票数据
  1537. const parseStockData = (message) => {
  1538. try {
  1539. console.log("进入parseStockData, message类型:", typeof message);
  1540. let parsedMessage;
  1541. // 如果isMorePacket是true,说明正在接受分包数据,无条件接收
  1542. // 如果message是字符串且以{开头,说明是JSON字符串,需要解析
  1543. // 如果不属于以上两种情况,说明是普通字符串,不预解析
  1544. if (message.includes("欢迎连接到股票数据服务器")) {
  1545. console.log("服务器命令列表,不予处理");
  1546. return;
  1547. }
  1548. if ((typeof message === "string" && message.includes("init_real_data_start")) || isMorePacket.init_real_time) {
  1549. if (typeof message === "string" && message.includes("init_real_data_start")) {
  1550. console.log("开始接受分包数据");
  1551. receivedMessage = "";
  1552. } else {
  1553. console.log("接收分包数据过程中");
  1554. }
  1555. isMorePacket.init_real_time = true;
  1556. receivedMessage += message;
  1557. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  1558. if (receivedMessage.includes("init_real_data_complete")) {
  1559. console.log("接受分包数据结束");
  1560. isMorePacket.init_real_time = false;
  1561. console.log("展示数据", receivedMessage);
  1562. const result = findJsonPacket(receivedMessage, "init_real_data_complete");
  1563. if (result.error) {
  1564. throw new Error("解析JSON字符串失败");
  1565. } else {
  1566. parsedMessage = result.json;
  1567. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  1568. if (parsedMessage.type === "daily_data") {
  1569. timeData.value = parsedMessage.data;
  1570. stockInformation.value.lastDayStockClosePrice = parsedMessage.pre_close;
  1571. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  1572. drawChart();
  1573. sendTcpMessage("stop_real_time");
  1574. sendTcpMessage("real_time");
  1575. }
  1576. }
  1577. }
  1578. } else if ((typeof message === "string" && message.includes("daily_data_start")) || isMorePacket.daily_data) {
  1579. if (typeof message === "string" && message.includes("daily_data_start")) {
  1580. console.log("开始接受分包数据");
  1581. receivedMessage = "";
  1582. } else {
  1583. console.log("接收分包数据过程中");
  1584. }
  1585. isMorePacket.daily_data = true;
  1586. receivedMessage += message;
  1587. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  1588. if (receivedMessage.includes("daily_data_complete")) {
  1589. console.log("接受分包数据结束");
  1590. isMorePacket.daily_data = false;
  1591. console.log("展示数据", receivedMessage);
  1592. const result = findJsonPacket(receivedMessage, "daily_data_complete");
  1593. if (result.error) {
  1594. throw new Error("解析JSON字符串失败");
  1595. } else {
  1596. parsedMessage = result.json;
  1597. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  1598. if (parsedMessage.type === "daily_data") {
  1599. klineData.value = parsedMessage.data.map((item) => ({
  1600. open: item.ask_open,
  1601. close: item.ask_close,
  1602. high: item.ask_high,
  1603. low: item.ask_low,
  1604. volume: item.tick_qty,
  1605. date: item.trade_date ? `${item.trade_date.slice(0, 4)}-${item.trade_date.slice(4, 6)}-${item.trade_date.slice(6, 8)}` : item.trade_date,
  1606. }));
  1607. stockInformation.value.lastDayStockClosePrice = klineData.value[klineData.value.length - 2].close;
  1608. touchState.offset = canvasWidth.value / klineData.value.length / 2;
  1609. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  1610. drawChart();
  1611. }
  1612. }
  1613. }
  1614. } else if ((typeof message === "string" && message.includes("weekly_data_start")) || isMorePacket.weekly_data) {
  1615. if (typeof message === "string" && message.includes("weekly_data_start")) {
  1616. console.log("开始接受分包数据");
  1617. receivedMessage = "";
  1618. } else {
  1619. console.log("接收分包数据过程中");
  1620. }
  1621. isMorePacket.weekly_data = true;
  1622. receivedMessage += message;
  1623. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  1624. if (receivedMessage.includes("weekly_data_complete")) {
  1625. console.log("接受分包数据结束");
  1626. isMorePacket.weekly_data = false;
  1627. console.log("展示数据", receivedMessage);
  1628. const result = findJsonPacket(receivedMessage, "weekly_data_complete");
  1629. if (result.error) {
  1630. throw new Error("解析JSON字符串失败");
  1631. } else {
  1632. parsedMessage = result.json;
  1633. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  1634. if (parsedMessage.type === "weekly_data") {
  1635. klineData.value = parsedMessage.data.map((item) => ({
  1636. open: item.bid_open,
  1637. close: item.bid_close,
  1638. high: item.bid_high,
  1639. low: item.bid_low,
  1640. volume: item.vol,
  1641. amount: item.amount,
  1642. date: item.trade_date ? `${item.trade_date.slice(0, 4)}-${item.trade_date.slice(4, 6)}-${item.trade_date.slice(6, 8)}` : item.trade_date,
  1643. }));
  1644. stockInformation.value.lastDayStockClosePrice = klineData.value[klineData.value.length - 2].close;
  1645. touchState.offset = canvasWidth.value / klineData.value.length / 2;
  1646. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  1647. drawChart();
  1648. }
  1649. }
  1650. }
  1651. } else if ((typeof message === "string" && message.includes("daily_one_minutes_data_start")) || isMorePacket.daily_one_minutes_data) {
  1652. if (typeof message === "string" && message.includes("daily_one_minutes_data_start")) {
  1653. console.log("开始接受分包数据");
  1654. receivedMessage = "";
  1655. } else {
  1656. console.log("接收分包数据过程中");
  1657. }
  1658. isMorePacket.daily_one_minutes_data = true;
  1659. receivedMessage += message;
  1660. // 如果当前消息包含},说明收到JSON字符串结尾,结束接收,开始解析
  1661. if (receivedMessage.includes("daily_one_minutes_data_complete")) {
  1662. console.log("接受分包数据结束");
  1663. isMorePacket.daily_one_minutes_data = false;
  1664. console.log("展示数据", receivedMessage);
  1665. const result = findJsonPacket(receivedMessage, "daily_one_minutes_data_complete");
  1666. if (result.error) {
  1667. throw new Error("解析JSON字符串失败");
  1668. } else {
  1669. parsedMessage = result.json;
  1670. console.log("JSON解析成功,解析后类型:", typeof parsedMessage, parsedMessage);
  1671. if (parsedMessage.type === "daily_one_minutes_data") {
  1672. // klineData.value = parsedMessage.data.map((item) => ({
  1673. // open: item.open,
  1674. // close: item.close,
  1675. // high: item.high,
  1676. // low: item.low,
  1677. // volume: item.volume,
  1678. // amount: item.amount,
  1679. // date: item.time,
  1680. // }));
  1681. stockInformation.value.lastDayStockClosePrice = klineData.value[klineData.value.length - 2].close;
  1682. touchState.offset = canvasWidth.value / klineData.value.length / 2;
  1683. console.log("lastDayStockClosePrice", stockInformation.value.lastDayStockClosePrice);
  1684. drawChart();
  1685. }
  1686. }
  1687. }
  1688. } else {
  1689. // 没有通过JSON解析判断,说明不是需要的数据
  1690. console.log("不是需要的数据,不做处理");
  1691. }
  1692. } catch (error) {
  1693. console.error("解析TCP股票数据失败:", error.message);
  1694. console.error("错误详情:", error);
  1695. }
  1696. };
  1697. // 移除TCP监听器
  1698. const removeTcpListeners = () => {
  1699. if (connectionListener.value) {
  1700. tcpConnection.removeConnectionListener(connectionListener.value);
  1701. connectionListener.value = null;
  1702. console.log("已移除TCP连接状态监听器");
  1703. }
  1704. if (messageListener.value) {
  1705. tcpConnection.removeMessageListener(messageListener.value);
  1706. messageListener.value = null;
  1707. console.log("已移除TCP消息监听器");
  1708. }
  1709. };
  1710. // 定时器标识(用于清除定时器)
  1711. let timer = null;
  1712. let index = 0;
  1713. // 定时添加数据的函数
  1714. const startAddDataTimer = () => {
  1715. if (timer) {
  1716. console.log("存在旧定时器,卸载旧定时器");
  1717. clearInterval(timer);
  1718. }
  1719. console.log("开始定时任务");
  1720. // 每隔5秒执行一次
  1721. timer = setInterval(() => {
  1722. if (index < testTimeData.length) {
  1723. timeData.value.push(testTimeData[index]);
  1724. console.log("新增数据:", testTimeData[index]);
  1725. // 触发图表重新绘制
  1726. drawChart();
  1727. index++;
  1728. } else {
  1729. clearInterval(timer);
  1730. }
  1731. }, 2000); // 5000毫秒 = 5秒
  1732. };
  1733. onLoad((options) => {
  1734. console.log("页面接收到的参数:", options);
  1735. // 处理通过data参数传递的复杂对象
  1736. if (options.data) {
  1737. try {
  1738. const stockData = JSON.parse(decodeURIComponent(options.data));
  1739. console.log("解析的股票数据:", stockData);
  1740. // 更新stockInformation
  1741. if (stockData) {
  1742. stockInformation.value.stockName = stockData.stockName;
  1743. stockInformation.value.stockCode = stockData.stockCode;
  1744. }
  1745. } catch (error) {
  1746. console.error("解析股票数据失败:", error);
  1747. }
  1748. }
  1749. // 处理通过stockInformation参数传递的数据(兼容globalIndex.vue的传参方式)
  1750. if (options.stockInformation) {
  1751. try {
  1752. const stockData = JSON.parse(decodeURIComponent(options.stockInformation));
  1753. console.log("解析的股票信息:", stockData);
  1754. // 更新stockInformation
  1755. if (stockData) {
  1756. stockInformation.value = {
  1757. ...stockInformation.value,
  1758. ...stockData,
  1759. };
  1760. }
  1761. } catch (error) {
  1762. console.error("解析股票信息失败:", error);
  1763. }
  1764. }
  1765. // 处理简单参数
  1766. if (options.stockCode) {
  1767. stockInformation.value.stockCode = options.stockCode;
  1768. }
  1769. if (options.stockName) {
  1770. stockInformation.value.stockName = decodeURIComponent(options.stockName);
  1771. }
  1772. });
  1773. // 保存定时器,用于页面卸载时清理
  1774. onUnmounted(() => {
  1775. removeTcpListeners();
  1776. disconnect();
  1777. if (timer) {
  1778. console.log("卸载定时器");
  1779. clearInterval(timer);
  1780. }
  1781. });
  1782. onMounted(async () => {
  1783. try {
  1784. console.log("步骤1: 初始化系统信息");
  1785. const systemInfo = uni.getSystemInfoSync();
  1786. pixelRatio.value = systemInfo.pixelRatio;
  1787. // 设置Canvas实际像素(考虑pixelRatio以获得高清效果)
  1788. // 1rpx = 设备屏幕宽度 / 750
  1789. const rpxToPx = systemInfo.windowWidth / 750;
  1790. const offsetHeight = (150 + 200 + 80 + 150 + 30) * rpxToPx; // 350rpx转换为px
  1791. const calculatedHeight = systemInfo.windowHeight - offsetHeight;
  1792. canvasWidth.value = systemInfo.windowWidth;
  1793. canvasHeight.value = Math.max(calculatedHeight, canvasHeight.value);
  1794. initTcpListeners();
  1795. await nextTick();
  1796. // 开始连接
  1797. startTcp();
  1798. if (timeData.value && timeData.value.length > 0) {
  1799. // 当前股价
  1800. stockInformation.value.currentPrice = timeData.value[timeData.value.length - 1].price;
  1801. // 涨跌额度
  1802. stockInformation.value.currentValue = stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice;
  1803. // 涨跌幅度
  1804. stockInformation.value.currentRatio = ((stockInformation.value.currentPrice - stockInformation.value.lastDayStockClosePrice) / stockInformation.value.lastDayStockClosePrice) * 100;
  1805. // 成交量
  1806. stockInformation.value.volume = timeData.value[timeData.value.length - 1].volume;
  1807. text[0][1].value = utils.formatPrice(stockInformation.value.currentPrice);
  1808. text[1][0].value = utils.formatStockNumber(stockInformation.value.volume);
  1809. } else {
  1810. console.warn("没有时间数据,跳过股票信息计算");
  1811. }
  1812. await nextTick();
  1813. initCanvas();
  1814. console.log("所有初始化步骤完成");
  1815. } catch (error) {
  1816. console.error("初始化过程中出现错误:", error);
  1817. }
  1818. });
  1819. </script>
  1820. <style>
  1821. .container {
  1822. width: 100%;
  1823. min-height: 100vh;
  1824. background-color: #f6f6f6;
  1825. }
  1826. .title-container {
  1827. width: 100%;
  1828. height: 150rpx;
  1829. background-color: white;
  1830. display: flex;
  1831. flex-direction: column;
  1832. justify-content: flex-end;
  1833. }
  1834. .title {
  1835. /* border: 1px solid #ff0000; */
  1836. position: relative;
  1837. /* 关键:作为绝对定位的父容器 */
  1838. width: 100%;
  1839. height: 80rpx;
  1840. display: flex;
  1841. justify-content: center;
  1842. align-items: center;
  1843. }
  1844. .back-homepage-btn {
  1845. margin-left: 40rpx;
  1846. margin-right: auto;
  1847. height: 30rpx;
  1848. width: 20rpx;
  1849. z-index: 1;
  1850. }
  1851. .mid-title {
  1852. position: absolute;
  1853. left: 0;
  1854. right: 0;
  1855. display: flex;
  1856. justify-content: center;
  1857. align-items: center;
  1858. }
  1859. .left-page {
  1860. height: 40rpx;
  1861. width: 40rpx;
  1862. }
  1863. .right-page {
  1864. height: 40rpx;
  1865. width: 40rpx;
  1866. }
  1867. .stock-id {
  1868. margin: 0rpx 40rpx;
  1869. display: flex;
  1870. flex-direction: column;
  1871. text-align: center;
  1872. }
  1873. .stock-name {
  1874. font-weight: bold;
  1875. }
  1876. .stock-code {
  1877. font-size: 0.8rem;
  1878. font-weight: bold;
  1879. color: #a1a1a1;
  1880. }
  1881. .search {
  1882. height: 40rpx;
  1883. width: 40rpx;
  1884. }
  1885. .more {
  1886. height: 100%;
  1887. display: flex;
  1888. justify-content: center;
  1889. align-items: center;
  1890. margin-right: 40rpx;
  1891. margin-left: 20rpx;
  1892. }
  1893. .body {
  1894. overflow: auto;
  1895. height: calc(100vh - 305rpx);
  1896. /* border: 1px solid red; */
  1897. }
  1898. .stock-information {
  1899. background-color: white;
  1900. width: 100%;
  1901. height: 200rpx;
  1902. margin: 10rpx 0rpx;
  1903. display: flex;
  1904. align-items: center;
  1905. position: relative;
  1906. /* 为伪元素定位做准备 */
  1907. }
  1908. /* 右下角黑色三角形 */
  1909. .stock-information::after {
  1910. content: "";
  1911. position: absolute;
  1912. bottom: 0;
  1913. right: 0;
  1914. width: 0;
  1915. height: 0;
  1916. border-left: 20rpx solid transparent;
  1917. border-bottom: 20rpx solid #6a6a6a;
  1918. }
  1919. .stock-detail-container {
  1920. position: absolute;
  1921. top: 100%;
  1922. /* 在父容器下方 */
  1923. left: 0;
  1924. /* 从左边开始 */
  1925. right: 0;
  1926. /* 到右边结束 */
  1927. width: 100%;
  1928. /* height: 300rpx; 使用固定高度替代calc,避免计算问题 */
  1929. height: calc(100vh - 515rpx);
  1930. display: flex;
  1931. flex-direction: column;
  1932. background-color: rgba(0, 0, 0, 0.6);
  1933. z-index: 100;
  1934. box-sizing: border-box;
  1935. pointer-events: auto;
  1936. }
  1937. .stock-detail {
  1938. border: 1px solid #cacaca;
  1939. width: 100%;
  1940. display: flex;
  1941. background-color: white;
  1942. padding: 10rpx 0;
  1943. }
  1944. .first-column,
  1945. .second-column {
  1946. width: 50%;
  1947. height: 100%;
  1948. display: flex;
  1949. flex-direction: column;
  1950. color: #6a6a6a;
  1951. font-size: 0.8rem;
  1952. }
  1953. .first-column-data,
  1954. .second-column-data {
  1955. display: flex;
  1956. justify-content: space-between;
  1957. align-items: center;
  1958. padding: 10rpx 40rpx;
  1959. }
  1960. .stock-detail-title {
  1961. color: #6a6a6a;
  1962. }
  1963. .stock-detail-value {
  1964. color: black;
  1965. }
  1966. .price-up {
  1967. color: #10b981;
  1968. }
  1969. .price-down {
  1970. color: #ef4444;
  1971. }
  1972. .price-none {
  1973. color: black;
  1974. }
  1975. .stock-current-data {
  1976. width: 30%;
  1977. height: 70%;
  1978. display: flex;
  1979. flex-direction: column;
  1980. }
  1981. .stock-current-price {
  1982. font-weight: bold;
  1983. font-size: 1.2rem;
  1984. width: 100%;
  1985. height: 50%;
  1986. display: flex;
  1987. justify-content: center;
  1988. align-items: center;
  1989. }
  1990. .stock-current-other {
  1991. width: 100%;
  1992. height: 50%;
  1993. display: flex;
  1994. font-weight: bold;
  1995. font-size: 0.6rem;
  1996. }
  1997. .stock-current-value {
  1998. width: 50%;
  1999. height: 100%;
  2000. display: flex;
  2001. justify-content: center;
  2002. align-items: center;
  2003. }
  2004. .stock-current-ratio {
  2005. width: 50%;
  2006. height: 100%;
  2007. display: flex;
  2008. justify-content: center;
  2009. align-items: center;
  2010. }
  2011. .stock-other-data {
  2012. height: 100%;
  2013. width: 70%;
  2014. display: flex;
  2015. flex-direction: column;
  2016. }
  2017. .first-line,
  2018. .second-line,
  2019. .third-line {
  2020. display: flex;
  2021. align-items: center;
  2022. width: 100%;
  2023. height: 33%;
  2024. /* font-weight: bold; */
  2025. font-size: 0.8rem;
  2026. }
  2027. .value {
  2028. margin-left: auto;
  2029. margin-right: 20rpx;
  2030. }
  2031. .high-price,
  2032. .volume,
  2033. .volume-ratio,
  2034. .low-price,
  2035. .amount,
  2036. .market-earn,
  2037. .open-price,
  2038. .turnover-ratio,
  2039. .market-value {
  2040. display: flex;
  2041. flex: 1;
  2042. }
  2043. .stock-chart {
  2044. width: 100%;
  2045. }
  2046. .stock-kline-tab {
  2047. display: flex;
  2048. width: 100%;
  2049. height: 80rpx;
  2050. /* border: 1px solid black; */
  2051. }
  2052. .tab-day,
  2053. .tab-month,
  2054. .tab-time,
  2055. .tab-week,
  2056. .tab-more,
  2057. .tab-setting {
  2058. display: flex;
  2059. justify-content: center;
  2060. align-items: center;
  2061. flex: 1;
  2062. font-size: 0.8rem;
  2063. color: #6a6a6a;
  2064. position: relative;
  2065. }
  2066. /* 向下小三角样式 */
  2067. .arrow-down {
  2068. width: 0;
  2069. height: 0;
  2070. border-left: 10rpx solid transparent;
  2071. border-right: 10rpx solid transparent;
  2072. border-bottom: 12rpx solid currentColor;
  2073. margin-left: 8rpx;
  2074. display: inline-block;
  2075. }
  2076. .arrow-up {
  2077. width: 0;
  2078. height: 0;
  2079. border-left: 10rpx solid transparent;
  2080. border-right: 10rpx solid transparent;
  2081. border-top: 12rpx solid currentColor;
  2082. margin-left: 8rpx;
  2083. display: inline-block;
  2084. }
  2085. .arrow-left {
  2086. width: 0;
  2087. height: 0;
  2088. border-top: 10rpx solid transparent;
  2089. border-bottom: 10rpx solid transparent;
  2090. border-right: 12rpx solid currentColor;
  2091. margin-left: 8rpx;
  2092. display: inline-block;
  2093. }
  2094. .arrow-right {
  2095. width: 0;
  2096. height: 0;
  2097. border-top: 10rpx solid transparent;
  2098. border-bottom: 10rpx solid transparent;
  2099. border-left: 12rpx solid currentColor;
  2100. margin-left: 8rpx;
  2101. display: inline-block;
  2102. }
  2103. .moreTabsContainer {
  2104. width: 100%;
  2105. display: flex;
  2106. gap: 5rpx;
  2107. margin-bottom: 5rpx;
  2108. }
  2109. .moreTabItem {
  2110. flex: 1;
  2111. padding: 10rpx 20rpx;
  2112. border-radius: 10rpx;
  2113. background-color: white;
  2114. font-size: 0.85rem;
  2115. color: #6a6a6a;
  2116. text-align: center;
  2117. }
  2118. .tab-selected {
  2119. color: #db1f1d;
  2120. }
  2121. .tab-selected::after {
  2122. content: "";
  2123. position: absolute;
  2124. bottom: 10rpx;
  2125. left: 50%;
  2126. transform: translateX(-50%);
  2127. width: 40rpx;
  2128. height: 5rpx;
  2129. background-color: #db1f1d;
  2130. }
  2131. .tab-setting-img {
  2132. width: 30rpx;
  2133. height: 30rpx;
  2134. }
  2135. .time-chart-container {
  2136. width: 100%;
  2137. min-height: 400rpx;
  2138. background-color: #ffffff;
  2139. box-sizing: border-box;
  2140. }
  2141. .stock-chart {
  2142. display: flex;
  2143. flex-direction: column;
  2144. align-items: center;
  2145. }
  2146. .kline-chart-container {
  2147. width: 100%;
  2148. height: 400rpx;
  2149. background-color: #ffffff;
  2150. padding: 20rpx;
  2151. box-sizing: border-box;
  2152. display: flex;
  2153. justify-content: center;
  2154. align-items: center;
  2155. }
  2156. .bottomTool {
  2157. width: 100%;
  2158. height: 150rpx;
  2159. position: fixed;
  2160. bottom: 0;
  2161. background-color: white;
  2162. display: flex;
  2163. box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.1);
  2164. }
  2165. .index,
  2166. .function,
  2167. .favorites {
  2168. flex: 1;
  2169. display: flex;
  2170. justify-content: center;
  2171. align-items: center;
  2172. flex-direction: column;
  2173. font-size: 12px;
  2174. transition: all 0.2s ease;
  2175. }
  2176. .index:active,
  2177. .function:active,
  2178. .favorites:active {
  2179. background-color: rgba(99, 99, 99, 0.5);
  2180. transform: scale(0.95);
  2181. transition: all 0.1s ease;
  2182. }
  2183. .icon {
  2184. width: 40rpx;
  2185. height: 40rpx;
  2186. }
  2187. </style>