使用d3.v5实现条形图
效果图:
条形图:

目录结构:

<!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实现条形图的更多相关文章
- 使用d3.v5实现饼状图
效果图: 饼状图: 目录结构: <!DOCTYPE html> <html lang="en"> <head> <meta charset ...
- 使用d3.v5实现折线图与面积图
d3最新是V5版的,比起V2的API变动了不少,写下我实现过程 效果图: 面积图: 折线图: 目录结构: <!DOCTYPE html> <html lang="en&qu ...
- D3(v5) in TypeScript 坐标轴之 饼状图生成
饼状图生成时依旧遇到了类型问题,记录如下: import * as d3 from 'd3'; import * as React from 'react'; class TestGraph exte ...
- D3(v5) in TypeScript 坐标轴之 scaleBand用法
在学习d3时候,发现在TS中实现D3的坐标轴中遇到一些错误,而这些错误却不会存在于js(因为ts的类型检查)写法中,因此做下笔记: import * as d3 from 'd3';import * ...
- Vue整合d3.v5.js制作--柱状图(rect)
先上效果图: 图中柱状图变成纯蓝色是鼠标滑动过的颜色(颜色可改,本人配色能力十分的强,建议直接用默认设置即可 ( ᖛ ̫ ᖛ )ʃ)) 1.环境说明 Vue版本:"vue": &q ...
- Vue整合d3.v5.js制作--折线图(line)
先上效果图(x轴固定为时间轴): 图中出现的悬浮框是鼠标悬停效果 1.环境说明: vue版本:"vue": "^2.5.2" d3版本:"d3&quo ...
- ASP.NET+d3.js实现Sqlserver数据库的可视化展示
效果: 数据库端: 前端展示: 实现原理: 1.在数据段建立两个存储过程 queryUserAnsawer(id) 根据用户ID返回每一题的得分,主要是bcp exe时不能直接在sqlserver中执 ...
- 使用D3.js构建实时图形
首先你需要在计算机上安装Node和npm. 数据的可视化表示是传递复杂信息的最有效手段之一,D3.js提供了创建这些数据可视化的强大工具和灵活性. D3.js是一个JavaScript库,用于使用SV ...
- 使用JavaScript和D3.js实现数据可视化
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由独木桥先生发表于云+社区专栏 介绍 D3.js是一个JavaScript库.它的全称是Data-Driven Documents(数据 ...
随机推荐
- 免密登录-python
要完成后台管理系统登录功能,通过查看登录页面,我们可以了解到,我们需要编写验证码图片获取接口和登录处理接口,然后在登录页面的HTML上编写AJAX. 在进行接口开发之前,还有一个重要的事情要处理,那就 ...
- InnoDB基本特性
Ⅰ.double write 目的:保证数据写入的可靠性 小知识: 什么是partial write? 16k的page只写入了4k,6k,8k,12k就断掉了的情况 corrupt的page就是pa ...
- 最近最久未使用页面淘汰算法———LRU算法(java实现)
请珍惜小编劳动成果,该文章为小编原创,转载请注明出处. LRU算法,即Last Recently Used ---选择最后一次访问时间距离当前时间最长的一页并淘汰之--即淘汰最长时间没有使用的页 按照 ...
- Reading Code Is Hard
注: 以下内容引自: https://blogs.msdn.microsoft.com/ericlippert/2004/06/14/reading-code-is-hard/ Reading Cod ...
- JSON 的小技巧
有的时候上游传过来的字段是string类型的,但是我们却想用变成数字来使用. 本来用一个json:",string" 就可以支持了,如果不知道golang的这些小技巧,就要大费周章 ...
- 使用Eclipse远程调试
在一般的开发中,有事因为项目需要,测试环境是在Linux下,这是如果出现异常或者bug,调试起来都是很费劲的,如果你还在为这个头疼,那就好好看接下来的总结 1,启动改程序时,不论是脚本启动,还是tom ...
- POJ_2318_TOYS&&POJ_2398_Toy Storage_二分+判断直线和点的位置关系
POJ_2318_TOYS&&POJ_2398_Toy Storage_二分+判断直线和点的位置 Description Calculate the number of toys th ...
- WAMP下配置FCGID+ZendGuardLoader
公司的项目里,有几个文件是被加密的,经过一翻折腾,终于配置成功 文件加密技术用的是ZendGuard,所以必须安装的PHP必须得是nts的 一.下载并配置PHP 先下载安装php,注意VC版本和是否n ...
- STM32 Cube mx 安装
原文链接:http://www.cnblogs.com/strongerHuang/p/5778216.html Ⅰ.写在前面 相信很多人都知道STM32CubeMX这个工具,也是近年来开发STM32 ...
- Vue-CLI和脚手架
但我们学习Vue时,很多教程都会说到用Vue-CLI构建项目,那么什么是脚手架?什么是Vue-CLI?为什么要用脚手架,好处在哪?以及为何我们用Vue开发项目时要用到Vue-CLI? 首先,CLI为c ...