vue3+typescript(Echarts tooltip中formatter自定义并求和)

vue3+typescript(Echarts tooltip中formatter自定义并求和)


一、自定义formatter(以下示例是关于折线图)

tooltip: {
    trigger: 'axis',
    // triggerOn: "click",
    alwaysShowContent: true,
    renderMode: "html", // 浮层的渲染模式,默认以 'html 即额外的 DOM 节点展示 tooltip;
    axisPointer: {
        // 坐标轴指示器配置项。
        type: "shadow", // 'line' 直线指示器  'shadow' 阴影指示器  'none' 无指示器  'cross' 十字准星指示器。
        axis: "auto", // 指示器的坐标轴。
        snap: true, // 坐标轴指示器是否自动吸附到点上
    },
    formatter: (params: any) => {
        var res = params[0].name + '<br/>' //tooltip 头部的标题
        //循环图表数据的每一项并用字符串拼接
        params.forEach((value: any, i: any) => {
            res += value.seriesName + ':' + value.data + '人' + '</br>' //tooltip里的数据必须要是字符串
        })
        //设置一个保存所有data的数组

        var arr: any = [];
        params.forEach((v: any, i: any) => {
            arr.push(Number(v.data))  //把数据push到数组中
        })

        //写一个reduce的函数进行求和
        function sum(arr: any) {
            return arr.reduce(function (total: any, value: any) {
                return total + value;
            }, 0);
        }
        res += '共计:' + sum(arr) + '人' //得到一个总数
        return res
    }
},

添加全部代码

<template>
    <div class="chart_box">
        <div id="myChart">
        </div>
    </div>
</template>
 
<script lang="ts">
import { defineComponent, onMounted, getCurrentInstance, ref } from 'vue'
import { GetDay, GetWeek, GetMouth } from "@/api/index";
import { number } from 'echarts';
export default defineComponent({
    setup() {
        //获取本日活跃用户数据
        let option: any = ref()
        const { proxy } = getCurrentInstance() as any
        GetDay().then((res: any) => {
            let DaysY: any = Object.values(res.data.data.lineChat)
            //获取keys
            let DaysX: any = Object.keys(res.data.data.lineChat)
            //设置数组增加字段
            let DayX: Array<string> = []
            DaysX.forEach((value: string) => {
                DayX.push(`${value}`)
            })
            option = {
                title: {
                    // text: `活跃用户统计`
                },
                tooltip: {
                    trigger: 'axis',
                    backgroundColor: 'rgba(#000819,#000819,#000819,0.5)',
                    textStyle: {
                        color: '#fff',
                    },
                    borderColor: "#00948D",
                    shadowColor: "#00948D",
                    // triggerOn: "click",
                    // alwaysShowContent: true,
                    renderMode: "html", // 浮层的渲染模式,默认以 'html 即额外的 DOM 节点展示 tooltip;
                    axisPointer: {
                        // 坐标轴指示器配置项。
                        type: "shadow", // 'line' 直线指示器  'shadow' 阴影指示器  'none' 无指示器  'cross' 十字准星指示器。
                        axis: "auto", // 指示器的坐标轴。
                        snap: true, // 坐标轴指示器是否自动吸附到点上
                    },
                    formatter: (params: any) => {
                        var res = params[0].name + '<br/>' //tooltip 头部的标题
                        //循环图表数据的每一项并用字符串拼接
                        params.forEach((value: any, i: any) => {
                            res += '在线人数' + ':' + value.data + '人' //tooltip里的数据必须要是字符串
                        })
                        return res
                    }
                },

                // legend: {
                //     data: ['本日']
                // },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                // toolbox: {
                //     feature: {
                //         saveAsImage: {}
                //     }
                // },
                xAxis: {
                    // type: 'category',
                    // boundaryGap: false,
                    name: `小时`,
                    data: DayX,
                    axisLine: {
                        lineStyle: {
                            color: '#0C9EB3', //x轴的颜色
                        }
                    },
                    nameTextStyle: {
                        color: "#fff"
                    },
                    axisLabel: {
                        color: "#fff"
                    },
                },
                yAxis: {
                    name: ``,
                    type: 'value',
                    nameTextStyle: {
                        color: "#fff",
                        padding: [0, 0, 5, -10]
                    },
                    axisLabel: {
                        color: "#fff"
                    },
                    splitLine: {
                        show: true,
                        lineStyle: {
                            type: 'dashed',
                            color: "#3670B3",
                            width: 0.5,
                        }
                    }
                    // axisLabel: {
                    //     formatter: '{value}'
                    // },
                    // scale: true,
                    // boundaryGap: [0, 0.2]

                },
                series: [
                    {
                        name: '本日',
                        type: 'line',
                        stack: 'Total',
                        smooth: true,
                        data: DaysY,
                        itemStyle: {
                            normal: {
                                color: "#33C5FF", //改变折线点的颜色
                                lineStyle: {
                                    color: "#33C5FF", //改变折线颜色
                                },
                            },
                        }
                    }
                ]
            }
            // 获取挂载的组件实例
            const echarts = proxy.$ECharts
            //初始化挂载
            const echarts1 = echarts.init(document.getElementById('myChart'))
            //添加配置
            echarts1.setOption(option, true)
            // 自适应
            // window.onresize = function () {
            //     echarts1.setOption(option)
            //     echarts1.resize()
            // }
            setTimeout(function () {
                window.addEventListener("resize", () => {
                    echarts1.resize()
                    echarts1.setOption(option, true)
                })
            }, 200)
        })


        onMounted(() => {

        })
        return {
        }
    }

})
</script>
<style scoped>
#myChart {
    width: 100%;
    height: 100%;
}

.chart_box {
    /* width: 60%; */
    flex: 1;
    height: 100%;
}
</style>

tooltip里必须是字符串拼接

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对你的问题,我可以给你提供一些思路和代码示例。 首先,你可以使用 `el-upload` 组件的 `before-upload` 和 `action` 属性来实现自定义上传图片的功能。具体来说,`before-upload` 用于在上传图片之前进行一些处理,而 `action` 则指定了上传图片的地址。 其次,在使用 `typescript` 时,你需要定义上传图片的数据类型。在 `vue3` ,你可以使用 `defineComponent` 函数来定义组件,并使用 `ref` 来引用组件的数据。 下面是一个示例代码,供你参考: ``` <template> <el-upload class="upload-demo" :action="uploadUrl" :before-upload="beforeUpload" :show-file-list="false"> <el-button size="small" type="primary">点击上传</el-button> </el-upload> </template> <script lang="ts"> import { defineComponent, ref } from 'vue'; interface UploadResponse { code: number; data: string; } export default defineComponent({ setup() { const uploadUrl = ref('your_upload_url'); const beforeUpload = (file: File) => { // 在上传之前处理图片 return true; }; const handleUploadSuccess = (response: UploadResponse) => { // 处理上传成功后的响应 }; return { uploadUrl, beforeUpload, handleUploadSuccess, }; }, }); </script> ``` 在这个示例,我定义了一个 `UploadResponse` 接口来表示上传图片后的响应数据类型。在 `setup` 函数,我使用 `ref` 来定义了 `uploadUrl` 变量,并将其传递给 `el-upload` 组件的 `action` 属性。同时,我还定义了 `beforeUpload` 和 `handleUploadSuccess` 函数来处理上传前和上传后的数据。 当然,这只是一个简单的示例,你还需要根据具体的需求进行代码的修改和优化。希望这些信息能够对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值