使用d3.v5实现饼状图

摘要:
呈现:饼图:目录结构:˂!

效果图:

饼状图:

使用d3.v5实现饼状图第1张

目录结构:

使用d3.v5实现饼状图第2张

使用d3.v5实现饼状图第3张使用d3.v5实现饼状图第4张
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link href="http://t.zoukankan.com/css/style.css" media="screen" rel="stylesheet" type="text/css"/>
    <title>Linechart1</title>
</head>
<body>
    <div id="container"></div>

    <script src="https://d3js.org/d3.v5.min.js"></script>
    <script src="http://t.zoukankan.com/js/index.js"></script>
</body>
</html>
index,html
使用d3.v5实现饼状图第3张使用d3.v5实现饼状图第6张
/*svg .path{*/
/*fill:#339999;*/
/*}*/

/*svg g path:hover {*/
/*cursor: pointer;*/
/*fill: #66cccc;*/
/*}*/
style.css
使用d3.v5实现饼状图第3张使用d3.v5实现饼状图第8张
d3.csv("Data/data.csv",function (d) {
    return {
        education:d.education,
        population:+d.population,
    }
}).then(data=>{
    console.log(data);
    var sum=d3.sum(data.map(function (d) {
        return d.population
    }))

    for(i in data){
        data[i].Percentage=(data[i].population/sum*100).toFixed(0)+"%";
    }
    console.log(data);

    var width=800,
        height=800,
        margin={"left":30,"top":30,"right":30,"bottom":30},
        svg_width=width+margin.left+margin.right,
        svg_height=height+margin.top+margin.bottom,
        font_size=15;

    var svg=d3.select("#container")
        .append("svg")
        .attr("width",width)
        .attr("height",height)


    var Pie=svg.append("g")
        .attr("transform","translate("+width/2+","+height/2+")")

    var arc_generator=d3.arc()
        .innerRadius(width/8)
        .outerRadius(width/4)
        // .startAngle(0)
        // .endAngle(120*Math.PI/180);

    var angle_data=d3.pie()
        .value(function (d) {
            return d.population;
        })
    console.log(angle_data(data));

    var color=d3.schemeCategory10;
    console.log(color)

    //生成内部圆环
    Pie.selectAll("path")
        .data(angle_data(data))
        .enter()
        .append("path")
        .attr("d",arc_generator)
        .style("fill",function (d,i) {
            return color[i];
        })
        .attr("class",".path")

    //标注
    var arc_label=d3.arc()
        .innerRadius(width/4)
        .outerRadius(width/2)

    Pie.selectAll(".arc_label")
        .data(angle_data(data))
        .enter()
        .append("path")
        .attr("d",arc_label)
        .attr("class",".arc_label")
        .style("fill","none")

    //画标注线
    function line_label(angle_data) {
        var str=""
        var i=-0;
        for (d in angle_data){
            str="M"+arc_generator.centroid(angle_data[d])[0]+","+arc_generator.centroid(angle_data[d])[1];
            str=str+"L"+arc_label.centroid(angle_data[d])[0]+","+arc_label.centroid(angle_data[d])[1]
            // console.log(str);
            Pie.append("path")
                .attr("d",str)
                .attr("stroke",color[i])
                .attr("stroke-width",2)
            i++;
            if(i>10)i=0;
        }
    }

    line_label(angle_data(data));

    var text=Pie.selectAll("text")
        .data(angle_data(data))
        .enter()
        .append("text")
        .attr("transform",function (d) {
            return "translate("+arc_label.centroid(d)+")"
        })
        .attr("text-anchor",function (d) {
            var x=arc_label.centroid(d)[0];
            return x<=0?"end":"start";
        })
        .attr("font-size",font_size)
        .style("fill",function (d,i) {
            return color[i];
        })
        .style("text-decoration","underline")
        .text(function (d) {
            return d.data.education+d.data.Percentage;
        })
})
index.js
使用d3.v5实现饼状图第3张使用d3.v5实现饼状图第10张
education,population
大专以及以上,11964
高中和中专,18799
初中,51966
小学,35876
文盲人口,5466
data.csv

 参考教程:https://www.imooc.com/learn/103

免责声明:文章转载自《使用d3.v5实现饼状图》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Python函数参数中的冒号与箭头Spring Security(15)——权限鉴定结构下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

mpvue 使用echarts动态绘制图表(数据改变重新渲染图表)

  最近在公司开发一款微信小程序,按照客户需求用饼状图显示当前设备状态(开机、故障、关机),于是就在网上寻找各种资料,找了很多mpvue使用关于echarts绘制图表,最终功夫不负有心人,找到一篇关于mpvue使用echarts的文章,链接点击这里,需要的同学自行查看。这里面说明了如何使用echarts绘制图表,其中echarts的配置项option大家可...

Vue生态圈之----Axios学习笔记

API 首先,我们把axios所有的【API】和【配置】及【返回内容】列一下: //通过配置发送请求 axios(config) axios(url[,config]) //别名 axios.request(config); axios.get(url[,config]); axios.delete(url[,config]); axios.head(u...

微信小程序基础学习笔记2:数据绑定相关

VUE语法 所有前端wxml中绑定的数据变量,都要定义在data中。 绑定事件,bindTap res ,取数据 currentTarget->dataset 这里的data-id中的data必须写,后面的id、title等可以任意自定义,H5的新属性,后面的id全部要用小写 myTap:function(res){ console.write }...

Vue与Django数据交互

首先配置路由信息,理论上都会添加二级路由:所以会有请求转发 1 from django.conf.urls import url,include 2 3 url(r'^api/(?P<version>w+)/',include("api.urls")), 此时请求会转发给二级路由:api.urls 1 url(r'^course/$'...

Golang: 解析JSON数据之一

JSON 作为目前最流行的数据传输格式, 相信每个程序员都跟它打过交道吧。使用 Go 语言时,也不可避免的要操作 JSON 数据,令人惊喜的是,Go 内置了序列化和反序列化 JSON 的功能,今天就来总结一下。 序列化是将结构对象转为 JSON 字符串,反序列化是将 JSON 字符串转为结构对象,它们分别对应 encoding/json 包下面的两个方法:...

[转]CURL常用命令

From:http://www.cnblogs.com/gbyukg/p/3326825.html 常用参数 -v/--verbose 小写的v参数,用于打印更多信息,包括发送的请求信息,这在调试脚本是特别有用。 -m/--max-time <seconds> 指定处理的最大时长 -H/--header <header> 指定请求头...