前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >d3成神之路(六):散点图

d3成神之路(六):散点图

作者头像
拿我格子衫来
发布2022-01-24 15:02:08
5760
发布2022-01-24 15:02:08
举报
文章被收录于专栏:TopFETopFE
在这里插入图片描述
在这里插入图片描述

散点图主要还是使用 svg 的 path元素,设置path元素的 d属性即可。 x轴,y轴都是 线性轴,这个比较简单。

核心代码是这三句

代码语言:javascript
复制
  .attr('d', d => `M${xScale(d.xValue)}, ${yScale(d.yValue)}h0`)
  .attr("stroke-linecap", "round")
  .attr("stroke-width", 20)

stroke-linecap 用于设置 开放路径的描边 值有这么几种 butt | round | square | inherit

代码语言:javascript
复制
<?xml version="1.0"?>
<svg width="120" height="120"
     viewPort="0 0 120 120" version="1.1"
     xmlns="http://www.w3.org/2000/svg">

    <line stroke-linecap="butt"
          x1="30" y1="30" x2="30" y2="90"
          stroke="black" stroke-width="20"/>

    <line stroke-linecap="round"
          x1="60" y1="30" x2="60" y2="90"
          stroke="black" stroke-width="20"/>

    <line stroke-linecap="square"
          x1="90" y1="30" x2="90" y2="90"
          stroke="black" stroke-width="20"/>

    <path d="M30,30 L30,90 M60,30 L60,90 M90,30 L90,90"
          stroke="white" />
</svg>

对应的效果

在这里插入图片描述
在这里插入图片描述

stroke-width 属性可以用于控制 点的大小。

代码语言:javascript
复制
<!DOCTYPE html>
<html>

<head>
  <title>基础散点图</title>
</head>

<body>
  <svg id="main" width="800" height="800"></svg>


  <script src="https://cdn.jsdelivr.net/npm/d3@7.0.1/dist/d3.min.js"></script>
  <script>
    const originData = [
      [10.0, 8.04],
      [8.07, 6.95],
      [13.0, 7.58],
      [9.05, 8.81],
      [11.0, 8.33],
      [14.0, 7.66],
      [13.4, 6.81],
      [10.0, 6.33],
      [14.0, 8.96],
      [12.5, 6.82],
      [9.15, 7.20],
      [11.5, 7.20],
      [3.03, 4.23],
      [12.2, 7.83],
      [2.02, 4.47],
      [1.05, 3.33],
      [4.05, 4.96],
      [6.03, 7.24],
      [12.0, 6.26],
      [12.0, 8.84],
      [7.08, 5.82],
      [5.02, 5.68]
    ]

    const data = originData.map(x => {
      return { xValue: x[0], yValue: x[1] }
    })

    const svg = d3.select("#main")
    const width = svg.attr('width')
    const height = svg.attr('height')
    const margin = { top: 60, right: 30, bottom: 60, left: 200 }
    const innerHeight = height - margin.top - margin.bottom
    const innerWidth = width - margin.left - margin.right

    const g = svg.append('g').attr('id', 'maingroup')
      .attr('transform', `translate(${margin.left}, ${margin.top})`)

    const xScale = d3.scaleLinear().domain([0, d3.max(data, x => x.xValue)])
      .range([0, innerWidth]).nice()

    const yScale = d3.scaleLinear()
      .domain([0, d3.max(data, x => x.yValue)])
      .range([innerHeight, 0]).nice()

    g.selectAll('.scatter').data(data)
      .join('path')
      .attr('class', 'scatter')
      .attr('d', d => `M${xScale(d.xValue)}, ${yScale(d.yValue)}h0`)
      .attr("stroke", "steelblue")
      .attr("stroke-linecap", "round")
      .attr("stroke-width", 20)
      .attr('fill', '#5470c6')


    const yAxis = d3.axisLeft(yScale)
    const xAxis = d3.axisBottom(xScale)

    g.append('g').call(yAxis)
    g.append('g').call(xAxis).attr('transform', `translate(0, ${innerHeight})`)

    d3.selectAll('.tick text').attr('font-size', '2em')
    g.append('text').text('基础散点图').attr('font-size', '3em')
      .attr('x', innerWidth / 2 - 200).attr('y', -10)



  </script>
</body>

</html>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档