<template>
|
<div class="mini-kline-chart">
|
<svg
|
v-if="linePoints"
|
class="mini-kline-svg"
|
viewBox="0 0 100 40"
|
preserveAspectRatio="none"
|
>
|
<!-- 折线:用收盘价连成线 -->{{ linePoints }}
|
<polyline
|
:points="linePoints"
|
fill="none"
|
:stroke="lineColor"
|
stroke-width="1.2"
|
stroke-linecap="round"
|
stroke-linejoin="round"
|
/>
|
</svg>
|
</div>
|
</template>
|
|
<script setup>
|
import { computed } from 'vue'
|
|
const props = defineProps({
|
/** K线/行情数据,每项 [open, close, low, high],折线取 close */
|
data: {
|
type: Array,
|
default: () => []
|
},
|
/** 涨跌,用于颜色:true 红涨绿跌,false 绿涨红跌 */
|
up: {
|
type: Boolean,
|
default: true
|
}
|
})
|
|
const upColor = '#E35561'
|
const downColor = '#11caa1'
|
|
const lineColor = computed(() => (props.up ? upColor : downColor))
|
|
const linePoints = computed(() => {
|
const list = props.data || []
|
if (!list.length) return ''
|
const closes = list.map((d) => d[1])
|
const min = Math.min(...closes)
|
const max = Math.max(...closes)
|
const range = max - min || 1
|
const n = list.length
|
const pad = 2
|
const chartH = 40 - pad * 2
|
const stepX = n > 1 ? 100 / (n - 1) : 0
|
|
return list
|
.map((d, i) => {
|
const close = d[1]
|
const x = n === 1 ? 50 : stepX * i
|
const y = pad + (1 - (close - min) / range) * chartH
|
return `${x},${y}`
|
})
|
.join(' ')
|
})
|
</script>
|
|
<style lang="scss" scoped>
|
.mini-kline-chart {
|
width: 100%;
|
height: 2.5rem;
|
min-height: 2rem;
|
}
|
|
.mini-kline-svg {
|
width: 100%;
|
height: 100%;
|
display: block;
|
}
|
</style>
|