[D3] Build an Area Chart with D3 v4
Similar to line charts, area charts are great for displaying temporal data. Whether you’re displaying a single set of data or multiple sets using an overlapping or stacked layout, D3 provides APIs to make the process straightforward. This lesson walks you through creating multiple layouts easily.
var margin = {
top: ,
right: ,
bottom: ,
left:
};
var width = - margin.left - margin.right;
var height = - margin.top - margin.bottom;
var svg = d3.select('.chart')
.append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.call(responsivefy)
.append('g')
.attr('transform', 'translate(' + margin.left + ', ' + margin.top + ')');
/**
* Load data
*/
d3.json('../data/data3.json', function (err, data) {
const parseTime = d3.timeParse('%Y/%m/%d');
data.forEach(company => {
company.values.forEach(d => {
d.date = parseTime(d.date)
d.close = +d.close
})
})
/**
* Y axis
*/
const yScale = d3.scaleLinear()
.domain([
d3.min(data, co => d3.min(co.values, d => d.close)),
d3.max(data, co => d3.max(co.values, d => d.close))
])
.range([height, ])
.nice();
const yAxis = d3.axisLeft(yScale);
svg.call(yAxis);
/**
* x axis
*/
const xScale = d3.scaleTime()
.domain([
d3.min(data, co => d3.min(co.values, d => d.date)),
d3.max(data, co => d3.max(co.values, d => d.date))
])
.range([, width])
.nice();
const xAxis = d3.axisBottom(xScale).tickSize().tickPadding();
svg.append('g')
.attr('transform', `translate(, ${height})`)
.call(xAxis)
.selectAll('text')
.attr('text-anchor', 'end')
.attr('transform', 'rotate(-45)');
console.log("yScale(yScale.domain()[0])", yScale(yScale.domain()[])); //yScale(680)-->525
/**
* Creat area chart
*/
const area = d3.area()
.x(d => xScale(d.date))
.y0(yScale(yScale.domain()[]))
.y1(d => yScale(d.close))
.curve(d3.curveCatmullRom.alpha(0.5));
svg.selectAll('.area')
.data(data)
.enter()
.append('path')
.attr('class', 'area')
.attr('d', d => area(d.values))
.style('stroke', (d, i) => ['#FF9900', '#3369E8'][i])
.style('sroke-width', )
.style('fill', (d, i) => ['#FF9900', '#3369E8'][i])
.style('fill-opacity', 0.5);
});
function responsivefy(svg) {
// get container + svg aspect ratio
var container = d3.select(svg.node().parentNode),
width = parseInt(svg.style("width")),
height = parseInt(svg.style("height")),
aspect = width / height;
// add viewBox and preserveAspectRatio properties,
// and call resize so that svg resizes on inital page load
svg.attr("viewBox", "0 0 " + width + " " + height)
.attr("preserveAspectRatio", "xMinYMid")
.call(resize);
// to register multiple listeners for same event type,
// you need to add namespace, i.e., 'click.foo'
// necessary if you call invoke this function for multiple svgs
// api docs: https://github.com/mbostock/d3/wiki/Selections#on
d3.select(window).on("resize." + container.attr("id"), resize);
// get width of container and resize svg to fit it
function resize() {
var targetWidth = parseInt(container.style("width"));
svg.attr("width", targetWidth);
svg.attr("height", Math.round(targetWidth / aspect));
}
}
[D3] Build an Area Chart with D3 v4的更多相关文章
- [D3] Build a Line Chart with D3 v4
Line charts are often used to plot temporal data, like a stock price over time. In this lesson we’ll ...
- [D3] Build a Column Chart with D3 v4
Column and bar charts are staples of every visualization library. They also make a great project for ...
- [D3] Build a Scatter Plot with D3 v4
Scatter plots, sometimes also known as bubble charts, are another common type of visualization. They ...
- javascript曲线图和面积图Line & Area chart控件功能及下载
Line & Area chart 控件是一款新型的.可用性极强的曲线图和面积图产品.一个您网站的访问者可以放大他感兴趣的一段区域,打开和关闭数值气球,并可显示和隐藏图表.您能创建简单.堆积. ...
- [D3] 14. Line and Area Charts with D3
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 使用 angular directive 和 json 数据 D3 随着标签 donut chart演示样本
使用angular resource载入中priorityData.json中间json数据,结合D3绘制甜甜圈图.执行index.html其结果见于图.: priorityData.json中jso ...
- [D3] Load and Inspect Data with D3 v4
You probably use a framework or standalone library to load data into your apps, but what if that’s o ...
- d3可视化实战02:理解d3数据驱动的真正含义
前文中已经提到,SVG从诞生之初起就可以非常方便地使用javascript脚本语言来进行其DOM对象的控制.当然,控制的方法有很多,有直接控制SVG对象的方法,例如使用原生js:有帮你封装一下图形接口 ...
- d3.svg.line()错误:TypeError: d3.svg.line is not a function
var line_generator= d3.svg.line() .x(function (d,i) { return i; }) .y(function (d) { return d; }) 错误 ...
随机推荐
- Zabbix监控平台部署
系统环境 Server端:192.168.149.128 Agent端:192.168.149.129 一.lamp环境安装 1.yum安装lamp yum install -y http http- ...
- 阿里&163 yum源
站点:https://opsx.alibaba.com/mirror #阿里云base cat /etc/yum.repos.d/alibase.repo [rhel7] name=ali-base ...
- 手把手教你用vue-cli构建一个简单的路由应用
上一章说道:十分钟上手-搭建vue开发环境(新手教程)https://www.jianshu.com/p/0c6678671635 开发环境搭建好之后,那么开始新添加一些页面,构建最基本的vue项目, ...
- JAVA实现排队论
转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/50401727 http://www.llwjy.com/blogdetail/3 ...
- cocos2d-x 3.2 之 2048 —— 第一篇
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- cgroups
CGROUPS官方解析,用户空间怎样监控 http://blog.chinaunix.net/uid-16763274-id-2103750.html cgroups概念 fr=aladdin&quo ...
- ios in-house 公布整个过程(startssl认证)
首先大体说一下步骤: 1.申请苹果enterprise 账号 为应用生成app id,provision profile等 详见:http://www.th7.cn/Program/IOS/20131 ...
- 小贝_redis高级应用-安全性
redis高级应用-安全性 一.为什么redis须要安全性 二.设置redis验证password 三.验证 一.为什么redis须要安全性 1.redis作为数据的存储介质.假设无法保证redi ...
- Android学习笔记之图像颜色处理(ColorMatrix)
对图像进行颜色方面的处理,通过使用颜色矩阵(ColorMatrix)来实现.从而可以达到很多特效如黑白老照片.泛黄旧照片等等. 1.颜色矩阵(ColorMatrix) 这里有详细的介绍:http:// ...
- 51nod1004 n^n的末位数字
题目来源: Author Ignatius.L (Hdu 1061) 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 给出一个整数N,输出N^N(N的 ...