d3js学习——7.绘制图形

条形图

使用d3js绘制条形图的步骤:

1.条形图的样式

svg rect {
	fill: gray;
}
svg text {
    fill: yellow;
    font: 12px sans-serif;
    /* 将文本定位到条的右端 */
    text-anchor: end;
}

2.设置变量值

const data = [10, 20, 23, 14];
//定义 svg的宽度、缩放到屏幕上可见的像素值、水平条的静态高度
const width = 500,
	scaleFactor = 20,
	barHeight = 30;

3.添加svg元素并设置宽高

const svg = d3
    .select("body")
    .append("svg")
    .attr("width", width)
    .attr("height", barHeight * data.length);

4.创建组元素,并设置条形图排列的样式

 const bar = svg
    .selectAll("g")
    .data(data)
    .enter()
    .append("g")
 // 上下间隔分开
    .attr("transform", function (d, i) {
      return "translate(0," + i * barHeight + ")";
    });

5.添加rect元素

bar.append("rect")
    .attr("width", function (d) {
      return d * scaleFactor;
    })
    .attr("height", barHeight - 1);

6.添加数据(文本)

bar.append("text")
    .attr("x", function (d) {
      return d * scaleFactor;
    })
    .attr("y", barHeight / 2)
    .attr("dy", ".35em") // 偏移,用于垂直对齐文本
    .text(function (d) {
      return d;
    });

完整代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="https://d3js.org/d3.v4.min.js"></script>
    <title>条形图</title>
    <style>
      svg rect {
        fill: gray;
      }
      svg text {
        fill: yellow;
        font: 12px sans-serif;
        /* 将文本定位到条的右端 */
        text-anchor: end;
      }
    </style>
  </head>
  <body>
    <script>
      const data = [10, 20, 23, 14];
      //   定义 svg的宽度、缩放到屏幕上可见的像素值、水平条的静态高度
      const width = 500,
        scaleFactor = 20,
        barHeight = 30;
      const svg = d3
        .select("body")
        .append("svg")
        .attr("width", width)
        .attr("height", barHeight * data.length);
      const bar = svg
        .selectAll("g")
        .data(data)
        .enter()
        .append("g")
        .attr("transform", function (d, i) {
          return "translate(0," + i * barHeight + ")";
        });
      bar
        .append("rect")
        .attr("width", function (d) {
          return d * scaleFactor;
        })
        .attr("height", barHeight - 1);
      bar
        .append("text")
        .attr("x", function (d) {
          return d * scaleFactor;
        })
        .attr("y", barHeight / 2)
        .attr("dy", ".35em")
        .text(function (d) {
          return d;
        });
    </script>
  </body>
</html>

圆图

使用d3js绘制圆图的步骤:

1.添加变量

const width = 400; // svg宽度
const height = 400; // svg高度
const data = [10, 20, 30]; // 数据元素数组
const colors = ["green", "purple", "red"]; // 对应块的颜色

2.添加 svg 元素

const svg = d3
    .select("body")
    .append("svg")
    .attr("width", width)
    .attr("height", height);

3.添加群组,进行转换

const group = svg
    .selectAll("g") // 创建用于保持圆圈的组元素
    .data(data) // 将数据绑定到组元素
    .enter() // 为组元素创建占位符
    .append("g") // 添加组元素
    .attr("transform", function (d, i) {
      return "translate(0,0)";  // 相对于原点定位元素
    });

4.添加圆形元素

group
    .append("circle")
    .attr("cx", function (d, i) {  // 每个圆的中心x坐标
      return i * 75 + 50;
    })
    .attr("cy", function (d, i) {  // 每个圆的中心y坐标
      return 75;
    })
    .attr("r", function (d) {   // 每个圆的半径
      return d * 1.5;
    })
    .attr("fill", function(d,i){  // 填充相应颜色
            return colors[i]
     });  

5.显示数据

group
    .append("text")
    .attr("x", function (d, i) { // 坐标
      return i * 75 + 25;
    })
    .attr("y", 30)
    .attr("stroke", "teal") // 颜色
    .attr("font-size", "10px")
    .attr("font-family", "sans-serif")
    .text(function (d) { // 内容
      return d;
    });

完整代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="https://d3js.org/d3.v4.min.js"></script>
    <title>圆形</title>
    </style>
  </head>
  <body>
    <script>
      const width = 400; // svg宽度
      const height = 400; // svg高度
      const data = [10, 20, 30]; // 数据元素数组
      const colors = ["green", "purple", "red"]; // 对应块的颜色
      const svg = d3
        .select("body")
        .append("svg")
        .attr("width", width)
        .attr("height", height);
      const group = svg
        .selectAll("g")
        .data(data)
        .enter()
        .append("g")
        .attr("transform", function () {
          return "translate(0,0)";
        });
      group
        .append("circle")
        .attr("cx", function (d, i) {
          return i * 75 + 50;
        })
        .attr("cy", function (d, i) {
          return 75;
        })
        .attr("r", function (d) {
          return d * 1.5;
        })
        .attr("fill", function (d, i) {
          return colors[i];
        });

      group
        .append("text")
        .attr("x", function (d, i) {
          return i * 75 + 25;
        })
        .attr("y", 30)
        .attr("stroke", "teal")
        .attr("font-size", "10px")
        .attr("font-family", "sans-serif")
        .text(function (d) {
          return d;
        });
    </script>
  </body>
</html>

饼状图

需要用到的两种方法:

  • arc():生成一个弧,需要设置内半径和外半径,内半径为0是饼图,否则是圆环图
  • pie():生成饼图,从数据集中获取数据并计算饼图的每个楔形的起始角度和结束角度

使用d3js绘制饼图的:

1.定义变量

// 数据
const data = [
    { label: "苹果", value: 30 },
    { label: "香蕉", value: 20 },
    { label: "橙子", value: 50 },
    { label: "西瓜", value: 60 },
    { label: "榴莲", value: 40 },
];
// 宽度和高度
const width = 400;
const height = 400;
// 半径
const radius = Math.min(width, height) / 2;

2.创建SVG元素并设置宽高属性

const svg = d3
    .select("body")
    .append("svg")
    .attr("width", width)
    .attr("height", height);

3.创建饼图的布局

const pie = d3.pie().value(function(d){
    return d.value;
})

4.创建弧生成器

const arc = d3.arc()
	.outerRadius(radius-10)
	.innerRadius(0)

5.创建颜色比例尺

const color = d3.scaleOrdinal(d3.schemeCategory10)

6.创建扇形

const arcs = svg
    .selectAll("arc")
    .data(pie(data))
    .enter()
    .append("g")
    .attr("class", "arc")
    .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

7.绘制扇形路径

arcs
    .append("path")
    .attr("d", arc)
    .attr("fill", function (d, i) {
      return color(i);
    });

8.添加扇形每部分的标签

arcs
    .append("text")
    .attr("transform", function (d) {
      return "translate(" + arc.centroid(d) + ")";
    })
    .attr("text-anchor", "middle")
    .text(function (d) {
      return d.data.label;
    });

完整代码:

<!DOCTYPE html>
<html>
  <head>
    <style>
      .arc text {
        font: 12px arial;
        text-anchor: middle;
      }
      .arc path {
        stroke: #fff;
      }
      .title {
        fill: green;
        font-weight: italic;
      }
    </style>
    <script type="text/javascript" src="https://d3js.org/d3.v4.min.js"></script>
  </head>
  <body>
    <script>
      // 数据
      const data = [
        { label: "苹果", value: 30 },
        { label: "香蕉", value: 20 },
        { label: "橙子", value: 50 },
        { label: "西瓜", value: 60 },
        { label: "榴莲", value: 40 },
      ];

      // 宽度和高度
      const width = 400;
      const height = 400;

      // 半径
      const radius = Math.min(width, height) / 2;

      // 创建svg元素
      const svg = d3
        .select("body")
        .append("svg")
        .attr("width", width)
        .attr("height", height);

      // 创建饼图布局
      const pie = d3.pie().value(function (d) {
        return d.value;
      });

      // 创建弧生成器
      const arc = d3
        .arc()
        .outerRadius(radius - 10)
        .innerRadius(0);

      // 创建颜色比例尺
      const color = d3.scaleOrdinal(d3.schemeCategory10);

      // 创建扇形
      const arcs = svg
        .selectAll("arc")
        .data(pie(data))
        .enter()
        .append("g")
        .attr("class", "arc")
        .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

      // 绘制扇形路径
      arcs
        .append("path")
        .attr("d", arc)
        .attr("fill", function (d, i) {
          return color(i);
        });

      // 添加标签
      arcs
        .append("text")
        .attr("transform", function (d) {
          return "translate(" + arc.centroid(d) + ")";
        })
        .attr("text-anchor", "middle")
        .text(function (d) {
          return d.data.label;
        });
    </script>
  </body>
</html>

甜甜圈图

相对于饼状图,甜甜圈图只需要将第四步的弧的innerRadius()>0即可,其余代码一样

完整代码:

<!DOCTYPE html>
<html>
  <head>
    <style>
      .arc text {
        font: 12px arial;
        text-anchor: middle;
      }
      .arc path {
        stroke: #fff;
      }
      .title {
        fill: green;
        font-weight: italic;
      }
    </style>
    <script type="text/javascript" src="https://d3js.org/d3.v4.min.js"></script>
  </head>
  <body>
    <script>
      // 数据
      const data = [
        { label: "苹果", value: 30 },
        { label: "香蕉", value: 20 },
        { label: "橙子", value: 50 },
        { label: "西瓜", value: 60 },
        { label: "榴莲", value: 40 },
      ];

      // 宽度和高度
      const width = 400;
      const height = 400;

      // 半径
      const radius = Math.min(width, height) / 2;

      // 创建svg元素
      const svg = d3
        .select("body")
        .append("svg")
        .attr("width", width)
        .attr("height", height);

      // 创建饼图布局
      const pie = d3.pie().value(function (d) {
        return d.value;
      });

      // 创建弧生成器
      const arc = d3
        .arc()
        .outerRadius(radius - 10)
        .innerRadius(100);

      // 创建颜色比例尺
      const color = d3.scaleOrdinal(d3.schemeCategory10);

      // 创建扇形
      const arcs = svg
        .selectAll("arc")
        .data(pie(data))
        .enter()
        .append("g")
        .attr("class", "arc")
        .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

      // 绘制扇形路径
      arcs
        .append("path")
        .attr("d", arc)
        .attr("fill", function (d, i) {
          return color(i);
        });

      // 添加标签
      arcs
        .append("text")
        .attr("transform", function (d) {
          return "translate(" + arc.centroid(d) + ")";
        })
        .attr("text-anchor", "middle")
        .text(function (d) {
          return d.data.label;
        });
    </script>
  </body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值