vue中实现(el-table)表格中,【echarts柱状图】的封装和调用

 1、创建 BarEcharts.vue 组件

<template>
    <div>
        <div id="bar"></div>
    </div>
</template>
<script>
import * as echarts from 'echarts'
export default {
    props: {
        markList: {
            type: Array,
            default: () => []
        }
    },
    data() {
        return {
            NumArr: [],
            CodeArr: []
        }
    },
    created() {
        this.$nextTick(() => {
            this.barBtn()
        })
        this.getMarkList()
    },
    methods: {
        getMarkList() {
            let a = ''
            let b = ''
            this.markList.forEach(mark => {
               a += mark.FactorDivide + ','
               b += mark.FactorCode + ','
            })
            this.NumArr = a.substring(0, a.length - 1).split(',').reverse().map(Number)
            this.CodeArr = b.substring(0, b.length - 1).split(',').reverse()
        },
        barBtn() {
            let myChart = echarts.init(document.getElementById('bar'))
            let option = {
                legend: {
                    show: false
                },
                grid: {
                    top: '0',
                    left: '0%',
                    right: '4%',
                    bottom: '0%',
                    containLabel: true
                },
                xAxis: {
                    type: 'value',
                    min: 0,
                    max: 5,
                    boundaryGap: [0, 0.01],
                    // 表格里面Y轴线条
                    splitLine: {
                        show: false
                    },
                    axisTick: {
                        // y轴刻度线
                        show: false
                    }
                },
                yAxis: {
                    type: 'category',
                    data: this.CodeArr,
                    // 表格里面Y轴线条
                    splitLine: {
                        show: false
                    },
                    axisTick: {
                        // y轴刻度线
                        show: false
                    }
                },
                series: [
                    {
                        name: '2011',
                        type: 'bar',
                        barWidth: 10, // 柱图宽度
                        markLine: {
                            itemStyle: {
                                color: 'red'
                            },
                            label: {
                                formatter: '阳性线',
                                itemStyle: {
                                    color: '#053d6f'
                                }
                            },
                            data: [
                                [
                                    {
                                        name: '两点之间的线',
                                        coord: [3, 0]
                                    },
                                    {
                                        name: '两点之间的线',
                                        coord: [3, 9]
                                    }
                                ]
                            ]
                        },
                        itemStyle: {
                             color: '#053d6f'
                        },
                        label: {
                            show: true,
                            position: 'right'
                        },
                        data: this.NumArr
                    }
                ]
            }
            myChart.setOption(option)
            // 让图表跟随屏幕自动的去适应
            window.addEventListener('resize', function () {
                myChart.resize()
            })
        }
    }
}
</script>
<style lang="scss" scoped>
#bar{
    width: 100mm;
    height: 110mm;
    margin: 0 auto;
}
</style>

2、在页面中调用

<template>
 <div class="detail_tables">
   <el-table :data="markList" border :span-method="objectSpanMethod">
        <el-table-column label="因子/指标名称" width="135">
            <template slot-scope="scope">
                {{scope.row.FactorCode}} {{scope.row.FactorName}}
            </template>
        </el-table-column>
        <el-table-column prop="FactorDivide" label="指标分数" width="82" align="center"></el-table-column>
        <el-table-column label="指标图" align="center">
            <template slot-scope="scope">
                <!-- 指标图 -->
                <BarEcharts :markList="markList"></BarEcharts>
            </template>
        </el-table-column>
        <el-table-column prop="DegreeLevel" label="程度" width="85" align="center"></el-table-column>
    </el-table>
  </div>
</template>
<script>
import BarEcharts '@/components/BarEcharts'
export default {
  components: {BarEcharts},
  data() {
    return {
      markList:[
       {FactorCode: 'F1', FactorName: '躯体化', FactorDivide: 2, DegreeLevel: '基本健康'},
       {FactorCode: 'F2', FactorName: '强迫', FactorDivide: 1.3, DegreeLevel: '需要改善'},
       {FactorCode: 'F3', FactorName: '人际关系敏感', FactorDivide: 2.5, DegreeLevel: '基本健康'},
       {FactorCode: 'F4', FactorName: '抑郁', FactorDivide: 0.7, DegreeLevel: '需要改善'},
       {FactorCode: 'F5', FactorName: '焦虑', FactorDivide: 1.8, DegreeLevel: '需要关注'},
       {FactorCode: 'F6', FactorName: '敌对', FactorDivide: 1.5, DegreeLevel: '需要关注'},
       {FactorCode: 'F7', FactorName: '恐怖', FactorDivide: 2.1, DegreeLevel: '需要关注'},
       {FactorCode: 'F8', FactorName: '偏执', FactorDivide: 1.6, DegreeLevel: '需要改善'},
       {FactorCode: 'F9', FactorName: '急性症状', FactorDivide: 0.8, DegreeLevel: '需要关注'},
       {FactorCode: 'F10', FactorName: '睡眠及饮食', FactorDivide: 1, DegreeLevel: '需要改善'}
       ]
     }
  },
  methods: {
    // 合并第3列
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
       if (columnIndex === 2) {
         if (rowIndex === 0) {
            return { // 合并第三列(columnIndex === 2),从第1行开始往下10行
              rowspan: 10,
              colspan: 1
            }
         } else {
            return {
               rowspan: 0,
               colspan: 0
            }
         }
       }
     }
  }
}
</script>

<style lang="scss" scoped>
/deep/ .detail_tables{
  display: flex;
  flex-wrap: wrap;
  margin: 10mm 0 20mm 0;
  .el-table{
     margin: 1.5mm;
  }
  .el-table__cell{
     font-size: 4mm;
     line-height: inherit;
     padding: 2mm 0px;
  }
  .el-table td, .el-table th.is-leaf,.el-table--border, .el-table--group{
     border-color: #333;
  }
}
</style>

      希望我的愚见能够帮助你哦~,若有不足之处,还望指出,你们有更好的解决方法,欢迎大家在评论区下方留言支持,大家一起相互学习参考呀~

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值