效果图:

条形图:

目录结构:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="css/style.css" media="screen" rel="stylesheet" type="text/css"/>
<title>Linechart1</title>
</head>
<body>
<div id="container"></div> <script src="https://d3js.org/d3.v5.min.js"></script>
<script src="js/index.js"></script>
</body>
</html>

index.html

svg rect{
fill:#339999;
} svg rect:hover {
cursor: pointer;
fill: #66cccc;
}

style.css

year,population
1953,5.94
1964,6.95
1982,10.08
1990,11.34
2000,12.66
2010,13.40

data.csv

/垂直Bar,加刻度

d3.csv("Data/data.csv",function (d) {
return {
// year: new Date(+d.year, 0, 1), // convert "Year" column to Date
year: d.year+"年",
population: +d.population // convert "population" column to number
};
}).then(function (data) {
console.log(data);
var //data=[1,8,5,6,8,9,3,5,2,20],
width=1000,
height=500,
margin={left:30,top:30,right:30,bottom:30},
svg_width=width+margin.left+margin.right,
svg_height=height+margin.top+margin.bottom; //离散缩放 population=>[0,width]
var scale_x=d3.scaleBand()
.domain(data.map(function (d) {
return d.year;
}))
.range([0,width])
.padding(0.1); //线性缩放
var scale_y=d3.scaleLinear()
.domain([0,d3.max(data,function (d) {
return d.population;
})])
.range([height,0]) var svg=d3.select("#container")
.append("svg")
.attr("width",svg_width)
.attr("height",svg_height) var chart=svg.append("g")
.attr("transform","translate("+margin.left+","+margin.top+")"); var x_axis_color="steelblue",
y_axis_color="steelblue"; var x_axis=chart.append('g')
.call(d3.axisBottom(scale_x))
.attr("transform","translate(0,"+height+")"); x_axis.selectAll("path")
.attr("stroke",x_axis_color) x_axis.selectAll("line")
.attr("stroke",x_axis_color) x_axis.selectAll("text")
.attr("font-size",'2em') var y_axis=chart.append('g')
.call(d3.axisLeft(scale_y)); y_axis.selectAll("path")
.attr("stroke",y_axis_color) y_axis.selectAll("line")
.attr("stroke",y_axis_color) y_axis.selectAll("text")
.attr("font-size",'2em') //Y轴注解
chart.append("text")
.text("(亿)")
// .attr("text-anchor","end")
// .attr("transform","rotate(-90)")
.attr("dy","1em")
//X轴注解
chart.append("text")
.text("(年)")
// .attr("text-anchor","end")
.attr("transform","translate("+width+","+height+")")
.attr("dy","1em") var bar=chart.selectAll(".bar")
.data(data)
.enter()
.append("g")
.attr("transform",function (d,i) {
return "translate("+scale_x(d.year)+",0)"
}) bar.append("rect")
.attr("y",function (d) {
return scale_y(d.population);
})
.attr("height",function (d) {
return height-scale_y(d.population);
})
.attr("width",scale_x.bandwidth() )
.attr("class",".rect") bar.append("text")
.text(function (d) {
return d.population+"亿";
})
.attr("y",function (d) {
return scale_y(d.population);
})
.attr("x",scale_x.bandwidth() /2)
.attr("dy","1em")
.style("text-anchor","middle")
.style("fill","white") })

index.js

 参考教程:https://www.imooc.com/learn/103

使用d3.v5实现条形图的更多相关文章

  1. 使用d3.v5实现饼状图

    效果图: 饼状图: 目录结构: <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  2. 使用d3.v5实现折线图与面积图

    d3最新是V5版的,比起V2的API变动了不少,写下我实现过程 效果图: 面积图: 折线图: 目录结构: <!DOCTYPE html> <html lang="en&qu ...

  3. D3(v5) in TypeScript 坐标轴之 饼状图生成

    饼状图生成时依旧遇到了类型问题,记录如下: import * as d3 from 'd3'; import * as React from 'react'; class TestGraph exte ...

  4. D3(v5) in TypeScript 坐标轴之 scaleBand用法

    在学习d3时候,发现在TS中实现D3的坐标轴中遇到一些错误,而这些错误却不会存在于js(因为ts的类型检查)写法中,因此做下笔记: import * as d3 from 'd3';import * ...

  5. Vue整合d3.v5.js制作--柱状图(rect)

    先上效果图: 图中柱状图变成纯蓝色是鼠标滑动过的颜色(颜色可改,本人配色能力十分的强,建议直接用默认设置即可 ( ᖛ ̫ ᖛ )ʃ)) 1.环境说明 Vue版本:"vue": &q ...

  6. Vue整合d3.v5.js制作--折线图(line)

    先上效果图(x轴固定为时间轴): 图中出现的悬浮框是鼠标悬停效果 1.环境说明: vue版本:"vue": "^2.5.2" d3版本:"d3&quo ...

  7. ASP.NET+d3.js实现Sqlserver数据库的可视化展示

    效果: 数据库端: 前端展示: 实现原理: 1.在数据段建立两个存储过程 queryUserAnsawer(id) 根据用户ID返回每一题的得分,主要是bcp exe时不能直接在sqlserver中执 ...

  8. 使用D3.js构建实时图形

    首先你需要在计算机上安装Node和npm. 数据的可视化表示是传递复杂信息的最有效手段之一,D3.js提供了创建这些数据可视化的强大工具和灵活性. D3.js是一个JavaScript库,用于使用SV ...

  9. 使用JavaScript和D3.js实现数据可视化

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由独木桥先生发表于云+社区专栏 介绍 D3.js是一个JavaScript库.它的全称是Data-Driven Documents(数据 ...

随机推荐

  1. 2017年BackBox5和Ubuntu16.04.1国内更新源

    BackBox是基于Ubuntu的Linux发行版,因此,我们可以使用Ubuntu的更新源作为BackBox的更新源. 1.查看系统版本信息: 输入: uname -a 显示计算机及操作系统的有关信息 ...

  2. HTTPS 之 TLS 性能调优

    HTTPS(HTTP over SSL)是以安全为目标的 HTTP 通道,可以理解为 HTTP + SSL/TLS,即在 HTTP 下加入 SSL/TLS 层作为安全基础.其中 TLS 的前身是 SS ...

  3. SpringCloud Eureka 报错 无法启动问题

    1.SpringCloud Eureka 报错 无法启动基本上都是spring boot的版本与spring cloud的版本不匹配导致的. <dependencyManagement> ...

  4. javascript this 的工作原理

    JavaScript 有一套完全不同于其它语言的对 this 的处理机制. 在五种不同的情况下 ,this 指向的各不相同. 1.全局范围内 当在全部范围内使用 this,它将会指向全局对象. 2.函 ...

  5. python使用@property

    在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改: s = Student() s.score = 9999 这显然不合逻辑.为了限制score的 ...

  6. Redis Rpop 命令

    Redis Rpop 命令用于移除并返回列表的最后一个元素. 语法 redis Rpop 命令基本语法如下: redis 127.0.0.1:6379> RPOP KEY_NAME 可用版本 & ...

  7. java中位运算

    1byte(字节)=8bit(比特) 1 0 0 0 0 0 0 0 1   2进制的1的原码 反码 补码 0 0 0 0 0 0 0 0   2进制的0的原码 反码 补码 -1 1 0 0 0 0 ...

  8. Intellij Idea中如何debug本地maven项目

    方法一:使用maven中的jetty插件调试本地maven项目 1.打断点 2.右击"jetty:run",选择Debug运行 3.浏览器发送http请求,开始调试 方法二:利用远 ...

  9. forwardPort.go

    packagemain import(     "encoding/json"     "flag"     "fmt"     " ...

  10. blog4go.go

    package blog4go import ( "bufio" "errors" "fmt" "io" "o ...