簡體   English   中英

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM