A simple scatterplot example in D3.js?

I’m looking for an example of how to draw a scatterplot in D3.js.

I haven’t been able to find a simple example by looking through the official D3.js examples (impressive though they are). I just want to know how to:

  • draw and label the x- and y-axes
  • draw scatter points on the graph.

I did find this example in this D3 reusable library, but it is much more complex than I need, with external files that make it hard to pull out the essential points. Could anyone point me at a simple scatterplot example to get started?

Thanks very much.

This should get you started. You can see it in action at 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')
.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()

.attr('transform', 'translate(0,' + height + ')')
.attr('class', 'main axis date')

// draw the y axis
var yAxis = d3.svg.axis()

.attr('transform', 'translate(0,0)')
.attr('class', 'main axis date')

// draw the graph object
var g = main.append("svg:g"); 

  .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

Used like this:

<!DOCTYPE html>
    <title>The d3 test</title>
    <script type="text/javascript" src="http://mbostock.github.com/d3/d3.v2.js" charset="utf-8"></script>
    <div class="content">
      <!-- /the chart goes here -->
    <script type="text/javascript" src="scatterchart.js"></script>

Take a look at this example of scatterplot with C3.js (D3-based): http://c3js.org/samples/chart_scatter.html

You can chang radius size like this : http://grokbase.com/t/gg/c3js/14a7jfj28c/bubble-chart-with-changing-radius-size-in-c3-js

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts