Files
hms/apps/miniprogram-uniapp/src/components/TrendChart.vue
iven 2c567bd772 fix(mp): T40 UI 审查全量修复 + 设计体系一致性优化
Phase 0 基础设施:
- statusTag.ts: getStatusInlineStyle() 移除内联 borderRadius/padding/fontSize,仅返回 {background, color}
- 新增 SEVERITY_COLORS + getSeverityStyle() + getSeverityLabel() 统一告警严重程度样式
- variables.scss: 新增 9 个语义颜色别名 ($success/$danger/$warning/$info 等)
- mixins.scss: 新增 status-inline mixin 统一状态标签样式
- 7 个消费者页面添加 @include status-inline CSS 补偿

Phase 1 HIGH 修复 (4 页面):
- P46 随访管理: 移除 getTypeStyle() 硬编码 fontSize,替换文字 Loading 为组件
- P45 咨询详情医护: 添加 Loading/ErrorState 三态模板 + error ref
- P02 健康数据: 添加 loading ref + Loading 组件 + 错误 toast 提示
- P48 告警中心: 替换本地 SEVERITY_COLORS/SEVERITY_LABELS 为 statusTag.ts 导出

Phase 2 全局一致性:
- 2.1 触控补全: 17 页面为可点击元素添加 min-height: $touch-min
- 2.2 字号替换: 19 文件 31 处硬编码 px → Design Token CSS 变量
- 2.3 颜色替换: 18 文件 ~50 处硬编码十六进制 → SCSS 语义变量
- 2.4 elder-mode.scss: 新增 9 个选择器到触控放大清单

Phase 3 LOW 修复:
- 3.1 统一 Loading: 21 页面旧式文字加载 → <Loading> 组件
- 3.2 useElderClass: 8 页面补全长者模式 class 绑定
- 3.3 零散修复: 按钮 44px→48px,诊断记录添加 scroll-view 无限加载

同时新增 UniApp (Vue 3 + Vite) 小程序完整代码库 (146 文件)
2026-05-15 11:22:51 +08:00

176 lines
5.1 KiB
Vue

<template>
<view v-if="!data || data.length === 0" class="trend-chart-empty">
<text class="trend-chart-empty-text">暂无数据</text>
</view>
<view v-else class="trend-chart" :style="{ height: (height || 500) + 'rpx' }">
<canvas type="2d" id="trend-chart-canvas" class="trend-canvas"
:style="{ width: '100%', height: '100%' }" />
</view>
</template>
<script setup lang="ts">
import { watch, onMounted, nextTick, ref } from 'vue'
const props = withDefaults(defineProps<{
data: { date: string; value: number }[]
referenceMin?: number
referenceMax?: number
unit?: string
height?: number
}>(), {
unit: '',
height: 500,
})
const canvasReady = ref(false)
function drawLine(ctx: any, points: { x: number; y: number }[]) {
if (points.length < 2) return
ctx.beginPath()
ctx.moveTo(points[0].x, points[0].y)
for (let i = 1; i < points.length; i++) {
const prev = points[i - 1]
const curr = points[i]
const cpx = (prev.x + curr.x) / 2
ctx.bezierCurveTo(cpx, prev.y, cpx, curr.y, curr.x, curr.y)
}
ctx.stroke()
}
function draw() {
if (!props.data || props.data.length === 0) return
const query = uni.createSelectorQuery()
query.select('#trend-chart-canvas').fields({ node: true, size: true }).exec((res) => {
if (!res || !res[0] || !res[0].node) return
const canvas = res[0].node
const ctx = canvas.getContext('2d')
const dpr = uni.getSystemInfoSync().pixelRatio || 2
const w = res[0].width
const h = res[0].height
canvas.width = w * dpr
canvas.height = h * dpr
ctx.scale(dpr, dpr)
const pad = { top: 20, right: 16, bottom: 32, left: 48 }
const cw = w - pad.left - pad.right
const ch = h - pad.top - pad.bottom
const values = props.data.map(d => d.value)
let yMin = Math.min(...values)
let yMax = Math.max(...values)
if (props.referenceMin !== undefined) yMin = Math.min(yMin, props.referenceMin)
if (props.referenceMax !== undefined) yMax = Math.max(yMax, props.referenceMax)
const yPad = (yMax - yMin) * 0.1 || 1
yMin -= yPad
yMax += yPad
ctx.clearRect(0, 0, w, h)
// Reference band
if (props.referenceMin !== undefined && props.referenceMax !== undefined) {
const ry1 = pad.top + ch * (1 - (props.referenceMax - yMin) / (yMax - yMin))
const ry2 = pad.top + ch * (1 - (props.referenceMin - yMin) / (yMax - yMin))
ctx.fillStyle = 'rgba(91, 122, 94, 0.1)'
ctx.fillRect(pad.left, ry1, cw, ry2 - ry1)
}
// Grid lines
ctx.strokeStyle = '#e5e5e5'
ctx.lineWidth = 0.5
for (let i = 0; i <= 4; i++) {
const y = pad.top + (ch / 4) * i
ctx.beginPath()
ctx.moveTo(pad.left, y)
ctx.lineTo(pad.left + cw, y)
ctx.stroke()
const val = yMax - ((yMax - yMin) / 4) * i
ctx.fillStyle = '#78716C'
ctx.font = '10px sans-serif'
ctx.textAlign = 'right'
ctx.fillText(val.toFixed(1), pad.left - 6, y + 3)
}
// X labels
ctx.textAlign = 'center'
ctx.fillStyle = '#78716C'
ctx.font = '10px sans-serif'
const step = Math.max(1, Math.floor(props.data.length / 6))
for (let i = 0; i < props.data.length; i += step) {
const x = pad.left + (cw / Math.max(1, props.data.length - 1)) * i
ctx.fillText(props.data[i].date.slice(5), x, h - 8)
}
// Data points
const points = props.data.map((d, i) => ({
x: pad.left + (cw / Math.max(1, props.data.length - 1)) * i,
y: pad.top + ch * (1 - (d.value - yMin) / (yMax - yMin)),
}))
// Area fill
ctx.beginPath()
ctx.moveTo(points[0].x, points[0].y)
for (let i = 1; i < points.length; i++) {
const cpx = (points[i - 1].x + points[i].x) / 2
ctx.bezierCurveTo(cpx, points[i - 1].y, cpx, points[i].y, points[i].x, points[i].y)
}
ctx.lineTo(points[points.length - 1].x, pad.top + ch)
ctx.lineTo(points[0].x, pad.top + ch)
ctx.closePath()
const grad = ctx.createLinearGradient(0, pad.top, 0, pad.top + ch)
grad.addColorStop(0, 'rgba(196, 98, 58, 0.3)')
grad.addColorStop(1, 'rgba(196, 98, 58, 0.02)')
ctx.fillStyle = grad
ctx.fill()
// Line
ctx.strokeStyle = '#C4623A'
ctx.lineWidth = 2
drawLine(ctx, points)
// Dots
for (let i = 0; i < points.length; i++) {
const d = props.data[i]
const outOfRange =
(props.referenceMin !== undefined && d.value < props.referenceMin) ||
(props.referenceMax !== undefined && d.value > props.referenceMax)
ctx.beginPath()
ctx.arc(points[i].x, points[i].y, outOfRange ? 5 : 3, 0, Math.PI * 2)
ctx.fillStyle = outOfRange ? '#B54A4A' : '#C4623A'
ctx.fill()
}
})
}
onMounted(() => {
nextTick(() => { canvasReady.value = true; draw() })
})
watch(() => [props.data, props.referenceMin, props.referenceMax], () => {
if (canvasReady.value) nextTick(draw)
})
</script>
<style lang="scss" scoped>
.trend-chart {
background: $card;
border-radius: $r;
padding: 16px;
box-shadow: $shadow-sm;
}
.trend-canvas {
display: block;
}
.trend-chart-empty {
@include flex-center;
padding: 40px;
}
.trend-chart-empty-text {
font-size: var(--tk-font-body-sm);
color: $tx3;
}
</style>