【无标题】

学习目标:

提示:这里可以添加学习目标

例如:

  • 入门vue3+d3

学习内容:

提示:这里可以添加要学的内容

1.安装Nodejs
下载并安装Nodejs,写一个新的文件 server.js,在命令行运行
node server.js。

2 安装vue并在vscode里打开项目文件
npm install vue 安装vue
npm install vite 安装Vite 包
npm init vite-app my-project 创建工程
npm run dev 运行
3.vue 基本用法文档结构
在这里插入图片描述
使用Npm run my-project 运行项目

4.导入d3
npm run d3
5.使用d3进行基本数据可视化
这是其中的一部分,画直方图。

<script>
import { defineComponent } from 'vue';
import * as d3 from "d3";
var confirm=[81900012,21410305,26228521,17926104,490511];
var color=d3.schemeCategory10;
var area=["美国","英国","法国","俄罗斯","中国"];
export default defineComponent({
    mounted(){
        this.drawBarChart(data);
    },
    methods:{
        drawBarChart(data){
            const width=800;
            const height=400;
            var scale=81900012/(height*0.9);
            var rectStep =width/confirm.length;
		    var rectWidth =rectStep-10;
            var svg=d3.select("#bar-chart-container")
                         .append("svg")
                        .attr("width",width)
                        .attr("height",height);
            // var svg2= d3.select("#bar-chart-container")
		  	// 				.append("svg")			
		  	// 				.attr("width", width)	
		  	// 				.attr("height", height*0.1);
            svg.selectAll("rect")
                .data(confirm)
                .enter()
                .append("rect")
                .attr("fill",(d,i)=>color[i])
                .attr("x",function(d,i){
                    return  i * rectStep;
                })
                .attr("y",d=>(height-d/scale))
                .attr("height",d=>(d/scale))
                .attr("width",rectWidth);
            svg.selectAll("text")
		  					.data(confirm)			
		  					.enter()				
		  					.append("text")			
		  					.attr("fill","black")
		  					.attr("font-size","15px")
		  					.attr("text-anchor","middle")
		  					.attr("x", function(d,i){
		  						return i * rectStep;
		  					})
		  					.attr("y", function(d){
		  						return height-d/scale-20;
		  					})
		  					.attr("dx",rectWidth/2)
		  					.attr("dy","1em")
		  					.text(function(d){
		  						console.log(d);
		  						return Math.floor(d);
		  					});
            // svg2.selectAll("text")
		  	// 					.data(area)			
		  	// 					.enter()
		  	// 					.append("text")			
		  	// 					.attr("fill","black")
		  	// 					.attr("font-size","15px")
		  	// 					.attr("text-anchor","middle")
		  	// 					.attr("x", function(d,i){
		  	// 						console.log(i);
		  	// 						return i * rectStep;
		  	// 					})
		  	// 					.attr("y", 0)
		  	// 					.attr("dx",rectWidth)
		  	// 					.attr("dy","1em")
		  	// 					.text(function(d){
		  	// 						console.log(d);
		  	// 						return d;
		  	// 					});
		  		
        }
    }
})
</script>

结果展示:

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值