SparkLine.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <view class="sparkline-wrap" :style="wrapStyle">
  3. <view
  4. v-for="(pt, idx) in renderedPoints"
  5. :key="getItemKey(idx)"
  6. class="spark-dot"
  7. :class="{ 'spark-dot-last': isLast(idx) }"
  8. :style="dotStyle(idx, pt)"
  9. />
  10. </view>
  11. </template>
  12. <script>
  13. /**
  14. * §6.2 SparkLine 组件 — 纯 view 点阵实现,禁用 canvas。
  15. * 约束:
  16. * - 8 个数据点(n > 8 等距抽样,首末点强制保留)
  17. * - 末点 6rpx 主色实心,其余 4rpx 次级灰
  18. * - 不画连线
  19. */
  20. import { colorClassFor } from '@/utils/indicator-change.js'
  21. export default {
  22. name: 'SparkLine',
  23. props: {
  24. /** [{ reportDate, numericValue }],已按时间升序,长度 ≤ 12 */
  25. points: { type: Array, default: function() { return [] } },
  26. width: { type: Number, default: 60 }, // rpx
  27. height: { type: Number, default: 24 }, // rpx
  28. /** 趋势色,来自 utils/indicator-change.js 的 colorClassFor */
  29. colorClass: { type: String, default: 'gray-text' }
  30. },
  31. computed: {
  32. wrapStyle: function() {
  33. return 'width:' + this.width + 'rpx;height:' + this.height + 'rpx'
  34. },
  35. /** 抽样后的点集(保留首末点) */
  36. sampledPoints: function() {
  37. var pts = this.points || []
  38. var n = pts.length
  39. if (n === 0) return []
  40. if (n <= 8) return pts
  41. // 等距抽样,强制保留首末
  42. var limit = 8
  43. var step = (n - 1) / (limit - 1)
  44. var out = []
  45. for (var i = 0; i < limit - 1; i++) {
  46. out.push(pts[Math.round(i * step)])
  47. }
  48. out.push(pts[n - 1]) // ← 末点强制保留
  49. return out
  50. },
  51. /** 极值用于归一化 */
  52. minVal: function() {
  53. var pts = this.sampledPoints
  54. if (pts.length === 0) return 0
  55. var m = Infinity
  56. pts.forEach(function(p) {
  57. if (p && !isNaN(p.numericValue) && p.numericValue < m) m = p.numericValue
  58. })
  59. return m === Infinity ? 0 : m
  60. },
  61. maxVal: function() {
  62. var pts = this.sampledPoints
  63. if (pts.length === 0) return 1
  64. var m = -Infinity
  65. pts.forEach(function(p) {
  66. if (p && !isNaN(p.numericValue) && p.numericValue > m) m = p.numericValue
  67. })
  68. return m === -Infinity ? 1 : m
  69. },
  70. range: function() {
  71. var r = this.maxVal - this.minVal
  72. return r === 0 ? 1 : r
  73. },
  74. /** 渲染点集:每条含 left%、top%、是否是末点 */
  75. renderedPoints: function() {
  76. var pts = this.sampledPoints
  77. if (pts.length === 0) return []
  78. var n = pts.length
  79. var minV = this.minVal
  80. var rng = this.range
  81. return pts.map(function(p, i) {
  82. var v = p && !isNaN(p.numericValue) ? p.numericValue : minV
  83. var leftPct = (n === 1) ? 50 : (i / (n - 1)) * 100
  84. // top: minV → 100%, maxV → 0%
  85. var topPct = 100 - ((v - minV) / rng) * 100
  86. return { leftPct: leftPct, topPct: topPct, isLast: i === n - 1 }
  87. })
  88. }
  89. },
  90. methods: {
  91. getItemKey: function(idx) { return 's_' + idx },
  92. isLast: function(idx) {
  93. return idx === (this.renderedPoints.length - 1)
  94. },
  95. dotStyle: function(idx, pt) {
  96. var size = pt.isLast ? 6 : 4
  97. return 'left:' + pt.leftPct + '%;top:' + pt.topPct + '%;width:' + size + 'rpx;height:' + size + 'rpx'
  98. }
  99. }
  100. }
  101. </script>
  102. <style scoped lang="scss">
  103. .sparkline-wrap {
  104. position: relative;
  105. overflow: hidden;
  106. display: inline-block;
  107. }
  108. .spark-dot {
  109. position: absolute;
  110. border-radius: 999rpx;
  111. transform: translate(-50%, -50%);
  112. // 默认次级灰
  113. background: $app-text-subtle;
  114. }
  115. .spark-dot-last {
  116. // 末点主色(由 colorClass 决定)
  117. }
  118. .green-text .spark-dot-last { background: $status-success-strong-2; }
  119. .red-text .spark-dot-last { background: $status-danger-deep; }
  120. .gray-text .spark-dot-last { background: $facade-text-muted; }
  121. </style>