D3中轴的异常值(混合数字和分类规格)

时间:2016-10-20 17:06:42

标签: d3.js axes outliers categorization

我正在尝试在D3中设置一些内容,其中我有一些轴用于某些数据点集合。但是,对于数据点的异常值,我想将这些异常值放在轴上的桶中。有没有办法可以为轴指定一个“异常值标记”作为放置这些数据点的分区?

Example: [1,3, 7, 12, 2048]
  *     *           *                 *                *
--1--2--3--4--5--6--7--8--9--10--11--12--13--14--15--O--

以下是我目前的代码。在我看来,尺度只能在数字上工作,所以我不确定如何将任意类别与数字尺度混合...

        let height = 1000;
        let width = 1000;
        let padding = 10;

        let svgContainer = d3.select("body").append("svg").attr("width", width).attr("height", height);

        let axisScale = d3.scale.linear().domain([0, 10]).range([ padding, width - padding]);
        let xAxis = d3.svg.axis().scale(axisScale);

        let xAxisYValue = height - padding * 3;
        let xAxisGroup = svgContainer.append("g").attr("transform", "translate(0," + xAxisYValue + ")").call(xAxis);

1 个答案:

答案 0 :(得分:1)

As I said in my comments, I'd do it something like this:

<!DOCTYPE html>
<meta charset="utf-8">
<style>
  body {
    font: 10px sans-serif;
  }
  .axis path,
  .axis line {
    fill: none;
    stroke: #000;
    shape-rendering: crispEdges;
  }
</style>

<body>
  <script src="//d3js.org/d3.v3.min.js"></script>
  <script>
    var data = [];
    for (i = 0; i <= 10; i++) {
      data.push({
        x: i,
        y: Math.random()
      });
    }
    data.push({
      x: "Other",
      y: Math.random()
    });
    data.push({
      x: "Other",
      y: Math.random()
    });
    data.push({
      x: "Other",
      y: Math.random()
    });

    var margin = {
        top: 20,
        right: 20,
        bottom: 30,
        left: 50
      },
      widthRight = 100,
      widthMain = 600 - margin.left - margin.right - widthRight,
      height = 500 - margin.top - margin.bottom;

    var x0 = d3.scale.linear()
      .range([0, widthMain])
      .domain([0, 10]);

    var x1 = d3.scale.ordinal()
      .rangePoints([widthMain, widthMain + widthRight], 2)
      .domain(["Other"]);

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

    var xAxis0 = d3.svg.axis()
      .scale(x0)
      .orient("bottom");

    var xAxis1 = d3.svg.axis()
      .scale(x1)
      .orient("bottom");

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

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

    svg.selectAll("circle")
      .data(data)
      .enter()
      .append("circle")
      .attr("cx", function(d) {
        if (isNaN(d.x)) {
          return x1(d.x);
        } else {
          return x0(d.x);
        }
      })
      .attr("cy",function(d) {
        return y(d.y);
      })
      .attr("r", 10)
      .style("fill", "steelblue")
      .style("stroke", "orange");

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

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

    svg.append("g")
      .attr("class", "y axis")
      .call(yAxis);
  </script>