当前位置:网站首页 > Vue.js开发 > 正文

vue3中,echarts在柱形图上加基准线,自定义tooltip

vue3中,echarts在柱形图上加基准线,自定义tooltip

效果图

在这里插入图片描述

代码

index.vue

<template> <div class="content"> <!-- <titleHeader :src="img" :title="title" @is-dialog="handleEvery" class="header-style" /> <div ref="echartsRef" class="attendance-data"></div> <!-- 二级页面 --> <everyday-material :is-show="showEveryday" @is-close="handleCloseEveryday"></everyday-material> </div> </template> <script setup lang="ts" name="absenteeism"> import * as echarts from "echarts"; import { useEcharts } from "@/hooks/useEcharts"; import img from "@/assets/svg/more.svg"; import titleHeader from "@/components//header/titleHeader.vue"; import everydayMaterial from "./modules/everydayMaterial.vue"; import { ref, onMounted, nextTick } from "vue"; const title = ref("模块题目名称"); const echartsRef = ref<HTMLElement>(); // 二级页面 const showEveryday = ref(false); const handleCloseEveryday = () => { showEveryday.value = false; }; const handleEvery = () => { showEveryday.value = true; }; const chartsView = () => { let myChart: echarts.ECharts = echarts.init(echartsRef.value as HTMLElement); let option: echarts.EChartsCoreOption = { title: { text: "达成率", left: 20, top: 10, textStyle: { color: "#ccc", fontSize: 16 } }, tooltip: { trigger: "axis", backgroundColor: "rgba(0, 0, 0, 1)", borderColor: "rgba(0, 0, 0, 1)", padding: [16, 24], textStyle: { color: "#fff" }, // 改造弹框 formatter: (params: any) => { // console.log(params); // 打印数据 let ele: string = ``; ele += `<div style="font-size:16px;margin-bottom:5px;">达成率</div>`; for (let i = 0; i < params.length; i++) { ele += `<div style="display:flex;align-items: center;height:30px;font-size:12px;"><div style="width: 8px;height: 8px;background: ${params[i].color};border-radius: 50%;margin-right: 10px;"></div>${params[i].name}<div style="font-size:20px;font-weight:bold;color:${params[i].color};margin:0 0px 0 10px;">${params[i].data}</div> %</div>`; } return ele; } }, xAxis: { type: "category", data: ["大型材料", "支护用品", "五小电器", "坑木类", "其他"] }, yAxis: { size: "16px", type: "value", axisLabel: { formatter(value: string | number | any) { if (value > 0) { return value + "%"; } return Math.abs(value) + "%"; } } }, series: [ { type: "bar", data: [80, 65, 80, 90, 110], // data: [80, 65, 80, 90, 110].map(item => { // if (item > 80) { // return { // value: item, // itemStyle: { // color: "#F33" // } // }; // } // return item; // }), itemStyle: { normal: { color: "#0075FF", barBorderRadius: [10, 10, 0, 0] } }, showBackground: true, backgroundStyle: { color: "rgba(180, 180, 180, 0.2)" }, markLine: { symbol: ["none", "none"], //去掉箭头 lineStyle: { normal: { type: "dashed", color: "#00FFBB" //基准线颜色 } }, data: [ { name: "基准线名称", yAxis: 45 // 基准线数据 } ], label: { show: true, // position: "insideStartBottom", color: "#00FFBB" // 基准线文字颜色 } } } ] }; useEcharts(myChart, option); }; onMounted(() => { nextTick(() => { chartsView(); }); }); </script> <style scoped lang="scss"> .content { margin-bottom: 8px; background: rgb(0 0 0 / 30%); border-radius: 10px; span { font-size: 16px; color: #ffffff; } // .header-style { // padding-top: 16px; // padding-bottom: 16px; // } } .attendance-data { height: calc(100% - 54px); overflow-y: auto; } </style> 

src/components//header/titleHeader.vue

<template> <div class="header-container"> <div class="title-img"> <!-- title文字 --> <span>{ 
  { props.title }}</span> <!-- title单选 --> <slot name="right"></slot> <!-- title图片 --> <img @click="isDialog" class="header" :src="props.src" v-show="props.src != ''" /> </div> <div class="mark"> <slot name="mark" /> </div> </div> </template> <script lang="ts" setup> const props = defineProps({ src: { type: String, required: false, default: "" }, title: { type: String, required: true, default: "" } }); // 展示弹框 const emit = defineEmits(["is-dialog"]); const isDialog = () => { emit("is-dialog", ""); }; </script> <style lang="scss" scoped> .header-container { padding: 24px 24px 6px; .title-img { display: flex; font-family: "PingFang SC"; font-size: 22px; color: rgb(255 255 255 / 100%); align-items: center; > span { margin-right: auto; } > img { cursor: pointer; margin-left: 24px; height: 20px; } } } </style> 
到此这篇vue3中,echarts在柱形图上加基准线,自定义tooltip的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • vue中,js封装方法之判断js对象类型 & 封装方法之js对象深拷贝2024-11-30 14:54:06
  • npm相关之npm初始化、切换npm镜像源、package.json释义、require 的加载机制 & ES6降级处理-babel包2024-11-30 14:54:06
  • vue常见源码面试题2024-11-30 14:54:06
  • vue3中,全局自定义指令——DirectiveBinding & v-throttle节流-保存和提交按钮时间间隔内点击多次只执行一次2024-11-30 14:54:06
  • vue3项目初始化配置流程(含注释)2024-11-30 14:54:06
  • vue3中,echarts使用(四)02——柱状图之堆叠条形图-定制化 & 封装切换tag标签组件 & 封装title组件2024-11-30 14:54:06
  • vue3中,echarts使用(四)01——柱状图之堆叠条形图-官网示例2024-11-30 14:54:06
  • vue3中,echarts使用(三)——柱状图之legend图例组件配置 & formatter自定义显示内容 & 封装title组件2024-11-30 14:54:06
  • vue3中,echarts使用(二)——柱状图之滚动条设置dataZoom、基准线-平均值markLine & calc() 函数-用于动态计算长度值2024-11-30 14:54:06
  • vue3中,echarts使用(一)——柱状图和折线图的结合使用2024-11-30 14:54:06
  • 全屏图片