vue项目基于D3js的3D饼图实现(四)

横向图例翻页的实现,效果图如下

一、修改Pie3D.vue文件,新增config配置 

二、修改pie.js文件中的翻页图例配置

if(config.legendType === 'scroll'){
                legendSvg.attr('width',700 - 80);
                    //计算每一个图例的距离
                    let list = []
                    let left = 0
                    legend.each(function(){
                        left += d3.select(this).node().getBBox().width + 10
                        list.push(left)
                    })
                    legend.attr('transform',function(d, i){
                        if(i === 0){
                            return 'translate(0,0)'
                        }else {
                            return `translate(${list[i - 1]},0)`
                        }
                    })
                    //图例宽度大于盒子宽度
                    if(legendSvg.node().getBBox().width > (700 - 80)){
                        //获取总页数
                        let legendNumLength = 0
                        let totalPage = 1 //总页数
                        let curPage = 1 //翻页定义当前页数
                        let transitionX = 0 //需要移动图例整体所占的距离
                        let lineList = [] //如果是普通图例判断一行显示多少个
                        legend.each(function(d,i){
                            legendNumLength += d3.select(this).node().getBBox().width + 10
                            if(legendNumLength > (700 - 80 - (d3.select(this).node().getBBox().width + 10))){
                                legendNumLength = 0
                                //避免移动到最后一页多加一夜
                                if(data.length - 1 !== i){
                                    lineList.push(i)
                                    totalPage++
                                }
                            }
                        })
                        //定义横向翻页的svg
                        const legendPageSvg = d3.select(id)
                            .append('svg')
                            .attr('width',100)
                            .attr('height',20)
                            .style('position','absolute')
                            .style('top',400 - 30)
                            .style('left',700 - 80);
                        let sym = d3.symbol().type(d3.symbolTriangle).size(100);

                        const pageBox = legendPageSvg.append('g')
                            .attr('id','legend_page')
                            .attr('transform','translate(15,8)');

                            //定义左侧翻页符号
                        pageBox.append('path')
                            .attr('d',sym)
                            .attr('fill','green')
                            .attr('transform','rotate(30)')
                            .style('cursor','pointer')
                            .on('click',function(){
                                if(curPage > 1){
                                    let legendNumLength = 0
                                    let curLength = 0
                                    legend.each(function(d,i){
                                        legendNumLength += d3.select(this).node().getBBox().width + 10
                                        if(legendNumLength <= 700 - 80){
                                            curLength = legendNumLength
                                        }
                                    })
                                    transitionX -= curLength
                                    legendBox.transition()
                                        .duration(500)
                                        .attr('transform',`translate(-${transitionX}, 0)`);
                                    curPage--
                                    legendText.text(`${curPage}/${totalPage}`)
                                }
                            });
                        //页码
                        const legendText = pageBox.append('text')
                            .text(`${curPage}/${totalPage}`)
                            .attr('fill','red')
                            .attr('font-size',12)
                            .attr('transform','translate(10,5)');
                        //右侧翻页按钮
                        pageBox.append('path')
                            .attr('d',sym)
                            .attr('fill','green')
                            .attr('transform','translate(40,0) rotate(-30)')
                            .style('cursor','pointer')
                            .on('click',function(){
                                if(curPage < totalPage){
                                    let legendNumLength = 0
                                    let curLength = 0
                                    legend.each(function(d,i){
                                        legendNumLength += d3.select(this).node().getBBox().width + 10
                                        if(legendNumLength <= 700 - 80){
                                            curLength = legendNumLength
                                        }
                                    });
                                    transitionX += curLength
                                    legendBox.transition()
                                        .duration(500)
                                        .attr('transform',`translate(-${transitionX}, 0)`);
                                    curPage++
                                    legendText.text(`${curPage}/${totalPage}`)
                                }
                            });
                    }
            }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值