d3js学习——8.绘制折线图

svg 中的原点(0,0)位于左上角。

绘制折线图的步骤:

1.先写一个svg标签,定好宽和高

<svg id="chart" width="400" height="300"></svg>

2.定义一个数据集合和一些变量

const data = [
    { year: 2010, value: 10 },
    { year: 2011, value: 20 },
    { year: 2012, value: 15 },
    { year: 2013, value: 25 },
    { year: 2014, value: 30 },
    { year: 2015, value: 18 },
];
const svg = d3.select("#chart"),
    margin = { top: 20, right: 20, bottom: 30, left: 50 },
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom,

3.添加一个群组,并加入transform属性

const g = svg
  .append("g")
  .attr(
    "transform",
    "translate(" + margin.left + "," + margin.top + ")"
  );

4.设置x,y的比例尺

d3.scaleLinear()是线性比例尺

d3.scaleTime()是时间比例尺,刻度可以是年、月、日

.domain()是刻度数值的范围

range()是刻度位置的范围

const x = d3
    .scaleTime()
    .domain(
      d3.extent(data, function (d) {
        return new Date(d.year);
      })
    )
    .range([0, width]);

const y = d3
    .scaleLinear()
    .domain([
      0,
      d3.max(data, function (d) {
        return d.value;
      }),
    ])
    .range([height, 0]);

5.创建折线生成器

.line().x()是x轴

.line().y()是y轴

const line = d3
    .line()
    .x(function (d) {
      return x(d.year);
    })
    .y(function (d) {
      return y(d.value);
    });

6.绘制x轴和y轴

.ticks(num)是指定刻度的数量

.tickFormat(d2.format("d"))是格式化刻度为整数

g.append("g")
    .attr("transform", "translate(0," + height + ")")
  .call(d3.axisBottom(x).ticks(data.length).tickFormat(d3.format("d")))
    .append("text")
    .attr("fill", "black")
    .attr("x", width + 10)
    .attr("y", -10)
    .attr("text-anchor", "end")
    .text("年份"); // x轴的名称

  g.append("g")
    .call(d3.axisLeft(y))
    .append("text")
    .attr("fill", "#000")
    .attr("transform", "rotate(-90)")
    .attr("y", 6)
    .attr("dy", "0.71em")
    .attr("text-anchor", "end")
    .text("值"); // y轴的名称

7.绘制折线——添加一些样式信息

 g.append("path")
    .datum(data)
    .attr("fill", "none")
    .attr("stroke", "steelblue")
    .attr("stroke-linejoin", "round")
    .attr("stroke-linecap", "round")
    .attr("stroke-width", 1.5)
    .attr("d", line);

完整代码

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>D3.js 折线图示例</title>
    <script src="https://d3js.org/d3.v6.min.js"></script>
  </head>
  <body>
    <svg id="chart" width="400" height="300"></svg>

    <script>
      // 数据集
      const data = [
        { year: 2010, value: 10 },
        { year: 2011, value: 20 },
        { year: 2012, value: 15 },
        { year: 2013, value: 25 },
        { year: 2014, value: 30 },
        { year: 2015, value: 18 },
      ];
      // 设置SVG容器和边距
      const svg = d3.select("#chart"),
        margin = { top: 20, right: 20, bottom: 30, left: 50 },
        width = +svg.attr("width") - margin.left - margin.right,
        height = +svg.attr("height") - margin.top - margin.bottom,
        g = svg
          .append("g")
          .attr(
            "transform",
            "translate(" + margin.left + "," + margin.top + ")"
          );

      // 设置x和y比例尺
      const x = d3
        .scaleTime()
        .domain(
          d3.extent(data, function (d) {
            return new Date(d.year);
          })
        )
        .range([0, width]);

      const y = d3
        .scaleLinear()
        .domain([
          0,
          d3.max(data, function (d) {
            return d.value;
          }),
        ])
        .range([height, 0]);

      // 创建折线生成器
      const line = d3
        .line()
        .x(function (d) {
          return x(d.year);
        })
        .y(function (d) {
          return y(d.value);
        });

      // 绘制x轴和y轴
      g.append("g")
        .attr("transform", "translate(0," + height + ")")
        .call(d3.axisBottom(x).ticks(data.length).tickFormat(d3.format("d")))
        .append("text")
        .attr("fill", "black")
        .attr("x", width + 10)
        .attr("y", -10)
        .attr("text-anchor", "end")
        .text("年份");

      g.append("g")
        .call(d3.axisLeft(y))
        .append("text")
        .attr("fill", "#000")
        .attr("transform", "rotate(-90)")
        .attr("y", 6)
        .attr("dy", "0.71em")
        .attr("text-anchor", "end")
        .text("值");

      // 绘制折线
      g.append("path")
        .datum(data)
        .attr("fill", "none")
        .attr("stroke", "steelblue")
        .attr("stroke-linejoin", "round")
        .attr("stroke-linecap", "round")
        .attr("stroke-width", 1.5)
        .attr("d", line);
    </script>
  </body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值