当前位置: 首页 > Web前端 > HTML

Echarts实现3-4环图

时间:2023-03-28 18:29:35 HTML

3/4环图实现通过循环数据,将每条数据放入一个环图中,每个环图分为3个空间:实际数据占用的空间和这里占用的空间需要换算成3/4的比例:value*0.75这里虚拟3/4环图的剩余数据是除实际数据外占用3/4空间后的剩余空间:(总数据-value)*0.751/4需要做成透明数据总是占据1/4的空间:Total*0.25,颜色设置为透明,透明色用于在y轴上显示下一个标注。注意x轴也要写!xAxis:[{show:false}],yAxis:[{type:"category",inverse:true,axisLine:{show:false,},axisTick:{show:false,},axisLabel:{},数据:lineYAxis,},],效果图优点:通过这一系列的计算,保证环图在数据量过大时不会溢出。缺点:环形图数据项不能太多,因为内圈的最小值为0%。放大的时候放大基本没问题,缩小的时候会出现数据标注不匹配的问题。因为这里是网格定位,y轴标签用来显示数据,考虑给网格设置一个高度,但是这个高度要随着环图的半径变化,环缩放时,resize方法是根据box给定的最小宽度或者高度来设置尺寸,保持为一个圆。所以不知道怎么取这个高度为最小值。。。希望各位读者解决这个问题评论补充!js实现代码:exportdefault{data(){return{ectOpts:{},error:"",loading:false,latest:true,data:[],};},computed:{total(){lettotal=this.data.reduce((item1,item2)=>{return(typeofitem1==="number"?item1:item1.num)+item2.num;});返回总计;},},created(){this.getData();},mounted(){this.initEchart();},methods:{getData(){this.data=[{name:"Walletpayment",num:13210},{name:"CashPayment",num:42111},{name:"BillPayment",num:81711},{name:"移动支付",num:121700},];this.data.forEach((item)=>{item.percent=((item.num/this.total)*100).toFixed(1);});},initEchart(){letcolor=["rgba(255,135,0,1)","rgba(255,195,0,1)","rgba(0,228,115,1)","rgba(0,157,255,1)",].reverse();让pieSeries=[];让lineYAxis=[];this.data.forEach((item,index)=>{letiname=item.name;letinum=item.num;pieSeries.push({name:iname,type:"pie",radius:[65-15*index+"%",57-15*index+"%"],//关闭悬停动画hoverAnimation:false,//是否顺时针旋转clockwise:false,center:["35%","50%"],label:{show:false,},data:[{//只为3/4环图设置3/4的值value:inum*0.75,name:iname,tooltip:{formatter:(item)=>{return`

${item.seriesName}:${item.value}辆
Proportion:${(item.percent/0.75).toFixed(1)+"%"}
`},}},{//这里的值为+上面的值是总和,但只占75%value:(this.total-inum)*0.75,itemStyle:{color:"rgba(0,132,251,0.2)",},tooltip:{show:false}},{//放弃25%环图不显示value:this.total*0.25,itemStyle:{color:"rgba(0,0,0,0)",},tooltip:{show:错误的}},],});lineYAxis.push({value:index,textStyle:{rich:{circle:{color:color[index],padding:[0,5],},},},});});this.ectOpts={tooltip:{trigger:"item",},color,grid:{top:"15%",bottom:"54%",left:"35%",containLabel:false,},//复制代码有yAxis必须有xAxisxAxis:[{show:false}],yAxis:[{type:"category",//反向坐标轴inverse:true,axisLine:{show:false,},axisTick:{show:false,},axisLabel:{格式化程序:(params)=>{letitem=this.data[params];return`{line|------------}{circle|●}{name|${item.name}:}{value|${item.num}辆,}{percent|占${item.percent}%}`;},interval:0,inside:true,rich:{line:{//width:50,//height:1,color:"rgba(236,236,236,1)",//当前echart版本不支持borderType,使用------代替//borderType:"dashed",//borderColor:"#D9DFEB",//borderWidth:1,},},textStyle:{颜色:"#fff",fontSize:12,},显示:true,},数据:lineYAxis,},],系列:pieSeries,};},},};