Echarts饼图 & Table数据联动

效果图: 

 

 

代码: 

<template>
    <div>
        <div id="myChart" :style="{width: '1000px', height: '400px'}"></div>
 
        <el-table :data="chartData" style="width:420px; margin-left: 300px" class="jk-table" :row-style="TableRowStyle">
            <el-table-column type="index" width="30" class-name="iconfont icon-circle" />
            <el-table-column prop="name" label="位置"></el-table-column>
            <el-table-column prop="value" label="数量"></el-table-column>
        </el-table>
    </div>
</template>

<script>
// 引入echarts
import * as echarts from 'echarts'
import {onMounted} from "vue";
import html2Canvas from 'html2canvas'
import JsPDF from 'jspdf'
 
export default {
    setup() {
        // const color = Math.random().toString(16).substr(2, 6).toUpperCase(); //生成随机颜色
        const chartData = [
            { value: 1048, name: 'Search Engine' },
            { value: 735, name: 'Direct' },
            { value: 580, name: 'Email' },
            { value: 484, name: 'Union Ads' },
            { value: 300, name: 'Video Ads' },
        ]
 
        const chartColors = ['rgb(254,67,101)','rgb(252,157,154)','rgb(249,205,173)','rgb(200,200,169)','rgb(131,175,155)']
 
        // 合并两个数组
        chartData.forEach( (x, key) => {
            chartColors.forEach( (i, keyss) => {
                if( key == keyss) {
                    x.color = i
                }
            })
        })
 
        onMounted(() => { // 需要获取到element,所以是onMounted的Hook
            let myChart = echarts.init(document.getElementById("myChart123"));
             
            // 绘制图表
            myChart.setOption({
                // title: {
                //     text: 'Referer of a Website',
                //     subtext: 'Fake Data',
                //     // left: 'center'
                // },
                tooltip: {
                    trigger: 'item',
                    formatter: '{a} <br/>{b} : {c} ({d}%)'
                },
                 legend: {
                    top: '5%',
                    left: 'center'
                },
                series: [
                    {
                        name: 'Access From',
                        type: 'pie',
                        radius: ['50%', '66%'],
                        label: {
                            show: false,
                            position: 'center'
                        },
                        data: chartData,
                        // emphasis: { //设置鼠标移入饼图的样式
                        //     itemStyle: {
                        //         shadowBlur: 10,
                        //         shadowOffsetX: 0,
                        //         shadowColor: 'rgba(0, 0, 0, 0.2)'
                        //     }
                        // }
                    }
                ],
                color: chartColors
            });
 
            window.onresize = function () { // 自适应大小
                myChart.resize();
            };
        });
 
        //设置table背景色
        const TableRowStyle = ({ row, rowIndex }) => {
            // 注意,这里返回的是一个对象
            let rowBackground = {};
            rowBackground.color = row.color;
            return rowBackground;
        }
 
        return {
            chartData,
            TableRowStyle
        }
    }
}
</script>

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值