Scatter plots, sometimes also known as bubble charts, are another common type of visualization. They’re extremely versatile thanks to their ability to display multiple dimensions of data simultaneously using x and y position, opacity, color, and even shape. This lesson introduces the SVG circle element as part of building a robust scatter plot.

var margin = {
top: 10,
right: 20,
bottom: 65,
left: 40
};
var width = 400 - margin.left - margin.right;
var height = 600 - 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/data2.json', function (err, data) {
/**
* Add Y axis
*/
var yScale = d3.scaleLinear()
.domain(d3.extent(data, (d) => d.expectancy))
.range([height, 0])
.nice();
var yAxis = d3.axisLeft(yScale);
svg.call(yAxis); /**
* Add X axis
*/
var xScale = d3.scaleLinear()
.domain(d3.extent(data, d => d.cost))
.range([0, width])
.nice(); var xAxis = d3.axisBottom(xScale);
svg
.append('g')
.attr('transform', `translate(0, ${height})`)
.call(xAxis); // For circle, we need scaleSqrt
var rScale = d3.scaleSqrt()
.domain([0, d3.max(data, d => d.population)])
.range([0, 40]); // Create some container to contain the circles
var circles = svg.selectAll('.ball')
.data(data)
.enter()
.append('g')
.attr('class', 'ball')
.attr('transform', d => {
return `translate(${xScale(d.cost)}, ${yScale(d.expectancy)})`
}); // Add circle to each containers
circles
.append('circle')
.attr('cx', 0)
.attr('cy', 0)
.attr('r', d => rScale(d.population))
.style('opacity', 0.5)
.style('fill', 'steelblue'); // Add text
circles
.append('text')
.style('text-anchor', 'middle')
.style('fill', 'black')
.attr('y', 4)
.text(d => d.code)
}); 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 a Scatter Plot 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 an Area Chart with D3 v4

    Similar to line charts, area charts are great for displaying temporal data. Whether you’re displayin ...

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

  4. [D3] 9. Scatter Plot

    Up until now we've just looked at bar charts. A handy chart, no doubt, but D3 offers a variety of ch ...

  5. [D3] Animate Chart Axis Transitions in D3 v4

    When the data being rendered by a chart changes, sometimes it necessitates a change to the scales an ...

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

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

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

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

  9. Matplotlib学习---用matplotlib画散点图,气泡图(scatter plot, bubble chart)

    Matplotlib里有两种画散点图的方法,一种是用ax.plot画,一种是用ax.scatter画. 一. 用ax.plot画 ax.plot(x,y,marker="o",co ...

随机推荐

  1. hdu 5312 Sequence(数学推导——三角形数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5312 Sequence Time Limit: 2000/2000 MS (Java/Others)  ...

  2. js深拷贝的实现方式

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

  3. SASS常用方法

    cnpm install --save-dev sass-loader //sass-loader依赖于node-sass cnpm install --save-dev node-sass //实现 ...

  4. 什么是事件委托?jquery和js怎么去实现?

    事件委托又叫事件代理,事件委托就是利用事件冒泡,只指定一个事件处理程序,就可以管理某一类型的所有事件. js: window.onload = function(){ var oul = docume ...

  5. iptables---linux防火墙

    iptables命令是Linux上常用的防火墙软件,是netfilter项目的一部分.可以直接配置,也可以通过许多前端和图形界面配置. 语法 iptables(选项)(参数) 选项 -t<表&g ...

  6. exit---退出目前的shell

    exit命令   exit命令同于退出shell,并返回给定值.在shell脚本中可以终止当前脚本执行.执行exit可使shell以指定的状态值退出.若不设置状态值参数,则shell以预设值退出.状态 ...

  7. Input/output subsystem having an integrated advanced programmable interrupt controller for use in a personal computer

    A computer system is described having one or more host processors, a host chipset and an input/outpu ...

  8. 1.4 Ecosystem官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ 1.4 Ecosystem 生态系统 There are a plethora of ...

  9. Monkey测试执行指导

    1.Monkey常规测试

  10. 全新linux中通过编译方式安装nginx

    先去官网下载linux.tar.gz包 http://nginx.org/en/download.html   传到linxu中 解压tar包 在软件包nginx-1.15.9目录下对NGINX进行配 ...