| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <template>
- <view class="sparkline-wrap" :style="wrapStyle">
- <view
- v-for="(pt, idx) in renderedPoints"
- :key="getItemKey(idx)"
- class="spark-dot"
- :class="{ 'spark-dot-last': isLast(idx) }"
- :style="dotStyle(idx, pt)"
- />
- </view>
- </template>
- <script>
- /**
- * §6.2 SparkLine 组件 — 纯 view 点阵实现,禁用 canvas。
- * 约束:
- * - 8 个数据点(n > 8 等距抽样,首末点强制保留)
- * - 末点 6rpx 主色实心,其余 4rpx 次级灰
- * - 不画连线
- */
- import { colorClassFor } from '@/utils/indicator-change.js'
- export default {
- name: 'SparkLine',
- props: {
- /** [{ reportDate, numericValue }],已按时间升序,长度 ≤ 12 */
- points: { type: Array, default: function() { return [] } },
- width: { type: Number, default: 60 }, // rpx
- height: { type: Number, default: 24 }, // rpx
- /** 趋势色,来自 utils/indicator-change.js 的 colorClassFor */
- colorClass: { type: String, default: 'gray-text' }
- },
- computed: {
- wrapStyle: function() {
- return 'width:' + this.width + 'rpx;height:' + this.height + 'rpx'
- },
- /** 抽样后的点集(保留首末点) */
- sampledPoints: function() {
- var pts = this.points || []
- var n = pts.length
- if (n === 0) return []
- if (n <= 8) return pts
- // 等距抽样,强制保留首末
- var limit = 8
- var step = (n - 1) / (limit - 1)
- var out = []
- for (var i = 0; i < limit - 1; i++) {
- out.push(pts[Math.round(i * step)])
- }
- out.push(pts[n - 1]) // ← 末点强制保留
- return out
- },
- /** 极值用于归一化 */
- minVal: function() {
- var pts = this.sampledPoints
- if (pts.length === 0) return 0
- var m = Infinity
- pts.forEach(function(p) {
- if (p && !isNaN(p.numericValue) && p.numericValue < m) m = p.numericValue
- })
- return m === Infinity ? 0 : m
- },
- maxVal: function() {
- var pts = this.sampledPoints
- if (pts.length === 0) return 1
- var m = -Infinity
- pts.forEach(function(p) {
- if (p && !isNaN(p.numericValue) && p.numericValue > m) m = p.numericValue
- })
- return m === -Infinity ? 1 : m
- },
- range: function() {
- var r = this.maxVal - this.minVal
- return r === 0 ? 1 : r
- },
- /** 渲染点集:每条含 left%、top%、是否是末点 */
- renderedPoints: function() {
- var pts = this.sampledPoints
- if (pts.length === 0) return []
- var n = pts.length
- var minV = this.minVal
- var rng = this.range
- return pts.map(function(p, i) {
- var v = p && !isNaN(p.numericValue) ? p.numericValue : minV
- var leftPct = (n === 1) ? 50 : (i / (n - 1)) * 100
- // top: minV → 100%, maxV → 0%
- var topPct = 100 - ((v - minV) / rng) * 100
- return { leftPct: leftPct, topPct: topPct, isLast: i === n - 1 }
- })
- }
- },
- methods: {
- getItemKey: function(idx) { return 's_' + idx },
- isLast: function(idx) {
- return idx === (this.renderedPoints.length - 1)
- },
- dotStyle: function(idx, pt) {
- var size = pt.isLast ? 6 : 4
- return 'left:' + pt.leftPct + '%;top:' + pt.topPct + '%;width:' + size + 'rpx;height:' + size + 'rpx'
- }
- }
- }
- </script>
- <style scoped lang="scss">
- .sparkline-wrap {
- position: relative;
- overflow: hidden;
- display: inline-block;
- }
- .spark-dot {
- position: absolute;
- border-radius: 999rpx;
- transform: translate(-50%, -50%);
- // 默认次级灰
- background: $app-text-subtle;
- }
- .spark-dot-last {
- // 末点主色(由 colorClass 决定)
- }
- .green-text .spark-dot-last { background: $status-success-strong-2; }
- .red-text .spark-dot-last { background: $status-danger-deep; }
- .gray-text .spark-dot-last { background: $facade-text-muted; }
- </style>
|