d3 散布圖 (2)

d3 散布圖 (2)

by cooper081

CSS

body {
    font: 10px sans-serif;
  }
  
  .axis path,
  .axis line {
    fill: none;
    stroke: #000;
    shape-rendering: crispEdges;
  }
  
  .line {
    fill: none;
    stroke: steelblue;
    stroke-width: 1.5px;
  }
  
  .overlay {
    fill: none;
    pointer-events: all;
  }
  
  .focus circle {
    fill: none;
    stroke: steelblue;
  }

JavaScript

var margin = {
        top: 20,
        right: 20,
        bottom: 30,
        left: 50
      },
      width = 960 - margin.left - margin.right,
      height = 500 - margin.top - margin.bottom;


    var x = d3.scale.ordinal()
      .rangePoints([0, width]);

    var y = d3.scale.linear()
      .range([height, 0]);

    var xAxis = d3.svg.axis()
      .scale(x)
      .orient("bottom");

    var yAxis = d3.svg.axis()
      .scale(y)
      .orient("left");

    var line = d3.svg.line()
      .x(function(d) {
        return x(d.x);
      })
      .y(function(d) {
        return y(d.y);
      });

    var svg = d3.select("body").append("svg")
      .attr("width", width + margin.left + margin.right)
      .attr("height", height + margin.top + margin.bottom)
      .append("g")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

    var data = [{
      x: 'A',
      y: Math.random() * 10
    }, {
      x: 'B',
      y: Math.random() * 10
    }, {
      x: 'C',
      y: Math.random() * 10
    }, {
      x: 'D',
      y: Math.random() * 10
    }, {
      x: 'E',
      y: Math.random() * 10
    }, {
      x: 'F',
      y: Math.random() * 10
    }, {
      x: 'G',
      y: Math.random() * 10
    }, {
      x: 'H',
      y: Math.random() * 10
    }, {
      x: 'I',
      y: Math.random() * 10
    }, {
      x: 'J',
      y: Math.random() * 10
    }];

    var ticks = data.map(function(d) {
      return d.x
    });
    x.domain(ticks);
    
    y.domain(d3.extent(data, function(d) {
      return d.y;
    }));

    svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);

    svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
      .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em");

    svg.append("path")
      .datum(data)
      .attr("class", "line")
      .attr("d", line);

    var focus = svg.append("g")
     ...