vue+D3 层次树图绘制

<template>
    <div id="log"></div>
      <div id="bar-chart-container"></div>
    </template>
  <style>
#g{
            background: url("F:\vite-vue\public\背景.jpg");
            background-repeat: no-repeat;
            background-position: center;
            height: 100%;
            color: black;
            width: 100%;
            background-size: cover;
            position: fixed;
            opacity: 0.7;
         }
#bar-chart-container{
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: url("F:\vite-vue\public\背景.jpg");
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center center;
    z-index: -1; /* 将背景图放在最底层 */
    opacity: 0.5;
}
 .tooltip{
          background: #eed5a4;
          line-height: 2;
          opacity:0.8;
          padding: 10px;
          font-size: 15px;
          border-radius: 5px;
          -moz-border-radius: 5px;
          -webkit-border-radius: 5px;
          overflow: auto;
      }
</style>
    <script>
    import { defineComponent } from 'vue';

    import axios from "axios";
    import * as d3 from "d3";
       
    var color = d3.schemeCategory10;
    export default defineComponent({
      mounted() {      
        axios.get("caixi.json").then((res) => {
                console.log(res.data);
                this.drawGraphChart(res.data);
            });    
      },
      methods:{
        drawGraphChart(data){
            var width=(window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth);
		    var height=(window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight);
          width=width*0.8;
          height=height*0.9;
            var color = d3.schemeCategory10;
            var svg = d3.select("body")			//选择<body>
					.append("svg")			//在<body>中添加<svg>
					.attr("width", width)	//设定<svg>的宽度属性
					.attr("height", height);//设定<svg>的高度属性
                    
                    
           var radius = width /2;
            var tree=d3.cluster()
                       .size([2*Math.PI,radius-300]);
            d3.json("caixi.json").then(function(data) {
               console.log(data);
               var hi=d3.hierarchy(data);
               console.log(hi);
               var root=tree(hi);
              var links=root.links();
               console.log(links);
              var nodes=root.descendants();
               console.log(nodes);
               svg.append("g")
                      .attr("fill", "none")
                      .attr("stroke", "#555")
                      .attr("stroke-width", 1)
                    .selectAll("path")
                    .data(links)
                    .join("path")
                    .attr("d", d3.linkRadial()
                      .angle(d => d.x)
                      .radius(d => d.y));

                svg.append("g")
                   .selectAll("circle")
                   .data(nodes)
                   .join("circle")
                   .attr("transform", d => `
                    rotate(${d.x * 180 / Math.PI - 90})
                    translate(${d.y},0)
                  `)
                  .attr("fill",d=>color[d.depth])
                  .attr("r", d=>(d.height*3+3))
                  .on("mouseenter",function(d,i)
                        {
                            const change=d3.select(this).attr("fill","white")
                            var t=change.attr("info")
                            tooltip.html("简介:"+t)
                                   .style("visibility","visible")
                        })
                        .on("mouseleave",function(d,i)
                        {
                            const change=d3.select(this).attr("fill","#bb996e")
                        })
                  ;

                  svg.append("g")
				  	    .attr("font-family", "sans-serif")
					    .attr("font-size", 10)
					    .attr("stroke-width", 0)
                     .selectAll("text")
                     .data(root.descendants())
                     .join("text")
                     .attr("transform", d => `
						rotate(${d.x * 180 / Math.PI - 90})
						translate(${d.y},0)
						rotate(${d.x >= Math.PI ? 180 : 0})
					  `)
                     .attr("dy", "0.31em")
                     .attr("x", d => d.x < Math.PI === !d.children ? 6 : -6)
                     .attr("text-anchor", d => d.x < Math.PI === !d.children ? "start" : "end")
                     .text(d => d.data.name)
                     .clone(true).lower()
                     .attr("fill", d=>color[d.depth]);

                  svg.selectAll("g")
                   .attr("transform", "translate(" + (width/2) + "," + (height/2) + ")");
                   var title = svg.append("text")
                        .text("中   国   菜    系")
                        .attr("x", 30)
                        .attr("y", 50)
                        .attr("font-size", "58")
                        .attr("fill", "black")
                        .style("writing-mode", "vertical-lr");
                    var shum = svg.append("text")
                        .text("20数科")
                        .attr("x", 65)
                        .attr("y", 330)
                        .attr("font-size", "12")
                        .attr("fill", "black")
                        .style("writing-mode", "vertical-lr");
            
            })
          }
      }
    })
    </script>
    

 

 

<template>
    <div id="log"></div>
      <div id="bar-chart-container"></div>
    </template>
  <style>
#g{
            background: url("F:\vite-vue\public\背景.jpg");
            background-repeat: no-repeat;
            background-position: center;
            height: 100%;
            color: black;
            width: 100%;
            background-size: cover;
            position: fixed;
            opacity: 0.7;
         }
#bar-chart-container{
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: url("F:\vite-vue\public\背景.jpg");
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center center;
    z-index: -1; /* 将背景图放在最底层 */
    opacity: 0.5;
}
 .tooltip{
          background: #eed5a4;
          line-height: 2;
          opacity:0.8;
          padding: 10px;
          font-size: 15px;
          border-radius: 5px;
          -moz-border-radius: 5px;
          -webkit-border-radius: 5px;
          overflow: auto;
      }
</style>
    <script>
    import { defineComponent } from 'vue';

    import axios from "axios";
    import * as d3 from "d3";
       
    var color = d3.schemeCategory10;
    export default defineComponent({
      mounted() {      
        axios.get("caixi.json").then((res) => {
                console.log(res.data);
                this.drawGraphChart(res.data);
            });    
      },
      methods:{
        drawGraphChart(data){
            var width=(window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth);
		    var height=(window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight);
          width=width*0.8;
          height=height*0.9;
            var color = d3.schemeCategory10;
            var svg = d3.select("body")			//选择<body>
					.append("svg")			//在<body>中添加<svg>
					.attr("width", width)	//设定<svg>的宽度属性
					.attr("height", height);//设定<svg>的高度属性
                    
                    
           var radius = width /2;
            var tree=d3.cluster()
                       .size([height,width*0.8]);
            d3.json("caixi.json").then(function(data) {
               console.log(data);
               var hi=d3.hierarchy(data);
               console.log(hi);
               var root=tree(hi);
              var links=root.links();
               console.log(links);
              var nodes=root.descendants();
               console.log(nodes);
 
            
            var gc=svg.append("g");
            var lines=gc.selectAll("path")
                        .data(links)
                        .enter()
                        .append("path")
                        .attr("fill","none")
                        .attr("stroke","#eed5a4")
                        .attr("d",d3.linkHorizontal()         //d3.linkHorizontal()
                                    .x(d=>d.y+90)
                                    .y(d=>d.x+10)
                        );
            var tooltip=d3.select("body")
                          .append("div")
                          .attr("class","tooltip")
                          .attr("opacity",0.0);
            var mynode=gc.selectAll("circle") 
                        .data(nodes)
                        .join("circle")
                        .attr("cx",d=>d.y+110)
                        .attr("cy",d=>d.x+10)
                        .attr("r",d=>(d.height+2)*3)
                        .attr("fill","#bb996e")
                        .attr("stroke","white")
                        .attr("info",(d)=>d.data.intro)
                        .on("mouseenter",function(d,i)
                        {
                            const change=d3.select(this).attr("fill","white")
                            var t=change.attr("info")
                            tooltip.html("简介:"+t)
                                   .style("visibility","visible")
                        })
                        .on("mouseleave",function(d,i)
                        {
                            const change=d3.select(this).attr("fill","#bb996e")
                        })
            var nodetxt=gc.selectAll("text") 
                        .data(nodes)
                        .join("text")
                        .attr("x",d=>d.y+110)
                        .attr("y",d=>d.x+20)
                        .attr("font-size","12")
                        .attr("fill","black")
                        .attr("transform",d=>`rotate(0,${d.x},${d.y})`)         
                        .text(d=>d.data.name);  
            var title=gc.append("text")
                        .text("中国菜系")
                        .attr("x",width/2-200)
                        .attr("y",50)
                        .attr("font-size","48")
                        .attr("fill","black")
            })
          }
      }
    })
    </script>
    

 

<template>
    <div id="log"></div>
      <div id="bar-chart-container"></div>
    </template>
  <style>
#g{
            background: url("F:\vite-vue\public\背景.jpg");
            background-repeat: no-repeat;
            background-position: center;
            height: 100%;
            color: black;
            width: 100%;
            background-size: cover;
            position: fixed;
            opacity: 0.7;
         }
#bar-chart-container{
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: url("F:\vite-vue\public\背景.jpg");
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center center;
    z-index: -1; /* 将背景图放在最底层 */
    opacity: 0.5;
}
 .tooltip{
          background: #eed5a4;
          line-height: 2;
          opacity:0.8;
          padding: 10px;
          font-size: 15px;
          border-radius: 5px;
          -moz-border-radius: 5px;
          -webkit-border-radius: 5px;
          overflow: auto;
      }
</style>
    <script>
    import { defineComponent } from 'vue';

    import axios from "axios";
    import * as d3 from "d3";
       
    var color = d3.schemeCategory10;
    export default defineComponent({
      mounted() {      
        axios.get("caixi.json").then((res) => {
                console.log(res.data);
                this.drawGraphChart(res.data);
            });    
      },
      methods:{
        drawGraphChart(data){
            var width=(window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth);
		    var height=(window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight);
          width=width*0.8;
          height=height*0.9;
            var color = d3.schemeCategory10;
            var svg = d3.select("body")			//选择<body>
					.append("svg")			//在<body>中添加<svg>
					.attr("width", width)	//设定<svg>的宽度属性
					.attr("height", height);//设定<svg>的高度属性
                    
                    
           var radius = width /2;
            var tree=d3.cluster()
                       .size([width,height*0.7]);
            d3.json("caixi.json").then(function(data) {
               console.log(data);
               var hi=d3.hierarchy(data);
               console.log(hi);
               var root=tree(hi);
              var links=root.links();
               console.log(links);
              var nodes=root.descendants();
               console.log(nodes);
 
            
            var gc=svg.append("g");
            var lines=gc.selectAll("path")
                        .data(links)
                        .enter()
                        .append("path")
                        .attr("fill","none")
                        .attr("stroke","#eed5a4")
                        .attr("d",d3.linkVertical()          //d3.linkHorizontal()
                                    .x(d=>d.x)
                                    .y(d=>d.y+90)
                        );
            var tooltip=d3.select("body")
                          .append("div")
                          .attr("class","tooltip")
                          .attr("opacity",0.0);
            var mynode=gc.selectAll("circle") 
                        .data(nodes)
                        .join("circle")
                        .attr("cx",d=>d.x)
                        .attr("cy",d=>d.y+90)
                        .attr("r",d=>(d.height+2)*3)
                        .attr("fill","#bb996e")
                        .attr("stroke","white")
                        .attr("info",(d)=>d.data.intro)
                        .on("mouseenter",function(d,i)
                        {
                            const change=d3.select(this).attr("fill","white")
                            var t=change.attr("info")
                            tooltip.html("简介:"+t)
                                   .style("visibility","visible")
                        })
                        .on("mouseleave",function(d,i)
                        {
                            const change=d3.select(this).attr("fill","#bb996e")
                        })
            var nodetxt=gc.selectAll("text") 
                        .data(nodes)
                        .join("text")
                        .attr("x",d=>d.x+95)
                        .attr("y",d=>d.y-10)
                        .attr("font-size","15")
                        .attr("fill","black")
                        .attr("transform",d=>`rotate(90,${d.x},${d.y})`)         
                        .text(d=>d.data.name);  
            var title=gc.append("text")
                        .text("中国菜系")
                        .attr("x",width/2-200)
                        .attr("y",50)
                        .attr("font-size","48")
                        .attr("fill","black")
            })
          }
      }
    })
    </script>
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值