echarts环形图点击旋转并高亮

本文转载于:猿2048网站https://www.mk2048.com/blog/blog.php?id=0h0jaa&title=echarts%E7%8E%AF%E5%BD%A2%E5%9B%BE%E7%82%B9%E5%87%BB%E6%97%8B%E8%BD%AC%E5%B9%B6%E9%AB%98%E4%BA%AE

通过计算某个扇形区域的值占整个圆的百分比来得到这个扇形的角度,从而根据startAngle这个属性来设定图形的开始渲染的角度,使点击某个扇形时圆环旋转使之始终对准某个点。
期间考虑到某扇形区域太小点击不到,来设置最小扇形区域。
const myChartContainer = document.getElementById( 'chart' );
const myChart = echarts.init( myChartContainer );
let minAngle = 30;// 最小扇形区域为30
for ( let i = 0; i < obj.data.length; i++ ) { //某项数据为0时,最小扇形区域为0
 if ( obj.data[ i ].value === 0 ) {
  minAngle = 0;
  break;
 }
}
const pieValue = obj.data.map( v => { 
 return v.value;
} )
const sum = pieValue.reduce( ( prev, cur ) => {//数据值的总和
 return prev + cur;
}, 0 );

const sum2 = pieValue.reduce( ( prev, cur ) => {
 if ( cur < sum / 12 && cur > 0 ) {//某个值大于0小于总和的1/12即30时,按30计算和
  return prev + sum / 12;
 }
 return prev + cur;
}, 0 );
let initPieValue = pieValue[ 0 ];// 初始值
if ( initPieValue < sum / 12 && initPieValue > 0 ) {
 initPieValue = sum / 12;
}
const option = {
tooltip: {
 show: false,
 trigger: 'item',
 formatter: '{a} <br/>{b}: {c} ({d}%)'
},
legend: {
 show: false,
 orient: 'vertical',
 x: 'left'
},
color: [ '#44bbf8', '#93e588', '#ffd87b', '#f88071' ],
series: [
{
 name: '',
 type: 'pie',
 radius: [ '45%', '79%' ],
 clockWise: false,
 startAngle: 167 - ( initPieValue / sum2 * 360 / 2 ),
 minAngle: minAngle,
 avoidLabelOverlap: false,
itemStyle: {
 emphasis: {
  radius: [ '46%', '100%' ]
 }
},
label: {
 normal: {
  show: false,
  position: 'center'
 },
 emphasis: {
  show: false,
  textStyle: {
  fontSize: '30',
  fontWeight: 'bold'
  }
 }
},
labelLine: {
  normal: {
   show: false
  }
},
 data: obj.data
  }
 ]
};
myChart.setOption( option );
if ( minAngle === 30 ) {  //最小扇形区域30时
myChart.dispatchAction( { type: 'highlight', seriesIndex: 0, dataIndex: 0 } );
}

let preDataIndex = 0;
myChart.on( 'click', ( v ) => {
 if ( v.dataIndex === preDataIndex ) {
  myChart.dispatchAction( {
  type: 'highlight',
  seriesIndex: 0,
  dataIndex: v.dataIndex
 } );
 return;
}
const sum1 = pieValue.reduce( ( prev, cur, index ) => {
 if ( index < v.dataIndex ) {
  if ( cur < sum / 12 && cur > 0 ) {
   return prev + sum / 12; // 饼图的扇形最小角度设置为30,占圆的1/12
  }
  return prev + cur;
 }
 return prev;
}, 0 );
let curPieValue = pieValue[ v.dataIndex ];
if ( curPieValue < sum / 12 && curPieValue > 0 ) {
 curPieValue = sum / 12;
}
option.series[ 0 ].startAngle = 167 - ( sum1 / sum2 * 360 + curPieValue / sum2 * 360 / 2 );// 开始渲染图形的角度
myChart.setOption( option );
preDataIndex = v.dataIndex;
window.setTimeout( () => {
 myChart.dispatchAction( {
 type: 'highlight',
 seriesIndex: 0,
 dataIndex: v.dataIndex
 } );
}, 400 );

this.mrkName = v.data.name;
this.mrkValue = v.data.value;
} );

本文转载于:猿2048https://www.mk2048.com/blog/blog.php?id=0h0jaa&title=echarts%E7%8E%AF%E5%BD%A2%E5%9B%BE%E7%82%B9%E5%87%BB%E6%97%8B%E8%BD%AC%E5%B9%B6%E9%AB%98%E4%BA%AE

转载于:https://my.oschina.net/u/4181724/blog/3088912

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值