D3.JS中常用的绘制图形的的API

D3.JS中常用的绘制图形的的API

其实SVG 提供的图形标签(圆形、矩形、线条、路径等)其实只是构成图形的基本元素,一张完整图表则是由几百个这些小元件组成的,如果只使用 SVG 提供的图形,就需要很辛苦地一个一个建立图形,才能完成一张图表。为了省去这个麻烦,D3 创建了很多不同的 API 来协助建构复杂图形/图表,常用的 有如下几种。

  1. Generator 产生 标签 中的d属性
  2. Component:产生 DOM 元素
  3. Layout:产生整张图表
  • d3.line() 方法

    d3.line() :分别是 x 跟 y 值,d3.line().x().y() 这两个参数可以是数字或是方法,用来计算出路径的起始点坐标,绘制出线条;

    <body>
        <svg width="500" height="400" style="border: 1px solid red"  >
            <path 
                d="M50 20 C80 90,40 200,250,100" stroke="black"    // 以前的计算方式,比较繁琐
                fill="red"
                stroke-width="5"
            ></path>
          
        </svg>
    </body>
    </html>
    <script>
      // 使用d3.line()
      const list = [{ x: 20, y: 200 }, { x: 40, y: 300 }, { x: 70, y: 130 }, { x: 100, y: 190 }, { x: 20, y: 250 }]
        const line = d3.line()
            .x(d => d.x)
            .y(d => d.y)
        d3.select('svg')
            .append('path')
            .attr('d', line(list))
            .attr('stroke', 'black')
            .attr('stroke-width', '5')
            .attr('fill', 'red')
    </script>
    

111.png

  • d3.area( ) 绘制封闭的多边形图形

该方法有三个参数;area.x( ) 计算出X的坐标,area.y1( ) y轴做边界;area.y0( ) 开始绘制区域的y轴范围


<body>
    <svg width="500" height="400" style="border: 1px solid red">
        <!-- <path d="M50 20 C80 90,40 200,250,100" stroke="black" fill="red" stroke-width="5"></path> -->
    </svg>
</body>

</html>
<script>
    const list = [{ x: 20, y: 20 }, { x: 40, y: 30 }, { x: 70, y: 130 }, { x: 100, y: 190 }]
    const area = d3.area()
        .x(d => d.x)
        .y1(d => d.y)
        .y0(20)  // 决定绘制区域离原点坐标X轴多远
    d3.select('svg')
        .append('path')
        .attr('d', area(list))
        .attr('stroke', 'black')
        .attr('stroke-width', '5')
        .attr('fill', 'red')
</script>

222.png
以上方法一般用在折线图中比较方便

333.png

  • d3.arc( ) 用来绘制圆弧

    参数作用
    arc.innerRadius( )内弧范围
    arc.outerRadius( )外层弧范围
    arc.startAngle( )起始角度
    arc.endAngle( )结束角度
    • 弧形的中心点永远 [0 , 0],如果想要移动这个弧形就用 transform (x , y)操作
    • 弧形角度由 .startAngle( ) 跟.endAngle( ) 去控制,当start 跟 end 的角度加起来大于或等于 τ (也就是2π) 的时候,就会形成一个完整的圆型
    • .startAngle( ) 起始角度从指针12点钟方向开始,顺时针开始画弧
    • pie ( ) 这个API 会将数据处理成 arc( ) 需要的 start 跟 end 角度,所以 arc( ) 通常跟 pie( ) 搭配使用
<body>
    <svg width="500" height="400" style="border: 1px solid red">
        <!-- <path d="M50 20 C80 90,40 200,250,100" stroke="black" fill="red" stroke-width="5"></path> -->
    </svg>
</body>

</html>
<script>
    const arc = d3.arc()
        .innerRadius(140)  // 内弧半径
        .outerRadius(150)  // 外弧班级
        .startAngle(0)  // 开始角度  从0开始顺时针
        .endAngle(Math.PI * 0.5)   // Math.PI 表示一个圆的周长与直径的比例,约为 3.14159:2Π就是一整个圆

    d3.select('svg')
        .append("g")
        .attr("transform", "translate(200,250)")
        .append('path')
        .attr('d', arc())
        .attr('stroke', 'pink')
        .attr('fill', 'red')
</script>

444.png
D3.stack( ) 图形块的使用
stack( ) 这个 API 主要用来画条形图,需要搭配 scale( )、axis( ) 等API 才能完整的画出一个条形图。

<body>
    <svg width="500" height="400" style="border: 1px solid red">
        <!-- <path d="M50 20 C80 90,40 200,250,100" stroke="black" fill="red" stroke-width="5"></path> -->
    </svg>
</body>

</html>
<script>
    const list = [
        { name: 125, color: 300, age: 120 },
        { name: 165, color: 230, age: 300 },
        { name: 230, color: 303, age: 350 },
    ];
    const stack = d3.stack()
        .keys(['name', 'color', 'age'])
    const stackedSeries = stack(list)
    const colorScale = d3.scaleOrdinal()   // 设置颜色的比例尺
        .domain(['name', 'color', 'age'])
        .range(["red", "blue", "purple"])

    
    const g = d3.select('svg')
        .style("padding",'20')
        .selectAll('g')
        .data(stackedSeries)
        .enter()
        .append('g')
        .attr('fill', d => colorScale(d.key)) 

 
    g.selectAll('rect')
        .data(d => {
            console.log(d);   // 这里的d是取到的stackedSeries处理后的数组值
            return d
        })
        .join('rect')
        .attr('width', d => d[1] - d[0])    // 计算出叠加的每一个矩形宽度
        .attr('x', d => d[0])    // 计算出x的坐标
        .attr('y', (d, i) => i * 50)  // y坐标表示间距
        .attr('height', 20);  // 高度可以固定
</script>

在这里插入图片描述

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值