前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >D3.js学习笔记【暂完,待续】

D3.js学习笔记【暂完,待续】

作者头像
无道
发布2019-11-13 15:53:09
5.3K0
发布2019-11-13 15:53:09
举报
文章被收录于专栏:无道编程无道编程

起步

select

style

代码语言:javascript
复制
d3.select("#container")
.append('p')
.text("hello")
.style("color","red");

SVG

原生

一个简单的代码:

代码语言:javascript
复制
<svg width="100" height="100">
<circle r="50" cx="50" cy="50" stroke="yellow"
stroke-width="4"
fill="red"></circle>
</svg>

上面的代码是通过原生的生成的,效果如下:

截图-1557833823
截图-1557833823

D3.js

代码语言:javascript
复制
const canvas = d3.select("#container");
// add an svg

const svg = canvas.append("svg");
svg.attr("width", 100)
    .attr("height", 100);

const circle = svg.append("circle")
.attr("cx",50)
.attr("cy",50)
.attr("r",50)
.attr("fill","blue")

circle

代码语言:javascript
复制
// circle
svg.append("circle")
.attr("cx",50)
.attr("cy",50)
.attr("r",50)
.attr("fill","blue")

rect

代码语言:javascript
复制
// rect
svg.append("rect")
.attr("x",50)
.attr("rx",50)
.attr("y",10)
.attr("ry",10)
.attr("width",10)
.attr("height",100)
.attr("fill","red")

line

代码语言:javascript
复制
// line
svg.append("line")
.attr("x1","129")
.attr("x2","45")
.attr("y1","300")
.attr("y2","100")
.attr("stroke","gray")

text

代码语言:javascript
复制
// text
svg.append("text")
.attr("x",30)
.attr("y",100)
.attr("font-size",20)
.attr("file","gray")
.text("hello")

遍历数据

代码语言:javascript
复制
let dataArr = [4, 15, 34];
// add an svg element
const svg = canvas.append("svg");
svg.attr("width", 600)
.attr("height", 600);
const rect = svg.append("rect");
rect.attr("width", "24")
.data(dataArr)
.attr("height", "100")
.attr("fill", "gray")
.attr("x", function (d, i) { return d * 5; })
.attr("y", function (d, i) { return d * 10; })
// what are d and i, the d mean data, and the i mean index

我们来画一个柱状图

代码语言:javascript
复制
let dataArr = [3, 10, 48, 31];
// add an svg element
const svg = canvas.append("svg")
    .attr("width", 600)
    .attr("height", 600);
const rect = svg.selectAll("rect");
// 这里必须是selectAll,不然会出错的。
rect.data(dataArr)
    .enter()
    .append("rect")
    .attr("fill", "orange")
    .attr("x", function (d, i) {
    console.log(d)
    return i * 25;
    })
    .attr("width", "20")
    .attr("y", function (d, i) {
    return 100 - (d * 3);
    })
    .attr("height", function (d, i) {
    return d * 3;
    })
console.log(rect)
截图-1557837782
截图-1557837782

d3 - layouts

Hierarchical layouts

使用的数据:

代码语言:javascript
复制
let data = {
        "name": "A1",
        "children": [
            {
                "name": "B1",
                "children": [
                    {
                        "name": "C1",
                        "value": 100
                    },
                    {
                        "name": "C2",
                        "value": 300
                    },
                    {
                        "name": "C3",
                        "value": 200
                    }
                ]
            },
            {
                "name": "B2",
                "value": 200
            }
        ]
    };

代码1

代码语言:javascript
复制
<div id="content">
    <svg width="400" height="220">
        <g transform="translate(5, 5)">
            <g class="links"></g>
            <g class="nodes"></g>
        </g>
    </svg>
</div>

返回一个扁平的数组来表达root的子孙后代,而root.links()则返回一个扁平的对象数组来表达所有的父子links

代码2

代码语言:javascript
复制
<script>

    let data = {
        "name": "A1",
        "children": [
            {
                "name": "B1",
                "children": [
                    {
                        "name": "C1",
                        "value": 100
                    },
                    {
                        "name": "C2",
                        "value": 300
                    },
                    {
                        "name": "C3",
                        "value": 200
                    }
                ]
            },
            {
                "name": "B2",
                "value": 200
            }
        ]
    };
    /* 数据是:A1 底下 有 B1 和 B2 ,B1 底下 有 C1 C2 C3*/
    let root = d3.hierarchy(data);
    console.log(root);
    let leaves = root.leaves();
    console.log(leaves);
    let links = root.links();
    console.log(links);
    let path = root.path(root.children[0].children[1]);
    console.log(path);
    let descendants = root.descendants();
    console.log("descendants: ", descendants);
    // 返回一个扁平的数组来表达root的子孙后代,而root.links()则返回一个扁平的对象数组来表达所有的父子links
    let treeLayout = d3.tree();
    // 使用size设置
    treeLayout.size([400, 200]);
    //随后我们可以调用treeLayout函数,传入我们的hierarchy object root:
    treeLayout(root);
    // 这个函数执行的结果是会将root的每一个node都增加上x和y的value

    // let svg = d3.select("#content")
    //     .append("svg")
    //     .attr("width", "100%")
    //     .attr("height", "100%");


    d3.select('svg g.nodes')
        .selectAll('circle.node')
        .data(root.descendants())
        .enter()
        .append('circle')
        .classed('node', true)
        .attr('cx', function (d) {
            return d.x;
        })
        .attr('cy', function (d) {
            return d.y;
        })
        .attr('r', 4);

    // Links
    d3.select('svg g.links')
        .selectAll('line.link')
        .data(root.links())
        .enter()
        .append('line')
        .classed('link', true)
        .attr('x1', function (d) {
            return d.source.x;
        })
        .attr('y1', function (d) {
            return d.source.y;
        })
        .attr('x2', function (d) {
            return d.target.x;
        })
        .attr('y2', function (d) {
            return d.target.y;
        });


</script>

scales

代码语言:javascript
复制
var myScale = d3.scaleLinear()
  .domain([0, 10])
  .range([0, 600]);

d3将创建一个myScale函数用于接收[0,10]之间的数据输入(domain)映射为[0,600]像素的位置数据(range)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-07-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 起步
  • SVG
    • 原生
      • D3.js
        • circle
          • rect
            • line
              • text
              • 遍历数据
              • d3 - layouts
                • Hierarchical layouts
                • scales
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档