如何用d3.js绘制散点图中的直线

use*_*980 3 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
Run Code Online (Sandbox Code Playgroud)

cke*_*sch 6

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

您的代码看起来如下所示:

chart.append('line')
    .attr('x1',x(10))
    .attr('x2',x(20))
    .attr('y1',y(5))
    .attr('y2',y(10))
Run Code Online (Sandbox Code Playgroud)

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

您可能感兴趣的一件事是SVG路径元素.这对于线而言比一次绘制一个直线段更常见.文档在这里.

另一方面,如果将所有数据创建为一个对象,您可能会发现在d3中使用数据更容易.例如,如果您的数据采用以下形式:

data = [{x: 5, y:3}, {x: 10, y:17}, {x: 15, y:4}, {x: 20, y:6}]
Run Code Online (Sandbox Code Playgroud)

你可以使用:

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
Run Code Online (Sandbox Code Playgroud)

如果您的数据变得更复杂,这将消除潜在的混乱索引.