d3.js - 你如何用 d3.js 在散点图中绘制线性线

标签 d3.js

我正在寻找使用 d3.js 库实现 ggplot2 类型的图形以实现交互目的。我喜欢 ggplot2,但用户对交互式图形感兴趣。我一直在探索 d3.js 库,似乎有很多不同的图形功能,但我真的没有看到任何统计图形,如线性线、预测等。给定散点图,是否也可以向图形。

我有这个绘制散点图的示例脚本。我将如何在 d3.js 中向该图中添加线性线?

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

最佳答案

要在绘图中添加一条线,您需要做的就是将一些线 SVG 附加到您的主 SVG ( chart ) 或包含您的 SVG 元素的组 ( main )。

您的代码如下所示:

chart.append('line')
    .attr('x1',x(10))
    .attr('x2',x(20))
    .attr('y1',y(5))
    .attr('y2',y(10))

这将从 (10,5) 到 (20,10) 画一条线。您可以类似地为您的行创建一个数据集并附加一大堆。

您可能感兴趣的一件事是 SVG 路径元素。这对于线条来说比一次绘制一个直线段更常见。文档是 here.

另一方面,如果您将所有数据创建为一个对象,您可能会发现在 d3 中处理数据会更容易。例如,如果您的数据采用以下形式:
data = [{x: 5, y:3}, {x: 10, y:17}, {x: 15, y:4}, {x: 20, y:6}]

你可以使用:
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.y); } ) //set y
      .attr("cx", function (d,i) { return x(d.x); } ) //set x

如果您的数据变得更复杂,这将消除潜在的困惑索引。

关于d3.js - 你如何用 d3.js 在散点图中绘制线性线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17407100/

相关文章:

javascript - d3.js 中未发生数据级联

svg - 如何使用d3.js在圆弧内沿textPath对齐/结束文本?

javascript - 将 JSON 树数据转换为节点和链接

d3.js - 如何更新 D3 中的特定节点

javascript - 根据多折线图显示中的数据缩放 y 轴

javascript - 如何正确地将颜色渐变应用于圆弧?

r - R中的Sankey图

javascript - 将 d3 个圆圈从中心圆圈移开 - 强制布局

javascript - d3.js html表格中的嵌套数据更新线图

javascript - 通过 d3.js 中的对象属性过滤嵌套的对象数组