d3最新是V5版的,比起V2的API变动了不少,写下我实现过程

效果图:

面积图:

折线图:

目录结构:

<!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

#container{
background: #ddd;
width: 500px;
height: 250px;
} path{
fill: none;
stroke: steelblue;
stroke-width: 2;
} .domain ,.tick line{
stroke:gray;
stroke-width: 1;
}

style.css

var width=500,height=250,
margin={left:50,top:30,right:20,bottom:20},
g_width=width-margin.left-margin.right,
g_height=height-margin.top-margin.bottom; //svg
d3.select("#container").append("svg")
//width,height
.attr("width",width)
.attr("height",height) var g=d3.select("svg")
.append("g")
.attr("transform","translate("+margin.left+","+margin.top+")"); var data=[1,8,5,6,8,9,3,5,2]
//Scale
var scale_x=d3.scaleLinear()
.domain([0,data.length-1])
.range([0,g_width]);
var scale_y=d3.scaleLinear()
.domain([0,d3.max(data)])
.range([g_height,0]); //画线函数
var line_generator= d3.line()
.x(function (d,i) {
return scale_x(i);
})
.y(function (d) {
return scale_y(d);
})
.curve(d3.curveMonotoneX)
// .curve(d3.curveMonotoneX) // apply smoothing to the line //画路径
g.append("path")
.attr("d",line_generator(data)) //d="M1,0L20,40..... d-path data // //画面积函数
// var area_generator= d3.area()
// .x(function (d,i) {
// return scale_x(i);
// })
// .y0(g_height)
// .y1(function (d) {
// return scale_y(d);
// })
// .curve(d3.curveMonotoneX)
//
// //画面积
// g.append("path")
// .attr("d",area_generator(data)) //d="M1,0L20,40..... d-path data
// .style("fill","steelblue") //X轴
g.append("g")
.call(d3.axisBottom(scale_x))
.attr("transform","translate(0,"+g_height+")") //Y轴
g.append("g")
.call(d3.axisLeft(scale_y)) //y轴文字
g.append("text")
.text("Price($)")
.attr("transform","rotate(-90)")
.attr("dy","1em")
.attr("text-anchor","end")

其中,使用红色部分,注释绿色部分是面积图

  使用绿色部分,注释红色部分是折线图

  data数组是数据来源。

参考教程:https://www.imooc.com/learn/103

使用d3.v5实现折线图与面积图的更多相关文章

  1. 06. Matplotlib 2 |折线图| 柱状图| 堆叠图| 面积图| 填图| 饼图| 直方图| 散点图| 极坐标| 图箱型图

    1.基本图表绘制 plt.plot() 图表类别:线形图.柱状图.密度图,以横纵坐标两个维度为主同时可延展出多种其他图表样式 plt.plot(kind='line', ax=None, figsiz ...

  2. Python交互图表可视化Bokeh:4. 折线图| 面积图

    折线图与面积图 ① 单线图.多线图② 面积图.堆叠面积图 1. 折线图--单线图 import numpy as np import pandas as pd import matplotlib.py ...

  3. Python使用Plotly绘图工具,绘制面积图

    今天我们来讲一下如何使用Python使用Plotly绘图工具,绘制面积图 绘制面积图与绘制散点图和折线图的画法类似,使用plotly graph_objs 中的Scatter函数,不同之处在于面积图对 ...

  4. Matplotlib学习---用matplotlib画面积图(area chart)

    这里利用Nathan Yau所著的<鲜活的数据:数据可视化指南>一书中的数据,学习画图. 数据地址:http://book.flowingdata.com/ch05/data/us-pop ...

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

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

  6. 第四篇:R语言数据可视化之折线图、堆积图、堆积面积图

    折线图简介 折线图通常用来对两个连续变量的依存关系进行可视化,其中横轴很多时候是时间轴. 但横轴也不一定是连续型变量,可以是有序的离散型变量. 绘制基本折线图 本例选用如下测试数据集: 绘制方法是首先 ...

  7. 使用d3.v5实现饼状图

    效果图: 饼状图: 目录结构: <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  8. pyecharts v1 版本 学习笔记 折线图,面积图

    折线图 折线图 基本demo import pyecharts.options as opts from pyecharts.charts import Line c = ( Line() .add_ ...

  9. [Swift通天遁地]三、手势与图表-(13)制作美观简介的滚动图表:折线图表、面积图表、柱形图表、散点图表

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

随机推荐

  1. Oracle-04:DDL语言数据表的操作

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- DDL操作: --创建学生表 create table student( sno number(4) not  ...

  2. ASP.NET Core & Docker 实战经验分享

    一.前言 最近一直在研究和实践ASP.NET Core.Docker.持续集成.在ASP.NET Core 和 Dcoker结合下遇到了一些坑,在此记录和分享,希望对大家有一些帮助. 二.中间镜像 我 ...

  3. JDK源码分析-String、StringBuilder、StringBuffer

    String类的申明 public final class String implements java.io.Serializable, Comparable<String>, Char ...

  4. 自研网关纳管Spring Cloud(一)

    摘要: 本文主要从网关的需求,以及Spring Cloud Zuul的线程模型和源码瓶颈分析结合,目前最近一段时间自研网关中间件纳管Spring Cloud的经验汇总整理. 一.自研网关纳管Sprin ...

  5. Node.js使用supervisor遭遇‘supervisor’不是内部或外部命令,如果解决?

    如果你有 PHP 开发经验,会习惯在修改 PHP 脚本后直接刷新浏览器以观察结果,而你 在开发 Node.js 实现的 HTTP 应用时会发现,无论你修改了代码的哪一部份,都必须终止 Node.js ...

  6. python函数注释, :与 ->

    python函数注释, :与 -> 如图:add1函数中的:意思是:函数中的参数说明    add2函数中:->意思是:函数的返回值为整型 这两种方法都是函数的注释方法,具体使用时要别人能 ...

  7. LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree

    258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...

  8. Go调用C代码,Cgo札记

    http://www.myexception.cn/program/679738.html Go调用C代码,Cgo笔记 参考: 官方文档: http://golang.org/cmd/cgo/ htt ...

  9. linux 显示当前所在文件位置 以及git 分支所在

    function git-branch-name {  git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3}function ...

  10. php自带验证邮箱 url ip函数

    以前用PHP验证邮箱.URL.IP是否合法都是通过自己写正则来实现,但是有时候脑子发昏,可能会写出一个不是完全正确的正则,导致验证出错,今天发现原来PHP本身自带了验证邮箱.URL.IP是否合法的函数 ...