当前位置: 首页 > news >正文

Vue 封装echarts柱状图(Bar)组件

目的:减少重复代码,便于维护

显示效果

组件代码

<template><div class="ldw-data-content-box"><div class="ldw-chilren-box"><div class="title" v-if="title">{{ title }}</div><div style="width:100%;flex:1;"><div :id="'canvas-box'+number" style="width: 100%;height:100%;"></div></div></div></div>
</template><script>
var echarts = require("echarts");
const total = function(data){return data.reduce((prev,cur)=>{return prev+cur.value},0)
}
export default {props:{title:"",data:{require:true,type:Array,default:()=>[]},w:{type:String,default:"auto"},column:{type:Boolean,default:true}},data(){return{bg:["#0090FF","#31CFB8","#E55240"],number:null,top:0,h:100,myChart:null}},watch: {data: {//深度监听,可监听到对象、数组的变化handler(val, oldVal) {this.initData();},deep: true, //true 深度监听}},created(){this.number = Math.random(1000)+1;},mounted(){this.initData()},methods:{initData(){let that = thislet canvas = document.getElementById(`canvas-box${this.number}`)this.myChart = echarts.init(canvas);this.myChart.on("click", function(params) {that.$emit('eClick',params)});let option = {title: {},grid: {top: "8%",left: "3%",right: "4%",bottom: "8%",containLabel: true},tooltip: {trigger: 'axis',axisPointer: {type: 'shadow'}},legend: {show:false},xAxis: [{type: 'category',axisTick: {show: false},axisLine:{show:false},axisLabel: {interval:0,rotate:40  //倾斜的程度},splitLine:{show:false},data:this.data.map((res)=>{return res.type})}],yAxis: [{type: 'value',axisTick: {show: false},axisLine:{show:false},splitLine:{show:false},}],series: [{name: '总计',type: 'bar',barWidth:35,colorBy:"series",label: {// 柱图头部显示值show: true,textStyle:{fontSize:14,fontWeight:600},position: "top",color:"#E55240",fontSize: "14px",},emphasis:{itemStyle:{color:"#F89387"},},itemStyle:{color:{type: 'linear',colorStops: [{offset: 0,color:"#F89387" // 0% 处的颜色}, {offset: 1,color:"#E55240" // 100% 处的颜色}],global: false // 缺省为 false},},data: this.data.map((res)=>{return res.value})},]};this.myChart.setOption(option)},resize(){this.myChart.resize()},colorFormat(arr){arr.forEach((item)=>{if(item.ldwColor){item.itemStyle = {color:{type: 'linear',x: 0,y: 0,x2: 0,y2: 1,colorStops: [{offset: 0, color: item.ldwColor[0] // 0% 处的颜色}, {offset: 1, color: item.ldwColor[1] // 100% 处的颜色}],global: false // 缺省为 false}}}})return arr}}
}
</script><style scoped>
.ldw-data-content-box{width:100%;height:100%;display: flex;
}.ldw-data-content-box>.ldw-chilren-box{width:100%;height:100%;display: flex;justify-content: center;align-items: center;flex-flow: column;overflow: hidden;
}
.ldw-data-content-box>.ldw-chilren-box>.title{font-size: 18px;color:#000;text-align: center;padding:24px 0;
}.ldw-bg-box{background: rgba(255,255,255,0.5);border: 1px solid #F4FDFE;border-radius: 20px;
}.ldw-text-text{display: inline-block;text-align: justify;line-height: 0;margin-left: 20px;
}.ldw-text-text::after{content:"";display: inline-block;width:100%;overflow:hidden;height:0;
}.ldw-quan-quan{display: flex;align-items: center;
}.ldw-w{margin-top:6px;position: relative;
}.ldw-quan-box{width: 13px;height: 13px;border-radius: 2px;margin-right: 20px;
}.flex-column{width:100%;display: flex;justify-content: space-around;
}.flex-column .ldw-w{width: auto;
}.flex-column .ldw-quan-box{margin-right: 10px;
}
</style>

调用代码

<template><div class="root flex flex-col border-box"><div  style="width: 400px; height: 400px;"  ><Bar  :title="'统计'" :barType="'x'" :data="chartData" ></Bar></div></div>
</template><script>import Bar from '@/components/echarts/barTwoInfo.vue'export default{name:'',created() {},components: {Bar},data() {return {chartData:[{value:100, type:'一季度'},{value:105, type:'二季度'},{value:201, type:'三季度'},{value:167, type:'四季度'},]}},methods:{}}
</script>

http://www.lryc.cn/news/266311.html

相关文章:

  • 异常(Java)
  • vue的插槽解析
  • Spring(3)Spring从零到入门 - Spring整合技术及AOP事务管理
  • 适配器模式学习
  • NET中使用Identity+CodeFirst+Jwt实现登录、鉴权
  • 详解Keras3.0 API: Optimizers
  • 【数据结构】字符串匹配|BF算法|KMP算法|next数组的优化
  • 阿里云 ACK One 新特性:多集群网关,帮您快速构建同城容灾系统
  • vscode自定义代码片段
  • 【贪心算法】专题练习一
  • 【JMeter】使用nmon进行性能资源监控
  • Unity预设体
  • Elasticsearch 写入优化探索:是什么影响了refresh 耗时?
  • Java8新特性——函数式接口
  • Epson打印机连接wifi
  • Chapter 7 - 6. Congestion Management in Ethernet Storage Networks以太网存储网络的拥塞管理
  • 【论文笔记】NeuRAD: Neural Rendering for Autonomous Driving
  • 通信原理 | 分贝dB、功率、功率谱、功率谱密度、信噪比
  • Go中的Context是什么?
  • 碳排放预测 | 基于ARIMA和GM(1,1)的碳排放预测(Matlab)
  • FPFA.一种二倍频电路代码描述以及测量详情
  • dotnet命令创建C#项目,VSCode打开
  • 在GitHub找开源项目
  • GAMES101-LAB1
  • Docker 编译OpenHarmony 4.0 release
  • Vue 3 表单处理精讲:打造响应式注册表单的艺术
  • 浅谈Guava Cache的参数使用
  • 交通流预测 | Matlab基于KNN-BiLSTM的交通流预测(对比SVR、LSTM、GRU、KNN-LSTM)
  • 云卷云舒:面向业务的智能运维(上)
  • centos 7.4 docker