index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <view class="page">
  3. <uni-card>
  4. <view class="charts-box">
  5. <qiun-data-charts
  6. type="area"
  7. :ontouch="true"
  8. :opts="opts"
  9. :chartData="chartData"
  10. />
  11. </view>
  12. <uni-section :title="name" />
  13. </uni-card>
  14. </view>
  15. </template>
  16. <script setup>
  17. import { onLoad } from '@dcloudio/uni-app'
  18. import { getCurrentInstance, reactive, ref } from "vue";
  19. import { getDeviceRealTimeChartData } from "@/api/realTimeData"
  20. import dayjs from "dayjs"
  21. const { appContext } = getCurrentInstance()
  22. const t = appContext.config.globalProperties.$t
  23. const opts = reactive({
  24. color: ['#0876C3'],
  25. padding: [15, 10, 0, 25],
  26. legend: {
  27. show: false,
  28. },
  29. rotate: true,
  30. rotateLock: true,
  31. enableScroll: true,
  32. xAxis: {
  33. disableGrid: true,
  34. rotateLabel: true, // 旋转x轴文字
  35. rotateAngle: 45, // x轴文字旋转角度
  36. enableScroll: true,
  37. itemCount: 20,
  38. marginTop: 5,
  39. fontColor: '#666666',
  40. fontSize: 10,
  41. },
  42. yAxis: {
  43. gridColor: '#BABABA',
  44. gridType: "dash",
  45. dashLength: 10,
  46. data: [{
  47. fontSize: 12,
  48. fontColor: '#666666',
  49. }]
  50. },
  51. extra: {
  52. area: {
  53. type: "curve",
  54. opacity: 0.8, // 区域图透明度
  55. addLine: true,
  56. width: 2,
  57. gradient: true,
  58. activeType: "hollow"
  59. }
  60. }
  61. })
  62. const chartData = reactive({
  63. categories: [],
  64. series: [],
  65. })
  66. const name = ref('')
  67. // 加载图表数据,并转换格式
  68. const loadChartData = async (params) => {
  69. const source = (await getDeviceRealTimeChartData(params.deviceCode, params.type, {
  70. beginTime: dayjs().add(-1, 'day').format('YYYY-MM-DD HH:mm:ss'),
  71. endTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
  72. })).data
  73. // 1. 提取x轴时间
  74. const categories = source
  75. .map(item => dayjs(item.timestamp).format('YYYY-MM-DD HH:mm:ss'))
  76. // .sort((a, b) => new Date(a) - new Date(b)); // 按时间升序排列
  77. // console.log("🚀 ~ loadChartData ~ categories:", categories)
  78. // 2. 提取y轴数据
  79. const seriesData = source.map(item => item?.value ? item.value.toFixed(2) : 0); // 确保数值为数字,避免 NaN
  80. // console.log("🚀 ~ loadChartData ~ seriesData:", seriesData)
  81. chartData.categories = categories
  82. chartData.series = [{
  83. name: params.name,
  84. data: seriesData,
  85. textColor: '#333333',
  86. textSize: 10,
  87. }]
  88. // console.log(chartData)
  89. }
  90. onLoad((params) => {
  91. const title = params.name + t('realTimeData.detail.chartTitle')
  92. name.value = title.split('').join('\n')
  93. loadChartData(params)
  94. })
  95. </script>
  96. <style scoped lang="scss">
  97. :deep(.uni-card) {
  98. height: 100%;
  99. margin: 0 !important;
  100. padding: 0 !important;
  101. .uni-card__content {
  102. height: calc(100% - 10px);
  103. display: flex;
  104. flex-direction: row;
  105. }
  106. .uni-section {
  107. position: absolute;
  108. right: -120px;
  109. top: 100px;
  110. background-color: transparent;
  111. }
  112. .uni-section-header {
  113. padding-right: 30px;
  114. }
  115. .uni-section__content-title {
  116. font-size: 16px !important;
  117. color: #333333 !important;
  118. font-weight: 600;
  119. writing-mode: vertical-lr;
  120. transform: rotate(90deg);
  121. text-align: start;
  122. line-height: 1.5em;
  123. padding: 20px 0 !important;
  124. }
  125. }
  126. .charts-box {
  127. width: calc(100% - 30px);
  128. height: 100%;
  129. }
  130. </style>