如何使用d3.js遮蔽2行之间的区域

时间:2018-02-22 14:56:41

标签: javascript d3.js

[编辑]输出图表:https://imgur.com/a/D0wtP

我试图在图表中的2行之间区域遮蔽。这些图显示了由软件工程师提交的提交,并显示了他们的tt100系列原始和生产代码,我试图遮蔽两条线之间的delta区域。我发现的一些解决方案似乎与我的方法不符。我感谢任何帮助。我的代码如下:

\"

data.csv文件如下:

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.axis--x path {
  display: none;
}

.axis--y path {
display: none;
}

.line {
  fill: none;
  stroke-width: 1.5px;
}

.grid line {
  stroke: lightgrey;
  stroke-opacity: 0.7;
  shape-rendering: crispEdges;
}

.grid path {
  stroke-width: 0;
}


</style>
<svg width="1080" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>

var svg = d3.select("svg"),
    margin = {top: 20, right: 80, bottom: 30, left: 50},
    width = svg.attr("width") - margin.left - margin.right - 50,
    height = svg.attr("height") - margin.top - margin.bottom;

    svg.append("rect")
    .attr("width", "100%")
    .attr("height", "100%")
    .attr("fill", "#f5f5f1")

var parseTime = d3.timeParse("%Y%m");

var x = d3.scaleTime().range([0, width]),
    y = d3.scaleLinear().range([height, 0]),
    z = d3.scaleOrdinal(d3.schemeCategory10);

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

var g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");


// gridlines in y axis function
function make_y_gridlines() {       
    return d3.axisLeft(y)
        .ticks(5)
}

d3.csv("data.csv", type, function(error, data) {
  if (error) throw error;

  var employees1 = data.columns.slice(1,3).map(function(id) {
    return {
      id: id,
      values: data.map(function(d) {
        return {date: d.date, time: d[id]};
      })
    };
  });

    var employees2 = data.columns.slice(3).map(function(id) {
    return {
      id: id,
      values: data.map(function(d) {
        return {date: d.date, time: d[id]};
      })
    };
  });

      var employees = data.columns.slice(1).map(function(id) {
    return {
      id: id,
      values: data.map(function(d) {
        return {date: d.date, time: d[id]};
      })
    };
  });




  x.domain(d3.extent(data, function(d) { return d.date; }));

  y.domain([
    0,
    d3.max(employees, function(c) { return d3.max(c.values, function(d) { return d.time; }); })
  ]);

  z.domain(employees.map(function(c) { return c.id; }));




  // add the Y gridlines
  g.append("g")         
      .attr("class", "grid")
      .call(make_y_gridlines()
          .tickSize(-width)
          .tickFormat("")
      )


  g.append("g")
      .attr("class", "axis axis--x")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x));

  g.append("g")
      .attr("class", "axis axis--y")
      .call(d3.axisLeft(y))
    .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", -45)
      .attr("dy", "0.71em")
      .attr("fill", "#000")
      .text("HOURS TO PRODUCE 100 LINES OF CODE (PRODUCTIVE VS RAW)");

  g.append("text")
        .attr("x", (width / 4))             
        .attr("y", 0 - (margin.top / 8))
        .attr("text-anchor", "middle")  
        .style("font-size", "24px")   
        .text("Churn Over Time");

  var employee1 = g.selectAll(".employee1")
    .data(employees1)
    .enter().append("g")
      .attr("class", "employee1");


    var employee2 = g.selectAll(".employee2")
    .data(employees2)
    .enter().append("g")
      .attr("class", "employee2");


  employee1.append("path")
      .attr("class", "line")
      .attr("d", function(d) { return line(d.values); })
      .style("stroke", "A057AE");



  employee1.append("text")
      .datum(function(d) { return {id: d.id, value: d.values[d.values.length - 1]}; })
      .attr("transform", function(d) { return "translate(" + x(d.value.date) + "," + y(d.value.time) + ")"; })
      .attr("x", 3)
      .attr("dy", "0.35em")
      .style("font", "10px sans-serif")
      .text(function(d) { return d.id; });


  employee2.append("path")
      .attr("class", "line")
      .attr("d", function(d) { return line(d.values); })
      .style("stroke", "91BF50");


  employee2.append("text")
      .datum(function(d) { return {id: d.id, value: d.values[d.values.length - 1]}; })
      .attr("transform", function(d) { return "translate(" + x(d.value.date) + "," + y(d.value.time) + ")"; })
      .attr("x", 3)
      .attr("dy", "0.35em")
      .style("font", "10px sans-serif")
      .text(function(d) { return d.id; });

});

function type(d, _, columns) {
  d.date = parseTime(d.date);
  for (var i = 1, n = columns.length, c; i < n; ++i) d[c = columns[i]] = +d[c];
  return d;
}

</script>

0 个答案:

没有答案