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.

  1. var margin = {
  2. top: ,
  3. right: ,
  4. bottom: ,
  5. left:
  6. };
  7. var width = - margin.left - margin.right;
  8. var height = - margin.top - margin.bottom;
  9.  
  10. var svg = d3.select('.chart')
  11. .append('svg')
  12. .attr('width', width + margin.left + margin.right)
  13. .attr('height', height + margin.top + margin.bottom)
  14. .call(responsivefy)
  15. .append('g')
  16. .attr('transform', 'translate(' + margin.left + ', ' + margin.top + ')');
  17.  
  18. /**
  19. * Load data
  20. */
  21. d3.json('../data/data3.json', function (err, data) {
  22. const parseTime = d3.timeParse('%Y/%m/%d');
  23.  
  24. data.forEach(company => {
  25. company.values.forEach(d => {
  26. d.date = parseTime(d.date)
  27. d.close = +d.close
  28. })
  29. })
  30.  
  31. /**
  32. * Y axis
  33. */
  34. const yScale = d3.scaleLinear()
  35. .domain([
  36. d3.min(data, co => d3.min(co.values, d => d.close)),
  37. d3.max(data, co => d3.max(co.values, d => d.close))
  38. ])
  39. .range([height, ])
  40. .nice();
  41. const yAxis = d3.axisLeft(yScale);
  42. svg.call(yAxis);
  43.  
  44. /**
  45. * x axis
  46. */
  47. const xScale = d3.scaleTime()
  48. .domain([
  49. d3.min(data, co => d3.min(co.values, d => d.date)),
  50. d3.max(data, co => d3.max(co.values, d => d.date))
  51. ])
  52. .range([, width])
  53. .nice();
  54. const xAxis = d3.axisBottom(xScale).tickSize().tickPadding();
  55. svg.append('g')
  56. .attr('transform', `translate(, ${height})`)
  57. .call(xAxis)
  58. .selectAll('text')
  59. .attr('text-anchor', 'end')
  60. .attr('transform', 'rotate(-45)');
  61.  
  62. console.log("yScale(yScale.domain()[0])", yScale(yScale.domain()[])); //yScale(680)-->525
  63. /**
  64. * Creat area chart
  65. */
  66. const area = d3.area()
  67. .x(d => xScale(d.date))
  68. .y0(yScale(yScale.domain()[]))
  69. .y1(d => yScale(d.close))
  70. .curve(d3.curveCatmullRom.alpha(0.5));
  71.  
  72. svg.selectAll('.area')
  73. .data(data)
  74. .enter()
  75. .append('path')
  76. .attr('class', 'area')
  77. .attr('d', d => area(d.values))
  78. .style('stroke', (d, i) => ['#FF9900', '#3369E8'][i])
  79. .style('sroke-width', )
  80. .style('fill', (d, i) => ['#FF9900', '#3369E8'][i])
  81. .style('fill-opacity', 0.5);
  82. });
  83.  
  84. function responsivefy(svg) {
  85. // get container + svg aspect ratio
  86. var container = d3.select(svg.node().parentNode),
  87. width = parseInt(svg.style("width")),
  88. height = parseInt(svg.style("height")),
  89. aspect = width / height;
  90.  
  91. // add viewBox and preserveAspectRatio properties,
  92. // and call resize so that svg resizes on inital page load
  93. svg.attr("viewBox", "0 0 " + width + " " + height)
  94. .attr("preserveAspectRatio", "xMinYMid")
  95. .call(resize);
  96.  
  97. // to register multiple listeners for same event type,
  98. // you need to add namespace, i.e., 'click.foo'
  99. // necessary if you call invoke this function for multiple svgs
  100. // api docs: https://github.com/mbostock/d3/wiki/Selections#on
  101. d3.select(window).on("resize." + container.attr("id"), resize);
  102.  
  103. // get width of container and resize svg to fit it
  104. function resize() {
  105. var targetWidth = parseInt(container.style("width"));
  106. svg.attr("width", targetWidth);
  107. svg.attr("height", Math.round(targetWidth / aspect));
  108. }
  109. }

[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. Mirai僵尸网络重出江湖

    近年数度感染数十万台路由器的僵尸网络程序Mirai,虽然原创者已经落网判刑,但是Mirai余孽却在网络上持续变种,现在安全人员发现,Mirai已经将焦点转向Linux服务器了. 安全公司Netcout ...

  2. CentOS下安装jdk1.8.0_181

    我安装的为 jdk1.8.0_181 1.检查是否存在open jdk,不存在直接跳到第 5 步 java -version 查看当前系统自带的open jdk版本信息 2.查看包含java字符串的文 ...

  3. 二:2.1 字符串与循环中的 while

    字符串:字符串是以单引号或双引号括起来的任意文本 创建字符串: str1 = "sunck is a good man!" str3 = "sunckis a nice ...

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

  5. codevs 1019 集合论与图论

    1019 集合论与图论  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 集合论与图论对于小松来说 ...

  6. DuiVision开发教程(19)-菜单

    DuiVision菜单类是CDuiMenu.有两种显示的位置,一种是在窗体顶部某个button点击后能够下拉一个菜单,还有一种是托盘图标的右键菜单. 窗体中的菜单定义方式是xml文件里设置某个butt ...

  7. log4j小结

    核心包: org.apache.log4j 三大组件 Loggers 日志操作 Appenders 日志的展现形式 Layouts 日志的展现格式 日志等级 TRACE DEBUG INFO WARN ...

  8. 百度地图ios环境配置

    1 前言 由于工作需要,要开始捣腾百度地图了,今天上午初始牛刀,各种碰壁,无奈之下,中午睡了一觉,养精蓄锐,以备下午大战三百回合,所幸下午中午把百度地图Demo捣腾出来了,在此与大家分享,环境搭建教程 ...

  9. select选择框实现跳转

    select选择框实现跳转 零.启示 1.启示把bom里面的几个对象稍微有点印象,那么主干有了,学其它的就感觉添置加瓦,很简单 就是关注树木的主干 2.万能的百度,不过当然要你基础好学得多才能看得懂, ...

  10. IBM Tivoli Netview在企业网络管理中的实践(附视频)

    今天我为大家介绍的一款高端网管软件名叫IBM Tivoli NetView,他主要关注是IBM整理解决方案的用户,分为Unix平台和Windwos平台两种,这里视频演示的是基于Windows 2003 ...