D3.js 中的简单散点图示例
A simple scatterplot example in D3.js?
我正在寻找如何在 D3 中绘制散点图的示例.js。
我无法通过查看官方 D3.js 示例找到一个简单的示例(尽管它们令人印象深刻)。我只想知道如何:
- 绘制和标记 X 轴和 Y 轴
- 在图形上绘制散点。
我确实在这个 D3 可重用库中找到了这个例子,但它比我需要的要复杂得多,外部文件很难提取要点。谁能指出我一个简单的散点图示例来开始?
非常感谢。
这应该让你开始。 您可以在 http://bl.ocks.org/2595950 看到它的实际效果。
// data that you want to plot, I've used separate arrays for x and y values
var xdata = [5, 10, 15, 20],
ydata = [3, 17, 4, 6];
// size and margins for the chart
var margin = {top: 20, right: 15, bottom: 60, left: 60}
, width = 960 - margin.left - margin.right
, height = 500 - margin.top - margin.bottom;
// x and y scales, I've used linear here but there are other options
// the scales translate data values to pixel values for you
var x = d3.scale.linear()
.domain([0, d3.max(xdata)]) // the range of the values to plot
.range([ 0, width ]); // the pixel range of the x-axis
var y = d3.scale.linear()
.domain([0, d3.max(ydata)])
.range([ height, 0 ]);
// the chart object, includes all margins
var chart = d3.select('body')
.append('svg:svg')
.attr('width', width + margin.right + margin.left)
.attr('height', height + margin.top + margin.bottom)
.attr('class', 'chart')
// the main object where the chart and axis will be drawn
var main = chart.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
.attr('width', width)
.attr('height', height)
.attr('class', 'main')
// draw the x axis
var xAxis = d3.svg.axis()
.scale(x)
.orient('bottom');
main.append('g')
.attr('transform', 'translate(0,' + height + ')')
.attr('class', 'main axis date')
.call(xAxis);
// draw the y axis
var yAxis = d3.svg.axis()
.scale(y)
.orient('left');
main.append('g')
.attr('transform', 'translate(0,0)')
.attr('class', 'main axis date')
.call(yAxis);
// draw the graph object
var g = main.append("svg:g");
g.selectAll("scatter-dots")
.data(ydata) // using the values in the ydata array
.enter().append("svg:circle") // create a new circle for each value
.attr("cy", function (d) { return y(d); } ) // translate y value to a pixel
.attr("cx", function (d,i) { return x(xdata[i]); } ) // translate x value
.attr("r", 10) // radius of circle
.style("opacity", 0.6); // opacity of circle
像这样使用:
<!DOCTYPE html>
<html>
<head>
<title>The d3 test</title>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.v2.js" charset="utf-8"></script>
</head>
<body>
<div class='content'>
<!-- /the chart goes here -->
</div>
<script type="text/javascript" src="scatterchart.js"></script>
</body>
</html
NVD3.js 有很好的例子。您还需要包含它们的库或查看它们的实现。看看这个散点图的例子:http://nvd3.org/livecode/#codemirrorNav
看看
这个带有 C3 的散点图示例.js(基于 D3): http://c3js.org/samples/chart_scatter.html
你可以像这样改变半径大小: http://grokbase.com/t/gg/c3js/14a7jfj28c/bubble-chart-with-changing-radius-size-in-c3-js
相关文章:
- 将工具提示添加到d3散点图中
- 具有多个数据集的分组散点图D3
- d3.js散点图中的刻度标签在Firefox 13.0.1中被截断
- 在D3散点图圆上添加标签
- 在NVD3散点图上绘制标签
- 高图表散点图:点击事件时的警报坐标
- 如何从javascript散点图中的另一个变量为x和y添加新值
- d3.js可以使用来自同一来源的数据在同一张图上绘制两个散点图吗
- 谷歌散点图-{role:'style'}{role:#39;工具提示'}不能以JSON形式工作
- d3js散点图自动更新不起作用
- 使用d3制作交互式散点图
- 如何使用d3在散点图上逐个绘制/添加节点
- 对散点图使用固定的颜色集
- 从 javascript 的高图表中的折线图/散点图中的点获取工具提示内容
- 在散点图 D3.js 中动态命名轴
- 高图:将点动态永久附加到散点图
- 散点图实现的固定轴
- c3.js 中的增量散点图
- D3.js 中的简单散点图示例
- D3简单散点图上的鱼眼失真