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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [D3] Build a Scatter Plot with D3 v4

    Scatter plots, sometimes also known as bubble charts, are another common type of visualization. They ...

  4. javascript曲线图和面积图Line & Area chart控件功能及下载

    Line & Area chart 控件是一款新型的.可用性极强的曲线图和面积图产品.一个您网站的访问者可以放大他感兴趣的一段区域,打开和关闭数值气球,并可显示和隐藏图表.您能创建简单.堆积. ...

  5. [D3] 14. Line and Area Charts with D3

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  6. 使用 angular directive 和 json 数据 D3 随着标签 donut chart演示样本

    使用angular resource载入中priorityData.json中间json数据,结合D3绘制甜甜圈图.执行index.html其结果见于图.: priorityData.json中jso ...

  7. [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 ...

  8. d3可视化实战02:理解d3数据驱动的真正含义

    前文中已经提到,SVG从诞生之初起就可以非常方便地使用javascript脚本语言来进行其DOM对象的控制.当然,控制的方法有很多,有直接控制SVG对象的方法,例如使用原生js:有帮你封装一下图形接口 ...

  9. 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; }) 错误 ...

随机推荐

  1. 学习参考《Python数据分析与挖掘实战(张良均等)》中文PDF+源代码

    学习Python的主要语法后,想利用python进行数据分析,感觉<Python数据分析与挖掘实战>可以用来学习参考,理论联系实际,能够操作数据进行验证,基础理论的内容对于新手而言还是挺有 ...

  2. qt获取磁盘序列号

    #include "windows.h" QString lpRootPathName = "c:\\"; LPTSTR lpVolumeNameBuffer= ...

  3. Docs-->.NET-->API reference-->System.​Web.​UI-->Control-->Methods-->Find​Control

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.control.findcontrol?view=netframework-4.7 ...

  4. 4. Brackets 前端编辑器试用

    转自:https://blog.csdn.net/wuji3390/article/details/71170579 Brackets编辑器介绍 "一个现代的,开源的,了解网页设计的编辑器& ...

  5. js数组操作(增、删、改、查)

    数组是js中非常常用的一个对象,它有一些经典的操作,今天零度就为大家介绍介绍. 首先,声明一个数组的方法有两种: var arr = []; 或者 var arr = new Array(); 一般情 ...

  6. cksum---检验文件CRC是否正确

  7. __get__()方法研究

    看源码的时候发现了这个魔法方法 class A(object): def __init__(self): self.name = 'mod' def __get__(self, instance, o ...

  8. 【例题 8-11 UVA-10954】Add All

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 就是合并果子.. 每次都合并最小的就可以啦. 别忘了初始化 [代码] /* 1.Shoud it use long long ? 2 ...

  9. userAgent判断客户端,以及各个浏览器的ua

    userAgent判断客户端,以及各个浏览器的ua http://blog.csdn.net/yoyoosyy/article/details/70142884 navigator.userAgent ...

  10. Dcloud课程8 开心一刻应用如何实现

    Dcloud课程8 开心一刻应用如何实现 一.总结 一句话总结:app就是远程调用接口获得数据,我们在后台要做的,就是写一个个让页面获得接口的数据.这里用的是公共笑话接口. 1.ajax返回给页面的h ...