css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <script src="https://d3js.org/d3.v5.min.js"></script>
</body>

</html>
            
          
!
            
              const data = {
  name: '中国',
  children: [
    {
      name: '浙江',
      children: [
        { name: '杭州', value: 100 },
        { name: '宁波', value: 100 },
        { name: '温州', value: 100 },
        { name: '绍兴', value: 100 }
      ]
    },
    {
      name: '广西',
      children: [
        {
          name: '桂林',
          children: [
            { name: '秀峰区', value: 100 },
            { name: '叠彩区', value: 100 },
            { name: '象山区', value: 100 },
            { name: '七星区', value: 100 }
          ]
        },
        { name: '南宁', value: 100 },
        { name: '柳州', value: 100 },
        { name: '防城港', value: 100 }
      ]
    },
    {
      name: '黑龙江',
      children: [
        { name: '哈尔滨', value: 100 },
        { name: '齐齐哈尔', value: 100 },
        { name: '牡丹江', value: 100 },
        { name: '大庆', value: 100 }
      ]
    },
    {
      name: '新疆',
      children:
        [
          { name: '乌鲁木齐' },
          { name: '克拉玛依' },
          { name: '吐鲁番' },
          { name: '哈密' }
        ]
    }
  ]
}

const marge = { top: 50, bottom: 0, left: 10, right: 0 };

const width = window.innerWidth,
  height = window.innerHeight,
  svg = d3.select('body').append('svg').attr('width', width).attr('height', height);

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

//指定树的层次数据结构
const hierarchyData = d3.hierarchy(data).sum(d => d.value)

//创建一个树布局
const tree = d3.tree().size([width / 2, height / 2]).separation((a, b) => (a.parent === b.parent ? 1 : 2) / a.depth)
//使用指定的hierarchy进行布局,并附加node.x和node.y两个属性
const treeData = tree(hierarchyData)
const nodes = treeData.descendants(),
  links = treeData.links()

//贝塞尔曲线生成器
const bezier_curve_generator = d3.linkHorizontal().x(d => d.y).y(d => d.x)

//连线
g.append('g').selectAll('path').data(links).enter()
  .append('path').attr('d', d => {
    const start = { x: d.source.x, y: d.source.y }
    const end = { x: d.target.x, y: d.target.y }
    return bezier_curve_generator({ source: start, target: end })
  })
  .attr('fill', 'none')
  .attr('stroke', '#444')
  .attr('stroke-width', 1)

//节点和对应文字分组
const gs = g.append('g').selectAll('g').data(nodes).enter().append('g')
  .attr('transform', d => `translate(${d.y}, ${d.x})`)

//绘制节点
gs.append('circle').attr('r', 6).attr('fill', 'white').attr('stroke', '#555').attr('stroke-width', 1)
.on('click', nodeClick)
gs.append('circle').attr('r', 2).attr('fill', 'white').attr('stroke', '#555').attr('stroke-width', 1)
  .on('click', nodeClick)

//文字
gs.append('text').attr('x', d => d.children ? -40 : 8).attr('y', -5).attr('dy', 10).text(d => d.data.name)

function nodeClick() {
  d3.select(this).transition().attr('r', 8)
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console