D3区域图表不能端到端渲染

时间:2017-08-10 18:18:55

标签: javascript d3.js svg

my area chart image我已将该示例用于区域图表:https://bl.ocks.org/mbostock/3883195并更改了数据。我没有在x轴上使用时间值,而是将值更改为字符串,并将域函数x.domain( data.map( function(d) { return d.xvalue; } ) );与比例函数var x = d3.scaleBand().rangeRound([0, width])一起使用。这有两个问题:1。x轴上的值与面积值(高点和低点)不同步。这个区域没有覆盖svg的整个宽度。我对scaleBand的理解是它采用字符串值并将它们放在x轴上相等的距离。请让我知道我做错了什么。提前谢谢..



var margin = {
  top: 40,
  right: 5,
  bottom: 25,
  left: 35
};
var width = 980 - margin.left - margin.right;
var height = 450 - margin.top - margin.bottom;



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

// gridlines in x axis function
function make_x_gridlines() {
  return d3.axisBottom(x)
    .ticks(0)
}

// gridlines in y axis function
function make_y_gridlines() {
  return d3.axisLeft(y)
    .ticks(5)
}
var portavgValue;
console.log(portavgValue);
var line = d3.line()
  .x(function(d) {
    return x(d.xvalue);
  })
  .y(function(d) {
    return y(d.portalAvg);
  });

var x = d3.scaleBand()
  .rangeRound([0, width]);

var y = d3.scaleLinear()
  .rangeRound([height, 0]);

var failRateArea = d3.area()
  .x(function(d) {
    return x(d.xvalue);
  })
  .y1(function(d) {
    return y(d.failrate);
  })
  .curve(d3.curveCatmullRom);

var successrateArea = d3.area()
  .x(function(d) {
    return x(d.xvalue);
  })
  .y1(function(d) {
    return y(d.successRate);
  })
  .curve(d3.curveCatmullRom);
//defining tooltip
var div = d3.select("body").append("div")
  .attr("class", "acadMetrictooltip")
  .style("opacity", 0);

d3.csv("data.csv", function(d) {

  d.failrate = +d.failrate;
  return d;
}, function(error, data) {
  if (error) throw error;
  x.domain(data.map(function(d) {
    return d.xvalue;
  }));
  y.domain([0, 100]).nice();

  failRateArea.y0(y(0));
  successrateArea.y0(y(0));

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

  // add the X gridlines
  g.append("g")
    .attr("class", "Xgrid")
    .attr("transform", "translate(0," + height + ")")
    .call(make_x_gridlines()
      .tickSize(-height)
      .tickFormat("")
    )




  g.append("g").attr("class", "sucRateBox")
    .append("path")
    .datum(data)
    .attr("fill", "#bdf4e1")
    .style("opacity", "0.9")
    .attr("d", successrateArea);

  g.append("g").attr("class", "failRateBox")
    .append("path")
    .datum(data)
    .attr("fill", "#d0aa9d")
    .style("opacity", "0.9")
    .attr("d", failRateArea);



  g.append("g").attr("class", "axisX")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.axisBottom(x).tickSizeOuter(0))
    .selectAll("text")
    .style("fill", "#6d6d6d")
    .style("font-size", "11px")
    .attr("transform", "rotate(0)");

  g.append("g").attr("class", "axisY")
    .call(d3.axisLeft(y).tickSizeOuter(0).tickFormat(function(d) {
      return d + "%"
    }))





});

<script src="https://d3js.org/d3.v4.min.js"></script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您不应在此处使用频段音阶:频段音阶具有相关带宽。

相反,使用点刻度:

var x = d3.scalePoint()
   .rangeRound([0, width]);