vue3 - 封装图表组件

把相同或者类似的图表进行封装
父组件使用:
1 2 3 4 5 6 7 8 9 | <Report :info= "main4" :xdata= "RXData4" :sdata= "RSData4" :title= "title4" :color= "color4" :barBorderRadius= "barBorderRadius4" class = "div" ></Report> |
引入组件
1 | import Report from "@/components/Report/index" ; |
定义变量
1 2 3 4 5 6 7 8 9 10 11 12 | const data = reactive({ RXData4: [], RSData4: [], }); const title4 = ref ( "重点应用单位" ); const color4 = ref ([ "#9FF6F6" , "#00E3E6" ]); const barBorderRadius4 = ref ([5, 5, 0, 0]); const main4 = ref ( "main4" ); const { RXData4, RSData4, } = toRefs(data); |
然后自己从接口里面取数据给RXData4和RSData4填充数据
ps:我页面组件使用了setup语法糖
子组件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | <template> <div :id= "info" ></div> </template> <script setup> import * as echarts from "echarts" ; const props = defineProps({ info: String, //dom的id title: String, xdata: { type: Array, default : [], }, sdata: { type: Array, default : [], }, barBorderRadius: { type: Array, default : [], }, color: { type: Array, default : [], }, }); const { info, title, xdata, sdata, barBorderRadius, color } = toRefs(props); watch( sdata, (newData, oldData) => { nextTick(() => { initReport(); }); }, { immediate: true , deep: true } ); function initReport() { const myChart = echarts.init(document.getElementById(info.value)); const option = { title: { text: title.value, left: "center" , padding: [40, 10], textStyle: { color: "#fff" , fontWeight: "lighter" , fontSize: 16, }, }, tooltip: { trigger: "axis" , //控制鼠标移入是否有提示信息 axisPointer: { type: "line" , }, }, grid: { top: "30%" , left: "5%" , right: "5%" , bottom: "6%" , containLabel: true , }, xAxis: { type: "category" , axisTick: { show: true , }, splitLine: { lineStyle: { color: "#666" , }, }, axisLabel: { interval: 0, rotate: 25, }, axisLine: { show: true , lineStyle: { color: "#fff" , }, }, data: xdata.value, }, yAxis: { type: "value" , axisTick: { show: true , }, splitLine: { lineStyle: { color: "#666" , }, }, axisLine: { show: true , lineStyle: { color: "#fff" , }, }, }, series: [ { data: sdata.value, type: "bar" , barWidth: 15, itemStyle: { normal: { // barBorderRadius: [5, 5, 0, 0], barBorderRadius: barBorderRadius.value, color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [ { offset: 0, color: color.value[0], // 0% 处的颜色 }, { offset: 1, color: color.value[1], // 100% 处的颜色 }, ]), }, }, }, ], }; myChart.setOption(option); window.addEventListener( "resize" , () => { myChart.resize(); }); } </script> <style> </style> |
ps:需要注意同步异步取数据的问题,我这边用的是在子组件用watch监听数组的方法,也可以在父组件中用v-if来实现,考虑到实现效果的美观度建议用watch和watcheffect
子组件props用defineprops后 可以直接使用props.属性取值 我这里是直接使用toRefs,然后直接使用x.value实现
本文仅提供参考,是本人闲时所写笔记,如有错误,还请赐教,作者:阿蒙不萌,大家可以随意转载
分类:
Echarts实战
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通