vue3纵向表格走势图表

案例需求:类似彩票开奖历史结果走势

实现思路:表格作为定位范围,开奖结果为标注节点以表格为父节点获取标注节点所在表格的定位坐标,数据上下相邻的两个标注节点为一个绘线图.

以下为部分案例代码,仅供参考

注意:table 必须是relative定位

<template>
    <!--历史结果-->
    <div class="open-result-container">
        <table ref="tableContainer" class="base-table">
            <colgroup>
                 <col width="30" height="30">
                 <col width="170" height="30">
                 <col 
                 v-for="(item,index) in 100" 
                     :key="index" 
                     width="30"
                     height="30"
                     :style="tableColStyle(index, 10)"
            </colgroup>
            <thead>
                 <tr>
                      <th>期数</th>
                      <th>开奖号码</th>
                      <template v-for="(item,index) in 10" :key="index">
                          <th v-for="(num,inde) in 10" :key="inde">{{inde + 1}}</th>
                      </template>
                 </tr>
            </thead>
            <tbody>
                 <tr v-for="(item,ind) in historyResultList.bodyList" :key="item.preIssue">
                      <td>{{item.preIssue}}</td>
                      <td>{{item.preDrawCode}}</td>
                      <template v-for="(missing,index) in item.subBodyList" :key="index">
                           <td v-for="(num,inde) in missing.missing" :key="inde">
                                <!-- 这里num数据大于0的是标注开奖项 -->
                                <span v-if="num > 0" :class="['mark',`mark_${ind}_${index}`]" :style="{background: colorList[index]}">{{inde + 1}}</span>
                                <span v-else>{{Math.abs(num)}}</span>
                           </td>
                      </template>
                      ...
                 </tr>
            </tbody>
            <div class="draw-line">
                 <template
                       v-for="(item,index) in startAndEndPositionArr" 
                       :key="index">
                        <!-- 多组走势图绘制 -->
                       <draw-line-canvas 
                          v-for="(colArr,inde) in item" 
                          :key="inde"
                          :startAndEndPosition="colArr" 
                          :lineColor="colorList[index][1]" />
                 </template>
             </div>
        </table>
    </div>
</template>
<script setup>
...
import drawLineCanvas from "../components/drawLineCanvas.vue"

// table 节点
const tableContainer = ref(null)

// 标点坐标系存储数据 同一表格多组走势图则保存成多维数组
const startAndEndPositionArr = ref([])

// 数据
const historyResultList = ref([])

// 计算并记录标点坐标系
const calcPosition = () => {
    if (!historyResult.value.bodyList || !historyResult.value.bodyList.length) {
        startAndEndPositionArr.value = []
        return
    }
    // 此处以 10 个走势图为例
    startAndEndPositionArr.value = [...Array.from(new Array(10)).map(() => { return [] })]
    let lastXYArr = []
    for(let index in historyResult.value.bodyList) {
        let subBodyList = historyResult.value.bodyList[index].subBodyList
        for(let inde in subBodyList) {
            let oDom = document.querySelector(`.mark_${index}_${inde}`);
            if (!oDom) continue
            let width = oDom.offsetWidth / 2
            let height = oDom.offsetHeight / 2
            // 获取号码元素相对table标签相对X Y
            let xyArr = [
                oDom.getBoundingClientRect().left - tableContainer.value.getBoundingClientRect().left + width,
                oDom.getBoundingClientRect().top - tableContainer.value.getBoundingClientRect().top + height
            ]
            if (!lastXYArr[inde]) {
                lastXYArr[inde] = xyArr
                continue
            } else {
                let nextXYArr = xyArr
                startAndEndPositionArr.value[inde].push([lastXYArr[inde],nextXYArr])
                lastXYArr[inde] = xyArr
            }
        }
    }
}

// 色调表
const colorList = reactive([
    ["#fcf8f3", "#fba75e"], // 0 底色,1 主题色
    ["#f0f9fc", "#1fa6e8"],
    ["#f0fcf0", "#08bf02"],
    ["#f0f1fc", "#8585fb"],
    ["#f0fcf7", "#46bd95"],
    ["#fcf0f5", "#e26bab"],
    ["#fcf8f3", "#f2b653"],
    ["#f0f1fc", "#628ef3"],
    ["#f0fcf0", "#5ec642"],
    ["#fff4f4", "#f66d6d"],
])

// 查询数据
const getData = () => {
    queryApi().then(res => {
        historyResultList = res.data
        // 这里返回的数据结构如下 仅供参考
        /** res.data = {
            bodyList: [
                {
                    preDrawCode: "",
                    preIssue: "",
                    subBodyList: [
                        {
                            missing: [-1,-3,1,....],
                            ...
                        }
                    ]
                }
            ]
        } */
        
        nextTick(() => {
            calcPosition()
        })
    })
}

onMounted(() => {
    getData()
})
</script>

绘制线条组件 drawLineCanvas.vue

<template>
<!-- canvas 画走势两点直线 -->
    <canvas ref="myCanvas"></canvas>
</template>

<script setup>
import { nextTick, ref, watch, computed } from "vue"

const props = defineProps({
    startAndEndPosition: {
        type: Array,
        default: () => {
            return [
                [],// 前标点坐标[X,y]
                [] // 后标点坐标[X,Y]
            ]
        }
    },
    lineColor: {
        type: String,
        default: "#1fa6e8"
    },
    zIndex: {
        type: Number,
        default: 1
    },
    lineWidth: {
        type: Number,
        default: 1
    }
})

const startAndEndPosition = computed(() => {
    return props.startAndEndPosition
})

const myCanvas = ref(null)
const myCanvasContext = ref(null)

watch(startAndEndPosition,() => {
    if (myCanvasContext.value) {
        drawMap(startAndEndPosition.value)
    } else{
        nextTick(() => {
            if (startAndEndPosition.value[0][0] !== undefined) {
                drawMap(startAndEndPosition.value)
            }
        })
    }
},{
    immediate: true,
    deep: true
})

// 绘制线条
const drawMap = (item) => {
    let width = item[1][0] - item[0][0]
    let height = item[1][1] - item[0][1]
    let moveToArrr = []
    let lineToArr = []
    let left = 0
    let top = item[0][1]
    if (width === 0) {
        // 垂直向下划线 
        width = props.lineWidth // 宽带加宽 垂直居中绘图 避免线条变细
        left = item[0][0] - (props.lineWidth / 2)
        moveToArrr = [1,0]
        lineToArr = [1,height]
    } else if (width < 0) {
        // 从右上角往左下角划线
        width *= -1
        left = item[1][0]
        moveToArrr = [width,0]
        lineToArr = [0,height]
    } else {
        // 从左上角往右下角划线
        left = item[0][0]
        moveToArrr = [0,0]
        lineToArr = [width,height]
    }
    myCanvas.value.width = width
    myCanvas.value.height = height
    myCanvas.value.style.position = "absolute"
    myCanvas.value.style.zIndex = props.zIndex
    myCanvas.value.style.left = left + "px"
    myCanvas.value.style.top = top + "px"
    myCanvasContext.value = myCanvas.value.getContext('2d');
    myCanvasContext.value.lineWidth = props.lineWidth
    myCanvasContext.value.strokeStyle = props.lineColor
    myCanvasContext.value.moveTo(...moveToArrr)
    myCanvasContext.value.lineTo(...lineToArr)
    myCanvasContext.value.stroke()
}
</script>

效果图:

### 回答1: Vue Element Table 纵向表格是一种表格布局方式,其中表头和表体的列是垂直排列的,而行则是水平排列的。这种表格布局方式适用于需要展示大量数据的情况,可以让用户更方便地查看和比较数据。Vue Element Table 纵向表格还支持排序、筛选、分页等功能,可以满足不同的数据展示需求。 ### 回答2: Vue Element Table是一个基于Vue.js的高效表格组件,它提供了丰富的表格需求实现方式,包括纵向表格。所谓纵向表格就是需要在表格中展示多列数据,同时列名跨行展示,一般用于展示多条件的数据对比,如财务报表等。 在Vue Element Table中实现纵向表格,需要先定义好表头,即列名。表头可以使用el-table-column组件来实现,该组件具有属性label和prop,分别代表列名和数据对应的字段名。使用纵向表格时,需要同时设置rowspan属性,用于定义列名所占据的行数。 下一步是定义表格数据。表格数据是一个数组,数组中每个元素代表一行数据,其中每个元素的属性名需与列名中的prop属性一致,用于映射数据到表格中。在纵向表格中,由于一行数据可能会对应多个字段,因此需要用对象或数组来表示某一行的多列数据。 完成表头和表格数据的定义后,需要将它们传给Vue Element Table组件,并在template中使用el-table和el-table-column组件构建表格结构。在el-table-column组件中,需要根据表头定义,使用自定义的template来显示数据,同时为表头设置rowspan属性。 代码示例: <template> <el-table :data="tableData" style="width: 100%"> <el-table-column label="字段1" prop="field1" rowspan="3"> <template slot-scope="scope">{{ scope.row.field1 }}</template> </el-table-column> <el-table-column label="字段2" prop="field2" rowspan="3"> <template slot-scope="scope">{{ scope.row.field2 }}</template> </el-table-column> <el-table-column label="字段3" prop="field3" rowspan="2"> <template slot-scope="scope">{{ scope.row.field3 }}</template> </el-table-column> <el-table-column label="字段4" prop="field4" rowspan="2"> <template slot-scope="scope">{{ scope.row.field4 }}</template> </el-table-column> <el-table-column label="字段5" prop="field5" rowspan="2"> <template slot-scope="scope">{{ scope.row.field5 }}</template> </el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { field1: '数据1', field2: '数据2', field3: ['数据3', '数据4'], field4: ['数据5', '数据6'], field5: ['数据7', '数据8'] }, { field1: '数据9', field2: '数据10', field3: ['数据11', '数据12'], field4: ['数据13', '数据14'], field5: ['数据15', '数据16'] } ] } } } </script> 以上示例中,表格中有5个字段需要展示,其中字段1和2跨3行,字段3-5跨2行,数据由一个数组来表示。当Vue Element Table渲染该表格时,会自动将数据显示在对应的单元格中,同时根据rowspan属性设置表头跨行展示。整个表格结构具有美观的外观和高效的性能。 ### 回答3: Vue Element Table是一种非常常见的数据表格展示组件,它支持水平和竖向布局。在Vue Element Table中,纵向表格是一种非常常见的表格布局方式。这种布局方式的特点是将列当作行来展示,即将表中的每一列转换为一行,原先的每一行则成为一个单元格。 纵向表格可以用来展示具有多种属性的数据,让用户能够更加轻松地比较不同属性之间的差异。例如,如果我们要展示一个商品列表,我们可以将商品名称、价格、销量等属性分别展示在每一个单元格中。 在使用Vue Element Table时,我们可以使用该组件提供的prop来控制表格的列数、行数和表头样式等参数。对于纵向表格,我们需要通过配置组件的columns参数来指定每一列的数据字段和样式。Vue Element Table还提供了一系列的插槽用于自定义表格的样式和内容。 总的来说,Vue Element Table的纵向表格是一种非常常见和实用的表格布局方式,它可以用于展示具备多属性的数据,并能够方便地进行比较和查看。如需更多详细信息,可参照Vue Element Table官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值