d3.js环形统计图代码

(function(){

    module.exports = function(weekNormal,weekHeight,weekLow,monthNormal,monthHeight,monthLow){

        //周

        var weekNormaldataset = [];

        var weekHeightdataset = [];

        var weekLowdataset = [];

        weekNormaldataset[0] = weekNormal;

        weekNormaldataset[1] = 100 - weekNormaldataset[0];

        weekHeightdataset[0] = weekHeight;

        weekHeightdataset[1] = 100 - weekHeightdataset[0];

        weekLowdataset[0] = weekLow;

        weekLowdataset[1] = 100 - weekLowdataset[0];

        //月

        var monthNormaldataset = [];

        var monthHeightdataset = [];

        var monthLowdataset = [];

        monthNormaldataset[0] = monthNormal;

        monthNormaldataset[1] = 100 - monthNormaldataset[0];

        monthHeightdataset[0] = monthHeight;

        monthHeightdataset[1] = 100 - monthHeightdataset[0];

        monthLowdataset[0] = monthLow;

        monthLowdataset[1] = 100 - monthLowdataset[0];

        var width = 100;

        var height = 100;

        //周

        var svg = d3.select(".oneWeekNormal")

            .append("svg")

            .attr("width", width)

            .attr("height", height);

        var svg1 = d3.select(".oneWeekHeight")

            .append("svg")

            .attr("width", width)

            .attr("height", height);

        var svg2 = d3.select(".oneWeekLow")

            .append("svg")

            .attr("width", width)

            .attr("height", height);

        //月

        var svg3 = d3.select(".oneMonthNormal")

            .append("svg")

            .attr("width", width)

            .attr("height", height);

        var svg4 = d3.select(".oneMonthHeight")

            .append("svg")

            .attr("width", width)

            .attr("height", height);

        var svg5 = d3.select(".oneMonthLow")

            .append("svg")

            .attr("width", width)

            .attr("height", height);

        var pie = d3.layout.pie();

        //周

        var weekNormalpiedata = pie(weekNormaldataset);

        var weekHeightpiedata = pie(weekHeightdataset);

        var weekLowpiedata = pie(weekLowdataset);

        //月

        var monthNormalpiedata = pie(monthNormaldataset);

        var monthHeightpiedata = pie(monthHeightdataset);

        var monthLowpiedata = pie(monthLowdataset);

        var outerRadius = 50; //外半径

        var innerRadius = 40; //内半径,为0则中间没有空白


        var arc = d3.svg.arc() //弧生成器

            .innerRadius(innerRadius) //设置内半径

            .outerRadius(outerRadius); //设置外半径


        var normalColor = ['#1BBD8F', '#E8EBED'];

        var heightColor = ['#FF455B', '#E8EBED'];

        var lowColor = ['#FF970C', '#E8EBED'];


        //周

        var arcs = svg.selectAll("g")

            .data(weekNormalpiedata)

            .enter()

            .append("g")

            .attr("transform","translate("+ (width/2) +","+ (width/2) +")");

        arcs.append("path")

            .attr('stroke-linecap', 'round')

            .attr("fill",function(d,i){

                return normalColor[i];

            })

            .attr("d",function(d){

                return arc(d);

            });

        var arcs1 = svg1.selectAll("g")

            .data(weekHeightpiedata)

            .enter()

            .append("g")

            .attr("transform","translate("+ (width/2) +","+ (width/2) +")");


        arcs1.append("path")

            .attr('stroke-linecap', 'round')

            .attr("fill",function(d,i){

                return heightColor[i];

            })

            .attr("d",function(d){

                return arc(d);

            });


        var arcs2 = svg2.selectAll("g")

            .data(weekLowpiedata)

            .enter()

            .append("g")

            .attr("transform","translate("+ (width/2) +","+ (width/2) +")");


        arcs2.append("path")

            .attr('stroke-linecap', 'round')

            .attr("fill",function(d,i){

                return lowColor[i];

            })

            .attr("d",function(d){

                return arc(d);

            });

        //月

        var arcs3 = svg3.selectAll("g")

            .data(monthNormalpiedata)

            .enter()

            .append("g")

            .attr("transform","translate("+ (width/2) +","+ (width/2) +")");


        arcs3.append("path")

            .attr('stroke-linecap', 'round')

            .attr("fill",function(d,i){

                return normalColor[i];

            })

            .attr("d",function(d){

                return arc(d);

            });

        console.log(arcs3);

        var arcs4 = svg4.selectAll("g")

            .data(monthHeightpiedata)

            .enter()

            .append("g")

            .attr("transform","translate("+ (width/2) +","+ (width/2) +")");


        arcs4.append("path")

            .attr('stroke-linecap', 'round')

            .attr("fill",function(d,i){

                return heightColor[i];

            })

            .attr("d",function(d){

                return arc(d);

            });

        var arcs5 = svg5.selectAll("g")

            .data(monthLowpiedata)

            .enter()

            .append("g")

            .attr("transform","translate("+ (width/2) +","+ (width/2) +")");


        arcs5.append("path")

            .attr('stroke-linecap', 'round')

            .attr("fill",function(d,i){

                return lowColor[i];

            })

            .attr("d",function(d){

                return arc(d);

            });

        //周

        arcs.append("text")

            .attr("text-anchor","middle")

            .text(function(d){

                return weekNormaldataset[0]+"%";

            });

        arcs1.append("text")

            .attr("text-anchor","middle")

            .text(function(d){

                return weekHeightdataset[0]+"%";

            });

        arcs2.append("text")

            .attr("text-anchor","middle")

            .text(function(d){

                return weekLowdataset[0]+"%";

            });

        //月

        arcs3.append("text")

            .attr("text-anchor","middle")

            .text(function(d){

                return monthNormaldataset[0]+"%";

            });

        arcs4.append("text")

            .attr("text-anchor","middle")

            .text(function(d){

                return monthHeightdataset[0]+"%";

            });

        arcs5.append("text")

            .attr("text-anchor","middle")

            .text(function(d){

                return monthLowdataset[0]+"%";

            });

    };

}).call(this);

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,062评论 0 2
  • 1. tab列表折叠效果 html: 能源系统事业部 岗位名称: 工作地点 岗位名...
    lilyping阅读 1,923评论 0 1
  • 以下是常用的代码收集,学习用。转自豪情博客园 1. PC - js 返回指定范围的随机数(m-n之间)的公式 re...
    自由加咖啡阅读 1,029评论 0 1
  • 动态效果图: 细看上面的动态效果图,可以发现: 一个值变换到一个新的值时,是一个渐变的过程; 圆弧末尾有一个竖线,...
    Evelynzzz阅读 6,764评论 2 5
  • “历史”总是有着惊人的相似。 当坐在我旁边的姑娘拿着我的摘抄本看,并时不时问一些诸如“这都是在哪抄的呀?”“为什么...
    雨炼阅读 810评论 2 2