[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; }) 错误 ...
随机推荐
- Mirai僵尸网络重出江湖
近年数度感染数十万台路由器的僵尸网络程序Mirai,虽然原创者已经落网判刑,但是Mirai余孽却在网络上持续变种,现在安全人员发现,Mirai已经将焦点转向Linux服务器了. 安全公司Netcout ...
- CentOS下安装jdk1.8.0_181
我安装的为 jdk1.8.0_181 1.检查是否存在open jdk,不存在直接跳到第 5 步 java -version 查看当前系统自带的open jdk版本信息 2.查看包含java字符串的文 ...
- 二:2.1 字符串与循环中的 while
字符串:字符串是以单引号或双引号括起来的任意文本 创建字符串: str1 = "sunck is a good man!" str3 = "sunckis a nice ...
- CSUOJ 1526 Beam me out!
Beam me out! King Remark, first of his name, is a benign ruler and every wrongdoer gets a second cha ...
- codevs 1019 集合论与图论
1019 集合论与图论 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 集合论与图论对于小松来说 ...
- DuiVision开发教程(19)-菜单
DuiVision菜单类是CDuiMenu.有两种显示的位置,一种是在窗体顶部某个button点击后能够下拉一个菜单,还有一种是托盘图标的右键菜单. 窗体中的菜单定义方式是xml文件里设置某个butt ...
- log4j小结
核心包: org.apache.log4j 三大组件 Loggers 日志操作 Appenders 日志的展现形式 Layouts 日志的展现格式 日志等级 TRACE DEBUG INFO WARN ...
- 百度地图ios环境配置
1 前言 由于工作需要,要开始捣腾百度地图了,今天上午初始牛刀,各种碰壁,无奈之下,中午睡了一觉,养精蓄锐,以备下午大战三百回合,所幸下午中午把百度地图Demo捣腾出来了,在此与大家分享,环境搭建教程 ...
- select选择框实现跳转
select选择框实现跳转 零.启示 1.启示把bom里面的几个对象稍微有点印象,那么主干有了,学其它的就感觉添置加瓦,很简单 就是关注树木的主干 2.万能的百度,不过当然要你基础好学得多才能看得懂, ...
- IBM Tivoli Netview在企业网络管理中的实践(附视频)
今天我为大家介绍的一款高端网管软件名叫IBM Tivoli NetView,他主要关注是IBM整理解决方案的用户,分为Unix平台和Windwos平台两种,这里视频演示的是基于Windows 2003 ...