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

2022-01-24 15:02:08 浏览数 (1)

散点图主要还是使用 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>

0 人点赞